Skip to content

Commit 38ac071

Browse files
[BSP] [NS800RT7P65]Feature/optimize ns800 uart Drive (RT-Thread#11423)
[ns800] Option Uart Kconfig, and Resolve the issue of data loss in serial communication at low baud rates.
1 parent 196a6f4 commit 38ac071

2 files changed

Lines changed: 82 additions & 11 deletions

File tree

  • bsp/novosns/ns800

bsp/novosns/ns800/libraries/HAL_Drivers/drivers/drv_uart.c

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,29 @@
4646
#define NS800_UART_DEFAULT_TX_TIMEOUT BSP_NS800_UART_TX_TIMEOUT
4747
#endif
4848

49+
/* Default UART configuration from Kconfig */
50+
#ifndef BSP_UART_DEFAULT_BAUDRATE
51+
#define BSP_UART_DEFAULT_BAUDRATE 115200
52+
#endif
53+
54+
/* Data bits mapping */
55+
#if defined(BSP_UART_DATABITS_7)
56+
#define NS800_UART_DEFAULT_DATA_BITS DATA_BITS_7
57+
#elif defined(BSP_UART_DATABITS_8)
58+
#define NS800_UART_DEFAULT_DATA_BITS DATA_BITS_8
59+
#elif defined(BSP_UART_DATABITS_9)
60+
#define NS800_UART_DEFAULT_DATA_BITS DATA_BITS_9
61+
#else
62+
#define NS800_UART_DEFAULT_DATA_BITS DATA_BITS_8
63+
#endif
64+
65+
/* Stop bits mapping */
66+
#ifdef BSP_UART_STOPBITS_2
67+
#define NS800_UART_DEFAULT_STOP_BITS STOP_BITS_2
68+
#else
69+
#define NS800_UART_DEFAULT_STOP_BITS STOP_BITS_1
70+
#endif
71+
4972
enum
5073
{
5174
#ifdef BSP_USING_UART1
@@ -377,31 +400,48 @@ static rt_err_t ns800_control(struct rt_serial_device *serial, int cmd, void *ar
377400
return RT_EOK;
378401
}
379402

380-
static int ns800_putc(struct rt_serial_device *serial, char c)
403+
static rt_err_t ns800_wait_tx_space(struct ns800_uart *uart, rt_uint32_t timeout_ms)
381404
{
382-
struct ns800_uart *uart;
383-
rt_uint32_t block_timeout;
405+
rt_tick_t start_tick;
406+
rt_tick_t timeout_tick;
384407

385-
RT_ASSERT(serial != RT_NULL);
408+
RT_ASSERT(uart != RT_NULL);
386409

387-
uart = rt_container_of(serial, struct ns800_uart, serial);
388-
block_timeout = uart->tx_block_timeout;
410+
if (UART_isSpaceAvailable(uart->handle.Instance))
411+
{
412+
return RT_EOK;
413+
}
414+
415+
timeout_tick = rt_tick_from_millisecond(timeout_ms);
416+
start_tick = rt_tick_get();
389417

390418
while (!UART_isSpaceAvailable(uart->handle.Instance))
391419
{
392-
if (block_timeout-- == 0U)
420+
if ((rt_tick_get() - start_tick) > timeout_tick)
393421
{
394-
return -1;
422+
return -RT_ETIMEOUT;
395423
}
396424
}
397425

398-
UART_writeChar(uart->handle.Instance, (rt_uint8_t)c);
426+
return RT_EOK;
427+
}
428+
429+
static int ns800_putc(struct rt_serial_device *serial, char c)
430+
{
431+
struct ns800_uart *uart;
432+
rt_err_t result;
433+
434+
RT_ASSERT(serial != RT_NULL);
399435

400-
while ((uart->handle.Instance->STAT.BIT.TC == false) && (--block_timeout != 0U))
436+
uart = rt_container_of(serial, struct ns800_uart, serial);
437+
result = ns800_wait_tx_space(uart, uart->tx_block_timeout);
438+
if (result != RT_EOK)
401439
{
440+
return -1;
402441
}
403442

404-
return (block_timeout != 0U) ? 1 : -1;
443+
UART_writeChar(uart->handle.Instance, (rt_uint8_t)c);
444+
return 1;
405445
}
406446

407447
static int ns800_getc(struct rt_serial_device *serial)
@@ -489,6 +529,11 @@ static void ns800_uart_fill_default_config(struct serial_configure *config,
489529

490530
*config = (struct serial_configure)RT_SERIAL_CONFIG_DEFAULT;
491531

532+
/* Override with Kconfig settings */
533+
config->baud_rate = BSP_UART_DEFAULT_BAUDRATE;
534+
config->data_bits = NS800_UART_DEFAULT_DATA_BITS;
535+
config->stop_bits = NS800_UART_DEFAULT_STOP_BITS;
536+
492537
#ifdef RT_USING_SERIAL_V2
493538
config->rx_bufsz = hw->rx_bufsz;
494539
config->tx_bufsz = hw->tx_bufsz;

bsp/novosns/ns800/ns800rt7p65-nssinepad/board/Kconfig

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,32 @@ menu "On-chip Peripheral Drivers"
2020
default 6000
2121
depends on !RT_USING_SERIAL_V2
2222

23+
config BSP_UART_DEFAULT_BAUDRATE
24+
int "UART default baudrate"
25+
default 115200
26+
help
27+
Default baudrate for all UART ports.
28+
29+
choice BSP_UART_DEFAULT_DATABITS
30+
prompt "UART default data bits"
31+
default BSP_UART_DATABITS_8
32+
config BSP_UART_DATABITS_7
33+
bool "7 bits"
34+
config BSP_UART_DATABITS_8
35+
bool "8 bits"
36+
config BSP_UART_DATABITS_9
37+
bool "9 bits"
38+
endchoice
39+
40+
choice BSP_UART_DEFAULT_STOPBITS
41+
prompt "UART default stop bits"
42+
default BSP_UART_STOPBITS_1
43+
config BSP_UART_STOPBITS_1
44+
bool "1 bit"
45+
config BSP_UART_STOPBITS_2
46+
bool "2 bits"
47+
endchoice
48+
2349
menuconfig BSP_USING_UART1
2450
bool "Enable UART1"
2551
default y

0 commit comments

Comments
 (0)