From 214ddc4fc404e00a0f65277fd94e6202f08b2ee9 Mon Sep 17 00:00:00 2001 From: smlng <s@mlng.net> Date: Fri, 10 Aug 2018 09:31:33 +0200 Subject: [PATCH] cpu/cc2538: adapt and cleanup periph/hwrng Some minor adaptions due to cleanup in periph/adc and usage of vendor header files. --- cpu/cc2538/periph/hwrng.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/cpu/cc2538/periph/hwrng.c b/cpu/cc2538/periph/hwrng.c index 6d0d9a850b..008bce9d01 100644 --- a/cpu/cc2538/periph/hwrng.c +++ b/cpu/cc2538/periph/hwrng.c @@ -21,22 +21,31 @@ * @} */ +#include "vendor/hw_memmap.h" +#include "vendor/hw_soc_adc.h" + #include "cpu.h" #include "periph/hwrng.h" +#define ENABLE_DEBUG (0) +#include "debug.h" + +static cc2538_soc_adc_t *soc_adc = (cc2538_soc_adc_t *)SOC_ADC_BASE; + void hwrng_init(void) { uint16_t seed = 0; int i; /* Make sure the RNG is on */ - SOC_ADC->cc2538_adc_adccon1.ADCCON1bits.RCTRL = 0; + uint32_t reg32 = soc_adc->ADCCON1 & ~(SOC_ADC_ADCCON1_RCTRL_M); + soc_adc->ADCCON1 = reg32; /* Enable clock for the RF Core */ SYS_CTRL_RCGCRFC = 1; /* Wait for the clock ungating to take effect */ - while (SYS_CTRL_RCGCRFC != 1); + while (SYS_CTRL_RCGCRFC != 1) {} /* Infinite RX - FRMCTRL0[3:2] = 10. This will mess with radio operation */ RFCORE_XREG_FRMCTRL0 = 0x00000008; @@ -63,8 +72,8 @@ void hwrng_init(void) } /* Seed the high byte first: */ - SOC_ADC_RNDL = (seed >> 8) & 0xff; - SOC_ADC_RNDL = seed & 0xff; + soc_adc->RNDH = (seed >> 8) & 0xff; + soc_adc->RNDL = seed & 0xff; /* Turn RF off: */ RFCORE_SFR_RFST = ISRFOFF; @@ -72,16 +81,16 @@ void hwrng_init(void) void hwrng_read(void *buf, unsigned int num) { - unsigned count; uint8_t *b = (uint8_t *)buf; - for (count = 0; count < num; ) { + for (unsigned count = 0; count < num; count++) { /* Clock the RNG LSFR once: */ - SOC_ADC->cc2538_adc_adccon1.ADCCON1bits.RCTRL = 1; - + soc_adc->ADCCON1 = soc_adc->ADCCON1 | (1UL << SOC_ADC_ADCCON1_RCTRL_S); /* Read up to 2 bytes of hwrng data: */ - b[count++] = SOC_ADC_RNDL; - if (count >= num) break; - b[count++] = SOC_ADC_RNDH; + b[count] = soc_adc->RNDL; + count++; + if (count < num) { + b[count] = soc_adc->RNDH; + } } } -- GitLab