mirror of https://github.com/ARMmbed/mbed-os.git
Support UART H/W module shared by multiple serial S/W objects
1. With GCC_ARM and uARM, some greentea tests fail due to no support for this. 2. Bind UART H/W module to correct serial S/W object for interrupt.pull/3290/head
parent
bfba208514
commit
1e480585db
|
@ -31,6 +31,7 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct nu_uart_var {
|
struct nu_uart_var {
|
||||||
|
uint32_t ref_cnt; // Reference count of the H/W module
|
||||||
serial_t * obj;
|
serial_t * obj;
|
||||||
uint32_t fifo_size_tx;
|
uint32_t fifo_size_tx;
|
||||||
uint32_t fifo_size_rx;
|
uint32_t fifo_size_rx;
|
||||||
|
@ -80,6 +81,7 @@ static int serial_is_irq_en(serial_t *obj, SerialIrq irq);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static struct nu_uart_var uart0_var = {
|
static struct nu_uart_var uart0_var = {
|
||||||
|
.ref_cnt = 0,
|
||||||
.obj = NULL,
|
.obj = NULL,
|
||||||
.fifo_size_tx = 16,
|
.fifo_size_tx = 16,
|
||||||
.fifo_size_rx = 16,
|
.fifo_size_rx = 16,
|
||||||
|
@ -91,6 +93,7 @@ static struct nu_uart_var uart0_var = {
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
static struct nu_uart_var uart1_var = {
|
static struct nu_uart_var uart1_var = {
|
||||||
|
.ref_cnt = 0,
|
||||||
.obj = NULL,
|
.obj = NULL,
|
||||||
.fifo_size_tx = 16,
|
.fifo_size_tx = 16,
|
||||||
.fifo_size_rx = 16,
|
.fifo_size_rx = 16,
|
||||||
|
@ -102,6 +105,7 @@ static struct nu_uart_var uart1_var = {
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
static struct nu_uart_var uart2_var = {
|
static struct nu_uart_var uart2_var = {
|
||||||
|
.ref_cnt = 0,
|
||||||
.obj = NULL,
|
.obj = NULL,
|
||||||
.fifo_size_tx = 16,
|
.fifo_size_tx = 16,
|
||||||
.fifo_size_rx = 16,
|
.fifo_size_rx = 16,
|
||||||
|
@ -113,6 +117,7 @@ static struct nu_uart_var uart2_var = {
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
static struct nu_uart_var uart3_var = {
|
static struct nu_uart_var uart3_var = {
|
||||||
|
.ref_cnt = 0,
|
||||||
.obj = NULL,
|
.obj = NULL,
|
||||||
.fifo_size_tx = 16,
|
.fifo_size_tx = 16,
|
||||||
.fifo_size_rx = 16,
|
.fifo_size_rx = 16,
|
||||||
|
@ -142,7 +147,7 @@ extern void mbed_sdk_init(void);
|
||||||
|
|
||||||
void serial_init(serial_t *obj, PinName tx, PinName rx)
|
void serial_init(serial_t *obj, PinName tx, PinName rx)
|
||||||
{
|
{
|
||||||
// NOTE: serial_init() gets called from _sys_open() timing of which is before main()/mbed_hal_init().
|
// NOTE: With armcc, serial_init() gets called from _sys_open() timing of which is before main()/mbed_sdk_init().
|
||||||
mbed_sdk_init();
|
mbed_sdk_init();
|
||||||
|
|
||||||
// Determine which UART_x the pins are used for
|
// Determine which UART_x the pins are used for
|
||||||
|
@ -156,6 +161,9 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
|
||||||
MBED_ASSERT(modinit != NULL);
|
MBED_ASSERT(modinit != NULL);
|
||||||
MBED_ASSERT(modinit->modname == obj->serial.uart);
|
MBED_ASSERT(modinit->modname == obj->serial.uart);
|
||||||
|
|
||||||
|
struct nu_uart_var *var = (struct nu_uart_var *) modinit->var;
|
||||||
|
|
||||||
|
if (! var->ref_cnt) {
|
||||||
// Reset this module
|
// Reset this module
|
||||||
SYS_ResetModule(modinit->rsetidx);
|
SYS_ResetModule(modinit->rsetidx);
|
||||||
|
|
||||||
|
@ -166,22 +174,18 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
|
||||||
|
|
||||||
pinmap_pinout(tx, PinMap_UART_TX);
|
pinmap_pinout(tx, PinMap_UART_TX);
|
||||||
pinmap_pinout(rx, PinMap_UART_RX);
|
pinmap_pinout(rx, PinMap_UART_RX);
|
||||||
// FIXME: Why PullUp?
|
|
||||||
//if (tx != NC) {
|
|
||||||
// pin_mode(tx, PullUp);
|
|
||||||
//}
|
|
||||||
//if (rx != NC) {
|
|
||||||
// pin_mode(rx, PullUp);
|
|
||||||
//}
|
|
||||||
obj->serial.pin_tx = tx;
|
obj->serial.pin_tx = tx;
|
||||||
obj->serial.pin_rx = rx;
|
obj->serial.pin_rx = rx;
|
||||||
|
}
|
||||||
|
var->ref_cnt ++;
|
||||||
|
|
||||||
// Configure the UART module and set its baudrate
|
// Configure the UART module and set its baudrate
|
||||||
serial_baud(obj, 9600);
|
serial_baud(obj, 9600);
|
||||||
// Configure data bits, parity, and stop bits
|
// Configure data bits, parity, and stop bits
|
||||||
serial_format(obj, 8, ParityNone, 1);
|
serial_format(obj, 8, ParityNone, 1);
|
||||||
|
|
||||||
obj->serial.vec = ((struct nu_uart_var *) modinit->var)->vec;
|
obj->serial.vec = var->vec;
|
||||||
|
|
||||||
#if DEVICE_SERIAL_ASYNCH
|
#if DEVICE_SERIAL_ASYNCH
|
||||||
obj->serial.dma_usage_tx = DMA_USAGE_NEVER;
|
obj->serial.dma_usage_tx = DMA_USAGE_NEVER;
|
||||||
|
@ -192,19 +196,28 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// For stdio management
|
// For stdio management
|
||||||
if (obj == &stdio_uart) {
|
if (obj->serial.uart == STDIO_UART) {
|
||||||
stdio_uart_inited = 1;
|
stdio_uart_inited = 1;
|
||||||
/* NOTE: Not required anymore because stdio_uart will be manually initialized in mbed-drivers/source/retarget.cpp from mbed beta */
|
memcpy(&stdio_uart, obj, sizeof(serial_t));
|
||||||
//memcpy(&stdio_uart, obj, sizeof(serial_t));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (var->ref_cnt) {
|
||||||
// Mark this module to be inited.
|
// Mark this module to be inited.
|
||||||
int i = modinit - uart_modinit_tab;
|
int i = modinit - uart_modinit_tab;
|
||||||
uart_modinit_mask |= 1 << i;
|
uart_modinit_mask |= 1 << i;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void serial_free(serial_t *obj)
|
void serial_free(serial_t *obj)
|
||||||
{
|
{
|
||||||
|
const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab);
|
||||||
|
MBED_ASSERT(modinit != NULL);
|
||||||
|
MBED_ASSERT(modinit->modname == obj->serial.uart);
|
||||||
|
|
||||||
|
struct nu_uart_var *var = (struct nu_uart_var *) modinit->var;
|
||||||
|
|
||||||
|
var->ref_cnt --;
|
||||||
|
if (! var->ref_cnt) {
|
||||||
#if DEVICE_SERIAL_ASYNCH
|
#if DEVICE_SERIAL_ASYNCH
|
||||||
if (obj->serial.dma_chn_id_tx != DMA_ERROR_OUT_OF_CHANNELS) {
|
if (obj->serial.dma_chn_id_tx != DMA_ERROR_OUT_OF_CHANNELS) {
|
||||||
dma_channel_free(obj->serial.dma_chn_id_tx);
|
dma_channel_free(obj->serial.dma_chn_id_tx);
|
||||||
|
@ -218,25 +231,26 @@ void serial_free(serial_t *obj)
|
||||||
|
|
||||||
UART_Close((UART_T *) NU_MODBASE(obj->serial.uart));
|
UART_Close((UART_T *) NU_MODBASE(obj->serial.uart));
|
||||||
|
|
||||||
const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab);
|
|
||||||
MBED_ASSERT(modinit != NULL);
|
|
||||||
MBED_ASSERT(modinit->modname == obj->serial.uart);
|
|
||||||
|
|
||||||
UART_DISABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), (UART_INTEN_RDAIEN_Msk | UART_INTEN_THREIEN_Msk | UART_INTEN_RXTOIEN_Msk));
|
UART_DISABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), (UART_INTEN_RDAIEN_Msk | UART_INTEN_THREIEN_Msk | UART_INTEN_RXTOIEN_Msk));
|
||||||
NVIC_DisableIRQ(modinit->irq_n);
|
NVIC_DisableIRQ(modinit->irq_n);
|
||||||
|
|
||||||
// Disable IP clock
|
// Disable IP clock
|
||||||
CLK_DisableModuleClock(modinit->clkidx);
|
CLK_DisableModuleClock(modinit->clkidx);
|
||||||
|
}
|
||||||
|
|
||||||
((struct nu_uart_var *) modinit->var)->obj = NULL;
|
if (var->obj == obj) {
|
||||||
|
var->obj = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
if (obj == &stdio_uart) {
|
if (obj->serial.uart == STDIO_UART) {
|
||||||
stdio_uart_inited = 0;
|
stdio_uart_inited = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (! var->ref_cnt) {
|
||||||
// Mark this module to be deinited.
|
// Mark this module to be deinited.
|
||||||
int i = modinit - uart_modinit_tab;
|
int i = modinit - uart_modinit_tab;
|
||||||
uart_modinit_mask &= ~(1 << i);
|
uart_modinit_mask &= ~(1 << i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void serial_baud(serial_t *obj, int baudrate) {
|
void serial_baud(serial_t *obj, int baudrate) {
|
||||||
|
@ -317,7 +331,6 @@ void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id)
|
||||||
MBED_ASSERT(modinit != NULL);
|
MBED_ASSERT(modinit != NULL);
|
||||||
MBED_ASSERT(modinit->modname == obj->serial.uart);
|
MBED_ASSERT(modinit->modname == obj->serial.uart);
|
||||||
|
|
||||||
((struct nu_uart_var *) modinit->var)->obj = obj;
|
|
||||||
obj->serial.irq_handler = (uint32_t) handler;
|
obj->serial.irq_handler = (uint32_t) handler;
|
||||||
obj->serial.irq_id = id;
|
obj->serial.irq_id = id;
|
||||||
|
|
||||||
|
@ -335,6 +348,11 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
|
||||||
NVIC_SetVector(modinit->irq_n, (uint32_t) obj->serial.vec);
|
NVIC_SetVector(modinit->irq_n, (uint32_t) obj->serial.vec);
|
||||||
NVIC_EnableIRQ(modinit->irq_n);
|
NVIC_EnableIRQ(modinit->irq_n);
|
||||||
|
|
||||||
|
struct nu_uart_var *var = (struct nu_uart_var *) modinit->var;
|
||||||
|
// Multiple serial S/W objects for single UART H/W module possibly.
|
||||||
|
// Bind serial S/W object to UART H/W module as interrupt is enabled.
|
||||||
|
var->obj = obj;
|
||||||
|
|
||||||
switch (irq) {
|
switch (irq) {
|
||||||
// NOTE: Setting inten_msk first to avoid race condition
|
// NOTE: Setting inten_msk first to avoid race condition
|
||||||
case RxIrq:
|
case RxIrq:
|
||||||
|
@ -634,7 +652,7 @@ int serial_irq_handler_asynch(serial_t *obj)
|
||||||
int event_rx = 0;
|
int event_rx = 0;
|
||||||
int event_tx = 0;
|
int event_tx = 0;
|
||||||
|
|
||||||
// Necessary for both interrup way and DMA way
|
// Necessary for both interrupt way and DMA way
|
||||||
if (serial_is_irq_en(obj, RxIrq)) {
|
if (serial_is_irq_en(obj, RxIrq)) {
|
||||||
event_rx = serial_rx_event_check(obj);
|
event_rx = serial_rx_event_check(obj);
|
||||||
if (event_rx) {
|
if (event_rx) {
|
||||||
|
@ -996,9 +1014,9 @@ static void serial_tx_enable_interrupt(serial_t *obj, uint32_t handler, uint8_t
|
||||||
MBED_ASSERT(modinit->modname == obj->serial.uart);
|
MBED_ASSERT(modinit->modname == obj->serial.uart);
|
||||||
|
|
||||||
// Necessary for both interrupt way and DMA way
|
// Necessary for both interrupt way and DMA way
|
||||||
((struct nu_uart_var *) modinit->var)->obj = obj;
|
struct nu_uart_var *var = (struct nu_uart_var *) modinit->var;
|
||||||
// With our own async vector, tx/rx handlers can be different.
|
// With our own async vector, tx/rx handlers can be different.
|
||||||
obj->serial.vec = ((struct nu_uart_var *) modinit->var)->vec_async;
|
obj->serial.vec = var->vec_async;
|
||||||
obj->serial.irq_handler_tx_async = (void (*)(void)) handler;
|
obj->serial.irq_handler_tx_async = (void (*)(void)) handler;
|
||||||
serial_irq_set(obj, TxIrq, enable);
|
serial_irq_set(obj, TxIrq, enable);
|
||||||
}
|
}
|
||||||
|
@ -1010,9 +1028,9 @@ static void serial_rx_enable_interrupt(serial_t *obj, uint32_t handler, uint8_t
|
||||||
MBED_ASSERT(modinit->modname == obj->serial.uart);
|
MBED_ASSERT(modinit->modname == obj->serial.uart);
|
||||||
|
|
||||||
// Necessary for both interrupt way and DMA way
|
// Necessary for both interrupt way and DMA way
|
||||||
((struct nu_uart_var *) modinit->var)->obj = obj;
|
struct nu_uart_var *var = (struct nu_uart_var *) modinit->var;
|
||||||
// With our own async vector, tx/rx handlers can be different.
|
// With our own async vector, tx/rx handlers can be different.
|
||||||
obj->serial.vec = ((struct nu_uart_var *) modinit->var)->vec_async;
|
obj->serial.vec = var->vec_async;
|
||||||
obj->serial.irq_handler_rx_async = (void (*) (void)) handler;
|
obj->serial.irq_handler_rx_async = (void (*) (void)) handler;
|
||||||
serial_irq_set(obj, RxIrq, enable);
|
serial_irq_set(obj, RxIrq, enable);
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,7 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct nu_uart_var {
|
struct nu_uart_var {
|
||||||
|
uint32_t ref_cnt; // Reference count of the H/W module
|
||||||
serial_t * obj;
|
serial_t * obj;
|
||||||
uint32_t fifo_size_tx;
|
uint32_t fifo_size_tx;
|
||||||
uint32_t fifo_size_rx;
|
uint32_t fifo_size_rx;
|
||||||
|
@ -84,6 +85,7 @@ static int serial_is_irq_en(serial_t *obj, SerialIrq irq);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static struct nu_uart_var uart0_var = {
|
static struct nu_uart_var uart0_var = {
|
||||||
|
.ref_cnt = 0,
|
||||||
.obj = NULL,
|
.obj = NULL,
|
||||||
.fifo_size_tx = 64,
|
.fifo_size_tx = 64,
|
||||||
.fifo_size_rx = 64,
|
.fifo_size_rx = 64,
|
||||||
|
@ -95,6 +97,7 @@ static struct nu_uart_var uart0_var = {
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
static struct nu_uart_var uart1_var = {
|
static struct nu_uart_var uart1_var = {
|
||||||
|
.ref_cnt = 0,
|
||||||
.obj = NULL,
|
.obj = NULL,
|
||||||
.fifo_size_tx = 16,
|
.fifo_size_tx = 16,
|
||||||
.fifo_size_rx = 16,
|
.fifo_size_rx = 16,
|
||||||
|
@ -106,6 +109,7 @@ static struct nu_uart_var uart1_var = {
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
static struct nu_uart_var uart2_var = {
|
static struct nu_uart_var uart2_var = {
|
||||||
|
.ref_cnt = 0,
|
||||||
.obj = NULL,
|
.obj = NULL,
|
||||||
.fifo_size_tx = 16,
|
.fifo_size_tx = 16,
|
||||||
.fifo_size_rx = 16,
|
.fifo_size_rx = 16,
|
||||||
|
@ -117,6 +121,7 @@ static struct nu_uart_var uart2_var = {
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
static struct nu_uart_var uart3_var = {
|
static struct nu_uart_var uart3_var = {
|
||||||
|
.ref_cnt = 0,
|
||||||
.obj = NULL,
|
.obj = NULL,
|
||||||
.fifo_size_tx = 16,
|
.fifo_size_tx = 16,
|
||||||
.fifo_size_rx = 16,
|
.fifo_size_rx = 16,
|
||||||
|
@ -128,6 +133,7 @@ static struct nu_uart_var uart3_var = {
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
static struct nu_uart_var uart4_var = {
|
static struct nu_uart_var uart4_var = {
|
||||||
|
.ref_cnt = 0,
|
||||||
.obj = NULL,
|
.obj = NULL,
|
||||||
.fifo_size_tx = 16,
|
.fifo_size_tx = 16,
|
||||||
.fifo_size_rx = 16,
|
.fifo_size_rx = 16,
|
||||||
|
@ -139,6 +145,7 @@ static struct nu_uart_var uart4_var = {
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
static struct nu_uart_var uart5_var = {
|
static struct nu_uart_var uart5_var = {
|
||||||
|
.ref_cnt = 0,
|
||||||
.obj = NULL,
|
.obj = NULL,
|
||||||
.fifo_size_tx = 16,
|
.fifo_size_tx = 16,
|
||||||
.fifo_size_rx = 16,
|
.fifo_size_rx = 16,
|
||||||
|
@ -170,7 +177,7 @@ extern void mbed_sdk_init(void);
|
||||||
|
|
||||||
void serial_init(serial_t *obj, PinName tx, PinName rx)
|
void serial_init(serial_t *obj, PinName tx, PinName rx)
|
||||||
{
|
{
|
||||||
// NOTE: serial_init() gets called from _sys_open() timing of which is before main()/mbed_sdk_init().
|
// NOTE: With armcc, serial_init() gets called from _sys_open() timing of which is before main()/mbed_sdk_init().
|
||||||
mbed_sdk_init();
|
mbed_sdk_init();
|
||||||
|
|
||||||
// Determine which UART_x the pins are used for
|
// Determine which UART_x the pins are used for
|
||||||
|
@ -184,6 +191,9 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
|
||||||
MBED_ASSERT(modinit != NULL);
|
MBED_ASSERT(modinit != NULL);
|
||||||
MBED_ASSERT(modinit->modname == obj->serial.uart);
|
MBED_ASSERT(modinit->modname == obj->serial.uart);
|
||||||
|
|
||||||
|
struct nu_uart_var *var = (struct nu_uart_var *) modinit->var;
|
||||||
|
|
||||||
|
if (! var->ref_cnt) {
|
||||||
// Reset this module
|
// Reset this module
|
||||||
SYS_ResetModule(modinit->rsetidx);
|
SYS_ResetModule(modinit->rsetidx);
|
||||||
|
|
||||||
|
@ -194,22 +204,18 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
|
||||||
|
|
||||||
pinmap_pinout(tx, PinMap_UART_TX);
|
pinmap_pinout(tx, PinMap_UART_TX);
|
||||||
pinmap_pinout(rx, PinMap_UART_RX);
|
pinmap_pinout(rx, PinMap_UART_RX);
|
||||||
// FIXME: Why PullUp?
|
|
||||||
//if (tx != NC) {
|
|
||||||
// pin_mode(tx, PullUp);
|
|
||||||
//}
|
|
||||||
//if (rx != NC) {
|
|
||||||
// pin_mode(rx, PullUp);
|
|
||||||
//}
|
|
||||||
obj->serial.pin_tx = tx;
|
obj->serial.pin_tx = tx;
|
||||||
obj->serial.pin_rx = rx;
|
obj->serial.pin_rx = rx;
|
||||||
|
}
|
||||||
|
var->ref_cnt ++;
|
||||||
|
|
||||||
// Configure the UART module and set its baudrate
|
// Configure the UART module and set its baudrate
|
||||||
serial_baud(obj, 9600);
|
serial_baud(obj, 9600);
|
||||||
// Configure data bits, parity, and stop bits
|
// Configure data bits, parity, and stop bits
|
||||||
serial_format(obj, 8, ParityNone, 1);
|
serial_format(obj, 8, ParityNone, 1);
|
||||||
|
|
||||||
obj->serial.vec = ((struct nu_uart_var *) modinit->var)->vec;
|
obj->serial.vec = var->vec;
|
||||||
|
|
||||||
#if DEVICE_SERIAL_ASYNCH
|
#if DEVICE_SERIAL_ASYNCH
|
||||||
obj->serial.dma_usage_tx = DMA_USAGE_NEVER;
|
obj->serial.dma_usage_tx = DMA_USAGE_NEVER;
|
||||||
|
@ -220,19 +226,28 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// For stdio management
|
// For stdio management
|
||||||
if (obj == &stdio_uart) {
|
if (obj->serial.uart == STDIO_UART) {
|
||||||
stdio_uart_inited = 1;
|
stdio_uart_inited = 1;
|
||||||
/* NOTE: Not required anymore because stdio_uart will be manually initialized in mbed-drivers/source/retarget.cpp from mbed beta */
|
memcpy(&stdio_uart, obj, sizeof(serial_t));
|
||||||
//memcpy(&stdio_uart, obj, sizeof(serial_t));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (var->ref_cnt) {
|
||||||
// Mark this module to be inited.
|
// Mark this module to be inited.
|
||||||
int i = modinit - uart_modinit_tab;
|
int i = modinit - uart_modinit_tab;
|
||||||
uart_modinit_mask |= 1 << i;
|
uart_modinit_mask |= 1 << i;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void serial_free(serial_t *obj)
|
void serial_free(serial_t *obj)
|
||||||
{
|
{
|
||||||
|
const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab);
|
||||||
|
MBED_ASSERT(modinit != NULL);
|
||||||
|
MBED_ASSERT(modinit->modname == obj->serial.uart);
|
||||||
|
|
||||||
|
struct nu_uart_var *var = (struct nu_uart_var *) modinit->var;
|
||||||
|
|
||||||
|
var->ref_cnt --;
|
||||||
|
if (! var->ref_cnt) {
|
||||||
#if DEVICE_SERIAL_ASYNCH
|
#if DEVICE_SERIAL_ASYNCH
|
||||||
if (obj->serial.dma_chn_id_tx != DMA_ERROR_OUT_OF_CHANNELS) {
|
if (obj->serial.dma_chn_id_tx != DMA_ERROR_OUT_OF_CHANNELS) {
|
||||||
dma_channel_free(obj->serial.dma_chn_id_tx);
|
dma_channel_free(obj->serial.dma_chn_id_tx);
|
||||||
|
@ -246,25 +261,26 @@ void serial_free(serial_t *obj)
|
||||||
|
|
||||||
UART_Close((UART_T *) NU_MODBASE(obj->serial.uart));
|
UART_Close((UART_T *) NU_MODBASE(obj->serial.uart));
|
||||||
|
|
||||||
const struct nu_modinit_s *modinit = get_modinit(obj->serial.uart, uart_modinit_tab);
|
|
||||||
MBED_ASSERT(modinit != NULL);
|
|
||||||
MBED_ASSERT(modinit->modname == obj->serial.uart);
|
|
||||||
|
|
||||||
UART_DISABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), (UART_INTEN_RDAIEN_Msk | UART_INTEN_THREIEN_Msk | UART_INTEN_RXTOIEN_Msk));
|
UART_DISABLE_INT(((UART_T *) NU_MODBASE(obj->serial.uart)), (UART_INTEN_RDAIEN_Msk | UART_INTEN_THREIEN_Msk | UART_INTEN_RXTOIEN_Msk));
|
||||||
NVIC_DisableIRQ(modinit->irq_n);
|
NVIC_DisableIRQ(modinit->irq_n);
|
||||||
|
|
||||||
// Disable IP clock
|
// Disable IP clock
|
||||||
CLK_DisableModuleClock(modinit->clkidx);
|
CLK_DisableModuleClock(modinit->clkidx);
|
||||||
|
}
|
||||||
|
|
||||||
((struct nu_uart_var *) modinit->var)->obj = NULL;
|
if (var->obj == obj) {
|
||||||
|
var->obj = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
if (obj == &stdio_uart) {
|
if (obj->serial.uart == STDIO_UART) {
|
||||||
stdio_uart_inited = 0;
|
stdio_uart_inited = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (! var->ref_cnt) {
|
||||||
// Mark this module to be deinited.
|
// Mark this module to be deinited.
|
||||||
int i = modinit - uart_modinit_tab;
|
int i = modinit - uart_modinit_tab;
|
||||||
uart_modinit_mask &= ~(1 << i);
|
uart_modinit_mask &= ~(1 << i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void serial_baud(serial_t *obj, int baudrate) {
|
void serial_baud(serial_t *obj, int baudrate) {
|
||||||
|
@ -345,7 +361,6 @@ void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id)
|
||||||
MBED_ASSERT(modinit != NULL);
|
MBED_ASSERT(modinit != NULL);
|
||||||
MBED_ASSERT(modinit->modname == obj->serial.uart);
|
MBED_ASSERT(modinit->modname == obj->serial.uart);
|
||||||
|
|
||||||
((struct nu_uart_var *) modinit->var)->obj = obj;
|
|
||||||
obj->serial.irq_handler = (uint32_t) handler;
|
obj->serial.irq_handler = (uint32_t) handler;
|
||||||
obj->serial.irq_id = id;
|
obj->serial.irq_id = id;
|
||||||
|
|
||||||
|
@ -363,6 +378,11 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
|
||||||
NVIC_SetVector(modinit->irq_n, (uint32_t) obj->serial.vec);
|
NVIC_SetVector(modinit->irq_n, (uint32_t) obj->serial.vec);
|
||||||
NVIC_EnableIRQ(modinit->irq_n);
|
NVIC_EnableIRQ(modinit->irq_n);
|
||||||
|
|
||||||
|
struct nu_uart_var *var = (struct nu_uart_var *) modinit->var;
|
||||||
|
// Multiple serial S/W objects for single UART H/W module possibly.
|
||||||
|
// Bind serial S/W object to UART H/W module as interrupt is enabled.
|
||||||
|
var->obj = obj;
|
||||||
|
|
||||||
switch (irq) {
|
switch (irq) {
|
||||||
// NOTE: Setting inten_msk first to avoid race condition
|
// NOTE: Setting inten_msk first to avoid race condition
|
||||||
case RxIrq:
|
case RxIrq:
|
||||||
|
@ -668,7 +688,7 @@ int serial_irq_handler_asynch(serial_t *obj)
|
||||||
int event_rx = 0;
|
int event_rx = 0;
|
||||||
int event_tx = 0;
|
int event_tx = 0;
|
||||||
|
|
||||||
// Necessary for both interrup way and DMA way
|
// Necessary for both interrupt way and DMA way
|
||||||
if (serial_is_irq_en(obj, RxIrq)) {
|
if (serial_is_irq_en(obj, RxIrq)) {
|
||||||
event_rx = serial_rx_event_check(obj);
|
event_rx = serial_rx_event_check(obj);
|
||||||
if (event_rx) {
|
if (event_rx) {
|
||||||
|
@ -1040,9 +1060,9 @@ static void serial_tx_enable_interrupt(serial_t *obj, uint32_t handler, uint8_t
|
||||||
MBED_ASSERT(modinit->modname == obj->serial.uart);
|
MBED_ASSERT(modinit->modname == obj->serial.uart);
|
||||||
|
|
||||||
// Necessary for both interrupt way and DMA way
|
// Necessary for both interrupt way and DMA way
|
||||||
((struct nu_uart_var *) modinit->var)->obj = obj;
|
struct nu_uart_var *var = (struct nu_uart_var *) modinit->var;
|
||||||
// With our own async vector, tx/rx handlers can be different.
|
// With our own async vector, tx/rx handlers can be different.
|
||||||
obj->serial.vec = ((struct nu_uart_var *) modinit->var)->vec_async;
|
obj->serial.vec = var->vec_async;
|
||||||
obj->serial.irq_handler_tx_async = (void (*)(void)) handler;
|
obj->serial.irq_handler_tx_async = (void (*)(void)) handler;
|
||||||
serial_irq_set(obj, TxIrq, enable);
|
serial_irq_set(obj, TxIrq, enable);
|
||||||
}
|
}
|
||||||
|
@ -1054,9 +1074,9 @@ static void serial_rx_enable_interrupt(serial_t *obj, uint32_t handler, uint8_t
|
||||||
MBED_ASSERT(modinit->modname == obj->serial.uart);
|
MBED_ASSERT(modinit->modname == obj->serial.uart);
|
||||||
|
|
||||||
// Necessary for both interrupt way and DMA way
|
// Necessary for both interrupt way and DMA way
|
||||||
((struct nu_uart_var *) modinit->var)->obj = obj;
|
struct nu_uart_var *var = (struct nu_uart_var *) modinit->var;
|
||||||
// With our own async vector, tx/rx handlers can be different.
|
// With our own async vector, tx/rx handlers can be different.
|
||||||
obj->serial.vec = ((struct nu_uart_var *) modinit->var)->vec_async;
|
obj->serial.vec = var->vec_async;
|
||||||
obj->serial.irq_handler_rx_async = (void (*) (void)) handler;
|
obj->serial.irq_handler_rx_async = (void (*) (void)) handler;
|
||||||
serial_irq_set(obj, RxIrq, enable);
|
serial_irq_set(obj, RxIrq, enable);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue