Skip to content

Commit 72fac61

Browse files
committed
STM32F4/F7: Add RNG HAL implementation
1 parent 02fd613 commit 72fac61

File tree

11 files changed

+84
-65
lines changed

11 files changed

+84
-65
lines changed

hal/targets.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@
795795
"progen": {"target": "nucleo-f410rb"},
796796
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT", "TRANSACTION_QUEUE_SIZE_SPI=2"],
797797
"detect_code": ["0740"],
798-
"device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES"],
798+
"device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "RNG"],
799799
"release_versions": ["2", "5"]
800800
},
801801
"NUCLEO_F411RE": {
@@ -1077,7 +1077,7 @@
10771077
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT","DEVICE_RTC_LSI=1","TRANSACTION_QUEUE_SIZE_SPI=2"],
10781078
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
10791079
"progen": {"target": "disco-f429zi"},
1080-
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES"],
1080+
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "RNG"],
10811081
"release_versions": ["2", "5"]
10821082
},
10831083
"DISCO_F469NI": {
@@ -1090,7 +1090,7 @@
10901090
"macros": ["MBEDTLS_ENTROPY_HARDWARE_ALT","TRANSACTION_QUEUE_SIZE_SPI=2"],
10911091
"progen": {"target": "disco-f469ni"},
10921092
"detect_code": ["0788"],
1093-
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
1093+
"device_has": ["ANALOGIN", "ANALOGOUT", "CAN", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "RNG"],
10941094
"release_versions": ["2", "5"]
10951095
},
10961096
"DISCO_L053C8": {

hal/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_DISCO_F429ZI/objects.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ struct can_s {
7070
int index;
7171
};
7272

73+
struct rng_s {
74+
RNG_HandleTypeDef handle;
75+
};
76+
7377
#include "common_objects.h"
7478
#include "gpio_object.h"
7579

hal/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_DISCO_F469NI/objects.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ struct can_s {
7070
int index;
7171
};
7272

73+
struct rng_s {
74+
RNG_HandleTypeDef handle;
75+
};
76+
7377
#include "common_objects.h"
7478
#include "gpio_object.h"
7579

hal/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_NUCLEO_F410RB/objects.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ struct dac_s {
6565
uint8_t channel;
6666
};
6767

68+
struct rng_s {
69+
RNG_HandleTypeDef handle;
70+
};
71+
6872
#include "common_objects.h"
6973
#include "gpio_object.h"
7074

hal/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_NUCLEO_F429ZI/objects.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ struct can_s {
7070
int index;
7171
};
7272

73+
struct rng_s {
74+
RNG_HandleTypeDef handle;
75+
};
76+
7377
#include "gpio_object.h"
7478
#include "common_objects.h"
7579

hal/targets/hal/TARGET_STM/TARGET_STM32F4/entropy_hardware_poll.c renamed to hal/targets/hal/TARGET_STM/TARGET_STM32F4/rng_api.c

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,58 +25,52 @@
2525
defined(TARGET_STM32F479xx)
2626
#include <stdlib.h>
2727
#include "cmsis.h"
28-
29-
/* RNG handler declaration */
30-
RNG_HandleTypeDef RngHandle;
31-
28+
#include "rng_api.h"
3229

3330
/** rng_get_byte
3431
* @brief Get one byte of entropy from the RNG, assuming it is up and running.
3532
* @param pointer to the hardware generated random byte.
3633
*/
37-
static void rng_get_byte( unsigned char *byte )
34+
static void rng_get_byte(rng_t *obj, unsigned char *byte )
3835
{
39-
*byte = (unsigned char)HAL_RNG_GetRandomNumber(&RngHandle);
36+
*byte = (unsigned char)HAL_RNG_GetRandomNumber(&obj->handle);
4037
}
4138

42-
43-
/** mbedtls_hardware_poll
44-
* @brief Get len bytes of entropy from the hardware RNG.
45-
* @param data pointer will be NULL
46-
* @param output pointer to the random generated bytes buffer
47-
* @param len input is the requested length of bytes to be generated
48-
* @param olen is the pointer to the length of bytes effectively generated
49-
* @returns 0 if the generation went well. -1 in case of error
50-
*/
51-
int mbedtls_hardware_poll( void *data, unsigned char *output, size_t len, size_t *olen )
39+
void rng_init(rng_t *obj)
5240
{
53-
int ret;
54-
((void) data);
55-
5641
/* RNG Peripheral clock enable */
5742
__HAL_RCC_RNG_CLK_ENABLE();
5843

5944
/* Initialize RNG instance */
60-
RngHandle.Instance = RNG;
61-
HAL_RNG_Init(&RngHandle);
45+
obj->handle.Instance = RNG;
46+
HAL_RNG_Init(&obj->handle);
6247

63-
/* Get Random byte */
64-
for( uint32_t i = 0; i < len; i++ ){
65-
rng_get_byte( output + i );
48+
}
6649

50+
void rng_free(rng_t *obj)
51+
{
52+
/*Disable the RNG peripheral */
53+
HAL_RNG_DeInit(&obj->handle);
54+
/* RNG Peripheral clock disable - assume we're the only users of RNG */
55+
__HAL_RCC_RNG_CLK_DISABLE();
56+
}
57+
58+
int rng_get_numbers(rng_t *obj, uint8_t *output, size_t length, size_t *output_length)
59+
{
60+
int ret;
61+
62+
/* Get Random byte */
63+
for( uint32_t i = 0; i < length; i++ ){
64+
rng_get_byte(obj, output + i );
6765
}
68-
*olen = len;
66+
67+
*output_length = length;
6968
/* Just be extra sure that we didn't do it wrong */
70-
if( ( __HAL_RNG_GET_FLAG(&RngHandle, (RNG_FLAG_CECS|RNG_FLAG_SECS)) ) != 0 ) {
69+
if( ( __HAL_RNG_GET_FLAG(&obj->handle, (RNG_FLAG_CECS | RNG_FLAG_SECS)) ) != 0 ) {
7170
ret = -1;
7271
} else {
7372
ret = 0;
7473
}
75-
/*Disable the RNG peripheral */
76-
HAL_RNG_DeInit(&RngHandle);
77-
/* RNG Peripheral clock disable - assume we're the only users of RNG */
78-
__HAL_RCC_RNG_CLK_DISABLE();
79-
8074

8175
return( ret );
8276
}

hal/targets/hal/TARGET_STM/TARGET_STM32F7/TARGET_DISCO_F746NG/objects.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ struct can_s {
9090
int index;
9191
};
9292

93+
struct rng_s {
94+
RNG_HandleTypeDef handle;
95+
};
96+
9397
#include "common_objects.h"
9498
#include "gpio_object.h"
9599

hal/targets/hal/TARGET_STM/TARGET_STM32F7/TARGET_DISCO_F769NI/objects.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ struct can_s {
9090
int index;
9191
};
9292

93+
struct rng_s {
94+
RNG_HandleTypeDef handle;
95+
};
96+
9397
#include "gpio_object.h"
9498
#include "common_objects.h"
9599

hal/targets/hal/TARGET_STM/TARGET_STM32F7/TARGET_NUCLEO_F746ZG/objects.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ struct can_s {
9090
int index;
9191
};
9292

93+
struct rng_s {
94+
RNG_HandleTypeDef handle;
95+
};
96+
9397
#include "common_objects.h"
9498
#include "gpio_object.h"
9599

hal/targets/hal/TARGET_STM/TARGET_STM32F7/TARGET_NUCLEO_F767ZI/objects.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ struct can_s {
9090
int index;
9191
};
9292

93+
struct rng_s {
94+
RNG_HandleTypeDef handle;
95+
};
9396

9497
#include "gpio_object.h"
9598
#include "common_objects.h"

0 commit comments

Comments
 (0)