In embedded design, how to choose the underlying software development framework?

Transfer from | Habayashi-kun

In the writing of the underlying code, the initial framework design will always face choices. For the actual hardware use environment, you have many choices for the software framework to be used. Today I will briefly describe some of the more commonly used architectures so that everyone can understand and choose the right one. Architecture.

Overview

1. Simple sequential execution of the program : This type of writing is the method used by most people. You don't need to think about the specific structure of the program, just write the application directly in the order of execution.

2. Front-end and back-end execution program: Add an interrupt front-end processing mechanism in the case of sequential execution, configure a large-scale back-end loop program executed in sequence, and combine them into a program that can respond in real time.

3. Time slice round-robin method: On the front-end and back-end execution architecture, the program is further planned through counters and specific segments are executed regularly.

4.  Real-time operating system: Real-time operating system is also called RTOS, real-time. The kernel of RTOS is responsible for managing all tasks. The kernel determines which task to run and when to stop the current task and switch to other tasks. This is the multitasking management capability of the kernel. . Multitasking management feels as if the chip has multiple CPUs. Multitasking management maximizes the use of CPU resources. Multitasking management helps to realize the modular development of programs and realize complex real-time applications.

In addition to real-time performance, there is also a deprivable kernel. As the name suggests, it can deprive other tasks of the CPU usage rights. It always runs the task with the highest priority among the ready tasks.

1. Simple sequence execution program

    This kind of application is relatively simple, generally used as an elementary simple, real-time and not too demanding, it can be used. The design of the program is relatively simple and the thinking is relatively clear. But when the logic of the main loop is more complicated, it is difficult for others to understand the logic of the program without a complete flow chart.

Write a program model for sequential execution below

int main(void) 
{
    uint8 TaskValue;     
    InitSys();                  // 初始化    
    while (1)     
    {
      TaskValue= GetTaskValue();         
      switch (TaskValue)        
      {             
        case x: 
          TaskDispStatus(); 
        break;             
        ...             
        default:
        break;         
      }     
   } 
}

2. Execute the program before and after

    The feature of this program is that the default program is always executed in the background large loop, the interrupt service program (ISR) generates the corresponding interrupt mark, and the main program runs the task program associated with the interrupt mark. The general implementation has the following ideas:

    By setting the flag variable, and then setting or resetting the flag variable when the foreground responds to the interrupt, the signal acquisition of the event is realized, and then the transaction or data corresponding to the interrupt is processed in the background main loop, and the program flow is transferred to the main program .

Front-end and back-end programs

void IRQHandler(void)
{
  if(GetITStatus == 1)
  {
    SysFlag = 1;
    GetITStatus = 0;
  }
}
int main(void) 
{
    uint8 TaskValue;     
    InitSys();                  // 初始化    
    while (1)     
    {
      TaskValue= GetTaskValue();         
      switch (TaskValue)        
      {             
        case x: 
          if(SysFlag == 1)
          {
            TaskDispStatus(); 
            SysFlag == 0;
           }
        break;             
        ...             
        default:
        break;         
      }     
   } 
}

3. Time slice round-robin architecture

Time slice round-robin method, when you see it, you usually compare it with the operating system. It's not that the operating system includes this method, but the front-end and back-end programs cooperate with time management to form a time slice round-robin architecture.

This architecture has been as close as possible to RTOS, time management, interrupt management, and task management, but RTOS will make more in-depth modifications to the kernel, including thread switching for delay delay, preemptive task switching, etc. For more complicated functions, etc.

Time slice round robin

Time slice management is mainly through multiplexing the timing, counting in the timer, and changing the flag at the timing, and then the main program judges the true or false of the flag to realize the execution of different task states at different times.

Because this architecture code is better, I will describe it in detail.

step

1: Initialize the corresponding timer: pay attention to setting the interval frequency of the timer, which can be set according to the performance of the chip. For example, set the timing interrupt to 1ms, or it can be set to 10ms. The timer part in the round-robin architecture has the same function as the timer part of the operating system. The interruption is too frequent and affects the efficiency of the main program execution; the interrupt interval is too long , Real-time response is poor.

2: Set a function structure flag for the task run by the timer for time counting and flag operation in the timing program.

#define TaskTAB_NUM  6 //任务数量
__packed typedef struct{
  u8 flag;  //定时标志
  u32 numcount;//按照定时中断进行计数
  u32 target;  //设置的定时目标数值
  int(*fun)(void);//设置定时执行的目标任务函数
}TaskTimTypeDef

step

    3: Establish a task table, and determine the timetable for task execution through the setting of the structure table.

When defining variables, we have initialized the values. The initialization of these values ​​is very important, and it has something to do with the specific execution time priority. This needs to be mastered by yourself.

/*MdmSendTimTab任务函数默认周期,单位5ms,TIM7*/
static TaskTimTypeDef TaskTimTab[TaskTAB_NUM] =
{
  {1, 0, 30000,      *Task00},          //Task00 3000数值是设置的定时目标值,如果觉得反应过慢,可以将此值设置小
  {1, 0, 3000,       *Task01},          //Task01
  {1, 0, 300,        *Task02},          //Task02
  {1, 0, 30,         *Task03},          //Task03
  {1, 0, 3,          *Task04},          //Task04
  {1, 0, 0xFFFFFFFF, *Task05},          //Task05  
  //可以按照TaskTAB_NUM数量添加任务
};
int Task00(void)//按照结构体的函数模板(int(*fun)(void);)写任务函数
{...}//假设执行按键操作
int Task01(void)
{...}//假设执行USART发送任务
int Task02(void)
{...}//假设执行CAN通讯
int Task03(void)
{...}//假设执行继电器控制
int Task04(void)
{...}//假设执行网络解析
int Task06(void)
{...}//假设执行空

step

    4: Timing interrupt service function, timing according to the time we need and flag operation.

 //定时中断服务函数
 void TimerInterrupt(void)
 {
    for(char i=0; i<TaskTAB_NUM; i++)
    {
      if(TaskTimTab[i].flag == 1)
      {
        (TaskTimTab[i].numcount< TaskTimTab[i].target)//比较目前定时计数与目标时间
        (TaskTimTab[i].numcount++):(TaskTimTab[i].flag = 0);
      }
    }
  }

step

    5: The main function performs task function execution.

int main(void) 
{      
  InitSys();                  // 初始化     
  while (1)     
  {
      for(char i=0; i<TaskTAB_NUM; i++) 任务处理    
      {
        if(TaskTimTab[i].flag == 0)
        {
          if(TaskTimTab.flag == 0)
          {
            TaskTimTab[i].flag  = 1;
            TaskTimTab[i].numcount= 0;
            TaskTimTab[i].fun();
          }
      }
  } 
} 

4. Operating system RTOS

    The embedded operating system is a more optimized execution framework, which is very well used for multi-tasking, complex functions, and strong scalability requirements. RTOS is a high-efficiency real-time multi-tasking kernel optimized for different processors. RTOS can provide similar API interfaces for dozens of series of embedded processors MPU, MCU, DSP, SOC, etc. This is based on device independence. Foundation of application development. Therefore, the C language program based on RTOS has great portability. At present, the operating systems for micro-embedded or single-chip microcomputers include VxWorks , UCOS, Free RTOS , and domestic RTT. These operating systems are similar, with similar basic functions: task management, synchronization and communication between tasks, memory management, real-time clock service, interrupt Management services .

(Image source blog)

RTOS continues to add task suspension and resume, blocking and switching threads, etc. on the time-round-robin architecture, which is a function accumulation and further optimization. Since this is not an introduction to RTOS, I have learned and applied several operating systems such as UCOS, RTT, and Free RTOS. Because of the limited space and time, I will take the time to introduce the detailed RTOS system architecture .

There are many RTOS systems at present, and many projects tend to use RTOS, but through the analysis of several architectures, it is understood that different projects require different architectures. Not all projects need them, and they are all suitable for using RTOS, such as the coupling of various tasks in the project. If it is too large, if you use RTOS, you need a lot of task synchronization, and you can't even plan the threads. This completely loses the meaning of RTOS. At this time, it is more appropriate to use some bare metal architecture.


1. Virtual Roundtable Part 1-Embedded System Information Security

2. The secret of Huawei's 5G was in the hands of a Turk? !

3. The latest semiconductor rankings are released, and Nvidia achieves 50% growth!

4. [MCU] A "flexible and resource-saving" IAP upgrade program

5.Why is RISC-V becoming a hot spot?

6. Introduction to Hongmeng OS suitable for developers~

Disclaimer: This article is reproduced online, and the copyright belongs to the original author. If you are involved in the copyright issues of the work, please contact us, we will confirm the copyright based on the copyright certification materials you provide and pay the author's remuneration or delete the content.

Guess you like

Origin blog.csdn.net/DP29syM41zyGndVF/article/details/110412124