void hwrng_read(void *buf, unsigned int num) { unsigned int count = 0; uint8_t *b = (uint8_t *)buf; /* power on and enable the device */ HWRNG_CLKEN(); KINETIS_RNGA->CR = RNG_CR_INTM_MASK | RNG_CR_HA_MASK | RNG_CR_GO_MASK; /* self-seeding */ while (!(KINETIS_RNGA->SR & RNG_SR_OREG_LVL_MASK)); KINETIS_RNGA->ER = KINETIS_RNGA->OR ^ (uint32_t)buf; while (count < num) { /* wait for random data to be ready to read */ while (!(KINETIS_RNGA->SR & RNG_SR_OREG_LVL_MASK)); uint32_t tmp = KINETIS_RNGA->OR; /* copy data into result vector */ for (int i = 0; i < 4 && count < num; i++) { b[count++] = (uint8_t)tmp; tmp = tmp >> 8; } } /* power off the device */ KINETIS_RNGA->CR = 0; HWRNG_CLKDIS(); }
void hwrng_read(void *buf, unsigned int num) { unsigned int count = 0; uint8_t *b = (uint8_t *)buf; HWRNG_CLKEN(); if ((KINETIS_RNGB->VER & RNG_VER_TYPE_MASK) != 0b0001) { /* Wrong type of RNG */ /* TODO: Handle */ } /* Software reset, bit is self-clearing */ BITBAND_REG32(KINETIS_RNGB->CMD, RNG_CMD_SR_SHIFT) = 1; /* Set up automatic reseed */ KINETIS_RNGB->CR = RNG_CR_AR_MASK | RNG_CR_MASKERR_MASK | RNG_CR_MASKDONE_MASK; while (count < num) { uint32_t tmp; /* wait for random data to be ready to read */ while (!(KINETIS_RNGB->SR & RNG_SR_FIFO_LVL_MASK)); tmp = KINETIS_RNGB->OUT; /* copy data into result vector */ for (int i = 0; i < 4 && count < num; i++) { b[count++] = (uint8_t)tmp; tmp = tmp >> 8; } } KINETIS_RNGB->CR = 0; HWRNG_CLKDIS(); }