1.数据类型的定义:
#ifndef PORTMICRO_H
#define PORTMICRO_H
#include "stdint.h"
#include "stddef.h"
#include "FreeRTOSConfig.h"
/*Data type redefinition*/
#define portCHAR char
#define portFLOAT float
#define portDOUBLE double
#define portLONG long
#define portSHORT short
#define portSTACK_TYPE uint32_t
#define portBASE_TYPE long
typedef portBASE_TYPE StackType_t;
typedef long BaseType_t;
typedef unsigned long UBaseType_t;
#if (configUSE_16BIT_TICKS == 1)
typedef uint16_t TickType_t;
#define portMAX_DELAY (TickType_t)0xFFFF
#else
typedef uint32_t TickType_t;
#define portMAX_DELAY (TickType_t)0xFFFFFFFFUL
#endif
#endif
2.配置宏的定义
#define configUSE_16BIT_TICKS 0
3.list.h的实现
/*
* @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
* @FilePath: \1.链表节点的插入\FreeRTOS\Source\Inc\list.h
* @Date: 2024-01-02 11:25:27
* @LastEditTime: 2024-01-03 13:48:40
* @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
* @Description:
*/
#ifndef LIST_H
#define LUST_H
#include "portmicro.h"
/*Initialize the owner of the node*/
#define listSET_LIST_ITEM_OWNER(pxListItem, pxOwner) \
((pxListItem)->pvOwner = (void *)pxOwner)
/*Get the owner of the node*/
#define listGET_LIST_ITEM_OWNER(pxListItem) \
((pxListItem)->pvOwner)
/*Initializes the sort value of the node*/
#define listSET_LIST_ITEM_VALUE(pxListItem, xValue) \
((pxListItem)->xItemValue = xValue)
/*Get the sort value of the node*/
#define listGET_LIST_ITEM_VALUE(pxListItem) \
((pxListItem)->xItemValue)
/*Get the value of the node counter for the root node*/
#define listGET_ITEM_VALUE_OF_HEAD_ENTRY(pxList) \
(((pxList)->xListEnd).pxNext->xItemValue)
/*Get the entry node of the linked list*/
#define listGET_HEAD_ENTRY(pxList) \
((pxList)->xListEnd.pxNext)
/*Get the last node of the linked list*/
#define listGET_END_MARKER(pxList) \
((ListItem_t const *)(&((pxList)->xListEnd)))
/*Determine whether the linked list is empty*/
#define listLIST_IS_EMPTY(pxList) \
((BaseType_t)((pxList)->uxNumberOfItems == (BaseType_t)0UL))
/*Get the current number of nodes in the linked list*/
#define lsitCURRENT_LIST_LENGTH(pxList) \
((pxList)->uxNumberOfItems)
/*Get the object to which the first node of the linked list belongs*/
#define listGET_OENER_OF_NEXT_ENTRY(pxTCB, pxList) \
{ \
List_t *const pxConstList = (pxList); \
((pxConstList)->pxIndex = (pxConstList)->pxIndex->pxNext); \
if ((void *)(pxConstList)->pxIndex == (void *)&((pxConstList)->xListEnd)) \
{ \
(pxConstList)->pxIndex = (pxConstList)->pxIndex->pxNext; \
} \
(pxTCB) = (pxConstList)->pxIndex->pvOwner; \
}
struct xLIST_ITEM
{
TickType_t xItemValue; // Used to help with node sorting
struct xLIST_ITEM *pxNext; // Point to next
struct xLIST_ITEM *pxPrevious; // Point to previous
void *pvOwner; // Point to the kernel object that owns the node,It is usually "TCB"
void *pvContainer; // Point to the linked list where the node resides
};
struct xMINI_LIST_ITEM
{
TickType_t xItemValue; // It is used to help nodes sort in ascending order
struct xLIST_ITEM *pxNext; // Point to next
struct xLIST_ITEM *pxPrevious; // Point to previous
};
typedef struct xMINI_LIST_ITEM MiniListItem_t;
typedef struct xLIST_ITEM ListItem_t;
typedef struct xList
{
UBaseType_t uxNumberOfItems; // Linked list node counters
ListItem_t *pxIndex; // Linked list node index pointer
MiniListItem_t xListEnd; // The end of node
} List_t;
void vlistInitialiseItem(ListItem_t *const pxItem);
void vlistInitialise(List_t *const pxList);
void vListInsertEnd(List_t *const pxList,ListItem_t *const pxNewListItem);
void ListInsert(List_t *const pxList,ListItem_t *const pxNewListItem);
#endif
4.list.c的实现
#include "list.h"
/**
* @Description: Initialise list item
* @param {ListItem_t} *pxItem
* @return {*}
*/
void vlistInitialiseItem(ListItem_t *const pxItem)
{
pxItem->pvContainer = NULL;
}
/**
* @Description: Initialize the root node
* @param {List_t} *pxList
* @return {*}
*/
void vlistInitialise(List_t *const pxList)
{
pxList->pxIndex = (ListItem_t *)&pxList->xListEnd;
pxList->xListEnd.xItemValue = portMAX_DELAY;
pxList->xListEnd.pxPrevious = (ListItem_t *)&pxList->xListEnd;
pxList->xListEnd.pxNext = (ListItem_t *)&pxList->xListEnd;
pxList->uxNumberOfItems=(UBaseType_t)0UL;
}
/**
* @Description: Trailing insertion of linked list
* @param {List_t} *pxList
* @param {ListItem_t} *pxNewListItem
* @return {*}
*/
void vListInsertEnd(List_t *const pxList,ListItem_t *const pxNewListItem)
{
ListItem_t *const pxIndex=pxList->pxIndex;
pxNewListItem->pxNext=pxIndex;
pxNewListItem->pxPrevious=pxIndex->pxPrevious;
pxIndex->pxPrevious->pxNext=pxNewListItem;
pxIndex->pxPrevious=pxNewListItem;
pxNewListItem->pvContainer=(void *)pxList;
(pxList->uxNumberOfItems)++;
}
/**
* @Description: Insert nodes in ascending order
* @param {List_t} *pxList
* @param {ListItem_t} *pxNewListItem
* @return {*}
*/
void ListInsert(List_t *const pxList,ListItem_t *const pxNewListItem)
{
ListItem_t *pxIterator;
const TickType_t xValueOfInseration=pxNewListItem->xItemValue;
if(xValueOfInseration==portMAX_DELAY)
{
pxIterator=pxList->xListEnd.xItemValue;
}
else
{
for(pxIterator=(ListItem_t *)&pxList->xListEnd;
pxIterator->pxNext->xItemValue<=xValueOfInseration;
pxIterator=pxIterator->pxNext)
{
}
}
pxNewListItem->pxNext=pxIterator->pxNext;
pxNewListItem->pxNext->pxPrevious=pxNewListItem;
pxNewListItem->pxPrevious=pxIterator;
pxIterator->pxNext=pxNewListItem;
pxNewListItem->pvContainer=(void *)pxList;
(pxList->uxNumberOfItems)++;
}
/**
* @Description: Delete the node
* @param {ListItem_t} *pxItemToRemove
* @return {UBaseType_t} pxList->uxNumberOfItems
*/
UBaseType_t uxListRemove(ListItem_t *const pxItemToRemove)
{
List_t *const pxList=pxItemToRemove->pvContainer;
pxItemToRemove->pxNext->pxPrevious=pxItemToRemove->pxPrevious;
pxItemToRemove->pxPrevious->pxNext=pxItemToRemove->pxNext;
if(pxList->pxIndex==pxItemToRemove)
{
pxList->pxIndex=pxItemToRemove->pxPrevious;
}
pxItemToRemove->pvContainer=NULL;
(pxList->uxNumberOfItems)--;
return (pxList->uxNumberOfItems);
}
5.测试
List_t list_Test;
ListItem_t list_Item1;
ListItem_t list_Item2;
ListItem_t list_Item3;
void listInitialiase_rootNode(List_t *const pxList)
{
vlistInitialise(pxList);
}
void test_func(List_t *const pxList, ListItem_t *const pxNewListItem,TickType_t itemValue)
{
vlistInitialiseItem(pxNewListItem);
pxNewListItem->xItemValue=itemValue;
ListInsert(pxList,pxNewListItem);
}
int main(void)
{
listInitialiase_rootNode(&list_Test);
test_func(&list_Test, &list_Item2, 2);
test_func(&list_Test, &list_Item1, 1);
test_func(&list_Test, &list_Item3, 3);
for (;;)
{
}
return 0;
}
6.测试结果
在主函数吧中依次插入的列表项是2,1,3;通过测试结果可以看出,成功将插入同一个列表的列表项进行了升序排列。