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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. static void _eeprom_begin(uint8_t * const pos, const uint8_t cmd) {
  38. const uint8_t eeprom_temp[3] = {
  39. cmd,
  40. (unsigned(pos) >> 8) & 0xFF, // Address High
  41. unsigned(pos) & 0xFF // Address Low
  42. };
  43. WRITE(SPI_EEPROM1_CS_PIN, HIGH); // Usually free already
  44. WRITE(SPI_EEPROM1_CS_PIN, LOW); // Activate the Bus
  45. spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 3);
  46. // Leave the Bus in-use
  47. }
  48. uint8_t eeprom_read_byte(uint8_t *pos) {
  49. _eeprom_begin(pos, CMD_READ); // Set read location and begin transmission
  50. const uint8_t v = spiRec(SPI_CHAN_EEPROM1); // After READ a value sits on the Bus
  51. WRITE(SPI_EEPROM1_CS_PIN, HIGH); // Done with device
  52. return v;
  53. }
  54. void eeprom_write_byte(uint8_t *pos, uint8_t value) {
  55. const uint8_t eeprom_temp = CMD_WREN;
  56. WRITE(SPI_EEPROM1_CS_PIN, LOW);
  57. spiSend(SPI_CHAN_EEPROM1, &eeprom_temp, 1); // Write Enable
  58. WRITE(SPI_EEPROM1_CS_PIN, HIGH); // Done with the Bus
  59. delay(1); // For a small amount of time
  60. _eeprom_begin(pos, CMD_WRITE); // Set write address and begin transmission
  61. spiSend(SPI_CHAN_EEPROM1, value); // Send the value to be written
  62. WRITE(SPI_EEPROM1_CS_PIN, HIGH); // Done with the Bus
  63. delay(EEPROM_WRITE_DELAY); // Give page write time to complete
  64. }
  65. #endif // USE_SHARED_EEPROM
  66. #endif // I2C_EEPROM