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

BL24CXX.cpp 8.1KB

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