i2c_master.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /******************************************************************************
  2. * Copyright 2013-2014 Espressif Systems (Wuxi)
  3. *
  4. * FileName: i2c_master.c
  5. *
  6. * Description: i2c master API
  7. *
  8. * Modification history:
  9. * 2014/3/12, v1.0 create this file.
  10. *******************************************************************************/
  11. #include "ets_sys.h"
  12. #include "osapi.h"
  13. #include "gpio.h"
  14. #include "driver/i2c_master.h"
  15. LOCAL uint8 m_nLastSDA;
  16. LOCAL uint8 m_nLastSCL;
  17. /******************************************************************************
  18. * FunctionName : i2c_master_setDC
  19. * Description : Internal used function -
  20. * set i2c SDA and SCL bit value for half clk cycle
  21. * Parameters : uint8 SDA
  22. * uint8 SCL
  23. * Returns : NONE
  24. *******************************************************************************/
  25. LOCAL void ICACHE_FLASH_ATTR
  26. i2c_master_setDC(uint8 SDA, uint8 SCL)
  27. {
  28. SDA &= 0x01;
  29. SCL &= 0x01;
  30. m_nLastSDA = SDA;
  31. m_nLastSCL = SCL;
  32. if ((0 == SDA) && (0 == SCL)) {
  33. I2C_MASTER_SDA_LOW_SCL_LOW();
  34. } else if ((0 == SDA) && (1 == SCL)) {
  35. I2C_MASTER_SDA_LOW_SCL_HIGH();
  36. } else if ((1 == SDA) && (0 == SCL)) {
  37. I2C_MASTER_SDA_HIGH_SCL_LOW();
  38. } else {
  39. I2C_MASTER_SDA_HIGH_SCL_HIGH();
  40. }
  41. }
  42. /******************************************************************************
  43. * FunctionName : i2c_master_getDC
  44. * Description : Internal used function -
  45. * get i2c SDA bit value
  46. * Parameters : NONE
  47. * Returns : uint8 - SDA bit value
  48. *******************************************************************************/
  49. LOCAL uint8 ICACHE_FLASH_ATTR
  50. i2c_master_getDC(void)
  51. {
  52. uint8 sda_out;
  53. sda_out = GPIO_INPUT_GET(GPIO_ID_PIN(I2C_MASTER_SDA_GPIO));
  54. return sda_out;
  55. }
  56. /******************************************************************************
  57. * FunctionName : i2c_master_init
  58. * Description : initilize I2C bus to enable i2c operations
  59. * Parameters : NONE
  60. * Returns : NONE
  61. *******************************************************************************/
  62. void ICACHE_FLASH_ATTR
  63. i2c_master_init(void)
  64. {
  65. uint8 i;
  66. i2c_master_setDC(1, 0);
  67. i2c_master_wait(5);
  68. // when SCL = 0, toggle SDA to clear up
  69. i2c_master_setDC(0, 0) ;
  70. i2c_master_wait(5);
  71. i2c_master_setDC(1, 0) ;
  72. i2c_master_wait(5);
  73. // set data_cnt to max value
  74. for (i = 0; i < 28; i++) {
  75. i2c_master_setDC(1, 0);
  76. i2c_master_wait(5); // sda 1, scl 0
  77. i2c_master_setDC(1, 1);
  78. i2c_master_wait(5); // sda 1, scl 1
  79. }
  80. // reset all
  81. i2c_master_stop();
  82. return;
  83. }
  84. /******************************************************************************
  85. * FunctionName : i2c_master_gpio_init
  86. * Description : config SDA and SCL gpio to open-drain output mode,
  87. * mux and gpio num defined in i2c_master.h
  88. * Parameters : NONE
  89. * Returns : NONE
  90. *******************************************************************************/
  91. void ICACHE_FLASH_ATTR
  92. i2c_master_gpio_init(void)
  93. {
  94. ETS_GPIO_INTR_DISABLE() ;
  95. // ETS_INTR_LOCK();
  96. PIN_FUNC_SELECT(I2C_MASTER_SDA_MUX, I2C_MASTER_SDA_FUNC);
  97. PIN_FUNC_SELECT(I2C_MASTER_SCL_MUX, I2C_MASTER_SCL_FUNC);
  98. 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;
  99. GPIO_REG_WRITE(GPIO_ENABLE_ADDRESS, GPIO_REG_READ(GPIO_ENABLE_ADDRESS) | (1 << I2C_MASTER_SDA_GPIO));
  100. 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;
  101. GPIO_REG_WRITE(GPIO_ENABLE_ADDRESS, GPIO_REG_READ(GPIO_ENABLE_ADDRESS) | (1 << I2C_MASTER_SCL_GPIO));
  102. I2C_MASTER_SDA_HIGH_SCL_HIGH();
  103. ETS_GPIO_INTR_ENABLE() ;
  104. // ETS_INTR_UNLOCK();
  105. i2c_master_init();
  106. }
  107. /******************************************************************************
  108. * FunctionName : i2c_master_start
  109. * Description : set i2c to send state
  110. * Parameters : NONE
  111. * Returns : NONE
  112. *******************************************************************************/
  113. void ICACHE_FLASH_ATTR
  114. i2c_master_start(void)
  115. {
  116. i2c_master_setDC(1, m_nLastSCL);
  117. i2c_master_wait(5);
  118. i2c_master_setDC(1, 1);
  119. i2c_master_wait(5); // sda 1, scl 1
  120. i2c_master_setDC(0, 1);
  121. i2c_master_wait(5); // sda 0, scl 1
  122. }
  123. /******************************************************************************
  124. * FunctionName : i2c_master_stop
  125. * Description : set i2c to stop sending state
  126. * Parameters : NONE
  127. * Returns : NONE
  128. *******************************************************************************/
  129. void ICACHE_FLASH_ATTR
  130. i2c_master_stop(void)
  131. {
  132. i2c_master_wait(5);
  133. i2c_master_setDC(0, m_nLastSCL);
  134. i2c_master_wait(5); // sda 0
  135. i2c_master_setDC(0, 1);
  136. i2c_master_wait(5); // sda 0, scl 1
  137. i2c_master_setDC(1, 1);
  138. i2c_master_wait(5); // sda 1, scl 1
  139. }
  140. /******************************************************************************
  141. * FunctionName : i2c_master_setAck
  142. * Description : set ack to i2c bus as level value
  143. * Parameters : uint8 level - 0 or 1
  144. * Returns : NONE
  145. *******************************************************************************/
  146. void ICACHE_FLASH_ATTR
  147. i2c_master_setAck(uint8 level)
  148. {
  149. i2c_master_setDC(m_nLastSDA, 0);
  150. i2c_master_wait(5);
  151. i2c_master_setDC(level, 0);
  152. i2c_master_wait(5); // sda level, scl 0
  153. i2c_master_setDC(level, 1);
  154. i2c_master_wait(8); // sda level, scl 1
  155. i2c_master_setDC(level, 0);
  156. i2c_master_wait(5); // sda level, scl 0
  157. i2c_master_setDC(1, 0);
  158. i2c_master_wait(5);
  159. }
  160. /******************************************************************************
  161. * FunctionName : i2c_master_getAck
  162. * Description : confirm if peer send ack
  163. * Parameters : NONE
  164. * Returns : uint8 - ack value, 0 or 1
  165. *******************************************************************************/
  166. uint8 ICACHE_FLASH_ATTR
  167. i2c_master_getAck(void)
  168. {
  169. uint8 retVal;
  170. i2c_master_setDC(m_nLastSDA, 0);
  171. i2c_master_wait(5);
  172. i2c_master_setDC(1, 0);
  173. i2c_master_wait(5);
  174. i2c_master_setDC(1, 1);
  175. i2c_master_wait(5);
  176. retVal = i2c_master_getDC();
  177. i2c_master_wait(5);
  178. i2c_master_setDC(1, 0);
  179. i2c_master_wait(5);
  180. return retVal;
  181. }
  182. /******************************************************************************
  183. * FunctionName : i2c_master_checkAck
  184. * Description : get dev response
  185. * Parameters : NONE
  186. * Returns : true : get ack ; false : get nack
  187. *******************************************************************************/
  188. bool ICACHE_FLASH_ATTR
  189. i2c_master_checkAck(void)
  190. {
  191. if(i2c_master_getAck()){
  192. return FALSE;
  193. }else{
  194. return TRUE;
  195. }
  196. }
  197. /******************************************************************************
  198. * FunctionName : i2c_master_send_ack
  199. * Description : response ack
  200. * Parameters : NONE
  201. * Returns : NONE
  202. *******************************************************************************/
  203. void ICACHE_FLASH_ATTR
  204. i2c_master_send_ack(void)
  205. {
  206. i2c_master_setAck(0x0);
  207. }
  208. /******************************************************************************
  209. * FunctionName : i2c_master_send_nack
  210. * Description : response nack
  211. * Parameters : NONE
  212. * Returns : NONE
  213. *******************************************************************************/
  214. void ICACHE_FLASH_ATTR
  215. i2c_master_send_nack(void)
  216. {
  217. i2c_master_setAck(0x1);
  218. }
  219. /******************************************************************************
  220. * FunctionName : i2c_master_readByte
  221. * Description : read Byte from i2c bus
  222. * Parameters : NONE
  223. * Returns : uint8 - readed value
  224. *******************************************************************************/
  225. uint8 ICACHE_FLASH_ATTR
  226. i2c_master_readByte(void)
  227. {
  228. uint8 retVal = 0;
  229. uint8 k, i;
  230. i2c_master_wait(5);
  231. i2c_master_setDC(m_nLastSDA, 0);
  232. i2c_master_wait(5); // sda 1, scl 0
  233. for (i = 0; i < 8; i++) {
  234. i2c_master_wait(5);
  235. i2c_master_setDC(1, 0);
  236. i2c_master_wait(5); // sda 1, scl 0
  237. i2c_master_setDC(1, 1);
  238. i2c_master_wait(5); // sda 1, scl 1
  239. k = i2c_master_getDC();
  240. i2c_master_wait(5);
  241. if (i == 7) {
  242. i2c_master_wait(3); ////
  243. }
  244. k <<= (7 - i);
  245. retVal |= k;
  246. }
  247. i2c_master_setDC(1, 0);
  248. i2c_master_wait(5); // sda 1, scl 0
  249. return retVal;
  250. }
  251. /******************************************************************************
  252. * FunctionName : i2c_master_writeByte
  253. * Description : write wrdata value(one byte) into i2c
  254. * Parameters : uint8 wrdata - write value
  255. * Returns : NONE
  256. *******************************************************************************/
  257. void ICACHE_FLASH_ATTR
  258. i2c_master_writeByte(uint8 wrdata)
  259. {
  260. uint8 dat;
  261. sint8 i;
  262. i2c_master_wait(5);
  263. i2c_master_setDC(m_nLastSDA, 0);
  264. i2c_master_wait(5);
  265. for (i = 7; i >= 0; i--) {
  266. dat = wrdata >> i;
  267. i2c_master_setDC(dat, 0);
  268. i2c_master_wait(5);
  269. i2c_master_setDC(dat, 1);
  270. i2c_master_wait(5);
  271. if (i == 0) {
  272. i2c_master_wait(3); ////
  273. }
  274. i2c_master_setDC(dat, 0);
  275. i2c_master_wait(5);
  276. }
  277. }