Skip to content

Commit

Permalink
Clean up EEPROM interfaces (MarlinFirmware#17803)
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead authored and Alexander Yasnogor committed Aug 7, 2020
1 parent ba76846 commit 7ca0dac
Show file tree
Hide file tree
Showing 8 changed files with 982 additions and 1,059 deletions.
962 changes: 947 additions & 15 deletions Marlin/src/HAL/DUE/eeprom_flash.cpp

Large diffs are not rendered by default.

1,006 changes: 0 additions & 1,006 deletions Marlin/src/HAL/DUE/eeprom_if_flash.cpp

This file was deleted.

3 changes: 2 additions & 1 deletion Marlin/src/HAL/LPC1768/eeprom_wired.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* with implementations supplied by the framework.
*/

#include "../shared/eeprom_if.h"
#include "../shared/eeprom_api.h"

#ifndef EEPROM_SIZE
Expand All @@ -40,7 +41,7 @@ size_t PersistentStore::capacity() { return EEPROM_SIZE; }
bool PersistentStore::access_finish() { return true; }

bool PersistentStore::access_start() {
TERN_(I2C_EEPROM, eeprom_init());
eeprom_init();
return true;
}

Expand Down
1 change: 1 addition & 0 deletions Marlin/src/HAL/STM32/eeprom_wired.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ size_t PersistentStore::capacity() { return E2END + 1; }
bool PersistentStore::access_finish() { return true; }

bool PersistentStore::access_start() {
eeprom_init();
return true;
}

Expand Down
39 changes: 17 additions & 22 deletions Marlin/src/HAL/STM32_F4_F7/eeprom_flash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,7 @@
//#define FLASH_FLAG_PGSERR FLASH_FLAG_ERSERR
#endif

void ee_init() {
static bool ee_initialized = false;
if (!ee_initialized) {
HAL_FLASH_Unlock();

__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);

/* EEPROM Init */
if (EE_Initialize() != EE_OK)
for (;;) HAL_Delay(1); // Spin forever until watchdog reset

HAL_FLASH_Lock();
ee_initialized = true;
}
}

void ee_write_byte(uint8_t *pos, unsigned char value) {
ee_init();

HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);

Expand All @@ -64,19 +46,32 @@ void ee_write_byte(uint8_t *pos, unsigned char value) {
}

uint8_t ee_read_byte(uint8_t *pos) {
ee_init();

uint16_t data = 0xFF;
const unsigned eeprom_address = (unsigned)pos;
(void)EE_ReadVariable(eeprom_address, &data); // Data unchanged on error

return uint8_t(data);
}

size_t PersistentStore::capacity() { return E2END + 1; }
bool PersistentStore::access_start() { return true; }
bool PersistentStore::access_finish() { return true; }

bool PersistentStore::access_start() {
static bool ee_initialized = false;
if (!ee_initialized) {
HAL_FLASH_Unlock();

__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);

/* EEPROM Init */
if (EE_Initialize() != EE_OK)
for (;;) HAL_Delay(1); // Spin forever until watchdog reset

HAL_FLASH_Lock();
ee_initialized = true;
}
return true;
}

bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
while (size--) {
uint8_t * const p = (uint8_t * const)pos;
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/STM32_F4_F7/eeprom_wired.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
#include "../shared/eeprom_api.h"

size_t PersistentStore::capacity() { return E2END + 1; }
bool PersistentStore::access_start() { return true; }
bool PersistentStore::access_finish() { return true; }

bool PersistentStore::access_start() {
eeprom_init();
return true;
}

Expand Down
18 changes: 7 additions & 11 deletions Marlin/src/HAL/shared/eeprom_if_i2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,18 @@

#include "../../inc/MarlinConfig.h"

#if BOTH(USE_SHARED_EEPROM, I2C_EEPROM)
#if ENABLED(I2C_EEPROM)

#include "../HAL.h"
#include "eeprom_if.h"
#include <Wire.h>

#include "eeprom_if.h"
void eeprom_init() { Wire.begin(); }

#if ENABLED(USE_SHARED_EEPROM)

#ifndef EEPROM_WRITE_DELAY
#define EEPROM_WRITE_DELAY 5
#endif

// ------------------------
// Private Variables
// ------------------------

#ifndef EEPROM_DEVICE_ADDRESS
#define EEPROM_DEVICE_ADDRESS 0x50
#endif
Expand All @@ -52,8 +49,6 @@ static constexpr uint8_t eeprom_device_address = I2C_ADDRESS(EEPROM_DEVICE_ADDRE
// Public functions
// ------------------------

void eeprom_init() { Wire.begin(); }

void eeprom_write_byte(uint8_t *pos, unsigned char value) {
const unsigned eeprom_address = (unsigned)pos;

Expand All @@ -79,4 +74,5 @@ uint8_t eeprom_read_byte(uint8_t *pos) {
return Wire.available() ? Wire.read() : 0xFF;
}

#endif // USE_SHARED_EEPROM && I2C_EEPROM
#endif // USE_SHARED_EEPROM
#endif // I2C_EEPROM
10 changes: 7 additions & 3 deletions Marlin/src/HAL/shared/eeprom_if_spi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@

#include "../../inc/MarlinConfig.h"

#if BOTH(USE_SHARED_EEPROM, SPI_EEPROM)
#if ENABLED(SPI_EEPROM)

#include "../HAL.h"
#include "eeprom_if.h"

void eeprom_init() {}

#if ENABLED(USE_SHARED_EEPROM)

#define CMD_WREN 6 // WREN
#define CMD_READ 2 // WRITE
#define CMD_WRITE 2 // WRITE
Expand Down Expand Up @@ -80,4 +83,5 @@ void eeprom_write_byte(uint8_t* pos, uint8_t value) {
delay(EEPROM_WRITE_DELAY); // wait for page write to complete
}

#endif // USE_SHARED_EEPROM && I2C_EEPROM
#endif // USE_SHARED_EEPROM
#endif // I2C_EEPROM

0 comments on commit 7ca0dac

Please sign in to comment.