i2c_master.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * ESPRSSIF MIT License
  3. *
  4. * Copyright (c) 2016 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
  5. *
  6. * Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case,
  7. * it is free of charge, to any person obtaining a copy of this software and associated
  8. * documentation files (the "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the Software is furnished
  11. * to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all copies or
  14. * substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. */
  24. #include "ets_sys.h"
  25. #include "osapi.h"
  26. #include "gpio.h"
  27. #include "driver/i2c_master.h"
  28. LOCAL uint8 m_nLastSDA;
  29. LOCAL uint8 m_nLastSCL;
  30. /******************************************************************************
  31. * FunctionName : i2c_master_setDC
  32. * Description : Internal used function -
  33. * set i2c SDA and SCL bit value for half clk cycle
  34. * Parameters : uint8 SDA
  35. * uint8 SCL
  36. * Returns : NONE
  37. *******************************************************************************/
  38. LOCAL void ICACHE_FLASH_ATTR
  39. i2c_master_setDC(uint8 SDA, uint8 SCL)
  40. {
  41. SDA &= 0x01;
  42. SCL &= 0x01;
  43. m_nLastSDA = SDA;
  44. m_nLastSCL = SCL;
  45. if ((0 == SDA) && (0 == SCL)) {
  46. I2C_MASTER_SDA_LOW_SCL_LOW();
  47. } else if ((0 == SDA) && (1 == SCL)) {
  48. I2C_MASTER_SDA_LOW_SCL_HIGH();
  49. } else if ((1 == SDA) && (0 == SCL)) {
  50. I2C_MASTER_SDA_HIGH_SCL_LOW();
  51. } else {
  52. I2C_MASTER_SDA_HIGH_SCL_HIGH();
  53. }
  54. }
  55. /******************************************************************************
  56. * FunctionName : i2c_master_getDC
  57. * Description : Internal used function -
  58. * get i2c SDA bit value
  59. * Parameters : NONE
  60. * Returns : uint8 - SDA bit value
  61. *******************************************************************************/
  62. LOCAL uint8 ICACHE_FLASH_ATTR
  63. i2c_master_getDC(void)
  64. {
  65. uint8 sda_out;
  66. sda_out = GPIO_INPUT_GET(GPIO_ID_PIN(I2C_MASTER_SDA_GPIO));
  67. return sda_out;
  68. }
  69. /******************************************************************************
  70. * FunctionName : i2c_master_init
  71. * Description : initilize I2C bus to enable i2c operations
  72. * Parameters : NONE
  73. * Returns : NONE
  74. *******************************************************************************/
  75. void ICACHE_FLASH_ATTR
  76. i2c_master_init(void)
  77. {
  78. uint8 i;
  79. i2c_master_setDC(1, 0);
  80. i2c_master_wait(5);
  81. // when SCL = 0, toggle SDA to clear up
  82. i2c_master_setDC(0, 0) ;
  83. i2c_master_wait(5);
  84. i2c_master_setDC(1, 0) ;
  85. i2c_master_wait(5);
  86. // set data_cnt to max value
  87. for (i = 0; i < 28; i++) {
  88. i2c_master_setDC(1, 0);
  89. i2c_master_wait(5); // sda 1, scl 0
  90. i2c_master_setDC(1, 1);
  91. i2c_master_wait(5); // sda 1, scl 1
  92. }
  93. // reset all
  94. i2c_master_stop();
  95. return;
  96. }
  97. /******************************************************************************
  98. * FunctionName : i2c_master_gpio_init
  99. * Description : config SDA and SCL gpio to open-drain output mode,
  100. * mux and gpio num defined in i2c_master.h
  101. * Parameters : NONE
  102. * Returns : NONE
  103. *******************************************************************************/
  104. void ICACHE_FLASH_ATTR
  105. i2c_master_gpio_init(void)
  106. {
  107. ETS_GPIO_INTR_DISABLE() ;
  108. // ETS_INTR_LOCK();
  109. PIN_FUNC_SELECT(I2C_MASTER_SDA_MUX, I2C_MASTER_SDA_FUNC);
  110. PIN_FUNC_SELECT(I2C_MASTER_SCL_MUX, I2C_MASTER_SCL_FUNC);
  111. GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN(I2C_MASTER_SDA_GPIO)), GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(I2C_MASTER_SDA_GPIO))) | GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_ENABLE)); //open drain;
  112. GPIO_REG_WRITE(GPIO_ENABLE_ADDRESS, GPIO_REG_READ(GPIO_ENABLE_ADDRESS) | (1 << I2C_MASTER_SDA_GPIO));
  113. GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN(I2C_MASTER_SCL_GPIO)), GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(I2C_MASTER_SCL_GPIO))) | GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_ENABLE)); //open drain;
  114. GPIO_REG_WRITE(GPIO_ENABLE_ADDRESS, GPIO_REG_READ(GPIO_ENABLE_ADDRESS) | (1 << I2C_MASTER_SCL_GPIO));
  115. I2C_MASTER_SDA_HIGH_SCL_HIGH();
  116. ETS_GPIO_INTR_ENABLE() ;
  117. // ETS_INTR_UNLOCK();
  118. i2c_master_init();
  119. }
  120. /******************************************************************************
  121. * FunctionName : i2c_master_start
  122. * Description : set i2c to send state
  123. * Parameters : NONE
  124. * Returns : NONE
  125. *******************************************************************************/
  126. void ICACHE_FLASH_ATTR
  127. i2c_master_start(void)
  128. {
  129. i2c_master_setDC(1, m_nLastSCL);
  130. i2c_master_wait(5);
  131. i2c_master_setDC(1, 1);
  132. i2c_master_wait(5); // sda 1, scl 1
  133. i2c_master_setDC(0, 1);
  134. i2c_master_wait(5); // sda 0, scl 1
  135. }
  136. /******************************************************************************
  137. * FunctionName : i2c_master_stop
  138. * Description : set i2c to stop sending state
  139. * Parameters : NONE
  140. * Returns : NONE
  141. *******************************************************************************/
  142. void ICACHE_FLASH_ATTR
  143. i2c_master_stop(void)
  144. {
  145. i2c_master_wait(5);
  146. i2c_master_setDC(0, m_nLastSCL);
  147. i2c_master_wait(5); // sda 0
  148. i2c_master_setDC(0, 1);
  149. i2c_master_wait(5); // sda 0, scl 1
  150. i2c_master_setDC(1, 1);
  151. i2c_master_wait(5); // sda 1, scl 1
  152. }
  153. /******************************************************************************
  154. * FunctionName : i2c_master_setAck
  155. * Description : set ack to i2c bus as level value
  156. * Parameters : uint8 level - 0 or 1
  157. * Returns : NONE
  158. *******************************************************************************/
  159. void ICACHE_FLASH_ATTR
  160. i2c_master_setAck(uint8 level)
  161. {
  162. i2c_master_setDC(m_nLastSDA, 0);
  163. i2c_master_wait(5);
  164. i2c_master_setDC(level, 0);
  165. i2c_master_wait(5); // sda level, scl 0
  166. i2c_master_setDC(level, 1);
  167. i2c_master_wait(8); // sda level, scl 1
  168. i2c_master_setDC(level, 0);
  169. i2c_master_wait(5); // sda level, scl 0
  170. i2c_master_setDC(1, 0);
  171. i2c_master_wait(5);
  172. }
  173. /******************************************************************************
  174. * FunctionName : i2c_master_getAck
  175. * Description : confirm if peer send ack
  176. * Parameters : NONE
  177. * Returns : uint8 - ack value, 0 or 1
  178. *******************************************************************************/
  179. uint8 ICACHE_FLASH_ATTR
  180. i2c_master_getAck(void)
  181. {
  182. uint8 retVal;
  183. i2c_master_setDC(m_nLastSDA, 0);
  184. i2c_master_wait(5);
  185. i2c_master_setDC(1, 0);
  186. i2c_master_wait(5);
  187. i2c_master_setDC(1, 1);
  188. i2c_master_wait(5);
  189. retVal = i2c_master_getDC();
  190. i2c_master_wait(5);
  191. i2c_master_setDC(1, 0);
  192. i2c_master_wait(5);
  193. return retVal;
  194. }
  195. /******************************************************************************
  196. * FunctionName : i2c_master_checkAck
  197. * Description : get dev response
  198. * Parameters : NONE
  199. * Returns : true : get ack ; false : get nack
  200. *******************************************************************************/
  201. bool ICACHE_FLASH_ATTR
  202. i2c_master_checkAck(void)
  203. {
  204. if(i2c_master_getAck()){
  205. return FALSE;
  206. }else{
  207. return TRUE;
  208. }
  209. }
  210. /******************************************************************************
  211. * FunctionName : i2c_master_send_ack
  212. * Description : response ack
  213. * Parameters : NONE
  214. * Returns : NONE
  215. *******************************************************************************/
  216. void ICACHE_FLASH_ATTR
  217. i2c_master_send_ack(void)
  218. {
  219. i2c_master_setAck(0x0);
  220. }
  221. /******************************************************************************
  222. * FunctionName : i2c_master_send_nack
  223. * Description : response nack
  224. * Parameters : NONE
  225. * Returns : NONE
  226. *******************************************************************************/
  227. void ICACHE_FLASH_ATTR
  228. i2c_master_send_nack(void)
  229. {
  230. i2c_master_setAck(0x1);
  231. }
  232. /******************************************************************************
  233. * FunctionName : i2c_master_readByte
  234. * Description : read Byte from i2c bus
  235. * Parameters : NONE
  236. * Returns : uint8 - readed value
  237. *******************************************************************************/
  238. uint8 ICACHE_FLASH_ATTR
  239. i2c_master_readByte(void)
  240. {
  241. uint8 retVal = 0;
  242. uint8 k, i;
  243. i2c_master_wait(5);
  244. i2c_master_setDC(m_nLastSDA, 0);
  245. i2c_master_wait(5); // sda 1, scl 0
  246. for (i = 0; i < 8; i++) {
  247. i2c_master_wait(5);
  248. i2c_master_setDC(1, 0);
  249. i2c_master_wait(5); // sda 1, scl 0
  250. i2c_master_setDC(1, 1);
  251. i2c_master_wait(5); // sda 1, scl 1
  252. k = i2c_master_getDC();
  253. i2c_master_wait(5);
  254. if (i == 7) {
  255. i2c_master_wait(3); ////
  256. }
  257. k <<= (7 - i);
  258. retVal |= k;
  259. }
  260. i2c_master_setDC(1, 0);
  261. i2c_master_wait(5); // sda 1, scl 0
  262. return retVal;
  263. }
  264. /******************************************************************************
  265. * FunctionName : i2c_master_writeByte
  266. * Description : write wrdata value(one byte) into i2c
  267. * Parameters : uint8 wrdata - write value
  268. * Returns : NONE
  269. *******************************************************************************/
  270. void ICACHE_FLASH_ATTR
  271. i2c_master_writeByte(uint8 wrdata)
  272. {
  273. uint8 dat;
  274. sint8 i;
  275. i2c_master_wait(5);
  276. i2c_master_setDC(m_nLastSDA, 0);
  277. i2c_master_wait(5);
  278. for (i = 7; i >= 0; i--) {
  279. dat = wrdata >> i;
  280. i2c_master_setDC(dat, 0);
  281. i2c_master_wait(5);
  282. i2c_master_setDC(dat, 1);
  283. i2c_master_wait(5);
  284. if (i == 0) {
  285. i2c_master_wait(3); ////
  286. }
  287. i2c_master_setDC(dat, 0);
  288. i2c_master_wait(5);
  289. }
  290. }