完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
RTT版本是4.0.5在接收串口的数据时,有时候一帧数据会被分成2段或更多,所以在串口回调函数中写了一个拼接数据的函数,同时会打印出这组数据,这样能保证接收正确的数据,但是,在运行中发现,串口回调函数有时候会被自己中断掉,也就是说,第一次接收的数据在串口中断函数中还没有打印完毕,另一次的串口中断就开始进来打印数据了,这样会导致系统出错死机。这样应该如何解决?
//初始化函数 int uart_dma_init(void) { rt_err_t ret = RT_EOK; char uart_name[RT_NAME_MAX]; static char msg_pool[8192]; struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /* 初始化配置参数 */ rt_strncpy(uart_name, SX1302_UART_NAME, RT_NAME_MAX); /* 查找串口设备 */ serial = rt_device_find(uart_name); if (!serial) { rt_kprintf("find %s failed! ", uart_name); return RT_ERROR; } /* step2:修改串口配置参数 */ config.baud_rate = BAUD_RATE_115200; //修改波特率为 115200 config.data_bits = DATA_BITS_8; //数据位 8 config.stop_bits = STOP_BITS_1; //停止位 1 config.bufsz = 1024; //修改缓冲区 buff size 为 1024 config.parity = PARITY_NONE; //无奇偶校验位 /* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */ rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config); /* 初始化消息队列 */ rt_mq_init(&rx_mq, "rx_mq", msg_pool, /* 存放消息的缓冲区 */ sizeof(struct rx_msg), /* 一条消息的最大长度 */ sizeof(msg_pool), /* 存放消息的缓冲区大小 */ RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */ rt_mq_init(&rx_mq_sx1302, "rx_mq_sx1302", msg_pool, /* 存放消息的缓冲区 */ sizeof(struct rx_msg), /* 一条消息的最大长度 */ sizeof(msg_pool), /* 存放消息的缓冲区大小 */ RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */ /* 以 DMA 接收及轮询发送方式打开串口设备 */ rt_device_open(serial, RT_DEVICE_FLAG_DMA_RX); /* 设置接收回调函数 */ rt_device_set_rx_indicate(serial, uart_input); ///* 发送字符串 */ //rt_device_write(serial, 0, str, (sizeof(str) - 1)); rt_kprintf("uart2 for sx1302-at is ready! "); return ret; } /* 接收数据回调函数 */ static rt_err_t uart_input(rt_device_t dev, rt_size_t size) { struct rx_msg msg,msg_rec; rt_err_t result; uint16_t crc,crc16,framelenth; msg.size = size; rt_memset(msg.msgbuffer,0,sizeof(msg.msgbuffer)); rt_kprintf("Lora rec size:%d,",msg.size);PrintTime(); //打印时间 rt_device_read(dev, 0, msg.msgbuffer, msg.size); /* 从串口读取数据*/ for(int i=0;i rt_kprintf("%02x ",msg.msgbuffer); } rt_kprintf(" "); if(Sx1302ConfigFlag == 1) //Sx1302ConfigFlag为1就直接发送消息,如果是0就要进行处理,防止被拆包 { result = rt_mq_send(&rx_mq_sx1302, &msg, sizeof(msg)); if ( result == -RT_EFULL) { /* 消息队列满 */ rt_kprintf("message queue full! "); } } else { //rt_kprintf("1..LoraRec.flag:%d,LoraRec.index:%d ",LoraRec.flag,LoraRec.index); if(LoraRec.flag == 0) { if(msg.msgbuffer[0]==0x12) { //rt_kprintf("1-msg.size%d ",msg.size); rt_memcpy(LoraRec.buffer,msg.msgbuffer,msg.size); framelenth = LORA_Frameheadlenth + Get16_FromFrame(LoraRec.buffer,LORA_Length_POS); //rt_kprintf("1-framelenth%d ",framelenth); if(msg.size == (framelenth+2)) { crc = Get16_FromFrame(LoraRec.buffer,framelenth); crc16=CRC16(LoraRec.buffer,framelenth); if(crc==crc16) { //完整数据包,发送 result = rt_mq_send(&rx_mq, &msg, sizeof(msg)); //rt_kprintf("xxxxxx "); if ( result == -RT_EFULL) { /* 消息队列满 */ rt_kprintf("message queue full! "); } } } else //不完整的包 { //rt_kprintf("2-msg.size%d ",msg.size); LoraRec.flag = 1; LoraRec.index = msg.size; } } } else if(LoraRec.flag == 1) { //rt_kprintf("3-msg.size%d ",msg.size); rt_memcpy(&LoraRec.buffer[LoraRec.index],msg.msgbuffer,msg.size); if((LoraRec.buffer[0]==0x12)&&(LoraRec.buffer[1]==0x34)) { framelenth = LORA_Frameheadlenth + Get16_FromFrame(LoraRec.buffer,LORA_Length_POS); //rt_kprintf("2-framelenth%d ",framelenth); LoraRec.reclenth = LoraRec.index + msg.size; //计算长度 //rt_kprintf("1-LoraRec.reclenth:%d ",LoraRec.reclenth); if(LoraRec.reclenth == (framelenth+2)) { crc = Get16_FromFrame(LoraRec.buffer,framelenth); crc16=CRC16(LoraRec.buffer,framelenth); if(crc==crc16) { //完整数据包,发送 rt_memcpy(msg_rec.msgbuffer,LoraRec.buffer,sizeof(LoraRec.buffer)); msg_rec.size = framelenth + 9; //数据帧头7加2个CRC result = rt_mq_send(&rx_mq, &msg_rec, sizeof(msg_rec)); for(int i=0;i rt_kprintf("%02x ",LoraRec.buffer); } //rt_kprintf("oooooooo "); rt_memset(LoraRec.buffer,0,sizeof(LoraRec.buffer)); LoraRec.index = 0; LoraRec.flag =0; if ( result == -RT_EFULL) { /* 消息队列满 */ rt_kprintf("message queue full! "); } } } else //不完整的包 { //rt_kprintf("1message clear "); rt_memset(LoraRec.buffer,0,sizeof(LoraRec.buffer)); LoraRec.index = 0; LoraRec.flag =0; } } else { //rt_kprintf("2message clear "); rt_memset(LoraRec.buffer,0,sizeof(LoraRec.buffer)); LoraRec.index = 0; LoraRec.flag =0; } } else { rt_memset(LoraRec.buffer,0,sizeof(LoraRec.buffer)); LoraRec.index = 0; LoraRec.flag =0; } } //rt_kprintf("2 LoraRec.flag:%d,LoraRec.index:%d ",LoraRec.flag,LoraRec.index); return result; } 出错信息 [2022-05-28 11:29:59] 1-process over.Sat May 28 11:30:07 2022 [2022-05-28 11:29:59] Lora rec size:4,Sat MayLora rec size:26,Sat May 28 11:30:07 2022 [2022-05-28 11:29:59] 12 34 03 01 00 03 01 c2 84 00 00 03 41 a0 00 00 02 40 6f 25 9a fd 01 07 00 fc [2022-05-28 11:29:59] 28 11:30:07 2022 [2022-05-28 11:29:59] 00 00 00 00 [2022-05-28 11:29:59] psr: 0xa100000f [2022-05-28 11:29:59] r00: 0x00000001 [2022-05-28 11:29:59] r01: 0x240015b8 [2022-05-28 11:29:59] r02: 0x256f4002 [2022-05-28 11:29:59] r03: 0x2400f098 [2022-05-28 11:29:59] r04: 0x2400e788 [2022-05-28 11:29:59] r05: 0x00000001 [2022-05-28 11:29:59] r06: 0x00000001 [2022-05-28 11:29:59] r07: 0x00000001 [2022-05-28 11:29:59] r08: 0xdeadbeef [2022-05-28 11:29:59] r09: 0xdeadbeef [2022-05-28 11:29:59] r10: 0xdeadbeef [2022-05-28 11:29:59] r11: 0xdeadbeef [2022-05-28 11:29:59] r12: 0x00000000 [2022-05-28 11:29:59] lr: 0x0804a02d [2022-05-28 11:29:59] pc: 0x080202c8 [2022-05-28 11:29:59] hard fault on handler 如何才能让串口回调函数不中断自己?是因为我在串口回调函数中写的东西太长了吗? |
|
相关推荐
1个回答
|
|
用 serialX 吧
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
590 浏览 0 评论
AI模型部署边缘设备的奇妙之旅:如何在边缘端部署OpenCV
2241 浏览 0 评论
tms320280021 adc采样波形,为什么adc采样频率上来波形就不好了?
1233 浏览 0 评论
1788 浏览 0 评论
1464 浏览 0 评论
74851 浏览 21 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-23 11:12 , Processed in 0.586582 second(s), Total 39, Slave 33 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号