Day1 FreeRTOS系统简介

1.FreeRTOS系统简介

​   FreeRTOS是一个可裁剪、可剥夺型的多任务系统。(开源)

1.1 FreeRTOS任务特性

1.简单

2.没有使用限制

3.支持抢占

4.支持优先级

5.每个任务都拥有堆栈导致RAM使用量增大

6.如果使用抢占的话必须仔细考虑重入的问题

1.2 FreeRTOS任务状态

四种任务状态

1.运行态

2.就绪态

3.阻塞态

4.挂起态

1.3 任务优先级

任务优先级决定了任务的执行优先级别,在FreeRTOS中的任务优先级可以设置可选范围:

0 ~ configMAX_PRIORITIES - 1

数字越大,优先级越高

1.4 任务实现

任务实现为任务的具体工作内容。

例:

void vATaskFunction(void *pvParameters)

{

​       for( ; ; )

​       {

​               任务应用程序;

​               vTaskDelay();  

​       }

​       /*不能从任务函数中返回或者退出,从任务函数中返回或退出的话

​           就会调用configASSERT(),前提是你定义了configASSERT()。如果

​           一定要从任务函数中退出的话那一定要调用函数vTaskDelete(NULL)

​           来删除此任务。

​       */

​       vTaskDelete(NULL);

}

1.5 任务控制块

​   每个任务的属性的集合用这个结构体来表示,叫做任务控制块

typedef struct tskTaskControlBlock

{

    volatile StackType_t    *pxTopOfStack;  /*< Points to the location of the last item placed on the tasks stack.  THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT. */   //任务堆栈栈顶

#if ( portUSING_MPU_WRAPPERS == 1 )

    xMPU_SETTINGS   xMPUSettings;       /*< The MPU settings are defined as part of the port layer.  THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */

    //MCU相关设置

#endif

ListItem_t          xStateListItem; /*< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */   //状态列表项

ListItem_t          xEventListItem;     /*< Used to reference a task from an event list. */   //事件列表项

UBaseType_t         uxPriority;         /*< The priority of the task.  0 is the lowest priority. */   //任务优先级

StackType_t         *pxStack;           /*< Points to the start of the stack. 任务堆栈起始地址*/

char                pcTaskName[ configMAX_TASK_NAME_LEN ];/*< Descriptive name given to the task when created.  Facilitates debugging only. */ /*lint !e971 Unqualified char types are allowed for strings and single characters only.任务名字 */

#if ( portSTACK_GROWTH > 0 )

    StackType_t     *pxEndOfStack;      /*< Points to the end of the stack on architectures where the stack grows up from low memory. 任务堆栈栈底*/

#endif

#if ( portCRITICAL_NESTING_IN_TCB == 1 )

    UBaseType_t     uxCriticalNesting;  /*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer.临界区嵌套深度 */

#endif

#if ( configUSE_TRACE_FACILITY == 1 )

    UBaseType_t     uxTCBNumber;        /*< Stores a number that increments each time a TCB is created.  It allows debuggers to determine when a task has been deleted and then recreated. */

    UBaseType_t     uxTaskNumber;       /*< Stores a number specifically for use by third party trace code. */

#endif

#if ( configUSE_MUTEXES == 1 )

    UBaseType_t     uxBasePriority;     /*< The priority last assigned to the task - used by the priority inheritance mechanism.任务基础优先级,优先级反转的时候用到 */

    UBaseType_t     uxMutexesHeld;   //任务获取到的互斥信号量个数

#endif

#if ( configUSE_APPLICATION_TASK_TAG == 1 )

    TaskHookFunction_t pxTaskTag;  

#endif

#if( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )   //与本地存储有关

    void *pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];

#endif

#if( configGENERATE_RUN_TIME_STATS == 1 )

    uint32_t        ulRunTimeCounter;   /*< Stores the amount of time the task has spent in the Running state.记录任务运行总时间 */

#endif

#if ( configUSE_NEWLIB_REENTRANT == 1 )

    /* Allocate a Newlib reent structure that is specific to this task.

    Note Newlib support has been included by popular demand, but is not

    used by the FreeRTOS maintainers themselves.  FreeRTOS is not

    responsible for resulting newlib operation.  User must be familiar with

    newlib and must provide system-wide implementations of the necessary

    stubs. Be warned that (at the time of writing) the current newlib design

    implements a system-wide malloc() that must be provided with locks. */

    struct  _reent xNewLib_reent;    //定义一个newlib结构体变量

#endif

#if( configUSE_TASK_NOTIFICATIONS == 1 )  //任务通知相关变量

    volatile uint32_t ulNotifiedValue;   //任务通知值

    volatile uint8_t ucNotifyState;   //任务通知状态

#endif

/* See the comments above the definition of

tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */

#if( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )   //用来标记任务是动态创建还是静态创建的,如果是静态创建的此变量就位pdTRUE,如果是动态创建的就为pdFALSE

    uint8_t ucStaticallyAllocated;      /*< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */

#endif

#if( INCLUDE_xTaskAbortDelay == 1 )

    uint8_t ucDelayAborted;

#endif

} tskTCB;

相比UCOSIII成员变量少很多,可裁剪,不用的功能可以不用开启,节省任务块大小占用的空间。

 1.6 任务堆栈

​   保存任务现场(CPU寄存器中的值),创建任务的时候需要指定任务堆栈,任务堆栈的变量类型为

StackType_t,此变量类型如下:

#define postSTACK_TYPE  uint32_t

#define portBASE_TYPE  long

#define portSTACK_TYPE  StackType_t

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值