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 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #define SMALL_EEPROM (MARLIN_EEPROM_SIZE <= 2048)
  54. // Combine Address high bits into the device address on <=16Kbit (2K) and >512Kbit (64K) EEPROMs.
  55. // Note: MARLIN_EEPROM_SIZE is specified in bytes, whereas EEPROM model numbers refer to bits.
  56. // e.g., The "16" in BL24C16 indicates a 16Kbit (2KB) size.
  57. static uint8_t _eeprom_calc_device_address(uint8_t * const pos) {
  58. const unsigned eeprom_address = (unsigned)pos;
  59. return (SMALL_EEPROM || MARLIN_EEPROM_SIZE > 65536)
  60. ? uint8_t(eeprom_device_address | ((eeprom_address >> (SMALL_EEPROM ? 8 : 16)) & 0x07))
  61. : eeprom_device_address;
  62. }
  63. static void _eeprom_begin(uint8_t * const pos) {
  64. const unsigned eeprom_address = (unsigned)pos;
  65. Wire.beginTransmission(_eeprom_calc_device_address(pos));
  66. if (!SMALL_EEPROM)
  67. Wire.write(uint8_t((eeprom_address >> 8) & 0xFF)); // Address High, if needed
  68. Wire.write(uint8_t(eeprom_address & 0xFF)); // Address Low
  69. }
  70. void eeprom_write_byte(uint8_t *pos, uint8_t value) {
  71. _eeprom_begin(pos);
  72. Wire.write(value);
  73. Wire.endTransmission();
  74. // wait for write cycle to complete
  75. // this could be done more efficiently with "acknowledge polling"
  76. delay(EEPROM_WRITE_DELAY);
  77. }
  78. uint8_t eeprom_read_byte(uint8_t *pos) {
  79. _eeprom_begin(pos);
  80. Wire.endTransmission();
  81. Wire.requestFrom(_eeprom_calc_device_address(pos), (byte)1);
  82. return Wire.available() ? Wire.read() : 0xFF;
  83. }
  84. #endif // USE_SHARED_EEPROM
  85. #endif // I2C_EEPROM