STM32 simple multi-task scheduling

Source: Internet
Author: User

At present, the development of STM32 is still in the "streaking" stage, which is at the cost of development. It may not be embedded in any RTOS system. Because it does not support the operating system, as a result, you cannot easily schedule and manage multiple tasks. In the main function, you may write as follows:

Int main (void) {while (1) {Task1 (); // call task 1Task2 (); // call Task 2 }}

However, in this case, there will be a problem. If Task 1 is a very urgent task, such as an AD sampling task, it must be continuously executed, task 2 is not an urgent task. It only needs to be executed once in a period of time (for example, to control the flashing of LED lights, it only needs to flash once every 1 s ), in this case, frequent call of Task 2 takes up the execution time of Task 1, and task 2 does not require such frequent execution, which consumes CPU processing. Therefore, we can consider implementing a scheduling policy to solve this problem. For each task, we can define a structure like this:

typedef struct{  void (*fTask)(void);  int64u uNextTick;  int32u uLenTick;}sTask;

FTask is the task pointer pointing to a specific task. uNextTick is the next execution time of the task, and uLenTick is the scheduling cycle or frequency of the task, that is, the execution interval.

According to this structure, you can pre-define a struct array, and list the tasks to be called and the scheduling time of the tasks as follows:

// Task list static sTask mTaskTab [] = {Task_SysTick, 0, 0}, {Task1, 0, 10} // execute once in 10 ms, {Task2, 0,200} // execute once in MS };

The first task, Task_SysTick, is used to calculate the system time and obtain the running time after power-on. (The task code is attached to the post of this Article ). The default task execution time is 0. In the main function, this array is continuously poll and the next call time of the current task is compared with the current time, if it is your turn to execute the task, run the task. After the task is completed, set the next execution time of the task to the current time plus the scheduling time of the task, then execute the next task according to this method. The Code is as follows:

While (1) {// task loop for (I = 0; I <ARRAYSIZE (mTaskTab); I ++) {if (mTaskTab [I]. uNextTick <= GetTimingTick () {mTaskTab [I]. uNextTick + = mTaskTab [I]. uLenTick; mTaskTab [I]. fTask ();}}}

In this way, you can perform a simple scheduling for multiple tasks. You only need to add the task in the mTaskTab table later. It should be emphasized that it takes time to execute each task, the actual scheduling cycle of a task may be longer than the preset scheduling cycle, which may result in inaccurate time. Of course, this is only suitable for tasks with not strict polling cycles, if you want to execute a task within a strict time period or require more precise time processing, you must use a timer.

Appendix:

Complete main file code:

# Ifndef arraysize # define arraysize (A) (sizeof (a)/sizeof (a) [0]) # endif // Task Structure typedef struct {void (* ftask) (void); u64 unexttick; u32 ulentick;} stask; // task list static stask mtasktab [] = {task_0000ick, 0, 0}, {task1, 0, 10} // execute once in 10 ms, {task2, 0,200} // execute once in MS // Add a task before this }; /*************************************** **************************************** * Function Name: main * Description: main program. * input: none * output: none * return: none *************************************** **************************************** /INT main (void) {int I = 0; // hardware initialization hw_init (); // initialize the system tick task dev_javasick_init (void );//... while (1) {// task loop for (I = 0; I <arraysize (mtasktab); I ++) {If (mtasktab [I]. unexttick <= gettimingtick () {mtasktab [I]. unexttick + = mtasktab [I]. ulentick; mtasktab [I]. ftask ();}}}

}

Task code:

Volatile int64u g_timingtick = 0; volatile int64u g_timingtickold = 0; // ================================================ ========================================================== ==============================/// [function name] void dev_javasick_init (void) // [parameter] // [function] initialization // [return value] NONE // [creator] 2010-07-27 firehood // ====== ========================================================== ========================================================== ========= void dev_systick_init (void) {tim_timebaseinittypedef tim_timebasestructure;/* time base configuration */percent = 65535; percent = 36000-1; percent = 0; percent = tim_countermode_up; tim_timebaseinit (tim2, & tim_timebasestructure ); tim_setcounter (tim2, 0);/* tim enable counter */tim_cmd (tim2, enable );} // ================================================ ========================================================== ==============================/// [function name] void gettimingtick (void) // [parameter] // [function] obtains the running time after the MCU is started // [return value] the running time after the MCU is started, unit: Ms // [creator] 2010-07-27 firehood // ========================== ========================================================== ============================== int64u gettimingtick (void) {return g_timingtick ;} // ================================================ ========================================================== ==============================/// [function name] void task_policick (void) // [parameter] // [function] Tick task, obtain the system time from tim2 // [return value] NONE // [creator] 2010-07-27 firehood // ========== ========================================================== ========================================================== = void task_systick (void) {int16u temp = tim_getcounter (tim2); If (temp> 1000) {tim_setcounter (tim2, 0); g_timingtickold = g_timingtickold + temp; temp = 0 ;} g_timingtick = g_timingtickold + temp ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.