Skip to content
Snippets Groups Projects
Commit 2f9e3c89 authored by Hauke Petersen's avatar Hauke Petersen
Browse files

periph/hwrng: use void* buf for hwrng_read()

parent 4403cc93
No related branches found
No related tags found
No related merge requests found
...@@ -69,16 +69,18 @@ void hwrng_init(void) ...@@ -69,16 +69,18 @@ void hwrng_init(void)
RFCORE_SFR_RFST = ISRFOFF; RFCORE_SFR_RFST = ISRFOFF;
} }
void hwrng_read(uint8_t *buf, unsigned int num) void hwrng_read(void *buf, unsigned int num)
{ {
unsigned count; unsigned count;
uint8_t *b = (uint8_t *)buf;
for (count = 0; count < num; ) { for (count = 0; count < num; ) {
/* Clock the RNG LSFR once: */ /* Clock the RNG LSFR once: */
SOC_ADC->cc2538_adc_adccon1.ADCCON1bits.RCTRL = 1; SOC_ADC->cc2538_adc_adccon1.ADCCON1bits.RCTRL = 1;
/* Read up to 2 bytes of hwrng data: */ /* Read up to 2 bytes of hwrng data: */
buf[count++] = SOC_ADC_RNDL; b[count++] = SOC_ADC_RNDL;
if (count >= num) break; if (count >= num) break;
buf[count++] = SOC_ADC_RNDH; b[count++] = SOC_ADC_RNDH;
} }
} }
...@@ -32,9 +32,10 @@ void hwrng_init(void) ...@@ -32,9 +32,10 @@ void hwrng_init(void)
/* nothing to do here */ /* nothing to do here */
} }
void hwrng_read(uint8_t *buf, unsigned int num) void hwrng_read(void *buf, unsigned int num)
{ {
unsigned int count = 0; unsigned int count = 0;
uint8_t *b = (uint8_t *)buf;
/* power on and enable the device */ /* power on and enable the device */
HWRNG_CLKEN(); HWRNG_CLKEN();
...@@ -53,7 +54,7 @@ void hwrng_read(uint8_t *buf, unsigned int num) ...@@ -53,7 +54,7 @@ void hwrng_read(uint8_t *buf, unsigned int num)
/* copy data into result vector */ /* copy data into result vector */
for (int i = 0; i < 4 && count < num; i++) { for (int i = 0; i < 4 && count < num; i++) {
buf[count++] = (uint8_t)tmp; b[count++] = (uint8_t)tmp;
tmp = tmp >> 8; tmp = tmp >> 8;
} }
} }
......
...@@ -33,9 +33,10 @@ void hwrng_init(void) ...@@ -33,9 +33,10 @@ void hwrng_init(void)
/* nothing to be done here */ /* nothing to be done here */
} }
void hwrng_read(uint8_t *buf, unsigned int num) void hwrng_read(void *buf, unsigned int num)
{ {
unsigned int count = 0; unsigned int count = 0;
uint8_t *b = (uint8_t *)buf;
HWRNG_CLKEN(); HWRNG_CLKEN();
...@@ -59,7 +60,7 @@ void hwrng_read(uint8_t *buf, unsigned int num) ...@@ -59,7 +60,7 @@ void hwrng_read(uint8_t *buf, unsigned int num)
/* copy data into result vector */ /* copy data into result vector */
for (int i = 0; i < 4 && count < num; i++) { for (int i = 0; i < 4 && count < num; i++) {
buf[count++] = (uint8_t)tmp; b[count++] = (uint8_t)tmp;
tmp = tmp >> 8; tmp = tmp >> 8;
} }
} }
......
...@@ -70,8 +70,10 @@ void hwrng_init(void) ...@@ -70,8 +70,10 @@ void hwrng_init(void)
initialized = 1; initialized = 1;
} }
void hwrng_read(uint8_t *buf, unsigned int num) void hwrng_read(void *buf, unsigned int num)
{ {
uint8_t *b = (uint8_t *)buf;
if (!initialized) { if (!initialized) {
warnx("hwrng_read: random device not initialized, failing\n"); warnx("hwrng_read: random device not initialized, failing\n");
return; return;
...@@ -80,10 +82,10 @@ void hwrng_read(uint8_t *buf, unsigned int num) ...@@ -80,10 +82,10 @@ void hwrng_read(uint8_t *buf, unsigned int num)
DEBUG("hwrng_read: writing %u bytes\n", num); DEBUG("hwrng_read: writing %u bytes\n", num);
switch (_native_rng_mode) { switch (_native_rng_mode) {
case 0: case 0:
_native_rng_read_hq(buf, num); _native_rng_read_hq(b, num);
break; break;
case 1: case 1:
_native_rng_read_det(buf, num); _native_rng_read_det(b, num);
break; break;
default: default:
err(EXIT_FAILURE, "hwrng_read: _native_rng_mode is in invalid state %i\n", err(EXIT_FAILURE, "hwrng_read: _native_rng_mode is in invalid state %i\n",
......
...@@ -29,9 +29,10 @@ void hwrng_init(void) ...@@ -29,9 +29,10 @@ void hwrng_init(void)
/* nothing to do here */ /* nothing to do here */
} }
void hwrng_read(uint8_t *buf, unsigned int num) void hwrng_read(void *buf, unsigned int num)
{ {
unsigned int count = 0; unsigned int count = 0;
uint8_t *b = (uint8_t *)buf;
/* power on RNG */ /* power on RNG */
#ifdef CPU_FAM_NRF51 #ifdef CPU_FAM_NRF51
...@@ -46,7 +47,7 @@ void hwrng_read(uint8_t *buf, unsigned int num) ...@@ -46,7 +47,7 @@ void hwrng_read(uint8_t *buf, unsigned int num)
cpu_sleep_until_event(); cpu_sleep_until_event();
} }
buf[count++] = (uint8_t)NRF_RNG->VALUE; b[count++] = (uint8_t)NRF_RNG->VALUE;
/* NRF51 PAN #21 -> read value before clearing VALRDY */ /* NRF51 PAN #21 -> read value before clearing VALRDY */
NRF_RNG->EVENTS_VALRDY = 0; NRF_RNG->EVENTS_VALRDY = 0;
} }
......
...@@ -32,9 +32,10 @@ void hwrng_init(void) ...@@ -32,9 +32,10 @@ void hwrng_init(void)
/* no need for initialization */ /* no need for initialization */
} }
void hwrng_read(uint8_t *buf, unsigned int num) void hwrng_read(void *buf, unsigned int num)
{ {
unsigned count = 0; unsigned count = 0;
uint8_t *b = (uint8_t *)buf;
/* enable clock signal for TRNG module */ /* enable clock signal for TRNG module */
PMC->PMC_PCER1 |= PMC_PCER1_PID41; PMC->PMC_PCER1 |= PMC_PCER1_PID41;
...@@ -48,7 +49,7 @@ void hwrng_read(uint8_t *buf, unsigned int num) ...@@ -48,7 +49,7 @@ void hwrng_read(uint8_t *buf, unsigned int num)
uint32_t tmp = TRNG->TRNG_ODATA; uint32_t tmp = TRNG->TRNG_ODATA;
/* extract copy bytes to result */ /* extract copy bytes to result */
for (int i = 0; i < 4 && count < num; i++) { for (int i = 0; i < 4 && count < num; i++) {
buf[count++] = (uint8_t)tmp; b[count++] = (uint8_t)tmp;
tmp = tmp >> 8; tmp = tmp >> 8;
} }
} }
......
...@@ -36,11 +36,12 @@ void hwrng_init(void) ...@@ -36,11 +36,12 @@ void hwrng_init(void)
} }
void hwrng_read(uint8_t *buf, unsigned int num) void hwrng_read(void *buf, unsigned int num)
{ {
/* cppcheck-suppress variableScope */ /* cppcheck-suppress variableScope */
uint32_t tmp; uint32_t tmp;
unsigned int count = 0; unsigned int count = 0;
uint8_t *b = (uint8_t *)buf;
/* enable RNG reset state */ /* enable RNG reset state */
periph_clk_en(AHB2, RCC_AHB2ENR_RNGEN); periph_clk_en(AHB2, RCC_AHB2ENR_RNGEN);
...@@ -54,7 +55,7 @@ void hwrng_read(uint8_t *buf, unsigned int num) ...@@ -54,7 +55,7 @@ void hwrng_read(uint8_t *buf, unsigned int num)
tmp = RNG->DR; tmp = RNG->DR;
/* copy data into result vector */ /* copy data into result vector */
for (int i = 0; i < 4 && count < num; i++) { for (int i = 0; i < 4 && count < num; i++) {
buf[count++] = (uint8_t)tmp; b[count++] = (uint8_t)tmp;
tmp = tmp >> 8; tmp = tmp >> 8;
} }
} }
......
...@@ -30,9 +30,10 @@ void hwrng_init(void) ...@@ -30,9 +30,10 @@ void hwrng_init(void)
/* no need for initialization */ /* no need for initialization */
} }
void hwrng_read(uint8_t *buf, unsigned int num) void hwrng_read(void *buf, unsigned int num)
{ {
unsigned int count = 0; unsigned int count = 0;
uint8_t *b = (uint8_t *)buf;
/* power on and enable the device */ /* power on and enable the device */
periph_clk_en(AHB2, RCC_AHB2ENR_RNGEN); periph_clk_en(AHB2, RCC_AHB2ENR_RNGEN);
...@@ -46,7 +47,7 @@ void hwrng_read(uint8_t *buf, unsigned int num) ...@@ -46,7 +47,7 @@ void hwrng_read(uint8_t *buf, unsigned int num)
uint32_t tmp = RNG->DR; uint32_t tmp = RNG->DR;
/* copy data into result vector */ /* copy data into result vector */
for (int i = 0; i < 4 && count < num; i++) { for (int i = 0; i < 4 && count < num; i++) {
buf[count++] = (uint8_t)tmp; b[count++] = (uint8_t)tmp;
tmp = tmp >> 8; tmp = tmp >> 8;
} }
} }
......
...@@ -57,7 +57,7 @@ void hwrng_init(void); ...@@ -57,7 +57,7 @@ void hwrng_init(void);
* @param[in] buf destination buffer to write the bytes to * @param[in] buf destination buffer to write the bytes to
* @param[in] num number of bytes to get from device * @param[in] num number of bytes to get from device
*/ */
void hwrng_read(uint8_t *buf, unsigned int num); void hwrng_read(void *buf, unsigned int num);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment