air105_i2c.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #ifndef __AIR105_I2C_H
  22. #define __AIR105_I2C_H
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. /* Includes ------------------------------------------------------------------*/
  27. #include "air105.h"
  28. /** @defgroup I2C_mode
  29. * @{
  30. */
  31. typedef enum
  32. {
  33. I2C_Mode_Master =((uint32_t)0x0001),
  34. I2C_Mode_Slave =((uint32_t)0x0002)
  35. }I2CMode_TypeDef;
  36. #define IS_I2C_MODE(MODE) (((MODE) == I2C_Mode_Master) || \
  37. ((MODE) == I2C_Mode_Slave))
  38. /**
  39. * @}
  40. */
  41. typedef struct
  42. {
  43. uint32_t I2C_ClockSpeed; /*!< Specifies the clock frequency. */
  44. I2CMode_TypeDef I2C_Mode;
  45. uint32_t I2C_DutyCycle; /*!< Specifies the I2C fast mode duty cycle.
  46. This parameter can be a value of @ref I2C_duty_cycle_in_fast_mode */
  47. uint32_t I2C_SDASetupTime;
  48. uint32_t I2C_SDAHoldTime;
  49. uint32_t I2C_TargetAddress;
  50. uint32_t I2C_TargetAddressMode;
  51. uint32_t I2C_OwnAddress; /*!< Specifies the slave mode own address.
  52. This parameter can be a 7-bit or 10-bit address. */
  53. uint32_t I2C_AcknowledgedAddress; /*!< Specifies if 7-bit or 10-bit address is acknowledged.
  54. This parameter can be a value of @ref I2C_acknowledged_address */
  55. uint32_t I2C_RXFIFOFullThreshold;
  56. uint32_t I2C_TXFIFOEmptyThreshold;
  57. FunctionalState I2C_GenerateRestartEnable;
  58. }I2C_InitTypeDef;
  59. typedef struct
  60. {
  61. uint32_t I2C_DMAReq;
  62. uint32_t I2C_DMAReceiveLevel;
  63. uint32_t I2C_DMATransmitLevel;
  64. FunctionalState I2C_DMAEnCmd;
  65. }I2C_DMAInitTypeDef;
  66. #define I2C_ClockSpeed_100KHz (100000)
  67. #define I2C_ClockSpeed_400KHz (400000)
  68. /** @defgroup I2C_duty_cycle_in_fast_mode
  69. * @{
  70. */
  71. /*Tlow/Thigh = ((I2C_DutyCycle_x_y & 0xFF00) >> 8) / (I2C_DutyCycle_x_y & 0x00FF)*/
  72. #define I2C_DutyCycle_1 ((uint16_t)0x0101) /*!< I2C standard mode Tlow/Thigh = 1 */
  73. #define I2C_DutyCycle_16_9 ((uint16_t)0x1009) /*!< I2C fast mode Tlow/Thigh = 16/9 */
  74. #define I2C_DutyCycle_2 ((uint16_t)0x0201) /*!< I2C fast mode Tlow/Thigh = 2 */
  75. #define I2C_DutyCycle_8_3 ((uint16_t)0x0803) /*!< I2C high mode Tlow/Thigh = 8/3 */
  76. #define IS_I2C_DUTY_CYCLE(CYCLE) (((CYCLE) == I2C_DutyCycle_16_9) || \
  77. ((CYCLE) == I2C_DutyCycle_2))
  78. /**
  79. * @}
  80. */
  81. #define IS_I2C_SDA_SETUP_TIME(TIME) ((TIME) <= 0x000000FF)
  82. #define IS_I2C_SDA_HOLD_TIME(TIME) ((TIME) <= 0x0000FFFF)
  83. /** @defgroup I2C_acknowledged_address
  84. * @{
  85. */
  86. #define I2C_AcknowledgedAddress_7bit ((uint16_t)0x0001)
  87. #define I2C_AcknowledgedAddress_10bit ((uint16_t)0x0002)
  88. #define IS_I2C_ACKNOWLEDGE_ADDRESS(ADDRESS) (((ADDRESS) == I2C_AcknowledgedAddress_7bit) || \
  89. ((ADDRESS) == I2C_AcknowledgedAddress_10bit))
  90. /**
  91. * @}
  92. */
  93. /** @defgroup Target_Address_Bit
  94. * @{
  95. */
  96. #define I2C_TargetAddressMode_7bit ((uint16_t)0x0001)
  97. #define I2C_TargetAddressMode_10bit ((uint16_t)0x0002)
  98. #define IS_I2C_TARGET_ADDRESS_MODE(ADDRESS) (((ADDRESS) == I2C_TargetAddressMode_7bit) || \
  99. ((ADDRESS) == I2C_TargetAddressMode_10bit))
  100. /**
  101. * @}
  102. */
  103. /** @defgroup SPI_RXFIFOFullThreshold
  104. * @{
  105. */
  106. #define I2C_RXFIFOFullThreshold_1 ((uint32_t)0x0000)
  107. #define I2C_RXFIFOFullThreshold_2 ((uint32_t)0x0001)
  108. #define I2C_RXFIFOFullThreshold_3 ((uint32_t)0x0002)
  109. #define I2C_RXFIFOFullThreshold_4 ((uint32_t)0x0003)
  110. #define I2C_RXFIFOFullThreshold_5 ((uint32_t)0x0004)
  111. #define I2C_RXFIFOFullThreshold_6 ((uint32_t)0x0005)
  112. #define I2C_RXFIFOFullThreshold_7 ((uint32_t)0x0006)
  113. #define I2C_RXFIFOFullThreshold_8 ((uint32_t)0x0007)
  114. #define IS_I2C_RX_FIFO_FULL_THRESHOLD(THRESHOLD) (THRESHOLD <= I2C_RXFIFOFullThreshold_8)
  115. /**
  116. * @}
  117. */
  118. /** @defgroup SPI_TXFIFOEmptyThreshold
  119. * @{
  120. */
  121. #define I2C_TXFIFOEmptyThreshold_0 ((uint32_t)0x0000)
  122. #define I2C_TXFIFOEmptyThreshold_1 ((uint32_t)0x0001)
  123. #define I2C_TXFIFOEmptyThreshold_2 ((uint32_t)0x0002)
  124. #define I2C_TXFIFOEmptyThreshold_3 ((uint32_t)0x0003)
  125. #define I2C_TXFIFOEmptyThreshold_4 ((uint32_t)0x0004)
  126. #define I2C_TXFIFOEmptyThreshold_5 ((uint32_t)0x0005)
  127. #define I2C_TXFIFOEmptyThreshold_6 ((uint32_t)0x0006)
  128. #define I2C_TXFIFOEmptyThreshold_7 ((uint32_t)0x0007)
  129. #define I2C_TXFIFOEmptyThreshold_8 ((uint32_t)0x0008)
  130. #define IS_I2C_TX_FIFO_EMPTY_THRESHOLD(THRESHOLD) (THRESHOLD <= I2C_TXFIFOEmptyThreshold_8)
  131. /**
  132. * @}
  133. */
  134. /** @defgroup I2C_DMAReceiveLevel
  135. * @{
  136. */
  137. #define I2C_DMAReceiveLevel_1 ((uint32_t)0x0000)
  138. #define I2C_DMAReceiveLevel_2 ((uint32_t)0x0001)
  139. #define I2C_DMAReceiveLevel_3 ((uint32_t)0x0002)
  140. #define I2C_DMAReceiveLevel_4 ((uint32_t)0x0003)
  141. #define I2C_DMAReceiveLevel_5 ((uint32_t)0x0004)
  142. #define I2C_DMAReceiveLevel_6 ((uint32_t)0x0005)
  143. #define I2C_DMAReceiveLevel_7 ((uint32_t)0x0006)
  144. #define I2C_DMAReceiveLevel_8 ((uint32_t)0x0007)
  145. #define IS_I2C_DMA_RECEIVE_LEVEL(LEVEL) (LEVEL <= I2C_DMAReceiveLevel_8)
  146. /**
  147. * @}
  148. */
  149. /** @defgroup I2C_DMATransmitLevel
  150. * @{
  151. */
  152. #define I2C_DMATransmitLevel_0 ((uint32_t)0x0000)
  153. #define I2C_DMATransmitLevel_1 ((uint32_t)0x0001)
  154. #define I2C_DMATransmitLevel_2 ((uint32_t)0x0002)
  155. #define I2C_DMATransmitLevel_3 ((uint32_t)0x0003)
  156. #define I2C_DMATransmitLevel_4 ((uint32_t)0x0004)
  157. #define I2C_DMATransmitLevel_5 ((uint32_t)0x0005)
  158. #define I2C_DMATransmitLevel_6 ((uint32_t)0x0006)
  159. #define I2C_DMATransmitLevel_7 ((uint32_t)0x0007)
  160. #define IS_I2C_DMA_TRANSMIT_LEVEL(LEVEL) (LEVEL <= I2C_DMATransmitLevel_7)
  161. /**
  162. * @}
  163. */
  164. /** @defgroup I2C_DMA_transfer_requests
  165. * @{
  166. */
  167. #define I2C_DMAReq_Rx ((uint32_t)0x0001)
  168. #define I2C_DMAReq_Tx ((uint32_t)0x0002)
  169. #define IS_I2C_DMAREQ(DMAREQ) (((DMAREQ) & I2C_DMAReq_Tx) || \
  170. ((DMAREQ) & I2C_DMAReq_Rx))
  171. /**
  172. * @}
  173. */
  174. #define I2C_IT_RXUDF ((uint32_t)0x0001) //clear by hardware
  175. #define I2C_IT_RXOVF ((uint32_t)0x0002) //clear by read
  176. #define I2C_IT_RXF ((uint32_t)0x0004) //clear by read
  177. #define I2C_IT_TXOVF ((uint32_t)0x0008) //clear by read
  178. #define I2C_IT_TXE ((uint32_t)0x0010) //clear by hardware
  179. #define I2C_IT_RD_REQ ((uint32_t)0x0020) //clear by read
  180. #define I2C_IT_TX_ABRT ((uint32_t)0x0040) //clear by read
  181. #define I2C_IT_RX_DONE ((uint32_t)0x0080) //clear by read
  182. #define I2C_IT_ACTIVITY ((uint32_t)0x0100) //clear by read
  183. #define I2C_IT_STOP_DET ((uint32_t)0x0200) //clear by read
  184. #define I2C_IT_START_DET ((uint32_t)0x0400)
  185. #define I2C_IT_GEN_CALL ((uint32_t)0x0800)
  186. #define I2C_IT_ALL ((uint32_t)0x0FFF)
  187. #define IS_I2C_CONFIG_IT(IT) ((((IT) & (~(uint32_t)0x0FFF)) == 0x00) && ((IT) != 0x00))
  188. #define IS_I2C_CLEAR_IT(IT) ((((IT) & (~(uint32_t)0x0FFF)) == 0x00) && ((IT) != 0x00))
  189. #define IS_I2C_GET_IT(IT) (((IT) == I2C_IT_RXUDF) || \
  190. ((IT) == I2C_IT_RXOVF) || \
  191. ((IT) == I2C_IT_RXF) || \
  192. ((IT) == I2C_IT_TXOVF) || \
  193. ((IT) == I2C_IT_TXE) || \
  194. ((IT) == I2C_IT_RD_REQ) || \
  195. ((IT) == I2C_IT_TX_ABRT) || \
  196. ((IT) == I2C_IT_RX_DONE) || \
  197. ((IT) == I2C_IT_ACTIVITY) || \
  198. ((IT) == I2C_IT_STOP_DET) || \
  199. ((IT) == I2C_IT_START_DET) || \
  200. ((IT) == I2C_IT_GEN_CALL) || \
  201. ((IT) == I2C_IT_ALL))
  202. #define I2C_FLAG_ACTIVITY ((uint32_t)0x0001)
  203. #define I2C_FLAG_TXNF ((uint32_t)0x0002)
  204. #define I2C_FLAG_TXE ((uint32_t)0x0004)
  205. #define I2C_FLAG_RXNE ((uint32_t)0x0008)
  206. #define I2C_FLAG_RXF ((uint32_t)0x0010)
  207. #define I2C_FLAG_MST_ACTIVITY ((uint32_t)0x0020)
  208. #define I2C_FLAG_SLV_ACTIVITY ((uint32_t)0x0040)
  209. #define I2C_FLAG_ALL ((uint32_t)0x007F)
  210. #define IS_I2C_GET_FLAG(FLAG) (((FLAG) == I2C_FLAG_ACTIVITY) || \
  211. ((FLAG) == I2C_FLAG_TXNF) || \
  212. ((FLAG) == I2C_FLAG_TXE) || \
  213. ((FLAG) == I2C_FLAG_RXNE) || \
  214. ((FLAG) == I2C_FLAG_RXF) || \
  215. ((FLAG) == I2C_FLAG_MST_ACTIVITY) || \
  216. ((FLAG) == I2C_FLAG_SLV_ACTIVITY) || \
  217. ((FLAG) == I2C_FLAG_ALL))
  218. #define I2C_TX_ABRT_7BIT_ADDR_NOACK ((uint32_t)0x00000001)
  219. #define I2C_TX_ABRT_10BIT_ADDR1_NOACK ((uint32_t)0x00000002)
  220. #define I2C_TX_ABRT_10BIT_ADDR2_NOACK ((uint32_t)0x00000004)
  221. #define I2C_TX_ABRT_TXDATA_NOACK ((uint32_t)0x00000008)
  222. #define I2C_TX_ABRT_GEN_CALL_NOACK ((uint32_t)0x00000010)
  223. #define I2C_TX_ABRT_GEN_CALL_READ ((uint32_t)0x00000020)
  224. #define I2C_TX_ABRT_HIGH_SPEED_ACKDET ((uint32_t)0x00000040)
  225. #define I2C_TX_ABRT_START_BYTE_ACKDET ((uint32_t)0x00000080)
  226. #define I2C_TX_ABRT_HIGH_SPEED_NORSTRT ((uint32_t)0x00000100)
  227. #define I2C_TX_ABRT_START_BYTE_NORSTRT ((uint32_t)0x00000200)
  228. #define I2C_TX_ABRT_10BIT_RD_NORSTRT ((uint32_t)0x00000400)
  229. #define I2C_TX_ABRT_LOST_ARB ((uint32_t)0x00000800)
  230. #define I2C_TX_ABRT_SLVFLUSH_TXFIFO ((uint32_t)0x00001000)
  231. #define I2C_TX_ABRT_SLV_LOST_ARB ((uint32_t)0x00002000)
  232. #define I2C_TX_ABRT_SLV_RD_INTX ((uint32_t)0x00004000)
  233. #define I2C_TX_ABRT_MASTER_DISABLE ((uint32_t)0x00008000)
  234. #define I2C_TX_ABRT_USER_ABRT ((uint32_t)0x00010000)
  235. #define I2C_TX_ABRT_TX_FLUSH_CNT ((uint32_t)0x00020000)
  236. #define IS_I2C_TX_ABRT(ABRT) (((ABRT) == I2C_TX_ABRT_7BIT_ADDR_NOACK) || \
  237. ((ABRT) == I2C_TX_ABRT_10BIT_ADDR1_NOACK) || \
  238. ((FLAG) == I2C_TX_ABRT_10BIT_ADDR2_NOACK) || \
  239. ((FLAG) == I2C_TX_ABRT_TXDATA_NOACK) || \
  240. ((FLAG) == I2C_TX_ABRT_GEN_CALL_NOACK) || \
  241. ((FLAG) == I2C_TX_ABRT_GEN_CALL_READ) || \
  242. ((FLAG) == I2C_TX_ABRT_HIGH_SPEED_ACKDET) || \
  243. ((FLAG) == I2C_TX_ABRT_START_BYTE_ACKDET) || \
  244. ((FLAG) == I2C_TX_ABRT_HIGH_SPEED_NORSTRT) || \
  245. ((FLAG) == I2C_TX_ABRT_START_BYTE_NORSTRT) || \
  246. ((FLAG) == I2C_TX_ABRT_10BIT_RD_NORSTRT) || \
  247. ((FLAG) == I2C_TX_ABRT_LOST_ARB) || \
  248. ((FLAG) == I2C_TX_ABRT_SLVFLUSH_TXFIFO) || \
  249. ((FLAG) == I2C_TX_ABRT_SLV_LOST_ARB) || \
  250. ((FLAG) == I2C_TX_ABRT_SLV_RD_INTX) || \
  251. ((FLAG) == I2C_TX_ABRT_MASTER_DISABLE) || \
  252. ((FLAG) == I2C_TX_ABRT_USER_ABRT) || \
  253. ((FLAG) == I2C_TX_ABRT_TX_FLUSH_CNT))
  254. typedef enum
  255. {
  256. I2C_DataCMD_Write = (uint32_t)0x01,
  257. I2C_DataCMD_Read = (uint32_t)0x02
  258. }I2CDataCMD_TypeDef;
  259. #define IS_I2C_DATA_CMD(CMD) (((CMD) == I2C_DataCMD_Write) || \
  260. ((CMD) == I2C_DataCMD_Read))
  261. typedef enum
  262. {
  263. I2C_DataEndCondition_None = (uint32_t)0x01,
  264. I2C_DataEndCondition_Stop = (uint32_t)0x02,
  265. I2C_DataEndCondition_Restart = (uint32_t)0x03
  266. }I2CDataEndCondition_TypeDef;
  267. #define IS_I2C_DATA_END_CONDITION(CONDITION) (((CONDITION) == I2C_DataEndCondition_None) || \
  268. ((CONDITION) == I2C_DataEndCondition_Stop) || \
  269. ((CONDITION) == I2C_DataEndCondition_Restart))
  270. void I2C_DeInit(I2C_TypeDef* I2Cx);
  271. void I2C_Init(I2C_TypeDef* I2Cx,I2C_InitTypeDef* I2C_InitStruct);
  272. void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct);
  273. void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  274. void I2C_SetTargetAddress(I2C_TypeDef* I2Cx, uint32_t TargetAddress, uint32_t TargetAddressMode);
  275. void I2C_DMAInit(I2C_TypeDef* I2Cx, I2C_DMAInitTypeDef* I2C_DMAInitStruct);
  276. void I2C_DMAStructInit(I2C_DMAInitTypeDef* I2C_DMAInitStruct);
  277. void I2C_DMACmd(I2C_TypeDef* I2Cx, uint32_t I2C_DMAReq, FunctionalState NewState);
  278. void I2C_SetSDASetupTime(I2C_TypeDef* I2Cx,uint32_t PCLKCycles);
  279. void I2C_SetSDAHoldTime(I2C_TypeDef* I2Cx,uint32_t PCLKCycles);
  280. void I2C_ITConfig(I2C_TypeDef* I2Cx,uint32_t I2C_IT, FunctionalState NewState);
  281. ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx,uint32_t I2C_IT);
  282. ITStatus I2C_GetRawITStatus(I2C_TypeDef* I2Cx,uint32_t I2C_IT);
  283. void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx,uint32_t I2C_IT);
  284. FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx,uint32_t I2C_FLAG);
  285. uint32_t I2C_GetFlagStatusReg(I2C_TypeDef* I2Cx);
  286. FlagStatus I2C_GetTXAbortSource(I2C_TypeDef* I2Cx,uint32_t I2C_TX_ABRT);
  287. uint32_t I2C_GetTXAbortSourceReg(I2C_TypeDef* I2Cx);
  288. I2CMode_TypeDef I2C_GetI2CMode(I2C_TypeDef* I2Cx);
  289. void I2C_MasterGenerateReceiveSCL(I2C_TypeDef* I2Cx, I2CDataEndCondition_TypeDef DataCondition);
  290. uint16_t I2C_ExtendData(uint8_t Data, I2CDataCMD_TypeDef DataCMD, I2CDataEndCondition_TypeDef DataCondition);
  291. void I2C_WriteDataToDR(I2C_TypeDef* I2Cx, uint16_t ExtendData);
  292. uint8_t I2C_ReadDataFromDR(I2C_TypeDef* I2Cx);
  293. void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data, I2CDataEndCondition_TypeDef DataCondition);
  294. uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx, I2CDataEndCondition_TypeDef DataCondition);
  295. void I2C_SendBytes(I2C_TypeDef* I2Cx, uint8_t* Data, uint32_t DataLen, I2CDataEndCondition_TypeDef DataCondition);
  296. void I2C_ReceiveBytes(I2C_TypeDef* I2Cx, uint8_t* Data, uint32_t DataLen, I2CDataEndCondition_TypeDef DataCondition);
  297. void I2C_SlaveGeneralNACKOnlyCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  298. FlagStatus I2C_IsEnable(I2C_TypeDef* I2Cx);
  299. #ifdef __cplusplus
  300. }
  301. #endif
  302. #endif
  303. /************************** (C) COPYRIGHT Megahunt *****END OF FILE****/