A PCIE-based communication protocol data interaction method
By designing the PC and embedded devices in a layered manner within the PCIe protocol and using a dual-buffered asynchronous approach, the problems of strong dependence on transmission media and low bandwidth in the PCI protocol are solved, enabling efficient data interaction and rapid porting, and improving communication performance and response speed.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- CHENGDU LEETRO AUTOMATION CO LTD
- Filing Date
- 2022-09-22
- Publication Date
- 2026-05-01
AI Technical Summary
Existing PCI-based communication protocols in the field of industrial automation motion control are highly dependent on the transmission medium and have relatively small bandwidth, making it difficult to achieve efficient data interaction.
It adopts a PCIe-based communication protocol, and adopts a layered design by using the PC as the master and the embedded device as the slave. The communication protocol is divided into application layer and transport layer. Data is sent asynchronously using instruction partitioning and double buffering, and timeout is set to handle exceptions, so as to realize point-to-point communication.
It achieves improved communication bandwidth and performance, reduced protocol modification costs, increased response speed and transmission rate, and ensures communication reliability and flexibility without relying on specific hardware architecture.
Smart Images

Figure CN115374043B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of general motion controllers, and more specifically, to a data interaction method based on a PCIe communication protocol. Background Technology
[0002] PCIe, short for PCI-Express, is a computer expansion bus standard. Its main features include high-speed serial communication, point-to-point communication, and hot-swapping support. In the field of industrial automation and motion control, PCIe is increasingly being used to connect motherboard peripherals to the PC for communication.
[0003] Based on their operating platform, general-purpose motion controllers can be divided into three main categories: PLC controllers, embedded controllers, and PC-based control cards. PC-based motion control cards, capable of achieving more complex motion control, have become one of the fastest-growing types of motion controllers. According to statistics, although my country's market share in the general-purpose motion control industry is continuously growing, PCI is currently the primary method used for communication in the main application areas of motion control products. However, PCI-based communication protocols are highly dependent on the transmission medium and have limited bandwidth. It is currently difficult to find a communication protocol based on PCIe for PC-based motion control cards. Therefore, there is an urgent need for a PCIe-based communication protocol data exchange method to solve the problems of high dependence on the transmission medium and limited bandwidth. Summary of the Invention
[0004] This invention addresses the problems of high dependence on transmission media and low bandwidth in existing PCI-based communication protocols. It proposes a PCIe-based data interaction method, which uses a PC connected to a PCIe driver module as the master end of the communication protocol and an embedded device connected to the same module as the slave end. The master and slave ends are further divided into application layer and transport layer protocols, respectively. This method not only enables communication between the PC and the device without relying on specific hardware architectures but also provides greater bandwidth, higher performance, rapid portability across different transport layers, and improved communication response speed.
[0005] The specific implementation details of this invention are as follows:
[0006] A PCIe-based communication protocol data interaction method is proposed, which performs layered operation of the communication protocol: the PC end connected to the PCIe driver module is the master end of the communication protocol, and the embedded device connected to the PCIe driver module is the slave end of the communication protocol. The master end is divided into an application layer protocol master module and a transport layer protocol master module, and the slave end is divided into an application layer protocol slave module and a transport layer protocol slave module; this method is used to realize communication protocol data interaction.
[0007] To better implement this invention, further, after performing the layering operation, an instruction partitioning operation is performed: the communication data between the master and slave ends is divided into different instructions according to the channel ID; the instructions include synchronous instructions, channel instructions, asynchronous instructions, file instructions, register instructions, and exception instructions;
[0008] The synchronization instruction is used to send communication data that is blocked until the result is returned;
[0009] The asynchronous instruction is used to send communication data that does not need to return a result immediately, but is retrieved after a period of time;
[0010] The channel command is used to send communication data that does not require a return result;
[0011] The file instructions are used to send / receive files;
[0012] The register instruction is used to obtain the slave status;
[0013] The exception command is used to send or receive abnormal communication data.
[0014] To better implement the present invention, after the instruction division operation, the communication data received from the user is further encapsulated in the application layer protocol master module, the application layer protocol slave module, the transport layer protocol master module, and the transport layer protocol slave module respectively.
[0015] The specific encapsulation operation is as follows: add a frame header and a frame trailer to the application layer protocol master module and the application layer protocol slave module. The frame header and frame trailer, together with the data body, form a data frame structure. Add a data size and a channel ID to the transport layer protocol master module and the transport layer protocol slave module. The data size and channel ID, together with the data body, form a data frame structure.
[0016] To better implement the present invention, further, after the encapsulation operation, the communication data sending operation is performed: the transport layer protocol master module assembles the communication data sent by the application layer protocol master module or the transport layer protocol slave module assembles the communication data sent by the application layer protocol slave module, and then sends the communication data in a double-buffered asynchronous manner.
[0017] To better implement this invention, the method of sending communication data in a dual-buffered asynchronous manner further includes the following operations: In the dual-buffered asynchronous method, three global variables are defined, denoted as buffer identifier, first identifier, and second identifier. If the buffer identifier is 1, the second identifier is set to 1, and the communication data is written to the second buffer global array. After the communication data is completely written to the second buffer global array, the buffer identifier is cleared to 0. After clearing to 0, the buffer identifier is set to 1. The communication data is retrieved, and it is determined whether the second identifier is 0. If it is 0, the communication data in the second buffer array is retrieved directly. If it is 1, the communication data is retrieved after it is completely written. If the buffer identifier is 0, the first identifier is set to 1, and the communication data is written to the first buffer global array. After the communication data is completely written to the first buffer global array, the buffer identifier is cleared to 0. After clearing to 0, the buffer identifier is set to 1. The communication data is retrieved, and it is determined whether the first identifier is 0. If it is 0, the communication data in the first buffer array is retrieved directly. If it is 1, the communication data is retrieved after it is completely written.
[0018] To better implement the present invention, further, after the data sending operation, a communication data receiving operation is performed: the communication data is buffered in the form of a queue, and when the communication data of the tail pointer and the communication data of the head pointer point to the upper bound of the communication data array, the communication data of the tail pointer and the communication data of the head pointer are redirected to the beginning of the communication data array.
[0019] To better implement this invention, after performing the communication data receiving operation, a timeout period is set in the master module and slave module of the transport layer protocol. If the set timeout period is exceeded when sending or receiving communication data, the sending or receiving of communication data will fail and a timeout error message will be returned. If three timeout error messages are returned consecutively, the master end calls the reset function to initiate a reset to the slave end.
[0020] To better implement the present invention, the channel ID corresponding to the abnormal instruction is further defined as 0x00;
[0021] The channel ID corresponding to the synchronization command is 0x01;
[0022] The channel ID corresponding to the channel command is 0x02;
[0023] The channel ID corresponding to the asynchronous instruction is 0x03;
[0024] The channel ID corresponding to the file instruction is 0x04;
[0025] The channel ID corresponding to the register instruction is 0x05.
[0026] The present invention has the following beneficial effects:
[0027] (1) The present invention performs layered operation of communication protocol, so that the instruction transmission does not depend on specific hardware structure and implementation method, realizes rapid porting based on different transmission layers, and reduces the cost of modifying the protocol after replacing the transmission medium.
[0028] (2) This invention realizes point-to-point communication between the master and slave ends by dividing the instructions into different types: asynchronous instructions, channel instructions, synchronous instructions, file instructions, register instructions and exception instructions.
[0029] (3) The present invention reduces the serial time of data transmission and improves the transmission rate by using a double-buffered asynchronous transmission method after the data sent by the application layer is packaged in the transport layer.
[0030] (4) The present invention handles abnormal communication by setting a timeout period. If communication is abnormal, the master end can send a reset command to the slave end to restore the communication between the master end and the slave end to the initial state. Attached Figure Description
[0031] Figure 1 Block diagram of the communication protocol module;
[0032] Figure 2 Schematic diagram of data storage and retrieval in a circular queue;
[0033] Figure 3 A flowchart for sending data asynchronously using a double-buffered method;
[0034] Figure 4 This is a diagram illustrating the relationship between commands and their corresponding channel IDs.
[0035] Figure 5 This is a diagram of the main data structures for the application layer and transport layer;
[0036] Figure 6 This is a schematic diagram of the overall process timing of the communication protocol;
[0037] Figure 7 A flowchart for data interaction between the main client and the secondary client;
[0038] Figure 8 This is a flowchart for data interaction from the slave device. Detailed Implementation
[0039] To more clearly illustrate the technical solutions of the embodiments of the present invention, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. It should be understood that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments, and therefore should not be regarded as a limitation on the scope of protection. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0040] In the description of this invention, it should be noted that, unless otherwise explicitly specified and limited, the terms "set up," "connected," and "linked" should be interpreted broadly. For example, they can refer to a fixed connection, a detachable connection, or an integral connection; they can refer to a mechanical connection or an electrical connection; they can refer to a direct connection or an indirect connection through an intermediate medium; and they can refer to the internal connection of two components. Those skilled in the art can understand the specific meaning of the above terms in this invention based on the specific circumstances.
[0041] Example 1:
[0042] This embodiment proposes a data interaction method based on the PCIe communication protocol, such as... Figure 1 As shown, the communication protocol is layered: the PC connected to the PCIE driver module is the master end of the communication protocol, and the embedded device connected to the PCIE driver module is the slave end of the communication protocol. The master end is divided into the application layer protocol master module and the transport layer protocol master module, and the slave end is divided into the application layer protocol slave module and the transport layer protocol slave module.
[0043] Working principle: This embodiment uses the PC connected to the PCIe driver module as the master end of the communication protocol and the embedded device connected to the PCIe driver module as the slave end of the communication protocol. The master end and slave end are respectively divided into application layer protocol and transport layer protocol. This not only realizes communication between PC and device without relying on specific hardware structure, but also has greater bandwidth and higher performance. It also realizes rapid porting based on different transport layers and improves the communication response speed.
[0044] Figure 1 In this protocol, the ARM / FPGA embedded device acts as the slave end, interacting with the PC (the master end) via PCIe. The dashed lines indicate the data flow. Hereinafter, the PC will be referred to as the master end, and the embedded device as the slave end.
[0045] This embodiment divides the protocol into a master module and a slave module. The protocols of the master module and the slave module are further divided into application layer and transport layer. The purpose of layering is to reduce the cost of modifying the protocol after replacing the transmission medium.
[0046] Example 2:
[0047] This embodiment is based on the above embodiment 1, such as... Figure 3 As shown, after the transport layer protocol master module or transport layer protocol slave module assembles the communication data sent by the application layer protocol master module or application layer protocol slave module into packets, it sends the communication data asynchronously in a double-buffered manner.
[0048] Working principle: The method of sending communication data using dual-buffered asynchronous transmission specifically includes the following operations: In the dual-buffered asynchronous mode, three global variables are defined, denoted as buffer identifier, first identifier, and second identifier. If the buffer identifier is 1, the second identifier is set to 1, and the communication data is written to the second buffer global array. After the communication data is completely written to the second buffer global array, the buffer identifier is cleared to 0. After clearing to 0, the buffer identifier is set to 1. The communication data is retrieved, and it is determined whether the second identifier is 0. If it is 0, the communication data in the second buffer array is retrieved directly. If it is 1, the communication data is retrieved after it is completely written. If the buffer identifier is 0, the first identifier is set to 1, and the communication data is written to the first buffer global array. After the communication data is completely written to the first buffer global array, the buffer identifier is cleared to 0. After clearing to 0, the buffer identifier is set to 1. The communication data is retrieved, and it is determined whether the first identifier is 0. If it is 0, the communication data in the first buffer array is retrieved directly. If it is 1, the communication data is retrieved after it is completely written.
[0049] After the data sent by the application layer is packaged at the transport layer, the data is sent asynchronously using a double-buffered method. The purpose of this design is to reduce the serial time of data transmission, achieve a certain degree of concurrency, and thus improve the transmission rate.
[0050] like Figure 3 As shown, Figure 3 In the first cache group, it is called cache 1, and the second cache group is called cache 2. The write identifier of cache 1 is the first identifier, and the write identifier of cache 2 is the second identifier. In the dual-caching asynchronous mode, three identifiers are defined, which are three global variables. Among them, the cache identifier is used to protect the switching between cache 1 and cache 2. Cache 1 and cache 2 are two global arrays that store data. The write identifier of cache 1 is used to protect the process of writing data to cache 1. Similarly, the write identifier of cache 2 is used to protect the process of writing data to cache 2.
[0051] The specific steps are as follows:
[0052] (1) If the cache identifier is 1, it means that the data in the cache 1 array is being sent and the data is written to cache 2; if the cache identifier is 0, it means that the data in the cache 2 array is being sent and the data is written to cache 2.
[0053] (2) To prevent data from being written to cache 1 / 2 while data is being retrieved from cache 1 / 2 array, identifiers 1 and 2 are added. When writing data to cache 1 array, identifier 1 is set to 1, and after writing, identifier 1 is cleared to 0; when writing data to cache 2 array, identifier 2 is set to 1, and after writing, identifier 2 is cleared to 0. When retrieving data from the cache for transmission, identifier 1 / 2 is checked first. If it is 0, the data in the array can be retrieved directly; if it is 1, the data is retrieved only after the data has been written.
[0054] The other parts of this embodiment are the same as those in Embodiment 1 above, so they will not be described again.
[0055] Example 3:
[0056] This embodiment is based on any one of the above embodiments 1-2, such as Figure 5 As shown, the communication data received from the user is encapsulated in the application layer protocol master module, application layer protocol slave module, transport layer protocol master module, and transport layer protocol slave module respectively.
[0057] Working principle: The specific encapsulation operation is as follows: add frame headers and frame trailers to the application layer protocol master module and application layer protocol slave module. The frame headers and frame trailers, together with the data body, form a data frame structure. Add data size and channel ID to the transport layer protocol master module and transport layer protocol slave module. The data size, channel ID, together with the data body, form a data frame structure.
[0058] The data sent by the user is encapsulated twice in the entire communication protocol, once at the application layer and once at the transport layer. For example... Figure 5 As shown, the application layer adds a frame header and frame trailer, which, together with the data body, form a data frame structure for easy parsing. The transport layer mainly adds the data size and channel ID to form a data packet. The channel ID is added to the data packet to distinguish different types of commands, so that after the data packet is sent to the slave end, the slave end can retrieve the data from the corresponding buffer according to the channel ID.
[0059] The other parts of this embodiment are the same as any one of the embodiments 1-2 above, so they will not be described again.
[0060] Example 4:
[0061] This embodiment is based on any one of embodiments 1-3 above, such as Figure 4 As shown, the communication data between the master and slave ends is divided into different instructions according to the channel ID; the instructions include synchronization instructions, channel instructions, asynchronous instructions, file instructions, register instructions, and exception instructions.
[0062] Working principle: The synchronization command is used to send communication data that is blocked until the result is returned;
[0063] The asynchronous instruction is used to send communication data that does not need to return a result immediately, but is retrieved after a period of time;
[0064] The channel command is used to send communication data that does not require a return result;
[0065] The file instructions are used to send / receive files;
[0066] The register instruction is used to obtain the slave status;
[0067] The exception command is used to send or receive abnormal communication data.
[0068] like Figure 4 As shown, the channel ID corresponding to the exception instruction is 0x00; the channel ID corresponding to the synchronization instruction is 0x01; the channel ID corresponding to the channel instruction is 0x02; the channel ID corresponding to the asynchronous instruction is 0x03; the channel ID corresponding to the file instruction is 0x04; and the channel ID corresponding to the register instruction is 0x05.
[0069] Based on practical application scenarios, this embodiment divides the communication data between the master and slave ends using instructions, categorizing them according to their different functions into synchronization instructions, channel instructions, asynchronous instructions, file instructions, register instructions, and exception instructions. The channel IDs corresponding to different types of instructions are as follows: Figure 4 As shown. The synchronous instruction channel is used to block until a result is returned after sending data; the asynchronous instruction channel is used to retrieve data after a period of time without needing an immediate return; the file instruction channel is used to send / receive files; the register instruction channel is used to obtain the slave's status; and the exception instruction channel is used when a communication error occurs.
[0070] The transport layer at both the master and slave ends is only responsible for sending and receiving data; the actual instruction processing is implemented at the application layer. The application layer notifies the transport layer to send / receive data from the corresponding channel buffer based on the channel ID. The priority of an instruction decreases as the channel ID increases. Instructions with higher priority receive more execution time slices within the same time frame.
[0071] The other parts of this embodiment are the same as any one of the embodiments 1-3 above, so they will not be described again.
[0072] Example 5:
[0073] This embodiment is based on any one of embodiments 1-4 above, such as Figure 2As shown, during the communication data transmission process, the communication data is buffered in the form of a queue. When the communication data of the tail pointer and the communication data of the head pointer point to the upper bound of the communication data array, the communication data of the tail pointer and the communication data of the head pointer are redirected to the beginning of the communication data array.
[0074] Working Principle: During data transmission, a buffer is used. To facilitate access, this protocol employs a queue to manage the buffer. A queue is a linear list where data is inserted from the tail and retrieved from the head, with sequential access. If a sequential queue is used, once the tail pointer points to the upper bound of the array, no more data can be inserted. Because data is constantly being retrieved from the head, the array is not full; this phenomenon is called "false overflow." A circular queue solves this problem. When both the tail and head pointers point to the upper bound of the array, the queue is full, and the tail and head pointers are reset to the beginning of the array (0).
[0075] like Figure 2 As shown, logically, the array can be viewed as a ring. During initialization, the head pointer and tail pointer point to the beginning of the array (0), and the queue is empty. After inserting 4 data items in sequence, the tail pointer points to the end of the array after storing 4 data items, while the head pointer remains unchanged. After retrieving 2 data items in sequence, the head pointer points to the end of the array after storing 2 data items, while the tail pointer remains unchanged.
[0076] The other parts of this embodiment are the same as any one of the embodiments 1-4 above, so they will not be described again.
[0077] Example 6:
[0078] Based on any one of embodiments 1-5 above, this embodiment sets a timeout period in the master module and slave module of the transport layer protocol. If the set timeout period is exceeded when sending or receiving communication data, the sending or receiving of communication data will fail and a timeout error message will be returned. If a timeout error message is returned 3 times in a row, the master end calls the multiplexing function to initiate a reset to the master end.
[0079] Working principle: Communication anomaly handling mainly includes the following two points:
[0080] (1) Timeout handling: Timeouts are set in the places where the master module and slave module receive and send data in the transport layer. If the data is not successfully sent or received after the specified time, the function returns a timeout error to the caller. If a timeout error occurs three times in a row, it indicates that there is a communication error between the master and slave. The master can call the reset function to initiate a reset to the slave.
[0081] (2) If the queue buffer for storing instructions is full, the master / slave end notifies the other party not to send any more data by sending an exception instruction.
[0082] The other parts of this embodiment are the same as any one of the embodiments 1-5 above, so they will not be described again.
[0083] Example 7:
[0084] This embodiment is based on any one of embodiments 1-6 above, such as... Figure 6 As shown, the specific workflows of the application layer master module, application layer slave module, transport layer master module, and transport layer slave module are explained.
[0085] 1. Application Layer Main Module
[0086] (1) Initialization: The initialization function of the transport layer main module is called; the global variables of this module are cleared. The initialization function needs to be called once before calling this module.
[0087] (2) Reset: The reset function of the transport layer main module is called; the global variables of this module are cleared; the send function of the transport layer main module is called to send a reset command to the slave end from the abnormal command channel, notifying the slave end to reset. After initialization, the reset function is called. If communication is abnormal, the reset function can also be called directly to restore communication to the initial state.
[0088] (3) Read register: Put the register offset address and the size of the data to be read into the data structure, call the send function of the transport layer main module to send the data from the register instruction channel. After successful transmission, call the receive function of the transport layer main module to receive the result returned from the register instruction channel. After successful reception, return the result to the caller.
[0089] (4) Write to register: Put the register offset address, the data to be written and the data size into the data structure, call the send function of the transport layer main module to send the data from the register instruction channel, and return directly after successful sending.
[0090] (5) Channel instruction: Call the receive function of the transport layer main module to receive data from the abnormal instruction channel, determine whether the channel instruction buffer of the slave end is full. If it is full, return an error. If it is not full, put the length of the data to be sent and the data into the array in sequence, call the send function of the transport layer main module to send the array from the channel instruction channel, and return directly after successful sending.
[0091] (6) Synchronization instruction: Call the receive function of the transport layer main module to receive data from the abnormal instruction channel, determine whether the synchronization instruction buffer of the slave end is full. If it is full, return an error. If it is not full, put the length of the data to be sent and the data into the array in sequence, call the send function of the transport layer main module to send the array from the synchronization instruction channel, and after successful sending, call the receive function of the transport layer main module to receive the result returned from the synchronization instruction channel. After successful reception, return the result to the caller.
[0092] (7) Asynchronous request instruction: Call the receive function of the transport layer main module to receive data from the exception instruction channel, determine whether the asynchronous instruction buffer of the slave end is full, return an error if it is full, if it is not full, assemble the data according to the frame header and frame tail of the data structure, and then call the send function of the transport layer main module to send data from the asynchronous instruction channel. If the sending is successful, return directly.
[0093] (8) Asynchronous result acquisition: Determine whether there is a result of the corresponding asynchronous instruction in the asynchronous instruction cache array. If there is, take it and return the result directly. If not, assemble the data according to the frame header and frame tail of the data structure, and then call the send function of the transport layer main module to send the data from the asynchronous instruction channel. After successful transmission, call the receive function of the transport layer main module to receive the result returned from the asynchronous instruction channel, and then parse whether the currently returned result is the result of the corresponding asynchronous instruction processing. If it is, return the result. If not, store the result in the cache.
[0094] (9) Open file: Assemble data according to data structure, call the send function of the main module of transport layer to send data from file command channel, and return directly when the sending is successful.
[0095] (10) Switching paths: Assemble data according to the data structure, call the send function of the main module of the transport layer to send data from the file command channel, and return directly when the sending is successful.
[0096] (11) Sending file data: Divide the data in the local file into fixed-size data packets and send them in packets. Assemble the data according to the data structure, call the send function of the transport layer main module to send the data from the file command channel. Each time the data is sent successfully, return 1 to notify the caller that the file is being sent and needs to be called again, until the entire file is sent successfully and return 0.
[0097] (12) Close the file: Assemble the data according to the data structure, call the send function of the main module of the transport layer to send the data from the file command channel, and return directly when the sending is successful.
[0098] 2. Transport Layer Main Module
[0099] (1) Initialization: Call the PCIe driver main module to get the number of devices; open the devices; clear the data in DMA; initialize the queue.
[0100] (2) Reset: Clears the global variables to zero.
[0101] (3) Sending data:
[0102] ① Check if the flag bit nFlag in the function parameter is 0. If it is 1, it means that the current data has been stored in the data packet, and the function returns 0; if it is 0, continue executing the code.
[0103] ② Check if the device open flag is 1. If it is 0, it means the device is not open and return an error; if it is 1, continue executing the code.
[0104] ③ Determine if the size of the input data exceeds the set maximum length of a single data entry (1K). If it does, return an error; if it is less than the set maximum length, continue executing the code.
[0105] ④ Check if the number of consecutive data sending timeouts exceeds 3. If it does, it indicates that the slave may be stuck; if it does not exceed 3, continue executing the code.
[0106] ⑤ Assemble the data according to the data structure and execute the code.
[0107] ⑥ If nChID in the parameter is 0, read the value of register 2. If the value is 0, write data and set the value of register 2 to 1; if the value is 1, return an error.
[0108] ⑦ If nChID in the parameter is not 0, store the data in the buffer according to the packet assembly method, read the value of register 0, if the value is 0, write the data, set the value of register 0 to 1, and the function returns 0; if the value is 1, return an error.
[0109] (4) Receiving data:
[0110] ① If nChID in the parameter is 0, read the value of register 4. If the value is 1, read the data and then set the value of register 4 to 0. If the value is 0, it indicates that the slave has no data to send to the master and returns an error.
[0111] ② If nChID in the parameter is not 0, read the value of register 1. If the value is 1, read the data and then set the value of register 1 to 0. If the value is 0, it indicates that the slave has no data to send to the master and returns an error.
[0112] ③ After parsing the read data, store it in the queue buffer of the corresponding channel and continue executing the code;
[0113] ④ Retrieve data from the corresponding queue buffer using the parameter nChID, assign the data address to the pBuf pointer in the parameter, retrieve the data from pData using the application layer main module, and the function returns 0.
[0114] 3. Application layer from modules
[0115] (1) Initialization: The initialization function of the transport layer slave module is called; the queue is initialized. The initialization function needs to be called once before calling this module.
[0116] (2) Reset: The reset function of the transport layer slave module is called; the global variables of this module are cleared to zero.
[0117] (3) Read register: Read data of a fixed size at the offset address passed in by the parameter from the status register and return directly.
[0118] (4) Write to register: Store the fixed-size data at the offset address in the parameters into the status register and return directly.
[0119] (5) Periodically read data from each channel: Call the transport layer receive function to read data from each channel and put it into the queue; if it is an abnormal instruction, process it directly and return; if it is a register instruction, first determine whether it is writing to the register or reading from the register. If it is writing to the register, write the data to the status register buffer according to the offset address. If it is reading from the register, get the fixed size of data sent from the master to the slave from the status register buffer, and then call the transport layer send function to send the result back to the master.
[0120] (6) Loop processing: Retrieves file instructions sequentially from the queue of stored file instructions and processes them. When using this function, a low-priority task is created to call the function repeatedly to process the file instructions.
[0121] (7) Synchronization instruction reception: Take a synchronization instruction from the queue that stores synchronization instructions and return it to the caller for processing.
[0122] (8) Synchronization instruction sending: Put the processed synchronization instruction result into an array, call the transport layer send function from the module to send data, and return 0 after successful sending.
[0123] (9) Asynchronous instruction reception: Take an asynchronous instruction from the queue that stores asynchronous instructions, first determine whether it is an asynchronous request instruction or an asynchronous result retrieval instruction. If it is an asynchronous request instruction, parse the data and return it to the caller. If it is an asynchronous result retrieval instruction, take the processing result from the queue that stores the processing result of the asynchronous instruction, call the transport layer from module sending function to send the result to the master. If there is no processing result in the queue, call the transport layer from module sending function to send an empty packet to the master.
[0124] (10) Asynchronous instruction sending: The processed asynchronous instruction results are stored in the queue for storing asynchronous instruction results.
[0125] (11) Channel instruction reception: Retrieve the channel instruction data of the specified size from the queue that stores the channel instruction data and return it to the caller.
[0126] 4. Transport layer from module
[0127] (1) Initialization: Call the PCIe driver to map the address from the module; clear the data in DMA to 0; initialize the queue.
[0128] (2) Reset: Clear the global variables to 0.
[0129] (3) Sending data:
[0130] ① Check if the flag bit nFlag in the function parameter is 0. If it is 1, it means that the current data has been stored in the data packet, and the function returns 0; if it is 0, continue executing the code.
[0131] ② Check if the device open flag is 1. If it is 0, it means the device is not open and return an error; if it is 1, continue executing the code.
[0132] ③ Determine if the size of the input data exceeds the set maximum length of a single data entry (1K). If it does, return an error; if it is less than the set maximum length, continue executing the code.
[0133] ④ Check if the number of consecutive data sending timeouts exceeds 3. If it does, it indicates that the slave may be stuck; if it does not exceed 3, continue executing the code.
[0134] ⑤ Assemble the data according to the data structure and execute the code.
[0135] ⑥ If nChID in the parameter is 0, read the value of register 4. If the value is 0, write the data and then set the value of register 4 to 1; if the value is 1, return an error.
[0136] ⑦ If nChID in the parameter is not 0, store the data in the buffer according to the packet assembly method, read the value of register 1, if the value is 0, write the data, then set the value of register 1 to 1, and the function returns 0; if the value is 1, return an error.
[0137] (4) Receiving data:
[0138] ① If nChID in the parameter is 0, read the value of register 2. If the value is 1, read the data and then set the value of register 2 to 0. If the value is 0, it indicates that the master has not sent any data to the slave and returns an error.
[0139] ② If nChID in the parameter is not 0, read the value of register 0. If the value is 1, read the data and then set the value of register 0 to 0. If the value is 0, it indicates that the master has not sent any data to the slave and returns an error.
[0140] ③ After parsing the read data, store it in the queue buffer of the corresponding channel and continue executing the code;
[0141] Data is retrieved from the corresponding queue buffer using the parameter nChID, and the data address is assigned to the pBuf pointer in the parameter. The application layer retrieves the data from pBuf using the module, and the function returns 0.
[0142] The other parts of this embodiment are the same as any one of embodiments 1-6 above, so they will not be described again.
[0143] Example 8:
[0144] This embodiment is based on any one of embodiments 1-7 above, such as Figure 7 , Figure 8 As shown, a specific embodiment will be described in detail.
[0145] This embodiment proposes a data interaction method based on the PCIe communication protocol, as detailed in the appendix. Figure 7 The specific implementation is as follows:
[0146] Step S1: Initialization operation.
[0147] Specifically, the following operations are included: initialization of the master and slave ends respectively, including master / slave device initialization, mapping address, and global variable initialization.
[0148] Step S2: Layered operation.
[0149] Specifically, the following operations are included: the PC end connected to the PCIE driver module is used as the master end of the communication protocol, and the embedded device connected to the PCIE driver module is used as the slave end of the communication protocol. The master end is divided into an application layer protocol master module and a transport layer protocol master module, and the slave end is divided into an application layer protocol slave module and a transport layer protocol slave module; this is used to realize data interaction of the communication protocol.
[0150] Step S3: Instruction division.
[0151] The communication data between the master and slave ends is divided into different instructions based on the channel ID; the instructions include synchronization instructions, channel instructions, asynchronous instructions, file instructions, register instructions, and exception instructions;
[0152] The synchronization instruction is used to send communication data that is blocked until the result is returned;
[0153] The asynchronous instruction is used to send communication data that does not need to return a result immediately, but is retrieved after a period of time;
[0154] The channel command is used to send communication data that does not require a return result;
[0155] The file instructions are used to send / receive files;
[0156] The register instruction is used to obtain the slave status;
[0157] The exception command is used to send or receive abnormal communication data.
[0158] Step S4: Reset operation.
[0159] Master / slave communication is restored to its initial state, and the values of global variables are set to their initial values.
[0160] Specifically, the following operations are included: After successful initialization, the master end sends a reset command, and after the slave end successfully receives the command, both ends begin to reset.
[0161] Step S5: Encapsulation operation.
[0162] The master end combines the instruction data, data length, and channel ID into a data packet, and then adds a frame header and frame trailer to encapsulate it into a data frame.
[0163] After the instruction division operation, the communication data received from the user will be encapsulated in the application layer protocol master module, application layer protocol slave module, transport layer protocol master module, and transport layer protocol slave module respectively.
[0164] The specific encapsulation operation is as follows: add a frame header and a frame trailer to the application layer protocol master module and the application layer protocol slave module. The frame header and frame trailer, together with the data body, form a data frame structure. Add a data size and a channel ID to the transport layer protocol master module and the transport layer protocol slave module. The data size and channel ID, together with the data body, form a data frame structure.
[0165] Step S6: Send data operation.
[0166] The master sends instructions to the slave and the slave receives instructions.
[0167] Specifically, the following operations are included: the master end sends data to the slave end asynchronously via double buffering. After receiving the instruction, the slave end puts it into a queue for parsing and processing. Similarly, the slave end sends data to the master end in the same way.
[0168] After the master and slave successfully establish a communication connection, the master sends a command to the slave. Once the slave successfully receives the command, it notifies the master that it can send the next command.
[0169] Step S7: Exception handling operation.
[0170] Timeout handling and reset;
[0171] Specifically, the operations include the following: After the master sends a command, if the slave does not receive it for an extended period, a timeout message is returned to the master notifying it that it will no longer wait. After multiple timeouts, communication is considered interrupted, and a reset operation can be initiated by the master. Similarly, if the master does not receive a result from the slave for an extended period, the slave returns a timeout message.
[0172] The master module and slave module of the transport layer protocol are set with timeout periods. If the set timeout period is exceeded when sending or receiving communication data, the sending or receiving of communication data will fail and a timeout error message will be returned. If three timeout error messages are returned consecutively, the master end calls the reset function to initiate a reset to the slave end.
[0173] Step S8: Receive data operation.
[0174] The slave device returns the result of the instruction processing to the master device, and the master device receives the result.
[0175] Specifically, the operation includes the following steps: After the slave successfully receives the instruction, it is first placed in the buffer queue, then retrieved from the buffer for processing, and the result is sent to the master. After the master successfully receives the instruction, it notifies the slave that it can send the processing result of the next instruction.
[0176] Step S9: Exception handling operation.
[0177] The same applies to step S7, so it will not be repeated here.
[0178] like Figure 7 The following is the main flowchart, as shown below. Figure 8 The diagram shown is a flowchart for the slave end, where the master end represents the PC and the slave end represents the embedded device.
[0179] The communication protocol in this embodiment adopts a hierarchical design with master and slave modules. After the device is powered on, the timer interrupt program and loop task on the slave end will continue to execute. The program on the master end is triggered at any time, but only after the slave end has been successfully started; otherwise, the communication connection will fail.
[0180] Before communication can begin, both the master and slave devices need to be initialized, including allocating memory and initializing the device. If device initialization fails, an error is returned to the caller, prompting them to check the device's power-on status, ports, and driver installation.
[0181] Since the slave program runs continuously after the device is powered on, if the master process exits abnormally, resulting in the failure to receive certain data and interruption of communication, communication is likely to fail when restarting the master process. Therefore, after successful initialization, the master initiates a reset, which means sending a reset command to the slave to restore the communication between the master and slave to the initial state and ensure that the communication connection is successfully established.
[0182] Figure 7 and Figure 8 The flag bit in the protocol corresponds to the register value mentioned in the communication protocol flow logic. After the master end finishes writing data, it sets the flag bit to 1 to notify the slave end to retrieve the data. After the slave end retrieves the data, it sets the flag bit to 0 to notify the master end that the retrieval was successful and it can continue sending the next data. Similarly, the slave end sends data to the master end in the same way. The purpose of this design is to prevent incomplete data from occurring when one end is writing data while the other end is retrieving it.
[0183] The master reset command is transmitted through channel 0x00. If flag 1 is 0, the data is encapsulated according to the data structure in the algorithm design and sent asynchronously in a double-buffered manner. After successful transmission, flag 1 is set to 1 to notify the slave to retrieve the data. If flag 1 is not 0, it indicates that the slave has not yet retrieved the previous data and waits until timeout and returns an error.
[0184] The slave receives data from each channel at regular intervals. When receiving data from channel 0x00, if flag 1 is 1, the reset instruction is taken away, flag 1 is cleared to 0, the master is notified that the reception was successful, and the reset function is called; if the flag is not 1, it indicates that there is no data in the current channel, and data from other channels continues to be received.
[0185] According to the instruction channels allocated in the algorithm design, except for exception instructions, other instructions from the master end are transmitted through channels 0x01 to 0x05.
[0186] Synchronous commands are commands that require an immediate return of results. After the master sends a command, it needs to wait for the slave to process it and return a result. Only after the master successfully receives the result can it send the next command.
[0187] Like synchronous instructions, read register instructions are also instructions that need to return results immediately. The difference is that write register instructions do not need to return results. After the slave receives the instruction, it only needs to notify the master that it has received it.
[0188] Because asynchronous instructions take a long time to process, they are divided into two parts: one to initiate a request and the other to retrieve the result. Both instructions need to return the result immediately. If an asynchronous instruction is being processed, the result retrieved by the asynchronous result retrieval instruction may be 0.
[0189] File commands, similar to those used in FTP file transfer, are divided into different commands, including opening files, changing paths, sending, and closing files. Similar to write register commands, file commands do not require reading the return result after being sent; the slave only needs to notify the master upon receiving the command. If an exception occurs while the slave is processing the command, it can send an exception command to the master.
[0190] The caller can classify the instructions into the above types as needed, and then realize data transmission through the protocol of this invention.
[0191] When the master sends instructions other than exception instructions, it encapsulates the instructions according to the data structure in the form of double buffered asynchronous transmission, adds frame headers and frame trailers, puts them into the buffer, and then checks whether the flag bit 0 is 0. If it is 0, it retrieves the instruction from the buffer and sends it, sets the flag bit to 1, and notifies the slave to receive the instruction. After the slave's timed task successfully receives the instruction, if there is a return result, the master waits for the slave to process it and then returns the result. If it is not 0, it waits until a timeout is returned.
[0192] The slave's timer task periodically reads the value of flag 0. If it is 1, it receives the instruction and stores the instruction in the buffer. If it is not 1, it waits for the next timer cycle to continue reading the value of flag 0.
[0193] During communication, timeouts are set at the points where the master sends instructions, receives instructions and returns results, and the slave sends processing results. This is to prevent the master and slave from waiting indefinitely without a response if communication errors occur.
[0194] The other parts of this embodiment are the same as any one of the embodiments 1-7 above, so they will not be described again.
[0195] The above is a detailed description of the data interaction between the master and slave ends in the communication protocol. Any equivalent conversions made using the content of this specification and drawings, or direct or indirect applications in other technical fields, are also included within the scope of patent protection of this invention.
Claims
1. A data interaction method based on PCIe communication protocol, characterized in that, The communication protocol is layered: the PC connected to the PCIE driver module is the master end of the communication protocol, and the embedded device connected to the PCIE driver module is the slave end of the communication protocol. The master end is divided into the application layer protocol master module and the transport layer protocol master module, and the slave end is divided into the application layer protocol slave module and the transport layer protocol slave module. Used to implement data interaction in communication protocols; After performing the layering operation, an instruction partitioning operation is performed: the communication data between the master and slave ends is divided into different instructions according to the channel ID; the instructions include synchronous instructions, channel instructions, asynchronous instructions, file instructions, register instructions, and exception instructions; The synchronization instruction is used to send communication data that is blocked until the result is returned; The asynchronous instruction is used to send communication data that does not need to return a result immediately, but is retrieved after a period of time; The channel command is used to send communication data that does not require a return result; The file instructions are used to send / receive files; The register instruction is used to obtain the slave status; The abnormal command is used to send or receive abnormal communication data; After the instruction division operation, the communication data received from the user will be encapsulated in the application layer protocol master module, application layer protocol slave module, transport layer protocol master module, and transport layer protocol slave module respectively. The specific encapsulation operation is as follows: add a frame header and a frame trailer to the application layer protocol master module and the application layer protocol slave module. The frame header and frame trailer, together with the data body, form a data frame structure. Add data size and channel ID to the transport layer protocol master module and transport layer protocol slave module. The data size, channel ID and data body form a data frame structure. After the encapsulation operation, the communication data is sent: the transport layer protocol master module assembles the communication data sent by the application layer protocol master module or the transport layer protocol slave module assembles the communication data sent by the application layer protocol slave module, and then sends the communication data in a double-buffered asynchronous manner. The specific operations for sending communication data in the dual-buffered asynchronous mode include the following: In the dual-buffered asynchronous mode, three global variables are defined, denoted as buffer identifier, first identifier, and second identifier. If the buffer identifier is 1, the second identifier is set to 1, and the communication data is written to the second buffer global array. After the communication data is completely written to the second buffer global array, the buffer identifier is cleared to 0. After clearing to 0, the buffer identifier is set to 1. The communication data is retrieved, and it is determined whether the second identifier is 0. If it is 0, the communication data in the second buffer array is retrieved directly; if it is 1, the data is retrieved only after the communication data has been written. Alternatively, if the buffer identifier is 0, the first identifier is set to 1, and the communication data is written to the first buffer global array. After the communication data is completely written to the first buffer global array, the buffer identifier is cleared to 0. After clearing to 0, the buffer identifier is set to 1. The communication data is retrieved, and it is determined whether the first identifier is 0. If it is 0, the communication data in the first buffer array is retrieved directly; if it is 1, the data is retrieved only after the communication data has been written.
2. The PCIe-based communication protocol data interaction method as described in claim 1, characterized in that, After sending data, perform communication data receiving operations: buffer communication data in the form of a queue, and when the communication data of the tail pointer and the communication data of the head pointer point to the upper bound of the communication data array, reset the communication data of the tail pointer and the communication data of the head pointer to the beginning of the communication data array.
3. The PCIE-based communication protocol data interaction method as described in claim 2, characterized in that, After performing a communication data reception operation, a timeout period is set in the master module and slave module of the transport layer protocol. If the set timeout period is exceeded when sending or receiving communication data, the sending or receiving of communication data will fail and a timeout error message will be returned. If a timeout error message is returned 3 times in a row, the master end calls the reset function to initiate a reset to the slave end.
4. The PCIe-based communication protocol data interaction method as described in claim 1, characterized in that, The channel ID corresponding to the abnormal instruction is 0x00; The channel ID corresponding to the synchronization command is 0x01; The channel ID corresponding to the channel command is 0x02; The channel ID corresponding to the asynchronous instruction is 0x03; The channel ID corresponding to the file instruction is 0x04; The channel ID corresponding to the register instruction is 0x05.
Citation Information
Patent Citations
PCIe device dynamic power consumption saving method and low-power PCIe device
CN111124975A
Double-cache method, device and equipment based on asynchronous decorator and storage medium
CN111444223A
Software radio platform and method and device for task program loading
CN113890782A