Skip to content

Commit a6dba4d

Browse files
authored
Merge pull request #6128 from marcuschangarm/fix_trng
[feature-nrf528xx] Fix TRNG for NRF52
2 parents 7981dbc + dc2311c commit a6dba4d

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

targets/TARGET_NORDIC/TARGET_NRF5x/trng_api.c

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,45 @@
3737
*/
3838

3939
#if defined(DEVICE_TRNG)
40-
#include "trng_api.h"
40+
41+
#include "hal/trng_api.h"
42+
#include "hal/critical_section_api.h"
43+
4144
#include "nrf_drv_rng.h"
4245

46+
/* Keep track of instantiated FlashIAP objects. */
47+
static int nordic_trng_counter = 0;
48+
4349
void trng_init(trng_t *obj)
4450
{
4551
(void) obj;
4652

47-
(void)nrf_drv_rng_init(NULL);
53+
/* Increment global counter inside critical section. */
54+
hal_critical_section_enter();
55+
nordic_trng_counter++;
56+
int counter = nordic_trng_counter;
57+
hal_critical_section_exit();
58+
59+
/* Initialize TRNG on first object only. */
60+
if (counter == 1) {
61+
nrf_drv_rng_init(NULL);
62+
}
4863
}
4964

5065
void trng_free(trng_t *obj)
5166
{
5267
(void) obj;
5368

54-
nrf_drv_rng_uninit();
69+
/* Decrement global counter inside critical section. */
70+
hal_critical_section_enter();
71+
nordic_trng_counter--;
72+
int counter = nordic_trng_counter;
73+
hal_critical_section_exit();
74+
75+
/* Deinitialize TRNG when all objects have been freed. */
76+
if (counter == 0) {
77+
nrf_drv_rng_uninit();
78+
}
5579
}
5680

5781
/* Get random data from NRF5x TRNG peripheral.
@@ -61,30 +85,35 @@ void trng_free(trng_t *obj)
6185
*/
6286
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
6387
{
64-
uint8_t bytes_available;
65-
6688
(void) obj;
6789

90+
/* Query how many bytes are available. */
91+
uint8_t bytes_available;
6892
nrf_drv_rng_bytes_available(&bytes_available);
6993

94+
/* If no bytes are cached, block until at least 1 byte is available. */
7095
if (bytes_available == 0) {
7196
nrf_drv_rng_block_rand(output, 1);
7297
*output_length = 1;
7398
} else {
7499

100+
/* Get up to the requested number of bytes. */
75101
if (bytes_available > length) {
76102
bytes_available = length;
77103
}
78104

79-
if (nrf_drv_rng_rand(output, bytes_available) != NRF_SUCCESS) {
80-
*output_length = 0;
81-
return -1;
82-
} else {
105+
ret_code_t result = nrf_drv_rng_rand(output, bytes_available);
106+
107+
/* Set output length with available bytes. */
108+
if (result == NRF_SUCCESS) {
83109
*output_length = bytes_available;
110+
} else {
111+
*output_length = 0;
84112
}
85113
}
86114

87-
return 0;
115+
/* Set return value based on how many bytes was read. */
116+
return (*output_length == 0) ? -1 : 0;
88117
}
89118

90119
#endif

targets/targets.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3442,6 +3442,7 @@
34423442
"device_has": [
34433443
"ITM",
34443444
"FLASH",
3445+
"TRNG",
34453446
"STCLK_OFF_DURING_SLEEP"
34463447
],
34473448
"extra_labels": [
@@ -3475,7 +3476,7 @@
34753476
"supported_form_factors": ["ARDUINO"],
34763477
"inherits": ["MCU_NRF52832"],
34773478
"macros_add": ["BOARD_PCA10040", "NRF52_PAN_12", "NRF52_PAN_15", "NRF52_PAN_58", "NRF52_PAN_55", "NRF52_PAN_54", "NRF52_PAN_31", "NRF52_PAN_30", "NRF52_PAN_51", "NRF52_PAN_36", "NRF52_PAN_53", "S132", "CONFIG_GPIO_AS_PINRESET", "BLE_STACK_SUPPORT_REQD", "SWI_DISABLE0", "NRF52_PAN_20", "NRF52_PAN_64", "NRF52_PAN_62", "NRF52_PAN_63"],
3478-
"device_has_add": ["ANALOGIN", "I2C", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPI_ASYNCH", "SPISLAVE", "TRNG"],
3479+
"device_has_add": ["ANALOGIN", "I2C", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPI_ASYNCH", "SPISLAVE"],
34793480
"release_versions": ["5"],
34803481
"device_name": "nRF52832_xxAA"
34813482
},
@@ -3549,6 +3550,7 @@
35493550
"device_has": [
35503551
"ITM",
35513552
"FLASH",
3553+
"TRNG",
35523554
"STCLK_OFF_DURING_SLEEP"
35533555
],
35543556
"extra_labels": [
@@ -3582,7 +3584,7 @@
35823584
"supported_form_factors": ["ARDUINO"],
35833585
"inherits": ["MCU_NRF52840"],
35843586
"macros_add": ["BOARD_PCA10056", "CONFIG_GPIO_AS_PINRESET", "SWI_DISABLE0", "NRF52_ERRATA_20"],
3585-
"device_has_add": ["ANALOGIN", "I2C", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "TRNG"],
3587+
"device_has_add": ["ANALOGIN", "I2C", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE"],
35863588
"release_versions": ["5"],
35873589
"device_name": "nRF52840_xxAA",
35883590
"bootloader_supported": true

0 commit comments

Comments
 (0)