Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved FLASH_Update() method - make OTA Update more reliable #19

Merged
merged 2 commits into from
Apr 13, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion SPARK_Firmware_Driver/inc/hw_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ void FLASH_Backup(uint32_t sFLASH_Address);
void FLASH_Restore(uint32_t sFLASH_Address);
/* External Flash Helper routines */
void FLASH_Begin(uint32_t sFLASH_Address);
void FLASH_Update(uint8_t *pBuffer, uint32_t bufferSize);
uint16_t FLASH_Update(uint8_t *pBuffer, uint32_t bufferSize);
void FLASH_End(void);
void FLASH_Read_ServerAddress(ServerAddress *server_addr);
void FLASH_Read_ServerPublicKey(uint8_t *keyBuffer);
Expand Down
54 changes: 22 additions & 32 deletions SPARK_Firmware_Driver/src/hw_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ uint32_t Internal_Flash_Address = 0;
uint32_t External_Flash_Address = 0;
uint32_t Internal_Flash_Data = 0;
uint8_t External_Flash_Data[4];
uint16_t Flash_Update_Index = 0;
uint32_t EraseCounter = 0;
uint32_t NbrOfPage = 0;
volatile FLASH_Status FLASHStatus = FLASH_COMPLETE;
Expand Down Expand Up @@ -1470,6 +1471,7 @@ void FLASH_Begin(uint32_t sFLASH_Address)
Save_SystemFlags();
//BKP_WriteBackupRegister(BKP_DR10, 0x5555);

Flash_Update_Index = 0;
External_Flash_Address = sFLASH_Address;

/* Define the number of External Flash pages to be erased */
Expand All @@ -1484,49 +1486,37 @@ void FLASH_Begin(uint32_t sFLASH_Address)
#endif
}

void FLASH_Update(uint8_t *pBuffer, uint32_t bufferSize)
uint16_t FLASH_Update(uint8_t *pBuffer, uint32_t bufferSize)
{
#ifdef SPARK_SFLASH_ENABLE

uint32_t i = bufferSize >> 2;
uint8_t *writeBuffer = pBuffer;
uint8_t readBuffer[bufferSize];

/* Program External Flash */
while (i--)
{
Internal_Flash_Data = *((uint32_t *)pBuffer);
pBuffer += 4;
/* Write Data Buffer to SPI Flash memory */
sFLASH_WriteBuffer(writeBuffer, External_Flash_Address, bufferSize);

/* Program Word to SPI Flash memory */
External_Flash_Data[0] = (uint8_t)(Internal_Flash_Data & 0xFF);
External_Flash_Data[1] = (uint8_t)((Internal_Flash_Data & 0xFF00) >> 8);
External_Flash_Data[2] = (uint8_t)((Internal_Flash_Data & 0xFF0000) >> 16);
External_Flash_Data[3] = (uint8_t)((Internal_Flash_Data & 0xFF000000) >> 24);
//OR
//*((uint32_t *)External_Flash_Data) = Internal_Flash_Data;
sFLASH_WriteBuffer(External_Flash_Data, External_Flash_Address, 4);
External_Flash_Address += 4;
}
/* Read Data Buffer from SPI Flash memory */
sFLASH_ReadBuffer(readBuffer, External_Flash_Address, bufferSize);

i = bufferSize & 3;

/* Not an aligned data */
if (i != 0)
/* Is the Data Buffer successfully programmed to SPI Flash memory */
if (0 == memcmp(writeBuffer, readBuffer, bufferSize))
{
/* Program the last word to SPI Flash memory */
Internal_Flash_Data = *((uint32_t *)pBuffer);

/* Program Word to SPI Flash memory */
External_Flash_Data[0] = (uint8_t)(Internal_Flash_Data & 0xFF);
External_Flash_Data[1] = (uint8_t)((Internal_Flash_Data & 0xFF00) >> 8);
External_Flash_Data[2] = (uint8_t)((Internal_Flash_Data & 0xFF0000) >> 16);
External_Flash_Data[3] = (uint8_t)((Internal_Flash_Data & 0xFF000000) >> 24);
//OR
//*((uint32_t *)External_Flash_Data) = Internal_Flash_Data;
sFLASH_WriteBuffer(External_Flash_Data, External_Flash_Address, 4);
External_Flash_Address += bufferSize;
Flash_Update_Index += 1;
}
else
{
/* Erase the problematic SPI Flash pages and back off the chunk index */
External_Flash_Address = ((uint32_t)(External_Flash_Address / sFLASH_PAGESIZE)) * sFLASH_PAGESIZE;
sFLASH_EraseSector(External_Flash_Address);
Flash_Update_Index = (uint16_t)((External_Flash_Address - EXTERNAL_FLASH_OTA_ADDRESS) / bufferSize);
}

LED_Toggle(LED_RGB);

return Flash_Update_Index;

#endif
}

Expand Down