Method for simulating IIC communication, single chip microcomputer product and storage medium

By breaking down IIC communication into bit-level steps within the timer interrupt task and managing them with a state machine, the latency problem caused by simulated IIC communication is solved, improving the system's real-time performance and task response efficiency.

CN118550856BActive Publication Date: 2024-11-01POWEROAK INNOVATION CO
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411002798.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-07-25
Publication Date
2024-11-01
Estimated Expiration
2044-07-25

AI Technical Summary

Technical Problem

Existing analog IIC communication methods are prone to causing significant delays, which can prevent other tasks of the same priority from responding in a timely manner, especially affecting the real-time performance of the system under high-frequency communication requirements.

Method used

Simulated IIC communication is implemented in a timer interrupt task. The data transmission task is broken down into bit-level steps and managed in the form of a state machine. Data writing or reading is performed through the IIC interrupt task, reducing task waiting time.

Benefits of technology

It improves the flexibility and efficiency of task scheduling, significantly enhances the real-time performance of the system, and reduces the latency impact of IIC communication on the main polling task.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118550856B_ABST
    Figure CN118550856B_ABST
Patent Text Reader

Abstract

The embodiment of the present invention discloses a method for simulating IIC communication, a single-chip microcomputer product and a storage medium, wherein the method includes: setting the parameter information required for simulating IIC communication for an IIC interrupt task, and when the IIC communication mode is writing, storing the data to be written in the write data storage address of the IIC interrupt task; entering the IIC interrupt task based on a preset timer, and performing the following steps: if the IIC communication mode is writing, executing a preset IIC simulation write program, and sending the data to be written to a slave; if the IIC communication mode is reading, executing a preset IIC simulation read program, obtaining the data to be read from the slave, and storing the data to be read in a preset read data storage address. The method of the present invention fundamentally solves the impact of the time consumption of simulating IIC communication on the real-time performance of the system.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of communication technology, and in particular to a method for simulating IIC communication, a single-chip microcomputer product and a storage medium. Background Art

[0002] With the development of electronic integration technology, more and more IC chips with special functions have appeared in the field of embedded electronics, and the layout of PCB boards has become more and more compact. There is often a main control MCU with several special function IC chips. The communication method between the main control MCU and each special IC also adopts the relatively simple topology IIC (Inter-Integrated Circuit, integrated circuit bus) communication.

[0003] Most 32-bit MCUs on the market currently have built-in IIC peripheral functions, but usually only one IIC, and its IO pin position is fixed, which is not convenient for porting. Therefore, when implementing IIC communication in software, the method of simulating IIC timing with a general IO port is often used. When traditional analog IIC communication is implemented inside a logical task, it is necessary to wait for the communication data to be sent before continuing to execute other logical tasks (except for high-priority tasks, which can interrupt low-priority tasks when triggered). When the main polling task has a low priority but relatively strict real-time requirements, this method will introduce a lot of delay consumption and will be difficult to adapt.

[0004] For example, in the field of energy storage, energy storage products usually include inverters and BMS (Battery Management System). BMS often involves EEPROM (Electrically Erasable Programmable Read-Only Memory) data storage and reading and writing, and AFE (Analog Front-End) sampling data (collecting single-cell voltage and current and other parameters). Both are usually IIC communications, and to ensure the real-time nature of the sampling data, AFE sampling data requires high-frequency (hundreds of ms level) communication reading; inverters usually involve high-priority tasks, and the real-time requirements for the tasks are extremely strict. In the past, the inverter and BMS were two independent modules controlled by two separate MCUs, and they would not have much impact on each other. However, as products develop towards a highly integrated direction, it is necessary to merge the functions of the two into the same MCU. The high-frequency communication reading of AFE sampling data requires each reading to take more than ten to dozens of milliseconds, and the inverter has extremely strict requirements on the real-time performance of the task. The high-frequency IIC communication (traditional analog IIC communication) introduces a lot of delay consumption, which is bound to be unresponsive to other tasks of the same priority. Therefore, the traditional IO port analog IIC method of waiting for data to be sent will be difficult to apply. Summary of the invention

[0005] The embodiments of the present invention aim to provide a method for simulating IIC communication, a single-chip microcomputer product and a storage medium, so as to solve the problem that the method for simulating IIC communication in the prior art easily causes a large amount of delay loss, thereby making it impossible for other tasks of the same priority to respond in time.

[0006] To solve the above technical problems, the embodiments of the present invention provide the following technical solutions:

[0007] According to one aspect of the present invention, there is provided a method for simulating IIC communication, the method comprising:

[0008] Setting the parameter information required for the IIC interrupt task to simulate IIC communication, and when the IIC communication mode is write, storing the data to be written in the write data storage address of the IIC interrupt task;

[0009] Enter the IIC interrupt task based on a preset timer and execute the following steps: if the IIC communication mode is writing, execute the preset IIC simulation writing program to send the data to be written to the slave; if the IIC communication mode is reading, execute the preset IIC simulation reading program to obtain the data to be read from the slave, and store the data to be read in the preset reading data storage address.

[0010] Optionally, each time the IIC interrupt task is executed once, the level of the clock line SCL is flipped once, and each time the IIC interrupt task is executed twice, 1 bit of data is read or written.

[0011] Optionally, the IIC interrupt task writing 1 bit of data includes: in a first interrupt, controlling the clock line SCL to flip from a high level to a low level, and sending 1 bit of data to the data line SDA, and in a second interrupt, controlling the clock line SCL to flip from a low level to a high level, so that the slave reads 1 bit of data from the data line SDA;

[0012] The IIC interrupt task to read 1 bit of data includes: in the first interrupt, the clock line SCL is controlled to flip from a high level to a low level, so that the slave sends 1 bit of data to the data line SDA; in the second interrupt, the clock line SCL is controlled to flip from a low level to a high level, and 1 bit of data is read from the data line SDA.

[0013] Optionally, the IIC analog write program and the IIC analog read program are both implemented in a state machine switching manner according to the IIC communication mechanism.

[0014] Optionally, the IIC analog write program completes the analog write of the data to be written after successively going through the analog start signal timing, the analog "device address" sending timing, the analog "register address" sending timing, the analog "data content" sending timing and the analog stop signal timing; the IIC analog read program completes the analog read of the data to be read after successively going through the analog start signal timing, the analog "device address" sending timing, the analog "register address" sending timing, the analog start signal timing again, the analog "device address" sending timing again, the analog "data content" reading timing and the analog stop signal timing.

[0015] Optionally, the IIC analog write program also includes a write end timing, and the program enters the write end timing after the analog stop signal timing in the IIC analog write program is completed; the IIC analog read program also includes a read end timing, and the program enters the read end timing after the analog stop signal timing in the IIC analog read program is completed. When the IIC analog write program executes to the write end timing or when the IIC analog read program executes to the read end timing, the IIC interrupt task is exited.

[0016] Optionally, the execution duration of the IIC interrupt task is much shorter than the period of the timer.

[0017] Optionally, within each cycle of the timer, the IIC interrupt task is executed first, and then the main polling task is executed.

[0018] According to another aspect of the present invention, a single-chip microcomputer product is provided, comprising a memory, a processor and a computer program stored and running on the memory, wherein when the processor executes the program, the steps of any one of the above-mentioned methods for simulating IIC communication are implemented.

[0019] According to another aspect of the present invention, a computer-readable storage medium is provided, wherein the computer-readable storage medium stores a computer program. When the computer program is executed by a processor, the processor executes the steps of any one of the above-mentioned methods for simulating IIC communication.

[0020] The beneficial effect of the embodiment of the present invention is: different from the prior art, in the embodiment of the present invention, a method for simulating IIC communication is provided, firstly, the parameter information required for simulating IIC communication in the IIC interrupt task is set, and when the IIC communication mode is write, the data to be written is stored in the write data storage address of the IIC interrupt task; then, based on the preset timer, the IIC interrupt task is entered to execute the preset IIC simulation write program or IIC simulation read program. The method of the present invention improves the traditional method of simulating IIC communication in the main polling task to be implemented in the timer interrupt task, and at the same time, a complete data sending task is subdivided into the bit level, decomposed into a plurality of minimum unit BIT bit steps, and managed in the form of a state machine, so that the execution of other tasks in the main polling task (low priority) does not need to wait for the completion of IIC communication data transmission, fundamentally solves the time consumption of simulating IIC communication on the real-time performance of the system, and significantly improves the flexibility and efficiency of task scheduling. In addition, since the time consumption of the IIC interrupt task is much less than the response requirement time of the main polling task, the added IIC interrupt task has little effect on the real-time performance of the main polling task of the entire system. BRIEF DESCRIPTION OF THE DRAWINGS

[0021] One or more embodiments are exemplarily described by pictures in the corresponding drawings, and these exemplified descriptions do not constitute limitations on the embodiments. Elements with the same reference numerals in the drawings represent similar elements, and unless otherwise stated, the figures in the drawings do not constitute proportional limitations.

[0022] Figure 1 is a timing diagram of IIC communication provided according to an embodiment;

[0023] Figure 2 It is an IIC communication process diagram of reading data frames and writing data frames provided according to an embodiment;

[0024] Figure 3 is a flow chart of a method for simulating IIC communication according to an embodiment;

[0025] Figure 4 is an execution flow chart of an IIC interrupt task provided according to an embodiment;

[0026] Figure 5 1 is a schematic diagram of an execution flow of an IIC simulation writing program provided according to an embodiment;

[0027] Figure 6 1 is a schematic diagram of an execution flow of an IIC simulation reading program provided according to an embodiment;

[0028] Figure 7 It is a schematic diagram of the execution flow of simulating the sending of a "device address" according to an embodiment;

[0029] Figure 8 It is an execution timing diagram of the IIC interrupt task and the main polling task in each timer cycle provided by the embodiment;

[0030] Fig. 9 It is a structural schematic diagram of a single-chip microcomputer product provided according to an embodiment. DETAILED DESCRIPTION

[0031] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0032] In addition, the technical features involved in the various embodiments of the present invention described below can be combined with each other as long as they do not conflict with each other.

[0033] It should be noted that the steps shown in the flowcharts of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and that, although a logical order is shown in the flowcharts, in some cases, the steps shown or described can be executed in an order different from that shown here.

[0034] The present invention provides a method for simulating IIC communication and a single-chip microcomputer product for implementing the method for simulating IIC communication. Different from the traditional method of implementing simulated IIC communication in a main polling task, the present invention implements simulated IIC communication in a timer interrupt task, subdivides a complete data transmission task into the bit level, decomposes it into multiple minimum unit BIT bit steps, and manages it in the form of a state machine. By decoupling and distributing the communication tasks, combined with an efficient state machine management mechanism, the contradiction between real-time performance and communication efficiency is successfully resolved. The following briefly introduces some basic knowledge of IIC communication to better understand the technical solution of the present invention.

[0035] The hardware topology of IIC communication consists of data line SDA and clock line SCL. The timing diagram of its communication is as follows: Figure 1 As shown in the figure, it mainly includes the start bit, 8 bits of data transmission (high first and then low), the response bit and the stop bit. In the idle state, the data line SDA and the clock line SCL remain in a high level state, and the data line SDA flips from a high level to a low level to send a start signal. When the IIC bus is transmitting data, when the clock line SCL is at a low level, the sender sends 1 bit of data to the data line SDA, during which the signal on the data line SDA is allowed to change; and when the clock line SCL is at a high level, the receiver reads 1 bit of data from the data line SDA, during which the signal on the data line SDA is not allowed to change and must remain stable. Every time the sender sends a byte of data, it releases the data line SDA during the 9th clock pulse, and then the receiver feeds back a response signal. When the feedback response signal is 0, it means that the receiver has successfully received the data; when the feedback response signal is 1, it means that the receiver has not successfully received the byte. When the clock line SCL is at a high level, the data line SDA flips from a low level to a high level, and a stop signal is sent.

[0036] See also Figure 2 , Figure 2It is an IIC communication process diagram for reading data frames and writing data frames provided according to the embodiment. Among them, the process of reading data frames is: the IIC host first sends a 1-bit start signal, then sends a byte of data (including 7-bit slave device address bits + 1-bit write mode bit), and receives a 1-bit slave response signal bit; the IIC host then sends a byte of slave register address, and then receives a 1-bit slave response signal bit; then, the IIC host changes the communication mode (the host changes from sending to receiving, and the slave changes from receiving to sending), resends a start signal and a byte of data (including 7-bit slave device address bits + 1-bit read mode bit), and after receiving a 1-bit slave response signal bit, it can receive 1 byte of data. After receiving, a 1-bit non-response signal (indicating that no data will be received) is fed back, and then a 1-bit stop signal is sent to end the data frame reading process. The process of writing data frames is similar to the process of reading data frames, and will not be repeated here.

[0037] Embodiment 1

[0038] See also Figure 3 , is a flow chart of a method for simulating IIC communication according to an embodiment. The method is applicable to single-chip microcomputer products, and the method includes:

[0039] Step S301, setting the parameter information required for the IIC interrupt task to simulate the IIC communication, and when the IIC communication mode is write, storing the data to be written into the write data storage address of the IIC interrupt task.

[0040] In the embodiment of the present invention, the single-chip microcomputer product is used as the host, and at least two system tasks are running in it, one is a main polling task based on time slices, and the other is an interrupt task. In order to solve the problem that IIC communication takes too long in the main polling task, making other tasks unable to respond in time, in the embodiment of the present invention, a fixed-period timer is preset in the system, and the simulated IIC communication is implemented in the interrupt task based on the timer.

[0041] Before the host and the slave communicate in IIC, it is necessary to determine the parameter information required for simulating IIC communication. The parameter information may include IIC communication mode, slave device address, slave register address, read data storage address, write data storage address, etc., wherein the IIC communication mode includes three modes: write, read and idle. Generally, these parameter information can be set by a master polling task, and when the IIC communication mode is write, the data to be written is also stored in the write data storage address of the IIC interrupt task. For some IIC communication tasks with a fixed period, some or all of the above parameter information can also be set in advance in the program.

[0042] Step S302, enter the IIC interrupt task based on the preset timer, and execute the following steps: if the IIC communication mode is write, execute the preset IIC analog write program to send the data to be written to the slave; if the IIC communication mode is read, execute the preset IIC analog read program to obtain the data to be read from the slave, and store the data to be read in the read data storage address.

[0043] In the embodiment, the timer is continuously turned on after the microcontroller product is started, and its cycle can be set according to the system's requirements for the IIC communication baud rate. Generally, the execution time of the IIC interrupt task (split into bit-level state machine step tasks: one bit at a time, IO port flipping) is much shorter than the timer cycle. In each timer cycle, the IIC interrupt task is executed first, and after the IIC interrupt task exits, it returns to the main polling task and continues to execute the main polling task until the next timer cycle.

[0044] See also Figure 4 , Figure 4 Detailed description of the execution flow chart of the IIC interrupt task provided in the embodiment, including:

[0045] Step 401, obtaining the IIC communication method.

[0046] In an embodiment, the current IIC communication mode may be obtained through a main polling task.

[0047] Step 402, when the IIC communication mode is writing, executing the IIC simulation writing program.

[0048] Step 403, when the IIC communication mode is read, execute the IIC simulation read program.

[0049] Step 404, when the IIC communication mode is idle, exit the IIC interrupt task.

[0050] As mentioned above, the read data frame and write data frame in the IIC communication protocol contain multiple steps respectively, and each step needs to transmit multiple bits of data, and the transmission process of each bit of data is: control the clock line SCL to perform two level flips, after the first flip, when the clock line SCL is at a low level, the sender sends 1 bit of data to the data line SDA; after the second flip, when the clock line SCL is at a high level, the receiver reads 1 bit of data from the data line SDA. Therefore, in the specific implementation, on the basis of simulating the data transmission timing of each bit in the IIC simulation write program or the IIC simulation read program, each step can be further implemented in the form of state machine switching according to the IIC communication mechanism.

[0051] In the embodiment, the corresponding simulation timing is designed in the IIC simulation writing program / IIC simulation reading program based on each step of writing data frame / reading data frame in the IIC communication mechanism, and the corresponding state value is defined for each simulation timing. When the previous simulation timing is completed, the next simulation timing is executed until all simulation timings are completed, thus realizing the simulation writing / reading of data.

[0052] See also Figure 5 , Figure 5 1 is a schematic diagram of an execution flow of an IIC analog write program provided according to an embodiment, including:

[0053] Step 501 , determine whether the last data to be written has been sent, if so, proceed to step 502 , if not, proceed to step 504 .

[0054] In the embodiment, the IIC analog write program completes the analog writing of data after the analog start signal timing, the analog "device address" sending timing, the analog "register address" sending timing, the analog "data content" sending timing and the analog stop signal timing. In order to realize the state switching, the state value of each analog timing contained in the IIC analog write program is defined as follows: the analog start signal timing corresponds to WStep_0, the analog "device address" sending timing corresponds to WStep_1, the analog "register address" sending timing corresponds to WStep_2, the analog "data content" sending timing corresponds to WStep_3, the analog stop signal timing corresponds to WStep_4, and the write end timing corresponds to WStep_end. In addition, the variable WStep is used to record the state value of the current write execution timing.

[0055] Based on the above settings, determine whether the last data to be written has been sent, that is, determine whether the current write execution timing WStep is the write end timing WStep_end. If so, it means that the last data to be written has been written. If not, it means that the last data to be written has not been written. Then enter the corresponding simulation timing according to the current write execution timing WStep.

[0056] Step 502, determine whether there is data to be sent, if so, go to step 503, if not, go to step 504.

[0057] In an embodiment, it can be determined whether there is data to be written according to the write data storage address. If so, a new round of data writing simulation process starts. If not, it goes to step 504, that is, entering the writing end sequence.

[0058] Step 503 , set the current write execution timing WStep to WStep_0 , and proceed to step 504 .

[0059] Step 504, determine the state value of the current write execution sequence WStep, and enter the corresponding simulation sequence.

[0060] Step 505, when the current write execution sequence WStep=WStep_0, execute the simulated start signal sequence. When the sequence is completed, set WStep to WStep_1, and set the number of bits to be sent for the sequence corresponding to WStep_1 (for example, if the sequence corresponding to WStep_1 is the simulated "device address" sending sequence, the number of bits to be sent is 8 bits), and exit this timer interrupt task; if not completed, exit this timer interrupt task (wait for the next interrupt, and continue to enter this sequence to complete the start signal simulation). The start signal is a jump signal, so the simulated start signal sequence can generally be completed in one interrupt task.

[0061] Step 506, when the current write execution sequence WStep=WStep_1, execute the simulated "device address" sending sequence. When the sequence is completed, set WStep to WStep_2, and set the number of bits to be sent in the sequence corresponding to WStep_2, and exit this timer interrupt task; if it is not completed, update the number of bits that have been sent in this sequence, and exit this timer interrupt task (wait for the next interrupt, and continue to enter this sequence to send the next bit).

[0062] Step 507, when the current write execution sequence WStep=WStep_2, execute the simulated "register address" sending sequence. When the sequence is completed, set WStep to WStep_3, and set the number of bits to be sent in the sequence corresponding to WStep_3, and exit this timer interrupt task; if it is not completed, update the number of bits that have been sent in this sequence, and exit this timer interrupt task (wait for the next interrupt, and continue to enter this sequence to send the next bit).

[0063] Step 508, when the current write execution sequence WStep=WStep_3, execute the simulated "data content" sending sequence. When the sequence is completed, set WStep to WStep_4 and exit the current timer interrupt task. If not completed, update the number of bits that have been sent in this sequence and exit the current timer interrupt task (wait for the next interrupt and continue to enter this sequence to send the next bit).

[0064] Step 509, when the current write execution sequence WStep=WStep_4, execute the simulated stop signal sequence. When the sequence is completed, set WStep to WStep_end and exit the current timer interrupt task. If it is not completed, exit the current timer interrupt task (wait for the next interrupt, continue to enter the current sequence and complete the stop signal simulation). The stop signal is a jump signal, so the simulated stop signal sequence can generally be completed in one interrupt task.

[0065] Step 510, when the current write execution timing WStep=WStep_end, exit the IIC interrupt service.

[0066] See also Figure 6 , Figure 6 1 is a schematic diagram of an execution flow of an IIC simulation reading program provided according to an embodiment, including:

[0067] Step 601, determine whether the last data to be read has been read, if so, go to step 602, if not, go to step 603.

[0068] In the embodiment, the IIC read-write program completes the simulated reading of data after successively simulating the start signal timing, simulating the "device address" sending timing, simulating the "register address" sending timing, simulating the start signal timing again, simulating the "device address" sending timing again, simulating the "data content" reading timing and simulating the stop signal timing. In order to realize the state switching, the state value of each simulated timing contained in the IIC simulated reading program is defined as follows: the simulated start signal timing corresponds to RStep_0, the simulated "device address" sending timing corresponds to RStep_1, the simulated "register address" sending timing corresponds to RStep_2, the simulated start signal timing corresponds to RStep_3 again, the simulated "device address" sending timing corresponds to RStep_4 again, the simulated "data content" reading timing corresponds to RStep_5, the simulated stop signal timing corresponds to RStep_6, and the read end timing corresponds to RStep_end. In addition, the variable RStep is used to record the state value of the current read execution timing.

[0069] Based on the above settings, determine whether the last data to be read has been read, that is, determine whether the current read execution timing RStep is the reading end timing RStep_end. If so, it means that the last data to be read has been read. If not, it means that the last data to be read has not been read. Then enter the corresponding simulation timing according to the current read execution timing RStep.

[0070] Step 602 , set the current read execution timing RStep to RStep_0 , and proceed to step 603 .

[0071] The last data to be read has been read, and the current read execution timing RStep is reset to RStep_0 to start a new round of data reading simulation process.

[0072] Step 603, determine the state value of the current read execution sequence RStep and enter the corresponding simulation sequence.

[0073] Step 604, when the current read execution sequence RStep=RStep_0, execute the simulated start signal sequence. When the sequence is completed, set RStep to RStep_1, and set the number of bits to be sent in the sequence corresponding to RStep_1, and exit this timer interrupt task; if not completed, exit this timer interrupt task (wait for the next interrupt, continue to enter this sequence to complete the start signal simulation). The start signal is a jump signal, so the simulated start signal sequence can generally be completed in one interrupt task.

[0074] Step 605, when the current read execution sequence RStep=RStep_1, execute the simulated "device address" sending sequence. When the sequence is completed, set RStep to RStep_2, and set the number of bits to be sent in the corresponding sequence of RStep_2, and exit this timer interrupt task; if it is not completed, update the number of bits that have been sent in this sequence, and exit this timer interrupt task (wait for the next interrupt, and continue to enter this sequence to send the next bit).

[0075] Step 606, when the current read execution sequence RStep=RStep_2, execute the simulated "register address" sending sequence. When the sequence is completed, set RStep to RStep_3 and exit the current timer interrupt task. If not completed, update the number of bits that have been sent in this sequence and exit the current timer interrupt task (wait for the next interrupt and continue to enter this sequence to send the next bit).

[0076] Step 607, when the current read execution sequence RStep=RStep_3, execute the simulation of the start signal sequence again. When the sequence is completed, set RStep to RStep_4, and set the number of bits to be sent in the sequence corresponding to RStep_4, and exit the timer interrupt task; if it is not completed, exit the timer interrupt task (wait for the next interrupt, continue to enter the sequence to complete the start signal simulation again).

[0077] Step 608, when the current read execution sequence RStep=RStep_4, execute the simulation of the "device address" sending sequence again. When the sequence is completed, set RStep to RStep_5, and set the number of bits to be read in the corresponding sequence of RStep_5, and exit this timer interrupt task; if it is not completed, update the number of bits that have been sent in this sequence, and exit this timer interrupt task (wait for the next interrupt, and continue to enter this sequence to send the next bit).

[0078] Step 609, when the current read execution sequence RStep=RStep_5, execute the simulated "data content" read sequence. When the sequence is completed, set RStep to RStep_6 and exit the current timer interrupt task. If not completed, update the number of bits that have been read in this sequence and exit the current timer interrupt task (wait for the next interrupt and continue to enter this sequence to read the next bit).

[0079] Step 610, when the current read execution sequence RStep=RStep_6, execute the simulated stop signal sequence. When the sequence is completed, set RStep to RStep_end and exit the current timer interrupt task. If it is not completed, exit the current timer interrupt task (wait for the next interrupt, continue to enter this sequence to complete the stop signal simulation). The stop signal is a jump signal, so the simulated stop signal sequence can generally be completed in one interrupt task.

[0080] Step 611, when the current read execution timing RStep=RStep_end, exit the timer interrupt task.

[0081] from Figure 5 and Figure 6 It can be seen that the IIC analog write / read timing includes "analog start signal timing", "analog data send timing" and "analog stop signal timing".

[0082] Specifically, the execution process of simulating the start signal timing is: set the clock line SCL and the data line SDA to a high level for a period of time, then pull the data line SDA level down, and the start signal simulation is completed. The execution process of simulating the stop signal timing is: set the clock line SCL and the data line SDA to a low level state for a period of time, then pull the data line SCL level up, and the stop signal simulation is completed.

[0083] The simulation data sending sequence is similar to the simulation data reading sequence. The following uses the simulation "device address" sending sequence to illustrate the execution process of the simulation data sending sequence. Figure 7 , Figure 7 It is a specific step diagram of simulating the "device address" sending timing according to the embodiment, including:

[0084] Step 701 , determine whether the number of transmitted data bits is less than 8, if so, proceed to step 702 , if not, release the data line SDA and proceed to step 705 .

[0085] The simulated "device address" sending sequence includes the "device address" sending process of steps 702 to 704 and the response signal receiving process of steps 705 to 707.

[0086] When the number of sent data bits is less than 8, it means that the 1-byte "device address" (including the read / write mode bit) has not been sent yet, and the process goes to step 702. The host acts as the transmitter and the slave acts as the receiver, and sends data bit by bit from high to low. When the number of sent data bits reaches 8, it means that the 1-byte "device address" has been sent, and the host releases the data line SDA, and the process goes to step 705 to start the response signal receiving process. In the initial state, the number of sent data bits is 0.

[0087] Step 702: if the current interrupt is the first interrupt, then go to step 703; if the current interrupt is the second interrupt, then go to step 709.

[0088] In the embodiment, the IIC interrupt task is designed to flip the level of the clock line SCL once every time an interrupt is executed, and read or write 1 bit of data every two interrupts. Under this design, the frequency of the timer interrupt is twice the baud rate of the IIC communication.

[0089] To distinguish the two interrupts, the interruption when the transmitter sends data to the data line SDA (the receiver does not take any action) is defined as the first interruption; the interruption when the receiver reads data from the data line SDA (the transmitter keeps the data on the data line SDA unchanged) is defined as the second interruption. In the initialization state, the current interruption is the first interruption.

[0090] Step 703, control the clock line SCL to flip from high level to low level, send 1 bit of data to the data line SDA, set the current interrupt to be the second interrupt, and exit the IIC interrupt task.

[0091] The host acts as the transmitter. In the first interrupt, it controls the clock line SCL to flip from a high level to a low level, sends 1 bit of data to the data line SDA, and then sets the current interrupt to the second interrupt.

[0092] Step 704, control the clock line SCL to flip from a low level to a high level, set the current interrupt to be the first interrupt, increase the number of sent data bits by one, and exit the IIC interrupt task.

[0093] The host acts as the transmitter. In the second interrupt, the host controls the clock line SCL to flip from low level to high level, keeps the data on the SDA data line unchanged, and sets the current interrupt to the first interrupt. The slave acts as the receiver. In the second interrupt, the slave completes the reading of the data bit, and the host increases the number of data bits sent by one.

[0094] Step 705: if the current interrupt is the first interrupt, then go to step 706; if the current interrupt is the second interrupt, then go to step 707.

[0095] In the response signal receiving process, the host changes from the sending end to the receiving end, and the slave changes from the receiving end to the sending end.

[0096] Step 706, control the clock line SCL to flip from a high level to a low level, set the current interrupt to be the second interrupt, and exit the IIC interrupt task.

[0097] The host acts as the receiving end. In the first interrupt, the host controls the clock line SCL to flip from a high level to a low level, does not set the SDA data line, and sets the current interrupt to the second interrupt; the slave acts as the sending end and sends the response signal to the data line SDA.

[0098] Step 707, control the clock line SCL to flip from low level to high level, read the response signal of the slave from the data line SDA, if the response signal is 0, update the current write step / current read step, if the response signal is 1, set the number of sent data bits to 0, and exit the IIC interrupt task.

[0099] The host acts as the receiving end. In the second interrupt, the host controls the clock line SCL to flip from a low level to a high level, and reads the response signal of the slave from the data line SDA. When the response signal is 0, it indicates that the 1-byte "device address" data sent by the host is successfully received, the simulation of the "device address" sending sequence is completed, and the current write step / current read step is updated to the next step; if the response signal is 1, it indicates that the 1-byte "device address" data sent by the host is not successfully received, the number of data bits sent is set to 0, and the "device address" simulation sending sequence is re-executed.

[0100] See also Figure 8 , Figure 8This is an execution timing diagram of the main polling task and the IIC interrupt task in each timer cycle according to the embodiment. Since the execution time of the IIC interrupt task is very short (us or even ns level), the main polling task can be executed almost during the entire period when the two levels of the SCL clock line are flipped. Although the addition of the IIC interrupt task will continue to interrupt the main polling task regularly, since the execution time of the IIC interrupt task is much shorter than the response requirement time of the main polling task (at least ms level), the execution of the IIC interrupt task has little effect on the real-time performance of the main polling task of the entire system.

[0101] If the IIC communication bit rate is set to 15K, and 100 bytes of data need to be read at one time, the time required to complete the reading is at least 53.33ms; when the traditional IO port is used to simulate IIC communication, the maximum response delay of the same level priority task is at least 53.33ms, which has a great impact on tasks that require a 1ms periodic response. When the above-mentioned method of simulating IIC communication based on interrupt timer is adopted, the response delay is reduced to the task execution time of a single timer interrupt (that is, the execution time of the IIC interrupt task). And under the premise of not being affected by the amount of communication data and the number of channels, the response delay is fixed to the task execution time of a single timer interrupt, thereby fundamentally solving the impact of the time consumption of simulated IIC communication on the real-time performance of the system.

[0102] The method for simulating IIC communication provided by an embodiment of the present invention first sets the parameter information required for the IIC interrupt task to simulate IIC communication, and when the IIC communication mode is write, stores the data to be written in the write data storage address of the IIC interrupt task; then enters the IIC interrupt task based on a preset timer to execute a preset IIC simulation write program or an IIC simulation read program. The method of the present invention improves the traditional method of implementing simulated IIC communication in the main polling task to implement it in the timer interrupt task, so that the execution of other tasks does not need to wait for the IIC communication data to be transmitted, which fundamentally solves the impact of the time consumption of simulating IIC communication on the real-time performance of the system. In addition, since the time consumption of the IIC interrupt task is much less than the response requirement time of the main polling task, the added IIC interrupt task has little impact on the real-time performance of the main polling task of the entire system.

[0103] Embodiment 2

[0104] According to an embodiment of the present invention, a single chip microcomputer product is provided, such as Fig. 9As shown, according to a structural schematic diagram of a single-chip microcomputer product provided in an embodiment, the single-chip microcomputer product may include a processing unit 901, an input-output interface 902, a program memory 903, a data memory 904, a communication bus 905, a timer 906 and an interrupt system 907, wherein the processing unit 901, the input-output interface 902, the program memory 903, and the data memory 904 communicate with each other through the communication bus 905. The processing unit 901 can call the logic instructions in the program memory 903 and update the state of the data memory 904 to execute the method of simulating IIC communication in Example 1. The method includes: setting the parameter information required for the IIC interrupt task to simulate IIC communication, and when the IIC communication mode is write, storing the data to be written in the write data storage address of the IIC interrupt task; entering the IIC interrupt task based on a preset timer, and executing the following steps: if the IIC communication mode is write, executing the preset IIC simulation write program, and sending the data to be written to the slave; if the IIC communication mode is read, executing the preset IIC simulation read program, obtaining the data to be read from the slave, and storing the data to be read in the preset read data storage address.

[0105] In addition, the logic instructions in the above-mentioned program memory 903 can be implemented in the form of software functional units and can be stored in several computer-readable storage media when they are sold or used as independent products. Based on such an understanding, the technical solution of the present invention can be essentially or partly embodied in the form of a software product that contributes to the prior art. The computer software product is stored in a storage medium, including several instructions for enabling a computer device (which can be a personal computer, a server or a network device, etc.) to perform all or part of the steps of any of the methods described in the first embodiment of the present invention. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.

[0106] The above-mentioned product can execute any method of simulating IIC communication described in Example 1, and has corresponding functional modules and beneficial effects of the method. For technical details not described in detail in this embodiment, please refer to the method of simulating IIC communication provided in Example 1 of the present invention.

[0107] Embodiment 3

[0108] According to an embodiment of the present invention, a computer-readable storage medium is provided, the type of which is as described in Embodiment 2, the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the processor executes the steps of the method for simulating IIC communication described in Embodiment 1.

[0109] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a general hardware platform, and of course, by hardware. Based on this understanding, the above technical solution, in essence or in other words, the part that contributes to the relevant technology, can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiment.

[0110] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit them. Under the concept of the present invention, the technical features in the above embodiments or different embodiments may also be combined, the steps may be implemented in any order, and there are many other changes in different aspects of the present invention as described above, which are not provided in detail for the sake of simplicity. Although the present invention has been described in detail with reference to the aforementioned embodiments, a person of ordinary skill in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features may be replaced by equivalents. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the scope of the technical solutions of the embodiments of the present application.

Claims

1. A method for simulating IIC communication, characterized in that: The method comprises: Setting the parameter information required for the IIC interrupt task to simulate IIC communication, and when the IIC communication mode is write, storing the data to be written in the write data storage address of the IIC interrupt task; Enter the IIC interrupt task based on a preset timer, and execute the following steps: if the IIC communication mode is writing, execute the preset IIC simulation writing program to send the data to be written to the slave; if the IIC communication mode is reading, execute the preset IIC simulation reading program to obtain the data to be read from the slave, and store the data to be read in the preset reading data storage address; The IIC interrupt task writes 1 bit of data including: in the first interrupt, controlling the clock line SCL to flip from a high level to a low level, and sending 1 bit of data to the data line SDA, and in the second interrupt, controlling the clock line SCL to flip from a low level to a high level, so that the slave reads 1 bit of data from the data line SDA; The IIC interrupt task to read 1 bit of data includes: in the first interrupt, the clock line SCL is controlled to flip from a high level to a low level, so that the slave sends 1 bit of data to the data line SDA; in the second interrupt, the clock line SCL is controlled to flip from a low level to a high level, and 1 bit of data is read from the data line SDA.

2. The method according to claim 1, characterized in that: The IIC analog write program and the IIC analog read program are both implemented in a state machine switching manner according to the IIC communication mechanism.

3. The method according to claim 2, characterized in that The IIC analog write program completes the analog write of the data to be written after successively going through the analog start signal timing, the analog "device address" sending timing, the analog "register address" sending timing, the analog "data content" sending timing and the analog stop signal timing; the IIC analog read program completes the analog read of the data to be read after successively going through the analog start signal timing, the analog "device address" sending timing, the analog "register address" sending timing, the analog start signal timing again, the analog "device address" sending timing again, the analog "data content" reading timing and the analog stop signal timing.

4. The method according to claim 3, characterized in that The IIC analog write program also includes a write end timing, and the program enters the write end timing after the analog stop signal timing in the IIC analog write program is completed; the IIC analog read program also includes a read end timing, and the program enters the read end timing after the analog stop signal timing in the IIC analog read program is completed. When the IIC analog write program executes to the write end timing or when the IIC analog read program executes to the read end timing, the IIC interrupt task is exited.

5. The method according to claim 1, characterized in that The execution time of the IIC interrupt task is much shorter than the period of the timer.

6. The method according to claim 5, characterized in that In each cycle of the timer, the IIC interrupt task is executed first, and then the main polling task is executed.

7. A single-chip microcomputer product, comprising a memory, a processor and a computer program stored and running on the memory, characterized in that: When the processor executes the program, the method steps for simulating IIC communication according to any one of claims 1 to 6 are implemented.

8. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program. When the computer program is executed by a processor, the processor executes the method steps for simulating IIC communication according to any one of claims 1 to 6.