drv_i2c.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * 2021-11-19 xieyangrun first version
  8. */
  9. #include <rthw.h>
  10. #include <rtdevice.h>
  11. #include "sunxi_hal_twi.h"
  12. #define DBG_LVL DBG_WARNING
  13. #define DBG_TAG "drv/i2c"
  14. #include <rtdbg.h>
  15. struct hal_i2c_bus
  16. {
  17. struct rt_i2c_bus_device parent;
  18. twi_port_t id;
  19. struct rt_mutex lock;
  20. };
  21. //connect am drv to rt drv.
  22. static rt_ssize_t _i2c_master_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg *msgs, rt_uint32_t num)
  23. {
  24. struct hal_i2c_bus *_i2c_bus = (struct hal_i2c_bus *)bus;
  25. struct rt_i2c_msg *msg;
  26. twi_msg_t *twi_msg;
  27. int i;
  28. twi_status_t status;
  29. extern twi_status_t hal_twi_xfer(twi_port_t port, twi_msg_t *msgs, int32_t num);
  30. twi_msg = rt_malloc(sizeof(*twi_msg) * num);
  31. if (!twi_msg)
  32. {
  33. LOG_E("i2c xfer malloc(%d) failure\n", sizeof(*twi_msg) * num);
  34. return -RT_ENOMEM;
  35. }
  36. for (i = 0; i < num; i++)
  37. {
  38. msg = &msgs[i];
  39. if (msg->flags == RT_I2C_RD)
  40. {
  41. twi_msg[i].flags = TWI_M_RD;
  42. }
  43. else if(msg->flags == RT_I2C_WR)
  44. {
  45. twi_msg[i].flags = 0;
  46. }
  47. if (_i2c_bus->parent.flags & RT_I2C_DEV_CTRL_10BIT)
  48. {
  49. twi_msg[i].flags |= TWI_M_TEN;
  50. }
  51. twi_msg[i].addr = msg->addr;
  52. twi_msg[i].len = msg->len;
  53. twi_msg[i].buf = msg->buf;
  54. }
  55. rt_mutex_take(&_i2c_bus->lock, RT_WAITING_FOREVER);
  56. status = hal_twi_xfer(_i2c_bus->id, twi_msg, i);
  57. rt_mutex_release(&_i2c_bus->lock);
  58. if (status != TWI_STATUS_OK)
  59. {
  60. i = 0;
  61. LOG_E("i2c xfer failure\n");
  62. }
  63. rt_free(twi_msg);
  64. return i;
  65. }
  66. static const struct rt_i2c_bus_device_ops _i2c_ops =
  67. {
  68. _i2c_master_xfer,
  69. RT_NULL,
  70. RT_NULL
  71. };
  72. #ifdef BSP_USING_I2C0
  73. static struct hal_i2c_bus _i2c_bus_0 = {
  74. .id = TWI_MASTER_0
  75. };
  76. #endif
  77. #ifdef BSP_USING_I2C1
  78. static struct hal_i2c_bus _i2c_bus_1 = {
  79. .id = TWI_MASTER_1,
  80. };
  81. #endif
  82. #ifdef BSP_USING_I2C2
  83. static struct hal_i2c_bus _i2c_bus_2 = {
  84. .id = TWI_MASTER_2,
  85. };
  86. #endif
  87. #ifdef BSP_USING_I2C3
  88. static struct hal_i2c_bus _i2c_bus_3 = {
  89. .id = TWI_MASTER_3,
  90. };
  91. #endif
  92. #define CFG_GPIO_PORT(p) ((p) - 'A' + 1)
  93. static const user_gpio_set_t _i2c_gpio_cfg[][2] = {
  94. {// twi0
  95. {
  96. .gpio_name = "twi0.sck",
  97. .port = CFG_GPIO_PORT('B'),
  98. .port_num = 3, // PB3
  99. .mul_sel = 4,
  100. .pull = 1, // pull up
  101. .drv_level = 3,
  102. .data = 1,
  103. },
  104. {
  105. .gpio_name = "twi0.sda",
  106. .port = CFG_GPIO_PORT('B'),
  107. .port_num = 2, // PB2
  108. .mul_sel = 4,
  109. .pull = 1, // pull up
  110. .drv_level = 3,
  111. .data = 1,
  112. }
  113. },
  114. {// twi1
  115. {
  116. .gpio_name = "twi1.sck",
  117. .port = CFG_GPIO_PORT('B'),
  118. .port_num = 4, // PB4
  119. .mul_sel = 4,
  120. .pull = 1, // pull up
  121. .drv_level = 3,
  122. .data = 1,
  123. },
  124. {
  125. .gpio_name = "twi1.sda",
  126. .port = CFG_GPIO_PORT('B'),
  127. .port_num = 5, // PB5
  128. .mul_sel = 4,
  129. .pull = 1, // pull up
  130. .drv_level = 3,
  131. .data = 1,
  132. }
  133. },
  134. #ifdef BSP_USING_MANGOPI // mango board
  135. {// twi2
  136. {
  137. .gpio_name = "twi2.sck",
  138. .port = CFG_GPIO_PORT('E'),
  139. .port_num = 12, // PE12
  140. .mul_sel = 2,
  141. .pull = 1, // pull up
  142. .drv_level = 3,
  143. .data = 1,
  144. },
  145. {
  146. .gpio_name = "twi2.sda",
  147. .port = CFG_GPIO_PORT('E'),
  148. .port_num = 13, // PE13
  149. .mul_sel = 2,
  150. .pull = 1, // pull up
  151. .drv_level = 3,
  152. .data = 1,
  153. }
  154. },
  155. #elif defined(BSP_USING_M7)
  156. {// twi2
  157. {
  158. .gpio_name = "twi2.sck",
  159. .port = CFG_GPIO_PORT('G'),
  160. .port_num = 6, // PG6
  161. .mul_sel = 3,
  162. .pull = 1, // pull up
  163. .drv_level = 3,
  164. .data = 1,
  165. },
  166. {
  167. .gpio_name = "twi2.sda",
  168. .port = CFG_GPIO_PORT('G'),
  169. .port_num = 7, // PG7
  170. .mul_sel = 3,
  171. .pull = 1, // pull up
  172. .drv_level = 3,
  173. .data = 1,
  174. }
  175. },
  176. #endif
  177. {// twi3
  178. {
  179. .gpio_name = "twi3.sck",
  180. .port = CFG_GPIO_PORT('B'),
  181. .port_num = 6, // PB6
  182. .mul_sel = 4,
  183. .pull = 1, // pull up
  184. .drv_level = 3,
  185. .data = 1,
  186. },
  187. {
  188. .gpio_name = "twi3.sda",
  189. .port = CFG_GPIO_PORT('B'),
  190. .port_num = 7, // PB7
  191. .mul_sel = 4,
  192. .pull = 1, // pull up
  193. .drv_level = 3,
  194. .data = 1,
  195. }
  196. },
  197. };
  198. int hal_i2c_gpio_cfg_load(user_gpio_set_t *gpio_cfg, int32_t GPIONum, int id)
  199. {
  200. int i;
  201. if (id > sizeof(_i2c_gpio_cfg) / sizeof(_i2c_gpio_cfg[0]))
  202. {
  203. rt_kprintf("twi id %d>%d\n", id, sizeof(_i2c_gpio_cfg) / sizeof(_i2c_gpio_cfg[0]));
  204. return -1;
  205. }
  206. /* twi0 */
  207. for (i = 0; i < GPIONum; i++)
  208. {
  209. memcpy(gpio_cfg, &_i2c_gpio_cfg[id][i], sizeof(user_gpio_set_t));
  210. gpio_cfg++;
  211. }
  212. return 0;
  213. }
  214. static int _i2c_dev_register(struct hal_i2c_bus* bus, const char* name)
  215. {
  216. rt_mutex_init(&bus->lock, name, RT_IPC_FLAG_PRIO);
  217. hal_twi_init(bus->id);
  218. bus->parent.ops = &_i2c_ops;
  219. if (rt_i2c_bus_device_register(&bus->parent, name) != RT_EOK)
  220. {
  221. LOG_E("i2c bus register:%s failure\n", name);
  222. return -1;
  223. }
  224. return 0;
  225. }
  226. int rt_hw_i2c_init(void)
  227. {
  228. #ifdef BSP_USING_I2C0
  229. /* init i2c bus device */
  230. _i2c_dev_register(&_i2c_bus_0, "i2c0");
  231. #endif
  232. #ifdef BSP_USING_I2C1
  233. /* init i2c bus device */
  234. _i2c_dev_register(&_i2c_bus_1, "i2c1");
  235. #endif
  236. #ifdef BSP_USING_I2C2
  237. _i2c_dev_register(&_i2c_bus_2, "i2c2");
  238. #endif
  239. #ifdef BSP_USING_I2C3
  240. _i2c_dev_register(&_i2c_bus_3, "i2c3");
  241. #endif
  242. //rt_kprintf("i2c_init!\n");
  243. return 0;
  244. }
  245. // #ifdef RT_USING_COMPONENTS_INIT
  246. INIT_BOARD_EXPORT(rt_hw_i2c_init);
  247. // #endif
  248. static void _i2c_test(int argc, char *args[])
  249. {
  250. struct rt_i2c_bus_device *i2c_bus;
  251. struct rt_i2c_msg msg[2];
  252. uint8_t buf[3] = {0x12, 0x34, 0x56};
  253. if (argc < 2) return;
  254. i2c_bus = (struct rt_i2c_bus_device*)rt_device_find("i2c2");
  255. if (!i2c_bus)
  256. {
  257. rt_kprintf("i2c2 not found\n");
  258. return;
  259. }
  260. msg[0].addr = 0x36;
  261. msg[0].flags = 0;
  262. msg[0].buf = buf;
  263. msg[0].len = sizeof(buf);
  264. msg[1].addr = 0x36;
  265. msg[1].flags = RT_I2C_RD;
  266. msg[1].buf = buf;
  267. msg[1].len = sizeof(buf);
  268. if (atoi(args[1]) == 0)
  269. {
  270. rt_i2c_transfer(i2c_bus, &msg[0], 1);
  271. }
  272. else
  273. {
  274. rt_i2c_transfer(i2c_bus, &msg[0], 2);
  275. }
  276. }
  277. MSH_CMD_EXPORT_ALIAS(_i2c_test, i2c_test, i2c bus test);
  278. static void _pin_test(void)
  279. {
  280. int i;
  281. rt_base_t pin;
  282. pin = GPIO_PE10;
  283. rt_pin_mode(pin, PIN_MODE_OUTPUT);
  284. for (i = 0; i < 20; i++)
  285. {
  286. rt_pin_write(pin, !!(i & 1));
  287. rt_thread_mdelay(2);
  288. }
  289. }
  290. MSH_CMD_EXPORT_ALIAS(_pin_test, pin_test, gpio pin test);
  291. /*@}*/