diff --git a/epicardium/ble/ble.c b/epicardium/ble/ble.c index 52c4f1a9b12415be9a15a7d2498e6ac12c779e4f..a667f9d1f739d8f7b15210971adb0caec78e1bd6 100644 --- a/epicardium/ble/ble.c +++ b/epicardium/ble/ble.c @@ -8,7 +8,6 @@ #include "ble_api.h" #include "hci_vs.h" #include "att_api.h" -#include "trng.h" #include "FreeRTOS.h" #include "timers.h" @@ -101,7 +100,7 @@ static void setAddress(void) bdAddr[0] = 0xCA; bdAddr[1] = 0x4D; bdAddr[2] = 0x10; - TRNG_Read(MXC_TRNG, bdAddr + 3, 3); + epic_trng_read(bdAddr + 3, 3); sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n", bdAddr[0], diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h index 88712b1763561b8b86ae539cab4071705e07fc1a..7ecf9b5711adbb8ca8f5e8195fb6b1d8ff6a636b 100644 --- a/epicardium/epicardium.h +++ b/epicardium/epicardium.h @@ -98,6 +98,9 @@ typedef _Bool bool; #define API_GPIO_GET_PIN_MODE 0xA1 #define API_GPIO_WRITE_PIN 0xA2 #define API_GPIO_READ_PIN 0xA3 + +#define API_TRNG_READ 0xB0 + /* clang-format on */ typedef uint32_t api_int_id_t; @@ -1209,4 +1212,21 @@ API(API_RTC_SCHEDULE_ALARM, int epic_rtc_schedule_alarm(uint32_t timestamp)); */ API_ISR(EPIC_INT_RTC_ALARM, epic_isr_rtc_alarm); +/** + * TRNG + * ==== + */ + +/** + * Read random bytes from the TRNG. + * + * :param uint8_t * dest: Destination buffer + * :param size: Number of bytes to read. + * :return: `0` on success or a negative value if an error occured. Possible + * errors: + * + * - ``-EFAULT``: Invalid destination address. + */ +API(API_TRNG_READ, int epic_trng_read(uint8_t *dest, size_t size)); + #endif /* _EPICARDIUM_H */ diff --git a/epicardium/modules/hardware.c b/epicardium/modules/hardware.c index 5453801ebd92c86184b712e7049281e0a5ff322f..3905ceb81811dc38c581d6ad99320c3dbadb16a5 100644 --- a/epicardium/modules/hardware.c +++ b/epicardium/modules/hardware.c @@ -18,6 +18,7 @@ #include "gpio.h" #include "i2c.h" #include "spi.h" +#include "trng.h" /* * Early init is called at the very beginning and is meant for modules which diff --git a/epicardium/modules/meson.build b/epicardium/modules/meson.build index 61e0cc634145022a7d1b98d49b75eb4efa22e340..31e7f5c078a74f39425d70048ac0f57be28c67f5 100644 --- a/epicardium/modules/meson.build +++ b/epicardium/modules/meson.build @@ -13,5 +13,6 @@ module_sources = files( 'rtc.c', 'serial.c', 'stream.c', + 'trng.c', 'vibra.c', ) diff --git a/epicardium/modules/trng.c b/epicardium/modules/trng.c new file mode 100644 index 0000000000000000000000000000000000000000..7faa2909c6f90843c647ccaacd27db24273d1d5f --- /dev/null +++ b/epicardium/modules/trng.c @@ -0,0 +1,12 @@ +#include "epicardium.h" +#include "trng.h" + +int epic_trng_read(uint8_t *dest, size_t size) +{ + if (dest == NULL) + return -EFAULT; + + TRNG_Read(MXC_TRNG, dest, size); + + return 0; +}