hosal_i2c.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (c) 2016-2022 Bouffalolab.
  3. *
  4. * This file is part of
  5. * *** Bouffalolab Software Dev Kit ***
  6. * (see www.bouffalolab.com).
  7. *
  8. * Redistribution and use in source and binary forms, with or without modification,
  9. * are permitted provided that the following conditions are met:
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * 3. Neither the name of Bouffalo Lab nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  25. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef __HOSAL_I2C_H_
  31. #define __HOSAL_I2C_H_
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /** @addtogroup hosal_i2c I2C
  36. * HOSAL I2C API
  37. *
  38. * @{
  39. */
  40. #include <stdint.h>
  41. #define HOSAL_WAIT_FOREVER 0xFFFFFFFFU /**< @brief i2c wait time */
  42. #define HOSAL_I2C_MODE_MASTER 1 /**< @brief i2c communication is master mode */
  43. #define HOSAL_I2C_MODE_SLAVE 2 /**< @brief i2c communication is slave mode */
  44. #define HOSAL_I2C_MEM_ADDR_SIZE_8BIT 1 /**< @brief i2c memory address size 8bit */
  45. #define HOSAL_I2C_MEM_ADDR_SIZE_16BIT 2 /**< @brief i2c memory address size 16bit */
  46. #define HOSAL_I2C_MEM_ADDR_SIZE_24BIT 3 /**< @brief i2c memory address size 24bit */
  47. #define HOSAL_I2C_MEM_ADDR_SIZE_32BIT 4 /**< @brief i2c memory address size 32bit */
  48. #define HOSAL_I2C_ADDRESS_WIDTH_7BIT 0 /**< @brief 7 bit mode */
  49. #define HOSAL_I2C_ADDRESS_WIDTH_10BIT 1 /**< @brief 10 bit mode */
  50. /**
  51. * @brief I2C configuration
  52. */
  53. typedef struct {
  54. uint32_t address_width; /**< @brief Addressing mode: 7 bit or 10 bit */
  55. uint32_t freq; /**< @brief CLK freq */
  56. uint8_t scl; /**< @brief i2c clk pin */
  57. uint8_t sda; /**< @brief i2c data pin */
  58. uint8_t mode; /**< @brief master or slave mode */
  59. } hosal_i2c_config_t;
  60. /**
  61. * @brief I2C device type
  62. */
  63. typedef struct {
  64. uint8_t port; /**< @brief i2c port */
  65. hosal_i2c_config_t config; /**< @brief i2c config */
  66. void *priv; /**< @brief priv data */
  67. } hosal_i2c_dev_t;
  68. /**
  69. * @brief Initialises an I2C interface
  70. *
  71. * @param[in] i2c the device for which the i2c port should be initialised
  72. *
  73. * @return
  74. * - 0 on success
  75. * - EIO if an error occurred with any step
  76. */
  77. int hosal_i2c_init(hosal_i2c_dev_t *i2c);
  78. /**
  79. * @brief I2c master send
  80. *
  81. * @param[in] i2c the i2c device
  82. * @param[in] dev_addr device address
  83. * @param[in] data i2c send data
  84. * @param[in] size i2c send data size
  85. * @param[in] timeout timeout in milisecond, set this value to HAL_WAIT_FOREVER
  86. * if you want to wait forever
  87. *
  88. * @return
  89. * - 0 on success
  90. * - EIO if an error occurred with any step
  91. */
  92. int hosal_i2c_master_send(hosal_i2c_dev_t *i2c, uint16_t dev_addr, const uint8_t *data,
  93. uint16_t size, uint32_t timeout);
  94. /**
  95. * @brief I2c master recv
  96. *
  97. * @param[in] i2c the i2c device
  98. * @param[in] dev_addr device address
  99. * @param[out] data i2c receive data
  100. * @param[in] size i2c receive data size
  101. * @param[in] timeout timeout in milisecond, set this value to HAL_WAIT_FOREVER
  102. * if you want to wait forever
  103. *
  104. * @return
  105. * - 0 on success
  106. * - EIO if an error occurred with any step
  107. */
  108. int hosal_i2c_master_recv(hosal_i2c_dev_t *i2c, uint16_t dev_addr, uint8_t *data,
  109. uint16_t size, uint32_t timeout);
  110. /**
  111. * @brief I2c slave send
  112. *
  113. * @param[in] i2c the i2c device
  114. * @param[in] data i2c slave send data
  115. * @param[in] size i2c slave send data size
  116. * @param[in] timeout timeout in milisecond, set this value to HAL_WAIT_FOREVER
  117. * if you want to wait forever
  118. *
  119. * @return
  120. * - 0 on success
  121. * - EIO if an error occurred with any step
  122. */
  123. int hosal_i2c_slave_send(hosal_i2c_dev_t *i2c, const uint8_t *data, uint16_t size, uint32_t timeout);
  124. /**
  125. * @brief I2c slave receive
  126. *
  127. * @param[in] i2c tthe i2c device
  128. * @param[out] data i2c slave receive data
  129. * @param[in] size i2c slave receive data size
  130. * @param[in] timeout timeout in milisecond, set this value to HAL_WAIT_FOREVER
  131. * if you want to wait forever
  132. *
  133. * @return
  134. * - 0 on success
  135. * - EIO if an error occurred with any step
  136. */
  137. int hosal_i2c_slave_recv(hosal_i2c_dev_t *i2c, uint8_t *data, uint16_t size, uint32_t timeout);
  138. /**
  139. * @brief I2c mem write
  140. *
  141. * @param[in] i2c the i2c device
  142. * @param[in] dev_addr device address
  143. * @param[in] mem_addr mem address
  144. * @param[in] mem_addr_size mem address
  145. * @param[in] data i2c master send data
  146. * @param[in] size i2c master send data size
  147. * @param[in] timeout timeout in milisecond, set this value to HAL_WAIT_FOREVER
  148. * if you want to wait forever
  149. *
  150. * @return
  151. * - 0 on success
  152. * - EIO if an error occurred with any step
  153. */
  154. int hosal_i2c_mem_write(hosal_i2c_dev_t *i2c, uint16_t dev_addr, uint32_t mem_addr,
  155. uint16_t mem_addr_size, const uint8_t *data, uint16_t size,
  156. uint32_t timeout);
  157. /**
  158. * @brief I2c master mem read
  159. *
  160. * @param[in] i2c the i2c device
  161. * @param[in] dev_addr device address
  162. * @param[in] mem_addr mem address
  163. * @param[in] mem_addr_size mem address
  164. * @param[out] data i2c master send data
  165. * @param[in] size i2c master send data size
  166. * @param[in] timeout timeout in milisecond, set this value to HAL_WAIT_FOREVER
  167. * if you want to wait forever
  168. *
  169. * @return
  170. * - 0 on success
  171. * - EIO if an error occurred with any step
  172. */
  173. int hosal_i2c_mem_read(hosal_i2c_dev_t *i2c, uint16_t dev_addr, uint32_t mem_addr,
  174. uint16_t mem_addr_size, uint8_t *data, uint16_t size,
  175. uint32_t timeout);
  176. /**
  177. * @brief Deinitialises an I2C device
  178. *
  179. * @param[in] i2c the i2c device
  180. *
  181. * @return
  182. * - 0 on success
  183. * - EIO if an error occurred with any step
  184. */
  185. int hosal_i2c_finalize(hosal_i2c_dev_t *i2c);
  186. /** @} */
  187. #ifdef __cplusplus
  188. }
  189. #endif
  190. #endif /* __HOSAL_I2C_H_ */
  191. /* end of file */