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.

BL24CXX.cpp 7.9KB

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