HW_I2C_ATY.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /**
  2. * @file HW_I2C_ATY.c
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2023 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief Familiar functions of I2C for all embedded device
  20. *
  21. * @version
  22. * - 1_01_220602 > ATY
  23. * -# Preliminary version, first Release
  24. * - 1_02_220804 > ATY
  25. * -# Change delay flow
  26. * -# Change SDA set function
  27. * -# Add Read/Write function
  28. * -Undone: hardware not test
  29. ********************************************************************************
  30. */
  31. #ifndef __HW_I2C_ATY_C
  32. #define __HW_I2C_ATY_C
  33. #include "HW_I2C_ATY.h"
  34. /******************************* For user *************************************/
  35. void OLED_Delay(uint8_t t)
  36. {
  37. while(t--);
  38. }
  39. /******************************************************************************/
  40. #ifndef __I2C_HARDWARE_ATY
  41. // 1: Out, 0: In, config default out
  42. uint8_t i2c_SdaDir = 1;
  43. /**
  44. * @brief Set SDA output
  45. */
  46. void I2C_SDA_SetOut(void)
  47. {
  48. if(i2c_SdaDir == 0)
  49. {
  50. i2c_SdaDir = 1;
  51. GPIO_SET_OUT(I2C_SDA_PORT, I2C_SDA_PIN);
  52. }
  53. }
  54. /**
  55. * @brief Set SDA input
  56. */
  57. void I2C_SDA_SetIn(void)
  58. {
  59. if(i2c_SdaDir == 1)
  60. {
  61. i2c_SdaDir = 0;
  62. GPIO_SET_IN(I2C_SDA_PORT, I2C_SDA_PIN);
  63. }
  64. }
  65. /**
  66. * @brief Star I2C bus
  67. * @note START:when CLK is high,DATA change form high to low
  68. */
  69. void I2C_Start(void)
  70. {
  71. I2C_SDA_SET_OUT;
  72. OLED_Delay(SHORT_DELAY);
  73. I2C_SDA_SET_H;
  74. I2C_SCL_SET_H;
  75. OLED_Delay(SHORT_DELAY);
  76. I2C_SDA_SET_L;
  77. OLED_Delay(SHORT_DELAY);
  78. I2C_SCL_SET_L;
  79. }
  80. /**
  81. * @brief Stop I2C bus
  82. * @note STOP:when CLK is high DATA change form low to high
  83. */
  84. void I2C_Stop(void)
  85. {
  86. I2C_SDA_SET_OUT;
  87. OLED_Delay(SHORT_DELAY);
  88. I2C_SCL_SET_L;
  89. I2C_SDA_SET_L;
  90. OLED_Delay(SHORT_DELAY);
  91. I2C_SCL_SET_H;
  92. OLED_Delay(SHORT_DELAY);
  93. I2C_SDA_SET_H;
  94. }
  95. /**
  96. * @brief I2C ack
  97. */
  98. void I2C_Ack(void)
  99. {
  100. I2C_SDA_SET_OUT;
  101. OLED_Delay(SHORT_DELAY);
  102. I2C_SCL_SET_L;
  103. I2C_SDA_SET_L;
  104. OLED_Delay(SHORT_DELAY);
  105. I2C_SCL_SET_H;
  106. OLED_Delay(SHORT_DELAY);
  107. I2C_SCL_SET_L;
  108. }
  109. /**
  110. * @brief I2C no ack
  111. */
  112. void I2C_NoAck(void)
  113. {
  114. I2C_SDA_SET_OUT;
  115. OLED_Delay(SHORT_DELAY);
  116. I2C_SCL_SET_L;
  117. I2C_SDA_SET_H;
  118. OLED_Delay(SHORT_DELAY);
  119. I2C_SCL_SET_H;
  120. OLED_Delay(SHORT_DELAY);
  121. I2C_SCL_SET_L;
  122. }
  123. /**
  124. * @brief Wait ack coming
  125. * @param maxErrTime max time to wait until fail
  126. * @return 0 success, !0: fail
  127. */
  128. uint8_t I2C_WaitAck(uint16_t maxErrTime)
  129. {
  130. uint16_t errTime = 0;
  131. I2C_SDA_SET_IN;
  132. OLED_Delay(SHORT_DELAY);
  133. I2C_SCL_SET_H;
  134. OLED_Delay(SHORT_DELAY);
  135. while(I2C_SDA_GET_H)
  136. {
  137. errTime++;
  138. OLED_Delay(2);
  139. if(errTime > maxErrTime)
  140. {
  141. I2C_Stop();
  142. return 1;
  143. }
  144. }
  145. I2C_SCL_SET_L;
  146. return 0;
  147. }
  148. /**
  149. * @brief I2C send one byte
  150. * @param byte data to send
  151. */
  152. void I2C_WriteByte(uint8_t byte)
  153. {
  154. uint8_t isb_i;
  155. I2C_SDA_SET_OUT;
  156. OLED_Delay(LONG_DELAY);
  157. I2C_SCL_SET_L;
  158. for(isb_i = 0; isb_i < 8; isb_i++)
  159. {
  160. OLED_Delay(SHORT_DELAY);
  161. if((byte & 0x80) >> 7)
  162. I2C_SDA_SET_H;
  163. else
  164. I2C_SDA_SET_L;
  165. OLED_Delay(SHORT_DELAY);
  166. I2C_SCL_SET_H;
  167. OLED_Delay(SHORT_DELAY);
  168. I2C_SCL_SET_L;
  169. byte <<= 1;
  170. }
  171. OLED_Delay(SHORT_DELAY);
  172. I2C_SDA_SET_H;
  173. OLED_Delay(SHORT_DELAY);
  174. I2C_SCL_SET_H;
  175. // OLED_Delay(SHORT_DELAY);
  176. // I2C_SCL_SET_L;
  177. }
  178. /**
  179. * @brief I2C read one byte
  180. * @param ack if send ACK or not
  181. * @return data to read
  182. */
  183. uint8_t I2C_ReadByte(uint8_t ack)
  184. {
  185. uint8_t irb_i, byte = 0;
  186. I2C_SDA_SET_IN;
  187. OLED_Delay(LONG_DELAY);
  188. for(irb_i = 0; irb_i < 8; irb_i++)
  189. {
  190. I2C_SCL_SET_L;
  191. OLED_Delay(SHORT_DELAY);
  192. I2C_SCL_SET_H;
  193. OLED_Delay(SHORT_DELAY);
  194. byte <<= 1;
  195. if(I2C_SDA_GET_H) byte++;
  196. }
  197. I2C_SCL_SET_L;
  198. if(ack)
  199. I2C_Ack();
  200. else
  201. I2C_NoAck();
  202. return byte;
  203. }
  204. /**
  205. * @brief Write serial data to reg
  206. * @param addr slave machine address
  207. * @param data_t data to write(uint8)
  208. * @param len length of write data(uint8)
  209. * @return 0: success, !0: error
  210. */
  211. uint8_t I2C_Write(uint8_t addr, uint8_t* data_t, uint8_t len)
  212. {
  213. uint8_t i = 0;
  214. I2C_Start();
  215. I2C_WriteByte(addr << 1 | 0);
  216. if(I2C_WaitAck(I2C_WAIT_ACK_TIME))
  217. return 1;
  218. for(i = 0; i < len; i++)
  219. {
  220. I2C_WriteByte(data_t[i]);
  221. if(I2C_WaitAck(I2C_WAIT_ACK_TIME))
  222. return 1;
  223. }
  224. I2C_Stop();
  225. return 0;
  226. }
  227. /**
  228. * @brief Write serial data to reg
  229. * @param addr slave machine address
  230. * @param data_t data to write(uint8)
  231. * @param len length of write data(uint8)
  232. * @return 0: success, !0: error
  233. */
  234. uint8_t I2C_Write_NoStop(uint8_t addr, uint8_t* data_t, uint8_t len)
  235. {
  236. uint8_t i = 0;
  237. I2C_Start();
  238. I2C_WriteByte(addr << 1 | 0);
  239. if(I2C_WaitAck(I2C_WAIT_ACK_TIME))
  240. return 1;
  241. for(i = 0; i < len; i++)
  242. {
  243. I2C_WriteByte(data_t[i]);
  244. if(I2C_WaitAck(I2C_WAIT_ACK_TIME))
  245. return 1;
  246. }
  247. return 0;
  248. }
  249. /**
  250. * @brief Read serial data to reg
  251. * @param addr slave machine address
  252. * @param data_t data to read(uint8)
  253. * @param len length of read data(uint8)
  254. * @return 0: success, !0: error
  255. */
  256. uint8_t I2C_Read(uint8_t addr, uint8_t* data_t, uint8_t len)
  257. {
  258. I2C_Start();
  259. I2C_WriteByte(addr << 1 | 1);
  260. if(I2C_WaitAck(I2C_WAIT_ACK_TIME))
  261. return 1;
  262. while(len--)
  263. {
  264. if(len > 0)
  265. *data_t++ = I2C_ReadByte(1);
  266. else
  267. *data_t++ = I2C_ReadByte(0);
  268. }
  269. I2C_Stop();
  270. return 0;
  271. }
  272. /**
  273. * @brief Write serial data to reg
  274. * @param addr slave machine address
  275. * @param reg reg addr to write
  276. * @param data_t data to write(uint8)
  277. * @param len length of write data(uint8)
  278. * @return 0: success, !0: error
  279. */
  280. uint8_t I2C_WriteReg(uint8_t addr, uint8_t reg, uint8_t* data_t, uint8_t len)
  281. {
  282. uint8_t i = 0;
  283. I2C_Start();
  284. I2C_WriteByte(addr << 1 | 0);
  285. if(I2C_WaitAck(I2C_WAIT_ACK_TIME))
  286. {
  287. I2C_Stop();
  288. return 1;
  289. }
  290. if(reg != 0)
  291. {
  292. I2C_WriteByte(reg);
  293. I2C_WaitAck(I2C_WAIT_ACK_TIME);
  294. }
  295. for(i = 0; i < len; i++)
  296. {
  297. I2C_WriteByte(data_t[i]);
  298. if(I2C_WaitAck(I2C_WAIT_ACK_TIME))
  299. {
  300. I2C_Stop();
  301. return 1;
  302. }
  303. }
  304. I2C_Stop();
  305. return 0;
  306. }
  307. /**
  308. * @brief Read serial data to reg
  309. * @param addr slave machine address
  310. * @param reg reg addr to read
  311. * @param data_t data to read(uint8)
  312. * @param len length of read data(uint8)
  313. * @return 0: success, !0: error
  314. */
  315. uint8_t I2C_ReadReg(uint8_t addr, uint8_t reg, uint8_t* data_t, uint8_t len)
  316. {
  317. I2C_Start();
  318. I2C_WriteByte(addr << 1 | 0);
  319. if(I2C_WaitAck(I2C_WAIT_ACK_TIME))
  320. {
  321. I2C_Stop();
  322. return 1;
  323. }
  324. I2C_WriteByte(reg);
  325. I2C_WaitAck(I2C_WAIT_ACK_TIME);
  326. I2C_Stop();
  327. I2C_Start();
  328. I2C_WriteByte(addr << 1 | 1);
  329. I2C_WaitAck(I2C_WAIT_ACK_TIME);
  330. while(len)
  331. {
  332. if(len == 1)
  333. *data_t = I2C_ReadByte(0);
  334. else
  335. *data_t = I2C_ReadByte(1);
  336. len--;
  337. data_t++;
  338. }
  339. I2C_Stop();
  340. return 0;
  341. }
  342. #else
  343. #if defined(__STM32_HAL_ATY)
  344. #include "i2c.h"
  345. /**
  346. * @brief Eliminate HAL_I2C bug
  347. * @warning Put "__HAL_RCC_I2C1_CLK_ENABLE();" before GPIO init
  348. */
  349. //void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
  350. //{
  351. // GPIO_InitTypeDef GPIO_InitStruct = {0};
  352. // if(hi2c->Instance==I2C1)
  353. // {
  354. // /* USER CODE BEGIN I2C1_MspInit 0 */
  355. // __HAL_RCC_I2C1_CLK_ENABLE();
  356. // /* USER CODE END I2C1_MspInit 0 */
  357. //
  358. // __HAL_RCC_GPIOB_CLK_ENABLE();
  359. // /**I2C1 GPIO Configuration
  360. // PB6 ------> I2C1_SCL
  361. // PB7 ------> I2C1_SDA
  362. // */
  363. // GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
  364. // GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  365. // GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  366. // HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  367. //
  368. // /* Peripheral clock enable */
  369. // //__HAL_RCC_I2C1_CLK_ENABLE();
  370. // /* USER CODE BEGIN I2C1_MspInit 1 */
  371. //
  372. // /* USER CODE END I2C1_MspInit 1 */
  373. // }
  374. //
  375. //}
  376. /**
  377. * @brief Write serial data to reg
  378. * @param addr slave machine address
  379. * @param data_t data to write(uint8)
  380. * @param len length of write data(uint8)
  381. * @return 0: success, !0: error
  382. */
  383. uint8_t I2C_Write(uint8_t addr, uint8_t* data_t, uint8_t len)
  384. {
  385. uint8_t i = 3;
  386. while(i--)
  387. {
  388. if(HAL_I2C_Master_Transmit(&I2CH_N, addr << 1 | 0, data_t, len, 1000) == HAL_OK)
  389. return 0;
  390. }
  391. return 1;
  392. }
  393. /**
  394. * @brief Read serial data to reg
  395. * @param addr slave machine address
  396. * @param data_t data to read(uint8)
  397. * @param len length of read data(uint8)
  398. * @return 0: success, !0: error
  399. */
  400. uint8_t I2C_Read(uint8_t addr, uint8_t* data_t, uint8_t len)
  401. {
  402. uint8_t i = 3;
  403. while(i--)
  404. {
  405. if(HAL_I2C_Master_Receive(&I2CH_N, addr << 1 | 1, data_t, len, 1000) == HAL_OK)
  406. return 0;
  407. }
  408. return 1;
  409. }
  410. #elif defined(__ESP8266_RTOS_ATY)
  411. #include "driver/i2c.h"
  412. #define I2C_EXAMPLE_MASTER_SCL_IO 2 /*!< gpio number for I2C master clock */
  413. #define I2C_EXAMPLE_MASTER_SDA_IO 14 /*!< gpio number for I2C master data */
  414. #define I2C_EXAMPLE_MASTER_NUM I2C_NUM_0 /*!< I2C port number for master dev */
  415. #define I2C_EXAMPLE_MASTER_TX_BUF_DISABLE 0 /*!< I2C master do not need buffer */
  416. #define I2C_EXAMPLE_MASTER_RX_BUF_DISABLE 0 /*!< I2C master do not need buffer */
  417. /**
  418. * @brief i2c master initialization
  419. */
  420. static esp_err_t i2c_example_master_init()
  421. {
  422. int i2c_master_port = I2CH_N;
  423. i2c_config_t conf;
  424. conf.mode = I2C_MODE_MASTER;
  425. conf.sda_io_num = I2C_EXAMPLE_MASTER_SDA_IO;
  426. conf.sda_pullup_en = 1;
  427. conf.scl_io_num = I2C_EXAMPLE_MASTER_SCL_IO;
  428. conf.scl_pullup_en = 1;
  429. conf.clk_stretch_tick = 300; // 300 ticks, Clock stretch is about 210us, you can make changes according to the actual situation.
  430. ESP_ERROR_CHECK(i2c_driver_install(i2c_master_port, conf.mode));
  431. ESP_ERROR_CHECK(i2c_param_config(i2c_master_port, &conf));
  432. return ESP_OK;
  433. }
  434. /**
  435. * @brief Write serial data to reg
  436. * @param addr slave machine address
  437. * @param data_t data to write(uint8)
  438. * @param len length of write data(uint8)
  439. * @return 0: success, !0: error
  440. */
  441. uint8_t I2C_Write(uint8_t addr, uint8_t* data_t, uint8_t len)
  442. {
  443. int ret;
  444. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  445. i2c_master_start(cmd);
  446. i2c_master_write_byte(cmd, addr << 1 | 0, ACK_CHECK_EN);
  447. i2c_master_write(cmd, data_t, len, ACK_CHECK_EN);
  448. i2c_master_stop(cmd);
  449. ret = i2c_master_cmd_begin(I2CH_N, cmd, 1000 / portTICK_RATE_MS);
  450. i2c_cmd_link_delete(cmd);
  451. return ret;
  452. }
  453. /**
  454. * @brief Read serial data to reg
  455. * @param addr slave machine address
  456. * @param data_t data to read(uint8)
  457. * @param len length of read data(uint8)
  458. * @return 0: success, !0: error
  459. */
  460. uint8_t I2C_Read(uint8_t addr, uint8_t* data_t, uint8_t len)
  461. {
  462. int ret;
  463. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  464. i2c_master_start(cmd);
  465. i2c_master_write_byte(cmd, addr << 1 | 0, ACK_CHECK_EN);
  466. i2c_master_stop(cmd);
  467. ret = i2c_master_cmd_begin(I2CH_N, cmd, 1000 / portTICK_RATE_MS);
  468. i2c_cmd_link_delete(cmd);
  469. if(ret != ESP_OK) {
  470. return ret;
  471. }
  472. cmd = i2c_cmd_link_create();
  473. i2c_master_start(cmd);
  474. i2c_master_write_byte(cmd, addr << 1 | 1, ACK_CHECK_EN);
  475. i2c_master_read(cmd, data_t, len, LAST_NACK_VAL);
  476. i2c_master_stop(cmd);
  477. ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
  478. i2c_cmd_link_delete(cmd);
  479. return ret;
  480. }
  481. // void Wait()
  482. // {
  483. // while(!(I2CMSST & 0x40));
  484. // I2CMSST &= ~0x40;
  485. // }
  486. // void Start()
  487. // {
  488. // I2CMSCR = 0x01; //发送START命令
  489. // Wait();
  490. // }
  491. // void SendData(char dat)
  492. // {
  493. // I2CTXD = dat; //写数据到数据缓冲区
  494. // I2CMSCR = 0x02; //发送SEND命令
  495. // Wait();
  496. // }
  497. // void RecvACK()
  498. // {
  499. // I2CMSCR = 0x03; //发送读ACK命令
  500. // Wait();
  501. // }
  502. // char RecvData()
  503. // {
  504. // I2CMSCR = 0x04; //发送RECV命令
  505. // Wait();
  506. // return I2CRXD;
  507. // }
  508. // void SendACK()
  509. // {
  510. // I2CMSST = 0x00; //设置ACK信号
  511. // I2CMSCR = 0x05; //发送ACK命令
  512. // Wait();
  513. // }
  514. // void SendNAK()
  515. // {
  516. // I2CMSST = 0x01; //设置NAK信号
  517. // I2CMSCR = 0x05; //发送ACK命令
  518. // Wait();
  519. // }
  520. // void Stop()
  521. // {
  522. // I2CMSCR = 0x06; //发送STOP命令
  523. // Wait();
  524. // }
  525. // void Delay()
  526. // {
  527. // int i;
  528. // for(i = 0; i < 3000; i++)
  529. // {
  530. // _nop_();
  531. // _nop_();
  532. // _nop_();
  533. // _nop_();
  534. // }
  535. // }
  536. #endif /* PLATFORM */
  537. #endif /* __I2C_HARDWARE_ATY */
  538. #endif /* __HW_I2C_ATY_C */
  539. /******************************** End Of File *********************************/