i2c_master.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /*
  2. * ESPRESSIF 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. * Rework of original driver: Natalia Sorokina <sonaux@gmail.com>, 2018
  25. */
  26. #include <stdlib.h>
  27. #include "ets_sys.h"
  28. #include "osapi.h"
  29. #include "gpio.h"
  30. #include "user_interface.h"
  31. #include "cpu_esp8266.h"
  32. #include "pin_map.h"
  33. #include "user_config.h"
  34. #include "driver/i2c_master.h"
  35. #ifndef I2C_MASTER_OLD_VERSION
  36. /******************************************************************************
  37. * NEW driver
  38. * Enabled if I2C_MASTER_OLD_VERSION is not defined in user_config.h
  39. *******************************************************************************/
  40. // Supports multiple i2c buses
  41. // I2C speed in range 25kHz - 550kHz (25kHz - 1MHz if CPU at 160MHz)
  42. // If GPIO16 is used as SCL then speed is limited to 25kHz - 400kHz
  43. // Speed is defined for every bus separately
  44. // enable use GPIO16 (D0) pin as SCL line
  45. #ifdef I2C_MASTER_GPIO16_ENABLE
  46. #define IS_PIN16(n) ((n)==16)
  47. // CPU_CYCLES_BETWEEN_DELAYS describes how much cpu cycles code runs
  48. // between i2c_master_setDC() calls if delay is zero and i2c_master_set_DC_delay()
  49. // is not being called. This is not exact value, but proportional with length of code.
  50. // Increasing the value results in less delay and faster i2c clock speed.
  51. #define CPU_CYCLES_BETWEEN_DELAYS 80
  52. // CPU_CYCLES_GPIO16 is added to CPU_CYCLES_BETWEEN_DELAYS,
  53. // as RTC-related IO takes much more time than standard GPIOs.
  54. // Increasing the value results in less delay and faster i2c clock speed for GPIO16.
  55. #define CPU_CYCLES_GPIO16 90
  56. #else
  57. // If GPIO16 support is not enabled, remove GPIO16-related code during compile
  58. // and change timing constants.
  59. #define IS_PIN16(n) (0)
  60. #define CPU_CYCLES_BETWEEN_DELAYS 74
  61. #endif //I2C_MASTER_GPIO16_ENABLE
  62. #define MIN_SPEED 25000
  63. #define MAX_NUMBER_OF_I2C NUM_I2C
  64. typedef struct {
  65. uint8 last_SDA;
  66. uint8 last_SCL;
  67. uint8 pin_SDA;
  68. uint8 pin_SCL;
  69. uint32 pin_SDA_SCL_mask;
  70. uint32 pin_SDA_mask;
  71. uint32 pin_SCL_mask;
  72. uint32 speed;
  73. sint16 cycles_delay;
  74. } i2c_master_state_t;
  75. static i2c_master_state_t *i2c[MAX_NUMBER_OF_I2C];
  76. /******************************************************************************
  77. * FunctionName : i2c_master_set_DC_delay
  78. * Description : Internal used function - calculate delay for i2c_master_setDC
  79. * Parameters : bus id
  80. * Returns : NONE
  81. *******************************************************************************/
  82. LOCAL void ICACHE_FLASH_ATTR
  83. i2c_master_set_DC_delay(uint16 id)
  84. {
  85. // [cpu cycles per half SCL clock period] - [cpu cycles that code takes to run]
  86. i2c[id]->cycles_delay = system_get_cpu_freq() * 500000 / i2c[id]->speed - CPU_CYCLES_BETWEEN_DELAYS;
  87. #ifdef I2C_MASTER_GPIO16_ENABLE
  88. if(IS_PIN16(i2c[id]->pin_SCL)){ //if GPIO16
  89. i2c[id]->cycles_delay -= CPU_CYCLES_GPIO16; //decrease delay
  90. }
  91. #endif //I2C_MASTER_GPIO16_ENABLE
  92. if(i2c[id]->cycles_delay < 0){
  93. i2c[id]->cycles_delay = 0;
  94. }
  95. }
  96. /******************************************************************************
  97. * FunctionName : i2c_master_wait_cpu_cycles
  98. * Description : Internal used function - wait for given count of cpu cycles
  99. * Parameters : sint16 cycles_delay
  100. * Returns : NONE
  101. *******************************************************************************/
  102. static inline void i2c_master_wait_cpu_cycles(sint16 cycles_delay)
  103. {
  104. uint32 cycles_start;
  105. uint32 cycles_curr;
  106. // uses special 'ccount' register which is increased every CPU cycle
  107. // to make precise delay
  108. asm volatile("rsr %0, ccount":"=a"(cycles_start));
  109. do{
  110. asm volatile("rsr %0, ccount":"=a"(cycles_curr));
  111. } while (cycles_curr - cycles_start < cycles_delay);
  112. }
  113. /******************************************************************************
  114. * FunctionName : i2c_master_wait_gpio_SCL_high
  115. * Description : Internal used function - wait until SCL line in a high state
  116. (slave device may hold SCL line low until it is ready to proceed)
  117. * Parameters : bus id
  118. * Returns : NONE
  119. *******************************************************************************/
  120. static inline void i2c_master_wait_gpio_SCL_high(uint16 id)
  121. {
  122. // retrieves bitmask of all GPIOs from memory-mapped gpio register and exits if SCL bit is set
  123. // equivalent, but slow variant:
  124. // while(!(READ_PERI_REG(PERIPHS_GPIO_BASEADDR + GPIO_IN_ADDRESS) & i2c[id]->pin_SCL_mask)) {};
  125. // even slower: while (!(gpio_input_get() & i2c[id]->pin_SCL_mask)) {};
  126. asm volatile("l_wait:"
  127. "l16ui %0, %[gpio_in_addr], 0;" //read gpio state into register %0
  128. "memw;" //wait for read completion
  129. "bnall %0, %[gpio_SCL_mask], l_wait;" // test if SCL bit not set
  130. ::[gpio_SCL_mask] "r" (i2c[id]->pin_SCL_mask),
  131. [gpio_in_addr] "r" (PERIPHS_GPIO_BASEADDR + GPIO_IN_ADDRESS)
  132. );
  133. }
  134. /******************************************************************************
  135. * FunctionName : i2c_master_setDC
  136. * Description : Internal used function -
  137. * set i2c SDA and SCL bit value for half clock cycle
  138. * Parameters : bus id, uint8 SDA, uint8 SCL
  139. * Returns : NONE
  140. *******************************************************************************/
  141. LOCAL void ICACHE_FLASH_ATTR
  142. i2c_master_setDC(uint16 id, uint8 SDA, uint8 SCL)
  143. {
  144. uint32 this_SDA_SCL_set_mask;
  145. uint32 this_SDA_SCL_clear_mask;
  146. i2c[id]->last_SDA = SDA;
  147. i2c[id]->last_SCL = SCL;
  148. if(i2c[id]->cycles_delay > 0){
  149. i2c_master_wait_cpu_cycles(i2c[id]->cycles_delay);
  150. }
  151. if (IS_PIN16(i2c[id]->pin_SCL)){ //GPIO16 wired differently, it has it's own register address
  152. WRITE_PERI_REG(RTC_GPIO_OUT, SCL); // write SCL value
  153. if(1 == SDA){
  154. GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, i2c[id]->pin_SDA_mask); //SDA = 1
  155. }else{
  156. GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, i2c[id]->pin_SDA_mask); // SDA = 0
  157. }
  158. if(1 == SCL){ //clock stretching, GPIO16 version
  159. while(!(READ_PERI_REG(RTC_GPIO_IN_DATA) & 1)) {}; //read SCL value until SCL goes high
  160. }else{
  161. // dummy read operation and empty CPU cycles to maintain equal times for low and high state
  162. READ_PERI_REG(RTC_GPIO_IN_DATA) & 1; asm volatile("nop;nop;nop;nop;");
  163. }
  164. }
  165. else{
  166. this_SDA_SCL_set_mask = (SDA << i2c[id]->pin_SDA) | (SCL << i2c[id]->pin_SCL);
  167. this_SDA_SCL_clear_mask = i2c[id]->pin_SDA_SCL_mask ^ this_SDA_SCL_set_mask;
  168. GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, this_SDA_SCL_clear_mask);
  169. GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, this_SDA_SCL_set_mask);
  170. if(1 == SCL) { //clock stretching
  171. i2c_master_wait_gpio_SCL_high(id);
  172. }else{
  173. asm volatile("nop;nop;nop;"); // empty CPU cycles to maintain equal times for low and high state
  174. }
  175. }
  176. }
  177. /******************************************************************************
  178. * FunctionName : i2c_master_getDC
  179. * Description : Internal used function -
  180. * get i2c SDA bit value
  181. * Parameters : bus id
  182. * Returns : uint8 - SDA bit value
  183. *******************************************************************************/
  184. static inline uint8 ICACHE_FLASH_ATTR
  185. i2c_master_getDC(uint16 id)
  186. {
  187. return (READ_PERI_REG(PERIPHS_GPIO_BASEADDR + GPIO_IN_ADDRESS) >> i2c[id]->pin_SDA) & 1;
  188. }
  189. /******************************************************************************
  190. * FunctionName : i2c_master_configured
  191. * Description : checks if i2c bus is configured
  192. * Parameters : bus id
  193. * Returns : boolean value, true if configured
  194. *******************************************************************************/
  195. bool ICACHE_FLASH_ATTR
  196. i2c_master_configured(uint16 id){
  197. return !(NULL == i2c[id]);
  198. }
  199. /******************************************************************************
  200. * FunctionName : i2c_master_init
  201. * Description : initialize I2C bus to enable i2c operations
  202. (reset state of all slave devices)
  203. * Parameters : bus id
  204. * Returns : NONE
  205. *******************************************************************************/
  206. void ICACHE_FLASH_ATTR
  207. i2c_master_init(uint16 id)
  208. {
  209. uint8 i;
  210. i2c_master_setDC(id, 1, 0);
  211. // when SCL = 0, toggle SDA to clear up
  212. i2c_master_setDC(id, 0, 0) ;
  213. i2c_master_setDC(id, 1, 0) ;
  214. // set data_cnt to max value
  215. for (i = 0; i < 28; i++) {
  216. i2c_master_setDC(id, 1, 0);
  217. i2c_master_setDC(id, 1, 1);
  218. }
  219. // reset all
  220. i2c_master_stop(id);
  221. return;
  222. }
  223. /******************************************************************************
  224. * FunctionName : i2c_master_setup
  225. * Description : Initializes and configures the driver on given bus ID
  226. * Parameters : bus id
  227. * Returns : configured speed
  228. *******************************************************************************/
  229. uint32 ICACHE_FLASH_ATTR
  230. i2c_master_setup(uint16 id, uint8 sda, uint8 scl, uint32 speed)
  231. {
  232. if(NULL == i2c[id]){
  233. i2c[id] = (i2c_master_state_t*) malloc(sizeof(i2c_master_state_t));
  234. }
  235. if(NULL == i2c[id]){ // if malloc failed
  236. return 0;
  237. }
  238. i2c[id]->last_SDA = 1; //default idle state
  239. i2c[id]->last_SCL = 1;
  240. i2c[id]->pin_SDA = pin_num[sda];
  241. i2c[id]->pin_SCL = pin_num[scl];
  242. i2c[id]->pin_SDA_mask = 1 << i2c[id]->pin_SDA;
  243. i2c[id]->pin_SCL_mask = 1 << i2c[id]->pin_SCL;
  244. i2c[id]->pin_SDA_SCL_mask = i2c[id]->pin_SDA_mask | i2c[id]->pin_SCL_mask;
  245. i2c[id]->speed = speed;
  246. i2c[id]->cycles_delay = 0;
  247. if(i2c[id]->speed < MIN_SPEED){
  248. i2c[id]->speed = MIN_SPEED;
  249. }
  250. i2c_master_set_DC_delay(id); // recalibrate clock
  251. ETS_GPIO_INTR_DISABLE(); //disable gpio interrupts
  252. if (IS_PIN16(i2c[id]->pin_SCL)){ //if GPIO16
  253. CLEAR_PERI_REG_MASK(PAD_XPD_DCDC_CONF, 0x43); //disable all functions for XPD_DCDC
  254. SET_PERI_REG_MASK(PAD_XPD_DCDC_CONF, 0x1); // select function RTC_GPIO0 for pin XPD_DCDC
  255. CLEAR_PERI_REG_MASK(RTC_GPIO_CONF, 0x1); //mux configuration for out enable
  256. SET_PERI_REG_MASK(RTC_GPIO_ENABLE, 0x1); //out enable
  257. SET_PERI_REG_MASK(RTC_GPIO_OUT, 0x1); // set SCL high
  258. }
  259. else{
  260. PIN_FUNC_SELECT(pin_mux[scl], pin_func[scl]);
  261. SET_PERI_REG_MASK(PERIPHS_GPIO_BASEADDR + GPIO_PIN_ADDR(GPIO_ID_PIN(i2c[id]->pin_SCL)),
  262. GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_ENABLE)); //open drain
  263. gpio_output_set(i2c[id]->pin_SCL_mask, 0, i2c[id]->pin_SCL_mask, 0); //enable and set high
  264. }
  265. PIN_FUNC_SELECT(pin_mux[sda], pin_func[sda]);
  266. SET_PERI_REG_MASK(PERIPHS_GPIO_BASEADDR + GPIO_PIN_ADDR(GPIO_ID_PIN(i2c[id]->pin_SDA)),
  267. GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_ENABLE)); //open drain
  268. gpio_output_set(i2c[id]->pin_SDA_mask, 0, i2c[id]->pin_SDA_mask, 0); //enable and set high
  269. ETS_GPIO_INTR_ENABLE(); //enable gpio interrupts
  270. if (! (gpio_input_get() ^ i2c[id]->pin_SCL_mask)){ //SCL is in low state, bus failure
  271. return 0;
  272. }
  273. i2c_master_init(id);
  274. if (! (gpio_input_get() ^ i2c[id]->pin_SDA_mask)){ //SDA is in low state, bus failure
  275. return 0;
  276. }
  277. return i2c[id]->speed;
  278. }
  279. /******************************************************************************
  280. * FunctionName : i2c_master_start
  281. * Description : set i2c to send state
  282. * Parameters : bus id
  283. * Returns : NONE
  284. *******************************************************************************/
  285. void ICACHE_FLASH_ATTR
  286. i2c_master_start(uint16 id)
  287. {
  288. i2c_master_set_DC_delay(id); // recalibrate clock
  289. i2c_master_setDC(id, 1, i2c[id]->last_SCL);
  290. i2c_master_setDC(id, 1, 1);
  291. i2c_master_setDC(id, 0, 1);
  292. }
  293. /******************************************************************************
  294. * FunctionName : i2c_master_stop
  295. * Description : set i2c to stop sending state
  296. * Parameters : bus id
  297. * Returns : NONE
  298. *******************************************************************************/
  299. void ICACHE_FLASH_ATTR
  300. i2c_master_stop(uint16 id)
  301. {
  302. i2c_master_setDC(id, 0, i2c[id]->last_SCL);
  303. i2c_master_setDC(id, 0, 1);
  304. i2c_master_setDC(id, 1, 1);
  305. }
  306. /******************************************************************************
  307. * FunctionName : i2c_master_readByte
  308. * Description : read Byte from i2c bus
  309. * Parameters : bus id
  310. * Returns : uint8 - readed value
  311. *******************************************************************************/
  312. uint8 ICACHE_FLASH_ATTR
  313. i2c_master_readByte(uint16 id, sint16 ack)
  314. {
  315. uint8 retVal = 0;
  316. uint8 k;
  317. sint8 i;
  318. //invert and clamp ACK to 0/1, because ACK == 1 for i2c means SDA in low state
  319. uint8 ackLevel = (ack ? 0 : 1);
  320. i2c_master_setDC(id, i2c[id]->last_SDA, 0);
  321. i2c_master_setDC(id, 1, 0);
  322. for (i = 7; i >= 0; i--) {
  323. i2c_master_setDC(id, 1, 1);
  324. k = i2c_master_getDC(id);
  325. i2c_master_setDC(id, 1, 0); // unnecessary in last iteration
  326. k <<= i;
  327. retVal |= k;
  328. }
  329. // set ACK
  330. i2c_master_setDC(id, ackLevel, 0);
  331. i2c_master_setDC(id, ackLevel, 1);
  332. i2c_master_setDC(id, 1, 0);
  333. return retVal;
  334. }
  335. /******************************************************************************
  336. * FunctionName : i2c_master_writeByte
  337. * Description : write wrdata value(one byte) into i2c
  338. * Parameters : bus id, uint8 wrdata - write value
  339. * Returns : NONE
  340. *******************************************************************************/
  341. uint8 ICACHE_FLASH_ATTR
  342. i2c_master_writeByte(uint16 id, uint8 wrdata)
  343. {
  344. uint8 dat;
  345. sint8 i;
  346. uint8 retVal;
  347. i2c_master_setDC(id, i2c[id]->last_SDA, 0);
  348. for (i = 7; i >= 0; i--) {
  349. dat = (wrdata >> i) & 1;
  350. i2c_master_setDC(id, dat, 0);
  351. i2c_master_setDC(id, dat, 1);
  352. }
  353. //get ACK
  354. i2c_master_setDC(id, 1, 0);
  355. i2c_master_setDC(id, 1, 1);
  356. retVal = i2c_master_getDC(id);
  357. i2c_master_setDC(id, 1, 0);
  358. return ! retVal;
  359. }
  360. #else // if defined I2C_MASTER_OLD_VERSION
  361. /******************************************************************************
  362. * OLD driver
  363. * Enabled when I2C_MASTER_OLD_VERSION is defined in user_config.h
  364. *******************************************************************************/
  365. #define I2C_MASTER_SDA_MUX (pin_mux[sda])
  366. #define I2C_MASTER_SCL_MUX (pin_mux[scl])
  367. #define I2C_MASTER_SDA_GPIO (pinSDA)
  368. #define I2C_MASTER_SCL_GPIO (pinSCL)
  369. #define I2C_MASTER_SDA_FUNC (pin_func[sda])
  370. #define I2C_MASTER_SCL_FUNC (pin_func[scl])
  371. #define I2C_MASTER_SDA_HIGH_SCL_HIGH() \
  372. gpio_output_set(1<<I2C_MASTER_SDA_GPIO | 1<<I2C_MASTER_SCL_GPIO, 0, 1<<I2C_MASTER_SDA_GPIO | 1<<I2C_MASTER_SCL_GPIO, 0)
  373. #define I2C_MASTER_SDA_HIGH_SCL_LOW() \
  374. gpio_output_set(1<<I2C_MASTER_SDA_GPIO, 1<<I2C_MASTER_SCL_GPIO, 1<<I2C_MASTER_SDA_GPIO | 1<<I2C_MASTER_SCL_GPIO, 0)
  375. #define I2C_MASTER_SDA_LOW_SCL_HIGH() \
  376. gpio_output_set(1<<I2C_MASTER_SCL_GPIO, 1<<I2C_MASTER_SDA_GPIO, 1<<I2C_MASTER_SDA_GPIO | 1<<I2C_MASTER_SCL_GPIO, 0)
  377. #define I2C_MASTER_SDA_LOW_SCL_LOW() \
  378. gpio_output_set(0, 1<<I2C_MASTER_SDA_GPIO | 1<<I2C_MASTER_SCL_GPIO, 1<<I2C_MASTER_SDA_GPIO | 1<<I2C_MASTER_SCL_GPIO, 0)
  379. #define i2c_master_wait os_delay_us
  380. #define I2C_MASTER_SPEED 100000
  381. #define I2C_MASTER_BUS_ID 0
  382. LOCAL uint8 m_nLastSDA;
  383. LOCAL uint8 m_nLastSCL;
  384. LOCAL uint8 pinSDA = 2;
  385. LOCAL uint8 pinSCL = 15;
  386. /******************************************************************************
  387. * FunctionName : i2c_master_setDC
  388. * Description : Internal used function -
  389. * set i2c SDA and SCL bit value for half clk cycle
  390. * Parameters : uint8 SDA, uint8 SCL
  391. * Returns : NONE
  392. *******************************************************************************/
  393. LOCAL void ICACHE_FLASH_ATTR
  394. i2c_master_setDC(uint8 SDA, uint8 SCL)
  395. {
  396. uint8 sclLevel;
  397. SDA &= 0x01;
  398. SCL &= 0x01;
  399. m_nLastSDA = SDA;
  400. m_nLastSCL = SCL;
  401. if ((0 == SDA) && (0 == SCL)) {
  402. I2C_MASTER_SDA_LOW_SCL_LOW();
  403. } else if ((0 == SDA) && (1 == SCL)) {
  404. I2C_MASTER_SDA_LOW_SCL_HIGH();
  405. } else if ((1 == SDA) && (0 == SCL)) {
  406. I2C_MASTER_SDA_HIGH_SCL_LOW();
  407. } else {
  408. I2C_MASTER_SDA_HIGH_SCL_HIGH();
  409. }
  410. if(1 == SCL) {
  411. do {
  412. sclLevel = GPIO_INPUT_GET(GPIO_ID_PIN(I2C_MASTER_SCL_GPIO));
  413. } while(sclLevel == 0);
  414. }
  415. i2c_master_wait(5);
  416. }
  417. /******************************************************************************
  418. * FunctionName : i2c_master_getDC
  419. * Description : Internal used function -
  420. * get i2c SDA bit value
  421. * Parameters : NONE
  422. * Returns : uint8 - SDA bit value
  423. *******************************************************************************/
  424. LOCAL uint8 ICACHE_FLASH_ATTR
  425. i2c_master_getDC()
  426. {
  427. uint8 sda_out;
  428. sda_out = GPIO_INPUT_GET(GPIO_ID_PIN(I2C_MASTER_SDA_GPIO));
  429. return sda_out;
  430. }
  431. /******************************************************************************
  432. * FunctionName : i2c_master_init
  433. * Description : initilize I2C bus to enable i2c operations
  434. * Parameters : bus id
  435. * Returns : NONE
  436. *******************************************************************************/
  437. void ICACHE_FLASH_ATTR
  438. i2c_master_init(uint16 id)
  439. {
  440. uint8 i;
  441. i2c_master_setDC(1, 0);
  442. // when SCL = 0, toggle SDA to clear up
  443. i2c_master_setDC(0, 0) ;
  444. i2c_master_setDC(1, 0) ;
  445. // set data_cnt to max value
  446. for (i = 0; i < 28; i++) {
  447. i2c_master_setDC(1, 0);
  448. i2c_master_setDC(1, 1);
  449. }
  450. // reset all
  451. i2c_master_stop(I2C_MASTER_BUS_ID);
  452. return;
  453. }
  454. /******************************************************************************
  455. * FunctionName : i2c_master_configured
  456. * Description : checks if i2c bus is configured
  457. * Parameters : bus id
  458. * Returns : boolean value, true if configured
  459. *******************************************************************************/
  460. bool ICACHE_FLASH_ATTR
  461. i2c_master_configured(uint16 id)
  462. {
  463. return true;
  464. }
  465. /******************************************************************************
  466. * FunctionName : i2c_master_setup
  467. * Description : config SDA and SCL gpio to open-drain output mode,
  468. * mux and gpio num defined in i2c_master.h
  469. * Parameters : bus id, uint8 sda, uint8 scl, uint32 speed
  470. * Returns : configured speed
  471. *******************************************************************************/
  472. uint32 ICACHE_FLASH_ATTR
  473. i2c_master_setup(uint16 id, uint8 sda, uint8 scl, uint32 speed)
  474. {
  475. pinSDA = pin_num[sda];
  476. pinSCL = pin_num[scl];
  477. ETS_GPIO_INTR_DISABLE() ;
  478. // ETS_INTR_LOCK();
  479. PIN_FUNC_SELECT(I2C_MASTER_SDA_MUX, I2C_MASTER_SDA_FUNC);
  480. PIN_FUNC_SELECT(I2C_MASTER_SCL_MUX, I2C_MASTER_SCL_FUNC);
  481. 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;
  482. GPIO_REG_WRITE(GPIO_ENABLE_ADDRESS, GPIO_REG_READ(GPIO_ENABLE_ADDRESS) | (1 << I2C_MASTER_SDA_GPIO));
  483. 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;
  484. GPIO_REG_WRITE(GPIO_ENABLE_ADDRESS, GPIO_REG_READ(GPIO_ENABLE_ADDRESS) | (1 << I2C_MASTER_SCL_GPIO));
  485. I2C_MASTER_SDA_HIGH_SCL_HIGH();
  486. ETS_GPIO_INTR_ENABLE() ;
  487. // ETS_INTR_UNLOCK();
  488. i2c_master_init(I2C_MASTER_BUS_ID);
  489. return I2C_MASTER_SPEED;
  490. }
  491. /******************************************************************************
  492. * FunctionName : i2c_master_start
  493. * Description : set i2c to send state
  494. * Parameters : bus id
  495. * Returns : NONE
  496. *******************************************************************************/
  497. void ICACHE_FLASH_ATTR
  498. i2c_master_start(uint16 id)
  499. {
  500. i2c_master_setDC(1, m_nLastSCL);
  501. i2c_master_setDC(1, 1);
  502. i2c_master_setDC(0, 1);
  503. }
  504. /******************************************************************************
  505. * FunctionName : i2c_master_stop
  506. * Description : set i2c to stop sending state
  507. * Parameters : bus id
  508. * Returns : NONE
  509. *******************************************************************************/
  510. void ICACHE_FLASH_ATTR
  511. i2c_master_stop(uint16 id)
  512. {
  513. i2c_master_wait(5);
  514. i2c_master_setDC(0, m_nLastSCL);
  515. i2c_master_setDC(0, 1);
  516. i2c_master_setDC(1, 1);
  517. }
  518. /******************************************************************************
  519. * FunctionName : i2c_master_readByte
  520. * Description : read Byte from i2c bus
  521. * Parameters : bus id
  522. * Returns : uint8 - readed value
  523. *******************************************************************************/
  524. uint8 ICACHE_FLASH_ATTR
  525. i2c_master_readByte(uint16 id, sint16 ack)
  526. {
  527. uint8 retVal = 0;
  528. uint8 k, i;
  529. uint8 ackLevel = (ack ? 0 : 1);
  530. i2c_master_wait(5);
  531. i2c_master_setDC(m_nLastSDA, 0);
  532. for (i = 0; i < 8; i++) {
  533. i2c_master_wait(5);
  534. i2c_master_setDC(1, 0);
  535. i2c_master_setDC(1, 1);
  536. k = i2c_master_getDC();
  537. i2c_master_wait(5);
  538. if (i == 7) {
  539. i2c_master_wait(3); ////
  540. }
  541. k <<= (7 - i);
  542. retVal |= k;
  543. }
  544. i2c_master_setDC(1, 0);
  545. // set ACK
  546. i2c_master_setDC(m_nLastSDA, 0);
  547. i2c_master_setDC(ackLevel, 0);
  548. i2c_master_setDC(ackLevel, 1);
  549. i2c_master_wait(3);
  550. i2c_master_setDC(ackLevel, 0);
  551. i2c_master_setDC(1, 0);
  552. return retVal;
  553. }
  554. /******************************************************************************
  555. * FunctionName : i2c_master_writeByte
  556. * Description : write wrdata value(one byte) into i2c
  557. * Parameters : bus id, uint8 wrdata - write value
  558. * Returns : NONE
  559. *******************************************************************************/
  560. uint8 ICACHE_FLASH_ATTR
  561. i2c_master_writeByte(uint16 id, uint8 wrdata)
  562. {
  563. uint8 dat;
  564. sint8 i;
  565. uint8 retVal;
  566. i2c_master_wait(5);
  567. i2c_master_setDC(m_nLastSDA, 0);
  568. for (i = 7; i >= 0; i--) {
  569. dat = wrdata >> i;
  570. i2c_master_setDC(dat, 0);
  571. i2c_master_setDC(dat, 1);
  572. if (i == 0) {
  573. i2c_master_wait(3); ////
  574. }
  575. i2c_master_setDC(dat, 0);
  576. }
  577. // get ACK
  578. i2c_master_setDC(m_nLastSDA, 0);
  579. i2c_master_setDC(1, 0);
  580. i2c_master_setDC(1, 1);
  581. retVal = i2c_master_getDC();
  582. i2c_master_wait(5);
  583. i2c_master_setDC(1, 0);
  584. return ! retVal;
  585. }
  586. #endif