Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
freemodbus
UART
串口
关于在freemodbus中配置uart5来实现RTU-slave功能异常的问题
发布于 2021-12-03 13:56:42 浏览:1227
订阅该版
开发平台 STM32H743VIT6开发板 开发环境 rtthread studio 内核版本 4.0.3 问题描述 已经通过studio配置了uart5,并且自己写了一个sample能够正常驱动 这是board.h的配置  这是硬件初始化代码  这是我自己写的sample ``` #include
#include
#include
#include
#include
#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,则一切正常 请问有大佬遇到相同的问题吗?
查看更多
whj467467222
认证专家
2021-12-03
开源,分享,交流,共同进步
楼主是用的 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 的,建议楼主根据自己的硬件在检查一下。
1
个回答
默认排序
按发布时间排序
撰写答案
登录
注册新账号
关注者
0
被浏览
1.2k
关于作者
adaphoto
这家伙很懒,什么也没写!
提问
9
回答
44
被采纳
5
关注TA
发私信
相关问题
1
rt thread 2.0.2 usart 接收缓存问题
2
关于STM32串口通信的问题
3
STM32F1+RTT串口接收终端数据丢失问题
4
UART TX丢数据?
5
RTT打开串口的时候如何自定义波特率呢?
6
STM32F4的USART数据接收问题
7
串口1234使用问题
8
串口接收回调函数
9
LPC18xx UART问题讨论
10
x1000串口配置的失败问题
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
最新文章
1
ART-Pi2快速上手--点灯
2
ART-Pi2移植CMSIS-DAP(基于CherryUSB协议栈)
3
RT-thred的stm32h723对应bsp包CubeMX添加其他外设报错
4
RT-Thread中的time溢出问题,时间戳溢出,解决方法
5
ART-PI使用env驱动ETH网卡,pc和板子可以ping通
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
DMA
USB
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
rt-smart
FAL
I2C_IIC
UART
ESP8266
cubemx
WIZnet_W5500
ota在线升级
PWM
BSP
flash
freemodbus
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
编译报错
中断
Debug
rt_mq_消息队列_msg_queue
keil_MDK
ulog
SFUD
msh
C++_cpp
MicroPython
本月问答贡献
RTT_逍遥
10
个答案
3
次被采纳
xiaorui
3
个答案
2
次被采纳
winfeng
2
个答案
2
次被采纳
三世执戟
8
个答案
1
次被采纳
KunYi
8
个答案
1
次被采纳
本月文章贡献
catcatbing
3
篇文章
5
次点赞
lizimu
2
篇文章
8
次点赞
swet123
1
篇文章
4
次点赞
Days
1
篇文章
4
次点赞
YZRD
1
篇文章
2
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部