diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h index 470a41853895ef3fbaec66ebea29cb2a8e68490c..3fa9c2dcc9b0e3df116b00328ef3d493273462c5 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 75bb9c79537a82de067d2618d1f3071348d2b032..5d9d2d97f1fd94c97f414549c4a50ddb4a245593 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 68c1b9d909170d69157fe844fe2100f119b00423..04cfa3db095f88b1e9d1020910aed12b65421cae 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 0000000000000000000000000000000000000000..e3a219120064010f251588ba932e152043605749 --- /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 895be888ee81bdd886b990d12f7c6d63321a6c2e..43e71f693406587190b017d22683f1155e155493 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 eab92f959051c62bfc4ab065d3385e721d6711f2..ab3dd833ff291d064c67d79b820343870d584992 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 0000000000000000000000000000000000000000..69aebb9f540de54a5b1213724c596d30b68a8fd3 --- /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 3dda54d667557f66d39978788fbde813f8e39390..79c19f3a597366ac7821b716238714d4727edd53 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 0000000000000000000000000000000000000000..d7e5007dd36d47a8235e6d808b7b5e277613db00 --- /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 6bfb2c6717fcddb0bfcd585fb13c401b5df86bb9..4d7276cad223980c69bc51707d43e8b634ea1fab 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