Skip to content

Commit 0acd761

Browse files
author
Cruz Monrreal
authored
Merge pull request #6474 from jeromecoutant/PR_LPTICKER_NEWAPI_LPTIM
STM32 LPTICKER update for targets supporting LPTIMER
2 parents df50e3e + a4b8979 commit 0acd761

File tree

4 files changed

+59
-114
lines changed

4 files changed

+59
-114
lines changed

hal/mbed_lp_ticker_api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static const ticker_interface_t lp_interface = {
2828
.read = lp_ticker_read,
2929
.disable_interrupt = lp_ticker_disable_interrupt,
3030
.clear_interrupt = lp_ticker_clear_interrupt,
31-
#if LOWPOWERTIMER_DELAY_TICKS > 0
31+
#if LPTICKER_DELAY_TICKS > 0
3232
.set_interrupt = lp_ticker_set_interrupt_wrapper,
3333
#else
3434
.set_interrupt = lp_ticker_set_interrupt,

hal/mbed_lp_ticker_wrapper.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
*/
1616
#include "hal/lp_ticker_api.h"
1717

18-
#if DEVICE_LOWPOWERTIMER && (LOWPOWERTIMER_DELAY_TICKS > 0)
18+
#if DEVICE_LPTICKER && (LPTICKER_DELAY_TICKS > 0)
1919

2020
#include "Timeout.h"
2121
#include "mbed_critical.h"
2222

23-
static const timestamp_t min_delta = LOWPOWERTIMER_DELAY_TICKS;
23+
static const timestamp_t min_delta = LPTICKER_DELAY_TICKS;
2424

2525
static bool init = false;
2626
static bool pending = false;
@@ -108,7 +108,7 @@ static void set_interrupt_later()
108108
* Wrapper around lp_ticker_set_interrupt to prevent blocking
109109
*
110110
* Problems this function is solving:
111-
* 1. Interrupt may not fire if set earlier than LOWPOWERTIMER_DELAY_TICKS low power clock cycles
111+
* 1. Interrupt may not fire if set earlier than LPTICKER_DELAY_TICKS low power clock cycles
112112
* 2. Setting the interrupt back-to-back will block
113113
*
114114
* This wrapper function prevents lp_ticker_set_interrupt from being called

targets/TARGET_STM/lp_ticker.c

Lines changed: 29 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,24 @@
3636

3737
LPTIM_HandleTypeDef LptimHandle;
3838

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+
4248
volatile uint8_t lp_Fired = 0;
4349

4450
static void LPTIM1_IRQHandler(void);
4551
static void (*irq_handler)(void);
4652

47-
4853
void lp_ticker_init(void)
4954
{
55+
NVIC_DisableIRQ(LPTIM1_IRQn);
56+
5057
/* Check if LPTIM is already configured */
5158
#if (TARGET_STM32L0)
5259
if (READ_BIT(RCC->APB1ENR, RCC_APB1ENR_LPTIM1EN) != RESET) {
@@ -111,21 +118,7 @@ void lp_ticker_init(void)
111118
LptimHandle.Instance = LPTIM1;
112119
LptimHandle.State = HAL_LPTIM_STATE_RESET;
113120
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;
129122

130123
LptimHandle.Init.Trigger.Source = LPTIM_TRIGSOURCE_SOFTWARE;
131124
LptimHandle.Init.OutputPolarity = LPTIM_OUTPUTPOLARITY_HIGH;
@@ -142,18 +135,17 @@ void lp_ticker_init(void)
142135
}
143136

144137
NVIC_SetVector(LPTIM1_IRQn, (uint32_t)LPTIM1_IRQHandler);
145-
NVIC_EnableIRQ(LPTIM1_IRQn);
146138

147139
#if !(TARGET_STM32L4)
148140
/* EXTI lines are not configured by default */
149141
__HAL_LPTIM_WAKEUPTIMER_EXTI_ENABLE_IT();
150142
__HAL_LPTIM_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE();
151143
#endif
152144

153-
__HAL_LPTIM_ENABLE_IT(&LptimHandle, LPTIM_IT_ARRM);
154145
__HAL_LPTIM_ENABLE_IT(&LptimHandle, LPTIM_IT_CMPM);
155-
__HAL_LPTIM_ENABLE_IT(&LptimHandle, LPTIM_IT_CMPOK);
156146
HAL_LPTIM_Counter_Start(&LptimHandle, 0xFFFF);
147+
148+
__HAL_LPTIM_COMPARE_SET(&LptimHandle, 0);
157149
}
158150

159151
static void LPTIM1_IRQHandler(void)
@@ -173,114 +165,63 @@ static void LPTIM1_IRQHandler(void)
173165
/* Clear Compare match flag */
174166
__HAL_LPTIM_CLEAR_FLAG(&LptimHandle, LPTIM_FLAG_CMPM);
175167

176-
if (lp_oc_int_part > 0) {
177-
lp_oc_int_part--;
178-
} else {
179168
if (irq_handler) {
180169
irq_handler();
181-
}
182170
}
183171
}
184172
}
185173

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-
203174
#if !(TARGET_STM32L4)
204175
__HAL_LPTIM_WAKEUPTIMER_EXTI_CLEAR_FLAG();
205176
#endif
206177
}
207178

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-
231179
uint32_t lp_ticker_read(void)
232180
{
233-
lp_ticker_init();
234-
return lp_ticker_read_TickCounter() * (uint32_t)lp_TickPeriod_us;
181+
uint32_t lp_time = LPTIM1->CNT;
182+
/* Reading the LPTIM_CNT register may return unreliable values.
183+
It is necessary to perform two consecutive read accesses and verify that the two returned values are identical */
184+
while (lp_time != LPTIM1->CNT) {
185+
lp_time = LPTIM1->CNT;
186+
}
187+
return lp_time;
235188
}
236189

237190
void lp_ticker_set_interrupt(timestamp_t timestamp)
238191
{
239-
// Disable IRQs
240-
core_util_critical_section_enter();
241-
242-
uint32_t timestamp_TickCounter = timestamp / (uint32_t)lp_TickPeriod_us;
243-
244192
LptimHandle.Instance = LPTIM1;
245193
irq_handler = (void (*)(void))lp_ticker_irq_handler;
246194

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-
251195
/* CMPOK is set by hardware to inform application that the APB bus write operation to the LPTIM_CMP register has been successfully completed */
196+
/* Any successive write before respectively the ARROK flag or the CMPOK flag be set, will lead to unpredictable results */
252197
while (__HAL_LPTIM_GET_FLAG(&LptimHandle, LPTIM_FLAG_CMPOK) == RESET) {
253198
}
254199

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-
}
200+
__HAL_LPTIM_CLEAR_FLAG(&LptimHandle, LPTIM_FLAG_CMPOK);
201+
__HAL_LPTIM_CLEAR_FLAG(&LptimHandle, LPTIM_FLAG_CMPM);
202+
__HAL_LPTIM_COMPARE_SET(&LptimHandle, timestamp);
263203

264-
// Enable IRQs
265-
core_util_critical_section_exit();
204+
NVIC_EnableIRQ(LPTIM1_IRQn);
266205
}
267206

268207
void lp_ticker_fire_interrupt(void)
269208
{
270209
lp_Fired = 1;
271210
NVIC_SetPendingIRQ(LPTIM1_IRQn);
211+
NVIC_EnableIRQ(LPTIM1_IRQn);
272212
}
273213

274214
void lp_ticker_disable_interrupt(void)
275215
{
276216
LptimHandle.Instance = LPTIM1;
277-
__HAL_LPTIM_DISABLE_IT(&LptimHandle, LPTIM_IT_CMPM);
217+
NVIC_DisableIRQ(LPTIM1_IRQn);
278218
}
279219

280220
void lp_ticker_clear_interrupt(void)
281221
{
282222
LptimHandle.Instance = LPTIM1;
283223
__HAL_LPTIM_CLEAR_FLAG(&LptimHandle, LPTIM_FLAG_CMPM);
224+
NVIC_ClearPendingIRQ(LPTIM1_IRQn);
284225
}
285226

286227
#else /* MBED_CONF_TARGET_LPTICKER_LPTIM */

0 commit comments

Comments
 (0)