6 #pragma GCC optimize ("O2") 10 #if CONFIG_HAL_BOARD == HAL_BOARD_F4LIGHT 14 #include "stm32f4xx.h" 31 if(FLASH->SR & 0xE0) FLASH->SR = 0xE0;
32 if(FLASH->SR & FLASH_FLAG_WRPERR) {
36 FLASH->SR |= FLASH_FLAG_WRPERR;
48 FLASH_WaitForLastOperation();
51 if(NewState != DISABLE)
53 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS &= (~OB_WRP);
57 *(__IO uint16_t*)OPTCR_BYTE2_ADDRESS |= (uint16_t)OB_WRP;
67 FLASH_Status sts = FLASH_ProgramHalfWord(addr, data);
69 if(sts != FLASH_COMPLETE ) {
71 if(n_try-- > 0)
goto again;
81 FLASH_Status sts = FLASH_ProgramByte(addr, data);
83 if(sts != FLASH_COMPLETE ) {
86 if(n_try-- > 0)
goto again;
95 FLASH->ACR |= FLASH_ACR_DCEN;
99 FLASH->ACR &= ~FLASH_ACR_DCEN;
112 uint32_t pageEnd = pageBase +
PageSize;
118 for(pageBase += 4; pageBase < pageEnd; pageBase += 4)
119 if (
read_32(pageBase) != 0xFFFFFFFF)
133 int Page_Offset = Page_Address - 0x08000000;
134 uint32_t FLASH_Sector;
136 if(Page_Offset < 0x10000) {
137 FLASH_Sector = Page_Offset / 0x4000;
138 }
else if(Page_Offset < 0x20000) {
141 FLASH_Sector = 4 + Page_Offset / 0x20000;
146 FLASH_Status ret = FLASH_EraseSector(8 * FLASH_Sector, VoltageRange_3);
148 if(ret != FLASH_COMPLETE ) {
150 if(n_try-- > 0)
goto again;
167 uint16_t data =
read_16(pageBase);
169 data =
read_16(pageBase + 2) + 1;
173 printf(
"\nEEprom erase page %d\n ", (uint16_t)((pageBase & 0x00ffffff) / 0x4000) );
175 gcs().
send_text(MAV_SEVERITY_INFO,
"EEprom erase page %d", (uint16_t)((pageBase & 0x00ffffff) / 0x4000) );
179 if (status == FLASH_COMPLETE)
180 status =
write_16(pageBase + 2, data);
198 if (status != FLASH_COMPLETE)
241 uint16_t varAddress, nextAddress;
243 uint32_t pageEnd = pageBase +
PageSize;
244 uint16_t mycount = 0;
246 for (pageBase += 6; pageBase < pageEnd; pageBase += 4) {
247 varAddress =
read_16(pageBase);
253 for(idx = pageBase + 4; idx < pageEnd; idx += 4) {
255 if ((nextAddress & ADDRESS_MASK) == (varAddress & ADDRESS_MASK)) {
276 uint32_t oldEnd, newEnd;
277 uint32_t oldIdx, newIdx, idx;
278 uint16_t address, data, found;
285 for (newIdx = newPage + 4; newIdx < newEnd; newIdx += 4)
286 if (
read_32(newIdx) == 0xFFFFFFFF)
288 if (newIdx >= newEnd)
291 oldEnd = oldPage + 4;
292 oldIdx = oldPage + (PageSize - 2);
294 for (; oldIdx > oldEnd; oldIdx -= 4) {
296 if ( address == SkipAddress || (address &
FLAGS_MASK)!=0)
301 for (idx = newPage + 6; idx < newIdx; idx += 4){
310 if (newIdx < newEnd) {
314 if (status != FLASH_COMPLETE)
318 if (status != FLASH_COMPLETE)
334 if (status != FLASH_COMPLETE)
354 uint32_t idx, pageBase, pageEnd, newPage;
367 for (idx = pageEnd - 2; idx > pageBase; idx -= 4) {
370 if (old_data == Data){
373 if (old_data == 0xFFFF || (~old_data & Data)==0 ) {
375 if (status == FLASH_COMPLETE &&
read_16(idx - 2) == Data)
383 for (idx = pageBase + 4; idx < pageEnd; idx += 4){
384 if (
read_32(idx) == 0xFFFFFFFF){
386 if (status != FLASH_COMPLETE)
389 if (status != FLASH_COMPLETE)
390 return 0x90 + status;
408 if (status != FLASH_COMPLETE)
412 status =
write_16(newPage + 4, Data);
413 if (status != FLASH_COMPLETE)
416 status =
write_16(newPage + 6, Address);
417 if (status != FLASH_COMPLETE)
436 uint16_t status0, status1, erased0;
446 if (erased0 == 0xffff) erased0 = 0;
449 printf(
"\nEEprom write cycles %d\n ", erased0);
485 if (status != FLASH_COMPLETE)
510 if (status != FLASH_COMPLETE)
556 if (status != FLASH_COMPLETE)
588 if (fs != FLASH_COMPLETE)
goto error;
597 if(n_try-- > 0)
goto again;
644 uint32_t pageBase, pageEnd;
653 pageEnd = pageBase + (
PageSize - 2);
657 uint32_t ptr = pageEnd;
660 for (pageBase += 6; ptr >= pageBase; ptr -= 4){
688 if (Address == 0xFFFF)
#define EEPROM_RECEIVE_DATA
int printf(const char *fmt,...)
FLASH_Status _ErasePage(uint32_t)
Erase page with increment erase counter (page + 2)
uint16_t read(uint16_t address, uint16_t *data)
Returns the last stored variable data, if found, which correspond to the passed virtual address...
uint16_t _format(void)
Erases PAGE0 and PAGE1 and writes EEPROM_VALID_PAGE / 0 header to PAGE0.
uint16_t _VerifyPageFullWriteVariable(uint16_t, uint16_t)
Verify if active page is full and Writes variable in EEPROM.
Interface definition for the various Ground Control System.
#define assert_param(expr)
uint16_t _CheckErasePage(uint32_t, uint16_t)
Check page for blank and erase it.
uint16_t _CheckPage(uint32_t, uint16_t)
Check page for blank.
static uint32_t read_32(uint32_t addr)
uint16_t _PageTransfer(uint32_t, uint32_t, uint16_t)
Transfers last updated variables data from the full Page to an empty one.
static FLASH_Status write_8(uint32_t addr, uint8_t data)
static void reset_flash_errors()
static void FLASH_OB_WRPConfig(uint32_t OB_WRP, FunctionalState NewState)
uint16_t init(uint32_t, uint32_t, uint32_t)
uint16_t _GetVariablesCount(uint32_t, uint16_t)
Calculate unique variables in EEPROM.
void send_text(MAV_SEVERITY severity, const char *fmt,...)
uint16_t write(uint16_t address, uint16_t data)
Writes/upadtes variable data in EEPROM.
#define EEPROM_VALID_PAGE
static void FLASH_Unlock_dis()
static FLASH_Status write_16(uint32_t addr, uint16_t data)
uint16_t format(void)
Erases PAGE0 and PAGE1 and writes EEPROM_VALID_PAGE / 0 header to PAGE0.
#define error(fmt, args ...)
static FLASH_Status _ErasePageByAddress(uint32_t Page_Address)
Erases a specified FLASH page by address.
static void FLASH_Lock_check()
uint32_t _FindValidPage(void)
Find valid Page for write or read operation.
#define EEPROM_DEFAULT_DATA
uint16_t count(uint16_t *data)
Return number of variable.
uint16_t erases(uint16_t *)
Returns the erase counter for current page.
static uint32_t read_16(uint32_t addr)