Skip to content

Commit 310a1fe

Browse files
committed
[Nuvoton] Synchronize lp_ticker code to us_ticker
This is to make us_ticker/lp_ticker code consistent.
1 parent 8e11ddf commit 310a1fe

File tree

4 files changed

+64
-136
lines changed

4 files changed

+64
-136
lines changed

targets/TARGET_NUVOTON/TARGET_M451/us_ticker.c

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -40,37 +40,23 @@ static const struct nu_modinit_s timer0_modinit = {TIMER_0, TMR0_MODULE, CLK_CLK
4040

4141
#define TIMER_MODINIT timer0_modinit
4242

43-
/* S/W interrupt enable/disable
44-
*
45-
* Because H/W interrupt enable/disable (TIMER_EnableInt/TIMER_DisableInt) needs delay for lp_ticker,
46-
* we introduce S/W interrupt enable/disable to avoid blocking code. With S/W interrupt enable/disable,
47-
* H/W interrupt is always enabled after ticker_init. A S/W flag is used to tell whether or not
48-
* ticker_irq_handler is ready to call.
49-
*/
50-
51-
/* Ticker uninitialized */
52-
#define NU_TICKER_UNINIT 0
53-
/* Ticker initialized with interrupt disabled */
54-
#define NU_TICKER_INIT_INTR_DIS 1
55-
/* Ticker initialized with interrupt enabled */
56-
#define NU_TICKER_INIT_INTR_EN 2
57-
5843
/* Track ticker status */
59-
static volatile uint16_t ticker_stat = NU_TICKER_UNINIT;
44+
static volatile uint16_t ticker_inited = 0;
6045

6146
#define TMR_CMP_MIN 2
6247
#define TMR_CMP_MAX 0xFFFFFFu
6348

6449
void us_ticker_init(void)
6550
{
66-
if (ticker_stat) {
51+
if (ticker_inited) {
6752
/* By HAL spec, ticker_init allows the ticker to keep counting and disables the
6853
* ticker interrupt. */
6954
us_ticker_disable_interrupt();
7055
us_ticker_clear_interrupt();
56+
NVIC_ClearPendingIRQ(TIMER_MODINIT.irq_n);
7157
return;
7258
}
73-
ticker_stat = NU_TICKER_INIT_INTR_DIS;
59+
ticker_inited = 1;
7460

7561
// Reset IP
7662
SYS_ResetModule(TIMER_MODINIT.rsetidx);
@@ -96,7 +82,7 @@ void us_ticker_init(void)
9682

9783
NVIC_SetVector(TIMER_MODINIT.irq_n, (uint32_t) TIMER_MODINIT.var);
9884

99-
NVIC_EnableIRQ(TIMER_MODINIT.irq_n);
85+
NVIC_DisableIRQ(TIMER_MODINIT.irq_n);
10086

10187
TIMER_EnableInt(timer_base);
10288

@@ -122,12 +108,12 @@ void us_ticker_free(void)
122108
/* Disable IP clock */
123109
CLK_DisableModuleClock(TIMER_MODINIT.clkidx);
124110

125-
ticker_stat = NU_TICKER_UNINIT;
111+
ticker_inited = 0;
126112
}
127113

128114
uint32_t us_ticker_read()
129115
{
130-
if (ticker_stat == NU_TICKER_UNINIT) {
116+
if (! ticker_inited) {
131117
us_ticker_init();
132118
}
133119

@@ -138,9 +124,6 @@ uint32_t us_ticker_read()
138124

139125
void us_ticker_set_interrupt(timestamp_t timestamp)
140126
{
141-
/* We can call ticker_irq_handler now. */
142-
ticker_stat = NU_TICKER_INIT_INTR_EN;
143-
144127
/* In continuous mode, counter will be reset to zero with the following sequence:
145128
* 1. Stop counting
146129
* 2. Configure new CMP value
@@ -156,12 +139,15 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
156139
uint32_t cmp_timer = timestamp * NU_TMRCLK_PER_TICK;
157140
cmp_timer = NU_CLAMP(cmp_timer, TMR_CMP_MIN, TMR_CMP_MAX);
158141
timer_base->CMP = cmp_timer;
142+
143+
/* We can call ticker_irq_handler now. */
144+
NVIC_EnableIRQ(TIMER_MODINIT.irq_n);
159145
}
160146

161147
void us_ticker_disable_interrupt(void)
162148
{
163149
/* We cannot call ticker_irq_handler now. */
164-
ticker_stat = NU_TICKER_INIT_INTR_DIS;
150+
NVIC_DisableIRQ(TIMER_MODINIT.irq_n);
165151
}
166152

167153
void us_ticker_clear_interrupt(void)
@@ -171,12 +157,12 @@ void us_ticker_clear_interrupt(void)
171157

172158
void us_ticker_fire_interrupt(void)
173159
{
174-
/* We can call ticker_irq_handler now. */
175-
ticker_stat = NU_TICKER_INIT_INTR_EN;
176-
177160
// NOTE: This event was in the past. Set the interrupt as pending, but don't process it here.
178161
// This prevents a recursive loop under heavy load which can lead to a stack overflow.
179162
NVIC_SetPendingIRQ(TIMER_MODINIT.irq_n);
163+
164+
/* We can call ticker_irq_handler now. */
165+
NVIC_EnableIRQ(TIMER_MODINIT.irq_n);
180166
}
181167

182168
const ticker_info_t* us_ticker_get_info()
@@ -190,15 +176,11 @@ const ticker_info_t* us_ticker_get_info()
190176

191177
static void tmr0_vec(void)
192178
{
193-
/* NOTE: We need to clear interrupt flag earlier to reduce possibility of dummy interrupt.
194-
* This is because "clear interrupt flag" needs delay which isn't added here to avoid
195-
* blocking in ISR code. */
196179
us_ticker_clear_interrupt();
180+
us_ticker_disable_interrupt();
197181

198182
// NOTE: us_ticker_set_interrupt() may get called in us_ticker_irq_handler();
199-
if (ticker_stat == NU_TICKER_INIT_INTR_EN) {
200-
us_ticker_irq_handler();
201-
}
183+
us_ticker_irq_handler();
202184
}
203185

204186
#endif

targets/TARGET_NUVOTON/TARGET_M480/us_ticker.c

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -40,37 +40,23 @@ static const struct nu_modinit_s timer0_modinit = {TIMER_0, TMR0_MODULE, CLK_CLK
4040

4141
#define TIMER_MODINIT timer0_modinit
4242

43-
/* S/W interrupt enable/disable
44-
*
45-
* Because H/W interrupt enable/disable (TIMER_EnableInt/TIMER_DisableInt) needs delay for lp_ticker,
46-
* we introduce S/W interrupt enable/disable to avoid blocking code. With S/W interrupt enable/disable,
47-
* H/W interrupt is always enabled after ticker_init. A S/W flag is used to tell whether or not
48-
* ticker_irq_handler is ready to call.
49-
*/
50-
51-
/* Ticker uninitialized */
52-
#define NU_TICKER_UNINIT 0
53-
/* Ticker initialized with interrupt disabled */
54-
#define NU_TICKER_INIT_INTR_DIS 1
55-
/* Ticker initialized with interrupt enabled */
56-
#define NU_TICKER_INIT_INTR_EN 2
57-
5843
/* Track ticker status */
59-
static volatile uint16_t ticker_stat = NU_TICKER_UNINIT;
44+
static volatile uint16_t ticker_inited = 0;
6045

6146
#define TMR_CMP_MIN 2
6247
#define TMR_CMP_MAX 0xFFFFFFu
6348

6449
void us_ticker_init(void)
6550
{
66-
if (ticker_stat) {
51+
if (ticker_inited) {
6752
/* By HAL spec, ticker_init allows the ticker to keep counting and disables the
6853
* ticker interrupt. */
6954
us_ticker_disable_interrupt();
7055
us_ticker_clear_interrupt();
56+
NVIC_ClearPendingIRQ(TIMER_MODINIT.irq_n);
7157
return;
7258
}
73-
ticker_stat = NU_TICKER_INIT_INTR_DIS;
59+
ticker_inited = 1;
7460

7561
// Reset IP
7662
SYS_ResetModule(TIMER_MODINIT.rsetidx);
@@ -96,7 +82,7 @@ void us_ticker_init(void)
9682

9783
NVIC_SetVector(TIMER_MODINIT.irq_n, (uint32_t) TIMER_MODINIT.var);
9884

99-
NVIC_EnableIRQ(TIMER_MODINIT.irq_n);
85+
NVIC_DisableIRQ(TIMER_MODINIT.irq_n);
10086

10187
TIMER_EnableInt(timer_base);
10288

@@ -122,12 +108,12 @@ void us_ticker_free(void)
122108
/* Disable IP clock */
123109
CLK_DisableModuleClock(TIMER_MODINIT.clkidx);
124110

125-
ticker_stat = NU_TICKER_UNINIT;
111+
ticker_inited = 0;
126112
}
127113

128114
uint32_t us_ticker_read()
129115
{
130-
if (ticker_stat == NU_TICKER_UNINIT) {
116+
if (! ticker_inited) {
131117
us_ticker_init();
132118
}
133119

@@ -138,9 +124,6 @@ uint32_t us_ticker_read()
138124

139125
void us_ticker_set_interrupt(timestamp_t timestamp)
140126
{
141-
/* We can call ticker_irq_handler now. */
142-
ticker_stat = NU_TICKER_INIT_INTR_EN;
143-
144127
/* In continuous mode, counter will be reset to zero with the following sequence:
145128
* 1. Stop counting
146129
* 2. Configure new CMP value
@@ -156,12 +139,15 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
156139
uint32_t cmp_timer = timestamp * NU_TMRCLK_PER_TICK;
157140
cmp_timer = NU_CLAMP(cmp_timer, TMR_CMP_MIN, TMR_CMP_MAX);
158141
timer_base->CMP = cmp_timer;
142+
143+
/* We can call ticker_irq_handler now. */
144+
NVIC_EnableIRQ(TIMER_MODINIT.irq_n);
159145
}
160146

161147
void us_ticker_disable_interrupt(void)
162148
{
163149
/* We cannot call ticker_irq_handler now. */
164-
ticker_stat = NU_TICKER_INIT_INTR_DIS;
150+
NVIC_DisableIRQ(TIMER_MODINIT.irq_n);
165151
}
166152

167153
void us_ticker_clear_interrupt(void)
@@ -171,12 +157,12 @@ void us_ticker_clear_interrupt(void)
171157

172158
void us_ticker_fire_interrupt(void)
173159
{
174-
/* We can call ticker_irq_handler now. */
175-
ticker_stat = NU_TICKER_INIT_INTR_EN;
176-
177160
// NOTE: This event was in the past. Set the interrupt as pending, but don't process it here.
178161
// This prevents a recursive loop under heavy load which can lead to a stack overflow.
179162
NVIC_SetPendingIRQ(TIMER_MODINIT.irq_n);
163+
164+
/* We can call ticker_irq_handler now. */
165+
NVIC_EnableIRQ(TIMER_MODINIT.irq_n);
180166
}
181167

182168
const ticker_info_t* us_ticker_get_info()
@@ -190,15 +176,11 @@ const ticker_info_t* us_ticker_get_info()
190176

191177
static void tmr0_vec(void)
192178
{
193-
/* NOTE: We need to clear interrupt flag earlier to reduce possibility of dummy interrupt.
194-
* This is because "clear interrupt flag" needs delay which isn't added here to avoid
195-
* blocking in ISR code. */
196179
us_ticker_clear_interrupt();
180+
us_ticker_disable_interrupt();
197181

198182
// NOTE: us_ticker_set_interrupt() may get called in us_ticker_irq_handler();
199-
if (ticker_stat == NU_TICKER_INIT_INTR_EN) {
200-
us_ticker_irq_handler();
201-
}
183+
us_ticker_irq_handler();
202184
}
203185

204186
#endif

targets/TARGET_NUVOTON/TARGET_NANO100/us_ticker.c

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -42,37 +42,23 @@ static const struct nu_modinit_s timer0_modinit = {TIMER_0, TMR0_MODULE, CLK_CLK
4242

4343
#define TIMER_MODINIT timer0_modinit
4444

45-
/* S/W interrupt enable/disable
46-
*
47-
* Because H/W interrupt enable/disable (TIMER_EnableInt/TIMER_DisableInt) needs delay for lp_ticker,
48-
* we introduce S/W interrupt enable/disable to avoid blocking code. With S/W interrupt enable/disable,
49-
* H/W interrupt is always enabled after ticker_init. A S/W flag is used to tell whether or not
50-
* ticker_irq_handler is ready to call.
51-
*/
52-
53-
/* Ticker uninitialized */
54-
#define NU_TICKER_UNINIT 0
55-
/* Ticker initialized with interrupt disabled */
56-
#define NU_TICKER_INIT_INTR_DIS 1
57-
/* Ticker initialized with interrupt enabled */
58-
#define NU_TICKER_INIT_INTR_EN 2
59-
6045
/* Track ticker status */
61-
static volatile uint16_t ticker_stat = NU_TICKER_UNINIT;
46+
static volatile uint16_t ticker_inited = 0;
6247

6348
#define TMR_CMP_MIN 2
6449
#define TMR_CMP_MAX 0xFFFFFFu
6550

6651
void us_ticker_init(void)
6752
{
68-
if (ticker_stat) {
53+
if (ticker_inited) {
6954
/* By HAL spec, ticker_init allows the ticker to keep counting and disables the
7055
* ticker interrupt. */
7156
us_ticker_disable_interrupt();
7257
us_ticker_clear_interrupt();
58+
NVIC_ClearPendingIRQ(TIMER_MODINIT.irq_n);
7359
return;
7460
}
75-
ticker_stat = NU_TICKER_INIT_INTR_DIS;
61+
ticker_inited = 1;
7662

7763
// Reset IP
7864
SYS_ResetModule(TIMER_MODINIT.rsetidx);
@@ -98,7 +84,7 @@ void us_ticker_init(void)
9884

9985
NVIC_SetVector(TIMER_MODINIT.irq_n, (uint32_t) TIMER_MODINIT.var);
10086

101-
NVIC_EnableIRQ(TIMER_MODINIT.irq_n);
87+
NVIC_DisableIRQ(TIMER_MODINIT.irq_n);
10288

10389
TIMER_EnableInt(timer_base);
10490

@@ -124,12 +110,12 @@ void us_ticker_free(void)
124110
/* Disable IP clock */
125111
CLK_DisableModuleClock(TIMER_MODINIT.clkidx);
126112

127-
ticker_stat = NU_TICKER_UNINIT;
113+
ticker_inited = 0;
128114
}
129115

130116
uint32_t us_ticker_read()
131117
{
132-
if (ticker_stat == NU_TICKER_UNINIT) {
118+
if (! ticker_inited) {
133119
us_ticker_init();
134120
}
135121

@@ -140,9 +126,6 @@ uint32_t us_ticker_read()
140126

141127
void us_ticker_set_interrupt(timestamp_t timestamp)
142128
{
143-
/* We can call ticker_irq_handler now. */
144-
ticker_stat = NU_TICKER_INIT_INTR_EN;
145-
146129
/* In continuous mode, counter will be reset to zero with the following sequence:
147130
* 1. Stop counting
148131
* 2. Configure new CMP value
@@ -158,12 +141,15 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
158141
uint32_t cmp_timer = timestamp * NU_TMRCLK_PER_TICK;
159142
cmp_timer = NU_CLAMP(cmp_timer, TMR_CMP_MIN, TMR_CMP_MAX);
160143
timer_base->CMPR = cmp_timer;
144+
145+
/* We can call ticker_irq_handler now. */
146+
NVIC_EnableIRQ(TIMER_MODINIT.irq_n);
161147
}
162148

163149
void us_ticker_disable_interrupt(void)
164150
{
165151
/* We cannot call ticker_irq_handler now. */
166-
ticker_stat = NU_TICKER_INIT_INTR_DIS;
152+
NVIC_DisableIRQ(TIMER_MODINIT.irq_n);
167153
}
168154

169155
void us_ticker_clear_interrupt(void)
@@ -173,12 +159,12 @@ void us_ticker_clear_interrupt(void)
173159

174160
void us_ticker_fire_interrupt(void)
175161
{
176-
/* We can call ticker_irq_handler now. */
177-
ticker_stat = NU_TICKER_INIT_INTR_EN;
178-
179162
// NOTE: This event was in the past. Set the interrupt as pending, but don't process it here.
180163
// This prevents a recursive loop under heavy load which can lead to a stack overflow.
181164
NVIC_SetPendingIRQ(TIMER_MODINIT.irq_n);
165+
166+
/* We can call ticker_irq_handler now. */
167+
NVIC_EnableIRQ(TIMER_MODINIT.irq_n);
182168
}
183169

184170
const ticker_info_t* us_ticker_get_info()
@@ -192,15 +178,11 @@ const ticker_info_t* us_ticker_get_info()
192178

193179
void TMR0_IRQHandler(void)
194180
{
195-
/* NOTE: We need to clear interrupt flag earlier to reduce possibility of dummy interrupt.
196-
* This is because "clear interrupt flag" needs delay which isn't added here to avoid
197-
* blocking in ISR code. */
198181
us_ticker_clear_interrupt();
182+
us_ticker_disable_interrupt();
199183

200184
// NOTE: us_ticker_set_interrupt() may get called in us_ticker_irq_handler();
201-
if (ticker_stat == NU_TICKER_INIT_INTR_EN) {
202-
us_ticker_irq_handler();
203-
}
185+
us_ticker_irq_handler();
204186
}
205187

206188
#endif

0 commit comments

Comments
 (0)