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_i2c.cpp 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 I2C connected external EEPROM.
  24. * Not platform dependent.
  25. *
  26. * TODO: Some platform Arduino libraries define these functions
  27. * so Marlin needs to add a glue layer to prevent the conflict.
  28. */
  29. #include "../../inc/MarlinConfig.h"
  30. #if ENABLED(I2C_EEPROM)
  31. #include "../HAL.h"
  32. #include <Wire.h>
  33. #ifndef EEPROM_WRITE_DELAY
  34. #define EEPROM_WRITE_DELAY 5
  35. #endif
  36. // ------------------------
  37. // Private Variables
  38. // ------------------------
  39. #ifndef EEPROM_DEVICE_ADDRESS
  40. #define EEPROM_DEVICE_ADDRESS 0x50
  41. #endif
  42. static constexpr uint8_t eeprom_device_address = I2C_ADDRESS(EEPROM_DEVICE_ADDRESS);
  43. // ------------------------
  44. // Public functions
  45. // ------------------------
  46. static void eeprom_init() { Wire.begin(); }
  47. void eeprom_write_byte(uint8_t *pos, unsigned char value) {
  48. const unsigned eeprom_address = (unsigned)pos;
  49. Wire.beginTransmission(eeprom_device_address);
  50. Wire.write(int(eeprom_address >> 8)); // MSB
  51. Wire.write(int(eeprom_address & 0xFF)); // LSB
  52. Wire.write(value);
  53. Wire.endTransmission();
  54. // wait for write cycle to complete
  55. // this could be done more efficiently with "acknowledge polling"
  56. delay(EEPROM_WRITE_DELAY);
  57. }
  58. // WARNING: address is a page address, 6-bit end will wrap around
  59. // also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes
  60. void eeprom_update_block(const void *pos, void *__dst, size_t n) {
  61. const unsigned eeprom_address = (unsigned)__dst;
  62. eeprom_init();
  63. Wire.beginTransmission(eeprom_device_address);
  64. Wire.write(int(eeprom_address >> 8)); // MSB
  65. Wire.write(int(eeprom_address & 0xFF)); // LSB
  66. Wire.endTransmission();
  67. uint8_t *ptr = (uint8_t*)pos;
  68. uint8_t flag = 0;
  69. Wire.requestFrom(eeprom_device_address, (byte)n);
  70. for (byte c = 0; c < n && Wire.available(); c++)
  71. flag |= Wire.read() ^ ptr[c];
  72. if (flag) {
  73. Wire.beginTransmission(eeprom_device_address);
  74. Wire.write(int(eeprom_address >> 8)); // MSB
  75. Wire.write(int(eeprom_address & 0xFF)); // LSB
  76. Wire.write((uint8_t*)pos, n);
  77. Wire.endTransmission();
  78. // wait for write cycle to complete
  79. // this could be done more efficiently with "acknowledge polling"
  80. delay(EEPROM_WRITE_DELAY);
  81. }
  82. }
  83. uint8_t eeprom_read_byte(uint8_t *pos) {
  84. const unsigned eeprom_address = (unsigned)pos;
  85. Wire.beginTransmission(eeprom_device_address);
  86. Wire.write(int(eeprom_address >> 8)); // MSB
  87. Wire.write(int(eeprom_address & 0xFF)); // LSB
  88. Wire.endTransmission();
  89. Wire.requestFrom(eeprom_device_address, (byte)1);
  90. return Wire.available() ? Wire.read() : 0xFF;
  91. }
  92. // Don't read more than 30..32 bytes at a time!
  93. void eeprom_read_block(void* pos, const void *__dst, size_t n) {
  94. const unsigned eeprom_address = (unsigned)__dst;
  95. eeprom_init();
  96. Wire.beginTransmission(eeprom_device_address);
  97. Wire.write(int(eeprom_address >> 8)); // MSB
  98. Wire.write(int(eeprom_address & 0xFF)); // LSB
  99. Wire.endTransmission();
  100. Wire.requestFrom(eeprom_device_address, (byte)n);
  101. for (byte c = 0; c < n; c++ )
  102. if (Wire.available()) *((uint8_t*)pos + c) = Wire.read();
  103. }
  104. #endif // I2C_EEPROM