cancel
Showing results for 
Search instead for 
Did you mean: 

SPI loop-back test - STM32F3

craig239955_stm1_st
Associate II
Posted on May 08, 2013 at 14:48

Hey all, I've been trying to get SPI working on my STM32F3 discovery board recently but it just doesn't seem to be working. I was hoping to test it in loop-back configuration and display the sent data on the board LEDs just to check if it was working, but no luck so far.UnfortunatelyI don't have a scope handy to check the signals either.

Here's the code and configuration I've written:

/* Includes ------------------------------------------------------------------*/
#include ''main.h''
/* variables ---------------------------------------------------------*/
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
/* function prototypes -----------------------------------------------*/
void
delay(
int
);
void
GPIO_cfg(
void
);
void
SPI_GPIO_init(
void
);
void
SPI_init(
void
);
int
main(
void
)
{
uint8_t test_data = 0xAF;
uint8_t rxbuf; 
GPIO_cfg(); 
SPI_GPIO_init();
SPI_init();
while
(1){
// Assert /CS followed by delay
GPIO_ResetBits(GPIOA, GPIO_Pin_4); 
delay(10);
while
(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE == 0) ); 
// Wait for TX buffer to be not empty. ie data loaded
SPI_SendData8(SPI1, test_data); 
// Send data
while
(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE == 0) ); 
//wait for last bit to be sent
rxbuf = SPI_ReceiveData8(SPI1); 
// Receive data
// Deassert /CS after delay
delay(10);
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
GPIO_SetBits(GPIOE, rxbuf<<8); 
// shift result up 8 bits to display on LEDs
delay(400);
}
}
void
delay(
int
n){
int
i,l;
for
(i=0; i<n; i++){
for
(l=0; l<n; l++){}
}
}
/* GPIO configuration for LEDs */
void
GPIO_cfg(){
// Configure/enable peripheral clocks
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE);
/* Configure PE15 in output pushpull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; 
// GPIO_Pin_all to address all pins
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOE, &GPIO_InitStructure);
}
/* Set up GPIO for SPI */
void
SPI_GPIO_init(){
//Disable SPI
SPI_I2S_DeInit(SPI1); 
// Enable clock for SPI 1
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
// Enable Clock for SPI GPIOA
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
// Set up SCLK, MISO and MOSI pins
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; 
// Select pins ( SCLK | MISO | MOSI)
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
// Set as AF mode
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Configure GPIO Alternate function as SPI1
GPIO_PinAFConfig(GPIOA, GPIO_Pin_5, GPIO_AF_5); 
// SPI1 SCLK
GPIO_PinAFConfig(GPIOA, GPIO_Pin_6, GPIO_AF_5); 
// SPI1 MISO
GPIO_PinAFConfig(GPIOA, GPIO_Pin_7, GPIO_AF_5); 
// SPI1 MOSI
// Set up CS pin
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
// OType, Speed and PuPd are the same
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/* Set up SPI and related parameters */
void
SPI_init(){
// Configure SPI
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;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
// Enable SPI1
SPI_Cmd(SPI1, ENABLE);
// ensure RXNE is initially clear
SPI_ReceiveData8(SPI1); 
}

#spi-stm32f3-loopback
5 REPLIES 5
mouna8154
Associate II
Posted on June 05, 2013 at 12:48

Hi,

i've tried your SPI code. i replaced my main.c file with yours.

when i try to rebuild it, these errors come: see attached file

i don't know how to solve it!

thanks for your eventual help.

________________

Attachments :

cap1.JPG : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HzqY&d=%2Fa%2F0X0000000bQc%2FfFURYScLL.w0ZvBcvG4GzUrgd.iPn_IDvgP2_JiuWAM&asPdf=false

cap2.JPG : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HzsK&d=%2Fa%2F0X0000000bQe%2FBEj.7i3JBiktJx3B9r5XDZTVy6j7skdivdWMtMUbOjs&asPdf=false
Posted on June 05, 2013 at 17:47

Putting user modifiable files in the ''C:\Program Files'' paths is not advisable

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mouna8154
Associate II
Posted on June 06, 2013 at 09:36

ok

mouna8154
Associate II
Posted on June 06, 2013 at 09:41

but most of the time i try the example projects (SPI, ADC,...) we find in the installation file of iar. that's why, i don't have the choice..

crt2
Associate II
Posted on June 06, 2013 at 11:30

Is that French? Anyways from what I've understood you don't have permission to write into the directories. Now its been a long time since I've seen Windows but you might want to check with rightclick (on specified directory)->properties->permission tab or user tab or something like that.

Looking at that example I'd still use interrupts to handle any faster events, so you might look at STDLIB examples.

This might also be useful although STD periph lib comes with nice documentation:

http://www.st.com/st-web-ui/static/active/en/resource/technical/document/user_manual/DM00023896.pdf