Skip to content

Nuvoton: Adhere to reworked ticker spec to release with Mbed OS 5.9 #7029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 2, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions TESTS/mbed_hal/common_tickers/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ unsigned int ticker_overflow_delta;

/* Auxiliary function to count ticker ticks elapsed during execution of N cycles of empty while loop.
* Parameter <step> is used to disable compiler optimisation. */
MBED_NOINLINE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this inserted?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind. Commit b18f690 explained it perfectly.

uint32_t count_ticks(uint32_t cycles, uint32_t step)
{
register uint32_t reg_cycles = cycles;
Expand Down
36 changes: 35 additions & 1 deletion targets/TARGET_NUVOTON/TARGET_M451/lp_ticker.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ static int ticker_inited = 0;
void lp_ticker_init(void)
{
if (ticker_inited) {
/* 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;
Expand Down Expand Up @@ -88,7 +92,7 @@ void lp_ticker_init(void)

NVIC_EnableIRQ(TIMER_MODINIT.irq_n);

TIMER_EnableInt(timer_base);
TIMER_DisableInt(timer_base);
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);

TIMER_EnableWakeup(timer_base);
Expand All @@ -101,6 +105,33 @@ void lp_ticker_init(void)
while(! (timer_base->CTL & TIMER_CTL_ACTSTS_Msk));
}

void lp_ticker_free(void)
{
TIMER_T *timer_base = (TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname);

/* Stop counting */
TIMER_Stop(timer_base);
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);

/* Wait for timer to stop counting and unset active flag */
while((timer_base->CTL & TIMER_CTL_ACTSTS_Msk));

/* Disable wakeup */
TIMER_DisableWakeup(timer_base);
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this wait_us needed? Same question for the wait_us below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c1728p9 All these wait_us are necessary.


/* Disable interrupt */
TIMER_DisableInt(timer_base);
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);

NVIC_DisableIRQ(TIMER_MODINIT.irq_n);

/* Disable IP clock */
CLK_DisableModuleClock(TIMER_MODINIT.clkidx);

ticker_inited = 0;
}

timestamp_t lp_ticker_read()
{
if (! ticker_inited) {
Expand Down Expand Up @@ -131,6 +162,9 @@ void lp_ticker_set_interrupt(timestamp_t timestamp)

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)
Expand Down
32 changes: 31 additions & 1 deletion targets/TARGET_NUVOTON/TARGET_M451/us_ticker.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/

#include "us_ticker_api.h"

#if DEVICE_USTICKER

#include "sleep_api.h"
#include "mbed_assert.h"
#include "nu_modutil.h"
Expand Down Expand Up @@ -45,6 +48,10 @@ static int ticker_inited = 0;
void us_ticker_init(void)
{
if (ticker_inited) {
/* By HAL spec, ticker_init allows the ticker to keep counting and disables the
* ticker interrupt. */
us_ticker_disable_interrupt();
us_ticker_clear_interrupt();
return;
}
ticker_inited = 1;
Expand Down Expand Up @@ -75,13 +82,32 @@ void us_ticker_init(void)

NVIC_EnableIRQ(TIMER_MODINIT.irq_n);

TIMER_EnableInt(timer_base);
TIMER_DisableInt(timer_base);

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

void us_ticker_free(void)
{
TIMER_T *timer_base = (TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname);

/* Stop counting */
TIMER_Stop(timer_base);
/* Wait for timer to stop counting and unset active flag */
while((timer_base->CTL & TIMER_CTL_ACTSTS_Msk));

/* Disable interrupt */
TIMER_DisableInt(timer_base);
NVIC_DisableIRQ(TIMER_MODINIT.irq_n);

/* Disable IP clock */
CLK_DisableModuleClock(TIMER_MODINIT.clkidx);

ticker_inited = 0;
}

uint32_t us_ticker_read()
{
if (! ticker_inited) {
Expand Down Expand Up @@ -110,6 +136,8 @@ void us_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);
timer_base->CMP = cmp_timer;

TIMER_EnableInt(timer_base);
}

void us_ticker_disable_interrupt(void)
Expand Down Expand Up @@ -145,3 +173,5 @@ static void tmr0_vec(void)
// NOTE: us_ticker_set_interrupt() may get called in us_ticker_irq_handler();
us_ticker_irq_handler();
}

#endif
36 changes: 35 additions & 1 deletion targets/TARGET_NUVOTON/TARGET_M480/lp_ticker.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ static int ticker_inited = 0;
void lp_ticker_init(void)
{
if (ticker_inited) {
/* 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;
Expand Down Expand Up @@ -88,7 +92,7 @@ void lp_ticker_init(void)

NVIC_EnableIRQ(TIMER_MODINIT.irq_n);

TIMER_EnableInt(timer_base);
TIMER_DisableInt(timer_base);
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);

TIMER_EnableWakeup(timer_base);
Expand All @@ -101,6 +105,33 @@ void lp_ticker_init(void)
while(! (timer_base->CTL & TIMER_CTL_ACTSTS_Msk));
}

void lp_ticker_free(void)
{
TIMER_T *timer_base = (TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname);

/* Stop counting */
TIMER_Stop(timer_base);
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);

/* Wait for timer to stop counting and unset active flag */
while((timer_base->CTL & TIMER_CTL_ACTSTS_Msk));

/* Disable wakeup */
TIMER_DisableWakeup(timer_base);
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);

/* Disable interrupt */
TIMER_DisableInt(timer_base);
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);

NVIC_DisableIRQ(TIMER_MODINIT.irq_n);

/* Disable IP clock */
CLK_DisableModuleClock(TIMER_MODINIT.clkidx);

ticker_inited = 0;
}

timestamp_t lp_ticker_read()
{
if (! ticker_inited) {
Expand Down Expand Up @@ -131,6 +162,9 @@ void lp_ticker_set_interrupt(timestamp_t timestamp)

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)
Expand Down
33 changes: 32 additions & 1 deletion targets/TARGET_NUVOTON/TARGET_M480/us_ticker.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/

#include "us_ticker_api.h"

#if DEVICE_USTICKER

#include "sleep_api.h"
#include "mbed_assert.h"
#include "nu_modutil.h"
Expand Down Expand Up @@ -45,6 +48,10 @@ static int ticker_inited = 0;
void us_ticker_init(void)
{
if (ticker_inited) {
/* By HAL spec, ticker_init allows the ticker to keep counting and disables the
* ticker interrupt. */
us_ticker_disable_interrupt();
us_ticker_clear_interrupt();
return;
}
ticker_inited = 1;
Expand Down Expand Up @@ -75,13 +82,33 @@ void us_ticker_init(void)

NVIC_EnableIRQ(TIMER_MODINIT.irq_n);

TIMER_EnableInt(timer_base);
TIMER_DisableInt(timer_base);

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

void us_ticker_free(void)
{
TIMER_T *timer_base = (TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname);

/* Stop counting */
TIMER_Stop(timer_base);

/* Wait for timer to stop counting and unset active flag */
while((timer_base->CTL & TIMER_CTL_ACTSTS_Msk));

/* Disable interrupt */
TIMER_DisableInt(timer_base);
NVIC_DisableIRQ(TIMER_MODINIT.irq_n);

/* Disable IP clock */
CLK_DisableModuleClock(TIMER_MODINIT.clkidx);

ticker_inited = 0;
}

uint32_t us_ticker_read()
{
if (! ticker_inited) {
Expand Down Expand Up @@ -110,6 +137,8 @@ void us_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);
timer_base->CMP = cmp_timer;

TIMER_EnableInt(timer_base);
}

void us_ticker_disable_interrupt(void)
Expand Down Expand Up @@ -145,3 +174,5 @@ static void tmr0_vec(void)
// NOTE: us_ticker_set_interrupt() may get called in us_ticker_irq_handler();
us_ticker_irq_handler();
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ void CLK_EnableCKO(uint32_t u32ClkSrc, uint32_t u32ClkDiv)
*/
void CLK_PowerDown(void)
{
SCB->SCR = SCB_SCR_SLEEPDEEP_Msk;
/* Set the processor uses deep sleep as its low power mode */
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;

CLK->PWRCTL |= (CLK_PWRCTL_PD_EN_Msk | CLK_PWRCTL_WK_DLY_Msk );
__WFI();
}
Expand All @@ -81,6 +83,9 @@ void CLK_PowerDown(void)
*/
void CLK_Idle(void)
{
/* Set the processor uses sleep as its low power mode */
SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;

CLK->PWRCTL &= ~(CLK_PWRCTL_PD_EN_Msk );
__WFI();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ define symbol __ICFEDIT_region_IRAM_start__ = 0x20000000;
define symbol __ICFEDIT_region_IRAM_end__ = 0x20004000 - 1;
/*-Sizes-*/
define symbol __ICFEDIT_size_cstack__ = 0x600;
define symbol __ICFEDIT_size_heap__ = 0x1000;
define symbol __ICFEDIT_size_heap__ = 0xE00;
/**** End of ICF editor section. ###ICF###*/


Expand Down
36 changes: 35 additions & 1 deletion targets/TARGET_NUVOTON/TARGET_NANO100/lp_ticker.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ static int ticker_inited = 0;
void lp_ticker_init(void)
{
if (ticker_inited) {
/* 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;
Expand Down Expand Up @@ -92,7 +96,7 @@ void lp_ticker_init(void)

NVIC_EnableIRQ(TIMER_MODINIT.irq_n);

TIMER_EnableInt(timer_base);
TIMER_DisableInt(timer_base);
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);

TIMER_EnableWakeup(timer_base);
Expand All @@ -105,6 +109,33 @@ void lp_ticker_init(void)
while(! (timer_base->CTL & TIMER_CTL_TMR_ACT_Msk));
}

void lp_ticker_free(void)
{
TIMER_T *timer_base = (TIMER_T *) NU_MODBASE(TIMER_MODINIT.modname);

/* Stop counting */
TIMER_Stop(timer_base);
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);

/* Wait for timer to stop counting and unset active flag */
while((timer_base->CTL & TIMER_CTL_TMR_ACT_Msk));

/* Disable wakeup */
TIMER_DisableWakeup(timer_base);
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);

/* Disable interrupt */
TIMER_DisableInt(timer_base);
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);

NVIC_DisableIRQ(TIMER_MODINIT.irq_n);

/* Disable IP clock */
CLK_DisableModuleClock(TIMER_MODINIT.clkidx);

ticker_inited = 0;
}

timestamp_t lp_ticker_read()
{
if (! ticker_inited) {
Expand Down Expand Up @@ -135,6 +166,9 @@ void lp_ticker_set_interrupt(timestamp_t timestamp)

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)
Expand Down
Loading