野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11058|回复: 8

Freertos 中使用链表问题

[复制链接]
发表于 2018-7-24 15:39:38 | 显示全部楼层 |阅读模式
我实现了一个链表,不在任务中调用一切正常,但是如果在任务中使用这个链表就会导致 HardFault_Handler 这个错误,下面是代码,请各位大佬帮忙看下,谢谢 !
[mw_shl_code=c,true]/****************************
功能: 初始化链表
说明:  
****************************/
fingertech_node*  Fingertech_listInit(void)
{
        fingertech_node* list = NULL;
        list = (fingertech_node*)pvPortMalloc(sizeof(fingertech_node));
        if (list == NULL)
        {
                return NULL;
        }
    memset(list,0xff,sizeof(fingertech_node));
    list->next = NULL;
        return list;
}

/****************************
功能: 向链表钟添加元素
说明:  
****************************/
uint32_t  Fingertech_listadd(fingertech_node **head, fingertech_node *list)
{
        fingertech_node *temp = NULL;
        //判断链表是否为空
        if (NULL == *head)
        {
        FingerTech_Loge("Fingertech_listadd is NULL\r\n");
                *head = list;
                (*head)->next = NULL;
        }
        else
        {
        FingerTech_Loge("Fingertech_listadd is ssss\r\n");
                temp = *head;
                while (temp)
                {
                        if (NULL == temp->next)
                        {
                                temp->next = list;
                                list->next = NULL;
                        }
                        temp = temp->next;
                }
        }
        return FINGERTECH_SUCCESS;
}

/****************************
功能: 删除元素
说明:  
****************************/
uint32_t FingerTech_listdel(fingertech_node **head, uint16_t fingertempnum)
{
        fingertech_node *temp, *p;
        temp = *head;
        if (NULL == temp)
        {
                return FINGERTECH_LISTERR;
        }
        else
        {
                //判断匹配的元素是否为链表的头部元素
                if (fingertempnum == temp->fingertempnum)
                {
                        *head = temp->next;
                }
                else
                {
                        while (temp->next)
                        {
                                p = temp;
                                temp = temp->next;
                                if (fingertempnum == temp->fingertempnum)
                                {
                                        p->next = temp->next;
                                        return FINGERTECH_SUCCESS;
                                }
                        }
                        return FINGERTECH_LISTERR;
                }
        }
        return FINGERTECH_LISTERR;
}


/****************************
功能: 修改元素值
说明:  
****************************/
uint32_t FingerTech_listmodify(fingertech_node **head, uint16_t fingertempnum, uint32_t fingertempsize)
{
        fingertech_node *temp;
        //头部作为临时变量
        temp = *head;
        while (temp)
        {
                if (fingertempnum == temp->fingertempnum)
                {
                        temp->fingertempsize = fingertempsize;
                        return FINGERTECH_SUCCESS;
                }
                temp = temp->next;
        }
        return FINGERTECH_LISTERR;
}

/****************************
功能: 查找元素
说明:  
****************************/
uint32_t FingerTech_listfind(fingertech_node **head, uint16_t fingertempnum)
{
        fingertech_node* temp;
        temp = *head;
        while (temp)
        {
                if (fingertempnum == temp->fingertempnum)
                {
                        return temp->fingertempsize;
                }
                temp = temp->next;
        }
        return FINGERTECH_LISTERR;
}

/****************************
功能: 打印链表值
说明:  
****************************/
void FingerTech_listPrint(fingertech_node **head)
{
        fingertech_node *temp;
        temp = *head;
        FingerTech_Logi("list information:\n");
        while (temp)
        {
                FingerTech_Logi("list %d : %d\r\n",temp->fingertempnum,temp->fingertempsize);
                temp = temp->next;
        }
}


/****************************
功能: 获取链表长度
说明:  
****************************/
uint8_t FingerTech_Getlistlen(fingertech_node **head)
{
        fingertech_node *temp;
        uint32_t count = 0;
        temp = *head;
        while (temp)
        {
                count++;
                temp = temp->next;
        }
        return count;
}

/****************************
功能: 获取链表中空余的最小的ID
说明:  
****************************/
uint8_t FingerTech_GetlistminID(fingertech_node **head)
{
        fingertech_node *temp;
        uint8_t tempid = 0xa0;
        uint8_t idtemp[10] = { 0,1,2,3,4,5,6,7,8,9 };
        uint8_t count = 0;
        temp = *head;
    FingerTech_Logi("get listmin id \r\n");
        while (temp)
        {
                idtemp[temp->fingertempnum] = 0xff;
                temp = temp->next;
        }
        for (count = 0; count<10; count++)
        {
                if (tempid > idtemp[count])
                {
                        tempid = idtemp[count];
                }
        }
        return tempid;
}

[/mw_shl_code]
回复

使用道具 举报

发表于 2018-7-24 15:43:22 | 显示全部楼层
执行哪个操作的时候hardfault啊
回复 支持 反对

使用道具 举报

发表于 2018-7-24 15:56:57 | 显示全部楼层
靠自己了,我今天刚刚把FreeRTOS 里面的 list.c 和list.h 全部看完。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-7-24 15:59:09 | 显示全部楼层
随风 发表于 2018-7-24 15:43
执行哪个操作的时候hardfault啊

FingerTech_listPrint 这个函数
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-7-24 15:59:35 | 显示全部楼层
fire 发表于 2018-7-24 15:56
靠自己了,我今天刚刚把FreeRTOS 里面的 list.c 和list.h 全部看完。

我是自己写的list,可以用freertos 里面的list吗 ?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-7-24 16:21:11 | 显示全部楼层
fire 发表于 2018-7-24 15:56
靠自己了,我今天刚刚把FreeRTOS 里面的 list.c 和list.h 全部看完。

我的意思是这样的,我初始化一个节点然后添加到链表中,在任务外能打印出来,在任务里面就不行。
[mw_shl_code=c,true]        fingertech_node* temp;
    FingerTech_BspInit();
        temp = Fingertech_listInit();
        temp->fingertempnum = 1;
        temp->fingertempsize = 100;
        Fingertech_listadd(&FingerTemplist_head,temp);
        FingerTech_listPrint(&FingerTemplist_head);[/mw_shl_code]

[mw_shl_code=c,true]void FingerTech_ledtask(void *pvParameters)
{
       
    while (1)
    {
                        FingerTech_Logi("led on\r\n");
                        FingerTech_listPrint(&FingerTemplist_head);
                        FingerTech_LedOn();
                        vTaskDelay(200);
                        FingerTech_LedOff();
                        vTaskDelay(200);
    }
}[/mw_shl_code]

[mw_shl_code=c,true]Fingertech_listadd is NULL
list information:
                 list 1 : 100
led on
list information:
                 list 13019 : 2143288132
list 42405 : 2147474467
[/mw_shl_code]
回复 支持 反对

使用道具 举报

发表于 2018-7-26 08:10:52 | 显示全部楼层
是你打印输出有问题吧
回复 支持 反对

使用道具 举报

发表于 2018-8-29 17:32:45 | 显示全部楼层
是不是多线程,或者线程+中断等多个地方调用同一个链表了。加锁或者开关中断看看。
回复 支持 反对

使用道具 举报

发表于 2018-10-8 13:39:11 | 显示全部楼层
圆周率 发表于 2018-7-24 15:59
我是自己写的list,可以用freertos 里面的list吗 ?

同问,后边再凑字,同问,同问
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-4-27 16:00 , Processed in 0.030725 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表