SPI communication (SPI)

The tutorial demonstrates how to use SPI to read the key values of a 2.4G wireless controller and print them through the serial port.

Hardware connection

image-20240825205618171

Since we have configured a special connection line, we only need to install it to the corresponding interface:

PeripheralsDevelopment Board
PS2: CSPB12
PS2: SCKPB13
PS2: MISOPB14
PS2: MOSIPB15

Control Principle

SPI (Serial Peripheral Interface) is a high-speed, full-duplex, synchronous serial communication interface that is commonly used to transmit data between microcontrollers, sensors, and external devices.

SPI PinFunction
MISOMaster data output line, sends data from the master to the slave
MOSIMaster data input line, transmits data from the slave to the master
SCLKClock signal line, used for timing control of synchronous data transmission
CSSlave select line, used to select a specific slave device to communicate with the master

Clock polarity (CPOL): controls the idle and active states of the clock signal (0: idle state low level, 1: idle state high level)

Clock phase (CPHA): controls the sampling and transmission time of data (0: odd transition edge sampling, 1: even transition edge sampling)

Clock polarity (CPOL)Clock phase (CPHA) 
00SCL idle state low level; data is sampled on the rising edge and transmitted on the falling edge
01SCL idle state low level; data is sampled on the falling edge and transmitted on the rising edge
10SCL idle state high level; data is sampled on the falling edge and transmitted on the rising edge
11SCL idle state high level; data is sampled on the rising edge and transmitted on the falling edge

image-20240903163539136

The 2.4G wireless handle is mainly composed of a handle and a receiver (the receiver adapter board is convenient for pin connection with the development board).

Handle

Send key information to the receiver.

Receiver

Receive the data sent by the handle and pass it to the development board; the development board can also send data through the handle to configure the handle's sending mode.

image-20240807212212404

Single light mode: one controller indicator light is on

Dual light mode: two controller indicator lights are on

image-20240819100651601

Controller ButtonProgram Function DescriptionProgram Definition (Part)
Direction Key (①): △ForwardPSB_PAD_UP
Direction Key (①): ▽BackwardPSB_PAD_DOWN
Direction Key (①): ◁Turn LeftPSB_PAD_LEFT
Direction Key (①): ▷Turn RightPSB_PAD_RIGHT
Function Key (②): ▲ForwardPSB_TRIANGLE/PSB_GREEN
Function Key (②): ×BackwardPSB_CROSS/PSB_BLUE
Function key (②): ☐Turn leftPSB_SQUARE/PSB_PINK
Function key (②): ◯Turn rightPSB_CIRCLE/PSB_RED
Joystick: ③Control direction (valid in dual-light mode)PSB_L3/PSS_LX/PSS_LY
Joystick: ④Control direction (valid in dual-light mode)PSB_R3/PSS_RX/PSS_RY
Button (⑤): L1/L2AcceleratePSB_L1/PSB_L2
Button (⑥): R1/R2DeceleratePSB_R1/PSB_R2
Button (⑦): STARTTurn off power saving modePSB_START
Button (⑧): MODESwitch mode (indicator light display: single light and dual light mode) 
Key (⑨): SELECTUnusedPSB_SELECT
Receiver pinDescription
DI/DATSignal flow, from handle to host, this signal is an 8-bit serial data, synchronously transmitted on the falling edge of the clock. The signal is read when the clock changes from high to low.
DI/DATSignal flow, from host to handle, this signal is relative to DI, the signal is an 8-bit serial data, synchronously transmitted on the falling edge of the clock.
NCEmpty port.
GNDReceiver working power supply, power supply range 3~5V.
VDDUsed to provide handle trigger signal. During communication, it is at low level.
CS/SELSignal flow, from host to handle, this signal is relative to DI, the signal is an 8-bit serial data, synchronously transmitted on the falling edge of the clock.
CS/SELClock signal, sent by the host, used to keep data synchronized.
NCEmpty port.
ACKAcknowledgement signal from the controller to the host. This signal goes low at the last cycle of each 8-bit data transmission and CS remains low. If the CS signal does not go low, the PS host will try another peripheral in about 60 microseconds. The ACK port is not used during programming. (Can be ignored)

image-20240807214832385

Software configuration

Pin definition

Main control chipPinMain function (after reset)Default multiplexing functionRedefine function
STM32F103RCT6PB12PB12SPI2_NSS/I2C2_SMBAI/USART3_CK/TIM1_BKIN 
STM32F103RCT6PB13PB13SPI2_SCK/USART3_CTS/ TIM1_CH1N 
STM32F103RCT6PB14PB14SPI2_MISO/USART3_RTS/TIM1_CH2N 
STM32F103RCT6PB15PB15SPI2_MOSI/TIM1_CH3N 

Software code

Since the default function of the pin is the ordinary IO pin function, we need to use the multiplexing function.

Control function

The tutorial only briefly introduces the code, you can open the project source code to read it in detail.

PS2_Init

PS2_Cmd

PS2_RedLight

PS2_ReadData

PS2_DataKey

PS2_AnologData

PS2_ClearData

PS2_Vibration

PS2_ShortPoll

PS2_EnterConfing

PS2_TurnOnAnalogMode

PS2_VibrationMode

PS2_ExitConfing

PS2_SetInit

Experimental phenomenon

The SPI.hex file generated by the project compilation is located in the OBJ folder of the SPI project. Find the SPI.hex file corresponding to the project and use the FlyMcu software to download the program to the development board.

After the program is successfully downloaded: the serial port prints the key value pressed by the wireless controller.

image-20240827173648504