Common Microcontroller Programming Frameworks

What is a framework?

A program framework is similar to an outline or template for a document. Writing a program is akin to writing an article; without an outline or template, the process can be cumbersome.

Why have a framework?

To save time and reduce errors. For a specific type of program, the code structure and logic are often the same, with many similarities or commonalities. We can extract these common elements to form a fixed program framework, allowing us to reuse this framework when developing new programs of the same type.

This greatly enhances our development efficiency, and since this framework is publicly used and maintained, it makes the code logic less prone to errors.

Components of Embedded Systems

Embedded control systems are generally composed of “normal tasks” and “interrupt tasks”.

  • • Normal tasks: These refer to tasks that do not have high time response requirements or are executed periodically;

  • • Interrupt tasks: These refer to tasks that require immediate processing due to high time response requirements;

Common Frameworks

1. Polling without Interrupts

Description: All tasks are executed in sequence. To reduce the overall system response time, there are two methods:

  1. 1. Do not use blocking delay functions in tasks;

  2. 2. If a task cannot be completed in one go, it should be broken down into smaller tasks, executing one small task in a loop until the task is complete. Both methods will consume memory, and what could have been handled with local variables now requires static or global variables.

Common Microcontroller Programming Frameworks

Advantages: The program execution flow is simple and clear;

Disadvantages: Modifying system functions is very inconvenient, and if the number of tasks increases, it will affect the overall system response time, making the system feel sluggish;

Pseudocode Implementation:

int main(void)
{
    while(1)
    {
        doSomething_1(); // Task 1
        doSomething_2(); // Task 2
        doSomething_3(); // Task 3
        /* Other various tasks */
    }
    return 0;
}

2. Only Interrupts

Description: In a system with “only interrupts”, the main function’s loop does not perform any operations.

Advantages: Can respond in real-time to exceptional tasks (events).

Disadvantages: Interrupt resources are limited, and when there are too many tasks, responses may be delayed.

Pseudocode Implementation:

int main(void)
{
    while(1)
    {
        ;
    }
}

/* Interrupt Service Routine 1 */
void ISR1_IRQHandler(void)
{
    doSomething_1();
}

/* Interrupt Service Routine 2 */
void ISR2_IRQHandler(void)
{
    doSomething_2();
}

3. Variants of the Only Interrupt Framework

Description: Uses a state machine mechanism to execute tasks. The interrupt function sets the state of the state machine, while the main function’s loop executes different tasks based on the state value. This does not truly belong to the “only interrupts” form.

int main(void)
{
    while(1)
    {
        if(flag_1)
        {
            doSomething_1();
        }
        if(flag_2)
        {
            doSomething_2();
        }
        if(flag_3)
        {
            doSomething_3();
        }
        /* Other various tasks */
    }
    return 0;
}

/********* Interrupt Service Routine 1 ************/
void ISR1_IRQHandler(void)
{
    flag_1 = ~flag_1;
}

/********* Interrupt Service Routine 2 ************/
void ISR2_IRQHandler(void)
{
    flag_2 = ~flag_2;
}

/********* Interrupt Service Routine 3 ************/
void ISR3_IRQHandler(void)
{
    flag_3 = ~flag_3;
}

4. Polling with Interrupts

Description: Some periodic tasks are executed in the main function’s loop.

Common Microcontroller Programming Frameworks

Advantages: Efficient resource utilization, separating regular tasks from urgent tasks.

Disadvantages: The program structure and logic are relatively complex, requiring significant effort in task allocation and cooperation.

Pseudocode Implementation:

int main(void)
{
    while(1)
    {
        if(flag_1)
        {
            doSomething_1();
        }
        if(flag_2)
        {
            doSomething_2();
        }
        if(flag_3)
        {
            doSomething_3();
        }
        /* Other various tasks */
    }
    return 0;
}

/********* Timer Interrupt Service Routine ************/
void ISR1_IRQHandler(void)
{
    
}

5. Polling with Interrupts – Virtual Timer

Common Microcontroller Programming Frameworks

Description: Uses different “virtual timer” timing to call different tasks. When the timer’s timing expires, it executes a callback function or calls a task function.

This is very suitable for tasks that run periodically, while interrupts can handle external sudden events. This ensures real-time performance, but care should be taken not to use blocking delays. Generally, the time base for virtual timers is 1ms.

Advantages: The time intervals for tasks can be controlled relatively accurately, and the overall system’s real-time performance is quite good due to the use of interrupts.

Disadvantages: The execution time of tasks cannot be controlled, and if the execution time of a timed task is too long, it will affect the timing accuracy of the virtual timer.

Virtual timer implementation code:

https://codeload.github.com/0x1abin/MultiTimer/zip/master

6. Non-preemptive Real-Time Operating System

Common Microcontroller Programming Frameworks

Description: There are no priority distinctions between tasks; each task is executed sequentially. However, the execution time of tasks is strictly controlled by the operating system. Even if a task is not completed, it will be suspended when the time slice expires.

Advantages: There is no need to worry about reducing delays in tasks; we can focus our efforts on business logic.

Disadvantages: Tasks are on the same level, which can lead to some tasks not receiving urgent processing.

Pseudocode implementation:

https://blog.csdn.net/twx11213030422/article/details/104637273

7. Preemptive Real-Time Operating System

Description: Each task is in a “dead loop” and has a priority. High-priority tasks can interrupt low-priority tasks, similar to interrupts. This provides excellent real-time performance, and each task is also controlled by time slices, meaning their execution times are predictable. It also supports interrupts to respond to urgent events.

Common Microcontroller Programming Frameworks

Advantages: Real-time response; engineers can focus on implementing business logic.

Disadvantages: Requires porting and has certain hardware resource requirements for microcontrollers.

Commonly used preemptive real-time operating systems: Keil RTX, FreeRTOS, uCosII/III, etc.

Leave a Comment