FreeRTOS Lists and List Items | FreeRTOS Seven

Table of contents

illustrate:

1. Understanding lists and list items

1.1. Introduction

1.3. Reasons why FreeRTOS is suitable for lists and list items

1.4. List structure

1.5. List structure diagram

1.6. List item structure

1.7. Mini list item structure

1.8. List item structure diagram

1.9. Mini list item structure diagram

1.10. Implementation ideas for inserting list items into a list

1.11. Implementation ideas for list deletion of list items

2. List and list item API functions

2.1. List initialization function

2.2. List item initialization function

2.3. List item insertion function

2.4. Insert function at the end of list item

2.5. List item deletion function

3. Examples of lists and list items

3.1. Example

3.2. Results


illustrate:

About the content:

1) The following contents are mostly conceptual understanding and step analysis

2) There is no personal sample code yet. The official sample code of FreeRTOS is used.

3) If you want to transplant the code for testing, please look elsewhere. There is no personal sample code for testing in the following content.

About others:

1) Operating system: win 10

2) Platform: keil 5 mdk

3) Language: c language

4) Board: STM32 series transplanted to FreeRTOS

1. Understanding lists and list items

1.1. Introduction

        1) Introduction to lists: Lists are a data structure in FreeRTOS. Conceptually similar to linked lists, lists are used to track tasks in FreeRTOS;

        2) Introduction to list items: List items are items stored in the list.

1.2. Characteristics of lists and list items

        1) The list is equivalent to a linked list, and the list items are equivalent to the nodes in the linked list. The list in FreeRTOS is a two-way circular linked list (connected head to tail);

        2) List characteristics: The addresses between list items are non-consecutive and are artificially connected together. The number of list items is determined by the number added later and can be changed at any time (how many are added);

        3) Characteristics of arrays: The addresses of array members are continuous. The number of members is determined when initially created and cannot be changed later.

1.3. Reasons why FreeRTOS is suitable for lists and list items

        In OS, the number of tasks is usually uncertain, and the status of tasks will be sent to change, so using a list (doubly linked list) is very suitable.

1.4. List structure

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;

Meaning of structure members:

名称:listFIRST_LIST_INTEGRITY_CHECK_VALUE

Meaning: Check list completeness

Conditions of use: The macro definition configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES needs to be set to 1. After turning it on, a variable xListIntegrityValue1 and xListIntegrityValue2 will be added to these two places respectively . When the list is initialized, a special value will be written to these two variables. This is not turned on by default. Function.

Name:uxNumberOfItems

Meaning: Used to record the number of list items in the list

Name:pxIndex

Meaning: record the current list item index number, used to index the list

Name:xListEnd

Meaning: The last list item in the list, used to indicate the end of the list

1.5. List structure diagram

As shown in Figure 1 below:

figure 1

1.6. List item structure

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;  

Meaning of structure members:

名称:listFIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE        

Meaning: Check list integrity, not enabled by default

Name:xItemValue

Meaning: the value of a list item

Name:pxNext

Meaning: points to the next list item

Name: pxPrevious

Meaning: Points to the previous list item, and works with pxNext to achieve a function similar to a doubly linked list

Name: pvOwner

Meaning: Record who owns this list item, usually a task control block

Name:pxContainer

Meaning: Used to record which list this list item belongs to

1.7. Mini list item structure

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;

Meaning of structure members:

Name:xItemValue

Meaning: the value of a list item

Name:pxNext

Meaning: points to the next list item

Name: pxPrevious

Meaning: Points to the previous list item

1.8. List item structure diagram

As shown in Figure 2 below:

Figure 12

1.9. Mini list item structure diagram

As shown in Figure 3 below:

image 3

 1.10. Implementation ideas for inserting list items into a list

Assume that an empty linked list wants to insert list item 1 (if there are multiple list items, to insert a list item, you need to compare the list item value xItemValue and sort it in ascending order). If you want to insert the last list item, it depends on whether the pxindex of the current list points to It is the last list item (pointing to whom, inserting in front of whom, this is not necessarily in ascending order), just use the following ideas to insert.

1) Initialization list and list items

2) The pxNext of the list points to the end list item of this list by default, and now the point needs to be changed;

3) Point pxNext to list item 1

4) Point pxPrevious of the last list item to list item 1

5) pxNext of list item 1 points to the last list item

6) pxPrevious of list item 1 points to pxlndex

7) Total number of list items +1

1.11. Implementation ideas for list deletion of list items

Assume that list item 1 and list item 2 exist in the list, and now we want to delete list item 2. The structure of the list is list item 1->list item 2->end list item;

1) Initialization list and list items

2) Point pxNext of list item 1 to the end list item

3) Point pxPrevious of the last list item to list item 1

4) Total number of list items -1

2. List and list item API functions

2.1. List initialization function

void vListInitialise( List_t * const pxList )
{
    pxList->pxIndex = ( ListItem_t * ) &( pxList->xListEnd );
    pxList->xListEnd.xItemValue = portMAX_DELAY;
    pxList->xListEnd.pxNext = ( ListItem_t * ) &( pxList->xListEnd );   
    pxList->xListEnd.pxPrevious = ( ListItem_t * ) &( pxList->xListEnd );

    pxList->uxNumberOfItems = ( UBaseType_t ) 0U;
    listSET_LIST_INTEGRITY_CHECK_1_VALUE( pxList );
    listSET_LIST_INTEGRITY_CHECK_2_VALUE( pxList );
}

2.2. List item initialization function

void vListInitialiseItem( ListItem_t * const pxItem )
{
    pxItem->pxContainer = NULL;
    listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem );
    listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem );
}

2.3. List item insertion function

void vListInsert( List_t * const pxList,  ListItem_t * const pxNewListItem )
{
    ListItem_t * pxIterator;
    const TickType_t xValueOfInsertion = pxNewListItem->xItemValue;
    listTEST_LIST_INTEGRITY( pxList );
    listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );
    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->pxContainer = pxList;

    ( pxList->uxNumberOfItems )++;
}

2.4. Insert function at the end of list item


void vListInsertEnd( List_t * const pxList, ListItem_t * const pxNewListItem )
{
    ListItem_t * const pxIndex = pxList->pxIndex;
    listTEST_LIST_INTEGRITY( pxList );
    listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );
    pxNewListItem->pxNext = pxIndex;
    pxNewListItem->pxPrevious = pxIndex->pxPrevious;
    mtCOVERAGE_TEST_DELAY();

    pxIndex->pxPrevious->pxNext = pxNewListItem;
    pxIndex->pxPrevious = pxNewListItem;
    pxNewListItem->pxContainer = pxList;

    ( pxList->uxNumberOfItems )++;
}

2.5. List item deletion function

UBaseType_t uxListRemove( ListItem_t * const pxItemToRemove )
{

    List_t * const pxList = pxItemToRemove->pxContainer;

    pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;
    pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;


    mtCOVERAGE_TEST_DELAY();


    if( pxList->pxIndex == pxItemToRemove )
    {
        pxList->pxIndex = pxItemToRemove->pxPrevious;
    }
    else
    {
        mtCOVERAGE_TEST_MARKER();
    }

    pxItemToRemove->pxContainer = NULL;
    ( pxList->uxNumberOfItems )--;

    return pxList->uxNumberOfItems;
}

3. Examples of lists and list items

3.1. Example

None yet

3.2. Results

None yet

Guess you like

Origin blog.csdn.net/qq_57663276/article/details/128875415