[Nuvoton] Reduce blocking code in lp_ticker

1. Introduce S/W interrupt enable/disable to reduce calls to TIMER_EnableInt/TIMER_DisableInt.
2. Allow dummy interrupt because clear interrupt flag is not synchronized.
3. Enable LPTICKER_DELAY_TICKS to make lp_ticker_set_interrupt non-blocking.
pull/7029/head
ccli8 2018-06-01 15:37:53 +08:00
parent b18f690b39
commit 86e194d075
5 changed files with 188 additions and 83 deletions

View File

@ -42,7 +42,23 @@ static const struct nu_modinit_s timer1_modinit = {TIMER_1, TMR1_MODULE, CLK_CLK
#define TIMER_MODINIT timer1_modinit #define TIMER_MODINIT timer1_modinit
static int ticker_inited = 0; /* S/W interrupt enable/disable
*
* Because H/W interrupt enable/disable (TIMER_EnableInt/TIMER_DisableInt) needs delay for lp_ticker,
* we introduce S/W interrupt enable/disable to avoid blocking code. With S/W interrupt enable/disable,
* H/W interrupt is always enabled after ticker_init. A S/W flag is used to tell whether or not
* ticker_irq_handler is ready to call.
*/
/* Ticker uninitialized */
#define NU_TICKER_UNINIT 0
/* Ticker initialized with interrupt disabled */
#define NU_TICKER_INIT_INTR_DIS 1
/* Ticker initialized with interrupt enabled */
#define NU_TICKER_INIT_INTR_EN 2
/* Track ticker status */
static volatile uint16_t ticker_stat = NU_TICKER_UNINIT;
#define TMR_CMP_MIN 2 #define TMR_CMP_MIN 2
#define TMR_CMP_MAX 0xFFFFFFu #define TMR_CMP_MAX 0xFFFFFFu
@ -52,14 +68,14 @@ static int ticker_inited = 0;
void lp_ticker_init(void) void lp_ticker_init(void)
{ {
if (ticker_inited) { if (ticker_stat) {
/* By HAL spec, ticker_init allows the ticker to keep counting and disables the /* By HAL spec, ticker_init allows the ticker to keep counting and disables the
* ticker interrupt. */ * ticker interrupt. */
lp_ticker_disable_interrupt(); lp_ticker_disable_interrupt();
lp_ticker_clear_interrupt(); lp_ticker_clear_interrupt();
return; return;
} }
ticker_inited = 1; ticker_stat = NU_TICKER_INIT_INTR_DIS;
// Reset module // Reset module
SYS_ResetModule(TIMER_MODINIT.rsetidx); SYS_ResetModule(TIMER_MODINIT.rsetidx);
@ -92,7 +108,7 @@ void lp_ticker_init(void)
NVIC_EnableIRQ(TIMER_MODINIT.irq_n); NVIC_EnableIRQ(TIMER_MODINIT.irq_n);
TIMER_DisableInt(timer_base); TIMER_EnableInt(timer_base);
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
TIMER_EnableWakeup(timer_base); TIMER_EnableWakeup(timer_base);
@ -129,12 +145,12 @@ void lp_ticker_free(void)
/* Disable IP clock */ /* Disable IP clock */
CLK_DisableModuleClock(TIMER_MODINIT.clkidx); CLK_DisableModuleClock(TIMER_MODINIT.clkidx);
ticker_inited = 0; ticker_stat = NU_TICKER_UNINIT;
} }
timestamp_t lp_ticker_read() timestamp_t lp_ticker_read()
{ {
if (! ticker_inited) { if (ticker_stat == NU_TICKER_UNINIT) {
lp_ticker_init(); lp_ticker_init();
} }
@ -145,6 +161,9 @@ timestamp_t lp_ticker_read()
void lp_ticker_set_interrupt(timestamp_t timestamp) void lp_ticker_set_interrupt(timestamp_t timestamp)
{ {
/* We can call ticker_irq_handler now. */
ticker_stat = NU_TICKER_INIT_INTR_EN;
/* In continuous mode, counter will be reset to zero with the following sequence: /* In continuous mode, counter will be reset to zero with the following sequence:
* 1. Stop counting * 1. Stop counting
* 2. Configure new CMP value * 2. Configure new CMP value
@ -160,27 +179,33 @@ void lp_ticker_set_interrupt(timestamp_t timestamp)
uint32_t cmp_timer = timestamp * NU_TMRCLK_PER_TICK; uint32_t cmp_timer = timestamp * NU_TMRCLK_PER_TICK;
cmp_timer = NU_CLAMP(cmp_timer, TMR_CMP_MIN, TMR_CMP_MAX); cmp_timer = NU_CLAMP(cmp_timer, TMR_CMP_MIN, TMR_CMP_MAX);
/* NOTE: Rely on LPTICKER_DELAY_TICKS to be non-blocking. */
timer_base->CMP = cmp_timer; timer_base->CMP = cmp_timer;
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
TIMER_EnableInt(timer_base);
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
} }
void lp_ticker_disable_interrupt(void) void lp_ticker_disable_interrupt(void)
{ {
TIMER_DisableInt((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); /* We cannot call ticker_irq_handler now. */
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); ticker_stat = NU_TICKER_INIT_INTR_DIS;
} }
void lp_ticker_clear_interrupt(void) void lp_ticker_clear_interrupt(void)
{ {
TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); /* To avoid sync issue, we clear TIF/TWKF simultaneously rather than call separate
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); * driver API:
*
* TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname));
* TIMER_ClearWakeupFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname));
*/
TIMER_T *timer_base = (TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname);
timer_base->INTSTS = TIMER_INTSTS_TIF_Msk | TIMER_INTSTS_TWKF_Msk;
} }
void lp_ticker_fire_interrupt(void) void lp_ticker_fire_interrupt(void)
{ {
/* We can call ticker_irq_handler now. */
ticker_stat = NU_TICKER_INIT_INTR_EN;
// NOTE: This event was in the past. Set the interrupt as pending, but don't process it here. // NOTE: This event was in the past. Set the interrupt as pending, but don't process it here.
// This prevents a recursive loop under heavy load which can lead to a stack overflow. // This prevents a recursive loop under heavy load which can lead to a stack overflow.
NVIC_SetPendingIRQ(TIMER_MODINIT.irq_n); NVIC_SetPendingIRQ(TIMER_MODINIT.irq_n);
@ -197,14 +222,15 @@ const ticker_info_t* lp_ticker_get_info()
static void tmr1_vec(void) static void tmr1_vec(void)
{ {
TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); /* NOTE: We need to clear interrupt flag earlier to reduce possibility of dummy interrupt.
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); * This is because "clear interrupt flag" needs delay which isn't added here to avoid
* blocking in ISR code. */
TIMER_ClearWakeupFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); lp_ticker_clear_interrupt();
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
// NOTE: lp_ticker_set_interrupt() may get called in lp_ticker_irq_handler(); // NOTE: lp_ticker_set_interrupt() may get called in lp_ticker_irq_handler();
lp_ticker_irq_handler(); if (ticker_stat == NU_TICKER_INIT_INTR_EN) {
lp_ticker_irq_handler();
}
} }
#endif #endif

View File

@ -42,7 +42,23 @@ static const struct nu_modinit_s timer1_modinit = {TIMER_1, TMR1_MODULE, CLK_CLK
#define TIMER_MODINIT timer1_modinit #define TIMER_MODINIT timer1_modinit
static int ticker_inited = 0; /* S/W interrupt enable/disable
*
* Because H/W interrupt enable/disable (TIMER_EnableInt/TIMER_DisableInt) needs delay for lp_ticker,
* we introduce S/W interrupt enable/disable to avoid blocking code. With S/W interrupt enable/disable,
* H/W interrupt is always enabled after ticker_init. A S/W flag is used to tell whether or not
* ticker_irq_handler is ready to call.
*/
/* Ticker uninitialized */
#define NU_TICKER_UNINIT 0
/* Ticker initialized with interrupt disabled */
#define NU_TICKER_INIT_INTR_DIS 1
/* Ticker initialized with interrupt enabled */
#define NU_TICKER_INIT_INTR_EN 2
/* Track ticker status */
static volatile uint16_t ticker_stat = NU_TICKER_UNINIT;
#define TMR_CMP_MIN 2 #define TMR_CMP_MIN 2
#define TMR_CMP_MAX 0xFFFFFFu #define TMR_CMP_MAX 0xFFFFFFu
@ -52,14 +68,14 @@ static int ticker_inited = 0;
void lp_ticker_init(void) void lp_ticker_init(void)
{ {
if (ticker_inited) { if (ticker_stat) {
/* By HAL spec, ticker_init allows the ticker to keep counting and disables the /* By HAL spec, ticker_init allows the ticker to keep counting and disables the
* ticker interrupt. */ * ticker interrupt. */
lp_ticker_disable_interrupt(); lp_ticker_disable_interrupt();
lp_ticker_clear_interrupt(); lp_ticker_clear_interrupt();
return; return;
} }
ticker_inited = 1; ticker_stat = NU_TICKER_INIT_INTR_DIS;
// Reset module // Reset module
SYS_ResetModule(TIMER_MODINIT.rsetidx); SYS_ResetModule(TIMER_MODINIT.rsetidx);
@ -92,7 +108,7 @@ void lp_ticker_init(void)
NVIC_EnableIRQ(TIMER_MODINIT.irq_n); NVIC_EnableIRQ(TIMER_MODINIT.irq_n);
TIMER_DisableInt(timer_base); TIMER_EnableInt(timer_base);
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
TIMER_EnableWakeup(timer_base); TIMER_EnableWakeup(timer_base);
@ -129,12 +145,12 @@ void lp_ticker_free(void)
/* Disable IP clock */ /* Disable IP clock */
CLK_DisableModuleClock(TIMER_MODINIT.clkidx); CLK_DisableModuleClock(TIMER_MODINIT.clkidx);
ticker_inited = 0; ticker_stat = NU_TICKER_UNINIT;
} }
timestamp_t lp_ticker_read() timestamp_t lp_ticker_read()
{ {
if (! ticker_inited) { if (ticker_stat == NU_TICKER_UNINIT) {
lp_ticker_init(); lp_ticker_init();
} }
@ -145,6 +161,9 @@ timestamp_t lp_ticker_read()
void lp_ticker_set_interrupt(timestamp_t timestamp) void lp_ticker_set_interrupt(timestamp_t timestamp)
{ {
/* We can call ticker_irq_handler now. */
ticker_stat = NU_TICKER_INIT_INTR_EN;
/* In continuous mode, counter will be reset to zero with the following sequence: /* In continuous mode, counter will be reset to zero with the following sequence:
* 1. Stop counting * 1. Stop counting
* 2. Configure new CMP value * 2. Configure new CMP value
@ -160,27 +179,33 @@ void lp_ticker_set_interrupt(timestamp_t timestamp)
uint32_t cmp_timer = timestamp * NU_TMRCLK_PER_TICK; uint32_t cmp_timer = timestamp * NU_TMRCLK_PER_TICK;
cmp_timer = NU_CLAMP(cmp_timer, TMR_CMP_MIN, TMR_CMP_MAX); cmp_timer = NU_CLAMP(cmp_timer, TMR_CMP_MIN, TMR_CMP_MAX);
/* NOTE: Rely on LPTICKER_DELAY_TICKS to be non-blocking. */
timer_base->CMP = cmp_timer; timer_base->CMP = cmp_timer;
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
TIMER_EnableInt(timer_base);
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
} }
void lp_ticker_disable_interrupt(void) void lp_ticker_disable_interrupt(void)
{ {
TIMER_DisableInt((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); /* We cannot call ticker_irq_handler now. */
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); ticker_stat = NU_TICKER_INIT_INTR_DIS;
} }
void lp_ticker_clear_interrupt(void) void lp_ticker_clear_interrupt(void)
{ {
TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); /* To avoid sync issue, we clear TIF/TWKF simultaneously rather than call separate
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); * driver API:
*
* TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname));
* TIMER_ClearWakeupFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname));
*/
TIMER_T *timer_base = (TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname);
timer_base->INTSTS = TIMER_INTSTS_TIF_Msk | TIMER_INTSTS_TWKF_Msk;
} }
void lp_ticker_fire_interrupt(void) void lp_ticker_fire_interrupt(void)
{ {
/* We can call ticker_irq_handler now. */
ticker_stat = NU_TICKER_INIT_INTR_EN;
// NOTE: This event was in the past. Set the interrupt as pending, but don't process it here. // NOTE: This event was in the past. Set the interrupt as pending, but don't process it here.
// This prevents a recursive loop under heavy load which can lead to a stack overflow. // This prevents a recursive loop under heavy load which can lead to a stack overflow.
NVIC_SetPendingIRQ(TIMER_MODINIT.irq_n); NVIC_SetPendingIRQ(TIMER_MODINIT.irq_n);
@ -197,14 +222,15 @@ const ticker_info_t* lp_ticker_get_info()
static void tmr1_vec(void) static void tmr1_vec(void)
{ {
TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); /* NOTE: We need to clear interrupt flag earlier to reduce possibility of dummy interrupt.
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); * This is because "clear interrupt flag" needs delay which isn't added here to avoid
* blocking in ISR code. */
TIMER_ClearWakeupFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); lp_ticker_clear_interrupt();
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
// NOTE: lp_ticker_set_interrupt() may get called in lp_ticker_irq_handler(); // NOTE: lp_ticker_set_interrupt() may get called in lp_ticker_irq_handler();
lp_ticker_irq_handler(); if (ticker_stat == NU_TICKER_INIT_INTR_EN) {
lp_ticker_irq_handler();
}
} }
#endif #endif

View File

@ -44,7 +44,23 @@ static const struct nu_modinit_s timer1_modinit = {TIMER_1, TMR1_MODULE, CLK_CLK
#define TIMER_MODINIT timer1_modinit #define TIMER_MODINIT timer1_modinit
static int ticker_inited = 0; /* S/W interrupt enable/disable
*
* Because H/W interrupt enable/disable (TIMER_EnableInt/TIMER_DisableInt) needs delay for lp_ticker,
* we introduce S/W interrupt enable/disable to avoid blocking code. With S/W interrupt enable/disable,
* H/W interrupt is always enabled after ticker_init. A S/W flag is used to tell whether or not
* ticker_irq_handler is ready to call.
*/
/* Ticker uninitialized */
#define NU_TICKER_UNINIT 0
/* Ticker initialized with interrupt disabled */
#define NU_TICKER_INIT_INTR_DIS 1
/* Ticker initialized with interrupt enabled */
#define NU_TICKER_INIT_INTR_EN 2
/* Track ticker status */
static volatile uint16_t ticker_stat = NU_TICKER_UNINIT;
#define TMR_CMP_MIN 2 #define TMR_CMP_MIN 2
#define TMR_CMP_MAX 0xFFFFFFu #define TMR_CMP_MAX 0xFFFFFFu
@ -54,14 +70,14 @@ static int ticker_inited = 0;
void lp_ticker_init(void) void lp_ticker_init(void)
{ {
if (ticker_inited) { if (ticker_stat) {
/* By HAL spec, ticker_init allows the ticker to keep counting and disables the /* By HAL spec, ticker_init allows the ticker to keep counting and disables the
* ticker interrupt. */ * ticker interrupt. */
lp_ticker_disable_interrupt(); lp_ticker_disable_interrupt();
lp_ticker_clear_interrupt(); lp_ticker_clear_interrupt();
return; return;
} }
ticker_inited = 1; ticker_stat = NU_TICKER_INIT_INTR_DIS;
// Reset module // Reset module
SYS_ResetModule(TIMER_MODINIT.rsetidx); SYS_ResetModule(TIMER_MODINIT.rsetidx);
@ -96,7 +112,7 @@ void lp_ticker_init(void)
NVIC_EnableIRQ(TIMER_MODINIT.irq_n); NVIC_EnableIRQ(TIMER_MODINIT.irq_n);
TIMER_DisableInt(timer_base); TIMER_EnableInt(timer_base);
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
TIMER_EnableWakeup(timer_base); TIMER_EnableWakeup(timer_base);
@ -133,12 +149,12 @@ void lp_ticker_free(void)
/* Disable IP clock */ /* Disable IP clock */
CLK_DisableModuleClock(TIMER_MODINIT.clkidx); CLK_DisableModuleClock(TIMER_MODINIT.clkidx);
ticker_inited = 0; ticker_stat = NU_TICKER_UNINIT;
} }
timestamp_t lp_ticker_read() timestamp_t lp_ticker_read()
{ {
if (! ticker_inited) { if (ticker_stat == NU_TICKER_UNINIT) {
lp_ticker_init(); lp_ticker_init();
} }
@ -149,6 +165,9 @@ timestamp_t lp_ticker_read()
void lp_ticker_set_interrupt(timestamp_t timestamp) void lp_ticker_set_interrupt(timestamp_t timestamp)
{ {
/* We can call ticker_irq_handler now. */
ticker_stat = NU_TICKER_INIT_INTR_EN;
/* In continuous mode, counter will be reset to zero with the following sequence: /* In continuous mode, counter will be reset to zero with the following sequence:
* 1. Stop counting * 1. Stop counting
* 2. Configure new CMP value * 2. Configure new CMP value
@ -164,27 +183,33 @@ void lp_ticker_set_interrupt(timestamp_t timestamp)
uint32_t cmp_timer = timestamp * NU_TMRCLK_PER_TICK; uint32_t cmp_timer = timestamp * NU_TMRCLK_PER_TICK;
cmp_timer = NU_CLAMP(cmp_timer, TMR_CMP_MIN, TMR_CMP_MAX); cmp_timer = NU_CLAMP(cmp_timer, TMR_CMP_MIN, TMR_CMP_MAX);
/* NOTE: Rely on LPTICKER_DELAY_TICKS to be non-blocking. */
timer_base->CMPR = cmp_timer; timer_base->CMPR = cmp_timer;
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
TIMER_EnableInt(timer_base);
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
} }
void lp_ticker_disable_interrupt(void) void lp_ticker_disable_interrupt(void)
{ {
TIMER_DisableInt((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); /* We cannot call ticker_irq_handler now. */
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); ticker_stat = NU_TICKER_INIT_INTR_DIS;
} }
void lp_ticker_clear_interrupt(void) void lp_ticker_clear_interrupt(void)
{ {
TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); /* To avoid sync issue, we clear TIF/TWKF simultaneously rather than call separate
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); * driver API:
*
* TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname));
* TIMER_ClearWakeupFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname));
*/
TIMER_T *timer_base = (TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname);
timer_base->ISR = TIMER_ISR_TMR_IS_Msk | TIMER_ISR_TMR_WAKE_STS_Msk;
} }
void lp_ticker_fire_interrupt(void) void lp_ticker_fire_interrupt(void)
{ {
/* We can call ticker_irq_handler now. */
ticker_stat = NU_TICKER_INIT_INTR_EN;
// NOTE: This event was in the past. Set the interrupt as pending, but don't process it here. // NOTE: This event was in the past. Set the interrupt as pending, but don't process it here.
// This prevents a recursive loop under heavy load which can lead to a stack overflow. // This prevents a recursive loop under heavy load which can lead to a stack overflow.
NVIC_SetPendingIRQ(TIMER_MODINIT.irq_n); NVIC_SetPendingIRQ(TIMER_MODINIT.irq_n);
@ -201,14 +226,15 @@ const ticker_info_t* lp_ticker_get_info()
void TMR1_IRQHandler(void) void TMR1_IRQHandler(void)
{ {
TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); /* NOTE: We need to clear interrupt flag earlier to reduce possibility of dummy interrupt.
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); * This is because "clear interrupt flag" needs delay which isn't added here to avoid
* blocking in ISR code. */
TIMER_ClearWakeupFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); lp_ticker_clear_interrupt();
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
// NOTE: lp_ticker_set_interrupt() may get called in lp_ticker_irq_handler(); // NOTE: lp_ticker_set_interrupt() may get called in lp_ticker_irq_handler();
lp_ticker_irq_handler(); if (ticker_stat == NU_TICKER_INIT_INTR_EN) {
lp_ticker_irq_handler();
}
} }
#endif #endif

View File

@ -42,7 +42,23 @@ static const struct nu_modinit_s timer1_modinit = {TIMER_1, TMR1_MODULE, CLK_CLK
#define TIMER_MODINIT timer1_modinit #define TIMER_MODINIT timer1_modinit
static int ticker_inited = 0; /* S/W interrupt enable/disable
*
* Because H/W interrupt enable/disable (TIMER_EnableInt/TIMER_DisableInt) needs delay for lp_ticker,
* we introduce S/W interrupt enable/disable to avoid blocking code. With S/W interrupt enable/disable,
* H/W interrupt is always enabled after ticker_init. A S/W flag is used to tell whether or not
* ticker_irq_handler is ready to call.
*/
/* Ticker uninitialized */
#define NU_TICKER_UNINIT 0
/* Ticker initialized with interrupt disabled */
#define NU_TICKER_INIT_INTR_DIS 1
/* Ticker initialized with interrupt enabled */
#define NU_TICKER_INIT_INTR_EN 2
/* Track ticker status */
static volatile uint16_t ticker_stat = NU_TICKER_UNINIT;
#define TMR_CMP_MIN 2 #define TMR_CMP_MIN 2
#define TMR_CMP_MAX 0xFFFFFFu #define TMR_CMP_MAX 0xFFFFFFu
@ -52,14 +68,14 @@ static int ticker_inited = 0;
void lp_ticker_init(void) void lp_ticker_init(void)
{ {
if (ticker_inited) { if (ticker_stat) {
/* By HAL spec, ticker_init allows the ticker to keep counting and disables the /* By HAL spec, ticker_init allows the ticker to keep counting and disables the
* ticker interrupt. */ * ticker interrupt. */
lp_ticker_disable_interrupt(); lp_ticker_disable_interrupt();
lp_ticker_clear_interrupt(); lp_ticker_clear_interrupt();
return; return;
} }
ticker_inited = 1; ticker_stat = NU_TICKER_INIT_INTR_DIS;
// Reset module // Reset module
SYS_ResetModule(TIMER_MODINIT.rsetidx); SYS_ResetModule(TIMER_MODINIT.rsetidx);
@ -91,7 +107,7 @@ void lp_ticker_init(void)
NVIC_EnableIRQ(TIMER_MODINIT.irq_n); NVIC_EnableIRQ(TIMER_MODINIT.irq_n);
TIMER_DisableInt(timer_base); TIMER_EnableInt(timer_base);
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
TIMER_EnableWakeup(timer_base); TIMER_EnableWakeup(timer_base);
@ -128,12 +144,12 @@ void lp_ticker_free(void)
/* Disable IP clock */ /* Disable IP clock */
CLK_DisableModuleClock(TIMER_MODINIT.clkidx); CLK_DisableModuleClock(TIMER_MODINIT.clkidx);
ticker_inited = 0; ticker_stat = NU_TICKER_UNINIT;
} }
timestamp_t lp_ticker_read() timestamp_t lp_ticker_read()
{ {
if (! ticker_inited) { if (ticker_stat == NU_TICKER_UNINIT) {
lp_ticker_init(); lp_ticker_init();
} }
@ -144,6 +160,9 @@ timestamp_t lp_ticker_read()
void lp_ticker_set_interrupt(timestamp_t timestamp) void lp_ticker_set_interrupt(timestamp_t timestamp)
{ {
/* We can call ticker_irq_handler now. */
ticker_stat = NU_TICKER_INIT_INTR_EN;
/* In continuous mode, counter will be reset to zero with the following sequence: /* In continuous mode, counter will be reset to zero with the following sequence:
* 1. Stop counting * 1. Stop counting
* 2. Configure new CMP value * 2. Configure new CMP value
@ -159,27 +178,33 @@ void lp_ticker_set_interrupt(timestamp_t timestamp)
uint32_t cmp_timer = timestamp * NU_TMRCLK_PER_TICK; uint32_t cmp_timer = timestamp * NU_TMRCLK_PER_TICK;
cmp_timer = NU_CLAMP(cmp_timer, TMR_CMP_MIN, TMR_CMP_MAX); cmp_timer = NU_CLAMP(cmp_timer, TMR_CMP_MIN, TMR_CMP_MAX);
/* NOTE: Rely on LPTICKER_DELAY_TICKS to be non-blocking. */
timer_base->CMP = cmp_timer; timer_base->CMP = cmp_timer;
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
TIMER_EnableInt(timer_base);
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
} }
void lp_ticker_disable_interrupt(void) void lp_ticker_disable_interrupt(void)
{ {
TIMER_DisableInt((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); /* We cannot call ticker_irq_handler now. */
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); ticker_stat = NU_TICKER_INIT_INTR_DIS;
} }
void lp_ticker_clear_interrupt(void) void lp_ticker_clear_interrupt(void)
{ {
TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); /* To avoid sync issue, we clear TIF/TWKF simultaneously rather than call separate
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); * driver API:
*
* TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname));
* TIMER_ClearWakeupFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname));
*/
TIMER_T *timer_base = (TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname);
timer_base->INTSTS = TIMER_INTSTS_TIF_Msk | TIMER_INTSTS_TWKF_Msk;
} }
void lp_ticker_fire_interrupt(void) void lp_ticker_fire_interrupt(void)
{ {
/* We can call ticker_irq_handler now. */
ticker_stat = NU_TICKER_INIT_INTR_EN;
// NOTE: This event was in the past. Set the interrupt as pending, but don't process it here. // NOTE: This event was in the past. Set the interrupt as pending, but don't process it here.
// This prevents a recursive loop under heavy load which can lead to a stack overflow. // This prevents a recursive loop under heavy load which can lead to a stack overflow.
NVIC_SetPendingIRQ(TIMER_MODINIT.irq_n); NVIC_SetPendingIRQ(TIMER_MODINIT.irq_n);
@ -196,14 +221,15 @@ const ticker_info_t* lp_ticker_get_info()
static void tmr1_vec(void) static void tmr1_vec(void)
{ {
TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); /* NOTE: We need to clear interrupt flag earlier to reduce possibility of dummy interrupt.
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); * This is because "clear interrupt flag" needs delay which isn't added here to avoid
* blocking in ISR code. */
TIMER_ClearWakeupFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); lp_ticker_clear_interrupt();
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
// NOTE: lp_ticker_set_interrupt() may get called in lp_ticker_irq_handler(); // NOTE: lp_ticker_set_interrupt() may get called in lp_ticker_irq_handler();
lp_ticker_irq_handler(); if (ticker_stat == NU_TICKER_INIT_INTR_EN) {
lp_ticker_irq_handler();
}
} }
#endif #endif

View File

@ -3843,7 +3843,7 @@
} }
}, },
"inherits": ["Target"], "inherits": ["Target"],
"macros_add": ["MBEDTLS_CONFIG_HW_SUPPORT"], "macros_add": ["MBEDTLS_CONFIG_HW_SUPPORT", "LPTICKER_DELAY_TICKS=3"],
"device_has": ["USTICKER", "LPTICKER", "RTC", "ANALOGIN", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "STDIO_MESSAGES", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "TRNG", "CAN", "FLASH", "EMAC"], "device_has": ["USTICKER", "LPTICKER", "RTC", "ANALOGIN", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "STDIO_MESSAGES", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "TRNG", "CAN", "FLASH", "EMAC"],
"release_versions": ["5"], "release_versions": ["5"],
"device_name": "NUC472HI8AE", "device_name": "NUC472HI8AE",
@ -3914,6 +3914,7 @@
} }
}, },
"inherits": ["Target"], "inherits": ["Target"],
"macros_add": ["LPTICKER_DELAY_TICKS=3"],
"progen": {"target": "numaker-pfm-m453"}, "progen": {"target": "numaker-pfm-m453"},
"device_has": ["USTICKER", "LPTICKER", "RTC", "ANALOGIN", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "STDIO_MESSAGES", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "CAN", "FLASH"], "device_has": ["USTICKER", "LPTICKER", "RTC", "ANALOGIN", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "STDIO_MESSAGES", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "CAN", "FLASH"],
"release_versions": ["2", "5"], "release_versions": ["2", "5"],
@ -3945,7 +3946,7 @@
} }
}, },
"inherits": ["Target"], "inherits": ["Target"],
"macros": ["CMSIS_VECTAB_VIRTUAL", "CMSIS_VECTAB_VIRTUAL_HEADER_FILE=\"cmsis_nvic.h\"","MBED_FAULT_HANDLER_DISABLED"], "macros": ["CMSIS_VECTAB_VIRTUAL", "CMSIS_VECTAB_VIRTUAL_HEADER_FILE=\"cmsis_nvic.h\"","MBED_FAULT_HANDLER_DISABLED", "LPTICKER_DELAY_TICKS=3"],
"device_has": ["USTICKER", "LPTICKER", "RTC", "ANALOGIN", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "STDIO_MESSAGES", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH"], "device_has": ["USTICKER", "LPTICKER", "RTC", "ANALOGIN", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "STDIO_MESSAGES", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH"],
"release_versions": ["5"], "release_versions": ["5"],
"device_name": "NANO130KE3BN" "device_name": "NANO130KE3BN"
@ -4112,7 +4113,7 @@
} }
}, },
"inherits": ["Target"], "inherits": ["Target"],
"macros_add": ["MBEDTLS_CONFIG_HW_SUPPORT"], "macros_add": ["MBEDTLS_CONFIG_HW_SUPPORT", "LPTICKER_DELAY_TICKS=3"],
"device_has": ["USTICKER", "LPTICKER", "RTC", "ANALOGIN", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "STDIO_MESSAGES", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "TRNG", "FLASH", "CAN", "EMAC"], "device_has": ["USTICKER", "LPTICKER", "RTC", "ANALOGIN", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "STDIO_MESSAGES", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "TRNG", "FLASH", "CAN", "EMAC"],
"release_versions": ["5"], "release_versions": ["5"],
"device_name": "M487JIDAE", "device_name": "M487JIDAE",