@@ -38,7 +38,7 @@ static int us_ticker_inited = 0;
38
38
static void us_timer_init (void );
39
39
40
40
static uint32_t us_ticker_target = 0 ;
41
- static volatile uint32_t msb_counter = 0 ;
41
+ static volatile uint16_t msb_counter = 0 ;
42
42
43
43
void us_ticker_init (void )
44
44
{
@@ -55,7 +55,6 @@ void us_ticker_init(void)
55
55
* which is why a software timer is required to get 32-bit word length.
56
56
******************************************************************************/
57
57
/* TODO - Need some sort of load value/prescale calculation for non-32MHz clock */
58
- /* TODO - Add msb_counter rollover protection at 16 bits count? */
59
58
/* TODO - How is overflow handled? */
60
59
61
60
/* Timer 0 for free running time */
@@ -108,22 +107,21 @@ static void us_timer_init(void)
108
107
/* Reads 32 bit timer's current value (16 bit s/w timer | 16 bit h/w timer) */
109
108
uint32_t us_ticker_read ()
110
109
{
111
- uint32_t retval , tim0cval ;
112
110
113
111
if (!us_ticker_inited ) {
114
112
us_timer_init ();
115
113
}
116
114
115
+ NVIC_DisableIRQ (Tim0_IRQn );
116
+ uint32_t retval , tim0cval ;
117
117
/* Get the current tick from the hw and sw timers */
118
118
tim0cval = TIM0REG -> VALUE ; /* read current time */
119
119
retval = (0xFFFF - tim0cval ); /* subtract down count */
120
120
121
- NVIC_DisableIRQ (Tim0_IRQn );
122
121
if (TIM0REG -> CONTROL .BITS .INT ) {
123
- TIM0REG -> CLEAR = 0 ;
124
- msb_counter ++ ;
125
- tim0cval = TIM0REG -> VALUE ; /* read current time again after interrupt */
126
- retval = (0xFFFF - tim0cval );
122
+ us_timer_isr (); /* handle ISR again */
123
+ NVIC_ClearPendingIRQ (Tim0_IRQn );
124
+ retval = (0xFFFF - TIM0REG -> VALUE );
127
125
}
128
126
retval |= msb_counter << 16 ; /* add software bits */
129
127
NVIC_EnableIRQ (Tim0_IRQn );
@@ -168,25 +166,21 @@ extern void us_ticker_isr(void)
168
166
/* Clear IRQ flag */
169
167
TIM1REG -> CLEAR = 0 ;
170
168
171
- int32_t delta = us_ticker_target - us_ticker_read ();
172
- if (delta <= 0 ) {
173
- TIM1REG -> CONTROL .BITS .ENABLE = False ;
174
- us_ticker_irq_handler ();
169
+ if (us_ticker_target > 0 ) {
170
+ -- us_ticker_target ;
171
+ ticker_set (0xFFFF );
175
172
} else {
176
- // Clamp at max value of timer
177
- if (delta > 0xFFFF ) {
178
- delta = 0xFFFF ;
179
- }
180
-
181
- ticker_set (delta );
173
+ us_ticker_irq_handler ();
182
174
}
183
175
}
184
176
185
177
/* Set timer 1 ticker interrupt */
186
178
void us_ticker_set_interrupt (timestamp_t timestamp )
187
179
{
188
- us_ticker_target = (uint32_t )timestamp ;
189
- int32_t delta = us_ticker_target - us_ticker_read ();
180
+ int32_t delta = timestamp - us_ticker_read ();
181
+ // we got 16 bit timer, use upper 16bit as a simple counter how many times
182
+ // we need to schedule full range ticker count
183
+ us_ticker_target = (uint32_t )delta >> 16 ;
190
184
191
185
if (delta <= 0 ) {
192
186
/* This event was in the past */
@@ -200,10 +194,6 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
200
194
return ;
201
195
}
202
196
203
- // Clamp at max value of timer
204
- if (delta > 0xFFFF ) {
205
- delta = 0xFFFF ;
206
- }
207
-
208
- ticker_set (delta );
197
+ // we set the full reminder of 16 bit, the next ISR will do the upper part
198
+ ticker_set (delta & 0xFFFF );
209
199
}
0 commit comments