|
46 | 46 | #define NS800_UART_DEFAULT_TX_TIMEOUT BSP_NS800_UART_TX_TIMEOUT |
47 | 47 | #endif |
48 | 48 |
|
| 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 | + |
49 | 72 | enum |
50 | 73 | { |
51 | 74 | #ifdef BSP_USING_UART1 |
@@ -377,31 +400,48 @@ static rt_err_t ns800_control(struct rt_serial_device *serial, int cmd, void *ar |
377 | 400 | return RT_EOK; |
378 | 401 | } |
379 | 402 |
|
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) |
381 | 404 | { |
382 | | - struct ns800_uart *uart; |
383 | | - rt_uint32_t block_timeout; |
| 405 | + rt_tick_t start_tick; |
| 406 | + rt_tick_t timeout_tick; |
384 | 407 |
|
385 | | - RT_ASSERT(serial != RT_NULL); |
| 408 | + RT_ASSERT(uart != RT_NULL); |
386 | 409 |
|
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(); |
389 | 417 |
|
390 | 418 | while (!UART_isSpaceAvailable(uart->handle.Instance)) |
391 | 419 | { |
392 | | - if (block_timeout-- == 0U) |
| 420 | + if ((rt_tick_get() - start_tick) > timeout_tick) |
393 | 421 | { |
394 | | - return -1; |
| 422 | + return -RT_ETIMEOUT; |
395 | 423 | } |
396 | 424 | } |
397 | 425 |
|
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); |
399 | 435 |
|
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) |
401 | 439 | { |
| 440 | + return -1; |
402 | 441 | } |
403 | 442 |
|
404 | | - return (block_timeout != 0U) ? 1 : -1; |
| 443 | + UART_writeChar(uart->handle.Instance, (rt_uint8_t)c); |
| 444 | + return 1; |
405 | 445 | } |
406 | 446 |
|
407 | 447 | static int ns800_getc(struct rt_serial_device *serial) |
@@ -489,6 +529,11 @@ static void ns800_uart_fill_default_config(struct serial_configure *config, |
489 | 529 |
|
490 | 530 | *config = (struct serial_configure)RT_SERIAL_CONFIG_DEFAULT; |
491 | 531 |
|
| 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 | + |
492 | 537 | #ifdef RT_USING_SERIAL_V2 |
493 | 538 | config->rx_bufsz = hw->rx_bufsz; |
494 | 539 | config->tx_bufsz = hw->tx_bufsz; |
|
0 commit comments