254 lines
6.9 KiB
C
254 lines
6.9 KiB
C
/**
|
|
******************************************************************************
|
|
* @file flash_if.h
|
|
* @author AMG Energy Solution Team
|
|
* @version V1.0.0
|
|
* @date 07-Feb-2022
|
|
* @brief This file provides all the headers of the flash memory functions.
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
|
|
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
|
|
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
|
|
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
|
|
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
|
|
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
|
|
*
|
|
* <h2><center>© COPYRIGHT 2022 AMG</center></h2>
|
|
******************************************************************************
|
|
*/
|
|
|
|
/** @addtogroup mBMS2
|
|
* @{
|
|
*/
|
|
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include "flash_if.h"
|
|
#include "stdio.h"
|
|
|
|
/* Private typedef -----------------------------------------------------------*/
|
|
/* Private define ------------------------------------------------------------*/
|
|
/* Private macro -------------------------------------------------------------*/
|
|
/* Private variables ---------------------------------------------------------*/
|
|
FW_INFO fwinfo;
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
/* Private functions ---------------------------------------------------------*/
|
|
|
|
/**
|
|
* @brief Unlocks Flash for write access
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
void FLASH_If_Init(void)
|
|
{
|
|
/* Unlock EFM. */
|
|
EFM_Unlock();
|
|
|
|
/* Enable flash. */
|
|
EFM_FlashCmd(Enable);
|
|
}
|
|
|
|
/**
|
|
* @brief This function does an erase of all user flash area
|
|
* @param StartSector: start of user flash area
|
|
* @retval 0: user flash area successfully erased
|
|
* 1: error occurred
|
|
*/
|
|
uint32_t FLASH_If_Erase(uint32_t StartSector, uint32_t uSize)
|
|
{
|
|
uint32_t UserStartSector, UserEndSector, i = 0;
|
|
|
|
/* Unlock EFM. */
|
|
EFM_Unlock();
|
|
|
|
/* Enable flash. */
|
|
EFM_FlashCmd(Enable);
|
|
/* Wait flash ready. */
|
|
while(Set != EFM_GetFlagStatus(EFM_FLAG_RDY))
|
|
{
|
|
;
|
|
}
|
|
|
|
/* Get the sector where start the user flash area */
|
|
UserStartSector = StartSector;
|
|
UserEndSector = (StartSector + uSize);
|
|
|
|
for(i = UserStartSector; i <= UserEndSector; i += SECTOR_SIZE)
|
|
{
|
|
/* Reload IWDG counter */
|
|
WDT_RefreshCounter();
|
|
|
|
/* Device voltage range supposed to be [2.7V to 3.6V], the operation will
|
|
be done by word */
|
|
if (EFM_SectorErase(i) != Ok)
|
|
{
|
|
/* Error occurred while page erase */
|
|
return (1);
|
|
}
|
|
}
|
|
|
|
/* Lock EFM. */
|
|
EFM_Lock();
|
|
|
|
return (0);
|
|
}
|
|
|
|
/**
|
|
* @brief This function does an erase of all user flash area
|
|
* @param StartSector: start of user flash area
|
|
* @retval 0: user flash area successfully erased
|
|
* 1: error occurred
|
|
*/
|
|
uint32_t FLASH_If_SingleSectorErase(uint32_t SectorAddr)
|
|
{
|
|
uint32_t result = 0;
|
|
|
|
/* Unlock EFM. */
|
|
EFM_Unlock();
|
|
|
|
/* Enable flash. */
|
|
EFM_FlashCmd(Enable);
|
|
/* Wait flash ready. */
|
|
while(Set != EFM_GetFlagStatus(EFM_FLAG_RDY))
|
|
{
|
|
;
|
|
}
|
|
|
|
/* Reload IWDG counter */
|
|
WDT_RefreshCounter();
|
|
|
|
/* Device voltage range supposed to be [2.7V to 3.6V], the operation will
|
|
be done by word */
|
|
if (EFM_SectorErase(SectorAddr) != Ok)
|
|
{
|
|
/* Error occurred while page erase */
|
|
result = (1);
|
|
}
|
|
|
|
/* Lock EFM. */
|
|
EFM_Lock();
|
|
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* @brief This function writes a data buffer in flash (data are 32-bit aligned).
|
|
* @note After writing data buffer, the flash content is checked.
|
|
* @param FlashAddress: start address for writing data buffer
|
|
* @param Data: pointer on data buffer
|
|
* @param DataLength: length of data buffer (unit is 32-bit word)
|
|
* @retval 0: Data successfully written to Flash memory
|
|
* 1: Error occurred while writing data in Flash memory
|
|
* 2: Written Data in flash memory is different from expected one
|
|
*/
|
|
uint32_t FLASH_If_Write(__IO uint32_t* FlashAddress, uint32_t* Data, uint32_t DataLength)
|
|
{
|
|
uint32_t i = 0;
|
|
uint32_t result = 0;
|
|
|
|
/* Unlock EFM. */
|
|
EFM_Unlock();
|
|
|
|
/* Enable flash. */
|
|
EFM_FlashCmd(Enable);
|
|
/* Wait flash ready. */
|
|
while(Set != EFM_GetFlagStatus(EFM_FLAG_RDY))
|
|
{
|
|
;
|
|
}
|
|
|
|
for (i = 0; (i < DataLength) && (*FlashAddress <= (mBMS_FW_DN_END_ADDRESS - 4)); i++)
|
|
{
|
|
/* Device voltage range supposed to be [2.7V to 3.6V], the operation will
|
|
be done by word */
|
|
if (EFM_SingleProgram(*FlashAddress, *(uint32_t *)(Data + i)) == Ok)
|
|
{
|
|
/* Check the written value */
|
|
if (*(uint32_t*)*FlashAddress != *(uint32_t *)(Data + i))
|
|
{
|
|
/* Flash content doesn't match SRAM content */
|
|
result = 2;
|
|
break;
|
|
}
|
|
/* Increment FLASH destination address */
|
|
*FlashAddress += 4;
|
|
}
|
|
else
|
|
{
|
|
/* Error occurred while writing data in Flash memory */
|
|
result = 1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
/* Lock EFM. */
|
|
EFM_Lock();
|
|
|
|
return result;
|
|
}
|
|
|
|
///**
|
|
// * @brief Returns the write protection status of user flash area.
|
|
// * @param None
|
|
// * @retval 0: No write protected sectors inside the user flash area
|
|
// * 1: Some sectors inside the user flash area are write protected
|
|
// */
|
|
//uint16_t FLASH_If_GetWriteProtectionStatus(void)
|
|
//{
|
|
// uint32_t UserStartSector = FLASH_Sector_1;
|
|
|
|
// /* Get the sector where start the user flash area */
|
|
// UserStartSector = GetSector(APPLICATION_ADDRESS);
|
|
|
|
// /* Check if there are write protected sectors inside the user flash area */
|
|
// if ((FLASH_OB_GetWRP() >> (UserStartSector/8)) == (0xFFF >> (UserStartSector/8)))
|
|
// { /* No write protected sectors inside the user flash area */
|
|
// return 1;
|
|
// }
|
|
// else
|
|
// { /* Some sectors inside the user flash area are write protected */
|
|
// return 0;
|
|
// }
|
|
//}
|
|
|
|
///**
|
|
// * @brief Disables the write protection of user flash area.
|
|
// * @param None
|
|
// * @retval 1: Write Protection successfully disabled
|
|
// * 2: Error: Flash write unprotection failed
|
|
// */
|
|
//uint32_t FLASH_If_DisableWriteProtection(void)
|
|
//{
|
|
// __IO uint32_t UserStartSector = FLASH_Sector_1, UserWrpSectors = OB_WRP_Sector_1;
|
|
|
|
// /* Get the sector where start the user flash area */
|
|
// UserStartSector = GetSector(APPLICATION_ADDRESS);
|
|
|
|
// /* Mark all sectors inside the user flash area as non protected */
|
|
// UserWrpSectors = 0xFFF-((1 << (UserStartSector/8))-1);
|
|
//
|
|
// /* Unlock the Option Bytes */
|
|
// FLASH_OB_Unlock();
|
|
|
|
// /* Disable the write protection for all sectors inside the user flash area */
|
|
// FLASH_OB_WRPConfig(UserWrpSectors, DISABLE);
|
|
|
|
// /* Start the Option Bytes programming process. */
|
|
// if (FLASH_OB_Launch() != FLASH_COMPLETE)
|
|
// {
|
|
// /* Error: Flash write unprotection failed */
|
|
// return (2);
|
|
// }
|
|
|
|
// /* Write Protection successfully disabled */
|
|
// return (1);
|
|
//}
|
|
|
|
/**
|
|
* @}
|
|
*/
|
|
|
|
/*******************(C)COPYRIGHT 2022 AMG ********************END OF FILE******/
|