Browse Source

HAL eeprom cleanup

vanilla_fb_2.0.x
Scott Lahteine 3 years ago
parent
commit
38b44e3fc9
  1. 2
      Marlin/src/HAL/STM32F1/eeprom_if_iic.cpp
  2. 2
      Marlin/src/HAL/shared/eeprom_if.h
  3. 17
      Marlin/src/HAL/shared/eeprom_if_i2c.cpp
  4. 59
      Marlin/src/HAL/shared/eeprom_if_spi.cpp

2
Marlin/src/HAL/STM32F1/eeprom_if_iic.cpp

@ -40,7 +40,7 @@ void eeprom_init() { BL24CXX::init(); }
// Public functions // Public functions
// ------------------------ // ------------------------
void eeprom_write_byte(uint8_t *pos, unsigned char value) { void eeprom_write_byte(uint8_t *pos, uint8_t value) {
const unsigned eeprom_address = (unsigned)pos; const unsigned eeprom_address = (unsigned)pos;
return BL24CXX::writeOneByte(eeprom_address, value); return BL24CXX::writeOneByte(eeprom_address, value);
} }

2
Marlin/src/HAL/shared/eeprom_if.h

@ -25,5 +25,5 @@
// EEPROM // EEPROM
// //
void eeprom_init(); void eeprom_init();
void eeprom_write_byte(uint8_t *pos, unsigned char value); void eeprom_write_byte(uint8_t *pos, uint8_t value);
uint8_t eeprom_read_byte(uint8_t *pos); uint8_t eeprom_read_byte(uint8_t *pos);

17
Marlin/src/HAL/shared/eeprom_if_i2c.cpp

@ -55,12 +55,15 @@ static constexpr uint8_t eeprom_device_address = I2C_ADDRESS(EEPROM_DEVICE_ADDRE
// Public functions // Public functions
// ------------------------ // ------------------------
void eeprom_write_byte(uint8_t *pos, unsigned char value) { static void _eeprom_begin(uint8_t * const pos) {
const unsigned eeprom_address = (unsigned)pos; const unsigned eeprom_address = (unsigned)pos;
Wire.beginTransmission(eeprom_device_address); Wire.beginTransmission(eeprom_device_address);
Wire.write(int(eeprom_address >> 8)); // MSB Wire.write(int(eeprom_address >> 8)); // Address High
Wire.write(int(eeprom_address & 0xFF)); // LSB Wire.write(int(eeprom_address & 0xFF)); // Address Low
}
void eeprom_write_byte(uint8_t *pos, uint8_t value) {
_eeprom_begin(pos);
Wire.write(value); Wire.write(value);
Wire.endTransmission(); Wire.endTransmission();
@ -70,11 +73,7 @@ void eeprom_write_byte(uint8_t *pos, unsigned char value) {
} }
uint8_t eeprom_read_byte(uint8_t *pos) { uint8_t eeprom_read_byte(uint8_t *pos) {
const unsigned eeprom_address = (unsigned)pos; _eeprom_begin(pos);
Wire.beginTransmission(eeprom_device_address);
Wire.write(int(eeprom_address >> 8)); // MSB
Wire.write(int(eeprom_address & 0xFF)); // LSB
Wire.endTransmission(); Wire.endTransmission();
Wire.requestFrom(eeprom_device_address, (byte)1); Wire.requestFrom(eeprom_device_address, (byte)1);
return Wire.available() ? Wire.read() : 0xFF; return Wire.available() ? Wire.read() : 0xFF;

59
Marlin/src/HAL/shared/eeprom_if_spi.cpp

@ -43,44 +43,41 @@ void eeprom_init() {}
#define EEPROM_WRITE_DELAY 7 #define EEPROM_WRITE_DELAY 7
#endif #endif
uint8_t eeprom_read_byte(uint8_t* pos) { static void _eeprom_begin(uint8_t * const pos, const uint8_t cmd) {
uint8_t v; const uint8_t eeprom_temp[3] = {
uint8_t eeprom_temp[3]; cmd,
(unsigned(pos) >> 8) & 0xFF, // Address High
// set read location unsigned(pos) & 0xFF // Address Low
// begin transmission from device };
eeprom_temp[0] = CMD_READ; WRITE(SPI_EEPROM1_CS, HIGH); // Usually free already
eeprom_temp[1] = ((unsigned)pos>>8) & 0xFF; // addr High WRITE(SPI_EEPROM1_CS, LOW); // Activate the Bus
eeprom_temp[2] = (unsigned)pos& 0xFF; // addr Low
WRITE(SPI_EEPROM1_CS, HIGH);
WRITE(SPI_EEPROM1_CS, LOW);
spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 3); spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 3);
// Leave the Bus in-use
}
uint8_t eeprom_read_byte(uint8_t* pos) {
_eeprom_begin(pos, CMD_READ); // Set read location and begin transmission
const uint8_t v = spiRec(SPI_CHAN_EEPROM1); // After READ a value sits on the Bus
WRITE(SPI_EEPROM1_CS, HIGH); // Done with device
v = spiRec(SPI_CHAN_EEPROM1);
WRITE(SPI_EEPROM1_CS, HIGH);
return v; return v;
} }
void eeprom_write_byte(uint8_t* pos, uint8_t value) { void eeprom_write_byte(uint8_t *pos, uint8_t value) {
uint8_t eeprom_temp[3]; const uint8_t eeprom_temp = CMD_WREN;
/*write enable*/
eeprom_temp[0] = CMD_WREN;
WRITE(SPI_EEPROM1_CS, LOW);
spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 1);
WRITE(SPI_EEPROM1_CS, HIGH);
delay(1);
/*write addr*/
eeprom_temp[0] = CMD_WRITE;
eeprom_temp[1] = ((unsigned)pos>>8) & 0xFF; //addr High
eeprom_temp[2] = (unsigned)pos & 0xFF; //addr Low
WRITE(SPI_EEPROM1_CS, LOW); WRITE(SPI_EEPROM1_CS, LOW);
spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 3); spiSend(SPI_CHAN_EEPROM1, &eeprom_temp, 1); // Write Enable
WRITE(SPI_EEPROM1_CS, HIGH); // Done with the Bus
delay(1); // For a small amount of time
_eeprom_begin(pos, CMD_WRITE); // Set write address and begin transmission
spiSend(SPI_CHAN_EEPROM1, value); spiSend(SPI_CHAN_EEPROM1, value); // Send the value to be written
WRITE(SPI_EEPROM1_CS, HIGH); WRITE(SPI_EEPROM1_CS, HIGH); // Done with the Bus
delay(EEPROM_WRITE_DELAY); // wait for page write to complete delay(EEPROM_WRITE_DELAY); // Give page write time to complete
} }
#endif // USE_SHARED_EEPROM #endif // USE_SHARED_EEPROM

Loading…
Cancel
Save