AnsweredAssumed Answered

STM32F4 Discovery as SPI slave with interrupts

Question asked by viswanathan.sree.001 on Jun 13, 2013
Latest reply on Dec 10, 2013 by canturk.gokhan
  I am working on  a project where I need to sample some data using the ADC in discovery at 1MSPs and transfer it to a raspberry pi board via SPI. As Rpi supports only SPI master mode the discovery board acts as a slave.However I am not able to get the discovery board work in slave mode. I am posting it a sample program I used to test spi in discovery board . 

#include <stm32f4xx.h>
#include <stm32f4xx_spi.h>

uint8_t SPI1_send(uint8_t data);

// this function initializes the SPI1 peripheral
void init_SPI1(void){
     GPIO_InitTypeDef GPIO_InitStruct;
     SPI_InitTypeDef SPI_InitStruct;
     // enable clock for used IO pins
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
     /* configure pins used by SPI1
      * PA5 = SCK
      * PA6 = MISO
      * PA7 = MOSI
     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
     GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
     GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
     GPIO_Init(GPIOB, &GPIO_InitStruct);
     // connect SPI1 pins to SPI alternate function
     GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2);
     GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2);
     GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2);
     // enable clock for used IO pins
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
     // enable peripheral clock
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
     /* configure SPI1 in Mode 0 
      * CPOL = 0 --> clock is low when idle
      * CPHA = 0 --> data is sampled at the first edge
     SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // set to full duplex mode, seperate MOSI and MISO lines
     SPI_InitStruct.SPI_Mode = SPI_Mode_Slave;
     SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; // one packet of data is 8 bits wide
     SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;        // clock is low when idle
     SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;      // data sampled at first edge
     SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set; // set the NSS management to internal and pull internal NSS high
     SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; // SPI frequency is APB2 frequency / 4
     SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first
     SPI_Init(SPI2, &SPI_InitStruct); 
     SPI_Cmd(SPI2, ENABLE); // enable SPI1

/* This funtion is used to transmit and receive data 
 * with SPI1
 *                data --> data to be transmitted
 *                returns received value
uint8_t SPI2_recv(){
      uint8_t temp ;
   while( !(SPI2->SR & SPI_I2S_FLAG_RXNE) ); // wait until receive complete
      temp = SPI2->DR ; // write data to be transmitted to the SPI data register
     while( !(SPI2->SR & SPI_I2S_FLAG_TXE) ); // wait until transmit complete     
     while( SPI2->SR & SPI_I2S_FLAG_BSY ); // wait until SPI is not busy anymore
     return temp; // return received data from SPI data register
#if 1
int main(void){
     uint8_t received_val = 0;

          received_val = SPI2_recv(); // transmit dummy byte and receive data


When I debug the code I see that  while( !(SPI2->SR & SPI_I2S_FLAG_RXNE) );  condition is never being met. Also how to I enable interrupts for the same?