开发平台
STM32H743VIT6开发板
开发环境
rtthread studio
内核版本
4.0.3
问题描述
已经通过studio配置了uart5,并且自己写了一个sample能够正常驱动
这是board.h的配置
这是硬件初始化代码
这是我自己写的sample
#include <rtdevice.h>
#include <rtthread.h>
#include <board.h>
#include <stdio.h>
#include <string.h>
#define SAMPLE_UART_NAME "uart5"
/* 用于接收消息的信号量 */
static struct rt_semaphore rx_sem;
static rt_device_t serial;
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /* 初始化配置参数 */
uint8_t fd_str[200];
/* 接收数据回调函数 */
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
/* 串口接收到数据后产生中断,调用此回调函数,然后发送接收信号量 */
rt_sem_release(&rx_sem);
return RT_EOK;
}
static void serial_thread_entry(void *parameter)
{
char ch;
while (1)
{
/* 从串口读取一个字节的数据,没有读取到则等待接收信号量 */
while (rt_device_read(serial, -1, &ch, 1) != 1)
{
/* 阻塞等待接收信号量,等到信号量后再次读取数据 */
rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
}
ch = ch + 1;
rt_device_write(serial, 0, &ch, 1);
}
}
static int uart_sample(int argc, char *argv[])
{
rt_err_t ret = RT_EOK;
char uart_name[RT_NAME_MAX];
char str[] = "Hello RT-Thread! Uart1. \r\n";
/* 查找系统中的串口设备 */
serial = rt_device_find(SAMPLE_UART_NAME);
if (!serial)
{
rt_kprintf("find %s failed!\n", uart_name);
return RT_ERROR;
}
/* 修改串口配置参数 */
config.baud_rate = BAUD_RATE_9600;
/* 控制串口设备。通过控制接口传入命令控制字,与控制参数 */
rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
/* 初始化信号量 */
rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
/* 以中断接收及轮询发送模式打开串口设备 */
rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
/* 设置接收回调函数 */
rt_device_set_rx_indicate(serial, uart_input);
/* 发送字符串 */
rt_device_write(serial, 0, str, (sizeof(str) - 1));
/* 创建 serial 线程 */
rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 2048, 25, 10);
/* 创建成功则启动线程 */
if (thread != RT_NULL)
{
rt_thread_startup(thread);
}
else
{
ret = RT_ERROR;
}
return ret;
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(uart_sample, uart device sample);
这是freemodbus的配置
具体表现为,通过MSH调试窗口输入启动modbus rtu的例子,串口IO口会被拉低,完全无法接收到数据。
如果将freemodbus配置成使用uart2,则一切正常
请问有大佬遇到相同的问题吗?
楼主是用的 RTT Studio 选择芯片创建工程的吧,在 drv_uart.c 中会自动配置引脚,而不会使用用户配置的引脚配置。
具体代码如下:
static rt_err_t stm32_gpio_configure(struct stm32_uart_config *config)
{
#define UART_IS_TX (1U<<7)
#define UART_IS_RX (0U)
rt_uint16_t tx_pin_num = 0;
int index = 0, tx_af_num = 0, rx_af_num = 0;
uint8_t uart_num = 0;
GPIO_TypeDef *tx_port;
GPIO_TypeDef *rx_port;
uint16_t tx_pin;
uint16_t rx_pin;
get_pin_by_name(config->rx_pin_name, &rx_port, &rx_pin);
get_pin_by_name(config->tx_pin_name, &tx_port, &tx_pin);
struct gpio_uart_af {
/* index get by GET_PIN */
uint16_t pin_index;
struct {
/* when is TX the bit7 is 1, bit6-bit0 is uart number (1-8) */
uint8_t uart_num;
uint8_t af_num;
} afs[1];
};
static const struct gpio_uart_af uart_afs[] =
{
{ .pin_index = GET_PIN(A, 0), .afs[0] = {.uart_num = UART_IS_TX|4, .af_num = 8}},
{ .pin_index = GET_PIN(A, 1), .afs[0] = {.uart_num = UART_IS_RX|4, .af_num = 8}},
{ .pin_index = GET_PIN(A, 2), .afs[0] = {.uart_num = UART_IS_TX|2, .af_num = 7}},
{ .pin_index = GET_PIN(A, 3), .afs[0] = {.uart_num = UART_IS_RX|2, .af_num = 7}},
串口外设的选择是这里查表来进行的。这个表是有 BUG 的,建议楼主根据自己的硬件在检查一下。
确实是引脚映射问题啊,好坑啊!
在表格中,把TX映射给了PB5 RX映射给了PB6,要将两个倒过来就好了。