furi_hal_i2c.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * @file furi_hal_i2c.h
  3. * I2C HAL API
  4. */
  5. #pragma once
  6. #include <stdbool.h>
  7. #include <stdint.h>
  8. #include <furi_hal_i2c_config.h>
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. /** Init I2C
  13. */
  14. void furi_hal_i2c_init();
  15. /** Acquire i2c bus handle
  16. *
  17. * @return Instance of FuriHalI2cBus
  18. */
  19. void furi_hal_i2c_acquire(FuriHalI2cBusHandle* handle);
  20. /** Release i2c bus handle
  21. *
  22. * @param bus instance of FuriHalI2cBus aquired in `furi_hal_i2c_acquire`
  23. */
  24. void furi_hal_i2c_release(FuriHalI2cBusHandle* handle);
  25. /** Perform I2C tx transfer
  26. *
  27. * @param handle pointer to FuriHalI2cBusHandle instance
  28. * @param address I2C slave address
  29. * @param data pointer to data buffer
  30. * @param size size of data buffer
  31. * @param timeout timeout in ticks
  32. *
  33. * @return true on successful transfer, false otherwise
  34. */
  35. bool furi_hal_i2c_tx(
  36. FuriHalI2cBusHandle* handle,
  37. const uint8_t address,
  38. const uint8_t* data,
  39. const uint8_t size,
  40. uint32_t timeout);
  41. /** Perform I2C rx transfer
  42. *
  43. * @param handle pointer to FuriHalI2cBusHandle instance
  44. * @param address I2C slave address
  45. * @param data pointer to data buffer
  46. * @param size size of data buffer
  47. * @param timeout timeout in ticks
  48. *
  49. * @return true on successful transfer, false otherwise
  50. */
  51. bool furi_hal_i2c_rx(
  52. FuriHalI2cBusHandle* handle,
  53. const uint8_t address,
  54. uint8_t* data,
  55. const uint8_t size,
  56. uint32_t timeout);
  57. /** Perform I2C tx and rx transfers
  58. *
  59. * @param handle pointer to FuriHalI2cBusHandle instance
  60. * @param address I2C slave address
  61. * @param tx_data pointer to tx data buffer
  62. * @param tx_size size of tx data buffer
  63. * @param rx_data pointer to rx data buffer
  64. * @param rx_size size of rx data buffer
  65. * @param timeout timeout in ticks
  66. *
  67. * @return true on successful transfer, false otherwise
  68. */
  69. bool furi_hal_i2c_trx(
  70. FuriHalI2cBusHandle* handle,
  71. const uint8_t address,
  72. const uint8_t* tx_data,
  73. const uint8_t tx_size,
  74. uint8_t* rx_data,
  75. const uint8_t rx_size,
  76. uint32_t timeout);
  77. /** Perform I2C device register read (8-bit)
  78. *
  79. * @param handle pointer to FuriHalI2cBusHandle instance
  80. * @param i2c_addr I2C slave address
  81. * @param reg_addr register address
  82. * @param data pointer to register value
  83. * @param timeout timeout in ticks
  84. *
  85. * @return true on successful transfer, false otherwise
  86. */
  87. bool furi_hal_i2c_read_reg_8(
  88. FuriHalI2cBusHandle* handle,
  89. uint8_t i2c_addr,
  90. uint8_t reg_addr,
  91. uint8_t* data,
  92. uint32_t timeout);
  93. /** Perform I2C device register read (16-bit)
  94. *
  95. * @param handle pointer to FuriHalI2cBusHandle instance
  96. * @param i2c_addr I2C slave address
  97. * @param reg_addr register address
  98. * @param data pointer to register value
  99. * @param timeout timeout in ticks
  100. *
  101. * @return true on successful transfer, false otherwise
  102. */
  103. bool furi_hal_i2c_read_reg_16(
  104. FuriHalI2cBusHandle* handle,
  105. uint8_t i2c_addr,
  106. uint8_t reg_addr,
  107. uint16_t* data,
  108. uint32_t timeout);
  109. /** Perform I2C device memory read
  110. *
  111. * @param handle pointer to FuriHalI2cBusHandle instance
  112. * @param i2c_addr I2C slave address
  113. * @param mem_addr memory start address
  114. * @param data pointer to data buffer
  115. * @param len size of data buffer
  116. * @param timeout timeout in ticks
  117. *
  118. * @return true on successful transfer, false otherwise
  119. */
  120. bool furi_hal_i2c_read_mem(
  121. FuriHalI2cBusHandle* handle,
  122. uint8_t i2c_addr,
  123. uint8_t mem_addr,
  124. uint8_t* data,
  125. uint8_t len,
  126. uint32_t timeout);
  127. /** Perform I2C device register write (8-bit)
  128. *
  129. * @param handle pointer to FuriHalI2cBusHandle instance
  130. * @param i2c_addr I2C slave address
  131. * @param reg_addr register address
  132. * @param data register value
  133. * @param timeout timeout in ticks
  134. *
  135. * @return true on successful transfer, false otherwise
  136. */
  137. bool furi_hal_i2c_write_reg_8(
  138. FuriHalI2cBusHandle* handle,
  139. uint8_t i2c_addr,
  140. uint8_t reg_addr,
  141. uint8_t data,
  142. uint32_t timeout);
  143. /** Perform I2C device register write (16-bit)
  144. *
  145. * @param handle pointer to FuriHalI2cBusHandle instance
  146. * @param i2c_addr I2C slave address
  147. * @param reg_addr register address
  148. * @param data register value
  149. * @param timeout timeout in ticks
  150. *
  151. * @return true on successful transfer, false otherwise
  152. */
  153. bool furi_hal_i2c_write_reg_16(
  154. FuriHalI2cBusHandle* handle,
  155. uint8_t i2c_addr,
  156. uint8_t reg_addr,
  157. uint16_t data,
  158. uint32_t timeout);
  159. /** Perform I2C device memory
  160. *
  161. * @param handle pointer to FuriHalI2cBusHandle instance
  162. * @param i2c_addr I2C slave address
  163. * @param mem_addr memory start address
  164. * @param data pointer to data buffer
  165. * @param len size of data buffer
  166. * @param timeout timeout in ticks
  167. *
  168. * @return true on successful transfer, false otherwise
  169. */
  170. bool furi_hal_i2c_write_mem(
  171. FuriHalI2cBusHandle* handle,
  172. uint8_t i2c_addr,
  173. uint8_t mem_addr,
  174. uint8_t* data,
  175. uint8_t len,
  176. uint32_t timeout);
  177. #ifdef __cplusplus
  178. }
  179. #endif