static msg_t tUsbTx(void *arg) { (void)arg; chRegSetThreadName("usbTx"); msg_t msg; usbPacket *usbBufp; enum {UsbTxComleteID = 0, UsbResetID = 1, UsbConfiguredID = 2}; EventListener elUsbTxComplete; EventListener elUsbReset; EventListener elUsbConfigured; eventmask_t activeEvents; chEvtRegister(&esUsbTxComplete, &elUsbTxComplete, UsbTxComleteID); chEvtRegister(&esUsbReset, &elUsbReset, UsbResetID); chEvtRegister(&esUsbConfigured, &elUsbConfigured, UsbConfiguredID); // Wait for the USB system to be configured. and clear all other event flags. chEvtWaitOne(EVENT_MASK(UsbConfiguredID)); chEvtGetAndClearEvents(EVENT_MASK(UsbTxComleteID) | EVENT_MASK(UsbResetID)); while (TRUE) { chMBFetch (&usbTXMailbox, &msg, TIME_INFINITE); // Check if USB has been reconfigured while waiting for message from sysctrl activeEvents = chEvtGetAndClearEvents(EVENT_MASK(UsbConfiguredID)); if (activeEvents == EVENT_MASK(UsbConfiguredID)) { // If so, clear the reset event since it is no longer relevant. activeEvents = chEvtGetAndClearEvents(EVENT_MASK(UsbResetID)); } // Typecast Mailbox message to command package pointer for readability usbBufp = (usbPacket*)msg; // Prepare transmit and start the transmission. This operation will return immediately usbPrepareTransmit(usbp, EP_IN, usbBufp->packet, (size_t)usbBufp->size); chSysLock(); usbStartTransmitI(usbp, EP_IN); chSysUnlock(); //Check for events from the USB system. activeEvents = chEvtWaitAny(EVENT_MASK(UsbTxComleteID) | EVENT_MASK(UsbResetID)); if (activeEvents == EVENT_MASK(UsbResetID)) { chEvtWaitOne(EVENT_MASK(UsbConfiguredID)); // Clear any events that has occurred while the usb was not configured. chEvtGetAndClearEvents(EVENT_MASK(UsbTxComleteID) | EVENT_MASK(UsbResetID)); } usbFreeMailboxBuffer (usbBufp); } return 0; }
static msg_t tUsbRx(void *arg) { (void)arg; chRegSetThreadName("usbRx"); enum {UsbRxComleteID = 0, UsbResetID = 1, UsbConfiguredID = 2}; usbPacket *usbBufp; EventListener elUsbRxComplete; EventListener elUsbReset; EventListener elUsbConfigured; eventmask_t activeEvents; chEvtRegister(&esUsbRxComplete, &elUsbRxComplete, UsbRxComleteID); chEvtRegister(&esUsbReset, &elUsbReset, UsbResetID); chEvtRegister(&esUsbConfigured, &elUsbConfigured, UsbConfiguredID); // Wait for the USB system to be configured. chEvtWaitOne(EVENT_MASK(UsbConfiguredID)); chEvtGetAndClearEvents(EVENT_MASK(UsbRxComleteID) | EVENT_MASK(UsbResetID)); while (TRUE) { // Allocate buffer space for reception of package in the sysctrl mempool usbBufp = usbAllocMailboxBuffer(); // Prepare receive operation and initiate the usb system to listen usbPrepareReceive(usbp, EP_OUT, usbBufp->packet, 64); chSysLock(); usbStartReceiveI(usbp, EP_OUT); chSysUnlock(); // Wait for events from the USB system activeEvents = chEvtWaitAny(EVENT_MASK(UsbRxComleteID) | EVENT_MASK(UsbResetID)); if (activeEvents == EVENT_MASK(UsbResetID)) { // If the system was reset, clean up and wait for new configure. usbFreeMailboxBuffer (usbBufp); chEvtWaitOne(EVENT_MASK(UsbConfiguredID)); chEvtGetAndClearEvents(EVENT_MASK(UsbRxComleteID) | EVENT_MASK(UsbResetID)); } else { // Post pagckage to sysctrl if receive was successful usbBufp->size = ep2outstate.rxcnt; chMBPost (&usbRXMailbox, (msg_t)usbBufp, TIME_INFINITE); } } return 0; }
/** * @brief Thread for the flash save operation. * * @param[in] arg Unused. * @return Unused. */ static THD_FUNCTION(ThreadSysInfoFlashSave, arg) { (void)arg; /* Event registration for new estimation */ event_listener_t el; /* Set thread name */ chRegSetThreadName("SysInfo FlashSave"); /* Register to new estimation */ chEvtRegisterMask(ptrGetFlashSaveEventSource(), &el, FLASHSAVE_SAVE_EVENTMASK); while (1) { /* Wait for new estimation */ chEvtWaitOne(FLASHSAVE_SAVE_EVENTMASK); /* Save settings to flash */ FlashSave_Write(FlashSave_STR2ID("SIVN"), true, (uint8_t *)system_strings.vehicle_name, VEHICLE_NAME_SIZE); FlashSave_Write(FlashSave_STR2ID("SIVT"), true, (uint8_t *)system_strings.vehicle_type, VEHICLE_TYPE_SIZE); } }
void log_saver_thread(void *p) { chRegSetThreadName("logsaver"); char filename[16]; g_fileindex = next_free_filename(); chsnprintf(filename, sizeof(filename), "%04d.txt", g_fileindex); unsigned bytes_written; FIL file; f_open(&file, filename, FA_WRITE | FA_CREATE_NEW); static const char header[] = "# SysTime BattU BattI Tmotor Tmosfet RPM " "AccX AccY AccZ GyrX TotAcc MotorTgt IAccum Brake MaxDuty\r\n" "# ms mV mA mC mC " " mg mg mg dps mg mA mA \r\n"; f_write(&file, header, sizeof(header) - 1, &bytes_written); for (;;) { eventmask_t event = chEvtWaitOne(EVENT_BUF1 | EVENT_BUF2); if (event & EVENT_BUF1) { f_write(&file, g_logbuffer1, sizeof(g_logbuffer1), &bytes_written); } if (event & EVENT_BUF2) { f_write(&file, g_logbuffer2, sizeof(g_logbuffer2), &bytes_written); } f_sync(&file); } }
static THD_FUNCTION(ThreadTestEvents, arg) { (void)arg; event_listener_t el; eventmask_t events; int i = 0; chEvtRegisterMask( &hmc5983cfg.data_holder->es, &el, HMC5983_DATA_AVAILABLE_EVENTMASK); while(1) { events = chEvtWaitOne(HMC5983_DATA_AVAILABLE_EVENTMASK); if (events == HMC5983_DATA_AVAILABLE_EVENTMASK) { if (i++ > 10) { palTogglePad(GPIOC, GPIOC_LED_USR); i = 0; } } } return MSG_OK; }
/* * Application entry point. */ int main(void) { static const evhandler_t evhndl[] = { InsertHandler, RemoveHandler }; Thread *shelltp = NULL; struct EventListener el0, el1; /* * 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. */ halInit(); chSysInit(); /* * Activates the serial driver 2 using the driver default configuration. */ sdStart(&SD2, NULL); /* * Shell manager initialization. */ shellInit(); /* * Initializes the MMC driver to work with SPI2. */ palSetPadMode(IOPORT2, GPIOB_SPI2NSS, PAL_MODE_OUTPUT_PUSHPULL); palSetPad(IOPORT2, GPIOB_SPI2NSS); mmcObjectInit(&MMCD1, &SPID2, &ls_spicfg, &hs_spicfg, mmc_is_protected, mmc_is_inserted); mmcStart(&MMCD1, NULL); /* * Creates the blinker thread. */ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); /* * Normal main() thread activity, in this demo it does nothing except * sleeping in a loop and listen for events. */ chEvtRegister(&MMCD1.inserted_event, &el0, 0); chEvtRegister(&MMCD1.removed_event, &el1, 1); while (TRUE) { if (!shelltp) shelltp = shellCreate(&shell_cfg1, SHELL_WA_SIZE, NORMALPRIO); else if (chThdTerminated(shelltp)) { chThdRelease(shelltp); /* Recovers memory of the previous shell. */ shelltp = NULL; /* Triggers spawning of a new shell. */ } chEvtDispatch(evhndl, chEvtWaitOne(ALL_EVENTS)); } return 0; }
static void cmdIr(BaseSequentialStream *chp, int argc, char *argv[]) { (void)argv; (void)argc; chEvtRegisterMask(&esSensorEvents , &el, SENSOR_EVENT_IR_END); chprintf(chp, "Pinging with IR...\r\n"); chEvtBroadcastFlags(&esSensorEvents, SENSOR_EVENT_IR_START); chEvtWaitOne(SENSOR_EVENT_IR_END); chThdSleepMilliseconds(50); ir_index_t i; ir_angle_t angle; chSysLock(); for (i = 0; i < IR_RX_COUNT; i++) { angle = irDetections[i]; chprintf(chp, "IR Angle: %D\r\n", angle); } chSysUnlock(); chEvtUnregister(&esSensorEvents, &el); }
/* * Application entry point. */ int main(void) { static const evhandler_t evhndl[] = { TimerHandler, InsertHandler, RemoveHandler }; static EvTimer evt; struct EventListener 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. */ halInit(); chSysInit(); /* * Activates the serial driver 1 using the driver default configuration. */ sdStart(&SD1, NULL); /* * Buzzer driver initialization. */ buzzInit(); /* * Initializes the MMC driver to work with SPI2. */ mmcObjectInit(&MMCD1); mmcStart(&MMCD1, &mmccfg); /* * Activates the card insertion monitor. */ tmr_init(&MMCD1); /* * Creates the blinker threads. */ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); chThdCreateStatic(waThread2, sizeof(waThread2), NORMALPRIO, Thread2, NULL); /* * Normal main() thread activity, in this demo it does nothing except * sleeping in a loop and listen for events. */ evtInit(&evt, MS2ST(500)); /* Initializes an event timer object. */ evtStart(&evt); /* Starts the event timer. */ chEvtRegister(&evt.et_es, &el0, 0); /* Registers on the timer event source. */ chEvtRegister(&inserted_event, &el1, 1); chEvtRegister(&removed_event, &el2, 2); while (TRUE) chEvtDispatch(evhndl, chEvtWaitOne(ALL_EVENTS)); return 0; }
int main(void) { halInit(); chSysInit(); uint8_t i; event_listener_t tel; // Serial Port Setup sdStart(&SD1, NULL); palSetPadMode(GPIOC, 4, PAL_MODE_ALTERNATE(7)); palSetPadMode(GPIOC, 5, PAL_MODE_ALTERNATE(7)); chprintf((BaseSequentialStream*)&SD1, "Up and Running\n\r"); palSetPadMode(GPIOB, 3, PAL_MODE_ALTERNATE(6)); /* SCK. */ palSetPadMode(GPIOB, 4, PAL_MODE_ALTERNATE(6)); /* MISO.*/ palSetPadMode(GPIOB, 5, PAL_MODE_ALTERNATE(6)); /* MOSI.*/ palSetPadMode(GPIOC, GPIOC_PIN1, PAL_MODE_OUTPUT_PUSHPULL); palSetPad(GPIOC, GPIOC_PIN1); palSetPadMode(GPIOC, GPIOC_PIN2, PAL_MODE_OUTPUT_PUSHPULL); palClearPad(GPIOC, GPIOC_PIN2); palSetPadMode(GPIOC, GPIOC_PIN3, PAL_MODE_INPUT_PULLUP); spiStart(&SPID3, &nrf24l01SPI); chMtxObjectInit(&nrfMutex); //FROM RX--- extStart(&EXTD1, &extcfg); //--- nrf24l01ObjectInit(&nrf24l01); nrf24l01Start(&nrf24l01, &nrf24l01Config); //FROM RX --- extChannelEnable(&EXTD1, 3); //----- initNRF24L01(&nrf24l01); chprintf((BaseSequentialStream*)&SD1, "\n\rUp and Running\n\r"); shellInit(); chEvtRegister(&shell_terminated, &tel, 0); shelltp1 = shellCreate(&shell_cfg1, sizeof(waShell), NORMALPRIO); //FROM RX--- chThdCreateStatic(recieverWorkingArea, sizeof(recieverWorkingArea), NORMALPRIO, receiverThread, NULL); //FROM RX^^^^ /* for (i=0;i<32;i++) { serialOutBuf[i] = 3; } */ for (;;) { chEvtDispatch(fhandlers, chEvtWaitOne(ALL_EVENTS)); } }
/* * Application entry point. */ int main(void) { static const evhandler_t evhndl[] = { InsertHandler, RemoveHandler }; Thread *shelltp = NULL; struct EventListener el0, el1; /* * 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. */ halInit(); chSysInit(); /* * Activates the serial driver 1 and SDC driver 1 using default * configuration. */ sdStart(&SD1, NULL); sdcStart(&SDCD1, NULL); /* * Shell manager initialization. */ shellInit(); /* * Activates the card insertion monitor. */ tmr_init(&SDCD1); /* * Creates the blinker thread. */ chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); /* * Normal main() thread activity, in this demo it does nothing except * sleeping in a loop and listen for events. */ chEvtRegister(&inserted_event, &el0, 0); chEvtRegister(&removed_event, &el1, 1); while (TRUE) { if (!shelltp) shelltp = shellCreate(&shell_cfg1, SHELL_WA_SIZE, NORMALPRIO); else if (chThdTerminated(shelltp)) { chThdRelease(shelltp); /* Recovers memory of the previous shell. */ shelltp = NULL; /* Triggers spawning of a new shell. */ } chEvtDispatch(evhndl, chEvtWaitOne(ALL_EVENTS)); } }
/*------------------------------------------------------------------------* * Simulator main. * *------------------------------------------------------------------------*/ int main(void) { EventListener sd1fel, sd2fel, tel; /* * HAL initialization. */ halInit(); /* * ChibiOS/RT initialization. */ chSysInit(); /* * Serial ports (simulated) initialization. */ sdStart(&SD1, NULL); sdStart(&SD2, NULL); /* * Shell manager initialization. */ shellInit(); chEvtRegister(&shell_terminated, &tel, 0); /* * Console thread started. */ cdtp = chThdCreateFromHeap(NULL, CONSOLE_WA_SIZE, NORMALPRIO + 1, console_thread, NULL); /* * Initializing connection/disconnection events. */ cputs("Shell service started on SD1, SD2"); cputs(" - Listening for connections on SD1"); (void) sdGetAndClearFlags(&SD1); chEvtRegister(&SD1.sevent, &sd1fel, 1); cputs(" - Listening for connections on SD2"); (void) sdGetAndClearFlags(&SD2); chEvtRegister(&SD2.sevent, &sd2fel, 2); /* * Events servicing loop. */ while (!chThdShouldTerminate()) chEvtDispatch(fhandlers, chEvtWaitOne(ALL_EVENTS)); /* * Clean simulator exit. */ chEvtUnregister(&SD1.sevent, &sd1fel); chEvtUnregister(&SD2.sevent, &sd2fel); return 0; }
/*------------------------------------------------------------------------* * Simulator main. * *------------------------------------------------------------------------*/ int main(void) { EventListener sd1fel, sd2fel, tel; /* * 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. */ halInit(); chSysInit(); /* * Serial ports (simulated) initialization. */ sdStart(&SD1, NULL); sdStart(&SD2, NULL); /* * Shell manager initialization. */ shellInit(); chEvtRegister(&shell_terminated, &tel, 0); /* * Console thread started. */ cdtp = chThdCreateFromHeap(NULL, CONSOLE_WA_SIZE, NORMALPRIO + 1, console_thread, NULL); /* * Initializing connection/disconnection events. */ cputs("Shell service started on SD1, SD2"); cputs(" - Listening for connections on SD1"); (void) chIOGetAndClearFlags(&SD1); chEvtRegister(chIOGetEventSource(&SD1), &sd1fel, 1); cputs(" - Listening for connections on SD2"); (void) chIOGetAndClearFlags(&SD2); chEvtRegister(chIOGetEventSource(&SD2), &sd2fel, 2); /* * Events servicing loop. */ while (!chThdShouldTerminate()) chEvtDispatch(fhandlers, chEvtWaitOne(ALL_EVENTS)); /* * Clean simulator exit. */ chEvtUnregister(chIOGetEventSource(&SD1), &sd1fel); chEvtUnregister(chIOGetEventSource(&SD2), &sd2fel); return 0; }
// Wait for a push on one or several buttons specified by event mask. // Return the mask of the activated button(s). int wait_button(int bt_evt_msk) { int rslt_msk = check_button(bt_evt_msk); while (! rslt_msk) { chEvtWaitOne(bt_evt_msk); rslt_msk = check_button(bt_evt_msk); } return rslt_msk; }
/*------------------------------------------------------------------------* * Simulator main. * *------------------------------------------------------------------------*/ int main(void) { initTestStream(&testStream); /* * 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. */ halInit(); chSysInit(); /* * Serial ports (simulated) initialization. */ sdStart(&SD1, NULL); sdStart(&SD2, NULL); /* * Console thread started. */ cdtp = chThdCreateFromHeap(NULL, CONSOLE_WA_SIZE, NORMALPRIO + 1, console_thread, NULL); /* * Initializing connection/disconnection events. */ cputs("Shell service started on SD1, SD2"); cputs(" - Listening for connections on SD1"); chEvtRegister(chnGetEventSource(&SD1), &sd1fel, 1); cputs(" - Listening for connections on SD2"); chEvtRegister(chnGetEventSource(&SD2), &sd2fel, 2); rusEfiFunctionalTest(); /* * Events servicing loop. */ while (!chThdShouldTerminate()) { chEvtDispatch(fhandlers, chEvtWaitOne(ALL_EVENTS)); printPendingMessages(); chThdSleepMilliseconds(100); } /* * Clean simulator exit. */ chEvtUnregister(chnGetEventSource(&SD1), &sd1fel); chEvtUnregister(chnGetEventSource(&SD2), &sd2fel); return 0; }
int main(void) { static const evhandler_t evhndl[] = {InsertHandler, RemoveHandler}; struct EventListener el0, el1; // os init halInit(); chSysInit(); // setup LED pads palSetPadMode(GPIOD, 12, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_HIGHEST); palClearPad(GPIOD, 12); // green LED palSetPadMode(GPIOD, 15, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_HIGHEST); palClearPad(GPIOD, 15); // blue LED // setup pads to USART2 function (connect these pads through RS232 transceiver with PC, terminal emu needs 38400 baud) sdStart(&SD2, NULL); palSetPadMode(GPIOA, 2, PAL_MODE_ALTERNATE(7) | PAL_STM32_OSPEED_HIGHEST); // TX palSetPadMode(GPIOA, 3, PAL_MODE_ALTERNATE(7)); // RX // setup pads to SPI1 function (connect these pads to your SD card accordingly) palSetPadMode(GPIOC, 4, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_HIGHEST); // NSS palSetPadMode(GPIOA, 5, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_HIGHEST); // SCK palSetPadMode(GPIOA, 6, PAL_MODE_ALTERNATE(5)); // MISO palSetPadMode(GPIOA, 7, PAL_MODE_ALTERNATE(5) | PAL_STM32_OSPEED_HIGHEST); // MOSI palSetPad(GPIOC, 4); // set NSS high // initialize MMC driver mmcObjectInit(&MMCD1, &SPID1, &ls_spicfg, &hs_spicfg, mmc_is_protected, mmc_is_inserted); mmcStart(&MMCD1, NULL); // ChibiOS has no I2S support yet; // codec.c initializes everything necessary // except the I2S TX DMA interrupt vector (because it would // conflict with the ChibiOS kernel) // we can make ChibiOS call our own handler by letting // it create the DMA stream for SPI3 dmaStreamAllocate(SPID3.dmatx, STM32_SPI_SPI3_IRQ_PRIORITY, (stm32_dmaisr_t)I2SDmaTxInterrupt, &SPID3); // blink thread; also checks the user button chThdCreateStatic(waBlinkThread, sizeof(waBlinkThread), NORMALPRIO, BlinkThread, NULL); chEvtRegister(&MMCD1.inserted_event, &el0, 0); chEvtRegister(&MMCD1.removed_event, &el1, 1); while(TRUE) { chEvtDispatch(evhndl, chEvtWaitOne(ALL_EVENTS)); } }
static msg_t wavePlayerThread(void *arg) { (void)arg; chRegSetThreadName("blinker"); UINT temp, bufSwitch=1; codec_pwrCtl(1); codec_muteCtl(0); f_read(&f1, buf, PLAYBACK_BUFFER_SIZE, &temp); bytesToPlay-=temp; chEvtAddFlags(1); while(bytesToPlay) { chEvtWaitOne(1); if (bufSwitch) { codec_audio_send(buf, temp/2); spiAcquireBus(&SPID1); f_read(&f1, buf2, PLAYBACK_BUFFER_SIZE, &temp); spiReleaseBus(&SPID1); bufSwitch=0; } else { codec_audio_send(buf2, temp/2); spiAcquireBus(&SPID1); f_read(&f1, buf, PLAYBACK_BUFFER_SIZE, &temp); spiReleaseBus(&SPID1); bufSwitch=1; } bytesToPlay-=temp; if (chThdShouldTerminate()) break; } codec_muteCtl(1); codec_pwrCtl(0); playerThread=NULL; f_close(&f1); return 0; }
static msg_t batterySurveyThd(void *arg) { (void)arg; chRegSetThreadName ("battery survey"); chEvtRegister(&powerOutageSource, &powerOutageListener, 1); chThdSleepMilliseconds (2000); register_adc_watchdog((uint32_t) ADC1, 4, V_ALERT, 0xfff, &powerOutageIsr); chEvtWaitOne(EVENT_MASK(1)); chibios_logFinish (); chThdExit(0); return 0; }
static void logic_scanThread(void * data) { (void) data; chRegSetThreadName("logic scan"); EventListener el; chEvtRegister(&event_i2c_buttons, &el, BUTTON_EVENT_ID | BUTTON_NOW_EVENT_ID); eventmask_t mask; static uint32_t recevie = 0; while (TRUE) { mask = chEvtWaitOne(BUTTON_NOW_EVENT_ID | BUTTON_EVENT_ID); recevie++; logic_button(active.bank, &foot_switch, mask); } }
msg_t WebThread(void *p) { EvTimer evt1, evt2; EventListener el0, el1, el2; uip_ipaddr_t ipaddr; (void)p; /* * Event sources setup. */ chEvtRegister(macGetReceiveEventSource(ÐD1), &el0, FRAME_RECEIVED_ID); chEvtAddFlags(EVENT_MASK(FRAME_RECEIVED_ID)); /* In case some frames are already buffered */ evtInit(&evt1, MS2ST(500)); evtStart(&evt1); chEvtRegister(&evt1.et_es, &el1, PERIODIC_TIMER_ID); evtInit(&evt2, S2ST(10)); evtStart(&evt2); chEvtRegister(&evt2.et_es, &el2, ARP_TIMER_ID); /* * EMAC driver start. */ macStart(ÐD1, &mac_config); (void)macPollLinkStatus(ÐD1); /* * uIP initialization. */ uip_init(); uip_setethaddr(macaddr); uip_ipaddr(ipaddr, IPADDR0, IPADDR1, IPADDR2, IPADDR3); uip_sethostaddr(ipaddr); httpd_init(); while (TRUE) { chEvtDispatch(evhndl, chEvtWaitOne(ALL_EVENTS)); } return 0; }
static void evt2_execute(void) { eventmask_t m; event_listener_t el1, el2; systime_t target_time; /* * Test on chEvtWaitOne() without wait. */ chEvtAddEvents(5); m = chEvtWaitOne(ALL_EVENTS); test_assert(1, m == 1, "single event error"); m = chEvtWaitOne(ALL_EVENTS); test_assert(2, m == 4, "single event error"); m = chEvtGetAndClearEvents(ALL_EVENTS); test_assert(3, m == 0, "stuck event"); /* * Test on chEvtWaitOne() with wait. */ test_wait_tick(); target_time = chVTGetSystemTime() + MS2ST(50); threads[0] = chThdCreateStatic(wa[0], WA_SIZE, chThdGetPriorityX() - 1, thread1, chThdGetSelfX()); m = chEvtWaitOne(ALL_EVENTS); test_assert_time_window(4, target_time, target_time + ALLOWED_DELAY); test_assert(5, m == 1, "single event error"); m = chEvtGetAndClearEvents(ALL_EVENTS); test_assert(6, m == 0, "stuck event"); test_wait_threads(); /* * Test on chEvtWaitAny() without wait. */ chEvtAddEvents(5); m = chEvtWaitAny(ALL_EVENTS); test_assert(7, m == 5, "unexpected pending bit"); m = chEvtGetAndClearEvents(ALL_EVENTS); test_assert(8, m == 0, "stuck event"); /* * Test on chEvtWaitAny() with wait. */ test_wait_tick(); target_time = chVTGetSystemTime() + MS2ST(50); threads[0] = chThdCreateStatic(wa[0], WA_SIZE, chThdGetPriorityX() - 1, thread1, chThdGetSelfX()); m = chEvtWaitAny(ALL_EVENTS); test_assert_time_window(9, target_time, target_time + ALLOWED_DELAY); test_assert(10, m == 1, "single event error"); m = chEvtGetAndClearEvents(ALL_EVENTS); test_assert(11, m == 0, "stuck event"); test_wait_threads(); /* * Test on chEvtWaitAll(). */ chEvtObjectInit(&es1); chEvtObjectInit(&es2); chEvtRegisterMask(&es1, &el1, 1); chEvtRegisterMask(&es2, &el2, 4); test_wait_tick(); target_time = chVTGetSystemTime() + MS2ST(50); threads[0] = chThdCreateStatic(wa[0], WA_SIZE, chThdGetPriorityX() - 1, thread2, "A"); m = chEvtWaitAll(5); test_assert_time_window(12, target_time, target_time + ALLOWED_DELAY); m = chEvtGetAndClearEvents(ALL_EVENTS); test_assert(13, m == 0, "stuck event"); test_wait_threads(); chEvtUnregister(&es1, &el1); chEvtUnregister(&es2, &el2); test_assert(14, !chEvtIsListeningI(&es1), "stuck listener"); test_assert(15, !chEvtIsListeningI(&es2), "stuck listener"); }
int main(void) { uint32_t i; FRDMSlave * slave; FRDMSlave::data_t * data_iter; FRDMSlave::data_t * buffer_end; halInit(); chSysInit(); main_thread = chThdSelf(); Platform::early_init(); for(slave = slaves; slave < slaves + num_slaves; slave++){ slave->init(); } timer_config(); button.set_press_handler((button_t::button_handler)button_cb, nullptr); term.launch(); while(true){ // Wait for a button press chEvtGetAndClearEvents(ALL_EVENTS); chEvtWaitOne(MAIN_SIG_BUTTON); /* for(slave = slaves; slave < slaves + num_slaves; slave++){ if(!slave->ping()) break; } if(slave != slaves + num_slaves){ // One of the slaves didn't respond... for(i = 0; i < 10; i++){ chThdSleep(MS2ST(100)); leds[0].toggle(); leds[1].toggle(); leds[2].toggle(); leds[3].toggle(); } } */ // Flash lights 3 times for(i = 0; i < 5; i++){ chThdSleep(MS2ST(500)); leds[0].toggle(); leds[1].toggle(); leds[2].toggle(); leds[3].toggle(); } // Let the last one go for a whole second chThdSleep(MS2ST(500)); leds[0].clear(); leds[1].clear(); leds[2].clear(); leds[3].clear(); // Trigger first sample sync_pin.toggle(); timer_start(); chEvtGetAndClearEvents(MAIN_SIG_BUTTON); sample_count = 0; data_iter = acc_buffer1; buffer_end = acc_buffer1 + sizeof(acc_buffer1)/sizeof(*acc_buffer1); while(!chEvtWaitOneTimeout(MAIN_SIG_BUTTON, TIME_IMMEDIATE)){ chEvtWaitOne(MAIN_SIG_TICK); for(slave = slaves; slave < slaves + num_slaves; slave++){ slave->start_read(); } for(slave = slaves; slave < slaves + num_slaves; slave++){ slave->finish_read(data_iter); data_iter += FRDMSlave::samples_per_transfer; sample_count += FRDMSlave::samples_per_transfer; if(data_iter == acc_buffer1 + (sizeof(acc_buffer1)/sizeof(*acc_buffer1))){ data_iter = acc_buffer2; } else if(data_iter == acc_buffer2 + (sizeof(acc_buffer2)/sizeof(*acc_buffer2))){ break; } } if(slave != slaves + num_slaves){ break; } // Trigger next sample sync_pin.toggle(); #if 0 for(slave = slaves; slave < slaves + num_slaves; slave++){ slave->finish_read(data_iter); data_iter += FRDMSlave::samples_per_transfer; if((uint32_t)(buffer_end - data_iter) < FRDMSlave::samples_per_transfer * sizeof(FRDMSlave::data_t)){ if(data_iter < acc_buffer1){ // Buffer is full! break; } data_iter = acc_buffer2; buffer_end = acc_buffer2 + sizeof(acc_buffer2)/sizeof(*acc_buffer2); } } if(slave != slaves + num_slaves){ for(i = 0; i < 10; i++){ chThdSleep(MS2ST(100)); leds[0].toggle(); leds[1].toggle(); leds[2].toggle(); leds[3].toggle(); } break; } #endif leds[0].toggle(); } timer_stop(); for(slave = slaves; slave < slaves + num_slaves; slave++){ slave->stop(); } leds[0].clear(); leds[1].clear(); leds[2].clear(); leds[3].clear(); chThdSleep(MS2ST(250)); } }
eventmask_t BaseThread::waitOneEvent(eventmask_t ewmask) { return chEvtWaitOne(ewmask); }
int main(void) { // copy vector table memcpy((char *)0x20000000, (const char *)&_vectors, 0x200); // remap SRAM1 to 0x00000000 SYSCFG->MEMRMP |= 0x03; /* system & hardware initialization */ halInit(); // float usb inputs, hope the host notices detach... palSetPadMode(GPIOA, 11, PAL_MODE_INPUT); palSetPadMode(GPIOA, 12, PAL_MODE_INPUT); // setup LEDs palSetPadMode(LED1_PORT,LED1_PIN,PAL_MODE_OUTPUT_PUSHPULL); palSetPadMode(LED2_PORT, LED2_PIN, PAL_MODE_OUTPUT_PUSHPULL); palClearPad(LED1_PORT,LED1_PIN); palSetPad(LED2_PORT,LED2_PIN); chSysInit(); palSetPadMode(GPIOA, 11, PAL_MODE_ALTERNATE(10)); palSetPadMode(GPIOA, 12, PAL_MODE_ALTERNATE(10)); palSetPadMode(GPIOC, 8, PAL_MODE_ALTERNATE(12) | PAL_STM32_OSPEED_HIGHEST); palSetPadMode(GPIOC, 9, PAL_MODE_ALTERNATE(12) | PAL_STM32_OSPEED_HIGHEST); palSetPadMode(GPIOC, 10, PAL_MODE_ALTERNATE(12) | PAL_STM32_OSPEED_HIGHEST); palSetPadMode(GPIOC, 11, PAL_MODE_ALTERNATE(12) | PAL_STM32_OSPEED_HIGHEST); palSetPadMode(GPIOC, 12, PAL_MODE_ALTERNATE(12) | PAL_STM32_OSPEED_HIGHEST); palSetPadMode(GPIOD, 2, PAL_MODE_ALTERNATE(12) | PAL_STM32_OSPEED_HIGHEST); chThdSleepMilliseconds(50); /* initialize the SD card */ sdcStart(&SDCD1, NULL); sdcConnect(&SDCD1); /* initialize the USB mass storage driver */ msdInit(&UMSD1); /* turn off green LED, turn on red LED */ palSetPadMode(LED1_PORT, LED1_PIN, PAL_MODE_OUTPUT_PUSHPULL); palClearPad(LED1_PORT, LED1_PIN); palSetPadMode(LED2_PORT, LED2_PIN, PAL_MODE_OUTPUT_PUSHPULL); palSetPad(LED2_PORT, LED2_PIN); /* start the USB mass storage service */ int ret = msdStart(&UMSD1, &msdConfig); if (ret != 0) { /* no media found : bye bye !*/ usbDisconnectBus(&USBD1); chThdSleepMilliseconds(1000); NVIC_SystemReset(); } /* watch the mass storage events */ EventListener connected; EventListener ejected; chEvtRegisterMask(&UMSD1.evt_connected, &connected, EVENT_MASK(1)); chEvtRegisterMask(&UMSD1.evt_ejected, &ejected, EVENT_MASK(2)); /* start the USB driver */ usbDisconnectBus(&USBD1); chThdSleepMilliseconds(1000); usbStart(&USBD1, &usbConfig); usbConnectBus(&USBD1); while (TRUE) { eventmask_t event = chEvtWaitOne(EVENT_MASK(1) | EVENT_MASK(2)); if (event == EVENT_MASK(1)) { /* media connected */ } else if (event == EVENT_MASK(2)) { /* media ejected : bye bye !*/ usbDisconnectBus(&USBD1); chThdSleepMilliseconds(1000); NVIC_SystemReset(); } } return 0; }
static msg_t thdUsbStorage(void *arg) { (void) arg; // unused chRegSetThreadName("UsbStorage:polling"); uint antiBounce=5; EventListener connected; // Should use EXTI interrupt instead of active polling, // but in the chibios_opencm3 implementation, since EXTI is // used via libopencm3, ISR are routed on pprz/opencm3 and cannot // be used concurrently by chibios api // Should be fixed when using chibios-rt branch while (!chThdShouldTerminate() && antiBounce) { const bool_t usbConnected = palReadPad (GPIOA, GPIOA_OTG_FS_VBUS); if (usbConnected) antiBounce--; else antiBounce=5; chThdSleepMilliseconds(20); } isRunning = true; chRegSetThreadName("UsbStorage:connected"); /* Stop the logs*/ chibios_logFinish (true); /* connect sdcard sdc interface sdio */ if (sdioConnect () == false) chThdExit (RDY_TIMEOUT); /* initialize the USB mass storage driver */ msdInit(&UMSD1); /* start the USB mass storage service */ msdStart(&UMSD1, &msdConfig); /* start the USB driver */ usbDisconnectBus(&USBD1); chThdSleepMilliseconds(1000); usbStart(&USBD1, &usbConfig); usbConnectBus(&USBD1); /* wait for a real usb storage connexion before shutting down autopilot */ chEvtRegisterMask(&UMSD1.evt_connected, &connected, EVENT_MASK(1)); chEvtWaitOne(EVENT_MASK(1)); /* stop autopilot */ if (pprzThdPtr != NULL) { chThdTerminate (pprzThdPtr); chThdWait (pprzThdPtr); pprzThdPtr = NULL; } /* wait until usb-storage is unmount and usb cable is unplugged*/ while (!chThdShouldTerminate() && palReadPad (GPIOA, GPIOA_OTG_FS_VBUS)) { chThdSleepMilliseconds(10); } /* then close open descriptors and reboot autopilot */ usbDisconnectBus(&USBD1); chThdSleepMilliseconds(500); msdStop(&UMSD1); sdioDisconnect (); MCU_RESTART(); return RDY_OK; }
eventmask_t Event::WaitOne(eventmask_t ewmask) { return chEvtWaitOne(ewmask); }