STM32G474xx HAL用户手册
stm32g4xx_ll_i2c.c
转到此文件的文档。
00001 /**
00002   ******************************************************************************
00003   * @file    stm32g4xx_ll_i2c.c
00004   * @author  MCD Application Team
00005   * @brief   I2C LL module driver.
00006   ******************************************************************************
00007   * @attention
00008   *
00009   * Copyright (c) 2019 STMicroelectronics.
00010   * All rights reserved.
00011   *
00012   * This software is licensed under terms that can be found in the LICENSE file
00013   * in the root directory of this software component.
00014   * If no LICENSE file comes with this software, it is provided AS-IS.
00015   *
00016   ******************************************************************************
00017   */
00018 #if defined(USE_FULL_LL_DRIVER)
00019 
00020 /* Includes ------------------------------------------------------------------*/
00021 #include "stm32g4xx_ll_i2c.h"
00022 #include "stm32g4xx_ll_bus.h"
00023 #ifdef  USE_FULL_ASSERT
00024 #include "stm32_assert.h"
00025 #else
00026 #define assert_param(expr) ((void)0U)
00027 #endif /* USE_FULL_ASSERT */
00028 
00029 /** @addtogroup STM32G4xx_LL_Driver
00030   * @{
00031   */
00032 
00033 #if defined (I2C1) || defined (I2C2) || defined (I2C3) || defined (I2C4)
00034 
00035 /** @defgroup I2C_LL I2C
00036   * @{
00037   */
00038 
00039 /* Private types -------------------------------------------------------------*/
00040 /* Private variables ---------------------------------------------------------*/
00041 /* Private constants ---------------------------------------------------------*/
00042 /* Private macros ------------------------------------------------------------*/
00043 /** @addtogroup I2C_LL_Private_Macros
00044   * @{
00045   */
00046 
00047 #define IS_LL_I2C_PERIPHERAL_MODE(__VALUE__)    (((__VALUE__) == LL_I2C_MODE_I2C)          || \
00048                                                  ((__VALUE__) == LL_I2C_MODE_SMBUS_HOST)   || \
00049                                                  ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE) || \
00050                                                  ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE_ARP))
00051 
00052 #define IS_LL_I2C_ANALOG_FILTER(__VALUE__)      (((__VALUE__) == LL_I2C_ANALOGFILTER_ENABLE) || \
00053                                                  ((__VALUE__) == LL_I2C_ANALOGFILTER_DISABLE))
00054 
00055 #define IS_LL_I2C_DIGITAL_FILTER(__VALUE__)     ((__VALUE__) <= 0x0000000FU)
00056 
00057 #define IS_LL_I2C_OWN_ADDRESS1(__VALUE__)       ((__VALUE__) <= 0x000003FFU)
00058 
00059 #define IS_LL_I2C_TYPE_ACKNOWLEDGE(__VALUE__)   (((__VALUE__) == LL_I2C_ACK) || \
00060                                                  ((__VALUE__) == LL_I2C_NACK))
00061 
00062 #define IS_LL_I2C_OWN_ADDRSIZE(__VALUE__)       (((__VALUE__) == LL_I2C_OWNADDRESS1_7BIT) || \
00063                                                  ((__VALUE__) == LL_I2C_OWNADDRESS1_10BIT))
00064 /**
00065   * @}
00066   */
00067 
00068 /* Private function prototypes -----------------------------------------------*/
00069 
00070 /* Exported functions --------------------------------------------------------*/
00071 /** @addtogroup I2C_LL_Exported_Functions
00072   * @{
00073   */
00074 
00075 /** @addtogroup I2C_LL_EF_Init
00076   * @{
00077   */
00078 
00079 /**
00080   * @brief  De-initialize the I2C registers to their default reset values.
00081   * @param  I2Cx I2C Instance.
00082   * @retval An ErrorStatus enumeration value:
00083   *          - SUCCESS: I2C registers are de-initialized
00084   *          - ERROR: I2C registers are not de-initialized
00085   */
00086 ErrorStatus LL_I2C_DeInit(const I2C_TypeDef *I2Cx)
00087 {
00088   ErrorStatus status = SUCCESS;
00089 
00090   /* Check the I2C Instance I2Cx */
00091   assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
00092 
00093   if (I2Cx == I2C1)
00094   {
00095     /* Force reset of I2C clock */
00096     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C1);
00097 
00098     /* Release reset of I2C clock */
00099     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C1);
00100   }
00101   else if (I2Cx == I2C2)
00102   {
00103     /* Force reset of I2C clock */
00104     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C2);
00105 
00106     /* Release reset of I2C clock */
00107     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C2);
00108 
00109   }
00110 #if defined(I2C3)
00111   else if (I2Cx == I2C3)
00112   {
00113     /* Force reset of I2C clock */
00114     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C3);
00115 
00116     /* Release reset of I2C clock */
00117     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C3);
00118   }
00119 #endif /* I2C3 */
00120 #if defined(I2C4)
00121   else if (I2Cx == I2C4)
00122   {
00123     /* Force reset of I2C clock */
00124     LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_I2C4);
00125 
00126     /* Release reset of I2C clock */
00127     LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_I2C4);
00128   }
00129 #endif /* I2C4 */
00130   else
00131   {
00132     status = ERROR;
00133   }
00134 
00135   return status;
00136 }
00137 
00138 /**
00139   * @brief  Initialize the I2C registers according to the specified parameters in I2C_InitStruct.
00140   * @param  I2Cx I2C Instance.
00141   * @param  I2C_InitStruct pointer to a @ref LL_I2C_InitTypeDef structure.
00142   * @retval An ErrorStatus enumeration value:
00143   *          - SUCCESS: I2C registers are initialized
00144   *          - ERROR: Not applicable
00145   */
00146 ErrorStatus LL_I2C_Init(I2C_TypeDef *I2Cx, const LL_I2C_InitTypeDef *I2C_InitStruct)
00147 {
00148   /* Check the I2C Instance I2Cx */
00149   assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
00150 
00151   /* Check the I2C parameters from I2C_InitStruct */
00152   assert_param(IS_LL_I2C_PERIPHERAL_MODE(I2C_InitStruct->PeripheralMode));
00153   assert_param(IS_LL_I2C_ANALOG_FILTER(I2C_InitStruct->AnalogFilter));
00154   assert_param(IS_LL_I2C_DIGITAL_FILTER(I2C_InitStruct->DigitalFilter));
00155   assert_param(IS_LL_I2C_OWN_ADDRESS1(I2C_InitStruct->OwnAddress1));