@@ -262,35 +262,43 @@ void lp_ticker_init(void)
262
262
#else
263
263
LptimHandle .Init .Clock .Prescaler = LPTIM_PRESCALER_DIV1 ;
264
264
#endif /* MBED_CONF_TARGET_LPTICKER_LPTIM_CLOCK */
265
-
266
265
LptimHandle .Init .Trigger .Source = LPTIM_TRIGSOURCE_SOFTWARE ;
267
266
#if defined (LPTIM_ACTIVEEDGE_FALLING )
268
267
LptimHandle .Init .Trigger .ActiveEdge = LPTIM_ACTIVEEDGE_FALLING ;
269
268
#endif
269
+ #if defined(TARGET_STM32U5 )
270
+ LptimHandle .Init .Period = 0xFFFF ;
271
+ #endif
270
272
#if defined (LPTIM_TRIGSAMPLETIME_DIRECTTRANSITION )
271
273
LptimHandle .Init .Trigger .SampleTime = LPTIM_TRIGSAMPLETIME_DIRECTTRANSITION ;
272
274
#endif
273
-
274
- LptimHandle .Init .UltraLowPowerClock .SampleTime = LPTIM_TRIGSAMPLETIME_DIRECTTRANSITION ; // L5 ?
275
-
275
+ #if defined(LPTIM_CLOCKPOLARITY_RISING )
276
+ LptimHandle .Init .UltraLowPowerClock .Polarity = LPTIM_CLOCKPOLARITY_RISING ;
277
+ LptimHandle .Init .UltraLowPowerClock .SampleTime = LPTIM_CLOCKSAMPLETIME_DIRECTTRANSITION ;
278
+ #endif
279
+ #if defined(LPTIM_OUTPUTPOLARITY_HIGH )
276
280
LptimHandle .Init .OutputPolarity = LPTIM_OUTPUTPOLARITY_HIGH ;
281
+ #endif
277
282
LptimHandle .Init .UpdateMode = LPTIM_UPDATE_IMMEDIATE ;
278
283
LptimHandle .Init .CounterSource = LPTIM_COUNTERSOURCE_INTERNAL ;
279
- #if defined (LPTIM_INPUT1SOURCE_GPIO ) /* STM32L4 / STM32L5 */
284
+ #if defined (LPTIM_INPUT1SOURCE_GPIO )
280
285
LptimHandle .Init .Input1Source = LPTIM_INPUT1SOURCE_GPIO ;
281
286
LptimHandle .Init .Input2Source = LPTIM_INPUT2SOURCE_GPIO ;
282
287
#endif /* LPTIM_INPUT1SOURCE_GPIO */
283
-
284
- #if defined(LPTIM_RCR_REP ) /* STM32L4 / STM32L5 */
288
+ #if defined(LPTIM_RCR_REP )
285
289
LptimHandle .Init .RepetitionCounter = 0 ;
286
290
#endif /* LPTIM_RCR_REP */
287
291
288
-
289
292
if (HAL_LPTIM_Init (& LptimHandle ) != HAL_OK ) {
290
293
error ("HAL_LPTIM_Init ERROR\n" );
291
294
return ;
292
295
}
293
296
297
+ #if defined(__HAL_RCC_LPTIM1_CLKAM_ENABLE )
298
+ /* Enable autonomous mode for LPTIM1 */
299
+ __HAL_RCC_LPTIM1_CLKAM_ENABLE ();
300
+ #endif
301
+
294
302
NVIC_SetVector (LPTIM_MST_IRQ , (uint32_t )LPTIM_IRQHandler );
295
303
296
304
#if (LPTIM_MST_BASE == LPTIM1_BASE )
@@ -306,8 +314,25 @@ void lp_ticker_init(void)
306
314
__HAL_LPTIM_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE ();
307
315
#endif
308
316
317
+ #if defined(LPTIM_FLAG_DIEROK )
318
+ HAL_LPTIM_Counter_Start (& LptimHandle );
319
+
320
+ __HAL_LPTIM_CLEAR_FLAG (& LptimHandle , LPTIM_FLAG_DIEROK );
321
+ __HAL_LPTIM_ENABLE_IT (& LptimHandle , LPTIM_IT_CC1 | LPTIM_IT_CMP1OK );
322
+ while (__HAL_LPTIM_GET_FLAG (& LptimHandle , LPTIM_FLAG_DIEROK ) == RESET ) {
323
+ }
324
+
325
+ /* Need to write a compare value in order to get LPTIM_FLAG_CMPOK in set_interrupt */
326
+ __HAL_LPTIM_CLEAR_FLAG (& LptimHandle , LPTIM_FLAG_CMP1OK );
327
+ __HAL_LPTIM_COMPARE_SET (& LptimHandle , LPTIM_CHANNEL_1 , 0 );
328
+ while (__HAL_LPTIM_GET_FLAG (& LptimHandle , LPTIM_FLAG_CMP1OK ) == RESET ) {
329
+ }
330
+ __HAL_LPTIM_CLEAR_FLAG (& LptimHandle , LPTIM_FLAG_CMP1OK );
331
+
332
+ #else
309
333
__HAL_LPTIM_ENABLE_IT (& LptimHandle , LPTIM_IT_CMPM );
310
334
__HAL_LPTIM_ENABLE_IT (& LptimHandle , LPTIM_IT_CMPOK );
335
+
311
336
HAL_LPTIM_Counter_Start (& LptimHandle , 0xFFFF );
312
337
313
338
/* Need to write a compare value in order to get LPTIM_FLAG_CMPOK in set_interrupt */
@@ -316,6 +341,7 @@ void lp_ticker_init(void)
316
341
while (__HAL_LPTIM_GET_FLAG (& LptimHandle , LPTIM_FLAG_CMPOK ) == RESET ) {
317
342
}
318
343
__HAL_LPTIM_CLEAR_FLAG (& LptimHandle , LPTIM_FLAG_CMPOK );
344
+ #endif
319
345
320
346
/* Init is called with Interrupts disabled, so the CMPOK interrupt
321
347
* will not be handled. Let's mark it is now safe to write to LP counter */
@@ -330,22 +356,42 @@ static void LPTIM_IRQHandler(void)
330
356
lp_Fired = 0 ;
331
357
/* We're already in handler and interrupt might be pending,
332
358
* so clear the flag, to avoid calling irq_handler twice */
359
+ #if defined(LPTIM_FLAG_CC1 )
360
+ __HAL_LPTIM_CLEAR_FLAG (& LptimHandle , LPTIM_FLAG_CC1 );
361
+ #else
333
362
__HAL_LPTIM_CLEAR_FLAG (& LptimHandle , LPTIM_FLAG_CMPM );
363
+ #endif
334
364
lp_ticker_irq_handler ();
335
365
}
336
366
337
367
/* Compare match interrupt */
368
+ #if defined(LPTIM_FLAG_CC1 )
369
+ if (__HAL_LPTIM_GET_FLAG (& LptimHandle , LPTIM_FLAG_CC1 ) != RESET ) {
370
+ if (__HAL_LPTIM_GET_IT_SOURCE (& LptimHandle , LPTIM_IT_CC1 ) != RESET ) {
371
+ /* Clear Compare match flag */
372
+ __HAL_LPTIM_CLEAR_FLAG (& LptimHandle , LPTIM_FLAG_CC1 );
373
+ lp_ticker_irq_handler ();
374
+ }
375
+ }
376
+ #else
338
377
if (__HAL_LPTIM_GET_FLAG (& LptimHandle , LPTIM_FLAG_CMPM ) != RESET ) {
339
378
if (__HAL_LPTIM_GET_IT_SOURCE (& LptimHandle , LPTIM_IT_CMPM ) != RESET ) {
340
379
/* Clear Compare match flag */
341
380
__HAL_LPTIM_CLEAR_FLAG (& LptimHandle , LPTIM_FLAG_CMPM );
342
381
lp_ticker_irq_handler ();
343
382
}
344
383
}
384
+ #endif
345
385
386
+ #if defined(LPTIM_FLAG_CMP1OK )
387
+ if (__HAL_LPTIM_GET_FLAG (& LptimHandle , LPTIM_FLAG_CMP1OK ) != RESET ) {
388
+ if (__HAL_LPTIM_GET_IT_SOURCE (& LptimHandle , LPTIM_IT_CMP1OK ) != RESET ) {
389
+ __HAL_LPTIM_CLEAR_FLAG (& LptimHandle , LPTIM_FLAG_CMP1OK );
390
+ #else
346
391
if (__HAL_LPTIM_GET_FLAG (& LptimHandle , LPTIM_FLAG_CMPOK ) != RESET ) {
347
392
if (__HAL_LPTIM_GET_IT_SOURCE (& LptimHandle , LPTIM_IT_CMPOK ) != RESET ) {
348
393
__HAL_LPTIM_CLEAR_FLAG (& LptimHandle , LPTIM_FLAG_CMPOK );
394
+ #endif
349
395
lp_cmpok = true;
350
396
if (sleep_manager_locked ) {
351
397
sleep_manager_unlock_deep_sleep ();
@@ -379,11 +425,11 @@ static void LPTIM_IRQHandler(void)
379
425
}
380
426
}
381
427
382
-
383
428
#if defined (__HAL_LPTIM_WAKEUPTIMER_EXTI_CLEAR_FLAG )
384
429
/* EXTI lines are not configured by default */
385
430
__HAL_LPTIM_WAKEUPTIMER_EXTI_CLEAR_FLAG ();
386
431
#endif
432
+
387
433
core_util_critical_section_exit ();
388
434
}
389
435
@@ -466,10 +512,19 @@ void lp_ticker_set_interrupt(timestamp_t timestamp)
466
512
*/
467
513
if ((timestamp < last_read_counter ) && (last_read_counter <= (0xFFFF - LP_TIMER_SAFE_GUARD ))) {
468
514
/* Workaround, because limitation */
515
+ #if defined(LPTIM_CHANNEL_1 )
516
+ __HAL_LPTIM_COMPARE_SET (& LptimHandle , LPTIM_CHANNEL_1 , ~0 );
517
+ #else
469
518
__HAL_LPTIM_COMPARE_SET (& LptimHandle , ~0 );
519
+ #endif
470
520
} else {
471
521
/* It is safe to write */
522
+ #if defined(LPTIM_CHANNEL_1 )
523
+ __HAL_LPTIM_COMPARE_SET (& LptimHandle , LPTIM_CHANNEL_1 , timestamp );
524
+ #else
472
525
__HAL_LPTIM_COMPARE_SET (& LptimHandle , timestamp );
526
+ #endif
527
+
473
528
}
474
529
475
530
/* We just programed the CMP so we'll need to wait for cmpok before
@@ -501,9 +556,15 @@ void lp_ticker_disable_interrupt(void)
501
556
core_util_critical_section_enter ();
502
557
503
558
if (!lp_cmpok ) {
559
+ #if defined(LPTIM_FLAG_CMP1OK )
560
+ while (__HAL_LPTIM_GET_FLAG (& LptimHandle , LPTIM_FLAG_CMP1OK ) == RESET ) {
561
+ }
562
+ __HAL_LPTIM_CLEAR_FLAG (& LptimHandle , LPTIM_FLAG_CMP1OK );
563
+ #else
504
564
while (__HAL_LPTIM_GET_FLAG (& LptimHandle , LPTIM_FLAG_CMPOK ) == RESET ) {
505
565
}
506
566
__HAL_LPTIM_CLEAR_FLAG (& LptimHandle , LPTIM_FLAG_CMPOK );
567
+ #endif
507
568
lp_cmpok = true;
508
569
}
509
570
/* now that CMPOK is set, allow deep sleep again */
@@ -522,7 +583,11 @@ void lp_ticker_disable_interrupt(void)
522
583
void lp_ticker_clear_interrupt (void )
523
584
{
524
585
core_util_critical_section_enter ();
586
+ #if defined(LPTIM_FLAG_CC1 )
587
+ __HAL_LPTIM_CLEAR_FLAG (& LptimHandle , LPTIM_FLAG_CC1 );
588
+ #else
525
589
__HAL_LPTIM_CLEAR_FLAG (& LptimHandle , LPTIM_FLAG_CMPM );
590
+ #endif
526
591
NVIC_ClearPendingIRQ (LPTIM_MST_IRQ );
527
592
core_util_critical_section_exit ();
528
593
}
0 commit comments