SPI Polling Example
Source path: example/hal/spi/polling
Supported Boards
sf32lb52-lcd_n16r8sf32lb58-lcd_n16r64n4sf32lb57-lcdsf32lb56-lcd
Overview
This example demonstrates SPI HAL in blocking polling mode with self loopback verification.
Core behavior:
SPI master mode, 2Lines, 8-bit, default MODE0.
Periodic
HAL_SPI_TransmitReceive()transactions.Per-round 16-byte incremental TX pattern (
seed + i) and mismatch counting.
Build and Download
For sf32lb52-lcd_n16r8, run in project directory:
scons --board=sf32lb52-lcd_n16r8 -j8
For sf32lb58-lcd_n16r64n4:
scons --board=sf32lb58-lcd_n16r64n4 -j8
After build, enter the generated build_* directory and run the download script.
Hardware Setup
Single-board loopback (recommended)
Short SPI1 or SPI2 MOSI and MISO:
Board Name |
MOSI |
MISO |
CLK |
CS |
|---|---|---|---|---|
sf32lb52-lcd |
PA24 |
PA25 |
PA28 |
PA29 |
sf32lb58-lcd |
PA21 |
PA20 |
PA28 |
PA29 |
sf32lb57-lcd |
PA37 |
PA38 |
PA39 |
PA40 |
sf32lb56-lcd |
PA64 |
PA69 |
PA73 |
PA71 |
Notes:
This example uses one-shot
HAL_SPI_TransmitReceiveper round; no manualTAKE/RELEASE CSis used.Without loopback wiring,
mismatchis usually high or data appears random.
Expected Logs
Startup logs:
Start spi polling loopback demo!
tip: short SPI1/SPI2 MOSI(DIO/DO) to MISO(DI) for loopback verification.
Runtime logs:
round=0 ret=0 mismatch=0 tx: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f rx: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
round=1 ret=0 mismatch=0 tx: 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 rx: 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10
Field meanings:
ret: HAL return code,0meansHAL_OK.mismatch: number of TX/RX mismatched bytes in current 16-byte frame.round: loop counter.
Key Configuration Flow
Configure SPI1 pinmux for
DIO/DO,DI,CLK,CS.Enable SPI1 clock:
HAL_RCC_EnableModule(RCC_MOD_SPI1).Initialize SPI:
Direction = SPI_DIRECTION_2LINESMode = SPI_MODE_MASTERDataSize = SPI_DATASIZE_8BITCLKPhase/CLKPolarityselected bySPI_MODE
Loop on
HAL_SPI_TransmitReceive()and print comparison result.
Troubleshooting
1) ret != 0
Check SPI1 pinmux settings.
Check SPI clock and system init status.
2) mismatch stays non-zero
Check MOSI↔MISO jumper quality first.
Check pin mapping and contact reliability.
Try lower SPI clock for signal integrity check.
3) No periodic logs
Confirm code reaches
spi_test().Confirm UART console output is working.
Reference Documents
EH-SF32LB52X_Pin_config_V1.3.0_20231110.xlsxDS0052-SF32LB52x-芯片技术规格书 V0p3.pdf
Update Log
Version |
Date |
Notes |
|---|---|---|
0.1.0 |
03/2026 |
Switched to polling loopback self TX/RX demo |