FreeRTOS 在调度程序启动之前(或停止之后)对 IO 进行排队

发布于 2024-08-31 02:46:02 字数 531 浏览 6 评论 0原文

我正在寻找有关如何在操作系统调度程序可能尚未启动、正在运行、挂起或可能已停止的环境中最好地实现线程安全 IO(例如,printf 进入调试串行端口)的建议。坠毁了。我正在使用 Newlib 和 FreeRTOS。

目前,我正在采用(看似标准的)FreeRTOS 方法,将 _write 系统调用(Newlib)队列字符放入 FreeRTOS 队列,然后从中断服务程序中清空(填充串行端口硬件 FIFO,然后等待 FIFO 空中断)。

这样做的缺点是(至少在 FreeRTOS 上)队列只能在调度程序运行时安全使用,并且在禁用中断时无法打印调试输出(因为它们在引导期间直到调度程序启动,或者在致命错误之后)错误条件(正是调试 printf 输出最有用的地方:-)

最好让 _write 系统调用查询调度程序和/或中断状态,并在调度程序正在运行时使用队列并使用阻塞/轮询串行 IO。当中断被禁用时?有一个我还没有想到的更优雅的想法吗

I'm looking for advice on how to best implement thread-safe IO (e.g. for printf going to a debug serial port) in an environment in which the operating system scheduler may yet to have started, be running, suspended or may have stopped or crashed. I'm using Newlib and FreeRTOS.

At the moment, I'm doing the (seemingly-standard) FreeRTOS approach of having the _write system call (Newlib) queue characters into a FreeRTOS queue which is then emptied from an interrupt service routine (filling the serial port hardware FIFO then waiting for the FIFO empty interrupt).

This has the disadvantage that (at least on FreeRTOS) the queue can only be used safely when the scheduler is running, and debug output can not be printed when interrupts are disabled (as they are during boot until the scheduler starts, or after a fatal error condition (exactly where debug printf output would be most useful :-).

Would it be best to have the _write system call query the scheduler and/or interrupt status, and use queues if the scheduler is running and use blocking/polling serial IO when interrupts are disabled? Is there a more elegant idea I haven't thought of yet?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左耳近心 2024-09-07 02:46:02

也许稍微优雅一点的是在 _write 系统调用中使用间接。不检查状态,而是根据调度程序是否正在运行、是否启用中断等,使用函数指针来实现所需的功能。然后在程序状态发生变化时设置适当的回调函数。

int (*writeCallback)(int, const void *, unsigned int);

int
_write(int fd, const void *buffer, unsigned int count)
{
    return writeCallback(fd, buffer, count);
}

...

writeCallback = polling_write;

...

writeCallback = rtos_write;

...

或者,您可以定义一个不同的调试函数,绕过标准库流系统(即 printf),以在您知道没有中断/RTOS 设施的地方使用(例如异常处理程序)。

Perhaps slightly more elegant would be to use indirection in the _write system call. Instead of checking the status, use a function pointer to achieve the desired functionality based on whether the scheduler is running, interrupts enabled, etc. Then set the appropriate callback function when the program state changes.

int (*writeCallback)(int, const void *, unsigned int);

int
_write(int fd, const void *buffer, unsigned int count)
{
    return writeCallback(fd, buffer, count);
}

...

writeCallback = polling_write;

...

writeCallback = rtos_write;

...

Alternatively, you define a different debug function that bypasses the standard library stream system (i.e. printf) to use in places that you know will not have interrupt/RTOS facilities (e.g. exception handlers).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文