AnsweredAssumed Answered

problem with stm32f4 discovery SPI2

Question asked by armoun.mohamad on Feb 21, 2014
Latest reply on Feb 21, 2014 by armoun.mohamad
hi guys.
i want to use my stm32f4 spi2 and i wrote some code and i think it should be true,
because i wrote the same code for SPI1 in this board and it works very well,
all i want is to send a data through  mosi and receive that data from miso and show that specific data through usart and serial port in my pc.
pls tell me what is the problem. is my code wrong ??
here is mu code : 

#include <stm32f4xx.h>


void RCC_configuration()
   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
   RCC_APB2PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
//    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

void init_GPIO()
  GPIO_InitTypeDef GPIO_InitStruct;
  /* configure pins used by SPI1
      * PB13 = SCK
      * PB14 = MISO
      * PB15 = 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_UP; 
  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);


void init_SPI2()
        SPI_InitTypeDef SPI_InitStruct;
        SPI_InitStruct.SPI_Direction =SPI_Direction_2Lines_FullDuplex;      // set to FullDuplexmode, seperate MOSI and MISO lines
     SPI_InitStruct.SPI_Mode = SPI_Mode_Master;          
     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_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; 
     SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;     // data is transmitted MSB first
     SPI_Init(SPI2, &SPI_InitStruct); 
     SPI_Cmd(SPI2, ENABLE); // enable SPI2

void init_usart(void){

     GPIO_InitTypeDef GPIO_InitStructure;
     USART_InitTypeDef USART_InitStructure;

     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
     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(GPIOA, &GPIO_InitStructure);

     GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);

     USART_InitStructure.USART_BaudRate = 9600;
     USART_InitStructure.USART_WordLength = USART_WordLength_8b;
     USART_InitStructure.USART_StopBits = USART_StopBits_1;
     USART_InitStructure.USART_Parity = USART_Parity_No;
     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
     USART_InitStructure.USART_Mode = USART_Mode_Tx;
     USART_Init(USART2, &USART_InitStructure);



int main()
  uint8_t received_val = 50;
  while (1)
    received_val = SPI_I2S_ReceiveData(SPI2);