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.

SpiEeprom.cpp 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. /**
  20. * Description: functions for SPI connected external EEPROM.
  21. * Not platform dependent.
  22. */
  23. #include "../../MarlinConfig.h"
  24. #if ENABLED(SPI_EEPROM)
  25. #include "HAL.h"
  26. #define CMD_WREN 6 // WREN
  27. #define CMD_READ 2 // WRITE
  28. #define CMD_WRITE 2 // WRITE
  29. uint8_t eeprom_read_byte(uint8_t* pos) {
  30. uint8_t v;
  31. uint8_t eeprom_temp[3];
  32. // set read location
  33. // begin transmission from device
  34. eeprom_temp[0] = CMD_READ;
  35. eeprom_temp[1] = ((unsigned)pos>>8) & 0xFF; // addr High
  36. eeprom_temp[2] = (unsigned)pos& 0xFF; // addr Low
  37. digitalWrite(SPI_EEPROM1_CS, HIGH);
  38. digitalWrite(SPI_EEPROM1_CS, LOW);
  39. spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 3);
  40. v = spiRec(SPI_CHAN_EEPROM1);
  41. digitalWrite(SPI_EEPROM1_CS, HIGH);
  42. return v;
  43. }
  44. void eeprom_read_block(void* dest, const void* eeprom_address, size_t n) {
  45. uint8_t eeprom_temp[3];
  46. // set read location
  47. // begin transmission from device
  48. eeprom_temp[0] = CMD_READ;
  49. eeprom_temp[1] = ((unsigned)eeprom_address>>8) & 0xFF; // addr High
  50. eeprom_temp[2] = (unsigned)eeprom_address& 0xFF; // addr Low
  51. digitalWrite(SPI_EEPROM1_CS, HIGH);
  52. digitalWrite(SPI_EEPROM1_CS, LOW);
  53. spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 3);
  54. uint8_t *p_dest = (uint8_t *)dest;
  55. while (n--)
  56. *p_dest++ = spiRec(SPI_CHAN_EEPROM1);
  57. digitalWrite(SPI_EEPROM1_CS, HIGH);
  58. }
  59. void eeprom_write_byte(uint8_t* pos, uint8_t value) {
  60. uint8_t eeprom_temp[3];
  61. /*write enable*/
  62. eeprom_temp[0] = CMD_WREN;
  63. digitalWrite(SPI_EEPROM1_CS, LOW);
  64. spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 1);
  65. digitalWrite(SPI_EEPROM1_CS, HIGH);
  66. delay(1);
  67. /*write addr*/
  68. eeprom_temp[0] = CMD_WRITE;
  69. eeprom_temp[1] = ((unsigned)pos>>8) & 0xFF; //addr High
  70. eeprom_temp[2] = (unsigned)pos & 0xFF; //addr Low
  71. digitalWrite(SPI_EEPROM1_CS, LOW);
  72. spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 3);
  73. spiSend(SPI_CHAN_EEPROM1, value);
  74. digitalWrite(SPI_EEPROM1_CS, HIGH);
  75. delay(7); // wait for page write to complete
  76. }
  77. void eeprom_update_block(const void* src, void* eeprom_address, size_t n) {
  78. uint8_t eeprom_temp[3];
  79. /*write enable*/
  80. eeprom_temp[0] = CMD_WREN;
  81. digitalWrite(SPI_EEPROM1_CS, LOW);
  82. spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 1);
  83. digitalWrite(SPI_EEPROM1_CS, HIGH);
  84. delay(1);
  85. /*write addr*/
  86. eeprom_temp[0] = CMD_WRITE;
  87. eeprom_temp[1] = ((unsigned)eeprom_address>>8) & 0xFF; //addr High
  88. eeprom_temp[2] = (unsigned)eeprom_address & 0xFF; //addr Low
  89. digitalWrite(SPI_EEPROM1_CS, LOW);
  90. spiSend(SPI_CHAN_EEPROM1, eeprom_temp, 3);
  91. spiSend(SPI_CHAN_EEPROM1, (const uint8_t*)src, n);
  92. digitalWrite(SPI_EEPROM1_CS, HIGH);
  93. delay(7); // wait for page write to complete
  94. }
  95. #endif // ENABLED(SPI_EEPROM)