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

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