STM32F3 and STM32F4 SPI Problems.
When porting one of my projects from the F1 series to the F3 series, I ran into a problem: SPI frames were always 16 bits long, but I needed 8. Interestingly, the F1 series has a single DFF (Data frame format) bit for setting the SPI frame length: when it is cleared the frame is 8 bits, when set, 16. In the F3 series a 4-bit DS (Data Size) field was allocated for this, and the frame length can be set anywhere from 4 to 16 bits.
But no matter how I changed the value of the DS bit field, the frames still remained 16-bit. I should probably explain what 16-bit frames mean here. If you connect an oscilloscope to the SPI_SCK pin, then when sending/receiving data you'll see 16 pulses on the screen, and data is transferred on the edges that form each pulse.
There is no information about this in RM0316, but I managed to find it on one of the forums. The frame width depends on how the DR register is accessed.
To make the frames 8-bit, the DR register must be accessed as an 8-bit register.
In addition, F3-series MCUs let you set the threshold for the RXNE (Receive buffer not empty) flag. The FRXTH (FIFO reception threshold) bit is responsible for this: if it is set, the RXNE flag is set when there are 8 bits or more in the FIFO. If it is cleared, 16 bits or more. In short, for 8-bit mode it must be set.
And finally, SPI initialization in 8-bit mode for the STM32F303VC.
As for the F4 series, the bug there is that SPI needs a 100ms delay to start. Otherwise the DR register always reads zero.
But no matter how I changed the value of the DS bit field, the frames still remained 16-bit. I should probably explain what 16-bit frames mean here. If you connect an oscilloscope to the SPI_SCK pin, then when sending/receiving data you'll see 16 pulses on the screen, and data is transferred on the edges that form each pulse.
There is no information about this in RM0316, but I managed to find it on one of the forums. The frame width depends on how the DR register is accessed.
To make the frames 8-bit, the DR register must be accessed as an 8-bit register.
#define SPI2_DR_8bit *(__IO uint8_t*)&(SPI2->DR)
In addition, F3-series MCUs let you set the threshold for the RXNE (Receive buffer not empty) flag. The FRXTH (FIFO reception threshold) bit is responsible for this: if it is set, the RXNE flag is set when there are 8 bits or more in the FIFO. If it is cleared, 16 bits or more. In short, for 8-bit mode it must be set.
And finally, SPI initialization in 8-bit mode for the STM32F303VC.
#define SPI2_DR_8bit *(__IO uint8_t*)&(SPI2->DR)
void spi_init(void)
{
//Enable port B clocking
RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
//pinout
//PB13 - SCL - Alternative func. push-pull - OUT
//PB14 - MISO - Alternative func. push-pull - OUT - IN
//PB15 - MOSI - Alternative func. push-pull - OUT
//PB12 - CS - GPIO - soft - OUT
//pins 13, 14, 15 - alternate function,
GPIOB->MODER |= GPIO_MODER_MODER13_1 | GPIO_MODER_MODER14_1 | GPIO_MODER_MODER15_1;
GPIOB->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR13 | GPIO_OSPEEDER_OSPEEDR15;
GPIOB->PUPDR &= ~(GPIO_PUPDR_PUPDR13 | GPIO_PUPDR_PUPDR15);
//assign the required alternate functions to the pins
GPIOB->AFR[1] |= (0x05<<5*4);
GPIOB->AFR[1] |= (0x05<<6*4);
GPIOB->AFR[1] |= (0x05<<7*4);
// pin 12 - output with pull-up
GPIOB->MODER |= GPIO_MODER_MODER12_0;
GPIOB->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR12;
GPIOB->PUPDR |= GPIO_PUPDR_PUPDR12_0;
//configure SPI2
RCC->APB1ENR |= RCC_APB1ENR_SPI2EN; //Enable oscil SPI2
SPI2->CR1 |= SPI_CR1_BR; //Baud rate = Fpclk/256
SPI2->CR1 |= SPI_CR1_CPOL; //Polarity cls signal CPOL = 0;
SPI2->CR1 |= SPI_CR1_CPHA; //Phase cls signal CPHA = 0;
SPI2->CR1 &= ~SPI_CR1_LSBFIRST; //MSB will be first
SPI2->CR1 |= SPI_CR1_SSM; //Program mode NSS
SPI2->CR1 |= SPI_CR1_SSI; //same as the state when NSS is 1
SPI2->CR1 |= SPI_CR1_MSTR; //Mode Master
SPI2->CR1 |= SPI_CR1_SPE; //Enable SPI2
SPI2->CR2 |= SPI_CR2_FRXTH; //RXNE 8 bit
SPI2->CR2 |= SPI_CR2_DS_0 | SPI_CR2_DS_1 | SPI_CR2_DS_2; //Data_Size
}
uint8_t spi_txrx(uint8_t data)
{
SPI2_DR_8bit = data;
while ((SPI2->SR & SPI_SR_RXNE) == 0);
return SPI2_DR_8bit;
}
As for the F4 series, the bug there is that SPI needs a 100ms delay to start. Otherwise the DR register always reads zero.
