cancel
Showing results for 
Search instead for 
Did you mean: 

SPI continuous transmfer problems

hsinan_hou
Associate
Posted on March 17, 2014 at 09:20

dear sir, 

I use the STM32F4 uP to control peripheral IC by SPI protocol.

And I composed the source code according to the the reference manual (Doc ID 018909 Rev 5). However, the data is invalid after SPI_I2S_FLAG_RXNE asserted. The data is valid after more than 1us. 

By a scope, the peripheral IC sends the data correctly. 

It seem not accordant with the the figure as the following. 

0690X000006056xQAA.png

Would you help me to figure out how to access the SPI correctly? 

Best Regards 

Shine Hou 

attached information 

Device: STM32F407IG

IDE: KeilC 

source code: 

1. SPI initialization 

{

  GPIO_InitTypeDef GPIO_InitStruct;

  SPI_InitTypeDef  SPI_InitStruct;

  // NVIC_InitTypeDef NVIC_InitStruct;  

  /* Peripheral Clock Enable -------------------------------------------------*/

  /* Enable the SPI clock (Port B) */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);  

  

  /* Enable GPIO clocks (Port B) */

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); 

  /* SPI GPIO Configuration --------------------------------------------------*/

  /* GPIO Deinitialisation */

  GPIO_DeInit(GPIOB);

  

  /* Connect SPI (SCK & MISO & MOSI) pins to AF5 */  

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2); // SPI2_SCK

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2); // SPI2_MISO

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2); // SPI2_MOSI

  

  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStruct.GPIO_PuPd  = GPIO_PuPd_DOWN;

  

  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;

  GPIO_Init(GPIOB, &GPIO_InitStruct);

  

  /* SPI CS1 pin configuration */

  GPIO_InitStruct.GPIO_Pin = SPI_CS_PIN; 

  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;

  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;

  GPIO_Init(GPIOB, &GPIO_InitStruct);

  

  GPIO_SetBits(GPIOB, GPIO_InitStruct.GPIO_Pin ); 

 

  /* SPI configuration -------------------------------------------------------*/

  /* SPI Deinitialisation */

  SPI_I2S_DeInit(SPI2);

  

  SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

  SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;

  SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;

  SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;

  SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;

  SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;

  SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;

  SPI_InitStruct.SPI_CRCPolynomial = 7;

  

  SPI_InitStruct.SPI_Mode = SPI_Mode_Master; // Master Mode

  SPI_Init(SPI2, &SPI_InitStruct);

  

  SPI_Cmd(SPI2, ENABLE);

}

2. SPI Read 

          GPIO_ResetBits(GPIOB, SPI_CS_PIN);             

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

          SPI_I2S_SendData(SPI2, reg_addr | 0x80);          

         

          for (n1=0; n1<reg_num; n1++ )

          {

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

            SPI_I2S_SendData(SPI2, 0x00);                        

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

            reg_rdata[n1] = SPI_I2S_ReceiveData(SPI2);

          }

          GPIO_SetBits(GPIOB, SPI_CS_PIN);             

#stm32f4
1 REPLY 1
francescatodiego
Associate II
Posted on March 17, 2014 at 18:51

Try with this

SPI Read 
GPIO_ResetBits(GPIOB, SPI_CS_PIN); 
GPIO_ResetBits(GPIOB, SPI_CS_PIN); // repeat CS select if you want add delay before start read 
GPIO_ResetBits(GPIOB, SPI_CS_PIN); 
// here CS ok
n1 = 0 ;
while (n1 < reg_num)
{
if (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) != RESET) 

{
SPI_I2S_SendData(SPI2, 0x00); 
}
if (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) != RESET) 
 { 
reg_rdata[n1++] = SPI_I2S_ReceiveData(SPI2);
 } 
}
GPIO_SetBits(GPIOB, SPI_CS_PIN);