Merge pull request #2300 from hierophect/stm32-urandom
STM32: implement OS urandom
This commit is contained in:
commit
41aa214589
@ -55,7 +55,7 @@
|
||||
#define HAL_I2S_MODULE_ENABLED
|
||||
/* #define HAL_IWDG_MODULE_ENABLED */
|
||||
/* #define HAL_LTDC_MODULE_ENABLED */
|
||||
/* #define HAL_RNG_MODULE_ENABLED */
|
||||
#define HAL_RNG_MODULE_ENABLED
|
||||
/* #define HAL_RTC_MODULE_ENABLED */
|
||||
/* #define HAL_SAI_MODULE_ENABLED */
|
||||
/* #define HAL_SD_MODULE_ENABLED */
|
||||
|
@ -55,7 +55,7 @@
|
||||
#define HAL_I2S_MODULE_ENABLED
|
||||
/* #define HAL_IWDG_MODULE_ENABLED */
|
||||
/* #define HAL_LTDC_MODULE_ENABLED */
|
||||
/* #define HAL_RNG_MODULE_ENABLED */
|
||||
#define HAL_RNG_MODULE_ENABLED
|
||||
/* #define HAL_RTC_MODULE_ENABLED */
|
||||
/* #define HAL_SAI_MODULE_ENABLED */
|
||||
/* #define HAL_SD_MODULE_ENABLED */
|
||||
|
@ -55,7 +55,7 @@
|
||||
#define HAL_I2S_MODULE_ENABLED
|
||||
/* #define HAL_IWDG_MODULE_ENABLED */
|
||||
/* #define HAL_LTDC_MODULE_ENABLED */
|
||||
/* #define HAL_RNG_MODULE_ENABLED */
|
||||
#define HAL_RNG_MODULE_ENABLED
|
||||
/* #define HAL_RTC_MODULE_ENABLED */
|
||||
/* #define HAL_SAI_MODULE_ENABLED */
|
||||
#define HAL_SD_MODULE_ENABLED
|
||||
|
@ -30,6 +30,11 @@
|
||||
#include "py/objstr.h"
|
||||
#include "py/objtuple.h"
|
||||
|
||||
#include "py/mperrno.h"
|
||||
#include "py/runtime.h"
|
||||
#include "stm32f4xx_hal.h"
|
||||
#include "stm32f4/periph.h"
|
||||
|
||||
STATIC const qstr os_uname_info_fields[] = {
|
||||
MP_QSTR_sysname, MP_QSTR_nodename,
|
||||
MP_QSTR_release, MP_QSTR_version, MP_QSTR_machine
|
||||
@ -56,11 +61,35 @@ mp_obj_t common_hal_os_uname(void) {
|
||||
return (mp_obj_t)&os_uname_info_obj;
|
||||
}
|
||||
|
||||
bool common_hal_os_urandom(uint8_t *buffer, uint32_t length) {
|
||||
#define RNG_TIMEOUT 5
|
||||
|
||||
for (uint32_t i = 0; i < length; i++) {
|
||||
buffer[i] = 4; //read in my coffee dregs; the truest random number, chosen by Nrthalotep
|
||||
//todo: spurn the gods, replace with actual code.
|
||||
bool common_hal_os_urandom(uint8_t *buffer, uint32_t length) {
|
||||
#if (HAS_TRNG)
|
||||
//init the RNG
|
||||
__HAL_RCC_RNG_CLK_ENABLE();
|
||||
RNG_HandleTypeDef handle;
|
||||
handle.Instance = RNG;
|
||||
if (HAL_RNG_Init(&handle) != HAL_OK) mp_raise_ValueError(translate("RNG Init Error"));
|
||||
|
||||
//Assign bytes
|
||||
for (uint i = 0; i < length; i++) {
|
||||
uint32_t temp;
|
||||
uint32_t start = HAL_GetTick();
|
||||
//the HAL function has a timeout, but it isn't long enough, and isn't adjustable
|
||||
while(!(__HAL_RNG_GET_FLAG(&handle,RNG_FLAG_DRDY)) && ((HAL_GetTick() - start) < RNG_TIMEOUT));
|
||||
//
|
||||
if (HAL_RNG_GenerateRandomNumber(&handle, &temp) != HAL_OK) {
|
||||
mp_raise_ValueError(translate("Random number generation error"));
|
||||
}
|
||||
*buffer = (uint8_t)temp;
|
||||
}
|
||||
|
||||
//shut down the peripheral
|
||||
if (HAL_RNG_DeInit(&handle) != HAL_OK) mp_raise_ValueError(translate("RNG DeInit Error"));
|
||||
__HAL_RCC_RNG_CLK_DISABLE();
|
||||
|
||||
return true;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
@ -142,11 +142,13 @@ typedef struct {
|
||||
|
||||
#ifdef STM32F411xE
|
||||
#define HAS_DAC 0
|
||||
#define HAS_TRNG 0
|
||||
#include "stm32f411xe/periph.h"
|
||||
#endif
|
||||
|
||||
#ifdef STM32F412Zx
|
||||
#define HAS_DAC 0
|
||||
#define HAS_TRNG 1
|
||||
#include "stm32f412zx/periph.h"
|
||||
#endif
|
||||
|
||||
@ -154,6 +156,7 @@ typedef struct {
|
||||
|
||||
#ifdef STM32F405xx
|
||||
#define HAS_DAC 1
|
||||
#define HAS_TRNG 1
|
||||
#include "stm32f405xx/periph.h"
|
||||
#endif
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user