From 86e194d075531d451890165528044c72e986c67b Mon Sep 17 00:00:00 2001 From: ccli8 Date: Fri, 1 Jun 2018 15:37:53 +0800 Subject: [PATCH] [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. --- .../TARGET_NUVOTON/TARGET_M451/lp_ticker.c | 66 +++++++++++++------ .../TARGET_NUVOTON/TARGET_M480/lp_ticker.c | 66 +++++++++++++------ .../TARGET_NUVOTON/TARGET_NANO100/lp_ticker.c | 66 +++++++++++++------ .../TARGET_NUVOTON/TARGET_NUC472/lp_ticker.c | 66 +++++++++++++------ targets/targets.json | 7 +- 5 files changed, 188 insertions(+), 83 deletions(-) diff --git a/targets/TARGET_NUVOTON/TARGET_M451/lp_ticker.c b/targets/TARGET_NUVOTON/TARGET_M451/lp_ticker.c index a2a8abee63..e557d30397 100644 --- a/targets/TARGET_NUVOTON/TARGET_M451/lp_ticker.c +++ b/targets/TARGET_NUVOTON/TARGET_M451/lp_ticker.c @@ -42,7 +42,23 @@ static const struct nu_modinit_s timer1_modinit = {TIMER_1, TMR1_MODULE, CLK_CLK #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_MAX 0xFFFFFFu @@ -52,14 +68,14 @@ static int ticker_inited = 0; 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 * ticker interrupt. */ lp_ticker_disable_interrupt(); lp_ticker_clear_interrupt(); return; } - ticker_inited = 1; + ticker_stat = NU_TICKER_INIT_INTR_DIS; // Reset module SYS_ResetModule(TIMER_MODINIT.rsetidx); @@ -92,7 +108,7 @@ void lp_ticker_init(void) 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); TIMER_EnableWakeup(timer_base); @@ -129,12 +145,12 @@ void lp_ticker_free(void) /* Disable IP clock */ CLK_DisableModuleClock(TIMER_MODINIT.clkidx); - ticker_inited = 0; + ticker_stat = NU_TICKER_UNINIT; } timestamp_t lp_ticker_read() { - if (! ticker_inited) { + if (ticker_stat == NU_TICKER_UNINIT) { lp_ticker_init(); } @@ -145,6 +161,9 @@ timestamp_t lp_ticker_read() 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: * 1. Stop counting * 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; 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; - 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) { - TIMER_DisableInt((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); - wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); + /* We cannot call ticker_irq_handler now. */ + ticker_stat = NU_TICKER_INIT_INTR_DIS; } void lp_ticker_clear_interrupt(void) { - TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); - wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); + /* To avoid sync issue, we clear TIF/TWKF simultaneously rather than call separate + * 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) { + /* 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. // This prevents a recursive loop under heavy load which can lead to a stack overflow. NVIC_SetPendingIRQ(TIMER_MODINIT.irq_n); @@ -197,14 +222,15 @@ const ticker_info_t* lp_ticker_get_info() static void tmr1_vec(void) { - TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); - wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); - - TIMER_ClearWakeupFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); - wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); + /* NOTE: We need to clear interrupt flag earlier to reduce possibility of dummy interrupt. + * This is because "clear interrupt flag" needs delay which isn't added here to avoid + * blocking in ISR code. */ + lp_ticker_clear_interrupt(); // 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 diff --git a/targets/TARGET_NUVOTON/TARGET_M480/lp_ticker.c b/targets/TARGET_NUVOTON/TARGET_M480/lp_ticker.c index 83c43fe13c..6d1a09e397 100644 --- a/targets/TARGET_NUVOTON/TARGET_M480/lp_ticker.c +++ b/targets/TARGET_NUVOTON/TARGET_M480/lp_ticker.c @@ -42,7 +42,23 @@ static const struct nu_modinit_s timer1_modinit = {TIMER_1, TMR1_MODULE, CLK_CLK #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_MAX 0xFFFFFFu @@ -52,14 +68,14 @@ static int ticker_inited = 0; 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 * ticker interrupt. */ lp_ticker_disable_interrupt(); lp_ticker_clear_interrupt(); return; } - ticker_inited = 1; + ticker_stat = NU_TICKER_INIT_INTR_DIS; // Reset module SYS_ResetModule(TIMER_MODINIT.rsetidx); @@ -92,7 +108,7 @@ void lp_ticker_init(void) 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); TIMER_EnableWakeup(timer_base); @@ -129,12 +145,12 @@ void lp_ticker_free(void) /* Disable IP clock */ CLK_DisableModuleClock(TIMER_MODINIT.clkidx); - ticker_inited = 0; + ticker_stat = NU_TICKER_UNINIT; } timestamp_t lp_ticker_read() { - if (! ticker_inited) { + if (ticker_stat == NU_TICKER_UNINIT) { lp_ticker_init(); } @@ -145,6 +161,9 @@ timestamp_t lp_ticker_read() 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: * 1. Stop counting * 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; 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; - 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) { - TIMER_DisableInt((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); - wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); + /* We cannot call ticker_irq_handler now. */ + ticker_stat = NU_TICKER_INIT_INTR_DIS; } void lp_ticker_clear_interrupt(void) { - TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); - wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); + /* To avoid sync issue, we clear TIF/TWKF simultaneously rather than call separate + * 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) { + /* 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. // This prevents a recursive loop under heavy load which can lead to a stack overflow. NVIC_SetPendingIRQ(TIMER_MODINIT.irq_n); @@ -197,14 +222,15 @@ const ticker_info_t* lp_ticker_get_info() static void tmr1_vec(void) { - TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); - wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); - - TIMER_ClearWakeupFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); - wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); + /* NOTE: We need to clear interrupt flag earlier to reduce possibility of dummy interrupt. + * This is because "clear interrupt flag" needs delay which isn't added here to avoid + * blocking in ISR code. */ + lp_ticker_clear_interrupt(); // 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 diff --git a/targets/TARGET_NUVOTON/TARGET_NANO100/lp_ticker.c b/targets/TARGET_NUVOTON/TARGET_NANO100/lp_ticker.c index 451834ed55..719ffb97c3 100644 --- a/targets/TARGET_NUVOTON/TARGET_NANO100/lp_ticker.c +++ b/targets/TARGET_NUVOTON/TARGET_NANO100/lp_ticker.c @@ -44,7 +44,23 @@ static const struct nu_modinit_s timer1_modinit = {TIMER_1, TMR1_MODULE, CLK_CLK #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_MAX 0xFFFFFFu @@ -54,14 +70,14 @@ static int ticker_inited = 0; 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 * ticker interrupt. */ lp_ticker_disable_interrupt(); lp_ticker_clear_interrupt(); return; } - ticker_inited = 1; + ticker_stat = NU_TICKER_INIT_INTR_DIS; // Reset module SYS_ResetModule(TIMER_MODINIT.rsetidx); @@ -96,7 +112,7 @@ void lp_ticker_init(void) 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); TIMER_EnableWakeup(timer_base); @@ -133,12 +149,12 @@ void lp_ticker_free(void) /* Disable IP clock */ CLK_DisableModuleClock(TIMER_MODINIT.clkidx); - ticker_inited = 0; + ticker_stat = NU_TICKER_UNINIT; } timestamp_t lp_ticker_read() { - if (! ticker_inited) { + if (ticker_stat == NU_TICKER_UNINIT) { lp_ticker_init(); } @@ -149,6 +165,9 @@ timestamp_t lp_ticker_read() 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: * 1. Stop counting * 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; 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; - 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) { - TIMER_DisableInt((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); - wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); + /* We cannot call ticker_irq_handler now. */ + ticker_stat = NU_TICKER_INIT_INTR_DIS; } void lp_ticker_clear_interrupt(void) { - TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); - wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); + /* To avoid sync issue, we clear TIF/TWKF simultaneously rather than call separate + * 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) { + /* 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. // This prevents a recursive loop under heavy load which can lead to a stack overflow. NVIC_SetPendingIRQ(TIMER_MODINIT.irq_n); @@ -201,14 +226,15 @@ const ticker_info_t* lp_ticker_get_info() void TMR1_IRQHandler(void) { - TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); - wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); - - TIMER_ClearWakeupFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); - wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); + /* NOTE: We need to clear interrupt flag earlier to reduce possibility of dummy interrupt. + * This is because "clear interrupt flag" needs delay which isn't added here to avoid + * blocking in ISR code. */ + lp_ticker_clear_interrupt(); // 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 diff --git a/targets/TARGET_NUVOTON/TARGET_NUC472/lp_ticker.c b/targets/TARGET_NUVOTON/TARGET_NUC472/lp_ticker.c index 015e7caf2b..86dd7c2215 100644 --- a/targets/TARGET_NUVOTON/TARGET_NUC472/lp_ticker.c +++ b/targets/TARGET_NUVOTON/TARGET_NUC472/lp_ticker.c @@ -42,7 +42,23 @@ static const struct nu_modinit_s timer1_modinit = {TIMER_1, TMR1_MODULE, CLK_CLK #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_MAX 0xFFFFFFu @@ -52,14 +68,14 @@ static int ticker_inited = 0; 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 * ticker interrupt. */ lp_ticker_disable_interrupt(); lp_ticker_clear_interrupt(); return; } - ticker_inited = 1; + ticker_stat = NU_TICKER_INIT_INTR_DIS; // Reset module SYS_ResetModule(TIMER_MODINIT.rsetidx); @@ -91,7 +107,7 @@ void lp_ticker_init(void) 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); TIMER_EnableWakeup(timer_base); @@ -128,12 +144,12 @@ void lp_ticker_free(void) /* Disable IP clock */ CLK_DisableModuleClock(TIMER_MODINIT.clkidx); - ticker_inited = 0; + ticker_stat = NU_TICKER_UNINIT; } timestamp_t lp_ticker_read() { - if (! ticker_inited) { + if (ticker_stat == NU_TICKER_UNINIT) { lp_ticker_init(); } @@ -144,6 +160,9 @@ timestamp_t lp_ticker_read() 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: * 1. Stop counting * 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; 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; - 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) { - TIMER_DisableInt((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); - wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); + /* We cannot call ticker_irq_handler now. */ + ticker_stat = NU_TICKER_INIT_INTR_DIS; } void lp_ticker_clear_interrupt(void) { - TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); - wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); + /* To avoid sync issue, we clear TIF/TWKF simultaneously rather than call separate + * 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) { + /* 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. // This prevents a recursive loop under heavy load which can lead to a stack overflow. NVIC_SetPendingIRQ(TIMER_MODINIT.irq_n); @@ -196,14 +221,15 @@ const ticker_info_t* lp_ticker_get_info() static void tmr1_vec(void) { - TIMER_ClearIntFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); - wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); - - TIMER_ClearWakeupFlag((TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname)); - wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3); + /* NOTE: We need to clear interrupt flag earlier to reduce possibility of dummy interrupt. + * This is because "clear interrupt flag" needs delay which isn't added here to avoid + * blocking in ISR code. */ + lp_ticker_clear_interrupt(); // 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 diff --git a/targets/targets.json b/targets/targets.json index 979d1b3423..2e5536b8bf 100755 --- a/targets/targets.json +++ b/targets/targets.json @@ -3843,7 +3843,7 @@ } }, "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"], "release_versions": ["5"], "device_name": "NUC472HI8AE", @@ -3914,6 +3914,7 @@ } }, "inherits": ["Target"], + "macros_add": ["LPTICKER_DELAY_TICKS=3"], "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"], "release_versions": ["2", "5"], @@ -3945,7 +3946,7 @@ } }, "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"], "release_versions": ["5"], "device_name": "NANO130KE3BN" @@ -4112,7 +4113,7 @@ } }, "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"], "release_versions": ["5"], "device_name": "M487JIDAE",