stm32f0xx_i2c.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_i2c.h
  4. * @author MCD Application Team
  5. * @version V1.5.0
  6. * @date 05-December-2014
  7. * @brief This file contains all the functions prototypes for the I2C firmware
  8. * library
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  13. *
  14. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  15. * You may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at:
  17. *
  18. * http://www.st.com/software_license_agreement_liberty_v2
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. *
  26. ******************************************************************************
  27. */
  28. /* Define to prevent recursive inclusion -------------------------------------*/
  29. #ifndef __STM32F0XX_I2C_H
  30. #define __STM32F0XX_I2C_H
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. /* Includes ------------------------------------------------------------------*/
  35. #include "stm32f0xx.h"
  36. /** @addtogroup STM32F0xx_StdPeriph_Driver
  37. * @{
  38. */
  39. /** @addtogroup I2C
  40. * @{
  41. */
  42. /* Exported types ------------------------------------------------------------*/
  43. /**
  44. * @brief I2C Init structure definition
  45. */
  46. typedef struct
  47. {
  48. uint32_t I2C_Timing; /*!< Specifies the I2C_TIMINGR_register value.
  49. This parameter must be set by referring to I2C_Timing_Config_Tool*/
  50. uint32_t I2C_AnalogFilter; /*!< Enables or disables analog noise filter.
  51. This parameter can be a value of @ref I2C_Analog_Filter*/
  52. uint32_t I2C_DigitalFilter; /*!< Configures the digital noise filter.
  53. This parameter can be a number between 0x00 and 0x0F*/
  54. uint32_t I2C_Mode; /*!< Specifies the I2C mode.
  55. This parameter can be a value of @ref I2C_mode*/
  56. uint32_t I2C_OwnAddress1; /*!< Specifies the device own address 1.
  57. This parameter can be a 7-bit or 10-bit address*/
  58. uint32_t I2C_Ack; /*!< Enables or disables the acknowledgement.
  59. This parameter can be a value of @ref I2C_acknowledgement*/
  60. uint32_t I2C_AcknowledgedAddress; /*!< Specifies if 7-bit or 10-bit address is acknowledged.
  61. This parameter can be a value of @ref I2C_acknowledged_address*/
  62. }I2C_InitTypeDef;
  63. /* Exported constants --------------------------------------------------------*/
  64. /** @defgroup I2C_Exported_Constants
  65. * @{
  66. */
  67. #define IS_I2C_ALL_PERIPH(PERIPH) (((PERIPH) == I2C1) || \
  68. ((PERIPH) == I2C2))
  69. #define IS_I2C_1_PERIPH(PERIPH) ((PERIPH) == I2C1)
  70. /** @defgroup I2C_Analog_Filter
  71. * @{
  72. */
  73. #define I2C_AnalogFilter_Enable ((uint32_t)0x00000000)
  74. #define I2C_AnalogFilter_Disable I2C_CR1_ANFOFF
  75. #define IS_I2C_ANALOG_FILTER(FILTER) (((FILTER) == I2C_AnalogFilter_Enable) || \
  76. ((FILTER) == I2C_AnalogFilter_Disable))
  77. /**
  78. * @}
  79. */
  80. /** @defgroup I2C_Digital_Filter
  81. * @{
  82. */
  83. #define IS_I2C_DIGITAL_FILTER(FILTER) ((FILTER) <= 0x0000000F)
  84. /**
  85. * @}
  86. */
  87. /** @defgroup I2C_mode
  88. * @{
  89. */
  90. #define I2C_Mode_I2C ((uint32_t)0x00000000)
  91. #define I2C_Mode_SMBusDevice I2C_CR1_SMBDEN
  92. #define I2C_Mode_SMBusHost I2C_CR1_SMBHEN
  93. #define IS_I2C_MODE(MODE) (((MODE) == I2C_Mode_I2C) || \
  94. ((MODE) == I2C_Mode_SMBusDevice) || \
  95. ((MODE) == I2C_Mode_SMBusHost))
  96. /**
  97. * @}
  98. */
  99. /** @defgroup I2C_acknowledgement
  100. * @{
  101. */
  102. #define I2C_Ack_Enable ((uint32_t)0x00000000)
  103. #define I2C_Ack_Disable I2C_CR2_NACK
  104. #define IS_I2C_ACK(ACK) (((ACK) == I2C_Ack_Enable) || \
  105. ((ACK) == I2C_Ack_Disable))
  106. /**
  107. * @}
  108. */
  109. /** @defgroup I2C_acknowledged_address
  110. * @{
  111. */
  112. #define I2C_AcknowledgedAddress_7bit ((uint32_t)0x00000000)
  113. #define I2C_AcknowledgedAddress_10bit I2C_OAR1_OA1MODE
  114. #define IS_I2C_ACKNOWLEDGE_ADDRESS(ADDRESS) (((ADDRESS) == I2C_AcknowledgedAddress_7bit) || \
  115. ((ADDRESS) == I2C_AcknowledgedAddress_10bit))
  116. /**
  117. * @}
  118. */
  119. /** @defgroup I2C_own_address1
  120. * @{
  121. */
  122. #define IS_I2C_OWN_ADDRESS1(ADDRESS1) ((ADDRESS1) <= (uint32_t)0x000003FF)
  123. /**
  124. * @}
  125. */
  126. /** @defgroup I2C_transfer_direction
  127. * @{
  128. */
  129. #define I2C_Direction_Transmitter ((uint16_t)0x0000)
  130. #define I2C_Direction_Receiver ((uint16_t)0x0400)
  131. #define IS_I2C_DIRECTION(DIRECTION) (((DIRECTION) == I2C_Direction_Transmitter) || \
  132. ((DIRECTION) == I2C_Direction_Receiver))
  133. /**
  134. * @}
  135. */
  136. /** @defgroup I2C_DMA_transfer_requests
  137. * @{
  138. */
  139. #define I2C_DMAReq_Tx I2C_CR1_TXDMAEN
  140. #define I2C_DMAReq_Rx I2C_CR1_RXDMAEN
  141. #define IS_I2C_DMA_REQ(REQ) ((((REQ) & (uint32_t)0xFFFF3FFF) == 0x00) && ((REQ) != 0x00))
  142. /**
  143. * @}
  144. */
  145. /** @defgroup I2C_slave_address
  146. * @{
  147. */
  148. #define IS_I2C_SLAVE_ADDRESS(ADDRESS) ((ADDRESS) <= (uint16_t)0x03FF)
  149. /**
  150. * @}
  151. */
  152. /** @defgroup I2C_own_address2
  153. * @{
  154. */
  155. #define IS_I2C_OWN_ADDRESS2(ADDRESS2) ((ADDRESS2) <= (uint16_t)0x00FF)
  156. /**
  157. * @}
  158. */
  159. /** @defgroup I2C_own_address2_mask
  160. * @{
  161. */
  162. #define I2C_OA2_NoMask ((uint8_t)0x00)
  163. #define I2C_OA2_Mask01 ((uint8_t)0x01)
  164. #define I2C_OA2_Mask02 ((uint8_t)0x02)
  165. #define I2C_OA2_Mask03 ((uint8_t)0x03)
  166. #define I2C_OA2_Mask04 ((uint8_t)0x04)
  167. #define I2C_OA2_Mask05 ((uint8_t)0x05)
  168. #define I2C_OA2_Mask06 ((uint8_t)0x06)
  169. #define I2C_OA2_Mask07 ((uint8_t)0x07)
  170. #define IS_I2C_OWN_ADDRESS2_MASK(MASK) (((MASK) == I2C_OA2_NoMask) || \
  171. ((MASK) == I2C_OA2_Mask01) || \
  172. ((MASK) == I2C_OA2_Mask02) || \
  173. ((MASK) == I2C_OA2_Mask03) || \
  174. ((MASK) == I2C_OA2_Mask04) || \
  175. ((MASK) == I2C_OA2_Mask05) || \
  176. ((MASK) == I2C_OA2_Mask06) || \
  177. ((MASK) == I2C_OA2_Mask07))
  178. /**
  179. * @}
  180. */
  181. /** @defgroup I2C_timeout
  182. * @{
  183. */
  184. #define IS_I2C_TIMEOUT(TIMEOUT) ((TIMEOUT) <= (uint16_t)0x0FFF)
  185. /**
  186. * @}
  187. */
  188. /** @defgroup I2C_registers
  189. * @{
  190. */
  191. #define I2C_Register_CR1 ((uint8_t)0x00)
  192. #define I2C_Register_CR2 ((uint8_t)0x04)
  193. #define I2C_Register_OAR1 ((uint8_t)0x08)
  194. #define I2C_Register_OAR2 ((uint8_t)0x0C)
  195. #define I2C_Register_TIMINGR ((uint8_t)0x10)
  196. #define I2C_Register_TIMEOUTR ((uint8_t)0x14)
  197. #define I2C_Register_ISR ((uint8_t)0x18)
  198. #define I2C_Register_ICR ((uint8_t)0x1C)
  199. #define I2C_Register_PECR ((uint8_t)0x20)
  200. #define I2C_Register_RXDR ((uint8_t)0x24)
  201. #define I2C_Register_TXDR ((uint8_t)0x28)
  202. #define IS_I2C_REGISTER(REGISTER) (((REGISTER) == I2C_Register_CR1) || \
  203. ((REGISTER) == I2C_Register_CR2) || \
  204. ((REGISTER) == I2C_Register_OAR1) || \
  205. ((REGISTER) == I2C_Register_OAR2) || \
  206. ((REGISTER) == I2C_Register_TIMINGR) || \
  207. ((REGISTER) == I2C_Register_TIMEOUTR) || \
  208. ((REGISTER) == I2C_Register_ISR) || \
  209. ((REGISTER) == I2C_Register_ICR) || \
  210. ((REGISTER) == I2C_Register_PECR) || \
  211. ((REGISTER) == I2C_Register_RXDR) || \
  212. ((REGISTER) == I2C_Register_TXDR))
  213. /**
  214. * @}
  215. */
  216. /** @defgroup I2C_interrupts_definition
  217. * @{
  218. */
  219. #define I2C_IT_ERRI I2C_CR1_ERRIE
  220. #define I2C_IT_TCI I2C_CR1_TCIE
  221. #define I2C_IT_STOPI I2C_CR1_STOPIE
  222. #define I2C_IT_NACKI I2C_CR1_NACKIE
  223. #define I2C_IT_ADDRI I2C_CR1_ADDRIE
  224. #define I2C_IT_RXI I2C_CR1_RXIE
  225. #define I2C_IT_TXI I2C_CR1_TXIE
  226. #define IS_I2C_CONFIG_IT(IT) ((((IT) & (uint32_t)0xFFFFFF01) == 0x00) && ((IT) != 0x00))
  227. /**
  228. * @}
  229. */
  230. /** @defgroup I2C_flags_definition
  231. * @{
  232. */
  233. #define I2C_FLAG_TXE I2C_ISR_TXE
  234. #define I2C_FLAG_TXIS I2C_ISR_TXIS
  235. #define I2C_FLAG_RXNE I2C_ISR_RXNE
  236. #define I2C_FLAG_ADDR I2C_ISR_ADDR
  237. #define I2C_FLAG_NACKF I2C_ISR_NACKF
  238. #define I2C_FLAG_STOPF I2C_ISR_STOPF
  239. #define I2C_FLAG_TC I2C_ISR_TC
  240. #define I2C_FLAG_TCR I2C_ISR_TCR
  241. #define I2C_FLAG_BERR I2C_ISR_BERR
  242. #define I2C_FLAG_ARLO I2C_ISR_ARLO
  243. #define I2C_FLAG_OVR I2C_ISR_OVR
  244. #define I2C_FLAG_PECERR I2C_ISR_PECERR
  245. #define I2C_FLAG_TIMEOUT I2C_ISR_TIMEOUT
  246. #define I2C_FLAG_ALERT I2C_ISR_ALERT
  247. #define I2C_FLAG_BUSY I2C_ISR_BUSY
  248. #define IS_I2C_CLEAR_FLAG(FLAG) ((((FLAG) & (uint32_t)0xFFFF4000) == 0x00) && ((FLAG) != 0x00))
  249. #define IS_I2C_GET_FLAG(FLAG) (((FLAG) == I2C_FLAG_TXE) || ((FLAG) == I2C_FLAG_TXIS) || \
  250. ((FLAG) == I2C_FLAG_RXNE) || ((FLAG) == I2C_FLAG_ADDR) || \
  251. ((FLAG) == I2C_FLAG_NACKF) || ((FLAG) == I2C_FLAG_STOPF) || \
  252. ((FLAG) == I2C_FLAG_TC) || ((FLAG) == I2C_FLAG_TCR) || \
  253. ((FLAG) == I2C_FLAG_BERR) || ((FLAG) == I2C_FLAG_ARLO) || \
  254. ((FLAG) == I2C_FLAG_OVR) || ((FLAG) == I2C_FLAG_PECERR) || \
  255. ((FLAG) == I2C_FLAG_TIMEOUT) || ((FLAG) == I2C_FLAG_ALERT) || \
  256. ((FLAG) == I2C_FLAG_BUSY))
  257. /**
  258. * @}
  259. */
  260. /** @defgroup I2C_interrupts_definition
  261. * @{
  262. */
  263. #define I2C_IT_TXIS I2C_ISR_TXIS
  264. #define I2C_IT_RXNE I2C_ISR_RXNE
  265. #define I2C_IT_ADDR I2C_ISR_ADDR
  266. #define I2C_IT_NACKF I2C_ISR_NACKF
  267. #define I2C_IT_STOPF I2C_ISR_STOPF
  268. #define I2C_IT_TC I2C_ISR_TC
  269. #define I2C_IT_TCR I2C_ISR_TCR
  270. #define I2C_IT_BERR I2C_ISR_BERR
  271. #define I2C_IT_ARLO I2C_ISR_ARLO
  272. #define I2C_IT_OVR I2C_ISR_OVR
  273. #define I2C_IT_PECERR I2C_ISR_PECERR
  274. #define I2C_IT_TIMEOUT I2C_ISR_TIMEOUT
  275. #define I2C_IT_ALERT I2C_ISR_ALERT
  276. #define IS_I2C_CLEAR_IT(IT) ((((IT) & (uint32_t)0xFFFFC001) == 0x00) && ((IT) != 0x00))
  277. #define IS_I2C_GET_IT(IT) (((IT) == I2C_IT_TXIS) || ((IT) == I2C_IT_RXNE) || \
  278. ((IT) == I2C_IT_ADDR) || ((IT) == I2C_IT_NACKF) || \
  279. ((IT) == I2C_IT_STOPF) || ((IT) == I2C_IT_TC) || \
  280. ((IT) == I2C_IT_TCR) || ((IT) == I2C_IT_BERR) || \
  281. ((IT) == I2C_IT_ARLO) || ((IT) == I2C_IT_OVR) || \
  282. ((IT) == I2C_IT_PECERR) || ((IT) == I2C_IT_TIMEOUT) || \
  283. ((IT) == I2C_IT_ALERT))
  284. /**
  285. * @}
  286. */
  287. /** @defgroup I2C_ReloadEndMode_definition
  288. * @{
  289. */
  290. #define I2C_Reload_Mode I2C_CR2_RELOAD
  291. #define I2C_AutoEnd_Mode I2C_CR2_AUTOEND
  292. #define I2C_SoftEnd_Mode ((uint32_t)0x00000000)
  293. #define IS_RELOAD_END_MODE(MODE) (((MODE) == I2C_Reload_Mode) || \
  294. ((MODE) == I2C_AutoEnd_Mode) || \
  295. ((MODE) == I2C_SoftEnd_Mode))
  296. /**
  297. * @}
  298. */
  299. /** @defgroup I2C_StartStopMode_definition
  300. * @{
  301. */
  302. #define I2C_No_StartStop ((uint32_t)0x00000000)
  303. #define I2C_Generate_Stop I2C_CR2_STOP
  304. #define I2C_Generate_Start_Read (uint32_t)(I2C_CR2_START | I2C_CR2_RD_WRN)
  305. #define I2C_Generate_Start_Write I2C_CR2_START
  306. #define IS_START_STOP_MODE(MODE) (((MODE) == I2C_Generate_Stop) || \
  307. ((MODE) == I2C_Generate_Start_Read) || \
  308. ((MODE) == I2C_Generate_Start_Write) || \
  309. ((MODE) == I2C_No_StartStop))
  310. /**
  311. * @}
  312. */
  313. /**
  314. * @}
  315. */
  316. /* Exported macro ------------------------------------------------------------*/
  317. /* Exported functions ------------------------------------------------------- */
  318. /* Initialization and Configuration functions *********************************/
  319. void I2C_DeInit(I2C_TypeDef* I2Cx);
  320. void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct);
  321. void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct);
  322. void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  323. void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx);
  324. void I2C_ITConfig(I2C_TypeDef* I2Cx, uint32_t I2C_IT, FunctionalState NewState);
  325. void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  326. void I2C_StopModeCmd(I2C_TypeDef* I2Cx, FunctionalState NewState); /*!< not applicable for STM32F030 devices */
  327. void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  328. void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint16_t Address, uint8_t Mask);
  329. void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  330. void I2C_SlaveByteControlCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  331. void I2C_SlaveAddressConfig(I2C_TypeDef* I2Cx, uint16_t Address);
  332. void I2C_10BitAddressingModeCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  333. /* Communications handling functions ******************************************/
  334. void I2C_AutoEndCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  335. void I2C_ReloadCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  336. void I2C_NumberOfBytesConfig(I2C_TypeDef* I2Cx, uint8_t Number_Bytes);
  337. void I2C_MasterRequestConfig(I2C_TypeDef* I2Cx, uint16_t I2C_Direction);
  338. void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState);
  339. void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState);
  340. void I2C_10BitAddressHeaderCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  341. void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState);
  342. uint8_t I2C_GetAddressMatched(I2C_TypeDef* I2Cx);
  343. uint16_t I2C_GetTransferDirection(I2C_TypeDef* I2Cx);
  344. void I2C_TransferHandling(I2C_TypeDef* I2Cx, uint16_t Address, uint8_t Number_Bytes, uint32_t ReloadEndMode, uint32_t StartStopMode);
  345. /* SMBUS management functions ************************************************/
  346. void I2C_SMBusAlertCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  347. void I2C_ClockTimeoutCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  348. void I2C_ExtendedClockTimeoutCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  349. void I2C_IdleClockTimeoutCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  350. void I2C_TimeoutAConfig(I2C_TypeDef* I2Cx, uint16_t Timeout);
  351. void I2C_TimeoutBConfig(I2C_TypeDef* I2Cx, uint16_t Timeout);
  352. void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState);
  353. void I2C_PECRequestCmd(I2C_TypeDef* I2Cx, FunctionalState NewState);
  354. uint8_t I2C_GetPEC(I2C_TypeDef* I2Cx);
  355. /* I2C registers management functions *****************************************/
  356. uint32_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register);
  357. /* Data transfers management functions ****************************************/
  358. void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data);
  359. uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx);
  360. /* DMA transfers management functions *****************************************/
  361. void I2C_DMACmd(I2C_TypeDef* I2Cx, uint32_t I2C_DMAReq, FunctionalState NewState);
  362. /* Interrupts and flags management functions **********************************/
  363. FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);
  364. void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG);
  365. ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
  366. void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT);
  367. #ifdef __cplusplus
  368. }
  369. #endif
  370. #endif /*__STM32F0XX_I2C_H */
  371. /**
  372. * @}
  373. */
  374. /**
  375. * @}
  376. */
  377. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/