A method and apparatus for SPI communication based on continuous register layout

The SPI communication method with continuous register layout solves the problems of scattered register layout and address gaps in existing SPI controllers in scenarios with multiple sensor acquisition and multiple frame length adaptation. It achieves a compact register layout and efficient data transmission, supports variable frame length and unified interrupt mechanism, reduces communication cost and improves efficiency.

CN123138162APending Publication Date: 2026-11-20SHANDONG LUHAI EQUIPMENT GROUP RIZHAO CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611618984.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-10-20
Publication Date
2026-11-20

AI Technical Summary

Technical Problem

Existing SPI controller architectures suffer from problems such as scattered register layout, numerous address gaps, the need for table lookup mapping in software drivers, insufficient bit width for frame length configuration, and lack of a unified interrupt/clear mechanism for frame completion and valid reception status in scenarios involving multi-sensor acquisition, multi-frame length adaptation, low-overhead driver development, and high-reliability data transmission.

Method used

An SPI communication method based on a continuous register layout is adopted. By acquiring initialization configuration data, continuous register initialization configuration is performed, including setting clock polarity, clock phase, clock division factor, variable frame length, bit order and interrupt enable switch. This achieves a compact layout of continuous registers, supports variable frame length and unified interrupt mechanism, and simplifies software driving.

Benefits of technology

It achieves a compact, hole-free register layout, reduces SPI communication costs, improves communication efficiency, solves the problems of register address holes and large software driver lookup mapping overhead, and supports efficient data transmission in scenarios with multiple frame lengths and multiple sensor acquisitions.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123138162A_ABST
    Figure CN123138162A_ABST
Patent Text Reader

Abstract

The application provides a serial peripheral interface (SPI) communication method and device based on continuous register layout. The method comprises the following steps: obtaining initialization configuration data; initializing and configuring an initial register according to the initialization configuration data and a preset continuous address to obtain a target register after configuration; performing data writing processing on the target register according to to-be-sent frame data to obtain a serial clock and sent cache data; obtaining a frame completion signal according to the serial clock, the sent cache data, the target register and received feedback data; obtaining an interrupt request signal according to the frame completion signal and the target register; and in response to a reading operation on a received data register, clearing a received valid flag bit, resetting a transmission state machine and canceling the interrupt request signal. The application can reduce the SPI communication cost and improve the communication efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of integrated circuit technology, and also to an SPI communication method and apparatus based on a continuous register layout. Background Technology

[0002] The Serial Peripheral Interface (SPI) is a commonly used high-speed peripheral interface in embedded System-on-Chip (SoC) devices, used for communication with external Flash (non-volatile memory), ADCs (analog-to-digital converters), sensors, etc. Existing mainstream SoCs integrate traditional SPI controller architectures with fixed architectures and significant design flaws. These flaws present numerous shortcomings in industrial and port data acquisition scenarios requiring multi-sensor acquisition, multi-frame-length adaptation, low-overhead driver development, and high-reliability data transmission. Specific technical defects include: disorganized register layout, numerous address gaps, requiring software drivers to look up tables for mapping, resulting in high firmware overhead; insufficient frame-length configuration bit width, and lack of support for variable frame lengths; during short-frame transmission, redundant high / low bits in the registers exceeding the effective bit width of the current frame participate in data determination, causing valid data contamination and leading to data parsing errors, sampling accuracy deviations, and other problems; and the lack of a unified interrupt / clear mechanism for frame completion and valid reception status. Summary of the Invention

[0003] The technical problem to be solved by the present invention is to provide an SPI communication method and apparatus based on a continuous register layout, so as to avoid register address gaps and improve communication efficiency.

[0004] To solve the above-mentioned technical problems, the technical solution of the present invention is as follows: A first aspect of the present invention provides an SPI communication method based on a contiguous register layout, comprising: Obtain initialization configuration data; the initialization configuration data includes at least clock polarity CPOL, clock phase CPHA, clock division factor, variable frame length, bit order, chip select channel number, and interrupt enable switch; the interrupt enable switch includes frame completion interrupt enable switch and receive valid interrupt enable switch; The initial register is initialized and configured according to the initialization configuration data and the preset consecutive addresses to obtain the configured target register. Data writing is performed based on the frame data to be transmitted and the target register to obtain the serial clock and transmit buffer data; The frame completion signal is obtained based on the serial clock, the transmitted buffer data, the target register, and the received feedback data; An interrupt request signal is obtained based on the frame completion signal and the target register; In response to a read operation on the receive data register, the receive valid flag is cleared, the transmission state machine is reset, and the interrupt request signal is canceled.

[0005] Optionally, the initial register is initialized according to the initialization configuration data and preset consecutive addresses to obtain the configured target register, including: The initial registers are arranged according to a preset consecutive address to obtain a consecutive register; the consecutive register includes: a control register, a status register, a transmit data register, a receive data register, a chip select register, and an interrupt enable register; The clock polarity CPOL, clock phase CPHA, clock division factor, variable frame length, and bit order from the initialization configuration data are written into the control register of the continuous register to obtain the target control register. Write the chip select channel number in the initialization configuration data into the chip select register in the consecutive registers to obtain the target chip select register; Write the interrupt enable switch from the initialization configuration data into the interrupt enable register in the continuous register to obtain the target interrupt enable register; The configured target register is obtained based on the target control register, the target chip select register, and the target interrupt enable register.

[0006] Optionally, data writing processing is performed based on the frame data to be transmitted and the target register to obtain the serial clock and transmit buffer data, including: Obtain the frame data to be sent; Write the frame data to be sent into the transmission data register in the target register; Based on the clock division coefficient of the target control register in the target register, a serial clock of the corresponding frequency is output; wherein, the serial clock is divided into two paths, one as the synchronization clock for data transmission and the other as the sampling timing. The frame data to be sent in the transmit data register is loaded into the transmit buffer to obtain transmit buffer data.

[0007] Optionally, a frame completion signal is obtained based on the serial clock, the transmitted buffer data, the target register, and the received feedback data, including: A dynamic bit index is generated based on the variable frame length and bit order of the target control register in the target register; Based on the dynamic bit index and the transmission buffer data, output the corresponding bits of the transmission buffer; A frame completion signal is generated based on the serial clock and the received feedback data.

[0008] Optionally, a frame completion signal is generated based on the serial clock and the received feedback data, including: The received feedback data is stored in the corresponding bit index position of the received data register. When the dynamic bit index has traversed all the valid bits of the frame length, the serial clock output is stopped and a frame completion signal is generated.

[0009] Optionally, an interrupt request signal is obtained based on the frame completion signal and the target register, including: The status register in the target register is set according to the frame completion signal, and the set status is output. Read the interrupt switch status of the target interrupt enable register in the target register. When the interrupt switch status is enabled, an interrupt request signal is obtained.

[0010] Optionally, in response to a read operation of the receive data register, clearing the receive valid flag and resetting the transmission state machine and canceling the interrupt request signal includes: Based on the set state and read instruction, read the receive data register in the target register and output the received data; Clear the receive valid flag, reset the transmission state machine, and cancel the interrupt request signal.

[0011] A second aspect of the present invention provides an SPI communication device based on a contiguous register layout, comprising: The acquisition module is used to acquire initialization configuration data; the initialization configuration data includes at least clock polarity CPOL, clock phase CPHA, clock division factor, variable frame length, bit order, chip select channel number, and interrupt enable switch; the interrupt enable switch includes frame completion interrupt enable switch and receive valid interrupt enable switch; The processing module is configured to initialize the initial register according to the initialization configuration data and the preset continuous address to obtain the configured target register; perform data writing processing according to the frame data to be sent and the target register to obtain the serial clock and transmit buffer data; obtain the frame completion signal according to the serial clock, the transmit buffer data, the target register and the received feedback data; obtain the interrupt request signal according to the frame completion signal and the target register; and clear the receive valid flag bit and reset the transmission state machine and cancel the interrupt request signal in response to the read operation of the receive data register.

[0012] A third aspect of the present invention provides a computing device, comprising: a processor and a memory storing a computer program, wherein the computer program, when executed by the processor, performs the method described in the first aspect.

[0013] A fourth aspect of the present invention provides a computer-readable storage medium storing instructions that, when executed on a computer, cause the computer to perform the method as described in the first aspect.

[0014] The above-described solution of the present invention has at least the following beneficial effects: The above-described solution of the present invention initializes the initial register according to the initialization configuration data and the preset continuous address to obtain the configured target register. Data writing is performed according to the frame data to be sent and the target register to obtain the serial clock and transmit buffer data. A frame completion signal is obtained based on the serial clock, the transmit buffer data, the target register, and the received feedback data. An interrupt request signal is obtained based on the frame completion signal and the target register. In response to a read operation on the receive data register, the receive valid flag is cleared, the transmission state machine is reset, and the interrupt request signal is canceled. This solves the problems of scattered register layout, numerous address holes, and high software-driven table lookup mapping overhead in existing SPI controllers, achieving a compact register layout without holes, reducing SPI communication costs, and improving communication efficiency. Attached Figure Description

[0015] Figure 1 This is a flowchart illustrating the SPI communication method based on a continuous register layout in an embodiment of the present invention. Figure 2 This is a schematic diagram of the register layout in an embodiment of the present invention; Figure 3 This is a schematic diagram of frame timing in an embodiment of the present invention; Figure 4 This is a schematic diagram of the transmission state machine in an embodiment of the present invention; Figure 5 This is a schematic diagram of the dual SPI extension in an embodiment of the present invention; Figure 6 This is a schematic diagram of the structure of an SPI communication device based on a continuous register layout in an embodiment of the present invention. Detailed Implementation

[0016] Exemplary embodiments of the invention will now be described in more detail with reference to the accompanying drawings. While exemplary embodiments of the invention are shown in the drawings, it should be understood that the invention may be implemented in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided so that this invention will be thorough and complete, and will fully convey the scope of the invention to those skilled in the art.

[0017] like Figure 1 As shown, an embodiment of the present invention proposes an SPI communication method based on a contiguous register layout, comprising the following steps: Step 101: Obtain initialization configuration data; the initialization configuration data includes at least clock polarity CPOL, clock phase CPHA, clock division factor, variable frame length, bit order, chip select channel number, and interrupt enable switch; the interrupt enable switch includes frame completion interrupt enable switch and receive valid interrupt enable switch. Step 102: Initialize the initial register according to the initialization configuration data and the preset continuous address to obtain the configured target register; Step 103: Perform data writing processing based on the frame data to be sent and the target register to obtain the serial clock and transmission buffer data; Step 104: Obtain the frame completion signal based on the serial clock, the transmitted buffer data, the target register, and the received feedback data; Step 105: Obtain an interrupt request signal based on the frame completion signal and the target register; Step 106: In response to the read operation of the receive data register, clear the receive valid flag bit, reset the transmission state machine, and cancel the interrupt request signal.

[0018] The SPI communication method based on a contiguous register layout in this invention initializes and configures an initial register according to initialization configuration data and a preset contiguous address to obtain a configured target register. Data writing is performed on the frame data to be transmitted and the target register to obtain a serial clock and transmit buffer data. A frame completion signal is obtained based on the serial clock, the transmit buffer data, the target register, and received feedback data. An interrupt request signal is obtained based on the frame completion signal and the target register. In response to a read operation on the receive data register, the receive valid flag is cleared, the transmission state machine is reset, and the interrupt request signal is canceled. This method solves the problems of scattered register layout, numerous address gaps, and high software-driven table lookup mapping overhead in existing SPI controllers, achieving a compact, gap-free register layout, reduced SPI communication costs, and improved communication efficiency.

[0019] In an optional embodiment of the present invention, in step 101, initialization configuration data is obtained; the initialization configuration data includes at least clock polarity CPOL, clock phase CPHA, clock division factor, variable frame length, bit order, chip select channel number, and interrupt enable switch; the interrupt enable switch includes frame completion interrupt enable switch and receive valid interrupt enable switch. Specifically, by acquiring initialization configuration data, a data foundation is provided for subsequent initialization configuration of registers. Depending on the actual application scenario or real-time mode, specific settings can be made for clock polarity CPOL, clock phase CPHA, clock division factor, variable frame length, bit sequence, chip select channel number, and interrupt enable switch.

[0020] In one specific embodiment, the CPU (Central Processing Unit) software inside the SoC determines the communication parameters matching the external SPI slave device according to the device datasheet, and assembles and generates initialization configuration data in memory. The initialization configuration data defines the timing, rate, frame width, data transmission order, which slave device is selected, and which events are allowed to generate interrupts for subsequent SPI communication.

[0021] In an optional embodiment of the present invention, step 102, initializing the initial register according to the initialization configuration data and the preset consecutive addresses to obtain the configured target register, may include: Step 1021: Arrange the initial registers according to the preset consecutive addresses to obtain consecutive registers; the consecutive registers include: control register, status register, transmit data register, receive data register, chip select register and interrupt enable register; Specifically, a contiguous physical address space is preset, and the six types of registers—control register, status register, transmit data register, receive data register, chip select register, and interrupt enable register—are arranged in a contiguous address layout with no gaps, eliminating the need for table lookups in the software driver. In this step, the physical addresses of the control register, status register, transmit data register, receive data register, chip select register, and interrupt enable register are directly calculated using fixed offsets; no address mapping table needs to be maintained in the software, and no table lookups are required, directly obtaining the register access addresses.

[0022] Step 1022: Write the clock polarity CPOL, clock phase CPHA, clock division factor, variable frame length, and bit order from the initialization configuration data into the control register of the continuous register to obtain the target control register; Specifically, by initiating a bus write transaction, the clock polarity CPOL, clock phase CPHA, clock divider coefficient, variable frame length, and bit order are written in parallel into the corresponding bit fields inside the control register; the register hardware latches the above values; the clock polarity and clock phase are used for subsequent serial clock (SCLK) idle level and data sampling edge control; the clock divider coefficient is used to divide the SoC system master clock to generate the SPI serial clock; the variable frame length limits the number of valid bits in a single transmission; and the bit order determines whether the output is high-order bit first or low-order bit first.

[0023] Step 1023: Write the chip select channel number in the initialization configuration data into the chip select register in the consecutive registers to obtain the target chip select register; Specifically, the chip select channel number is written into the chip select register, the chip select register decodes the channel number, and drives the corresponding chip select (CS) pin to output an effective level (usually low level). The chip select (CS) pins of the other unselected slaves on the bus remain inactive. Only the selected external SPI slave device (the slave device can be Flash, ADC, sensor, etc.) will respond to the serial clock (SCLK) and MOSI signal (master output, slave input).

[0024] Step 1024: Write the interrupt enable switch in the initialization configuration data into the interrupt enable register in the continuous register to obtain the target interrupt enable register; Specifically, through bus write operations, the frame completion interrupt enable bit and receive valid interrupt enable bit are written into the interrupt enable register. Hardware latch enable status: an enable bit of 1 means that an interrupt request is allowed to be sent to the CPU when the corresponding event occurs; an enable bit of 0 means that even if the event occurs, only the status flag is set and no interrupt signal is generated.

[0025] Step 1025: Obtain the configured target register based on the target control register, the target chip select register, and the target interrupt enable register.

[0026] Specifically, the configured target registers include the target control register, target chip select register, target interrupt enable register, status register, transmit data register, and receive data register. The status register, transmit data register, and receive data register are not initialized during the above steps. The status register is primarily a hardware read-only register used to store real-time hardware-generated flags: frame completion flag and receive valid flag. The status register does not store communication configuration; it only reflects the transmission event status and does not contain any service parameters that can be configured by software. The transmit data register stores the frame data to be transmitted. Its default value is 0 after reset, and transmission is triggered only after subsequent writes to the transmit data register. The receive data register stores data returned by external slave devices; its value is undefined after reset / hardware cleared.

[0027] In an optional embodiment of the present invention, step 103, which involves writing data based on the frame data to be transmitted and the target register to obtain the serial clock and transmit buffer data, may include: Step 1031: Obtain the frame data to be sent; Specifically, depending on business needs, the assembly will send instructions or data to external SPI slave devices (such as Flash, ADC, sensors, etc.) as the frame data to be sent; if it is a read operation from the slave, this is filled with dummy data (invalid values, in the SPI full-duplex mechanism, bits must be sent in exchange for data returned by the slave).

[0028] Step 1032: Write the frame data to be sent into the transmission data register in the target register; Specifically, the address of the transmit data register is found using a fixed offset of consecutive addresses. The frame data to be transmitted is written to the transmit data register in parallel through the system bus. The transmit data register is a register accessible to the CPU and is used to transfer the content to be transmitted between software and hardware.

[0029] In addition, upon detecting a write strobe pulse to the transmit data register, an internal transmission start signal is automatically generated. The idle transmission state machine, upon receiving this start signal, leaves the idle state and enters the transmission working state. This embodiment does not require the CPU to write to a dedicated start register; writing transmit data triggers transmission, writing to the transmit data register initiates a frame transmission, and the effective bit width of this frame is determined according to the frame length bit in the control register.

[0030] Step 1033: Output a serial clock of the corresponding frequency according to the clock division coefficient of the target control register in the target register; wherein, the serial clock is divided into two paths, one path is used as the synchronization clock for data transmission, and the other path is used as the sampling timing. Specifically, a serial clock of the corresponding frequency is generated based on the clock division factor. One path of the serial clock is output to the external SPI slave device, and the other path is supplied to the internal state machine and bit index circuit of the SPI controller as a synchronization timing reference for data transmission and reception. The SoC high-frequency master clock is divided according to the clock division factor to obtain the SPI communication serial clock. This clock signal is divided into two paths: one path is output to the chip pin and sent to the external SPI slave device as a communication synchronization beat; the other path remains inside the chip to drive state machine transitions, bit index updates, MOSI output, and MISO (master input, slave output) sampling.

[0031] Step 1034: Load the frame data to be sent from the transmission data register into the transmission buffer to obtain transmission buffer data.

[0032] Specifically, upon receiving the transmission start signal, the frame data to be sent in the transmission data register is copied to an internal transmission buffer that the CPU cannot directly access, achieving hardware isolation: during frame transmission, the content subsequently written to the transmission data register by the CPU will not overwrite the currently being transmitted frame, and the new data is buffered in the transmission data register for use in the next transmission.

[0033] In an optional embodiment of the present invention, step 104, obtaining the frame completion signal based on the serial clock, the transmit buffer data, the target register, and the received feedback data, may include: Step 1041: Generate a dynamic bit index based on the variable frame length and bit order of the target control register in the target register; Specifically, based on the variable frame length and bit order latched in the target control register of the target register, the bit index hardware counter is initialized with an initial value according to the bit order: high-order bit priority, initial value = frame length - 1; low-order bit priority, initial value = 0. With each valid edge of the serial clock, the bit index is decremented or incremented. Bits in the receive register that exceed the valid bit width are not included in data determination. The bit index serves as the index for bit selection, directly pointing to the bit position in the transmit buffer and receive data register, without using the traditional bit-by-bit shift counter for indirect processing.

[0034] Step 1042: Output the corresponding bits of the transmission buffer according to the dynamic bit index and the transmission buffer data; Specifically, at each valid edge of the serial clock, the corresponding bit of the transmit buffer data is selected based on the current bit index and output to the external SPI slave device via the MOSI pin. At the output edge of the serial clock, the hardware uses the current bit index as the address subscript to select the corresponding bit from the internal transmit buffer. This bit drives the output of the MOSI pin and is sent to the external SPI slave device. One bit is output every clock cycle.

[0035] Step 1043: Generate a frame completion signal based on the serial clock and the received feedback data.

[0036] In an optional embodiment of the present invention, step 1043 includes: The received feedback data is stored in the corresponding bit index position of the received data register. When the dynamic bit index has traversed all the valid bits of the frame length, the serial clock output is stopped and a frame completion signal is generated.

[0037] Specifically, the bits returned by the external SPI slave device on the MISO pin are synchronously sampled and stored in the corresponding bit index position of the receive data register. SPI is a full-duplex communication; while outputting 1 bit, the MISO pin level is sampled at the sampling edge of the serial clock to obtain the bit returned by the external SPI slave device. According to the current bit index, the sampled bit is written to the corresponding bit position in the receive data register. The physical bit width of the receive data register is greater than the maximum supported frame length. Meaningless residual old data may exist in the high bits of the register. Based on the variable frame length in the initialization configuration parameters, bits exceeding the frame length are masked. When the CPU reads data subsequently, it only uses the valid bits within the frame length range, and the excess bits are not included in the data judgment, thus avoiding old data from contaminating the current reception result.

[0038] When the bit index has traversed all valid bits of the frame length, the serial clock output stops and a frame completion signal is generated. The bit index is compared with the frame length boundary in real time. When the bit index has traversed all valid bit positions, it is determined that all bits of this frame have been transmitted and received. The serial clock output is turned off, no new clock pulses are generated, and a frame completion signal is generated as the end marker of this frame.

[0039] In an optional embodiment of the present invention, step 105, obtaining the interrupt request signal based on the frame completion signal and the target register, may include: Step 1051: Set the status register in the target register according to the frame completion signal, and output the set status. Specifically, based on the frame completion signal, the frame completion flag and receive valid flag bits inside the status register are set, marking the completion of this frame transmission and the existence of valid received data in the receive data register. Specifically, after receiving the frame completion signal, the frame completion flag and receive valid flag bits in the status register are set to logic 1, without requiring the CPU to issue a write register instruction. The frame completion flag indicates the completion of a frame transmission, and the receive valid flag indicates that valid slave data has been returned to the receive data register. These two bits in the status register are read-only; the CPU can only read them and cannot modify them. The set state is the status flag after it has been set.

[0040] Step 1052: Read the interrupt switch status of the target interrupt enable register in the target register. When the interrupt switch status is enabled, an interrupt request signal is obtained.

[0041] Specifically, the system reads the two sets of enable bits already configured and latched in the interrupt enable register to obtain the interrupt switch status, which is used to determine whether to output an interrupt request. If the corresponding interrupt switch is enabled, the corresponding interrupt request signal is output to the SoC's CPU. If the switch is disabled, only the status flag is retained, and no interrupt request is output. If frame completion interrupt enable = 1, a frame completion interrupt request is output; if receive valid interrupt enable = 1, a receive valid interrupt request is output. If the enable bit is equal to 0, even if the flag bit is set to 1, no interrupt will be sent to the CPU, and the CPU can only query the event by polling the status register.

[0042] At this point, the state machine remains in the end-of-transmission state, waiting for a CPU response. The transmission state machine remains in the end-of-frame state, maintaining the status register flags unchanged, waiting for the CPU to execute the receive data register read operation and enter the subsequent reset process; it will not automatically initiate the next frame transmission.

[0043] In an optional embodiment of the present invention, step 106, in response to a read operation on the receive data register, clearing the receive valid flag and resetting the transmission state machine and canceling the interrupt request signal, may include: Step 1061: Based on the set state and read instruction, read the receive data register in the target register and output the received data; Specifically, after the CPU responds to an interrupt or polls to find that the status flag is valid, it issues an access instruction to read the receive data register via the system bus, locating the hardware address of the receive data register based on the continuous address offset. The CPU then initiates a read instruction for the receive data register, outputting valid data within the frame length range of the receive data register (i.e., the received data). Redundant bits exceeding the frame length are masked and filtered out and will not be transmitted to the CPU as valid data. The CPU then obtains the service data returned by the external SPI slave device.

[0044] Step 1062: Clear the receive valid flag, reset the transmission state machine, and cancel the interrupt request signal.

[0045] Specifically, upon detecting the read strobe signal in the receive data register, the receive valid flag is automatically cleared. This clearing action is dependent on the hardware behavior of the read register and does not require the CPU to write to the clear register separately. The frame completion flag can be configured to be automatically cleared by the hardware following the read operation, or cleared by the CPU software write operation, adapting to different software architecture requirements. After the flag is cleared, the internal transmission state machine jumps back to the idle standby state, and at the same time, the interrupt request signal is canceled, waiting for the next time the CPU writes data to the transmit data register, which can then trigger a new round of frame transmission.

[0046] A specific embodiment of the SPI communication method based on a contiguous register layout according to the present invention includes: Step 111: Obtain initialization configuration data; the initialization configuration data includes at least clock polarity CPOL, clock phase CPHA, clock division factor, variable frame length, bit order, chip select channel number, and interrupt enable switch; the interrupt enable switch includes frame completion interrupt enable switch and receive valid interrupt enable switch. Clock polarity CPOL=0, clock phase CPHA=0, clock division factor=2, variable frame length code=0b10 (representing 24 bits), bit sequence=1 (high-order priority), chip select channel number is selected as CS0 channel, output low level selects the ADC slave device, interrupt enable switch is frame completion interrupt enable enabled.

[0047] Step 112: Initialize the initial register according to the initialization configuration data and the preset continuous address to obtain the configured target register; The control register is initialized and configured according to clock polarity CPOL=0, clock phase CPHA=0, clock division factor=2, variable frame length code=0b10 (representing 24 bits), and bit order=1 (highest bit priority); the chip select register is initialized and configured according to the chip select channel number CS0; the interrupt enable register is initialized and configured according to the interrupt enable switch.

[0048] Here, as Figure 2 As shown, the control registers (including enable bit, CPOL / CPHA bit, frame length bit, frequency divider bit, and bit sequence bit), status register, transmit data register, receive data register, chip select register, and interrupt enable register are arranged in a continuous address layout with no gaps, so the software driver does not need to look up tables for mapping.

[0049] Step 113: Perform data writing processing based on the frame data to be sent and the target register to obtain the serial clock and transmission buffer data; The CPU writes dummy data `0x000000` to the transmit data register (0x4000_1008), detects the write signal to the transmit register, generates a transmission start signal, wakes up the state machine, and outputs a 10MHz SCLK clock. For example... Figure 3 As shown, writing to the transmit data register starts a frame transmission. The SPI clock frequency is equal to the peripheral clock divided by twice the divider value plus one. The frame length is 2 bits, corresponding to 8 / 16 / 24 / 32-bit four levels. All four modes of CPOL / CPHA are supported. The loopback of MODE0 / 3 has been verified (simulation verification data).

[0050] Step 114: Obtain the frame completion signal based on the serial clock, the transmitted buffer data, the target register, and the received feedback data; like Figure 4 As shown, according to the high-order priority, the initial value of the bit index = frame length - 1 = 23. In each SCLK cycle, the transmitting path: take the bit pointed to by the bit index and output the dummy bit from MOSI to the ADC; the receiving path: sample the sampled bit of the ADC on MISO and store it in the position corresponding to the bit index of the receiving register. Here, the current bit index = the bit index of the previous serial clock cycle - 1, and the initial bit index = variable frame length - 1. The bit index is decremented from 23 to 0 and the last bit is completed. The completion is determined after the last bit is completed, which means that all 24 bits have been transmitted and received. The loop ends and the frame completion signal is output.

[0051] Step 115: Obtain an interrupt request signal based on the frame completion signal and the target register; Based on the frame completion signal, the frame completion flag and receive valid flag in the status register are set, and hardware interrupt requests are selectively sent to the CPU based on the interrupt enable switch status.

[0052] Step 116: In response to the read operation of the receive data register, clear the receive valid flag bit, reset the transmission state machine, and cancel the interrupt request signal.

[0053] An interrupt request signal is sent to the CPU to indicate that valid data has been received. The CPU reads the received data register according to the read instruction sent by the CPU and outputs clean received data within the valid bit width range to the CPU. Simultaneously, the valid receive flag is automatically cleared, the transmission state machine is reset, and the interrupt request signal is canceled.

[0054] The SPI communication method based on continuous register layout in this invention features a compact, hole-free register layout and concise driver code, solving the problems of scattered register layout, numerous address holes, and high software driver lookup and mapping overhead in existing SPI controllers. It supports four frame lengths (8 / 16 / 24 / 32-bit) and dual bit order, adapting to different slave protocols and addressing the issues of insufficient frame length configuration bit width and lack of support for variable frame lengths. Direct bit selection via bit index simplifies state machine implementation and increases determinism, resolving issues such as reliance on counters and shifters for bit-by-bit processing in transmission state machines, indirect bit indexing, and data corruption due to exceeding the effective bit width of the receive register. A unified frame completion interrupt and read clear mechanism reduces software polling overhead, addressing the lack of a unified interrupt / clear mechanism for frame completion and valid receive status.

[0055] The SPI communication method based on a contiguous register layout, as described in this invention, can be applied to multiple SPI controllers. These controllers have homogeneous register layouts and fixed base address intervals, allowing for reuse using a unified driver descriptor. For example... Figure 5 As shown, in the port's multi-sensor acquisition node, multiple sensor slaves (i.e., slave devices) are connected to the same SPI controller via different chip select signals. The firmware collects data from each slave device by polling frame by frame or by triggering events.

[0056] like Figure 6 As shown, an embodiment of the present invention proposes an SPI communication device 600 based on a continuous register layout, comprising: The acquisition module 601 is used to acquire initialization configuration data; the initialization configuration data includes at least clock polarity CPOL, clock phase CPHA, clock division factor, variable frame length, bit order, chip select channel number, and interrupt enable switch; the interrupt enable switch includes frame completion interrupt enable switch and receive valid interrupt enable switch; The processing module 602 is configured to initialize the initial register according to the initialization configuration data and the preset continuous address to obtain the configured target register; perform data writing processing according to the frame data to be sent and the target register to obtain the serial clock and the transmit buffer data; obtain the frame completion signal according to the serial clock, the transmit buffer data, the target register and the received feedback data; obtain the interrupt request signal according to the frame completion signal and the target register; and clear the receive valid flag bit and reset the transmission state machine and cancel the interrupt request signal in response to the read operation of the receive data register.

[0057] Optionally, the initial register is initialized according to the initialization configuration data and preset consecutive addresses to obtain the configured target register, including: The initial registers are arranged according to a preset consecutive address to obtain a consecutive register; the consecutive register includes: a control register, a status register, a transmit data register, a receive data register, a chip select register, and an interrupt enable register; The clock polarity CPOL, clock phase CPHA, clock division factor, variable frame length, and bit order from the initialization configuration data are written into the control register of the continuous register to obtain the target control register. Write the chip select channel number in the initialization configuration data into the chip select register in the consecutive registers to obtain the target chip select register; Write the interrupt enable switch from the initialization configuration data into the interrupt enable register in the continuous register to obtain the target interrupt enable register; The configured target register is obtained based on the target control register, the target chip select register, and the target interrupt enable register.

[0058] Optionally, data writing processing is performed based on the frame data to be transmitted and the target register to obtain the serial clock and transmit buffer data, including: Obtain the frame data to be sent; Write the frame data to be sent into the transmission data register in the target register; Based on the clock division coefficient of the target control register in the target register, a serial clock of the corresponding frequency is output; wherein, the serial clock is divided into two paths, one as the synchronization clock for data transmission and the other as the sampling timing. The frame data to be sent in the transmit data register is loaded into the transmit buffer to obtain transmit buffer data.

[0059] Optionally, a frame completion signal is obtained based on the serial clock, the transmitted buffer data, the target register, and the received feedback data, including: A dynamic bit index is generated based on the variable frame length and bit order of the target control register in the target register; Based on the dynamic bit index and the transmission buffer data, output the corresponding bits of the transmission buffer; A frame completion signal is generated based on the serial clock and the received feedback data.

[0060] Optionally, a frame completion signal is generated based on the serial clock and the received feedback data, including: The received feedback data is stored in the corresponding bit index position of the received data register. When the dynamic bit index has traversed all the valid bits of the frame length, the serial clock output is stopped and a frame completion signal is generated.

[0061] Optionally, an interrupt request signal is obtained based on the frame completion signal and the target register, including: The status register in the target register is set according to the frame completion signal, and the set status is output. Read the interrupt switch status of the target interrupt enable register in the target register. When the interrupt switch status is enabled, an interrupt request signal is obtained.

[0062] Optionally, in response to a read operation of the receive data register, clearing the receive valid flag and resetting the transmission state machine and canceling the interrupt request signal includes: Based on the set state and read instruction, read the receive data register in the target register and output the received data; Clear the receive valid flag, reset the transmission state machine, and cancel the interrupt request signal.

[0063] It should be noted that this device corresponds to the method described above, and all implementations in the method embodiments described above are applicable to the embodiments of this device and can achieve the same technical effect. Further details are omitted in this embodiment.

[0064] This invention also provides a computing device, including: a processor and a memory storing a computer program. When the processor executes the computer program, it performs the method described in the above embodiments. All implementations in the above method embodiments are applicable to the embodiments of this device and can achieve the same technical effects. Further details are omitted in this embodiment.

[0065] This invention also provides a computer-readable storage medium storing instructions that, when executed on a computer, cause the computer to perform the methods described in the above embodiments. All implementations in the above method embodiments are applicable to the embodiments of this device and can achieve the same technical effects. Further details are omitted in this embodiment.

[0066] It should be noted that in the apparatus and method of the present invention, the components or steps can obviously be decomposed and / or recombined. These decompositions and / or recombinations should be considered equivalent solutions of the present invention. Furthermore, the steps for performing the above series of processes can naturally be performed in the order described and in chronological order, but are not necessarily required to be performed in chronological order. Some steps can be performed in parallel, overlapping, or independently of each other.

[0067] It should be noted that in the above embodiments, 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. Without further limitations, 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. Furthermore, it should be noted that the scope of the methods and apparatuses in the embodiments described above is not limited to performing functions in the order shown or discussed, but may also include performing functions substantially simultaneously or in the reverse order, depending on the functions involved. For example, the described methods may be performed in a different order than described, and various steps may be added, omitted, or combined. Additionally, features described with reference to certain examples may be combined in other examples.

[0068] The above description represents the preferred embodiments of the present invention. It should be noted that those skilled in the art can make various improvements and modifications without departing from the principles of the present invention, and these improvements and modifications should also be considered within the scope of protection of the present invention.

Claims

1. An SPI communication method based on a contiguous register layout, characterized in that, include: Get initial configuration data; The initialization configuration data includes at least clock polarity CPOL, clock phase CPHA, clock division factor, variable frame length, bit order, chip select channel number, and interrupt enable switch; The interrupt enable switch includes a frame completion interrupt enable switch and a receive valid interrupt enable switch; The initial register is initialized and configured according to the initialization configuration data and the preset consecutive addresses to obtain the configured target register. Data writing is performed based on the frame data to be transmitted and the target register to obtain the serial clock and transmit buffer data; The frame completion signal is obtained based on the serial clock, the transmitted buffer data, the target register, and the received feedback data; An interrupt request signal is obtained based on the frame completion signal and the target register; In response to a read operation on the receive data register, the receive valid flag is cleared, the transmission state machine is reset, and the interrupt request signal is canceled.

2. The SPI communication method based on a contiguous register layout according to claim 1, characterized in that, The initial register is initialized and configured according to the initialization configuration data and the preset consecutive addresses to obtain the configured target register, including: The initial registers are arranged according to a preset consecutive address to obtain a consecutive register; the consecutive register includes: a control register, a status register, a transmit data register, a receive data register, a chip select register, and an interrupt enable register; The clock polarity CPOL, clock phase CPHA, clock division factor, variable frame length, and bit order from the initialization configuration data are written into the control register of the continuous register to obtain the target control register. Write the chip select channel number in the initialization configuration data into the chip select register in the consecutive registers to obtain the target chip select register; Write the interrupt enable switch from the initialization configuration data into the interrupt enable register in the continuous register to obtain the target interrupt enable register; The configured target register is obtained based on the target control register, the target chip select register, and the target interrupt enable register.

3. The SPI communication method based on a contiguous register layout according to claim 1, characterized in that, Data writing is performed based on the frame data to be transmitted and the target register to obtain the serial clock and transmit buffer data, including: Obtain the frame data to be sent; Write the frame data to be sent into the transmission data register in the target register; Based on the clock division coefficient of the target control register in the target register, a serial clock of the corresponding frequency is output; wherein, the serial clock is divided into two paths, one as the synchronization clock for data transmission and the other as the sampling timing. The frame data to be sent in the transmit data register is loaded into the transmit buffer to obtain transmit buffer data.

4. The SPI communication method based on a contiguous register layout according to claim 3, characterized in that, Based on the serial clock, the transmitted buffer data, the target register, and the received feedback data, a frame completion signal is obtained, including: A dynamic bit index is generated based on the variable frame length and bit order of the target control register in the target register; Based on the dynamic bit index and the transmission buffer data, output the corresponding bits of the transmission buffer; A frame completion signal is generated based on the serial clock and the received feedback data.

5. The SPI communication method based on a contiguous register layout according to claim 4, characterized in that, Based on the serial clock and the received feedback data, a frame completion signal is generated, including: The received feedback data is stored in the corresponding bit index position of the received data register. When the dynamic bit index has traversed all the valid bits of the frame length, the serial clock output is stopped and a frame completion signal is generated.

6. The SPI communication method based on a contiguous register layout according to claim 1, characterized in that, Based on the frame completion signal and the target register, an interrupt request signal is obtained, including: The status register in the target register is set according to the frame completion signal, and the set status is output. Read the interrupt switch status of the target interrupt enable register in the target register. When the interrupt switch status is enabled, an interrupt request signal is obtained.

7. The SPI communication method based on a contiguous register layout according to claim 6, characterized in that, In response to a read operation on the receive data register, clearing the receive valid flag, resetting the transmission state machine, and canceling the interrupt request signal include: Based on the set state and read instruction, read the receive data register in the target register and output the received data; Clear the receive valid flag, reset the transmission state machine, and cancel the interrupt request signal.

8. An SPI communication device based on a contiguous register layout, characterized in that, include: The acquisition module is used to obtain initialization configuration data; The initialization configuration data includes at least clock polarity CPOL, clock phase CPHA, clock division factor, variable frame length, bit order, chip select channel number, and interrupt enable switch; The interrupt enable switch includes a frame completion interrupt enable switch and a receive valid interrupt enable switch; The processing module is used to initialize the initial register according to the initialization configuration data and the preset continuous address to obtain the configured target register; perform data writing processing according to the frame data to be sent and the target register to obtain the serial clock and the transmit buffer data; and obtain the frame completion signal according to the serial clock, the transmit buffer data, the target register and the received feedback data. An interrupt request signal is obtained based on the frame completion signal and the target register; in response to a read operation on the receive data register, the receive valid flag is cleared, the transmission state machine is reset, and the interrupt request signal is canceled.

9. A computing device, characterized in that, include: A processor, a memory storing a computer program, wherein the computer program, when executed by the processor, performs the method as described in any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that, It stores instructions that, when executed on a computer, cause the computer to perform the method as described in any one of claims 1 to 7.