Skip to content

Commit 451af7f

Browse files
jeromecoutantbulislaw
authored andcommitted
STM32 LPTICKER update for targets supporting LPTIMER
1 parent c79009a commit 451af7f

File tree

1 file changed

+24
-88
lines changed

1 file changed

+24
-88
lines changed

targets/TARGET_STM/lp_ticker.c

Lines changed: 24 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,58 @@ 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+
return lp_time;
235183
}
236184

237185
void lp_ticker_set_interrupt(timestamp_t timestamp)
238186
{
239-
// Disable IRQs
240-
core_util_critical_section_enter();
241-
242-
uint32_t timestamp_TickCounter = timestamp / (uint32_t)lp_TickPeriod_us;
243-
244187
LptimHandle.Instance = LPTIM1;
245188
irq_handler = (void (*)(void))lp_ticker_irq_handler;
246189

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-
251190
/* 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 */
252192
while (__HAL_LPTIM_GET_FLAG(&LptimHandle, LPTIM_FLAG_CMPOK) == RESET) {
253193
}
254194

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);
263198

264-
// Enable IRQs
265-
core_util_critical_section_exit();
199+
NVIC_EnableIRQ(LPTIM1_IRQn);
266200
}
267201

268202
void lp_ticker_fire_interrupt(void)
269203
{
270204
lp_Fired = 1;
271205
NVIC_SetPendingIRQ(LPTIM1_IRQn);
206+
NVIC_EnableIRQ(LPTIM1_IRQn);
272207
}
273208

274209
void lp_ticker_disable_interrupt(void)
275210
{
276211
LptimHandle.Instance = LPTIM1;
277-
__HAL_LPTIM_DISABLE_IT(&LptimHandle, LPTIM_IT_CMPM);
212+
NVIC_DisableIRQ(LPTIM1_IRQn);
278213
}
279214

280215
void lp_ticker_clear_interrupt(void)
281216
{
282217
LptimHandle.Instance = LPTIM1;
283218
__HAL_LPTIM_CLEAR_FLAG(&LptimHandle, LPTIM_FLAG_CMPM);
219+
NVIC_ClearPendingIRQ(LPTIM1_IRQn);
284220
}
285221

286222
#else /* MBED_CONF_TARGET_LPTICKER_LPTIM */

0 commit comments

Comments
 (0)