i2c_master.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 "esp_misc.h"
  14. #include "platform.h"
  15. #include "gpio.h"
  16. #include "driver/i2c_master.h"
  17. #include "pin_map.h"
  18. LOCAL uint8 m_nLastSDA;
  19. LOCAL uint8 m_nLastSCL;
  20. LOCAL uint8 pinSDA = 2;
  21. LOCAL uint8 pinSCL = 15;
  22. /******************************************************************************
  23. * FunctionName : i2c_master_setDC
  24. * Description : Internal used function -
  25. * set i2c SDA and SCL bit value for half clk cycle
  26. * Parameters : uint8 SDA
  27. * uint8 SCL
  28. * Returns : NONE
  29. *******************************************************************************/
  30. LOCAL void ICACHE_FLASH_ATTR
  31. i2c_master_setDC(uint8 SDA, uint8 SCL)
  32. {
  33. SDA &= 0x01;
  34. SCL &= 0x01;
  35. m_nLastSDA = SDA;
  36. m_nLastSCL = SCL;
  37. if ((0 == SDA) && (0 == SCL)) {
  38. I2C_MASTER_SDA_LOW_SCL_LOW();
  39. } else if ((0 == SDA) && (1 == SCL)) {
  40. I2C_MASTER_SDA_LOW_SCL_HIGH();
  41. } else if ((1 == SDA) && (0 == SCL)) {
  42. I2C_MASTER_SDA_HIGH_SCL_LOW();
  43. } else {
  44. I2C_MASTER_SDA_HIGH_SCL_HIGH();
  45. }
  46. }
  47. /******************************************************************************
  48. * FunctionName : i2c_master_getDC
  49. * Description : Internal used function -
  50. * get i2c SDA bit value
  51. * Parameters : NONE
  52. * Returns : uint8 - SDA bit value
  53. *******************************************************************************/
  54. LOCAL uint8 ICACHE_FLASH_ATTR
  55. i2c_master_getDC(void)
  56. {
  57. uint8 sda_out;
  58. sda_out = GPIO_INPUT_GET(GPIO_ID_PIN(I2C_MASTER_SDA_GPIO));
  59. return sda_out;
  60. }
  61. /******************************************************************************
  62. * FunctionName : i2c_master_init
  63. * Description : initilize I2C bus to enable i2c operations
  64. * Parameters : NONE
  65. * Returns : NONE
  66. *******************************************************************************/
  67. void ICACHE_FLASH_ATTR
  68. i2c_master_init(void)
  69. {
  70. uint8 i;
  71. i2c_master_setDC(1, 0);
  72. i2c_master_wait(5);
  73. // when SCL = 0, toggle SDA to clear up
  74. i2c_master_setDC(0, 0) ;
  75. i2c_master_wait(5);
  76. i2c_master_setDC(1, 0) ;
  77. i2c_master_wait(5);
  78. // set data_cnt to max value
  79. for (i = 0; i < 28; i++) {
  80. i2c_master_setDC(1, 0);
  81. i2c_master_wait(5); // sda 1, scl 0
  82. i2c_master_setDC(1, 1);
  83. i2c_master_wait(5); // sda 1, scl 1
  84. }
  85. // reset all
  86. i2c_master_stop();
  87. return;
  88. }
  89. uint8 i2c_master_get_pinSDA(){
  90. return pinSDA;
  91. }
  92. uint8 i2c_master_get_pinSCL(){
  93. return pinSCL;
  94. }
  95. /******************************************************************************
  96. * FunctionName : i2c_master_gpio_init
  97. * Description : config SDA and SCL gpio to open-drain output mode,
  98. * mux and gpio num defined in i2c_master.h
  99. * Parameters : NONE
  100. * Returns : NONE
  101. *******************************************************************************/
  102. void ICACHE_FLASH_ATTR
  103. i2c_master_gpio_init(uint8 sda, uint8 scl)
  104. {
  105. pinSDA = pin_num[sda];
  106. pinSCL = pin_num[scl];
  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. }