HAL_I2C.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. ******************************************************************************
  3. * @file HAL_I2C.h
  4. * @version V1.0.0
  5. * @date 2020
  6. * @brief Header file of I2C HAL module.
  7. ******************************************************************************
  8. */
  9. #ifndef __HAL_I2C_H__
  10. #define __HAL_I2C_H__
  11. #include "ACM32Fxx_HAL.h"
  12. /**************** Bit definition for I2C CR register ********************/
  13. #define I2C_CR_STOPF_INTEN BIT20
  14. #define I2C_CR_RX_ADDR3_INTEN BIT19
  15. #define I2C_CR_DMA_EN BIT18
  16. #define I2C_CR_TXE_SEL BIT17
  17. #define I2C_CR_MARLO_INTEN BIT16
  18. #define I2C_CR_TX_AUTO_EN BIT15
  19. #define I2C_CR_OD_MODE BIT14
  20. #define I2C_CR_RX_ADDR2_INT_EN BIT12
  21. #define I2C_CR_OVR_INT_EN BIT11
  22. #define I2C_CR_RXNE_INT_EN BIT10
  23. #define I2C_CR_TXE_INT_EN BIT9
  24. #define I2C_CR_RX_ADDR1_INT_EN BIT8
  25. #define I2C_CR_MTF_INT_EN BIT7
  26. #define I2C_CR_TACK BIT6
  27. #define I2C_CR_STOP BIT5
  28. #define I2C_CR_START BIT4
  29. #define I2C_CR_TX BIT3
  30. #define I2C_CR_MASTER BIT2
  31. #define I2C_CR_NOSTRETCH BIT1
  32. #define I2C_CR_MEN BIT0
  33. /**************** Bit definition for I2C SR register ********************/
  34. #define I2C_SR_TIMEOUTBF BIT16
  35. #define I2C_SR_TIMEOUTAF BIT15
  36. #define I2C_SR_RX_ADDR3 BIT14
  37. #define I2C_SR_RX_ADDR2 BIT12
  38. #define I2C_SR_OVR BIT11
  39. #define I2C_SR_RXNE BIT10
  40. #define I2C_SR_TXE BIT9
  41. #define I2C_SR_RX_ADDR1 BIT8
  42. #define I2C_SR_MTF BIT7
  43. #define I2C_SR_MARLO BIT6
  44. #define I2C_SR_TX_RX_FLAG BIT5
  45. #define I2C_SR_BUS_BUSY BIT4
  46. #define I2C_SR_SRW BIT3
  47. #define I2C_SR_STOPF BIT2
  48. #define I2C_SR_STARTF BIT1
  49. #define I2C_SR_RACK BIT0
  50. /************** Bit definition for I2C SLAVE ADDR2/3 register **************/
  51. #define I2C_ADDR3_EN BIT8
  52. #define I2C_ADDR2_EN BIT0
  53. /************** Bit definition for I2C TIMEOUT register **************/
  54. #define I2C_TIMEOUT_EXTEN BIT31
  55. #define I2C_TOUTB_INTEN BIT30
  56. #define I2C_EXT_MODE BIT29
  57. #define I2C_TIMEOUT_TIMOUTEN BIT15
  58. #define I2C_TOUTA_INTEN BIT14
  59. /** @defgroup I2C_MODE
  60. * @{
  61. */
  62. #define I2C_MODE_SLAVE (0U)
  63. #define I2C_MODE_MASTER (1U)
  64. /**
  65. * @}
  66. */
  67. /** @defgroup CLOCK_SPEED
  68. * @{
  69. */
  70. #define CLOCK_SPEED_STANDARD (100000U)
  71. #define CLOCK_SPEED_FAST (400000U)
  72. #define CLOCK_SPEED_FAST_PLUS (1000000U)
  73. /**
  74. * @}
  75. */
  76. /** @defgroup TX_AUTO_EN
  77. * @{
  78. */
  79. #define TX_AUTO_EN_DISABLE (0U)
  80. #define TX_AUTO_EN_ENABLE (1U)
  81. /**
  82. * @}
  83. */
  84. /** @defgroup NO_STRETCH_MODE
  85. * @{
  86. */
  87. #define NO_STRETCH_MODE_STRETCH (0U)
  88. #define NO_STRETCH_MODE_NOSTRETCH (1U)
  89. /**
  90. * @}
  91. */
  92. /** @defgroup SLAVE State machine
  93. * @{
  94. */
  95. #define SLAVE_RX_STATE_IDLE (0U)
  96. #define SLAVE_RX_STATE_RECEIVING (1U)
  97. #define SLAVE_TX_STATE_IDLE (0U)
  98. #define SLAVE_TX_STATE_SENDING (1U)
  99. /**
  100. * @}
  101. */
  102. /** @defgroup I2C_Memory_Address_Size I2C Memory Address Size
  103. * @{
  104. */
  105. #define I2C_MEMADD_SIZE_8BIT (0U)
  106. #define I2C_MEMADD_SIZE_16BIT (1U)
  107. /**
  108. * @}
  109. */
  110. /* Private macros ------------------------------------------------------------*/
  111. /** @defgroup I2C_Private_Macros I2C Private Macros
  112. * @{
  113. */
  114. #define I2C_MEM_ADD_MSB(__ADDRESS__) ((uint8_t)((uint16_t)(((uint16_t)((__ADDRESS__) & (uint16_t)0xFF00)) >> 8)))
  115. #define I2C_MEM_ADD_LSB(__ADDRESS__) ((uint8_t)((uint16_t)((__ADDRESS__) & (uint16_t)0x00FF)))
  116. /**
  117. * @brief I2C Configuration Structure definition
  118. */
  119. #define __HAL_I2C_GET_FLAG(__HANDLE__, __FLAG__) (((((__HANDLE__)->Instance->SR) & (__FLAG__)) == (__FLAG__) ) ? 1 : 0)
  120. typedef enum
  121. {
  122. RESET = 0,
  123. SET = !RESET
  124. } FlagStatus, ITStatus;
  125. typedef struct
  126. {
  127. uint32_t I2C_Mode; /* This parameter can be a value of @ref I2C_MODE */
  128. uint32_t Tx_Auto_En; /* This parameter can be a value of @ref TX_AUTO_EN */
  129. uint32_t No_Stretch_Mode; /* This parameter can be a value of @ref NO_STRETCH_MODE */
  130. uint32_t Own_Address; /* This parameter can be a 7-bit address */
  131. uint32_t Clock_Speed; /* This parameter can be a value of @ref CLOCK_SPEED */
  132. } I2C_InitTypeDef;
  133. /******************************** Check I2C Parameter *******************************/
  134. #define IS_I2C_ALL_MODE(I2C_MODE) (((I2C_MODE) == I2C_MODE_SLAVE) || \
  135. ((I2C_MODE) == I2C_MODE_MASTER))
  136. #define IS_I2C_CLOCK_SPEED(CLOCK_SPEED) (((CLOCK_SPEED) > 0U) && ((CLOCK_SPEED) <=1000000U))
  137. #define IS_I2C_TX_AUTO_EN(TX_AUTO_EN) (((TX_AUTO_EN) == TX_AUTO_EN_DISABLE) || \
  138. ((TX_AUTO_EN) == TX_AUTO_EN_ENABLE))
  139. #define IS_I2C_STRETCH_EN(STRETCH_EN) (((STRETCH_EN) == NO_STRETCH_MODE_STRETCH) || \
  140. ((STRETCH_EN) == NO_STRETCH_MODE_NOSTRETCH))
  141. /**
  142. * @brief I2C handle Structure definition
  143. */
  144. typedef struct
  145. {
  146. I2C_TypeDef *Instance; /* I2C registers base address */
  147. I2C_InitTypeDef Init; /* I2C communication parameters */
  148. uint32_t Slave_RxState; /* I2C Slave state machine */
  149. uint32_t Slave_TxState; /* I2C Slave state machine */
  150. uint8_t *Rx_Buffer; /* I2C Rx Buffer */
  151. uint8_t *Tx_Buffer; /* I2C Tx Buffer */
  152. uint32_t Rx_Size; /* I2C Rx Size */
  153. uint32_t Tx_Size; /* I2C Tx Size */
  154. uint32_t Rx_Count; /* I2C Rx Count */
  155. uint32_t Tx_Count; /* I2C Tx Count */
  156. DMA_HandleTypeDef *HDMA_Rx; /* I2C Rx DMA handle parameters */
  157. DMA_HandleTypeDef *HDMA_Tx; /* I2C Tx DMA handle parameters */
  158. void (*I2C_STOPF_Callback)(void); /* I2C STOP flag interrupt callback */
  159. }I2C_HandleTypeDef;
  160. /******************************** I2C Instances *******************************/
  161. #define IS_I2C_ALL_INSTANCE(INSTANCE) (((INSTANCE) == I2C1) || ((INSTANCE) == I2C2))
  162. /* Function : HAL_I2C_IRQHandler */
  163. void HAL_I2C_IRQHandler(I2C_HandleTypeDef *hi2c);
  164. /* Function : HAL_I2C_MspInit */
  165. void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c);
  166. /* Function : HAL_I2C_MspDeInit */
  167. void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c);
  168. /* Function : HAL_I2C_Init */
  169. HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c);
  170. /* Function : HAL_I2C_DeInit */
  171. HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hi2c);
  172. /* Function : HAL_I2C_Master_Transmit */
  173. HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  174. /* Function : HAL_I2C_Master_Receive */
  175. HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  176. /* Function : HAL_I2C_Slave_Transmit */
  177. HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint32_t Size, uint32_t Timeout);
  178. /* Function : HAL_I2C_Slave_Receive */
  179. HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint32_t Size, uint32_t Timeout);
  180. /* Function : HAL_I2C_Slave_Transmit_IT */
  181. HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint32_t Size);
  182. /* Function : HAL_I2C_Slave_Receive_IT */
  183. HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint32_t Size);
  184. /* Function : HAL_I2C_Slave_Receive_DMA */
  185. HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint32_t Size);
  186. /* Function : HAL_I2C_Slave_Transmit_DMA */
  187. HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint32_t Size);
  188. /* Function : HAL_I2C_Mem_Write */
  189. HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint8_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  190. /* Function : HAL_I2C_Mem_Read */
  191. HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint8_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  192. /* Function : HAL_I2C_GetSlaveRxState */
  193. uint8_t HAL_I2C_GetSlaveRxState(I2C_HandleTypeDef *hi2c);
  194. /* Function : HAL_I2C_GetSlaveTxState */
  195. uint8_t HAL_I2C_GetSlaveTxState(I2C_HandleTypeDef *hi2c);
  196. #endif