I'm using the STM32F4 trying to read over SPI. Debug environments is Visual Studio with the VisualGDB plugin. The spi config is as follows. 8-bit frames, MSB first, using GPIOA/B for the SPI signals
void spiInit() {
RCC->AHB1ENR |= 3; //enable gpioA/B clock
RCC->APB2ENR |= 0x1000; //enable SPI1 clock (bit 12)
// configure A5 for SCLK
GPIOA->MODER &= ~0x00000C00; //clear pin mode for pin 5
GPIOA->MODER |= 0x00000800; //alternate function mode for pin 5
GPIOA->AFR[0] &= ~0x00F00000; //clear alternate mode
GPIOA->AFR[0] |= 0x00500000; //setup pins 5 for AF5
// configure B4 for MISO
GPIOB->MODER &= ~0x00000300; //clear pin mode for pin 4
GPIOB->MODER |= 0x00000200; //alternate function mode for pin 5
GPIOB->AFR[0] &= ~0x000F0000; //clear alternate mode
GPIOB->AFR[0] |= 0x00050000; //setup pins 4 for AF5
// configure B5 for MOSI
GPIOB->MODER &= ~0x00000C00; //clear pin mode for pin 5
GPIOB->MODER |= 0x00000800; //alternate function mode for pin 5
GPIOB->AFR[0] &= ~0x00F00000; //clear alternate mode
GPIOB->AFR[0] |= 0x00500000; //setup pins 5 for AF5
// configure A15 for output SS
GPIOA->MODER &= ~0xC0000000; //clear PA15 function bits
GPIOA->MODER |= 0x40000000; //set PA15 (SS) as output
SPI1->CR1 = 0x31E; //8-bit frames
SPI1->CR2 = 0; //mostly read/status/interrupt config
SPI1->CR1 |= 0x40; //enable SPI1
GPIOA->BSRR = 0x00008000; //deassert SS
}
Writes are fine, data is loaded correctly to slave and it responds as expected, reads from the slave however are not working. I can see that MISO looks good on the bus when i look at the waveforms, but it's not picked up by the Master.
After a few attempts at watching the SPI status-register flags, (getting the results described above) I'm using a utility function that i saw on another SO question (here). The data always reads 0 and i am confused, the code waits for the receive buffer to fill, but there seems to nothing there when it is read.
uint8_t SPI_ReadWrite8(SPI_TypeDef *spi, uint8_t data)
{
while(!(spi -> SR & SPI_SR_TXE)); //Wait for tx buffer to empty
*(volatile uint8_t *)&spi->DR = data; //Send data
while (!(spi -> SR & SPI_SR_RXNE)); //Wait for rx buffer to fill
return *(volatile uint8_t *)&spi->DR;
}
Any ideas what i'm missing?
User contributions licensed under CC BY-SA 3.0