drv_i2c.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. *
  3. * SPDX-License-Identifier: Apache-2.0
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the License); you may
  6. * not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. * Change Logs:
  18. * Date Author Notes
  19. * 2019-11-01 wangyq update libraries
  20. * 2020-01-14 wangyq the first version
  21. * 2021-04-20 liuhy the second version
  22. * 2022-07-11 shiwa Support for RT_NO_START/RT_NO_STOP
  23. */
  24. #include <rthw.h>
  25. #include <rtthread.h>
  26. #include <rtdevice.h>
  27. #include "board.h"
  28. #include "drv_i2c.h"
  29. #ifdef RT_USING_I2C
  30. #define TIMEOUT 0x0FFF
  31. /* I2C struct definition */
  32. #ifdef BSP_USING_I2C0
  33. static i2c_handle_t _h_i2c0;
  34. #endif
  35. #ifdef BSP_USING_I2C1
  36. static i2c_handle_t _h_i2c1;
  37. #endif
  38. static void _i2c_init(void)
  39. {
  40. gpio_init_t gpio_instruct;
  41. /* Initialize I2C Pin */
  42. gpio_instruct.mode = GPIO_MODE_OUTPUT;
  43. gpio_instruct.odos = GPIO_OPEN_DRAIN;
  44. gpio_instruct.pupd = GPIO_PUSH_UP;
  45. gpio_instruct.podrv = GPIO_OUT_DRIVE_6;
  46. gpio_instruct.nodrv = GPIO_OUT_DRIVE_6;
  47. gpio_instruct.flt = GPIO_FILTER_DISABLE;
  48. gpio_instruct.type = GPIO_TYPE_CMOS;
  49. #ifdef BSP_USING_I2C0
  50. #if defined(ES_I2C0_SCL_GPIO_FUNC)&&defined(ES_I2C0_SCL_GPIO_PORT)&&defined(ES_I2C0_SCL_GPIO_PIN)
  51. gpio_instruct.func = ES_I2C0_SCL_GPIO_FUNC;
  52. ald_gpio_init(ES_I2C0_SCL_GPIO_PORT, ES_I2C0_SCL_GPIO_PIN, &gpio_instruct);
  53. #endif
  54. #if defined(ES_I2C0_SDA_GPIO_FUNC)&&defined(ES_I2C0_SDA_GPIO_PORT)&&defined(ES_I2C0_SDA_GPIO_PIN)
  55. gpio_instruct.func = ES_I2C0_SDA_GPIO_FUNC;
  56. ald_gpio_init(ES_I2C0_SDA_GPIO_PORT, ES_I2C0_SDA_GPIO_PIN, &gpio_instruct);
  57. #endif
  58. /* Initialize I2C Function */
  59. _h_i2c0.perh = I2C0;
  60. _h_i2c0.init.module = I2C_MODULE_MASTER;
  61. _h_i2c0.init.clk_speed = ES_I2C0_CLK_SPEED;
  62. _h_i2c0.init.own_addr1 = ES_I2C0_OWN_ADDR1;
  63. _h_i2c0.init.addr_mode = ES_I2C0_ADDR_MODE;
  64. _h_i2c0.init.general_call = ES_I2C0_GENERAL_CALL;
  65. _h_i2c0.init.no_stretch = ES_I2C0_STRETCH;
  66. ald_i2c_reset(&_h_i2c0);
  67. ald_i2c_init(&_h_i2c0);
  68. #endif
  69. #ifdef BSP_USING_I2C1
  70. #if defined(ES_I2C1_SCL_GPIO_FUNC)&&defined(ES_I2C1_SCL_GPIO_PORT)&&defined(ES_I2C1_SCL_GPIO_PIN)
  71. gpio_instruct.func = ES_I2C1_SCL_GPIO_FUNC;
  72. ald_gpio_init(ES_I2C1_SCL_GPIO_PORT, ES_I2C1_SCL_GPIO_PIN, &gpio_instruct);
  73. #endif
  74. #if defined(ES_I2C1_SDA_GPIO_FUNC)&&defined(ES_I2C1_SDA_GPIO_PORT)&&defined(ES_I2C1_SDA_GPIO_PIN)
  75. gpio_instruct.func = ES_I2C1_SDA_GPIO_FUNC;
  76. ald_gpio_init(ES_I2C1_SDA_GPIO_PORT, ES_I2C1_SDA_GPIO_PIN, &gpio_instruct);
  77. #endif
  78. /* Initialize i2c function */
  79. _h_i2c1.perh = I2C1;
  80. _h_i2c1.init.module = I2C_MODULE_MASTER;
  81. _h_i2c1.init.clk_speed = ES_I2C1_CLK_SPEED;
  82. _h_i2c1.init.own_addr1 = ES_I2C1_OWN_ADDR1;
  83. _h_i2c1.init.addr_mode = ES_I2C1_ADDR_MODE;
  84. _h_i2c1.init.general_call = ES_I2C1_GENERAL_CALL;
  85. _h_i2c1.init.no_stretch = ES_I2C1_STRETCH;
  86. ald_i2c_reset(&_h_i2c1);
  87. ald_i2c_init(&_h_i2c1);
  88. #endif
  89. }
  90. #define _I2C_NO_START 0x1
  91. #define _I2C_NO_STOP 0x2
  92. int _i2c_master_req(i2c_handle_t *hperh, uint16_t dev_addr, uint32_t timeout,uint32_t req_write)
  93. {
  94. if (hperh->init.addr_mode == I2C_ADDR_7BIT) {
  95. CLEAR_BIT(hperh->perh->CON2, I2C_CON2_ADD10_MSK);
  96. }
  97. else {
  98. SET_BIT(hperh->perh->CON2, I2C_CON2_ADD10_MSK);
  99. }
  100. MODIFY_REG(hperh->perh->CON2, I2C_CON2_SADD_MSK, dev_addr << I2C_CON2_SADD_POSS);
  101. if (req_write)
  102. CLEAR_BIT(hperh->perh->CON2, I2C_CON2_RD_WRN_MSK);
  103. else
  104. SET_BIT(hperh->perh->CON2, I2C_CON2_RD_WRN_MSK);
  105. return OK;
  106. }
  107. int _i2c_wait_flag(i2c_handle_t *hperh, uint32_t flag, flag_status_t status, uint32_t timeout)
  108. {
  109. uint32_t tickstart = 0;
  110. tickstart = ald_get_tick();
  111. while (I2C_GET_FLAG(hperh, flag) == status) {
  112. if ((timeout == 0) || ((ald_get_tick() - tickstart ) > timeout)) {
  113. hperh->error_code |= I2C_ERROR_TIMEOUT;
  114. return TIMEOUT;
  115. }
  116. }
  117. return OK;
  118. }
  119. int _i2c_wait_txe(i2c_handle_t *hperh, uint32_t timeout)
  120. {
  121. uint32_t tickstart = ald_get_tick();
  122. while (I2C_GET_FLAG(hperh, I2C_STAT_THTH) == RESET) {
  123. if (I2C_GET_IT_FLAG(hperh, I2C_IT_ARLO)) {
  124. hperh->error_code |= I2C_ERROR_ARLO;
  125. return ERROR;
  126. }
  127. if (I2C_GET_IT_FLAG(hperh, I2C_IT_NACK) == SET) {
  128. hperh->error_code |= I2C_ERROR_AF;
  129. return ERROR;
  130. }
  131. if ((timeout == 0) || ((ald_get_tick() - tickstart) > timeout)) {
  132. hperh->error_code |= I2C_ERROR_TIMEOUT;
  133. return ERROR;
  134. }
  135. }
  136. return OK;
  137. }
  138. int _i2c_master_send(i2c_handle_t *hperh, uint16_t dev_addr, uint8_t *buf,
  139. uint32_t size, uint32_t timeout,uint32_t flag)
  140. {
  141. if (hperh->state != I2C_STATE_READY)
  142. return BUSY;
  143. if ((buf == NULL) || (size == 0))
  144. return ERROR;
  145. if ((flag&_I2C_NO_START)==0x0) //NOSTART==0
  146. {
  147. if (_i2c_wait_flag(hperh, I2C_STAT_BUSY, SET, 100) != OK)
  148. return BUSY;
  149. _i2c_master_req(hperh, dev_addr, timeout,1);
  150. }
  151. assert_param(IS_I2C_TYPE(hperh->perh));
  152. __LOCK(hperh);
  153. hperh->state = I2C_STATE_BUSY_TX;
  154. hperh->mode = I2C_MODE_MASTER;
  155. hperh->error_code = I2C_ERROR_NONE;
  156. hperh->p_buff = buf;
  157. hperh->xfer_size = size;
  158. hperh->xfer_count = 0;
  159. if ((flag&_I2C_NO_STOP)!=0) //NOSTOP==1
  160. SET_BIT(hperh->perh->CON2, I2C_CON2_RELOAD_MSK);
  161. else
  162. CLEAR_BIT(hperh->perh->CON2, I2C_CON2_RELOAD_MSK);
  163. if (size <= 0xFF) {
  164. MODIFY_REG(hperh->perh->CON2, I2C_CON2_NBYTES_MSK, size << I2C_CON2_NBYTES_POSS);
  165. }
  166. else {
  167. MODIFY_REG(hperh->perh->CON2, I2C_CON2_NBYTES_MSK, 0xFF << I2C_CON2_NBYTES_POSS);
  168. SET_BIT(hperh->perh->CON2, I2C_CON2_RELOAD_MSK);
  169. }
  170. SET_BIT(hperh->perh->FCON, I2C_FCON_TXFRST_MSK);
  171. if ((flag&_I2C_NO_START)==0x0) //NOSTART=0
  172. SET_BIT(hperh->perh->CON2, I2C_CON2_START_MSK);
  173. while (size > 0) {
  174. hperh->perh->TXDATA = (*buf++);
  175. size--;
  176. hperh->xfer_count++;
  177. if (_i2c_wait_txe(hperh, timeout) != OK)
  178. goto ERROR;
  179. if (((hperh->xfer_count % 0xFF) == 0) && (READ_BIT(hperh->perh->CON2, I2C_CON2_RELOAD_MSK))) {
  180. if (_i2c_wait_flag(hperh, I2C_STAT_TCR, RESET, 10) == OK) {
  181. if (size > 0xFF) {
  182. MODIFY_REG(hperh->perh->CON2, I2C_CON2_NBYTES_MSK, 0xFF << I2C_CON2_NBYTES_POSS);
  183. }
  184. else {
  185. MODIFY_REG(hperh->perh->CON2, I2C_CON2_NBYTES_MSK, size << I2C_CON2_NBYTES_POSS);
  186. if ((flag&_I2C_NO_STOP)==0)
  187. CLEAR_BIT(hperh->perh->CON2, I2C_CON2_RELOAD_MSK);
  188. }
  189. }
  190. else {
  191. goto ERROR;
  192. }
  193. }
  194. }
  195. if (READ_BIT(hperh->perh->CON2, I2C_CON2_AUTOEND_MSK) == SET)
  196. goto SUCCESS;
  197. //NOSTOP==1
  198. if ((flag&_I2C_NO_STOP)!=0&&_i2c_wait_flag(hperh, I2C_STAT_TCR, RESET, 10) == OK)
  199. {
  200. goto SUCCESS;
  201. }
  202. if (_i2c_wait_flag(hperh, I2C_STAT_TC, RESET, 10) == OK) {
  203. if ((flag&_I2C_NO_STOP)==0x0) //NOSTOP==0
  204. SET_BIT(hperh->perh->CON2, I2C_CON2_STOP_MSK);
  205. goto SUCCESS;
  206. }
  207. else {
  208. goto ERROR;
  209. }
  210. ERROR:
  211. SET_BIT(hperh->perh->CON2, I2C_CON2_STOP_MSK);
  212. hperh->state = I2C_STATE_READY;
  213. hperh->mode = I2C_MODE_NONE;
  214. __UNLOCK(hperh);
  215. return ERROR;
  216. SUCCESS:
  217. hperh->state = I2C_STATE_READY;
  218. hperh->mode = I2C_MODE_NONE;
  219. __UNLOCK(hperh);
  220. return OK;
  221. }
  222. static rt_ssize_t es32f3_master_xfer(struct rt_i2c_bus_device *bus,
  223. struct rt_i2c_msg msgs[],
  224. rt_uint32_t num)
  225. {
  226. struct rt_i2c_msg *msg;
  227. rt_uint32_t i;
  228. rt_err_t ret = -RT_ERROR;
  229. for (i = 0; i < num; i++)
  230. {
  231. msg = &msgs[i];
  232. if (msg->buf==NULL||msg->len==0)
  233. {
  234. continue;
  235. }
  236. if (msg->flags & RT_I2C_RD)
  237. {
  238. if (ald_i2c_master_recv(bus->priv, msg->addr << 1, msg->buf, msg->len, TIMEOUT) != 0)
  239. {
  240. LOG_E("i2c bus write failed,i2c bus stop!\n");
  241. goto out;
  242. }
  243. }
  244. else
  245. {
  246. uint32_t f=((msg->flags&RT_I2C_NO_START)?0x1:0)|((msg->flags&RT_I2C_NO_STOP)?0x2:0);
  247. if (I2C_GET_FLAG((i2c_handle_t *)bus->priv, I2C_STAT_BUSY) == RESET)
  248. {
  249. f=f&(~_I2C_NO_START);
  250. }
  251. if (_i2c_master_send(bus->priv, msg->addr << 1, msg->buf, msg->len, TIMEOUT,f) != 0)
  252. {
  253. LOG_E("i2c bus write failed,i2c bus stop!\n");
  254. goto out;
  255. }
  256. }
  257. }
  258. ret = i;
  259. out:
  260. LOG_D("send stop condition\n");
  261. return ret;
  262. }
  263. const struct rt_i2c_bus_device_ops es32f3_i2c_ops =
  264. {
  265. es32f3_master_xfer,
  266. RT_NULL,
  267. RT_NULL,
  268. };
  269. int rt_hw_i2c_init(void)
  270. {
  271. int result = RT_EOK;
  272. _i2c_init();
  273. #ifdef BSP_USING_I2C0
  274. /* define i2c Instance */
  275. static struct rt_i2c_bus_device _i2c_device0;
  276. rt_memset((void *)&_i2c_device0, 0, sizeof(struct rt_i2c_bus_device));
  277. _i2c_device0.ops = &es32f3_i2c_ops;
  278. _i2c_device0.priv = &_h_i2c0;
  279. result = rt_i2c_bus_device_register(&_i2c_device0, ES_DEVICE_NAME_I2C0);
  280. if (result != RT_EOK)
  281. {
  282. return result;
  283. }
  284. #endif
  285. #ifdef BSP_USING_I2C1
  286. /* define i2c Instance */
  287. static struct rt_i2c_bus_device _i2c_device1;
  288. rt_memset((void *)&_i2c_device1, 0, sizeof(struct rt_i2c_bus_device));
  289. _i2c_device1.ops = &es32f3_i2c_ops;
  290. _i2c_device1.priv = &_h_i2c1;
  291. rt_i2c_bus_device_register(&_i2c_device1, ES_DEVICE_NAME_I2C1);
  292. if (result != RT_EOK)
  293. {
  294. return result;
  295. }
  296. #endif
  297. return RT_EOK;
  298. }
  299. INIT_DEVICE_EXPORT(rt_hw_i2c_init);
  300. #endif