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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. /**
  23. * SAMD51 HAL developed by Giuliano Zaro (AKA GMagician)
  24. */
  25. #ifdef __SAMD51__
  26. #include "../../inc/MarlinConfig.h"
  27. #if ENABLED(FLASH_EEPROM_EMULATION)
  28. #include "../shared/eeprom_api.h"
  29. #define NVMCTRL_CMD(c) do{ \
  30. SYNC(!NVMCTRL->STATUS.bit.READY); \
  31. NVMCTRL->INTFLAG.bit.DONE = true; \
  32. NVMCTRL->CTRLB.reg = c | NVMCTRL_CTRLB_CMDEX_KEY; \
  33. SYNC(NVMCTRL->INTFLAG.bit.DONE); \
  34. }while(0)
  35. #define NVMCTRL_FLUSH() do{ \
  36. if (NVMCTRL->SEESTAT.bit.LOAD) \
  37. NVMCTRL_CMD(NVMCTRL_CTRLB_CMD_SEEFLUSH); \
  38. }while(0)
  39. size_t PersistentStore::capacity() {
  40. const uint8_t psz = NVMCTRL->SEESTAT.bit.PSZ,
  41. sblk = NVMCTRL->SEESTAT.bit.SBLK;
  42. return (!psz && !sblk) ? 0
  43. : (psz <= 2) ? (0x200 << psz)
  44. : (sblk == 1 || psz == 3) ? 4096
  45. : (sblk == 2 || psz == 4) ? 8192
  46. : (sblk <= 4 || psz == 5) ? 16384
  47. : (sblk >= 9 && psz == 7) ? 65536
  48. : 32768;
  49. }
  50. bool PersistentStore::access_start() {
  51. NVMCTRL->SEECFG.reg = NVMCTRL_SEECFG_WMODE_BUFFERED; // Buffered mode and segment reallocation active
  52. if (NVMCTRL->SEESTAT.bit.RLOCK)
  53. NVMCTRL_CMD(NVMCTRL_CTRLB_CMD_USEE); // Unlock E2P data write access
  54. return true;
  55. }
  56. bool PersistentStore::access_finish() {
  57. NVMCTRL_FLUSH();
  58. if (!NVMCTRL->SEESTAT.bit.LOCK)
  59. NVMCTRL_CMD(NVMCTRL_CTRLB_CMD_LSEE); // Lock E2P data write access
  60. return true;
  61. }
  62. bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
  63. while (size--) {
  64. const uint8_t v = *value;
  65. SYNC(NVMCTRL->SEESTAT.bit.BUSY);
  66. if (NVMCTRL->INTFLAG.bit.SEESFULL)
  67. NVMCTRL_FLUSH(); // Next write will trigger a sector reallocation. I need to flush 'pagebuffer'
  68. ((volatile uint8_t *)SEEPROM_ADDR)[pos] = v;
  69. SYNC(!NVMCTRL->INTFLAG.bit.SEEWRC);
  70. crc16(crc, &v, 1);
  71. pos++;
  72. value++;
  73. }
  74. return false;
  75. }
  76. bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
  77. while (size--) {
  78. SYNC(NVMCTRL->SEESTAT.bit.BUSY);
  79. uint8_t c = ((volatile uint8_t *)SEEPROM_ADDR)[pos];
  80. if (writing) *value = c;
  81. crc16(crc, &c, 1);
  82. pos++;
  83. value++;
  84. }
  85. return false;
  86. }
  87. #endif // FLASH_EEPROM_EMULATION
  88. #endif // __SAMD51__