2014-08-14 02:44 PM
I am writing an SPI interface where after 4 messages are sent I toggle a pin. My issue is my pin is toggling before the transmission is complete(I can see my pin go high after the 3rd message). I am monitoring the bsy and the tx flags but not getting the results I was expecting. Any help is appreciated. I am using an STM32F103.
LCD_Write_Command(0x60 | tmp);LCD_Write_Command(0x70 | tmp);LCD_Write_Command(0x00 | tmp);LCD_Write_Command(0x10 | tmp);GPIO_WriteBit(GPIOB,LCD_CD,Bit_SET);voidLCD_Write_Command(unsigned char dat){ while ((SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == 1)); SPI_I2S_SendData(SPI1, dat); while ((SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == 0));} #spi #stm32103
2014-08-14 04:00 PM
The SPI is buffered so you can't rely on just the SR status register or the TXNE bit. But you can take advantage of how the underlying SPI mechanism works to determine when the last bit has been shifted out and the SPI is done.
Since SPI is bi-directional you need to check the RXE flag and clear it after every byte is sent out. The RXE tells you that the input was clocked in and completed. You need to do this for every byte sent. After the last byte of the message, the final RXE bit will tell you the data has also been clocked out and is complete. Then the SPI is done. If you happen to clock in data before clock out (a rare setup) you have to allow for one bit time before the SPI is done. Jack Peacock