Appiko
device_tick.c
1 
19 #include "device_tick.h"
20 #include "irq_msg_util.h"
21 #include "ms_timer.h"
22 #include "nrf_assert.h"
23 #include "common_util.h"
24 
25 #define DEV_TICK_MSTIMER CONCAT_2(MS_TIMER,MS_TIMER_USED_DEVICE_TICKS)
26 
27 #define MSTIMER_TICKS_TO_DEV_TICKS(lfclk_ticks) (lfclk_ticks/DEVICE_TICK_MSTIMER_DIV_FACTOR)
28 #define DEV_TICKS_TO_MSTIMER_TICKS(dev_ticks) (dev_ticks*DEVICE_TICK_MSTIMER_DIV_FACTOR)
29 
30 struct {
31  device_tick_mode current_mode;
32  uint32_t half_current_interval;
33  uint32_t fast_tick_interval;
34  uint32_t slow_tick_interval;
35  uint32_t last_tick_count;
36 }device_tick_ctx;
37 
38 static void add_tick(void){
39  uint32_t current_count, duration;
40 
41  current_count = ms_timer_get_current_count();
42  duration = (current_count + (1<<24) - device_tick_ctx.last_tick_count) & 0xFFFFFF;
43  device_tick_ctx.last_tick_count = current_count;
44  irq_msg_push(MSG_NEXT_INTERVAL,(void *) (uint32_t) MSTIMER_TICKS_TO_DEV_TICKS(duration));
45 }
46 
47 void tick_timer_handler(void)
48 {
49  add_tick();
50 }
51 
53 {
54  ASSERT(cfg->fast_mode_ticks < cfg->slow_mode_ticks);
55 
56  if(cfg->mode != DEVICE_TICK_SAME)
57  {
58  device_tick_ctx.current_mode = cfg->mode;
59  }
60  device_tick_ctx.fast_tick_interval = cfg->fast_mode_ticks;
61  device_tick_ctx.slow_tick_interval = cfg->slow_mode_ticks;
62 
63  device_tick_ctx.half_current_interval =
64  ((device_tick_ctx.current_mode == DEVICE_TICK_SLOW)?
65  cfg->slow_mode_ticks:cfg->fast_mode_ticks)/2;
66 
67  ms_timer_start(DEV_TICK_MSTIMER, MS_REPEATED_CALL,
68  2*device_tick_ctx.half_current_interval,
69  tick_timer_handler);
70  add_tick();
71 }
72 
74 {
75  ASSERT(mode != DEVICE_TICK_SAME);
76 
77  if(device_tick_ctx.current_mode != mode){
78  device_tick_ctx.current_mode = mode;
79  device_tick_ctx.half_current_interval = ((mode == DEVICE_TICK_SLOW)?
80  device_tick_ctx.slow_tick_interval:
81  device_tick_ctx.fast_tick_interval)/2;
82 
83  ms_timer_start(DEV_TICK_MSTIMER, MS_REPEATED_CALL,
84  2*device_tick_ctx.half_current_interval, tick_timer_handler);
85  add_tick();
86  }
87 }
88 
90 {
91  uint32_t current_count, duration;
92 
93  current_count = ms_timer_get_current_count();
94  duration = (current_count + (1<<24) - device_tick_ctx.last_tick_count) & 0xFFFFFF;
95 
96  if(duration > device_tick_ctx.half_current_interval)
97  {
98  ms_timer_start(DEV_TICK_MSTIMER, MS_REPEATED_CALL,
99  2*device_tick_ctx.half_current_interval, tick_timer_handler);
100 
101  device_tick_ctx.last_tick_count = current_count;
102  irq_msg_push(MSG_NEXT_INTERVAL,
103  (void *) (uint32_t) MSTIMER_TICKS_TO_DEV_TICKS(duration));
104  }
105 }
uint32_t ms_timer_get_current_count(void)
Return the current count (24 bit) of the RTC timer used.
Definition: ms_timer.h:127
device_tick_mode mode
The number of LFCLK ticks for the slow mode.
Definition: device_tick.h:67
uint32_t slow_mode_ticks
The number of LFCLK ticks for the fast mode.
Definition: device_tick.h:66
device_tick_mode
The two different modes at which the device ticks, fast & slow.
Definition: device_tick.h:53
Repeated call of the timer.
Definition: ms_timer.h:83
Stucture for passing the configuration for initializing the Device Tick module.
Definition: device_tick.h:64
#define ASSERT(expression)
Macro for runtime assertion of an expression. If the expression is false the assert_nrf_callback func...
Definition: nrf_assert.h:48
void device_tick_init(device_tick_cfg *cfg)
Initializes and starts the Device tick module. Provides a next interval event immediately....
Definition: device_tick.c:52
Same as previous mode.
Definition: device_tick.h:57
void device_tick_process(void)
The function to be called whenever the SoC wakes up to see if more than half the time of the current ...
Definition: device_tick.c:89
void ms_timer_start(ms_timer_num id, ms_timer_mode mode, uint64_t ticks, void(*handler)(void))
Definition: ms_timer.c:134
void device_tick_switch_mode(device_tick_mode mode)
Switches from the fast to slow mode or vice versa.
Definition: device_tick.c:73
void irq_msg_push(irq_msg_types pushed_msg, void *more_data)
Definition: irq_msg_util.c:64
Slow mode.
Definition: device_tick.h:55