HAL_i2c.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /**
  2. ******************************************************************************
  3. * @file HAL_i2c.h
  4. * @author AE Team
  5. * @version V1.1.0
  6. * @date 28/08/2019
  7. * @brief This file contains all the functions prototypes for the I2C firmware
  8. * library.
  9. ******************************************************************************
  10. * @copy
  11. *
  12. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  13. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  14. * TIME. AS A RESULT, MindMotion SHALL NOT BE HELD LIABLE FOR ANY
  15. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  16. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  17. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  18. *
  19. * <h2><center>&copy; COPYRIGHT 2019 MindMotion</center></h2>
  20. */
  21. /* Define to prevent recursive inclusion -------------------------------------*/
  22. #ifndef __HAL_I2C_H
  23. #define __HAL_I2C_H
  24. /* Includes ------------------------------------------------------------------*/
  25. #include "HAL_device.h"
  26. /** @addtogroup StdPeriph_Driver
  27. * @{
  28. */
  29. /** @addtogroup I2C
  30. * @{
  31. */
  32. /** @defgroup I2C_Exported_Types
  33. * @{
  34. */
  35. /**
  36. * @brief I2C Init structure definition
  37. */
  38. /*
  39. typedef struct
  40. {
  41. uint16_t I2C_Mode;
  42. uint16_t I2C_DutyCycle;
  43. uint16_t I2C_OwnAddress1;
  44. uint16_t I2C_Ack;
  45. uint16_t I2C_AcknowledgedAddress;
  46. uint32_t I2C_ClockSpeed;
  47. }I2C_InitTypeDef;
  48. */
  49. typedef struct
  50. {
  51. uint16_t I2C_Mode;
  52. uint16_t I2C_Speed;
  53. uint16_t I2C_OwnAddress;
  54. uint32_t I2C_ClockSpeed;
  55. } I2C_InitTypeDef;
  56. /**
  57. * @}
  58. */
  59. /** @defgroup I2C_Exported_Constants
  60. * @{
  61. */
  62. #define IS_I2C_ALL_PERIPH(PERIPH) (((*(uint32_t*)&(PERIPH)) == I2C1_BASE) || \
  63. ((*(uint32_t*)&(PERIPH)) == I2C2_BASE))
  64. /** @defgroup I2C_modes
  65. * @{
  66. */
  67. #define TX_EMPTY_CTRL (0x0001<<8)
  68. #define IC_SLAVE_DISABLE (0x0001<<6)
  69. #define IC_SLAVE_ENABLE (0x0000<<6)
  70. #define IC_RESTART_EN (0x0001<<5)
  71. #define IC_7BITADDR_MASTER (0x0000<<4)
  72. #define IC_7BITADDR_SLAVE (0x0000<<3)
  73. #define I2C_Speed_STANDARD ((uint16_t)0x0002)
  74. #define I2C_Speed_FAST ((uint16_t)0x0004)
  75. #define I2C_Mode_MASTER ((uint16_t)0x0001)
  76. #define I2C_Mode_SLAVE ((uint16_t)0x0000)
  77. #define TDMAE_SET ((uint16_t)0x0002)
  78. #define RDMAE_SET ((uint16_t)0x0001)
  79. #define CMD_READ ((uint16_t)0x0100)
  80. #define CMD_WRITE ((uint16_t)0x0000)
  81. #define I2C_Mode_I2C ((uint16_t)0x0000)
  82. #define IS_I2C_MODE(MODE) (((MODE) == I2C_Mode_I2C))
  83. /**
  84. * @}
  85. */
  86. /** @defgroup I2C_transfer_direction
  87. * @{
  88. */
  89. #define I2C_Direction_Transmitter ((uint8_t)0x00)
  90. #define I2C_Direction_Receiver ((uint8_t)0x01)
  91. #define IS_I2C_DIRECTION(DIRECTION) (((DIRECTION) == I2C_Direction_Transmitter) || \
  92. ((DIRECTION) == I2C_Direction_Receiver))
  93. /**
  94. * @}
  95. */
  96. /** @defgroup I2C_acknowledged_address_defines
  97. * @{
  98. */
  99. #define I2C_AcknowledgedAddress_7bit ((uint16_t)0x4000)
  100. #define I2C_AcknowledgedAddress_10bit ((uint16_t)0xC000)
  101. #define IS_I2C_ACKNOWLEDGE_ADDRESS(ADDRESS) (((ADDRESS) == I2C_AcknowledgedAddress_7bit) || \
  102. ((ADDRESS) == I2C_AcknowledgedAddress_10bit))
  103. /**
  104. * @}
  105. */
  106. /** @defgroup I2C_interrupts_definition
  107. * @{
  108. */
  109. #define IS_I2C_CONFIG_IT(IT) ((((IT) & (uint16_t)0xF8FF) == 0x00) && ((IT) != 0x00))
  110. /**
  111. * @}
  112. */
  113. /** @defgroup I2C_interrupts_definition
  114. * @{
  115. */
  116. #define I2C_IT_RX_UNDER ((uint16_t)0x0001)
  117. #define I2C_IT_RX_OVER ((uint16_t)0x0002)
  118. #define I2C_IT_RX_FULL ((uint16_t)0x0004)
  119. #define I2C_IT_TX_OVER ((uint16_t)0x0008)
  120. #define I2C_IT_TX_EMPTY ((uint16_t)0x0010)
  121. #define I2C_IT_RD_REQ ((uint16_t)0x0020)
  122. #define I2C_IT_TX_ABRT ((uint16_t)0x0040)
  123. #define I2C_IT_RX_DONE ((uint16_t)0x0080)
  124. #define I2C_IT_ACTIVITY ((uint16_t)0x0100)
  125. #define I2C_IT_STOP_DET ((uint16_t)0x0200)
  126. #define I2C_IT_START_DET ((uint16_t)0x0400)
  127. #define I2C_IT_GEN_CALL ((uint16_t)0x0800)
  128. #define IS_I2C_CLEAR_IT(IT) ((((IT) & (uint16_t)0xF000) == 0x00) && ((IT) != (uint16_t)0x00))
  129. #define IS_I2C_GET_IT(IT) (((IT) == I2C_IT_RX_UNDER) || ((IT) == I2C_IT_RX_OVER) || \
  130. ((IT) == I2C_IT_RX_FULL) || ((IT) == I2C_IT_TX_OVER) || \
  131. ((IT) == I2C_IT_TX_EMPTY) || ((IT) == I2C_IT_RD_REQ) || \
  132. ((IT) == I2C_IT_TX_ABRT) || ((IT) == I2C_IT_RX_DONE) || \
  133. ((IT) == I2C_IT_ACTIVITY) || ((IT) == I2C_IT_STOP_DET) || \
  134. ((IT) == I2C_IT_START_DET) || ((IT) == I2C_IT_GEN_CALL))
  135. /**
  136. * @}
  137. */
  138. /** @defgroup I2C_flags_definition
  139. * @{
  140. */
  141. #define I2C_FLAG_RX_UNDER ((uint16_t)0x0001)
  142. #define I2C_FLAG_RX_OVER ((uint16_t)0x0002)
  143. #define I2C_FLAG_RX_FULL ((uint16_t)0x0004)
  144. #define I2C_FLAG_TX_OVER ((uint16_t)0x0008)
  145. #define I2C_FLAG_TX_EMPTY ((uint16_t)0x0010)
  146. #define I2C_FLAG_RD_REQ ((uint16_t)0x0020)
  147. #define I2C_FLAG_TX_ABRT ((uint16_t)0x0040)
  148. #define I2C_FLAG_RX_DONE ((uint16_t)0x0080)
  149. #define I2C_FLAG_ACTIVITY ((uint16_t)0x0100)
  150. #define I2C_FLAG_STOP_DET ((uint16_t)0x0200)
  151. #define I2C_FLAG_START_DET ((uint16_t)0x0400)
  152. #define I2C_FLAG_GEN_CALL ((uint16_t)0x0800)
  153. #define IS_I2C_CLEAR_FLAG(FLAG) ((((FLAG) & (uint16_t)0xF000) == 0x00) && ((FLAG) != (uint16_t)0x00))
  154. #define IS_I2C_GET_FLAG(FLAG) (((FLAG) == I2C_FLAG_RX_UNDER) || ((FLAG) == I2C_FLAG_RX_OVER) || \
  155. ((FLAG) == I2C_FLAG_RX_FULL) || ((FLAG) == I2C_FLAG_TX_OVER) || \
  156. ((FLAG) == I2C_FLAG_TX_EMPTY) || ((FLAG) == I2C_FLAG_RD_REQ) || \
  157. ((FLAG) == I2C_FLAG_TX_ABRT) || ((FLAG) == I2C_FLAG_RX_DONE) || \
  158. ((FLAG) == I2C_FLAG_ACTIVITY) || ((FLAG) == I2C_FLAG_STOP_DET) || \
  159. ((FLAG) == I2C_FLAG_START_DET) || ((FLAG) == I2C_FLAG_GEN_CALL))
  160. /** @defgroup I2C_Statusflags_definition
  161. * @{
  162. */
  163. #define I2C_STATUS_FLAG_ACTIVITY ((uint16_t)0x8001)
  164. #define I2C_STATUS_FLAG_TFNF ((uint16_t)0x8002)
  165. #define I2C_STATUS_FLAG_TFE ((uint16_t)0x8004)
  166. #define I2C_STATUS_FLAG_RFNE ((uint16_t)0x8008)
  167. #define I2C_STATUS_FLAG_RFF ((uint16_t)0x8010)
  168. #define I2C_STATUS_FLAG_M_ACTIVITY ((uint16_t)0x8020)
  169. #define I2C_STATUS_FLAG_S_ACTIVITY ((uint16_t)0x8040)
  170. /**
  171. * @}
  172. */
  173. /** @defgroup I2C_Events
  174. * @{
  175. */
  176. #define I2C_EVENT_RX_UNDER ((uint32_t)0x0001)
  177. #define I2C_EVENT_RX_OVER ((uint32_t)0x0002)
  178. #define I2C_EVENT_RX_FULL ((uint32_t)0x0004)
  179. #define I2C_EVENT_TX_OVER ((uint32_t)0x0008)
  180. #define I2C_EVENT_TX_EMPTY ((uint32_t)0x0010)
  181. #define I2C_EVENT_RD_REQ ((uint32_t)0x0020)
  182. #define I2C_EVENT_TX_ABRT ((uint32_t)0x0040)
  183. #define I2C_EVENT_RX_DONE ((uint32_t)0x0080)
  184. #define I2C_EVENT_ACTIVITY ((uint32_t)0x0100)
  185. #define I2C_EVENT_STOP_DET ((uint32_t)0x0200)
  186. #define I2C_EVENT_START_DET ((uint32_t)0x0400)
  187. #define I2C_EVENT_GEN_CALL ((uint32_t)0x0800)
  188. #define IS_I2C_EVENT(EVENT) (((EVENT) == I2C_EVENT_RX_UNDER) || \
  189. ((EVENT) == I2C_EVENT_RX_OVER) || \
  190. ((EVENT) == I2C_EVENT_RX_FULL) || \
  191. ((EVENT) == I2C_EVENT_TX_OVER) || \
  192. ((EVENT) == I2C_EVENT_RD_REQ) || \
  193. ((EVENT) == I2C_EVENT_TX_ABRT) || \
  194. ((EVENT) == I2C_EVENT_RX_DONE) || \
  195. ((EVENT) == (I2C_EVENT_ACTIVITY | I2C_EVENT_STOP_DET)) || \
  196. ((EVENT) == (I2C_EVENT_START_DET | I2C_EVENT_GEN_CALL)))
  197. /**
  198. * @}
  199. */
  200. /** @defgroup I2C_own_address1
  201. * @{
  202. */
  203. #define IS_I2C_OWN_ADDRESS1(ADDRESS1) ((ADDRESS1) <= 0x3FF)
  204. /**
  205. * @}
  206. */
  207. /** @defgroup I2C_clock_speed
  208. * @{
  209. */
  210. #define IS_I2C_CLOCK_SPEED(SPEED) (((SPEED) >= 0x1) && ((SPEED) <= 400000))
  211. /**
  212. * @}
  213. */
  214. /**
  215. * @}
  216. */
  217. /** @defgroup I2C_Exported_Macros
  218. * @{
  219. */
  220. /**
  221. * @}
  222. */
  223. /** @defgroup I2C_Exported_Functions
  224. * @{
  225. */
  226. void I2C_DeInit(I2C_TypeDef* I2Cx);
  227. void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct);
  228. void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct);
  229. void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  230. void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  231. void I2C_DMALastTransferCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  232. void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState);
  233. void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState);
  234. void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint8_t Address);
  235. void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  236. void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  237. void I2C_ITConfig(I2C_TypeDef* I2Cx, uint16_t I2C_IT, FunctionalState NewState);
  238. void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data);
  239. void I2C_SendLastDataOrStop(I2C_TypeDef* I2Cx, uint8_t Data);
  240. void I2C_ReadCmd(I2C_TypeDef* I2Cx);
  241. uint8_t I2C_ReadLastDataOrStop(I2C_TypeDef* I2Cx);
  242. uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx);
  243. void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction);
  244. uint16_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register);
  245. uint32_t I2C_GetLastEvent(I2C_TypeDef* I2Cx);
  246. ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT);
  247. FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);
  248. void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);
  249. ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
  250. void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
  251. #endif /*__HAL_I2C_H */
  252. /**
  253. * @}
  254. */
  255. /**
  256. * @}
  257. */
  258. /**
  259. * @}
  260. */
  261. /*-------------------------(C) COPYRIGHT 2019 MindMotion ----------------------*/