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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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. // --------------------------------------------------------------------------
  29. // Includes
  30. // --------------------------------------------------------------------------
  31. #include "HAL.h"
  32. #include <Wire.h>
  33. // --------------------------------------------------------------------------
  34. // Externals
  35. // --------------------------------------------------------------------------
  36. // --------------------------------------------------------------------------
  37. // Local defines
  38. // --------------------------------------------------------------------------
  39. // --------------------------------------------------------------------------
  40. // Types
  41. // --------------------------------------------------------------------------
  42. // --------------------------------------------------------------------------
  43. // Variables
  44. // --------------------------------------------------------------------------
  45. // --------------------------------------------------------------------------
  46. // Public Variables
  47. // --------------------------------------------------------------------------
  48. // --------------------------------------------------------------------------
  49. // Private Variables
  50. // --------------------------------------------------------------------------
  51. // --------------------------------------------------------------------------
  52. // Function prototypes
  53. // --------------------------------------------------------------------------
  54. // --------------------------------------------------------------------------
  55. // Private functions
  56. // --------------------------------------------------------------------------
  57. // --------------------------------------------------------------------------
  58. // Public functions
  59. // --------------------------------------------------------------------------
  60. static bool eeprom_initialised = false;
  61. static uint8_t eeprom_device_address = 0x50;
  62. static void eeprom_init(void) {
  63. if (!eeprom_initialised) {
  64. Wire.begin();
  65. eeprom_initialised = true;
  66. }
  67. }
  68. void eeprom_write_byte(unsigned char *pos, unsigned char value) {
  69. unsigned eeprom_address = (unsigned) pos;
  70. eeprom_init();
  71. Wire.beginTransmission(eeprom_device_address);
  72. Wire.write((int)(eeprom_address >> 8)); // MSB
  73. Wire.write((int)(eeprom_address & 0xFF)); // LSB
  74. Wire.write(value);
  75. Wire.endTransmission();
  76. // wait for write cycle to complete
  77. // this could be done more efficiently with "acknowledge polling"
  78. delay(5);
  79. }
  80. // WARNING: address is a page address, 6-bit end will wrap around
  81. // also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes
  82. void eeprom_update_block(const void* pos, void* eeprom_address, size_t n) {
  83. uint8_t eeprom_temp[32] = {0};
  84. uint8_t flag = 0;
  85. eeprom_init();
  86. Wire.beginTransmission(eeprom_device_address);
  87. Wire.write((int)((unsigned)eeprom_address >> 8)); // MSB
  88. Wire.write((int)((unsigned)eeprom_address & 0xFF)); // LSB
  89. Wire.endTransmission();
  90. Wire.requestFrom(eeprom_device_address, (byte)n);
  91. for (byte c = 0; c < n; c++) {
  92. if (Wire.available()) eeprom_temp[c] = Wire.read();
  93. flag |= (eeprom_temp[c] ^ *((uint8_t*)pos + c));
  94. }
  95. if (flag) {
  96. Wire.beginTransmission(eeprom_device_address);
  97. Wire.write((int)((unsigned)eeprom_address >> 8)); // MSB
  98. Wire.write((int)((unsigned)eeprom_address & 0xFF)); // LSB
  99. Wire.write((uint8_t*)(pos), n);
  100. Wire.endTransmission();
  101. // wait for write cycle to complete
  102. // this could be done more efficiently with "acknowledge polling"
  103. delay(5);
  104. }
  105. }
  106. unsigned char eeprom_read_byte(unsigned char *pos) {
  107. byte data = 0xFF;
  108. unsigned eeprom_address = (unsigned) pos;
  109. eeprom_init ();
  110. Wire.beginTransmission(eeprom_device_address);
  111. Wire.write((int)(eeprom_address >> 8)); // MSB
  112. Wire.write((int)(eeprom_address & 0xFF)); // LSB
  113. Wire.endTransmission();
  114. Wire.requestFrom(eeprom_device_address, (byte)1);
  115. if (Wire.available())
  116. data = Wire.read();
  117. return data;
  118. }
  119. // maybe let's not read more than 30 or 32 bytes at a time!
  120. void eeprom_read_block(void* pos, const void* eeprom_address, size_t n) {
  121. eeprom_init();
  122. Wire.beginTransmission(eeprom_device_address);
  123. Wire.write((int)((unsigned)eeprom_address >> 8)); // MSB
  124. Wire.write((int)((unsigned)eeprom_address & 0xFF)); // LSB
  125. Wire.endTransmission();
  126. Wire.requestFrom(eeprom_device_address, (byte)n);
  127. for (byte c = 0; c < n; c++ )
  128. if (Wire.available()) *((uint8_t*)pos + c) = Wire.read();
  129. }
  130. #endif // ENABLED(I2C_EEPROM)