Data transmission method and device, equipment and storage medium
By setting at least two buffers in the controller and using timers to control data storage and forwarding, the problem of data frame loss when CAN data is forwarded via SPI is solved, improving the integrity and efficiency of data transmission.
Patent Information
- Application Number
- CN202511278551.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-08
- Publication Date
- 2025-12-23
AI Technical Summary
In existing technologies, CAN data is forwarded via SPI using only a single buffer, which leads to data frame loss.
Set up at least two buffers in the controller and control the data storage and forwarding process through a timer to ensure that when data is written to one buffer, the other buffer has already completed data forwarding, thus avoiding data loss caused by a single buffer being full.
It improves the integrity and efficiency of data transmission, ensures the effectiveness of data loss prevention, and addresses the problem of data frame loss. Existing technologies have solved the problem of data frame loss.
Smart Images

Figure CN121187993A_ABST
Abstract
Description
Technical Field
[0001] This application relates to, but is not limited to, the field of data transmission technology, and in particular to a data transmission method, apparatus, device, and storage medium. Background Technology
[0002] In related technologies, when CAN data is forwarded via SPI, only a single buffer is designed. However, after the data is stored in the single buffer, the CAN data can only be stored again after the SPI transmission is completed, which will cause data frame loss. Summary of the Invention
[0003] In view of the above, embodiments of this application provide at least one data transmission method, apparatus, device, and storage medium.
[0004] The technical solution of this application embodiment is implemented as follows:
[0005] In a first aspect, embodiments of this application provide a data transmission method applied to a controller, comprising: in response to starting a first timer, storing CAN data transmitted by an external device via a CAN controller local area network bus into an idle buffer among the at least two buffers; during the storage of the CAN data in the idle buffer, forwarding the CAN data stored in the occupied buffer among the at least two buffers via a serial peripheral interface (SPI) bus; wherein the data transmission rate of the CAN bus is less than the data transmission rate of the SPI bus.
[0006] Secondly, embodiments of this application provide a data transmission device applied to a controller. The device includes: a storage module, configured to, in response to the start of a first timer, store CAN data transmitted by an external device via a CAN controller local area network bus into an idle buffer among the at least two buffers; and a forwarding module, configured to, during the process of storing the CAN data in the idle buffer, forward the CAN data stored in the occupied buffer among the at least two buffers via a serial peripheral interface (SPI) bus; wherein the data transmission rate of the CAN bus is less than the data transmission rate of the SPI bus.
[0007] Thirdly, embodiments of this application provide a computer device, including a memory and a processor, wherein the memory stores a computer program that can run on the processor, and the processor executes the program to implement some or all of the steps in the above-described method.
[0008] Fourthly, embodiments of this application provide a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements some or all of the steps in the above-described method.
[0009] Fifthly, embodiments of this application provide a computer program product, including a computer program or instructions, which, when executed by a processor, implement some or all of the steps in the above-described method.
[0010] Technical Effects: By setting at least two buffers on the main controller, in response to the start of the first timer, CAN data sent by external devices via the CAN controller LAN bus is stored in the idle buffer among the at least two buffers; and CAN data stored in the occupied buffer is forwarded via the serial peripheral interface (SPI) bus. This way, when data is written to one buffer, data from the other buffer is forwarded, avoiding data loss due to a single buffer being full. The data transmission rate of the CAN bus is lower than that of the SPI bus. This ensures that while one buffer is storing data in one timer cycle, there is sufficient time to forward data from the other buffer, thus avoiding data frame loss. This improves both the integrity and efficiency of data transmission.
[0011] It should be understood that the above general description and the following detailed description are merely exemplary and explanatory, and are not intended to limit the technical solutions of this application. Attached Figure Description
[0012] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with this application and, together with the specification, serve to explain the technical solutions of this application.
[0013] Figure 1 A schematic diagram illustrating the implementation flow of a data transmission method provided in an embodiment of this application;
[0014] Figure 2 A schematic diagram illustrating the implementation flow of a data transmission method provided in an embodiment of this application;
[0015] Figure 3 A schematic diagram illustrating the implementation flow of a data transmission method provided in an embodiment of this application;
[0016] Figure 4 A schematic diagram illustrating the implementation flow of a data transmission method provided in an embodiment of this application;
[0017] Figure 5 A schematic diagram illustrating the implementation flow of a data transmission method provided in an embodiment of this application;
[0018] Figure 6 A schematic diagram of CAN data interaction provided in an embodiment of this application;
[0019] Figure 7A schematic diagram illustrating data transmission as provided in an embodiment of this application;
[0020] Figure 8 A schematic diagram illustrating data transmission as provided in an embodiment of this application;
[0021] Figure 9 This is a schematic diagram of the composition structure of a data transmission device provided in an embodiment of this application;
[0022] Figure 10 This is a schematic diagram of the hardware entity of a computer device provided in an embodiment of this application. Detailed Implementation
[0023] To make the objectives, technical solutions, and advantages of this application clearer, the technical solutions of this application are further described in detail below with reference to the accompanying drawings and embodiments. The described embodiments should not be regarded as limitations on this application. All other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0024] In the following description, references to "some embodiments" refer to a subset of all possible embodiments. It is understood that "some embodiments" may be the same or different subsets of all possible embodiments and may be combined with each other without conflict. The terms "first / second / third" are used merely to distinguish similar objects and do not represent a specific ordering of objects. It is understood that "first / second / third" may be interchanged in a specific order or sequence where permitted, so that the embodiments of this application described herein can be implemented in an order other than that illustrated or described herein.
[0025] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this application pertains. The terminology used herein is for descriptive purposes only and is not intended to limit the scope of this application.
[0026] In related technologies, when CAN data is forwarded via SPI, only a single buffer is designed. However, after the data is stored in the single buffer, the CAN data can only be stored again after the SPI transmission is completed, which will cause data frame loss.
[0027] This application provides a data transmission method, which can be executed by a processor of a computer device. The computer device refers to a server, laptop computer, tablet computer, desktop computer, smart TV, set-top box, mobile device (e.g., mobile phone, portable video player, personal digital assistant, dedicated messaging device, portable gaming device), or any other device with data processing capabilities.
[0028] Figure 1This is a schematic diagram illustrating the implementation flow of a data transmission method provided in an embodiment of this application. Figure 1 As shown, the method is applied to a controller, which has at least two buffers; the method includes the following steps S101 and S102, combining... Figure 1 The steps are explained below.
[0029] Step S101: In response to starting the first timer, store the CAN data sent by the external device through the CAN controller LAN bus into the idle buffer of the at least two buffers.
[0030] In some embodiments, the controller may be a controller located in the vehicle, such as a microcontroller unit (MCU).
[0031] In some embodiments, the controller has at least two buffers, each with a memory size that may be the same or different, and each buffer can be used as both a send buffer and a receive buffer; it is understood that each buffer can be used to store data or to send data.
[0032] In some embodiments, the first timer can be used as a trigger switch to trigger data storage and data transmission. After the first timer is started, the timing begins, and the controller controls the idle buffer to store CAN data.
[0033] In some embodiments, the controller monitors the status of each buffer in real time to ensure that CAN data is stored in the idle buffer. Flag bits can be added to each buffer to distinguish its status as "idle," "occupied," or "writing in progress." For example, the flag bits can include 0, 1, and 2, where 0 represents an "idle" buffer, 1 represents an "occupied" buffer, and 2 represents a "writing in progress" buffer.
[0034] In some embodiments, the flag bits of each buffer can be determined based on the operating state of the first timer. It is understood that when the first timer transitions from an interrupt state to a start state, the flag bits of buffers whose flag bits were 1 before the transition are set to 0, and the flag bits of buffers whose flag bits were 0 before the transition are set to 1.
[0035] In some embodiments, by setting at least two buffers, parallel "store-forward" processing can be achieved. When one buffer is written with CAN data, the other buffer can forward old data, avoiding data loss due to a single buffer being full. At the same time, the usage frequency of each buffer can be balanced through a round-robin mechanism (such as a circular buffer).
[0036] In some embodiments, the external device is a device with CAN communication capability, with a built-in CAN controller and transceiver, capable of encapsulating data frames (including ID, data fields, etc.) according to the CAN protocol specification and transmitting them through the CAN bus, and interacting with the controller through the CAN bus.
[0037] In some embodiments, external devices may be sensors, actuators, etc., installed in the vehicle; for example, vehicle speed sensor, water temperature sensor, acceleration sensor, fuel injector, brake booster pump, lighting control module, etc.
[0038] In some embodiments, the external device may also be a gateway, used to convert protocols between different buses / networks into CAN data and send it to the controller via the CAN bus; for example, in automotive electronics, the vehicle gateway accesses the CAN network via the CAN interface.
[0039] In some embodiments, when the first timer is started, the controller determines the idle state buffer in at least two buffers and stores the CAN data sent by the external device via the CAN bus into the idle state buffer.
[0040] For example, after starting the first timer, the controller acquires the buffer with the flag bit set to 0 from at least two buffers and stores the CAN data forwarded by the vehicle gateway in the buffer with the flag bit set to 0.
[0041] Step S102: During the process of storing the CAN data in the idle buffer, the CAN data stored in the occupied buffer of the at least two buffers is forwarded through the serial peripheral interface SPI bus.
[0042] The data transmission rate of the CAN bus is lower than that of the SPI bus.
[0043] In some embodiments, the controller interacts with other controllers or devices via an SPI bus to achieve data exchange. For example, in a vehicle control system, the controller MCU communicates with the system-on-chip (SOC) via an SPI bus.
[0044] In some embodiments, while starting a first timer to trigger the storage of newly received CAN data, the CAN data already stored in the occupied buffer is forwarded to the SOC connected to the controller via the SPI bus.
[0045] In some embodiments, after starting the first timer, the CAN data can be stored in the buffer of the idle state and the CAN data in the buffer of the occupied state can be forwarded on the SPI bus simultaneously.
[0046] In some embodiments, after starting the first timer, the CAN data can be stored in the idle state buffer first, and during the process of storing the CAN data in the idle state buffer, the CAN data in the occupied state buffer can be forwarded on the SPI bus. For example, if the timing period of the first timer is 50ms, the CAN data can be stored in the idle state buffer for 20ms before the execution of forwarding the CAN data in the occupied state buffer on the SPI bus begins.
[0047] In some embodiments, the time for forwarding CAN data in the occupied buffer via the SPI bus needs to be less than or equal to the time for storing the CAN data in the idle buffer; it is understood that the data transmission rate of the SPI bus needs to be much greater than the data transmission rate of the CAN bus.
[0048] In some embodiments, the buffer is capable of storing at least a target number of bytes of data; the target number of bytes is the number of bytes of CAN data transmitted at the transmission rate of the CAN bus for one working cycle of the timer.
[0049] The timer's operating period is pre-defined, ranging from the start state to the timeout interrupt state, for example, 50ms. After the timer starts, it counts for 50ms. During this period, the external device stores CAN data into the idle buffer at the CAN bus transmission rate. For example, if the CAN bus transmission rate is 2 Mbit / s, and 10,000 bytes of data can be transmitted in 50ms, then the buffer memory must be at least 10,000 bytes.
[0050] In some embodiments, the data transmission rate of the CAN bus and the data transmission rate of the SPI bus can be dynamically adjusted based on the size of the buffer and / or the working cycle of the timer.
[0051] In some embodiments, the buffer needs to be able to store at least one working cycle of timer data transmitted at the CAN bus transmission rate. If the CAN bus transmission rate decreases, the size of the buffer can be dynamically reduced, and vice versa. If the working cycle of the timer decreases, the size of the buffer can be dynamically reduced, and vice versa.
[0052] For example, if the CAN bus has a transmission rate of 2 Mbit / s and the timer has a working period of 50 ms, it can transmit 10,000 bytes of data, so the buffer memory must be at least 10,000 bytes; if the CAN bus has a transmission rate of 1 Mbit / s, the buffer memory size can be adjusted to at least 5,000 bytes.
[0053] In this embodiment, by setting at least two buffers on the main controller, in response to the start of the first timer, CAN data sent by external devices via the CAN controller LAN bus is stored in the idle buffer among the at least two buffers; and the CAN data stored in the occupied buffer is forwarded via the serial peripheral interface (SPI) bus. This way, when data is written to one buffer, data in the other buffer is forwarded, avoiding data loss due to a single buffer being full. The data transmission rate of the CAN bus is lower than that of the SPI bus. This ensures that while one buffer is storing data in one timer cycle, there is sufficient time to forward the data in the other buffer, thus avoiding data frame loss. This improves both the integrity and efficiency of data transmission.
[0054] In some embodiments, after step S101, the following scheme is further included:
[0055] In response to a timeout interrupt of the first timer, the idle buffer is switched to an occupied state; and the occupied buffer is switched to an idle state.
[0056] In some embodiments, when the first timer reaches the set timing period (timeout), an interrupt request is sent to the controller (such as an MCU).
[0057] In some embodiments, after the first timer timeout interrupt, it is first necessary to identify the status of each buffer. Since the data transmission rate of the SPI bus is higher than that of the CAN bus, after the first timer timeout interrupt, the CAN data in the previously occupied buffer can be forwarded completely, thereby switching the occupied buffer to an idle state. After the first timer timeout interrupt, it indicates that 50ms of CAN data has been stored in the previously idle buffer at the CAN bus transmission rate, thereby switching the previously idle buffer to an occupied state.
[0058] In some embodiments, the controller includes a first buffer and a second buffer. Before the first timer is started, the first buffer is in an idle state and the second buffer is in an occupied state. After the first timer times out and is interrupted, if 50ms of CAN data is stored in the first buffer at the CAN bus transmission rate, the state of the first buffer is switched to the occupied state. If the CAN data in the second buffer is forwarded through the SPI bus, the state of the second buffer is switched to the idle state.
[0059] Specifically, the second buffer, which is in an idle state, can continue to store CAN data sent by external devices via the CAN bus; and the CAN data in the first buffer, which is in an occupied state, can be forwarded via the SPI bus.
[0060] For example, before the first timer is started, the status flag of the first buffer is 0 (indicating an idle state) and the status flag of the second buffer is 1 (indicating an occupied state); in response to the timeout interrupt of the first timer, the status flag of the first buffer is switched to 1 and the status flag of the second buffer is switched to 0.
[0061] In this embodiment, the idle buffer is switched to an occupied state in response to a timeout interrupt of the first timer; and the occupied buffer is switched to an idle state. When an external device sends data via the CAN bus, it can be ensured that the data is continuously stored in the idle buffer, and the SPI bus only forwards data in the occupied buffer. This avoids incomplete data forwarding caused by the SPI bus simultaneously forwarding data to a buffer while CAN data is being written to it, or data being overwritten by the CAN bus while the SPI bus is reading data, resulting in the loss of old data, thus ensuring the integrity of the CAN data transmission process.
[0062] Figure 2 This is a schematic diagram illustrating the implementation flow of a data transmission method provided in an embodiment of this application. The method may include steps S201 to S202, combining... Figure 2 The steps shown are explained.
[0063] Step S201: In response to the timeout interrupt of the first timer, start the second timer.
[0064] In some embodiments, timing begins when the first timer is started, and continues until the timing reaches the working cycle of the first timer, at which point a timeout interrupt of the first timer is triggered, and a second timer is started.
[0065] For example, if the working period of the first timer is 50ms, then after the first timer starts counting for 50ms, the timeout interrupt of the first timer is triggered, and the second timer is started, and the working period of the second timer begins to be counted.
[0066] In some embodiments, the duty cycle of the second timer may be the same as or different from that of the first timer; it may be adjusted based on the CAN data cached in the CAN bus.
[0067] For example, if the CAN data buffered in the CAN bus includes 100,000 bytes of data, meaning that the external device needs to send 100,000 bytes of data to the controller, then to ensure the efficiency and accuracy of data transmission, the working cycle of the second timer can be increased to increase the amount of CAN data stored in the idle buffer at one time. For example, if the CAN bus transmission rate is 2 Mbit / s and the timer's working cycle is 50 ms, then the amount of CAN data stored in the idle buffer at one time is 10,000 bytes, which means that it would take 10 timer cycles to store the data in the idle buffer. Therefore, the timer's working cycle can be increased to 100 ms, then the amount of CAN data stored in the idle buffer at one time is 20,000 bytes, and thus only 5 timer cycles are needed to store the data in the idle buffer to transmit the 100,000 bytes of CAN data.
[0068] In some embodiments, in response to a timeout interrupt of the first timer, the cached CAN data in the CAN bus is monitored. If CAN data still exists in the CAN bus, a second timer is started; if no CAN data exists in the CAN bus, the process ends and the second timer is not started. It is understood that, in response to a timeout interrupt of the first timer, if the external device is still sending CAN data to the controller via the CAN bus, the second timer is started; if the external device is not sending CAN data to the controller, the process stops and the second timer is not started.
[0069] Step S202: In response to starting the second timer, the CAN data sent by the external device via the CAN bus is stored in the buffer of the idle state, and the CAN data in the buffer of the occupied state is forwarded via the SPI bus.
[0070] In some embodiments, in response to the start of a second timer, it indicates that an external device continuously sends CAN data to the controller via the CAN bus, the controller needs to receive the CAN data, and needs to forward the received CAN data to other controllers via the SPI bus.
[0071] In some embodiments, after the second timer is started, the idle buffer is first identified and timing begins so that CAN data transmitted by the external device via the CAN bus is stored in the idle buffer during the working cycle of the second timer.
[0072] For example, after starting the second timer, the status flags of each buffer are obtained, the buffers with status flags of 0 are filtered out, that is, the buffers in the idle state, the timing is started, and the CAN data sent by the gateway through the CAN bus is stored in the buffer with status flags of 0.
[0073] In some embodiments, during the process of storing CAN data transmitted by an external device via the CAN bus in an idle buffer, or after starting a second timer, an occupied buffer is identified, and the stored CAN data in the occupied buffer is forwarded via the SPI bus.
[0074] For example, during the process of storing CAN data transmitted by external devices via the CAN bus in an idle buffer, or after starting the second timer, a buffer with a status flag of 1 (i.e., an occupied buffer) is identified, and the CAN data in the buffer with the status flag of 1 is forwarded to the system-on-a-chip via the SPI bus.
[0075] In this embodiment, upon starting the second timer, CAN data transmitted by the external device via the CAN bus is stored in the idle buffer, and CAN data stored in the occupied buffer of the at least two buffers is forwarded via the SPI bus. This way, when data is written to one buffer, data from the other buffer is forwarded, avoiding data loss due to a single buffer being full, thereby improving the accuracy of data transmission.
[0076] Figure 3 This is a schematic diagram illustrating the implementation flow of a data transmission method provided in an embodiment of this application. The at least two buffers include a first buffer and a second buffer. When both the first buffer and the second buffer are in an idle state, the method further includes the following steps S301 and S302, combining... Figure 3 The steps shown are explained.
[0077] Step S301: In response to the CAN data sent by the external device via the CAN bus, start the third timer and store the CAN data in the first buffer by default.
[0078] In some embodiments, when an external device sends CAN data to the controller via the CAN bus, the controller first checks the status of all buffers. If all buffers are idle, a third timer is started to begin timing, and the CAN data is stored in the default first buffer among at least two buffers.
[0079] For example, when an external device sends CAN data to the controller via the CAN bus, the status flag bits of each buffer are identified to filter out the buffers with status flag bits of 0. If the status flag bits of all buffers are 0, the first buffer is used as the default buffer, the third timer is started to start counting, and the CAN data is stored in the default first buffer.
[0080] In other embodiments, when an external device first sends CAN data to the controller via the CAN bus, the buffers set in the controller are all in an idle state, so the CAN data sent by the external device can be stored based on the default buffer.
[0081] Step S302: In response to the timeout interrupt of the third timer, the first buffer in the idle state is switched to the occupied state, and the CAN data in the first buffer in the occupied state is forwarded through the SPI bus.
[0082] In some embodiments, when the third timer reaches the set timing period (timeout), an interrupt request is sent to the controller (such as the MCU).
[0083] In some embodiments, after the first timer times out and is interrupted, that is, after the first buffer has stored CAN data for one working cycle of the third timer at the CAN bus transmission rate, the first buffer in the idle state is switched to the occupied state.
[0084] In some embodiments, after switching the first buffer from an idle state to an occupied state, the CAN data stored in the first buffer in the occupied state is forwarded via the SPI bus.
[0085] In this embodiment, when both the first buffer and the second buffer are idle, a third timer is started in response to CAN data sent by an external device via the CAN bus, and the CAN data is stored in the default first buffer. This ensures that the CAN data is stored immediately, thus avoiding frame loss. In response to a timeout interrupt of the third timer, the idle first buffer is switched to an occupied state, and the CAN data in the occupied first buffer is forwarded via the SPI bus. This improves data timeliness while ensuring data integrity.
[0086] In some embodiments, step S302 further includes the following:
[0087] When forwarding CAN data in the first buffer in the occupied state via the SPI bus, CAN data sent by external devices via the CAN bus is stored in the second buffer in the idle state.
[0088] In some embodiments, when forwarding CAN data in the first buffer in the occupied state via the SPI bus, a fourth timer is started to begin timing, and CAN data continuously transmitted by the external device via the CAN bus is stored in the second buffer in the idle state.
[0089] In other embodiments, in response to a timeout interrupt of the third timer, a fourth timer is started to begin timing, storing the CAN data continuously transmitted by the external device via the CAN bus in a second buffer in an idle state; and while the CAN data continuously transmitted via the CAN bus is stored in the second buffer in an idle state, the CAN data in the second buffer in an occupied state is forwarded via the SPI bus.
[0090] In some embodiments, in response to a timeout interrupt of a fourth timer, the second buffer in the idle state is switched to the occupied state, and the first buffer in the occupied state is switched to the idle state; if the external device does not send CAN data to the controller, the CAN data in the second buffer in the occupied state is forwarded via the SPI bus; if the external device continues to send CAN data to the controller, the CAN data is stored in the first buffer in a loop, and the CAN data in the second buffer is forwarded via the SPI bus.
[0091] In this embodiment, when forwarding CAN data in the occupied first buffer via the SPI bus, CAN data sent by external devices via the CAN bus is stored in the idle second buffer. This ensures that while forwarding CAN data in one buffer, data is stored in another, thus avoiding data frame loss. This improves data transmission integrity and efficiency.
[0092] Figure 4 This is a schematic diagram illustrating the implementation flow of a data transmission method provided in an embodiment of this application. Based on Figure 1 , Figure 1 Step S101 can be updated to steps S401 and S402, which will combine Figure 4 The steps shown are explained.
[0093] Step S401: Based on the transmission time of each CAN frame in the CAN data sent by the external device through the CAN controller LAN bus, add a timestamp to the initial identifier of each CAN frame to obtain the target identifier.
[0094] The CAN data includes at least two CAN frames with the same initial identifier.
[0095] In some embodiments, the CAN data includes multiple CAN frames, each of which includes an initial identifier and a corresponding value; wherein, the initial identifier indicates the source of the value (e.g., a receiving sensor, a temperature sensor, etc.), and if the same source (the same sensor) sends the same type of data at two times, then the initial identifiers of the two data are the same.
[0096] If this data is stored in the traditional key-value pair format, the value corresponding to the initial identifier of the latest time is usually replaced with the value corresponding to the initial identifier of the old time, resulting in the loss of the value corresponding to the initial identifier of the old time. For example, if the temperature sensor sends a value of 24 degrees at the first time, the CAN frame is "Temperature Sensor -24 Degrees". If it sends a value of 30 degrees at the second time, the CAN frame is "Temperature Sensor -30 Degrees". If these two CAN frames are stored in the key-value pair format, since one identifier corresponds to only one value in the key-value pair format, the two CAN frames will be merged, and the value of the second time (new time) will replace the value of the first time, which is "Temperature Sensor -30 Degrees", thus causing the CAN frame of the first time "-24 Degrees" to be lost.
[0097] To address the aforementioned technical issues, this application adds the transmission time of each CAN frame to the initial identifier. For example, if the initial identifier is "sensor", then the target identifier is "sensor-time".
[0098] In some embodiments, when an external device sends CAN data via the CAN bus, the controller records the actual transmission time of each CAN frame. For each CAN frame, the "transmission time" (timestamp) obtained in the record is appended to the "initial identifier" of that frame to form a new target identifier.
[0099] For example, the target identifier is composed of a combination of "initial identifier + timestamp" (e.g., by concatenating data structures or by adding a timestamp field to the frame header). If two CAN frames with an initial identifier of 0x123 are sent at times of 100ms and 150ms respectively, the target identifier can be changed to 0x123_100 and 0x123_150.
[0100] Step S402: Based on the target identifier, store the CAN data into the buffer of the idle state in chronological order.
[0101] In some embodiments, after receiving CAN data, the controller parses each CAN frame to obtain the timestamp in the target identifier of each CAN frame, compares the timestamps of each CAN frame to sort the CAN frames, and obtains the time sequence of each CAN frame in the CAN data.
[0102] In some embodiments, after obtaining the time series, the controller identifies at least two buffers in an idle state and stores each CAN frame in the CAN data into the idle buffer in a time series.
[0103] In this embodiment, a target identifier is obtained by adding a timestamp to the initial identifier of each CAN frame based on the transmission time of each CAN frame in the CAN data sent by the external device via the CAN controller area network bus. The CAN data includes at least two CAN frames with the same initial identifier. Based on the target identifier, the CAN data is stored in the idle buffer in chronological order. This avoids the problem of overwriting identical initial identifiers and causing frame loss, as is common in existing technologies, when storing CAN frames with the same initial identifier in the CAN data. Therefore, the integrity of data transmission is improved.
[0104] Figure 5 This is a schematic diagram illustrating the implementation flow of a data transmission method provided in an embodiment of this application. Based on Figure 1 , Figure 1 Step S102 can be updated to steps S501 and S502, combining... Figure 5 The steps shown are explained.
[0105] Step S501: Convert the CAN data to obtain converted CAN data; the converted CAN data satisfies the transmission conditions of the SPI bus.
[0106] In some embodiments, the raw format characteristics of CAN data transmitted on the CAN bus include: 1. Data structure: typically includes a frame start, arbitration field (including CANID), control field, data field (0-8 bytes), CRC field, acknowledgment field, and frame end. 2. Data length: the total length of a single CAN data frame is fixed (e.g., a standard frame is approximately 44 bits), but the maximum effective data payload is 8 bytes. 3. Encoding method: based on non-return-to-zero encoding, using differential signal transmission, and featuring error detection and automatic retransmission mechanisms.
[0107] In some embodiments, the data format characteristics transmitted on the SPI bus include: 1. Data format: Typically based on bytes (8 bits), supporting continuous multi-byte transmission, with no fixed frame structure (frame format needs to be defined by software). 2. Timing requirements: Data transmission is synchronized with the clock signal (SCLK), and clock polarity (CPOL) and phase (CPHA) need to be agreed upon. 3. Control signals: Slave devices are activated by the chip select signal (CS), with no built-in error detection mechanism (extra parity bits need to be added). 4. Rate matching: The SPI transmission rate must be matched with the processing capabilities of the MCU / SoC to avoid data overflow.
[0108] In some embodiments, based on the above, the conversion process includes: 1. If the length of the CAN data is less than the minimum unit for SPI bus transmission (e.g., a single byte), it needs to be padded with zeros or filled with specific identifiers; if the length is too long, it needs to be transmitted in frames and frame sequence numbers need to be added (e.g., "frame header + sequence number + data + frame tail"). 2. Since SPI has no built-in error detection, a check field (e.g., CRC check, checksum) needs to be added to the converted data for the receiving end to verify data integrity. 3. If there are level differences at the hardware level (e.g., CAN is a differential signal, SPI is a single-ended signal), it needs to be adapted through a level conversion circuit to ensure signal compatibility.
[0109] Step S502: Based on the time sequence, forward the converted CAN data via the SPI bus.
[0110] In some embodiments, the SPI bus is first initialized to ensure a communication link between the controller and the receiving device.
[0111] In some embodiments, after the SPI bus is initialized, based on the time sequence determined by the timestamp in the target identifier of each CAN frame, the SPI bus forwards each CAN frame in the converted CAN data to other devices in chronological order.
[0112] In this embodiment, the CAN data is format-converted to obtain converted CAN data; the converted CAN data satisfies the transmission conditions of the SPI bus; and the converted CAN data is forwarded via the SPI bus based on the chronological order. This ensures that the timing of the data processed by the receiver is consistent with the actual transmission timing of the original CAN bus, avoiding logical errors caused by disordered transmission order. Therefore, the accuracy of data transmission is improved.
[0113] The following describes an exemplary application of a data transmission method provided in this application in a real-world scenario.
[0114] Most automotive electronic components communicate with each other via CAN, such as Figure 6 As shown, Figure 6This illustration shows a CAN data interaction method provided in an embodiment of this application, including a GW (gateway) domain 601 and a CDC (cockpit) domain 602. The CDC domain 602 typically contains two main chips: an MCU 603 and a SOC 604, which mostly communicate via SPI. SPI is characterized by its high speed and low cost. Due to its high speed and low power consumption, the MCU 603 is generally used for CAN communication, sending received CAN data to the SOC 604 via the SPI bus. However, a common problem in this technology is that data loss frequently occurs when CAN data is transmitted to the SOC via SPI.
[0115] Figure 7 This is a schematic diagram of data transmission provided in an embodiment of this application, wherein 701 is a CAN bus, 702 is an SPI bus, and 703 is a unique buffer.
[0116] In some embodiments, such as Figure 7 As shown, there is only a single buffer 703, but after the single buffer 703 saves data, it can only continue to save CAN data after the SPI bus 701 finishes transmitting. This will cause data frame loss, such as... Figure 7 704 represents the SPI bus 701 transmission process, during which frame drops may occur on the SPI bus 701.
[0117] In some embodiments, identical CANID data is overwritten. For example, if there are two frames of CANID 0x288, the overwriting will cause the data of the previous CANID 0x288 frame to be lost.
[0118] In some embodiments, the size of the receive CAN data buffer is unreasonable. For example, most SPI settings with a 50ms cycle do not set it to 10,000 bytes; many use 1024 bytes or 2048 bytes to save memory. Then, when data frames are lost, useless CANIDs are discarded. This design has a significant flaw because as the project progresses, many previously useless CANIDs become useful again as requirements change, leading to more frame drops. Every change results in this problem.
[0119] To address the aforementioned problem, the frame loss phenomenon is primarily caused by the fact that CAN data is real-time, while SPI data is periodic. The difference in their communication methods easily leads to data frame loss. This application addresses this issue through the following improvements:
[0120] 1. Set the SPI speed requirements.
[0121] 2. Set up a backup buffer to back up data.
[0122] 3. Ensure data integrity accurately by configuring parameters such as cycle and interrupt.
[0123] 4. Management of Buffer data.
[0124] Figure 8 This diagram illustrates a data transmission method provided in an embodiment of this application. The CAN bus 701 speed is typically set to 2 Mbit / s, with approximately 10,000 bytes of data transmitted per 50ms cycle under full load. The MCU and SOC are configured to transmit CAN data for this period every 50ms. The CAN data is distributed in the blue area of the diagram above.
[0125] In some embodiments, the SPI bus 702 rate is set to 10 Mbit / s (significantly higher than the CAN bus 801 rate). Transmitting 10,000 bytes of data takes approximately 10 ms. Figure 8 In the diagram, 801 represents a single transmission period on the SPI bus. Transmission stops after this 801-minute period. Transmitting 10,000 bytes of data takes approximately 10ms. SPI transmits data every 50ms.
[0126] In some embodiments, two arrays are defined, including a first buffer 802 (CAN_Buf0) and a second buffer 803 (CAN_Buf1), each with a size of approximately 10,000 bytes, and the arrays are used to buffer CAN data.
[0127] In some embodiments, after startup, all CAN bus data is received using the default first buffer 802 and arranged sequentially from beginning to end. For example, as shown in Table 1.
[0128] Table 1
[0129] CAN_Buf0[x] CANID (always occupies two bytes) CANID data length CAN_Buf0[0-9] 0x288 8 CAN_Buf0[10-75] 0x289 64 CAN_Buf0[76-85] 0x290 8 CAN_Buf0[86-95] 0x288 8 CAN_Buf0[96-105] 0x291 8 … … …
[0130] Duplicate CANIDs are also included. For example, in the table above, there are two frames of 0x288. After the MCU sends this buffer data to the SOC, it is agreed that the older data comes first and the newer data comes last. After the SOC parses the two 0x288 CANID data, it performs data sorting based on the age of the data.
[0131] In some embodiments, the MCU sets a 50ms timer. When the 50ms timer expires, an interrupt is generated, and a new 50ms timer is set. After program startup, all CAN bus data is received using the default CAN_Buf0. Immediately after the interrupt occurs, newly received CAN data is stored in CAN_Buf1. CAN_Buf0 is then encapsulated and sent to the SOC via the SPI bus.
[0132] In some embodiments, when the next 50ms timer interrupt occurs, the cycle continues to restart the next 50ms timer. Immediately after the interrupt occurs, the newly received CAN data is stored in CAN_Buf0. CAN_Buf1 is then stored and sent to the SOC via the SPI bus. The subsequent program will continue to execute repeatedly.
[0133] In this embodiment, all data on the CAN bus is transferred from the MCU to the SPI without any data loss. The data rate is variable; for example, the CAN rate can be set to 1 Mbit / s, and the SPI rate can be set to 5 Mbit / s. The principle is that the SPI rate is significantly higher than the CAN rate, and the CAN_Buf size must be sufficient to receive all data within a given cycle. All CANID data can be sent to the SOC, including duplicate CANIDs; sorting according to the old and new rules ensures no data loss.
[0134] Based on the foregoing embodiments, this application provides a data transmission device, which includes the included units and the modules included in each unit, which can be implemented by a processor in a computer device; of course, it can also be implemented by specific logic circuits; in the implementation process, the processor can be a central processing unit (CPU), a microprocessor unit (MPU), a digital signal processor (DSP), or a field programmable gate array (FPGA), etc.
[0135] Figure 9 This is a schematic diagram of the composition structure of a data transmission device provided in an embodiment of this application, as shown below. Figure 9 As shown, the data transmission device 900 includes: a storage module 901 and a forwarding module 902, wherein:
[0136] Storage module 901 is used to store CAN data sent by an external device via the CAN controller LAN bus into an idle buffer in the at least two buffers in response to the start of a first timer;
[0137] The forwarding module 902 is used to forward the CAN data stored in the occupied buffer of at least two buffers via the serial peripheral interface SPI bus during the process of storing the CAN data in the idle buffer; wherein the data transmission rate of the CAN bus is less than the data transmission rate of the SPI bus.
[0138] In some embodiments, the data transmission device 900 further includes a switching module (not shown in the figure), the switching module being configured to switch the idle buffer to an occupied state in response to a timeout interrupt of the first timer; and to switch the occupied buffer to an idle state.
[0139] In some embodiments, the forwarding module 902 is further configured to start a second timer in response to a timeout interrupt of the first timer; and in response to starting the second timer, to store the CAN data sent by the external device via the CAN bus in the idle state buffer, and to instruct the forwarding of the CAN data in the occupied state buffer via the SPI bus.
[0140] In some embodiments, the at least two buffers include a first buffer and a second buffer. When both the first buffer and the second buffer are in an idle state, the forwarding module 902 is further configured to, in response to CAN data sent by the external device via the CAN bus, start a third timer and store the CAN data in the first buffer by default; in response to a timeout interrupt of the third timer, switch the idle first buffer to an occupied state and forward the CAN data in the occupied first buffer via the SPI bus.
[0141] In some embodiments, the storage module 901 is further configured to store CAN data sent by an external device via the CAN bus in the second buffer in the idle state when forwarding CAN data in the first buffer in the occupied state via the SPI bus.
[0142] In some embodiments, the storage module 901 is further configured to add a timestamp to the initial identifier of each CAN frame based on the transmission time of each CAN frame in the CAN data sent by the external device via the CAN controller local area network bus, to obtain a target identifier; wherein the CAN data includes at least two CAN frames with the same initial identifier; and based on the target identifier, the CAN data is stored in the buffer of the idle state in chronological order.
[0143] In some embodiments, the forwarding module 902 is further configured to convert the CAN data into a format to obtain converted CAN data; the converted CAN data satisfies the transmission conditions of the SPI bus; and the converted CAN data is forwarded through the SPI bus based on the time sequence.
[0144] The descriptions of the apparatus embodiments above are similar to those of the method embodiments above, and have similar beneficial effects. In some embodiments, the functions or modules included in the apparatus provided in this application can be used to perform the methods described in the method embodiments above. For technical details not disclosed in the apparatus embodiments of this application, please refer to the descriptions of the method embodiments of this application for understanding.
[0145] It should be noted that, in the embodiments of this application, if the above-described methods are implemented as software functional modules and sold or used as independent products, they can also be stored in a computer-readable storage medium. Based on this understanding, the technical solutions of the embodiments of this application, or the parts that contribute to related technologies, can be embodied in the form of a software product. This software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), magnetic disks, or optical disks. Thus, the embodiments of this application are not limited to any specific hardware, software, or firmware, or any combination of hardware, software, and firmware.
[0146] This application provides a computer device including a memory and a processor. The memory stores a computer program that can run on the processor. When the processor executes the program, it implements some or all of the steps in the above-described method.
[0147] This application provides a computer-readable storage medium storing a computer program thereon, which, when executed by a processor, implements some or all of the steps in the above-described method. The computer-readable storage medium can be transient or non-transient.
[0148] This application provides a computer program including computer-readable code, wherein when the computer-readable code is executed in a computer device, a processor in the computer device performs some or all of the steps in the above-described method.
[0149] This application provides a computer program product, which includes a non-transitory computer-readable storage medium storing a computer program. When the computer program is read and executed by a computer, it implements some or all of the steps in the above-described method. This computer program product can be implemented specifically through hardware, software, or a combination thereof. In some embodiments, the computer program product is specifically embodied as a computer storage medium; in other embodiments, the computer program product is specifically embodied as a software product, such as a software development kit (SDK), etc.
[0150] It should be noted that the descriptions of the various embodiments above tend to emphasize the differences between them, while their similarities or commonalities can be referred to interchangeably. The descriptions of the above embodiments of the device, storage medium, computer program, and computer program product are similar to the descriptions of the above method embodiments and have similar beneficial effects. For technical details not disclosed in the embodiments of the device, storage medium, computer program, and computer program product of this application, please refer to the descriptions of the method embodiments of this application for understanding.
[0151] Figure 10 This application provides a hardware entity diagram of a computer device as an embodiment of the present application, such as... Figure 10 As shown, the hardware entity of the computer device 1000 includes a processor 1001 and a memory 1002, wherein the memory 1002 stores a computer program that can run on the processor 1001, and the processor 1001 executes the program to implement the steps in the method of any of the above embodiments.
[0152] The memory 1002 stores computer programs that can run on the processor. The memory 1002 is configured to store instructions and applications that can be executed by the processor 1001. It can also cache data to be processed or already processed (e.g., image data, audio data, voice communication data, and video communication data) in the processor 1001 and various modules in the computer device 1000. It can be implemented by flash memory or random access memory (RAM).
[0153] The processor 1001 executes the steps of any of the above methods when executing a program. The processor 1001 typically controls the overall operation of the computer device 1000.
[0154] This application provides a computer storage medium that stores one or more programs, which can be executed by one or more processors to implement the steps of the methods described in any of the above embodiments.
[0155] It should be noted that the descriptions of the storage medium and device embodiments above are similar to the descriptions of the method embodiments above, and have similar beneficial effects. For technical details not disclosed in the storage medium and device embodiments of this application, please refer to the descriptions of the method embodiments of this application for understanding.
[0156] The aforementioned processor can be at least one of the following: Application Specific Integrated Circuit (ASIC), Digital Signal Processor (DSP), Digital Signal Processing Device (DSPD), Programmable Logic Device (PLD), Field Programmable Gate Array (FPGA), Central Processing Unit (CPU), Controller, Microcontroller, and Microprocessor. It is understood that other electronic devices can also implement the functions of the aforementioned processor, and this application does not specifically limit the specific implementation.
[0157] The aforementioned computer storage media / memory can be read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), magnetic random access memory (FRAM), flash memory, magnetic surface memory, optical disc, or compact disc read-only memory (CD-ROM), etc.; or it can be various terminals that include one or any combination of the above-mentioned memories, such as mobile phones, computers, tablet devices, personal digital assistants, etc.
[0158] It should be understood that the phrase "an embodiment" or "one embodiment" throughout the specification means that a specific feature, structure, or characteristic related to the embodiment is included in at least one embodiment of this application. Therefore, "in one embodiment" or "one embodiment" appearing throughout the specification does not necessarily refer to the same embodiment. Furthermore, these specific features, structures, or characteristics can be combined in any suitable manner in one or more embodiments. It should be understood that in the various embodiments of this application, the sequence numbers of the above steps / processes do not imply a sequential order of execution; the execution order of each step / process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of this application. The sequence numbers of the above embodiments of this application are merely descriptive and do not represent the superiority or inferiority of the embodiments.
[0159] It should be noted that, in this document, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Unless otherwise specified, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes that element.
[0160] In the several embodiments provided in this application, it should be understood that the disclosed devices and methods can be implemented in other ways. The device embodiments described above are merely illustrative. For example, the division of units is only a logical functional division, and in actual implementation, there may be other division methods, such as: multiple units or components can be combined, or integrated into another system, or some features can be ignored or not executed. In addition, the coupling, direct coupling, or communication connection between the various components shown or discussed can be through some interfaces, and the indirect coupling or communication connection between devices or units can be electrical, mechanical, or other forms.
[0161] The units described above as separate components may or may not be physically separate. The components shown as units may or may not be physical units. They may be located in one place or distributed across multiple network units. Some or all of the units may be selected to achieve the purpose of this embodiment according to actual needs.
[0162] Furthermore, in the various embodiments of this application, all functional units can be integrated into one processing unit, or each unit can be a separate unit, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or in a combination of hardware and software functional units. Those skilled in the art will understand that all or part of the steps of the above method embodiments can be implemented by hardware related to program instructions. The aforementioned program can be stored in a computer-readable storage medium. When the program is executed, it performs the steps of the above method embodiments. The aforementioned storage medium includes various media capable of storing program code, such as mobile storage devices, read-only memory (ROM), magnetic disks, or optical disks.
[0163] Alternatively, if the integrated units described above are implemented as software functional modules and sold or used as independent products, they can also be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence or the part that contributes to related technologies, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as mobile storage devices, ROM, magnetic disks, or optical disks.
[0164] The above description is merely an embodiment of this application, but the scope of protection of this application is not limited thereto. Any changes or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application.
Claims
1. A data transmission method, characterized in that, Applied to a controller having at least two buffers, the method includes: In response to the start of the first timer, the CAN data sent by the external device via the CAN controller LAN bus is stored in the idle buffer of the at least two buffers; During the process of storing the CAN data through the idle buffer, the CAN data stored in the occupied buffer of the at least two buffers is forwarded through the serial peripheral interface SPI bus; The data transmission rate of the CAN bus is lower than that of the SPI bus.
2. The method according to claim 1, characterized in that, The method further includes: In response to a timeout interrupt of the first timer, the idle buffer is switched to an occupied state; and the occupied buffer is switched to an idle state.
3. The method according to claim 2, characterized in that, The method further includes: In response to the timeout interrupt of the first timer, the second timer is started; In response to starting a second timer, the CAN data transmitted by the external device via the CAN bus is stored in the idle state buffer, and the CAN data in the occupied state buffer is forwarded via the SPI bus.
4. The method according to any one of claims 1 to 3, characterized in that, The at least two buffers include a first buffer and a second buffer. When both the first buffer and the second buffer are in an idle state, the method further includes: In response to CAN data sent by the external device via the CAN bus, a third timer is started, and the CAN data is stored in the first buffer by default. In response to the timeout interrupt of the third timer, the first buffer in the idle state is switched to the occupied state, and the CAN data in the first buffer in the occupied state is forwarded via the SPI bus.
5. The method according to claim 4, characterized in that, The method further includes: When forwarding CAN data in the first buffer in the occupied state via the SPI bus, CAN data sent by external devices via the CAN bus is stored in the second buffer in the idle state.
6. The method according to any one of claims 1 to 3, characterized in that, The step of storing CAN data transmitted by external devices via the CAN controller area network bus into an idle buffer among the at least two buffers includes: Based on the transmission time of each CAN frame in the CAN data sent by the external device via the CAN controller LAN bus, a timestamp is added to the initial identifier of each CAN frame to obtain the target identifier; wherein, the CAN data includes at least two CAN frames with the same initial identifier; Based on the target identifier, the CAN data is stored in the idle state buffer in chronological order.
7. The method according to claim 6, characterized in that, The forwarding of CAN data stored in the at least two occupied buffers via the Serial Peripheral Interface (SPI) bus includes: The CAN data is converted to a new format to obtain converted CAN data; the converted CAN data satisfies the transmission conditions of the SPI bus. The converted CAN data is forwarded via the SPI bus based on the stated time sequence.
8. A data transmission device, characterized in that, Applied to a controller, wherein the controller is provided with at least two buffers, the device includes: The storage module is used to store CAN data sent by the external device via the CAN controller LAN bus into the idle buffer of the at least two buffers in response to the start of the first timer; The forwarding module is used to forward the CAN data stored in the occupied buffers of at least two buffers via the serial peripheral interface SPI bus during the process of storing the CAN data in the idle buffer. The data transmission rate of the CAN bus is lower than that of the SPI bus.
9. A computer device comprising a memory and a processor, the memory storing a computer program executable on the processor, characterized in that, When the processor executes the program, it implements the steps of the method according to any one of claims 1 to 7.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 7.
Citation Information
Cited By
Interaction control method and device based on SPI simulation CAN communication and medium
CN122339888A
An interactive control method and device based on SPI analog CAN communication and a medium
CN122339888B