dev_i2c.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. * @addtogroup Drivers RTTHREAD Driver
  16. * @defgroup I2C I2C
  17. *
  18. * @brief I2C driver api
  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. * @ingroup Drivers
  171. */
  172. /*!
  173. * @addtogroup I2C
  174. * @{
  175. */
  176. #ifdef __cplusplus
  177. extern "C" {
  178. #endif
  179. #define RT_I2C_WR 0x0000 /*!< i2c wirte flag */
  180. #define RT_I2C_RD (1u << 0) /*!< i2c read flag */
  181. #define RT_I2C_ADDR_10BIT (1u << 2) /*!< this is a ten bit chip address */
  182. #define RT_I2C_NO_START (1u << 4) /*!< do not generate START condition */
  183. #define RT_I2C_IGNORE_NACK (1u << 5) /*!< ignore NACK from slave */
  184. #define RT_I2C_NO_READ_ACK (1u << 6) /* when I2C reading, we do not ACK */
  185. #define RT_I2C_NO_STOP (1u << 7) /*!< do not generate STOP condition */
  186. #define RT_I2C_DEV_CTRL_10BIT (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x01)
  187. #define RT_I2C_DEV_CTRL_ADDR (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x02)
  188. #define RT_I2C_DEV_CTRL_TIMEOUT (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x03)
  189. #define RT_I2C_DEV_CTRL_RW (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x04)
  190. #define RT_I2C_DEV_CTRL_CLK (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x05)
  191. #define RT_I2C_DEV_CTRL_UNLOCK (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x06)
  192. #define RT_I2C_DEV_CTRL_GET_STATE (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x07)
  193. #define RT_I2C_DEV_CTRL_GET_MODE (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x08)
  194. #define RT_I2C_DEV_CTRL_GET_ERROR (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x09)
  195. /**
  196. * @brief I2C Private Data
  197. */
  198. struct rt_i2c_priv_data
  199. {
  200. struct rt_i2c_msg *msgs;
  201. rt_size_t number;
  202. };
  203. /**
  204. * @brief I2C Message
  205. */
  206. struct rt_i2c_msg
  207. {
  208. rt_uint16_t addr;
  209. rt_uint16_t flags;
  210. rt_uint16_t len;
  211. rt_uint8_t *buf;
  212. };
  213. struct rt_i2c_bus_device;
  214. /**
  215. * @brief I2C Bus Device Operations
  216. */
  217. struct rt_i2c_bus_device_ops
  218. {
  219. rt_ssize_t (*master_xfer)(struct rt_i2c_bus_device *bus,
  220. struct rt_i2c_msg msgs[],
  221. rt_uint32_t num);
  222. rt_ssize_t (*slave_xfer)(struct rt_i2c_bus_device *bus,
  223. struct rt_i2c_msg msgs[],
  224. rt_uint32_t num);
  225. rt_err_t (*i2c_bus_control)(struct rt_i2c_bus_device *bus,
  226. int cmd,
  227. void *args);
  228. };
  229. /**
  230. * @brief I2C Bus Device
  231. */
  232. struct rt_i2c_bus_device
  233. {
  234. struct rt_device parent;
  235. const struct rt_i2c_bus_device_ops *ops;
  236. rt_uint16_t flags;
  237. struct rt_mutex lock;
  238. rt_uint32_t timeout;
  239. rt_uint32_t retries;
  240. void *priv;
  241. };
  242. /**
  243. * @brief I2C Client
  244. */
  245. struct rt_i2c_client
  246. {
  247. #ifdef RT_USING_DM
  248. struct rt_device parent;
  249. const char *name;
  250. const struct rt_i2c_device_id *id;
  251. const struct rt_ofw_node_id *ofw_id;
  252. #endif
  253. struct rt_i2c_bus_device *bus;
  254. rt_uint16_t client_addr;
  255. };
  256. #ifdef RT_USING_DM
  257. struct rt_i2c_device_id
  258. {
  259. char name[20];
  260. void *data;
  261. };
  262. struct rt_i2c_driver
  263. {
  264. struct rt_driver parent;
  265. const struct rt_i2c_device_id *ids;
  266. const struct rt_ofw_node_id *ofw_ids;
  267. rt_err_t (*probe)(struct rt_i2c_client *client);
  268. rt_err_t (*remove)(struct rt_i2c_client *client);
  269. rt_err_t (*shutdown)(struct rt_i2c_client *client);
  270. };
  271. rt_err_t rt_i2c_driver_register(struct rt_i2c_driver *driver);
  272. rt_err_t rt_i2c_device_register(struct rt_i2c_client *client);
  273. #define RT_I2C_DRIVER_EXPORT(driver) RT_DRIVER_EXPORT(driver, i2c, BUILIN)
  274. #endif /* RT_USING_DM */
  275. /**
  276. * @brief I2C Bus Device Initialization
  277. *
  278. * @param bus the I2C bus device
  279. * @param name the name of I2C bus device
  280. *
  281. * @return rt_err_t error code
  282. */
  283. rt_err_t rt_i2c_bus_device_device_init(struct rt_i2c_bus_device *bus,
  284. const char *name);
  285. /**
  286. * @brief I2C Bus Device Register
  287. *
  288. * @param bus the I2C bus device
  289. * @param bus_name the name of I2C bus device
  290. *
  291. * @return rt_err_t error code
  292. */
  293. rt_err_t rt_i2c_bus_device_register(struct rt_i2c_bus_device *bus,
  294. const char *bus_name);
  295. /**
  296. * @brief I2C Bus Device Find
  297. *
  298. * @param bus_name the name of I2C bus device
  299. *
  300. * @return rt_i2c_bus_device the I2C bus device
  301. */
  302. struct rt_i2c_bus_device *rt_i2c_bus_device_find(const char *bus_name);
  303. /**
  304. * @brief I2C data transmission.
  305. *
  306. * @param bus the I2C bus device
  307. * @param msgs the I2C message list
  308. * @param num the number of I2C message
  309. *
  310. * @return rt_ssize_t the actual length of transmitted
  311. */
  312. rt_ssize_t rt_i2c_transfer(struct rt_i2c_bus_device *bus,
  313. struct rt_i2c_msg msgs[],
  314. rt_uint32_t num);
  315. /**
  316. * @brief I2C Control
  317. *
  318. * @param bus the I2C bus device
  319. * @param cmd the I2C control command
  320. * @param args the I2C control arguments
  321. *
  322. * @return rt_err_t error code
  323. */
  324. rt_err_t rt_i2c_control(struct rt_i2c_bus_device *bus,
  325. int cmd,
  326. void *args);
  327. /**
  328. * @brief I2C Master Send
  329. *
  330. * @param bus the I2C bus device
  331. * @param addr the I2C slave address
  332. * @param flags the I2C flags
  333. * @param buf the I2C send buffer
  334. * @param count the I2C send buffer length
  335. *
  336. * @return rt_ssize_t the actual length of transmitted
  337. */
  338. rt_ssize_t rt_i2c_master_send(struct rt_i2c_bus_device *bus,
  339. rt_uint16_t addr,
  340. rt_uint16_t flags,
  341. const rt_uint8_t *buf,
  342. rt_uint32_t count);
  343. /**
  344. * @brief I2C Master Receive
  345. *
  346. * @param bus the I2C bus device
  347. * @param addr the I2C slave address
  348. * @param flags the I2C flags
  349. * @param buf the I2C receive buffer
  350. * @param count the I2C receive buffer length
  351. *
  352. * @return rt_ssize_t the actual length of received
  353. */
  354. rt_ssize_t rt_i2c_master_recv(struct rt_i2c_bus_device *bus,
  355. rt_uint16_t addr,
  356. rt_uint16_t flags,
  357. rt_uint8_t *buf,
  358. rt_uint32_t count);
  359. rt_inline rt_err_t rt_i2c_bus_lock(struct rt_i2c_bus_device *bus, rt_tick_t timeout)
  360. {
  361. return rt_mutex_take(&bus->lock, timeout);
  362. }
  363. rt_inline rt_err_t rt_i2c_bus_unlock(struct rt_i2c_bus_device *bus)
  364. {
  365. return rt_mutex_release(&bus->lock);
  366. }
  367. #ifdef __cplusplus
  368. }
  369. #endif
  370. /*! @}*/
  371. #endif