1、 在FreeRTOS文件夹添加list.c和list.h、portmacro.h文件。
2、 在FreeRTOS双击,接着文件类型为All file, 然后在FreeRTOS文件选择list.c和portmacro.h,最后点击Add。添加好的效果如第二张图
)
3、 点击Options for Target…,①修改晶振频率 ②选择软件仿真。具体操作如下图。
4、填充代码,在最后面。
5、运行结果如下
在portmacro.h文件添加如下代码
#ifndef PORTMACRO_H
#define PORTMACRO_H
#include "stdint.h"
#include "stddef.h"
#include <string.h>
#include <stdio.h>
#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 portSTACK_TYPE StackType_t;
typedef long BaseType_t;
typedef unsigned long UBaseType_t;
typedef uint32_t TickType_t;
#define portMAX_DELAY ( TickType_t ) 0xffffffffUL
#endif /* PORTMACRO_H */
在list.c添加如下代码
#include "list.h"
void vListInitialiseItem(ListItem_t * const pxItem)
{
pxItem->pvContainer = NULL;
}
void vListInitialise(List_t *const pxList)
{
pxList->uxNumberOfItems = 0;
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);
}
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最后一个节点
pxIndex->pxPrevious = pxNewListItem;
pxNewListItem->pvContainer = (void*)pxList;
pxList->uxNumberOfItems++;
}
void vListInsert(List_t * const pxList,ListItem_t * const pxNewListItem)
{
ListItem_t * pxIterator;
const TickType_t xValueOfInsertion = pxNewListItem->xItemValue;
if (xValueOfInsertion == portMAX_DELAY)
{
pxIterator = pxList->xListEnd.pxPrevious;
}else
{
for (pxIterator=(ListItem_t *)&(pxList->xListEnd);
pxIterator->pxNext->xItemValue <= xValueOfInsertion;
pxIterator = pxIterator->pxNext )
{
}
}
pxNewListItem->pxNext = pxIterator->pxNext;
pxNewListItem->pxNext->pxPrevious = pxNewListItem;
pxNewListItem->pxPrevious = pxIterator;
pxIterator->pxNext = pxNewListItem;
pxNewListItem->pvContainer = (void *)pxList;
pxList->uxNumberOfItems++;
}
UBaseType_t uxListRemove(ListItem_t * const pxItemToRemove)
{
List_t *const pxList = (List_t*)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;
}
在main.c添加如下代码
#include "portmacro.h"
#include "list.h"
/* 定义链表根节点 */
struct xLIST List_Test;
/* 定义节点 */
struct xLIST_ITEM List_Item1;
struct xLIST_ITEM List_Item2;
struct xLIST_ITEM List_Item3;
int main(void)
{
/* 链表根节点初始化 */
vListInitialise( &List_Test );
/* 节点 1 初始化 */
vListInitialiseItem( &List_Item1 );
List_Item1.xItemValue = 1;
/* 节点 2 初始化 */
vListInitialiseItem( &List_Item2 );
List_Item2.xItemValue = 2;
/* 节点 3 初始化 */
vListInitialiseItem( &List_Item3 );
List_Item3.xItemValue = 3;
/* 将节点插入链表,按照升序排列 */
vListInsert( &List_Test, &List_Item2 );
vListInsert( &List_Test, &List_Item1 );
vListInsert( &List_Test, &List_Item3 );
for (;;)
{
/* 啥事不干 */
}
}