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

85 lines
2.6 KiB
C++
Raw Normal View History

/**
* Marlin 3D Printer Firmware
2020-02-03 08:00:57 -06:00
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
2019-06-27 23:57:50 -05:00
* 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
2020-07-22 22:20:14 -05:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
/**
2020-04-29 14:46:33 -05:00
* Platform-independent Arduino functions for SPI EEPROM.
* Enable USE_SHARED_EEPROM if not supplied by the framework.
*/
#include "../../inc/MarlinConfig.h"
2020-05-01 00:04:55 -05:00
#if ENABLED(SPI_EEPROM)
2020-04-29 14:46:33 -05:00
#include "eeprom_if.h"
2020-05-01 00:04:55 -05:00
void eeprom_init() {}
#if ENABLED(USE_SHARED_EEPROM)
#define CMD_WREN 6 // WREN
#define CMD_READ 2 // WRITE
#define CMD_WRITE 2 // WRITE
#ifndef EEPROM_WRITE_DELAY
#define EEPROM_WRITE_DELAY 7
#endif
2021-03-17 23:55:55 -05:00
static void _eeprom_begin(uint8_t * const pos, const uint8_t cmd) {
const uint8_t eeprom_temp[3] = {
cmd,
(unsigned(pos) >> 8) & 0xFF, // Address High
unsigned(pos) & 0xFF // Address Low
};
WRITE(SPI_EEPROM1_CS_PIN, HIGH); // Usually free already
WRITE(SPI_EEPROM1_CS_PIN, LOW); // Activate the Bus
spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 3);
2021-03-17 23:55:55 -05:00
// Leave the Bus in-use
}
2021-03-29 20:36:37 -05:00
uint8_t eeprom_read_byte(uint8_t *pos) {
2021-03-17 23:55:55 -05:00
_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_PIN, HIGH); // Done with device
return v;
}
2021-03-17 23:55:55 -05:00
void eeprom_write_byte(uint8_t *pos, uint8_t value) {
const uint8_t eeprom_temp = CMD_WREN;
WRITE(SPI_EEPROM1_CS_PIN, LOW);
2021-03-17 23:55:55 -05:00
spiSend(SPI_CHAN_EEPROM1, &eeprom_temp, 1); // Write Enable
WRITE(SPI_EEPROM1_CS_PIN, HIGH); // Done with the Bus
2021-03-17 23:55:55 -05:00
delay(1); // For a small amount of time
_eeprom_begin(pos, CMD_WRITE); // Set write address and begin transmission
2021-03-17 23:55:55 -05:00
spiSend(SPI_CHAN_EEPROM1, value); // Send the value to be written
WRITE(SPI_EEPROM1_CS_PIN, HIGH); // Done with the Bus
2021-03-17 23:55:55 -05:00
delay(EEPROM_WRITE_DELAY); // Give page write time to complete
}
2020-05-01 00:04:55 -05:00
#endif // USE_SHARED_EEPROM
#endif // I2C_EEPROM