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.

persistent_store_flash.cpp 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 <http://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/MarlinConfigPre.h"
  39. #if ENABLED(FLASH_EEPROM_EMULATION)
  40. #include "persistent_store_api.h"
  41. #include "../../inc/MarlinConfig.h"
  42. extern "C" {
  43. #include <lpc17xx_iap.h>
  44. }
  45. #define SECTOR_START(sector) ((sector < 16) ? (sector * 0x1000) : ((sector - 14) * 0x8000))
  46. #define EEPROM_SECTOR 29
  47. #define EEPROM_SIZE (4096)
  48. #define SECTOR_SIZE (32768)
  49. #define EEPROM_SLOTS (SECTOR_SIZE/EEPROM_SIZE)
  50. #define EEPROM_ERASE (0xFF)
  51. #define SLOT_ADDRESS(sector, slot) (((uint8_t *)SECTOR_START(sector)) + slot * EEPROM_SIZE)
  52. static uint8_t ram_eeprom[EEPROM_SIZE] __attribute__((aligned(4))) = {0};
  53. static bool eeprom_dirty = false;
  54. static int current_slot = 0;
  55. bool PersistentStore::access_start() {
  56. uint32_t first_nblank_loc, first_nblank_val;
  57. IAP_STATUS_CODE status;
  58. // discover which slot we are currently using.
  59. __disable_irq();
  60. status = BlankCheckSector(EEPROM_SECTOR, EEPROM_SECTOR, &first_nblank_loc, &first_nblank_val);
  61. __enable_irq();
  62. if (status == CMD_SUCCESS) {
  63. // sector is blank so nothing stored yet
  64. for (int i = 0; i < EEPROM_SIZE; i++) ram_eeprom[i] = EEPROM_ERASE;
  65. current_slot = EEPROM_SLOTS;
  66. }
  67. else {
  68. // current slot is the first non blank one
  69. current_slot = first_nblank_loc / EEPROM_SIZE;
  70. uint8_t *eeprom_data = SLOT_ADDRESS(EEPROM_SECTOR, current_slot);
  71. // load current settings
  72. for (int i = 0; i < EEPROM_SIZE; i++) ram_eeprom[i] = eeprom_data[i];
  73. }
  74. eeprom_dirty = false;
  75. return true;
  76. }
  77. bool PersistentStore::access_finish() {
  78. if (eeprom_dirty) {
  79. IAP_STATUS_CODE status;
  80. if (--current_slot < 0) {
  81. // all slots have been used, erase everything and start again
  82. __disable_irq();
  83. status = EraseSector(EEPROM_SECTOR, EEPROM_SECTOR);
  84. __enable_irq();
  85. current_slot = EEPROM_SLOTS - 1;
  86. }
  87. __disable_irq();
  88. status = CopyRAM2Flash(SLOT_ADDRESS(EEPROM_SECTOR, current_slot), ram_eeprom, IAP_WRITE_4096);
  89. __enable_irq();
  90. if (status != CMD_SUCCESS) return false;
  91. eeprom_dirty = false;
  92. }
  93. return true;
  94. }
  95. bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
  96. for (size_t i = 0; i < size; i++) ram_eeprom[pos + i] = value[i];
  97. eeprom_dirty = true;
  98. crc16(crc, value, size);
  99. pos += size;
  100. return false; // return true for any error
  101. }
  102. bool PersistentStore::read_data(int &pos, uint8_t* value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
  103. const uint8_t * const buff = writing ? &value[0] : &ram_eeprom[pos];
  104. if (writing) for (size_t i = 0; i < size; i++) value[i] = ram_eeprom[pos + i];
  105. crc16(crc, buff, size);
  106. pos += size;
  107. return false; // return true for any error
  108. }
  109. size_t PersistentStore::capacity() { return EEPROM_SIZE; }
  110. #endif // FLASH_EEPROM_EMULATION
  111. #endif // TARGET_LPC1768