static void thd4_execute(void) { systime_t time; test_wait_tick(); /* Timeouts in microseconds.*/ time = chTimeNow(); chThdSleepMicroseconds(100000); test_assert_time_window(1, time + US2ST(100000), time + US2ST(100000) + 1); /* Timeouts in milliseconds.*/ time = chTimeNow(); chThdSleepMilliseconds(100); test_assert_time_window(2, time + MS2ST(100), time + MS2ST(100) + 1); /* Timeouts in seconds.*/ time = chTimeNow(); chThdSleepSeconds(1); test_assert_time_window(3, time + S2ST(1), time + S2ST(1) + 1); /* Absolute timelines.*/ time = chTimeNow() + MS2ST(100); chThdSleepUntil(time); test_assert_time_window(4, time, time + 1); }
static void test_001_002_execute(void) { systime_t time; /* The current system time is read then a sleep is performed for 100 system ticks and on exit the system time is verified again.*/ test_set_step(1); { time = chVTGetSystemTimeX(); chThdSleep(100); test_assert_time_window(time + 100, time + 100 + 1, "out of time window"); } /* The current system time is read then a sleep is performed for 100000 microseconds and on exit the system time is verified again.*/ test_set_step(2); { time = chVTGetSystemTimeX(); chThdSleepMicroseconds(100); test_assert_time_window(time + US2ST(100), time + US2ST(100) + 1, "out of time window"); } /* The current system time is read then a sleep is performed for 100 milliseconds and on exit the system time is verified again.*/ test_set_step(3); { time = chVTGetSystemTimeX(); chThdSleepMilliseconds(100); test_assert_time_window(time + MS2ST(100), time + MS2ST(100) + 1, "out of time window"); } /* The current system time is read then a sleep is performed for 1 second and on exit the system time is verified again.*/ test_set_step(4); { time = chVTGetSystemTimeX(); chThdSleepSeconds(1); test_assert_time_window(time + S2ST(1), time + S2ST(1) + 1, "out of time window"); } test_set_step(5); { time = chVTGetSystemTimeX(); chThdSleepUntil(time + 100); test_assert_time_window(time + 100, time + 100 + 1, "out of time window"); } }
/* * Application entry point. */ int main(void) { msg_t status = RDY_TIMEOUT; halInit(); chSysInit(); chBSemInit(&alarm_sem, TRUE); rtcGetTime(&RTCD1, ×pec); alarmspec.tv_sec = timespec.tv_sec + RTC_ALARMPERIOD; rtcSetAlarm(&RTCD1, 0, &alarmspec); rtcSetCallback(&RTCD1, rtc_cb); while (TRUE) { /* Wait until alarm callback signaled semaphore.*/ status = chBSemWaitTimeout(&alarm_sem, S2ST(RTC_ALARMPERIOD + 10)); if (status == RDY_TIMEOUT) { chSysHalt(); } else { rtcGetTime(&RTCD1, ×pec); alarmspec.tv_sec = timespec.tv_sec + RTC_ALARMPERIOD; rtcSetAlarm(&RTCD1, 0, &alarmspec); } } return 0; }
void app_cfg_idle() { if ((chTimeNow() - last_idle) > S2ST(2)) { app_cfg_flush(); last_idle = chTimeNow(); } }
RadInputValue inputEncoderProcessor(RadInputState* state) { RadInputValue v; v.encoder.delta = state->encoder.value - state->encoder.last_value; state->encoder.last_value = state->encoder.value; v.encoder.rate = (float)v.encoder.delta / (chTimeNow() - state->encoder.last_time) * S2ST(1); state->encoder.last_time = chTimeNow(); return v; }
/** Clear unhealthy flags after some time, so we eventually retry those sats in case they recover from their sickness. Call this function regularly, and once per day it will reset the flags. */ static void check_clear_unhealthy(void) { static systime_t ticks; if (chVTTimeElapsedSinceX(ticks) < S2ST(24*60*60)) return; ticks = chVTGetSystemTime(); for (u32 i=0; i<PLATFORM_SIGNAL_COUNT; i++) { if (acq_status[i].state == ACQ_PRN_UNHEALTHY) acq_status[i].state = ACQ_PRN_ACQUIRING; } }
/** Clear unhealthy flags after some time. Flags are reset one per day. */ static void check_clear_unhealthy(void) { static systime_t ticks; if (chTimeElapsedSince(ticks) < S2ST(24*60*60)) return; ticks = chTimeNow(); for (u8 prn=0; prn<32; prn++) { if (acq_prn_param[prn].state == ACQ_PRN_UNHEALTHY) acq_prn_param[prn].state = ACQ_PRN_ACQUIRING; } }
THD_FUNCTION(moduleSAT, arg) { // Print infos module_params_t* parm = (module_params_t*)arg; TRACE_INFO("SAT > Startup module SATELLITE"); TRACE_MODULE_INFO(parm, "SAT", "SATELLITE"); systime_t time = chVTGetSystemTimeX(); while(true) { TRACE_INFO("SAT > Do module SATELLITE cycle"); TRACE_WARN("SAT > Module SATELLITE not implemented"); // FIXME time = chThdSleepUntilWindowed(time, time + S2ST(parm->cycle)); // Wait until time + cycletime } }
int main(void) { halInit(); chSysInit(); debugInit(); PRINT("Main is running",0); chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); mp45dt02Init(); while(1) { chThdSleep(S2ST(1)); } }
int main(void) { mcu_init(); downlink_init(); /* * Creates the blinker thread. */ chThdCreateStatic(waThdBlinker, sizeof(waThdBlinker), NORMALPRIO, ThdBlinker, NULL); chThdCreateStatic(waThdTx, sizeof(waThdTx), NORMALPRIO, ThdTx, NULL); while (TRUE) { chThdSleep(S2ST(1)); } return 0; }
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 msg_t screen_saver_thread(void* arg) { (void)arg; chRegSetThreadName("screen_saver"); while (1) { screen_saver_t* s = arg; quantity_t screen_saver_timeout = app_cfg_get_screen_saver(); if (!s->active && screen_saver_timeout.value > .5f && (chTimeNow() - s->last_touch_time) > S2ST(60 * screen_saver_timeout.value)) { s->active = true; active = true; lcd_set_brightness(0); gui_push_screen(s->screen); } chThdSleepSeconds(10); } return 0; }
void App_t::ITask() { while(true) { chThdSleepMilliseconds(999); // uint32_t EvtMsk = chEvtWaitAny(ALL_EVENTS); #if 0 // ==== Buttons ==== if(EvtMsk & EVTMSK_BUTTONS) { BtnEvtInfo_t EInfo; while(ButtonEvtBuf.Get(&EInfo) == OK) { // Uart.Printf("\rEinfo: %u, %u, %A", EInfo.Type, EInfo.BtnCnt, EInfo.BtnID, EInfo.BtnCnt, '-'); Beeper.StartSequence(bsqButton); // Switch backlight on Lcd.Backlight(81); chVTRestart(&ITmrBacklight, MS2ST(4500), EVTMSK_BCKLT_OFF); // Process buttons switch(EInfo.BtnID[0]) { case btnLTop: // Iterate IDs if(Settings.ID < ID_MAX) Settings.ID++; else Settings.ID = ID_MIN; SettingsHasChanged = true; SaveSettings(); Interface.ShowID(); break; case btnLBottom: // Deadtime on/off Settings.DeadtimeEnabled = !Settings.DeadtimeEnabled; SettingsHasChanged = true; SaveSettings(); Interface.ShowDeadtimeSettings(); break; case btnRTop: if(Settings.DurationActive_s < DURATION_ACTIVE_MAX_S) { Settings.DurationActive_s += 10; SettingsHasChanged = true; SaveSettings(); Interface.ShowDurationActive(); } break; case btnRBottom: if(Settings.DurationActive_s > DURATION_ACTIVE_MIN_S) { Settings.DurationActive_s -= 10; SettingsHasChanged = true; SaveSettings(); Interface.ShowDurationActive(); } break; default: break; } // switch } // while get } // if buttons #endif #if 0 // ==== Radio ==== if(EvtMsk & EVTMSK_RADIO_RX) { // Uart.Printf("\rRadioRx"); RadioIsOn = true; Interface.ShowRadio(); chVTRestart(&ITmrRadioTimeout, S2ST(RADIO_NOPKT_TIMEOUT_S), EVTMSK_RADIO_ON_TIMEOUT); IProcessLedLogic(); // Radio RX disables Deadtime if it is on DeadTimeIsNow = false; chVTReset(&ITmrDeadTime); } if(EvtMsk & EVTMSK_RADIO_ON_TIMEOUT) { // Uart.Printf("\rRadioTimeout"); RadioIsOn = false; Interface.ShowRadio(); IProcessLedLogic(); } #endif #if 0 // ==== Saving settings ==== if(EvtMsk & EVTMSK_SAVE) { ISaveSettingsReally(); } #endif } // while true }
void App_t::SaveSettings() { chSysLock(); if(chVTIsArmedI(&ITmrSaving)) chVTResetI(&ITmrSaving); // Reset timer chVTSetEvtI(&ITmrSaving, S2ST(4), EVTMSK_SAVE); chSysUnlock(); }
/** * Tracking Module (Thread) */ THD_FUNCTION(moduleTRACKING, arg) { // Print infos module_params_t* parm = (module_params_t*)arg; TRACE_INFO("TRAC > Startup module TRACKING MANAGER"); TRACE_INFO("TRAC > Module TRACKING MANAGER info\r\n" "%s Cycle: %d sec", TRACE_TAB, parm->cycle ); uint32_t id = 1; lastTrackPoint = &trackPoints[0]; systime_t time = chVTGetSystemTimeX(); while(true) { parm->lastCycle = chVTGetSystemTimeX(); // Watchdog timer TRACE_INFO("TRAC > Do module TRACKING MANAGER cycle"); trackPoint_t* tp = &trackPoints[id % (sizeof(trackPoints) / sizeof(trackPoint_t))]; // Current track point trackPoint_t* ltp = &trackPoints[(id-1) % (sizeof(trackPoints) / sizeof(trackPoint_t))]; // Last track point // Search for GPS satellites gpsFix_t gpsFix; GPS_Init(); do { gps_get_fix(&gpsFix); } while(!isGPSLocked(&gpsFix) && chVTGetSystemTimeX() <= time + S2ST(parm->cycle-3)); // Do as long no GPS lock and within timeout, timeout=cycle-1sec (-1sec in order to keep synchronization) if(isGPSLocked(&gpsFix)) { // GPS locked // Switch off GPS GPS_Deinit(); // Debug TRACE_INFO("TRAC > GPS sampling finished GPS LOCK"); TRACE_GPSFIX(&gpsFix); // Calibrate RTC setTime(gpsFix.time); // Take time from GPS tp->time.year = gpsFix.time.year; tp->time.month = gpsFix.time.month; tp->time.day = gpsFix.time.day; tp->time.hour = gpsFix.time.hour; tp->time.minute = gpsFix.time.minute; tp->time.second = gpsFix.time.second; // Set new GPS fix tp->gps_lat = gpsFix.lat; tp->gps_lon = gpsFix.lon; tp->gps_alt = gpsFix.alt; tp->gps_lock = isGPSLocked(&gpsFix); tp->gps_sats = gpsFix.num_svs; } else { // GPS lost (keep GPS switched on) // Debug TRACE_WARN("TRAC > GPS sampling finished GPS LOSS"); // Take time from internal RTC ptime_t time; getTime(&time); tp->time.year = time.year; tp->time.month = time.month; tp->time.day = time.day; tp->time.hour = time.hour; tp->time.minute = time.minute; tp->time.second = time.second; // Take GPS fix from old lock tp->gps_lat = ltp->gps_lat; tp->gps_lon = ltp->gps_lon; tp->gps_alt = ltp->gps_alt; // Mark gpsloss tp->gps_lock = false; tp->gps_sats = 0; } tp->id = id; // Serial ID tp->gps_ttff = ST2S(chVTGetSystemTimeX() - time); // Time to first fix // Power management tp->adc_solar = getSolarVoltageMV(); tp->adc_battery = getBatteryVoltageMV(); tp->adc_charge = pac1720_getAverageChargePower(); tp->adc_discharge = pac1720_getAverageDischargePower(); bme280_t bmeInt; bme280_t bmeExt; // Atmosphere condition if(BME280_isAvailable(BME280_ADDRESS_INT)) { BME280_Init(&bmeInt, BME280_ADDRESS_INT); tp->air_press = BME280_getPressure(&bmeInt, 256); tp->air_hum = BME280_getHumidity(&bmeInt); tp->air_temp = BME280_getTemperature(&bmeInt); } else { // No internal BME280 found TRACE_ERROR("TRAC > Internal BME280 not available"); tp->air_press = 0; tp->air_hum = 0; tp->air_temp = 0; } // Balloon condition if(BME280_isAvailable(BME280_ADDRESS_EXT)) { BME280_Init(&bmeExt, BME280_ADDRESS_EXT); tp->bal_press = BME280_getPressure(&bmeExt, 256); tp->bal_hum = BME280_getHumidity(&bmeExt); tp->bal_temp = BME280_getTemperature(&bmeExt); } else { // No external BME280 found TRACE_WARN("TRAC > External BME280 not available"); tp->bal_press = 0; tp->bal_hum = 0; tp->bal_temp = 0; } // Trace data TRACE_INFO( "TRAC > New tracking point available (ID=%d)\r\n" "%s Time %04d-%02d-%02d %02d:%02d:%02d\r\n" "%s Pos %d.%07d %d.%07d Alt %dm\r\n" "%s Sats %d TTFF %dsec\r\n" "%s ADC Vbat=%d.%03dV Vsol=%d.%03dV Pin=%dmW Pout=%dmW\r\n" "%s Air p=%6d.%01dPa T=%2d.%02ddegC phi=%2d.%01d%%\r\n" "%s Ball p=%6d.%01dPa T=%2d.%02ddegC phi=%2d.%01d%%", tp->id, TRACE_TAB, tp->time.year, tp->time.month, tp->time.day, tp->time.hour, tp->time.minute, tp->time.day, TRACE_TAB, tp->gps_lat/10000000, tp->gps_lat%10000000, tp->gps_lon/10000000, tp->gps_lon%10000000, tp->gps_alt, TRACE_TAB, tp->gps_sats, tp->gps_ttff, TRACE_TAB, tp->adc_battery/1000, (tp->adc_battery%1000), tp->adc_solar/1000, (tp->adc_solar%1000), tp->adc_charge, tp->adc_discharge, TRACE_TAB, tp->air_press/10, tp->air_press%10, tp->air_temp/100, tp->air_temp%100, tp->air_hum/10, tp->air_hum%10, TRACE_TAB, tp->bal_press/10, tp->bal_press%10, tp->bal_temp/100, tp->bal_temp%100, tp->bal_hum/10, tp->bal_hum%10 ); // Switch last recent track point lastTrackPoint = tp; id++; time = chThdSleepUntilWindowed(time, time + S2ST(parm->cycle)); // Wait until time + cycletime } }
/** * @brief LWIP handling thread. * * @param[in] p pointer to a @p lwipthread_opts structure or @p NULL * @return The function does not return. */ msg_t lwip_thread(void *p) { EvTimer evt; EventListener el0, el1; struct ip_addr ip, gateway, netmask; static struct netif thisif; static const MACConfig mac_config = {thisif.hwaddr}; chRegSetThreadName("lwipthread"); /* Initializes the thing.*/ sys_init(); mem_init(); memp_init(); pbuf_init(); netif_init(); ip_init(); tcpip_init(NULL, NULL); /* TCP/IP parameters, runtime or compile time.*/ if (p) { struct lwipthread_opts *opts = p; unsigned i; for (i = 0; i < 6; i++) thisif.hwaddr[i] = opts->macaddress[i]; ip.addr = opts->address; gateway.addr = opts->gateway; netmask.addr = opts->netmask; } else { thisif.hwaddr[0] = LWIP_ETHADDR_0; thisif.hwaddr[1] = LWIP_ETHADDR_1; thisif.hwaddr[2] = LWIP_ETHADDR_2; thisif.hwaddr[3] = LWIP_ETHADDR_3; thisif.hwaddr[4] = LWIP_ETHADDR_4; thisif.hwaddr[5] = LWIP_ETHADDR_5; LWIP_IPADDR(&ip); LWIP_GATEWAY(&gateway); LWIP_NETMASK(&netmask); } macStart(ÐD1, &mac_config); netif_add(&thisif, &ip, &netmask, &gateway, NULL, ethernetif_init, tcpip_input); netif_set_default(&thisif); netif_set_up(&thisif); /* Setup event sources.*/ evtInit(&evt, S2ST(5)); evtStart(&evt); chEvtRegisterMask(&evt.et_es, &el0, PERIODIC_TIMER_ID); chEvtRegisterMask(macGetReceiveEventSource(ÐD1), &el1, FRAME_RECEIVED_ID); chEvtAddEvents(PERIODIC_TIMER_ID | FRAME_RECEIVED_ID); /* Goes to the final priority after initialization.*/ chThdSetPriority(LWIP_THREAD_PRIORITY); while (TRUE) { eventmask_t mask = chEvtWaitAny(ALL_EVENTS); if (mask & PERIODIC_TIMER_ID) { bool_t current_link_status = macPollLinkStatus(ÐD1); if (current_link_status != netif_is_link_up(&thisif)) { if (current_link_status) tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_up, &thisif, 0); else tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_down, &thisif, 0); } } if (mask & FRAME_RECEIVED_ID) { struct pbuf *p; while ((p = low_level_input(&thisif)) != NULL) { struct eth_hdr *ethhdr = p->payload; switch (htons(ethhdr->type)) { /* IP or ARP packet? */ case ETHTYPE_IP: case ETHTYPE_ARP: #if PPPOE_SUPPORT /* PPPoE packet? */ case ETHTYPE_PPPOEDISC: case ETHTYPE_PPPOE: #endif /* PPPOE_SUPPORT */ /* full packet send to tcpip_thread to process */ if (thisif.input(p, &thisif) == ERR_OK) break; LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n")); default: pbuf_free(p); } } } } return 0; }
static void test_002_001_execute(void) { systime_t time; /* [2.1.1] The current system time is read then a sleep is performed for 100 system ticks and on exit the system time is verified again.*/ test_set_step(1); { time = chVTGetSystemTimeX(); chThdSleep(100); test_assert_time_window(time + 100, time + 100 + CH_CFG_ST_TIMEDELTA + 1, "out of time window"); } /* [2.1.2] The current system time is read then a sleep is performed for 100000 microseconds and on exit the system time is verified again.*/ test_set_step(2); { time = chVTGetSystemTimeX(); chThdSleepMicroseconds(100000); test_assert_time_window(time + US2ST(100000), time + US2ST(100000) + CH_CFG_ST_TIMEDELTA + 1, "out of time window"); } /* [2.1.3] The current system time is read then a sleep is performed for 100 milliseconds and on exit the system time is verified again.*/ test_set_step(3); { time = chVTGetSystemTimeX(); chThdSleepMilliseconds(100); test_assert_time_window(time + MS2ST(100), time + MS2ST(100) + CH_CFG_ST_TIMEDELTA + 1, "out of time window"); } /* [2.1.4] The current system time is read then a sleep is performed for 1 second and on exit the system time is verified again.*/ test_set_step(4); { time = chVTGetSystemTimeX(); chThdSleepSeconds(1); test_assert_time_window(time + S2ST(1), time + S2ST(1) + CH_CFG_ST_TIMEDELTA + 1, "out of time window"); } /* [2.1.5] Function chThdSleepUntil() is tested with a timeline of "now" + 100 ticks.*/ test_set_step(5); { time = chVTGetSystemTimeX(); chThdSleepUntil(time + 100); test_assert_time_window(time + 100, time + 100 + CH_CFG_ST_TIMEDELTA + 1, "out of time window"); } }