在 FreeRTOS 中创建新任务用于 USART 接收
我使用 EVK1105 开发板和 AVR Studio 5 作为我的 AVR 项目的开发 IDE。 我在其中使用 FreeRTOS。我这块板上有 3 个 USART 端口。一个外部模块通过 USART-RS232 模式连接到我的 AVR32 板。它以 19230 波特率、7 个数据位、奇校验、停止位 1 和正常通道模式向我的 USART0 板上发送连续串行数据。我为此创建了一个新任务。每 9 个数据字节后发送 '\n' 和 '\r'。因此,在我的任务中,我继续收集字符串缓冲区中的 9 个数据字节,然后在 USART1 上传输它。我正在使用轮询方法从接收端口 USAR0 收集数据。但我在接收数据时遇到问题。我不知道是否是计时问题或调度程序在收集数据时切换任务。但我没有得到所需的数据。
以下是我已经检查过的故障排除内容 1. 将我的外部模块连接到我的 PC 超级终端,这给了我完美的结果。 2. 实现了与没有 FreeRTOS 一样使用从 USART0 接收的相同操作,并将接收到的任何内容传输到 USART1。它工作得很好。
请提出一些可能有问题的想法。我正在使用队列在 Tx 和 Rx 任务之间进行通信,以将字符串缓冲区从 USART0 传递到 USART1。处理队列有问题吗?如何排除队列故障?
我在 Rx Task 的无限任务循环中使用了 50ms 的延迟。它会产生问题吗?如果我不使用任何延迟,操作系统就会崩溃。请建议一些在 FreeRTOS 中创建新任务的良好做法,这样我就不会遇到任何计时问题。
I am using EVK1105 development board with AVR Studio 5 as development IDE for my AVR project.
I am using FreeRTOS in it. I have 3 USART ports on this board. One external module is connected to my AVR32 board via USART-RS232 mode. It sends me continuous serial data to my board on USART0 with 19230 baudrate, 7-databits, odd parity, stopbit-1 and normal-channel mode. I created a new task for this purpose. After each 9 data bytes it sends '\n' and '\r'. So in my task I keep on collecting the 9 databytes in a string buffer and then transmit it on USART1. I am using polling method to collect data from USAR0 which is receiving port. But I am facing problem in receiving data. I don't know if its timing issue or something or the scheduler switches the task while collects the data. But I don't get the required data.
Following are things I have already checked as troubleshooting
1. Connected my external module to my PC hyper-terminal which gives me perfect result.
2. Implemented the same thing of using receiving from USART0 and whatever received is transmitted to USART1 as without FreeRTOS. Its works fine.
Please suggest some idea what may be wrong. I am using a queue to communicate between Tx and Rx task to pass the string buffer from USART0 to USART1. Is it problem in handling queue? How can I troubleshoot the queue?
I am using a delay of 50ms in my infinite task loop in Rx Task. Can it create a problem? If I don't use any delay the OS crashes. Please suggest some good practices to create a new task in FreeRTOS so that I will not get any timing issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于这样的用例,我不会使用延迟 50ms 的轮询方法来从 UART 外设检索数据。根据系统负载和 UART 接收缓冲区大小,您很容易丢失接收到的数据。
至少在 UART 数据接收上使用中断,将每个接收到的字节复制到本地缓冲区中,该缓冲区将由 TX 线程读取。
您可以有一个更好的解决方案,使用 DMA 通道来接收数据帧,并在收到 9 个字节时收到通知。我不知道你的AVR设备是否有DMA外设。
For such a use case, I would not use a polling method with 50ms delay to retrieve data from UART peripheral. You can easily lose received data depending on the system load and UART reception buffer size.
At least use an interrupt on UART data reception that copies every received byte into a local buffer that will be read by your TX thread.
You can have an even better solution using a DMA channel to receive your data frame and be notified when 9 bytes have been received. I don't know if your AVR device has a DMA peripheral or not.
你还在做这方面的工作吗?您的问题的陈述很模糊,但我有几个建议/引导性问题。
1)您可能需要一些文件来查看寄存器是什么
获取巨大的 pdf 数据表:
http://www.atmel.com/dyn/products/product_docs.asp?category_id=163&family_id=607&subfamily_id=2138&part_id=4117
2) 在此和之前的您声明在某些情况下您能够接收数据。您需要从这些示例项目中找到 USART 硬件初始化代码,并将它们放入 freeRTOS 示例项目中。特别是调用
To connect to USART to CPU
我相信
仅仅这样做就需要研究大量代码 - 有很多依赖项。
2) 您调用什么函数来从 USART0 检索数据? 19kbaud 大约为 2000 字节/秒或 1 字节/0.5 毫秒,因此 50 毫秒轮询还不够。我建议您的 RX 任务连续轮询(从不显式休眠),但优先级低于 TX 任务。
3) 集中调试调用检索数据时的 RX 任务。使用调试器查看 usart 的硬件寄存器。特别是
USART0 cr 寄存器 AVR32_USART_CR_RXEN_MASK 应设置为启用 RX
USART0 csr寄存器AVR32_USART_CSR_RXRDY_MASK将指示那里是否有新数据
您还可以检查溢出标志以查看是否丢失了某些数据。
当读取 USART0 rhr 时,它应该是您发送的一个字节。
如果您仍在研究此问题,我可以进一步研究一下。
Are you still working on this? The statement of your problem is vague, but there I have several suggestions/leading questions.
1) You may want some documents to see what the registers are
Get the giant datasheet pdfs at
http://www.atmel.com/dyn/products/product_docs.asp?category_id=163&family_id=607&subfamily_id=2138&part_id=4117
2) In this and an earlier post you state that you have, in some cases, been able to RX data. You will need to find the USART HW initialization code from those example projects and get them into the freeRTOS example project. In particular calls to
To connect to USART to CPU
and i believe
Just doing this requires poking around a lot of code - there's alot of dependencies.
2) What function are you calling to retrieve data from USART0? 19kbaud is approximately 2000bytes/sec or 1 byte/0.5ms, so 50ms polling is not nearly enough. I'd suggest that your RX task poll continuously (never sleep explicitly) but at a lower priority than the TX task.
3) Concentrate on debugging the RX task at the call to retrieve data. Use the debugger to look at the hardware registers for the usart. In particular
USART0 cr register AVR32_USART_CR_RXEN_MASK should be set to enable RX
USART0 csr register AVR32_USART_CSR_RXRDY_MASK will indicate if there is new data there
You can also check the overlow flag to see if you have missed some data.
When the read of USART0 rhr occurs it should be a byte that you sent.
If you are still working on this I can look into this a bit more.