Skip to content

Commit ebd93ba

Browse files
committed
[Nuvoton] Meet new us_ticker HAL spec (Mbed OS 5.9)
1. Add USTICKER in device_has option of targets.json file. 2. Disable ticker interrupt in us_ticker_init 3. Add us_ticker_free 4. Enable interrupt in us_ticker_set_interrupt
1 parent f3424da commit ebd93ba

File tree

5 files changed

+131
-8
lines changed

5 files changed

+131
-8
lines changed

targets/TARGET_NUVOTON/TARGET_M451/us_ticker.c

Lines changed: 31 additions & 1 deletion
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"
@@ -45,6 +48,10 @@ static int ticker_inited = 0;
4548
void us_ticker_init(void)
4649
{
4750
if (ticker_inited) {
51+
/* By HAL spec, ticker_init allows the ticker to keep counting and disables the
52+
* ticker interrupt. */
53+
us_ticker_disable_interrupt();
54+
us_ticker_clear_interrupt();
4855
return;
4956
}
5057
ticker_inited = 1;
@@ -75,13 +82,32 @@ void us_ticker_init(void)
7582

7683
NVIC_EnableIRQ(TIMER_MODINIT.irq_n);
7784

78-
TIMER_EnableInt(timer_base);
85+
TIMER_DisableInt(timer_base);
7986

8087
TIMER_Start(timer_base);
8188
/* Wait for timer to start counting and raise active flag */
8289
while(! (timer_base->CTL & TIMER_CTL_ACTSTS_Msk));
8390
}
8491

92+
void us_ticker_free(void)
93+
{
94+
TIMER_T *timer_base = (TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname);
95+
96+
/* Stop counting */
97+
TIMER_Stop(timer_base);
98+
/* Wait for timer to stop counting and unset active flag */
99+
while((timer_base->CTL & TIMER_CTL_ACTSTS_Msk));
100+
101+
/* Disable interrupt */
102+
TIMER_DisableInt(timer_base);
103+
NVIC_DisableIRQ(TIMER_MODINIT.irq_n);
104+
105+
/* Disable IP clock */
106+
CLK_DisableModuleClock(TIMER_MODINIT.clkidx);
107+
108+
ticker_inited = 0;
109+
}
110+
85111
uint32_t us_ticker_read()
86112
{
87113
if (! ticker_inited) {
@@ -110,6 +136,8 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
110136
uint32_t cmp_timer = timestamp * NU_TMRCLK_PER_TICK;
111137
cmp_timer = NU_CLAMP(cmp_timer, TMR_CMP_MIN, TMR_CMP_MAX);
112138
timer_base->CMP = cmp_timer;
139+
140+
TIMER_EnableInt(timer_base);
113141
}
114142

115143
void us_ticker_disable_interrupt(void)
@@ -145,3 +173,5 @@ static void tmr0_vec(void)
145173
// NOTE: us_ticker_set_interrupt() may get called in us_ticker_irq_handler();
146174
us_ticker_irq_handler();
147175
}
176+
177+
#endif

targets/TARGET_NUVOTON/TARGET_M480/us_ticker.c

Lines changed: 32 additions & 1 deletion
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"
@@ -45,6 +48,10 @@ static int ticker_inited = 0;
4548
void us_ticker_init(void)
4649
{
4750
if (ticker_inited) {
51+
/* By HAL spec, ticker_init allows the ticker to keep counting and disables the
52+
* ticker interrupt. */
53+
us_ticker_disable_interrupt();
54+
us_ticker_clear_interrupt();
4855
return;
4956
}
5057
ticker_inited = 1;
@@ -75,13 +82,33 @@ void us_ticker_init(void)
7582

7683
NVIC_EnableIRQ(TIMER_MODINIT.irq_n);
7784

78-
TIMER_EnableInt(timer_base);
85+
TIMER_DisableInt(timer_base);
7986

8087
TIMER_Start(timer_base);
8188
/* Wait for timer to start counting and raise active flag */
8289
while(! (timer_base->CTL & TIMER_CTL_ACTSTS_Msk));
8390
}
8491

92+
void us_ticker_free(void)
93+
{
94+
TIMER_T *timer_base = (TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname);
95+
96+
/* Stop counting */
97+
TIMER_Stop(timer_base);
98+
99+
/* Wait for timer to stop counting and unset active flag */
100+
while((timer_base->CTL & TIMER_CTL_ACTSTS_Msk));
101+
102+
/* Disable interrupt */
103+
TIMER_DisableInt(timer_base);
104+
NVIC_DisableIRQ(TIMER_MODINIT.irq_n);
105+
106+
/* Disable IP clock */
107+
CLK_DisableModuleClock(TIMER_MODINIT.clkidx);
108+
109+
ticker_inited = 0;
110+
}
111+
85112
uint32_t us_ticker_read()
86113
{
87114
if (! ticker_inited) {
@@ -110,6 +137,8 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
110137
uint32_t cmp_timer = timestamp * NU_TMRCLK_PER_TICK;
111138
cmp_timer = NU_CLAMP(cmp_timer, TMR_CMP_MIN, TMR_CMP_MAX);
112139
timer_base->CMP = cmp_timer;
140+
141+
TIMER_EnableInt(timer_base);
113142
}
114143

115144
void us_ticker_disable_interrupt(void)
@@ -145,3 +174,5 @@ static void tmr0_vec(void)
145174
// NOTE: us_ticker_set_interrupt() may get called in us_ticker_irq_handler();
146175
us_ticker_irq_handler();
147176
}
177+
178+
#endif

targets/TARGET_NUVOTON/TARGET_NANO100/us_ticker.c

Lines changed: 32 additions & 1 deletion
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"
@@ -47,6 +50,10 @@ static int ticker_inited = 0;
4750
void us_ticker_init(void)
4851
{
4952
if (ticker_inited) {
53+
/* By HAL spec, ticker_init allows the ticker to keep counting and disables the
54+
* ticker interrupt. */
55+
us_ticker_disable_interrupt();
56+
us_ticker_clear_interrupt();
5057
return;
5158
}
5259
ticker_inited = 1;
@@ -77,13 +84,33 @@ void us_ticker_init(void)
7784

7885
NVIC_EnableIRQ(TIMER_MODINIT.irq_n);
7986

80-
TIMER_EnableInt(timer_base);
87+
TIMER_DisableInt(timer_base);
8188

8289
TIMER_Start(timer_base);
8390
/* Wait for timer to start counting and raise active flag */
8491
while(! (timer_base->CTL & TIMER_CTL_TMR_ACT_Msk));
8592
}
8693

94+
void us_ticker_free(void)
95+
{
96+
TIMER_T *timer_base = (TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname);
97+
98+
/* Stop counting */
99+
TIMER_Stop(timer_base);
100+
101+
/* Wait for timer to stop counting and unset active flag */
102+
while((timer_base->CTL & TIMER_CTL_TMR_ACT_Msk));
103+
104+
/* Disable interrupt */
105+
TIMER_DisableInt(timer_base);
106+
NVIC_DisableIRQ(TIMER_MODINIT.irq_n);
107+
108+
/* Disable IP clock */
109+
CLK_DisableModuleClock(TIMER_MODINIT.clkidx);
110+
111+
ticker_inited = 0;
112+
}
113+
87114
uint32_t us_ticker_read()
88115
{
89116
if (! ticker_inited) {
@@ -112,6 +139,8 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
112139
uint32_t cmp_timer = timestamp * NU_TMRCLK_PER_TICK;
113140
cmp_timer = NU_CLAMP(cmp_timer, TMR_CMP_MIN, TMR_CMP_MAX);
114141
timer_base->CMPR = cmp_timer;
142+
143+
TIMER_EnableInt(timer_base);
115144
}
116145

117146
void us_ticker_disable_interrupt(void)
@@ -147,3 +176,5 @@ void TMR0_IRQHandler(void)
147176
// NOTE: us_ticker_set_interrupt() may get called in us_ticker_irq_handler();
148177
us_ticker_irq_handler();
149178
}
179+
180+
#endif

targets/TARGET_NUVOTON/TARGET_NUC472/us_ticker.c

Lines changed: 32 additions & 1 deletion
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"
@@ -45,6 +48,10 @@ static int ticker_inited = 0;
4548
void us_ticker_init(void)
4649
{
4750
if (ticker_inited) {
51+
/* By HAL spec, ticker_init allows the ticker to keep counting and disables the
52+
* ticker interrupt. */
53+
us_ticker_disable_interrupt();
54+
us_ticker_clear_interrupt();
4855
return;
4956
}
5057
ticker_inited = 1;
@@ -74,13 +81,33 @@ void us_ticker_init(void)
7481

7582
NVIC_EnableIRQ(TIMER_MODINIT.irq_n);
7683

77-
TIMER_EnableInt(timer_base);
84+
TIMER_DisableInt(timer_base);
7885

7986
TIMER_Start(timer_base);
8087
/* Wait for timer to start counting and raise active flag */
8188
while(! (timer_base->CTL & TIMER_CTL_ACTSTS_Msk));
8289
}
8390

91+
void us_ticker_free(void)
92+
{
93+
TIMER_T *timer_base = (TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname);
94+
95+
/* Stop counting */
96+
TIMER_Stop(timer_base);
97+
98+
/* Wait for timer to stop counting and unset active flag */
99+
while((timer_base->CTL & TIMER_CTL_ACTSTS_Msk));
100+
101+
/* Disable interrupt */
102+
TIMER_DisableInt(timer_base);
103+
NVIC_DisableIRQ(TIMER_MODINIT.irq_n);
104+
105+
/* Disable IP clock */
106+
CLK_DisableModuleClock(TIMER_MODINIT.clkidx);
107+
108+
ticker_inited = 0;
109+
}
110+
84111
uint32_t us_ticker_read()
85112
{
86113
if (! ticker_inited) {
@@ -109,6 +136,8 @@ void us_ticker_set_interrupt(timestamp_t timestamp)
109136
uint32_t cmp_timer = timestamp * NU_TMRCLK_PER_TICK;
110137
cmp_timer = NU_CLAMP(cmp_timer, TMR_CMP_MIN, TMR_CMP_MAX);
111138
timer_base->CMP = cmp_timer;
139+
140+
TIMER_EnableInt(timer_base);
112141
}
113142

114143
void us_ticker_disable_interrupt(void)
@@ -144,3 +173,5 @@ static void tmr0_vec(void)
144173
// NOTE: us_ticker_set_interrupt() may get called in us_ticker_irq_handler();
145174
us_ticker_irq_handler();
146175
}
176+
177+
#endif

targets/targets.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3844,7 +3844,7 @@
38443844
},
38453845
"inherits": ["Target"],
38463846
"macros_add": ["MBEDTLS_CONFIG_HW_SUPPORT"],
3847-
"device_has": ["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"],
3847+
"device_has": ["USTICKER", "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"],
38483848
"release_versions": ["5"],
38493849
"device_name": "NUC472HI8AE",
38503850
"bootloader_supported": true,
@@ -3915,7 +3915,7 @@
39153915
},
39163916
"inherits": ["Target"],
39173917
"progen": {"target": "numaker-pfm-m453"},
3918-
"device_has": ["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"],
3918+
"device_has": ["USTICKER", "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"],
39193919
"release_versions": ["2", "5"],
39203920
"device_name": "M453VG6AE",
39213921
"bootloader_supported": true
@@ -3946,7 +3946,7 @@
39463946
},
39473947
"inherits": ["Target"],
39483948
"macros": ["CMSIS_VECTAB_VIRTUAL", "CMSIS_VECTAB_VIRTUAL_HEADER_FILE=\"cmsis_nvic.h\"","MBED_FAULT_HANDLER_DISABLED"],
3949-
"device_has": ["RTC", "ANALOGIN", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "STDIO_MESSAGES", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH"],
3949+
"device_has": ["USTICKER", "RTC", "ANALOGIN", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "SERIAL", "SERIAL_ASYNCH", "SERIAL_FC", "STDIO_MESSAGES", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH"],
39503950
"release_versions": ["5"],
39513951
"device_name": "NANO130KE3BN"
39523952
},
@@ -4113,7 +4113,7 @@
41134113
},
41144114
"inherits": ["Target"],
41154115
"macros_add": ["MBEDTLS_CONFIG_HW_SUPPORT"],
4116-
"device_has": ["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"],
4116+
"device_has": ["USTICKER", "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"],
41174117
"release_versions": ["5"],
41184118
"device_name": "M487JIDAE",
41194119
"bootloader_supported": true,

0 commit comments

Comments
 (0)