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

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