LPC176x SPI / I2C PersistentStore (#17651)

This commit is contained in:
randellhodges 2020-04-28 02:27:55 -05:00 committed by GitHub
parent 7c3909bc3f
commit 5f7a75979f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
29 changed files with 132 additions and 210 deletions

View File

@ -992,7 +992,7 @@ void eeprom_write_byte(uint8_t* addr, uint8_t value) {
ee_Write((uint32_t)addr, value);
}
void eeprom_update_block(const void* __src, void* __dst, size_t __n) {
void eeprom_update_block(const void *__src, void *__dst, size_t __n) {
uint8_t* dst = (uint8_t*)__dst;
const uint8_t* src = (const uint8_t*)__src;
while (__n--) {
@ -1002,7 +1002,7 @@ void eeprom_update_block(const void* __src, void* __dst, size_t __n) {
}
}
void eeprom_read_block(void* __dst, const void* __src, size_t __n) {
void eeprom_read_block(void *__dst, const void *__src, size_t __n) {
uint8_t* dst = (uint8_t*)__dst;
uint8_t* src = (uint8_t*)__src;
while (__n--) {

View File

@ -0,0 +1,86 @@
/**
* 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 <http://www.gnu.org/licenses/>.
*
*/
/**
* I2C/SPI EEPROM interface for LPC1768
*/
#ifdef TARGET_LPC1768
#include "../../inc/MarlinConfig.h"
#if USE_WIRED_EEPROM
#include "../shared/eeprom_api.h"
#include <Wire.h>
#ifndef EEPROM_SIZE
#define EEPROM_SIZE 0x8000 // 32kB
#endif
bool PersistentStore::access_start() {
TERN_(SPI_EEPROM, 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;
// EEPROM has only ~100,000 write cycles,
// so only write bytes that have changed!
uint8_t * const p = (uint8_t * const)pos;
if (v != eeprom_read_byte(p)) {
eeprom_write_byte(p, v);
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 {
// Read from external EEPROM
const uint8_t c = eeprom_read_byte((uint8_t*)pos);
if (writing) *value = c;
crc16(crc, &c, 1);
pos++;
value++;
} while (--size);
return false;
}
size_t PersistentStore::capacity() { return EEPROM_SIZE; }
#endif // USE_WIRED_EEPROM
#endif // TARGET_LPC1768

View File

@ -28,13 +28,8 @@
#include "../shared/eeprom_api.h"
bool PersistentStore::access_start() {
return true;
}
bool PersistentStore::access_finish() {
return true;
}
bool PersistentStore::access_start() { 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--) {
@ -84,13 +79,7 @@ bool PersistentStore::read_data(int &pos, uint8_t* value, size_t size, uint16_t
}
size_t PersistentStore::capacity() {
return (
#if USE_WIRED_EEPROM
E2END + 1
#else
4096 // 4kB
#endif
);
return TERN(USE_WIRED_EEPROM, E2END + 1, 4096); // 4K for emulated
}
#endif // USE_WIRED_EEPROM || SRAM_EEPROM_EMULATION

View File

@ -21,7 +21,7 @@
*/
#pragma once
// If no real EEPROM, Flash emulation, or SRAM emulation is available fall back to SD emulation
// If no real or emulated EEPROM selected, fall back to SD emulation
#if USE_FALLBACK_EEPROM
#define SDCARD_EEPROM_EMULATION
#endif

View File

@ -80,7 +80,7 @@ void eeprom_write_byte(uint8_t *pos, unsigned char value) {
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);
uint16_t eeprom_address = unsigned(pos);
const unsigned eeprom_address = (unsigned)pos;
if (EE_WriteVariable(eeprom_address, uint16_t(value)) != EE_OK)
for (;;) HAL_Delay(1); // Spin forever until watchdog reset
@ -91,7 +91,7 @@ uint8_t eeprom_read_byte(uint8_t *pos) {
eeprom_init();
uint16_t data = 0xFF;
uint16_t eeprom_address = unsigned(pos);
const unsigned eeprom_address = (unsigned)pos;
(void)EE_ReadVariable(eeprom_address, &data); // Data unchanged on error
return uint8_t(data);
@ -101,7 +101,7 @@ void eeprom_read_block(void *__dst, const void *__src, size_t __n) {
eeprom_init();
uint16_t data = 0xFF;
uint16_t eeprom_address = unsigned(__src);
const unsigned eeprom_address = (unsigned)__src;
LOOP_L_N(c, __n) {
EE_ReadVariable(eeprom_address+c, &data);
*((uint8_t*)__dst + c) = data;

View File

@ -26,4 +26,5 @@
#undef SRAM_EEPROM_EMULATION
#undef SDCARD_EEPROM_EMULATION
#define FLASH_EEPROM_EMULATION
#warning "Forcing use of FLASH_EEPROM_EMULATION."
#endif

View File

@ -35,44 +35,50 @@
#include "../HAL.h"
#include <Wire.h>
#ifndef EEPROM_WRITE_DELAY
#define EEPROM_WRITE_DELAY 5
#endif
// ------------------------
// Private Variables
// ------------------------
static constexpr uint8_t eeprom_device_address = I2C_ADDRESS(0x50);
#ifndef EEPROM_DEVICE_ADDRESS
#define EEPROM_DEVICE_ADDRESS 0x50
#endif
static constexpr uint8_t eeprom_device_address = I2C_ADDRESS(EEPROM_DEVICE_ADDRESS);
// ------------------------
// Public functions
// ------------------------
static void eeprom_init() {
Wire.begin();
}
static void eeprom_init() { Wire.begin(); }
void eeprom_write_byte(uint8_t *pos, unsigned char value) {
unsigned eeprom_address = (unsigned) pos;
eeprom_init();
const unsigned eeprom_address = (unsigned)pos;
Wire.beginTransmission(eeprom_device_address);
Wire.write((int)(eeprom_address >> 8)); // MSB
Wire.write((int)(eeprom_address & 0xFF)); // LSB
Wire.write(int(eeprom_address >> 8)); // MSB
Wire.write(int(eeprom_address & 0xFF)); // LSB
Wire.write(value);
Wire.endTransmission();
// wait for write cycle to complete
// this could be done more efficiently with "acknowledge polling"
delay(5);
delay(EEPROM_WRITE_DELAY);
}
// WARNING: address is a page address, 6-bit end will wrap around
// also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes
void eeprom_update_block(const void *pos, void* eeprom_address, size_t n) {
void eeprom_update_block(const void *pos, void *__dst, size_t n) {
const unsigned eeprom_address = (unsigned)__dst;
eeprom_init();
Wire.beginTransmission(eeprom_device_address);
Wire.write((int)((unsigned)eeprom_address >> 8)); // MSB
Wire.write((int)((unsigned)eeprom_address & 0xFF)); // LSB
Wire.write(int(eeprom_address >> 8)); // MSB
Wire.write(int(eeprom_address & 0xFF)); // LSB
Wire.endTransmission();
uint8_t *ptr = (uint8_t*)pos;
@ -83,37 +89,37 @@ void eeprom_update_block(const void *pos, void* eeprom_address, size_t n) {
if (flag) {
Wire.beginTransmission(eeprom_device_address);
Wire.write((int)((unsigned)eeprom_address >> 8)); // MSB
Wire.write((int)((unsigned)eeprom_address & 0xFF)); // LSB
Wire.write(int(eeprom_address >> 8)); // MSB
Wire.write(int(eeprom_address & 0xFF)); // LSB
Wire.write((uint8_t*)pos, n);
Wire.endTransmission();
// wait for write cycle to complete
// this could be done more efficiently with "acknowledge polling"
delay(5);
delay(EEPROM_WRITE_DELAY);
}
}
uint8_t eeprom_read_byte(uint8_t *pos) {
unsigned eeprom_address = (unsigned)pos;
eeprom_init();
const unsigned eeprom_address = (unsigned)pos;
Wire.beginTransmission(eeprom_device_address);
Wire.write((int)(eeprom_address >> 8)); // MSB
Wire.write((int)(eeprom_address & 0xFF)); // LSB
Wire.write(int(eeprom_address >> 8)); // MSB
Wire.write(int(eeprom_address & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(eeprom_device_address, (byte)1);
return Wire.available() ? Wire.read() : 0xFF;
}
// Don't read more than 30..32 bytes at a time!
void eeprom_read_block(void* pos, const void* eeprom_address, size_t n) {
void eeprom_read_block(void* pos, const void *__dst, size_t n) {
const unsigned eeprom_address = (unsigned)__dst;
eeprom_init();
Wire.beginTransmission(eeprom_device_address);
Wire.write((int)((unsigned)eeprom_address >> 8)); // MSB
Wire.write((int)((unsigned)eeprom_address & 0xFF)); // LSB
Wire.write(int(eeprom_address >> 8)); // MSB
Wire.write(int(eeprom_address & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(eeprom_device_address, (byte)n);
for (byte c = 0; c < n; c++ )

View File

@ -35,6 +35,10 @@
#define CMD_READ 2 // WRITE
#define CMD_WRITE 2 // WRITE
#ifndef EEPROM_WRITE_DELAY
#define EEPROM_WRITE_DELAY 7
#endif
uint8_t eeprom_read_byte(uint8_t* pos) {
uint8_t v;
uint8_t eeprom_temp[3];
@ -90,7 +94,7 @@ void eeprom_write_byte(uint8_t* pos, uint8_t value) {
spiSend(SPI_CHAN_EEPROM1, value);
WRITE(SPI_EEPROM1_CS, HIGH);
delay(7); // wait for page write to complete
delay(EEPROM_WRITE_DELAY); // wait for page write to complete
}
void eeprom_update_block(const void* src, void* eeprom_address, size_t n) {
@ -112,7 +116,7 @@ void eeprom_update_block(const void* src, void* eeprom_address, size_t n) {
spiSend(SPI_CHAN_EEPROM1, (const uint8_t*)src, n);
WRITE(SPI_EEPROM1_CS, HIGH);
delay(7); // wait for page write to complete
delay(EEPROM_WRITE_DELAY); // wait for page write to complete
}
#endif // SPI_EEPROM

View File

@ -31,14 +31,6 @@
#define BOARD_INFO_NAME "AZSMZ MINI"
//
// EEPROM
//
#if NONE(FLASH_EEPROM_EMULATION, SDCARD_EEPROM_EMULATION)
#define FLASH_EEPROM_EMULATION
//#define SDCARD_EEPROM_EMULATION
#endif
//
// Servos
//

View File

@ -38,14 +38,6 @@
#define BOARD_INFO_NAME "BIQU Thunder B300 V1.0"
#endif
//
// EEPROM
//
#if NONE(FLASH_EEPROM_EMULATION, SDCARD_EEPROM_EMULATION)
#define FLASH_EEPROM_EMULATION
//#define SDCARD_EEPROM_EMULATION
#endif
//
// Limit Switches
//

View File

@ -36,14 +36,6 @@
#define BOARD_INFO_NAME "BIQU BQ111-A4"
//
// EEPROM
//
#if NONE(FLASH_EEPROM_EMULATION, SDCARD_EEPROM_EMULATION)
#define FLASH_EEPROM_EMULATION
//#define SDCARD_EEPROM_EMULATION
#endif
//
// Limit Switches
//

View File

@ -23,14 +23,6 @@
#define BOARD_INFO_NAME "BIGTREE SKR 1.1"
//
// EEPROM
//
#if NONE(FLASH_EEPROM_EMULATION, SDCARD_EEPROM_EMULATION)
#define FLASH_EEPROM_EMULATION
//#define SDCARD_EEPROM_EMULATION
#endif
//
// Limit Switches
//

View File

@ -23,14 +23,6 @@
#define BOARD_INFO_NAME "BIGTREE SKR 1.3"
//
// EEPROM
//
#if NONE(FLASH_EEPROM_EMULATION, SDCARD_EEPROM_EMULATION)
#define FLASH_EEPROM_EMULATION
//#define SDCARD_EEPROM_EMULATION
#endif
//
// Trinamic Stallguard pins
//

View File

@ -25,14 +25,6 @@
#define BOARD_INFO_NAME "BIGTREE SKR 1.4"
#endif
//
// EEPROM
//
#if NONE(FLASH_EEPROM_EMULATION, SDCARD_EEPROM_EMULATION)
#define FLASH_EEPROM_EMULATION
//#define SDCARD_EEPROM_EMULATION
#endif
//
// SD Connection
//

View File

@ -32,10 +32,6 @@
// Ignore temp readings during development.
//#define BOGUS_TEMPERATURE_GRACE_PERIOD 2000
#if DISABLED(SDCARD_EEPROM_EMULATION)
#define FLASH_EEPROM_EMULATION
#endif
//
// Steppers
//

View File

@ -30,14 +30,6 @@
// Ignore temp readings during develpment.
//#define BOGUS_TEMPERATURE_GRACE_PERIOD 2000
//
// EEPROM
//
#if NONE(FLASH_EEPROM_EMULATION, SDCARD_EEPROM_EMULATION)
#define FLASH_EEPROM_EMULATION
//#define SDCARD_EEPROM_EMULATION
#endif
//
// Enable 12MHz clock output on P1.27 pin to sync TMC2208 chip clocks
//

View File

@ -38,14 +38,6 @@
#define BOARD_WEBSITE_URL "github.com/makerbase-mks/MKS-SBASE"
#endif
//
// EEPROM
//
#if NONE(FLASH_EEPROM_EMULATION, SDCARD_EEPROM_EMULATION)
#define FLASH_EEPROM_EMULATION
//#define SDCARD_EEPROM_EMULATION
#endif
#define LED_PIN P1_18 // Used as a status indicator
#define LED2_PIN P1_19
#define LED3_PIN P1_20

View File

@ -32,14 +32,6 @@
#define BOARD_INFO_NAME "MKS SGen-L"
#define BOARD_WEBSITE_URL "github.com/makerbase-mks/MKS-SGEN_L"
//
// EEPROM
//
#if NONE(FLASH_EEPROM_EMULATION, SDCARD_EEPROM_EMULATION)
#define FLASH_EEPROM_EMULATION
//#define SDCARD_EEPROM_EMULATION
#endif
//
// Servos
//

View File

@ -42,14 +42,6 @@
#define BOARD_INFO_NAME "Re-ARM RAMPS 1.4"
//
// EEPROM
//
#if NONE(FLASH_EEPROM_EMULATION, SDCARD_EEPROM_EMULATION)
#define FLASH_EEPROM_EMULATION
//#define SDCARD_EEPROM_EMULATION
#endif
//
// Servos
//

View File

@ -32,14 +32,6 @@
#define BOARD_INFO_NAME "Selena Compact"
#define BOARD_WEBSITE_URL "github.com/Ales2-k/Selena"
//
// EEPROM
//
#if NONE(FLASH_EEPROM_EMULATION, SDCARD_EEPROM_EMULATION)
#define FLASH_EEPROM_EMULATION
//#define SDCARD_EEPROM_EMULATION
#endif
//
// Servos
//

View File

@ -32,14 +32,6 @@
#define BOARD_INFO_NAME "Azteeg X5 GT"
#define BOARD_WEBSITE_URL "tinyurl.com/yx8tdqa3"
//
// EEPROM
//
#if NONE(FLASH_EEPROM_EMULATION, SDCARD_EEPROM_EMULATION)
#define FLASH_EEPROM_EMULATION
//#define SDCARD_EEPROM_EMULATION
#endif
//
// Servos
//

View File

@ -187,14 +187,6 @@
#endif // HAS_SPI_LCD
//
// EEPROM
//
#if NONE(FLASH_EEPROM_EMULATION, SDCARD_EEPROM_EMULATION)
#define FLASH_EEPROM_EMULATION
//#define SDCARD_EEPROM_EMULATION
#endif
//
// SD Support
//

View File

@ -31,14 +31,6 @@
#define BOARD_INFO_NAME "Azteeg X5 MINI WIFI"
//
// EEPROM
//
#if NONE(FLASH_EEPROM_EMULATION, SDCARD_EEPROM_EMULATION)
#define FLASH_EEPROM_EMULATION
//#define SDCARD_EEPROM_EMULATION
#endif
//
// DIGIPOT slave addresses
//

View File

@ -24,14 +24,6 @@
#define BOARD_INFO_NAME "BIGTREE SKR 1.4 TURBO"
#define SKR_HAS_LPC1769
//
// EEPROM
//
#if NONE(FLASH_EEPROM_EMULATION, SDCARD_EEPROM_EMULATION)
#define FLASH_EEPROM_EMULATION
//#define SDCARD_EEPROM_EMULATION
#endif
//
// Include SKR 1.4 pins
//

View File

@ -31,14 +31,6 @@
#define BOARD_INFO_NAME "Cohesion3D Mini"
//
// EEPROM
//
#if NONE(FLASH_EEPROM_EMULATION, SDCARD_EEPROM_EMULATION)
#define FLASH_EEPROM_EMULATION
//#define SDCARD_EEPROM_EMULATION
#endif
//
// Servos
//

View File

@ -31,14 +31,6 @@
#define BOARD_INFO_NAME "Cohesion3D ReMix"
//
// EEPROM
//
#if NONE(FLASH_EEPROM_EMULATION, SDCARD_EEPROM_EMULATION)
#define FLASH_EEPROM_EMULATION
//#define SDCARD_EEPROM_EMULATION
#endif
//
// Servos
//

View File

@ -32,14 +32,6 @@
#define BOARD_INFO_NAME "MKS SGen"
#define BOARD_WEBSITE_URL "github.com/makerbase-mks/MKS-SGEN"
//
// EEPROM
//
#if NONE(FLASH_EEPROM_EMULATION, SDCARD_EEPROM_EMULATION)
#define FLASH_EEPROM_EMULATION
//#define SDCARD_EEPROM_EMULATION
#endif
#define MKS_HAS_LPC1769
#include "../lpc1768/pins_MKS_SBASE.h"

View File

@ -32,14 +32,6 @@
#define BOARD_INFO_NAME "Smoothieboard"
#define BOARD_WEBSITE_URL "smoothieware.org/smoothieboard"
//
// EEPROM
//
#if NONE(FLASH_EEPROM_EMULATION, SDCARD_EEPROM_EMULATION)
#define FLASH_EEPROM_EMULATION
//#define SDCARD_EEPROM_EMULATION
#endif
//
// Servos
//

View File

@ -32,14 +32,6 @@
#define BOARD_INFO_NAME "TH3D EZBoard"
#define BOARD_WEBSITE_URL "th3dstudio.com"
//
// EEPROM
//
#if NONE(FLASH_EEPROM_EMULATION, SDCARD_EEPROM_EMULATION)
#define FLASH_EEPROM_EMULATION
//#define SDCARD_EEPROM_EMULATION
#endif
//
// Servos
//