Skip to content

Commit 6729b65

Browse files
committed
[M2351] Meet new lp_ticker HAL spec (Mbed OS 5.9)
1. Add LPTICKER in device_has option of targets.json file. 2. Disable interrupt in lp_ticker_init 3. Add lp_ticker_free 4. Enable interrupt in lp_ticker_set_interrupt/lp_ticker_fire_interrupt 5. Disable interupt in ISR
1 parent 9cbc8b2 commit 6729b65

File tree

2 files changed

+83
-15
lines changed

2 files changed

+83
-15
lines changed

targets/TARGET_NUVOTON/TARGET_M2351/lp_ticker.c

Lines changed: 81 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
#include "lp_ticker_api.h"
1818

19-
#if DEVICE_LOWPOWERTIMER
19+
#if DEVICE_LPTICKER
2020

2121
#include "sleep_api.h"
2222
#include "mbed_wait_api.h"
@@ -67,7 +67,14 @@ static const struct nu_modinit_s timer3_modinit = {TIMER_3, TMR3_MODULE, CLK_CLK
6767

6868
#endif
6969

70-
static int ticker_inited = 0;
70+
/* Timer interrupt enable/disable
71+
*
72+
* Because Timer interrupt enable/disable (TIMER_EnableInt/TIMER_DisableInt) needs wait for lp_ticker,
73+
* we call NVIC_DisableIRQ/NVIC_EnableIRQ instead.
74+
*/
75+
76+
/* Track ticker status */
77+
static volatile uint16_t ticker_inited = 0;
7178

7279
#define TMR_CMP_MIN 2
7380
#define TMR_CMP_MAX 0xFFFFFFu
@@ -78,6 +85,11 @@ static int ticker_inited = 0;
7885
void lp_ticker_init(void)
7986
{
8087
if (ticker_inited) {
88+
/* By HAL spec, ticker_init allows the ticker to keep counting and disables the
89+
* ticker interrupt. */
90+
lp_ticker_disable_interrupt();
91+
lp_ticker_clear_interrupt();
92+
NVIC_ClearPendingIRQ(TIMER_MODINIT.irq_n);
8193
return;
8294
}
8395
ticker_inited = 1;
@@ -120,7 +132,7 @@ void lp_ticker_init(void)
120132
// Set vector
121133
NVIC_SetVector(TIMER_MODINIT.irq_n, (uint32_t) TIMER_MODINIT.var);
122134

123-
NVIC_EnableIRQ(TIMER_MODINIT.irq_n);
135+
NVIC_DisableIRQ(TIMER_MODINIT.irq_n);
124136

125137
TIMER_EnableInt(timer_base);
126138
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
@@ -135,6 +147,36 @@ void lp_ticker_init(void)
135147
while(! (timer_base->CTL & TIMER_CTL_ACTSTS_Msk));
136148
}
137149

150+
void lp_ticker_free(void)
151+
{
152+
TIMER_T *timer_base = (TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname);
153+
154+
/* Stop counting */
155+
TIMER_Stop(timer_base);
156+
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
157+
158+
/* Wait for timer to stop counting and unset active flag */
159+
while((timer_base->CTL & TIMER_CTL_ACTSTS_Msk));
160+
161+
/* Disable wakeup */
162+
TIMER_DisableWakeup(timer_base);
163+
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
164+
165+
/* Disable interrupt */
166+
TIMER_DisableInt(timer_base);
167+
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
168+
169+
NVIC_DisableIRQ(TIMER_MODINIT.irq_n);
170+
171+
/* Disable IP clock
172+
*
173+
* NOTE: We must call secure version (from non-secure domain) because SYS/CLK regions are secure.
174+
*/
175+
CLK_DisableModuleClock_S(TIMER_MODINIT.clkidx);
176+
177+
ticker_inited = 0;
178+
}
179+
138180
timestamp_t lp_ticker_read()
139181
{
140182
if (! ticker_inited) {
@@ -163,27 +205,39 @@ void lp_ticker_set_interrupt(timestamp_t timestamp)
163205
uint32_t cmp_timer = timestamp * NU_TMRCLK_PER_TICK;
164206
cmp_timer = NU_CLAMP(cmp_timer, TMR_CMP_MIN, TMR_CMP_MAX);
165207

208+
/* NOTE: Rely on LPTICKER_DELAY_TICKS to be non-blocking. */
166209
timer_base->CMP = cmp_timer;
167-
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
210+
211+
/* We can call ticker_irq_handler now. */
212+
NVIC_EnableIRQ(TIMER_MODINIT.irq_n);
168213
}
169214

170215
void lp_ticker_disable_interrupt(void)
171216
{
172-
TIMER_DisableInt((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname));
173-
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
217+
/* We cannot call ticker_irq_handler now. */
218+
NVIC_DisableIRQ(TIMER_MODINIT.irq_n);
174219
}
175220

176221
void lp_ticker_clear_interrupt(void)
177222
{
178-
TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname));
179-
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
223+
/* To avoid sync issue, we clear TIF/TWKF simultaneously rather than call separate
224+
* driver API:
225+
*
226+
* TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname));
227+
* TIMER_ClearWakeupFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname));
228+
*/
229+
TIMER_T *timer_base = (TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname);
230+
timer_base->INTSTS = TIMER_INTSTS_TIF_Msk | TIMER_INTSTS_TWKF_Msk;
180231
}
181232

182233
void lp_ticker_fire_interrupt(void)
183234
{
184235
// NOTE: This event was in the past. Set the interrupt as pending, but don't process it here.
185236
// This prevents a recursive loop under heavy load which can lead to a stack overflow.
186237
NVIC_SetPendingIRQ(TIMER_MODINIT.irq_n);
238+
239+
/* We can call ticker_irq_handler now. */
240+
NVIC_EnableIRQ(TIMER_MODINIT.irq_n);
187241
}
188242

189243
const ticker_info_t* lp_ticker_get_info()
@@ -201,11 +255,25 @@ static void tmr1_vec(void)
201255
static void tmr3_vec(void)
202256
#endif
203257
{
204-
TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname));
205-
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
206-
207-
TIMER_ClearWakeupFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname));
208-
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
258+
/* NOTE: Avoid blocking in ISR due to wait for "clear interrupt flag"
259+
*
260+
* "clear interrupt flag" needs wait to take effect which isn't added here to avoid
261+
* blocking in ISR.
262+
*
263+
* Continuing above, we will get stuck in ISR due to dummy interrupt until
264+
* "clear interrupt flag" takes effect. To avoid it, we disable interrupt here and enable
265+
* interrupt in lp_ticker_fire_interrupt/lp_ticker_set_interrupt. There is another risk
266+
* that we may get stuck in a loop of ISR and lp_ticker_fire_interrupt/
267+
* lp_ticker_set_interrupt (called by lp_ticker_irq_handler), but actually we don't:
268+
* 1. When lp_ticker_fire_interrupt gets called, it means there is a past event and so this
269+
* interrupt isn't dummy.
270+
* 2. With LPTICKER_DELAY_TICKS enabled, it is lp_ticker_set_interrupt_wrapper rather than
271+
* lp_ticker_set_interrupt that gets straight called. lp_ticker_set_interrupt_wrapper
272+
* guarantees that lp_ticker_set_interrupt won't get re-called in LPTICKER_DELAY_TICKS ticks
273+
* which is just enough for "clear interrupt flag" to take effect.
274+
*/
275+
lp_ticker_clear_interrupt();
276+
lp_ticker_disable_interrupt();
209277

210278
// NOTE: lp_ticker_set_interrupt() may get called in lp_ticker_irq_handler();
211279
lp_ticker_irq_handler();

targets/targets.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4224,7 +4224,7 @@
42244224
"default_toolchain": "GCC_ARM",
42254225
"extra_labels": ["NUVOTON", "M2351", "M2351KIAAEES", "FLASH_CMSIS_ALGO"],
42264226
"OUTPUT_EXT": "hex",
4227-
"macros": ["MBED_FAULT_HANDLER_DISABLED", "MBED_TZ_DEFAULT_ACCESS=1"],
4227+
"macros": ["MBED_FAULT_HANDLER_DISABLED", "MBED_TZ_DEFAULT_ACCESS=1", "LPTICKER_DELAY_TICKS=3"],
42284228
"is_disk_virtual": true,
42294229
"supported_toolchains": ["GCC_ARM", "IAR", "ARMC6"],
42304230
"config": {
@@ -4246,7 +4246,7 @@
42464246
}
42474247
},
42484248
"inherits": ["Target"],
4249-
"device_has": ["USTICKER", "ANALOGIN", "I2C", "I2CSLAVE", "I2C_ASYNCH", "LOWPOWERTIMER", "RTC", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "STDIO_MESSAGES", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "TRNG", "FLASH"],
4249+
"device_has": ["USTICKER", "LPTICKER", "ANALOGIN", "I2C", "I2CSLAVE", "I2C_ASYNCH", "RTC", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "STDIO_MESSAGES", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "TRNG", "FLASH"],
42504250
"detect_code": ["1305"],
42514251
"release_versions": ["5"],
42524252
"device_name": "M2351KIAAEES",

0 commit comments

Comments
 (0)