123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622 |
- /*
- * Copyright (C) 2021, Huada Semiconductor Co., Ltd.
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2021-08-19 pjq first version
- */
- /*******************************************************************************
- * Include files
- ******************************************************************************/
- #include <rtdevice.h>
- #include <rthw.h>
- #include "gpio.h"
- #include "uart.h"
- #include "drv_usart.h"
- #ifdef RT_USING_SERIAL
- #if !defined(BSP_USING_UART0) && !defined(BSP_USING_UART1)
- #error "Please define at least one BSP_USING_UARTx"
- /* UART instance can be selected at menuconfig -> Hardware Drivers Config -> On-chip Peripheral Drivers -> Enable UART */
- #endif
- /*******************************************************************************
- * Local type definitions ('typedef')
- ******************************************************************************/
- /* HC32 config Rx timeout */
- struct hc32_uart_rxto
- {
- rt_uint32_t channel;
- rt_size_t timeout_bits;
- };
- /* HC32 UART index */
- struct uart_index
- {
- rt_uint8_t index;
- rt_uint8_t idx;
- };
- /* HC32 UART irq handler */
- struct uart_irq_handler
- {
- void (*tx_irq_handler)(void);
- void (*rxerr_irq_handler)(void);
- void (*rx_irq_handler)(void);
- void (*cts_irq_handler)(void);
- void (*pei_irq_handler)(void);
- };
- /*******************************************************************************
- * Local pre-processor symbols/macros ('#define')
- ******************************************************************************/
- /*******************************************************************************
- * Global variable definitions (declared in header file with 'extern')
- ******************************************************************************/
- /*******************************************************************************
- * Local function prototypes ('static')
- ******************************************************************************/
- #ifdef RT_SERIAL_USING_DMA
- static void hc32_dma_config(struct rt_serial_device *serial, rt_ubase_t flag);
- #endif
- /*******************************************************************************
- * Local variable definitions ('static')
- ******************************************************************************/
- enum
- {
- #ifdef BSP_USING_UART0
- UART0_INDEX,
- #endif
- #ifdef BSP_USING_UART1
- UART1_INDEX,
- #endif
- UART_INDEX_MAX,
- };
- static const struct uart_index uart_map[] =
- {
- #ifdef BSP_USING_UART0
- {UART0_INDEX, UARTCH0},
- #endif
- #ifdef BSP_USING_UART1
- {UART1_INDEX, UARTCH1},
- #endif
- };
- static struct hc32_uart_config uart_config[] =
- {
- #ifdef BSP_USING_UART0
- { \
- .name = "uart0", \
- .idx = UARTCH0, \
- },
- #endif
- #ifdef BSP_USING_UART1
- { \
- .name = "uart1", \
- .idx = UARTCH1, \
- }
- #endif
- };
- #ifdef BSP_USING_UART0
- static int uart0_rx_flag;
- #endif
- #ifdef BSP_USING_UART1
- static int uart1_rx_flag;
- #endif
- static struct hc32_uart uart_obj[sizeof(uart_config) / sizeof(uart_config[0])] = {0};
- static const struct uart_irq_handler uart_irq_handlers[sizeof(uart_obj) / sizeof(uart_obj[0])];
- /*******************************************************************************
- * Function implementation - global ('extern') and local ('static')
- ******************************************************************************/
- //static uint32_t hc32_get_uart_index(M0P_UART_TypeDef *Instance)
- static uint32_t hc32_get_uart_index(uint8_t idx)
- {
- uint32_t index = UART_INDEX_MAX;
- for (uint8_t i = 0U; i < ARRAY_SZ(uart_map); i++)
- {
- if (uart_map[i].idx == idx)
- {
- index = uart_map[i].index;
- RT_ASSERT(index < UART_INDEX_MAX)
- break;
- }
- }
- return index;
- }
- #if defined(BSP_USING_UART0)
- void uart0_gpioinit(void)
- {
- stc_gpio_config_t stcGpioCfg;
- DDL_ZERO_STRUCT(stcGpioCfg);
- stcGpioCfg.enDir = GpioDirOut;
- Gpio_Init(GpioPortA,GpioPin9,&stcGpioCfg);
- Gpio_SetAfMode(GpioPortA,GpioPin9,GpioAf1);//TX
- stcGpioCfg.enDir = GpioDirIn;
- Gpio_Init(GpioPortA,GpioPin10,&stcGpioCfg);
- Gpio_SetAfMode(GpioPortA,GpioPin10,GpioAf1);//RX
- }
- #endif
- #if defined(BSP_USING_UART1)
- void uart1_gpioinit(void)
- {
- stc_gpio_config_t stcGpioCfg;
- DDL_ZERO_STRUCT(stcGpioCfg);
- stcGpioCfg.enDir = GpioDirOut;
- Gpio_Init(GpioPortA,GpioPin2,&stcGpioCfg);
- Gpio_SetAfMode(GpioPortA,GpioPin2,GpioAf1);//TX
- stcGpioCfg.enDir = GpioDirIn;
- Gpio_Init(GpioPortA,GpioPin3,&stcGpioCfg);
- Gpio_SetAfMode(GpioPortA,GpioPin3,GpioAf1);//RX
- }
- #endif
- static rt_err_t hc32_configure(struct rt_serial_device *serial,
- struct serial_configure *cfg)
- {
- struct hc32_uart *uart;
- uint16_t u16Scnt = 0;
- stc_uart_config_t stcConfig;
- stc_uart_irq_cb_t stcUartIrqCb;
- stc_uart_multimode_t stcMulti;
- stc_uart_baud_t stcBaud;
- uint8_t index;
- en_uart_mmdorck_t enTb8;
- DDL_ZERO_STRUCT(stcConfig);
- DDL_ZERO_STRUCT(stcUartIrqCb);
- DDL_ZERO_STRUCT(stcMulti);
- DDL_ZERO_STRUCT(stcBaud);
- RT_ASSERT(RT_NULL != cfg);
- RT_ASSERT(RT_NULL != serial);
- uart = rt_container_of(serial, struct hc32_uart, serial);
- #if defined(BSP_USING_UART0)
- if (uart->config->idx == UARTCH0)
- {
- uart0_gpioinit();
- }
- #endif
- #if defined(BSP_USING_UART1)
- if (uart->config->idx == UARTCH1)
- {
- uart1_gpioinit();
- }
- #endif
- /* Configure USART initialization structure */
- index = hc32_get_uart_index(uart->config->idx);
- stcUartIrqCb.pfnRxIrqCb = uart_irq_handlers[index].rx_irq_handler;
- stcUartIrqCb.pfnTxIrqCb = uart_irq_handlers[index].tx_irq_handler;
- stcUartIrqCb.pfnRxFEIrqCb = uart_irq_handlers[index].rxerr_irq_handler;
- stcUartIrqCb.pfnPEIrqCb = uart_irq_handlers[index].pei_irq_handler;
- stcUartIrqCb.pfnCtsIrqCb = uart_irq_handlers[index].cts_irq_handler;
- stcConfig.pstcIrqCb = &stcUartIrqCb;
- stcConfig.bTouchNvic = TRUE;
- stcConfig.enRunMode = UartMode3;
- stcMulti.enMulti_mode = UartNormal;
- if(BIT_ORDER_LSB == cfg->bit_order)
- {
- }
- else
- {
- }
- switch(cfg->stop_bits)
- {
- case STOP_BITS_1:
- stcConfig.enStopBit = Uart1bit;
- break;
- case STOP_BITS_2:
- stcConfig.enStopBit = Uart2bit;
- break;
- default:
- break;
- }
- switch(cfg->parity)
- {
- case PARITY_NONE:
- enTb8 = UartDataOrAddr;
- break;
- case PARITY_EVEN:
- enTb8 = UartEven;
- break;
- case PARITY_ODD:
- enTb8 = UartOdd;
- break;
- default:
- enTb8 = UartDataOrAddr;
- break;
- }
- switch(cfg->data_bits)
- {
- case DATA_BITS_8:
- break;
- default:
- return -RT_ERROR;
- }
- Uart_SetMMDOrCk(uart->config->idx, enTb8);
- stcConfig.pstcMultiMode = &stcMulti;
- Uart_Init(uart->config->idx, &stcConfig);
- Uart_SetClkDiv(uart->config->idx, Uart8Or16Div);
- stcBaud.u32Pclk = Sysctrl_GetPClkFreq();
- stcBaud.enRunMode = UartMode3;
- stcBaud.u32Baud = cfg->baud_rate;
- u16Scnt = Uart_CalScnt(uart->config->idx, &stcBaud);
- Uart_SetBaud(uart->config->idx, u16Scnt);
-
- Uart_ClrStatus(uart->config->idx, UartTC);
- Uart_ClrStatus(uart->config->idx, UartRC);
- Uart_DisableIrq(uart->config->idx, UartTxIrq);
- Uart_DisableIrq(uart->config->idx, UartRxIrq);
- Uart_EnableFunc(uart->config->idx, UartRx);
- return RT_EOK;
- }
- static rt_err_t hc32_control(struct rt_serial_device *serial, int cmd, void *arg)
- {
- struct hc32_uart *uart;
- RT_ASSERT(serial != RT_NULL);
- uart = rt_container_of(serial, struct hc32_uart, serial);
- switch (cmd)
- {
- /* disable interrupt */
- case RT_DEVICE_CTRL_CLR_INT:
- Uart_DisableIrq(uart->config->idx, UartRxIrq);
- break;
- /* enable interrupt */
- case RT_DEVICE_CTRL_SET_INT:
- /* enable rx irq */
- Uart_ClrStatus(uart->config->idx, UartRC);
- Uart_EnableIrq(uart->config->idx, UartRxIrq);
- break;
- case RT_DEVICE_CTRL_CLOSE:
- break;
- }
- return RT_EOK;
- }
- static int hc32_putc(struct rt_serial_device *serial, char c)
- {
- struct hc32_uart *uart;
- RT_ASSERT(RT_NULL != serial);
- uart = rt_container_of(serial, struct hc32_uart, serial);
-
- if(serial->parent.open_flag & RT_DEVICE_FLAG_INT_TX)
- {
- if (Uart_GetStatus(uart->config->idx, UartTC) == FALSE)
- {
- return -1;
- }
- }
- Uart_SendData(uart->config->idx, c);
- return 1;
- }
- static int hc32_getc(struct rt_serial_device *serial)
- {
- int ch= -1;
- struct hc32_uart *uart;
- RT_ASSERT(RT_NULL != serial);
- uart = rt_container_of(serial, struct hc32_uart, serial);
- #if defined(BSP_USING_UART0)
- if (uart->config->idx == UARTCH0)
- {
- if (serial->parent.open_flag & RT_DEVICE_FLAG_INT_RX)
- {
- if (uart0_rx_flag)
- {
- ch = Uart_ReceiveData(uart->config->idx);
- uart0_rx_flag = 0;
- }
- }
- else
- {
- if(Uart_GetStatus(uart->config->idx, UartRC))
- {
- Uart_ClrStatus(uart->config->idx, UartRC);
- ch = Uart_ReceiveData(uart->config->idx);
- }
- }
- }
- #endif
- #if defined(BSP_USING_UART1)
- if (uart->config->idx == UARTCH1)
- {
- if (serial->parent.open_flag & RT_DEVICE_FLAG_INT_RX)
- {
- if (uart1_rx_flag)
- {
- ch = Uart_ReceiveData(uart->config->idx);
- uart1_rx_flag = 0;
- }
- }
- else
- {
- if(Uart_GetStatus(uart->config->idx, UartRC))
- {
- Uart_ClrStatus(uart->config->idx, UartRC);
- ch = Uart_ReceiveData(uart->config->idx);
- }
- }
- }
- #endif
- return ch;
- }
- static rt_size_t hc32_dma_transmit(struct rt_serial_device *serial,
- rt_uint8_t *buf,
- rt_size_t size,
- int direction)
- {
- return 0;
- }
- static void hc32_uart_rx_irq_handler(struct hc32_uart *uart)
- {
- RT_ASSERT(RT_NULL != uart);
- rt_hw_serial_isr(&uart->serial, RT_SERIAL_EVENT_RX_IND);
- }
- static void hc32_uart_tx_irq_handler(struct hc32_uart *uart)
- {
- RT_ASSERT(RT_NULL != uart);
- if (uart->serial.parent.open_flag & RT_DEVICE_FLAG_INT_TX)
- {
- rt_hw_serial_isr(&uart->serial, RT_SERIAL_EVENT_TX_DONE);
- }
- }
- static void hc32_uart_rxerr_irq_handler(struct hc32_uart *uart)
- {
- RT_ASSERT(RT_NULL != uart);
- }
- static void hc32_uart_cts_irq_handler(struct hc32_uart *uart)
- {
- RT_ASSERT(RT_NULL != uart);
- }
- static void hc32_uart_pei_irq_handler(struct hc32_uart *uart)
- {
- RT_ASSERT(RT_NULL != uart);
- }
- #ifdef RT_SERIAL_USING_DMA
- static void hc32_dma_config(struct rt_serial_device *serial, rt_ubase_t flag)
- {
- }
- #endif
- #if defined(BSP_USING_UART0)
- static void hc32_uart0_rx_irq_handler(void)
- {
- /* enter interrupt */
- rt_interrupt_enter();
- uart0_rx_flag = 1;
- hc32_uart_rx_irq_handler(&uart_obj[UART0_INDEX]);
- /* leave interrupt */
- rt_interrupt_leave();
- }
- static void hc32_uart0_tx_irq_handler(void)
- {
- /* enter interrupt */
- rt_interrupt_enter();
- hc32_uart_tx_irq_handler(&uart_obj[UART0_INDEX]);
- /* leave interrupt */
- rt_interrupt_leave();
- }
- static void hc32_uart0_rxerr_irq_handler(void)
- {
- /* enter interrupt */
- rt_interrupt_enter();
- hc32_uart_rxerr_irq_handler(&uart_obj[UART0_INDEX]);
- /* leave interrupt */
- rt_interrupt_leave();
- }
- static void hc32_uart0_cts_irq_handler(void)
- {
- /* enter interrupt */
- rt_interrupt_enter();
- hc32_uart_cts_irq_handler(&uart_obj[UART0_INDEX]);
- /* leave interrupt */
- rt_interrupt_leave();
- }
- static void hc32_uart0_pei_irq_handler(void)
- {
- /* enter interrupt */
- rt_interrupt_enter();
- hc32_uart_pei_irq_handler(&uart_obj[UART0_INDEX]);
- /* leave interrupt */
- rt_interrupt_leave();
- }
- #endif /* BSP_USING_UART0 */
- #if defined(BSP_USING_UART1)
- static void hc32_uart1_tx_irq_handler(void)
- {
- /* enter interrupt */
- rt_interrupt_enter();
- hc32_uart_tx_irq_handler(&uart_obj[UART1_INDEX]);
- /* leave interrupt */
- rt_interrupt_leave();
- }
- static void hc32_uart1_rxerr_irq_handler(void)
- {
- /* enter interrupt */
- rt_interrupt_enter();
- hc32_uart_rxerr_irq_handler(&uart_obj[UART1_INDEX]);
- /* leave interrupt */
- rt_interrupt_leave();
- }
- static void hc32_uart1_rx_irq_handler(void)
- {
- /* enter interrupt */
- rt_interrupt_enter();
- uart1_rx_flag = 1;
- hc32_uart_rx_irq_handler(&uart_obj[UART1_INDEX]);
- /* leave interrupt */
- rt_interrupt_leave();
- }
- static void hc32_uart1_cts_irq_handler(void)
- {
- /* enter interrupt */
- rt_interrupt_enter();
- hc32_uart_cts_irq_handler(&uart_obj[UART1_INDEX]);
- /* leave interrupt */
- rt_interrupt_leave();
- }
- static void hc32_uart1_pei_irq_handler(void)
- {
- /* enter interrupt */
- rt_interrupt_enter();
- hc32_uart_pei_irq_handler(&uart_obj[UART1_INDEX]);
- /* leave interrupt */
- rt_interrupt_leave();
- }
- #endif /* BSP_USING_UART1 */
- static const struct uart_irq_handler uart_irq_handlers[] =
- {
- #ifdef BSP_USING_UART0
- { hc32_uart0_tx_irq_handler, hc32_uart0_rxerr_irq_handler, hc32_uart0_rx_irq_handler,
- hc32_uart0_cts_irq_handler, hc32_uart0_pei_irq_handler
- },
- #endif
- #ifdef BSP_USING_UART1
- { hc32_uart1_tx_irq_handler, hc32_uart1_rxerr_irq_handler, hc32_uart1_rx_irq_handler,
- hc32_uart1_cts_irq_handler, hc32_uart1_pei_irq_handler
- },
- #endif
- };
- static void hc32_uart_get_dma_config(void)
- {
- }
- static const struct rt_uart_ops hc32_uart_ops =
- {
- .configure = hc32_configure,
- .control = hc32_control,
- .putc = hc32_putc,
- .getc = hc32_getc,
- .dma_transmit = hc32_dma_transmit
- };
- int hc32_hw_uart_init(void)
- {
- rt_err_t result = RT_EOK;
- rt_size_t obj_num = sizeof(uart_obj) / sizeof(struct hc32_uart);
- struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
- hc32_uart_get_dma_config();
- Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio,TRUE);
- #ifdef BSP_USING_UART0
- Sysctrl_SetPeripheralGate(SysctrlPeripheralUart0,TRUE);
- #endif
- #ifdef BSP_USING_UART1
- Sysctrl_SetPeripheralGate(SysctrlPeripheralUart1,TRUE);
- #endif
- for (int i = 0; i < obj_num; i++)
- {
- /* init UART object */
- uart_obj[i].serial.ops = &hc32_uart_ops;
- uart_obj[i].serial.config = config;
- uart_obj[i].config = &uart_config[i];
- /* register UART device */
- result = rt_hw_serial_register(&uart_obj[i].serial,
- uart_obj[i].config->name,
- (RT_DEVICE_FLAG_RDWR |
- RT_DEVICE_FLAG_INT_RX |
- RT_DEVICE_FLAG_INT_TX |
- uart_obj[i].uart_dma_flag),
- &uart_obj[i]);
- RT_ASSERT(result == RT_EOK);
- }
- return result;
- }
- INIT_BOARD_EXPORT(hc32_hw_uart_init);
- #endif /* RT_USING_SERIAL */
- /*******************************************************************************
- * EOF (not truncated)
- ******************************************************************************/
|