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.

I2cEeprom.cpp 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. /**
  20. * Description: functions for I2C connected external EEPROM.
  21. * Not platform dependent.
  22. */
  23. #include "../../MarlinConfig.h"
  24. #if ENABLED(I2C_EEPROM)
  25. // --------------------------------------------------------------------------
  26. // Includes
  27. // --------------------------------------------------------------------------
  28. #include "HAL.h"
  29. #include <Wire.h>
  30. // --------------------------------------------------------------------------
  31. // Externals
  32. // --------------------------------------------------------------------------
  33. // --------------------------------------------------------------------------
  34. // Local defines
  35. // --------------------------------------------------------------------------
  36. // --------------------------------------------------------------------------
  37. // Types
  38. // --------------------------------------------------------------------------
  39. // --------------------------------------------------------------------------
  40. // Variables
  41. // --------------------------------------------------------------------------
  42. // --------------------------------------------------------------------------
  43. // Public Variables
  44. // --------------------------------------------------------------------------
  45. // --------------------------------------------------------------------------
  46. // Private Variables
  47. // --------------------------------------------------------------------------
  48. // --------------------------------------------------------------------------
  49. // Function prototypes
  50. // --------------------------------------------------------------------------
  51. // --------------------------------------------------------------------------
  52. // Private functions
  53. // --------------------------------------------------------------------------
  54. // --------------------------------------------------------------------------
  55. // Public functions
  56. // --------------------------------------------------------------------------
  57. static bool eeprom_initialised = false;
  58. static uint8_t eeprom_device_address = 0x50;
  59. static void eeprom_init(void) {
  60. if (!eeprom_initialised) {
  61. Wire.begin();
  62. eeprom_initialised = true;
  63. }
  64. }
  65. void eeprom_write_byte(unsigned char *pos, unsigned char value) {
  66. unsigned eeprom_address = (unsigned) pos;
  67. eeprom_init();
  68. Wire.beginTransmission(eeprom_device_address);
  69. Wire.write((int)(eeprom_address >> 8)); // MSB
  70. Wire.write((int)(eeprom_address & 0xFF)); // LSB
  71. Wire.write(value);
  72. Wire.endTransmission();
  73. // wait for write cycle to complete
  74. // this could be done more efficiently with "acknowledge polling"
  75. delay(5);
  76. }
  77. // WARNING: address is a page address, 6-bit end will wrap around
  78. // also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes
  79. void eeprom_update_block(const void* pos, void* eeprom_address, size_t n) {
  80. uint8_t eeprom_temp[32] = {0};
  81. uint8_t flag = 0;
  82. eeprom_init();
  83. Wire.beginTransmission(eeprom_device_address);
  84. Wire.write((int)((unsigned)eeprom_address >> 8)); // MSB
  85. Wire.write((int)((unsigned)eeprom_address & 0xFF)); // LSB
  86. Wire.endTransmission();
  87. Wire.requestFrom(eeprom_device_address, (byte)n);
  88. for (byte c = 0; c < n; c++) {
  89. if (Wire.available()) eeprom_temp[c] = Wire.read();
  90. flag |= (eeprom_temp[c] ^ *((uint8_t*)pos + c));
  91. }
  92. if (flag) {
  93. Wire.beginTransmission(eeprom_device_address);
  94. Wire.write((int)((unsigned)eeprom_address >> 8)); // MSB
  95. Wire.write((int)((unsigned)eeprom_address & 0xFF)); // LSB
  96. Wire.write((uint8_t*)(pos), n);
  97. Wire.endTransmission();
  98. // wait for write cycle to complete
  99. // this could be done more efficiently with "acknowledge polling"
  100. delay(5);
  101. }
  102. }
  103. unsigned char eeprom_read_byte(unsigned char *pos) {
  104. byte data = 0xFF;
  105. unsigned eeprom_address = (unsigned) pos;
  106. eeprom_init ();
  107. Wire.beginTransmission(eeprom_device_address);
  108. Wire.write((int)(eeprom_address >> 8)); // MSB
  109. Wire.write((int)(eeprom_address & 0xFF)); // LSB
  110. Wire.endTransmission();
  111. Wire.requestFrom(eeprom_device_address, (byte)1);
  112. if (Wire.available())
  113. data = Wire.read();
  114. return data;
  115. }
  116. // maybe let's not read more than 30 or 32 bytes at a time!
  117. void eeprom_read_block(void* pos, const void* eeprom_address, size_t n) {
  118. eeprom_init();
  119. Wire.beginTransmission(eeprom_device_address);
  120. Wire.write((int)((unsigned)eeprom_address >> 8)); // MSB
  121. Wire.write((int)((unsigned)eeprom_address & 0xFF)); // LSB
  122. Wire.endTransmission();
  123. Wire.requestFrom(eeprom_device_address, (byte)n);
  124. for (byte c = 0; c < n; c++ )
  125. if (Wire.available()) *((uint8_t*)pos + c) = Wire.read();
  126. }
  127. #endif // ENABLED(I2C_EEPROM)