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_spi.cpp 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * Description: functions for SPI connected external EEPROM.
  24. * Not platform dependent.
  25. */
  26. #include "../../inc/MarlinConfig.h"
  27. #if ENABLED(SPI_EEPROM)
  28. #include "../HAL.h"
  29. #define CMD_WREN 6 // WREN
  30. #define CMD_READ 2 // WRITE
  31. #define CMD_WRITE 2 // WRITE
  32. uint8_t eeprom_read_byte(uint8_t* pos) {
  33. uint8_t v;
  34. uint8_t eeprom_temp[3];
  35. // set read location
  36. // begin transmission from device
  37. eeprom_temp[0] = CMD_READ;
  38. eeprom_temp[1] = ((unsigned)pos>>8) & 0xFF; // addr High
  39. eeprom_temp[2] = (unsigned)pos& 0xFF; // addr Low
  40. WRITE(SPI_EEPROM1_CS, HIGH);
  41. WRITE(SPI_EEPROM1_CS, LOW);
  42. spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 3);
  43. v = spiRec(SPI_CHAN_EEPROM1);
  44. WRITE(SPI_EEPROM1_CS, HIGH);
  45. return v;
  46. }
  47. void eeprom_read_block(void* dest, const void* eeprom_address, size_t n) {
  48. uint8_t eeprom_temp[3];
  49. // set read location
  50. // begin transmission from device
  51. eeprom_temp[0] = CMD_READ;
  52. eeprom_temp[1] = ((unsigned)eeprom_address>>8) & 0xFF; // addr High
  53. eeprom_temp[2] = (unsigned)eeprom_address& 0xFF; // addr Low
  54. WRITE(SPI_EEPROM1_CS, HIGH);
  55. WRITE(SPI_EEPROM1_CS, LOW);
  56. spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 3);
  57. uint8_t *p_dest = (uint8_t *)dest;
  58. while (n--)
  59. *p_dest++ = spiRec(SPI_CHAN_EEPROM1);
  60. WRITE(SPI_EEPROM1_CS, HIGH);
  61. }
  62. void eeprom_write_byte(uint8_t* pos, uint8_t value) {
  63. uint8_t eeprom_temp[3];
  64. /*write enable*/
  65. eeprom_temp[0] = CMD_WREN;
  66. WRITE(SPI_EEPROM1_CS, LOW);
  67. spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 1);
  68. WRITE(SPI_EEPROM1_CS, HIGH);
  69. delay(1);
  70. /*write addr*/
  71. eeprom_temp[0] = CMD_WRITE;
  72. eeprom_temp[1] = ((unsigned)pos>>8) & 0xFF; //addr High
  73. eeprom_temp[2] = (unsigned)pos & 0xFF; //addr Low
  74. WRITE(SPI_EEPROM1_CS, LOW);
  75. spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 3);
  76. spiSend(SPI_CHAN_EEPROM1, value);
  77. WRITE(SPI_EEPROM1_CS, HIGH);
  78. delay(7); // wait for page write to complete
  79. }
  80. void eeprom_update_block(const void* src, void* eeprom_address, size_t n) {
  81. uint8_t eeprom_temp[3];
  82. /*write enable*/
  83. eeprom_temp[0] = CMD_WREN;
  84. WRITE(SPI_EEPROM1_CS, LOW);
  85. spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 1);
  86. WRITE(SPI_EEPROM1_CS, HIGH);
  87. delay(1);
  88. /*write addr*/
  89. eeprom_temp[0] = CMD_WRITE;
  90. eeprom_temp[1] = ((unsigned)eeprom_address>>8) & 0xFF; //addr High
  91. eeprom_temp[2] = (unsigned)eeprom_address & 0xFF; //addr Low
  92. WRITE(SPI_EEPROM1_CS, LOW);
  93. spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 3);
  94. spiSend(SPI_CHAN_EEPROM1, (const uint8_t*)src, n);
  95. WRITE(SPI_EEPROM1_CS, HIGH);
  96. delay(7); // wait for page write to complete
  97. }
  98. #endif // SPI_EEPROM