FreeRTOS列表与列表项相关知识总结以及列表项的插入与删除实战

1.列表与列表项概念及结构体介绍

1.1列表项简介

列表相当于链表,列表项相当于节点,FreeRTOS 中的列表是一个双向环形链表
在这里插入图片描述

1.2 列表、列表项、迷你列表项结构体

1)列表结构体
typedef struct xLIST
{
listFIRST_LIST_INTEGRITY_CHECK_VALUE /* 校验值 /
volatile UBaseType_t uxNumberOfItems; /
列表中的列表项数量 /
ListItem_t * configLIST_VOLATILE pxIndex /
用于遍历列表项的指针 /
MiniListItem_t xListEnd /
末尾列表项 /
listSECOND_LIST_INTEGRITY_CHECK_VALUE /
校验值 /
} List_t;
列表结构体示意图
在这里插入图片描述
2)列表结构体
struct xLIST_ITEM
{
listFIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE /
用于检测列表项的数据完整性 /
configLIST_VOLATILE TickType_t xItemValue /
列表项的值 /
struct xLIST_ITEM * configLIST_VOLATILE pxNext /
下一个列表项 /
struct xLIST_ITEM * configLIST_VOLATILE pxPrevious /
上一个列表项 /
void * pvOwner /
列表项的拥有者 (通常是任务控制块)/
struct xLIST * configLIST_VOLATILE pxContainer; /
列表项所在列表 /
listSECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE /
用于检测列表项的数据完整性 /
};
typedef struct xLIST_ITEM ListItem_t;
2)迷你结构体
迷你列表项也是列表项,但迷你列表项仅用于标记列表的末尾和挂载其他插入列表中的列表项
struct xMINI_LIST_ITEM
{
listFIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE /
用于检测数据完整性 /
configLIST_VOLATILE TickType_t xItemValue; /
列表项的值 /
struct xLIST_ITEM * configLIST_VOLATILE pxNext; /
上一个列表项 /
struct xLIST_ITEM * configLIST_VOLATILE pxPrevious; /
下一个列表项 */
};
typedef struct xMINI_LIST_ITEM MiniListItem_t;

2.列表相关API函数介绍

1)void vListInitialise(List_t * const pxList)
形参:pxList 待初始化列表项
描述:初始化列表,pxIndex 指向 xListEnd,xListEnd为oxFFFFFF
2)void vListInitialiseItem( ListItem_t * const pxItem )
形参: pxItem 带初始化列表项
描述: 初始化列表项,列表项所在列表为空,pxItem->pxContainer = NULL
3void vListInsertEnd ( List_t * const pxList , ListItem_t * const pxNewList)
形参:pxList 列表
pxNewListItem 待插入列表项 ,,无序的插入方法
描述:列表末尾插入列表项
4)void vListInsert ( List_t * const pxList , ListItem_t * const pxNewListItem )
形参:pxList 列表
pxNewListItem 待插入列表项
描述: 列表插入列表项,有序地插入到列表中
5)UBaseType_t uxListRemove ( ListItem_t * const pxItemToRemove )
形参:pxItemToRemove 待移除列表项
返回值:整数: 待移除列表项移除后,所在列表剩余列表项的数量
描述:列表移除列表项

3,列表项的插入和删除实验free_

3.1freertos_demo.h

#ifndef __FREERTOS_DEMO_H
#define __FREERTOS_DEMO_H

//定义freeRTOS实验函数
void freertos_demo(void);

#endif

3.1freertos_demo.c


#include "freertos_demo.h"
#include "./SYSTEM/usart/usart.h"
#include "./BSP/LCD/lcd.h"
#include "./BSP/KEY/key.h"
/*FreeRTOS*********************************************************************************************/
#include "FreeRTOS.h"
#include "task.h"

///******************************************************************************************************/
//1.定义start_task配置与task任务配置以及声明列表项1.2.3
//1.1 start_task任务配置、包括栈大小、任务优先级、任务句柄以及start_task()函数定义
#define START_TASK_PRIO            1
#define START_STK_SIZE           128
TaskHandle_t  g_start_task_handler;
void start_task(void* pvParameters);

//1.2 task1任务配置
#define TASK1_PRIO                 2
#define TASK1_SIZE               128
TaskHandle_t  g_task1_handler;
void task1(void* pvParameters);

//1.3 列表与列表项1、2、3声明
List_t           test_list;
ListItem_t     list_item1;
ListItem_t     list_item2;
ListItem_t     list_item3;


//2.创建freertos_demo()函数,在此函数中创建start_task任务,并开始任务调度
void freertos_demo(void)
{
    //2.1创建任务satrt_task
     xTaskCreate( (TaskFunction_t)    start_task,                //函数地址
                   (char *)           "start_task",              //函数名称
                   (uint16_t)         START_STK_SIZE,            //栈大小
                   (void *)           NULL,                      //传递给任务函数参数
                   (UBaseType_t)      START_TASK_PRIO,           //任务优先级
                   (TaskHandle_t *)   &g_start_task_handler );   //任务句柄
    //2.2 开启任务调度器
     vTaskStartScheduler();
}


//3.创建start_task()函数中创建任务task1,并删除start_task任务
void start_task(void* pvParameters)
{
    //3.2 设置临界代码保护
    taskENTER_CRITICAL();
    //3.1创建任务task1
       xTaskCreate( (TaskFunction_t)    task1,               //函数地址
                   (char *)            "task1",              //函数名称
                   (uint16_t)         TASK1_SIZE,            //栈大小
                   (void *)           NULL,                  //传递给任务函数参数
                   (UBaseType_t)      TASK1_PRIO,            //任务优先级
                   (TaskHandle_t *)   &g_task1_handler );    //任务句柄
    
    
    //3.3 删除任务start_task
     vTaskDelete(g_start_task_handler);
                   
     taskEXIT_CRITICAL();

}


//4.在task1()函数中进行列表项与列表项的初始化、地址打印、插入与删除
void task1(void* pvParameters)
{
    //4.1 初始化列表和列表项的地址
    vListInitialise(&test_list);
    vListInitialiseItem(&list_item1);
    vListInitialiseItem(&list_item2);
    vListInitialiseItem(&list_item3);
    //4.2打印列表和各列表项的地址
    printf("/**************第二步:打印列表和列表项的地址**************/\r\n");
    printf("项目\t\t\t地址\r\n");
    printf("test_list\t\t0x%p\t\r\n", &test_list);
    printf("test_list->pxIndex\t0x%p\t\r\n", test_list.pxIndex);
    printf("test_list->xListEnd\t0x%p\t\r\n", (&test_list.xListEnd));
    printf("list_item1\t\t0x%p\t\r\n", &list_item1);
    printf("list_item2\t\t0x%p\t\r\n", &list_item2);
    printf("list_item3\t\t0x%p\t\r\n", &list_item3);
    printf("/**************************结束***************************/\r\n");
    
    //4.3设置key0,插入列表项1,2,3,打印列表及列表项地址
    printf("按下KEY0键继续!\r\n\r\n\r\n");
    while(key_scan(0) != KEY0_PRES)
    {
        vTaskDelay(10);
    }
    printf("/**************第三步:插入列表项1,2,3打印列表和列表项的地址**************/\r\n");
    vListInsert(&test_list,&list_item1);
    vListInsert(&test_list,&list_item2);
    vListInsert(&test_list,&list_item3);
    
    printf("项目\t\t\t\t地址\r\n");
    printf("test_list->xListEnd->pxNext\t0x%p\r\n", (test_list.xListEnd.pxNext));
    printf("test_list->xListEnd->pxPrevious\t0x%p\r\n", (test_list.xListEnd.pxPrevious));
    printf("list_item1->pxNext\t\t0x%p\r\n",     (list_item1.pxNext));
    printf("list_item1->pxPrevious\t\t0x%p\r\n", (list_item1.pxPrevious));
    printf("list_item2->pxNext\t\t0x%p\r\n",     (list_item2.pxNext));
    printf("list_item2->pxPrevious\t\t0x%p\r\n", (list_item2.pxPrevious));
    printf("list_item3->pxNext\t\t0x%p\r\n",     (list_item3.pxNext));
    printf("list_item3->pxPrevious\t\t0x%p\r\n", (list_item3.pxPrevious));
    printf("/**************************结束***************************/\r\n");
    
    
    //4.4设置key0,删除列表项2,打印列表及列表项地址
    printf("按下KEY0键继续!\r\n\r\n\r\n");
    while(key_scan(0) != KEY0_PRES)
    {
        vTaskDelay(10);
    }
    printf("/*******************第六步:移除列表项2********************/\r\n");
    
    uxListRemove(&list_item2);
    
    printf("项目\t\t\t\t地址\r\n");
    printf("test_list->xListEnd->pxNext\t0x%p\r\n", (test_list.xListEnd.pxNext));
    printf("test_list->xListEnd->pxPrevious\t0x%p\r\n", (test_list.xListEnd.pxPrevious));
    printf("list_item1->pxNext\t\t0x%p\r\n", (list_item1.pxNext));
    printf("list_item1->pxNext\t\t0x%p\r\n", (list_item1.pxNext));
    printf("list_item3->pxPrevious\t\t0x%p\r\n", (list_item3.pxPrevious));
    printf("list_item3->pxPrevious\t\t0x%p\r\n", (list_item3.pxPrevious));
    printf("/**************************结束***************************/\r\n");
    //4.5设置key0,尾插法插入列表项3,打印列表及列表项地址
    printf("按下KEY0键继续!\r\n\r\n\r\n");
    while(key_scan(0) != KEY0_PRES)
    {
        vTaskDelay(10);
    }
    printf("/*******************第六步:尾差法插入列表项2********************/\r\n");
    
    vListInsertEnd(&test_list,&list_item2);
    
    printf("项目\t\t\t\t地址\r\n");
    printf("test_list->xListEnd->pxNext\t0x%p\r\n", (test_list.xListEnd.pxNext));
    printf("test_list->xListEnd->pxPrevious\t0x%p\r\n", (test_list.xListEnd.pxPrevious));
    printf("list_item1->pxNext\t\t0x%p\r\n", (list_item1.pxNext));
    printf("list_item1->pxNext\t\t0x%p\r\n", (list_item1.pxNext));
    printf("list_item3->pxPrevious\t\t0x%p\r\n", (list_item3.pxPrevious));
    printf("list_item3->pxPrevious\t\t0x%p\r\n", (list_item3.pxPrevious));
    printf("list_item2->pxPrevious\t\t0x%p\r\n", (list_item2.pxPrevious));
    printf("list_item2->pxPrevious\t\t0x%p\r\n", (list_item2.pxPrevious));
    printf("/**************************结束***************************/\r\n");
}
  • 25
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_libinc++

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值