cmem7_i2c.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. *****************************************************************************
  3. * @file cmem7_i2c.h
  4. *
  5. * @brief CMEM7 I2C header file
  6. *
  7. *
  8. * @version V1.0
  9. * @date 3. September 2013
  10. *
  11. * @note
  12. *
  13. *****************************************************************************
  14. * @attention
  15. *
  16. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  17. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  18. * TIME. AS A RESULT, CAPITAL-MICRO SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  19. * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  20. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  21. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  22. *
  23. * <h2><center>&copy; COPYRIGHT 2013 Capital-micro </center></h2>
  24. *****************************************************************************
  25. */
  26. #ifndef __CMEM7_I2C_H
  27. #define __CMEM7_I2C_H
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. #include "cmem7.h"
  32. #include "cmem7_conf.h"
  33. #define IS_I2C_ALL_PERIPH(PERIPH) (((PERIPH) == I2C0) || \
  34. ((PERIPH) == I2C1))
  35. /** @defgroup I2C_Mode
  36. * @{
  37. */
  38. #define I2C_Mode_Slave 0
  39. #define I2C_Mode_Master 1
  40. #define IS_I2C_MODE(MODE) (((MODE) == I2C_Mode_Slave) || \
  41. ((MODE) == I2C_Mode_Master))
  42. /**
  43. * @}
  44. */
  45. /** @defgroup I2C_ADDR_WIDTH
  46. * @{
  47. */
  48. #define I2C_ADDR_WIDTH_7BIT 0
  49. #define I2C_ADDR_WIDTH_10BIT 1
  50. #define IS_I2C_ADDR_WIDTH(WIDTH) (((WIDTH) == I2C_ADDR_WIDTH_7BIT) || \
  51. ((WIDTH) == I2C_ADDR_WIDTH_10BIT))
  52. /**
  53. * @}
  54. */
  55. /** @defgroup I2C_INT
  56. * @{
  57. */
  58. #define I2C_INT_RX_FIFO_NOT_EMPTY 0x00000004 /*!< Can't be clear but read FIFO */
  59. #define I2C_INT_RD_REQUEST 0x00000020 /*!< Slave was requested to send data */
  60. #define I2C_INT_TX_ABORT 0x00000040 /*!< Error while sending data */
  61. #define I2C_INT_RX_DONE 0x00000080 /*!< Slave sent all requested data */
  62. #define I2C_INT_TX_DONE 0x00000100 /*!< Master accomplish to send all data */
  63. #define I2C_INT_ALL (I2C_INT_RX_FIFO_NOT_EMPTY | \
  64. I2C_INT_RD_REQUEST | \
  65. I2C_INT_TX_ABORT | \
  66. I2C_INT_RX_DONE | \
  67. I2C_INT_TX_DONE)
  68. #define IS_I2C_INT(INT) (((INT) != 0) && (((INT) & ~I2C_INT_ALL) == 0))
  69. /**
  70. * @}
  71. */
  72. /** @defgroup I2C_STATUS
  73. * @{
  74. */
  75. #define I2C_STATUS_RX_FIFO_NOT_EMPTY 0x00200000 /*!< Can't be clear but read FIFO */
  76. #define I2C_STATUS_RD_REQUEST 0x01000000 /*!< Slave was requested to send data */
  77. #define I2C_STATUS_TX_ABORT 0x02000000 /*!< Error while sending data */
  78. #define I2C_STATUS_RX_DONE 0x04000000 /*!< Slave sent all requested data */
  79. #define I2C_STATUS_TX_DONE 0x08000000 /*!< Master accomplish to send all data */
  80. #define I2C_STATUS_ALL (I2C_STATUS_RX_FIFO_NOT_EMPTY | \
  81. I2C_STATUS_RD_REQUEST | \
  82. I2C_STATUS_TX_ABORT | \
  83. I2C_STATUS_RX_DONE | \
  84. I2C_STATUS_TX_DONE)
  85. #define IS_I2C_STATUS(STATUS) (((STATUS) != 0) && (((STATUS) & ~I2C_STATUS_ALL) == 0))
  86. /**
  87. * @}
  88. */
  89. /**
  90. * @brief I2C timing structure
  91. */
  92. typedef struct
  93. {
  94. uint32_t I2C_Freq; /*!< I2C frquency */
  95. uint16_t I2C_TsuDat; /*!< nano second of TSU:DAT */
  96. uint16_t I2C_Tsetup; /*!< nano second of THD:STA and TSU:STO */
  97. uint16_t I2C_Tbuf; /*!< nano second of TBUF */
  98. uint16_t I2C_TsuSta; /*!< nano second of TSU:STA */
  99. BOOL I2C_SdaFilterEn; /*!< enabled flag of SDA filter */
  100. uint8_t I2C_SdaFilterSpike; /*!< spikes of SDA filter */
  101. BOOL I2C_SclFilterEn; /*!< enabled flag of SCL filter */
  102. uint8_t I2C_SclFilterSpike; /*!< spikes of SCL filter */
  103. } I2C_InitTimingDef;
  104. /**
  105. * @brief I2C initialization structure
  106. */
  107. typedef struct
  108. {
  109. uint8_t I2C_Mode; /*!< Specifies the I2C mode.
  110. This parameter can be a value of @ref I2C_mode */
  111. uint8_t I2C_AddressWidth; /*!< 7- or 10-bits width address, ref as @ref I2C_ADDR_WIDTH */
  112. uint8_t I2C_Address; /*!< 7- or 10-bits address */
  113. I2C_InitTimingDef* timing; /*!< timing structure, null if don't set */
  114. } I2C_InitTypeDef;
  115. /**
  116. * @brief I2C initialization
  117. * @note This function should be called at first before any other interfaces.
  118. * @param[in] I2Cx I2C peripheral, which is I2C0 or I2C1
  119. * @param[in] init A pointer to structure I2C_InitTypeDef
  120. * @retval None
  121. */
  122. void I2C_Init(I2C0_Type* I2Cx, I2C_InitTypeDef* I2C_Init);
  123. /**
  124. * @brief Enable or disable I2C.
  125. * @param[in] I2Cx I2C peripheral, which is I2C0 or I2C1
  126. * @param[in] Enable The bit indicates if the specific I2C is enable or not
  127. * @retval None
  128. */
  129. void I2C_Enable(I2C0_Type* I2Cx, BOOL enable);
  130. /**
  131. * @brief Enable or disable I2C interrupt.
  132. * @param[in] I2Cx I2C peripheral, which is I2C0 or I2C1
  133. * @param[in] Int interrupt mask bits, which can be the combination of @ref I2C_Int
  134. * @param[in] Enable The bit indicates if specific interrupts are enable or not
  135. * @retval None
  136. */
  137. void I2C_EnableInt(I2C0_Type* I2Cx, uint32_t Int, BOOL enable);
  138. /**
  139. * @brief Check specific interrupts are set or not
  140. * @param[in] I2Cx I2C peripheral, which is I2C0 or I2C1
  141. * @param[in] Int interrupt mask bits, which can be the combination of @ref I2C_Int
  142. * @retval BOOL The bit indicates if specific interrupts are enable or not
  143. */
  144. BOOL I2C_GetIntStatus(I2C0_Type* I2Cx, uint32_t Int);
  145. /**
  146. * @brief Clear specific interrupts
  147. * @note Specific interrupt clear will clear correspective status as well
  148. * @param[in] I2Cx I2C peripheral, which is I2C0 or I2C1
  149. * @param[in] Int interrupt mask bits, which can be the combination of @ref I2C_Int
  150. * @retval None
  151. */
  152. void I2C_ClearInt(I2C0_Type* I2Cx, uint32_t Int);
  153. /**
  154. * @brief Check specific status are set or not
  155. * @param[in] I2Cx I2C peripheral, which is I2C0 or I2C1
  156. * @param[in] Status Status mask bits, which can be the combination of @ref I2C_STATUS
  157. * @retval BOOL The bit indicates if specific status are set or not
  158. */
  159. BOOL I2C_GetStatus(I2C0_Type* I2Cx, uint32_t Status);
  160. /**
  161. * @brief Clear specific status
  162. * @note Specific status clear will clear correspective interrupt as well
  163. * @param[in] I2Cx I2C peripheral, which is I2C0 or I2C1
  164. * @param[in] Status Status mask bits, which can be the combination of @ref I2C_STATUS
  165. * @retval None
  166. */
  167. void I2C_ClearStatus(I2C0_Type* I2Cx, uint32_t Status);
  168. /**
  169. * @brief I2C send read request in master mode
  170. * @note Users must call I2C_StopReq between 2 requests
  171. * @param[in] I2Cx I2C peripheral, which is I2C0 or I2C1
  172. * @param[in] size Expected data size to be read
  173. * @retval BOOL The bit indicates if read request to be sent is valid
  174. * @see I2C_StopReq
  175. */
  176. BOOL I2C_MasterReadReq(I2C0_Type* I2Cx, uint8_t size);
  177. /**
  178. * @brief Read data from I2C
  179. * @param[in] I2Cx I2C peripheral, which is I2C0 or I2C1
  180. * @param[in] size Expected data size to be read
  181. * @param[out] Data A user-allocated buffer to fetch data to be read
  182. * @retval uint8_t Actual read data size
  183. */
  184. uint8_t I2C_ReadFifo(I2C0_Type* I2Cx, uint8_t size, uint8_t* data);
  185. /**
  186. * @brief I2C send write request in master or slave mode
  187. * @note Users must call I2C_StopReq between 2 requests
  188. * @param[in] I2Cx I2C peripheral, which is I2C0 or I2C1
  189. * @param[in] size Expected data size to be written, includes the first data
  190. * @param[in] firstData The first data to be written
  191. * @retval BOOL The bit indicates if write request to be sent is valid
  192. * @see I2C_StopReq
  193. */
  194. BOOL I2C_WriteReq(I2C0_Type* I2Cx, uint8_t size, uint8_t firstData);
  195. /**
  196. * @brief Write data to I2C
  197. * @param[in] I2Cx I2C peripheral, which is I2C0 or I2C1
  198. * @param[in] size Expected data size to be written
  199. * @param[in] Data A pointer to the data to be written
  200. * @retval uint8_t Actual written data size
  201. */
  202. uint8_t I2C_WriteFifo(I2C0_Type* I2Cx, uint8_t size, uint8_t* data);
  203. /**
  204. * @brief I2C stop request
  205. * @note Users must call I2C_StopReq between 2 requests
  206. * @param[in] I2Cx I2C peripheral, which is I2C0 or I2C1
  207. * @retval BOOL The bit indicates if request is stopped.
  208. * @see I2C_MasterReadReq I2C_WriteReq
  209. */
  210. BOOL I2C_StopReq(I2C0_Type* I2Cx);
  211. #ifdef __cplusplus
  212. }
  213. #endif
  214. #endif /*__CMEM7_I2C_H */