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_if_spi.cpp 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. * Platform-independent Arduino functions for SPI EEPROM.
  24. * Enable USE_SHARED_EEPROM if not supplied by the framework.
  25. */
  26. #include "../../inc/MarlinConfig.h"
  27. #if ENABLED(SPI_EEPROM)
  28. #include "eeprom_if.h"
  29. void eeprom_init() {}
  30. #if ENABLED(USE_SHARED_EEPROM)
  31. #define CMD_WREN 6 // WREN
  32. #define CMD_READ 2 // WRITE
  33. #define CMD_WRITE 2 // WRITE
  34. #ifndef EEPROM_WRITE_DELAY
  35. #define EEPROM_WRITE_DELAY 7
  36. #endif
  37. uint8_t eeprom_read_byte(uint8_t* pos) {
  38. uint8_t v;
  39. uint8_t eeprom_temp[3];
  40. // set read location
  41. // begin transmission from device
  42. eeprom_temp[0] = CMD_READ;
  43. eeprom_temp[1] = ((unsigned)pos>>8) & 0xFF; // addr High
  44. eeprom_temp[2] = (unsigned)pos& 0xFF; // addr Low
  45. WRITE(SPI_EEPROM1_CS, HIGH);
  46. WRITE(SPI_EEPROM1_CS, LOW);
  47. spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 3);
  48. v = spiRec(SPI_CHAN_EEPROM1);
  49. WRITE(SPI_EEPROM1_CS, HIGH);
  50. return v;
  51. }
  52. void eeprom_write_byte(uint8_t* pos, uint8_t value) {
  53. uint8_t eeprom_temp[3];
  54. /*write enable*/
  55. eeprom_temp[0] = CMD_WREN;
  56. WRITE(SPI_EEPROM1_CS, LOW);
  57. spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 1);
  58. WRITE(SPI_EEPROM1_CS, HIGH);
  59. delay(1);
  60. /*write addr*/
  61. eeprom_temp[0] = CMD_WRITE;
  62. eeprom_temp[1] = ((unsigned)pos>>8) & 0xFF; //addr High
  63. eeprom_temp[2] = (unsigned)pos & 0xFF; //addr Low
  64. WRITE(SPI_EEPROM1_CS, LOW);
  65. spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 3);
  66. spiSend(SPI_CHAN_EEPROM1, value);
  67. WRITE(SPI_EEPROM1_CS, HIGH);
  68. delay(EEPROM_WRITE_DELAY); // wait for page write to complete
  69. }
  70. #endif // USE_SHARED_EEPROM
  71. #endif // I2C_EEPROM