@@ -85,10 +85,7 @@ void us_ticker_init(void)
85
85
86
86
cycles_per_us = system_gclk_gen_get_hz (config_tc .clock_source ) / 1000000 ;
87
87
MBED_ASSERT (cycles_per_us > 0 );
88
- /*while((cycles_per_us & 1) == 0 && prescaler <= 10) {
89
- cycles_per_us = cycles_per_us >> 1;
90
- prescaler++;
91
- }*/
88
+
92
89
while ((cycles_per_us > 1 ) && (prescaler <= 10 )) {
93
90
cycles_per_us = cycles_per_us >> 1 ;
94
91
prescaler ++ ;
@@ -115,6 +112,11 @@ void us_ticker_init(void)
115
112
116
113
/* Enable the timer module */
117
114
tc_enable (& us_ticker_module );
115
+
116
+ /* Enable the timer interrupt */
117
+ tc_disable_callback (& us_ticker_module , TC_CALLBACK_CC_CHANNEL0 );
118
+ NVIC_SetVector (TICKER_COUNTER_IRQn , (uint32_t )TICKER_COUNTER_Handlr );
119
+ NVIC_EnableIRQ (TICKER_COUNTER_IRQn );
118
120
}
119
121
120
122
uint32_t us_ticker_read ()
@@ -127,37 +129,33 @@ uint32_t us_ticker_read()
127
129
128
130
void us_ticker_set_interrupt (timestamp_t timestamp )
129
131
{
130
- uint32_t cur_time ;
131
- int32_t delta ;
132
-
133
- cur_time = us_ticker_read ();
134
- delta = (int32_t )((uint32_t )timestamp - cur_time );
135
- if (delta < 0 ) {
136
- /* Event already occurred in past */
137
- us_ticker_irq_handler ();
138
- return ;
139
- }
140
-
141
- NVIC_DisableIRQ (TICKER_COUNTER_IRQn );
142
- NVIC_SetVector (TICKER_COUNTER_IRQn , (uint32_t )TICKER_COUNTER_Handlr );
143
-
144
132
/* Enable the callback */
145
133
tc_enable_callback (& us_ticker_module , TC_CALLBACK_CC_CHANNEL0 );
146
134
tc_set_compare_value (& us_ticker_module , TC_COMPARE_CAPTURE_CHANNEL_0 , (uint32_t )timestamp );
147
-
148
- NVIC_EnableIRQ (TICKER_COUNTER_IRQn );
149
135
}
150
136
151
137
void us_ticker_disable_interrupt (void )
152
138
{
153
139
/* Disable the callback */
154
140
tc_disable_callback (& us_ticker_module , TC_CALLBACK_CC_CHANNEL0 );
155
- NVIC_DisableIRQ (TICKER_COUNTER_IRQn );
156
141
}
157
142
158
143
void us_ticker_fire_interrupt (void )
159
144
{
160
- NVIC_SetPendingIRQ (TICKER_COUNTER_IRQn );
145
+ /**
146
+ * We should be able to trigger this by just calling:
147
+ *
148
+ * NVIC_SetPendingIRQ(TICKER_COUNTER_IRQn);
149
+ *
150
+ * However, tc_interrupt.c -> _tc_interrupt_handler then gets the wrong value from reg.
151
+ * It should be 48 (when called via the normal API), but is 32. I've tried setting
152
+ * this register before triggering the interrupt, and this fails in the same way.
153
+ * I have no clue how to fix this properly... If someone comes along who understands
154
+ * Atmel IRQ handling better I'd love to get a better patch
155
+ */
156
+
157
+ // this gives us a minimum tick of 400 us.
158
+ us_ticker_set_interrupt (us_ticker_read () + 400 );
161
159
}
162
160
163
161
void us_ticker_clear_interrupt (void )
@@ -175,5 +173,8 @@ void us_ticker_clear_interrupt(void)
175
173
176
174
void us_ticker_free (void )
177
175
{
176
+ tc_clear_interrupt (& us_ticker_module , TC_CALLBACK_CC_CHANNEL0 );
177
+ NVIC_DisableIRQ (TICKER_COUNTER_IRQn );
178
178
179
+ us_ticker_inited = 0 ;
179
180
}
0 commit comments