title: STM32F4 SPI issue
body: <p>I'am using STM32F407vg and i'am trying to write data in SPI data register, the following code shows the configuration function </p>
  
  <pre><code>void init_SPI1(void){
  
  GPIO_InitTypeDef GPIO_InitStruct;
  SPI_InitTypeDef SPI_InitStruct;
  
  // enable clock for used IO pins
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  
  /* configure pins used by SPI1
       * PA4 = NSS
   * PA5 = SCK
   * PA6 = MISO
   * PA7 = MOSI
   */
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_5|GPIO_Pin_4;
  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(GPIOA, &amp;GPIO_InitStruct);
  
  // connect SPI1 pins to SPI alternate function
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_SPI1);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);
  
  //Set chip select high 
  GPIOA-&gt;BSRRL |= GPIO_Pin_4; // set PE4 high
  
  // enable peripheral clock
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
  
  /* configure SPI1 in Mode 0 
   * CPOL = 0 --&gt; clock is low when idle
   * CPHA = 0 --&gt; data is sampled at the first edge
   */
  SPI_StructInit(&amp;SPI_InitStruct); // set default config 
  SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // set to full duplex mode, seperate MOSI and MISO lines
  SPI_InitStruct.SPI_Mode = SPI_Mode_Master;     // transmit in master mode, NSS pin has to be always high
  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 ; // set the NSS management to internal and pull internal NSS high
  SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; // SPI frequency is APB2 frequency / 4
  SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first
  SPI_Init(SPI1, &amp;SPI_InitStruct); 
  
  SPI_Cmd(SPI1, ENABLE); // enable SPI1
  } 
  </code></pre>
  
  <p>and this is the function that send data </p>
  
  <pre><code> uint8_t SPI1_send(uint8_t data){
  
  SPI1-&gt;DR = data; // write data to be transmitted to the SPI data register
  while( !(SPI1-&gt;SR &amp; SPI_I2S_FLAG_TXE) ); // wait until transmit complete
  while( !(SPI1-&gt;SR &amp; SPI_I2S_FLAG_RXNE) ); // wait until receive complete
  while( SPI1-&gt;SR &amp; SPI_I2S_FLAG_BSY ); // wait until SPI is not busy anymore
  return SPI1-&gt;DR; // return received data from SPI data register
  }
  </code></pre>
  
  <p>I think that i have set the good configuration (the choice of pins is not wrong in my opinion and also the configuration of the bus used ) the following pictures are extracted from the <a href="http://www.st.com/web/en/resource/technical/document/datasheet/DM00037051.pdf" rel="nofollow noreferrer">STM32F407 DataSheet</a>
  <a href="https://i.stack.imgur.com/thXAi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/thXAi.png" alt="SPI_Alternate_Function_Mapping"></a>
  <a href="https://i.stack.imgur.com/CTXTC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CTXTC.png" alt="enter image description here"></a></p>
  
  <p>The problem is that the data in the DR registry is different than the data passed as argument <a href="https://i.stack.imgur.com/lC4gQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lC4gQ.png" alt="Debug_Figure"></a>.I don't know why this happen .Could anyone guide me to the right point  </p>