Skip to content

Commit 9cbc8b2

Browse files
committed
[M2351] Meet new us_ticker HAL spec (Mbed OS 5.9)
1. Add USTICKER in device_has option of targets.json file. 2. Disable interrupt in us_ticker_init 3. Add us_ticker_free 4. Enable interrupt in us_ticker_set_interrupt/us_ticker_fire_interrupt 5. Disable interrupt in ISR
1 parent de83cb2 commit 9cbc8b2

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

targets/TARGET_NUVOTON/TARGET_M2351/us_ticker.c

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616

1717
#include "us_ticker_api.h"
18+
19+
#if DEVICE_USTICKER
20+
1821
#include "sleep_api.h"
1922
#include "mbed_assert.h"
2023
#include "nu_modutil.h"
@@ -60,15 +63,20 @@ static const struct nu_modinit_s timer2_modinit = {TIMER_2, TMR2_MODULE, CLK_CLK
6063
#define TIMER_MODINIT timer2_modinit
6164

6265
#endif
63-
64-
static int ticker_inited = 0;
66+
/* Track ticker status */
67+
static volatile uint16_t ticker_inited = 0;
6568

6669
#define TMR_CMP_MIN 2
6770
#define TMR_CMP_MAX 0xFFFFFFu
6871

6972
void us_ticker_init(void)
7073
{
7174
if (ticker_inited) {
75+
/* By HAL spec, ticker_init allows the ticker to keep counting and disables the
76+
* ticker interrupt. */
77+
us_ticker_disable_interrupt();
78+
us_ticker_clear_interrupt();
79+
NVIC_ClearPendingIRQ(TIMER_MODINIT.irq_n);
7280
return;
7381
}
7482
ticker_inited = 1;
@@ -106,7 +114,7 @@ void us_ticker_init(void)
106114

107115
NVIC_SetVector(TIMER_MODINIT.irq_n, (uint32_t) TIMER_MODINIT.var);
108116

109-
NVIC_EnableIRQ(TIMER_MODINIT.irq_n);
117+
NVIC_DisableIRQ(TIMER_MODINIT.irq_n);
110118

111119
TIMER_EnableInt(timer_base);
112120

@@ -115,6 +123,29 @@ void us_ticker_init(void)
115123
while(! (timer_base->CTL & TIMER_CTL_ACTSTS_Msk));
116124
}
117125

126+
void us_ticker_free(void)
127+
{
128+
TIMER_T *timer_base = (TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname);
129+
130+
/* Stop counting */
131+
TIMER_Stop(timer_base);
132+
133+
/* Wait for timer to stop counting and unset active flag */
134+
while((timer_base->CTL & TIMER_CTL_ACTSTS_Msk));
135+
136+
/* Disable interrupt */
137+
TIMER_DisableInt(timer_base);
138+
NVIC_DisableIRQ(TIMER_MODINIT.irq_n);
139+
140+
/* Disable IP clock
141+
*
142+
* NOTE: We must call secure version (from non-secure domain) because SYS/CLK regions are secure.
143+
*/
144+
CLK_DisableModuleClock_S(TIMER_MODINIT.clkidx);
145+
146+
ticker_inited = 0;
147+
}
148+
118149
uint32_t us_ticker_read()
119150
{
120151
if (! ticker_inited) {
@@ -143,11 +174,15 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
143174
uint32_t cmp_timer = timestamp * NU_TMRCLK_PER_TICK;
144175
cmp_timer = NU_CLAMP(cmp_timer, TMR_CMP_MIN, TMR_CMP_MAX);
145176
timer_base->CMP = cmp_timer;
177+
178+
/* We can call ticker_irq_handler now. */
179+
NVIC_EnableIRQ(TIMER_MODINIT.irq_n);
146180
}
147181

148182
void us_ticker_disable_interrupt(void)
149183
{
150-
TIMER_DisableInt((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname));
184+
/* We cannot call ticker_irq_handler now. */
185+
NVIC_DisableIRQ(TIMER_MODINIT.irq_n);
151186
}
152187

153188
void us_ticker_clear_interrupt(void)
@@ -160,6 +195,9 @@ void us_ticker_fire_interrupt(void)
160195
// NOTE: This event was in the past. Set the interrupt as pending, but don't process it here.
161196
// This prevents a recursive loop under heavy load which can lead to a stack overflow.
162197
NVIC_SetPendingIRQ(TIMER_MODINIT.irq_n);
198+
199+
/* We can call ticker_irq_handler now. */
200+
NVIC_EnableIRQ(TIMER_MODINIT.irq_n);
163201
}
164202

165203
const ticker_info_t* us_ticker_get_info()
@@ -177,8 +215,11 @@ static void tmr0_vec(void)
177215
static void tmr2_vec(void)
178216
#endif
179217
{
180-
TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname));
181-
218+
us_ticker_clear_interrupt();
219+
us_ticker_disable_interrupt();
220+
182221
// NOTE: us_ticker_set_interrupt() may get called in us_ticker_irq_handler();
183222
us_ticker_irq_handler();
184223
}
224+
225+
#endif

targets/targets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4246,7 +4246,7 @@
42464246
}
42474247
},
42484248
"inherits": ["Target"],
4249-
"device_has": ["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", "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"],
42504250
"detect_code": ["1305"],
42514251
"release_versions": ["5"],
42524252
"device_name": "M2351KIAAEES",

0 commit comments

Comments
 (0)