Analog i2c communication method and system
By configuring the I2C sending task to the highest priority and adopting a multi-round single-frame data sending strategy in the FreeRTOS operating system, the problem of abnormal data sending in simulated I2C communication was solved, and data integrity and fast execution of high-priority tasks were achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- FORYOU GENERAL ELECTRONICS
- Filing Date
- 2022-03-24
- Publication Date
- 2026-04-14
AI Technical Summary
The use of a preemptive task mechanism in the FreeRTOS operating system can lead to abnormal I2C data transmission in the simulated I2C communication system, which in turn can cause problems with operation commands and even system malfunctions.
Configure the I2C transmission task as the highest priority and adopt a multi-round single-frame data transmission strategy. High-priority tasks are only processed after the current frame has been sent to ensure data integrity.
This effectively prevents low-priority I2C data from being interrupted by high-priority tasks during transmission, ensuring data integrity while quickly executing high-priority tasks.
Smart Images

Figure CN114756342B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of system task scheduling technology, and in particular to a simulated I2C communication method and system. Background Technology
[0002] When a high-level task acquires resources and enters the ready state, a low-level task exits the execution state and enters the ready state, allowing the high-level task to execute. This is called a preemptive task scheduling mechanism.
[0003] In existing analog I2C technology, the program corresponding to each I2C slave device calls the analog I2C module to send data to the corresponding I2C slave device, which is not a problem in time-slice single-loop software.
[0004] However, in the FreeRTOS operating system, which uses a preemptive task mechanism, the programs corresponding to each I2C slave device have different priorities. Therefore, it is possible that while program 1 (e.g., radio) is sending I2C data, the system schedules a higher-priority program 2 (e.g., volume control), interrupting program 1's simulated I2C data transmission and starting to send program 2's simulated I2C data. This causes an anomaly in program 1's I2C data transmission, leading to operational commands and even system malfunctions. Summary of the Invention
[0005] This invention provides a simulated I2C communication method and system, which solves the technical problem that the existing FreeRTOS operating system uses a preemptive task mechanism, which causes communication interruption during task scheduling, resulting in abnormal I2C data transmission and thus operational command and even system abnormalities.
[0006] To address the above technical problems, this invention provides a method for simulating I2C communication, comprising the following steps:
[0007] S1. Set up an I2C send task and configure it as the highest priority;
[0008] S2. According to the preset strategy and the I2C sending task, execute the current pending task to send multiple rounds of single-frame data;
[0009] S3. When a higher-priority target task is detected, determine whether the current round of single-frame data transmission is complete. If so, execute the target task according to the preset strategy to send multiple rounds of single-frame data; otherwise, continue to execute the current round of single-frame data.
[0010] S4. When the target pending task is detected to have been completed, resume the execution of the current pending task.
[0011] This basic solution improves upon the original preemptive task mechanism by setting the I2C sending task to multiple rounds of single-frame data transmission, with only one frame of data sent in each round. When a target task with a higher priority than the current task is detected, the target task is executed only after the current round of single-frame data transmission is completed. In this way, when the slave devices are conducting simulated I2C communication, the communication between the low-priority slave device and the master will not be arbitrarily interrupted by the high-priority slave device. The single-frame data is complete during the data transmission process, and the high-priority target task can be executed quickly.
[0012] In a further implementation, configuring it as the highest priority specifically means: configuring the priority of the I2C sending task to be higher than the priority of the data tasks corresponding to all I2C slave devices.
[0013] This solution configures the I2C send task to have a higher priority than the data tasks corresponding to all I2C slave devices, ensuring that only one task is running at any given time in the RTOS. When a data task corresponding to one of the slave devices calls the simulated I2C module to send I2C data, even if other slave devices' data tasks are ready, they cannot interrupt the current I2C data transmission. Only after the current frame of the current pending task has been sent will the I2C send task accept the data transmission request from the higher-priority data task (i.e., the target pending task) according to the task's priority, thus effectively avoiding simulated I2C conflicts.
[0014] In a further embodiment, step S2 includes the following steps:
[0015] S21. Control the current pending task to enter the execution state, and sequentially send one frame of data from the corresponding first I2C data to the I2C sending task.
[0016] S22. Control the current pending task to enter a suspended state;
[0017] S23. The I2C sending task receives the current frame of the first I2C data and sends it to the corresponding I2C slave device according to the slave device address therein;
[0018] S24. After the current frame is sent, determine whether there is a task with a higher priority than the current task among the ready tasks. If there is, determine it as the target task and proceed to step S3; otherwise, return to step S21.
[0019] In a further implementation, step S22 specifically involves calling the vTaskSuspend() API function to put the current pending task into a suspended state.
[0020] In a further implementation, the step of executing the target task according to the preset strategy to send multiple rounds of single-frame data includes the following steps:
[0021] A. Control the target pending task to enter the execution state, and sequentially send one frame of data from the corresponding second I2C data to the I2C sending task;
[0022] B. Control the target pending task to enter a suspended state;
[0023] C. The I2C sending task receives the current frame of the second I2C data and sends it to the corresponding I2C slave device according to the slave device address therein;
[0024] D. Determine whether the second I2C data has been sent. If yes, proceed to step S4; otherwise, return to step A.
[0025] This solution sets a preset strategy: after the current pending task enters the execution state, it sequentially sends each frame of data in the corresponding first I2C data to the I2C sending task for data transmission. Simultaneously, after the current frame is sent, it checks whether there is a pending task with a higher priority than the current pending task. If so, it controls the target pending task to enter the execution state and continues to execute the current pending task after the corresponding second I2C data is sent. This ensures that low-priority I2C data is not interrupted after the current frame is sent, and because the complete transmission of a single frame of data can guarantee data integrity, it avoids being interrupted by high-priority I2C data, which could lead to errors in the data received by the corresponding I2C device.
[0026] In a further implementation, step B specifically involves calling the vTaskSuspend() API function to put the target task into a suspended state.
[0027] The present invention also provides an analog I2C communication system, including an MCU and multiple slave devices; the MCU includes multiple data task modules corresponding one-to-one with the slave devices, as well as an I2C transmission task module, an analog I2C module, and an I / O interface; the data task module, the I2C transmission task module, and the analog I2C module are connected in sequence, and the analog I2C module is connected to the slave devices through the I / O interface;
[0028] The data task module is used to generate I2C data corresponding to the slave device;
[0029] The I2C sending task module is used to receive the I2C data and control the data sending according to the corresponding slave device.
[0030] The simulated I2C module is used to control the level of the I / O interface to simulate the I2C communication protocol, thereby enabling communication between the MCU and multiple slave devices.
[0031] In a further implementation, the control for receiving the I2C data and sending data according to the corresponding slave device specifically includes:
[0032] S1. Set up an I2C send task and configure it to have a higher priority than the data tasks corresponding to all I2C slave devices;
[0033] S2. According to the preset strategy and the I2C sending task, execute the current pending task to send multiple rounds of single-frame data;
[0034] S3. When a higher-priority target task is detected, determine whether the current round of single-frame data transmission is complete. If so, execute the target task according to the preset strategy to send multiple rounds of single-frame data; otherwise, continue to execute the current round of single-frame data.
[0035] S4. When the target pending task is detected to have been completed, resume the execution of the current pending task.
[0036] In a further implementation, the priority of the data tasks generated by the plurality of data task modules is equivalent to the priority of the corresponding slave device. Attached Figure Description
[0037] Figure 1 This is a flowchart of a simulated I2C communication method provided in Embodiment 1 of the present invention;
[0038] Figure 2 This is a system framework diagram of a simulated I2C communication system provided in Embodiment 2 of the present invention. Detailed Implementation
[0039] The embodiments of the present invention are described in detail below with reference to the accompanying drawings. The embodiments are given for illustrative purposes only and should not be construed as limiting the present invention. The accompanying drawings are for reference and illustration only and do not constitute a limitation on the scope of patent protection of the present invention, because many changes can be made to the present invention without departing from the spirit and scope of the present invention.
[0040] Example 1
[0041] This invention provides a simulated I2C communication method, such as... Figure 1 As shown, in this embodiment, steps S1 to S4 are included:
[0042] S1. Set up an I2C send task and configure it as the highest priority;
[0043] In this embodiment, configuring it as the highest priority specifically means: configuring the priority of the I2C sending task to be higher than the priority of the data tasks corresponding to all I2C slave devices.
[0044] Task status includes running, ready, blocked, and suspended.
[0045] In this embodiment, the I2C transmission task is configured with a higher priority than the data tasks corresponding to all I2C slave devices, ensuring that only one task is running at any given time in the RTOS. When a data task corresponding to one of the slave devices calls the simulated I2C module to send I2C data, even if data tasks of other slave devices are ready, they cannot interrupt the current I2C data transmission. Only after the current frame of the current pending task has been sent will the I2C transmission task receive the data transmission request from the higher-priority data task (i.e., the target pending task) according to the task's priority, thereby effectively avoiding simulated I2C conflicts.
[0046] S2. According to the preset strategy and I2C sending task, execute the current pending task to send multiple rounds of single-frame data, including steps S21 to S24:
[0047] S21. Control the current pending task to enter the execution state, and send one frame of data from the corresponding first I2C data to the I2C sending task in sequence.
[0048] S22. Control the current pending task to enter the suspended state, specifically by calling the vTaskSuspend() API function to put the current pending task into the suspended state.
[0049] S23, The I2C sending task receives the current frame of the first I2C data and sends it to the corresponding I2C slave device according to the slave device address in it;
[0050] S24. After the current frame is sent, determine whether there is a task with a higher priority than the current task among the ready tasks. If there is, identify it as the target task and proceed to step S3; otherwise, return to step S21.
[0051] S3. When a higher-priority target task is detected, determine whether the current round of single-frame data transmission is complete. If so, execute the target task according to the preset strategy to send multiple rounds of single-frame data; otherwise, continue to execute the current round of single-frame data.
[0052] In this embodiment, the process of sending multiple rounds of single-frame data according to a preset strategy to execute a target task includes steps A to D:
[0053] A. Control the target pending task to enter the execution state, and send one frame of data from the corresponding second I2C data to the I2C sending task in sequence;
[0054] B. Control the target task to enter the suspended state, specifically by calling the vTaskSuspend() API function to put the target task into the suspended state.
[0055] C. The I2C sending task receives the current frame of the second I2C data and sends it to the corresponding I2C slave device according to the slave device address in the frame.
[0056] D. Determine whether the second I2C data has been sent. If yes, proceed to step S4; otherwise, return to step A.
[0057] This embodiment sets a preset strategy. After the current pending task enters the execution state, each frame of data in the corresponding first I2C data is sent to the I2C sending task in sequence. At the same time, after the current frame is sent, it is determined whether there is a pending task with a higher priority than the current pending task. If so, the target pending task is controlled to enter the execution state, and the current pending task continues to be executed after the corresponding second I2C data is sent. This ensures that low-priority I2C data is interrupted only after the current frame is sent. Since the complete transmission of a single frame of data can guarantee data integrity, it avoids the corresponding I2C device receiving incorrect data after being interrupted by high-priority I2C data.
[0058] S4. Once the target pending task is detected to have been completed, resume the execution of the current pending task.
[0059] This invention improves upon the existing preemptive task mechanism by setting the I2C sending task to multiple rounds of single-frame data transmission, with only one frame of data sent in each round. When a target task with a higher priority than the current task is detected, the target task is executed only after the current round of single-frame data transmission is completed. In this way, when the slave devices are performing simulated I2C communication, the communication between the low-priority slave device and the master will not be arbitrarily interrupted by the high-priority slave device. The single-frame data is complete during the data transmission process, and the high-priority target task can be executed quickly.
[0060] Example 2
[0061] In this embodiment, the reference numerals in the accompanying drawings include: master control device 01, slave devices B1 to Bn, data task modules A1 to An, I2C sending task module 02, analog I2C module 03, I / O1, and I / O2.
[0062] This invention also provides an analog I2C communication system, see [link to documentation]. Figure 2 This includes the main control device 01 and multiple slave devices (such as...) Figure 2The master control device 01 includes multiple data task modules corresponding one-to-one with the slave devices, as well as an I2C sending task module 02, an analog I2C module 03, and an I / O interface. The data task module, the I2C sending task module 02, and the analog I2C module 03 are connected in sequence, and the analog I2C module 03 is connected to the slave devices through the I / O interface.
[0063] The data task module is used to generate I2C data corresponding to the slave device; such as Figure 2 Data task modules A1 to An (n>1) are defined in the dataset.
[0064] I2C transmission task module 02 is used to receive I2C data and control the data transmission according to its corresponding slave device;
[0065] The simulated I2C module 03 is used to control the level of the I / O interface to simulate the I2C communication protocol, enabling communication between the master device 01 and multiple slave devices.
[0066] The I / O interfaces include I / O1 and I / O2.
[0067] In this embodiment, the control for receiving I2C data and sending data according to its corresponding slave device specifically includes:
[0068] S1. Set up an I2C send task and configure it to have a higher priority than the data tasks corresponding to all I2C slave devices;
[0069] S2. Based on the preset strategy and I2C sending task, execute the current pending task to send multiple rounds of single-frame data;
[0070] S3. When a higher-priority target task is detected, determine whether the current round of single-frame data transmission is complete. If so, execute the target task according to the preset strategy to send multiple rounds of single-frame data; otherwise, continue to execute the current round of single-frame data.
[0071] S4. Once the target pending task is detected to have been completed, resume the execution of the current pending task.
[0072] In this embodiment, the priority of the data tasks generated by the multiple data task modules is equivalent to the priority of the corresponding slave device.
[0073] The communication system provided in this embodiment uses various modules to implement each step of the communication method, providing a hardware foundation for the communication method and facilitating its implementation.
[0074] The above embodiments are preferred embodiments of the present invention, but the embodiments of the present invention are not limited to the above embodiments. Any changes, modifications, substitutions, combinations, or simplifications made without departing from the spirit and principle of the present invention shall be considered equivalent substitutions and shall be included within the protection scope of the present invention.
Claims
1. A simulated I2C communication method, characterized in that, Including the following steps: S1. Set up an I2C send task and configure it as the highest priority; S2. According to the preset strategy and the I2C sending task, execute the current pending task to send multiple rounds of single-frame data; S3. When a higher-priority target task is detected, determine whether the current round of single-frame data transmission is complete. If so, execute the target task according to the preset strategy to send multiple rounds of single-frame data; otherwise, continue to execute the current round of single-frame data. S4. When the target pending task is detected to have been completed, resume the execution of the current pending task; Step S2 includes the following steps: S21. Control the current pending task to enter the execution state, and sequentially send one frame of data from the corresponding first I2C data to the I2C sending task. S22. Control the current pending task to enter a suspended state; S23. The I2C sending task receives the current frame of the first I2C data and sends it to the corresponding I2C slave device according to the slave device address therein; S24. After the current frame is sent, determine whether there is a task with a higher priority than the current task among the ready tasks. If there is, determine it as the target task and proceed to step S3; otherwise, return to step S21.
2. The simulated I2C communication method as described in claim 1, characterized in that, Specifically, configuring it as the highest priority means setting the priority of the I2C sending task to be higher than the priority of the data tasks corresponding to all I2C slave devices.
3. The simulated I2C communication method as described in claim 1, characterized in that, Step S22 specifically involves calling the vTaskSuspend() API function to put the current pending task into a suspended state.
4. The simulated I2C communication method as described in claim 1, characterized in that, The step of executing the target task according to the preset strategy and sending multiple rounds of single-frame data includes the following steps: A. Control the target pending task to enter the execution state, and sequentially send one frame of data from the corresponding second I2C data to the I2C sending task; B. Control the target pending task to enter a suspended state; C. The I2C sending task receives the current frame of the second I2C data and sends it to the corresponding I2C slave device according to the slave device address therein; D. Determine whether the second I2C data has been sent. If yes, proceed to step S4; otherwise, return to step A.
5. The simulated I2C communication method as described in claim 4, characterized in that, Step B specifically involves calling the vTaskSuspend() API function to put the target task into a suspended state.
6. A simulated I2C communication system, used to implement the simulated I2C communication method as described in any one of claims 1 to 5, characterized in that: It includes an MCU and multiple slave devices; the MCU includes multiple data task modules corresponding one-to-one with each of the slave devices, as well as an I2C transmission task module, an analog I2C module, and an I / O interface; the data task module, the I2C transmission task module, and the analog I2C module are connected in sequence, and the analog I2C module is connected to the slave device through the I / O interface; The data task module is used to generate data tasks corresponding to the I2C data of the slave device; The I2C sending task module is used to receive the I2C data and control the data sending according to the corresponding slave device. The simulated I2C module is used to control the level of the I / O interface to simulate the I2C communication protocol, thereby enabling communication between the MCU and multiple slave devices.
7. The analog I2C communication system as described in claim 6, characterized in that, The control for receiving the I2C data and sending data according to the corresponding slave device specifically includes: S1. Set up an I2C send task and configure it to have a higher priority than the data tasks corresponding to all the I2C slave devices; S2. According to the preset strategy and the I2C sending task, execute the current pending task to send multiple rounds of single-frame data; S3. When a higher priority target task is detected, determine whether the current round of single-frame data transmission is completed. If so, execute the target task according to the preset strategy to send multiple rounds of single-frame data. Otherwise, continue executing the current single-frame data; S4. When the target pending task is detected to have been completed, resume the execution of the current pending task.
Citation Information
Patent Citations
Subway digital audio-video broadcasting method and system based compressed sensing
CN107070571A
Reuse method and device for OLT optical module pin, and OLT optical module
CN107332553A