cancel
Showing results for 
Search instead for 
Did you mean: 

SPI_I2S_FLAG_TXE flag doesn't clear, SPI init done right?

geoff239955_st
Associate II
Posted on September 13, 2012 at 23:40

I'm tring to set up SPI2 as a master SPI device to communicate with another device.  After configuring the clocks, GPIO and SPI device, I wait for SPI_I2S_FLAG_TXE to clear before writing.  But the flag never clears, so I'm stuck.  I took this code as close as possible from the examples included with the peripheral library.  I'm not sure if I'm missing any other setup steps.

static void InitSPI( void )

{

   GPIO_InitTypeDef GPIO_InitStructure;

   SPI_InitTypeDef  SPI_InitStructure;

  

   // PCLK2 = HCLK / 2

   RCC_PCLK2Config(RCC_HCLK_Div2);

  

   // Enable GPIO clock for SPI2

   RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);

   RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );

   //

   //Allocate GPIO pins to SPI interface

   //

   //Initialize GPIO config struct

   GPIO_StructInit(&GPIO_InitStructure);  

  

   //Pins B13 and B15

   GPIO_InitStructure.GPIO_Pin = ( GPIO_Pin_13 | GPIO_Pin_15 );

   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

   GPIO_Init(GPIOB, &GPIO_InitStructure);

   //Pins B14

   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;

   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

   GPIO_Init(GPIOB, &GPIO_InitStructure);

  

   //

   //Configure SPI bus

   //

  

   //Initialize SPI config struct

   SPI_StructInit(&SPI_InitStructure);

   //Set SPI config struct

   SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

   SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

   SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;

   SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; // idle logic low

   SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; // capture on falling edge

   SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

   SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;

   SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

   SPI_InitStructure.SPI_CRCPolynomial = 7;

   SPI_Init(SPI2, &SPI_InitStructure);

   // Enable SPI_MASTER NSS output for master mode

   SPI_SSOutputCmd(SPI2, ENABLE);

  

   // Enable SPI_MASTER

   SPI_Cmd(SPI2, ENABLE);

} The problem occurs just before the read:

  //Wait for SPI2 Tx buffer empty

   while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);

  

   //Send first byte to start read

   SPI2->DR = SPI_CMD_READ;

#stm32-spi
2 REPLIES 2
Posted on September 14, 2012 at 00:49

TXE goes high when the holding buffer is empty.

When initially writing DR, expect it to go low briefly, then high again as the first byte moves from the holding register to the shift register, subsequent bytes will see it remain low for longer, typically 8 or 16 bit times depending on the settings.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on September 14, 2012 at 10:11

Clive,

Geoff is apparently aware of the ''inverse'' nature of the TXE bit (although the ''wait for clear'' wording is misleading indeed):

> //Wait for SPI2 Tx buffer empty

>    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);

Geoff,

Did you switch on the APB1 clock?

JW