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.

QSPIFlash.cpp 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #include "../../inc/MarlinConfig.h"
  23. #if ENABLED(QSPI_EEPROM)
  24. #include "QSPIFlash.h"
  25. #define INVALID_ADDR 0xffffffff
  26. #define SECTOR_OF(a) (a & ~(SFLASH_SECTOR_SIZE - 1))
  27. #define OFFSET_OF(a) (a & (SFLASH_SECTOR_SIZE - 1))
  28. Adafruit_SPIFlashBase * QSPIFlash::_flashBase = nullptr;
  29. uint8_t QSPIFlash::_buf[SFLASH_SECTOR_SIZE];
  30. uint32_t QSPIFlash::_addr = INVALID_ADDR;
  31. void QSPIFlash::begin() {
  32. if (_flashBase != nullptr) return;
  33. _flashBase = new Adafruit_SPIFlashBase(new Adafruit_FlashTransport_QSPI());
  34. _flashBase->begin(NULL);
  35. }
  36. size_t QSPIFlash::size() {
  37. return _flashBase->size();
  38. }
  39. uint8_t QSPIFlash::readByte(const uint32_t address) {
  40. if (SECTOR_OF(address) == _addr) return _buf[OFFSET_OF(address)];
  41. return _flashBase->read8(address);
  42. }
  43. void QSPIFlash::writeByte(const uint32_t address, const uint8_t value) {
  44. uint32_t const sector_addr = SECTOR_OF(address);
  45. // Page changes, flush old and update new cache
  46. if (sector_addr != _addr) {
  47. flush();
  48. _addr = sector_addr;
  49. // read a whole page from flash
  50. _flashBase->readBuffer(sector_addr, _buf, SFLASH_SECTOR_SIZE);
  51. }
  52. _buf[OFFSET_OF(address)] = value;
  53. }
  54. void QSPIFlash::flush() {
  55. if (_addr == INVALID_ADDR) return;
  56. _flashBase->eraseSector(_addr / SFLASH_SECTOR_SIZE);
  57. _flashBase->writeBuffer(_addr, _buf, SFLASH_SECTOR_SIZE);
  58. _addr = INVALID_ADDR;
  59. }
  60. #endif // QSPI_EEPROM