Skip to content

Nuvoton: Refine UART init/deinit #8900

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 4 commits into from
Dec 4, 2018
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
2 changes: 0 additions & 2 deletions targets/TARGET_NUVOTON/TARGET_M2351/objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ struct analogin_s {

struct serial_s {
UARTName uart;
PinName pin_tx;
PinName pin_rx;

uint32_t baudrate;
uint32_t databits;
Expand Down
76 changes: 43 additions & 33 deletions targets/TARGET_NUVOTON/TARGET_M2351/serial_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,39 +200,45 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
struct nu_uart_var *var = (struct nu_uart_var *) modinit->var;

if (! var->ref_cnt) {
do {
/* Reset module
*
* NOTE: We must call secure version (from non-secure domain) because SYS/CLK regions are secure.
*/
SYS_ResetModule_S(modinit->rsetidx);

/* Select IP clock source
*
* NOTE: We must call secure version (from non-secure domain) because SYS/CLK regions are secure.
*/
CLK_SetModuleClock_S(modinit->clkidx, modinit->clksrc, modinit->clkdiv);

/* Enable IP clock
*
* NOTE: We must call secure version (from non-secure domain) because SYS/CLK regions are secure.
*/
CLK_EnableModuleClock_S(modinit->clkidx);

pinmap_pinout(tx, PinMap_UART_TX);
pinmap_pinout(rx, PinMap_UART_RX);
} while (0);
/* Reset module
*
* NOTE: We must call secure version (from non-secure domain) because SYS/CLK regions are secure.
*/
SYS_ResetModule_S(modinit->rsetidx);

/* Select IP clock source
*
* NOTE: We must call secure version (from non-secure domain) because SYS/CLK regions are secure.
*/
CLK_SetModuleClock_S(modinit->clkidx, modinit->clksrc, modinit->clkdiv);

/* Enable IP clock
*
* NOTE: We must call secure version (from non-secure domain) because SYS/CLK regions are secure.
*/
CLK_EnableModuleClock_S(modinit->clkidx);

pinmap_pinout(tx, PinMap_UART_TX);
pinmap_pinout(rx, PinMap_UART_RX);

// Configure baudrate
int baudrate = 9600;
if (obj->serial.uart == STDIO_UART) {
#if MBED_CONF_PLATFORM_STDIO_BAUD_RATE
baudrate = MBED_CONF_PLATFORM_STDIO_BAUD_RATE;
#endif
} else {
#if MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE
baudrate = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE;
#endif
}
serial_baud(obj, baudrate);

obj->serial.pin_tx = tx;
obj->serial.pin_rx = rx;
// Configure data bits, parity, and stop bits
serial_format(obj, 8, ParityNone, 1);
}
var->ref_cnt ++;

// Configure the UART module and set its baudrate
serial_baud(obj, 9600);
// Configure data bits, parity, and stop bits
serial_format(obj, 8, ParityNone, 1);

obj->serial.vec = var->vec;
obj->serial.irq_en = 0;

Expand All @@ -244,10 +250,12 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
obj->serial.dma_chn_id_rx = DMA_ERROR_OUT_OF_CHANNELS;
#endif

// For stdio management
if (obj->serial.uart == STDIO_UART) {
/* With support for checking H/W UART initialized or not, we allow serial_init(&stdio_uart)
* calls in even though H/W UART 'STDIO_UART' has initialized. When serial_init(&stdio_uart)
* calls in, we only need to set the 'stdio_uart_inited' flag. */
if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) {
MBED_ASSERT(obj->serial.uart == STDIO_UART);
stdio_uart_inited = 1;
memcpy(&stdio_uart, obj, sizeof(serial_t));
}

if (var->ref_cnt) {
Expand Down Expand Up @@ -296,7 +304,9 @@ void serial_free(serial_t *obj)
var->obj = NULL;
}

if (obj->serial.uart == STDIO_UART) {
/* Clear the 'stdio_uart_inited' flag when serial_free(&stdio_uart) calls in. */
if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) {
MBED_ASSERT(obj->serial.uart == STDIO_UART);
stdio_uart_inited = 0;
}

Expand Down
2 changes: 0 additions & 2 deletions targets/TARGET_NUVOTON/TARGET_M451/objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ struct analogin_s {

struct serial_s {
UARTName uart;
PinName pin_tx;
PinName pin_rx;

uint32_t baudrate;
uint32_t databits;
Expand Down
46 changes: 29 additions & 17 deletions targets/TARGET_NUVOTON/TARGET_M451/serial_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,25 +172,33 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
if (! var->ref_cnt) {
// Reset this module
SYS_ResetModule(modinit->rsetidx);

// Select IP clock source
CLK_SetModuleClock(modinit->clkidx, modinit->clksrc, modinit->clkdiv);
// Enable IP clock
CLK_EnableModuleClock(modinit->clkidx);

pinmap_pinout(tx, PinMap_UART_TX);
pinmap_pinout(rx, PinMap_UART_RX);

obj->serial.pin_tx = tx;
obj->serial.pin_rx = rx;

// Configure baudrate
int baudrate = 9600;
if (obj->serial.uart == STDIO_UART) {
#if MBED_CONF_PLATFORM_STDIO_BAUD_RATE
baudrate = MBED_CONF_PLATFORM_STDIO_BAUD_RATE;
#endif
} else {
#if MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE
baudrate = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE;
#endif
}
serial_baud(obj, baudrate);

// Configure data bits, parity, and stop bits
serial_format(obj, 8, ParityNone, 1);
}
var->ref_cnt ++;

// Configure the UART module and set its baudrate
serial_baud(obj, 9600);
// Configure data bits, parity, and stop bits
serial_format(obj, 8, ParityNone, 1);


obj->serial.vec = var->vec;
obj->serial.irq_en = 0;

Expand All @@ -202,12 +210,14 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
obj->serial.dma_chn_id_rx = DMA_ERROR_OUT_OF_CHANNELS;
#endif

// For stdio management
if (obj->serial.uart == STDIO_UART) {
/* With support for checking H/W UART initialized or not, we allow serial_init(&stdio_uart)
* calls in even though H/W UART 'STDIO_UART' has initialized. When serial_init(&stdio_uart)
* calls in, we only need to set the 'stdio_uart_inited' flag. */
if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) {
MBED_ASSERT(obj->serial.uart == STDIO_UART);
stdio_uart_inited = 1;
memcpy(&stdio_uart, obj, sizeof(serial_t));
}

if (var->ref_cnt) {
// Mark this module to be inited.
int i = modinit - uart_modinit_tab;
Expand Down Expand Up @@ -248,11 +258,13 @@ void serial_free(serial_t *obj)
if (var->obj == obj) {
var->obj = NULL;
}

if (obj->serial.uart == STDIO_UART) {

/* Clear the 'stdio_uart_inited' flag when serial_free(&stdio_uart) calls in. */
if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) {
MBED_ASSERT(obj->serial.uart == STDIO_UART);
stdio_uart_inited = 0;
}

if (! var->ref_cnt) {
// Mark this module to be deinited.
int i = modinit - uart_modinit_tab;
Expand Down
2 changes: 0 additions & 2 deletions targets/TARGET_NUVOTON/TARGET_M480/objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ struct analogin_s {

struct serial_s {
UARTName uart;
PinName pin_tx;
PinName pin_rx;

uint32_t baudrate;
uint32_t databits;
Expand Down
56 changes: 33 additions & 23 deletions targets/TARGET_NUVOTON/TARGET_M480/serial_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,29 +200,35 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
struct nu_uart_var *var = (struct nu_uart_var *) modinit->var;

if (! var->ref_cnt) {
do {
// Reset this module
SYS_ResetModule(modinit->rsetidx);

// Select IP clock source
CLK_SetModuleClock(modinit->clkidx, modinit->clksrc, modinit->clkdiv);
// Enable IP clock
CLK_EnableModuleClock(modinit->clkidx);

pinmap_pinout(tx, PinMap_UART_TX);
pinmap_pinout(rx, PinMap_UART_RX);
} while (0);
// Reset this module
SYS_ResetModule(modinit->rsetidx);

// Select IP clock source
CLK_SetModuleClock(modinit->clkidx, modinit->clksrc, modinit->clkdiv);
// Enable IP clock
CLK_EnableModuleClock(modinit->clkidx);

pinmap_pinout(tx, PinMap_UART_TX);
pinmap_pinout(rx, PinMap_UART_RX);

// Configure baudrate
int baudrate = 9600;
if (obj->serial.uart == STDIO_UART) {
#if MBED_CONF_PLATFORM_STDIO_BAUD_RATE
baudrate = MBED_CONF_PLATFORM_STDIO_BAUD_RATE;
#endif
} else {
#if MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE
baudrate = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE;
#endif
}
serial_baud(obj, baudrate);

obj->serial.pin_tx = tx;
obj->serial.pin_rx = rx;
// Configure data bits, parity, and stop bits
serial_format(obj, 8, ParityNone, 1);
}
var->ref_cnt ++;

// Configure the UART module and set its baudrate
serial_baud(obj, 9600);
// Configure data bits, parity, and stop bits
serial_format(obj, 8, ParityNone, 1);

obj->serial.vec = var->vec;
obj->serial.irq_en = 0;

Expand All @@ -234,10 +240,12 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
obj->serial.dma_chn_id_rx = DMA_ERROR_OUT_OF_CHANNELS;
#endif

// For stdio management
if (obj->serial.uart == STDIO_UART) {
/* With support for checking H/W UART initialized or not, we allow serial_init(&stdio_uart)
* calls in even though H/W UART 'STDIO_UART' has initialized. When serial_init(&stdio_uart)
* calls in, we only need to set the 'stdio_uart_inited' flag. */
if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) {
MBED_ASSERT(obj->serial.uart == STDIO_UART);
stdio_uart_inited = 1;
memcpy(&stdio_uart, obj, sizeof(serial_t));
}

if (var->ref_cnt) {
Expand Down Expand Up @@ -283,7 +291,9 @@ void serial_free(serial_t *obj)
var->obj = NULL;
}

if (obj->serial.uart == STDIO_UART) {
/* Clear the 'stdio_uart_inited' flag when serial_free(&stdio_uart) calls in. */
if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) {
MBED_ASSERT(obj->serial.uart == STDIO_UART);
stdio_uart_inited = 0;
}

Expand Down
2 changes: 0 additions & 2 deletions targets/TARGET_NUVOTON/TARGET_NANO100/objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ struct analogin_s {

struct serial_s {
UARTName uart;
PinName pin_tx;
PinName pin_rx;

uint32_t baudrate;
uint32_t databits;
Expand Down
48 changes: 30 additions & 18 deletions targets/TARGET_NUVOTON/TARGET_NANO100/serial_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,28 +137,36 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
if (! var->ref_cnt) {
// Reset this module
SYS_ResetModule(modinit->rsetidx);

// Select IP clock source
CLK_SetModuleClock(modinit->clkidx, modinit->clksrc, modinit->clkdiv);
// Enable IP clock
CLK_EnableModuleClock(modinit->clkidx);

pinmap_pinout(tx, PinMap_UART_TX);
pinmap_pinout(rx, PinMap_UART_RX);

obj->serial.pin_tx = tx;
obj->serial.pin_rx = rx;

// Configure baudrate
int baudrate = 9600;
if (obj->serial.uart == STDIO_UART) {
#if MBED_CONF_PLATFORM_STDIO_BAUD_RATE
baudrate = MBED_CONF_PLATFORM_STDIO_BAUD_RATE;
#endif
} else {
#if MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE
baudrate = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE;
#endif
}
serial_baud(obj, baudrate);

// Configure data bits, parity, and stop bits
serial_format(obj, 8, ParityNone, 1);
}
var->ref_cnt ++;

// Configure the UART module and set its baudrate
serial_baud(obj, 9600);
// Configure data bits, parity, and stop bits
serial_format(obj, 8, ParityNone, 1);


obj->serial.vec = var->vec;
obj->serial.irq_en = 0;

#if DEVICE_SERIAL_ASYNCH
obj->serial.dma_usage_tx = DMA_USAGE_NEVER;
obj->serial.dma_usage_rx = DMA_USAGE_NEVER;
Expand All @@ -167,12 +175,14 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
obj->serial.dma_chn_id_rx = DMA_ERROR_OUT_OF_CHANNELS;
#endif

// For stdio management
if (obj->serial.uart == STDIO_UART) {
/* With support for checking H/W UART initialized or not, we allow serial_init(&stdio_uart)
* calls in even though H/W UART 'STDIO_UART' has initialized. When serial_init(&stdio_uart)
* calls in, we only need to set the 'stdio_uart_inited' flag. */
if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) {
MBED_ASSERT(obj->serial.uart == STDIO_UART);
stdio_uart_inited = 1;
memcpy(&stdio_uart, obj, sizeof(serial_t));
}

if (var->ref_cnt) {
// Mark this module to be inited.
int i = modinit - uart_modinit_tab;
Expand Down Expand Up @@ -213,11 +223,13 @@ void serial_free(serial_t *obj)
if (var->obj == obj) {
var->obj = NULL;
}

if (obj->serial.uart == STDIO_UART) {

/* Clear the 'stdio_uart_inited' flag when serial_free(&stdio_uart) calls in. */
if (((uintptr_t) obj) == ((uintptr_t) &stdio_uart)) {
MBED_ASSERT(obj->serial.uart == STDIO_UART);
stdio_uart_inited = 0;
}

if (! var->ref_cnt) {
// Mark this module to be deinited.
int i = modinit - uart_modinit_tab;
Expand Down
2 changes: 0 additions & 2 deletions targets/TARGET_NUVOTON/TARGET_NUC472/objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ struct analogin_s {

struct serial_s {
UARTName uart;
PinName pin_tx;
PinName pin_rx;

uint32_t baudrate;
uint32_t databits;
Expand Down
Loading