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 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  5. * Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
  6. * Copyright (c) 2015-2016 Nico Tonnhofer wurstnase.reprap@gmail.com
  7. * Copyright (c) 2016 Victor Perez victor_pv@hotmail.com
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. /**
  24. * persistent_store_flash.cpp
  25. * HAL for stm32duino and compatible (STM32F1)
  26. * Implementation of EEPROM settings in SDCard
  27. */
  28. #ifdef __STM32F1__
  29. #include "../../inc/MarlinConfig.h"
  30. // This is for EEPROM emulation in flash
  31. #if ENABLED(EEPROM_SETTINGS) && ENABLED(FLASH_EEPROM_EMULATION)
  32. #include "../shared/persistent_store_api.h"
  33. #include <flash_stm32.h>
  34. #include <EEPROM.h>
  35. // Store settings in the last two pages
  36. // Flash pages must be erased before writing, so keep track.
  37. bool firstWrite = false;
  38. uint32_t pageBase = EEPROM_START_ADDRESS;
  39. bool PersistentStore::access_start() {
  40. firstWrite = true;
  41. return true;
  42. }
  43. bool PersistentStore::access_finish() {
  44. FLASH_Lock();
  45. firstWrite = false;
  46. return true;
  47. }
  48. bool PersistentStore::write_data(int &pos, const uint8_t *value, const size_t size, uint16_t *crc) {
  49. FLASH_Status status;
  50. if (firstWrite) {
  51. FLASH_Unlock();
  52. status = FLASH_ErasePage(EEPROM_PAGE0_BASE);
  53. if (status != FLASH_COMPLETE) return true;
  54. status = FLASH_ErasePage(EEPROM_PAGE1_BASE);
  55. if (status != FLASH_COMPLETE) return true;
  56. firstWrite = false;
  57. }
  58. // First write full words
  59. int i = 0;
  60. int wordsToWrite = size / sizeof(uint16_t);
  61. uint16_t* wordBuffer = (uint16_t *)value;
  62. while (wordsToWrite) {
  63. status = FLASH_ProgramHalfWord(pageBase + pos + (i * 2), wordBuffer[i]);
  64. if (status != FLASH_COMPLETE) return true;
  65. wordsToWrite--;
  66. i++;
  67. }
  68. // Now, write any remaining single byte
  69. if (size & 1) {
  70. uint16_t temp = value[size - 1];
  71. status = FLASH_ProgramHalfWord(pageBase + pos + i, temp);
  72. if (status != FLASH_COMPLETE) return true;
  73. }
  74. crc16(crc, value, size);
  75. pos += ((size + 1) & ~1);
  76. return false;
  77. }
  78. bool PersistentStore::read_data(int &pos, uint8_t* value, const size_t size, uint16_t *crc, const bool writing/*=true*/) {
  79. for (uint16_t i = 0; i < size; i++) {
  80. byte* accessPoint = (byte*)(pageBase + pos + i);
  81. uint8_t c = *accessPoint;
  82. if (writing) value[i] = c;
  83. crc16(crc, &c, 1);
  84. }
  85. pos += ((size + 1) & ~1);
  86. return false;
  87. }
  88. size_t PersistentStore::capacity() { return E2END + 1; }
  89. #endif // EEPROM_SETTINGS && EEPROM FLASH
  90. #endif // __STM32F1__