uint32_t RTC_setNextAlarm(uint16_t freq) { uint32_t unixTime = RTC_getUnixTime(); //can't be faster than once every 2 seconds if (freq > 43200) freq = 43200; //period in seconds float period = 86400.0 / freq; //current time, in seconds uint32_t nowSeconds = unixTime % 86400; //the 'number' of the next alarm uint32_t alarmNumber = (int) (nowSeconds / period) + 1; //time of the next alarm uint32_t newSeconds; if (alarmNumber == freq) newSeconds = 0; else newSeconds = (alarmNumber * 86400) / freq; unixTime = unixTime - nowSeconds + newSeconds; if (newSeconds == 0) unixTime += 86400; if (RTC_setAlarm(unixTime) == 1) return 0; return unixTime; }
/* This function is called for every incoming unicast packet. */ static void recv_uc(struct unicast_conn *c, const rimeaddr_t *from) { struct RTC_time t_now; struct unicast_message *msg, *tmp, sMsg; rimeaddr_t *add; /* The packetbuf_dataptr() returns a pointer to the first data byte in the received packet. */ msg = (struct unicast_message *)packetbuf_dataptr(); if(msg->type==INIT_RESP) { rimeaddr_copy(&nmaddr, from); id = msg->id; t_now = msg->time; RTC_setTime(&t_now); q.month = t_now.month; q.day = t_now.day; q.hours = 18; q.minutes = 30; q.seconds = 0; ioPins_init(); RTC_setAlarm(&q, alarmCallback, RPT_SECOND); } }
PROCESS_THREAD(test_RTC_process, ev, data) { PROCESS_BEGIN(); leds_off(LEDS_ALL); // Set the RTC time to be 12:34:56 on 30/1/15 // struct RTC_time t = { 0, // hundredths 0, // tenths 56, // seconds 34, // minutes 12, // hours 30, // day 01, // month 15 // year }; struct RTC_alarm q = { 10, // seconds 35, // minutes 12, // hours 30, // day 01 // month }; RTC_setTime(&t); // Alarm can be set with a callback // (or this can be NULL) RTC_setAlarm(&q, alarmCallback, RPT_MINUTE); int i = 0; while(1) { printf("GOING TO SLEEP\n"); clock_delay_msec(50); /* ------------ * Go to sleep * ------------ */ *CRM_SLEEP_CNTL = 0x71; // hibernate, keep all RAM pages, retain state, don't power GPIO, approx. 2kHz = 16.1uA while((*CRM_STATUS & 0x1) == 0) // wait for the sleep cycle to complete { continue; } *CRM_STATUS = 1; // write 1 to sleep_sync --- this clears the bit (it's a r1wc bit) and powers down /* ------------ * Wake up again * ------------ */ printf("AWAKE AGAIN\n"); for (i = 0; i < 2; i++) FLASH_LED(LEDS_ALL); PROCESS_YIELD(); RTC_getTime(&t); printf("%02d/%02d/%02d %02d:%02d:%02d.%d%d\n", t.day, t.month, t.year, t.hours, t.minutes, t.seconds, t.tenths, t.hundredths); } PROCESS_END(); }