drv_i2c.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-08-21 heyuanjie87 first version
  9. * 2023-03-31 Vandoul formatting code.
  10. */
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include "i2c.h"
  14. #include "board.h"
  15. #include "drivers/dev_i2c.h"
  16. #include "gpiohs.h"
  17. #include "utils.h"
  18. #include "sleep.h"
  19. #include "fpioa.h"
  20. #ifdef RT_USING_I2C
  21. #ifndef BSP_I2C0_SCL_PIN
  22. #define BSP_I2C0_SCL_PIN 0
  23. #endif
  24. #ifndef BSP_I2C0_SDA_PIN
  25. #define BSP_I2C0_SDA_PIN 1
  26. #endif
  27. #ifndef BSP_I2C1_SCL_PIN
  28. #define BSP_I2C1_SCL_PIN 30
  29. #endif
  30. #ifndef BSP_I2C1_SDA_PIN
  31. #define BSP_I2C1_SDA_PIN 31
  32. #endif
  33. #ifndef BSP_I2C2_SCL_PIN
  34. #define BSP_I2C2_SCL_PIN 4
  35. #endif
  36. #ifndef BSP_I2C2_SDA_PIN
  37. #define BSP_I2C2_SDA_PIN 5
  38. #endif
  39. static rt_err_t ki2c_send(
  40. volatile i2c_t *i2c_adapter,
  41. rt_uint8_t *send_buf,
  42. rt_uint32_t send_buf_len)
  43. {
  44. rt_uint32_t fifo_len, index;
  45. while (send_buf_len)
  46. {
  47. fifo_len = 8 - i2c_adapter->txflr;
  48. fifo_len = send_buf_len < fifo_len ? send_buf_len : fifo_len;
  49. for (index = 0; index < fifo_len; index++)
  50. i2c_adapter->data_cmd = I2C_DATA_CMD_DATA(*send_buf++);
  51. if (i2c_adapter->tx_abrt_source != 0)
  52. {
  53. while (i2c_adapter->status & I2C_STATUS_ACTIVITY); //
  54. i2c_adapter->clr_intr = i2c_adapter->clr_intr; //
  55. return -RT_ERROR;
  56. }
  57. send_buf_len -= fifo_len;
  58. }
  59. return RT_EOK;
  60. }
  61. static rt_err_t ki2c_recv(
  62. volatile i2c_t *i2c_adapter,
  63. rt_uint8_t *receive_buf,
  64. rt_uint32_t receive_buf_len)
  65. {
  66. rt_uint32_t fifo_len, index;
  67. rt_uint32_t rx_len = receive_buf_len;
  68. while (receive_buf_len || rx_len)
  69. {
  70. fifo_len = i2c_adapter->rxflr;
  71. fifo_len = rx_len < fifo_len ? rx_len : fifo_len;
  72. for (index = 0; index < fifo_len; index++)
  73. *receive_buf++ = (rt_uint8_t)i2c_adapter->data_cmd;
  74. rx_len -= fifo_len;
  75. fifo_len = 8 - i2c_adapter->txflr;
  76. fifo_len = receive_buf_len < fifo_len ? receive_buf_len : fifo_len;
  77. for (index = 0; index < fifo_len; index++)
  78. i2c_adapter->data_cmd = I2C_DATA_CMD_CMD;
  79. if (i2c_adapter->tx_abrt_source != 0)
  80. return -RT_ERROR;
  81. receive_buf_len -= fifo_len;
  82. }
  83. return RT_EOK;
  84. }
  85. static void ki2c_setaddr(
  86. volatile i2c_t *i2c_adapter,
  87. rt_uint16_t addr,
  88. int width)
  89. {
  90. i2c_adapter->tar = I2C_TAR_ADDRESS(addr) & I2C_TAR_ADDRESS_MASK;
  91. if(width == 10)
  92. {
  93. i2c_adapter->tar |= I2C_TAR_10BITADDR_MASTER;
  94. }
  95. else
  96. {
  97. i2c_adapter->tar &= ~I2C_TAR_10BITADDR_MASTER;
  98. }
  99. }
  100. static int ki2c_waittx(volatile i2c_t *i2c_adapter, int timeout_ms)
  101. {
  102. rt_tick_t start;
  103. start = rt_tick_get();
  104. while ((i2c_adapter->status & I2C_STATUS_ACTIVITY) || !(i2c_adapter->status & I2C_STATUS_TFE))
  105. {
  106. if (rt_tick_from_millisecond(rt_tick_get() - start) > timeout_ms)
  107. break;
  108. }
  109. if (i2c_adapter->tx_abrt_source != 0)
  110. return -RT_ERROR;
  111. return RT_EOK;
  112. }
  113. static void ki2c_clearerr(volatile i2c_t *i2c_adapter)
  114. {
  115. i2c_adapter->clr_tx_abrt = i2c_adapter->clr_tx_abrt;
  116. }
  117. static rt_ssize_t _i2c_mst_xfer(struct rt_i2c_bus_device *bus,
  118. struct rt_i2c_msg msgs[],
  119. rt_uint32_t num)
  120. {
  121. rt_ssize_t i;
  122. i2c_t *kbus = (i2c_t *)bus->priv;
  123. rt_err_t status;
  124. int waittx = 0;
  125. RT_ASSERT(bus != RT_NULL);
  126. if(msgs[0].flags & RT_I2C_ADDR_10BIT)
  127. {
  128. ki2c_setaddr(kbus, msgs[0].addr, 10);
  129. }
  130. else
  131. {
  132. ki2c_setaddr(kbus, msgs[0].addr, 7);
  133. }
  134. ki2c_clearerr(kbus);
  135. for (i = 0; i < num; i++)
  136. {
  137. waittx = 0;
  138. if (msgs[i].flags & RT_I2C_RD)
  139. {
  140. status = ki2c_recv(kbus, msgs[i].buf, msgs[i].len);
  141. }
  142. else
  143. {
  144. status = ki2c_send(kbus, msgs[i].buf, msgs[i].len);
  145. waittx = 1;
  146. }
  147. if (status != RT_EOK)
  148. {
  149. goto _out;
  150. }
  151. }
  152. if (waittx)
  153. {
  154. status = ki2c_waittx(kbus, 2000);
  155. if (status != RT_EOK)
  156. {
  157. goto _out;
  158. }
  159. }
  160. return i;
  161. _out:
  162. return status;
  163. }
  164. static const struct rt_i2c_bus_device_ops i2c_ops =
  165. {
  166. .master_xfer = _i2c_mst_xfer,
  167. .slave_xfer = RT_NULL,
  168. .i2c_bus_control = RT_NULL,
  169. };
  170. #ifdef RT_USING_I2C_BITOPS
  171. typedef struct pin_info_s {
  172. uint32_t scl;
  173. uint32_t sda;
  174. } pin_info_t;
  175. static void set_sda(void *data, rt_int32_t state)
  176. {
  177. pin_info_t *pin = (pin_info_t *)data;
  178. /* state = 1: disable output. state = 0: enable output.*/
  179. set_gpio_bit(gpiohs->output_en.u32, pin->sda, !state);
  180. }
  181. static void set_scl(void *data, rt_int32_t state)
  182. {
  183. pin_info_t *pin = (pin_info_t *)data;
  184. /* state = 1: disable output. state = 0: enable output.*/
  185. set_gpio_bit(gpiohs->output_en.u32, pin->scl, !state);
  186. }
  187. static rt_int32_t get_sda(void *data)
  188. {
  189. pin_info_t *pin = (pin_info_t *)data;
  190. /* disable output.*/
  191. set_gpio_bit(gpiohs->output_en.u32, pin->sda, 0);
  192. return get_gpio_bit(gpiohs->input_val.u32, pin->sda);
  193. }
  194. static rt_int32_t get_scl(void *data)
  195. {
  196. pin_info_t *pin = (pin_info_t *)data;
  197. /* disable output.*/
  198. set_gpio_bit(gpiohs->output_en.u32, pin->scl, 0);
  199. return get_gpio_bit(gpiohs->input_val.u32, pin->scl);
  200. }
  201. static void udelay(rt_uint32_t us)
  202. {
  203. usleep((uint64_t)us);
  204. }
  205. static struct rt_i2c_bit_ops bit_ops_0 =
  206. {
  207. RT_NULL,
  208. set_sda,
  209. set_scl,
  210. get_sda,
  211. get_scl,
  212. udelay,
  213. 5,
  214. 5
  215. };
  216. static struct rt_i2c_bit_ops bit_ops_1 =
  217. {
  218. RT_NULL,
  219. set_sda,
  220. set_scl,
  221. get_sda,
  222. get_scl,
  223. udelay,
  224. 5,
  225. 5
  226. };
  227. static struct rt_i2c_bit_ops bit_ops_2 =
  228. {
  229. RT_NULL,
  230. set_sda,
  231. set_scl,
  232. get_sda,
  233. get_scl,
  234. udelay,
  235. 5,
  236. 5
  237. };
  238. extern int get_pin_channel(rt_base_t pin_index);
  239. #endif
  240. int rt_hw_i2c_init(void)
  241. {
  242. struct rt_i2c_bus_device *busdev;
  243. #ifdef BSP_USING_I2C0
  244. static struct rt_i2c_bus_device i2c_dev0;
  245. busdev = &i2c_dev0;
  246. #ifdef RT_USING_I2C_BITOPS
  247. fpioa_set_function(BSP_I2C0_SCL_PIN, FUNC_RESV0);
  248. fpioa_set_function(BSP_I2C0_SDA_PIN, FUNC_RESV0);
  249. rt_pin_write(BSP_I2C0_SCL_PIN, PIN_LOW);
  250. rt_pin_write(BSP_I2C0_SDA_PIN, PIN_LOW);
  251. rt_pin_mode(BSP_I2C0_SCL_PIN, PIN_MODE_INPUT_PULLUP);
  252. rt_pin_mode(BSP_I2C0_SDA_PIN, PIN_MODE_INPUT_PULLUP);
  253. static pin_info_t pin0;
  254. pin0.scl = get_pin_channel(BSP_I2C0_SCL_PIN);
  255. pin0.sda = get_pin_channel(BSP_I2C0_SDA_PIN);
  256. bit_ops_0.data = (void *)&pin0;
  257. busdev->priv = (void *)&bit_ops_0;
  258. rt_i2c_bit_add_bus(busdev, "i2c0");
  259. #else
  260. busdev->ops = &i2c_ops;
  261. busdev->priv = (void *)I2C0_BASE_ADDR;
  262. i2c_init(I2C_DEVICE_0, 0, 7, 100000);
  263. rt_i2c_bus_device_register(busdev, "i2c0");
  264. #endif
  265. #endif
  266. #ifdef BSP_USING_I2C1
  267. static struct rt_i2c_bus_device i2c_dev1;
  268. busdev = &i2c_dev1;
  269. #ifdef RT_USING_I2C_BITOPS
  270. fpioa_set_function(BSP_I2C1_SCL_PIN, FUNC_RESV0);
  271. fpioa_set_function(BSP_I2C1_SDA_PIN, FUNC_RESV0);
  272. rt_pin_write(BSP_I2C1_SCL_PIN, PIN_LOW);
  273. rt_pin_write(BSP_I2C1_SDA_PIN, PIN_LOW);
  274. rt_pin_mode(BSP_I2C1_SCL_PIN, PIN_MODE_INPUT_PULLUP);
  275. rt_pin_mode(BSP_I2C1_SDA_PIN, PIN_MODE_INPUT_PULLUP);
  276. static pin_info_t pin1;
  277. pin1.scl = get_pin_channel(BSP_I2C1_SCL_PIN);
  278. pin1.sda = get_pin_channel(BSP_I2C1_SDA_PIN);
  279. bit_ops_1.data = (void *)&pin1;
  280. busdev->priv = (void *)&bit_ops_1;
  281. rt_i2c_bit_add_bus(busdev, "i2c1");
  282. #else
  283. busdev->ops = &i2c_ops;
  284. busdev->priv = (void *)I2C1_BASE_ADDR;
  285. i2c_init(I2C_DEVICE_1, 0, 7, 100000);
  286. rt_i2c_bus_device_register(busdev, "i2c1");
  287. #endif
  288. #endif
  289. #ifdef BSP_USING_I2C2
  290. static struct rt_i2c_bus_device i2c_dev2;
  291. busdev = &i2c_dev2;
  292. #ifdef RT_USING_I2C_BITOPS
  293. fpioa_set_function(BSP_I2C2_SCL_PIN, FUNC_RESV0);
  294. fpioa_set_function(BSP_I2C2_SDA_PIN, FUNC_RESV0);
  295. rt_pin_write(BSP_I2C2_SCL_PIN, PIN_LOW);
  296. rt_pin_write(BSP_I2C2_SDA_PIN, PIN_LOW);
  297. rt_pin_mode(BSP_I2C2_SCL_PIN, PIN_MODE_INPUT_PULLUP);
  298. rt_pin_mode(BSP_I2C2_SDA_PIN, PIN_MODE_INPUT_PULLUP);
  299. static pin_info_t pin2;
  300. pin2.scl = get_pin_channel(BSP_I2C2_SCL_PIN);
  301. pin2.sda = get_pin_channel(BSP_I2C2_SDA_PIN);
  302. bit_ops_2.data = (void *)&pin2;
  303. busdev->priv = (void *)&bit_ops_2;
  304. rt_i2c_bit_add_bus(busdev, "i2c2");
  305. #else
  306. busdev->ops = &i2c_ops;
  307. busdev->priv = (void *)I2C2_BASE_ADDR;
  308. i2c_init(I2C_DEVICE_2, 0, 7, 100000);
  309. rt_i2c_bus_device_register(busdev, "i2c2");
  310. #endif
  311. #endif
  312. return 0;
  313. }
  314. INIT_BOARD_EXPORT(rt_hw_i2c_init);
  315. #endif