00001
00028
00029 #include <string.h>
00030 #include "roboot.h"
00031 #include "stm32f10x.h"
00032
00033
00034
00035 #define UnlockTimeout ((uint32_t)0x0000000F)
00036
00037 static uint32_t FlashUnlock(uint32_t page_address);
00038 static void FlashLock(uint32_t page_address);
00039 static uint32_t FlashErasePage(uint32_t page_address);
00040 static uint32_t FlashWriteData(uint32_t write_addr, const uint8_t *chunk, uint32_t chunk_len);
00041
00042 const ROBOOT_FLASH_Operations_T flash_oper = { FlashUnlock, FlashLock, FlashErasePage, FlashWriteData };
00043
00044
00045
00046
00047
00048 static uint32_t FlashUnlock(uint32_t page_address)
00049 {
00050 (void) page_address;
00051
00052 FLASH_Unlock();
00053
00054 return FLASH_WaitForLastOperation(UnlockTimeout) != FLASH_COMPLETE;
00055
00056 }
00057
00058
00059
00060
00061
00062 static void FlashLock(uint32_t page_address)
00063 {
00064 (void) page_address;
00065
00066 FLASH_Lock();
00067
00068 }
00069
00070
00071
00072
00073
00074 static uint32_t FlashErasePage(uint32_t page_address)
00075 {
00076 return FLASH_ErasePage(page_address) != FLASH_COMPLETE;
00077
00078 }
00079
00080
00081
00082
00083
00084 static uint32_t FlashWriteData(uint32_t write_addr, const uint8_t *chunk, uint32_t chunk_len)
00085 {
00086 uint16_t *data;
00087 uint32_t data_len;
00088 uint32_t prg_addr;
00089
00090 data = (uint16_t*)chunk;
00091 data_len = (chunk_len + 1U) / 2U;
00092 prg_addr = write_addr;
00093
00094 while (data_len)
00095 {
00096 if (FLASH_ProgramHalfWord(prg_addr, *data) != FLASH_COMPLETE)
00097 {
00098 return 1;
00099 }
00100 data++;
00101 data_len--;
00102 prg_addr += 2U;
00103 }
00104
00105 return memcmp((const void*)write_addr, chunk, chunk_len);
00106
00107 }