PLC system data communication handshaking and caching method and system based on FPGA
By using the communication handshake module and data cache synchronization module of FPGA in the PLC system, the problem of inconsistent communication rates between FPGA and the master and slave devices is solved, and reliable data transmission and cache synchronization is realized, and the communication stability of PLC devices is improved. and independence.
Patent Information
- Application Number
- CN202410936562.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-12
- Publication Date
- 2024-11-12
AI Technical Summary
In PLC systems, the communication rate between the FPGA and the master and slave devices is inconsistent, resulting in unreliable data transmission, and the prior art is difficult to effectively solve the problem of handshake protocol and cache, resulting in communication interruption or data loss.
The FPGA-based communication handshake module and data cache synchronization module are adopted, and the 2bit register and state machine design realizes the synchronization cache and transmission of data, ensuring reliable data transmission under different communication rates.
It improves the communication stability and independence of PLC equipment, ensures the correctness and rate consistency of data transmission, meets the needs of high-speed parallel bus communication, and ensures the reliability and stability of communication data.
Smart Images

Figure CN118945000A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of integrated circuits, and in particular to a method and system for data communication handshake and cache of a PLC system based on FPGA. Background Art
[0002] As the demand for PLC industrial control equipment increases, using FPGA to communicate with slave devices can greatly increase the communication rate and thus improve the performance of PLC devices. When data is communicated between FPGA and master device upward and FPGA controls slave device downward, the read and write rates between the three cannot be guaranteed to be the same. The master device and FPGA need to use a handshake protocol to ensure that both parties are in a stable read and write state. When data is transmitted between the master device, FPGA and slave device, the communication rate between the master device and FPGA is different from the communication rate between FPGA and slave device. If the master device sends data too slowly, FPGA cannot send data to the slave device on time, or the master device reads data too quickly, FPGA cannot read data from the slave device in time, resulting in communication interruption or unreliable data transmission.
[0003] Patent document CN115579036A discloses a DDR continuous storage circuit based on FPGA and its implementation method. Even if the memory controller generates an interrupt signal during the processing of high-speed data, the data continuous writing function can be realized without data loss. The circuit includes a sampling cache module, a data switching module, a first register, a second register, a data processing module, an address switching module and a memory controller. When the memory controller sends an interrupt signal: the address switching module controls the sampling cache module to output data to the data switching module, the data switching module stops sending data, and stores the received data in the first register and the second register respectively, and the data processing module stops forwarding data. After the interrupt ends, the data processing module reads the data stored in the first and second registers and transmits them to the memory controller, and then continues to forward the data output by the data processing module to the memory controller; the memory controller writes the received data into the DDR memory through the DDR physical layer interface. This patent is applied to DDR continuous storage, and is mainly implemented to avoid data loss through the data switching module when the memory controller generates an interrupt. The application background and problem solving are different. Summary of the invention
[0004] In view of the defects in the prior art, the object of the present invention is to provide a PLC system data communication handshake and caching method and system based on FPGA.
[0005] According to the present invention, a PLC system data communication handshake and cache method based on FPGA is provided, comprising:
[0006] Step S1: The master device sends instruction data to the FPGA based on the communication handshake module in the FPGA;
[0007] Step S2: The communication handshake module in the FPGA controls the data cache synchronization module in the FPGA to cache read and write data through read and write commands;
[0008] Step S3: Data is transmitted with the slave device through the sending and receiving data control module in the FPGA.
[0009] Preferably, the communication handshake module comprises: a register consisting of 2 bits, including: master_flag and fpga_flag;
[0010] The master_flag is controlled to be written by the master device; the fpga_flag is controlled to be written by the FPGA;
[0011] When the master device transmits data to the FPGA, the register value is read. If the XOR result of the 2-bit data is 0, it means that the FPGA is in an idle state and data can be sent. After the master device inverts the master_flag and sends the handshake mark data to the FPGA, the FPGA writes the two-bit handshake mark data into the fixed address of the register corresponding to the current data.
[0012] FPGA performs XOR operation on the two bits of data in the register. If the result is 1, the master device has sent data. FPGA stores the data to be received in the buffer area set in FPGA. After the data is received, FPGA inverts fpga_flag. The XOR result of the two bits of data in the current register is 0, and FPGA is in idle state.
[0013] Preferably, the data cache synchronization module includes: a write area and a read area;
[0014] The write area is used to cache write data sent by the master device to the FPGA, waiting to be sent to the communication bus;
[0015] The read area is used to cache read data received from the communication bus to the FPGA, waiting for the master device to read;
[0016] The write area includes a write_a area and a write_b area; wherein the write_a area and the write_b area are used alternately, and when the write_a area is used to send write data after writing complete write data, the write_b area is used to cache new write data sent to the FPGA;
[0017] The read area includes a read_a area and a read_b area; wherein the functions of the read_a area and the read_b area are used alternately, the read_a area caches the received read data and is used to wait for the master device to read the data; the read_b area is used to cache the read data subsequently received by the communication bus.
[0018] Preferably, when the master device sends data to the FPGA, when the state machine of the data cache synchronization module is IDLE, the data sent by the master device is written into the write_a area; when the data reception completion mark is received, the cache area where the master device sends the data is switched to the write_b area, and the state machine of the data cache synchronization module is switched to the SLAVE_BUSY state.
[0019] Preferably, when the state machine of the data cache synchronization module is SLAVE_BUSY, and the master device cycle is twice or more faster than the slave device cycle, the master device still needs to send data to the FPGA. When the data sent by the master device has been written in the write_a area, the current cache area is switched to the write_b area, and the data sent by the current master device is stored in the write_b area.
[0020] Preferably, when the state machine of the data cache synchronization module is SLAVE_BUSY, the FPGA sends the data in the write_a area to the slave device. When receiving the data sending completion mark, the area where the FPGA is to send data to the slave device is switched to the write_b area, and the state machine switches to the WAIT_SLAVE state.
[0021] Preferably, a cache area slave_rx area is set in the FPGA. When data is transmitted back on the bus, the data is stored in the cache area and waits for processing; in the WAIT_SLAVE state, wait for the slave device to transmit back the data reception completion mark, and put the data stored in the cache area into the read_a area. When the data is completed and the read_a area mark is enabled, the master device read area is switched to the read_a area, and the state is switched to the IDLE state. The master device reads the updated data from the FPGA. If the read and write cycle of the master device meets the preset requirements, before this, the master device reads the data cached in the read_b area.
[0022] According to the present invention, a PLC system data communication handshake and cache system based on FPGA is provided, comprising:
[0023] Module M1: The master device sends command data to the FPGA based on the communication handshake module in the FPGA;
[0024] Module M2: The communication handshake module in the FPGA controls the data cache synchronization module in the FPGA to cache read and write data through read and write commands;
[0025] Module M3: Data transmission with the slave device is carried out through the send and receive data control module in the FPGA.
[0026] Preferably, the communication handshake module comprises: a register consisting of 2 bits, including: master_flag and fpga_flag;
[0027] The master_flag is controlled to be written by the master device; the fpga_flag is controlled to be written by the FPGA;
[0028] When the master device transmits data to the FPGA, the register value is read. If the XOR result of the 2-bit data is 0, it means that the FPGA is in an idle state and data can be sent. After the master device inverts the master_flag and sends the handshake mark data to the FPGA, the FPGA writes the two-bit handshake mark data into the fixed address of the register corresponding to the current data.
[0029] FPGA performs XOR operation on the two bits of data in the register. If the result is 1, the master device has sent data. FPGA stores the data to be received in the buffer area set in FPGA. After the data is received, FPGA inverts fpga_flag. The XOR result of the two bits of data in the current register is 0, and FPGA is in idle state.
[0030] Preferably, the data cache synchronization module includes: a write area and a read area;
[0031] The write area is used to cache write data sent by the master device to the FPGA, waiting to be sent to the communication bus;
[0032] The read area is used to cache read data received from the communication bus to the FPGA, waiting for the master device to read;
[0033] The write area includes a write_a area and a write_b area; wherein the write_a area and the write_b area are used alternately, and when the write_a area is used to send write data after writing complete write data, the write_b area is used to cache new write data sent to the FPGA;
[0034] The read area includes a read_a area and a read_b area; wherein the functions of the read_a area and the read_b area are used alternately, the read_a area caches the received read data and is used to wait for the master device to read the data; the read_b area is used to cache the read data subsequently received by the communication bus;
[0035] When the master device sends data to the FPGA, when the state machine of the data cache synchronization module is IDLE, the data sent by the master device is written into the write_a area; when the data reception completion mark is received, the cache area for the master device to send data is switched to the write_b area, and the state machine of the data cache synchronization module is switched to the SLAVE_BUSY state;
[0036] When the state machine of the data cache synchronization module is SLAVE_BUSY, and the master device cycle is twice or more than twice as fast as the slave device cycle, the master device still needs to send data to the FPGA. When the data sent by the master device has been written into the write_a area, the current cache area is switched to the write_b area, and the data sent by the current master device is stored in the write_b area.
[0037] When the state machine of the data cache synchronization module is SLAVE_BUSY, the FPGA sends the data in the write_a area to the slave device. When receiving the data sending completion mark, the area where the FPGA is to send data to the slave device is switched to the write_b area, and the state machine switches to the WAIT_SLAVE state;
[0038] A cache area slave_rx area is set in the FPGA. When data is transmitted back on the bus, the data is stored in the cache area and waits for processing. In the WAIT_SLAVE state, the slave device is waited for to transmit a data reception completion mark, and the data stored in the cache area is placed in the read_a area. When the data is completed and placed in the read_a area mark is enabled, the master device read area is switched to the read_a area, and the state is switched to the IDLE state. The master device reads the updated data from the FPGA. If the read and write cycle of the master device meets the preset requirements, before this, the master device reads the data cached in the read_b area.
[0039] Compared with the prior art, the present invention has the following beneficial effects:
[0040] 1. In the context of PLC design, a method for processing data interaction when using FPGA to communicate with the main device through a high-speed parallel bus. Specifically, a method for improving communication stability and independence through the design of handshake mechanism and cache method during data communication;
[0041] 2. The present invention designs a handshake mechanism and a cache method. When the communication methods and communication rates between the FPGA, the master device and the slave device are different, the data reading and writing can still be completed through the handshake mechanism. The data cache synchronization module is designed so that the data transmission speed and the correctness of the transmitted data can still be guaranteed between the three.
[0042] 3. The present invention provides a communication method for PLC devices to use FPGA to control slave devices through a data communication handshake and cache method based on FPGA applied to PLC systems, so that PLC devices can use FPGA to improve communication rate, meet the requirements of handshake interaction during communication and cache data synchronization, and ensure the reliability and stability of communication data. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] Other features, objects and advantages of the present invention will become more apparent from the detailed description of non-limiting embodiments made with reference to the following drawings:
[0044] Figure 1 The figure is a schematic diagram of the communication mode presented in the context of PLC application, using FPGA for communication transmission and data processing between the master device and the slave device.
[0045] Figure 2 A schematic diagram of a data communication handshake module and a data cache synchronization module provided by the present invention.
[0046] Figure 3 Design a schematic diagram for the interactive handshake mark between two parties.
[0047] Figure 4 The figure is a flow chart of the data communication handshake and cache method of the FPGA-based PLC system.
[0048] Figure 5 Schematic diagram of data communication handshake and cache system of FPGA-based PLC system.
[0049] Figure 6 Schematic diagram of data communication handshake and cache system of FPGA-based PLC system. DETAILED DESCRIPTION
[0050] The present invention is described in detail below in conjunction with specific embodiments. The following embodiments will help those skilled in the art to further understand the present invention, but are not intended to limit the present invention in any form. It should be noted that, for those of ordinary skill in the art, several changes and improvements can also be made without departing from the concept of the present invention. These all belong to the protection scope of the present invention.
[0051] Example 1
[0052] According to the present invention, a PLC system data communication handshake and cache system based on FPGA is provided, comprising:
[0053] like Figure 1As shown, the master device transmits data via the communication bus with the FPGA. The FPGA processes the data into the data required to control the slave device and transmits it to the slave device. At the same time, the FPGA also needs to parse the data sent back by the slave device for the master device to read.
[0054] like Figure 2-6 As shown, the FPGA-based PLC system data communication handshake and cache system includes: a master device CPU, a communication handshake module, a data cache synchronization module and a send and receive control module. The master device sends instruction data and handshakes to the communication handshake module in the FPGA through the set communication handshake rules, controls the data cache synchronization module to cache read and write data through read and write commands, and finally sends and receives data to the slave device through the send and receive data control module.
[0055] The communication handshake module is designed for the handshake mark between the FPGA and the host device when they communicate through a high-speed parallel bus. This mechanism ensures the normal operation of the communication between the two parties. Figure 3 As shown in the figure, a communication handshake module is designed. There are 2-bit registers in the communication handshake, namely master_flag and fpga_flag. The master_flag is only controlled by the master device to write, and the fpga_flag is only controlled by the FPGA to write. The default value of the register is 00. When the master device sends data to the FPGA, it first reads the register. If the XOR result of the 2-bit data is 0, it means that the FPGA is in an idle state and data can be sent. The master device negates master_flag and sends the handshake mark data to the FPGA. After receiving, the FPGA writes the two-bit handshake mark data into the fixed address of the register corresponding to the data. The FPGA XORs the two-bit data of the register. If the result is 1, it means that the master device has sent the data. The FPGA will store the next received data in the buffer area set in the FPGA. After the data is received, the FPGA negates the fpga_flag. At this time, the XOR result of the two-bit data in the register is 0, indicating that the FPGA is in an idle state. The communication handshake mark value corresponds to the state as shown in the figure. Figure 3 The same is true when the master device reads data.
[0056] The data cache synchronization module designs a cache method for data that needs to be transmitted between the master device, FPGA, and slave device. This mechanism solves the problem caused by the different read and write data rates among the three. For the master device, the data sent downward and the data read back are in two areas, named write area and read area; for the FPGA, the data cache synchronization module prepares two areas for the data sent from the master device to the FPGA, that is, the data that needs to be sent from the FPGA to the slave device, such as Figure 2As shown, the write_a and write_b areas are named here, and two areas are prepared for the data sent from the slave device to the FPGA, that is, the data that the master device wants to read back from the FPGA, such as Figure 2 As shown, they are named read_a area and read_b area here; a state machine is designed for the data cache synchronization module, IDLE initial state, for example, the data sent by the master device is written into the write_a area, when the data reception completion mark is received, the cache area of the master device sending data is switched to the write_b area, and the state machine switches to the SLAVE_BUSY state; if in the SLAVE_BUSY state, because the master device cycle is twice or more than twice the slave device cycle, the master device still needs to send data to the FPGA at this time, assuming that the data sent by the master device before is written into the write_a area, at this time the cache area The domain is switched to the write_b area. At this time, the data sent by the master device is always stored in the write_b area. Even if the data in the write_b area has been refreshed in the previous cycle, if the data in the write_a area has not completed the processing cycle, the new data is still refreshed in the write_b area. In the SLAVE_BUSY state, assuming that the FPGA sends the data in the write_a area to the slave device, when it receives the data sending completion mark, the area where the FPGA is going to send data to the slave device is switched to the write_b area, and the state machine switches to the WAIT_SLAVE state. If the slave device transmits The speed is fast, the data volume is large and the transmission time is long. In order to improve the communication efficiency, the slave device usually processes and forwards the data at the same time. Therefore, the data sent by the FPGA to the slave device may not be sent completely, but the slave device has started to send back the data. To solve this problem, a cache area is opened in the master device FPGA, which is named slave_rx area. When there is data sent back on the bus, the slave_rx area first caches the data and waits for processing; in the WAIT_SLAVE state, wait for the slave device to send back the data reception completion mark, and put the data cached in slave_rx into the read area, such as into the read area. ad_a area, when the data is completed and placed in the read_a area and the mark is enabled, the master device read area is switched to the read_a area, and the state is switched to the IDLE state. The master device can read the updated data from the FPGA. If the master device has a fast read and write cycle, before this, the master device reads the data cached in another read area (such as read_b area); in this way, the FPGA switches the data sent by the master device back and forth between the two write areas, and switches the data sent back from the device back and forth between the two read areas, so that when the read and write cycles at both ends of the FPGA are different, the data can still be cached and updated in time.
[0057] The present invention also provides an FPGA-based PLC system data communication handshake and cache system, which can be implemented by executing the process steps of the FPGA-based PLC system data communication handshake and cache method, that is, those skilled in the art can understand the FPGA-based PLC system data communication handshake and cache method as a preferred implementation of the FPGA-based PLC system data communication handshake and cache system.
[0058] Those skilled in the art know that, in addition to realizing the system and its various devices, modules, and units provided by the present invention in a purely computer-readable program code, it is entirely possible to realize the same functions in the form of logic gates, switches, application-specific integrated circuits, programmable logic controllers, and embedded microcontrollers by logically programming the method steps. Therefore, the system and its various devices, modules, and units provided by the present invention can be considered as a hardware component, and the devices, modules, and units included therein for realizing various functions can also be regarded as structures within the hardware component; the devices, modules, and units for realizing various functions can also be regarded as both software modules for realizing the method and structures within the hardware component.
[0059] The above describes the specific embodiments of the present invention. It should be understood that the present invention is not limited to the above specific embodiments, and those skilled in the art can make various changes or modifications within the scope of the claims, which does not affect the essence of the present invention. In the absence of conflict, the embodiments of the present application and the features in the embodiments can be combined with each other arbitrarily.
Claims
1. A data communication handshake and cache method for PLC system based on FPGA, It is characterized in that include: Step S1: The master device sends instruction data to the FPGA based on the communication handshake module in the FPGA; Step S2: The communication handshake module in the FPGA controls the data cache synchronization module in the FPGA to cache read and write data through read and write commands; Step S3: Data is transmitted with the slave device through the sending and receiving data control module in the FPGA.
2. According to the FPGA-based PLC system data communication handshake and cache method of claim 1, It is characterized in that The communication handshake module includes: a 2-bit register, including: master_flag and fpga_flag; The master_flag is controlled to be written by the master device; the fpga_flag is controlled to be written by the FPGA; When the master device transmits data to the FPGA, the register value is read. If the XOR result of the 2-bit data is 0, it means that the FPGA is in an idle state and data can be sent. After the master device inverts the master_flag and sends the handshake mark data to the FPGA, the FPGA writes the two-bit handshake mark data into the fixed address of the register corresponding to the current data. FPGA performs XOR operation on the two bits of data in the register. If the result is 1, the master device has sent data. FPGA stores the data to be received in the buffer area set in FPGA. After the data is received, FPGA inverts fpga_flag. The XOR result of the two bits of data in the current register is 0, and FPGA is in idle state.
3. The FPGA-based PLC system data communication handshake and cache method according to claim 1, It is characterized in that The data cache synchronization module includes: a write area and a read area; The write area is used to cache write data sent by the master device to the FPGA, waiting to be sent to the communication bus; The read area is used to cache read data received from the communication bus to the FPGA, waiting for the master device to read; The write area includes a write_a area and a write_b area; wherein the write_a area and the write_b area are used alternately, and when the write_a area is used to send write data after writing complete write data, the write_b area is used to cache new write data sent to the FPGA; The read area includes a read_a area and a read_b area; wherein the functions of the read_a area and the read_b area are used alternately, the read_a area caches the received read data and is used to wait for the master device to read the data; the read_b area is used to cache the read data subsequently received by the communication bus.
4. The FPGA-based PLC system data communication handshake and cache method according to claim 3, It is characterized in that When the master device sends data to the FPGA, when the state machine of the data cache synchronization module is IDLE, the data sent by the master device is written to the write_a area; when the data reception completion mark is received, the cache area where the master device sends the data is switched to the write_b area, and the state machine of the data cache synchronization module is switched to the SLAVE_BUSY state.
5. The FPGA-based PLC system data communication handshake and cache method according to claim 4, It is characterized in that When the state machine of the data cache synchronization module is SLAVE_BUSY, and the master device cycle is twice or more than twice as fast as the slave device cycle, the master device still needs to send data to the FPGA. When the data sent by the master device has been written in the write_a area, the current cache area is switched to the write_b area, and the data sent by the current master device is stored in the write_b area.
6. The FPGA-based PLC system data communication handshake and cache method according to claim 5, It is characterized in that When the state machine of the data cache synchronization module is SLAVE_BUSY, the FPGA sends the data in the write_a area to the slave device. When receiving the data sending completion mark, the area where the FPGA is going to send data to the slave device is switched to the write_b area, and the state machine switches to the WAIT_SLAVE state.
7. The FPGA-based PLC system data communication handshake and cache method according to claim 6, It is characterized in that A cache area is set in the FPGA. When data is transmitted back on the bus, the data is stored in the cache area and waits for processing. In the WAIT_SLAVE state, the slave device is waited for to transmit a data reception completion mark, and the data stored in the cache area is placed in the read_a area. When the data is completed and placed in the read_a area mark is enabled, the master device read area is switched to the read_a area, and the state is switched to the IDLE state. The master device reads updated data from the FPGA. If the read and write cycle of the master device meets the preset requirements, before this, the master device reads the data cached in the read_b area.
8. A PLC system data communication handshake and cache system based on FPGA, It is characterized in that include: Module M1: The master device sends command data to the FPGA based on the communication handshake module in the FPGA; Module M2: The communication handshake module in the FPGA controls the data cache synchronization module in the FPGA to cache read and write data through read and write commands; Module M3: Data transmission with the slave device is carried out through the send and receive data control module in the FPGA.
9. The FPGA-based PLC system data communication handshake and cache system according to claim 8, It is characterized in that The communication handshake module includes: a 2-bit register, including: master_flag and fpga_flag; The master_flag is controlled to be written by the master device; the fpga_flag is controlled to be written by the FPGA; When the master device transmits data to the FPGA, the register value is read. If the XOR result of the 2-bit data is 0, it means that the FPGA is in an idle state and data can be sent. After the master device inverts the master_flag and sends the handshake mark data to the FPGA, the FPGA writes the two-bit handshake mark data into the fixed address of the register corresponding to the current data. FPGA performs XOR operation on the two bits of data in the register. If the result is 1, the master device has sent data. FPGA stores the data to be received in the buffer area set in FPGA. After the data is received, FPGA inverts fpga_flag. The XOR result of the two bits of data in the current register is 0, and FPGA is in idle state.
10. The FPGA-based PLC system data communication handshake and cache system according to claim 8, It is characterized in that The data cache synchronization module includes: a write area and a read area; The write area is used to cache write data sent by the master device to the FPGA, waiting to be sent to the communication bus; The read area is used to cache read data received from the communication bus to the FPGA, waiting for the master device to read; The write area includes a write_a area and a write_b area; wherein the write_a area and the write_b area are used alternately, and when the write_a area is used to send write data after writing complete write data, the write_b area is used to cache new write data sent to the FPGA; The read area includes a read_a area and a read_b area; wherein the functions of the read_a area and the read_b area are used alternately, the read_a area caches the received read data and is used to wait for the master device to read the data; the read_b area is used to cache the read data subsequently received by the communication bus; When the master device sends data to the FPGA, when the state machine of the data cache synchronization module is IDLE, the data sent by the master device is written into the write_a area; when the data reception completion mark is received, the cache area for the master device to send data is switched to the write_b area, and the state machine of the data cache synchronization module is switched to the SLAVE_BUSY state; When the state machine of the data cache synchronization module is SLAVE_BUSY, and the master device cycle is twice or more than twice as fast as the slave device cycle, the master device still needs to send data to the FPGA. When the data sent by the master device has been written into the write_a area, the current cache area is switched to the write_b area, and the data sent by the current master device is stored in the write_b area. When the state machine of the data cache synchronization module is SLAVE_BUSY, the FPGA sends the data in the write_a area to the slave device. When receiving the data sending completion mark, the area where the FPGA is to send data to the slave device is switched to the write_b area, and the state machine switches to the WAIT_SLAVE state; A cache area is set in the FPGA. When data is transmitted back on the bus, the data is stored in the cache area and waits for processing. In the WAIT_SLAVE state, the slave device is waited for to transmit a data reception completion mark, and the data stored in the cache area is placed in the read_a area. When the data is completed and placed in the read_a area mark is enabled, the master device read area is switched to the read_a area, and the state is switched to the IDLE state. The master device reads updated data from the FPGA. If the read and write cycle of the master device meets the preset requirements, before this, the master device reads the data cached in the read_b area.
