diff --git a/cpu/cc2538/periph/hwrng.c b/cpu/cc2538/periph/hwrng.c index 6d0d9a850b7b0e059bc8ecc615e2370919672599..008bce9d01d4a4ae40e23a6c0b8d69453f5f7e49 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; + } } }