1 /** 2 ****************************************************************************** 3 * @file stm32l1xx_hal_i2c.h 4 * @author MCD Application Team 5 * @brief Header file of I2C HAL module. 6 ****************************************************************************** 7 * @attention 8 * 9 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> 10 * 11 * Redistribution and use in source and binary forms, with or without modification, 12 * are permitted provided that the following conditions are met: 13 * 1. Redistributions of source code must retain the above copyright notice, 14 * this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright notice, 16 * this list of conditions and the following disclaimer in the documentation 17 * and/or other materials provided with the distribution. 18 * 3. Neither the name of STMicroelectronics nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 ****************************************************************************** 34 */ 35 36 /* Define to prevent recursive inclusion -------------------------------------*/ 37 #ifndef __STM32L1xx_HAL_I2C_H 38 #define __STM32L1xx_HAL_I2C_H 39 40 #ifdef __cplusplus 41 extern "C" { 42 #endif 43 44 /* Includes ------------------------------------------------------------------*/ 45 #include "stm32l1xx_hal_def.h" 46 47 /** @addtogroup STM32L1xx_HAL_Driver 48 * @{ 49 */ 50 51 /** @addtogroup I2C 52 * @{ 53 */ 54 55 /* Exported types ------------------------------------------------------------*/ 56 /** @defgroup I2C_Exported_Types I2C Exported Types 57 * @{ 58 */ 59 60 /** @defgroup I2C_Configuration_Structure_definition I2C Configuration Structure definition 61 * @brief I2C Configuration Structure definition 62 * @{ 63 */ 64 typedef struct 65 { 66 uint32_t ClockSpeed; /*!< Specifies the clock frequency. 67 This parameter must be set to a value lower than 400kHz */ 68 69 uint32_t DutyCycle; /*!< Specifies the I2C fast mode duty cycle. 70 This parameter can be a value of @ref I2C_duty_cycle_in_fast_mode */ 71 72 uint32_t OwnAddress1; /*!< Specifies the first device own address. 73 This parameter can be a 7-bit or 10-bit address. */ 74 75 uint32_t AddressingMode; /*!< Specifies if 7-bit or 10-bit addressing mode is selected. 76 This parameter can be a value of @ref I2C_addressing_mode */ 77 78 uint32_t DualAddressMode; /*!< Specifies if dual addressing mode is selected. 79 This parameter can be a value of @ref I2C_dual_addressing_mode */ 80 81 uint32_t OwnAddress2; /*!< Specifies the second device own address if dual addressing mode is selected 82 This parameter can be a 7-bit address. */ 83 84 uint32_t GeneralCallMode; /*!< Specifies if general call mode is selected. 85 This parameter can be a value of @ref I2C_general_call_addressing_mode */ 86 87 uint32_t NoStretchMode; /*!< Specifies if nostretch mode is selected. 88 This parameter can be a value of @ref I2C_nostretch_mode */ 89 90 }I2C_InitTypeDef; 91 92 /** 93 * @} 94 */ 95 96 /** @defgroup HAL_state_structure_definition HAL state structure definition 97 * @brief HAL State structure definition 98 * @note HAL I2C State value coding follow below described bitmap : 99 * b7-b6 Error information 100 * 00 : No Error 101 * 01 : Abort (Abort user request on going) 102 * 10 : Timeout 103 * 11 : Error 104 * b5 IP initilisation status 105 * 0 : Reset (IP not initialized) 106 * 1 : Init done (IP initialized and ready to use. HAL I2C Init function called) 107 * b4 (not used) 108 * x : Should be set to 0 109 * b3 110 * 0 : Ready or Busy (No Listen mode ongoing) 111 * 1 : Listen (IP in Address Listen Mode) 112 * b2 Intrinsic process state 113 * 0 : Ready 114 * 1 : Busy (IP busy with some configuration or internal operations) 115 * b1 Rx state 116 * 0 : Ready (no Rx operation ongoing) 117 * 1 : Busy (Rx operation ongoing) 118 * b0 Tx state 119 * 0 : Ready (no Tx operation ongoing) 120 * 1 : Busy (Tx operation ongoing) 121 * @{ 122 */ 123 typedef enum 124 { 125 HAL_I2C_STATE_RESET = 0x00U, /*!< Peripheral is not yet Initialized */ 126 HAL_I2C_STATE_READY = 0x20U, /*!< Peripheral Initialized and ready for use */ 127 HAL_I2C_STATE_BUSY = 0x24U, /*!< An internal process is ongoing */ 128 HAL_I2C_STATE_BUSY_TX = 0x21U, /*!< Data Transmission process is ongoing */ 129 HAL_I2C_STATE_BUSY_RX = 0x22U, /*!< Data Reception process is ongoing */ 130 HAL_I2C_STATE_LISTEN = 0x28U, /*!< Address Listen Mode is ongoing */ 131 HAL_I2C_STATE_BUSY_TX_LISTEN = 0x29U, /*!< Address Listen Mode and Data Transmission 132 process is ongoing */ 133 HAL_I2C_STATE_BUSY_RX_LISTEN = 0x2AU, /*!< Address Listen Mode and Data Reception 134 process is ongoing */ 135 HAL_I2C_STATE_ABORT = 0x60U, /*!< Abort user request ongoing */ 136 HAL_I2C_STATE_TIMEOUT = 0xA0U, /*!< Timeout state */ 137 HAL_I2C_STATE_ERROR = 0xE0U /*!< Error */ 138 139 }HAL_I2C_StateTypeDef; 140 141 /** 142 * @} 143 */ 144 145 /** @defgroup HAL_mode_structure_definition HAL mode structure definition 146 * @brief HAL Mode structure definition 147 * @note HAL I2C Mode value coding follow below described bitmap : 148 * b7 (not used) 149 * x : Should be set to 0 150 * b6 151 * 0 : None 152 * 1 : Memory (HAL I2C communication is in Memory Mode) 153 * b5 154 * 0 : None 155 * 1 : Slave (HAL I2C communication is in Slave Mode) 156 * b4 157 * 0 : None 158 * 1 : Master (HAL I2C communication is in Master Mode) 159 * b3-b2-b1-b0 (not used) 160 * xxxx : Should be set to 0000 161 * @{ 162 */ 163 typedef enum 164 { 165 HAL_I2C_MODE_NONE = 0x00U, /*!< No I2C communication on going */ 166 HAL_I2C_MODE_MASTER = 0x10U, /*!< I2C communication is in Master Mode */ 167 HAL_I2C_MODE_SLAVE = 0x20U, /*!< I2C communication is in Slave Mode */ 168 HAL_I2C_MODE_MEM = 0x40U /*!< I2C communication is in Memory Mode */ 169 170 }HAL_I2C_ModeTypeDef; 171 172 /** 173 * @} 174 */ 175 176 /** @defgroup I2C_Error_Code_definition I2C Error Code definition 177 * @brief I2C Error Code definition 178 * @{ 179 */ 180 #define HAL_I2C_ERROR_NONE (0x00000000U) /*!< No error */ 181 #define HAL_I2C_ERROR_BERR (0x00000001U) /*!< BERR error */ 182 #define HAL_I2C_ERROR_ARLO (0x00000002U) /*!< ARLO error */ 183 #define HAL_I2C_ERROR_AF (0x00000004U) /*!< AF error */ 184 #define HAL_I2C_ERROR_OVR (0x00000008U) /*!< OVR error */ 185 #define HAL_I2C_ERROR_DMA (0x00000010U) /*!< DMA transfer error */ 186 #define HAL_I2C_ERROR_TIMEOUT (0x00000020U) /*!< Timeout Error */ 187 /** 188 * @} 189 */ 190 191 /** @defgroup I2C_handle_Structure_definition I2C handle Structure definition 192 * @brief I2C handle Structure definition 193 * @{ 194 */ 195 typedef struct 196 { 197 I2C_TypeDef *Instance; /*!< I2C registers base address */ 198 199 I2C_InitTypeDef Init; /*!< I2C communication parameters */ 200 201 uint8_t *pBuffPtr; /*!< Pointer to I2C transfer buffer */ 202 203 uint16_t XferSize; /*!< I2C transfer size */ 204 205 __IO uint16_t XferCount; /*!< I2C transfer counter */ 206 207 __IO uint32_t XferOptions; /*!< I2C transfer options */ 208 209 __IO uint32_t PreviousState; /*!< I2C communication Previous state and mode 210 context for internal usage */ 211 212 DMA_HandleTypeDef *hdmatx; /*!< I2C Tx DMA handle parameters */ 213 214 DMA_HandleTypeDef *hdmarx; /*!< I2C Rx DMA handle parameters */ 215 216 HAL_LockTypeDef Lock; /*!< I2C locking object */ 217 218 __IO HAL_I2C_StateTypeDef State; /*!< I2C communication state */ 219 220 __IO HAL_I2C_ModeTypeDef Mode; /*!< I2C communication mode */ 221 222 __IO uint32_t ErrorCode; /*!< I2C Error code */ 223 224 __IO uint32_t Devaddress; /*!< I2C Target device address */ 225 226 __IO uint32_t Memaddress; /*!< I2C Target memory address */ 227 228 __IO uint32_t MemaddSize; /*!< I2C Target memory address size */ 229 230 __IO uint32_t EventCount; /*!< I2C Event counter */ 231 232 }I2C_HandleTypeDef; 233 234 /** 235 * @} 236 */ 237 238 /** 239 * @} 240 */ 241 /* Exported constants --------------------------------------------------------*/ 242 /** @defgroup I2C_Exported_Constants I2C Exported Constants 243 * @{ 244 */ 245 246 /** @defgroup I2C_duty_cycle_in_fast_mode I2C duty cycle in fast mode 247 * @{ 248 */ 249 #define I2C_DUTYCYCLE_2 (0x00000000U) 250 #define I2C_DUTYCYCLE_16_9 I2C_CCR_DUTY 251 /** 252 * @} 253 */ 254 255 /** @defgroup I2C_addressing_mode I2C addressing mode 256 * @{ 257 */ 258 #define I2C_ADDRESSINGMODE_7BIT (0x00004000U) 259 #define I2C_ADDRESSINGMODE_10BIT (I2C_OAR1_ADDMODE | (0x00004000U)) 260 /** 261 * @} 262 */ 263 264 /** @defgroup I2C_dual_addressing_mode I2C dual addressing mode 265 * @{ 266 */ 267 #define I2C_DUALADDRESS_DISABLE (0x00000000U) 268 #define I2C_DUALADDRESS_ENABLE I2C_OAR2_ENDUAL 269 /** 270 * @} 271 */ 272 273 /** @defgroup I2C_general_call_addressing_mode I2C general call addressing mode 274 * @{ 275 */ 276 #define I2C_GENERALCALL_DISABLE (0x00000000U) 277 #define I2C_GENERALCALL_ENABLE I2C_CR1_ENGC 278 /** 279 * @} 280 */ 281 282 /** @defgroup I2C_nostretch_mode I2C nostretch mode 283 * @{ 284 */ 285 #define I2C_NOSTRETCH_DISABLE (0x00000000U) 286 #define I2C_NOSTRETCH_ENABLE I2C_CR1_NOSTRETCH 287 /** 288 * @} 289 */ 290 291 /** @defgroup I2C_Memory_Address_Size I2C Memory Address Size 292 * @{ 293 */ 294 #define I2C_MEMADD_SIZE_8BIT (0x00000001U) 295 #define I2C_MEMADD_SIZE_16BIT (0x00000010U) 296 /** 297 * @} 298 */ 299 300 /** @defgroup I2C_XferDirection_definition I2C XferDirection definition Master Point of View 301 * @{ 302 */ 303 #define I2C_DIRECTION_RECEIVE (0x00000000U) 304 #define I2C_DIRECTION_TRANSMIT (0x00000001U) 305 /** 306 * @} 307 */ 308 309 /** @defgroup I2C_XferOptions_definition I2C XferOptions definition 310 * @{ 311 */ 312 #define I2C_FIRST_FRAME (0x00000001U) 313 #define I2C_NEXT_FRAME (0x00000002U) 314 #define I2C_FIRST_AND_LAST_FRAME (0x00000004U) 315 #define I2C_LAST_FRAME (0x00000008U) 316 /** 317 * @} 318 */ 319 320 /** @defgroup I2C_Interrupt_configuration_definition I2C Interrupt configuration definition 321 * @brief I2C Interrupt definition 322 * Elements values convention: 0xXXXXXXXX 323 * - XXXXXXXX : Interrupt control mask 324 * @{ 325 */ 326 #define I2C_IT_BUF I2C_CR2_ITBUFEN 327 #define I2C_IT_EVT I2C_CR2_ITEVTEN 328 #define I2C_IT_ERR I2C_CR2_ITERREN 329 /** 330 * @} 331 */ 332 333 /** @defgroup I2C_Flag_definition I2C Flag definition 334 * @{ 335 */ 336 #define I2C_FLAG_OVR ((uint32_t)(1U << 16U | I2C_SR1_OVR)) 337 #define I2C_FLAG_AF ((uint32_t)(1U << 16U | I2C_SR1_AF)) 338 #define I2C_FLAG_ARLO ((uint32_t)(1U << 16U | I2C_SR1_ARLO)) 339 #define I2C_FLAG_BERR ((uint32_t)(1U << 16U | I2C_SR1_BERR)) 340 #define I2C_FLAG_TXE ((uint32_t)(1U << 16U | I2C_SR1_TXE)) 341 #define I2C_FLAG_RXNE ((uint32_t)(1U << 16U | I2C_SR1_RXNE)) 342 #define I2C_FLAG_STOPF ((uint32_t)(1U << 16U | I2C_SR1_STOPF)) 343 #define I2C_FLAG_ADD10 ((uint32_t)(1U << 16U | I2C_SR1_ADD10)) 344 #define I2C_FLAG_BTF ((uint32_t)(1U << 16U | I2C_SR1_BTF)) 345 #define I2C_FLAG_ADDR ((uint32_t)(1U << 16U | I2C_SR1_ADDR)) 346 #define I2C_FLAG_SB ((uint32_t)(1U << 16U | I2C_SR1_SB)) 347 #define I2C_FLAG_DUALF ((uint32_t)(2U << 16U | I2C_SR2_DUALF)) 348 #define I2C_FLAG_GENCALL ((uint32_t)(2U << 16U | I2C_SR2_GENCALL)) 349 #define I2C_FLAG_TRA ((uint32_t)(2U << 16U | I2C_SR2_TRA)) 350 #define I2C_FLAG_BUSY ((uint32_t)(2U << 16U | I2C_SR2_BUSY)) 351 #define I2C_FLAG_MSL ((uint32_t)(2U << 16U | I2C_SR2_MSL)) 352 /** 353 * @} 354 */ 355 356 /** 357 * @} 358 */ 359 360 /* Exported macros -----------------------------------------------------------*/ 361 /** @defgroup I2C_Exported_Macros I2C Exported Macros 362 * @{ 363 */ 364 365 /** @brief Reset I2C handle state. 366 * @param __HANDLE__ specifies the I2C Handle. 367 * @retval None 368 */ 369 #define __HAL_I2C_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_I2C_STATE_RESET) 370 371 /** @brief Enable the specified I2C interrupt. 372 * @param __HANDLE__ specifies the I2C Handle. 373 * @param __INTERRUPT__: specifies the interrupt source to enable. 374 * This parameter can be one of the following values: 375 * @arg I2C_IT_BUF: Buffer interrupt enable 376 * @arg I2C_IT_EVT: Event interrupt enable 377 * @arg I2C_IT_ERR: Error interrupt enable 378 * @retval None 379 */ 380 #define __HAL_I2C_ENABLE_IT(__HANDLE__, __INTERRUPT__) SET_BIT((__HANDLE__)->Instance->CR2,(__INTERRUPT__)) 381 382 /** @brief Disable the specified I2C interrupt. 383 * @param __HANDLE__ specifies the I2C Handle. 384 * @param __INTERRUPT__: specifies the interrupt source to disable. 385 * This parameter can be one of the following values: 386 * @arg I2C_IT_BUF: Buffer interrupt enable 387 * @arg I2C_IT_EVT: Event interrupt enable 388 * @arg I2C_IT_ERR: Error interrupt enable 389 * 390 * @retval None 391 */ 392 #define __HAL_I2C_DISABLE_IT(__HANDLE__, __INTERRUPT__) CLEAR_BIT((__HANDLE__)->Instance->CR2, (__INTERRUPT__)) 393 394 /** @brief Check whether the specified I2C interrupt source is enabled or not. 395 * @param __HANDLE__ specifies the I2C Handle. 396 * @param __INTERRUPT__: specifies the I2C interrupt source to check. 397 * This parameter can be one of the following values: 398 * @arg I2C_IT_BUF: Buffer interrupt enable 399 * @arg I2C_IT_EVT: Event interrupt enable 400 * @arg I2C_IT_ERR: Error interrupt enable 401 * @retval The new state of __INTERRUPT__ (TRUE or FALSE). 402 */ 403 #define __HAL_I2C_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) ((((__HANDLE__)->Instance->CR2 & (__INTERRUPT__)) == (__INTERRUPT__)) ? SET : RESET) 404 405 /** @brief Check whether the specified I2C flag is set or not. 406 * @param __HANDLE__ specifies the I2C Handle. 407 * @param __FLAG__ specifies the flag to check. 408 * This parameter can be one of the following values: 409 * @arg I2C_FLAG_OVR: Overrun/Underrun flag 410 * @arg I2C_FLAG_AF: Acknowledge failure flag 411 * @arg I2C_FLAG_ARLO: Arbitration lost flag 412 * @arg I2C_FLAG_BERR: Bus error flag 413 * @arg I2C_FLAG_TXE: Data register empty flag 414 * @arg I2C_FLAG_RXNE: Data register not empty flag 415 * @arg I2C_FLAG_STOPF: Stop detection flag 416 * @arg I2C_FLAG_ADD10: 10-bit header sent flag 417 * @arg I2C_FLAG_BTF: Byte transfer finished flag 418 * @arg I2C_FLAG_ADDR: Address sent flag 419 * Address matched flag 420 * @arg I2C_FLAG_SB: Start bit flag 421 * @arg I2C_FLAG_DUALF: Dual flag 422 * @arg I2C_FLAG_GENCALL: General call header flag 423 * @arg I2C_FLAG_TRA: Transmitter/Receiver flag 424 * @arg I2C_FLAG_BUSY: Bus busy flag 425 * @arg I2C_FLAG_MSL: Master/Slave flag 426 * @retval The new state of __FLAG__ (TRUE or FALSE). 427 */ 428 #define __HAL_I2C_GET_FLAG(__HANDLE__, __FLAG__) ((((uint8_t)((__FLAG__) >> 16U)) == 0x01U)?(((((__HANDLE__)->Instance->SR1) & ((__FLAG__) & I2C_FLAG_MASK)) == ((__FLAG__) & I2C_FLAG_MASK)) ? SET : RESET): \ 429 (((((__HANDLE__)->Instance->SR2) & ((__FLAG__) & I2C_FLAG_MASK)) == ((__FLAG__) & I2C_FLAG_MASK)) ? SET : RESET)) 430 431 /** @brief Clear the I2C pending flags which are cleared by writing 0 in a specific bit. 432 * @param __HANDLE__ specifies the I2C Handle. 433 * @param __FLAG__ specifies the flag to clear. 434 * This parameter can be any combination of the following values: 435 * @arg I2C_FLAG_OVR: Overrun/Underrun flag (Slave mode) 436 * @arg I2C_FLAG_AF: Acknowledge failure flag 437 * @arg I2C_FLAG_ARLO: Arbitration lost flag (Master mode) 438 * @arg I2C_FLAG_BERR: Bus error flag 439 * @retval None 440 */ 441 #define __HAL_I2C_CLEAR_FLAG(__HANDLE__, __FLAG__) ((__HANDLE__)->Instance->SR1 = ~((__FLAG__) & I2C_FLAG_MASK)) 442 443 /** @brief Clears the I2C ADDR pending flag. 444 * @param __HANDLE__: specifies the I2C Handle. 445 * @retval None 446 */ 447 #define __HAL_I2C_CLEAR_ADDRFLAG(__HANDLE__) \ 448 do{ \ 449 __IO uint32_t tmpreg = 0x00U; \ 450 tmpreg = (__HANDLE__)->Instance->SR1; \ 451 tmpreg = (__HANDLE__)->Instance->SR2; \ 452 UNUSED(tmpreg); \ 453 }while(0) 454 455 /** @brief Clears the I2C STOPF pending flag. 456 * @param __HANDLE__: specifies the I2C Handle. 457 * @retval None 458 */ 459 #define __HAL_I2C_CLEAR_STOPFLAG(__HANDLE__) \ 460 do{ \ 461 __IO uint32_t tmpreg = 0x00U; \ 462 tmpreg = (__HANDLE__)->Instance->SR1; \ 463 SET_BIT((__HANDLE__)->Instance->CR1, I2C_CR1_PE); \ 464 UNUSED(tmpreg); \ 465 }while(0) 466 467 /** @brief Enable the I2C peripheral. 468 * @param __HANDLE__: specifies the I2C Handle. 469 * This parameter can be I2Cx where x: 1 or 2 to select the I2C peripheral. 470 * @retval None 471 */ 472 #define __HAL_I2C_ENABLE(__HANDLE__) SET_BIT((__HANDLE__)->Instance->CR1, I2C_CR1_PE) 473 474 /** @brief Disable the I2C peripheral. 475 * @param __HANDLE__: specifies the I2C Handle. 476 * This parameter can be I2Cx where x: 1 or 2 to select the I2C peripheral. 477 * @retval None 478 */ 479 #define __HAL_I2C_DISABLE(__HANDLE__) CLEAR_BIT((__HANDLE__)->Instance->CR1, I2C_CR1_PE) 480 481 /** 482 * @} 483 */ 484 485 /* Exported functions --------------------------------------------------------*/ 486 /** @addtogroup I2C_Exported_Functions 487 * @{ 488 */ 489 490 /** @addtogroup I2C_Exported_Functions_Group1 491 * @{ 492 */ 493 /* Initialization and de-initialization functions******************************/ 494 HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c); 495 HAL_StatusTypeDef HAL_I2C_DeInit (I2C_HandleTypeDef *hi2c); 496 void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c); 497 void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c); 498 499 /** 500 * @} 501 */ 502 503 /** @addtogroup I2C_Exported_Functions_Group2 504 * @{ 505 */ 506 /* IO operation functions ****************************************************/ 507 /******* Blocking mode: Polling */ 508 HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout); 509 HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout); 510 HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout); 511 HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout); 512 HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout); 513 HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout); 514 HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout); 515 516 /******* Non-Blocking mode: Interrupt */ 517 HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size); 518 HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size); 519 HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size); 520 HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size); 521 HAL_StatusTypeDef HAL_I2C_Mem_Write_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size); 522 HAL_StatusTypeDef HAL_I2C_Mem_Read_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size); 523 524 HAL_StatusTypeDef HAL_I2C_Master_Sequential_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions); 525 HAL_StatusTypeDef HAL_I2C_Master_Sequential_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions); 526 HAL_StatusTypeDef HAL_I2C_Slave_Sequential_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions); 527 HAL_StatusTypeDef HAL_I2C_Slave_Sequential_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions); 528 HAL_StatusTypeDef HAL_I2C_Master_Abort_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress); 529 HAL_StatusTypeDef HAL_I2C_EnableListen_IT(I2C_HandleTypeDef *hi2c); 530 HAL_StatusTypeDef HAL_I2C_DisableListen_IT(I2C_HandleTypeDef *hi2c); 531 532 /******* Non-Blocking mode: DMA */ 533 HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size); 534 HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size); 535 HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size); 536 HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size); 537 HAL_StatusTypeDef HAL_I2C_Mem_Write_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size); 538 HAL_StatusTypeDef HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size); 539 /** 540 * @} 541 */ 542 543 /** @addtogroup I2C_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks 544 * @{ 545 */ 546 /******* I2C IRQHandler and Callbacks used in non blocking modes (Interrupt and DMA) */ 547 void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c); 548 void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c); 549 void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c); 550 void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c); 551 void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c); 552 void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c); 553 void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode); 554 void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c); 555 void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c); 556 void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c); 557 void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c); 558 void HAL_I2C_AbortCpltCallback(I2C_HandleTypeDef *hi2c); 559 /** 560 * @} 561 */ 562 563 /** @addtogroup I2C_Exported_Functions_Group3 564 * @{ 565 */ 566 /* Peripheral State, Mode and Error functions *********************************/ 567 HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *hi2c); 568 HAL_I2C_ModeTypeDef HAL_I2C_GetMode(I2C_HandleTypeDef *hi2c); 569 uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c); 570 571 /** 572 * @} 573 */ 574 /** 575 * @} 576 */ 577 578 /* Private constants ---------------------------------------------------------*/ 579 /** @defgroup I2C_Private_Constants I2C Private Constants 580 * @{ 581 */ 582 #define I2C_FLAG_MASK (0x0000FFFFU) 583 /** 584 * @} 585 */ 586 587 /* Private macros ------------------------------------------------------------*/ 588 /** @defgroup I2C_Private_Macro I2C Private Macros 589 * @{ 590 */ 591 592 #define IS_I2C_ADDRESSING_MODE(MODE) (((MODE) == I2C_ADDRESSINGMODE_7BIT) || \ 593 ((MODE) == I2C_ADDRESSINGMODE_10BIT)) 594 595 596 #define IS_I2C_DUAL_ADDRESS(ADDRESS) (((ADDRESS) == I2C_DUALADDRESS_DISABLE) || \ 597 ((ADDRESS) == I2C_DUALADDRESS_ENABLE)) 598 599 #define IS_I2C_DUTY_CYCLE(CYCLE) (((CYCLE) == I2C_DUTYCYCLE_2) || \ 600 ((CYCLE) == I2C_DUTYCYCLE_16_9)) 601 #define IS_I2C_OWN_ADDRESS2(ADDRESS2) (((ADDRESS2) & (0xFFFFFF01U)) == 0U) 602 #define IS_I2C_OWN_ADDRESS1(ADDRESS1) (((ADDRESS1) & (0xFFFFFC00U)) == 0U) 603 #define IS_I2C_GENERAL_CALL(CALL) (((CALL) == I2C_GENERALCALL_DISABLE) || \ 604 ((CALL) == I2C_GENERALCALL_ENABLE)) 605 606 #define IS_I2C_NO_STRETCH(STRETCH) (((STRETCH) == I2C_NOSTRETCH_DISABLE) || \ 607 ((STRETCH) == I2C_NOSTRETCH_ENABLE)) 608 609 #define IS_I2C_MEMADD_SIZE(SIZE) (((SIZE) == I2C_MEMADD_SIZE_8BIT) || \ 610 ((SIZE) == I2C_MEMADD_SIZE_16BIT)) 611 612 #define IS_I2C_CLOCK_SPEED(SPEED) (((SPEED) > 0U) && ((SPEED) <= 400000U)) 613 614 615 #define I2C_MEM_ADD_MSB(__ADDRESS__) ((uint8_t)((uint16_t)(((uint16_t)((__ADDRESS__) & (uint16_t)(0xFF00U))) >> 8U))) 616 #define I2C_MEM_ADD_LSB(__ADDRESS__) ((uint8_t)((uint16_t)((__ADDRESS__) & (uint16_t)(0x00FFU)))) 617 618 619 #define I2C_FREQ_RANGE(__PCLK__) ((__PCLK__)/1000000U) 620 #define I2C_RISE_TIME(__FREQRANGE__, __SPEED__) (((__SPEED__) <= 100000U) ? ((__FREQRANGE__) + 1U) : ((((__FREQRANGE__) * 300U) / 1000U) + 1U)) 621 #define I2C_SPEED_STANDARD(__PCLK__, __SPEED__) (((((__PCLK__)/((__SPEED__) << 1U)) & I2C_CCR_CCR) < 4U)? 4U:((__PCLK__) / ((__SPEED__) << 1U))) 622 #define I2C_SPEED_FAST(__PCLK__, __SPEED__, __DUTYCYCLE__) (((__DUTYCYCLE__) == I2C_DUTYCYCLE_2)? ((__PCLK__) / ((__SPEED__) * 3U)) : (((__PCLK__) / ((__SPEED__) * 25U)) | I2C_DUTYCYCLE_16_9)) 623 #define I2C_SPEED(__PCLK__, __SPEED__, __DUTYCYCLE__) (((__SPEED__) <= 100000U)? (I2C_SPEED_STANDARD((__PCLK__), (__SPEED__))) : \ 624 ((I2C_SPEED_FAST((__PCLK__), (__SPEED__), (__DUTYCYCLE__)) & I2C_CCR_CCR) == 0U)? 1U : \ 625 ((I2C_SPEED_FAST((__PCLK__), (__SPEED__), (__DUTYCYCLE__))) | I2C_CCR_FS)) 626 627 #define I2C_7BIT_ADD_WRITE(__ADDRESS__) ((uint8_t)((__ADDRESS__) & (~I2C_OAR1_ADD0))) 628 #define I2C_7BIT_ADD_READ(__ADDRESS__) ((uint8_t)((__ADDRESS__) | I2C_OAR1_ADD0)) 629 630 #define I2C_10BIT_ADDRESS(__ADDRESS__) ((uint8_t)((uint16_t)((__ADDRESS__) & (uint16_t)(0x00FFU)))) 631 #define I2C_10BIT_HEADER_WRITE(__ADDRESS__) ((uint8_t)((uint16_t)((uint16_t)(((uint16_t)((__ADDRESS__) & (uint16_t)(0x0300U))) >> 7U) | (uint16_t)(0xF0U)))) 632 #define I2C_10BIT_HEADER_READ(__ADDRESS__) ((uint8_t)((uint16_t)((uint16_t)(((uint16_t)((__ADDRESS__) & (uint16_t)(0x0300U))) >> 7U) | (uint16_t)(0xF1U)))) 633 634 #define IS_I2C_TRANSFER_OPTIONS_REQUEST(REQUEST) (((REQUEST) == I2C_FIRST_FRAME) || \ 635 ((REQUEST) == I2C_NEXT_FRAME) || \ 636 ((REQUEST) == I2C_FIRST_AND_LAST_FRAME) || \ 637 ((REQUEST) == I2C_LAST_FRAME)) 638 /** 639 * @} 640 */ 641 642 /* Private Functions ---------------------------------------------------------*/ 643 /** @defgroup I2C_Private_Functions I2C Private Functions 644 * @{ 645 */ 646 /* Private functions are defined in stm32f0xx_hal_i2c.c file */ 647 /** 648 * @} 649 */ 650 651 /** 652 * @} 653 */ 654 655 /** 656 * @} 657 */ 658 659 #ifdef __cplusplus 660 } 661 #endif 662 663 664 #endif /* __STM32L1xx_HAL_I2C_H */ 665 666 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 667 668