Skip to content

Commit 41aa214

Browse files
authored
Merge pull request #2300 from hierophect/stm32-urandom
STM32: implement OS urandom
2 parents 2da1b68 + 4a25c23 commit 41aa214

File tree

5 files changed

+38
-6
lines changed

5 files changed

+38
-6
lines changed

ports/stm32f4/boards/feather_stm32f405_express/stm32f4xx_hal_conf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
#define HAL_I2S_MODULE_ENABLED
5656
/* #define HAL_IWDG_MODULE_ENABLED */
5757
/* #define HAL_LTDC_MODULE_ENABLED */
58-
/* #define HAL_RNG_MODULE_ENABLED */
58+
#define HAL_RNG_MODULE_ENABLED
5959
/* #define HAL_RTC_MODULE_ENABLED */
6060
/* #define HAL_SAI_MODULE_ENABLED */
6161
/* #define HAL_SD_MODULE_ENABLED */

ports/stm32f4/boards/pyboard_v11/stm32f4xx_hal_conf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
#define HAL_I2S_MODULE_ENABLED
5656
/* #define HAL_IWDG_MODULE_ENABLED */
5757
/* #define HAL_LTDC_MODULE_ENABLED */
58-
/* #define HAL_RNG_MODULE_ENABLED */
58+
#define HAL_RNG_MODULE_ENABLED
5959
/* #define HAL_RTC_MODULE_ENABLED */
6060
/* #define HAL_SAI_MODULE_ENABLED */
6161
/* #define HAL_SD_MODULE_ENABLED */

ports/stm32f4/boards/stm32f412zg_discovery/stm32f4xx_hal_conf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
#define HAL_I2S_MODULE_ENABLED
5656
/* #define HAL_IWDG_MODULE_ENABLED */
5757
/* #define HAL_LTDC_MODULE_ENABLED */
58-
/* #define HAL_RNG_MODULE_ENABLED */
58+
#define HAL_RNG_MODULE_ENABLED
5959
/* #define HAL_RTC_MODULE_ENABLED */
6060
/* #define HAL_SAI_MODULE_ENABLED */
6161
#define HAL_SD_MODULE_ENABLED

ports/stm32f4/common-hal/os/__init__.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
#include "py/objstr.h"
3131
#include "py/objtuple.h"
3232

33+
#include "py/mperrno.h"
34+
#include "py/runtime.h"
35+
#include "stm32f4xx_hal.h"
36+
#include "stm32f4/periph.h"
37+
3338
STATIC const qstr os_uname_info_fields[] = {
3439
MP_QSTR_sysname, MP_QSTR_nodename,
3540
MP_QSTR_release, MP_QSTR_version, MP_QSTR_machine
@@ -56,11 +61,35 @@ mp_obj_t common_hal_os_uname(void) {
5661
return (mp_obj_t)&os_uname_info_obj;
5762
}
5863

64+
#define RNG_TIMEOUT 5
65+
5966
bool common_hal_os_urandom(uint8_t *buffer, uint32_t length) {
67+
#if (HAS_TRNG)
68+
//init the RNG
69+
__HAL_RCC_RNG_CLK_ENABLE();
70+
RNG_HandleTypeDef handle;
71+
handle.Instance = RNG;
72+
if (HAL_RNG_Init(&handle) != HAL_OK) mp_raise_ValueError(translate("RNG Init Error"));
6073

61-
for (uint32_t i = 0; i < length; i++) {
62-
buffer[i] = 4; //read in my coffee dregs; the truest random number, chosen by Nrthalotep
63-
//todo: spurn the gods, replace with actual code.
74+
//Assign bytes
75+
for (uint i = 0; i < length; i++) {
76+
uint32_t temp;
77+
uint32_t start = HAL_GetTick();
78+
//the HAL function has a timeout, but it isn't long enough, and isn't adjustable
79+
while(!(__HAL_RNG_GET_FLAG(&handle,RNG_FLAG_DRDY)) && ((HAL_GetTick() - start) < RNG_TIMEOUT));
80+
//
81+
if (HAL_RNG_GenerateRandomNumber(&handle, &temp) != HAL_OK) {
82+
mp_raise_ValueError(translate("Random number generation error"));
83+
}
84+
*buffer = (uint8_t)temp;
6485
}
86+
87+
//shut down the peripheral
88+
if (HAL_RNG_DeInit(&handle) != HAL_OK) mp_raise_ValueError(translate("RNG DeInit Error"));
89+
__HAL_RCC_RNG_CLK_DISABLE();
90+
6591
return true;
92+
#else
93+
return false;
94+
#endif
6695
}

ports/stm32f4/peripherals/stm32f4/periph.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,21 @@ typedef struct {
142142

143143
#ifdef STM32F411xE
144144
#define HAS_DAC 0
145+
#define HAS_TRNG 0
145146
#include "stm32f411xe/periph.h"
146147
#endif
147148

148149
#ifdef STM32F412Zx
149150
#define HAS_DAC 0
151+
#define HAS_TRNG 1
150152
#include "stm32f412zx/periph.h"
151153
#endif
152154

153155
//Foundation Lines
154156

155157
#ifdef STM32F405xx
156158
#define HAS_DAC 1
159+
#define HAS_TRNG 1
157160
#include "stm32f405xx/periph.h"
158161
#endif
159162

0 commit comments

Comments
 (0)