cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F3 discovery SPI communication

walpro
Associate
Posted on May 04, 2013 at 00:34

hello!

i'm an engineering student and i'm workingon a quad-copterproject. i have to use the stm32f3 discovery board to send gyro and accelerometer data throught spi to a rasberry pi board! the first step i did is trying to test the stm32 spi communication by doing a loopback test(MOSI to MISO) but i did not managed to do it! i did not found spi examples on stm32f3 but this is what i've coded :

/// clock configuration
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
//// GPIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
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_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/// SPI
SPI_I2S_DeInit(SPI2);
SPIS.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPIS.SPI_DataSize = SPI_DataSize_8b;
SPIS.SPI_CPOL = SPI_CPOL_Low;
SPIS.SPI_CPHA = SPI_CPHA_1Edge;
SPIS.SPI_NSS = SPI_NSS_Soft;
SPIS.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
SPIS.SPI_FirstBit = SPI_FirstBit_MSB;
SPIS.SPI_CRCPolynomial = 7;
SPIS.SPI_Mode = SPI_Mode_Master;
SPI_Init(SPI2, &SPIS);
////sending data 
a=0x08;
SPI_Cmd(SPI2, ENABLE);
SPI_SendData8(SPI2,a);
b=SPI_ReceiveData8(SPI2);
//testing the trasmission 
if(b==0x08)
{ 
a++;
STM_EVAL_LEDOn(LED4);
}

can you help plz?
3 REPLIES 3
Posted on May 04, 2013 at 01:19

STM32F37x_DSP_StdPeriph_Lib_V1.0.0\Project\STM32F37x_StdPeriph_Examples\SPI\SPI_TwoBoards\SPI_DataExchangeInterrupt ??

Yeah, you'll want to be making sure TXE is asserted before sending data, and RXNE is asserted before reading data.

SPI examples in the other STM32 firmware libraries may also be instructive.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
hillpatrickb
Associate II
Posted on May 04, 2013 at 06:09

Since SPI uses a shift register to move out one bit at a time you you need to implement a while loop to to wait for the SPI Busy Flag to become a 0 then you can make b =

b=SPI_ReceiveData8(SPI2);

So while SPI Busy Flag is 1 do nothing, then once it becomes a 0 set b equal to the MISO data.

hillpatrickb
Associate II
Posted on May 04, 2013 at 06:09

Since SPI uses a shift register to move out one bit at a time you you need to implement a while loop to to wait for the SPI Busy Flag to become a 0 then you can make b =

b=SPI_ReceiveData8(SPI2);

So while SPI Busy Flag is 1 do nothing, then once it becomes a 0 set b equal to the MISO data.