FreeRTOS之添加链表代码

1、 在FreeRTOS文件夹添加list.c和list.h、portmacro.h文件。
在这里插入图片描述

2、 在FreeRTOS双击,接着文件类型为All file, 然后在FreeRTOS文件选择list.c和portmacro.h,最后点击Add。添加好的效果如第二张图

在这里插入图片描述

![在这里插入图片描述](https://img-blog.csdnimg.cn/20210507145352242.png
)

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 (;;)
    {
        /* 啥事不干 */
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值