static inline void modem_clock_init(void) { /* Use the CLK_OUT of the modem as the clock source. */ /* Enable GPIO clock gates */ KW2XDRF_PORT_CLKEN(); KW2XDRF_CLK_CTRL_CLKEN(); /* Modem RST_B is connected to PTB19 and can be used to reset the modem. */ KW2XDRF_PORT_DEV->PCR[KW2XDRF_RST_PIN] = PORT_PCR_MUX(1); BITBAND_REG32(KW2XDRF_GPIO->PDDR, KW2XDRF_RST_PIN) = 1; KW2XDRF_GPIO->PCOR = (1 << KW2XDRF_RST_PIN); /* Modem GPIO5 is connected to PTC0 and can be used to select CLK_OUT frequency, */ /* set PTC0 high for CLK_OUT=32.787kHz and low for CLK_OUT=4MHz. */ KW2XDRF_CLK_CTRL_PORT_DEV->PCR[KW2XDRF_CLK_CTRL_PIN] = PORT_PCR_MUX(1); BITBAND_REG32(KW2XDRF_CLK_CTRL_GPIO->PDDR, KW2XDRF_CLK_CTRL_PIN) = 1; KW2XDRF_CLK_CTRL_GPIO->PCOR = (1 << KW2XDRF_CLK_CTRL_PIN); /* Modem IRQ_B is connected to PTB3, modem interrupt request to the MCU. */ KW2XDRF_PORT_DEV->PCR[KW2XDRF_IRQ_PIN] = PORT_PCR_MUX(1); BITBAND_REG32(KW2XDRF_GPIO->PDDR, KW2XDRF_IRQ_PIN) = 0; /* release the reset */ KW2XDRF_GPIO->PSOR = (1 << KW2XDRF_RST_PIN); /* wait for modem IRQ_B interrupt request */ while (KW2XDRF_GPIO->PDIR & (1 << KW2XDRF_IRQ_PIN)); }
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(); }
void pre_startup(void) { /* disable the WDOG */ wdog_disable(); #ifdef SIM_SCGC7_FLEXBUS_SHIFT /* * Workaround for hardware errata e4218: "SIM/FLEXBUS: SIM_SCGC7[FLEXBUS] * bit should be cleared when the FlexBus is not being used." */ BITBAND_REG32(SIM->SCGC7, SIM_SCGC7_FLEXBUS_SHIFT) = 0; #endif }
void random_poweron(void) { RANDOM_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; }