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_if_i2c.cpp 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * Platform-independent Arduino functions for I2C EEPROM.
  24. * Enable USE_SHARED_EEPROM if not supplied by the framework.
  25. */
  26. #include "../../inc/MarlinConfig.h"
  27. #if ENABLED(I2C_EEPROM)
  28. #include "eeprom_if.h"
  29. #if ENABLED(SOFT_I2C_EEPROM)
  30. #include <SlowSoftWire.h>
  31. SlowSoftWire Wire = SlowSoftWire(I2C_SDA_PIN, I2C_SCL_PIN, true);
  32. #else
  33. #include <Wire.h>
  34. #endif
  35. void eeprom_init() {
  36. Wire.begin(
  37. #if PINS_EXIST(I2C_SCL, I2C_SDA) && DISABLED(SOFT_I2C_EEPROM)
  38. uint8_t(I2C_SDA_PIN), uint8_t(I2C_SCL_PIN)
  39. #endif
  40. );
  41. }
  42. #if ENABLED(USE_SHARED_EEPROM)
  43. #ifndef EEPROM_WRITE_DELAY
  44. #define EEPROM_WRITE_DELAY 5
  45. #endif
  46. #ifndef EEPROM_DEVICE_ADDRESS
  47. #define EEPROM_DEVICE_ADDRESS 0x50
  48. #endif
  49. static constexpr uint8_t eeprom_device_address = I2C_ADDRESS(EEPROM_DEVICE_ADDRESS);
  50. // ------------------------
  51. // Public functions
  52. // ------------------------
  53. static void _eeprom_begin(uint8_t * const pos) {
  54. const unsigned eeprom_address = (unsigned)pos;
  55. Wire.beginTransmission(eeprom_device_address);
  56. Wire.write(int(eeprom_address >> 8)); // Address High
  57. Wire.write(int(eeprom_address & 0xFF)); // Address Low
  58. }
  59. void eeprom_write_byte(uint8_t *pos, uint8_t value) {
  60. _eeprom_begin(pos);
  61. Wire.write(value);
  62. Wire.endTransmission();
  63. // wait for write cycle to complete
  64. // this could be done more efficiently with "acknowledge polling"
  65. delay(EEPROM_WRITE_DELAY);
  66. }
  67. uint8_t eeprom_read_byte(uint8_t *pos) {
  68. _eeprom_begin(pos);
  69. Wire.endTransmission();
  70. Wire.requestFrom(eeprom_device_address, (byte)1);
  71. return Wire.available() ? Wire.read() : 0xFF;
  72. }
  73. #endif // USE_SHARED_EEPROM
  74. #endif // I2C_EEPROM