dev_i2c.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * Copyright (c) 2006-2024 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-04-25 weety first version
  9. * 2021-04-20 RiceChen added support for bus control api
  10. */
  11. #ifndef __DEV_I2C_H__
  12. #define __DEV_I2C_H__
  13. #include <rtthread.h>
  14. /**
  15. * @defgroup group_drivers_i2c I2C
  16. * @brief I2C driver api
  17. * @ingroup group_device_driver
  18. *
  19. * <b>Example</b>
  20. * @code {.c}
  21. * #include <rtthread.h>
  22. * #include <rtdevice.h>
  23. *
  24. * #define AHT10_I2C_BUS_NAME "i2c1" // 传感器连接的I2C总线设备名称
  25. * #define AHT10_ADDR 0x38 // 从机地址
  26. * #define AHT10_CALIBRATION_CMD 0xE1 // 校准命令
  27. * #define AHT10_NORMAL_CMD 0xA8 // 一般命令
  28. * #define AHT10_GET_DATA 0xAC // 获取数据命令
  29. *
  30. * static struct rt_i2c_bus_device *i2c_bus = RT_NULL; // I2C总线设备句柄
  31. * static rt_bool_t initialized = RT_FALSE; // 传感器初始化状态
  32. *
  33. * // 写传感器寄存器
  34. * static rt_err_t write_reg(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint8_t *data)
  35. * {
  36. * rt_uint8_t buf[3];
  37. * struct rt_i2c_msg msgs;
  38. * rt_uint32_t buf_size = 1;
  39. *
  40. * buf[0] = reg; //cmd
  41. * if (data != RT_NULL)
  42. * {
  43. * buf[1] = data[0];
  44. * buf[2] = data[1];
  45. * buf_size = 3;
  46. * }
  47. *
  48. * msgs.addr = AHT10_ADDR;
  49. * msgs.flags = RT_I2C_WR;
  50. * msgs.buf = buf;
  51. * msgs.len = buf_size;
  52. *
  53. * // 调用I2C设备接口传输数据
  54. * if (rt_i2c_transfer(bus, &msgs, 1) == 1)
  55. * {
  56. * return RT_EOK;
  57. * }
  58. * else
  59. * {
  60. * return -RT_ERROR;
  61. * }
  62. * }
  63. *
  64. * // 读传感器寄存器数据
  65. * static rt_err_t read_regs(struct rt_i2c_bus_device *bus, rt_uint8_t len, rt_uint8_t *buf)
  66. * {
  67. * struct rt_i2c_msg msgs;
  68. *
  69. * msgs.addr = AHT10_ADDR;
  70. * msgs.flags = RT_I2C_RD;
  71. * msgs.buf = buf;
  72. * msgs.len = len;
  73. *
  74. * // 调用I2C设备接口传输数据
  75. * if (rt_i2c_transfer(bus, &msgs, 1) == 1)
  76. * {
  77. * return RT_EOK;
  78. * }
  79. * else
  80. * {
  81. * return -RT_ERROR;
  82. * }
  83. * }
  84. *
  85. * static void read_temp_humi(float *cur_temp, float *cur_humi)
  86. * {
  87. * rt_uint8_t temp[6];
  88. *
  89. * write_reg(i2c_bus, AHT10_GET_DATA, RT_NULL); // 发送命令
  90. * rt_thread_mdelay(400);
  91. * read_regs(i2c_bus, 6, temp); // 获取传感器数据
  92. *
  93. * // 湿度数据转换
  94. * *cur_humi = (temp[1] << 12 | temp[2] << 4 | (temp[3] & 0xf0) >> 4) * 100.0 / (1 << 20);
  95. * // 温度数据转换
  96. * *cur_temp = ((temp[3] & 0xf) << 16 | temp[4] << 8 | temp[5]) * 200.0 / (1 << 20) - 50;
  97. * }
  98. *
  99. * static void aht10_init(const char *name)
  100. * {
  101. * rt_uint8_t temp[2] = {0, 0};
  102. *
  103. * // 查找I2C总线设备,获取I2C总线设备句柄
  104. * i2c_bus = (struct rt_i2c_bus_device *)rt_device_find(name);
  105. *
  106. * if (i2c_bus == RT_NULL)
  107. * {
  108. * rt_kprintf("can't find %s device!\n", name);
  109. * }
  110. * else
  111. * {
  112. * write_reg(i2c_bus, AHT10_NORMAL_CMD, temp);
  113. * rt_thread_mdelay(400);
  114. *
  115. * temp[0] = 0x08;
  116. * temp[1] = 0x00;
  117. * write_reg(i2c_bus, AHT10_CALIBRATION_CMD, temp);
  118. * rt_thread_mdelay(400);
  119. * initialized = RT_TRUE;
  120. * }
  121. * }
  122. *
  123. * static void i2c_aht10_sample(int argc, char *argv[])
  124. * {
  125. * float humidity, temperature;
  126. * char name[RT_NAME_MAX];
  127. *
  128. * humidity = 0.0;
  129. * temperature = 0.0;
  130. *
  131. * if (argc == 2)
  132. * {
  133. * rt_strncpy(name, argv[1], RT_NAME_MAX);
  134. * }
  135. * else
  136. * {
  137. * rt_strncpy(name, AHT10_I2C_BUS_NAME, RT_NAME_MAX);
  138. * }
  139. *
  140. * if (!initialized)
  141. * {
  142. * // 传感器初始化
  143. * aht10_init(name);
  144. * }
  145. * if (initialized)
  146. * {
  147. * // 读取温湿度数据
  148. * read_temp_humi(&temperature, &humidity);
  149. *
  150. * rt_kprintf("read aht10 sensor humidity : %d.%d %%\n", (int)humidity, (int)(humidity * 10) % 10);
  151. * if( temperature >= 0 )
  152. * {
  153. * rt_kprintf("read aht10 sensor temperature: %d.%d°C\n", (int)temperature, (int)(temperature * 10) % 10);
  154. * }
  155. * else
  156. * {
  157. * rt_kprintf("read aht10 sensor temperature: %d.%d°C\n", (int)temperature, (int)(-temperature * 10) % 10);
  158. * }
  159. * }
  160. * else
  161. * {
  162. * rt_kprintf("initialize sensor failed!\n");
  163. * }
  164. * }
  165. * // 导出到 msh 命令列表中
  166. * MSH_CMD_EXPORT(i2c_aht10_sample, i2c aht10 sample);
  167. * @endcode
  168. */
  169. /*!
  170. * @addtogroup group_drivers_i2c
  171. * @{
  172. */
  173. #ifdef __cplusplus
  174. extern "C" {
  175. #endif
  176. #define RT_I2C_WR 0x0000 /*!< i2c wirte flag */
  177. #define RT_I2C_RD (1u << 0) /*!< i2c read flag */
  178. #define RT_I2C_ADDR_10BIT (1u << 2) /*!< this is a ten bit chip address */
  179. #define RT_I2C_NO_START (1u << 4) /*!< do not generate START condition */
  180. #define RT_I2C_IGNORE_NACK (1u << 5) /*!< ignore NACK from slave */
  181. #define RT_I2C_NO_READ_ACK (1u << 6) /* when I2C reading, we do not ACK */
  182. #define RT_I2C_NO_STOP (1u << 7) /*!< do not generate STOP condition */
  183. #define RT_I2C_DEV_CTRL_10BIT (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x01)
  184. #define RT_I2C_DEV_CTRL_ADDR (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x02)
  185. #define RT_I2C_DEV_CTRL_TIMEOUT (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x03)
  186. #define RT_I2C_DEV_CTRL_RW (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x04)
  187. #define RT_I2C_DEV_CTRL_CLK (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x05)
  188. #define RT_I2C_DEV_CTRL_UNLOCK (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x06)
  189. #define RT_I2C_DEV_CTRL_GET_STATE (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x07)
  190. #define RT_I2C_DEV_CTRL_GET_MODE (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x08)
  191. #define RT_I2C_DEV_CTRL_GET_ERROR (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x09)
  192. /**
  193. * @brief I2C Private Data
  194. */
  195. struct rt_i2c_priv_data
  196. {
  197. struct rt_i2c_msg *msgs;
  198. rt_size_t number;
  199. };
  200. /**
  201. * @brief I2C Message
  202. */
  203. struct rt_i2c_msg
  204. {
  205. rt_uint16_t addr;
  206. rt_uint16_t flags;
  207. rt_uint16_t len;
  208. rt_uint8_t *buf;
  209. };
  210. struct rt_i2c_bus_device;
  211. /**
  212. * @brief I2C Bus Device Operations
  213. */
  214. struct rt_i2c_bus_device_ops
  215. {
  216. rt_ssize_t (*master_xfer)(struct rt_i2c_bus_device *bus,
  217. struct rt_i2c_msg msgs[],
  218. rt_uint32_t num);
  219. rt_ssize_t (*slave_xfer)(struct rt_i2c_bus_device *bus,
  220. struct rt_i2c_msg msgs[],
  221. rt_uint32_t num);
  222. rt_err_t (*i2c_bus_control)(struct rt_i2c_bus_device *bus,
  223. int cmd,
  224. void *args);
  225. };
  226. /**
  227. * @brief I2C Bus Device
  228. */
  229. struct rt_i2c_bus_device
  230. {
  231. struct rt_device parent;
  232. const struct rt_i2c_bus_device_ops *ops;
  233. rt_uint16_t flags;
  234. struct rt_mutex lock;
  235. rt_uint32_t timeout;
  236. rt_uint32_t retries;
  237. void *priv;
  238. };
  239. /**
  240. * @brief I2C Client
  241. */
  242. struct rt_i2c_client
  243. {
  244. #ifdef RT_USING_DM
  245. struct rt_device parent;
  246. const char *name;
  247. const struct rt_i2c_device_id *id;
  248. const struct rt_ofw_node_id *ofw_id;
  249. #endif
  250. struct rt_i2c_bus_device *bus;
  251. rt_uint16_t client_addr;
  252. };
  253. #ifdef RT_USING_DM
  254. struct rt_i2c_device_id
  255. {
  256. char name[20];
  257. void *data;
  258. };
  259. struct rt_i2c_driver
  260. {
  261. struct rt_driver parent;
  262. const struct rt_i2c_device_id *ids;
  263. const struct rt_ofw_node_id *ofw_ids;
  264. rt_err_t (*probe)(struct rt_i2c_client *client);
  265. rt_err_t (*remove)(struct rt_i2c_client *client);
  266. rt_err_t (*shutdown)(struct rt_i2c_client *client);
  267. };
  268. rt_err_t rt_i2c_driver_register(struct rt_i2c_driver *driver);
  269. rt_err_t rt_i2c_device_register(struct rt_i2c_client *client);
  270. #define RT_I2C_DRIVER_EXPORT(driver) RT_DRIVER_EXPORT(driver, i2c, BUILIN)
  271. #endif /* RT_USING_DM */
  272. /**
  273. * @brief I2C Bus Device Initialization
  274. *
  275. * @param bus the I2C bus device
  276. * @param name the name of I2C bus device
  277. *
  278. * @return rt_err_t error code
  279. */
  280. rt_err_t rt_i2c_bus_device_device_init(struct rt_i2c_bus_device *bus,
  281. const char *name);
  282. /**
  283. * @brief I2C Bus Device Register
  284. *
  285. * @param bus the I2C bus device
  286. * @param bus_name the name of I2C bus device
  287. *
  288. * @return rt_err_t error code
  289. */
  290. rt_err_t rt_i2c_bus_device_register(struct rt_i2c_bus_device *bus,
  291. const char *bus_name);
  292. /**
  293. * @brief I2C Bus Device Find
  294. *
  295. * @param bus_name the name of I2C bus device
  296. *
  297. * @return rt_i2c_bus_device the I2C bus device
  298. */
  299. struct rt_i2c_bus_device *rt_i2c_bus_device_find(const char *bus_name);
  300. /**
  301. * @brief I2C data transmission.
  302. *
  303. * @param bus the I2C bus device
  304. * @param msgs the I2C message list
  305. * @param num the number of I2C message
  306. *
  307. * @return rt_ssize_t the actual length of transmitted
  308. */
  309. rt_ssize_t rt_i2c_transfer(struct rt_i2c_bus_device *bus,
  310. struct rt_i2c_msg msgs[],
  311. rt_uint32_t num);
  312. /**
  313. * @brief I2C Control
  314. *
  315. * @param bus the I2C bus device
  316. * @param cmd the I2C control command
  317. * @param args the I2C control arguments
  318. *
  319. * @return rt_err_t error code
  320. */
  321. rt_err_t rt_i2c_control(struct rt_i2c_bus_device *bus,
  322. int cmd,
  323. void *args);
  324. /**
  325. * @brief I2C Master Send
  326. *
  327. * @param bus the I2C bus device
  328. * @param addr the I2C slave address
  329. * @param flags the I2C flags
  330. * @param buf the I2C send buffer
  331. * @param count the I2C send buffer length
  332. *
  333. * @return rt_ssize_t the actual length of transmitted
  334. */
  335. rt_ssize_t rt_i2c_master_send(struct rt_i2c_bus_device *bus,
  336. rt_uint16_t addr,
  337. rt_uint16_t flags,
  338. const rt_uint8_t *buf,
  339. rt_uint32_t count);
  340. /**
  341. * @brief I2C Master Receive
  342. *
  343. * @param bus the I2C bus device
  344. * @param addr the I2C slave address
  345. * @param flags the I2C flags
  346. * @param buf the I2C receive buffer
  347. * @param count the I2C receive buffer length
  348. *
  349. * @return rt_ssize_t the actual length of received
  350. */
  351. rt_ssize_t rt_i2c_master_recv(struct rt_i2c_bus_device *bus,
  352. rt_uint16_t addr,
  353. rt_uint16_t flags,
  354. rt_uint8_t *buf,
  355. rt_uint32_t count);
  356. rt_inline rt_err_t rt_i2c_bus_lock(struct rt_i2c_bus_device *bus, rt_tick_t timeout)
  357. {
  358. return rt_mutex_take(&bus->lock, timeout);
  359. }
  360. rt_inline rt_err_t rt_i2c_bus_unlock(struct rt_i2c_bus_device *bus)
  361. {
  362. return rt_mutex_release(&bus->lock);
  363. }
  364. #ifdef __cplusplus
  365. }
  366. #endif
  367. /*! @}*/
  368. #endif