AnsweredAssumed Answered

SPI loop-back test - STM32F3

Question asked by kureigu on May 8, 2013
Latest reply on Jun 6, 2013 by crt
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. Unfortunately I 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;
  while (1){
        // Assert /CS followed by delay
        GPIO_ResetBits(GPIOA, GPIO_Pin_4);
        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
        GPIO_ResetBits(GPIOA, GPIO_Pin_4);
        GPIO_SetBits(GPIOE, rxbuf<<8); // shift result up 8 bits to  display on LEDs
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
  /* 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
    // 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