/**
  * @brief  Wait for EEPROM SPI Standby state.
  * 
  * @note  This function allows to wait and check that EEPROM has finished the 
  *        last operation. It is mostly used after Write operation.
  * 
  * @retval EEPROM_OK (0) if operation is correctly performed, else return value 
  *         different from EEPROM_OK (0) or the timeout user callback.
  */
static uint32_t EEPROM_SPI_WaitEepromStandbyState(void)  
{
  if(EEPROM_SPI_IO_WaitEepromStandbyState() != HAL_OK)
  {
    BSP_EEPROM_TIMEOUT_UserCallback();
    return EEPROM_TIMEOUT;
  }
  return EEPROM_OK;
}
/**
  * @brief  Write data to SPI EEPROM driver
  * @param  MemAddress: Internal memory address
  * @param  pBuffer: Pointer to data buffer
  * @param  BufferSize: Amount of data to be read
  * @retval HAL_StatusTypeDef HAL Status
  */
HAL_StatusTypeDef EEPROM_SPI_IO_WriteData(uint16_t MemAddress, uint8_t* pBuffer, uint32_t BufferSize)
{
  /*!< Enable the write access to the EEPROM */
  /*!< Select the EEPROM: Chip Select low */
  EEPROM_CS_LOW();

  /*!< Send "Write Enable" instruction */
  SPIx_Write(EEPROM_CMD_WREN);

  /*!< Deselect the EEPROM: Chip Select high */
  EEPROM_CS_HIGH();
  
  /*!< Select the EEPROM: Chip Select low */
  EEPROM_CS_LOW();

  /*!< Send "Write to Memory " instruction */
  /* Send the byte */
  SPIx_Write(EEPROM_CMD_WRITE);
  
  /*!< Send MemAddress high nibble address byte to write to */
  SPIx_Write((MemAddress & 0xFF0000) >> 16);
  
  /*!< Send MemAddress medium nibble address byte to write to */
  SPIx_Write((MemAddress & 0xFF00) >> 8);
  
  /*!< Send MemAddress low nibble address byte to write to */
  SPIx_Write(MemAddress & 0xFF);
  
  /*!< while there is data to be written on the EEPROM */
  while ((BufferSize)--)
  {
    /*!< Send the current byte */
    SPIx_Write(*pBuffer);
    /*!< Point on the next byte to be written */
    pBuffer++;
  }
  
  /*!< Deselect the EEPROM: Chip Select high */
  EEPROM_CS_HIGH();
  
  /*!< Wait the end of EEPROM writing */
  EEPROM_SPI_IO_WaitEepromStandbyState();
  
  /*!< Disable the write access to the EEROM */
  EEPROM_CS_LOW();

  /*!< Send "Write Disable" instruction */
  SPIx_Write(EEPROM_CMD_WRDI);

  /*!< Deselect the EEPROM: Chip Select high */
  EEPROM_CS_HIGH();

  return HAL_OK;
}