Question
SPI timing issues on STM32F103
Posted on May 25, 2012 at 03:36
I'm having some issues with the SPI on the STM32 and I can't seem to figure out what the problem is. I'm having issues on both the TX and RX directions. I'm using it in master mode in both cases. I have multiple devices hanging onto the SPI bus so I'm using GPIO under software control slave selects.
A) In unidirectional TX mode I do the following: 1) Set RCC->APB2ENR to 1 on bits 12 and 0 to enable the SPI1 clock and enable the SPI. 2) Fill a structure typedef'ed by SPI_InitTypeDef i.e. master, 16 bits, TX only, etc. 3) SPI_Init(SPI1, &SPI_typedef); 4) SPI_Cmd(SPI1, ENABLE); 5) Reset a GPIO line low for the software controlled slave select 6) SPI_I2S_SendData(SPI1, (u16)(MyOutputData)); 7) while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)==RESET); //wait for TXE = 1 8) Set the GPIO line back high for the software controlled slave select. 9) SPI_Cmd(SPI1, DISABLE); Now everything seems fine except for the fact that when I look at the SCLK versus the GPIO slave select on a scope, I see that the slave select goes back high before the SCLK finishes the transaction. I thought that by waiting for TXE to become set, the software slave select would only be able to go back high after the transaction was completed? What am I missing? In order to get things to look right, I have to insert a short software delay before retting the slave select back high. B) In unidirectional RX mode I do the following: 1) Set RCC->APB2ENR to 1 on bits 12 and 0 to enable the SPI1 clock and enable the SPI. 2) Fill a structure typedef'ed by SPI_InitTypeDef. 16 bits, RX only, etc 3) SPI_Init(SPI1, &SPI_typedef); 4) Reset a GPIO line low for the software controlled slave select 5) SPI_Cmd(SPI1, ENABLE); 6) while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)==SET); 7) MyInData = SPI_I2S_ReceiveData(SPI1)); 8) Set the GPIO line high for the software controlled slave select SPI_Cmd(SPI1, DISABLE); Now in this case, several odd things are happening. Again the slave select is going high before the transaction is complete. But the main issue is that I have set SPI_InitTypeDef.SPI_DataSize = SPI_DataSize_16b so I'm expecting there to be only 16 clocks in the transaction. But instead I'm seeing 48 clocks. Why is that happening? Thanks! #spi-stm32