Task processing method and lightweight operating system
Patent Information
- Application Number
- CN202510949076.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-10
- Publication Date
- 2026-10-09
- Estimated Expiration
- 2045-07-10
AI Technical Summary
[0004]本申请的目的在于,针对上述现有技术中的不足,提供一种任务处理方法以及轻量级操作系统,以解决现有技术中操作系统对硬件资源要求高的问题
[0015] The beneficial effects of this application are as follows: Centralized task management is achieved through the task pool setup, allowing the operating system to quickly locate tasks and improve task management efficiency. Setting up task mailboxes enables a flexible communication mechanism between tasks, facilitating complex task collaboration. Precise time control via a target timer improves the accuracy and reliability of task execution. By combining the target timer's status with emails in the task mailbox, the operating system can dynamically adjust task execution strategies based on real-time data and time requirements, enhancing its flexibility and adaptability. Cyclic execution of tasks in the task pool ensures that all tasks are executed in an orderly manner, preventing task backlog and resource waste, and improving the reliability and stability of the operating system.
Smart Images

Figure CN120821543B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and more specifically, to a task processing method and a lightweight operating system. Background Technology
[0002] An operating system is a software system used to manage computer hardware resources and provide a runtime environment for applications. Real-time operating systems can complete tasks within a specified time or a defined timeframe, ensuring that critical operations are executed within strict time constraints.
[0003] Currently, real-time operating systems store task data through a stack. When multiple functions are enabled in the system, a large amount of task data needs to be stored, which places high demands on hardware resources. Summary of the Invention
[0004] The purpose of this application is to address the shortcomings of the prior art by providing a task processing method and a lightweight operating system, thereby solving the problem of high hardware resource requirements of existing operating systems.
[0005] To achieve the above objectives, the technical solution adopted in this application is as follows: Firstly, this application provides a task processing method applied to a lightweight operating system, the method comprising: Step A: Determine the target task currently pending processing in the task pool. The task pool stores multiple tasks in sequence, and each task corresponds to a task mailbox. The task mailbox is used to store the emails of the task. Step B: Determine the target timer for the target task, read the task email address for the target task, and execute the target task based on the emails in the task email address and the target timer corresponding to the target task. Repeat steps A through B until all tasks in the task pool have been completed.
[0006] Optionally, determining the target task currently pending processing in the task pool includes: Determine the currently polling task based on the current task cursor value; If the currently polled task is an active task, then the currently polled task is taken as the target task, and the value of the task cursor is incremented by 1.
[0007] Optionally, determining the currently polled task based on the current task cursor includes: If the value of the task cursor is less than a preset task quantity threshold, then the task indicated by the value of the task cursor is taken as the currently polled task; If the value of the task cursor is greater than or equal to a preset task quantity threshold, then the value of the task cursor is set to a preset value, and the task indicated by the preset value is taken as the currently polled task.
[0008] Optionally, determining the target task currently pending processing in the task pool includes: Upon receiving a task notification, the task corresponding to the task notification in the task pool is determined based on the task notification. Activate the task corresponding to the task notification, and designate the task corresponding to the task notification as the target task.
[0009] Optionally, executing the target task based on the emails in the task mailbox and the target timer corresponding to the target task includes: Determine whether the task mailbox is empty, and determine the task mode of the target task, wherein the task mode includes: single task or recurring task; If the task mailbox is not empty, then read the email to be executed from the task mailbox; The target timer is controlled to start timing, and the target task is executed according to the instructions of the email to be executed.
[0010] Optionally, after determining whether the task mailbox is empty and determining the task mode of the target task, the method further includes: If the task mailbox is empty and the task mode of the target task is a single task, then the target task is destroyed and the next target task to be processed is determined from the task pool. If the task mailbox is empty and the task mode of the target task is a recurring task, then the target task is paused and the next target task to be processed is determined from the task pool.
[0011] Optionally, the method further includes: When executing the first task, if a first email for the second task is generated, the first email is sent to the task email address of the second task. After the first task is completed, the next task to be processed is determined from the task pool as the target task in a preset order.
[0012] Optionally, the method further includes: If a task notification for the second task is generated while the first task is being executed, the second task is activated and set as the new target task.
[0013] Optionally, the task pool includes multiple primary tasks and multiple secondary tasks, with each secondary task belonging to a primary task. Sending the first email to the task email address of the second task includes: If both the first task and the second task are second-level tasks, then the first task will send the first email to the task email address of the first-level task to which the first task belongs, the first-level task to which the first task belongs will send the first email to the task email address of the first-level task corresponding to the second task, and the first-level task corresponding to the second task will send the first email to the task email address of the second task. If the first task is a second-level task and the second task is a first-level task, then the first task will send the first email to the task email address of the first-level task to which the first task belongs, and the first-level task to which the first task belongs will send the first email to the task email address of the second task. If both the first task and the second task are level-one tasks, then the first task will send the first email to the task email address of the second task. If the first task is a level 1 task and the second task is a level 2 task, then the first task will send the first email to the task email address of the level 1 task to which the second task belongs, and the level 1 task to which the second task belongs will send the first email to the task email address of the second task.
[0014] Secondly, this application provides a lightweight operating system, a business application layer, a task scheduling layer, and an MCU driver layer, wherein the task scheduling layer is used to execute the steps in the task processing method as described in the first aspect.
[0015] The beneficial effects of this application are as follows: Centralized task management is achieved through the task pool setup, allowing the operating system to quickly locate tasks and improve task management efficiency. Setting up task mailboxes enables a flexible communication mechanism between tasks, facilitating complex task collaboration. Precise time control via a target timer improves the accuracy and reliability of task execution. By combining the target timer's status with emails in the task mailbox, the operating system can dynamically adjust task execution strategies based on real-time data and time requirements, enhancing its flexibility and adaptability. Cyclic execution of tasks in the task pool ensures that all tasks are executed in an orderly manner, preventing task backlog and resource waste, and improving the reliability and stability of the operating system.
[0016] To make the above-mentioned objectives, features and advantages of this application more apparent and understandable, preferred embodiments are described below in detail with reference to the accompanying drawings. Attached Figure Description
[0017] To more clearly illustrate the technical solutions of the embodiments of this application, the accompanying drawings used in the embodiments will be briefly introduced below. It should be understood that the following drawings only show some embodiments of this application and should not be regarded as a limitation of the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.
[0018] Figure 1 This illustration shows a schematic diagram of the architecture of a lightweight operating system in a battery management scenario provided by an embodiment of this application; Figure 2 A flowchart of a task processing method provided in an embodiment of this application is shown; Figure 3 This application provides a flowchart of a task polling method according to an embodiment of the present application. Figure 4 This application provides a flowchart illustrating how to determine the currently polling task according to an embodiment of the present application. Figure 5 This application provides a flowchart for determining a target task according to an embodiment of the present application. Figure 6 A flowchart illustrating an embodiment of this application for performing a target task is shown; Figure 7 This document illustrates a flowchart of a method for re-determining a target task when the task mailbox is empty, as provided in an embodiment of this application. Figure 8 This illustration shows an architecture diagram of tasks in a task pool provided in an embodiment of this application; Figure 9 A schematic diagram of the structure of an electronic device provided in an embodiment of this application is shown. Detailed Implementation
[0019] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. The components of the embodiments of this application described and shown in the accompanying drawings can generally be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of this application provided in the accompanying drawings is not intended to limit the scope of the claimed application, but merely represents selected embodiments of this application. All other embodiments obtained by those skilled in the art based on the embodiments of this application without inventive effort are within the scope of protection of this application.
[0020] It should be noted that the term "comprising" will be used in the embodiments of this application to indicate the presence of the features declared thereafter, but does not exclude the addition of other features.
[0021] In existing technologies, real-time operating systems require stacks to store task context data, which means that each task stack requires significant resource overhead. Furthermore, tasks themselves also consume resources during operation. Therefore, real-time operating systems generally have high hardware resource requirements for MCUs (Microcontroller Units).
[0022] To address the issue of high hardware resource requirements for real-time operating systems (RTOS), this application proposes a lightweight operating system. Compared to ROS and existing lightweight operating systems, the lightweight operating system proposed in this application has low hardware resource requirements and can guarantee the real-time performance of task scheduling within the operating system.
[0023] The lightweight operating system of this application can be applied to microcontrollers to manage hardware resources and provide a runtime environment for applications within the microcontroller. As an example, the lightweight operating system of this application can be applied to a lithium battery management scenario, see [reference]. Figure 1 This is a schematic diagram of the architecture of a lightweight operating system. The content circled in red is the lightweight operating system. Users can issue commands through applications in the business application layer. The lightweight operating system executes the corresponding tasks according to the user's commands and calls resources in the MCU driver to realize the execution of user commands.
[0024] It should be understood that this application is only used as an example of the application scenario of lithium battery management. The method of this application can also be applied to other application scenarios, such as network management scenarios, or other scenarios where there is interaction with both the application program and the MCU. This application does not impose any limitations on these scenarios.
[0025] To facilitate understanding, the relevant concepts of lightweight operating systems involved in this application will first be explained. The operating system mentioned below refers to a lightweight operating system.
[0026] Reference Figure 1 A lightweight operating system provides a runtime environment for applications deployed on it. Each application corresponds to multiple tasks; each task can be understood as an independent execution unit within the application, used to complete a specific function of the application, such as... Figure 1 The charge / discharge status monitoring function corresponds to a task, which is used to monitor the charge / discharge status of the lithium battery. Each task has its own program counter, stack, etc., and can run independently. Tasks can communicate and synchronize with each other through the task scheduling method of a lightweight operating system.
[0027] The task processing method of this application will be described next. This method can be applied to lightweight operating systems, such as... Figure 2 As shown, the method includes: S201. Determine the target task currently pending in the task pool. The task pool stores multiple tasks in sequence, and each task corresponds to a task mailbox. The task mailbox is used to store the emails of the task.
[0028] Optionally, the task pool can be a collection that stores multiple tasks. The tasks in the task pool are waiting to be scheduled and executed by a lightweight operating system. The tasks in the task pool can be arranged in a preset order, such as according to the task creation time or according to the task execution priority. This application does not impose any restrictions on this.
[0029] A task mailbox is a storage area associated with a task, used to store emails related to that task. These emails can be communication data between tasks or notifications sent to tasks by the operating system. The maximum number of emails that can be stored in a task mailbox can be determined by the user based on actual business needs.
[0030] Optionally, the target task can be a task that currently needs to be processed in the task pool.
[0031] In the first implementation, tasks in the task pool can be identified as target tasks in sequence according to their order. After the target task is completed, the next task in the task pool in sequence can be taken as the target task.
[0032] For example, suppose there are three tasks "T1, T2, T3" in the task pool. After the operating system starts running, it can first set T1 as the target task. After T1 is completed, T2 will be set as the target task, and so on, until all tasks in the task pool are completed.
[0033] In the second implementation, when there is a task that needs to be executed immediately, a task notification can be generated based on the user's instructions. In this case, the lightweight operating system can take the task indicated by the task notification as the target task.
[0034] For example, refer to Figure 1Suppose that during the task of monitoring battery cell voltage, it is found that the cell voltage is higher than a preset voltage threshold. To prevent damage to the cell from the abnormal voltage, the voltage needs to be controlled immediately. Therefore, a task notification can be generated. When the operating system receives the task notification, it can take the task indicated by the notification as the target task. For example, if the task order in the task pool is "monitoring task, measurement task, control task, background task", and an abnormal voltage situation occurs during the execution of the monitoring task as shown in the example above, a task notification can be generated to instruct the voltage to be controlled. In this case, the operating system does not need to execute the tasks in the preset order, but skips the measurement task and takes the control task as the target task.
[0035] S202. Determine the target timer for the target task, read the task email address of the target task, and execute the target task based on the email in the task email address and the target timer corresponding to the target task.
[0036] Optionally, the target timer can be a timer associated with the task, used to control the execution time of the task or trigger the execution of the task. The target timer can set the execution time interval of the task or trigger the execution of the task at a specific time.
[0037] Optionally, to meet different hardware requirements, the lightweight operating system of this application may include two types of timers: a timer providing precise timing and a timer providing coarse timing. Based on different hardware requirements, corresponding timers can be preset for each task. When a task starts executing, the timer is controlled to start counting, and the task execution result is returned when the preset value is reached.
[0038] The operating system can determine the target timer of the target task, check whether the timer has expired or whether the task needs to be executed. If so, it reads the task mailbox of the target task and executes the target task based on the email in the task mailbox and the status of the target timer.
[0039] For example, suppose there is an email in the task mailbox of task T1 with the content "Start voltage data acquisition". The target timer of task T1 is set to trigger once every 5 seconds. The operating system reads the task mailbox of T1, finds that the email content is "Start voltage data acquisition", and checks and finds that the target timer has expired. Then task T1 can be executed to start the voltage data acquisition task according to the email content.
[0040] Repeat steps S201-S202 until all tasks in the task pool have been completed.
[0041] Optionally, the operating system can repeatedly execute steps S201-S202. When all tasks in the task pool have been completed, the operating system can re-traverse the task pool from the first task in the task pool according to the order of the tasks in the task pool, determine the target task, and repeat steps S201-S202. After all tasks in the current round have been completed, the task pool will be traversed from the beginning in the next round.
[0042] In this embodiment, a task pool enables centralized task management, allowing the operating system to quickly locate tasks and improve management efficiency. Setting up task mailboxes establishes a flexible communication mechanism between tasks, facilitating complex task collaboration. Precise time control via a target timer improves the accuracy and reliability of task execution. By combining the target timer's status with emails in the task mailboxes, the operating system dynamically adjusts task execution strategies based on real-time data and time requirements, enhancing its flexibility and adaptability. Cyclic execution of tasks in the task pool ensures orderly execution, preventing task backlog and resource waste, and improving the operating system's reliability and stability.
[0043] The following is a further explanation of the first implementation method for determining the target tasks currently pending in the task pool, as described above. Figure 3 As shown, the above step S201 includes: S301. Determine the currently polled task based on the current task cursor value.
[0044] Optionally, the task cursor can be a variable used to record the position of the currently polled task. The value of the task cursor can be an integer value, starting from the beginning of the task pool and pointing sequentially to each task in the task pool. For example, assuming the value of the task cursor is 9, the ninth task in the task pool can be selected as the currently polled task.
[0045] When the value of the task cursor is less than the total number of tasks in the task pool, the task being polled can be determined based on the value of the task cursor. If the value of the task cursor is greater than the total number of tasks in the task pool, an error message can be returned or the value of the task cursor can be reset to its initial value.
[0046] S302. If the currently polled task is an active task, then the currently polled task is taken as the target task, and the value of the task cursor is incremented by 1.
[0047] Among them, an activated task can refer to a task that is ready to run, usually in a ready state, waiting for the operating system to schedule it.
[0048] Once a task is identified as the target task, the task cursor is incremented by 1, pointing to the next task in the task pool, ready for the next round of polling.
[0049] In this embodiment, the operating system dynamically selects active tasks for execution through a polling mechanism, ensuring timely task response. Incrementing the task cursor ensures that tasks in the task pool are processed sequentially, avoiding task chaos and conflicts. By quickly locating and executing active tasks, the operating system can efficiently manage task execution, improving overall system performance.
[0050] Optionally, the steps described above for determining the currently polled task based on the current task cursor are as follows: Figure 4 As shown, it includes: S401. If the value of the task cursor is less than the preset task quantity threshold, then the task indicated by the value of the task cursor is taken as the currently polled task.
[0051] The task quantity threshold can be the total number of tasks in the task pool.
[0052] If the value of the task cursor is less than the total number of tasks in the task pool, then the task indicated by the value of the task cursor can be used as the currently polled task.
[0053] S402. If the value of the task cursor is greater than or equal to the preset task quantity threshold, then the value of the task cursor is set to the preset value, and the task indicated by the preset value is taken as the currently polled task.
[0054] If the value of the task cursor is greater than the preset task quantity threshold, it means that all tasks in the current task pool have been polled. In this case, a task cursor error can be returned, or the value of the task cursor can be set to a preset value, such as setting the value of the task cursor to point to the first task in the task pool, so as to start a new round of task polling.
[0055] The following is a further explanation of the second implementation method for determining the target tasks currently pending in the task pool, as described above. Figure 5 As shown, the above step S201 includes: S501. Upon receiving a task notification, determine the task in the task pool that corresponds to the task notification based on the task notification.
[0056] In this context, a task notification can be an urgent task issued by the user through the operating system, or an urgent task generated by the operating system during task execution. The task corresponding to the task notification can be the task that receives the task notification.
[0057] For example, if an urgent task for task B is generated during the execution of task A, a task notification can be generated, with the task corresponding to task B, and the task notification can be sent to task B.
[0058] S502. Activate the task corresponding to the task notification and set the task corresponding to the task notification as the target task.
[0059] Optionally, if the task corresponding to the task notification is inactive, when sending the task notification, the task corresponding to the task notification can be activated first, the task status can be activated to the ready state, and the task can be used as the target task.
[0060] If the task corresponding to the task notification is already active, then the task that received the task notification will be directly used as the target task.
[0061] In this embodiment, when an urgent task needs to be executed, the target task is directly determined from the task pool through task notification. For non-urgent tasks, they can be executed in a round-robin fashion according to the task order in the task pool. This simplifies task scheduling and ensures the real-time execution of urgent tasks.
[0062] The following is a further explanation of how to execute a target task based on emails in the task's mailbox and the target timer corresponding to the target task. Figure 6 As shown, the above step S202 includes: S601. Determine if the task mailbox is empty and determine the task mode of the target task. The task mode includes: single task or recurring task.
[0063] Optionally, when the task mode is a single task, the task can be destroyed after all emails in the task mailbox have been processed. When the task mode is a recurring task, the task can be paused and retained after all emails in the task mailbox have been processed.
[0064] In one possible implementation, a recurring task can be a task that is repeatedly executed at predetermined time intervals or under predetermined conditions. A single task can be a task that is executed only once and is not repeated after it is completed.
[0065] S602. If the task mailbox is not empty, read the email to be executed from the task mailbox.
[0066] If the task mailbox is empty, an error message can be returned through a callback function, and a new target task can be determined in the task pool based on the steps in S201 above.
[0067] Emails in the task mailbox can be arranged in the order they were received. When reading emails to be executed from the task mailbox, the earliest received email can be selected as the email to be executed.
[0068] S603: Control the target timer to start timing and execute the target task according to the instructions of the email to be executed.
[0069] Optionally, the email to be executed includes task instructions. After reading the email, the target timer can be controlled to start timing, and the task can be executed according to the instructions in the email. The target timer controls the execution time of the task, ensuring that the target task is executed within the predetermined time.
[0070] Furthermore, after determining whether the task mailbox is empty and determining the task mode of the target task, as described above, Figure 7 As shown, the method of this application further includes: S701. If the task mailbox is empty and the task mode of the target task is a single task, then destroy the target task and determine the next target task to be processed from the task pool.
[0071] When the task mailbox is empty and the task mode is single task, it means that the operating system considers the task to have completed its mission. At this time, the target task can be destroyed, the resources it occupies can be released, and the next task to be processed can be selected from the task pool to continue the task scheduling.
[0072] S702. If the task mailbox is empty and the target task's task mode is a recurring task, then pause the target task and determine the next target task to be processed from the task pool.
[0073] When the task mailbox is empty and the task mode is a recurring task, it means that the operating system believes that there are no instructions or data to be processed in the current task. However, the task may be triggered later. Therefore, the task can be paused and the next task to be processed can be selected from the task pool to continue the task scheduling.
[0074] In this embodiment, by combining task mailboxes and task modes, the lightweight operating system can flexibly manage task execution. When the task mailbox is empty, the operating system decides whether to destroy or suspend the task based on the task mode, ensuring efficient resource utilization and flexible task scheduling. This mechanism not only improves the system's resource management efficiency but also ensures the continuity and flexibility of task scheduling.
[0075] The lightweight operating system described in this application will be further explained below with reference to the specific system implementation method.
[0076] The operating system in this application includes timers, mailboxes, and task pools. The task structure data mainly includes: task name, task execution callback function, task counter, task execution time interval, private parameters, task activation flag, task creation flag, task counter execution precision (0 for fine timer, 1 for coarse timer), task completion callback function, task start callback function, task working mode (0 for recurring task, 1 for single task), task suspension flag, and task notification callback function.
[0077] For example, the task structure can be represented by the following code: typedef struct MP_OS { char *taskname ; void (* task)(void *parameter); uint32_t tickcount; uint32_t default_count; / * data * / void *parameter; uint8_t is_active; uint8_t is_used; uint8_t tick_mode; #ifdef USERDO_PREPARE void (* do_before_stop)(void *parameter); void (* do_before_restart)(void *parameter); #endif uint8_t work_mode; uint8_t is_pausing ; / *notify* / int (*notify_task)(uint32_t msg); uint32_t time_slice; uint8_t if_needslice ; }_mpos_task; The task mailbox structure data includes: a pointer to the dynamically allocated mailbox storage area, the index of the current read position, the index of the current write position, the total number of unread emails in the current mailbox, the maximum number of emails that can be stored, and the number of bytes occupied by each email.
[0078] For example, the structure of a task mailbox can be represented by the following code: typedef struct MP_MAIL { / * data * / void *mailbuff; uint8_t rptr; uint8_t wptr; uint8_t rlen ; uint8_t maxn ; uint8_t mailsize; }_mpos_mailbox; After initializing the task module in the operating system, tasks and their corresponding task mailboxes can be created.
[0079] The task creation process can be represented as follows: int create_task(uint8_t taskptr, void (*task)(void *parameter), void *parameter, uint32_t default_tick, uint8_t tick_m, uint8_t workmode, void (* do_before_stop)(void *parameter), void (* do_before_restart)(void *parameter), int (*notify_task)(uint32_t msg)) The specific process includes: S10. If the task cursor taskptr is greater than the maximum number of tasks MAXTASK_N, the function returns a task cursor error. The maximum number of tasks can be the maximum number of tasks in the task pool.
[0080] S12. If the task execution callback function is empty (NULL), the function returns an empty task error.
[0081] S13, disable_irq(), disable global interrupts.
[0082] S14, my_task[taskptr].task = task, assigns the task execution callback function to the corresponding static task structure cache parameter.
[0083] S15, my_task[taskptr].tickcount = default_tick, assigns the task counter to the corresponding static task structure cache parameter.
[0084] S16, my_task[taskptr].default_count = default_tick, assigns the task execution time interval to the corresponding static task structure cache parameter.
[0085] S17, my_task[taskptr].is_used = true, the flag indicating whether the task has been created is true by default, and is assigned to the corresponding static task structure cache parameter.
[0086] S18, my_task[taskptr].tick_mode = tick_m, assigns the task counter's running precision to the corresponding static task structure cache parameter.
[0087] S19, my_task[taskptr].work_mode = workmode, assigns the task work mode to the corresponding static task structure cache parameter.
[0088] S110, my_task[taskptr].do_before_stop = do_before_stop, assigns the callback function for the preparatory work before the task ends to the corresponding static task structure cache parameter.
[0089] S111, my_task[taskptr].do_before_restart = do_before_restart, assigns the callback function for the preparatory work before the task starts to the corresponding static task structure cache parameter.
[0090] S112, my_task[taskptr].is_pausing = false, the task suspension flag defaults to false, and is assigned to the corresponding static task structure cache parameter.
[0091] S113, my_task[taskptr].notify_task = notify_task, assigns the task execution callback function to the corresponding static task structure cache parameter.
[0092] S114, enable_irq(), enables global interrupts.
[0093] After creating a task, it can be activated to put it in a ready state, allowing the operating system to execute it. The steps for task activation include: S20. If the task cursor taskptr is greater than the maximum number of tasks MAXTASK_N, the function returns.
[0094] S21. Determine if my_task[taskptr].task != NULL, the task execution function is not equal to empty, and my_task[taskptr].is_used == true, the task has been created.
[0095] S22. If the above judgment is true, my_task[taskptr].is_active = true, the task activation flag is set to true and assigned to the corresponding static task structure cache parameter.
[0096] The steps for starting the task described above, `void start_task(uint8_t taskptr, uint32_t count, uint8_t wmode)`, are implemented as follows: S30. If the task cursor taskptr is greater than the maximum number of tasks (MAXTASK_N), the function returns.
[0097] S31. Determine if my_task[taskptr].task != NULL, the task execution function is not equal to empty, and my_task[taskptr].is_used == true, the task has been created.
[0098] S32. Determine if my_task[taskptr].is_active == true whether the task is already active.
[0099] S33. If the above judgment is true, then my_task[taskptr].work_mode = wmode, setting the task work mode value; my_task[taskptr].tickcount = count, setting the task counter value; and assigning it to the corresponding static task structure cache parameter.
[0100] S34. At this point, check if my_task[taskptr].is_pausing== true to determine if the task is suspended.
[0101] S35. If the above judgment is true, then my_task[taskptr].is_pausing = false, clear the pausing flag, and if a callback function for calling the pre-task preparation work is registered, y_task[taskptr].do_before_restart(my_task[taskptr].parameter) calls the callback function for calling the pre-task preparation work.
[0102] After completing the task and creating the task email address using the steps above, and activating the task, you can send and receive emails using the following steps.
[0103] S40. Determine if mailbox[taskptr].mailbuff == NULL || message == NULL. Check if the mailbox buffer or message pointer is NULL. If so, the function returns an error indicating that the mailbox does not exist.
[0104] S41. Determine if mailbox[taskptr].rlen>= mailbox[taskptr].maxn, check if the mailbox is full, and return a mailbox full error.
[0105] S42, disable_irq(), disable global interrupts.
[0106] S43, wptr=(uint8_t*)&ptr[mailbox[taskptr].wptr*mailbox[taskptr].mailsize], calculate the write position.
[0107] S44, for(i = 0;i <mailbox[taskptr].mailsize ; i++) {*wptr = wbuff[i] ;wptr++ ;} , copies message data byte by byte into the buffer.
[0108] S45, mailbox[taskptr].rlen++, update the number of readable messages rlen.
[0109] S46, mailbox[taskptr].wptr=(mailbox[taskptr].wptr+1)%mailbox[taskptr].maxn, update the circular write pointer wptr.
[0110] S47. Check if my_task[taskptr].tickcount != IMMEDIATE_OP to determine if the task counter is set to execute immediately.
[0111] S48, run_task_immdiate(taskptr, my_task[taskptr].work_mode), if the above judgment is true, the corresponding task will be executed immediately.
[0112] S49, enable_irq(), enable global interrupt.
[0113] For example, the third step, receiving emails using `int recieve_mailbox(uint8_t taskptr, void *mail)`, includes the following steps: S50. Determine if mailbox[taskptr].mailbuff == NULL || message == NULL. Check if the mailbox buffer or message pointer is NULL. If so, the function returns an error indicating that the mailbox does not exist.
[0114] S51. Check if mailbox[taskptr].rlen == 0 to see if the mailbox is empty. The function returns a "mailbox is full" error.
[0115] S52, for(i = 0;i <mailbox[taskptr].mailsize ; i++) { rbuff[i] = rptr[i];}, copies data byte by byte from the mailbox to the receive buffer.
[0116] S53, mailbox[taskptr].rptr=(mailbox[taskptr].rptr+1)%mailbox[taskptr].maxn, update the circular read pointer rptr.
[0117] S54, mailbox[taskptr].rlen -- , updates the number of readable messages rlen.
[0118] S55, return MAIL_EXIST, returns the mail existence flag.
[0119] Optionally, task scheduling can be performed in the operating system through the kernel function for task scheduling void run_task_system(void), and the task scheduling kernel function can be summarized into the following steps.
[0120] S60, for(taskptr = 0 ;taskptr<MAXTASK_N ;taskptr ++), traverse the task list.
[0121] S61, determine whether my_task[taskptr].task != NULL&&my_task[taskptr].is_used ==true, and only process valid tasks.
[0122] S62, determine whether my_task[taskptr].is_active == true to check whether the task is activated.
[0123] S63, determine whether my_task[taskptr].tickcount == IMMEDIATE_OP, to check whether the task counter is set to immediate execution.
[0124] S64, if all the above conditions are satisfied, determine whether my_task[taskptr].work_mode == CYCLE_RUN to check whether the work mode is cyclic mode; if it is cyclic mode, reset the task counter as my_task[taskptr].tickcount= my_task[taskptr].default_count .
[0125] S65, determine whether get_mailbox_empty_status(taskptr) == true to check whether the mailbox is empty; if the mailbox is empty, cancel the task execution by calling set_task_t(taskptr,CANCEL_TASK).
[0126] S66, my_task[taskptr].task(my_task[taskptr].parameter), call the task function pointer task(parameter) and pass in the parameter.
[0127] During task execution, the operating system can control task scheduling through a timer, and the operation logic of the timer includes functions such as timer creation, timer destruction, timer starting, timer stopping and timer resetting.
[0128] The timer structure can be represented by the following code.
[0129] struct timer_struct { uint32_t tcount; uint32_t period; void(*callback)(timer_handler, void*); void *private_data; TIMER_STATE state; TIMER_TYPE type; } In the process of task management by the operating system, the internal processing procedure of the timer running function void timer_run(void) includes the following steps.
[0130] S70: for(timer_ptr=0;timer_ptr<TIMER_MAX_NUMBER; timer_ptr++), traverse the timer array.
[0131] S71: determine whether timer_table[timer_ptr].state == TIMER_RUNNING, and only process timers in the running state.
[0132] S72: timer_table[timer_ptr].tcount++, increment the counter.
[0133] S73: check whether timer_table[timer_ptr].tcount>= timer_table[timer_ptr].period, that is, check whether the timing period is reached.
[0134] S74: if the above condition is satisfied, set timer_table[timer_ptr].tcount = 0 to reset the counter.
[0135] S75: determine whether timer_table[timer_ptr].type == TIMER_ONESHOT to check whether it is a one-shot timer processing; if yes, mark it as completed, and set timer_table[timer_ptr].state = TIMER_COMPLETED.
[0136] S76. Check if timer_table[timer_ptr].callback != NULL. If the timer callback function is not empty, execute the timer callback function: timer_table[timer_ptr].callback((timer_handler)&timer_table[timer_ptr], timer_table[timer_ptr].private_data).
[0137] The following describes the communication process between tasks in this application. This communication includes two methods. The first method, used when handling tasks of low urgency, involves a task mailbox. The specific process includes: When executing the first task, if a first email for the second task is generated, the first email is sent to the task email address of the second task. After the first task is completed, the next task to be processed is determined from the task pool as the target task in a preset order.
[0138] The first task can be a target task that is being executed in the task pool. If a first email for the second task is generated during the execution of the first task, the first email can be sent to the task email address of the second task, and the tasks can continue to be executed in the order of execution in the task pool.
[0139] When the second task is executed as the target task, the emails in the task mailbox are read and executed sequentially according to the order of the emails in the task mailbox of the second task.
[0140] In the second implementation method, when the task to be executed is an urgent task, it can be implemented using task notification, specifically including: If a task notification for the second task is generated while the first task is being executed, the second task is activated and set as the new target task.
[0141] In one possible implementation, task notifications can be implemented using callback functions. Urgent tasks can be placed under the task notification callback function, allowing urgent tasks to be processed in real time, thereby improving the real-time performance of the operating system.
[0142] In this embodiment, by combining task email and task notification, both the lightweight nature of the operating system and the real-time performance of the operating system are achieved.
[0143] In this embodiment of the application, tasks can be divided into primary tasks and secondary tasks. The task pool includes multiple primary tasks and multiple secondary tasks, and each secondary task belongs to a primary task.
[0144] As one possible implementation method, Figure 8 This is a schematic diagram of the task architecture in a system task pool. When the operating system is used in battery management applications, the primary tasks include monitoring tasks, measurement tasks, control tasks, and background tasks.
[0145] The secondary tasks belonging to the monitoring tasks include charging and discharging status monitoring, cell voltage monitoring, cell temperature monitoring, charging and discharging current monitoring, total battery voltage monitoring, power calculation, and equalization logic tasks. The secondary tasks belonging to the measurement tasks include metering chip data acquisition and ADC (Analog-to-Digital Converter) data acquisition. The secondary tasks belonging to the control tasks include MOS (metal-oxide-semiconductor) switch control, LED (light-emitting diode) indicator control, LED flashlight control, LED display control, and FAN (fan) switch control. The secondary tasks belonging to the background tasks include external power input monitoring, cascade communication tasks, alarm system module processing tasks, AC monitoring tasks, key scanning tasks, and MOD BUS (Modbus protocol) communication tasks.
[0146] The process of sending the first email to the task email address of the second task includes: If both the first task and the second task are second-level tasks, then the first task will send the first email to the task email address of the first-level task to which the first task belongs, the first-level task to which the first task belongs will send the first email to the task email address of the first-level task corresponding to the second task, and the first-level task corresponding to the second task will send the first email to the task email address of the second task.
[0147] Reference Figure 8 If the first task is cell voltage monitoring and the second task is MOS switch control, the first task can send the first email to the task email address of the monitoring task, which will then send the first email to the corresponding email address of the control task, which will then send the first email to the task email address of the MOS switch control task.
[0148] The first email may include an identifier for the second task. Based on the identifier of the second task, the first-level task to which the first task belongs can be determined, and the first-level task to which the second task belongs can also be determined based on the identifier of the second task.
[0149] If the first task is a second-level task and the second task is a first-level task, then the first task will send the first email to the task email address of the first-level task to which the first task belongs, and the first-level task to which the first task belongs will send the first email to the task email address of the second task.
[0150] For example, refer to Figure 8 If the first task is to monitor the total battery voltage and the second task is a background task, the first task can send the first email to the task email address of the monitoring task, and then the monitoring task will send the first email to the task email address of the background task.
[0151] If both the first and second tasks are level one tasks, then the first task will send the first email to the task email address of the second task.
[0152] Reference Figure 8 If the first task is a monitoring task and the second task is a control task, then the monitoring task can send the first email to the task email address of the control task.
[0153] If the first task is a level 1 task and the second task is a level 2 task, then the first task will send the first email to the task email address of the level 1 task to which the second task belongs, and the level 1 task to which the second task belongs will send the first email to the task email address of the second task.
[0154] Reference Figure 8 If the first task is a monitoring task and the second task is a cascading communication task, the monitoring task can send the first email to the task email address of the background task, and the background task can then send the first email to the task email address of the cascading communication task.
[0155] It is worth noting that this application only uses a two-level task architecture as an example for illustration, but multi-level task architectures can also be set in the task pool, such as a three-level task structure. This application does not impose any restrictions here. When a three-level task architecture is set, task emails can be forwarded layer by layer according to the task architecture.
[0156] In this embodiment of the application, by setting up a first-level task and a second-level task architecture in the task pool, the task emails can be forwarded in order, ensuring the orderliness of task scheduling in the operating system.
[0157] Based on the same inventive concept, this application also provides a lightweight operating system corresponding to the task processing method. Since the principle of the operating system in this application is similar to the task processing method described above, the implementation of the operating system can refer to the implementation of the method, and the repeated parts will not be described again.
[0158] Figure 9This illustration shows a schematic diagram of an electronic device provided in an embodiment of this application. The electronic device is equipped with the aforementioned lightweight operating system and includes a processor 901, a storage medium 902, and a bus 903. The storage medium 902 stores machine-readable instructions executable by the processor 901. When the electronic device runs a task processing method as described in the embodiment, the processor 901 communicates with the storage medium 902 via the bus 903. The processor 901 executes the machine-readable instructions and the preamble of the method item of the processor 901 to perform the steps in the above-mentioned task processing method.
[0159] This application also provides a computer-readable storage medium storing a computer program, which is executed by a processor, and the processor performs the steps in the above-described task processing method.
[0160] In this embodiment, the computer program, when run by the processor, can also execute other machine-readable instructions to perform other methods as described in the embodiments. For details on the specific execution steps and principles, please refer to the description of the embodiments, which will not be repeated here.
[0161] In the embodiments provided in this application, it should be understood that the disclosed apparatus and methods can be implemented in other ways. The apparatus embodiments described above are merely illustrative. For example, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. Furthermore, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Additionally, the displayed or discussed mutual couplings, direct couplings, or communication connections may be through some communication interfaces; indirect couplings or communication connections between devices or units may be electrical, mechanical, or other forms.
[0162] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0163] In addition, the functional units in the embodiments provided in this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit.
[0164] If the aforementioned functions are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0165] It should be noted that similar labels and letters in the following figures indicate similar items. Therefore, once an item is defined in one figure, it does not need to be further defined and explained in subsequent figures. In addition, the terms "first", "second", "third", etc. are used only to distinguish descriptions and should not be construed as indicating or implying relative importance.
[0166] Finally, it should be noted that the above-described embodiments are merely specific implementations of this application, used to illustrate the technical solutions of this application, and not to limit them. The protection scope of this application is not limited thereto. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that any person skilled in the art can still modify or easily conceive of changes to the technical solutions described in the foregoing embodiments, or make equivalent substitutions for some of the technical features, within the scope of the technology disclosed in this application; and these modifications, changes, or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this application. All should be covered within the protection scope of this application. Therefore, the protection scope of this application should be determined by the protection scope of the claims.
Claims
1. A task processing method, characterized in that, A lightweight operating system is applied to a microcontroller to manage hardware resources and provide a runtime environment for applications within the microcontroller. The method includes: Step A: Determine the target task currently pending processing in the task pool. The task pool stores multiple tasks in sequence, and each task corresponds to a task mailbox. The task mailbox is used to store the emails of the task. Step B: Determine the target timer for the target task, read the task email address for the target task, and execute the target task based on the emails in the task email address and the target timer corresponding to the target task. Repeat steps A through B until all tasks in the task pool have been completed. When executing the first task, if a first email for the second task is generated, the first email is sent to the task email address of the second task. After the first task is completed, the next task to be processed is determined from the task pool as the target task in a preset order. The task pool includes multiple primary tasks and multiple secondary tasks, with each secondary task belonging to a primary task. Sending the first email to the task email address of the second task includes: If both the first task and the second task are second-level tasks, then the first task will send the first email to the task email address of the first-level task to which the first task belongs, the first-level task to which the first task belongs will send the first email to the task email address of the first-level task corresponding to the second task, and the first-level task corresponding to the second task will send the first email to the task email address of the second task. If the first task is a second-level task and the second task is a first-level task, then the first task will send the first email to the task email address of the first-level task to which the first task belongs, and the first-level task to which the first task belongs will send the first email to the task email address of the second task. If both the first task and the second task are level-one tasks, then the first task will send the first email to the task email address of the second task. If the first task is a level 1 task and the second task is a level 2 task, then the first task will send the first email to the task email address of the level 1 task to which the second task belongs, and the level 1 task to which the second task belongs will send the first email to the task email address of the second task.
2. The method according to claim 1, characterized in that, The determination of the target tasks currently pending in the task pool includes: Determine the currently polling task based on the current task cursor value; If the currently polled task is an active task, then the currently polled task is taken as the target task, and the value of the task cursor is incremented by 1.
3. The method according to claim 2, characterized in that, The step of determining the currently polled task based on the current task cursor includes: If the value of the task cursor is less than a preset task quantity threshold, then the task indicated by the value of the task cursor is taken as the currently polled task; If the value of the task cursor is greater than or equal to a preset task quantity threshold, then the value of the task cursor is set to a preset value, and the task indicated by the preset value is taken as the currently polled task.
4. The method according to claim 1, characterized in that, The determination of the target tasks currently pending in the task pool includes: Upon receiving a task notification, the task corresponding to the task notification in the task pool is determined based on the task notification. Activate the task corresponding to the task notification, and designate the task corresponding to the task notification as the target task.
5. The method according to claim 1, characterized in that, The step of executing the target task based on the emails in the task mailbox and the target timer corresponding to the target task includes: Determine whether the task mailbox is empty, and determine the task mode of the target task, wherein the task mode includes: single task or recurring task; If the task mailbox is not empty, then read the email to be executed from the task mailbox; The target timer is controlled to start timing, and the target task is executed according to the instructions of the email to be executed.
6. The method according to claim 5, characterized in that, After determining whether the task mailbox is empty and determining the task mode of the target task, the method further includes: If the task mailbox is empty and the task mode of the target task is a single task, then the target task is destroyed and the next target task to be processed is determined from the task pool. If the task mailbox is empty and the task mode of the target task is a recurring task, then the target task is paused and the next target task to be processed is determined from the task pool.
7. The method according to claim 5, characterized in that, The method further includes: If a task notification for the second task is generated while the first task is being executed, the second task is activated and set as the new target task.
8. A lightweight operating system, characterized in that, include: The system comprises a business application layer, a task scheduling layer, and an MCU driver layer, wherein the task scheduling layer is used to execute the steps in the task processing method as described in any one of claims 1-7.
Citation Information
Patent Citations
Method for implementing multi-task real-time operating system embedded into 51 series singlechip
CN102063325A
Task distributed scheduling method and device
CN115129438A
Timer processing method and device and electronic equipment
CN117251272A