My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

I2cEeprom.cpp 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. // --------------------------------------------------------------------------
  29. // Includes
  30. // --------------------------------------------------------------------------
  31. #include HAL_PATH(.., 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 uint8_t eeprom_device_address = 0x50;
  61. static void eeprom_init(void) {
  62. static bool eeprom_initialized = false;
  63. if (!eeprom_initialized) {
  64. Wire.begin();
  65. eeprom_initialized = true;
  66. }
  67. }
  68. void eeprom_write_byte(uint8_t *pos, unsigned char value) {
  69. unsigned eeprom_address = (unsigned) pos;
  70. eeprom_init();
  71. Wire.beginTransmission(I2C_ADDRESS(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. eeprom_init();
  84. Wire.beginTransmission(I2C_ADDRESS(eeprom_device_address));
  85. Wire.write((int)((unsigned)eeprom_address >> 8)); // MSB
  86. Wire.write((int)((unsigned)eeprom_address & 0xFF)); // LSB
  87. Wire.endTransmission();
  88. uint8_t *ptr = (uint8_t*)pos;
  89. uint8_t flag = 0;
  90. Wire.requestFrom(eeprom_device_address, (byte)n);
  91. for (byte c = 0; c < n && Wire.available(); c++)
  92. flag |= Wire.read() ^ ptr[c];
  93. if (flag) {
  94. Wire.beginTransmission(I2C_ADDRESS(eeprom_device_address));
  95. Wire.write((int)((unsigned)eeprom_address >> 8)); // MSB
  96. Wire.write((int)((unsigned)eeprom_address & 0xFF)); // LSB
  97. Wire.write((uint8_t*)pos, n);
  98. Wire.endTransmission();
  99. // wait for write cycle to complete
  100. // this could be done more efficiently with "acknowledge polling"
  101. delay(5);
  102. }
  103. }
  104. uint8_t eeprom_read_byte(uint8_t *pos) {
  105. unsigned eeprom_address = (unsigned)pos;
  106. eeprom_init();
  107. Wire.beginTransmission(I2C_ADDRESS(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. return Wire.available() ? Wire.read() : 0xFF;
  113. }
  114. // maybe let's not read more than 30 or 32 bytes at a time!
  115. void eeprom_read_block(void* pos, const void* eeprom_address, size_t n) {
  116. eeprom_init();
  117. Wire.beginTransmission(I2C_ADDRESS(eeprom_device_address));
  118. Wire.write((int)((unsigned)eeprom_address >> 8)); // MSB
  119. Wire.write((int)((unsigned)eeprom_address & 0xFF)); // LSB
  120. Wire.endTransmission();
  121. Wire.requestFrom(eeprom_device_address, (byte)n);
  122. for (byte c = 0; c < n; c++ )
  123. if (Wire.available()) *((uint8_t*)pos + c) = Wire.read();
  124. }
  125. #endif // I2C_EEPROM