i2c_master.c 8.3 KB

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