dev_i2c.h 12 KB

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