You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

296 lines
13 KiB

3 years ago
  1. // Copyright 2019-2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef _I2C_BUS_H_
  14. #define _I2C_BUS_H_
  15. #include "driver/i2c.h"
  16. #define NULL_I2C_MEM_ADDR 0xFF /*!< set mem_address to NULL_I2C_MEM_ADDR if i2c device has no internal address during read/write */
  17. #define NULL_I2C_DEV_ADDR 0xFF /*!< invalid i2c device address */
  18. typedef void *i2c_bus_handle_t; /*!< i2c bus handle */
  19. typedef void *i2c_bus_device_handle_t; /*!< i2c device handle */
  20. #ifdef __cplusplus
  21. extern "C"
  22. {
  23. #endif
  24. /**************************************** Public Functions (Application level)*********************************************/
  25. /**
  26. * @brief Create an I2C bus instance then return a handle if created successfully. Each I2C bus works in a singleton mode,
  27. * which means for an i2c port only one group parameter works. When i2c_bus_create is called more than one time for the
  28. * same i2c port, following parameter will override the previous one.
  29. *
  30. * @param port I2C port number
  31. * @param conf Pointer to I2C bus configuration
  32. * @return i2c_bus_handle_t Return the I2C bus handle if created successfully, return NULL if failed.
  33. */
  34. i2c_bus_handle_t i2c_bus_create(i2c_port_t port, const i2c_config_t *conf);
  35. /**
  36. * @brief Delete and release the I2C bus resource.
  37. *
  38. * @param p_bus_handle Point to the I2C bus handle, if delete succeed handle will set to NULL.
  39. * @return
  40. * - ESP_OK Success
  41. * - ESP_FAIL Fail
  42. */
  43. esp_err_t i2c_bus_delete(i2c_bus_handle_t *p_bus_handle);
  44. /**
  45. * @brief Scan i2c devices attached on i2c bus
  46. *
  47. * @param bus_handle I2C bus handle
  48. * @param buf Pointer to a buffer to save devices' address, if NULL no address will be saved.
  49. * @param num Maximum number of addresses to save, invalid if buf set to NULL,
  50. * higer addresses will be discarded if num less-than the total number found on the I2C bus.
  51. * @return uint8_t Total number of devices found on the I2C bus
  52. */
  53. uint8_t i2c_bus_scan(i2c_bus_handle_t bus_handle, uint8_t *buf, uint8_t num);
  54. /**
  55. * @brief Get current active clock speed.
  56. *
  57. * @param bus_handle I2C bus handle
  58. * @return uint32_t current clock speed
  59. */
  60. uint32_t i2c_bus_get_current_clk_speed(i2c_bus_handle_t bus_handle);
  61. /**
  62. * @brief Get created device number of the bus.
  63. *
  64. * @param bus_handle I2C bus handle
  65. * @return uint8_t created device number of the bus
  66. */
  67. uint8_t i2c_bus_get_created_device_num(i2c_bus_handle_t bus_handle);
  68. /**
  69. * @brief Create an I2C device on specific bus.
  70. * Dynamic configuration must be enable to achieve multiple devices with different configs on a single bus.
  71. * menuconfig:Bus Options->I2C Bus Options->enable dynamic configuration
  72. *
  73. * @param bus_handle Point to the I2C bus handle
  74. * @param dev_addr i2c device address
  75. * @param clk_speed device specified clock frequency the i2c_bus will switch to during each transfer. 0 if use current bus speed.
  76. * @return i2c_bus_device_handle_t return a device handle if created successfully, return NULL if failed.
  77. */
  78. i2c_bus_device_handle_t i2c_bus_device_create(i2c_bus_handle_t bus_handle, uint8_t dev_addr, uint32_t clk_speed);
  79. /**
  80. * @brief Delete and release the I2C device resource, i2c_bus_device_delete should be used in pairs with i2c_bus_device_create.
  81. *
  82. * @param p_dev_handle Point to the I2C device handle, if delete succeed handle will set to NULL.
  83. * @return
  84. * - ESP_OK Success
  85. * - ESP_FAIL Fail
  86. */
  87. esp_err_t i2c_bus_device_delete(i2c_bus_device_handle_t *p_dev_handle);
  88. /**
  89. * @brief Get device's I2C address
  90. *
  91. * @param dev_handle I2C device handle
  92. * @return uint8_t I2C address, return NULL_I2C_DEV_ADDR if dev_handle is invalid.
  93. */
  94. uint8_t i2c_bus_device_get_address(i2c_bus_device_handle_t dev_handle);
  95. /**
  96. * @brief Read single byte from i2c device with 8-bit internal register/memory address
  97. *
  98. * @param dev_handle I2C device handle
  99. * @param mem_address The internal reg/mem address to read from, set to NULL_I2C_MEM_ADDR if no internal address.
  100. * @param data Pointer to a buffer to save the data that was read
  101. * @return esp_err_t
  102. * - ESP_OK Success
  103. * - ESP_ERR_INVALID_ARG Parameter error
  104. * - ESP_FAIL Sending command error, slave doesn't ACK the transfer.
  105. * - ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.
  106. * - ESP_ERR_TIMEOUT Operation timeout because the bus is busy.
  107. */
  108. esp_err_t i2c_bus_read_byte(i2c_bus_device_handle_t dev_handle, uint8_t mem_address, uint8_t *data);
  109. /**
  110. * @brief Read multiple bytes from i2c device with 8-bit internal register/memory address.
  111. * If internal reg/mem address is 16-bit, please refer i2c_bus_read_reg16
  112. *
  113. * @param dev_handle I2C device handle
  114. * @param mem_address The internal reg/mem address to read from, set to NULL_I2C_MEM_ADDR if no internal address.
  115. * @param data_len Number of bytes to read
  116. * @param data Pointer to a buffer to save the data that was read
  117. * @return esp_err_t
  118. * - ESP_OK Success
  119. * - ESP_ERR_INVALID_ARG Parameter error
  120. * - ESP_FAIL Sending command error, slave doesn't ACK the transfer.
  121. * - ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.
  122. * - ESP_ERR_TIMEOUT Operation timeout because the bus is busy.
  123. */
  124. esp_err_t i2c_bus_read_bytes(i2c_bus_device_handle_t dev_handle, uint8_t mem_address, size_t data_len, uint8_t *data);
  125. /**
  126. * @brief Read single bit of a byte from i2c device with 8-bit internal register/memory address
  127. *
  128. * @param dev_handle I2C device handle
  129. * @param mem_address The internal reg/mem address to read from, set to NULL_I2C_MEM_ADDR if no internal address.
  130. * @param bit_num The bit number 0 - 7 to read
  131. * @param data Pointer to a buffer to save the data that was read. *data == 0 -> bit = 0, *data !=0 -> bit = 1.
  132. * @return esp_err_t
  133. * - ESP_OK Success
  134. * - ESP_ERR_INVALID_ARG Parameter error
  135. * - ESP_FAIL Sending command error, slave doesn't ACK the transfer.
  136. * - ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.
  137. * - ESP_ERR_TIMEOUT Operation timeout because the bus is busy.
  138. */
  139. esp_err_t i2c_bus_read_bit(i2c_bus_device_handle_t dev_handle, uint8_t mem_address, uint8_t bit_num, uint8_t *data);
  140. /**
  141. * @brief Read multiple bits of a byte from i2c device with 8-bit internal register/memory address
  142. *
  143. * @param dev_handle I2C device handle
  144. * @param mem_address The internal reg/mem address to read from, set to NULL_I2C_MEM_ADDR if no internal address.
  145. * @param bit_start The bit to start from, 0 - 7, MSB at 0
  146. * @param length The number of bits to read, 1 - 8
  147. * @param data Pointer to a buffer to save the data that was read
  148. * @return esp_err_t
  149. * - ESP_OK Success
  150. * - ESP_ERR_INVALID_ARG Parameter error
  151. * - ESP_FAIL Sending command error, slave doesn't ACK the transfer.
  152. * - ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.
  153. * - ESP_ERR_TIMEOUT Operation timeout because the bus is busy.
  154. */
  155. esp_err_t i2c_bus_read_bits(i2c_bus_device_handle_t dev_handle, uint8_t mem_address, uint8_t bit_start, uint8_t length, uint8_t *data);
  156. /**
  157. * @brief Write single byte to i2c device with 8-bit internal register/memory address
  158. *
  159. * @param dev_handle I2C device handle
  160. * @param mem_address The internal reg/mem address to write to, set to NULL_I2C_MEM_ADDR if no internal address.
  161. * @param data The byte to write.
  162. * @return esp_err_t
  163. * - ESP_OK Success
  164. * - ESP_ERR_INVALID_ARG Parameter error
  165. * - ESP_FAIL Sending command error, slave doesn't ACK the transfer.
  166. * - ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.
  167. * - ESP_ERR_TIMEOUT Operation timeout because the bus is busy.
  168. */
  169. esp_err_t i2c_bus_write_byte(i2c_bus_device_handle_t dev_handle, uint8_t mem_address, uint8_t data);
  170. /**
  171. * @brief Write multiple byte to i2c device with 8-bit internal register/memory address
  172. * If internal reg/mem address is 16-bit, please refer i2c_bus_write_reg16
  173. *
  174. * @param dev_handle I2C device handle
  175. * @param mem_address The internal reg/mem address to write to, set to NULL_I2C_MEM_ADDR if no internal address.
  176. * @param data_len Number of bytes to write
  177. * @param data Pointer to the bytes to write.
  178. * @return esp_err_t
  179. * - ESP_OK Success
  180. * - ESP_ERR_INVALID_ARG Parameter error
  181. * - ESP_FAIL Sending command error, slave doesn't ACK the transfer.
  182. * - ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.
  183. * - ESP_ERR_TIMEOUT Operation timeout because the bus is busy.
  184. */
  185. esp_err_t i2c_bus_write_bytes(i2c_bus_device_handle_t dev_handle, uint8_t mem_address, size_t data_len, const uint8_t *data);
  186. /**
  187. * @brief Write single bit of a byte to an i2c device with 8-bit internal register/memory address
  188. *
  189. * @param dev_handle I2C device handle
  190. * @param mem_address The internal reg/mem address to write to, set to NULL_I2C_MEM_ADDR if no internal address.
  191. * @param bit_num The bit number 0 - 7 to write
  192. * @param data The bit to write, data == 0 means set bit = 0, data !=0 means set bit = 1.
  193. * @return esp_err_t
  194. * - ESP_OK Success
  195. * - ESP_ERR_INVALID_ARG Parameter error
  196. * - ESP_FAIL Sending command error, slave doesn't ACK the transfer.
  197. * - ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.
  198. * - ESP_ERR_TIMEOUT Operation timeout because the bus is busy.
  199. */
  200. esp_err_t i2c_bus_write_bit(i2c_bus_device_handle_t dev_handle, uint8_t mem_address, uint8_t bit_num, uint8_t data);
  201. /**
  202. * @brief Write multiple bits of a byte to an i2c device with 8-bit internal register/memory address
  203. *
  204. * @param dev_handle I2C device handle
  205. * @param mem_address The internal reg/mem address to write to, set to NULL_I2C_MEM_ADDR if no internal address.
  206. * @param bit_start The bit to start from, 0 - 7, MSB at 0
  207. * @param length The number of bits to write, 1 - 8
  208. * @param data The bits to write.
  209. * @return esp_err_t
  210. * - ESP_OK Success
  211. * - ESP_ERR_INVALID_ARG Parameter error
  212. * - ESP_FAIL Sending command error, slave doesn't ACK the transfer.
  213. * - ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.
  214. * - ESP_ERR_TIMEOUT Operation timeout because the bus is busy.
  215. */
  216. esp_err_t i2c_bus_write_bits(i2c_bus_device_handle_t dev_handle, uint8_t mem_address, uint8_t bit_start, uint8_t length, uint8_t data);
  217. /**************************************** Public Functions (Low level)*********************************************/
  218. /**
  219. * @brief I2C master send queued commands create by ``i2c_cmd_link_create`` .
  220. * This function will trigger sending all queued commands.
  221. * The task will be blocked until all the commands have been sent out.
  222. * If I2C_BUS_DYNAMIC_CONFIG enable, i2c_bus will dynamically check configs and re-install i2c driver before each transfer,
  223. * hence multiple devices with different configs on a single bus can be supported.
  224. * @note
  225. * Only call this function when ``i2c_bus_read/write_xx`` do not meet the requirements
  226. *
  227. * @param dev_handle I2C device handle
  228. * @param cmd I2C command handler
  229. * @return esp_err_t
  230. * - ESP_OK Success
  231. * - ESP_ERR_INVALID_ARG Parameter error
  232. * - ESP_FAIL Sending command error, slave doesn't ACK the transfer.
  233. * - ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.
  234. * - ESP_ERR_TIMEOUT Operation timeout because the bus is busy.
  235. */
  236. esp_err_t i2c_bus_cmd_begin(i2c_bus_device_handle_t dev_handle, i2c_cmd_handle_t cmd);
  237. /**
  238. * @brief Write date to an i2c device with 16-bit internal reg/mem address
  239. *
  240. * @param dev_handle I2C device handle
  241. * @param mem_address The internal 16-bit reg/mem address to write to, set to NULL_I2C_MEM_ADDR if no internal address.
  242. * @param data_len Number of bytes to write
  243. * @param data Pointer to the bytes to write.
  244. * @return esp_err_t
  245. * - ESP_OK Success
  246. * - ESP_ERR_INVALID_ARG Parameter error
  247. * - ESP_FAIL Sending command error, slave doesn't ACK the transfer.
  248. * - ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.
  249. * - ESP_ERR_TIMEOUT Operation timeout because the bus is busy.
  250. */
  251. esp_err_t i2c_bus_write_reg16(i2c_bus_device_handle_t dev_handle, uint16_t mem_address, size_t data_len, const uint8_t *data);
  252. /**
  253. * @brief Read date from i2c device with 16-bit internal reg/mem address
  254. *
  255. * @param dev_handle I2C device handle
  256. * @param mem_address The internal 16-bit reg/mem address to read from, set to NULL_I2C_MEM_ADDR if no internal address.
  257. * @param data_len Number of bytes to read
  258. * @param data Pointer to a buffer to save the data that was read
  259. * @return esp_err_t
  260. * - ESP_OK Success
  261. * - ESP_ERR_INVALID_ARG Parameter error
  262. * - ESP_FAIL Sending command error, slave doesn't ACK the transfer.
  263. * - ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.
  264. * - ESP_ERR_TIMEOUT Operation timeout because the bus is busy.
  265. */
  266. esp_err_t i2c_bus_read_reg16(i2c_bus_device_handle_t dev_handle, uint16_t mem_address, size_t data_len, uint8_t *data);
  267. #ifdef __cplusplus
  268. }
  269. #endif
  270. #endif