Firmware2/Marlin/src/HAL/HAL_STM32_F4_F7/EmulatedEeprom.cpp

123 lines
3.2 KiB
C++
Raw Normal View History

2018-01-11 22:29:08 +01:00
/**
* Marlin 3D Printer Firmware
2020-02-03 15:00:57 +01:00
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
2018-01-11 22:29:08 +01: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/>.
*
*/
2019-07-10 06:54:34 +02:00
#if defined(STM32GENERIC) && (defined(STM32F4) || defined(STM32F7))
2018-01-15 09:28:39 +01:00
2018-01-11 22:29:08 +01:00
/**
* Description: Functions for a Flash emulated EEPROM
2018-01-11 22:29:08 +01:00
* Not platform dependent.
*/
2019-07-10 06:54:34 +02:00
// Include configs and pins to get all EEPROM flags
2018-01-11 22:29:08 +01:00
#include "../../inc/MarlinConfig.h"
2019-07-10 06:54:34 +02:00
#ifdef STM32F7
#define HAS_EMULATED_EEPROM 1
#else
#define HAS_EMULATED_EEPROM NONE(I2C_EEPROM, SPI_EEPROM)
#endif
#if HAS_EMULATED_EEPROM && ENABLED(EEPROM_SETTINGS)
2019-07-10 05:30:06 +02:00
// ------------------------
2018-01-11 22:29:08 +01:00
// Includes
2019-07-10 05:30:06 +02:00
// ------------------------
2018-01-11 22:29:08 +01:00
#include "HAL.h"
2019-07-10 06:54:34 +02:00
#include "eeprom_emul.h"
2018-01-11 22:29:08 +01:00
2019-07-10 05:30:06 +02:00
// ------------------------
2018-01-11 22:29:08 +01:00
// Local defines
2019-07-10 05:30:06 +02:00
// ------------------------
2018-01-11 22:29:08 +01:00
// FLASH_FLAG_PGSERR (Programming Sequence Error) was renamed to
2019-07-10 06:54:34 +02:00
// FLASH_FLAG_ERSERR (Erasing Sequence Error) in STM32F4/7
#ifdef STM32F7
#define FLASH_FLAG_PGSERR FLASH_FLAG_ERSERR
#else
//#define FLASH_FLAG_PGSERR FLASH_FLAG_ERSERR
#endif
2018-01-11 22:29:08 +01:00
2019-07-10 05:30:06 +02:00
// ------------------------
// Private Variables
// ------------------------
2018-01-11 22:29:08 +01:00
2019-07-10 05:30:06 +02:00
static bool eeprom_initialized = false;
// ------------------------
// Public functions
// ------------------------
2018-01-11 22:29:08 +01:00
void eeprom_init() {
2018-08-23 00:14:38 +02:00
if (!eeprom_initialized) {
2018-01-11 22:29:08 +01:00
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);
/* EEPROM Init */
2018-08-23 00:14:38 +02:00
if (EE_Initialize() != EE_OK)
2018-02-25 07:13:46 +01:00
for (;;) HAL_Delay(1); // Spin forever until watchdog reset
2018-01-11 22:29:08 +01:00
HAL_FLASH_Lock();
2018-08-23 00:14:38 +02:00
eeprom_initialized = true;
2018-01-11 22:29:08 +01:00
}
}
2018-10-10 01:59:49 +02:00
void eeprom_write_byte(uint8_t *pos, unsigned char value) {
2018-01-11 22:29:08 +01:00
eeprom_init();
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);
2018-02-25 07:13:46 +01:00
2019-07-10 06:54:34 +02:00
uint16_t eeprom_address = unsigned(pos);
if (EE_WriteVariable(eeprom_address, uint16_t(value)) != EE_OK)
for (;;) HAL_Delay(1); // Spin forever until watchdog reset
2018-02-25 07:13:46 +01:00
2018-01-11 22:29:08 +01:00
HAL_FLASH_Lock();
}
2018-10-10 01:59:49 +02:00
uint8_t eeprom_read_byte(uint8_t *pos) {
2018-01-11 22:29:08 +01:00
eeprom_init();
uint16_t data = 0xFF;
uint16_t eeprom_address = unsigned(pos);
(void)EE_ReadVariable(eeprom_address, &data); // Data unchanged on error
return uint8_t(data);
2018-01-11 22:29:08 +01:00
}
2018-02-25 07:13:46 +01:00
void eeprom_read_block(void *__dst, const void *__src, size_t __n) {
2018-01-11 22:29:08 +01:00
eeprom_init();
2019-07-10 06:54:34 +02:00
uint16_t data = 0xFF;
uint16_t eeprom_address = unsigned(__src);
2018-02-25 07:13:46 +01:00
for (uint8_t c = 0; c < __n; c++) {
2018-01-11 22:29:08 +01:00
EE_ReadVariable(eeprom_address+c, &data);
*((uint8_t*)__dst + c) = data;
}
}
2018-02-25 07:13:46 +01:00
void eeprom_update_block(const void *__src, void *__dst, size_t __n) {
2018-01-11 22:29:08 +01:00
}
2019-07-10 06:54:34 +02:00
#endif // EEPROM_SETTINGS
#endif // STM32GENERIC && (STM32F4 || STM32F7)