RTOS 删除练习中显式堆栈的替代方案?
在 ARM7 上用 C 语言编程的嵌入式应用程序(具有可移植性要求)中,当前使用基于商业优先级的抢占式 RTOS,我们需要根据客户要求删除该 RTOS 和任何 RTOS 依赖性。 我们有 8 个任务使用许多硬件接口、睡眠语句、I2C 通信。 事实上,软件充分利用了 RTOS 功能来简化代码,尽管在没有 RTOS 的情况下也可以管理时序要求。
多个函数(包括在许多地方调用的例程)当前实现了对 I2c 驱动程序函数、睡眠语句等的阻塞(对于该线程)调用序列。 基于对I2C调用/睡眠的轮询对于客户来说是不可接受的前提,那么此类调用必须是非阻塞的并返回。 当然,问题是当 I2C 完成或睡眠时间已过时,“返回”到“语句”,可能会从顶级任务入口向下调用 4 个调用。
我正在致力于为每个任务设计分层状态机,并在顶部有一个简单的调度程序。 但是,处理多个例程(这些例程过去用于进行 then 阻塞调用序列,现在每个例程都成为一个状态机,可以在多个位置和不同的函数调用深度进行调用)似乎需要为每个任务提供显式堆栈功能,因此每次启动子状态机时,我都可以为该进程分配状态并将它们推送到该任务的“状态堆栈”上,以便该任务的下一个调度程序调用将能够执行所有任务分层状态继续处理“停止”的地方。
您能否看到适用于该问题的其他设计架构、将代码快速移植到非抢占式范例的考虑因素,或者指出有关“RTOS 删除”技术和设计的丰富思想资源和讨论?
这三个答案共同描绘了基于状态机的开发相关性和避免重新发明轮子的相关工具的良好画面。 我们的客户不会接受任何类型的许可,包括 GPL。 从答案来看,如果想要使用没有 RTOS 且禁止轮询调用的分层状态机,则似乎没有办法绕过缓存状态。 由于分层 SM 通过保留现有代码的结构(对例程的函数调用变成了子状态机的调用)来帮助移植现有代码,因此我将采用这种方式,使用提供的工具作为很好的示例。 - 谢谢。
In an embedded application programmed on C on ARM7 (with portability requirements), currently using a commercial priority-based preemptive RTOS, we need to remove that RTOS and any RTOS dependency per customer mandate. We have 8 tasks using many HW interfaces, sleep statements, I2C communications. As it is, the SW makes good use of RTOS features for simplification of the code, though timing requirements would be manageable without an RTOS.
Several functions, including routines called in many places currently implement sequences of blocking (for that thread) calls to I2c driver functions, sleep statements, etc.
Based on the premise that polling on I2C calls / sleep is not acceptable for customers, such calls must then be non-blocking and return. The issue of course is to "come back" to the "statement", possibly 4 calls down from top-level task entry, when I2C completes or sleep time has elapsed.
I'm converging towards a hierarchical state machine design for each task, with a simple scheduler on top. But handling several routines, which used to make sequences of then-blocking calls and now become each a state machine, which can get called in several places and at different function call depths, seems to necessitate an explicit stack feature for each task, so that each time I start a sub-state-machine, I can allocate the states for that process and push them on the "state-stack" of that task, so that the next scheduler call to that task will be able to go down all the hierarchical states to continue processing where it "left off".
Can you see other design architectures applicable to the issue, considerations for fast porting of the code to a non-preemptive paradigm, or point to thought-enriching resources and discussions about "RTOS-removal" techniques and designs?
The three answers all together paint a good picture of state machine-based development relevancy and the associated tools to avoid re-inventing the wheel. Our customers won't take any kind of license, including GPL. From the answers it seems that there is no way around caching states if one wants to use hierarchical state machines without RTOS and with polling calls prohibited. As hierarchical SM help a lot porting the existing code by conserving its structure (function calls to routines become invocation of sub-state machines), I will go that way, using the provided tools as good examples. - Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您查看过Adam Dunkels 的原型线程吗? 他称它们为“C 语言中的轻量级、无堆栈线程”,
我不会重新发明轮子,而是直接在此处引用 protothreads 网站的一点内容:
Protothreads 是极其轻量级的无堆栈线程,专为内存严重受限的系统而设计,例如小型嵌入式系统或无线传感器网络节点。 Protothreads 为用 C 实现的事件驱动系统提供线性代码执行。Protothreads 可以与底层操作系统一起使用,也可以不与底层操作系统一起使用,以提供阻塞事件处理程序。 无需复杂的状态机或完整的多线程。
Protothreads 提供顺序控制流, Samek 的 QP HSM——它们都是解决重叠域问题的良好解决方案。 为此,我可能会倾向于原型线程。
您提到消除商业 RTOS。 想知道这是否是因为代码空间、成本、工程师学习曲线、性能……您可以用(许多)免费操作系统之一替换 RTOS 吗? 我想不会,但问一下也没什么坏处。
PS Dunkels 还有一个很棒的网站,其中有很多有用的资源和信息。 面向嵌入式开发人员的软件 - 检查一下(Contiki、协议栈等...)
Have you checked out Adam Dunkels' Protothreads? He calls them "Lightweight, Stackless Threads in C"
Rather than re-invent the wheel, I'll quote a tad from the protothreads site directly in-line here:
Protothreads are extremely lightweight stackless threads designed for severely memory constrained systems, such as small embedded systems or wireless sensor network nodes. Protothreads provide linear code execution for event-driven systems implemented in C. Protothreads can be used with or without an underlying operating system to provide blocking event-handlers. Protothreads provide sequential flow of control without complex state machines or full multi-threading.
I've used Protothreads & Samek's QP HSMs -- they're both good solutions for problems in overlapping domains. For this I'd probably lean towards protothreads.
You mentioned eliminating the commercial RTOS. Wondering if that's because of code space, cost, engineer learning curves, performance... could you just replace the RTOS with one of the (many) free ones? I guess not but it doesn't hurt to ask.
P.S. Dunkels also has a great website with a lot of useful resources & software for embedded developers - check it out (Contiki, protocol stacks, etc..)
使用工具,例如 IAR visualState,您可以为分层状态机生成代码,而无需单独的堆栈。 有一个免费的SMC,它效率稍低,功能较少,并且不支持漂亮的 UML StateChart 图片。
您还可以将状态机手动编码为带有 switch 语句和静态变量的函数来保存状态。
有一些基于轻量级状态机的伪线程库可以使用 C 宏来执行此操作。 查看原型线程
Using a tool, such as IAR visualState, you can generate code for hierarchical state machines without separate stacks. There is a freebie SMC that is a bit less efficient, with fewer features, and that doesn't support the pretty UML StateChart pictures.
You can also hand code state machines as functions with switch statements and static variables to hold the state.
There are lightweight state machine based pseudo-threading libraries that do this using C macros. Check out protothreads
我强烈建议您查看 Miro Samek 的量子编程框架。 他拥有最高效的 HSM 之一,可以在 ARM7 等许多平台上运行。 他还有各种复杂程度的框架来满足您的需求。 我建议您也阅读他的书 Practical StateCharts in C/C++。 他在 C 和 C++ 中都有一个分层状态机。 该框架的优点在于,以一定程度的效率从 UML 或状态图转换为实际代码并不困难。 该框架可以作为自己的调度程序运行,也可以与 RTOS 一起使用。
实际上,我已经用 C++ 为我公司的一款自制 RTOS 实现了自己的 HSM,并取得了一些成功。 我使用了很多 Samek 的设计原则,但由于 GPL(免费版本)和商业许可证(非 GPL)的定价而没有使用他的代码。
I would highly recommend looking at Miro Samek's Quantum programming framework. He has one of the most efficient HSM's out there that runs on a lot of platforms such as the ARM7. He also has various levels of complexity of the framework to suit your needs. I would recommend grabbing his book called Practical StateCharts in C/C++ as well. He has a hierarchical state machine in both C and C++. The beauty of this framework is that it's not to hard to go from UML or state diagrams to actual code with some degree of efficiency. This framework can be run as it's own scheduler or used along side an RTOS.
I have actually implemented my own HSM in C++ for one of my company's home grown RTOS with some success. I used a lot of Samek's design principles, but not his code due to GPL (free version) and pricing for commercial license (non-GPL).
我还想选择量子编程框架!
I would as well go for the Quantum Programming Framework!