ES32VF2264集成了5个UART接口(涵盖UART0至UART4),其中UART0与UART1为增强的EUART版本,而UART2、UART3及UART4则为标准的CUART配置。通用异步收发器(UART)以其高度的灵活性,让MCU能够借助工业标准的NRZ格式,与外部设备轻松实现全双工异步串行通信。借助小数波特率发生器,UART提供了宽广无垠的波特率设置范围。不仅如此,UART还支持异步通讯及半双工单线通讯模式,同时兼容LIN(本地互联网络)、智能卡协议、IrDA(红外数据协会)SIR ENDEC规范及modem流控(CTS/RTS)操作。此外,它还具备多机通讯能力,进一步拓展了其应用范围。
测试串口可以不适用多于的连接,因为我们的调试器可以实现虚拟串口,对应连接的开发板串口如下:
实物JP2和JP3也是已经连接上了:
接下来我们就进行串口的初始化工作,首先是IO的初始化:
- #define UART_TX_PORT GPIOA
- #define UART_TX_PIN ALD_GPIO_PIN_2
- #define UART_RX_PORT GPIOA
- #define UART_RX_PIN ALD_GPIO_PIN_3
- void init_uart_pin(void)
- {
- ald_gpio_init_t gpio_init;
- memset(&gpio_init, 0x00, sizeof(ald_gpio_init_t));
- /* Initialize tx pin: PA2 */
- gpio_init.mode = ALD_GPIO_MODE_OUTPUT;
- gpio_init.od = ALD_GPIO_PUSH_PULL;
- gpio_init.pupd = ALD_GPIO_PUSH_UP;
- gpio_init.odrv = ALD_GPIO_OUT_DRIVE_NORMAL;
- gpio_init.flt = ALD_GPIO_FILTER_DISABLE;
- gpio_init.type = ALD_GPIO_TYPE_CMOS;
- gpio_init.func = ALD_GPIO_FUNC_2;
- ald_gpio_init(UART_TX_PORT, UART_TX_PIN, &gpio_init);
- /* Initialize rx pin: PA3 */
- gpio_init.mode = ALD_GPIO_MODE_INPUT;
- gpio_init.od = ALD_GPIO_PUSH_PULL;
- gpio_init.pupd = ALD_GPIO_PUSH_UP;
- gpio_init.odrv = ALD_GPIO_OUT_DRIVE_NORMAL;
- gpio_init.flt = ALD_GPIO_FILTER_DISABLE;
- gpio_init.type = ALD_GPIO_TYPE_CMOS;
- gpio_init.func = ALD_GPIO_FUNC_2;
- ald_gpio_init(UART_RX_PORT, UART_RX_PIN, &gpio_init);
- return;
- }
接下来是串口初始化:
- ald_uart_handle_t g_uart_init;
- uint8_t g_rx_buf[256];
- uint32_t g_rx_len = sizeof(g_rx_buf);
- void init_uart1(void)
- {
- memset(&g_uart_init, 0x00, sizeof(ald_uart_handle_t));
- /* Initialize uart */
- g_uart_init.perh = CUART1;
- g_uart_init.init.baud = 115200;
- g_uart_init.init.word_length = ALD_UART_WORD_LENGTH_8B;
- g_uart_init.init.stop_bits = ALD_UART_STOP_BITS_1;
- g_uart_init.init.parity = ALD_UART_PARITY_NONE;
- g_uart_init.init.mode = ALD_UART_MODE_UART;
- g_uart_init.init.fctl = ALD_UART_HW_FLOW_CTL_DISABLE;
- g_uart_init.rx_cplt_cbk = uart_recv_complete;
- g_uart_init.tx_cplt_cbk = uart_send_complete;
- ald_uart_init(&g_uart_init);
-
- ald_mcu_irq_config(CUART1_IRQn, 4, ENABLE);
- ald_uart_recv_by_it(&g_uart_init, g_rx_buf, 1);
- }
- void uart_send_complete(ald_uart_handle_t *arg)
- {
- return;
- }
- void uart_recv_complete(ald_uart_handle_t *arg)
- {
- static uint8_t s_i;
- s_i++;
- if ((s_i > 1) && (*(g_uart_init.rx_buf - 2) == 0x0D) && (*(g_uart_init.rx_buf - 1) == 0x0A))
- {
- ald_uart_send_by_it(&g_uart_init, g_rx_buf, s_i);
- ald_uart_recv_by_it(&g_uart_init, g_rx_buf, 1);
- s_i = 0;
- }
- else
- {
- if (s_i >= g_rx_len)
- s_i = 0;
- ald_uart_recv_by_it(&g_uart_init, g_rx_buf + s_i, 1);
- }
- return;
- }
实现的功能是收到/r/n结尾的数据会回传,效果如下: