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.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  5. * SAMD51 HAL developed by Giuliano Zaro (AKA GMagician)
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. *
  20. */
  21. #ifdef __SAMD51__
  22. #include "../../inc/MarlinConfig.h"
  23. #if ENABLED(FLASH_EEPROM_EMULATION)
  24. #include "../shared/eeprom_api.h"
  25. #define NVMCTRL_CMD(c) do{ \
  26. SYNC(!NVMCTRL->STATUS.bit.READY); \
  27. NVMCTRL->INTFLAG.bit.DONE = true; \
  28. NVMCTRL->CTRLB.reg = c | NVMCTRL_CTRLB_CMDEX_KEY; \
  29. SYNC(NVMCTRL->INTFLAG.bit.DONE); \
  30. }while(0)
  31. #define NVMCTRL_FLUSH() do{ \
  32. if (NVMCTRL->SEESTAT.bit.LOAD) \
  33. NVMCTRL_CMD(NVMCTRL_CTRLB_CMD_SEEFLUSH); \
  34. }while(0)
  35. size_t PersistentStore::capacity() {
  36. const uint8_t psz = NVMCTRL->SEESTAT.bit.PSZ,
  37. sblk = NVMCTRL->SEESTAT.bit.SBLK;
  38. return (!psz && !sblk) ? 0
  39. : (psz <= 2) ? (0x200 << psz)
  40. : (sblk == 1 || psz == 3) ? 4096
  41. : (sblk == 2 || psz == 4) ? 8192
  42. : (sblk <= 4 || psz == 5) ? 16384
  43. : (sblk >= 9 && psz == 7) ? 65536
  44. : 32768;
  45. }
  46. bool PersistentStore::access_start() {
  47. NVMCTRL->SEECFG.reg = NVMCTRL_SEECFG_WMODE_BUFFERED; // Buffered mode and segment reallocation active
  48. if (NVMCTRL->SEESTAT.bit.RLOCK)
  49. NVMCTRL_CMD(NVMCTRL_CTRLB_CMD_USEE); // Unlock E2P data write access
  50. return true;
  51. }
  52. bool PersistentStore::access_finish() {
  53. NVMCTRL_FLUSH();
  54. if (!NVMCTRL->SEESTAT.bit.LOCK)
  55. NVMCTRL_CMD(NVMCTRL_CTRLB_CMD_LSEE); // Lock E2P data write access
  56. return true;
  57. }
  58. bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
  59. while (size--) {
  60. const uint8_t v = *value;
  61. SYNC(NVMCTRL->SEESTAT.bit.BUSY);
  62. if (NVMCTRL->INTFLAG.bit.SEESFULL)
  63. NVMCTRL_FLUSH(); // Next write will trigger a sector reallocation. I need to flush 'pagebuffer'
  64. ((volatile uint8_t *)SEEPROM_ADDR)[pos] = v;
  65. SYNC(!NVMCTRL->INTFLAG.bit.SEEWRC);
  66. crc16(crc, &v, 1);
  67. pos++;
  68. value++;
  69. }
  70. return false;
  71. }
  72. bool PersistentStore::read_data(int &pos, uint8_t* value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
  73. while (size--) {
  74. SYNC(NVMCTRL->SEESTAT.bit.BUSY);
  75. uint8_t c = ((volatile uint8_t *)SEEPROM_ADDR)[pos];
  76. if (writing) *value = c;
  77. crc16(crc, &c, 1);
  78. pos++;
  79. value++;
  80. }
  81. return false;
  82. }
  83. #endif // FLASH_EEPROM_EMULATION
  84. #endif // __SAMD51__