From 4c5d77197b2b5a34be1f8b57ce3a89fc77452f96 Mon Sep 17 00:00:00 2001 From: Florian Anderiasch <florian.anderiasch@kinexon.com> Date: Tue, 23 Jul 2019 21:57:46 +0200 Subject: [PATCH] WIP: #36 breaks everything :P --- epicardium/epicardium.h | 11 ++++++ epicardium/main.c | 11 ++++++ epicardium/modules/meson.build | 1 + epicardium/modules/rng.c | 47 ++++++++++++++++++++++++ pycardium/meson.build | 1 + pycardium/modules/py/meson.build | 1 + pycardium/modules/py/rng.py | 30 ++++++++++++++++ pycardium/modules/qstrdefs.h | 4 +++ pycardium/modules/sys_rng.c | 61 ++++++++++++++++++++++++++++++++ pycardium/mpconfigport.h | 1 + 10 files changed, 168 insertions(+) create mode 100644 epicardium/modules/rng.c create mode 100644 pycardium/modules/py/rng.py create mode 100644 pycardium/modules/sys_rng.c diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h index 470a4185..3fa9c2dc 100644 --- a/epicardium/epicardium.h +++ b/epicardium/epicardium.h @@ -48,6 +48,11 @@ typedef unsigned int size_t; #define API_DISP_RECT 0x16 #define API_DISP_CIRC 0x17 #define API_DISP_PIXEL 0x18 + +#define API_RNG_OPEN 0x19 +#define API_RNG_CLOSE 0x20 +#define API_RNG_FOOBAR 0x21 +#define API_RNG_RANDOMX 0x22 /* clang-format on */ typedef uint32_t api_int_id_t; @@ -439,4 +444,10 @@ API(API_LIGHT_SENSOR_GET, int epic_light_sensor_get(uint16_t* value)); */ API(API_LIGHT_SENSOR_STOP, int epic_light_sensor_stop()); + +API(API_RNG_OPEN, int epic_rng_open()); +API(API_RNG_CLOSE, int epic_rng_close()); +API(API_RNG_FOOBAR, int epic_rng_foobar()); +API(API_RNG_RANDOMX, int epic_rng_randomx()); + #endif /* _EPICARDIUM_H */ diff --git a/epicardium/main.c b/epicardium/main.c index 75bb9c79..5d9d2d97 100644 --- a/epicardium/main.c +++ b/epicardium/main.c @@ -54,6 +54,17 @@ int main(void) LOG_ERR("startup", "USB-Serial unavailable"); } + + int asdf = epic_rng_open(); + int x = epic_rng_foobar(); + printf("rng1 %d", x); + LOG_INFO("startup", "rng1 %d ", x); + int y = epic_rng_randomx(); + printf("rng2 %d", y); + LOG_INFO("startup", "rng2 %d", y); + asdf = epic_rng_close(); + + fatfs_init(); api_interrupt_init(); stream_init(); diff --git a/epicardium/modules/meson.build b/epicardium/modules/meson.build index 68c1b9d9..04cfa3db 100644 --- a/epicardium/modules/meson.build +++ b/epicardium/modules/meson.build @@ -4,6 +4,7 @@ module_sources = files( 'leds.c', 'log.c', 'pmic.c', + 'rng.c', 'serial.c', 'stream.c', 'vibra.c', diff --git a/epicardium/modules/rng.c b/epicardium/modules/rng.c new file mode 100644 index 00000000..e3a21912 --- /dev/null +++ b/epicardium/modules/rng.c @@ -0,0 +1,47 @@ +#include "epicardium.h" +#include "trng.h" + +#define TRNG_32BIT_RND_NO 4 +#define TRNG_LEN 1 // 16 + +#define MXC_AES_DATA_LEN (128 / 8) +#define MXC_AES_KEY_128_LEN (128 / 8) + +/***** Globals *****/ +//unsigned int rnd_no[TRNG_32BIT_RND_NO] = {0}; +//uint8_t var_rnd_no[TRNG_LEN] = {0}; + +//char aes_result[512]; +//char temp[] = {0x00, 0x00, 0x00}; + + +int epic_rng_open() +{ + int rv = TRNG_Init(NULL); + return rv; +} + +int epic_rng_close() +{ + int rv = TRNG_Shutdown(); + return rv; +} + +int epic_rng_foobar() +{ + return 23; +} + +int epic_rng_randomx() +{ + unsigned int rnd_no[TRNG_32BIT_RND_NO] = {0}; + //uint8_t var_rnd_no[TRNG_LEN] = {0}; + + int i; + for(i = 0; i < TRNG_32BIT_RND_NO; ++i){ + rnd_no[i] = TRNG_Read32BIT(MXC_TRNG); + //printf("%0x\n", rnd_no[i]); + } + + return rnd_no[0]; +} \ No newline at end of file diff --git a/pycardium/meson.build b/pycardium/meson.build index 895be888..43e71f69 100644 --- a/pycardium/meson.build +++ b/pycardium/meson.build @@ -4,6 +4,7 @@ modsrc = files( 'modules/interrupt.c', 'modules/leds.c', 'modules/sys_display.c', + 'modules/sys_rng.c', 'modules/utime.c', 'modules/vibra.c', 'modules/light_sensor.c' diff --git a/pycardium/modules/py/meson.build b/pycardium/modules/py/meson.build index eab92f95..ab3dd833 100644 --- a/pycardium/modules/py/meson.build +++ b/pycardium/modules/py/meson.build @@ -2,6 +2,7 @@ python_modules = files( 'color.py', 'htmlcolor.py', 'display.py', + 'rng.py', ) frozen_modules = mpy_cross.process(python_modules) diff --git a/pycardium/modules/py/rng.py b/pycardium/modules/py/rng.py new file mode 100644 index 00000000..69aebb9f --- /dev/null +++ b/pycardium/modules/py/rng.py @@ -0,0 +1,30 @@ +import sys_rng + +class Rng: + def __init__(self): + sys_rng.open() + + def __enter__(self): + return self + + def __exit__(self): + self.close() + + @classmethod + def open(cls): + return cls() + + @staticmethod + def close(): + sys_rng.close() + + def foobar(self): + rv = sys_rng.foobar() + print("py_foobar", rv) + return rv + + def randomx(self): + rv = sys_rng.randomx() + print("py_randomx", rv) + return rv + diff --git a/pycardium/modules/qstrdefs.h b/pycardium/modules/qstrdefs.h index 3dda54d6..79c19f3a 100644 --- a/pycardium/modules/qstrdefs.h +++ b/pycardium/modules/qstrdefs.h @@ -41,6 +41,10 @@ Q(rect) Q(circ) Q(clear) +Q(sys_rng) +Q(foobar) +Q(randomx) + /* ambient */ Q(light_sensor) Q(start) diff --git a/pycardium/modules/sys_rng.c b/pycardium/modules/sys_rng.c new file mode 100644 index 00000000..d7e5007d --- /dev/null +++ b/pycardium/modules/sys_rng.c @@ -0,0 +1,61 @@ +#include "py/obj.h" +#include "py/objstr.h" +#include "py/objint.h" +#include "py/runtime.h" + +#include "epicardium.h" +#include <stdio.h> + +static mp_obj_t mp_rng_open() +{ + return mp_const_none; +} + +static mp_obj_t mp_rng_close() +{ + return mp_const_none; +} + +static mp_obj_t mp_rng_foobar() +{ + int rv = epic_rng_foobar(); + printf("sys_rng.foobar %d", rv); + return mp_obj_new_int_from_uint(rv); +} + +static mp_obj_t mp_rng_randomx() +{ + int rv = epic_rng_randomx(); + printf("sys_rng.randomx %d", rv); + return mp_obj_new_int_from_uint(rv); +} + +/* +STATIC_MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN( +// rng_foobar_obj, 3, 3, mp_rng_foobar +); +*/ + +STATIC MP_DEFINE_CONST_FUN_OBJ_0(rng_open_obj, mp_rng_open); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(rng_close_obj, mp_rng_close); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(rng_foobar_obj, mp_rng_foobar); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(rng_randomx_obj, mp_rng_randomx); + +static const mp_rom_map_elem_t rng_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_sys_rng) }, + { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&rng_open_obj) }, + { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&rng_close_obj) }, + { MP_ROM_QSTR(MP_QSTR_foobar), MP_ROM_PTR(&rng_foobar_obj) }, + { MP_ROM_QSTR(MP_QSTR_randomx), MP_ROM_PTR(&rng_randomx_obj) }, +}; +static MP_DEFINE_CONST_DICT( + rng_module_globals, rng_module_globals_table +); + +const mp_obj_module_t rng_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t *)&rng_module_globals, +}; + +/* clang-format off */ +MP_REGISTER_MODULE(MP_QSTR_sys_rng, rng_module, MODULE_RNG_ENABLED); diff --git a/pycardium/mpconfigport.h b/pycardium/mpconfigport.h index 6bfb2c67..4d7276ca 100644 --- a/pycardium/mpconfigport.h +++ b/pycardium/mpconfigport.h @@ -43,6 +43,7 @@ #define MODULE_INTERRUPT_ENABLED (1) #define MODULE_DISPLAY_ENABLED (1) #define MODULE_LIGHT_SENSOR_ENABLED (1) +#define MODULE_RNG_ENABLED (1) /* * This port is intended to be 32-bit, but unfortunately, int32_t for -- GitLab