Equipment and method for converting SPI to UART communication

Through the equipment and methods for realizing SPI to UART communication between CPU and CPLD, the problems of high cost and poor flexibility of SPI to UART communication in the prior art are solved, and the effect of flexible configuration of data transmission rates and reducing operation and maintenance costs are achieved.

CN119883966APending Publication Date: 2025-04-25RAISECOM TECH +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411984790.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-31
Publication Date
2025-04-25

AI Technical Summary

Technical Problem

In the prior art, when converting SPI protocol data on the host processor side to serial protocol data on the device side, special chips are required, resulting in high hardware costs and poor flexibility and adaptability, making it difficult to adapt to changes in peripheral device parameters and business volume.

Method used

Through the equipment and methods of realizing SPI to UART communication between the central processor CPU and the complex editable logic device CPLD, the software driver module and the hardware working logic of the CPLD are used to convert the serial port protocol data into SPI protocol data, and support flexible configuration of the data transmission rate.

Benefits of technology

It realizes SPI to UART communication without separate wiring and reduces hardware costs, supports flexible configuration of data transmission rates based on changes in the traffic volume of peripheral devices, and allows direct reuse of existing serial-based applications, improving adaptability and reducing operation and maintenance costs.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119883966A_ABST
    Figure CN119883966A_ABST
Patent Text Reader

Abstract

The invention provides equipment and a method for converting SPI (Serial Peripheral Interface) to UART (Universal Asynchronous Receiver / Transmitter) communication, the equipment comprises a CPU (Central Processing Unit), an SPI (Serial Peripheral Interface) and a CPLD (Complex Programmable Logic Device), the CPU is connected with the CPLD through the SPI, and the CPLD is connected with peripheral equipment through a serial port; the CPU is used for configuring a software driving module in the CPU and the CPLD according to a configuration file, and executing control based on related configuration through the software driving module: serial port protocol data sent to peripheral equipment is converted into SPI protocol data, and the SPI interface is called to be written into the CPLD; calling an SPI interface to read SPI protocol data from the periphery from the CPLD, and converting the SPI protocol data into serial port protocol data; and the CPLD is used for executing hardware working logic based on related configuration. According to the equipment and the method provided by the invention, the adaptability is high, and the hardware and operation and maintenance cost is lower.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of 5G networks and voice calls, and in particular to a device and method for SPI to UART communication. Background Art

[0002] UART (Universal Asynchronous Receiver / Transmitter) serial port has become the most widely used communication interface in the communication industry because it can achieve strong transmission function and a wide range of transmission speeds using simple communication signal lines. SPI (Serial Peripheral Interface) protocol is a protocol widely used for data transmission between integrated circuits, especially between a host processor (also called SPI master) and one or more peripheral devices (also called SPI slaves).

[0003] In some practical application scenarios, the host side does not have a spare serial port interface but has a spare SPI interface, while the device side only provides a serial port interface. Therefore, it is necessary to provide a method to convert the SPI protocol data on the host processor side and the serial port protocol data on the device side, and keep the existing serial port-based application code for use without or with little change. The existing technology is to use a dedicated chip for SPI to UART for conversion, but it requires separate wiring, high hardware cost, and when the peripheral device changes, or the application business volume changes and the data transmission rate needs to be changed frequently, the serial port-based application read and write configuration interface needs to be transplanted to the SPI-based application read and write configuration interface to change the configuration to adapt to the new requirements or peripheral device parameters. The flexibility and adaptability are poor, and the operation and maintenance cost after going online is high. Summary of the invention

[0004] The embodiment of the present invention provides a method, device and equipment for converting SPI protocol to serial port.

[0005] In a first aspect, an embodiment of the present application provides a device for SPI to UART communication, including a central processing unit CPU, a serial peripheral interface SPI interface and a complex programmable logic device CPLD, wherein:

[0006] The CPU is connected to the CPLD via the SPI interface, and the CPLD is connected to the peripheral device via the serial port;

[0007] The CPU is used to configure the software driver module in the CPU and the CPLD according to the configuration file, and the software driver module performs control based on the relevant configuration: converting the serial port protocol data sent to the peripheral device into SPI protocol data, and calling the SPI interface to write into the CPLD; calling the SPI interface to read the SPI protocol data from the peripheral from the CPLD and converting it into serial port protocol data;

[0008] The CPLD is used to execute hardware working logic based on relevant configurations: converting the SPI protocol data written to the peripheral device into serial port data before sending it, and converting the serial port data received from the peripheral device into SPI protocol data.

[0009] In some possible embodiments, the CPLD further includes:

[0010] A first register is used to store SPI protocol data written into the CPLD and sent to a peripheral device;

[0011] The second register is used to store the SPI protocol data obtained by converting the serial port protocol data received from the peripheral device;

[0012] The SPI interface is called to write SPI protocol data into the first register, and the SPI protocol data from the peripheral device is read from the second register.

[0013] In some possible embodiments, the software driver module further includes:

[0014] The first buffer is used to cache the SPI protocol data converted from the serial port protocol data sent to the peripheral device;

[0015] A second buffer is used to cache SPI protocol data from a peripheral device read from the CPLD;

[0016] The software driver module is used to convert the serial port protocol data sent to the peripheral device into SPI protocol data and store it in the first buffer when it is determined that the first write data condition is met based on the status of the first register, the second register, the first buffer and the second buffer; read the SPI protocol data from the first buffer and write it into the first register when it is determined that the second write data condition is met; read the SPI protocol data from the second register and cache it into the second buffer when it is determined that the first read data condition is met; and read the SPI protocol data from the second buffer and convert it into serial port protocol data when it is determined that the second read data condition is met.

[0017] In some possible embodiments, determining whether the write data condition / read data condition is satisfied includes:

[0018] When it is determined that the first buffer is not full, determining that a first data writing condition is met;

[0019] When it is determined that the first register is not full and the first buffer is not empty, it is determined that the second data writing condition is met;

[0020] When it is determined that the second buffer is not full and the second register is not empty, the first data reading condition is satisfied;

[0021] When it is determined that the second buffer is not empty, it is determined that the second data reading condition is met.

[0022] In some possible embodiments, the software driver module is further used for:

[0023] When it is determined that the first register is full, it is determined that the second data writing condition is not met, and a delay setting time is performed before determining whether the second data writing condition is met;

[0024] When it is determined that the first register is not full and the first buffer is empty, it is determined that the second data writing condition is not met, and a delay of a set time is made before determining whether the second data writing condition is met;

[0025] When it is determined that the second buffer is full, it is determined that the first data reading condition is not satisfied, and the first data reading condition is determined again after a delay of a set time;

[0026] When it is determined that the second buffer is not full and the second register is empty, it is determined that the first data reading condition is not satisfied, and the first data reading condition is determined again after a delay of a set time.

[0027] In some possible embodiments, the software driver module is further used for:

[0028] When it is determined that the first buffer is full, it is determined that the first data writing condition is not satisfied, a write task corresponding to the serial port protocol data sent to the peripheral device is created and inserted into a write waiting queue;

[0029] When it is determined that the second buffer is empty, it is determined that the second data reading condition is not met, and a read task corresponding to the serial port protocol data is created and inserted into a read waiting queue.

[0030] In some possible embodiments, the software driver module is further used for:

[0031] When it is determined that the first buffer is not full, it is determined that the first data writing condition is met, the earliest writing task in the writing waiting queue is awakened from the writing waiting queue, the serial port protocol data in the writing task is converted into SPI protocol data, and the data is written into the first buffer;

[0032] When it is determined that the second buffer is not empty, it is determined that the second data reading condition is met, the earliest queued read task is awakened from the read waiting queue, and the SPI protocol data corresponding to the serial port protocol data in the read task is converted into serial port protocol data.

[0033] In some possible embodiments, the software driver module is implemented by a virtual serial port character device file, and the software driver module performs control based on relevant configurations, including:

[0034] The software driver module executes through the write interface: converting the serial port protocol data sent to the peripheral device into SPI protocol data;

[0035] The software driver module is executed through a write thread in the kernel: calling the SPI interface to write the converted SPI protocol data into the CPLD;

[0036] The software driver module executes through the read interface: converting the SPI protocol data read from the peripheral device into serial port protocol data;

[0037] The software driver module is executed through a read thread in the kernel: the SPI interface is called to read the SPI protocol data from the periphery from the CPLD.

[0038] In a second aspect, the embodiment of the present application further proposes a method for SPI to UART communication, comprising:

[0039] Configure the software driver module and CPLD in the CPU according to the configuration file;

[0040] Through the software driver module, based on relevant configuration, the serial port protocol data sent to the peripheral device is converted into SPI protocol data, and the SPI interface is called to write into the CPLD; the SPI interface is called to read the SPI protocol data from the peripheral from the CPLD and convert it into serial port protocol data;

[0041] Through the CPLD, based on relevant configuration, the SPI protocol data written to the peripheral device is converted into serial port data and then sent, and the serial port data received from the peripheral device is converted into SPI protocol data.

[0042] In some possible embodiments, the method further includes:

[0043] Storing the SPI protocol data written into the CPLD and sent to the peripheral device through the first register in the CPLD;

[0044] The SPI protocol data obtained by converting the serial port protocol data received from the peripheral device is stored through the second register in the CPLD;

[0045] The software driver module calls the SPI interface to write SPI protocol data into the first register, and reads SPI protocol data from the peripheral device from the second register.

[0046] The embodiment of the present application proposes a device and method for SPI to UART communication, which can eliminate the existing dedicated chip for SPI to UART and eliminate the need for separate wiring, thereby reducing the plate making area and thus the hardware cost. At the same time, it supports flexible configuration of the data transmission rate according to the changes in the business volume corresponding to the peripheral device and supports direct reuse of existing serial port-based applications.

[0047] Other features and advantages of the present application will be described in the following description, and partly become apparent from the description, or be understood by practicing the present application. The purpose and other advantages of the present application can be realized and obtained by the structures specifically pointed out in the written description, claims, and drawings. BRIEF DESCRIPTION OF THE DRAWINGS

[0048] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in the embodiments of the present application will be briefly introduced below. Obviously, the drawings introduced below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.

[0049] Figure 1 This is a schematic diagram of a practical application scenario in an embodiment of the present application;

[0050] Figure 2 This is a schematic diagram of a device for SPI to UART communication in an embodiment of the present application;

[0051] Figure 3 Schematic diagram of CPLD in the device in the embodiment of the present application;

[0052] Figure 4 A schematic diagram of a software driver module in an embodiment of the present application;

[0053] Figure 5 This is a schematic diagram of an initialization process in an embodiment of the present application;

[0054] Figure 6 This is a schematic diagram of a data writing process in an embodiment of the present application;

[0055] Figure 7 This is a schematic diagram of a data reading process in an embodiment of the present application;

[0056] Figure 8 A flow chart of a method for converting SPI to UART communication in an embodiment of the present application;

[0057] Fig. 9 This is a schematic diagram of a data conversion process in an embodiment of the present application. DETAILED DESCRIPTION

[0058] To further illustrate the technical solution provided by the embodiments of the present application, a detailed description is given below in conjunction with the accompanying drawings and specific implementation methods.

[0059] The Universal Asynchronous Receiver Transmitter (UART) serial port has become the most widely used communication interface in the communication industry because it can achieve strong transmission functions and a wide range of transmission speeds using simple communication signal lines. The Serial Peripheral Interface (SPI) protocol is a protocol widely used for data transmission between integrated circuits, especially between a host processor (also called an SPI master) and one or more peripheral devices (also called SPI slaves).

[0060] In some practical application scenarios, such as Figure 1 As shown, the host side has no spare serial port interface 101 but has a spare SPI interface 102, and the device side only provides a serial port interface 103, so it is necessary to provide a method to convert the host side SPI protocol data and the device side serial port protocol data to each other, and keep the existing application code based on the serial port protocol data for use without or with little change. The prior art is to use a dedicated chip for SPI to UART for conversion, but it requires separate wiring, high hardware cost, and when the peripheral device changes, or the application business volume changes and the data transmission rate needs to be changed frequently, the serial port-based application read and write configuration interface needs to be transplanted to the SPI-based application read and write configuration interface to change the configuration to adapt to the new requirements or peripheral device parameters, which has poor flexibility and adaptability, and high operation and maintenance costs after going online.

[0061] In view of the problem that the dedicated chip described in the above-mentioned prior art requires separate wiring and has low adaptability and is not flexible enough, the embodiment of the present application proposes a SPI to UART communication device, which converts the serial port protocol data and SPI protocol data sent and received by the CPU through a software driver module, and then converts the SPI protocol data into UART protocol data with the help of the logic of CPLD (Complex Programmable Logic Device), and finally transmits it to the real serial port interface on the peripheral device.

[0062] In the embodiment of the present application, the device is as follows Figure 2 As shown, it includes a central processing unit CPU, a serial peripheral interface SPI interface and a complex programmable logic device CPLD, wherein:

[0063] The CPU is connected to the CPLD via the SPI interface, and the CPLD is connected to the peripheral device via a serial port.

[0064] Complex programmable logic devices (CPLDs) are widely used in many application scenarios in the communications industry. Many low-speed interfaces of the host processor are generally connected to the pins of the CPLD first to facilitate multiplexing, configuration and control.

[0065] In an embodiment of the present application, the CPU is used to configure the software driver module in the CPU and the CPLD according to the configuration file, and the software driver module performs control based on the relevant configuration: converting the serial port protocol data sent to the peripheral device into SPI protocol data, and calling the SPI interface to write into the CPLD; calling the SPI interface to read the SPI protocol data from the peripheral from the CPLD and converting it into serial port protocol data.

[0066] Through the above-mentioned software driver module, the serial port protocol data of the CPU can be converted into SPI protocol data. The software driver module is executed by the created virtual serial port character device file. Therefore, the existing application program based on the serial port protocol data can be transplanted to a SPI to URAT communication device in the embodiment of the present application without or with a small amount of adaptation, and can run seamlessly to complete the data conversion with high adaptability.

[0067] In an embodiment of the present application, the CPLD is used to execute hardware working logic based on relevant configurations: converting the SPI protocol data written to the peripheral device into serial port data and then sending it, and converting the serial port data received from the peripheral device into SPI protocol data.

[0068] Through the above-mentioned CPLD, SPI protocol data can be converted into serial port data and sent to the serial port interface corresponding to the peripheral device, and the data transmission between the device and the peripheral device can be completed in cooperation with the software driver module described in the above-mentioned embodiment of the present application. In addition, the CPLD supports online upgrades and can flexibly respond to changes in data transmission rate requirements according to changes in business volume corresponding to different peripheral devices, so it has high adaptability.

[0069] In some possible embodiments, the CPLD, such as Figure 3 As shown, it also includes:

[0070] The first register 301 is used to store the SPI protocol data written into the CPLD and sent to the peripheral device;

[0071] The second register 302 is used to store the SPI protocol data obtained by converting the serial port protocol data received from the peripheral device;

[0072] The SPI interface is called to write SPI protocol data into the first register, and the SPI protocol data from the peripheral device is read from the second register.

[0073] According to the buffer size preset in the configuration file in the CPU, the number of buffers, the baud rate and other parameters, the logic and the first and second registers of the CPLD are flexibly configured. Based on the configuration logic and parameters of the CPLD, the CPLD is used to realize the conversion between SPI protocol data and serial port protocol data, which can replace the dedicated chip in the prior art and has low hardware cost.

[0074] In some possible embodiments, the SPI to UART communication device, such as Figure 4 As shown, the software driver module also includes:

[0075] The first buffer 401 is used to cache the SPI protocol data converted from the serial port protocol data sent to the peripheral device;

[0076] The second buffer 402 is used to cache the SPI protocol data from the peripheral device read from the CPLD.

[0077] In an embodiment of the present application, the buffer is flexibly configured according to parameters such as the buffer size, number of buffers, baud rate, etc. preset in a configuration file in the CPU to create one or more first buffers and one or more second buffers.

[0078] In some possible embodiments, the software driver module is first configured according to the configuration file in the CPU, a first buffer and a second buffer of corresponding size and quantity are created, and then the logic and the first and second registers of the CPLD are configured to complete the initialization process of the device. The initialization configuration of the software driver module also includes configuring the baud rate, parity check, stop bit of UART communication, and MODE of SPI communication.

[0079] In some possible embodiments, the software driver module is implemented through a virtual serial port character device file and is dynamically inserted into the kernel in the form of software driver module KO. Through the configuration program, the user can place the size, number, baud rate and other parameters of the buffer in the software driver module in the configuration file according to the business volume and actual hardware conditions of the peripheral device. The configuration program parses the configuration file during the initialization process and configures the software driver module.

[0080] Specifically, by opening the user configuration file, reading the configuration parameters, and then opening the virtual serial port character device file corresponding to the software driver module, the configuration parameters are input into the kernel of the software driver module, the buffer size and number of the software driver module are configured, and the corresponding CPLD is configured according to the configuration parameters, the size and number of the corresponding first and second registers are configured, and the baud rate of UART communication and the mode MODE of SPI communication are configured.

[0081] In an embodiment of the present application, the software driver module and CPLD can perform data conversion and transmission after initialization configuration, wherein the software driver module is used to determine based on the status of the first register, the second register, the first buffer and the second buffer that when the corresponding conditions are met, execute control based on the relevant configuration.

[0082] In the embodiment of the present application, the control performed by the software driver module includes:

[0083] The software driver module executes through the write interface: converting the serial port protocol data sent to the peripheral device into SPI protocol data;

[0084] The software driver module is executed through a write thread in the kernel: calling the SPI interface to write the converted SPI protocol data into the CPLD;

[0085] The software driver module executes through the read interface: converting the SPI protocol data read from the peripheral device into serial port protocol data;

[0086] The software driver module is executed through a read thread in the kernel: the SPI interface is called to read the SPI protocol data from the periphery from the CPLD.

[0087] Through the above-mentioned read-write interface and read-write thread, data transmission in two directions is completed. By calling the SPI interface on the device and cooperating with the CPLD, the conversion function between the serial port protocol data in the CPU to the SPI protocol data and then to the serial port protocol data of the peripheral device is realized.

[0088] In the embodiment of the present application, the data transmission between the device and the peripheral device has two directions, one direction is the data output from the device to the peripheral device, and the other direction is the data reading from the peripheral device to the device. The data output includes two stages. First, when it is determined that the first data writing condition is met, the serial port protocol data sent to the peripheral device is converted into SPI protocol data and stored in the first buffer, which is specifically executed through the above-mentioned write interface. Then, when it is determined that the second data writing condition is met, the SPI protocol data is read from the first buffer and written into the first register, which is specifically executed by the write thread in the above-mentioned kernel.

[0089] Correspondingly, data reading also includes two stages. First, when it is determined that the first data reading condition is met, the SPI protocol data is read from the second register and cached in the second buffer, which is specifically executed by the read thread in the above-mentioned kernel. Then, when it is determined that the second data reading condition is met, the SPI protocol data is read from the second buffer and converted into serial port protocol data, which is specifically executed by the above-mentioned read interface.

[0090] In some possible embodiments, the software driver module, when determining that the corresponding conditions are met, performs control based on the relevant configuration to complete the four stages in the above two data transmission directions, specifically including:

[0091] When it is determined that the first data writing condition is met, the serial port protocol data sent to the peripheral device is converted into SPI protocol data through the write interface and stored in the first buffer;

[0092] When it is determined that the second data writing condition is met, start the write thread, call the SPI interface, read the SPI protocol data from the first buffer and write it into the first register;

[0093] When it is determined that the first data reading condition is met, the read thread is started, the SPI interface is called, and the SPI protocol data is read from the second register and cached into the second buffer;

[0094] When it is determined that the second data reading condition is met, the SPI protocol data is read from the second buffer through the read interface and converted into serial port protocol data.

[0095] In some possible embodiments, when the software driver module determines that the write data condition / read data condition is met based on the states of the first register, the second register, the first buffer, and the second buffer, the software driver module includes:

[0096] When it is determined that the first buffer is not full, determining that a first data writing condition is met;

[0097] When it is determined that the first register is not full and the first buffer is not empty, it is determined that the second data writing condition is met;

[0098] When it is determined that the second buffer is not full and the second register is not empty, the first data reading condition is satisfied;

[0099] When it is determined that the second buffer is not empty, it is determined that the second data reading condition is met.

[0100] In the embodiment of the present application, there is a timing relationship between the data output and the data reading. When the data output is completed, the peripheral device at the other end will immediately return the corresponding serial port protocol data after successfully receiving the corresponding serial port protocol data. Therefore, the second register will then read the returned data. Even if the read thread is not started in time to perform the read task, the corresponding read task can be judged later by the received data content. Therefore, in some cases, it is necessary to delay and then determine whether to read / write data.

[0101] In view of the above-mentioned timing problem and the register size problem of the CPLD, the software driver module is also used for:

[0102] When it is determined that the first register is full, it is determined that the second data writing condition is not met, and a delay setting time is performed before determining whether the second data writing condition is met;

[0103] When it is determined that the first register is not full and the first buffer is empty, it is determined that the second data writing condition is not met, and a delay of a set time is made before determining whether the second data writing condition is met;

[0104] When it is determined that the second buffer is full, it is determined that the first data reading condition is not satisfied, and the first data reading condition is determined again after a delay of a set time;

[0105] When it is determined that the second buffer is not full and the second register is empty, it is determined that the first data reading condition is not satisfied, and the first data reading condition is determined again after a delay of a set time.

[0106] In some possible embodiments, due to timing issues, the acquired data needs to be recorded and queued, and the kernel of the software driver module also includes a write waiting queue and a read waiting queue, wherein:

[0107] When it is determined that the first buffer is full, it is determined that the first data writing condition is not satisfied, a write task corresponding to the serial port protocol data sent to the peripheral device is created and inserted into a write waiting queue;

[0108] When it is determined that the second buffer is empty, it is determined that the second data reading condition is not met, and a read task corresponding to the serial port protocol data is created and inserted into a read waiting queue.

[0109] In some possible embodiments, if the virtual serial port character device file corresponding to the above-mentioned software driver module is configured with the non-blocking mode flag O_NONBLOCK attribute, and it is determined that the first write data condition / the second read data condition is not met, it returns immediately, that is, the corresponding write / read task is not created, nor is the task inserted into the waiting queue.

[0110] Correspondingly, for the tasks in the waiting queue, when certain conditions are met, they will be awakened and restarted in the order of entering the queue. The software driver module is also used for:

[0111] When it is determined that the first buffer is not full, it is determined that the first data writing condition is met, the earliest writing task in the writing waiting queue is awakened from the writing waiting queue, the serial port protocol data in the writing task is converted into SPI protocol data, and the data is written into the first buffer;

[0112] When it is determined that the second buffer is not empty, it is determined that the second data reading condition is met, the earliest enqueued read task is awakened from the read waiting queue, the SPI protocol data corresponding to the serial port protocol data in the read task is converted into serial port protocol data, and written into the second buffer.

[0113] Based on the device in the above embodiment of the present application, an initialization process is given below, such as Figure 5 As shown, including:

[0114] Step 501, inserting the software driver module KO into the kernel;

[0115] Step 502, opening the configuration file uart_profile, and configuring the size, number, baud rate and other parameters of the buffer in the software driver module according to the parameters in the configuration file through the configuration program;

[0116] Step 503, configuring the CPLD logic and register size, number, baud rate and other parameters according to the parameters in the configuration file;

[0117] Step 504, creating a virtual serial port character device file;

[0118] Step 505, creating a first buffer and a second buffer of corresponding numbers and sizes, and creating a write waiting queue and a read waiting queue;

[0119] Step 506: create a write thread and a read thread and start the threads.

[0120] Based on the device in the above embodiment of the present application, a data writing process is given below, wherein the virtual serial port character device file is not configured with the O_NONBLOCK attribute, including a first buffer and a first register, such as Figure 6 As shown, including:

[0121] Step 1, open the virtual serial port character device file, initialize the corresponding software driver module according to the configuration file, prepare data, and call the WRITE interface to enter the kernel;

[0122] Step 2, determining the state of the first register, and performing related control through the software driver module, wherein:

[0123] When it is determined that the first register is not full and the first buffer is not empty, it is determined that the second data writing condition is met, a write thread is started, and the SPI protocol data is read from the first buffer and written into the first register;

[0124] When it is determined that the first register is full, it is determined that the second data writing condition is not met, and a delay setting time is set before determining whether the second data writing condition is met, at which time CPU scheduling occurs;

[0125] When it is determined that the first register is not full and the first buffer is empty, it is determined that the second data writing condition is not met, and a delay setting time is made to determine whether the second data writing condition is met, at which time CPU scheduling occurs;

[0126] Step 3, determining the state of the first buffer, and performing related control through the software driver module, wherein:

[0127] When it is determined that the first buffer is not full, it is determined that the first data writing condition is met, and the serial port protocol data sent to the peripheral device is converted into SPI protocol data through the write interface and stored in the first buffer and returned successfully;

[0128] When it is determined that the first buffer is full, it is determined that the first data writing condition is not met, creating a write task corresponding to the serial port protocol data sent to the peripheral device through the write interface and inserting it into the write waiting queue;

[0129] When it is determined that the first buffer is not full, it is determined that the first data writing condition is met, the earliest enqueued write task is awakened from the write waiting queue through the write interface, the serial port protocol data in the write task is converted into SPI protocol data, and written into the first buffer.

[0130] In the embodiment of the present application, the data output loop determines two flags: one is whether the first register is full; the other is whether the first buffer is full, and the former is determined first. Only when the first register is not full, the core read thread can send new data to the peripheral device.

[0131] Based on the device in the above embodiment of the present application, a data reading process is given below, wherein the virtual serial port character device file is not configured with the O_NONBLOCK attribute, including a second buffer and a second register, such as Figure 7 As shown, including:

[0132] Step 1, open the virtual serial port character device file, initialize the corresponding software driver module according to the configuration file, prepare data, and call the READ interface to enter the kernel;

[0133] Step 2, determining the state of the second buffer, and performing related control through the software driver module, wherein:

[0134] When it is determined that the second buffer is not empty, it is determined that the second data reading condition is met, and the SPI protocol data read from the peripheral device is converted into serial port protocol data through the read interface;

[0135] When it is determined that the second buffer is full, it is determined that the first data reading condition is not satisfied, and the first data reading condition is determined again after a delay of a set time, at which time CPU scheduling occurs;

[0136] When it is determined that the second buffer is empty, it is determined that the second data reading condition is not met, creating a read task corresponding to the serial port protocol data through a read interface and inserting it into a read waiting queue;

[0137] When it is determined that the second buffer is not empty, it is determined that the second data reading condition is met, and the earliest enqueued read task is awakened from the read waiting queue through the read interface, and the SPI protocol data read from the peripheral device is converted into serial port protocol data;

[0138] Step 3, determining the state of the second register, and performing related control through the software driver module, wherein:

[0139] When it is determined that the second buffer is not full and the second register is not empty, the first data reading condition is met, a read thread is started, the SPI protocol data is read from the second register and cached in the second buffer;

[0140] When it is determined that the second buffer is not full and the second register is empty, it is determined that the first data reading condition is not satisfied, and the first data reading condition is judged again after a delay setting time, at which time CPU scheduling occurs.

[0141] In the embodiment of the present application, the data reading loop determines two flags: one is whether the second register is empty; the other is whether the second buffer is full, and the latter is determined first. Only when the second buffer is not full, the kernel reading thread can write the read data into the second buffer.

[0142] Through the device proposed in the above-mentioned embodiment of the present application, the conversion function between the user's serial port data to the SPI protocol data on the host processor side and then to the serial port data on the device side is realized. Compared with the existing dedicated chips, the hardware cost of the present application is lower, and the conversion parameters of the above-mentioned software driver module and CPLD can be configured according to the parameters required by the peripheral devices and the size of the application business volume. Compared with the prior art that requires "the serial port-based application read and write configuration interface to be transplanted to the SPI-based application read and write configuration interface" to change the configuration to adapt to new requirements or peripheral device parameters, it is more flexible and adaptable, and the operation and maintenance cost after going online is also lower.

[0143] Based on the same inventive concept, the present application embodiment also proposes a method for SPI to UART communication, such as Figure 8 As shown, including:

[0144] Step 801, configuring the software driver module and CPLD in the CPU according to the configuration file;

[0145] Step 802, through the software driver module, based on relevant configuration, convert the serial port protocol data sent to the peripheral device into SPI protocol data, and call the SPI interface to write into the CPLD; call the SPI interface to read the SPI protocol data from the peripheral from the CPLD and convert it into serial port protocol data;

[0146] Step 803, through the CPLD, based on relevant configuration, the written SPI protocol data sent to the peripheral device is converted into serial port data and then sent, and the serial port data received from the peripheral device is converted into SPI protocol data.

[0147] In some possible embodiments, the method further includes:

[0148] Storing the SPI protocol data written into the CPLD and sent to the peripheral device through the first register in the CPLD;

[0149] The SPI protocol data obtained by converting the serial port protocol data received from the peripheral device is stored through the second register in the CPLD;

[0150] The software driver module calls the SPI interface to write SPI protocol data into the first register, and reads SPI protocol data from the peripheral device from the second register.

[0151] The following is a flow chart of a method for converting data using a SPI to UART communication method in the embodiment of the present application. Fig. 9 As shown, including:

[0152] Step 1: Configure the software driver module and CPLD in the CPU according to the configuration file.

[0153] Step 2, inserting the software driver module into the kernel, based on relevant configuration, converting the serial port protocol data sent to the peripheral device into SPI protocol data through the software driver module, and calling the SPI interface to write into the CPLD, wherein:

[0154] When it is determined that the first register is not full and the first buffer is not empty, it is determined that the second data writing condition is met, and the SPI protocol data is read from the first buffer and written into the first register;

[0155] When it is determined that the first register is full, it is determined that the second data writing condition is not met, and a delay setting time is set before determining whether the second data writing condition is met, at which time CPU scheduling occurs;

[0156] When it is determined that the first register is not full and the first buffer is empty, it is determined that the second data writing condition is not met, and a delay setting time is made to determine whether the second data writing condition is met, at which time CPU scheduling occurs;

[0157] When it is determined that the first buffer is not full, it is determined that the first data writing condition is met, the serial port protocol data sent to the peripheral device is converted into SPI protocol data and stored in the first buffer and returned successfully;

[0158] When it is determined that the first buffer is full, it is determined that the first data writing condition is not satisfied, a write task corresponding to the serial port protocol data sent to the peripheral device is created and inserted into a write waiting queue;

[0159] When it is determined that the first buffer is not full, it is determined that the first data writing condition is met, the earliest enqueued write task is awakened from the write waiting queue, the serial port protocol data in the write task is converted into SPI protocol data, and written into the first buffer.

[0160] Step 3, calling the SPI interface through the software driver module to read the SPI protocol data from the peripheral from the CPLD and convert it into serial port protocol data, wherein:

[0161] When it is determined that the second buffer is not empty, determining that a second data reading condition is met, and reading serial port protocol data from the second buffer;

[0162] When it is determined that the second buffer is full, it is determined that the first data reading condition is not satisfied, and the first data reading condition is determined again after a delay of a set time, at which time CPU scheduling occurs;

[0163] When it is determined that the second buffer is empty, it is determined that the second data reading condition is not met, a read task corresponding to the serial port protocol data is created and inserted into a read waiting queue;

[0164] When it is determined that the second buffer is not empty, it is determined that the second data reading condition is met, the earliest read task in the read waiting queue is awakened from the read waiting queue, and the SPI protocol data corresponding to the serial port protocol data in the read task is converted into serial port protocol data;

[0165] When it is determined that the second buffer is not full and the second register is not empty, the first data reading condition is met, and the SPI protocol data is read from the second register and cached into the second buffer;

[0166] When it is determined that the second buffer is not full and the second register is empty, it is determined that the first data reading condition is not satisfied, and the first data reading condition is judged again after a delay setting time, at which time CPU scheduling occurs.

[0167] Step 4, through the CPLD, based on relevant configuration, the written SPI protocol data sent to the peripheral device is converted into serial port data and then sent, and the serial port data received from the peripheral device is converted into SPI protocol data.

[0168] An SPI to UART communication method proposed in an embodiment of the present application can eliminate the existing SPI to UART dedicated chip, eliminate the need for separate wiring, reduce the plate making area, and thus reduce hardware costs. At the same time, it supports flexible configuration of data transmission rate according to changes in business volume corresponding to peripheral devices, and supports direct reuse of existing serial port-based applications.

[0169] Obviously, those skilled in the art can make various changes and modifications to the present application without departing from the spirit and scope of the present application. Thus, if these modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is also intended to include these modifications and variations.

Claims

1. A device for SPI to UART communication, characterized in that: It includes a central processing unit CPU, a serial peripheral interface SPI interface and a complex programmable logic device CPLD, among which: The CPU is connected to the CPLD via the SPI interface, and the CPLD is connected to the peripheral device via the serial port; The CPU is used to configure the software driver module in the CPU and the CPLD according to the configuration file, and the software driver module performs control based on the relevant configuration: converting the serial port protocol data sent to the peripheral device into SPI protocol data, and calling the SPI interface to write into the CPLD; calling the SPI interface to read the SPI protocol data from the peripheral from the CPLD and converting it into serial port protocol data; The CPLD is used to execute hardware working logic based on relevant configurations: converting the SPI protocol data written to the peripheral device into serial port data before sending it, and converting the serial port data received from the peripheral device into SPI protocol data.

2. The device according to claim 1, characterized in that The CPLD further comprises: A first register is used to store SPI protocol data written into the CPLD and sent to a peripheral device; The second register is used to store the SPI protocol data obtained by converting the serial port protocol data received from the peripheral device; The SPI interface is called to write SPI protocol data into the first register, and the SPI protocol data from the peripheral device is read from the second register.

3. The device according to claim 2, characterized in that The software driver module also includes: The first buffer is used to cache the SPI protocol data converted from the serial port protocol data sent to the peripheral device; A second buffer is used to cache SPI protocol data from a peripheral device read from the CPLD; The software driver module is used to convert the serial port protocol data sent to the peripheral device into SPI protocol data and store it in the first buffer when it is determined that the first write data condition is met based on the status of the first register, the second register, the first buffer and the second buffer; read the SPI protocol data from the first buffer and write it into the first register when it is determined that the second write data condition is met; read the SPI protocol data from the second register and cache it into the second buffer when it is determined that the first read data condition is met; and read the SPI protocol data from the second buffer and convert it into serial port protocol data when it is determined that the second read data condition is met.

4. The device according to claim 3, characterized in that The determining that the write data condition / read data condition is satisfied includes: When it is determined that the first buffer is not full, determining that a first data writing condition is met; When it is determined that the first register is not full and the first buffer is not empty, it is determined that the second data writing condition is met; When it is determined that the second buffer is not full and the second register is not empty, the first data reading condition is satisfied; When it is determined that the second buffer is not empty, it is determined that the second data reading condition is met.

5. The device according to claim 3, characterized in that The software driver module is also used for: When it is determined that the first register is full, it is determined that the second data writing condition is not met, and a delay setting time is performed before determining whether the second data writing condition is met; When it is determined that the first register is not full and the first buffer is empty, it is determined that the second data writing condition is not met, and a delay of a set time is made before determining whether the second data writing condition is met; When it is determined that the second buffer is full, it is determined that the first data reading condition is not satisfied, and the first data reading condition is determined again after a delay of a set time; When it is determined that the second buffer is not full and the second register is empty, it is determined that the first data reading condition is not satisfied, and the first data reading condition is determined again after a delay of a set time.

6. The device according to claim 3, characterized in that The software driver module is also used for: When it is determined that the first buffer is full, it is determined that the first data writing condition is not satisfied, a write task corresponding to the serial port protocol data sent to the peripheral device is created and inserted into a write waiting queue; When it is determined that the second buffer is empty, it is determined that the second data reading condition is not met, and a read task corresponding to the serial port protocol data is created and inserted into a read waiting queue.

7. The device according to claim 3, characterized in that The software driver module is also used for: When it is determined that the first buffer is not full, it is determined that the first data writing condition is met, the earliest writing task in the writing waiting queue is awakened from the writing waiting queue, the serial port protocol data in the writing task is converted into SPI protocol data, and the data is written into the first buffer; When it is determined that the second buffer is not empty, it is determined that the second data reading condition is met, the earliest queued read task is awakened from the read waiting queue, and the SPI protocol data corresponding to the serial port protocol data in the read task is converted into serial port protocol data.

8. The device according to claim 1, characterized in that The software driver module is implemented by a virtual serial port character device file, and the software driver module performs control based on relevant configurations, including: The software driver module executes through the write interface: converting the serial port protocol data sent to the peripheral device into SPI protocol data; The software driver module is executed through a write thread in the kernel: calling the SPI interface to write the converted SPI protocol data into the CPLD; The software driver module is executed through a read thread in the kernel: calling an SPI interface to read SPI protocol data from the periphery from the CPLD; The software driver module executes, through the read interface, the conversion of the SPI protocol data read from the peripheral device into serial port protocol data.

9. A method for converting SPI to UART communication, characterized in that: include: Configure the software driver module and CPLD in the CPU according to the configuration file; Through the software driver module, based on relevant configuration, the serial port protocol data sent to the peripheral device is converted into SPI protocol data, and the SPI interface is called to write into the CPLD; Calling the SPI interface to read the SPI protocol data from the peripheral from the CPLD and converting it into serial port protocol data; Through the CPLD, based on relevant configuration, the SPI protocol data written to the peripheral device is converted into serial port data and then sent, and the serial port data received from the peripheral device is converted into SPI protocol data.

10. The method according to claim 9, characterized in that Also includes: Storing the SPI protocol data written into the CPLD and sent to the peripheral device through the first register in the CPLD; The SPI protocol data obtained by converting the serial port protocol data received from the peripheral device is stored through the second register in the CPLD; The software driver module calls the SPI interface to write SPI protocol data into the first register, and reads SPI protocol data from the peripheral device from the second register.