Freertos

时间:2024-03-28 14:31:17

一、引言:

中断,类似闹钟,中断函数调用时主函数转而执行某内容,这样会让程序效率低下、冗杂。

rtos特点为实时性,可靠性,安全性

Rtos分类:

Hard real time system 硬实时系统


在设置的截止时间内可完成相应功能的,如果完不成,系统会崩溃。


Firm real time system 强实时系统


强实时性比硬实时要求弱一些,如果在截止时间内完不成,系统不会崩溃,忽略这次执行。


Soft real time system 软实时系统


软实时比强实时弱一些,如果在截止时间内没有完成,完全不受影响,继续运行。

RTOS举例:LynxOS, RTLinux, VxWorks, FreeRTOS, OSE, QNX, Windows CE

硬实时案例:

反导弹系统要求硬实时。反导弹系统由一系列硬实时任务组成。反导系统必须首先探测所有来袭导弹,正确定位反导炮,然后在导弹来袭之前将其摧毁。所有这些任务本质上都是硬实时的,如果反导弹系统有任何一个任务失败都将无法成功拦截来袭导弹。

强实时案例:

ADC + DMA + TIM的双缓冲数据采集并对数据做实时滤波分析。这种情况一般需要强实时处理,一个缓冲通过DMA存储ADC数据期间,另一个缓冲的数据可以提取出来做分析,如果不能再缓冲填满之前分析完,另一个缓冲数据将被覆盖。

又比如DS18B20这种对时序要求比较高的,如果没有在设置的时序内执行,读取结果都是错的,错误的数据我们丢弃即可,继续下一轮读取。

软实时案例:

GUI类的应用都是软实时的,有点延迟什么的并不影响,只是体验稍差点。还有网页浏览等。

二、RTOS 特点(牺牲代码效率换取实时性)

Advantages:

  • Abstract out timing information
  • Maintainability/Extensibility
  • Modularity(模块性)
  • Cleaner interfaces(简洁界面)
  • Easier testing (in some cases)
  • Code reuse
  • Improved efficiency
  • Idle time(空闲时间 idle 进程)
  • Flexible interrupt handling
  • Mixed processing requirements
  • Easier control over peripherals

Cons:

  • Low Priority Tasks
  • Precision of code
  • Limited Tasks
  • Complex Algorithms(算法复杂)
  • Device driver and interrupt signals
  • Thread Priority(线程优先级)
  • Expensive
  • Not easy to program

众多的调度算法:

  • Difference Between Preemptive and Non-Preemptive Scheduling
  • What is a Scheduling Algorithm
  • Rate Monotonic Scheduling
  • Earliest Deadline First
  • Least Laxity First

main kernel features:

  • Fixed Priority Preemptive Time slicing Scheduler
  • Inter-Process Communication through Queues
  • Tasks Management such as Task priority setting, task suspension, task deletion, and Task delay
  • Tasks Synchronization with Gatekeeper Tasks, Semaphore, and Mutex
  • Timing measurement hook
  • Run time Tracing hooks
  • Software timers management
  • Interrupt management

三、FreeRTOS任务管理

在多任务系统中,应用程序可以由许多任务组成。如果我们使用的是单核处理器,那么在任何给定时间,处理器上只能运行一个任务。因此,只有一个任务将处于运行状态,所有其他任务将处于未运行状态。这意味着,在基于 RTOS 的应用程序中,任务可以处于运行状态或非运行状态。

任务的运行状态可以进一步分为阻止状态、就绪状态和挂起状态等三个子状态。下图显示了多任务系统中任务的转换生存期。

阻止状态

由于多种原因,任务可能处于阻止状态。例如,由于任务的周期性,任务会延迟,并且在每次指定的延迟后,任务将定期可用。另一个原因可能是由于中断等待或资源等待。任务正在等待外部中断或资源,例如二进制信号量、计数信号量和互斥锁。

暂停状态

挂起状态也是未运行任务的基质。应用程序程序员可以使用 vTaskSuspend() FreeRTOS API 函数暂停任务,并使用 vTaskResume() API 函数恢复任务。

注意:处于阻止或暂停状态的任务无法由调度程序调度。

就绪状态

如果任务未处于阻止或挂起状态,则它将处于就绪状态。处于就绪状态的任务,一旦处理器根据计划策略选取它们,就可以执行这些任务。

每当任务从运行状态转换到非运行状态时,都会发生上下文切换。这意味着,它将其寄存器值、临时变量值等保存到其任务控制块中,下次当它再次进入运行状态时,它将从离开执行的同一点开始执行。要从同一位置开始执行,它将再次将值从 TCB 任务控制部件(Task Control Block)加载到处理器寄存器中。

FreeRTOS task Creation

xTaskCreate(MyTask_pointer, "task_name", 100, Parameter, Priority, TaskHandle);
Pointer to Task Function

MyTask_pointer: This first argument to task creation function is a pointer to a function definition of a task. Because we need to define a task with the help function.

task_name: This argument is just the name of the function/task that we will create.

Stack Size(堆栈大小)

StackDepth: In multitasking, each task/thread has its own stack. It defines the stack size of a task in bytes. Therefore, you should make sure to select stack size according to the complexity of computation. For instance, we select 100 bytes size in this example.

Parameter: If we want to pass a pointer to a variable as an argument to the task function, we can use this argument. Otherwise, we can pass the NULL value. This argument is a pointer to a variable that the task (function) can receive.

Priority:  0, 1,2, and 3. zero means the lowest priority, and 3 means the highest priority.

TaskHandle: This argument keeps the handle of the function that we can use to change function features such as the deletion of a task, changing its priority, etc.

设置任务执行模式

vTaskDelayUntil 用于定义任务执行的确定性序列。例如,如果我们有四个任务,并且我们希望在 100、120、130 和 140 毫秒后执行每个任务,则 vTaskDelayUntil 会在任务首次执行后在定义的时间内阻止该任务。

注意:vTaskDelayUntil 与 Arduino IDE 的 delay() 不同。因为 delay() 会停止 CPU 执行。另一方面,vTaskDelayUntil 会延迟特定任务,而 CPU 会继续执行其他线程。