2025-08-26 11:54 AM
I am having a problem reading SPI data on an STM32F407 where it always returns 0x00. I actually have this working using HAL on the same hardware, however for legacy reasons I need to get this working with SPL. This application is just sending clock signals to the slave and reading the received data. There is no activity on MOSI.
I can scope the signals and see the expected traffic on the SCK and MISO so I at least know my clock transmit is working. I just can grab the correct read data. Here is my code. I feel like I'm missing something basic for SPL. Any insight would be helpful.
#include "main.h"
#include "stm32f4xx_spi.h"
#include "stm32f4xx_usart.h"
#include "config.h"
#include "board_init.h"
#include "gpio.h"
#define TIMEOUT_TIME 1000;
uint16_t SPI_TransmitReceiveByte(uint16_t data);
uint16_t spiData;
void main()
{
uint32_t loopCounter = 0;
SystemInit();
init_IO();
SPI3_Init();
powerOnLED();
while (1)
{
loopCounter++;
if (loopCounter == 10000)
{
spiData = SPI_TransmitReceiveByte(0x0000);
loopCounter = 0;
if (spiData != 0) powerOffLED();
}
}
}
uint16_t SPI_TransmitReceiveByte(uint16_t data) {
uint32_t timeout = TIMEOUT_TIME;
uint8_t dummy;
timeout = TIMEOUT_TIME;
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) != RESET)
{
dummy = *(volatile uint8_t *)&SPI3 -> DR;
}
timeout = TIMEOUT_TIME;
while ((SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET) & (timeout != 0)) {
timeout--;
}
SPI_SendData(SPI3, data);
timeout = TIMEOUT_TIME;
while ((SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == RESET) & (timeout != 0)) {
timeout--;
}
return SPI_ReceiveData(SPI3);
}
void SPI3_Init()
{
GPIO_InitTypeDef spi3_io;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
// Configure SPI pins (SCK, MISO, MOSI)
// Example for SPI3 on PC10, PC11, PC12
spi3_io.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_12 | GPIO_Pin_12;
spi3_io.GPIO_Mode = GPIO_Mode_AF;// Alternate Function
spi3_io.GPIO_OType = GPIO_OType_PP;
spi3_io.GPIO_PuPd = GPIO_PuPd_DOWN;
spi3_io.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &spi3_io);
// Connect SPI pins to alternate function
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_SPI3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_SPI3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_SPI3);
SPI_DeInit(SPI3); // De-initialize SPI3 first
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; // Clock Polarity (adjust based on slave device)
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; // Clock Phase (adjust based on slave device)
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; // Software NSS control
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32; // Adjust for desired speed
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7; // CRC polynomial (optional)
SPI_Init(SPI3, &SPI_InitStructure);
SPI_Cmd(SPI3, ENABLE); // Enable SPI3
}
Solved! Go to Solution.
2025-08-26 12:17 PM
Please ignore. I found the error in the SPI pin setup shortly after this post. PC11 was not setup properly.
2025-08-26 12:17 PM
Please ignore. I found the error in the SPI pin setup shortly after this post. PC11 was not setup properly.