Skip to content

Commit 02fd613

Browse files
committed
k66f: add RNG HAL implementation
1 parent 7d68492 commit 02fd613

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

hal/targets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@
603603
"inherits": ["Target"],
604604
"progen": {"target": "frdm-k66f"},
605605
"detect_code": ["0311"],
606-
"device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES"],
606+
"device_has": ["ANALOGIN", "ANALOGOUT", "ERROR_RED", "I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "STDIO_MESSAGES", "RNG"],
607607
"release_versions": ["2", "5"]
608608
},
609609
"NUCLEO_F030R8": {
Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,25 @@
2626
#include "cmsis.h"
2727
#include "fsl_common.h"
2828
#include "fsl_clock.h"
29+
#include "rng_api.h"
30+
31+
void rng_init(rng_t *obj)
32+
{
33+
CLOCK_EnableClock(kCLOCK_Rnga0);
34+
CLOCK_DisableClock(kCLOCK_Rnga0);
35+
CLOCK_EnableClock(kCLOCK_Rnga0);
36+
}
37+
38+
void rng_free(rng_t *obj)
39+
{
40+
CLOCK_DisableClock(kCLOCK_Rnga0);
41+
}
2942

3043
/*
3144
* Get one byte of entropy from the RNG, assuming it is up and running.
3245
* As recommended (34.1.1), get only one bit of each output.
3346
*/
34-
static void rng_get_byte( unsigned char *byte )
47+
static void rng_get_byte(unsigned char *byte)
3548
{
3649
size_t bit;
3750

@@ -43,41 +56,25 @@ static void rng_get_byte( unsigned char *byte )
4356
}
4457
}
4558

46-
/*
47-
* Get len bytes of entropy from the hardware RNG.
48-
*/
49-
int mbedtls_hardware_poll( void *data,
50-
unsigned char *output, size_t len, size_t *olen )
59+
int rng_get_numbers(rng_t *obj, uint8_t *output, size_t length, size_t *output_length)
5160
{
5261
size_t i;
5362
int ret;
54-
((void) data);
55-
56-
CLOCK_EnableClock( kCLOCK_Rnga0 );
57-
CLOCK_DisableClock( kCLOCK_Rnga0 );
58-
CLOCK_EnableClock( kCLOCK_Rnga0 );
5963

6064
/* Set "Interrupt Mask", "High Assurance" and "Go",
6165
* unset "Clear interrupt" and "Sleep" */
6266
RNG->CR = RNG_CR_INTM_MASK | RNG_CR_HA_MASK | RNG_CR_GO_MASK;
6367

64-
for( i = 0; i < len; i++ )
65-
rng_get_byte( output + i );
68+
for (i = 0; i < length; i++) {
69+
rng_get_byte(output + i);
70+
}
6671

6772
/* Just be extra sure that we didn't do it wrong */
68-
if( ( RNG->SR & RNG_SR_SECV_MASK ) != 0 )
69-
{
70-
ret = -1;
71-
goto cleanup;
73+
if ((RNG->SR & RNG_SR_SECV_MASK) != 0) {
74+
return -1;
7275
}
7376

74-
*olen = len;
75-
ret = 0;
76-
77-
cleanup:
78-
/* Disable clock to save power - assume we're the only users of RNG */
79-
CLOCK_DisableClock( kCLOCK_Rnga0 );
77+
*output_length = length;
8078

81-
return( ret );
79+
return 0;
8280
}
83-

0 commit comments

Comments
 (0)