FreeRTOS Study Notes 0921

Source: Internet
Author: User
Tags mutex relative semaphore switches
Sixth Chapter FreeRTOS task-related API functions

6.1 Task creation and deletion API functions

1. Function Xtaskcreate () creates a task using a dynamic method.

Configsupport_dynamic_allocation

The newly created task defaults to the ready state, and if there is currently no higher priority task running then this task will immediately enter the running state
Start running, you can create tasks either before the Task Scheduler starts or after it is started.

The required RAM is automatically allocated from the FreeRTOS heap, so the memory management file must be provided
Xtaskcreate (Interrupt_task,//Task function
"Interrupt_task",//Task Name
Interrupt_stack_size,//task stack size
(void *) NULL,//arguments passed to the task function
interrupt_task_priority,//Task Priority
(taskhandle_t*) &interrupttask_handler);//Task handle

2. function Xtaskcreatestatic ()

The RAM required to create a task using this function needs to be supplied by the user.


4. function Vtaskdelete ()

When the call function Vtaskdelete () deletes a task, the stack and control block memory requested before this task is freed in the idle task.
Therefore, when the function Vtaskdelete () is called, the idle task must be run for a certain amount of time after the task is deleted.


6.4 Task suspend and restore API functions

1. function Vtasksuspend ()

void Vtasksuspend (taskhandle_t xtasktosuspend)

Xtasktosuspend: The handle of the task to suspend.

2. function Vtaskresume ()

void Vtaskresume (taskhandle_t xtasktoresume)
Xtasktoresume: The task handle to restore

3. Function Xtaskresumefromisr () interrupt version
basetype_t Xtaskresumefromisr (taskhandle_t xtasktoresume)
Xtasktoresume: The task handle to restore.


Seventh chapter FreeRTOS list and list items

7.1 What are list and list items

7.1.1 List

Lists are used to track tasks in FreeRTOS, list related things in Files list.c and list.h

list_t

typedef struct XLIST
{
Listfirst_list_integrity_check_value (1)/*< Set to a known VALUE if Configuse_list_data_integrity_check_ BYTES is set to 1. */
Configlist_volatile ubasetype_t Uxnumberofitems; (2)
listitem_t * Configlist_volatile Pxindex; (3)/*< used to walk through the list. Points to the last item returned by a call to Listget_owner_of_next_entry (). */
Minilistitem_t Xlistend; (4)/*< List item that contains the maximum possible item value meaning it's always at the end Of the list and is therefore used as a marker. */
Listsecond_list_integrity_check_value (5)/*< Set to a known VALUE if Configuse_list_data_integrity_check _bytes is set to 1. */
} list_t;

(1) and (5) to check the completeness of the list
(2), Uxnumberofitems used to record the number of list items in a list
(3), Pxindex used to record the current list item index number, used to traverse the list.
(4), the last list item in the list to indicate the end of the list.


7.2 List and list item initialization

7.2.1 List Initialization

Vlistinitialise ()


void Vlistinitialise (list_t * const pxlist)
{
/* The list structure contains a list item which is used to mark the
End of the list. To initialise the list of the list end is inserted
As the only list entry. */
Pxlist->pxindex = (listitem_t *) & (Pxlist->xlistend);  /*lint!e826!e740 The mini list structure is used as the list end to save RAM. This is checked and valid. */

/* The list end value is the highest possible value in the list to
Ensure it remains at the end of the list. */
Pxlist->xlistend.xitemvalue = Portmax_delay;

/* The list end Next and previous pointers point to itself so we know
When the list is empty. */
Pxlist->xlistend.pxnext = (listitem_t *) & (Pxlist->xlistend);  /*lint!e826!e740 The mini list structure is used as the list end to save RAM. This is checked and valid. */
Pxlist->xlistend.pxprevious = (listitem_t *) & (pxlist->xlistend)/*lint!e826!e740 the Mini list structure  is used as the list end to save RAM. This is checked and valid. */

Pxlist->uxnumberofitems = (ubasetype_t) 0U;

/* Write known values into the list if
Configuse_list_data_integrity_check_bytes is set to 1. */
Listset_list_integrity_check_1_value (pxlist);
Listset_list_integrity_check_2_value (pxlist);
}



Eighth chapter FreeRTOS Task creation and scheduler open

Use of idle tasks:

1, to determine whether the system has a task to delete
2, run the user set idle task hook function
3, determine whether to open the low-power tickless mode, if it is open, you need to do the corresponding processing.


Nineth Chapter FreeRTOS Task switching

9.1 PENDSV Anomalies

PENDSV interrupts can be triggered by ICSR the interrupt control and status register to Bit28, which is the PENDSV hang position.

OS support Features

Authoritative Guide chapter 10th OS support features

Introduction to 10.1 OS support features

The shadow stack pointer. There are two stack pointers available, MSP for OS kernel and interrupt handling, and PSP for application tasks.

Systick timer. The simple timers located inside the processor make the same embedded OS available on a variety of cortex-m microcontrollers.

SVC and PENDSV exceptions. Both of these exceptions are important for operations in the embedded OS, such as the implementation of context switches.

Non-privileged execution level. You can use it to implement a basic security model that restricts access to certain application tasks. Separation of privileged and non-privileged levels
It can also be used together with the Memory Protection Unit (MPU) to further improve the robustness of the embedded system.

Exclusive access. Exclusive load and store directives are used for semaphore and mutex (mutex) operations in the OS.

A debug feature called Command Tracking Macro unit (ITM) is available for OS debugging in a variety of debugging tools.

10.2 Shadow Stack pointer

The main stack pointer (MSP) is the default stack pointer.

Process stack pointer (PSP)

For systems with embedded OS or RTOS, exception handling (including some OS cores) uses MSP, while application tasks use the PSP.

Each application task has its own stack space. The context switch code in the OS updates the PSP every time the context switches.


10.3 SVC exception



10.4 PENDSV Anomalies

PENDSV (a system call that can be suspended)

Taskyield ()

Execute system call

Task switch function Taskyield ()


9.2.2 System tick Timer (Systick) interrupt

Task switching also occurs in the FreeRTOS timer (SysTick) Interrupt service function.

9.3 PENDSV Interrupt Service function

#define Xportpendsvhandler Pendsv_handler

9.4 Finding the next task to run

Vtaskswitchcontext () to get the next task to run, which is to find the highest-priority task that is already ready.


9.6 FreeRTOS time Slice scheduling

Configuse_preemption
Configuse_time_slicing



Tenth chapter Rreertos System kernel control function



11th Chapter FreeRTOS Other Task API functions


12th Chapter FreeRTOS Time Management

Vtaskdelay () is a relative mode (relative delay function)

Vtaskdelayuntil () is absolute mode (absolute delay function)

Include_vtaskdelay


Prvaddcurrenttasktodelayedlist () is used to add the current task to the wait list.


13th Chapter FreeRTOS Queue

13.1 Introduction to Queues

1. Data storage

1. Multi-Task Access

The queue does not belong to a task specified by a task, and any task can send messages to the queue or extract messages from the queue.

2, the team blocked

When a task attempts to read a message from a queue, you can specify a blocking time, which is when the task reads from the queue
The time the task was blocked when the message was not valid.

The outbound is the message read from the queue, and the outbound blocking is for the task that reads the message from the queue.

When the blocking time is set to Portmax_delay, the task will remain in the blocking state until the data is received.


3, the queue blocked

It's like a squad jam.

4, the queue operation process diagram

Sending a message to a queue is a copy, so once the message is sent, the variable x can be used again, assigning another value.


13.2 Queue Structure



13.3 Queue Creation

1, Xqueuecreate ()

queuehandle_t xqueuecreate (ubasetype_t uxqueuelength,//Queue Length of the queue to be created, here is the number of items in the queue.
ubasetype_t uxitemsize)//The length of each item (message) in the queue, in bytes


Functions Xqueuesend (), Xqueuesendtoback (), and Xqueuesendtofront ()

All three functions are used to send messages to the queue, and three of the functions are essentially macros. Can only be used in task functions and cannot be used to interrupt services
function, the interrupt service function has a dedicated function that ends with "" "Fromisr"

Xqueuesend (
Queue handle
Point to the message to be sent
Blocking time
)


function Xqueueoverwrite ()

This function also sends data to the queue, and when the queue is full it will overwrite the old data, whether or not the old data has been
Task or interrupt take away.

Xqueueoverwrite (
Queue handle
Point to the message to be sent
)

4. Function Xqueuesendfromisr (), Xqueuesendtobackfromisr (), XQUEUESENDTOFROMISR ()


13.5 Queue lockout and unlocking

Prvlockqueue ()
Prvunlockqueue ()



14th Chapter FreeRTOS Signal Volume

Two-value signal volume

A binary semaphore is typically used for mutually exclusive access or synchronization, and a binary semaphore is very similar to a mutex semaphore, but there is still a subtle difference between mutually exclusive
The semaphore has a precedence inheritance mechanism, and the binary semaphore has no precedence inheritance. Therefore, the binary signal is more suitable for synchronization, and the mutex signal is suitable for
Simple, mutually exclusive access.

1, Vsemaphorecreatebinary ()

void Vsemaphorecreatebinary (semaphorehandle_t xsemaphore)

Xsemaphore: Save the two-value semaphore handle that was created successfully.


Release semaphore

function xsemaphoregive ()

basetype_t xsemaphoregive (Xsemaphore)

Xsemaphore: The semaphore handle to release

Get the semaphore

function Xsemaphoretake ()



taskhandle_t Starttask_handler;


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.