mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
287 lines
9.1 KiB
Diff
287 lines
9.1 KiB
Diff
*** stock_iot-lab_M3/openwsn/opentimers.c Thu Apr 24 11:01:37 2014
|
|
--- riot-openwsn-wip/openwsn/opentimers.c Thu Apr 24 16:55:54 2014
|
|
***************
|
|
*** 9,33 ****
|
|
|
|
#include "openwsn.h"
|
|
#include "opentimers.h"
|
|
! #include "bsp_timer.h"
|
|
! #include "leds.h"
|
|
|
|
//=========================== define ==========================================
|
|
|
|
//=========================== variables =======================================
|
|
|
|
- typedef struct {
|
|
- opentimers_t timersBuf[MAX_NUM_TIMERS];
|
|
- bool running;
|
|
- PORT_TIMER_WIDTH currentTimeout; // current timeout, in ticks
|
|
- } opentimers_vars_t;
|
|
-
|
|
opentimers_vars_t opentimers_vars;
|
|
//uint32_t counter; //counts the elapsed time.
|
|
|
|
//=========================== prototypes ======================================
|
|
|
|
! void opentimers_timer_callback();
|
|
|
|
//=========================== public ==========================================
|
|
|
|
--- 9,34 ----
|
|
|
|
#include "openwsn.h"
|
|
#include "opentimers.h"
|
|
! // #include "bsp_timer.h"
|
|
! #include "board_ow.h"
|
|
! #include "leds_ow.h"
|
|
!
|
|
! #include "hwtimer_cpu.h"
|
|
! #include "arch/hwtimer_arch.h"
|
|
! // #include "hwtimer.h"
|
|
|
|
//=========================== define ==========================================
|
|
|
|
//=========================== variables =======================================
|
|
|
|
opentimers_vars_t opentimers_vars;
|
|
//uint32_t counter; //counts the elapsed time.
|
|
+ uint32_t timer_id;
|
|
|
|
//=========================== prototypes ======================================
|
|
|
|
! void opentimers_int_handler(int);
|
|
! void opentimers_timer_callback(void);
|
|
|
|
//=========================== public ==========================================
|
|
|
|
***************
|
|
*** 36,42 ****
|
|
|
|
Initializes data structures and hardware timer.
|
|
*/
|
|
! void opentimers_init(){
|
|
uint8_t i;
|
|
|
|
// initialize local variables
|
|
--- 37,43 ----
|
|
|
|
Initializes data structures and hardware timer.
|
|
*/
|
|
! void opentimers_init(void){
|
|
uint8_t i;
|
|
|
|
// initialize local variables
|
|
***************
|
|
*** 51,57 ****
|
|
}
|
|
|
|
// set callback for bsp_timers module
|
|
! bsp_timer_set_callback(opentimers_timer_callback);
|
|
}
|
|
|
|
/**
|
|
--- 52,71 ----
|
|
}
|
|
|
|
// set callback for bsp_timers module
|
|
! // bsp_timer_set_callback(opentimers_timer_callback);
|
|
! hwtimer_arch_enable_interrupt();
|
|
! hwtimer_arch_init(opentimers_int_handler, F_CPU);
|
|
! // hwtimer_init();
|
|
! }
|
|
!
|
|
! /**
|
|
! \brief opentimers interrupt handler
|
|
!
|
|
! This is a wrapper to fit the hwtimer_arch API
|
|
! */
|
|
! void opentimers_int_handler(int t) {
|
|
! (void)t;
|
|
! opentimers_timer_callback();
|
|
}
|
|
|
|
/**
|
|
***************
|
|
*** 64,70 ****
|
|
- if not, insert it in the list
|
|
|
|
\param duration Number milli-seconds after which the timer will fire.
|
|
! \param type The type of timer, indicating whether it's a one-shot or a period timer.
|
|
\param callback The function to call when the timer fires.
|
|
|
|
\returns The id of the timer (which serves as a handler to stop it) if the
|
|
--- 78,89 ----
|
|
- if not, insert it in the list
|
|
|
|
\param duration Number milli-seconds after which the timer will fire.
|
|
! \param type Type of timer:
|
|
! - #TIMER_PERIODIC for a periodic timer.
|
|
! - #TIMER_ONESHOT for a on-shot timer.
|
|
! \param timetype Units of the <tt>duration</tt>:
|
|
! - #TIME_MS when <tt>duration</tt> is in ms.
|
|
! - #TIME_TICS when <tt>duration</tt> is in clock ticks.
|
|
\param callback The function to call when the timer fires.
|
|
|
|
\returns The id of the timer (which serves as a handler to stop it) if the
|
|
***************
|
|
*** 74,79 ****
|
|
--- 93,99 ----
|
|
opentimer_id_t opentimers_start(uint32_t duration, timer_type_t type, time_type_t timetype, opentimers_cbt callback) {
|
|
|
|
uint8_t id;
|
|
+ // puts("timer set");
|
|
|
|
// find an unused timer
|
|
for (id=0; id<MAX_NUM_TIMERS && opentimers_vars.timersBuf[id].isrunning==TRUE; id++);
|
|
***************
|
|
*** 122,130 ****
|
|
) {
|
|
opentimers_vars.currentTimeout = opentimers_vars.timersBuf[id].ticks_remaining;
|
|
if (opentimers_vars.running==FALSE) {
|
|
! bsp_timer_reset();
|
|
}
|
|
! bsp_timer_scheduleIn(opentimers_vars.timersBuf[id].ticks_remaining);
|
|
}
|
|
|
|
opentimers_vars.running = TRUE;
|
|
--- 142,155 ----
|
|
) {
|
|
opentimers_vars.currentTimeout = opentimers_vars.timersBuf[id].ticks_remaining;
|
|
if (opentimers_vars.running==FALSE) {
|
|
! // bsp_timer_reset();
|
|
! hwtimer_arch_unset(OPENTIMERS_HWTIMER_ID);
|
|
! // hwtimer_remove(timer_id);
|
|
}
|
|
! // bsp_timer_scheduleIn(opentimers_vars.timersBuf[id].ticks_remaining);
|
|
! hwtimer_arch_set(opentimers_vars.timersBuf[id].ticks_remaining, OPENTIMERS_HWTIMER_ID);
|
|
! // hwtimer_arch_enable_interrupt();
|
|
! // timer_id = hwtimer_set(opentimers_vars.timersBuf[id].ticks_remaining, opentimers_timer_callback, NULL);
|
|
}
|
|
|
|
opentimers_vars.running = TRUE;
|
|
***************
|
|
*** 197,204 ****
|
|
corresponding callback(s), and restarts the hardware timer with the next timer
|
|
to expire.
|
|
*/
|
|
! void opentimers_timer_callback() {
|
|
!
|
|
opentimer_id_t id;
|
|
PORT_TIMER_WIDTH min_timeout;
|
|
bool found;
|
|
--- 222,228 ----
|
|
corresponding callback(s), and restarts the hardware timer with the next timer
|
|
to expire.
|
|
*/
|
|
! void opentimers_timer_callback(void) {
|
|
opentimer_id_t id;
|
|
PORT_TIMER_WIDTH min_timeout;
|
|
bool found;
|
|
***************
|
|
*** 275,284 ****
|
|
if (found==TRUE) {
|
|
// at least one timer pending
|
|
opentimers_vars.currentTimeout = min_timeout;
|
|
! bsp_timer_scheduleIn(opentimers_vars.currentTimeout);
|
|
} else {
|
|
// no more timers pending
|
|
opentimers_vars.running = FALSE;
|
|
}
|
|
}
|
|
|
|
--- 299,392 ----
|
|
if (found==TRUE) {
|
|
// at least one timer pending
|
|
opentimers_vars.currentTimeout = min_timeout;
|
|
! // bsp_timer_scheduleIn(opentimers_vars.currentTimeout);
|
|
! hwtimer_arch_set(opentimers_vars.currentTimeout, OPENTIMERS_HWTIMER_ID);
|
|
! // timer_id = hwtimer_set(opentimers_vars.currentTimeout, opentimers_timer_callback, NULL);
|
|
} else {
|
|
// no more timers pending
|
|
opentimers_vars.running = FALSE;
|
|
}
|
|
}
|
|
|
|
+ void opentimers_sleepTimeCompesation(uint16_t sleepTime)
|
|
+ {
|
|
+ opentimer_id_t id;
|
|
+ PORT_TIMER_WIDTH min_timeout;
|
|
+ bool found;
|
|
+
|
|
+ //step 1. reCount the ticks_remain after waking up from sleep
|
|
+ for(id=0; id<MAX_NUM_TIMERS; id++)
|
|
+ {
|
|
+ if (opentimers_vars.timersBuf[id].isrunning==TRUE)
|
|
+ {
|
|
+ if(opentimers_vars.timersBuf[id].ticks_remaining > sleepTime)
|
|
+ {
|
|
+ opentimers_vars.timersBuf[id].ticks_remaining -= sleepTime;
|
|
+ }
|
|
+ else
|
|
+ {
|
|
+ if(opentimers_vars.timersBuf[id].wraps_remaining > 0)
|
|
+ {
|
|
+ opentimers_vars.timersBuf[id].wraps_remaining--;
|
|
+ opentimers_vars.timersBuf[id].ticks_remaining += (MAX_TICKS_IN_SINGLE_CLOCK - sleepTime);
|
|
+ }
|
|
+ else
|
|
+ {
|
|
+ opentimers_vars.timersBuf[id].hasExpired = TRUE;
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+ // step 2. call callbacks of expired timers
|
|
+ for(id=0; id<MAX_NUM_TIMERS; id++) {
|
|
+ if (opentimers_vars.timersBuf[id].hasExpired==TRUE){
|
|
+
|
|
+ // call the callback
|
|
+ opentimers_vars.timersBuf[id].callback();
|
|
+ opentimers_vars.timersBuf[id].hasExpired = FALSE;
|
|
+
|
|
+ // reload the timer, if applicable
|
|
+ if (opentimers_vars.timersBuf[id].type==TIMER_PERIODIC) {
|
|
+ opentimers_vars.timersBuf[id].wraps_remaining = (opentimers_vars.timersBuf[id].period_ticks/MAX_TICKS_IN_SINGLE_CLOCK);//65535=maxValue of uint16_t
|
|
+ //if the number of ticks falls below a 16bit value, use it, otherwise set to max 16bit value
|
|
+ if(opentimers_vars.timersBuf[id].wraps_remaining==0)
|
|
+ opentimers_vars.timersBuf[id].ticks_remaining = opentimers_vars.timersBuf[id].period_ticks;
|
|
+ else
|
|
+ opentimers_vars.timersBuf[id].ticks_remaining = MAX_TICKS_IN_SINGLE_CLOCK;
|
|
+
|
|
+ } else {
|
|
+ opentimers_vars.timersBuf[id].isrunning = FALSE;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ }
|
|
+
|
|
+ // step 3. find the minimum remaining timeout among running timers
|
|
+ found = FALSE;
|
|
+ for(id=0;id<MAX_NUM_TIMERS;id++) {
|
|
+ if (
|
|
+ opentimers_vars.timersBuf[id].isrunning==TRUE &&
|
|
+ (
|
|
+ found==FALSE
|
|
+ ||
|
|
+ opentimers_vars.timersBuf[id].ticks_remaining < min_timeout
|
|
+ )
|
|
+ ) {
|
|
+ min_timeout = opentimers_vars.timersBuf[id].ticks_remaining;
|
|
+ found = TRUE;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ // step 4. schedule next timeout
|
|
+ if (found==TRUE) {
|
|
+ // at least one timer pending
|
|
+ opentimers_vars.currentTimeout = min_timeout;
|
|
+ // bsp_timer_scheduleIn(opentimers_vars.currentTimeout);
|
|
+ hwtimer_arch_set(opentimers_vars.currentTimeout, OPENTIMERS_HWTIMER_ID);
|
|
+ // timer_id = hwtimer_set(opentimers_vars.currentTimeout, opentimers_timer_callback, NULL);
|
|
+ } else {
|
|
+ // no more timers pending
|
|
+ opentimers_vars.running = FALSE;
|
|
+ }
|
|
+ }
|