static void roStartListeningTimerCb(void *x) { // PRINTF("%lu: (%c) LISTEN START\n", getSyncTimeMs(), isRoutingInfoValid() ? '+' : '-'); if (!isListening) { TPRINTF("+++ start\n"); } alarmSchedule(&roStartListeningTimer, timeToNextFrame() + 2000); radioOn(); isListening = true; alarmSchedule(&roStopListeningTimer, 2000 + 2 * MOTE_TIME_FULL * MAX_MOTES); }
// Application entry point void appMain(void) { // Initialize and start timer #1 (after 700 milliseconds) alarmInit(&timer1, onTimer1, NULL); alarmSchedule(&timer1, 700); // Initialize and start timer #2 (after 1000 milliseconds) alarmInit(&timer2, onTimer2, NULL); alarmSchedule(&timer2, 1000); // Enter infinite low-power loop for (;;) { msleep(2000); } }
void amb8420Init(void) { RPRINTF("amb8420Init...\n"); amb8420InitSerial(); pinAsOutput(AMB8420_RESET_PORT, AMB8420_RESET_PIN); pinAsOutput(AMB8420_CONFIG_PORT, AMB8420_CONFIG_PIN); pinAsOutput(AMB8420_SLEEP_PORT, AMB8420_SLEEP_PIN); pinAsOutput(AMB8420_TRX_DISABLE_PORT, AMB8420_TRX_DISABLE_PIN); // pinAsOutput(AMB8420_DATA_REQUEST_PORT, AMB8420_DATA_REQUEST_PIN); pinAsInput(AMB8420_RTS_PORT, AMB8420_RTS_PIN); // pinAsInput(AMB8420_DATA_INDICATE_PORT, AMB8420_DATA_INDICATE_PIN); pinSet(AMB8420_CONFIG_PORT, AMB8420_CONFIG_PIN); // pinClear(AMB8420_DATA_REQUEST_PORT, AMB8420_DATA_REQUEST_PIN); // in case interrupts are used (for non-command mode): //pinIntRising(AMB8420_DATA_INDICATE_PORT, AMB8420_DATA_INDICATE_PIN); //pinEnableInt(AMB8420_DATA_INDICATE_PORT, AMB8420_DATA_INDICATE_PIN); initResult = amb8420Reset(); // make sure low power mode is enabled pinSet(AMB8420_TRX_DISABLE_PORT, AMB8420_TRX_DISABLE_PIN); pinSet(AMB8420_SLEEP_PORT, AMB8420_SLEEP_PIN); alarmInit(&radioResetTimer, onRadioReset, NULL); alarmSchedule(&radioResetTimer, RADIO_RESET_INTERVAL); RPRINTF("..done\n"); }
void onTimer(void *x) { TPRINTF("onTimer\n"); blueLedToggle(); alarmSchedule(&timer, 1000); }
// Timer #1 callback function void onTimer1(void *x) { static uint8_t counter; PRINTF("onTimer1\n"); redLedToggle(); // start (schedule) timer #1 alarmSchedule(&timer1, 700); if (++counter == 3) { // In case the counter is 3, start (schedule) timer #2 too PRINTF("reschedule alarm\n"); alarmSchedule(&timer2, 200); } }
void onAlarm(void *x) { redLedToggle(); // schedule the next alarm *exactly* 1000 milliseconds after this one nextPeriod += PERIOD; alarmSchedule(&alarm, nextPeriod - getJiffies()); }
void appMain(void) { timerBInit(); alarmInit(&blinkTimer, onBlinkTimer, NULL); alarmInit(&randTimer, onRandTimer, NULL); alarmSchedule(&blinkTimer, 100); alarmSchedule(&randTimer, 101); PRINTF("\n*** starting the app ***\n\n"); for (;;) { msleep(1000); } }
static void onRadioReset(void *x) { if (initResult == AMB8420_INIT_HARD_FAIL) return; #if RADIO_CHIP == RADIO_CHIP_AMB8420 amb8420ResetIfInactive(); #endif alarmSchedule(&radioResetTimer, RADIO_RESET_INTERVAL); }
static void roGreenLedTimerCb(void *x) { isGreenLedOn = !isGreenLedOn; if (isGreenLedOn) { if (isRoutingInfoValid()) greenLedOn(); } else { greenLedOff(); } alarmSchedule(&roGreenLedTimer, isGreenLedOn ? 100 : 5000); }
void onBlinkTimer(void *x) { redLedOn(); mdelay(100); redLedOff(); uint32_t now = getTimeMs(); uint32_t untilFrameEnd = BLINK_INTERVAL - now % BLINK_INTERVAL; alarmSchedule(&blinkTimer, untilFrameEnd); }
void routingInit(void) { socketOpen(&roSocket, routingReceive); socketBind(&roSocket, ROUTING_PROTOCOL_PORT); alarmInit(&roCheckTimer, roCheckTimerCb, NULL); alarmInit(&roForwardTimer, roForwardTimerCb, NULL); alarmInit(&roOutOfOrderForwardTimer, roOutOfOrderForwardTimerCb, NULL); alarmInit(&roRequestTimer, roRequestTimerCb, NULL); alarmInit(&roStopListeningTimer, roStopListeningTimerCb, NULL); alarmInit(&roStartListeningTimer, roStartListeningTimerCb, NULL); alarmInit(&roGreenLedTimer, roGreenLedTimerCb, NULL); alarmInit(&watchdogTimer, watchdogTimerCb, NULL); alarmSchedule(&roCheckTimer, randomInRange(1000, 3000)); alarmSchedule(&roForwardTimer, calcNextForwardTime(0)); alarmSchedule(&roStartListeningTimer, 110); alarmSchedule(&roGreenLedTimer, 10000); // alarmSchedule(&watchdogTimer, 1000); }
void alarmCallback(void *param) { uint16_t i = (uint16_t) param; // PRINTF("alarm %d at jiffies=%lu\n", i, getJiffies()); alarm.data = (void *) (i + 1); // reschedule the alarm alarmSchedule(&alarm, 10); // simulate a lockup if (i == 10) for (;;); }
// ------------------------------------- // Main function // ------------------------------------- void appMain(void) { uint16_t i; // timers (used to get an extra interrupt context) alarmInit(&timer, onTimer, NULL); alarmSchedule(&timer, 1000); // radio radioSetReceiveHandle(radioRecvCb); radioOn(); for (i = 0; i < BUFFER_SIZE; i++) { buffer[i] = i; } randomInit(); // SELECT_FLASH; // extFlashBulkErase(); // UNSELECT_FLASH; for (i = 0; ; i++) { uint32_t address = i * 64ul; SELECT_FLASH; if (IS_ALIGNED(address, EXT_FLASH_SECTOR_SIZE)) { PRINTF("erase address %lu\n", address); flashErase(address); } PRINTF("write address %lu\n", address); flashWrite(address); if (address > 0) { PRINTF("verify...\n"); flashRead(address - 64); } UNSELECT_FLASH; msleep(randomInRange(400, 1000)); PRINTF("send smth to radio...\n"); radioSend("hello world", sizeof("hello world")); greenLedToggle(); } }
void appMain(void) { alarmInit(&alarm, alarmCallback, (void *) 0); alarmSchedule(&alarm, 10); for (;;) { // PRINT("in app main...\n"); redLedToggle(); #if 1 // will get different error messages depending on whether in mdelay or in msleep msleep(1000); #else mdelay(1000); #endif } }
static void roRequestTimerCb(void *x) { // check if already found the info static uint8_t cnt; if (isRoutingInfoValid() && cnt > 5) return; cnt++; if (isRoutingInfoValid()) return; // add jitter routingRequestTimeout += randomNumberBounded(100); alarmSchedule(&roRequestTimer, routingRequestTimeout); if (routingRequestTimeout < ROUTING_REQUEST_MAX_EXP_TIMEOUT) { // use exponential increments routingRequestTimeout *= 2; } else if (routingRequestTimeout < ROUTING_REQUEST_MAX_TIMEOUT) { // use linear increments routingRequestTimeout += ROUTING_REQUEST_MAX_EXP_TIMEOUT; } else { // move back to initial (small) timeout routingRequestTimeout = ROUTING_REQUEST_INIT_TIMEOUT; } TPRINTF("SEND ROUTING REQUEST\n"); radioOn(); // wait for response isListening = true; RoutingRequestPacket_t req; req.packetType = ROUTING_REQUEST; req.senderType = SENDER_COLLECTOR; socketSendEx(&roSocket, &req, sizeof(req), MOS_ADDR_BROADCAST); alarmSchedule(&roStopListeningTimer, ROUTING_REPLY_WAIT_TIMEOUT); }
void alarmCallback(void *param) { (void) param; // lock the mutex in kernel context uint32_t start, end; start = getJiffies(); mutexLock(&testMutex); end = getJiffies(); PRINTF("in alarm callback, mutex lock time=%lu\n", end - start); mdelay(1300); mutexUnlock(&testMutex); // reschedule the alarm alarmSchedule(&alarm, ALARM_INTERVAL); }
void appMain(void) { redLedToggle(); // initialize and schedule the alarm alarmInit(&alarm, onAlarm, NULL); nextPeriod = PERIOD; // jiffie counter starts from 0 (zero) after reset alarmSchedule(&alarm, PERIOD - getJiffies()); // enter infinite low-power loop for (;;) { sleep(10); } }
static void roForwardTimerCb(void *x) { static uint8_t moteToProcess; bool roOK = isRoutingInfoValid(); if (motes[moteToProcess].address != 0 && timeAfter32(motes[moteToProcess].validUntil, (uint32_t)getJiffies()) && hopCountToRoot < MAX_HOP_COUNT && roOK) { forwardRoutingInfo(moteToProcess); } moteToProcess++; if (moteToProcess == MAX_MOTES) moteToProcess = 0; alarmSchedule(&roForwardTimer, calcNextForwardTime(moteToProcess)); }
void appMain(void) { // initialize the alarm alarmInit(&alarm, alarmCallback, NULL); // schedule the alarm after specific interval alarmSchedule(&alarm, ALARM_INTERVAL); for (;;) { ledToggle(); // lock the mutex in user context uint32_t start, end; start = getJiffies(); mutexLock(&testMutex); end = getJiffies(); PRINTF("in user main, mutex lock time=%lu\n", end - start); mdelay(1000); mutexUnlock(&testMutex); } }
static void roCheckTimerCb(void *x) { alarmSchedule(&roCheckTimer, 5000 + randomNumberBounded(1000)); bool routingOk = isRoutingInfoValid(); if (routingSearching) { // was searching for routing info if (routingOk) { routingSearching = false; alarmRemove(&roRequestTimer); } } else { // was not searching for routing info if (!routingOk) { routingSearching = true; routingRequestTimeout = ROUTING_REQUEST_INIT_TIMEOUT; roRequestTimerCb(NULL); } } }
static void watchdogTimerCb(void *x) { watchdogStart(WATCHDOG_EXPIRE_1000MS); alarmSchedule(&watchdogTimer, 500); }
static void routingReceive(Socket_t *s, uint8_t *data, uint16_t len) { // PRINTF("routingReceive %d bytes from %#04x\n", len, // s->recvMacInfo->originalSrc.shortAddr); // PRINTF("routing rx\n"); if (len == 0) { PRINTF("routingReceive: no data!\n"); return; } #if PRECONFIGURED_NH_TO_ROOT if (s->recvMacInfo->originalSrc.shortAddr != PRECONFIGURED_NH_TO_ROOT) { PRINTF("Dropping routing info: not from the nexthop, but from %#04x\n", s->recvMacInfo->originalSrc.shortAddr); return; } PRINTF("Got routing info from the nexthop\n"); #endif uint8_t type = data[0]; if (type == ROUTING_REQUEST) { uint8_t senderType = data[1]; if (senderType != SENDER_MOTE) return; uint8_t idx = markAsSeen(s->recvMacInfo->originalSrc.shortAddr, true); if (gotRreq == 0xff) { gotRreq = idx; alarmSchedule(&roOutOfOrderForwardTimer, randomNumberBounded(400)); } return; } if (type != ROUTING_INFORMATION) { PRINTF("routingReceive: unknown type!\n"); return; } if (len < sizeof(RoutingInfoPacket_t)) { PRINTF("routingReceive: too short for info packet!\n"); return; } RoutingInfoPacket_t ri; memcpy(&ri, data, sizeof(RoutingInfoPacket_t)); bool update = false; if (!isRoutingInfoValid() || timeAfter16(ri.seqnum, lastSeenSeqnum)) { // XXX: theoretically should add some time to avoid switching to // worse path only because packets from it travel faster update = true; TPRINTF("RI updated: > seqnum\n"); } else if (ri.seqnum == lastSeenSeqnum) { if (ri.hopCount < hopCountToRoot) { update = true; TPRINTF("RI updated: < metric\n"); } else if (ri.hopCount == hopCountToRoot && !seenRoutingInThisFrame) { update = true; TPRINTF("RI updated: == metric\n"); } } if (ri.hopCount > MAX_HOP_COUNT) update = false; if (update) { if (timeSinceFrameStart() < 2000 || timeSinceFrameStart() > 4000) { PRINTF("*** forwarder (?) sends out of time!\n"); } seenRoutingInThisFrame = true; rootAddress = ri.rootAddress; nexthopToRoot = s->recvMacInfo->originalSrc.shortAddr; lastSeenSeqnum = ri.seqnum; hopCountToRoot = ri.hopCount; lastRootMessageTime = (uint32_t) getJiffies(); int64_t oldRootClockDeltaMs = rootClockDeltaMs; rootClockDeltaMs = ri.rootClockMs - getTimeMs64(); if (abs((int32_t)oldRootClockDeltaMs - (int32_t)rootClockDeltaMs) > 500) { PRINTF("large delta change=%ld, time sync off?!\n", (int32_t)rootClockDeltaMs - (int32_t)oldRootClockDeltaMs); PRINTF("delta: old=%ld, new=%ld\n", (int32_t)oldRootClockDeltaMs, (int32_t)rootClockDeltaMs); } // TPRINTF("OK!%s\n", isListening ? "" : " (not listening)"); // reschedule next listen start after this timesync alarmSchedule(&roStartListeningTimer, timeToNextFrame() + 2000); } else { TPRINTF("RI not updated!\n"); } }
void onRandTimer(void *x) { alarmSchedule(&randTimer, 100 + randomNumberBounded(100)); }