/**
  * @brief  Writes more than one byte to the EEPROM with a single WRITE cycle.
  *
  * @note   The number of bytes (combined to write start address) must not 
  *         cross the EEPROM page boundary. This function can only write into
  *         the boundaries of an EEPROM page.
  *         This function doesn't check on boundaries condition (in this driver 
  *         the function BSP_EEPROM_WriteBuffer() which calls BSP_EEPROM_WritePage() is 
  *         responsible of checking on Page boundaries).
  * 
  * @param  pBuffer: pointer to the buffer containing the data to be written to 
  *         the EEPROM.
  * @param  WriteAddr: EEPROM's internal address to write to.
  * @param  NumByteToWrite: pointer to the variable holding number of bytes to 
  *         be written into the EEPROM. 
  * 
  *        @note The variable pointed by NumByteToWrite is reset to 0 when all the 
  *              data are written to the EEPROM. Application should monitor this 
  *              variable in order know when the transfer is complete.
  * 
  *        @note This function just configure the communication and enable the DMA 
  *              channel to transfer data. Meanwhile, the user application may perform 
  *              other tasks in parallel.
  * 
  * @retval EEPROM_OK (0) if operation is correctly performed, else return value 
  *         different from EEPROM_OK (0) or the timeout user callback.
  */
uint32_t BSP_EEPROM_WritePage(uint8_t* pBuffer, uint16_t WriteAddr, uint8_t* NumByteToWrite)
{ 
  uint32_t buffersize = *NumByteToWrite;
  uint32_t status = EEPROM_OK;
  
  /* Set the pointer to the Number of data to be written. This pointer will be used 
      by the DMA Transfer Completer interrupt Handler in order to reset the 
      variable to 0. User should check on this variable in order to know if the 
      DMA transfer has been complete or not. */
  EEPROMDataWrite = *NumByteToWrite;  
  
  if(EEPROM_IO_WriteData(EEPROMAddress, WriteAddr, pBuffer, buffersize) != HAL_OK)
  {
    BSP_EEPROM_TIMEOUT_UserCallback();
    status = EEPROM_FAIL;
  }
  
  if(BSP_EEPROM_WaitEepromStandbyState() != EEPROM_OK) 
  {
    return EEPROM_FAIL;
  }
  
  /* If all operations OK, return EEPROM_OK (0) */
  return status;
}
/**
  * @brief  Writes more than one byte to the EEPROM with a single WRITE cycle.
  *
  * @note   The number of bytes (combined to write start address) must not 
  *         cross the EEPROM page boundary. This function can only write into
  *         the boundaries of an EEPROM page.
  *         This function doesn't check on boundaries condition (in this driver 
  *         the function BSP_EEPROM_WriteBuffer() which calls BSP_EEPROM_WritePage() is 
  *         responsible of checking on Page boundaries).
  * 
  * @param  pBuffer : pointer to the buffer containing the data to be written to 
  *         the EEPROM.
  * @param  WriteAddr : EEPROM's internal address to write to.
  * @param  NumByteToWrite : pointer to the variable holding number of bytes to 
  *         be written into the EEPROM. 
  * 
  *        @note The variable pointed by NumByteToWrite is reset to 0 when all the 
  *              data are written to the EEPROM. Application should monitor this 
  *              variable in order know when the transfer is complete.
  * 
  * @retval EEPROM_OK (0) if operation is correctly performed, else return value 
  *         different from EEPROM_OK (0) or the timeout user callback.
  */
uint32_t BSP_EEPROM_WritePage(uint8_t* pBuffer, uint16_t WriteAddr, uint8_t* NumByteToWrite)
{ 
  uint32_t buffersize = *NumByteToWrite;
  uint32_t status = EEPROM_OK;
  
  /* Set the pointer to the Number of data to be written */
  EEPROMDataWrite = *NumByteToWrite;  
  if(EEPROM_IO_WriteData(EEPROM_I2C_ADDRESS, WriteAddr, pBuffer, buffersize) != HAL_OK)  
  {
    BSP_EEPROM_TIMEOUT_UserCallback();
    status = EEPROM_FAIL;
  }
  
  while(BSP_EEPROM_WaitEepromStandbyState() != EEPROM_OK)
  {
    return EEPROM_FAIL;
  }

  /* If all operations OK, return EEPROM_OK (0) */
  return status;
}
Example #3
0
/**
  * @brief  Show EEPROM Features
  * @param  Feature : feature index
  * @retval None
  */
static void EEPROM_Show_Feature(uint8_t Feature)
{
  uint8_t Tx1Buffer[] = "STM324x9I-EVAL EEPROM Ex.";
  
  uint8_t Rx1Buffer[BUFFER_SIZE1] = {0}; 
  uint8_t Tx2Buffer[BUFFER_SIZE1] = {0}; 
  __IO TestStatus TransferStatus1 = FAILED;
  __IO uint16_t NumDataRead = 0; 

  BSP_LCD_SetBackColor(LCD_COLOR_WHITE);  
  BSP_LCD_SetTextColor(LCD_COLOR_WHITE);    
  BSP_LCD_FillRect(12, 92, BSP_LCD_GetXSize() - 24, BSP_LCD_GetYSize()- 104);
  BSP_LCD_SetTextColor(LCD_COLOR_BLACK);

  /* Initialize the I2C EEPROM driver ----------------------------------------*/
  if(BSP_EEPROM_Init() != EEPROM_OK)
  {
    BSP_LCD_SetTextColor(LCD_COLOR_RED);    
    BSP_LCD_DisplayStringAt(0, 115, (uint8_t *)"Initialization problem", CENTER_MODE);
    BSP_LCD_DisplayStringAt(0, 130, (uint8_t *)"Check if HW connected or", CENTER_MODE);
    BSP_LCD_DisplayStringAt(0, 145, (uint8_t *)"HW version not supported", CENTER_MODE);
    return;
  }
  EEPROMConnected = 1;
  
  /* Wait for EEPROM standby state */
  BSP_EEPROM_WaitEepromStandbyState();  
  
  switch (Feature)
  {
  case 0:
    /* Read old parameter in EEPROM */
    if(EEPROMConnected == 1)
    {
      /* Set the Number of data to be read */
      NumDataRead = BUFFER_SIZE1;
      
      /* Read from I2C EEPROM from EEPROM_READ_ADDRESS1 */
      if(BSP_EEPROM_ReadBuffer(Rx1Buffer, EEPROM_READ_ADDRESS1, (uint16_t *)(&NumDataRead)) != EEPROM_OK)
      {
        BSP_LCD_SetTextColor(LCD_COLOR_RED);    
        BSP_LCD_DisplayStringAt(0, 115, (uint8_t *)"Init issue at read old data", CENTER_MODE);
        BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
        BSP_LCD_DisplayStringAt(0, 145, (uint8_t *)"Press again USER key", CENTER_MODE);
        BSP_LCD_DisplayStringAt(0, 160, (uint8_t *)"To write new data", CENTER_MODE);
        return;
      }
      BSP_LCD_DisplayStringAt(0, 115, (uint8_t *)"PASSED String read", CENTER_MODE);
      BSP_LCD_DisplayStringAt(0, 130, (uint8_t *)"in EEPROM:", CENTER_MODE);
      BSP_LCD_SetTextColor(LCD_COLOR_BLUE);    
      BSP_LCD_DisplayStringAt(0, 160, Rx1Buffer, CENTER_MODE); 
      BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
      BSP_LCD_DisplayStringAt(0, 190, (uint8_t *)"Press USER key", CENTER_MODE);
      BSP_LCD_DisplayStringAt(0, 205, (uint8_t *)"To write new data", CENTER_MODE);
    }
    else
    {
      BSP_LCD_SetTextColor(LCD_COLOR_RED);    
      BSP_LCD_DisplayStringAt(0, 115, (uint8_t *)"Problem to communicate", CENTER_MODE);
      BSP_LCD_DisplayStringAt(0, 130, (uint8_t *)"with EEPROM", CENTER_MODE);
      BSP_LCD_DisplayStringAt(0, 145, (uint8_t *)"Press again USER key", CENTER_MODE);
    }
    
    break;
    
  case 1:
     /* Write new parameter in EEPROM */
   
    if(EEPROMConnected == 1)
    {
      snprintf((char*)Tx2Buffer, BUFFER_SIZE1, "%s Test %d", Tx1Buffer, NbLoop);
      /* First write in the memory followed by a read of the written data --------*/
      /* Write on I2C EEPROM to EEPROM_WRITE_ADDRESS1 */
      if(BSP_EEPROM_WriteBuffer(Tx2Buffer, EEPROM_WRITE_ADDRESS1, BUFFER_SIZE1) != EEPROM_OK)
      {
        BSP_LCD_SetTextColor(LCD_COLOR_RED);    
        BSP_LCD_DisplayStringAt(0, 115, (uint8_t *)"Init issue at write", CENTER_MODE);
        return;
      }
      
      /* Wait for EEPROM standby state */
      BSP_EEPROM_WaitEepromStandbyState();  
      
      /* Set the Number of data to be read */
      NumDataRead = BUFFER_SIZE1;
      
      /* Read from I2C EEPROM from EEPROM_READ_ADDRESS1 */
      if(BSP_EEPROM_ReadBuffer(Rx1Buffer, EEPROM_READ_ADDRESS1, (uint16_t *)(&NumDataRead)) != EEPROM_OK)
      {
        BSP_LCD_SetTextColor(LCD_COLOR_RED);    
        BSP_LCD_DisplayStringAt(0, 115, (uint8_t *)"Init issue at read", CENTER_MODE);
        return;
      }
      
      /* Check if the data written to the memory is read correctly */
      TransferStatus1 = Buffercmp(Tx2Buffer, Rx1Buffer, BUFFER_SIZE1);
      if(TransferStatus1 != FAILED)
      {
        BSP_LCD_DisplayStringAt(0, 115, (uint8_t *)"PASSED String writes", CENTER_MODE);
        BSP_LCD_DisplayStringAt(0, 130, (uint8_t *)"in EEPROM:", CENTER_MODE);
        BSP_LCD_SetTextColor(LCD_COLOR_BLUE);    
        BSP_LCD_DisplayStringAt(0, 160, Tx2Buffer, CENTER_MODE); 
      }
      else 
      {
        BSP_LCD_SetTextColor(LCD_COLOR_RED);    
        BSP_LCD_DisplayStringAt(0, 115, (uint8_t *)"FAILED to write!", CENTER_MODE);
        BSP_LCD_DisplayStringAt(0, 130, (uint8_t *)"Press USER key to end test", CENTER_MODE);
      }
    }
    else
    {
      BSP_LCD_SetTextColor(LCD_COLOR_RED);    
      BSP_LCD_DisplayStringAt(0, 115, (uint8_t *)"Problem to communicate", CENTER_MODE);
      BSP_LCD_DisplayStringAt(0, 130, (uint8_t *)"again with EEPROM", CENTER_MODE);
      BSP_LCD_DisplayStringAt(0, 130, (uint8_t *)"Press USER key to end test", CENTER_MODE);
    }
    break;
  }
}
/**
  * @brief  Writes buffer of data to the I2C EEPROM.
  * @param  pBuffer : pointer to the buffer  containing the data to be written 
  *         to the EEPROM.
  * @param  WriteAddr : EEPROM's internal address to write to.
  * @param  NumByteToWrite : number of bytes to write to the EEPROM.
  * @retval EEPROM_OK (0) if operation is correctly performed, else return value 
  *         different from EEPROM_OK (0) or the timeout user callback.
  */
uint32_t BSP_EEPROM_WriteBuffer(uint8_t* pBuffer, uint16_t WriteAddr, uint16_t NumByteToWrite)
{
  uint16_t numofpage = 0, numofsingle = 0, count = 0;
  uint16_t addr = 0;

  addr = WriteAddr % EEPROM_PAGESIZE;
  count = EEPROM_PAGESIZE - addr;
  numofpage =  NumByteToWrite / EEPROM_PAGESIZE;
  numofsingle = NumByteToWrite % EEPROM_PAGESIZE;
 
  /*!< If WriteAddr is EEPROM_PAGESIZE aligned  */
  if(addr == 0) 
  {
    /*!< If NumByteToWrite < EEPROM_PAGESIZE */
    if(numofpage == 0) 
    {
      /* Store the number of data to be written */
      EEPROMDataNum = numofsingle;
      /* Start writing data */
      if (BSP_EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEPROMDataNum)) != EEPROM_OK)
      {
        return EEPROM_FAIL;
      }
      /* Wait transfer through DMA to be complete */
      EEPROMTimeout = HAL_GetTick();
      while (EEPROMDataNum > 0)
      {
        if((HAL_GetTick() - EEPROMTimeout) < EEPROM_LONG_TIMEOUT) {
          BSP_EEPROM_TIMEOUT_UserCallback(); 
          return EEPROM_TIMEOUT;
        }
      }
      if (BSP_EEPROM_WaitEepromStandbyState() != EEPROM_OK) return EEPROM_FAIL;
    }
    /*!< If NumByteToWrite > EEPROM_PAGESIZE */
    else  
    {
      while(numofpage--)
      {
        /* Store the number of data to be written */
        EEPROMDataNum = EEPROM_PAGESIZE;        
        if (BSP_EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEPROMDataNum)) != EEPROM_OK) 
        {
          return EEPROM_FAIL;
        }
        /* Wait transfer through DMA to be complete */
        EEPROMTimeout = HAL_GetTick();
        while (EEPROMDataNum > 0)
        {
          if((HAL_GetTick() - EEPROMTimeout) < EEPROM_LONG_TIMEOUT) {
            BSP_EEPROM_TIMEOUT_UserCallback(); 
            return EEPROM_TIMEOUT;
          }
        }
        if (BSP_EEPROM_WaitEepromStandbyState() != EEPROM_OK) return EEPROM_FAIL;
        WriteAddr +=  EEPROM_PAGESIZE;
        pBuffer += EEPROM_PAGESIZE;
      }

      if(numofsingle!=0)
      {
        /* Store the number of data to be written */
        EEPROMDataNum = numofsingle;          
        if (BSP_EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEPROMDataNum)) != EEPROM_OK)
        {
          return EEPROM_FAIL;
        }
        /* Wait transfer through DMA to be complete */
        EEPROMTimeout = HAL_GetTick();
        while (EEPROMDataNum > 0)
        {
          if((HAL_GetTick() - EEPROMTimeout) < EEPROM_LONG_TIMEOUT) {
            BSP_EEPROM_TIMEOUT_UserCallback(); 
            return EEPROM_TIMEOUT;
          }
        }
        if (BSP_EEPROM_WaitEepromStandbyState() != EEPROM_OK) return EEPROM_FAIL;
      }
    }
  }
  /*!< If WriteAddr is not EEPROM_PAGESIZE aligned  */
  else 
  {
    /*!< If NumByteToWrite < EEPROM_PAGESIZE */
    if(numofpage== 0) 
    {
      /*!< If the number of data to be written is more than the remaining space 
      in the current page: */
      if (NumByteToWrite > count)
      {
        /* Store the number of data to be written */
        EEPROMDataNum = count;        
        /*!< Write the data contained in same page */
        if (BSP_EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEPROMDataNum)) != EEPROM_OK)
        {
          return EEPROM_FAIL;
        }
        /* Wait transfer through DMA to be complete */
        EEPROMTimeout = HAL_GetTick();
        while (EEPROMDataNum > 0)
        {
          if((HAL_GetTick() - EEPROMTimeout) < EEPROM_LONG_TIMEOUT) {
            BSP_EEPROM_TIMEOUT_UserCallback(); 
            return EEPROM_TIMEOUT;
          }
          }
        if (BSP_EEPROM_WaitEepromStandbyState() != EEPROM_OK) return EEPROM_FAIL;      
        
        /* Store the number of data to be written */
        EEPROMDataNum = (NumByteToWrite - count);          
        /*!< Write the remaining data in the following page */
        if (BSP_EEPROM_WritePage((uint8_t*)(pBuffer + count), (WriteAddr + count), (uint8_t*)(&EEPROMDataNum)) != EEPROM_OK)
        {
          return EEPROM_FAIL;
        }
        /* Wait transfer through DMA to be complete */
        EEPROMTimeout = HAL_GetTick();
        while (EEPROMDataNum > 0)
        {
          if((HAL_GetTick() - EEPROMTimeout) < EEPROM_LONG_TIMEOUT) {
            BSP_EEPROM_TIMEOUT_UserCallback(); 
            return EEPROM_TIMEOUT;
          }
        }
        if (BSP_EEPROM_WaitEepromStandbyState() != EEPROM_OK) return EEPROM_FAIL;        
      }      
      else      
      {
        /* Store the number of data to be written */
        EEPROMDataNum = numofsingle;         
        if (BSP_EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEPROMDataNum)) != EEPROM_OK)
        {
          return EEPROM_FAIL;
        }
        /* Wait transfer through DMA to be complete */
        EEPROMTimeout = HAL_GetTick();
        while (EEPROMDataNum > 0)
        {
          if((HAL_GetTick() - EEPROMTimeout) < EEPROM_LONG_TIMEOUT) {
            BSP_EEPROM_TIMEOUT_UserCallback(); 
            return EEPROM_TIMEOUT;
          }
        }
        if (BSP_EEPROM_WaitEepromStandbyState() != EEPROM_OK) return EEPROM_FAIL;        
          }
        }
    /*!< If NumByteToWrite > EEPROM_PAGESIZE */
    else
    {
      NumByteToWrite -= count;
      numofpage =  NumByteToWrite / EEPROM_PAGESIZE;
      numofsingle = numofsingle % EEPROM_PAGESIZE;
      
      if(count != 0)
      {  
        /* Store the number of data to be written */
        EEPROMDataNum = count;         
        if (BSP_EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEPROMDataNum)) != EEPROM_OK)
        {
          return EEPROM_FAIL;
        }
        /* Wait transfer through DMA to be complete */
        EEPROMTimeout = HAL_GetTick();
        while (EEPROMDataNum > 0)
        {
          if((HAL_GetTick() - EEPROMTimeout) < EEPROM_LONG_TIMEOUT) {
            BSP_EEPROM_TIMEOUT_UserCallback(); 
            return EEPROM_TIMEOUT;
          }
        }
        if (BSP_EEPROM_WaitEepromStandbyState() != EEPROM_OK) return EEPROM_FAIL;
        WriteAddr += count;
        pBuffer += count;
      } 
      
      while(numofpage--)
      {
        /* Store the number of data to be written */
        EEPROMDataNum = EEPROM_PAGESIZE;          
        if (BSP_EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEPROMDataNum)) != EEPROM_OK)
        {
          return EEPROM_FAIL;
        }
        /* Wait transfer through DMA to be complete */
        EEPROMTimeout = HAL_GetTick();
        while (EEPROMDataNum > 0)
        {
          if((HAL_GetTick() - EEPROMTimeout) < EEPROM_LONG_TIMEOUT) {
            BSP_EEPROM_TIMEOUT_UserCallback();
            return EEPROM_TIMEOUT;
          }
        }
        if (BSP_EEPROM_WaitEepromStandbyState() != EEPROM_OK) return EEPROM_FAIL;
        WriteAddr +=  EEPROM_PAGESIZE;
        pBuffer += EEPROM_PAGESIZE;  
      }
      if(numofsingle != 0)
      {
        /* Store the number of data to be written */
        EEPROMDataNum = numofsingle;           
        if (BSP_EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEPROMDataNum)) != EEPROM_OK) 
        {
          return EEPROM_FAIL;
        }
        /* Wait transfer through DMA to be complete */
        EEPROMTimeout = HAL_GetTick();
        while (EEPROMDataNum > 0)
        {
          if((HAL_GetTick() - EEPROMTimeout) < EEPROM_LONG_TIMEOUT) {
            BSP_EEPROM_TIMEOUT_UserCallback(); 
            return EEPROM_TIMEOUT;
        }
      }
        if (BSP_EEPROM_WaitEepromStandbyState() != EEPROM_OK) return EEPROM_FAIL;
      }
    }
  }  
  
  /* If all operations OK, return EEPROM_OK (0) */
  return EEPROM_OK;
}