A data communication method and system based on SPI
By using communication flag bits, position check bits and accuracy check bits in SPI communication, the data disorder caused by SPI communication clock offset is solved, the completeness and accuracy of data transmission is achieved, and the communication is improved Reliability and robustness are suitable for resource-constrained embedded systems.
Patent Information
- Application Number
- CN202411204651.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-30
- Publication Date
- 2024-11-15
- Estimated Expiration
- 2044-08-30
AI Technical Summary
SPI communication is prone to data confusion when clock offset, and lacks data flow control and response mechanisms, affecting the integrity and accuracy of data transmission.
By introducing communication flags, position check bits and accuracy check bits in SPI communication, the data frames are checksum restored using the preset frame protocol to ensure the integrity and accuracy of data transmission and avoid time shifts and signals distortion.
Effectively eliminate the impact of time shift, detect and correct data misalignment, avoid signal distortion caused by interference, improve the reliability and robustness of data transmission, and does not require additional hardware circuits and complex algorithms, and has low resource utilization.
Smart Images

Figure CN118733511B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of communication technology, and in particular to a data communication method and system based on SPI. Background Art
[0002] SPI (Serial Peripheral Interface) communication has the advantages of full-duplex operation capability, simple operation, and high data transmission rate. Therefore, it has been widely used in scenarios where high communication speed is required between microcontrollers (MCUs).
[0003] However, SPI communication also has some limitations. For example, in the communication between a master device and a slave device, at least three pins are required, which places high demands on hardware resources. In addition, SPI only supports a single-host architecture, lacks data flow control and response mechanisms, and cannot confirm whether the data is successfully received. These characteristics may bring certain inconveniences in some applications. In addition, SPI communication has high requirements for signal integrity and is easily affected by external interference. When the clock signal of SPI is interfered with, the rising edge or falling edge of the clock signal detected by the master device and the slave device may be inconsistent, resulting in data transmission errors or misalignment.
[0004] Therefore, a data communication method and system based on SPI is needed, which can solve the data confusion problem caused by clock offset when using SPI communication. Summary of the invention
[0005] One of the purposes of the present invention is to provide a data communication method based on SPI, which can solve the problem of data confusion caused by clock offset when using SPI communication.
[0006] In order to solve the above technical problems, this application provides the following technical solutions:
[0007] A data communication method based on SPI comprises the following steps:
[0008] S1, the master device and the slave device are powered on and initialized;
[0009] S2, the master device determines whether the communication flag is detected to be set to the first preset value, and waits if it is not detected. If the communication flag is detected to be the first preset value, the master device starts to send data to the slave device and generates a clock signal at the same time;
[0010] S3, after sending a data, the master device waits for the slave device to reply data, and then sends the next data after receiving the data; after receiving the data sent by the master device, the slave device immediately returns the corresponding reply data and waits for the master device to send the next data;
[0011] S4, the master device determines whether the transmission is completed. If the transmission is not completed, jump to step S3. If the transmission is completed, stop data transmission and jump to step S5.
[0012] S5, the master device and the slave device determine whether the frame data sent is complete. If it is incomplete, the frame data is discarded and the process goes to step S8. If it is complete, the process goes to step S6.
[0013] S6, the master device and the slave device determine whether the frame data is time-shifted, and if so, restore the frame data;
[0014] S7, the master device and the slave device determine whether the frame data is distorted, if distorted, discard the frame data and jump to step S8, if not distorted, use the frame data and jump to step S8;
[0015] S8. Set the communication flag to a second preset value, end the communication, and jump to step S2.
[0016] Further, in step S2, according to the preset frame protocol, the data frame is represented by an array, the length of an array is n+3, including a position check bit 1, a position check bit 2, an accuracy check bit and n data bits for storing data; wherein, the position check bit 1 is stored in the 0th bit of the array; the position check bit 2 is stored in the n+2th bit of the array; the accuracy check bit is stored in the n+1th bit of the array, and the data bits are stored in the 1st to nth bits of the array after being assigned values;
[0017] When the master device detects that the communication flag bit is the first preset value, it sums the first bit to the nth bit of the array to be sent, and stores the result in the accuracy check bit.
[0018] Further, in step S3, the master device sends data in the order of the array, waits for the slave device to reply data after sending the first data, starts sending the next data after receiving the reply data from the slave device, and continues to calculate the number of sent data until all the data in the array are sent;
[0019] After receiving a data from the master device, the slave device triggers the receive interrupt. After entering the receive interrupt, the slave device first assigns a value to the data bit to be sent according to the preset frame protocol, sums the data bits from the 1st to the nth bit of the array, and stores it in the accuracy check bit; after receiving the data transmitted by the master device, the slave device sends the data in the array as reply data to the master device, and calculates the number of sent data at the same time.
[0020] Furthermore, in step S4, the master device and the slave device start to verify after sending all the data, and clear the calculated number of sent data at the same time.
[0021] Furthermore, in step S5, the master device and the slave device determine whether the data of the position check bit 1 and the position check bit 2 are received at the same time. If they are received at the same time, the frame data is complete.
[0022] Further, in step S6, the master device and the slave device determine whether the actual positions of the position check bit 1 and the position check bit 2 are consistent with the positions specified in the preset frame protocol. If they are consistent, the received array is directly used as the result array; if they are inconsistent, a time shift has occurred, and the time shift amount is determined according to the actual positions of the position check bit 1 and the position check bit 2, and the received array is restored according to the time shift amount to obtain a result array.
[0023] Furthermore, in step S7, the master device and the slave device sum the first to nth bits in the final result array, and compare the summation result with the accuracy check bit in the final result array. If the two are the same, there is no distortion.
[0024] Furthermore, the first preset value is 1, and the second preset value is 0.
[0025] A second object of the present invention is to provide a data communication system based on SPI, comprising a master device and a slave device, wherein the master device and the slave device use the above method to perform data transmission.
[0026] This solution can eliminate the impact of time shift when SPI communication is time shifted, and can also detect the distortion caused by communication interference to avoid the adverse effects of erroneous data. This solution is simple and does not require the addition of additional hardware circuits and excessive algorithms, and occupies less MCU resources.
[0027] In summary, this solution can solve the data confusion problem caused by clock offset when using SPI communication. BRIEF DESCRIPTION OF THE DRAWINGS
[0028] Figure 1 It is a flow chart of a data communication method embodiment 1 based on SPI;
[0029] Figure 2 It is a schematic diagram of the offset of the sending array and the receiving array in the second embodiment of the data communication method based on SPI;
[0030] Figure 3 It is a schematic diagram of restoring a receiving array in Embodiment 2 of a data communication method based on SPI;
[0031] Figure 4 This is a schematic diagram of a second embodiment of a data communication method based on SPI after the receiving array is restored. DETAILED DESCRIPTION
[0032] The following is further described in detail through specific implementation methods:
[0033] Embodiment 1
[0034] like Figure 1 As shown, a data communication method based on SPI in this embodiment includes the following contents:
[0035] S1, the master device and the slave device are powered on and initialized. In this embodiment, the master device and the slave device are MCUs, and the master device and the slave device perform data transmission through an SPI communication module;
[0036] S2, the master device determines whether the communication flag is set to the first preset value, if not, it waits, if the communication flag is the first preset value, the master device starts to send data to the slave device, and generates a clock signal. In this embodiment, the first preset value is 1, that is, the communication flag is 1.
[0037] Specifically, according to the preset frame protocol, the data frame is represented by an array, and the length of an array is n+3, including a position check bit 1, a position check bit 2, an accuracy check bit and n data bits for storing data. Among them, the position check bit 1 is stored in the 0th bit of the array; the position check bit 2 is stored in the n+2th bit of the array; the accuracy check bit is stored in the n+1th bit of the array, and the data bits are stored in the 1st to nth bits of the array after being assigned values.
[0038] When the master device detects that the communication flag is 1, it sums the 1st to nth bits of the array to be sent and stores the result in the accuracy check bit; that is, it first assigns values to the data bits, then sums the data bits and stores them in the accuracy check bit.
[0039] S3. After sending a data, the master device waits for the slave device to reply data, and then sends the next data after receiving the data. After receiving the data sent by the master device, the slave device immediately returns the corresponding reply data and waits for the master device to send the next data.
[0040] Specifically, the master device sends data in the order of the array. After sending the first data, the master device waits for the slave device to reply with data. After receiving the reply data from the slave device, the master device starts to send the next data and continues to calculate the number of sent data until all the data in the array are sent.
[0041] After receiving a data from the master device, the slave device triggers a receive interrupt. After entering the receive interrupt, the slave device assigns a value to the data bit to be sent according to the preset frame protocol, sums the data bits from the 1st to the nth bit of the array, and stores it in the accuracy check bit (in this embodiment, this operation only needs to be performed when the frame data is sent for the first time). After the slave device receives the data transmitted by the master device, it sends the data in the array as reply data to the master device, and calculates the number of sent data at the same time. The slave device can determine whether the frame data has been sent by recording how much data has been sent during the sending process.
[0042] S4, the master device determines whether the sending is completed. If the sending is not completed, jump to step S3. If the sending is completed, stop sending data and jump to step S5. Specifically, the master device and the slave device start to verify after sending all the data, and clear the calculated number of sent data at the same time.
[0043] S5, the master device and the slave device determine whether the frame data is complete. If not, discard the frame data and jump to step S8. If complete, jump to step S6. Specifically, determine whether the data of position check bit 1 and position check bit 2 are received at the same time. If they are received at the same time, the frame data is complete.
[0044] S6, the master device and the slave device determine whether the frame data has time shifted. If time shifted, the frame data is restored. Specifically, determine whether the actual positions of the position check bit 1 and the position check bit 2 are consistent with the positions specified in the preset frame protocol. If they are consistent, the received array is directly used as the result array. If they are inconsistent, time shift has occurred. The time shift amount is determined according to the actual positions of the position check bit 1 and the position check bit 2, and the received array is restored according to the time shift amount to obtain a result array.
[0045] S7, the master device and the slave device determine whether the frame data is distorted, if distorted, discard the frame data and jump to step S8, if not distorted, use the frame data and jump to step S8;
[0046] The first to nth bits in the final result array are summed, and the summed result is compared with the accuracy check bit in the final result array. If the two are the same, there is no distortion.
[0047] S8, setting the communication flag bit to a second preset value, ending the communication, and jumping to step S2, waiting for the next communication flag bit to be set to 1 for communication. In this embodiment, the second preset value is 0, that is, the communication flag bit is 0.
[0048] Each communication starts with the master device. The slave device starts transmitting data after receiving a data sent by the master device. The communication ends after the master device sends and receives a complete frame of data. When the master device and the slave device start a new round of communication, the contents of the send array will be updated.
[0049] A data communication system based on SPI includes a master device and a slave device. In this embodiment, the master device and the slave device are MCUs, and both the master device and the slave device include SPI communication modules. The SPI communication modules of the master device and the slave device perform data transmission through the above method.
[0050] SPI communication has shortcomings such as lack of response mechanism, lack of verification and data flow control. Existing research has methods that add hardware assistance, such as adding a handshake signal line to determine whether the communication is over, but this method is greatly restricted by hardware resources. In some actual projects, there may be situations where signal lines cannot be added due to hardware limitations. There is also a method using a software response mechanism. During formal communication, a response bit is first generated to determine whether the clock is offset. If a deviation occurs, the response bit is sent again by regenerating the clock signal, etc. until it is determined that there is no time shift before sending the formal communication content. The software response method will increase communication delay, and the judgment of communication operations will occupy more CPU resources, which is not conducive to the operation of other tasks.
[0051] The scheme of this embodiment ensures the accuracy and integrity of data transmission by setting the communication flag bit, position check bit and accuracy check bit during the communication process. First, through the double check of position check bit 1 and position check bit 2, the communication errors caused by time shift can be effectively detected and corrected to ensure that the position of the received data frame is accurate. Secondly, by summing the data bits and comparing them with the accuracy check bit, the signal distortion caused by interference during the communication process can be detected, thereby avoiding the use of erroneous data during the communication process. Finally, this scheme does not need to add additional hardware circuits and complex algorithms, and the MCU resource occupation is very low. It is suitable for application in resource-constrained embedded systems, and significantly improves the reliability and robustness of SPI communication. This scheme only needs to use less checksums and logical judgments to complete the judgment recovery of SPI communication time shift and the verification of data accuracy, which has high practicality and is relatively simple to implement.
[0052] Embodiment 2
[0053] like Figure 2 As shown, in this embodiment, the position check bit 1 is stored in the 0th bit of the array, and the content is 0x1111; the position check bit 2 is stored in the n+2th bit of the array, and the content is 0x9999; the accuracy check bit is stored in the n+1th bit of the array.
[0054] The array sent by the master device to the slave device, and the array sent by the slave device to the master device is called the sending array. Similarly, the array received by the slave device from the master device, and the array received by the master device from the slave device is called the receiving array.
[0055] In step S5, during the integrity check, the received array content is searched, and when 0x1111 is found, the position of the data in the received array is saved, and when 0x9999 is found, the position of the data in the received array is saved. If 0x1111 and 0x9999 are not found at the same time after the search of the entire received array content is completed, the frame data is incomplete; that is, if only one of 0x1111 and 0x9999 is found, or neither of them is found, the frame data is discarded.
[0056] In step S6, when checking for time shift, when 0x1111 and 0x9999 are found at the same time, it is determined whether their positions are consistent with the positions specified in the preset frame protocol. If they are consistent, the received array is directly used as the result array; if they are inconsistent, time shift has occurred. Figure 2 As shown, at this time, 0x1111 in the receiving array is moved from the 0th bit to the 2nd bit compared to the position in the sending array, and a total of two offsets are sent.
[0057] like Figure 3 As shown, when restoring, it is only necessary to store 0x1111 to the 0th bit of the final result array, and at the same time, store the total n data from the 3rd bit to the n+2th bit of the receiving array to the 1st bit to the nth bit of the final result array in the existing order.
[0058] The total length of the receiving array is n+3 minus the position 2 where 0x1111 is located is equal to n+1. The position where 0x9999 is located in the receiving array is 1, which means there are two data from the starting position 0 to 0x9999. Figure 4 As shown, it is only necessary to store the two data in the final result array in sequence, that is, to store the 0th data of the receiving array in the n+1th bit of the final result array, and to store the 1st data of the receiving array in the n+2th bit of the final result array.
[0059] In step S7, after determining that the SPI communication has not shifted or the shift has been restored, the 1st to nth bits in the final result array are summed. The summed result is compared with the n+1th bit in the final result array. If the two are the same, it proves that the data has not been mutated due to interference, that is, it is not distorted. If they are not the same, the frame data is discarded.
[0060] The above are only embodiments of the present invention. The invention is not limited to the fields involved in this implementation case. The common sense such as the known specific structures and characteristics in the scheme is not described in detail here. The ordinary technicians in the relevant field know all the common technical knowledge in the technical field to which the invention belongs before the application date or the priority date, can obtain all the existing technologies in the field, and have the ability to apply the conventional experimental means before that date. The ordinary technicians in the relevant field can improve and implement this scheme in combination with their own abilities under the enlightenment given by this application. Some typical known structures or known methods should not become obstacles for ordinary technicians in the relevant field to implement this application. It should be pointed out that for those skilled in the art, without departing from the structure of the present invention, several deformations and improvements can be made, which should also be regarded as the protection scope of the present invention, which will not affect the effect of the implementation of the present invention and the practicality of the patent. The protection scope required by this application shall be based on the content of its claims, and the specific implementation methods and other records in the specification can be used to explain the content of the claims.
Claims
1. A data communication method based on SPI, characterized in that: The steps include: S1, the master device and the slave device are powered on and initialized; S2, the master device determines whether the communication flag is detected to be set to the first preset value, and waits if it is not detected. If the communication flag is detected to be the first preset value, the master device starts to send data to the slave device and generates a clock signal at the same time; According to the preset frame protocol, the data frame is represented by an array, the length of an array is n+3, including a position check bit 1, a position check bit 2, an accuracy check bit and n data bits for storing data; wherein, the position check bit 1 is stored in the 0th bit of the array; the position check bit 2 is stored in the n+2th bit of the array; the accuracy check bit is stored in the n+1th bit of the array, and the data bits are stored in the 1st to nth bits of the array after being assigned values; When the master device detects that the communication flag bit is the first preset value, it sums the first bit to the nth bit of the array to be sent, and stores the result in the accuracy check bit; S3, after sending a data, the master device waits for the slave device to reply data, and then sends the next data after receiving the data; after receiving the data sent by the master device, the slave device immediately returns the corresponding reply data and waits for the master device to send the next data; S4, the master device determines whether the transmission is completed. If the transmission is not completed, jump to step S3. If the transmission is completed, stop data transmission and jump to step S5. S5, the master device and the slave device determine whether the sent frame data is complete. If it is incomplete, the frame data is discarded and the process goes to step S8. If it is complete, the process goes to step S6. S6, the master device and the slave device determine whether the frame data is time-shifted, and if so, restore the frame data; The master device and the slave device determine whether the actual positions of the position check bit 1 and the position check bit 2 are consistent with the positions specified in the preset frame protocol. If they are consistent, the received array is directly used as the result array; if they are inconsistent, a time shift has occurred, and the time shift amount is determined according to the actual positions of the position check bit 1 and the position check bit 2, and the received array is restored according to the time shift amount to obtain a result array; S7, the master device and the slave device determine whether the frame data is distorted, if distorted, discard the frame data and jump to step S8, if not distorted, use the frame data and jump to step S8; S8. Set the communication flag to a second preset value, end the communication, and jump to step S2.
2. The SPI-based data communication method according to claim 1, wherein: In step S3, the master device sends data in the order of the array, waits for the slave device to reply data after sending the first data, starts sending the next data after receiving the reply data from the slave device, and continues to calculate the number of sent data until all the data in the array are sent; After receiving a data from the master device, the slave device triggers a receive interrupt. After entering the receive interrupt, the slave device first assigns a value to the data bit to be sent according to the preset frame protocol, sums the data bits from the 1st to the nth bit of the array, and stores it in the accuracy check bit; After receiving the data transmitted by the master device, the slave device sends the data in the array as reply data to the master device and calculates the number of sent data at the same time.
3. The SPI-based data communication method according to claim 2, wherein: In step S4, after the master device and the slave device have sent all the data, verification is started and the calculated number of sent data is cleared.
4. The SPI-based data communication method according to claim 3, characterized in that: In step S5, the master device and the slave device determine whether the data of the position check bit 1 and the position check bit 2 are received at the same time. If they are received at the same time, the frame data is complete.
5. The SPI-based data communication method according to claim 4, characterized in that: In step S7, the master device and the slave device sum the first to nth bits in the final result array, and compare the summation result with the accuracy check bit in the final result array. If the summation result and the accuracy check bit in the final result array are the same, there is no distortion.
6. The SPI-based data communication method according to claim 5, characterized in that: The first preset value is 1, and the second preset value is 0.
7. A data communication system based on SPI, comprising a master device and a slave device, characterized in that: The master device and the slave device perform data transmission using the method described in any one of claims 1 to 6.
