完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
代码与我的另一个贴子差不多. 现在的问题不一样, 目前碰到的问题是串口采用DMA发送时会死机. 详见代码
//监控主站的软件看门狗 static void profibus_watchdog_proc(void) { m_rtUsart2ProfibusErrCnt++; //超时则重新初始化状态 if (m_rtUsart2ProfibusErrCnt > m_rtUsart2WatchDogTime) { m_rtUsart2ProfibusErrCnt = 0; .......... } } } // @Brief dma_tx回调函数 static rt_err_t uart2_dma_tx_callback(rt_device_t dev, void * dataBuf) { rt_err_t result; rt_pin_write(RS485_RX_TX_EN, PIN_LOW); return result; } //@brief dma回调函数 static rt_err_t uart2_dma_rx_callback(rt_device_t dev, rt_size_t size) { /* 串口接收到数据后产生中断,调用此回调函数,然后发送接收信号量 */ struct rx_msg msg; rt_err_t result; msg.dev = dev; msg.size = size; result = rt_mq_send(&profidp_rx_mq, &msg, sizeof(msg)); if ( result == -RT_EFULL) { /* 消息队列满 */ rt_kprintf("message queue full!n"); } return result; } // @brief 串口接收线程 static void serial_thread_entry(void *parameter) { struct rx_msg msg; rt_err_t result; rt_uint16_t rx_length; while (1) { if (m_rtUsart2TxToRxUs > 0) { rt_thread_mdelay(1); m_rtUsart2TxToRxUs--; } else { rt_pin_write(RS485_RX_TX_EN, PIN_LOW); } rt_memset(&msg, 0, sizeof(msg)); rt_memset(m_rtUsart2RxBuf, 0x00, sizeof(m_rtUsart2RxBuf)); profibus_watchdog_proc(); /* 从消息队列中读取消息*/ result = rt_mq_recv(&profidp_rx_mq, &msg, sizeof(msg), RT_WAITING_NO/*RT_WAITING_NO RT_WAITING_FOREVER*/); if (result == RT_EOK) { /* 从串口读取数据*/ rx_length = rt_device_read(msg.dev, 0, m_rtUsart2RxBuf, msg.size); if (rx_length >= 6 && rx_length <= PROFIDP_RX_BUF_LEN) { //发送使能 rt_pin_write(RS485_RX_TX_EN, PIN_HIGH); hw_profibusdp_rx_proc(m_rtUsart2RxBuf, rx_length); hw_profibusdp_auto_tx_proc(); m_rtUsart2TxToRxUs = 20; } } } } * @brief 串口初始化 int profibus_uart_init(void) { rt_err_t open_result = RT_EOK; rt_pin_mode(RS485_RX_TX_EN, PIN_MODE_OUTPUT); rt_pin_write(RS485_RX_TX_EN, PIN_LOW); profibusdp_serial=rt_device_find(DP_UART_PORT_NAME); if(RT_NULL == profibusdp_serial) { rt_kprintf("uart2 find failed !n"); } else { hw_profibusdp_init(); rt_mq_init(&profidp_rx_mq, "profidp_rx_mq" ,msg_pool, sizeof(struct rx_msg), sizeof(msg_pool), RT_IPC_FLAG_PRIO); //RT_IPC_FLAG_PRIO | RT_IPC_FLAG_FIFO); profibusdp_uart_config.baud_rate = BAUD_RATE_9600; profibusdp_uart_config.data_bits = DATA_BITS_9; profibusdp_uart_config.stop_bits = STOP_BITS_1; profibusdp_uart_config.bufsz = PROFIDP_RX_BUF_LEN; profibusdp_uart_config.parity = PARITY_EVEN; rt_device_control(profibusdp_serial, RT_DEVICE_CTRL_CONFIG, &profibusdp_uart_config); open_result = rt_device_open(profibusdp_serial, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_DMA_RX | RT_DEVICE_FLAG_DMA_TX); RT_ASSERT(open_result == RT_EOK); rt_device_set_rx_indicate(profibusdp_serial, uart2_dma_rx_callback); rt_device_set_tx_complete(profibusdp_serial, uart2_dma_tx_callback); } rt_thread_t thread = rt_thread_create("profidp_serial", serial_thread_entry, RT_NULL, 1024, 1, 10); if (thread != RT_NULL) { rt_thread_startup(thread); } return RT_EOK; } |
|
相关推荐
1个回答
|
|
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
AI模型部署边缘设备的奇妙之旅:如何在边缘端部署OpenCV
1379 浏览 0 评论
tms320280021 adc采样波形,为什么adc采样频率上来波形就不好了?
1080 浏览 0 评论
1502 浏览 0 评论
1385 浏览 0 评论
1030 浏览 0 评论
74591 浏览 21 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-18 18:19 , Processed in 0.487835 second(s), Total 40, Slave 33 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号