Download ST7 USER GUIDE
Transcript
2 - How does a typical microcontroller work? 2.6 AN APPLICATION USING INTERRUPTS: A MULTITASKING KERNEL The conclusion in the previous paragraph states that interrupts are well-suited for a certain class of events to be processed. However, there are other cases outside this category. Some of them are better addressed by the concept of multitasking. In many applications, several processings are required that do not match the specificity of the interrupt-driven processes. For example: ■ Two or more processes are continuously active, that each take long processing times. ■ These processes are not (or not directly) started by external events. ■ They do not require a quick reaction time. In such cases, the interrupt concept is obviously inappropriate. Actually, these processes seem to require each a core of their own. However, considerations of cost may not allow for multiple microcontrollers on the same board. The concept of multitasking is the answer to this requirement. It is a software solution that does not require extra components, and makes the system believe the various tasks run on different cores, although it is simply the same core that is shared between all the tasks at the expense of the computing power that is shared between this tasks, plus a certain waste produced by the specific mechanisms that provide for the multitasking. This waste limits the frequency at which the tasks can be switched; if they are switched too often, the proportion of the time taken to switch tasks becomes too large, and the corresponding part of the microcontroller computing power is lost. The designer must check whether the power remaining for each task is sufficient or not; if not, the type of microcontroller is probably unsuitable for the project. There are two kinds of multitasking, namely pre-emptive multitasking and non pre-emptive multitasking. The second kind is also called cooperative multitasking. 2.6.1 Pre-emptive multitasking Pre-emptive means that the computing power that is allocated to a task is withdrawn from it without notice, that is, that particular task is stopped at an unexpected place by brute force. Then, the power is allocated to another task, until it is stopped in turn, and so on for all the tasks; then, the first task that is currently sleeping regains control and continues for some time. The task switching is done under control of an interrupt triggered by a timer. This allows the core time to be partitioned at will between the various tasks. The time for which each task is allowed to run may be the same for all tasks; or it may be decided to allow more time for some more important (or time-consuming) tasks, and less for others. In a word, the multitasking kernel may fine-tune the resource sharing between the tasks. The main drawback of this system is that since the tasks are interrupted at any place in the code, many precautions must be taken to ensure the coherence of the data, just as explained above about interrupts. In fact, if a task starts to write a piece of data and is put asleep in the 39/315