36
36
37
37
LPTIM_HandleTypeDef LptimHandle ;
38
38
39
- volatile uint32_t lp_SlaveCounter = 0 ;
40
- volatile uint32_t lp_oc_int_part = 0 ;
41
- volatile uint16_t lp_TickPeriod_us ;
39
+ const ticker_info_t * lp_ticker_get_info ()
40
+ {
41
+ static const ticker_info_t info = {
42
+ RTC_CLOCK ,
43
+ 16
44
+ };
45
+ return & info ;
46
+ }
47
+
42
48
volatile uint8_t lp_Fired = 0 ;
43
49
44
50
static void LPTIM1_IRQHandler (void );
45
51
static void (* irq_handler )(void );
46
52
47
-
48
53
void lp_ticker_init (void )
49
54
{
55
+ NVIC_DisableIRQ (LPTIM1_IRQn );
56
+
50
57
/* Check if LPTIM is already configured */
51
58
#if (TARGET_STM32L0 )
52
59
if (READ_BIT (RCC -> APB1ENR , RCC_APB1ENR_LPTIM1EN ) != RESET ) {
@@ -111,21 +118,7 @@ void lp_ticker_init(void)
111
118
LptimHandle .Instance = LPTIM1 ;
112
119
LptimHandle .State = HAL_LPTIM_STATE_RESET ;
113
120
LptimHandle .Init .Clock .Source = LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC ;
114
-
115
- /* Prescaler impact:
116
- tick period = Prescaler division factor / LPTIM clock
117
- Example with LPTIM clock = 32768 Hz LSE
118
- Prescaler = LPTIM_PRESCALER_DIV1 => lp_TickPeriod_us = 31us => 2s with 16b timer
119
- Prescaler = LPTIM_PRESCALER_DIV2 => lp_TickPeriod_us = 61us => 4s with 16b timer
120
- Prescaler = LPTIM_PRESCALER_DIV4 => lp_TickPeriod_us = 122us => 8s with 16b timer
121
- Prescaler = LPTIM_PRESCALER_DIV8 => lp_TickPeriod_us = 244us => 16s with 16b timer
122
- Prescaler = LPTIM_PRESCALER_DIV16 => lp_TickPeriod_us = 488us => 32s with 16b timer
123
- Prescaler = LPTIM_PRESCALER_DIV32 => lp_TickPeriod_us = 976us => 64s with 16b timer
124
- Prescaler = LPTIM_PRESCALER_DIV64 => lp_TickPeriod_us = 1.9ms => 128s with 16b timer
125
- Prescaler = LPTIM_PRESCALER_DIV128 => lp_TickPeriod_us = 3.9ms => 256s with 16b timer
126
- */
127
- LptimHandle .Init .Clock .Prescaler = LPTIM_PRESCALER_DIV2 ;
128
- lp_TickPeriod_us = 2 * 1000000 / RTC_CLOCK ;
121
+ LptimHandle .Init .Clock .Prescaler = LPTIM_PRESCALER_DIV1 ;
129
122
130
123
LptimHandle .Init .Trigger .Source = LPTIM_TRIGSOURCE_SOFTWARE ;
131
124
LptimHandle .Init .OutputPolarity = LPTIM_OUTPUTPOLARITY_HIGH ;
@@ -142,18 +135,17 @@ void lp_ticker_init(void)
142
135
}
143
136
144
137
NVIC_SetVector (LPTIM1_IRQn , (uint32_t )LPTIM1_IRQHandler );
145
- NVIC_EnableIRQ (LPTIM1_IRQn );
146
138
147
139
#if !(TARGET_STM32L4 )
148
140
/* EXTI lines are not configured by default */
149
141
__HAL_LPTIM_WAKEUPTIMER_EXTI_ENABLE_IT ();
150
142
__HAL_LPTIM_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE ();
151
143
#endif
152
144
153
- __HAL_LPTIM_ENABLE_IT (& LptimHandle , LPTIM_IT_ARRM );
154
145
__HAL_LPTIM_ENABLE_IT (& LptimHandle , LPTIM_IT_CMPM );
155
- __HAL_LPTIM_ENABLE_IT (& LptimHandle , LPTIM_IT_CMPOK );
156
146
HAL_LPTIM_Counter_Start (& LptimHandle , 0xFFFF );
147
+
148
+ __HAL_LPTIM_COMPARE_SET (& LptimHandle , 0 );
157
149
}
158
150
159
151
static void LPTIM1_IRQHandler (void )
@@ -173,114 +165,58 @@ static void LPTIM1_IRQHandler(void)
173
165
/* Clear Compare match flag */
174
166
__HAL_LPTIM_CLEAR_FLAG (& LptimHandle , LPTIM_FLAG_CMPM );
175
167
176
- if (lp_oc_int_part > 0 ) {
177
- lp_oc_int_part -- ;
178
- } else {
179
168
if (irq_handler ) {
180
169
irq_handler ();
181
- }
182
170
}
183
171
}
184
172
}
185
173
186
- /* Compare write interrupt */
187
- if (__HAL_LPTIM_GET_FLAG (& LptimHandle , LPTIM_FLAG_CMPOK ) != RESET ) {
188
- if (__HAL_LPTIM_GET_IT_SOURCE (& LptimHandle , LPTIM_IT_CMPOK ) != RESET ) {
189
- /* Clear Compare write flag */
190
- __HAL_LPTIM_CLEAR_FLAG (& LptimHandle , LPTIM_FLAG_CMPOK );
191
- }
192
- }
193
-
194
- /* Autoreload match interrupt */
195
- if (__HAL_LPTIM_GET_FLAG (& LptimHandle , LPTIM_FLAG_ARRM ) != RESET ) {
196
- if (__HAL_LPTIM_GET_IT_SOURCE (& LptimHandle , LPTIM_IT_ARRM ) != RESET ) {
197
- /* Clear Autoreload match flag */
198
- __HAL_LPTIM_CLEAR_FLAG (& LptimHandle , LPTIM_FLAG_ARRM );
199
- lp_SlaveCounter ++ ;
200
- }
201
- }
202
-
203
174
#if !(TARGET_STM32L4 )
204
175
__HAL_LPTIM_WAKEUPTIMER_EXTI_CLEAR_FLAG ();
205
176
#endif
206
177
}
207
178
208
-
209
- uint32_t lp_ticker_read_TickCounter (void )
210
- {
211
- uint16_t cntH_old , cntH , cntL ;
212
-
213
- LptimHandle .Instance = LPTIM1 ;
214
-
215
- /* same algo as us_ticker_read in us_ticker_16b.c */
216
- do {
217
- cntH_old = lp_SlaveCounter ;
218
- if (__HAL_LPTIM_GET_FLAG (& LptimHandle , LPTIM_FLAG_ARRM ) == SET ) {
219
- cntH_old += 1 ;
220
- }
221
- cntL = LPTIM1 -> CNT ;
222
- cntH = lp_SlaveCounter ;
223
- if (__HAL_LPTIM_GET_FLAG (& LptimHandle , LPTIM_FLAG_ARRM ) == SET ) {
224
- cntH += 1 ;
225
- }
226
- } while (cntH_old != cntH );
227
- uint32_t lp_time = (uint32_t )(cntH << 16 | cntL );
228
- return lp_time ;
229
- }
230
-
231
179
uint32_t lp_ticker_read (void )
232
180
{
233
- lp_ticker_init () ;
234
- return lp_ticker_read_TickCounter () * ( uint32_t ) lp_TickPeriod_us ;
181
+ uint32_t lp_time = LPTIM1 -> CNT ;
182
+ return lp_time ;
235
183
}
236
184
237
185
void lp_ticker_set_interrupt (timestamp_t timestamp )
238
186
{
239
- // Disable IRQs
240
- core_util_critical_section_enter ();
241
-
242
- uint32_t timestamp_TickCounter = timestamp / (uint32_t )lp_TickPeriod_us ;
243
-
244
187
LptimHandle .Instance = LPTIM1 ;
245
188
irq_handler = (void (* )(void ))lp_ticker_irq_handler ;
246
189
247
- __HAL_LPTIM_CLEAR_FLAG (& LptimHandle , LPTIM_FLAG_CMPOK );
248
- __HAL_LPTIM_CLEAR_FLAG (& LptimHandle , LPTIM_FLAG_CMPM );
249
- __HAL_LPTIM_COMPARE_SET (& LptimHandle , timestamp_TickCounter & 0xFFFF );
250
-
251
190
/* CMPOK is set by hardware to inform application that the APB bus write operation to the LPTIM_CMP register has been successfully completed */
191
+ /* Any successive write before respectively the ARROK flag or the CMPOK flag be set, will lead to unpredictable results */
252
192
while (__HAL_LPTIM_GET_FLAG (& LptimHandle , LPTIM_FLAG_CMPOK ) == RESET ) {
253
193
}
254
194
255
- /* same algo as us_ticker_set_interrupt in us_ticker_16b.c */
256
- uint32_t current_time_TickCounter = lp_ticker_read_TickCounter ();
257
- uint32_t delta = timestamp_TickCounter - current_time_TickCounter ;
258
- lp_oc_int_part = (delta - 1 ) >> 16 ;
259
- if ( ((delta - 1 ) & 0xFFFF ) >= 0x8000 &&
260
- __HAL_LPTIM_GET_FLAG (& LptimHandle , LPTIM_FLAG_CMPM ) == SET ) {
261
- ++ lp_oc_int_part ;
262
- }
195
+ __HAL_LPTIM_CLEAR_FLAG (& LptimHandle , LPTIM_FLAG_CMPOK );
196
+ __HAL_LPTIM_CLEAR_FLAG (& LptimHandle , LPTIM_FLAG_CMPM );
197
+ __HAL_LPTIM_COMPARE_SET (& LptimHandle , timestamp );
263
198
264
- // Enable IRQs
265
- core_util_critical_section_exit ();
199
+ NVIC_EnableIRQ (LPTIM1_IRQn );
266
200
}
267
201
268
202
void lp_ticker_fire_interrupt (void )
269
203
{
270
204
lp_Fired = 1 ;
271
205
NVIC_SetPendingIRQ (LPTIM1_IRQn );
206
+ NVIC_EnableIRQ (LPTIM1_IRQn );
272
207
}
273
208
274
209
void lp_ticker_disable_interrupt (void )
275
210
{
276
211
LptimHandle .Instance = LPTIM1 ;
277
- __HAL_LPTIM_DISABLE_IT ( & LptimHandle , LPTIM_IT_CMPM );
212
+ NVIC_DisableIRQ ( LPTIM1_IRQn );
278
213
}
279
214
280
215
void lp_ticker_clear_interrupt (void )
281
216
{
282
217
LptimHandle .Instance = LPTIM1 ;
283
218
__HAL_LPTIM_CLEAR_FLAG (& LptimHandle , LPTIM_FLAG_CMPM );
219
+ NVIC_ClearPendingIRQ (LPTIM1_IRQn );
284
220
}
285
221
286
222
#else /* MBED_CONF_TARGET_LPTICKER_LPTIM */
0 commit comments