Subversion Repositories LedShow

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /**
  2.   ******************************************************************************
  3.   * @file    stm32f1xx_hal_i2c.h
  4.   * @author  MCD Application Team
  5.   * @brief   Header file of I2C HAL module.
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>&copy; COPYRIGHT(c) 2016 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 __STM32F1xx_HAL_I2C_H
  38. #define __STM32F1xx_HAL_I2C_H
  39.  
  40. #ifdef __cplusplus
  41.  extern "C" {
  42. #endif
  43.  
  44. /* Includes ------------------------------------------------------------------*/
  45. #include "stm32f1xx_hal_def.h"
  46.  
  47. /** @addtogroup STM32F1xx_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. /**
  61.   * @brief  I2C Configuration Structure definition
  62.   */
  63. typedef struct
  64. {
  65.   uint32_t ClockSpeed;       /*!< Specifies the clock frequency.
  66.                                   This parameter must be set to a value lower than 400kHz */
  67.  
  68.   uint32_t DutyCycle;        /*!< Specifies the I2C fast mode duty cycle.
  69.                                   This parameter can be a value of @ref I2C_duty_cycle_in_fast_mode */
  70.  
  71.   uint32_t OwnAddress1;      /*!< Specifies the first device own address.
  72.                                   This parameter can be a 7-bit or 10-bit address. */
  73.  
  74.   uint32_t AddressingMode;   /*!< Specifies if 7-bit or 10-bit addressing mode is selected.
  75.                                   This parameter can be a value of @ref I2C_addressing_mode */
  76.  
  77.   uint32_t DualAddressMode;  /*!< Specifies if dual addressing mode is selected.
  78.                                   This parameter can be a value of @ref I2C_dual_addressing_mode */
  79.  
  80.   uint32_t OwnAddress2;      /*!< Specifies the second device own address if dual addressing mode is selected
  81.                                   This parameter can be a 7-bit address. */
  82.  
  83.   uint32_t GeneralCallMode;  /*!< Specifies if general call mode is selected.
  84.                                   This parameter can be a value of @ref I2C_general_call_addressing_mode */
  85.  
  86.   uint32_t NoStretchMode;    /*!< Specifies if nostretch mode is selected.
  87.                                   This parameter can be a value of @ref I2C_nostretch_mode */
  88.  
  89. }I2C_InitTypeDef;
  90.  
  91. /**
  92.   * @brief  HAL State structure definition
  93.   * @note  HAL I2C State value coding follow below described bitmap :
  94.   *          b7-b6  Error information
  95.   *             00 : No Error
  96.   *             01 : Abort (Abort user request on going)
  97.   *             10 : Timeout
  98.   *             11 : Error
  99.   *          b5     IP initilisation status
  100.   *             0  : Reset (IP not initialized)
  101.   *             1  : Init done (IP initialized and ready to use. HAL I2C Init function called)
  102.   *          b4     (not used)
  103.   *             x  : Should be set to 0
  104.   *          b3
  105.   *             0  : Ready or Busy (No Listen mode ongoing)
  106.   *             1  : Listen (IP in Address Listen Mode)
  107.   *          b2     Intrinsic process state
  108.   *             0  : Ready
  109.   *             1  : Busy (IP busy with some configuration or internal operations)
  110.   *          b1     Rx state
  111.   *             0  : Ready (no Rx operation ongoing)
  112.   *             1  : Busy (Rx operation ongoing)
  113.   *          b0     Tx state
  114.   *             0  : Ready (no Tx operation ongoing)
  115.   *             1  : Busy (Tx operation ongoing)
  116.   */
  117. typedef enum
  118. {
  119.   HAL_I2C_STATE_RESET             = 0x00U,   /*!< Peripheral is not yet Initialized         */
  120.   HAL_I2C_STATE_READY             = 0x20U,   /*!< Peripheral Initialized and ready for use  */
  121.   HAL_I2C_STATE_BUSY              = 0x24U,   /*!< An internal process is ongoing            */
  122.   HAL_I2C_STATE_BUSY_TX           = 0x21U,   /*!< Data Transmission process is ongoing      */
  123.   HAL_I2C_STATE_BUSY_RX           = 0x22U,   /*!< Data Reception process is ongoing         */
  124.   HAL_I2C_STATE_LISTEN            = 0x28U,   /*!< Address Listen Mode is ongoing            */
  125.   HAL_I2C_STATE_BUSY_TX_LISTEN    = 0x29U,   /*!< Address Listen Mode and Data Transmission
  126.                                                  process is ongoing                         */
  127.   HAL_I2C_STATE_BUSY_RX_LISTEN    = 0x2AU,   /*!< Address Listen Mode and Data Reception
  128.                                                  process is ongoing                         */
  129.   HAL_I2C_STATE_ABORT             = 0x60U,   /*!< Abort user request ongoing                */
  130.   HAL_I2C_STATE_TIMEOUT           = 0xA0U,   /*!< Timeout state                             */
  131.   HAL_I2C_STATE_ERROR             = 0xE0U    /*!< Error                                     */
  132.  
  133. }HAL_I2C_StateTypeDef;
  134.  
  135. /**
  136.   * @brief  HAL Mode structure definition
  137.   * @note  HAL I2C Mode value coding follow below described bitmap :
  138.   *          b7     (not used)
  139.   *             x  : Should be set to 0
  140.   *          b6
  141.   *             0  : None
  142.   *             1  : Memory (HAL I2C communication is in Memory Mode)
  143.   *          b5
  144.   *             0  : None
  145.   *             1  : Slave (HAL I2C communication is in Slave Mode)
  146.   *          b4
  147.   *             0  : None
  148.   *             1  : Master (HAL I2C communication is in Master Mode)
  149.   *          b3-b2-b1-b0  (not used)
  150.   *             xxxx : Should be set to 0000
  151.   */
  152. typedef enum
  153. {
  154.   HAL_I2C_MODE_NONE               = 0x00U,   /*!< No I2C communication on going             */
  155.   HAL_I2C_MODE_MASTER             = 0x10U,   /*!< I2C communication is in Master Mode       */
  156.   HAL_I2C_MODE_SLAVE              = 0x20U,   /*!< I2C communication is in Slave Mode        */
  157.   HAL_I2C_MODE_MEM                = 0x40U    /*!< I2C communication is in Memory Mode       */
  158.  
  159. }HAL_I2C_ModeTypeDef;
  160.  
  161. /**
  162.   * @brief  I2C handle Structure definition
  163.   */
  164. typedef struct
  165. {
  166.   I2C_TypeDef                *Instance;      /*!< I2C registers base address               */
  167.                                              
  168.   I2C_InitTypeDef            Init;           /*!< I2C communication parameters             */
  169.                                              
  170.   uint8_t                    *pBuffPtr;      /*!< Pointer to I2C transfer buffer           */
  171.                                              
  172.   uint16_t                   XferSize;       /*!< I2C transfer size                        */
  173.                                              
  174.   __IO uint16_t              XferCount;      /*!< I2C transfer counter                     */
  175.                                              
  176.   __IO uint32_t              XferOptions;    /*!< I2C transfer options                     */
  177.                                              
  178.   __IO uint32_t              PreviousState;  /*!< I2C communication Previous state and mode
  179.                                                   context for internal usage               */
  180.                                              
  181.   DMA_HandleTypeDef          *hdmatx;        /*!< I2C Tx DMA handle parameters             */
  182.                                              
  183.   DMA_HandleTypeDef          *hdmarx;        /*!< I2C Rx DMA handle parameters             */
  184.                                              
  185.   HAL_LockTypeDef            Lock;           /*!< I2C locking object                       */
  186.                                              
  187.   __IO HAL_I2C_StateTypeDef  State;          /*!< I2C communication state                  */
  188.                                              
  189.   __IO HAL_I2C_ModeTypeDef   Mode;           /*!< I2C communication mode                   */
  190.                                              
  191.   __IO uint32_t              ErrorCode;      /*!< I2C Error code                           */
  192.  
  193.   __IO uint32_t              Devaddress;     /*!< I2C Target device address                */
  194.  
  195.   __IO uint32_t              Memaddress;     /*!< I2C Target memory address                */
  196.  
  197.   __IO uint32_t              MemaddSize;     /*!< I2C Target memory address  size          */
  198.  
  199.   __IO uint32_t              EventCount;     /*!< I2C Event counter                        */
  200.        
  201. }I2C_HandleTypeDef;
  202.  
  203. /**
  204.   * @}
  205.   */
  206.  
  207. /* Exported constants --------------------------------------------------------*/
  208. /** @defgroup I2C_Exported_Constants I2C Exported Constants
  209.   * @{
  210.   */
  211.  
  212. /** @defgroup I2C_Error_Code I2C Error Code
  213.   * @brief    I2C Error Code
  214.   * @{
  215.   */
  216. #define HAL_I2C_ERROR_NONE       0x00000000U    /*!< No error           */
  217. #define HAL_I2C_ERROR_BERR       0x00000001U    /*!< BERR error         */
  218. #define HAL_I2C_ERROR_ARLO       0x00000002U    /*!< ARLO error         */
  219. #define HAL_I2C_ERROR_AF         0x00000004U    /*!< AF error           */
  220. #define HAL_I2C_ERROR_OVR        0x00000008U    /*!< OVR error          */
  221. #define HAL_I2C_ERROR_DMA        0x00000010U    /*!< DMA transfer error */
  222. #define HAL_I2C_ERROR_TIMEOUT    0x00000020U    /*!< Timeout Error      */
  223. /**
  224.   * @}
  225.   */
  226.  
  227. /** @defgroup I2C_duty_cycle_in_fast_mode I2C duty cycle in fast mode
  228.   * @{
  229.   */
  230. #define I2C_DUTYCYCLE_2                 0x00000000U
  231. #define I2C_DUTYCYCLE_16_9              I2C_CCR_DUTY
  232. /**
  233.   * @}
  234.   */
  235.  
  236. /** @defgroup I2C_addressing_mode I2C addressing mode
  237.   * @{
  238.   */
  239. #define I2C_ADDRESSINGMODE_7BIT         0x00004000U
  240. #define I2C_ADDRESSINGMODE_10BIT        (I2C_OAR1_ADDMODE | 0x00004000U)
  241. /**
  242.   * @}
  243.   */
  244.  
  245. /** @defgroup I2C_dual_addressing_mode  I2C dual addressing mode
  246.   * @{
  247.   */
  248. #define I2C_DUALADDRESS_DISABLE        0x00000000U
  249. #define I2C_DUALADDRESS_ENABLE         I2C_OAR2_ENDUAL
  250. /**
  251.   * @}
  252.   */
  253.  
  254. /** @defgroup I2C_general_call_addressing_mode I2C general call addressing mode
  255.   * @{
  256.   */
  257. #define I2C_GENERALCALL_DISABLE        0x00000000U
  258. #define I2C_GENERALCALL_ENABLE         I2C_CR1_ENGC
  259. /**
  260.   * @}
  261.   */
  262.  
  263. /** @defgroup I2C_nostretch_mode I2C nostretch mode
  264.   * @{
  265.   */
  266. #define I2C_NOSTRETCH_DISABLE          0x00000000U
  267. #define I2C_NOSTRETCH_ENABLE           I2C_CR1_NOSTRETCH
  268. /**
  269.   * @}
  270.   */
  271.  
  272. /** @defgroup I2C_Memory_Address_Size I2C Memory Address Size
  273.   * @{
  274.   */
  275. #define I2C_MEMADD_SIZE_8BIT            0x00000001U
  276. #define I2C_MEMADD_SIZE_16BIT           0x00000010U
  277. /**
  278.   * @}
  279.   */
  280.  
  281. /** @defgroup I2C_XferDirection_definition I2C XferDirection definition
  282.   * @{
  283.   */
  284. #define I2C_DIRECTION_RECEIVE           0x00000000U
  285. #define I2C_DIRECTION_TRANSMIT          0x00000001U
  286. /**
  287.   * @}
  288.   */
  289.  
  290. /** @defgroup I2C_XferOptions_definition I2C XferOptions definition
  291.   * @{
  292.   */
  293. #define  I2C_FIRST_FRAME                0x00000001U
  294. #define  I2C_NEXT_FRAME                 0x00000002U
  295. #define  I2C_FIRST_AND_LAST_FRAME       0x00000004U
  296. #define  I2C_LAST_FRAME                 0x00000008U
  297. /**
  298.   * @}
  299.   */
  300.  
  301. /** @defgroup I2C_Interrupt_configuration_definition I2C Interrupt configuration definition
  302.   * @{
  303.   */
  304. #define I2C_IT_BUF                      I2C_CR2_ITBUFEN
  305. #define I2C_IT_EVT                      I2C_CR2_ITEVTEN
  306. #define I2C_IT_ERR                      I2C_CR2_ITERREN
  307. /**
  308.   * @}
  309.   */
  310.  
  311. /** @defgroup I2C_Flag_definition I2C Flag definition
  312.   * @{
  313.   */
  314. #define I2C_FLAG_SMBALERT               0x00018000U
  315. #define I2C_FLAG_TIMEOUT                0x00014000U
  316. #define I2C_FLAG_PECERR                 0x00011000U
  317. #define I2C_FLAG_OVR                    0x00010800U
  318. #define I2C_FLAG_AF                     0x00010400U
  319. #define I2C_FLAG_ARLO                   0x00010200U
  320. #define I2C_FLAG_BERR                   0x00010100U
  321. #define I2C_FLAG_TXE                    0x00010080U
  322. #define I2C_FLAG_RXNE                   0x00010040U
  323. #define I2C_FLAG_STOPF                  0x00010010U
  324. #define I2C_FLAG_ADD10                  0x00010008U
  325. #define I2C_FLAG_BTF                    0x00010004U
  326. #define I2C_FLAG_ADDR                   0x00010002U
  327. #define I2C_FLAG_SB                     0x00010001U
  328. #define I2C_FLAG_DUALF                  0x00100080U
  329. #define I2C_FLAG_SMBHOST                0x00100040U
  330. #define I2C_FLAG_SMBDEFAULT             0x00100020U
  331. #define I2C_FLAG_GENCALL                0x00100010U
  332. #define I2C_FLAG_TRA                    0x00100004U
  333. #define I2C_FLAG_BUSY                   0x00100002U
  334. #define I2C_FLAG_MSL                    0x00100001U
  335. /**
  336.   * @}
  337.   */
  338.  
  339. /**
  340.   * @}
  341.   */
  342.  
  343. /* Exported macro ------------------------------------------------------------*/
  344. /** @defgroup I2C_Exported_Macros I2C Exported Macros
  345.   * @{
  346.   */
  347.  
  348. /** @brief Reset I2C handle state
  349.   * @param  __HANDLE__: specifies the I2C Handle.
  350.   *         This parameter can be I2C where x: 1, 2, or 3 to select the I2C peripheral.
  351.   * @retval None
  352.   */
  353. #define __HAL_I2C_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_I2C_STATE_RESET)
  354.  
  355. /** @brief  Enable or disable the specified I2C interrupts.
  356.   * @param  __HANDLE__: specifies the I2C Handle.
  357.   *         This parameter can be I2C where x: 1, 2, or 3 to select the I2C peripheral.
  358.   * @param  __INTERRUPT__: specifies the interrupt source to enable or disable.
  359.   *         This parameter can be one of the following values:
  360.   *            @arg I2C_IT_BUF: Buffer interrupt enable
  361.   *            @arg I2C_IT_EVT: Event interrupt enable
  362.   *            @arg I2C_IT_ERR: Error interrupt enable
  363.   * @retval None
  364.   */
  365. #define __HAL_I2C_ENABLE_IT(__HANDLE__, __INTERRUPT__)   ((__HANDLE__)->Instance->CR2 |= (__INTERRUPT__))
  366. #define __HAL_I2C_DISABLE_IT(__HANDLE__, __INTERRUPT__)  ((__HANDLE__)->Instance->CR2 &= (~(__INTERRUPT__)))
  367.  
  368. /** @brief  Checks if the specified I2C interrupt source is enabled or disabled.
  369.   * @param  __HANDLE__: specifies the I2C Handle.
  370.   *         This parameter can be I2C where x: 1, 2, or 3 to select the I2C peripheral.
  371.   * @param  __INTERRUPT__: specifies the I2C interrupt source to check.
  372.   *          This parameter can be one of the following values:
  373.   *            @arg I2C_IT_BUF: Buffer interrupt enable
  374.   *            @arg I2C_IT_EVT: Event interrupt enable
  375.   *            @arg I2C_IT_ERR: Error interrupt enable
  376.   * @retval The new state of __INTERRUPT__ (TRUE or FALSE).
  377.   */
  378. #define __HAL_I2C_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) ((((__HANDLE__)->Instance->CR2 & (__INTERRUPT__)) == (__INTERRUPT__)) ? SET : RESET)
  379.  
  380. /** @brief  Checks whether the specified I2C flag is set or not.
  381.   * @param  __HANDLE__: specifies the I2C Handle.
  382.   *         This parameter can be I2C where x: 1, 2, or 3 to select the I2C peripheral.
  383.   * @param  __FLAG__: specifies the flag to check.
  384.   *         This parameter can be one of the following values:
  385.   *            @arg I2C_FLAG_SMBALERT: SMBus Alert flag
  386.   *            @arg I2C_FLAG_TIMEOUT: Timeout or Tlow error flag
  387.   *            @arg I2C_FLAG_PECERR: PEC error in reception flag
  388.   *            @arg I2C_FLAG_OVR: Overrun/Underrun flag
  389.   *            @arg I2C_FLAG_AF: Acknowledge failure flag
  390.   *            @arg I2C_FLAG_ARLO: Arbitration lost flag
  391.   *            @arg I2C_FLAG_BERR: Bus error flag
  392.   *            @arg I2C_FLAG_TXE: Data register empty flag
  393.   *            @arg I2C_FLAG_RXNE: Data register not empty flag
  394.   *            @arg I2C_FLAG_STOPF: Stop detection flag
  395.   *            @arg I2C_FLAG_ADD10: 10-bit header sent flag
  396.   *            @arg I2C_FLAG_BTF: Byte transfer finished flag
  397.   *            @arg I2C_FLAG_ADDR: Address sent flag
  398.   *                                Address matched flag
  399.   *            @arg I2C_FLAG_SB: Start bit flag
  400.   *            @arg I2C_FLAG_DUALF: Dual flag
  401.   *            @arg I2C_FLAG_SMBHOST: SMBus host header
  402.   *            @arg I2C_FLAG_SMBDEFAULT: SMBus default header
  403.   *            @arg I2C_FLAG_GENCALL: General call header flag
  404.   *            @arg I2C_FLAG_TRA: Transmitter/Receiver flag
  405.   *            @arg I2C_FLAG_BUSY: Bus busy flag
  406.   *            @arg I2C_FLAG_MSL: Master/Slave flag
  407.   * @retval The new state of __FLAG__ (TRUE or FALSE).
  408.   */
  409. #define __HAL_I2C_GET_FLAG(__HANDLE__, __FLAG__) ((((uint8_t)((__FLAG__) >> 16U)) == 0x01U)?((((__HANDLE__)->Instance->SR1) & ((__FLAG__) & I2C_FLAG_MASK)) == ((__FLAG__) & I2C_FLAG_MASK)): \
  410.                                                  ((((__HANDLE__)->Instance->SR2) & ((__FLAG__) & I2C_FLAG_MASK)) == ((__FLAG__) & I2C_FLAG_MASK)))
  411.  
  412. /** @brief  Clears the I2C pending flags which are cleared by writing 0 in a specific bit.
  413.   * @param  __HANDLE__: specifies the I2C Handle.
  414.   *         This parameter can be I2C where x: 1, 2, or 3 to select the I2C peripheral.
  415.   * @param  __FLAG__: specifies the flag to clear.
  416.   *         This parameter can be any combination of the following values:
  417.   *            @arg I2C_FLAG_SMBALERT: SMBus Alert flag
  418.   *            @arg I2C_FLAG_TIMEOUT: Timeout or Tlow error flag
  419.   *            @arg I2C_FLAG_PECERR: PEC error in reception flag
  420.   *            @arg I2C_FLAG_OVR: Overrun/Underrun flag (Slave mode)
  421.   *            @arg I2C_FLAG_AF: Acknowledge failure flag
  422.   *            @arg I2C_FLAG_ARLO: Arbitration lost flag (Master mode)
  423.   *            @arg I2C_FLAG_BERR: Bus error flag
  424.   * @retval None
  425.   */
  426. #define __HAL_I2C_CLEAR_FLAG(__HANDLE__, __FLAG__) ((__HANDLE__)->Instance->SR1 = ~((__FLAG__) & I2C_FLAG_MASK))
  427.  
  428. /** @brief  Clears the I2C ADDR pending flag.
  429.   * @param  __HANDLE__: specifies the I2C Handle.
  430.   *         This parameter can be I2C where x: 1, 2, or 3 to select the I2C peripheral.
  431.   * @retval None
  432.   */
  433. #define __HAL_I2C_CLEAR_ADDRFLAG(__HANDLE__)    \
  434.   do{                                           \
  435.     __IO uint32_t tmpreg = 0x00U;               \
  436.     tmpreg = (__HANDLE__)->Instance->SR1;       \
  437.     tmpreg = (__HANDLE__)->Instance->SR2;       \
  438.     UNUSED(tmpreg);                             \
  439.   } while(0U)
  440.  
  441. /** @brief  Clears the I2C STOPF pending flag.
  442.   * @param  __HANDLE__: specifies the I2C Handle.
  443.   *         This parameter can be I2C where x: 1, 2, or 3 to select the I2C peripheral.
  444.   * @retval None
  445.   */
  446. #define __HAL_I2C_CLEAR_STOPFLAG(__HANDLE__)    \
  447.   do{                                           \
  448.     __IO uint32_t tmpreg = 0x00U;               \
  449.     tmpreg = (__HANDLE__)->Instance->SR1;       \
  450.     (__HANDLE__)->Instance->CR1 |= I2C_CR1_PE;  \
  451.     UNUSED(tmpreg);                             \
  452.   } while(0U)
  453.    
  454. /** @brief  Enable the I2C peripheral.
  455.   * @param  __HANDLE__: specifies the I2C Handle.
  456.   *         This parameter can be I2Cx where x: 1 or 2  to select the I2C peripheral.
  457.   * @retval None
  458.   */
  459. #define __HAL_I2C_ENABLE(__HANDLE__)                             ((__HANDLE__)->Instance->CR1 |=  I2C_CR1_PE)
  460.  
  461. /** @brief  Disable the I2C peripheral.
  462.   * @param  __HANDLE__: specifies the I2C Handle.
  463.   *         This parameter can be I2Cx where x: 1 or 2  to select the I2C peripheral.
  464.   * @retval None
  465.   */
  466. #define __HAL_I2C_DISABLE(__HANDLE__)                            ((__HANDLE__)->Instance->CR1 &=  ~I2C_CR1_PE)
  467.  
  468. /**
  469.   * @}
  470.   */
  471.  
  472. /* Exported functions --------------------------------------------------------*/
  473. /** @addtogroup I2C_Exported_Functions
  474.   * @{
  475.   */
  476.  
  477. /** @addtogroup I2C_Exported_Functions_Group1
  478.   * @{
  479.   */
  480. /* Initialization/de-initialization functions  **********************************/
  481. HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c);
  482. HAL_StatusTypeDef HAL_I2C_DeInit (I2C_HandleTypeDef *hi2c);
  483. void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c);
  484. void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c);
  485. /**
  486.   * @}
  487.   */
  488.  
  489. /** @addtogroup I2C_Exported_Functions_Group2
  490.   * @{
  491.   */
  492. /* I/O operation functions  *****************************************************/
  493. /******* Blocking mode: Polling */
  494. HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  495. HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  496. HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  497. HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t Timeout);
  498. 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);
  499. 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);
  500. HAL_StatusTypeDef HAL_I2C_IsDeviceReady(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout);
  501.  
  502. /******* Non-Blocking mode: Interrupt */
  503. HAL_StatusTypeDef HAL_I2C_Master_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size);
  504. HAL_StatusTypeDef HAL_I2C_Master_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size);
  505. HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size);
  506. HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size);
  507. 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);
  508. 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);
  509.  
  510. HAL_StatusTypeDef HAL_I2C_Master_Sequential_Transmit_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions);
  511. HAL_StatusTypeDef HAL_I2C_Master_Sequential_Receive_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions);
  512. HAL_StatusTypeDef HAL_I2C_Slave_Sequential_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions);
  513. HAL_StatusTypeDef HAL_I2C_Slave_Sequential_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size, uint32_t XferOptions);
  514. HAL_StatusTypeDef HAL_I2C_Master_Abort_IT(I2C_HandleTypeDef *hi2c, uint16_t DevAddress);
  515. HAL_StatusTypeDef HAL_I2C_EnableListen_IT(I2C_HandleTypeDef *hi2c);
  516. HAL_StatusTypeDef HAL_I2C_DisableListen_IT(I2C_HandleTypeDef *hi2c);
  517.  
  518. /******* Non-Blocking mode: DMA */
  519. HAL_StatusTypeDef HAL_I2C_Master_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size);
  520. HAL_StatusTypeDef HAL_I2C_Master_Receive_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size);
  521. HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size);
  522. HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint16_t Size);
  523. 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);
  524. 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);
  525.  
  526. /******* I2C IRQHandler and Callbacks used in non blocking modes (Interrupt and DMA) */
  527. void HAL_I2C_EV_IRQHandler(I2C_HandleTypeDef *hi2c);
  528. void HAL_I2C_ER_IRQHandler(I2C_HandleTypeDef *hi2c);
  529. void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c);
  530. void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c);
  531. void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c);
  532. void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c);
  533. void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode);
  534. void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c);
  535. void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *hi2c);
  536. void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c);
  537. void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c);
  538. void HAL_I2C_AbortCpltCallback(I2C_HandleTypeDef *hi2c);
  539. /**
  540.   * @}
  541.   */
  542.  
  543. /** @addtogroup I2C_Exported_Functions_Group3
  544.   * @{
  545.   */
  546. /* Peripheral State, Mode and Errors functions  *********************************/
  547. HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *hi2c);
  548. HAL_I2C_ModeTypeDef HAL_I2C_GetMode(I2C_HandleTypeDef *hi2c);
  549. uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c);
  550.  
  551. /**
  552.   * @}
  553.   */
  554.  
  555. /**
  556.   * @}
  557.   */
  558. /* Private types -------------------------------------------------------------*/
  559. /* Private variables ---------------------------------------------------------*/
  560. /* Private constants ---------------------------------------------------------*/
  561. /** @defgroup I2C_Private_Constants I2C Private Constants
  562.   * @{
  563.   */
  564. #define I2C_FLAG_MASK                    0x0000FFFFU
  565. #define I2C_MIN_PCLK_FREQ_STANDARD       2000000U     /*!< 2 MHz                     */
  566. #define I2C_MIN_PCLK_FREQ_FAST           4000000U     /*!< 4 MHz                     */
  567. /**
  568.   * @}
  569.   */
  570.  
  571. /* Private macros ------------------------------------------------------------*/
  572. /** @defgroup I2C_Private_Macros I2C Private Macros
  573.   * @{
  574.   */
  575.  
  576. #define I2C_MIN_PCLK_FREQ(__PCLK__, __SPEED__)             (((__SPEED__) <= 100000U) ? ((__PCLK__) < I2C_MIN_PCLK_FREQ_STANDARD) : ((__PCLK__) < I2C_MIN_PCLK_FREQ_FAST))
  577. #define I2C_CCR_CALCULATION(__PCLK__, __SPEED__, __COEFF__)     (((((__PCLK__) - 1U)/((__SPEED__) * (__COEFF__))) + 1U) & I2C_CCR_CCR)
  578. #define I2C_FREQRANGE(__PCLK__)                            ((__PCLK__)/1000000U)
  579. #define I2C_RISE_TIME(__FREQRANGE__, __SPEED__)            (((__SPEED__) <= 100000U) ? ((__FREQRANGE__) + 1U) : ((((__FREQRANGE__) * 300U) / 1000U) + 1U))
  580. #define I2C_SPEED_STANDARD(__PCLK__, __SPEED__)            ((I2C_CCR_CALCULATION((__PCLK__), (__SPEED__), 2U) < 4U)? 4U:I2C_CCR_CALCULATION((__PCLK__), (__SPEED__), 2U))
  581. #define I2C_SPEED_FAST(__PCLK__, __SPEED__, __DUTYCYCLE__) (((__DUTYCYCLE__) == I2C_DUTYCYCLE_2)? I2C_CCR_CALCULATION((__PCLK__), (__SPEED__), 3U) : (I2C_CCR_CALCULATION((__PCLK__), (__SPEED__), 25U) | I2C_DUTYCYCLE_16_9))
  582. #define I2C_SPEED(__PCLK__, __SPEED__, __DUTYCYCLE__)      (((__SPEED__) <= 100000U)? (I2C_SPEED_STANDARD((__PCLK__), (__SPEED__))) : \
  583.                                                                   ((I2C_SPEED_FAST((__PCLK__), (__SPEED__), (__DUTYCYCLE__)) & I2C_CCR_CCR) == 0U)? 1U : \
  584.                                                                   ((I2C_SPEED_FAST((__PCLK__), (__SPEED__), (__DUTYCYCLE__))) | I2C_CCR_FS))
  585.  
  586. #define I2C_7BIT_ADD_WRITE(__ADDRESS__)                    ((uint8_t)((__ADDRESS__) & (~I2C_OAR1_ADD0)))
  587. #define I2C_7BIT_ADD_READ(__ADDRESS__)                     ((uint8_t)((__ADDRESS__) | I2C_OAR1_ADD0))
  588.  
  589. #define I2C_10BIT_ADDRESS(__ADDRESS__)                     ((uint8_t)((uint16_t)((__ADDRESS__) & (uint16_t)(0x00FFU))))
  590. #define I2C_10BIT_HEADER_WRITE(__ADDRESS__)                ((uint8_t)((uint16_t)((uint16_t)(((uint16_t)((__ADDRESS__) & (uint16_t)(0x0300U))) >> 7U) | (uint16_t)(0x00F0U))))
  591. #define I2C_10BIT_HEADER_READ(__ADDRESS__)                 ((uint8_t)((uint16_t)((uint16_t)(((uint16_t)((__ADDRESS__) & (uint16_t)(0x0300U))) >> 7U) | (uint16_t)(0x00F1U))))
  592.  
  593. #define I2C_MEM_ADD_MSB(__ADDRESS__)                       ((uint8_t)((uint16_t)(((uint16_t)((__ADDRESS__) & (uint16_t)(0xFF00U))) >> 8U)))
  594. #define I2C_MEM_ADD_LSB(__ADDRESS__)                       ((uint8_t)((uint16_t)((__ADDRESS__) & (uint16_t)(0x00FFU))))
  595.  
  596. /** @defgroup I2C_IS_RTC_Definitions I2C Private macros to check input parameters
  597.   * @{
  598.   */
  599. #define IS_I2C_DUTY_CYCLE(CYCLE) (((CYCLE) == I2C_DUTYCYCLE_2) || \
  600.                                   ((CYCLE) == I2C_DUTYCYCLE_16_9))
  601. #define IS_I2C_ADDRESSING_MODE(ADDRESS) (((ADDRESS) == I2C_ADDRESSINGMODE_7BIT) || \
  602.                                          ((ADDRESS) == I2C_ADDRESSINGMODE_10BIT))
  603. #define IS_I2C_DUAL_ADDRESS(ADDRESS) (((ADDRESS) == I2C_DUALADDRESS_DISABLE) || \
  604.                                       ((ADDRESS) == I2C_DUALADDRESS_ENABLE))
  605. #define IS_I2C_GENERAL_CALL(CALL) (((CALL) == I2C_GENERALCALL_DISABLE) || \
  606.                                    ((CALL) == I2C_GENERALCALL_ENABLE))
  607. #define IS_I2C_NO_STRETCH(STRETCH) (((STRETCH) == I2C_NOSTRETCH_DISABLE) || \
  608.                                     ((STRETCH) == I2C_NOSTRETCH_ENABLE))
  609. #define IS_I2C_MEMADD_SIZE(SIZE) (((SIZE) == I2C_MEMADD_SIZE_8BIT) || \
  610.                                   ((SIZE) == I2C_MEMADD_SIZE_16BIT))
  611. #define IS_I2C_CLOCK_SPEED(SPEED) (((SPEED) > 0) && ((SPEED) <= 400000U))
  612. #define IS_I2C_OWN_ADDRESS1(ADDRESS1) (((ADDRESS1) & (uint32_t)(0xFFFFFC00U)) == 0U)
  613. #define IS_I2C_OWN_ADDRESS2(ADDRESS2) (((ADDRESS2) & (uint32_t)(0xFFFFFF01U)) == 0U)
  614. #define IS_I2C_TRANSFER_OPTIONS_REQUEST(REQUEST)      (((REQUEST) == I2C_FIRST_FRAME)              || \
  615.                                                        ((REQUEST) == I2C_NEXT_FRAME)               || \
  616.                                                        ((REQUEST) == I2C_FIRST_AND_LAST_FRAME)     || \
  617.                                                        ((REQUEST) == I2C_LAST_FRAME))
  618. /**
  619.   * @}
  620.   */
  621.  
  622. /**
  623.   * @}
  624.   */
  625.  
  626. /* Private functions ---------------------------------------------------------*/
  627. /** @defgroup I2C_Private_Functions I2C Private Functions
  628.   * @{
  629.   */
  630.  
  631. /**
  632.   * @}
  633.   */
  634.  
  635. /**
  636.   * @}
  637.   */
  638.  
  639. /**
  640.   * @}
  641.   */
  642.  
  643. #ifdef __cplusplus
  644. }
  645. #endif
  646.  
  647.  
  648. #endif /* __STM32F1xx_HAL_I2C_H */
  649.  
  650. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  651.