i2c_master.c 9.6 KB

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