My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

eeprom_flash.cpp 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #ifdef TARGET_LPC1768
  23. /**
  24. * Emulate EEPROM storage using Flash Memory
  25. *
  26. * Use a single 32K flash sector to store EEPROM data. To reduce the
  27. * number of erase operations a simple "levelling" scheme is used that
  28. * maintains a number of EEPROM "slots" within the larger flash sector.
  29. * Each slot is used in turn and the entire sector is only erased when all
  30. * slots have been used.
  31. *
  32. * A simple RAM image is used to hold the EEPROM data during I/O operations
  33. * and this is flushed to the next available slot when an update is complete.
  34. * If RAM usage becomes an issue we could store this image in one of the two
  35. * 16Kb I/O buffers (intended to hold DMA USB and Ethernet data, but currently
  36. * unused).
  37. */
  38. #include "../../inc/MarlinConfig.h"
  39. #if ENABLED(FLASH_EEPROM_EMULATION)
  40. #include "../shared/eeprom_api.h"
  41. extern "C" {
  42. #include <lpc17xx_iap.h>
  43. }
  44. #ifndef MARLIN_EEPROM_SIZE
  45. #define MARLIN_EEPROM_SIZE 0x1000 // 4KB
  46. #endif
  47. #define SECTOR_START(sector) ((sector < 16) ? (sector << 12) : ((sector - 14) << 15))
  48. #define EEPROM_SECTOR 29
  49. #define SECTOR_SIZE 32768
  50. #define EEPROM_SLOTS ((SECTOR_SIZE)/(MARLIN_EEPROM_SIZE))
  51. #define EEPROM_ERASE 0xFF
  52. #define SLOT_ADDRESS(sector, slot) (((uint8_t *)SECTOR_START(sector)) + slot * (MARLIN_EEPROM_SIZE))
  53. static uint8_t ram_eeprom[MARLIN_EEPROM_SIZE] __attribute__((aligned(4))) = {0};
  54. static bool eeprom_dirty = false;
  55. static int current_slot = 0;
  56. size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }
  57. bool PersistentStore::access_start() {
  58. uint32_t first_nblank_loc, first_nblank_val;
  59. IAP_STATUS_CODE status;
  60. // discover which slot we are currently using.
  61. __disable_irq();
  62. status = BlankCheckSector(EEPROM_SECTOR, EEPROM_SECTOR, &first_nblank_loc, &first_nblank_val);
  63. __enable_irq();
  64. if (status == CMD_SUCCESS) {
  65. // sector is blank so nothing stored yet
  66. for (int i = 0; i < MARLIN_EEPROM_SIZE; i++) ram_eeprom[i] = EEPROM_ERASE;
  67. current_slot = EEPROM_SLOTS;
  68. }
  69. else {
  70. // current slot is the first non blank one
  71. current_slot = first_nblank_loc / (MARLIN_EEPROM_SIZE);
  72. uint8_t *eeprom_data = SLOT_ADDRESS(EEPROM_SECTOR, current_slot);
  73. // load current settings
  74. for (int i = 0; i < MARLIN_EEPROM_SIZE; i++) ram_eeprom[i] = eeprom_data[i];
  75. }
  76. eeprom_dirty = false;
  77. return true;
  78. }
  79. bool PersistentStore::access_finish() {
  80. if (eeprom_dirty) {
  81. IAP_STATUS_CODE status;
  82. if (--current_slot < 0) {
  83. // all slots have been used, erase everything and start again
  84. __disable_irq();
  85. status = EraseSector(EEPROM_SECTOR, EEPROM_SECTOR);
  86. __enable_irq();
  87. current_slot = EEPROM_SLOTS - 1;
  88. }
  89. __disable_irq();
  90. status = CopyRAM2Flash(SLOT_ADDRESS(EEPROM_SECTOR, current_slot), ram_eeprom, IAP_WRITE_4096);
  91. __enable_irq();
  92. if (status != CMD_SUCCESS) return false;
  93. eeprom_dirty = false;
  94. }
  95. return true;
  96. }
  97. bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
  98. for (size_t i = 0; i < size; i++) ram_eeprom[pos + i] = value[i];
  99. eeprom_dirty = true;
  100. crc16(crc, value, size);
  101. pos += size;
  102. return false; // return true for any error
  103. }
  104. bool PersistentStore::read_data(int &pos, uint8_t* value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
  105. const uint8_t * const buff = writing ? &value[0] : &ram_eeprom[pos];
  106. if (writing) for (size_t i = 0; i < size; i++) value[i] = ram_eeprom[pos + i];
  107. crc16(crc, buff, size);
  108. pos += size;
  109. return false; // return true for any error
  110. }
  111. #endif // FLASH_EEPROM_EMULATION
  112. #endif // TARGET_LPC1768