2017-06-17 18:36:10 -05:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2019-02-12 15:06:53 -06:00
|
|
|
* Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2017-06-17 18:36:10 -05:00
|
|
|
*
|
2017-08-03 11:04:04 -05:00
|
|
|
* Based on Sprinter and grbl.
|
|
|
|
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
|
|
|
|
*
|
2017-06-17 18:36:10 -05:00
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Description: functions for SPI connected external EEPROM.
|
|
|
|
* Not platform dependent.
|
|
|
|
*/
|
|
|
|
|
2018-08-14 03:28:52 -05:00
|
|
|
#include "../../inc/MarlinConfig.h"
|
2017-06-17 18:36:10 -05:00
|
|
|
|
|
|
|
#if ENABLED(SPI_EEPROM)
|
|
|
|
|
2018-08-14 17:54:12 -05:00
|
|
|
#include HAL_PATH(.., HAL.h)
|
2017-06-17 18:36:10 -05:00
|
|
|
|
|
|
|
#define CMD_WREN 6 // WREN
|
|
|
|
#define CMD_READ 2 // WRITE
|
|
|
|
#define CMD_WRITE 2 // WRITE
|
|
|
|
|
|
|
|
uint8_t eeprom_read_byte(uint8_t* pos) {
|
|
|
|
uint8_t v;
|
|
|
|
uint8_t eeprom_temp[3];
|
|
|
|
|
|
|
|
// set read location
|
|
|
|
// begin transmission from device
|
|
|
|
eeprom_temp[0] = CMD_READ;
|
|
|
|
eeprom_temp[1] = ((unsigned)pos>>8) & 0xFF; // addr High
|
|
|
|
eeprom_temp[2] = (unsigned)pos& 0xFF; // addr Low
|
2017-11-05 19:20:03 -06:00
|
|
|
WRITE(SPI_EEPROM1_CS, HIGH);
|
|
|
|
WRITE(SPI_EEPROM1_CS, LOW);
|
2017-06-17 18:36:10 -05:00
|
|
|
spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 3);
|
|
|
|
|
|
|
|
v = spiRec(SPI_CHAN_EEPROM1);
|
2017-11-05 19:20:03 -06:00
|
|
|
WRITE(SPI_EEPROM1_CS, HIGH);
|
2017-06-17 18:36:10 -05:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
void eeprom_read_block(void* dest, const void* eeprom_address, size_t n) {
|
|
|
|
uint8_t eeprom_temp[3];
|
|
|
|
|
|
|
|
// set read location
|
|
|
|
// begin transmission from device
|
|
|
|
eeprom_temp[0] = CMD_READ;
|
|
|
|
eeprom_temp[1] = ((unsigned)eeprom_address>>8) & 0xFF; // addr High
|
|
|
|
eeprom_temp[2] = (unsigned)eeprom_address& 0xFF; // addr Low
|
2017-11-05 19:20:03 -06:00
|
|
|
WRITE(SPI_EEPROM1_CS, HIGH);
|
|
|
|
WRITE(SPI_EEPROM1_CS, LOW);
|
2017-06-17 18:36:10 -05:00
|
|
|
spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 3);
|
|
|
|
|
|
|
|
uint8_t *p_dest = (uint8_t *)dest;
|
|
|
|
while (n--)
|
|
|
|
*p_dest++ = spiRec(SPI_CHAN_EEPROM1);
|
2017-11-05 19:20:03 -06:00
|
|
|
WRITE(SPI_EEPROM1_CS, HIGH);
|
2017-06-17 18:36:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void eeprom_write_byte(uint8_t* pos, uint8_t value) {
|
|
|
|
uint8_t eeprom_temp[3];
|
|
|
|
|
|
|
|
/*write enable*/
|
|
|
|
eeprom_temp[0] = CMD_WREN;
|
2017-11-05 19:20:03 -06:00
|
|
|
WRITE(SPI_EEPROM1_CS, LOW);
|
2017-06-17 18:36:10 -05:00
|
|
|
spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 1);
|
2017-11-05 19:20:03 -06:00
|
|
|
WRITE(SPI_EEPROM1_CS, HIGH);
|
2017-06-17 18:36:10 -05:00
|
|
|
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
|
2017-11-05 19:20:03 -06:00
|
|
|
WRITE(SPI_EEPROM1_CS, LOW);
|
2017-06-17 18:36:10 -05:00
|
|
|
spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 3);
|
|
|
|
|
|
|
|
spiSend(SPI_CHAN_EEPROM1, value);
|
2017-11-05 19:20:03 -06:00
|
|
|
WRITE(SPI_EEPROM1_CS, HIGH);
|
2017-06-17 18:36:10 -05:00
|
|
|
delay(7); // wait for page write to complete
|
|
|
|
}
|
|
|
|
|
|
|
|
void eeprom_update_block(const void* src, void* eeprom_address, size_t n) {
|
|
|
|
uint8_t eeprom_temp[3];
|
|
|
|
|
|
|
|
/*write enable*/
|
|
|
|
eeprom_temp[0] = CMD_WREN;
|
2017-11-05 19:20:03 -06:00
|
|
|
WRITE(SPI_EEPROM1_CS, LOW);
|
2017-06-17 18:36:10 -05:00
|
|
|
spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 1);
|
2017-11-05 19:20:03 -06:00
|
|
|
WRITE(SPI_EEPROM1_CS, HIGH);
|
2017-06-17 18:36:10 -05:00
|
|
|
delay(1);
|
|
|
|
|
|
|
|
/*write addr*/
|
|
|
|
eeprom_temp[0] = CMD_WRITE;
|
|
|
|
eeprom_temp[1] = ((unsigned)eeprom_address>>8) & 0xFF; //addr High
|
|
|
|
eeprom_temp[2] = (unsigned)eeprom_address & 0xFF; //addr Low
|
2017-11-05 19:20:03 -06:00
|
|
|
WRITE(SPI_EEPROM1_CS, LOW);
|
2017-06-17 18:36:10 -05:00
|
|
|
spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 3);
|
|
|
|
|
|
|
|
spiSend(SPI_CHAN_EEPROM1, (const uint8_t*)src, n);
|
2017-11-05 19:20:03 -06:00
|
|
|
WRITE(SPI_EEPROM1_CS, HIGH);
|
2017-06-17 18:36:10 -05:00
|
|
|
delay(7); // wait for page write to complete
|
|
|
|
}
|
|
|
|
|
2019-06-14 20:16:22 -05:00
|
|
|
#endif // SPI_EEPROM
|