Skip to content

Commit

Permalink
Increment size of emulated EEPROM
Browse files Browse the repository at this point in the history
Defines paremeter Pages for use more than one page per bank on emulated EEPROM.
  • Loading branch information
svmac committed Sep 14, 2019
1 parent a3a5686 commit f4e3c29
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
31 changes: 23 additions & 8 deletions STM32F1/libraries/EEPROM/EEPROM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
uint16 EEPROMClass::EE_CheckPage(uint32 pageBase, uint16 status)
{
uint32 pageEnd = pageBase + (uint32)PageSize;
uint32 pageEnd = pageBase + (uint32)PageSize * Pages;

// Page Status not EEPROM_ERASED and not a "state"
if ((*(__IO uint16*)pageBase) != EEPROM_ERASED && (*(__IO uint16*)pageBase) != status)
Expand Down Expand Up @@ -40,6 +40,10 @@ FLASH_Status EEPROMClass::EE_ErasePage(uint32 pageBase)
FlashStatus = FLASH_ErasePage(pageBase);
if (FlashStatus == FLASH_COMPLETE)
FlashStatus = FLASH_ProgramHalfWord(pageBase + 2, data);
for (size_t i = 1; i < Pages; i++)
{
FLASH_ErasePage(pageBase + PageSize * i);
}

return FlashStatus;
}
Expand Down Expand Up @@ -95,7 +99,7 @@ uint16 EEPROMClass::EE_GetVariablesCount(uint32 pageBase, uint16 skipAddress)
{
uint16 varAddress, nextAddress;
uint32 idx;
uint32 pageEnd = pageBase + (uint32)PageSize;
uint32 pageEnd = pageBase + (uint32)PageSize * Pages;
uint16 count = 0;

for (pageBase += 6; pageBase < pageEnd; pageBase += 4)
Expand Down Expand Up @@ -136,7 +140,7 @@ uint16 EEPROMClass::EE_PageTransfer(uint32 newPage, uint32 oldPage, uint16 SkipA
FLASH_Status FlashStatus;

// Transfer process: transfer variables from old to the new active page
newEnd = newPage + ((uint32)PageSize);
newEnd = newPage + ((uint32)PageSize * Pages);

// Find first free element in new page
for (newIdx = newPage + 4; newIdx < newEnd; newIdx += 4)
Expand All @@ -146,7 +150,7 @@ uint16 EEPROMClass::EE_PageTransfer(uint32 newPage, uint32 oldPage, uint16 SkipA
return EEPROM_OUT_SIZE;

oldEnd = oldPage + 4;
oldIdx = oldPage + (uint32)(PageSize - 2);
oldIdx = oldPage + (uint32)(PageSize * Pages - 2);

for (; oldIdx > oldEnd; oldIdx -= 4)
{
Expand Down Expand Up @@ -219,7 +223,7 @@ uint16 EEPROMClass::EE_VerifyPageFullWriteVariable(uint16 Address, uint16 Data)
return EEPROM_NO_VALID_PAGE;

// Get the valid Page end Address
pageEnd = pageBase + PageSize; // Set end of page
pageEnd = pageBase + PageSize * Pages; // Set end of page

for (idx = pageEnd - 2; idx > pageBase; idx -= 4)
{
Expand Down Expand Up @@ -254,7 +258,7 @@ uint16 EEPROMClass::EE_VerifyPageFullWriteVariable(uint16 Address, uint16 Data)
// Empty slot not found, need page transfer
// Calculate unique variables in page
count = EE_GetVariablesCount(pageBase, Address) + 1;
if (count >= (PageSize / 4 - 1))
if (count >= (PageSize * Pages / 4 - 1))
return EEPROM_OUT_SIZE;

if (pageBase == PageBase1)
Expand Down Expand Up @@ -284,6 +288,7 @@ EEPROMClass::EEPROMClass(void)
PageBase0 = EEPROM_PAGE0_BASE;
PageBase1 = EEPROM_PAGE1_BASE;
PageSize = EEPROM_PAGE_SIZE;
Pages = 1;
Status = EEPROM_NOT_INIT;
}

Expand All @@ -292,6 +297,16 @@ uint16 EEPROMClass::init(uint32 pageBase0, uint32 pageBase1, uint32 pageSize)
PageBase0 = pageBase0;
PageBase1 = pageBase1;
PageSize = pageSize;
Pages = 1;
return init();
}

uint16 EEPROMClass::init(uint32 pageBase0, uint32 pageBase1, uint32 pageSize, uint16 pages)
{
PageBase0 = pageBase0;
PageBase1 = pageBase1;
PageSize = pageSize;
Pages = pages;
return init();
}

Expand Down Expand Up @@ -481,7 +496,7 @@ uint16 EEPROMClass::read(uint16 Address, uint16 *Data)
return EEPROM_NO_VALID_PAGE;

// Get the valid Page end Address
pageEnd = pageBase + ((uint32)(PageSize - 2));
pageEnd = pageBase + ((uint32)(PageSize * Pages - 2));

// Check each active page address starting from end
for (pageBase += 6; pageEnd >= pageBase; pageEnd -= 4)
Expand Down Expand Up @@ -564,7 +579,7 @@ uint16 EEPROMClass::count(uint16 *Count)

uint16 EEPROMClass::maxcount(void)
{
return ((PageSize / 4)-1);
return ((PageSize * Pages / 4)-1);
}

EEPROMClass EEPROM;
2 changes: 2 additions & 0 deletions STM32F1/libraries/EEPROM/EEPROM.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class EEPROMClass

uint16 init(void);
uint16 init(uint32, uint32, uint32);
uint16 init(uint32, uint32, uint32, uint16);

uint16 format(void);

Expand All @@ -75,6 +76,7 @@ class EEPROMClass
uint32 PageBase0;
uint32 PageBase1;
uint32 PageSize;
uint16 Pages;
uint16 Status;
private:
FLASH_Status EE_ErasePage(uint32);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
int ledPin = 13; // LED connected to digital pin 13
const char HELP_MSG[] = "Press :\r\n" \
" 0 display configuration\r\n" \
" 1 set configuration to 0x801F000 / 0x801F800 / 0x400 (RB MCU)\r\n" \
" 2 set configuration to 0x801F000 / 0x801F800 / 0x800 (ZE/RE MCU)\r\n" \
" 1 set configuration to 0x801F000 / 0x801F800 / 0x400 / 2 (RB MCU)\r\n" \
" 2 set configuration to 0x801F000 / 0x801F800 / 0x800 / 1 (ZE/RE MCU)\r\n" \
" 3 write/read variable\r\n" \
" 4 increment address\r\n" \
" 5 display pages top/bottom\r\n" \
Expand Down Expand Up @@ -40,13 +40,15 @@ void loop()
EEPROM.PageBase0 = 0x801F000;
EEPROM.PageBase1 = 0x801F800;
EEPROM.PageSize = 0x400;
EEPROM.Pages = 2;
DisplayConfig();
}
else if (cmd == '2')
{
EEPROM.PageBase0 = 0x801F000;
EEPROM.PageBase1 = 0x801F800;
EEPROM.PageSize = 0x800;
EEPROM.Pages = 1;
DisplayConfig();
}
else if (cmd == '3')
Expand Down Expand Up @@ -111,6 +113,8 @@ void DisplayConfig(void)
Serial.print (" (");
Serial.print (EEPROM.PageSize, DEC);
Serial.println(")");
Serial.print ("EEPROM.Pages : ");
Serial.println(EEPROM.Pages, DEC);
}

void DisplayHex(uint16 value)
Expand Down Expand Up @@ -149,7 +153,7 @@ void DisplayPagesEnd(uint32 endIndex)
{
Serial.println("Page 0 Bottom Page 1");

for (uint32 idx = EEPROM.PageSize - endIndex; idx < EEPROM.PageSize; idx += 4)
for (uint32 idx = EEPROM.PageSize * EEPROM.Pages - endIndex; idx < EEPROM.PageSize * EEPROM.Pages; idx += 4)
{
Serial.print (EEPROM.PageBase0 + idx, HEX);
Serial.print (" : ");
Expand Down

0 comments on commit f4e3c29

Please sign in to comment.