5934 views|49 replies

5213

Posts

239

Resources
The OP
 

The first FreeRTOS check-in station is open: Application scenario station, closing time is August 14 [Copy link]

 

Activity Overview: Click here to view (including activity encouragement and activity learning content)

Check-in start and end time for this site: August 12-August 14 (3 days)

Check-in tasks:

1. Read Cruelfox's first article: FreeRTOS Learning Notes (1) Application Scenarios

2. Please imagine a specific application that is suitable for implementation with a single-chip microcomputer, describe the software components that can become "tasks", and analyze what benefits can be achieved by introducing a real-time operating system into the software of this application.

FreeRTOS Learning Notes (1) Application Scenarios (Author: cruelfox)

The following content is copied and pasted from cruelfox's " FreeRTOS Learning Notes (1) Application Scenarios ". For your convenience, I copied it directly. Although it is the first article, the whole article is concise and requires you to read it carefully.

I was forced to take a look at FreeRTOS during the NXP KW41 competition. I later planned to systematically learn it and try to apply it to my own DIY projects. FreeRTOS is just one of many RTOS (literally meaning real-time operating system). Because it is widely used and open source, it is a good choice for learning. I have roughly read its documentation, and now I am going back to sort it out and study the implementation details of some of them while writing this series.

When can FreeRTOS be used?


  Do microcontrollers also need operating systems? If we use Windows, Linux, BSD, or even DOS, which are used in daily life, to represent operating systems, it is a very absurd idea to run operating systems on microcontrollers - because for most microcontrollers, RAM is too little. FreeRTOS is not intended to provide a platform for running software on microcontrollers, installing software one by one on it, and allowing users to choose what to run. It has no user interface; it is not a housekeeper, nor does it have any hardware drivers, nor does it provide file system services. FreeRTOS is just an operating system kernel, which first provides the most important feature of the operating system: task scheduling.

  In other words, with FreeRTOS, it will be easier to implement multitasking on microcontrollers. There are at least two meanings here. One is whether multitasking must be implemented with RTOS? Of course not. For microcontroller development, all system resources are yours, and it is not difficult to handle different tasks in different interrupt services. Second, is there no need for RTOS without multitasking? This also depends on the specific situation, how to define the concept of "task", and a complex thing may also be divided into several tasks in the program to handle.

  Let's give a few examples.

1. SD card MP3 music player
Let’s ignore the user interface for now. In the playback state, the player works like this from the data flow (somewhat simplified):

I2S interface DMA buffer is idle
Fill the buffer with remaining PCM data, request to decode the next block of MP3 data
Request to read the next section of the MP3 file
Locate the sector position to be read on the SD card
SDIO controller sends read command
SDIO controller receives data and completes interrupt
Fill the file data buffer
Decode MP3 data and write PCM data buffer
Fill the DMA buffer of the I2S interface

  This process involves four key software parts:


  If the software design idea is from top to bottom, the I2S device driver is awakened at a fixed beat to fill the buffer with PCM data, so it is necessary to call the MP3 decoding program regularly. The MP3 decoder decides whether to access the file system based on the result of the previous decoding operation (because the amount of PCM audio data generated by MP3 decoding a block of data cannot be exactly the size requested by the I2S device driver), and how many bytes of MP3 file content need to be read, and the decoded data that is not used temporarily should also be saved for next time. When it comes to the file system, the location and length of the file requested to be read may not be exactly a sector on the SD card, so there is also a cache, and it is also necessary to track the index of the file on the SD card. The SDIO device driver reads the SD card according to the request of the file system, and returns after waiting for the operation to complete. Note that it is different from the previous module in that there is a hardware IO waiting time in which nothing is done.
  The nested call relationship is as follows:

  Note that each function call represents a complete operation: filling a buffer, decoding a piece of MP3 data, reading a piece of file, and reading a piece of data from the SD card. The subroutine at the lower level is called, and after completion, it returns to the program at the upper level to continue execution. In the absence of an exception (interrupt), the program will not leave this nested call relationship.
  It should also be noted that although the main functions listed above are complete in one call operation, their internal states may not be the same after each operation. In C language, these functions need to have static local variables, or use some global variables to remember what the state was after the last call. A typical example is the file system read_file(). After such a function is called, it needs to remember the position of the file pointer so that it can continue reading next time.
  The music player implementation described above has an important disadvantage: during the waiting time of the SD card read operation, the CPU can only stay in the SD card access function and cannot be used for MP3 decoding operations, so the processing power is wasted. During the time when the buffer of the I2S device is full and needs to be refilled next time, the CPU is also in an idle state. In fact, these two idle times can be used to do other things, such as pre-decoding part of the MP3 data. So the question is, how to jump out of a function to execute other functions and then jump back during the execution of a function? Use interrupts, yes, but what to put in the interrupt?

2. USB mass storage device
  It is to use the USB on-chip device of the microcontroller to simulate a U disk. The main content of making such a device is to respond to various requests from the USB host. When the USB host sends a request to the device, the USB hardware will generate an IRQ, and then the USB interrupt service function (IRQ handler) will be executed. Usually, the USB driver library provides some callback function interfaces, which are functions written by the user for the USB IRQ handler to call when needed.
  As a USB mass storage device, the callback functions that need to be provided must have functions such as reading storage devices and writing storage devices to generate actual disk data for the USB and receive disk data required by the USB to be written. When these functions are called, the application cannot foresee because they are in the response process after the USB interrupt occurs. In this way, the callback function handles the transactions simulated by the USB disk, and the main program does not need to care about it, and can handle other unrelated tasks, thus achieving multitasking!
  However, executing user code in an interrupt environment does not seem to be a good choice, because interrupts with the same or lower priority cannot be responded to at this time. In actual USB mass storage devices, the data of the USB disk needs to be read from an off-chip storage device, such as NAND Flash, and the situation will be bad - reading data requires waiting, and this waiting occurs entirely in the USB IRQ handler, and the USB host's request cannot be responded to for the time being. If the tasks in the main program also require hardware I/O interrupts, it may be affected.
  Well, for an application that only simulates a USB flash drive and does not perform other tasks, this does not seem to matter: it is all waiting anyway. The performance of the USB flash drive device under USB full speed (12Mbps) that I have personally tested, the transmission speed is up to more than 500 kB, which is much worse than 12Mbps. Why?

  Looking at the figure above, there is no data transmission during the USB IRQ interrupt response, so part of the 12Mbps effective bandwidth is wasted, and the throughput of the USB flash drive naturally cannot reach the theoretical value. When the callback function is waiting for I/O, the USB host is also waiting for the response of the USB device. After the USB flash drive data is ready, the USB hardware sends the data, and the CPU has nothing to do until the next request arrives. A more reasonable design is to use the USB TX time to pre-read the I/O of the actual storage device, that is, guess that the USB will continue to request to read the data after the last read, then read the data into the memory first. Once the guess is correct, the response time of the next request can be shortened, and the read throughput of the simulated USB flash drive is improved. To achieve this, you cannot simply handle I/O tasks in the callback function.

3. Storage management of data recording devices
  An application needs to perform the following operations on a remote device (connected via UART): ReadChipID, ReadStatus, ReadData, EraseData, and WriteData to achieve data management. In terms of specific implementation, these operations all send command data in a pre-defined format via UART, and then parse the returned data content received by UART to determine whether the operation is successful and receive valid data.
  If we focus on sending commands via UART, the structure of the program may be as follows:

  Such a design assumes that the remote device will send out a response as expected, but has almost no fault tolerance for the UART interaction process. Once an unexpected event occurs during the interaction, it is impossible to recover from the error state and can only start over.
  Once the possible exceptions in UART communication are taken into account, the writing of the program becomes less straightforward. One way is to write the function of parsing the data received by UART into a state machine, and determine the state of program execution based on whether the data conforms to the predetermined format and the identification of the returned data. When certain states are reached, commands are sent from UART. However, it is difficult to see the intention of program execution from the perspective of code reading. The operation process and communication error correction are mixed together, which is not conducive to code maintenance.
  How to integrate communication status identification and error handling mechanisms while keeping the program flow intuitive? According to general thinking, this is a single-task (thread) program, and the program is executed sequentially without the need for interrupts. The program is operating according to a process, but it also has to handle UART communication anomalies that are not closely related to the process. These two tasks are staggered in time. What if it is assumed that these are two tasks in progress, but they are switching between each other in a predictable way? It seems that this makes simple things complicated again...

  The above three examples may all use "task scheduling" to achieve more effective runtime allocation. In my opinion, FreeRTOS provides a new way of organizing programs - tasks, which divides complex things into independent small blocks and writes them separately; at the same time, it provides some mechanisms for these small blocks to work together to achieve the overall purpose. FreeRTOS tasks are written in C language functions, which provide a feature that allows several functions to appear to be executed "simultaneously". Of course, when there is only one CPU, they are actually executed in turns, but this is also a major difference from ordinary C language programs.
  We know that C language functions can be nested and recursively called. For example, funcA() calls funcB(), and then funcB() calls funcC(), or even funcC() calls funcA(). However, funcA() must wait until funcB() returns before it can continue to execute the content behind it. When funcB() calls funcC(), it also waits for funcC() to complete before returning control.

  For FreeRTOS tasks, this is not the case. TaskA() can actively or passively hand over control during execution. TaskB() can get control at this time, but it does not start from the beginning, but continues to execute from the place where it handed over control last time. At some point, taskB() hands over control, and the system chooses to execute taskC(). Then, when taskC() hands over control, taskA() resumes execution.


  The above diagram is a little simplified. In fact, when jumping out of a task function and continuing to execute in another task function, a section of FreeRTOS kernel code must be executed in the middle. In other words, the kernel is responsible for scheduling which task to execute next, how to suspend the current task, and how to resume another task.

  Does multi-tasking have to be implemented with a certain RTOS? Not really. Using RTOS will help, but the resource overhead will be a little higher. For example, using interrupts to switch tasks, as mentioned above, can already implement simple multi-tasking. For another example, decompose a task into multiple steps, each step corresponds to a function, and then choose which step of which task to execute each time in a large loop. This kind of multi-tasking is non-preemptive, while multi-tasking implemented with interrupts is preemptive.
  Please note that regardless of whether a certain RTOS is used to implement it, the characteristic of multi-tasking operation is that as long as each task is not completed, its state (including private data) must be completely saved. Once the function representing the task (such as an interrupt service routine or a step of a task) returns, the scope of the local variable disappears and cannot be used to save the state. Therefore, the task state must be saved using either global variables, static local variables, or dynamically allocated storage. The FreeRTOS method is to suspend the execution of the task function without returning. Multiple such functions without returning can exist at the same time, and then any one of them can be selected to resume execution. How to implement this feature? Please listen to my analysis in the next article.

After reading, you are welcome to communicate with this post and reply to the questions: Please imagine a specific application suitable for implementation by a single-chip microcomputer, describe the software components that can become "tasks", and analyze what benefits can be brought by introducing a real-time operating system into the software of this application.

This post is from MCU
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle

Latest reply

Transplanting freeRTOS into an application, task division and its scheduling configuration are difficult! It is difficult to find other people's projects to look at and learn from.  Details Published on 2021-12-29 15:58
 

1w

Posts

16

Resources
2
 

I think the operating system is only used in concurrent programs and when data is not lost. For example, keystrokes require real-time response. Serial communication requires no data loss. The difficulty now is that large programs are too large to be used at all. Because a project has a time requirement. If only one or two functions are turned into processes, there is a fear of errors in the overall code.

This post is from MCU

Comments

nmg
So, have you thought of any examples suitable for introducing an operating system? Welcome to post and exchange ~ Check-in task: "Please imagine a specific application suitable for implementation by a single-chip microcomputer, describe the software components that can become a "task", and analyze if the software in this application  Details Published on 2020-8-12 08:48
 
 

73

Posts

0

Resources
3
 

The benefit of introducing an operating system into a microcontroller is that it makes full use of the microcontroller's idle time. Synchronization and process switching are achieved through task switching. For example, sensor data collection can be divided into multiple tasks such as data collection, command issuance, data transmission, and data maintenance. If no operation command is received or it is not within the transmission period, there is no need to process other things.

This post is from MCU

Comments

nmg
The system arranges the MCU clearly, and you are welcome to give an example, follow & communicate the punch-in tasks of this site. "Please imagine a specific application suitable for MCU implementation, and describe the software that can become a "task" in it.  Details Published on 2020-8-12 08:46
 
 
 

1972

Posts

0

Resources
4
 

After studying RTOS for a while, I understand the principles of task scheduling, message queues, and memory management. I have no problem running a simple task. However, once I apply it to a project, I often don’t know how to plan tasks to implement the functions under the requirements of complex functions. Most of the learning materials on the Internet are principle-oriented, and there are few basic RTOS application designs. I have no motivation to continue without applying it to the project.

This post is from MCU

Comments

Because the principle is simple and easy to explain. The real difficulty is task division. If you don't know how to divide, it is very likely that the efficiency will be far less than that of bare metal programs. This article always feels that it has gone off track from the example.  Details Published on 2020-8-24 14:08
 
 
 

5213

Posts

239

Resources
5
 
symic posted on 2020-8-12 08:27 The benefit of introducing an operating system into a microcontroller is to make full use of the idle time of the microcontroller. Synchronization and process switching are achieved through task switching. For example, sensor data...

The system arranges the microcontroller clearly, and it works.

You are also welcome to give examples , follow and discuss the check-in tasks on this site.

"Please imagine a specific application that is suitable for implementation with a microcontroller, describe the software components that can become "tasks", and analyze what benefits can be achieved by introducing a real-time operating system into the software of this application."

This post is from MCU
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle
 
 
 

5213

Posts

239

Resources
6
 
ddllxxrr posted on 2020-8-12 07:33 I think the operating system is only used in concurrent programs and without data loss. For example, keystrokes require real-time response. And serial port communication requires no data loss. And now...

So, have you thought of any examples suitable for introducing operating systems? Welcome to post and exchange ~

Check-in tasks:

"Please imagine a specific application that is suitable for implementation with a microcontroller, describe the software components that can become "tasks", and analyze what benefits can be achieved by introducing a real-time operating system into the software of this application."

This post is from MCU
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle
 
 
 

60

Posts

1

Resources
7
 

Prepare to make a signal record and record the voltage and temperature in the SD card

This post is from MCU
 
 
 

3

Posts

0

Resources
8
 

A simple motor control project. I don't know if it is reasonable to divide the tasks. The tasks can be divided into the following tasks: current acquisition task, speed acquisition task, PID control output task, motor status printing task, and instruction receiving task. The advantage of task switching is that it not only guarantees the performance of the system, but also each task does not interfere with each other and can be fully executed.

This post is from MCU
 
 
 

74

Posts

0

Resources
9
 

Take out the stm32 development board in your hand to learn freertos. First, make the online upgrade function into a task, and then do a data collection

This post is from MCU

Comments

nmg
I also have my own study plan, which is great. I look forward to communicating with you about your progress at any time. I guess many people have stm32 boards. I hope this activity can help you complete your plan.  Details Published on 2020-8-12 14:49
 
 
 

282

Posts

2

Resources
10
 

If you make an indoor environment monitor, you can use a real-time operating system, in which the data of each sensor can be treated as a single task, and display, text message sending, etc. can be divided into separate tasks. This makes it convenient for each task to process its own data, with clear levels and maximum utilization of the microcontroller's resources.

This post is from MCU
 
 
 

6

Posts

0

Resources
11
 

There happens to be a project that needs to use freertos, so let's learn it in advance

This post is from MCU

Comments

nmg
You can think about the project architecture in advance according to the questions asked by the original poster. If you can, you can follow the thread to communicate.  Details Published on 2020-8-12 14:48
 
 
 

24

Posts

0

Resources
12
 

In the greenhouse environment control system, the single-chip microcomputer not only collects sensor data such as air temperature, air humidity, soil humidity, carbon dioxide concentration, oxygen concentration, light intensity, but also processes, stores and analyzes the data, and then calls the pre-stored crop life cycle data, and finally controls the exhaust fan, watering valve, and shading roller blind. Report the crop status to the terminal. If the process encounters multiple layers of interruption, the execution will be slow and the waiting time will be too long. If a real-time operating system is used, multiple tasks can be performed simultaneously. For example, after collecting data, data analysis and reporting are performed simultaneously, and each task and reporting are performed simultaneously. Shorten the execution time.

This post is from MCU
 
 
 

33

Posts

0

Resources
13
 

I want to use the nucleo-f334r8 development version of stm32 to make a communication relay function. I want to send the data graphs of spi and i2c to the pc through uart. I haven't figured out how to plan the tasks to achieve these functions. I'll probably count each function as a task to see what rtos can do.

This post is from MCU
 
 
 

7422

Posts

2

Resources
14
 

The top floor does write a lot, but it basically misses the point. If you only talk about tactics without talking about strategy, you will not find an application scenario.

This post is from MCU

Comments

nmg
Welcome big shrimps to share and exchange, and send benefits to small shrimps~  Details Published on 2020-8-12 14:47
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

5213

Posts

239

Resources
15
 
freebsder posted on 2020-8-12 13:46 The top floor did write a lot, but basically didn't get to the point. Only talking about tactics without talking about strategy will not find an application scenario.

Welcome big shrimps to share and exchange, and send benefits to small shrimps~

This post is from MCU
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle

Comments

We'll talk about it when we have the chance. Giving out benefits requires stimulation, hahaha.  Details Published on 2020-8-13 08:52
 
 
 

5213

Posts

239

Resources
16
 
Rain, Published on 2020-8-12 11:36 I just happened to have a project that needed to use freertos, so I learned it in advance

You can think about the project architecture in advance according to the questions asked by the original poster. If you can, you can follow the thread to communicate.

This post is from MCU
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle
 
 
 

5213

Posts

239

Resources
17
 
Moonlight Gale published on 2020-8-12 10:28 Take out the stm32 development board in your hand to learn freertos, first make the online upgrade function into a task, and then do a data collection

I also have my own study plan, which is great. I look forward to communicating with you about my progress at any time.

I guess many people have stm32 boards. I hope this activity can help you complete your plan.

This post is from MCU
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle
 
 
 

375

Posts

10

Resources
18
 
In the process of interaction between multi-state buttons (single-click, long-press, double-click, etc.), GUI, and communication, using RTOS can reduce the delay of all parties
This post is from MCU
 
 
 

10

Posts

0

Resources
19
 

A protocol conversion board, multiple serial ports on the board can communicate with each other. If an operating system is applied, the configuration and debugging of the priority of each serial port can be omitted, and more attention can be paid to the function itself. The communication between each serial port can be a task

This post is from MCU
 
 
 

419

Posts

1

Resources
20
 

Thinking from the design of a simple oscilloscope, in my opinion, the oscilloscope can be divided into three software-related modules:

1. Real-time data sampling: the oscilloscope needs to fully collect data according to the set sampling accuracy and storage depth, and has high real-time requirements;

2. Data storage and processing requires sufficient space to store data and be able to process signals as needed, ensuring that the data processing speed and storage speed keep up with the sampling speed to avoid data loss or even confusion;

3. Data reproduction, the reproduction terminal can be a screen or a host computer, and the data refresh time cannot be too slow;

Other considerations include:

The signal normal display and trigger functions can be designed into two different working modes;

If you are considering a multi-channel oscilloscope, you also need to ensure that the sampling data between channels is parallel and does not interfere with each other;

If you want to take data away through media such as USB flash drives, serial ports, or network ports, there are file operation requirements;

In addition, issues such as internal self-check during the operation of the whole machine, whether the collected data needs to be compensated, and how to deal with data storage failures are also issues that need to be considered during each cycle operation.

In terms of functions, the MCU should not be involved in things that can be completed by hardware, leaving precious computing resources to participate in data processing and task scheduling; for example, data storage and processing are performed in the reading and writing gaps of hardware acquisition and display, which requires accurate time measurement; the above functions can be achieved without using a real-time system, but the disadvantages are: 1. It is not conducive to maintenance and the code is complex; 2. It is not conducive to improvement; if new functions are added, the program architecture needs to be reconsidered; 3. Poor portability; the delay caused by different platforms and even different components will affect the original timing arrangement.

This post is from MCU
 
Personal signature君应有语,渺万里层云,千山暮雪,知向谁边?
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list