My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

BL24CXX.cpp 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. #include "../inc/MarlinConfig.h"
  23. #if ENABLED(IIC_BL24CXX_EEPROM)
  24. /**
  25. * PersistentStore for Arduino-style EEPROM interface
  26. * with simple implementations supplied by Marlin.
  27. */
  28. #include "BL24CXX.h"
  29. #ifdef __STM32F1__
  30. #include <libmaple/gpio.h>
  31. #else
  32. #include "../HAL/shared/Delay.h"
  33. #define delay_us(n) DELAY_US(n)
  34. #endif
  35. #ifndef EEPROM_WRITE_DELAY
  36. #define EEPROM_WRITE_DELAY 10
  37. #endif
  38. #ifndef EEPROM_DEVICE_ADDRESS
  39. #define EEPROM_DEVICE_ADDRESS (0x50 << 1)
  40. #endif
  41. // IO direction setting
  42. #ifdef __STM32F1__
  43. #define SDA_IN() do{ PIN_MAP[IIC_EEPROM_SDA].gpio_device->regs->CRH &= 0XFFFF0FFF; PIN_MAP[IIC_EEPROM_SDA].gpio_device->regs->CRH |= 8 << 12; }while(0)
  44. #define SDA_OUT() do{ PIN_MAP[IIC_EEPROM_SDA].gpio_device->regs->CRH &= 0XFFFF0FFF; PIN_MAP[IIC_EEPROM_SDA].gpio_device->regs->CRH |= 3 << 12; }while(0)
  45. #elif defined(STM32F1) || defined(STM32F4)
  46. #define SDA_IN() SET_INPUT(IIC_EEPROM_SDA)
  47. #define SDA_OUT() SET_OUTPUT(IIC_EEPROM_SDA)
  48. #endif
  49. // IO ops
  50. #define IIC_SCL_0() WRITE(IIC_EEPROM_SCL, LOW)
  51. #define IIC_SCL_1() WRITE(IIC_EEPROM_SCL, HIGH)
  52. #define IIC_SDA_0() WRITE(IIC_EEPROM_SDA, LOW)
  53. #define IIC_SDA_1() WRITE(IIC_EEPROM_SDA, HIGH)
  54. #define READ_SDA() READ(IIC_EEPROM_SDA)
  55. //
  56. // Simple IIC interface via libmaple
  57. //
  58. // Initialize IIC
  59. void IIC::init() {
  60. SET_OUTPUT(IIC_EEPROM_SDA);
  61. SET_OUTPUT(IIC_EEPROM_SCL);
  62. IIC_SCL_1();
  63. IIC_SDA_1();
  64. }
  65. // Generate IIC start signal
  66. void IIC::start() {
  67. SDA_OUT(); // SDA line output
  68. IIC_SDA_1();
  69. IIC_SCL_1();
  70. delay_us(4);
  71. IIC_SDA_0(); // START:when CLK is high, DATA change from high to low
  72. delay_us(4);
  73. IIC_SCL_0(); // Clamp the I2C bus, ready to send or receive data
  74. }
  75. // Generate IIC stop signal
  76. void IIC::stop() {
  77. SDA_OUT(); // SDA line output
  78. IIC_SCL_0();
  79. IIC_SDA_0(); // STOP:when CLK is high DATA change from low to high
  80. delay_us(4);
  81. IIC_SCL_1();
  82. IIC_SDA_1(); // Send I2C bus end signal
  83. delay_us(4);
  84. }
  85. // Wait for the response signal to arrive
  86. // 1 = failed to receive response
  87. // 0 = response received
  88. uint8_t IIC::wait_ack() {
  89. uint8_t ucErrTime = 0;
  90. SDA_IN(); // SDA is set as input
  91. IIC_SDA_1(); delay_us(1);
  92. IIC_SCL_1(); delay_us(1);
  93. while (READ_SDA()) {
  94. if (++ucErrTime > 250) {
  95. stop();
  96. return 1;
  97. }
  98. }
  99. IIC_SCL_0(); // Clock output 0
  100. return 0;
  101. }
  102. // Generate ACK response
  103. void IIC::ack() {
  104. IIC_SCL_0();
  105. SDA_OUT();
  106. IIC_SDA_0();
  107. delay_us(2);
  108. IIC_SCL_1();
  109. delay_us(2);
  110. IIC_SCL_0();
  111. }
  112. // No ACK response
  113. void IIC::nAck() {
  114. IIC_SCL_0();
  115. SDA_OUT();
  116. IIC_SDA_1();
  117. delay_us(2);
  118. IIC_SCL_1();
  119. delay_us(2);
  120. IIC_SCL_0();
  121. }
  122. // Send one IIC byte
  123. // Return whether the slave responds
  124. // 1 = there is a response
  125. // 0 = no response
  126. void IIC::send_byte(uint8_t txd) {
  127. SDA_OUT();
  128. IIC_SCL_0(); // Pull down the clock to start data transmission
  129. LOOP_L_N(t, 8) {
  130. // IIC_SDA = (txd & 0x80) >> 7;
  131. if (txd & 0x80) IIC_SDA_1(); else IIC_SDA_0();
  132. txd <<= 1;
  133. delay_us(2); // All three delays are necessary for TEA5767
  134. IIC_SCL_1();
  135. delay_us(2);
  136. IIC_SCL_0();
  137. delay_us(2);
  138. }
  139. }
  140. // Read 1 byte, when ack=1, send ACK, ack=0, send nACK
  141. uint8_t IIC::read_byte(unsigned char ack_chr) {
  142. unsigned char receive = 0;
  143. SDA_IN(); // SDA is set as input
  144. LOOP_L_N(i, 8) {
  145. IIC_SCL_0();
  146. delay_us(2);
  147. IIC_SCL_1();
  148. receive <<= 1;
  149. if (READ_SDA()) receive++;
  150. delay_us(1);
  151. }
  152. ack_chr ? ack() : nAck(); // Send ACK / send nACK
  153. return receive;
  154. }
  155. /******************** EEPROM ********************/
  156. // Initialize the IIC interface
  157. void BL24CXX::init() { IIC::init(); }
  158. // Read a byte at the specified address
  159. // ReadAddr: the address to start reading
  160. // Return: the byte read
  161. uint8_t BL24CXX::readOneByte(uint16_t ReadAddr) {
  162. uint8_t temp = 0;
  163. IIC::start();
  164. if (EE_TYPE > BL24C16) {
  165. IIC::send_byte(EEPROM_DEVICE_ADDRESS); // Send write command
  166. IIC::wait_ack();
  167. IIC::send_byte(ReadAddr >> 8); // Send high address
  168. IIC::wait_ack();
  169. }
  170. else
  171. IIC::send_byte(EEPROM_DEVICE_ADDRESS + ((ReadAddr >> 8) << 1)); // Send device address 0xA0, write data
  172. IIC::wait_ack();
  173. IIC::send_byte(ReadAddr & 0xFF); // Send low address
  174. IIC::wait_ack();
  175. IIC::start();
  176. IIC::send_byte(EEPROM_DEVICE_ADDRESS | 0x01); // Send byte
  177. IIC::wait_ack();
  178. temp = IIC::read_byte(0);
  179. IIC::stop(); // Generate a stop condition
  180. return temp;
  181. }
  182. // Write a data at the address specified by BL24CXX
  183. // WriteAddr: The destination address for writing data
  184. // DataToWrite: the data to be written
  185. void BL24CXX::writeOneByte(uint16_t WriteAddr, uint8_t DataToWrite) {
  186. IIC::start();
  187. if (EE_TYPE > BL24C16) {
  188. IIC::send_byte(EEPROM_DEVICE_ADDRESS); // Send write command
  189. IIC::wait_ack();
  190. IIC::send_byte(WriteAddr >> 8); // Send high address
  191. }
  192. else
  193. IIC::send_byte(EEPROM_DEVICE_ADDRESS + ((WriteAddr >> 8) << 1)); // Send device address 0xA0, write data
  194. IIC::wait_ack();
  195. IIC::send_byte(WriteAddr & 0xFF); // Send low address
  196. IIC::wait_ack();
  197. IIC::send_byte(DataToWrite); // Receiving mode
  198. IIC::wait_ack();
  199. IIC::stop(); // Generate a stop condition
  200. delay(10);
  201. }
  202. // Start writing data of length Len at the specified address in BL24CXX
  203. // This function is used to write 16bit or 32bit data.
  204. // WriteAddr: the address to start writing
  205. // DataToWrite: the first address of the data array
  206. // Len: The length of the data to be written 2, 4
  207. void BL24CXX::writeLenByte(uint16_t WriteAddr, uint32_t DataToWrite, uint8_t Len) {
  208. LOOP_L_N(t, Len)
  209. writeOneByte(WriteAddr + t, (DataToWrite >> (8 * t)) & 0xFF);
  210. }
  211. // Start reading data of length Len from the specified address in BL24CXX
  212. // This function is used to read 16bit or 32bit data.
  213. // ReadAddr: the address to start reading
  214. // Return value: data
  215. // Len: The length of the data to be read 2,4
  216. uint32_t BL24CXX::readLenByte(uint16_t ReadAddr, uint8_t Len) {
  217. uint32_t temp = 0;
  218. LOOP_L_N(t, Len) {
  219. temp <<= 8;
  220. temp += readOneByte(ReadAddr + Len - t - 1);
  221. }
  222. return temp;
  223. }
  224. // Check if BL24CXX is normal
  225. // Return 1: Detection failed
  226. // return 0: detection is successful
  227. #define BL24CXX_TEST_ADDRESS 0x00
  228. #define BL24CXX_TEST_VALUE 0x55
  229. bool BL24CXX::_check() {
  230. return (readOneByte(BL24CXX_TEST_ADDRESS) != BL24CXX_TEST_VALUE); // false = success!
  231. }
  232. bool BL24CXX::check() {
  233. if (_check()) { // Value was written? Good EEPROM!
  234. writeOneByte(BL24CXX_TEST_ADDRESS, BL24CXX_TEST_VALUE); // Write now and check.
  235. return _check();
  236. }
  237. return false; // success!
  238. }
  239. // Start reading the specified number of data at the specified address in BL24CXX
  240. // ReadAddr: The address to start reading is 0~255 for 24c02
  241. // pBuffer: the first address of the data array
  242. // NumToRead: the number of data to be read
  243. void BL24CXX::read(uint16_t ReadAddr, uint8_t *pBuffer, uint16_t NumToRead) {
  244. for (; NumToRead; NumToRead--)
  245. *pBuffer++ = readOneByte(ReadAddr++);
  246. }
  247. // Start writing the specified number of data at the specified address in BL24CXX
  248. // WriteAddr: the address to start writing, 0~255 for 24c02
  249. // pBuffer: the first address of the data array
  250. // NumToWrite: the number of data to be written
  251. void BL24CXX::write(uint16_t WriteAddr, uint8_t *pBuffer, uint16_t NumToWrite) {
  252. for (; NumToWrite; NumToWrite--, WriteAddr++)
  253. writeOneByte(WriteAddr, *pBuffer++);
  254. }
  255. #endif // IIC_BL24CXX_EEPROM