Skip to content

Commit

Permalink
Ender 3 V2 BL24C16 EEPROM support (MarlinFirmware#18758)
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead authored and vgadreau committed Dec 9, 2020
1 parent 8ab9256 commit ccfd4e5
Show file tree
Hide file tree
Showing 15 changed files with 495 additions and 303 deletions.
81 changes: 81 additions & 0 deletions Marlin/src/HAL/STM32F1/eeprom_bl24cxx.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/

/**
* PersistentStore for Arduino-style EEPROM interface
* with simple implementations supplied by Marlin.
*/

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

#if ENABLED(IIC_BL24CXX_EEPROM)

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

//
// PersistentStore
//

#ifndef MARLIN_EEPROM_SIZE
#error "MARLIN_EEPROM_SIZE is required for IIC_BL24CXX_EEPROM."
#endif

size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }

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

bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
while (size--) {
uint8_t v = *value;
uint8_t * const p = (uint8_t * const)pos;
// EEPROM has only ~100,000 write cycles,
// so only write bytes that have changed!
if (v != eeprom_read_byte(p)) {
eeprom_write_byte(p, v);
delay(2);
if (eeprom_read_byte(p) != v) {
SERIAL_ECHO_MSG(STR_ERR_EEPROM_WRITE);
return true;
}
}
crc16(crc, &v, 1);
pos++;
value++;
}
return false;
}

bool PersistentStore::read_data(int &pos, uint8_t* value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
do {
uint8_t * const p = (uint8_t * const)pos;
uint8_t c = eeprom_read_byte(p);
if (writing) *value = c;
crc16(crc, &c, 1);
pos++;
value++;
} while (--size);
return false;
}

#endif // IIC_BL24CXX_EEPROM
51 changes: 51 additions & 0 deletions Marlin/src/HAL/STM32F1/eeprom_if_iic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/

/**
* Platform-independent Arduino functions for I2C EEPROM.
* Enable USE_SHARED_EEPROM if not supplied by the framework.
*/

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

#if ENABLED(IIC_BL24CXX_EEPROM)

#include "../../libs/BL24CXX.h"
#include "../shared/eeprom_if.h"

void eeprom_init() { BL24CXX::init(); }

// ------------------------
// Public functions
// ------------------------

void eeprom_write_byte(uint8_t *pos, unsigned char value) {
const unsigned eeprom_address = (unsigned)pos;
return BL24CXX::writeOneByte(eeprom_address, value);
}

uint8_t eeprom_read_byte(uint8_t *pos) {
const unsigned eeprom_address = (unsigned)pos;
return BL24CXX::readOneByte(eeprom_address);
}

#endif // IIC_BL24CXX_EEPROM
20 changes: 19 additions & 1 deletion Marlin/src/HAL/shared/eeprom_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,38 @@

class PersistentStore {
public:

// Total available persistent storage space (in bytes)
static size_t capacity();

// Prepare to read or write
static bool access_start();

// Housecleaning after read or write
static bool access_finish();

// Write one or more bytes of data and update the CRC
// Return 'true' on write error
static bool write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc);

// Read one or more bytes of data and update the CRC
// Return 'true' on read error
static bool read_data(int &pos, uint8_t* value, size_t size, uint16_t *crc, const bool writing=true);
static size_t capacity();

// Write one or more bytes of data
// Return 'true' on write error
static inline bool write_data(const int pos, const uint8_t* value, const size_t size=sizeof(uint8_t)) {
int data_pos = pos;
uint16_t crc = 0;
return write_data(data_pos, value, size, &crc);
}

// Write a single byte of data
// Return 'true' on write error
static inline bool write_data(const int pos, const uint8_t value) { return write_data(pos, &value); }

// Read one or more bytes of data
// Return 'true' on read error
static inline bool read_data(const int pos, uint8_t* value, const size_t size=1) {
int data_pos = pos;
uint16_t crc = 0;
Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/MarlinCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
#endif

#if ENABLED(IIC_BL24CXX_EEPROM)
#include "lcd/dwin/eeprom_BL24CXX.h"
#include "libs/BL24CXX.h"
#endif

#if ENABLED(DIRECT_STEPPING)
Expand Down Expand Up @@ -1171,7 +1171,7 @@ void setup() {
#if ENABLED(IIC_BL24CXX_EEPROM)
BL24CXX::init();
const uint8_t err = BL24CXX::check();
SERIAL_ECHO_TERNARY(err, "I2C_EEPROM Check ", "failed", "succeeded", "!\n");
SERIAL_ECHO_TERNARY(err, "BL24CXX Check ", "failed", "succeeded", "!\n");
#endif

#if ENABLED(DWIN_CREALITY_LCD)
Expand Down
36 changes: 36 additions & 0 deletions Marlin/src/gcode/eeprom/M500-M504.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,47 @@ void GcodeSuite::M502() {
#endif // !DISABLE_M503

#if ENABLED(EEPROM_SETTINGS)

#if ENABLED(MARLIN_DEV_MODE)
#include "../../libs/hex_print_routines.h"
#endif

/**
* M504: Validate EEPROM Contents
*/
void GcodeSuite::M504() {
#if ENABLED(MARLIN_DEV_MODE)
const bool dowrite = parser.seenval('W');
if (dowrite || parser.seenval('R')) {
uint8_t val = 0;
int addr = parser.value_ushort();
if (dowrite) {
val = parser.byteval('V');
persistentStore.write_data(addr, &val);
SERIAL_ECHOLNPAIR("Wrote address ", addr, " with ", int(val));
}
else {
if (parser.seenval('T')) {
const int endaddr = parser.value_ushort();
while (addr <= endaddr) {
persistentStore.read_data(addr, &val);
SERIAL_ECHOLNPAIR("0x", hex_word(addr), ":", hex_byte(val));
addr++;
safe_delay(10);
}
SERIAL_EOL();
}
else {
persistentStore.read_data(addr, &val);
SERIAL_ECHOLNPAIR("Read address ", addr, " and got ", int(val));
}
}
return;
}
#endif

if (settings.validate())
SERIAL_ECHO_MSG("EEPROM OK");
}

#endif
3 changes: 3 additions & 0 deletions Marlin/src/inc/Conditionals_post.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
#define USE_EMULATED_EEPROM 1
#elif ANY(I2C_EEPROM, SPI_EEPROM)
#define USE_WIRED_EEPROM 1
#elif ENABLED(IIC_BL24CXX_EEPROM)
// nothing
#else
#define USE_FALLBACK_EEPROM 1
#endif
Expand All @@ -60,6 +62,7 @@
#undef SDCARD_EEPROM_EMULATION
#undef SRAM_EEPROM_EMULATION
#undef FLASH_EEPROM_EMULATION
#undef IIC_BL24CXX_EEPROM
#endif

#ifdef TEENSYDUINO
Expand Down
9 changes: 7 additions & 2 deletions Marlin/src/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -2131,8 +2131,13 @@ static_assert(hbm[Z_AXIS] >= 0, "HOMING_BUMP_MM.Z must be greater than or equal
*/
#if ENABLED(EEPROM_SETTINGS)
#if 1 < 0 \
+ ENABLED(I2C_EEPROM) + ENABLED(SPI_EEPROM) + ENABLED(QSPI_EEPROM) \
+ ENABLED(SDCARD_EEPROM_EMULATION) + ENABLED(FLASH_EEPROM_EMULATION) + ENABLED(SRAM_EEPROM_EMULATION)
+ ENABLED(I2C_EEPROM) \
+ ENABLED(SPI_EEPROM) \
+ ENABLED(QSPI_EEPROM) \
+ ENABLED(SDCARD_EEPROM_EMULATION) \
+ ENABLED(FLASH_EEPROM_EMULATION) \
+ ENABLED(SRAM_EEPROM_EMULATION) \
+ ENABLED(IIC_BL24CXX_EEPROM)
#error "Please select only one method of EEPROM Persistent Storage."
#endif
#endif
Expand Down
9 changes: 5 additions & 4 deletions Marlin/src/lcd/dwin/dwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,11 @@ int temphot = 0, tempbed = 0;
float zprobe_zoffset = 0;
float last_zoffset = 0, last_probe_zoffset = 0;

#define FONT_EEPROM_OFFSET 0
#define DWIN_LANGUAGE_EEPROM_ADDRESS 0x01 // Between 0x01 and 0x63 (EEPROM_OFFSET-1)
// BL24CXX::check() uses 0x00

void lcd_select_language(void) {
BL24CXX::read(FONT_EEPROM_OFFSET, (uint8_t*)&HMI_flag.language_flag, sizeof(HMI_flag.language_flag));
BL24CXX::read(DWIN_LANGUAGE_EEPROM_ADDRESS, (uint8_t*)&HMI_flag.language_flag, sizeof(HMI_flag.language_flag));
if (HMI_flag.language_flag)
DWIN_JPG_CacheTo1(Language_Chinese);
else
Expand All @@ -198,12 +199,12 @@ void lcd_select_language(void) {
void set_english_to_eeprom(void) {
HMI_flag.language_flag = 0;
DWIN_JPG_CacheTo1(Language_English);
BL24CXX::write(FONT_EEPROM_OFFSET, (uint8_t*)&HMI_flag.language_flag, sizeof(HMI_flag.language_flag));
BL24CXX::write(DWIN_LANGUAGE_EEPROM_ADDRESS, (uint8_t*)&HMI_flag.language_flag, sizeof(HMI_flag.language_flag));
}
void set_chinese_to_eeprom(void) {
HMI_flag.language_flag = 1;
DWIN_JPG_CacheTo1(Language_Chinese);
BL24CXX::write(FONT_EEPROM_OFFSET, (uint8_t*)&HMI_flag.language_flag, sizeof(HMI_flag.language_flag));
BL24CXX::write(DWIN_LANGUAGE_EEPROM_ADDRESS, (uint8_t*)&HMI_flag.language_flag, sizeof(HMI_flag.language_flag));
}

void show_plus_or_minus(uint8_t size, uint16_t bColor, uint8_t iNum, uint8_t fNum, uint16_t x, uint16_t y, long value) {
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/lcd/dwin/dwin.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

#include "dwin_lcd.h"
#include "rotary_encoder.h"
#include "eeprom_BL24CXX.h"
#include "../../libs/BL24CXX.h"

#include <stdint.h>

Expand Down
Loading

0 comments on commit ccfd4e5

Please sign in to comment.