title: What is the correct way to format data to send by HAL SPI in STM32?
body: <p>I am trying to read/write data from ST EEPROM M95640-W</p>
  
  <p>Acording to datasheet, for read operations data command must be formated like:</p>
  
  <p><a href="https://i.stack.imgur.com/JE0c0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JE0c0.png" alt="M95640-W Read operation"></a></p>
  
  <p>First is 8-bit instruction "Read from Memory Array": <strong>0x03 (B00000011)</strong></p>
  
  <p>Second is 16-bit address.</p>
  
  <p>But when I send command and data I got this:</p>
  
  <p><strong>Address 0x0001 (B00000001):</strong>
  <a href="https://i.stack.imgur.com/JPt2G.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JPt2G.png" alt="M95640 Read from address 0x1"></a></p>
  
  <p>And just to check data position with mirrored bits</p>
  
  <p><strong>Address 0x0081 (B10000001):</strong>
  <a href="https://i.stack.imgur.com/eNWVL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eNWVL.png" alt="M95640 Read from address 0x81"></a></p>
  
  <hr>
  
  <p>So I can not find an error in my code, why it send data in that incorrect way. It send <em>0x20</em> command except of <em>0x03</em>. It send <em>0x48</em> address except of <em>0x01</em> and <em>0x80</em> except of <em>0x81</em>. Looks like I got an incorrect data translation between types uint16_t -> uint8_t*.</p>
  
  <p>So here is the code:</p>
  
  <pre class="lang-c prettyprint-override"><code>#define EEPROM_SPI   hspi1
  #define EEPROM_READ  0x03  // Read from Memory Array
  
   /**
    * @brief  Reads a block of data from the EEPROM.
    * @param  pBuffer: pointer to the buffer that receives the data read from the EEPROM.
    * @param  ReadAddr: EEPROM's internal address to read from.
    * @param  NumByteToRead: number of bytes to read from the EEPROM.
    * @retval None
    */
  uint8_t sEE_ReadBuffer(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t NumByteToRead) {
      /*!&lt; Select the EEPROM: Chip Select low */
      EEPROM_LED_ON();
      EEPROM_CS_LOW();
  
      /*!&lt; Send "Read from Memory" instruction and MSB of WriteAddr  */
      EEPROM_SendInstruction((uint8_t*)EEPROM_READ, 1);
  
      /*!&lt; Send WriteAddr address byte to read from */
      EEPROM_SendInstruction((uint8_t*)ReadAddr, 2);
  
      if (HAL_SPI_Receive_DMA(&amp;EEPROM_SPI, (uint8_t*)pBuffer, NumByteToRead) != HAL_OK) {
          Error_Handler();
      }
  
      /*!&lt; Deselect the EEPROM: Chip Select high */
      EEPROM_CS_HIGH();
      EEPROM_LED_OFF();
  
      return 0;
  }
  
  void EEPROM_SendInstruction(uint8_t *instruction, uint8_t size) {
      while (EEPROM_SPI.State == HAL_SPI_STATE_RESET) {
          osDelay(1);
      }
  
      if (HAL_SPI_Transmit_DMA(&amp;EEPROM_SPI, (uint8_t*)instruction, (uint16_t)size) != HAL_OK) {
          Error_Handler();
      }
  }
  </code></pre>
  
  <p>SPI initialization:</p>
  
  <pre class="lang-c prettyprint-override"><code>static void MX_SPI1_Init(void) {
      hspi1.Instance = SPI1;
      hspi1.Init.Mode = SPI_MODE_MASTER;
      hspi1.Init.Direction = SPI_DIRECTION_2LINES;
      hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
      hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
      hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
      hspi1.Init.NSS = SPI_NSS_SOFT;
      hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
      hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
      hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
      hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
      hspi1.Init.CRCPolynomial = 7;
  
      if (HAL_SPI_Init(&amp;hspi1) != HAL_OK) {
          Error_Handler();
      }
  }
  </code></pre>
  
  <p><strong>UPD1:</strong> I did the packet like @Guillaume Michel said and it worked:
  <a href="https://i.stack.imgur.com/cdBcD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cdBcD.png" alt="M95640 Read from address 0x81 Working way"></a></p>
  
  <p><strong>UPD2</strong> I published the libriary at <a href="https://github.com/firebull/STM32-EEPROM-SPI" rel="nofollow noreferrer">Github</a>, writing and reading data is fully functional. Help is welcome to review the code and add some new features.</p>