先来一段代码作为前菜:
- rt_device_t uart = rt_device_find(uart_name);
- struct serial_configure serial_config = RT_SERIAL_CONFIG_DEFAULT;
- serial_config.baud_rate = baud;
- serial_config.data_bits = data_bits;
- serial_config.parity = parity;
- serial_config.stop_bits = stop_bits;
- rt_err_t err = rt_device_control(uart, RT_DEVICE_CTRL_CONFIG, &serial_config);
- if(err != RT_EOK) {
- LOG_E("configure %s failed!rn", uart_name);
- RT_ASSERT(err != RT_EOK);
- }
- err = rt_device_open(uart, RT_DEVICE_FLAG_RDWR|RT_DEVICE_FLAG_INT_RX|RT_DEVICE_FLAG_STREAM);
- if(err != RT_EOK) {
- LOG_E("open %s failed! %drn", uart_name, err);
- RT_ASSERT(err != RT_EOK);
- }
- rt_device_set_rx_indicate(uart, uart1_rx_indicate_callback);
1. 通过rt_device_find查找名为uart_name的设备,传入的参数为"uart1";
2. 通过rt_device_control对串口设备进行初始化操作,传入的参数类型为struct serial_configure;
3. 打开串口设备;
4. 通过rt_device_set_rx_indicate设置串口接收回调,用于通知串口收到了数据。
下面是drv_usart.c中的初始化代码:
- int rt_hw_usart_init(void)
- {
- rt_size_t obj_num = sizeof(uart_obj) / sizeof(struct ab32_uart);
- struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
- rt_err_t result = 0;
- rt_hw_interrupt_install(IRQ_UART0_2_VECTOR, uart_isr, RT_NULL, "ut_isr");
- for (int i = 0; i < obj_num; i++)
- {
- /* init UART object */
- uart_obj[i].config = &uart_config[i];
- uart_obj[i].rx_idx = 0;
- uart_obj[i].rx_idx_prev = 0;
- uart_obj[i].serial.ops = &ab32_uart_ops;
- uart_obj[i].serial.config = config;
- uart_obj[i].serial.config.baud_rate = 1500000;
- uart_obj[i].rx_buf = rt_malloc(uart_config[i].fifo_size);
- if (uart_obj[i].rx_buf == RT_NULL) {
- LOG_E("uart%d malloc failed!", i);
- continue;
- }
- /* 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
- , RT_NULL);
- RT_ASSERT(result == RT_EOK);
- }
- return result;
- }
由于默认写死了波特率是1500000,所以一开始的代码通过
rt_device_control进行了配置。
下面总结一下AB32
开发板硬件调试串口。
一到手的时候发现用普通的串口助手回显字符是有问题的比如发送"help"命令,回显是"hheellpp",通过查看代码,插入日志打印等方式定位到多余的一个回显字符发生在接收中断之前,也就是说应该是硬件有回环,然后查看了原理图及调试串口的配置发现,果然还是有门道的,通过下载工具带的串口调试是正常的。
硬件回环:
单引脚复用: