Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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

Large diffs are not rendered by default.

Binary file not shown.
12 changes: 12 additions & 0 deletions targets/TARGET_NUVOTON/TARGET_M2351/device/stddriver_secure.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,18 @@ void SYS_UnlockReg_S(void)
SYS_UnlockReg();
}

__NONSECURE_ENTRY
void CLK_Idle_S(void)
{
CLK_Idle();
}

__NONSECURE_ENTRY
void CLK_PowerDown_S(void)
{
CLK_PowerDown();
}

static bool check_mod_ns(int modclass, uint32_t modidx)
{
const nu_modidx_ns_t *modidx_ns = modidx_ns_tab;
Expand Down
8 changes: 8 additions & 0 deletions targets/TARGET_NUVOTON/TARGET_M2351/device/stddriver_secure.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ void SYS_LockReg_S(void);
__NONSECURE_ENTRY
void SYS_UnlockReg_S(void);

/* Secure CLK_Idle */
__NONSECURE_ENTRY
void CLK_Idle_S(void);

/* Secure CLK_PowerDown */
__NONSECURE_ENTRY
void CLK_PowerDown_S(void);

#ifdef __cplusplus
}
#endif
Expand Down
12 changes: 6 additions & 6 deletions targets/TARGET_NUVOTON/TARGET_M2351/lp_ticker.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
#if DEVICE_LPTICKER

#include "sleep_api.h"
#include "mbed_wait_api.h"
#include "mbed_assert.h"
#include "nu_modutil.h"
#include "nu_timer.h"
#include "nu_miscutil.h"
#include "partition_M2351.h"

Expand Down Expand Up @@ -137,24 +137,24 @@ void lp_ticker_init(void)
// Continuous mode
// NOTE: TIMER_CTL_CNTDATEN_Msk exists in NUC472, but not in M451/M480/M2351. In M451/M480/M2351, TIMER_CNT is updated continuously by default.
timer_base->CTL = TIMER_CONTINUOUS_MODE | prescale_timer/* | TIMER_CTL_CNTDATEN_Msk*/;
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
nu_busy_wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);

timer_base->CMP = cmp_timer;
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
nu_busy_wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);

// Set vector
NVIC_SetVector(TIMER_MODINIT.irq_n, (uint32_t) TIMER_MODINIT.var);

NVIC_DisableIRQ(TIMER_MODINIT.irq_n);

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

TIMER_EnableWakeup(timer_base);
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
nu_busy_wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);

TIMER_Start(timer_base);
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
nu_busy_wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);

/* Wait for timer to start counting and raise active flag */
while(! (timer_base->CTL & TIMER_CTL_ACTSTS_Msk));
Expand Down
22 changes: 22 additions & 0 deletions targets/TARGET_NUVOTON/TARGET_M2351/serial_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "nu_modutil.h"
#include "nu_bitutil.h"
#include <string.h>
#include <stdbool.h>

#if DEVICE_SERIAL_ASYNCH
#include "dma_api.h"
Expand Down Expand Up @@ -87,6 +88,8 @@ static void serial_check_dma_usage(DMAUsage *dma_usage, int *dma_ch);
static int serial_is_irq_en(serial_t *obj, SerialIrq irq);
#endif

bool serial_can_deep_sleep(void);

static struct nu_uart_var uart0_var = {
.ref_cnt = 0,
.obj = NULL,
Expand Down Expand Up @@ -1171,4 +1174,23 @@ static int serial_is_irq_en(serial_t *obj, SerialIrq irq)
}

#endif // #if DEVICE_SERIAL_ASYNCH

bool serial_can_deep_sleep(void)
{
bool sleep_allowed = 1;
const struct nu_modinit_s *modinit = uart_modinit_tab;
while (modinit->var != NULL) {
struct nu_uart_var *uart_var = (struct nu_uart_var *) modinit->var;
UART_T *uart_base = (UART_T *) NU_MODBASE(modinit->modname);
if (uart_var->ref_cnt > 0) {
if (!UART_IS_TX_EMPTY(uart_base)) {
sleep_allowed = 0;
break;
}
}
modinit++;
}
return sleep_allowed;
}

#endif // #if DEVICE_SERIAL
26 changes: 22 additions & 4 deletions targets/TARGET_NUVOTON/TARGET_M2351/sleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,48 @@
#include "device.h"
#include "objects.h"
#include "PeripheralPins.h"
#include <stdbool.h>

#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
#if DEVICE_SERIAL
bool serial_can_deep_sleep(void);
#endif

/**
* Enter idle mode, in which just CPU is halted.
*/
__NONSECURE_ENTRY
void hal_sleep(void)
{
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
SYS_UnlockReg();
CLK_Idle();
SYS_LockReg();
#else
SYS_UnlockReg_S();
CLK_Idle_S();
SYS_LockReg_S();
#endif
}

/**
* Enter power-down mode, in which HXT/HIRC are halted.
*/
__NONSECURE_ENTRY
void hal_deepsleep(void)
{
#if DEVICE_SERIAL
if (!serial_can_deep_sleep()) {
return;
}
#endif

#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
SYS_UnlockReg();
CLK_PowerDown();
SYS_LockReg();
#else
SYS_UnlockReg_S();
CLK_PowerDown_S();
SYS_LockReg_S();
#endif
}

#endif
#endif
12 changes: 6 additions & 6 deletions targets/TARGET_NUVOTON/TARGET_M451/lp_ticker.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
#if DEVICE_LPTICKER

#include "sleep_api.h"
#include "mbed_wait_api.h"
#include "mbed_assert.h"
#include "nu_modutil.h"
#include "nu_timer.h"
#include "nu_miscutil.h"

/* Micro seconds per second */
Expand Down Expand Up @@ -103,24 +103,24 @@ void lp_ticker_init(void)
// Continuous mode
// NOTE: TIMER_CTL_CNTDATEN_Msk exists in NUC472, but not in M451. In M451, TIMER_CNT is updated continuously by default.
timer_base->CTL = TIMER_CONTINUOUS_MODE | prescale_timer/* | TIMER_CTL_CNTDATEN_Msk*/;
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
nu_busy_wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);

timer_base->CMP = cmp_timer;
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
nu_busy_wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);

// Set vector
NVIC_SetVector(TIMER_MODINIT.irq_n, (uint32_t) TIMER_MODINIT.var);

NVIC_DisableIRQ(TIMER_MODINIT.irq_n);

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

TIMER_EnableWakeup(timer_base);
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
nu_busy_wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);

TIMER_Start(timer_base);
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
nu_busy_wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);

/* Wait for timer to start counting and raise active flag */
while(! (timer_base->CTL & TIMER_CTL_ACTSTS_Msk));
Expand Down
22 changes: 22 additions & 0 deletions targets/TARGET_NUVOTON/TARGET_M451/serial_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "nu_modutil.h"
#include "nu_bitutil.h"
#include <string.h>
#include <stdbool.h>

#if DEVICE_SERIAL_ASYNCH
#include "dma_api.h"
Expand Down Expand Up @@ -83,6 +84,8 @@ static void serial_check_dma_usage(DMAUsage *dma_usage, int *dma_ch);
static int serial_is_irq_en(serial_t *obj, SerialIrq irq);
#endif

bool serial_can_deep_sleep(void);

static struct nu_uart_var uart0_var = {
.ref_cnt = 0,
.obj = NULL,
Expand Down Expand Up @@ -1088,4 +1091,23 @@ static int serial_is_irq_en(serial_t *obj, SerialIrq irq)
}

#endif // #if DEVICE_SERIAL_ASYNCH

bool serial_can_deep_sleep(void)
{
bool sleep_allowed = 1;
const struct nu_modinit_s *modinit = uart_modinit_tab;
while (modinit->var != NULL) {
struct nu_uart_var *uart_var = (struct nu_uart_var *) modinit->var;
UART_T *uart_base = (UART_T *) NU_MODBASE(modinit->modname);
if (uart_var->ref_cnt > 0) {
if (!UART_IS_TX_EMPTY(uart_base)) {
sleep_allowed = 0;
break;
}
}
modinit++;
}
return sleep_allowed;
}

#endif // #if DEVICE_SERIAL
11 changes: 11 additions & 0 deletions targets/TARGET_NUVOTON/TARGET_M451/sleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
#include "device.h"
#include "objects.h"
#include "PeripheralPins.h"
#include <stdbool.h>

#if DEVICE_SERIAL
bool serial_can_deep_sleep(void);
#endif

/**
* Enter idle mode, in which just CPU is halted.
Expand All @@ -38,6 +43,12 @@ void hal_sleep(void)
*/
void hal_deepsleep(void)
{
#if DEVICE_SERIAL
if (!serial_can_deep_sleep()) {
return;
}
#endif

SYS_UnlockReg();
CLK_PowerDown();
SYS_LockReg();
Expand Down
12 changes: 6 additions & 6 deletions targets/TARGET_NUVOTON/TARGET_M480/lp_ticker.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
#if DEVICE_LPTICKER

#include "sleep_api.h"
#include "mbed_wait_api.h"
#include "mbed_assert.h"
#include "nu_modutil.h"
#include "nu_timer.h"
#include "nu_miscutil.h"

/* Micro seconds per second */
Expand Down Expand Up @@ -103,24 +103,24 @@ void lp_ticker_init(void)
// Continuous mode
// NOTE: TIMER_CTL_CNTDATEN_Msk exists in NUC472, but not in M451/M480. In M451/M480, TIMER_CNT is updated continuously by default.
timer_base->CTL = TIMER_CONTINUOUS_MODE | prescale_timer/* | TIMER_CTL_CNTDATEN_Msk*/;
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
nu_busy_wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);

timer_base->CMP = cmp_timer;
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
nu_busy_wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);

// Set vector
NVIC_SetVector(TIMER_MODINIT.irq_n, (uint32_t) TIMER_MODINIT.var);

NVIC_DisableIRQ(TIMER_MODINIT.irq_n);

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

TIMER_EnableWakeup(timer_base);
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
nu_busy_wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);

TIMER_Start(timer_base);
wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);
nu_busy_wait_us((NU_US_PER_SEC / NU_TMRCLK_PER_SEC) * 3);

/* Wait for timer to start counting and raise active flag */
while(! (timer_base->CTL & TIMER_CTL_ACTSTS_Msk));
Expand Down
22 changes: 22 additions & 0 deletions targets/TARGET_NUVOTON/TARGET_M480/serial_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "nu_modutil.h"
#include "nu_bitutil.h"
#include <string.h>
#include <stdbool.h>

#if DEVICE_SERIAL_ASYNCH
#include "dma_api.h"
Expand Down Expand Up @@ -87,6 +88,8 @@ static void serial_check_dma_usage(DMAUsage *dma_usage, int *dma_ch);
static int serial_is_irq_en(serial_t *obj, SerialIrq irq);
#endif

bool serial_can_deep_sleep(void);

static struct nu_uart_var uart0_var = {
.ref_cnt = 0,
.obj = NULL,
Expand Down Expand Up @@ -1145,4 +1148,23 @@ static int serial_is_irq_en(serial_t *obj, SerialIrq irq)
}

#endif // #if DEVICE_SERIAL_ASYNCH

bool serial_can_deep_sleep(void)
{
bool sleep_allowed = 1;
const struct nu_modinit_s *modinit = uart_modinit_tab;
while (modinit->var != NULL) {
struct nu_uart_var *uart_var = (struct nu_uart_var *) modinit->var;
UART_T *uart_base = (UART_T *) NU_MODBASE(modinit->modname);
if (uart_var->ref_cnt > 0) {
if (!UART_IS_TX_EMPTY(uart_base)) {
sleep_allowed = 0;
break;
}
}
modinit++;
}
return sleep_allowed;
}

#endif // #if DEVICE_SERIAL
11 changes: 11 additions & 0 deletions targets/TARGET_NUVOTON/TARGET_M480/sleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
#include "device.h"
#include "objects.h"
#include "PeripheralPins.h"
#include <stdbool.h>

#if DEVICE_SERIAL
bool serial_can_deep_sleep(void);
#endif

/**
* Enter idle mode, in which just CPU is halted.
Expand All @@ -38,6 +43,12 @@ void hal_sleep(void)
*/
void hal_deepsleep(void)
{
#if DEVICE_SERIAL
if (!serial_can_deep_sleep()) {
return;
}
#endif

SYS_UnlockReg();
CLK_PowerDown();
SYS_LockReg();
Expand Down
Loading