int main(void) { halInit(); chSysInit(); rccEnableAHB2(RCC_AHB2ENR_RNGEN, 0); { LedCfg cfg = {GPIOD, GPIOD_LED_ORANGE}; orangeTP = chThdCreateStatic(orangeStack, sizeof(orangeStack), THD_PRIO, ledThd, &cfg); } { LedCfg cfg = {GPIOD, GPIOD_LED_RED}; redTP = chThdCreateStatic(redStack, sizeof(redStack), THD_PRIO, ledThd, &cfg); } { LedCfg cfg = {GPIOD, GPIOD_LED_BLUE}; blueTP = chThdCreateStatic(blueStack, sizeof(blueStack), THD_PRIO, ledThd, &cfg); } { LedCfg cfg = {GPIOD, GPIOD_LED_GREEN}; greenTP = chThdCreateStatic(greenStack, sizeof(greenStack), THD_PRIO, ledThd, &cfg); } chThdExit(1); return 0; }
// ================================= Random ==================================== uint32_t Random(uint32_t TopValue) { rccEnableAHB2(RCC_AHB2ENR_RNGEN, FALSE); // Enable clock RNG->CR |= RNG_CR_RNGEN; // Enable generator while(!(RNG->SR & RNG_SR_DRDY)); // Wait until ready uint32_t Rnd = RNG->DR; Rnd = Rnd % (TopValue + 1); rccDisableAHB2(RCC_AHB2ENR_RNGEN, FALSE); // Stop clock return Rnd; }
/* * Application entry point. */ int main(void) { static const evhandler_t evhndl[] = { InsertHandler, RemoveHandler, ShellHandler }; event_listener_t el0, el1, el2; /* * System initializations. * - HAL initialization, this also initializes the configured device drivers * and performs the board-specific initializations. * - Kernel initialization, the main() function becomes a thread and the * RTOS is active. * - lwIP subsystem initialization using the default configuration. */ halInit(); chSysInit(); /* * Initialize RNG */ rccEnableAHB2(RCC_AHB2ENR_RNGEN, 0); RNG->CR |= RNG_CR_IE; RNG->CR |= RNG_CR_RNGEN; /* lwip */ lwipInit(NULL); /* * Target-dependent setup code. */ portab_setup(); /* * Initializes a serial-over-USB CDC driver. */ sduObjectInit(&PORTAB_SDU1); sduStart(&PORTAB_SDU1, &serusbcfg); /* * Activates the USB driver and then the USB bus pull-up on D+. * Note, a delay is inserted in order to not have to disconnect the cable * after a reset. */ usbDisconnectBus(serusbcfg.usbp); chThdSleepMilliseconds(1500); usbStart(serusbcfg.usbp, &usbcfg); usbConnectBus(serusbcfg.usbp); /* * Shell manager initialization. */ shellInit(); /* * Activates the SDC driver 1 using default configuration. */ sdcStart(&SDCD1, NULL); /* * Activates the card insertion monitor. */ tmr_init(&SDCD1); /* * Creates the blinker thread. */ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); /* * Creates the HTTPS thread (it changes priority internally). */ chThdCreateStatic(wa_https_server, sizeof(wa_https_server), NORMALPRIO + 1, https_server, NULL); /* * Normal main() thread activity, handling SD card events and shell * start/exit. */ chEvtRegister(&inserted_event, &el0, 0); chEvtRegister(&removed_event, &el1, 1); chEvtRegister(&shell_terminated, &el2, 2); while (true) { if (!shelltp && (PORTAB_SDU1.config->usbp->state == USB_ACTIVE)) { shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, "shell", NORMALPRIO + 1, shellThread, (void *)&shell_cfg1); } chEvtDispatch(evhndl, chEvtWaitOneTimeout(ALL_EVENTS, TIME_MS2I(500))); } }