Method, system and equipment for reading and writing data based on I2C (Inter-Integrated Circuit) bus
By optimizing the I2C data frame structure and data packet format, the problem of low batch reading and writing data on the I2C bus is solved, and more efficient data transmission is achieved.
Patent Information
- Application Number
- CN202411392411.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-08
- Publication Date
- 2025-02-25
AI Technical Summary
In the prior art, when batch reading and writing data through the I2C bus, the efficiency is low, and the reading and writing interactive process steps are many, and each waiting time is long.
By optimizing the I2C data frame structure, adding register fields and device data units, filling in the target register address, device ID and operation identification value, forming an I2C data packet, and sending it through the I2C bus for the slave device to perform batch read and write operations.
It effectively solves the problem of low reading and writing data efficiency in I2C, reduces the number of read and write interactions, and improves data transmission efficiency.
Smart Images

Figure CN119513009A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of communication technology, and in particular to a data reading and writing method, system and device based on an I2C bus. Background Art
[0002] The I2C (Inter-Integrated Circuit) bus is a bus that allows simple, bidirectional, two-wire synchronous serial communication between integrated circuits (ICs). The bus is widely used in electronic device design because of its small number of pins, simple hardware implementation, and strong scalability.
[0003] The I2C bus uses only two signal lines: a serial data line SDA (Serial Data Line) and a serial clock line SCL (Serial Clock Line). These two lines are connected to the power supply through pull-up resistors, and they are in a high state when no device drives them. Data communication is started and ended by pulling down the level on the SDA or SCL line, thereby achieving the sending and receiving of communication data. On the I2C bus, there are one or more master devices and one or more slave devices, such as Figure 1 As shown in the figure, the host is responsible for starting and terminating data transmission and generating clock signals; the slave responds to the host's request. Each slave device has a unique address, which is used by the host to address a specific slave. Data transmission usually contains an address byte to specify the target slave, followed by a data byte. The I2C bus write data process is as follows: Figure 2 As shown, the I2C bus read data process is as follows Figure 3 shown.
[0004] However, the prior art of batch reading and writing data through the I2C bus has the problem of low reading and writing efficiency, that is, only one byte can be read and written each time; and the I2C reading and writing interaction process has many steps and a long waiting period each time. Summary of the invention
[0005] In view of this, the embodiments of the present application provide a data reading and writing method, system and device based on the I2C bus, which can effectively solve the problem of low efficiency of I2C reading and writing data.
[0006] In a first aspect, an embodiment of the present application provides a method for batch reading and writing data based on an I2C bus, which is applicable to a host device in an I2C industrial control system; the method comprises:
[0007] Filling a target register address in a register field in a preset I2C data frame structure; the target register address is used to indicate to each slave device to be subjected to a batch read / write operation to determine a register address corresponding to the read / write operation;
[0008] According to the number of slave devices to be subjected to batch read and write operations, device data units equal to the number are set in the I2C data frame structure;
[0009] Filling the device ID and the corresponding operation identification value of the corresponding slave device in each of the device data units; wherein the device ID is used to instruct the corresponding slave device to search for the device data unit corresponding to its own slave; and the operation identification value is used to instruct the corresponding slave device to determine the timing of executing the read and write operations;
[0010] According to the filled I2C data frame, an I2C data message corresponding to the read and write operation is obtained, and the I2C data message is sent to the I2C bus, so that each slave device can read and write data of its own device according to the I2C data message.
[0011] In some embodiments, each of the device data units includes: an operation status identification field; the operation status identification field is used to store the operation identification value; the operation identification value determines the slave device according to the preset order in which each of the slave devices processes the I2C data message;
[0012] Filling the device ID and the corresponding operation identification value of the corresponding slave device in each of the device data units includes:
[0013] Filling a ready state value in the running state identification field corresponding to the preset first slave device; the ready state value is used to instruct the first slave device to immediately execute the read and write operation;
[0014] Fill the waiting state value in the running state identification field corresponding to the intermediate slave device;
[0015] The last device identification value is filled in the running status identification field corresponding to the last slave device; the last device identification value is used to instruct the last slave device to feed back the I2C data message after the read and write processing of each slave device to the host device.
[0016] In some embodiments, each of the device data units further includes: a read / write identification bit and a data storage field;
[0017] The method further comprises:
[0018] When determining to perform a read operation on the corresponding slave device, a preset value is filled in the corresponding data storage field, and a read operation type identification value is filled in the corresponding read-write identification bit, so that the corresponding slave device can determine that it performs a read operation;
[0019] When determining to perform a write operation on the corresponding slave device, the data to be written is filled in the corresponding data storage field, and the write operation type identification value is filled in the corresponding read-write identification bit, so that the corresponding slave device can determine that it performs a write operation.
[0020] In some embodiments, the I2C data frame structure further includes a service type identification field;
[0021] The method further comprises:
[0022] In the service type identification field in the preset I2C data frame structure, a service identification value determined according to the service type corresponding to the batch read and write operation is filled; the service identification value is used to instruct the slave device to determine a method for parsing the I2C data message and determine a service type to be executed;
[0023] The method further comprises:
[0024] Receive and analyze the response information fed back by the slave device;
[0025] If the response information is determined to be the I2C data message returned after the read and write operations of each slave device based on the business type identification value stored in the business type identification field in the response information, and based on the read and write result status stored in the operation status identification field, it is determined to feedback the read and write result status to the management end; the read and write result status includes operation success and operation failure.
[0026] In a second aspect, the present application provides a data reading and writing method based on an I2C bus, which is applicable to a slave device in an I2C industrial system; the method comprises:
[0027] Get I2C data message from I2C bus;
[0028] Parsing the target register address stored in the register field in the I2C data message;
[0029] Parsing the device ID stored in each device data unit in the I2C data message, and matching it with the device ID of the own device, to obtain the target device data unit corresponding to the own device;
[0030] Parsing the operation identification value stored in the target device data unit, and determining the timing of executing the read and write operations according to the operation identification value; wherein the operation identification value is determined according to a preset order in which each of the slave devices processes the I2C data message;
[0031] According to the timing, it is determined to perform a read or write operation on a register corresponding to the target register address.
[0032] In some embodiments, parsing the operation identification value stored in the target device data unit and determining the timing of performing the read and write operations according to the operation identification value includes:
[0033] If the operation identification value is a ready state value, the timing for the self-device to execute the read and write operation is immediate execution;
[0034] If the operation identification value is a waiting state value, the timing for the self-device to perform the read and write operations is waiting;
[0035] If the operation identification value is the last execution state value, the own device is the last slave device, and the timing of executing the read and write operations is immediate execution.
[0036] In some embodiments, each of the device data units includes: an operation status identification field;
[0037] After determining, according to the timing, to perform a read / write operation on a register corresponding to the target register address, the method further includes:
[0038] According to the read / write result status of the own device, write the read / write result status value into the operation status identification field;
[0039] In the I2C data message, obtain a device data unit adjacent to the device data unit corresponding to the own device to obtain the next device data unit;
[0040] If the slave device corresponding to the next device data unit is an intermediate slave device, updating the operation identification value of the operation status identification field in the next device data unit from the waiting state value to the ready state value;
[0041] If the slave device corresponding to the next device data unit is the last slave device, the operation identification value of the operation status identification field in the next device data unit is updated from the last device identification value to the last ready state value.
[0042] In some embodiments, each of the device data units further includes: a read / write identification bit and a data storage field;
[0043] The determining, according to the timing, to perform a read / write operation on a register corresponding to the target register address comprises:
[0044] If the read / write identification bit stores a read operation type identification value, a read operation is performed according to the register corresponding to the target register address, and the read data is stored in the corresponding data storage field;
[0045] If the read / write identification bit stores a write operation type identification value, the data stored in the corresponding data storage field is written into the register corresponding to the target register address.
[0046] In some embodiments, the I2C data message also includes a service type identification field;
[0047] Before parsing the target register address stored in the register field in the I2C data message, the method further includes:
[0048] Parse the service identification value stored in the service type identification field in the I2C data message; determine that the I2C data message is a read-write operation service according to the service identification value, and then determine to continue parsing other fields in the I2C data message.
[0049] In some embodiments, writing the read / write result status value to the operation status identification field according to the read / write result status of the own device includes:
[0050] When the own device is a non-last slave device, if the read / write operation is successful, a first operation success status value is written to the corresponding operation status identification field; if the read / write operation fails, a first operation failure status value is written to the corresponding operation status identification field;
[0051] When the own device is the last slave device, if the read / write operation is successful, the second operation success status value is written to the corresponding operation status identification field; if the read / write operation fails, the second operation failure status value is written to the corresponding operation status identification field;
[0052] The method further comprises:
[0053] If the device itself is determined to be the last slave device according to the operation identification value stored in the operation status identification field, after the last slave device completes the read and write operations, it is necessary to send the I2C data message processed by the read and write operations to the I2C bus for reception by the host device.
[0054] In a third aspect, the present application provides an I2C industrial control system, the system comprising: a host device and multiple slave devices; the host device uses a data batch reading and writing method based on the I2C bus provided by the first aspect of the present application to send I2C data messages for batch reading and writing data to multiple slave devices; the slave device uses a data reading and writing method based on the I2C bus provided by the second aspect of the present application to read and write data of its own device.
[0055] In a fourth aspect, the present application provides an electronic device, comprising a processor and a memory, wherein the memory stores a computer program, and the processor is used to execute the computer program to implement a data batch reading and writing method based on an I2C bus provided in the first aspect of the present application.
[0056] In a fifth aspect, the present application further provides an electronic device, comprising a processor and a memory, wherein the memory stores a computer program, and the processor is used to execute the computer program to implement a data reading and writing method based on an I2C bus provided in the second aspect of the present application.
[0057] The embodiments of the present application have the following beneficial effects:
[0058] In the present application, the preset I2C data frame structure is optimized, and the optimized I2C data frame structure includes a register field and a plurality of device data units, wherein the target register address is used to indicate that each slave device to be subjected to batch read and write operations determines the address corresponding to the read and write operations; according to the number of slave devices to be subjected to batch read and write operations, a corresponding number of device data units are set in the I2C data frame structure; the device ID and the corresponding operation identification value of the corresponding slave device are filled in each of the device data units; wherein the device ID is used to indicate that the corresponding slave device searches for the device data unit corresponding to itself; the operation identification value is used to indicate that the corresponding slave device determines the timing of performing the read and write operations; according to the filled I2C data frame, the I2C data message corresponding to the read and write operations is obtained, and the I2C data message is sent to the I2C bus, so that each slave device can read and write the data of its own device according to the I2C data message. Thus, the problem of low efficiency of I2C reading and writing data can be effectively solved. BRIEF DESCRIPTION OF THE DRAWINGS
[0059] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in the embodiments will be briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present application and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without paying creative work.
[0060] Figure 1 A schematic diagram of bus SDA and SCL signal connection involved in the background technology of this application is shown;
[0061] Figure 2 A schematic diagram of an I2C bus data writing process involved in the background technology of this application is shown;
[0062] Figure 3A schematic diagram of an I2C bus data reading process involved in the background technology of this application is shown;
[0063] Figure 4 A schematic diagram of a structure of a preset I2C data frame structure in a method for batch reading and writing data based on an I2C bus in an embodiment of the present application is shown;
[0064] Figure 5 A flow chart of a method for batch reading and writing data based on an I2C bus according to an embodiment of the present application is shown;
[0065] Figure 6 A flow chart of a data reading and writing method based on an I2C bus according to an embodiment of the present application is shown;
[0066] Figure 7 Another flow chart of the method for batch reading and writing data based on the I2C bus according to an embodiment of the present application is shown;
[0067] Figure 8 A general schematic diagram of the change process of the I2C data message in the data batch reading and writing method based on the I2C bus in an embodiment of the present application is shown;
[0068] FIG. 8A to FIG. 8E Schematic diagrams of the change process of I2C data messages in the data batch reading and writing method based on the I2C bus in an embodiment of the present application are shown in sequence;
[0069] Fig. 9 A schematic diagram of the structure of a data batch reading and writing device based on an I2C bus according to an embodiment of the present application is shown;
[0070] Fig.10 A structural schematic diagram of a data reading and writing device based on an I2C bus according to an embodiment of the present application is shown.
[0071] Description of main component symbols:
[0072] 910 - address filling module; 920 - data unit setting module; 930 - data unit filling module; 940 - information sending module; 1010 - message acquisition module; 1020 - address resolution module; 1030 - target data resolution module; 1040 - read / write timing determination module; 1050 - read / write module. DETAILED DESCRIPTION
[0073] The technical solutions in the embodiments of the present application will be described clearly and completely below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, rather than all of the embodiments.
[0074] The components of the embodiments of the present application generally described and shown in the drawings herein may be arranged and designed in various configurations. Therefore, the following detailed description of the embodiments of the present application provided in the drawings is not intended to limit the scope of the application claimed for protection, but merely represents the selected embodiments of the present application. Based on the embodiments of the present application, all other embodiments obtained by those skilled in the art without making creative work belong to the scope of protection of the present application.
[0075] Hereinafter, the terms "including", "having" and their cognates that can be used in various embodiments of the present application are intended only to indicate specific features, numbers, steps, operations, elements, components or a combination of the foregoing items, and should not be understood as first excluding the existence of one or more other features, numbers, steps, operations, elements, components or a combination of the foregoing items or increasing the possibility of one or more features, numbers, steps, operations, elements, components or a combination of the foregoing items. In addition, the terms "first", "second", "third" and the like are only used to distinguish descriptions and cannot be understood as indicating or implying relative importance.
[0076] Unless otherwise defined, all terms (including technical terms and scientific terms) used herein have the same meanings as those generally understood by those skilled in the art to which the various embodiments of the present application belong. The terms (such as those defined in generally used dictionaries) will be interpreted as having the same meanings as the contextual meanings in the relevant technical field and will not be interpreted as having idealized meanings or overly formal meanings unless clearly defined in the various embodiments of the present application.
[0077] In conjunction with the accompanying drawings, some embodiments of the present application are described in detail below. In the absence of conflict, the following embodiments and features in the embodiments can be combined with each other.
[0078] In order to improve the I2C reading and writing efficiency, the present application provides a data reading and writing method, system and device based on the I2C bus, wherein the data frame format of the I2C communication data is improved and the I2C interaction process is modified synchronously.
[0079] The present application provides an I2C industrial control system. Exemplarily, the system includes: a host device and multiple slave devices, such as Figure 1 The host device uses the I2C bus-based data batch reading and writing method provided by the present application to send I2C data messages for batch reading and writing data to multiple slave devices; the slave device uses the I2C bus-based data reading and writing method provided by the present application to read and write data of its own device.
[0080] That is, the host device and each slave device communicate using a preset I2C data frame. Figure 4 As shown, the preset I2C data frame structure includes a register field and multiple device data units; the register field is used to store the unified register address of each slave device for batch reading and writing, for use in batch reading and writing operations. The device data unit is used to store the read and write information of the corresponding slave device.
[0081] Furthermore, the I2C data frame structure also includes a service type identification field. The service type identification field is used to store a service identification value. For example, the service identification value in the service type identification field is 0x5A to indicate a batch read and write operation service.
[0082] The device data unit includes: a slave device identification bit, an operation status identification field, a read / write identification bit and a data storage field. The slave device identification bit is used to store the device ID of the corresponding slave device, so that the slave device can match the device data unit corresponding to its own device in the I2C data message. The read / write identification bit is used to store the read or write operation type identification value to indicate the read or write operation that the slave device needs to perform according to the device data unit. The data storage field is used to store the data written by the host device to the slave device during a write operation, and to store the data of the slave device to be read back when the host device needs a read operation. The operation status identification field is used to store the operation identification value and the read / write result status of the slave device read / write operation data. The operation identification value includes a ready state value, a waiting state value, and a last device identification value. The read / write result status includes a successful operation and a failed operation.
[0083] For example, the host device and multiple slave devices communicate using the Ethercat protocol, and the preset I2C data frame is an improved Ethercat protocol frame. The structure description of the improved Ethercat protocol frame is shown in Table 1:
[0084] Table 1 Improved Ethercat protocol frame structure description
[0085]
[0086]
[0087]
[0088] When the host device and the slave device perform read and write interaction, the host device and each slave device are powered on, and the device ID of the slave device has been configured and recorded in the host device. The host device records the device ID of each slave device on the I2C bus through a static method for use when reading and writing slave devices.
[0089] The data batch reading and writing method based on the I2C bus is described below in conjunction with some specific embodiments.
[0090] Figure 5 A flow chart of a method for batch reading and writing data based on an I2C bus according to an embodiment of the present application is shown. Exemplarily, the method for batch reading and writing data based on an I2C bus is applicable to a host device in an I2C industrial control system, and the method for batch reading and writing data based on an I2C bus comprises the following steps:
[0091] S110, fill the target register address in the register field of the preset I2C data frame structure; the target register address is used to indicate each slave device to be subjected to batch read and write operations to determine the register address corresponding to the read and write operations. The target register address is determined according to the target register to be subjected to batch read and write operations.
[0092] S120: According to the number of slave devices to be subjected to batch read and write operations, device data units equal to the number are set in the I2C data frame structure.
[0093] S130, filling in the device ID and the corresponding operation identification value of the corresponding slave device in each of the device data units; wherein the device ID is used to instruct the corresponding slave device to search for the device data unit corresponding to its own slave; and the operation identification value is used to instruct the corresponding slave device to determine the timing of executing the read and write operations.
[0094] Further, each of the device data units includes: a slave device identification bit and an operation status identification field;
[0095] Filling the device ID and the corresponding operation identification value of the corresponding slave device in each of the device data units includes:
[0096] Filling the device ID of the corresponding slave device in each of the slave device identification bits;
[0097] Fill the operation identification value in each operation status identification field; that is, the operation status identification field is used to store the operation identification value; the operation identification value determines the slave device according to the preset order in which each slave device processes the I2C data message. The order in which each slave device processes the I2C data message is set according to business requirements. For example, if the I2C industrial control system includes four slave devices, the preset order can be slave device 1, slave device 2, slave device 3, slave device 4, or slave device 1, slave device 3, slave device 4, slave device 2.
[0098] Furthermore, in step S130, filling the device ID of the corresponding slave device and the corresponding operation identification value in each of the device data units includes:
[0099] S131, fill in the ready state value in the running state identification field corresponding to the preset first slave device; the ready state value is used to indicate that the first slave device immediately executes the read and write operation. For example, the ready state value READY can be 001b, that is, 001b is stored in the RUN field. Among them, the first slave device is the first slave device in the preset order, and the last slave device is the last slave device in the preset order. For example, the preset order is slave device 1, slave device 2, slave device 3, slave device 4, then slave device 1 is the first slave device. The intermediate slave devices include slave device 2 and slave device 3, and slave device 4 is the last slave device.
[0100] S132, fill the waiting state value in the running state identification field corresponding to the intermediate slave device. For example, the waiting state value WAIT is 000b, that is, 000b is stored in the RUN field.
[0101] S133, fill the last device identification value in the running status identification field corresponding to the last slave device; the last device identification value is used to instruct the last slave device to feed back the I2C data message after the read and write processing of each slave device to the host device. For example, the last device identification value LAST_READY can be 101b.
[0102] S140, obtaining an I2C data message corresponding to the read and write operation according to the filled I2C data frame, and sending the I2C data message to the I2C bus, so that each slave device can read and write data of its own device according to the I2C data message.
[0103] In one implementation, the I2C data frame structure also includes a service type identification field.
[0104] The method further comprises:
[0105] In the service type identification field in the preset I2C data frame structure, a service identification value determined according to the service type corresponding to the batch read and write operation is filled; the service identification value is used to instruct the slave device to determine the method for parsing the I2C data message and determine the service type to be executed.
[0106] After the last slave device completes the read / write operation, it is necessary to send the I2C data message processed by the read / write operation to the I2C bus for reception by the host device.
[0107] It is understandable that the method further comprises:
[0108] Receive and analyze the response information fed back by the slave device;
[0109] If the response information is determined to be the I2C data message returned after the read and write operation of each slave device according to the service type identification value stored in the service type identification field in the response information, and according to the read and write result status stored in the operation status identification field, it is determined to feedback the read and write result status to the management end; the read and write result status includes operation success and operation failure. Further, according to whether the slave device is the last slave device or the non-last slave device (including the first slave device and the middle slave device), the read and write result status is divided into a first operation success status value (corresponding to the operation success status value in Table 1), a first operation failure status value (corresponding to the operation failure status value in Table 1), a second operation success status value and a second operation failure status value. For example, the first operation success status value can be 010b, the first operation failure status value can be 011b, the second operation success status value can be 110b, and the second operation failure status value can be 111b.
[0110] The step of determining to feed back the read / write result status to the management terminal according to the read / write result status stored in the operation status identification field includes:
[0111] In the case where the read / write result status includes the second operation success status value (last device operation success) and the second operation failure status value (last device operation failure) (i.e., the RUN field value in a device data unit is 110b or 111b), it is determined to feedback the read / write result status to the management end. That is, after the last slave device completes the processing, the processed I2C data message is returned to the host device; the host device processes the I2C data message, otherwise it does not process it and does not feedback the read / write result to the management end.
[0112] In one implementation, each of the device data units further includes: a read / write identification bit and a data storage field;
[0113] The method further includes: when determining to perform a read operation on the corresponding slave device, filling a preset value in the corresponding data storage field and filling a read operation type identification value in the corresponding read-write identification bit, so that the corresponding slave device can determine that it performs a read operation.
[0114] When determining to perform a write operation on the corresponding slave device, the data to be written is filled in the corresponding data storage field, and the write operation type identification value is filled in the corresponding read-write identification bit, so that the corresponding slave device can determine that it performs a write operation.
[0115] The data reading and writing method based on the I2C bus is described below in conjunction with some specific embodiments.
[0116] Figure 6A flow chart of a data reading and writing method based on an I2C bus according to an embodiment of the present application is shown. Exemplarily, the data reading and writing method based on the I2C bus is applicable to a slave device in an I2C industrial control system, and the data reading and writing method based on the I2C bus includes the following steps:
[0117] S210, obtaining an I2C data message from the I2C bus.
[0118] S220, parsing the target register address stored in the register field in the I2C data message.
[0119] Furthermore, before parsing the target register address stored in the register field in the I2C data message, the method further includes:
[0120] Parse the service identification value stored in the service type identification field in the I2C data message; determine that the I2C data message is a read-write operation service according to the service identification value, and then determine to continue parsing other fields in the I2C data message.
[0121] S230, parsing the device ID stored in each device data unit in the I2C data message, and matching it with the device ID of the own device, to obtain a target device data unit corresponding to the own device.
[0122] Furthermore, each of the device data units includes: a slave device identification bit and an operation status identification field.
[0123] In step S230, the device ID stored in each device data unit in the I2C data message is parsed and matched with the device ID of the own device to obtain the target device data unit corresponding to the own device, including:
[0124] The device ID stored in the slave device identification bit in each device data unit in the I2C data message is parsed and matched with the own device ID to obtain the target device data unit corresponding to the own device.
[0125] S240, parsing the operation identification value stored in the target device data unit, and determining the timing of executing the read and write operations according to the operation identification value; wherein the operation identification value is determined according to the preset order in which each of the slave devices processes the I2C data message. The parsing of the operation identification value stored in the target device data unit, and determining the timing of executing the read and write operations according to the operation identification value, includes:
[0126] The operation identification value stored in the operation status identification field in the target device data unit is parsed, and the timing of executing the read and write operations is determined according to the operation identification value.
[0127] Further, the parsing the operation identification value stored in the target device data unit and determining the timing of performing the read and write operations according to the operation identification value includes:
[0128] S241: If the operation identification value is a ready state value, the timing for the own device to execute the read and write operation is immediate execution.
[0129] S242: If the operation identification value is a waiting state value, the timing for the own device to perform the read and write operations is waiting.
[0130] S243: If the operation identification value is the last execution state value, the own device is the last slave device, and the timing of executing the read and write operations is immediate execution.
[0131] S250: Determine, according to the timing, whether to perform a read or write operation on a register corresponding to the target register address.
[0132] Further, in step S2250, determining to perform a read / write operation on a register corresponding to the target register address according to the timing includes:
[0133] If the read / write identification bit stores a read operation type identification value, a read operation is performed according to the register corresponding to the target register address, and the read data is stored in the corresponding data storage field;
[0134] If the read / write identification bit stores a write operation type identification value, the data stored in the corresponding data storage field is written into the register corresponding to the target register address.
[0135] Further, after determining to perform a read / write operation on a register corresponding to the target register address according to the timing, the method further includes:
[0136] S260, writing the read / write result status value into the operation status identification field according to the read / write result status of the own device.
[0137] S270, obtaining, in the I2C data message, a device data unit adjacent to the device data unit corresponding to the own device, to obtain the next device data unit.
[0138] S280, if the slave device corresponding to the next device data unit is an intermediate slave device, then the operation identification value of the operation status identification field in the next device data unit is updated from the waiting state value to the ready state value. Waiting state value = 000b, ready state value = 001b, then the waiting state value is added by 1 to obtain the ready state value, and stored in the operation status identification field.
[0139] S290, if the slave device corresponding to the next device data unit is the last slave device, then the operation identification value of the operation status identification field in the next device data unit is updated from the last device identification value to the last ready state value. Last device identification value = 100b, last ready state value = 101b, and the last device identification value is added by 1 to obtain the last ready state value.
[0140] Further, in step S260, writing the read / write result status value into the operation status identification field according to the read / write result status of the own device includes:
[0141] When the own device is not the last slave device, if the read / write operation is successful, the first operation success status value is written to the corresponding operation status identification field; if the read / write operation fails, the first operation failure status value is written to the corresponding operation status identification field.
[0142] When the own device is the last slave device, if the read / write operation is successful, the second operation success status value is written to the corresponding operation status identification field; if the read / write operation fails, the second operation failure status value is written to the corresponding operation status identification field.
[0143] Furthermore, the method further comprises:
[0144] If the own device is determined to be the last slave device according to the operation identification value stored in the operation status identification field, after the last slave device completes the read / write operation, the I2C data message after the read / write operation is sent to the I2C bus for reception by the host device. If the operation identification value is the last ready state value, the own device is determined to be the last slave device.
[0145] The following is an example to illustrate the data batch reading and writing method based on the I2C bus of the present application. Figure 7 , Figure 8 As shown, the following steps are included:
[0146] S610, the host device and the slave device are powered on, and the host device records the device ID of each slave device on the I2C bus through a static method; according to the requirements of the batch read and write operation, the target register address of the target register of the host device for batch read and write is determined to be 0x12, and each slave device for the batch read and write operation. The slave devices for the batch read and write operation include slave device 0x01, slave device 0x02, slave device 0x03, and slave device 0x04. Specifically, the host device needs to perform the following read or write operations:
[0147] Read the target register 0x12 on the slave device 0x01, where the device ID is 0x01 and the target register address is 0x12;
[0148] Write the target register 0x12 on slave device 0x02, and write the data as 0x34; write the target register 0x12 on slave device 0x03, and write the data as 0x56;
[0149] Write the target register 0x12 on slave device 0x04 and write the data 0x78; slave device 0x04 is the last slave device.
[0150] The preset I2C data frame is constructed in the host device as follows:
[0151] S611, fill in 0x5A in the service type identification field, that is, the START field in the preset I2C data frame.
[0152] S612, fill in the target register address 0x12 in the register field REGISTER field in the preset I2C data frame.
[0153] S613, the running status identification field of the device data unit corresponding to slave device 1, that is, the RUN field is filled with 001b, indicating the ready state value, that is, the ready state READY, indicating the slave device to be executed immediately; the slave device identification bit (DEV ID) is filled with the device ID as 0x01, indicating slave device 1, the read / write identification bit R / W is filled with 1, indicating a read operation, and the DATA field is filled with 0xXX to indicate no concern, such as Fig. 8A Shows Figure 8 A local enlarged view of point A in the overall schematic diagram of the corresponding change process.
[0154] S614, in the device data unit corresponding to slave device 2, DEV ID is 0x02, indicating slave device 2; the RUN field of the device data unit corresponding to slave device 2 is filled with 000b, indicating the waiting state WAIT, indicating the slave device waiting for execution; the R / W field is 0, indicating a write operation, and the DATA field is filled with 0x34, indicating that the register 0x12 of slave device 2 needs to be written to 0x34, such as Fig. 8A shown.
[0155] S615, in the device data unit corresponding to slave device 3, the RUN field is filled with 000b, indicating the waiting state WAIT, indicating the slave device waiting for execution; DEV ID is 0x03, indicating slave device 3, the R / W field is 0, indicating a write operation, and the DATA field is filled with 0x56, indicating that the register 0x12 of slave device 2 needs to be written to 0x56, such as Fig. 8A shown.
[0156] S616, in the device data unit corresponding to slave device 4, the RUN field is filled with 100b, indicating the last device LAST_WAIT, that is, the last slave device executed; 0x04 is filled in DEV ID, indicating slave device 4, 0 is filled in R / W field, indicating a write operation; 0x78 is filled in DATA field, indicating that 0x78 needs to be written to register 0x12 of slave device 4.
[0157] S617, obtaining an I2C data message according to the preset I2C data frame, and sending it to the I2C bus.
[0158] S618, slave device 1 receives the I2C data message, and determines the read / write operation service type according to the service type identification field, determines the corresponding device data unit in the I2C data message according to its own device ID, determines that it needs to process the I2C data message immediately according to the RUN field value 001b, determines the read operation according to the read / write identification bit R / W, and determines the address of the read data according to the register field.
[0159] It can be understood that slave device 2 to slave device 3 parse the I2C data message, and determine that the corresponding RUN field value is 000b and is in the waiting state, and then discard the I2C data message without processing.
[0160] S619, after the slave device 1 completes the read operation, the RUN field corresponding to the slave device 1 is changed to 010b, indicating that the execution is completed; if the read operation fails, 011b is filled in the RUN field; in addition, after the slave device 1 completes the read operation, the RUN field in the device data unit behind it needs to be updated to the READY state, that is, the RUN field value in the device data unit corresponding to the slave device 2 is updated from 000b to 001b, and the RUN fields in the device data units corresponding to other slave devices remain unchanged. If the device data unit behind it corresponds to the last slave device, the RUN field value is updated from 100b to 101b.
[0161] Finally, the processed I2C data message is sent to the I2C bus, such as Figure 8B Shows Figure 8 A local enlarged view of point B in the corresponding overall schematic diagram of the change process.
[0162] Slave device 2, slave device 3, and slave device 4 receive the I2C data message on the I2C bus and perform the corresponding write operation according to the processing steps of slave device 1. After slave device 2 completes the write operation, the RUN field in its corresponding device data unit changes to 010b, indicating that the execution is completed. If the operation fails, the RUN field changes to 011b, such as Figure 8C Shows Figure 8The corresponding partial enlarged view of point C in the overall schematic diagram of the change process. When slave device 2 completes the write operation, it updates the RUN status of the device data unit behind its device data unit to the READY status, that is, updates the RUN status of the corresponding device data unit of slave device 3 to the READY status, and the RUN status of other slave devices does not change. After slave device 3 completes the write operation, it changes the RUN field in the corresponding device data unit to 010b, indicating that the execution is completed. If the operation fails, it changes to 011b, such as Fig.8D Shows Figure 8 A local enlarged view of point D in the corresponding overall schematic diagram of the change process.
[0163] S620, slave device 4 determines the read / write operation service type according to the service type identification field, determines the corresponding device data unit in the I2C data message according to its own device ID, determines that it needs to process the I2C data message immediately according to the RUN field value 101b, determines the write operation according to the read / write identification bit R / W, determines the address of the write data according to the register field, and determines the data to be written to the register according to DATA. When the slave device 4 completes the execution and succeeds, the RUN field changes to 110b, and when the device completes the execution but the operation fails, the RUN field changes to 111b, such as Fig. 8E Shows Figure 8 The corresponding partial enlarged diagram of point E in the overall schematic diagram of the change process. And the slave device 4 needs to return the I2C data message after the operation to the host device through various levels of slave devices.
[0164] S621, when the host device receives a message with a START field of 0x5A, it determines that it is an I2C data message returned after the read and write operations of each slave device are processed, and if the RUN field value in a device data unit in the I2C data message is 110b or 111b, the read and write result status contained in each RUN field in the I2C data message is reported to the MCU or CPU through an interrupt to perform batch processing of the response message. The interaction between the entire host device and the slave device is completed.
[0165] Before the improvement, the Ethercat frame could only interact through a single byte, which resulted in low communication efficiency and a need to wait after each message interaction. Taking 100kbps, operating 4 slave devices, and each slave device only operating the read and write of one byte as an example, each device interacts 4 times, and 4 devices need to interact 16 times. However, this application optimizes the Ethercat frame structure and uses the optimized Ethercat frame to perform the above operations. The number of interactions between devices is 5, which greatly reduces the operation process and improves the read and write efficiency.
[0166] Fig. 9A schematic diagram of a data batch reading and writing device based on an I2C bus according to an embodiment of the present application is shown. Exemplarily, the data batch reading and writing device based on an I2C bus includes: an address filling module 910, a data unit setting module 920, a data unit filling module 930 and an information sending module 940.
[0167] The address filling module 910 is used to fill the target register address in the register field in the preset I2C data frame structure; the target register address is used to indicate each slave device to be subjected to batch read and write operations to determine the register address corresponding to the read and write operations;
[0168] A data unit setting module 920, configured to set, in the I2C data frame structure, device data units equal to the number of slave devices to be subjected to batch read and write operations;
[0169] The data unit filling module 930 is used to fill the device ID and the corresponding operation identification value of the corresponding slave device in each of the device data units; wherein the device ID is used to instruct the corresponding slave device to search for the device data unit corresponding to its own slave; and the operation identification value is used to instruct the corresponding slave device to determine the timing of performing the read and write operations;
[0170] The information sending module 940 is used to obtain the I2C data message corresponding to the read and write operation according to the filled I2C data frame, and send the I2C data message to the I2C bus so that each slave device can read and write data of its own device according to the I2C data message.
[0171] It can be understood that the device of this embodiment corresponds to the data batch reading and writing method based on the I2C bus of the above embodiment, and the options in the above embodiment are also applicable to this embodiment, so they will not be described repeatedly here.
[0172] Fig.10 A schematic diagram of a data batch reading and writing device based on an I2C bus according to an embodiment of the present application is shown. Exemplarily, the data batch reading and writing device based on an I2C bus includes: a message acquisition module 1010, an address resolution module 1020, a target data resolution module 1030, a read / write timing determination module 1040 and a read / write module 1050.
[0173] The message acquisition module 1010 is used to acquire the I2C data message from the I2C bus;
[0174] An address parsing module 1020, configured to parse a target register address stored in a register field in the I2C data message;
[0175] The target data parsing module 1030 is used to parse the device ID stored in each device data unit in the I2C data message and match it with the device ID of the own device to obtain the target device data unit corresponding to the own device;
[0176] A read / write timing determination module 1040, configured to parse the operation identification value stored in the target device data unit, and determine the timing of executing the read / write operation according to the operation identification value; wherein the operation identification value is determined according to a preset order in which each of the slave devices processes the I2C data message;
[0177] The read / write module 1050 is used to determine, according to the timing, whether to perform a read / write operation on the register corresponding to the target register address.
[0178] It can be understood that the device of this embodiment corresponds to the data reading and writing method based on the I2C bus of the above embodiment, and the options in the above embodiment are also applicable to this embodiment, so they will not be described repeatedly here.
[0179] The present application also provides an electronic device, which exemplary comprises a processor and a memory, wherein the memory stores a computer program, and the processor runs the computer program to enable the electronic device to perform the functions of each module in the above-mentioned I2C bus-based data batch reading and writing method or the above-mentioned I2C bus-based data batch reading and writing device. Exemplarily, the electronic device is a host device in an I2C industrial control system.
[0180] The present application also provides an electronic device, which exemplarily includes a processor and a memory, wherein the memory stores a computer program, and the processor runs the computer program to enable the electronic device to perform the functions of each module in the above-mentioned data reading and writing method based on the I2C bus or the above-mentioned data reading and writing device based on the I2C bus. Exemplarily, the electronic device is a host device in an I2C industrial control system. Exemplarily, the electronic device is a slave device in an I2C industrial control system.
[0181] Among them, the processor can be an integrated circuit chip with signal processing capabilities. The processor can be a general-purpose processor, including a central processing unit (CPU), a graphics processing unit (GPU) and a network processor (NP), a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field programmable gate array (FPGA) or at least one of other programmable logic devices, discrete gates or transistor logic devices, and discrete hardware components. The general-purpose processor can be a microprocessor or the processor can also be any conventional processor, etc., which can implement or execute the disclosed methods, steps and logic block diagrams in the embodiments of the present application.
[0182] The memory may be, but is not limited to, a random access memory (RAM), a read only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), etc. The memory is used to store a computer program, and the processor may execute the computer program accordingly after receiving an execution instruction.
[0183] The present application also provides a computer-readable storage medium for storing the computer program used in the above electronic device. For example, the computer-readable storage medium may include, but is not limited to, 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.
[0184] In several embodiments provided in the present application, it should be understood that the disclosed devices and methods can also be implemented in other ways. The device embodiments described above are merely schematic. For example, the flowcharts and structure diagrams in the accompanying drawings show the possible architecture, functions and operations of the devices, methods and computer program products according to multiple embodiments of the present application. In this regard, each box in the flowchart or block diagram can represent a module, a program segment or a part of a code, and the module, a program segment or a part of a code contains one or more executable instructions for implementing the specified logical function. It should also be noted that in an alternative implementation, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two consecutive boxes can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the structure diagram and / or the flow diagram, and the combination of boxes in the structure diagram and / or the flow diagram, can be implemented with a dedicated hardware-based system that performs a specified function or action, or can be implemented with a combination of dedicated hardware and computer instructions.
[0185] In addition, the functional modules or units in the various embodiments of the present application may be integrated together to form an independent part, or each module may exist separately, or two or more modules may be integrated to form an independent part.
[0186] If the functions are implemented in the form of software function modules 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 the present application, or the part that contributes to the prior art, or the part of the technical solution, can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for a computer device (which can be a smart phone, a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the methods described in the various embodiments of the present application.
[0187] The above description is only a specific implementation manner of the present application, but the protection scope of the present application is not limited thereto. Any technician familiar with the technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application.
Claims
1. A method for batch reading and writing data based on I2C bus, characterized in that: Applicable to a host device in an I2C industrial control system; the method comprises: Filling a target register address in a register field in a preset I2C data frame structure; the target register address is used to indicate to each slave device to be subjected to a batch read / write operation to determine a register address corresponding to the read / write operation; According to the number of slave devices to be subjected to batch read and write operations, device data units equal to the number are set in the I2C data frame structure; Filling the device ID and the corresponding operation identification value of the corresponding slave device in each of the device data units; wherein the device ID is used to instruct the corresponding slave device to search for the device data unit corresponding to its own slave; and the operation identification value is used to instruct the corresponding slave device to determine the timing of executing the read and write operations; According to the filled I2C data frame, an I2C data message corresponding to the read and write operation is obtained, and the I2C data message is sent to the I2C bus, so that each slave device can read and write data of its own device according to the I2C data message.
2. The method for batch reading and writing data based on the I2C bus according to claim 1, characterized in that: Each of the device data units includes: an operation status identification field; the operation status identification field is used to store the operation identification value; the operation identification value determines the slave device according to the preset order in which each of the slave devices processes the I2C data message; Filling the device ID and the corresponding operation identification value of the corresponding slave device in each of the device data units includes: Filling a ready state value in the running state identification field corresponding to the preset first slave device; the ready state value is used to instruct the first slave device to immediately execute the read and write operation; Fill the waiting state value in the running state identification field corresponding to the intermediate slave device; The last device identification value is filled in the running status identification field corresponding to the last slave device; the last device identification value is used to instruct the last slave device to feed back the I2C data message after the read and write processing of each slave device to the host device.
3. The method for batch reading and writing data based on the I2C bus according to claim 1, characterized in that: Each of the device data units also includes: a read / write identification bit and a data storage field; The method further comprises: When determining to perform a read operation on the corresponding slave device, a preset value is filled in the corresponding data storage field, and a read operation type identification value is filled in the corresponding read-write identification bit, so that the corresponding slave device can determine that it performs a read operation; When determining to perform a write operation on the corresponding slave device, the data to be written is filled in the corresponding data storage field, and the write operation type identification value is filled in the corresponding read-write identification bit, so that the corresponding slave device can determine that it performs a write operation.
4. The method for batch reading and writing data based on the I2C bus according to claim 2, characterized in that: The I2C data frame structure also includes a service type identification field; The method further comprises: In the service type identification field in the preset I2C data frame structure, a service identification value determined according to the service type corresponding to the batch read and write operation is filled; the service identification value is used to instruct the slave device to determine a method for parsing the I2C data message and determine a service type to be executed; The method further comprises: Receive and analyze the response information fed back by the slave device; If the response information is determined to be the I2C data message returned after the read and write operations of each slave device based on the business type identification value stored in the business type identification field in the response information, and based on the read and write result status stored in the operation status identification field, it is determined to feedback the read and write result status to the management end; the read and write result status includes operation success and operation failure.
5. A data reading and writing method based on I2C bus, characterized in that: Applicable to a slave device in an I2C industrial system; the method comprises: Get I2C data message from I2C bus; Parsing the target register address stored in the register field in the I2C data message; Parsing the device ID stored in each device data unit in the I2C data message, and matching it with the device ID of the own device, to obtain the target device data unit corresponding to the own device; Parsing the operation identification value stored in the target device data unit, and determining the timing of executing the read and write operations according to the operation identification value; wherein the operation identification value is determined according to a preset order in which each of the slave devices processes the I2C data message; According to the timing, it is determined to perform a read or write operation on a register corresponding to the target register address.
6. The data reading and writing method based on I2C bus according to claim 5, characterized in that: The parsing the operation identification value stored in the target device data unit and determining the timing of performing the read and write operations according to the operation identification value includes: If the operation identification value is a ready state value, the timing for the self-device to execute the read and write operation is immediate execution; If the operation identification value is a waiting state value, the timing for the self-device to perform the read and write operations is waiting; If the operation identification value is the last execution state value, the own device is the last slave device, and the timing of executing the read and write operations is immediate execution.
7. The data reading and writing method based on I2C bus according to claim 5, characterized in that: Each of the device data units includes: an operation status identification field; After determining, according to the timing, to perform a read / write operation on a register corresponding to the target register address, the method further includes: According to the read / write result status of the own device, write the read / write result status value into the operation status identification field; In the I2C data message, obtain a device data unit adjacent to the device data unit corresponding to the own device to obtain the next device data unit; If the slave device corresponding to the next device data unit is an intermediate slave device, updating the operation identification value of the operation status identification field in the next device data unit from the waiting state value to the ready state value; If the slave device corresponding to the next device data unit is the last slave device, the operation identification value of the operation status identification field in the next device data unit is updated from the last device identification value to the last ready state value.
8. The data reading and writing method based on I2C bus according to claim 5, characterized in that: Each of the device data units also includes: a read / write identification bit and a data storage field; The determining, according to the timing, to perform a read / write operation on a register corresponding to the target register address comprises: If the read / write identification bit stores a read operation type identification value, a read operation is performed according to the register corresponding to the target register address, and the read data is stored in the corresponding data storage field; If the read / write identification bit stores a write operation type identification value, the data stored in the corresponding data storage field is written into the register corresponding to the target register address.
9. The data reading and writing method based on I2C bus according to claim 7, characterized in that: The I2C data message also includes a service type identification field; Before parsing the target register address stored in the register field in the I2C data message, the method further includes: Parse the service identification value stored in the service type identification field in the I2C data message; determine that the I2C data message is a read-write operation service according to the service identification value, and then determine to continue parsing other fields in the I2C data message.
10. The data reading and writing method based on I2C bus according to claim 7 or 9, characterized in that: The step of writing the read / write result status value into the operation status identification field according to the read / write result status of the own device includes: When the own device is a non-last slave device, if the read / write operation is successful, a first operation success status value is written to the corresponding operation status identification field; if the read / write operation fails, a first operation failure status value is written to the corresponding operation status identification field; When the own device is the last slave device, if the read / write operation is successful, the second operation success status value is written to the corresponding operation status identification field; if the read / write operation fails, the second operation failure status value is written to the corresponding operation status identification field; The method further comprises: If the device itself is determined to be the last slave device according to the operation identification value stored in the operation status identification field, after the last slave device completes the read and write operations, it is necessary to send the I2C data message processed by the read and write operations to the I2C bus for reception by the host device.
11. An I2C industrial control system, characterized in that: The system includes: a host device and multiple slave devices; the host device uses the data batch reading and writing method based on the I2C bus as described in any one of claims 1-4 to send I2C data messages for batch reading and writing data to multiple slave devices; the slave device uses the data reading and writing method based on the I2C bus as described in any one of claims 5-10 to read and write data of its own device.
12. An electronic device, characterized in that: The electronic device comprises a processor and a memory, wherein the memory stores a computer program, and the processor is used to execute the computer program to implement the data batch reading and writing method based on the I2C bus according to any one of claims 1 to 4.
13. An electronic device, characterized in that: The electronic device comprises a processor and a memory, wherein the memory stores a computer program, and the processor is used to execute the computer program to implement the data reading and writing method based on the I2C bus according to any one of claims 5 to 10.
Citation Information
Cited By
Master-slave device read operation communication method and communication system
CN121614432A