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

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