From 0 begin to learn FreeRTOS-1

Under We know that (single-core) microcontroller can only do one thing at a time, it will result in a waste of microcontroller resources, but there may be not enough time to respond, so, in real-time is relatively large program or requirements are relatively high, we can be ported operating system. Because in this case the operating system a lot easier than the bare metal, high efficiency. Below, JJ will take you into the world of FreeRTOS casual look.

Here started this article.

Before we did not use the operating system, run the microcontroller is performed sequentially, that is, very often, the microcontroller in the implementation of this matter, can not switch to another thing. This has resulted in a waste of resources, and missed the signal bursts. So, spend time operating system, it is easy to avoid such problems.

Very simple, from the feeling, like a single chip at the same time more or thing, why like it, because the MCU execution speed quickly approaching we simply can not feel it, but at the same time it is impossible to do two things, in the (single-core) microcontroller, because its hardware structure determines the CPU can only do one thing at a time, such as:
not os

As this chart, in order to perform all of these things, assuming that each task (event) of time infinitely small, as small as we simply can not tell the difference, then we will feel the microcontroller at the same time do these six things.

The truth is this: as if all tasks are executed, but in fact only one task to perform at any one time

If so, then add the interrupt system, it can be understood as the figure below:
the

The program is usually divided into two parts: the front desk systems and back-office systems. Simple little system is usually front and back office systems, such programs include an infinite loop and several interrupt service routine: The application is an infinite loop that calls the API function to complete the desired action, this is called a cycle back-end systems. Interrupt service routine for asynchronous event handling system, which is the reception system. Reception is interrupt level, the background is the task level. In simple terms it is that the program has been executed in order to do an interrupt to the interrupt (foreground) things. Things processed interrupt (foreground), it is back to the big cycle (background) in order to continue.

So the question is, with such a system is certainly not a good system, I do want to do the first task when the fourth task, simply can not do ah, in fact, can do, let the program execution pointer cp points the fourth task on the list. But once complex task, then the structure, portability, and readability of the code of the whole project, it will certainly be poor.

 FreeRTOS

Then the operating system is an integral part of the transplant. What is RTOS? : Real Time OS, real-time operating system, the emphasis is real-time, is to specify what time do the task. So if the same time, we need to perform two or more tasks how to do. Then we can artificially task prioritization, which is an important task, to do first, because the front has always stressed that the microcontroller can not do two things at the same time, at a certain moment can only do one thing.

So FreeRTOS is how to operate it? FreeRTOS kernel look at it:

FreeRTOS is a cut, preemptive multitasking kernel, and does not limit the number of tasks. FreeRTOS provides all the functions required for real-time operating systems, including resource management, synchronization, communications and other tasks. FreeRTOS is written in C and assembly, most of which are written in C, and only a handful of closely associated with the processor part of the code is written in assembler, FreeRTOS simple structure, very readable ! RTOS kernel is responsible for managing all the tasks, the kernel determines which task to run, when to stop the current task switch to another task, this is a multi-task management capabilities kernel.

Preemptive kernel name suggests is to deprive the right to use CPU for other tasks, it always runs the highest priority task that is ready tasks.
FreeRTOS

In FreeRTOS, each task is an infinite loop, in general, the task will not be the end of the run, are not allowed to return to duty, the task structure are generally

While(1)
{
/****一直在循环执行*****/
}

If you do this task, and then delete it.

Tutorial transplant I do not write, super simple, in accordance with existing tutorials to do a lot on the line. (If you do not have resources, you can find me in the background, I give a transplant tutorial / source)

In fact, the use of FreeRTOS and simple, successful transplantation accordance with their wishes can be configured, and there are a lot of manual FreeRTOS, although of poor English, but I have Google translate! ! ! Ha ha ha

Since it has been said that the task task, it must have the task ah, create a task:

// task. h  task.c
BaseType_t xTaskCreate(      TaskFunction_t pvTaskCode,
                              const char * const pcName,
                              uint16_t usStackDepth,
                              void *pvParameters,
                              UBaseType_t uxPriority,
                              TaskHandle_t *pvCreatedTask
                          );

Function prototype has, as understood literally

TaskFunction_t pvTaskCode        //传递进来的是任务函数
const char * const pcName         //传递进来的是任务Name
uint16_t usStackDepth            //传入的是堆栈的大小

Here to explain, in the development of bare metal, we regardless of local variables or global variables, anyway, defines can be used, when an interrupt occurs, the function returns the address of where to send, we do not care. But in the operating system, we have to figure out how our parameters are stored, their size is how much we need to define the size of the stack. It is used to store these things we are. It is too small, resulting in a stack overflow exception occurs. (Inside the microcontroller RAM stack is a contiguous memory space)

Because in a multitasking system, each task is independent, non-interfering, so be assigned a separate stack space for each task.

void *pvParameters              //传递给任务函数的参数
UBaseType_t uxPriority          //任务优先级
TaskHandle_t *pvCreatedTask     //任务句柄

Task handle is also very important things, how we delete the task is to use task handle, in fact, plainly, my operating system how do you know what your mission, must rely on judgment task handle before they know which tasks to perform, which tasks It is suspended. The next task which is to be executed and so on are task handle.

Then to use these things, we are sure to achieve it, is to achieve defined below, to define priorities, stack size, handle tasks, functions and other tasks.

//任务优先级
#define LED_TASK_PRIO           2
//任务堆栈大小     
#define LED_STK_SIZE             50
//任务句柄
TaskHandle_t LED_Task_Handler;
//任务函数
void LED_Task(void *pvParameters);

After you create a task, you can open the task scheduler, and then the system will start running.

xTaskCreate((TaskFunction_t )LED_Task,    //任务函数
            (const char*    )"led_task",   //任务名称
            (uint16_t       )LED_STK_SIZE, //任务堆栈大小
            (void*          )NULL, //传递给任务函数的参数
            (UBaseType_t    )START_TASK_PRIO, //任务优先级
            (TaskHandle_t*  )&LED_Task_Handler);//任务句柄 
 vTaskStartScheduler();          //开启任务调度

XTaskCreate this function to create a task that returns a value, the type of the return value is BaseType_t.

We look at the description:

// @return pdPASS if the task was successfully created and added to a readylist, otherwise an error code defined in the file projdefs.h

In fact, we can determine the scheduling time about the return value is pdPASS create tasks so they know whether to build success. And a print information as debugging. Because the use of these latter when the semaphore should know whether the semaphore is successfully created, making the code robust number. That nothing be hidden bug.

Then is the concrete realization of our mission in doing what LED_Task

Of course, you can implement multiple tasks. It is very simple.

//LED任务函数
void LED_Task(void *pvParameters)
{
    while(1)
    {
        LED0  =  !LED0;
        vTaskDelay(1000);
    }
}

This is a simple overview of the operating system.

Next, the specific process should be open about the task scheduling and task switching.

This can refer to the book wildfire "teach you to write uCOS-III from 0-1"

I welcome public attention No.

For more information please concern "Things IoT development," the public number!

Guess you like

Origin www.cnblogs.com/iot-dev/p/11680785.html