cancel
Showing results for 
Search instead for 
Did you mean: 

SPI STM32f407 problem

mone6980
Associate
Posted on October 26, 2016 at 21:07

Hello everyone. I am having problem with SPI inerface and stm32f4 development board. I am sending 16 bits of information. While the bis are sending the CS must be low and when they are sent must be high. Then goes low to send the other 16 bits. The problem is that CS is getting low earlier than expected( there is a time that is low for no reason). Well as you can see in the pictures the problem is with the sclk. When i increase the prescaler value the clock is ok ( no low state at all), but then the CS goes low in wrong times and very frequently. I think tha something goes wrong with the SPI_I2S_ITConfig( SPI2, SPI_I2S_IT_RXNE, ENABLE ); but I don;t know what or why . Here is my code 

#include <stm32f4xx.h>

#include <stm32f4xx_spi.h>

//--  NSS -- PB12 -- OUT -- PP --

//--  SCK -- PB13 --  AF -- PP --

//-- MOSI -- PB15 --  AF -- PP --

void SPI_Config(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;

  SPI_InitTypeDef SPI_InitStructure;

  /* Enable GPIO clocks */

  RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOB, ENABLE );

  

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_15; //B13=SCK  B15=MOSI

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;

  GPIO_Init( GPIOB, &GPIO_InitStructure );

  /* Connect SPI pins to AF */

  GPIO_PinAFConfig( GPIOB, GPIO_PinSource13, GPIO_AF_SPI2 );

  GPIO_PinAFConfig( GPIOB, GPIO_PinSource15, GPIO_AF_SPI2 );

  /* Enable GPIO clocks */

   RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOB, ENABLE );

  //FS Config

  RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOB, ENABLE );

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;

  GPIO_Init( GPIOB, &GPIO_InitStructure );

  /* Enable the SPI clock */

   RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );

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

  SPI_I2S_DeInit(SPI2);

  SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx ;

  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

  SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;

  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;

  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;

  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;

  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

  SPI_InitStructure.SPI_CRCPolynomial = 7;

  SPI_Init(SPI2, &SPI_InitStructure);

  SPI_Cmd(SPI2, ENABLE);

  SPI_I2S_ITConfig( SPI2, SPI_I2S_IT_RXNE, ENABLE );

}

void SendData( uint16_t what );

int main(void){

SPI_Config();

SendData(0b1000000000000000);

while(1){

SendData(0b0001000100010001);

             }

}

void SendData( uint16_t what ){

  GPIOB->BSRRH = GPIO_Pin_12; //Low B12 (FS)

  SPI_I2S_SendData ( SPI2, what );

  while((SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_RXNE) == SET));

  GPIOB->BSRRL = GPIO_Pin_12; //High B12 (FS)

}

I would apreciate if anyone can help me. Thank you in advance.
2 REPLIES 2
Posted on October 26, 2016 at 21:34

Don't enable the interrupt unless you are using an IRQHandler for it.

Don't use the GetITStatus _IT_ version, use the GetFlagStatus _FLAG_ version

You could also do a fake read of the DR before you send data and check the TXE to ensure it is ready to send.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mone6980
Associate
Posted on October 27, 2016 at 08:56

Thanks for the reply. I did what you told me but again although SCL signal is ok the cs is completely wrong. I am very confused. I can't understand why CS goes High and Low whenever it wants..