Skip to content
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

Nuvoton: Fix mbed_hal-sleep test failed #8049

Merged
merged 7 commits into from Oct 1, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions targets/TARGET_NUVOTON/TARGET_M2351/serial_api.c
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
11 changes: 11 additions & 0 deletions targets/TARGET_NUVOTON/TARGET_M2351/sleep.c
Expand Up @@ -22,9 +22,14 @@
#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.
*/
Expand All @@ -42,6 +47,12 @@ void hal_sleep(void)
__NONSECURE_ENTRY
void hal_deepsleep(void)
{
#if DEVICE_SERIAL
if (!serial_can_deep_sleep()) {
return;
}
#endif

SYS_UnlockReg();
CLK_PowerDown();
SYS_LockReg();
Expand Down
22 changes: 22 additions & 0 deletions targets/TARGET_NUVOTON/TARGET_M451/serial_api.c
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
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
22 changes: 22 additions & 0 deletions targets/TARGET_NUVOTON/TARGET_M480/serial_api.c
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
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
24 changes: 23 additions & 1 deletion targets/TARGET_NUVOTON/TARGET_NANO100/serial_api.c
Expand Up @@ -18,13 +18,14 @@

#if DEVICE_SERIAL

#include <string.h>
#include "cmsis.h"
#include "mbed_error.h"
#include "mbed_assert.h"
#include "PeripheralPins.h"
#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 @@ -76,6 +77,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 @@ -990,4 +993,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_NANO100/sleep.c
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
22 changes: 22 additions & 0 deletions targets/TARGET_NUVOTON/TARGET_NUC472/serial_api.c
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 @@ -1136,4 +1139,23 @@ static int serial_is_irq_en(serial_t *obj, SerialIrq irq)
}

#endif // #if DEVICE_SERIAL_ASYNCH

bool serial_can_deep_sleep(void)
Copy link
Member

Choose a reason for hiding this comment

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

I think the code of this fun is the same for every target in this PR. It might be a good idea to move it to a common file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@fkjagodzinski serial_can_deep_sleep is recommended in #7698 by @c1728p9 , but it is not defined in HAL spec. I can only fix on Nuvoton targets.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I wasn't aware of the origin of this function. This could be a useful HAL feature. 👍

Anyway, my original comment was only about your new code. It might be convenient not to keep a few copies of one function. In this case it would stay inside targets/TARGET_NUVOTON/.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@fkjagodzinski Currently, on Nuvoton targets, HAL implementations (e.g. serial_api.c) for each target (NUMAKER_PFM_NUC472, NUMAKER_PFM_M453, etc.) are branched. Except these HAL implementations are resolved to merge into one, serial_can_deep_sleep would be kept branched for consistency.

Copy link
Member

Choose a reason for hiding this comment

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

Sure, no problem for me.

{
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_NUC472/sleep.c
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