ql_i2c.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*================================================================
  2. Copyright (c) 2021, Quectel Wireless Solutions Co., Ltd. All rights reserved.
  3. Quectel Wireless Solutions Proprietary and Confidential.
  4. =================================================================*/
  5. /**
  6. @file
  7. ql_i2c.h
  8. @brief
  9. This file provides the definitions for i2c API functions.
  10. */
  11. /*=================================================================
  12. EDIT HISTORY FOR MODULE
  13. This section contains comments describing changes made to the module.
  14. Notice that changes are listed in reverse chronological order.
  15. WHEN WHO WHAT, WHERE, WHY
  16. ------------ ------- -------------------------------------------------------------------------------
  17. =================================================================*/
  18. #ifndef QL_I2C_H
  19. #define QL_I2C_H
  20. #include "ql_api_common.h"
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /*===========================================================================
  25. * Macro Definition
  26. ===========================================================================*/
  27. #define i2c_max_count 3
  28. #define QL_I2C_ERRCODE_BASE (QL_COMPONENT_BSP_I2C<<16)
  29. /*===========================================================================
  30. * Enum
  31. ===========================================================================*/
  32. typedef enum
  33. {
  34. STANDARD_MODE = 0, //Standard mode (100K)
  35. FAST_MODE = 1, //Fast mode (400K)
  36. } ql_i2c_mode_e;
  37. typedef enum
  38. {
  39. i2c_1 = 0, //i2c channel 1
  40. i2c_2, //i2c channel 2
  41. i2c_3, //i2c channel 3
  42. }ql_i2c_channel_e;
  43. typedef enum
  44. {
  45. QL_I2C_SUCCESS = QL_SUCCESS,
  46. QL_I2C_INIT_ERR = 1|QL_I2C_ERRCODE_BASE,
  47. QL_I2C_NOT_INIT_ERR,
  48. QL_I2C_INVALID_PARAM_ERR,
  49. QL_I2C_WRITE_ERR = 5|QL_I2C_ERRCODE_BASE,
  50. QL_I2C_READ_ERR,
  51. QL_I2C_RELEASE_ERR,
  52. QL_I2C_CREATE_MUTEX_ERR,
  53. QL_I2C_MUTEX_TIMEOUT_ERR
  54. }ql_errcode_i2c_e;
  55. /*===========================================================================
  56. * Struct
  57. ===========================================================================*/
  58. /*===========================================================================
  59. * Function
  60. ===========================================================================*/
  61. /*****************************************************************
  62. * Function: ql_I2cInit
  63. *
  64. * Description: Initialize the i2c master
  65. *
  66. * Parameters:
  67. * i2c_no [in] the i2c channel
  68. * fastMode [in] the i2c speed mode
  69. *
  70. * Return:
  71. * QL_I2C_SUCCESS
  72. * QL_I2C_INIT_ERR
  73. *
  74. *****************************************************************/
  75. ql_errcode_i2c_e ql_I2cInit(ql_i2c_channel_e i2c_no, ql_i2c_mode_e Mode);
  76. /*****************************************************************
  77. * Function: ql_I2cWrite
  78. *
  79. * Description: i2c master write
  80. *
  81. * Parameters:
  82. * i2c_no [in] the i2c channel
  83. * slave [in] the i2c slave address
  84. * addr [in] the i2c slave regiser address
  85. * data [in] the data need to be sent
  86. * length [in] the length of the data
  87. *
  88. * Return:
  89. * QL_I2C_SUCCESS
  90. * QL_I2C_WRITE_ERR
  91. *
  92. *****************************************************************/
  93. ql_errcode_i2c_e ql_I2cWrite(ql_i2c_channel_e i2c_no, uint8_t slave, uint8_t addr, uint8_t *data, uint32_t length);
  94. /*****************************************************************
  95. * Function: ql_I2cRead
  96. *
  97. * Description: i2c master read
  98. *
  99. * Parameters:
  100. * i2c_no [in] the i2c channel
  101. * slave [in] the i2c slave address
  102. * addr [in] the i2c slave regiser address
  103. * buf [out] the data that was read
  104. * length [in] the length of the data
  105. *
  106. * Return:
  107. * QL_I2C_SUCCESS
  108. * QL_I2C_READ_ERR
  109. *
  110. *****************************************************************/
  111. ql_errcode_i2c_e ql_I2cRead(ql_i2c_channel_e i2c_no, uint8_t slave, uint8_t addr, uint8_t *buf, uint32_t length);
  112. /*****************************************************************
  113. * Function: ql_I2cRelease
  114. *
  115. * Description: i2c master release
  116. *
  117. * Parameters:
  118. * i2c_no [in] the i2c channel
  119. *
  120. * Return:
  121. * QL_I2C_SUCCESS
  122. * QL_I2C_RELEASE_ERR
  123. *
  124. *****************************************************************/
  125. ql_errcode_i2c_e ql_I2cRelease(ql_i2c_channel_e i2c_no);
  126. /*****************************************************************
  127. * Function: ql_I2cWrite_16bit_addr
  128. *
  129. * Description: i2c master write
  130. *
  131. * Parameters:
  132. * i2c_no [in] the i2c channel
  133. * slave [in] the i2c slave address
  134. * addr [in] the i2c slave regiser address
  135. * data [in] the data need to be sent
  136. * length [in] the length of the data
  137. *
  138. * Return:
  139. * QL_I2C_SUCCESS
  140. * QL_I2C_WRITE_ERR
  141. *
  142. *****************************************************************/
  143. ql_errcode_i2c_e ql_I2cWrite_16bit_addr(ql_i2c_channel_e i2c_no, uint8_t slave, uint16_t addr, uint8_t *data, uint32_t length);
  144. /*****************************************************************
  145. * Function: ql_I2cRead_16bit_addr
  146. *
  147. * Description: i2c master read
  148. *
  149. * Parameters:
  150. * i2c_no [in] the i2c channel
  151. * slave [in] the i2c slave address
  152. * addr [in] the i2c slave regiser address
  153. * buf [out] the data that was read
  154. * length [in] the length of the data
  155. *
  156. * Return:
  157. * QL_I2C_SUCCESS
  158. * QL_I2C_READ_ERR
  159. *
  160. *****************************************************************/
  161. ql_errcode_i2c_e ql_I2cRead_16bit_addr(ql_i2c_channel_e i2c_no, uint8_t slave, uint16_t addr, uint8_t *buf, uint32_t length);
  162. /****** I2C's slave API 是为特定场景使用,其他场景请勿使用 ******/
  163. /****** 请在明确需求后使用以下API,以下API没有寄存器的概念 ******/
  164. /****** 根据从机地址找到设备后,直接传输data,没有寄存器的设置 ******/
  165. /****** 7bits API 是为了让slave API与其他I2C API在从机地址上保持一致 ******/
  166. // for ql_i2c_write_slave_data/ql_i2c_read_slave_data, [slave] param shift 1 bit left from the slave address.
  167. // for ql_i2c_write_slave_data_7bits/ql_i2c_read_slave_data_7bits, same as ql_I2cWrite, the [slave] param don't need left shift.
  168. #define ql_i2c_write_slave_data_7bits(i2c_no, slave, data, length) \
  169. ql_i2c_write_slave_data(i2c_no, (slave << 1), data, length)
  170. #define ql_i2c_read_slave_data_7bits(i2c_no, slave, data, length) \
  171. ql_i2c_read_slave_data(i2c_no, (slave << 1) + 1, data, length)
  172. /*****************************************************************
  173. * Function: ql_i2c_write_slave_data
  174. *
  175. * Description: i2c master write pure data
  176. *
  177. * Parameters:
  178. * i2c_no [in] the i2c channel
  179. * slave [in] the i2c slave address
  180. * notice: the value shift 1 bit left from the slave address
  181. * write operation: value no change
  182. * data [in] the data need to be sent
  183. * length [in] the length of the data
  184. *
  185. * Return:
  186. * QL_I2C_SUCCESS
  187. * QL_I2C_WRITE_ERR
  188. *
  189. *****************************************************************/
  190. ql_errcode_i2c_e ql_i2c_write_slave_data(ql_i2c_channel_e i2c_no, uint8_t slave, uint8_t *data, uint32_t length);
  191. /*****************************************************************
  192. * Function: ql_i2c_read_slave_data
  193. *
  194. * Description: i2c master read pure value
  195. *
  196. * Parameters:
  197. * i2c_no [in] the i2c channel
  198. * slave [in] the i2c slave address
  199. * notice: the value shift 1 bit left from the slave address
  200. * read operation: value need add 1
  201. * buf [out] the data that was read
  202. * length [in] the length of the data
  203. *
  204. * Return:
  205. * QL_I2C_SUCCESS
  206. * QL_I2C_READ_ERR
  207. *
  208. *****************************************************************/
  209. ql_errcode_i2c_e ql_i2c_read_slave_data(ql_i2c_channel_e i2c_no, uint8_t slave, uint8_t *buf, uint32_t length);
  210. #ifdef __cplusplus
  211. } /*"C" */
  212. #endif
  213. #endif /* QL_I2C_H */