A method, device, equipment and storage medium for implementing SPI communication
By using OS event processing protocol data in SPI communication, the problem of excessive execution time of interrupt service functions is solved, ensuring the real-time and stability of the operating system.
Patent Information
- Application Number
- CN202411925751.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-25
- Publication Date
- 2025-04-04
AI Technical Summary
In SPI communication, when data processing is time-consuming, the system's interrupt service function execution time is too long, resulting in poor real-time performance of the operating system and affecting the robustness and stability of the operating system.
By detecting whether there is a DMA interrupt after MCAL sends protocol data, if it exists, the protocol data processing is implemented based on the OS event, and wait for MCAL to send the next packet of protocol data; if it does not exist, wait for MCAL to send the next packet of protocol data and then execute the DMA interrupt judgment again, and loop this process.
It effectively avoids the problem of interrupting service functions execution time for too long, ensures the real-time requirements of the operating system, and ensures the robustness and stability of the operating system.
Smart Images

Figure CN119759830A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of data communication processing, and in particular to a method, device, equipment and storage medium for implementing SPI communication. Background Art
[0002] Currently, the Davinci tool (an automotive open system architecture Autosar configuration tool) is generally used to configure MSPI (Multi-Serial Peripheral Interface, a high-performance serial communication interface) and configure user callbacks. At the same time, the Davinci tool is used to configure MSPI-related interrupts in Autosar OS (a real-time operating system designed specifically for automobiles).
[0003] In actual applications, the MCAL (MicroController Abstraction Layer) related calls sent by the SPI (Serial Peripheral Interface) of the slave device are ready after the master device sends the data, and the data processing of the slave device is completely performed in the calback provided by MCAL. When DMA (Direct Memory Access) is configured as an interrupt, the data processing of SPI is completed in the interrupt service. When data processing is more time-consuming, the interrupt service function of the system will also increase the time consumption accordingly, which is contrary to the original design of not performing time-consuming processing in the interrupt service function, resulting in poor real-time performance of the operating system and affecting the robustness and stability of the operating system. Summary of the invention
[0004] The present application provides an SPI communication implementation method, apparatus, device and storage medium, which can effectively avoid the problem of long execution time of interrupt service functions, guarantee the real-time requirements of the operating system, and ensure the robustness and stability of the operating system.
[0005] In a first aspect, an embodiment of the present application provides a method for implementing SPI communication, the method for implementing SPI communication comprising:
[0006] After detecting the MCAL sending protocol data, execute the DMA interrupt judgment:
[0007] If yes, the protocol data is processed based on the created OS event, and the MCAL is waiting to send the next packet of protocol data.
[0008] If not, after detecting that MCAL sends the next packet of protocol data, it is again determined whether there is a DMA interrupt.
[0009] In combination with the first aspect, in one implementation, the processing of protocol data based on the created OS event specifically includes:
[0010] MCAL performs interrupt reception and transmission processing and executes the reserved callback function;
[0011] An OS event is created and triggered in MCAL based on the callback function, and the protocol data is processed through the task function of the OS event.
[0012] In combination with the first aspect, in one implementation, the task function of the OS event implements the processing of the protocol data, wherein the processing of the protocol data specifically includes:
[0013] Perform cyclic redundancy check on protocol data, and parse and distribute protocol data according to the set protocol.
[0014] In combination with the first aspect, in one implementation, the waiting for MCAL to send the next packet of protocol data specifically includes:
[0015] The task function of the OS event calls the MCAL sending function to prepare for sending the next packet of protocol data.
[0016] In combination with the first aspect, in one implementation, the preparing to send the next packet of protocol data, wherein the preparing for sending the protocol data specifically includes:
[0017] The protocol data to be sent is spliced according to the set protocol, and a cyclic redundancy check is performed on the protocol data to be sent.
[0018] In combination with the first aspect, in one implementation, after the to-be-detected MCAL sends the next packet of protocol data, the determination of whether a DMA interrupt exists is performed again, specifically including:
[0019] After detecting that MCAL sends the next packet of protocol data, it is again determined whether there is a DMA interrupt. If so, the protocol data is processed based on the created OS event, and the process continues to wait for MCAL to send the next packet of protocol data. If not, it continues to wait for MCAL to send the next packet of protocol data, and the cycle continues.
[0020] In combination with the first aspect, in one implementation, before the MCAL sending protocol data is first detected, the method further includes:
[0021] Initialize SPI and DMA, and start Autosar OS;
[0022] Call the sending function of MCAL to send protocol data.
[0023] In a second aspect, an embodiment of the present application provides an SPI communication implementation device, the SPI communication implementation device comprising:
[0024] A judgment module, which is used to detect whether there is a DMA interrupt after obtaining the MCAL sending protocol data, and if so, drive the first execution module to work, and if not, drive the second execution module to work;
[0025] A first execution module, which is used to process the protocol data based on the created OS event and wait for MCAL to send the next packet of protocol data;
[0026] The second execution module is used to execute again whether there is a DMA interruption judgment after the next packet of protocol data sent by MCAL is detected.
[0027] In a third aspect, an embodiment of the present application provides an SPI communication implementation device, which includes a processor, a memory, and an SPI communication implementation program stored in the memory and executable by the processor, wherein when the SPI communication implementation program is executed by the processor, the steps of the above-mentioned SPI communication implementation method are implemented.
[0028] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, on which an SPI communication implementation program is stored, wherein when the SPI communication implementation program is executed by a processor, the steps of the above-mentioned SPI communication implementation method are implemented.
[0029] The beneficial effects brought by the technical solution provided by the embodiments of the present application include:
[0030] By introducing OS events, when MCAL determines that a DMA interrupt exists after sending protocol data, the protocol data is processed based on the OS events, that is, an OS event is triggered in the MCAL service function, and the processing of the protocol data is moved to the task function of the OS event, which effectively avoids the problem of long execution time of the interrupt service function, guarantees the real-time requirements of the operating system, and ensures the robustness and stability of the operating system. BRIEF DESCRIPTION OF THE DRAWINGS
[0031] Figure 1 This is a flow chart of the SPI communication implementation method of this application;
[0032] Figure 2 This is a detailed execution flow diagram of the SPI communication implementation method of this application;
[0033] Figure 3 This is a functional module diagram of the SPI communication implementation device of this application;
[0034] Figure 4 This is a schematic diagram of the hardware structure of the SPI communication implementation device for this application. DETAILED DESCRIPTION
[0035] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0036] In order to make the objectives, technical solutions and advantages of the present application clearer, the implementation methods of the present application will be further described in detail below with reference to the accompanying drawings.
[0037] In a first aspect, an embodiment of the present application provides a method for implementing SPI communication, which implements SPI communication based on Autosar OS and MCAL, and can effectively avoid the problem of long execution time of interrupt service function. The MCAL model in the present application can be RH850.
[0038] In one embodiment, referring to Figure 1 , Figure 1 This is a flow chart of the SPI communication implementation method of this application.
[0039] like Figure 1 As shown, the SPI communication implementation method includes:
[0040] S1: After detecting the MCAL sending protocol data, it is determined whether there is a DMA interrupt. If yes, it goes to step S2; if no, it goes to step S3;
[0041] S2: Process the protocol data based on the created OS event and wait for MCAL to send the next packet of protocol data;
[0042] S3: After the next packet of protocol data is detected to be sent by MCAL, it is again determined whether there is a DMA interrupt.
[0043] Specifically, after MCAL sends the protocol data, it determines whether there is a DMA interrupt. When there is a DMA interrupt, the protocol data sent by MCAL is processed through the created OS event. When there is no DMA interrupt, it waits for MCAL to send the next packet of protocol data, and after MCAL sends the next packet of protocol data, it determines whether there is a DMA interrupt again, and repeats this cycle.
[0044] Further, in one embodiment, the processing of protocol data is implemented based on the created OS event, specifically including:
[0045] S101: MCAL performs interrupt reception and transmission processing and executes the reserved callback function;
[0046] S102: Creating and triggering an OS event in MCAL based on the callback function, and processing the protocol data through the task function of the OS event.
[0047] Specifically, after MCAL sends the protocol data, it determines whether there is a DMA interrupt. If there is a DMA interrupt, MCAL first performs interrupt transmission and reception processing. After the interrupt transmission and reception processing is completed, the callback function callback reserved for the user is executed, and the OS event is set through the callback function callback. After that, the task function task of the OS event implements the processing of the protocol data sent by MCAL.
[0048] It should be noted that in the present application, the processing of protocol data is realized by the task function of the OS event, wherein the processing of protocol data specifically includes: performing a cyclic redundancy check on the protocol data, and parsing and distributing the protocol data according to the set protocol. That is, when the task function task of the OS event processes the protocol data, the specific processing process includes but is not limited to: performing a cyclic redundancy check (i.e., CRC check) on the protocol data, parsing the protocol data according to the pre-set parsing-related protocol, and then distributing it.
[0049] Further, in one embodiment, waiting for MCAL to send the next packet of protocol data specifically includes: calling the sending function of MCAL through the task function of the OS event to prepare for sending the next packet of protocol data. That is, after the task function task of the OS event completes processing the protocol data, the task function task of the OS event calls the sending function of MCAL to prepare the next packet of protocol data and wait for sending the next packet of protocol data.
[0050] It should be noted that in the present application, the preparation for sending the next packet of protocol data includes: splicing the protocol data to be sent according to the set protocol, and performing a cyclic redundancy check on the protocol data to be sent. That is, when the task function task of the OS event calls the sending function of MCAL to prepare for sending the next packet of protocol data, the specific preparation process includes but is not limited to: splicing the protocol data to be sent according to the pre-set splicing-related protocol, and performing a cyclic redundancy check on the protocol data to be sent. After the preparation is completed, the protocol data is formed, so that subsequent sending is carried out.
[0051] Further, in one embodiment, after the next packet of protocol data is detected to be sent by MCAL, whether there is a DMA interruption is determined again, specifically including:
[0052] After detecting that MCAL sends the next packet of protocol data, it is again determined whether there is a DMA interrupt. If so, the protocol data is processed based on the created OS event, and the process continues to wait for MCAL to send the next packet of protocol data. If not, it continues to wait for MCAL to send the next packet of protocol data, and the cycle continues.
[0053] Furthermore, in one embodiment, before the MCAL sending protocol data is first detected, the method further includes:
[0054] a: Initialize SPI and DMA, and start Autosar OS;
[0055] b: Call MCAL's sending function to send protocol data.
[0056] Specifically, SPI and DMA are initialized, and then Autosar OS is started, and then the sending function of MCAL is called to send protocol data. After detecting that MCAL sends protocol data, it is determined whether there is a DMA interrupt.
[0057] The following combination Figure 2 , the specific execution process of the SPI communication implementation method of the present application is explained.
[0058] S11: Initialize SPI and DMA, go to S12;
[0059] S12: Start Autosar OS and go to S13;
[0060] S13: Call the sending function of MCAL to send the protocol data, and go to S14;
[0061] S14: Determine whether there is a DMA interruption. If so, go to S15. If not, go to S14 to determine whether there is a DMA interruption again after the next packet of protocol data is sent.
[0062] S15: MCAL performs interrupt reception and transmission processing, executes the reserved callback function, and then goes to S16;
[0063] S16: Setting an OS event based on the callback function, and going to S17;
[0064] S17: Process the protocol data through the task function of the OS event, and go to S18;
[0065] S18: Call the sending function of MCAL through the task function of the OS event to prepare for sending the next packet of protocol data, and after the next packet of protocol data is sent, go to S14.
[0066] The SPI communication implementation method of the embodiment of the present application introduces OS events, so that when MCAL sends protocol data and determines that a DMA interrupt exists, the protocol data is processed based on the OS event, that is, an OS event is triggered in the MCAL service function, and the processing of the protocol data is moved to the task function of the OS event, which effectively avoids the problem of long execution time of the interrupt service function, guarantees the real-time requirements of the operating system, and ensures the robustness and stability of the operating system.
[0067] In a second aspect, an embodiment of the present application also provides an SPI communication implementation device.
[0068] In one embodiment, referring to Figure 3 , Figure 3 This is a functional module diagram of the SPI communication implementation device of this application. Figure 3 As shown, the SPI communication implementation device includes: a judgment module, a first execution module, and a second execution module.
[0069] The judgment module is used to detect whether there is a DMA interrupt after MCAL sends the protocol data. If so, the first execution module is driven to work, and if not, the second execution module is driven to work; the first execution module is used to implement the processing of protocol data based on the created OS event, and wait for MCAL to send the next packet of protocol data; the second execution module is used to detect whether there is a DMA interrupt again after MCAL sends the next packet of protocol data.
[0070] Specifically, after MCAL sends the protocol data, it determines whether there is a DMA interrupt. When there is a DMA interrupt, the protocol data sent by MCAL is processed through the created OS event. When there is no DMA interrupt, it waits for MCAL to send the next packet of protocol data, and after MCAL sends the next packet of protocol data, it determines whether there is a DMA interrupt again, and repeats this cycle.
[0071] In the present application, the first execution module specifically includes a first working module and a second working module. The first working module is used to drive MCAL to perform interrupt reception and transmission processing when there is a DMA interrupt, and execute a reserved callback function, and then create and trigger an OS event in MCAL based on the callback function, and implement the processing of protocol data through the task function of the OS event. The second working module is used to call the sending function of MCAL through the task function of the OS event to prepare for the sending of the next packet of protocol data.
[0072] It should be noted that when the first working module processes the protocol data, the specific processing process includes: performing a cyclic redundancy check on the protocol data, and parsing and distributing the protocol data according to the set protocol. That is, when the task function task of the OS event processes the protocol data, the specific processing process includes but is not limited to: performing a cyclic redundancy check (i.e., CRC check) on the protocol data, parsing the protocol data according to the pre-set parsing-related protocol, and then distributing it.
[0073] When the second working module is preparing to send the protocol data, the specific preparation process includes: splicing the protocol data to be sent according to the set protocol, and performing a cyclic redundancy check on the protocol data to be sent. That is, when the task function task of the OS event calls the sending function of MCAL and prepares to send the next packet of protocol data, the specific preparation process includes but is not limited to: splicing the protocol data to be sent according to the pre-set splicing-related protocol, and performing a cyclic redundancy check on the protocol data to be sent. After the preparation is completed, the protocol data is formed, so that subsequent sending is carried out.
[0074] In the present application, the second execution module is specifically used for: after detecting that MCAL sends the next packet of protocol data, it again executes the judgment whether there is a DMA interruption. If so, it implements the processing of the protocol data based on the created OS event, and continues to wait for MCAL to send the next packet of protocol data. If not, it continues to wait for MCAL to send the next packet of protocol data, and repeats this cycle.
[0075] Furthermore, the SPI communication implementation device of the embodiment of the present application also includes an initialization module, which is used to initialize SPI and DMA and start Autosar OS before detecting and obtaining MCAL sending protocol data for the first time, and then call the sending function of MCAL to send protocol data.
[0076] The SPI communication implementation device of the embodiment of the present application introduces OS events, so that when MCAL sends protocol data and determines that a DMA interrupt exists, the protocol data is processed based on the OS event, that is, an OS event is triggered in the MCAL service function, and the processing of the protocol data is moved to the task function of the OS event, which effectively avoids the problem of the interrupt service function executing for too long, guarantees the real-time requirements of the operating system, and ensures the robustness and stability of the operating system.
[0077] In a third aspect, an embodiment of the present application provides an SPI communication implementation device, which may be a personal computer (PC), a laptop computer, a server, or other device with data processing capabilities.
[0078] Reference Figure 4 , Figure 4 The hardware structure diagram of the SPI communication implementation device involved in the embodiment of the present application is shown in FIG. In the embodiment of the present application, the SPI communication implementation device may include a processor, a memory, a communication interface, and a communication bus.
[0079] The communication bus may be of any type and is used to interconnect the processor, the memory, and the communication interface.
[0080] The communication interface includes input / output (I / O) interface, physical interface and logical interface, etc., which are used to realize the interconnection of devices inside the SPI communication device, and the interface used to realize the interconnection of the SPI communication device with other devices (such as other computing devices or user devices). The physical interface can be an Ethernet interface, a fiber optic interface, an ATM interface, etc.; the user device can be a display, a keyboard, etc.
[0081] The memory can be various types of storage media, such as random access memory (RAM), read-only memory (ROM), non-volatile RAM (NVRAM), flash memory, optical storage, hard disk, programmable ROM (PROM), erasable PROM (EPROM), electrically erasable PROM (EEPROM), etc.
[0082] The processor may be a general-purpose processor, and the general-purpose processor may call the SPI communication implementation program stored in the memory and execute the SPI communication implementation method provided in the embodiment of the present application. For example, the general-purpose processor may be a central processing unit (CPU). The method executed when the SPI communication implementation program is called may refer to the various embodiments of the SPI communication implementation method of the present application, and will not be repeated here.
[0083] Those skilled in the art will understand that Figure 4 The hardware structure shown in the figure does not constitute a limitation on the present application, and may include more or fewer components than shown in the figure, or combine certain components, or arrange the components differently.
[0084] In a fourth aspect, an embodiment of the present application also provides a computer-readable storage medium.
[0085] The computer-readable storage medium of the present application stores an SPI communication implementation program, wherein when the SPI communication implementation program is executed by a processor, the steps of the SPI communication implementation method described above are implemented.
[0086] Among them, the method implemented when the SPI communication implementation program is executed can refer to the various embodiments of the SPI communication implementation method of the present application, and will not be repeated here.
[0087] The terms "including" and "having" and any variations thereof in the specification and claims of this application and the above-mentioned drawings are intended to cover non-exclusive inclusions. For example, a process, method, system, product or device that includes a series of steps or units is not limited to the listed steps or units, but optionally includes steps or units that are not listed, or optionally includes other steps or units inherent to these processes, methods, products or devices. The terms "first", "second" and "third" are used to distinguish different objects, etc., and do not represent a sequence, nor do they limit "first", "second" and "third" to different types.
[0088] In the description of the embodiments of the present application, "exemplary", "for example" or "for example" are used to indicate examples, illustrations or descriptions. Any embodiment or design described as "exemplary", "for example" or "for example" in the embodiments of the present application should not be interpreted as being more preferred or more advantageous than other embodiments or designs. Specifically, the use of words such as "exemplary", "for example" or "for example" is intended to present related concepts in a specific way.
[0089] In the description of the embodiments of the present application, unless otherwise specified, “ / ” means or, for example, A / B can mean A or B; the “and / or” in the text is merely a description of the association relationship of associated objects, indicating that three relationships may exist, for example, A and / or B can mean: A exists alone, A and B exist at the same time, and B exists alone. In addition, in the description of the embodiments of the present application, “multiple” refers to two or more than two.
[0090] In some processes described in the embodiments of the present application, multiple operations or steps that appear in a specific order are included, but it should be understood that these operations or steps may not be executed in the order in which they appear in the embodiments of the present application or in parallel, and the sequence number of the operation is only used to distinguish the different operations, and the sequence number itself does not represent any execution order. In addition, these processes may include more or fewer operations, and these operations or steps may be executed in sequence or in parallel, and these operations or steps may be combined.
[0091] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present application is essentially or the part that contributes to the prior art can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, disk, CD) as described above, and includes a number of instructions for a terminal device to execute the methods described in each embodiment of the present application.
[0092] The above are only preferred embodiments of the present application, and are not intended to limit the patent scope of the present application. Any equivalent structure or equivalent process transformation made using the contents of the present application specification and drawings, or directly or indirectly applied in other related technical fields, are also included in the patent protection scope of the present application.
Claims
1. A method for implementing SPI communication, characterized in that: The SPI communication implementation method comprises: After detecting the MCAL sending protocol data, execute the DMA interrupt judgment: If yes, the protocol data is processed based on the created OS event, and the MCAL is waiting to send the next packet of protocol data. If not, after detecting that MCAL sends the next packet of protocol data, it is again determined whether there is a DMA interrupt.
2. A method for implementing SPI communication as claimed in claim 1, characterized in that: The processing of protocol data based on the created OS event specifically includes: MCAL performs interrupt reception and transmission processing and executes the reserved callback function; An OS event is created and triggered in MCAL based on the callback function, and the protocol data is processed through the task function of the OS event.
3. A method for implementing SPI communication as claimed in claim 2, characterized in that: The processing of the protocol data is realized by the task function of the OS event, wherein the processing of the protocol data specifically includes: Perform cyclic redundancy check on protocol data, and parse and distribute protocol data according to the set protocol.
4. A method for implementing SPI communication as claimed in claim 2, characterized in that: The step of waiting for MCAL to send the next packet of protocol data specifically includes: The task function of the OS event calls the MCAL sending function to prepare for sending the next packet of protocol data.
5. A method for implementing SPI communication as claimed in claim 4, characterized in that: The preparation for sending the next packet of protocol data, wherein the preparation for sending the protocol data specifically includes: The protocol data to be sent is spliced according to the set protocol, and a cyclic redundancy check is performed on the protocol data to be sent.
6. A method for implementing SPI communication as claimed in claim 1, characterized in that: After the next packet of protocol data is sent by MCAL, the determination of whether there is a DMA interruption is performed again, specifically including: After detecting that MCAL sends the next packet of protocol data, it is again determined whether there is a DMA interrupt. If so, the protocol data is processed based on the created OS event, and the process continues to wait for MCAL to send the next packet of protocol data. If not, it continues to wait for MCAL to send the next packet of protocol data, and the cycle continues.
7. A method for implementing SPI communication as claimed in claim 1, characterized in that: Before the first detection of MCAL sending protocol data, it also includes: Initialize SPI and DMA, and start Autosar OS; Call the sending function of MCAL to send protocol data.
8. An SPI communication implementation device, characterized in that: The SPI communication implementation device comprises: A judgment module, which is used to detect whether there is a DMA interrupt after obtaining the MCAL sending protocol data, and if so, drive the first execution module to work, and if not, drive the second execution module to work; A first execution module, which is used to process the protocol data based on the created OS event and wait for MCAL to send the next packet of protocol data; The second execution module is used to execute again whether there is a DMA interruption judgment after the next packet of protocol data sent by MCAL is detected.
9. A SPI communication implementation device, characterized in that: The SPI communication implementation device includes a processor, a memory, and a SPI communication implementation program stored in the memory and executable by the processor, wherein when the SPI communication implementation program is executed by the processor, the steps of the SPI communication implementation method according to any one of claims 1 to 7 are implemented.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores an SPI communication implementation program, wherein when the SPI communication implementation program is executed by a processor, the steps of the SPI communication implementation method according to any one of claims 1 to 7 are implemented.