STM32H735xx HAL User Manual
stm32h7xx_ll_i2c.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32h7xx_ll_i2c.c
00004   * @author  MCD Application Team
00005   * @brief   I2C LL module driver.
00006   ******************************************************************************
00007   * @attention
00008   *
00009   * Copyright (c) 2017 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 "stm32h7xx_ll_i2c.h"
00022 #include "stm32h7xx_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 STM32H7xx_LL_Driver
00030   * @{
00031   */
00032 
00033 #if defined (I2C1) || defined (I2C2) || defined (I2C3) || defined (I2C4) || defined (I2C5)
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(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   else if (I2Cx == I2C3)
00111   {
00112     /* Force reset of I2C clock */
00113     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C3);
00114 
00115     /* Release reset of I2C clock */
00116     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C3);
00117   }
00118   else if (I2Cx == I2C4)
00119   {
00120     /* Force reset of I2C clock */
00121     LL_APB4_GRP1_ForceReset(LL_APB4_GRP1_PERIPH_I2C4);
00122 
00123     /* Release reset of I2C clock */
00124     LL_APB4_GRP1_ReleaseReset(LL_APB4_GRP1_PERIPH_I2C4);
00125   }
00126 #if defined(I2C5)
00127   else if (I2Cx == I2C5)
00128   {
00129     /* Force reset of I2C clock */
00130     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C5);
00131 
00132     /* Release reset of I2C clock */
00133     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C5);
00134   }
00135 #endif /* I2C5 */
00136   else
00137   {
00138     status = ERROR;
00139   }
00140 
00141   return status;
00142 }
00143 
00144 /**
00145   * @brief  Initialize the I2C registers according to the specified parameters in I2C_InitStruct.
00146   * @param  I2Cx I2C Instance.
00147   * @param  I2C_InitStruct pointer to a @ref LL_I2C_InitTypeDef structure.
00148   * @retval An ErrorStatus enumeration value:
00149   *          - SUCCESS: I2C registers are initialized
00150   *          - ERROR: Not applicable
00151   */
00152 ErrorStatus LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct)
00153 {
00154   /* Check the I2C Instance I2Cx */
00155   assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
00156 
00157   /* Check the I2C parameters from I2C_InitStruct */
00158   assert_param(IS_LL_I2C_PERIPHERAL_MODE(I2C_InitStruct->PeripheralMode));
00159   assert_param(IS_LL_I2C_ANALOG_FILTER(I2C_InitStruct->AnalogFilter));
00160   assert_param(IS_LL_I2C_DIGITAL_FILTER(I2C_InitStruct->DigitalFilter));
00161   assert_param(IS_LL_I2C_OWN_ADDRESS1(I2C_InitStruct->OwnAddress1));
00162   assert_param(IS_LL_I2C_TYPE_ACKNOWLEDGE(I2C_InitStruct->TypeAcknowledge));
00163   assert_param(IS_LL_I2C_OWN_ADDRSIZE(I2C_InitStruct->OwnAddrSize));
00164 
00165   /* Disable the selected I2Cx Peripheral */
00166   LL_I2C_Disable(I2Cx);
00167 
00168   /*---------------------------- I2Cx CR1 Configuration ------------------------
00169    * Configure the analog and digital noise filters with parameters :
00170    * - AnalogFilter: I2C_CR1_ANFOFF bit
00171    * - DigitalFilter: I2C_CR1_DNF[3:0] bits
00172    */
00173   LL_I2C_ConfigFilters(I2Cx, I2C_InitStruct->AnalogFilter, I2C_InitStruct->DigitalFilter);
00174 
00175   /*---------------------------- I2Cx TIMINGR Configuration --------------------
00176    * Configure the SDA setup, hold time and the SCL high, low period with parameter :
00177    * - Timing: I2C_TIMINGR_PRESC[3:0], I2C_TIMINGR_SCLDEL[3:0], I2C_TIMINGR_SDADEL[3:0],
00178    *           I2C_TIMINGR_SCLH[7:0] and I2C_TIMINGR_SCLL[7:0] bits
00179    */
00180   LL_I2C_SetTiming(I2Cx, I2C_InitStruct->Timing);
00181 
00182   /* Enable the selected I2Cx Peripheral */
00183   LL_I2C_Enable(I2Cx);
00184 
00185   /*---------------------------- I2Cx OAR1 Configuration -----------------------
00186    * Disable, Configure and Enable I2Cx device own address 1 with parameters :
00187    * - OwnAddress1:  I2C_OAR1_OA1[9:0] bits
00188    * - OwnAddrSize:  I2C_OAR1_OA1MODE bit
00189    */
00190   LL_I2C_DisableOwnAddress1(I2Cx);
00191   LL_I2C_SetOwnAddress1(I2Cx, I2C_InitStruct->OwnAddress1, I2C_InitStruct->OwnAddrSize);
00192 
00193   /* OwnAdress1 == 0 is reserved for General Call address */
00194   if (I2C_InitStruct->OwnAddress1 != 0U)
00195   {
00196     LL_I2C_EnableOwnAddress1(I2Cx);
00197   }
00198 
00199   /*---------------------------- I2Cx MODE Configuration -----------------------
00200   * Configure I2Cx peripheral mode with parameter :
00201    * - PeripheralMode: I2C_CR1_SMBDEN and I2C_CR1_SMBHEN bits
00202    */
00203   LL_I2C_SetMode(I2Cx, I2C_InitStruct->PeripheralMode);
00204 
00205   /*---------------------------- I2Cx CR2 Configuration ------------------------
00206    * Configure the ACKnowledge or Non ACKnowledge condition
00207    * after the address receive match code or next received byte with parameter :
00208    * - TypeAcknowledge: I2C_CR2_NACK bit
00209    */
00210   LL_I2C_AcknowledgeNextData(I2Cx, I2C_InitStruct->TypeAcknowledge);
00211 
00212   return SUCCESS;
00213 }
00214 
00215 /**
00216   * @brief  Set each @ref LL_I2C_InitTypeDef field to default value.
00217   * @param  I2C_InitStruct Pointer to a @ref LL_I2C_InitTypeDef structure.
00218   * @retval None
00219   */
00220 void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct)
00221 {
00222   /* Set I2C_InitStruct fields to default values */
00223   I2C_InitStruct->PeripheralMode  = LL_I2C_MODE_I2C;
00224   I2C_InitStruct->Timing          = 0U;
00225   I2C_InitStruct->AnalogFilter    = LL_I2C_ANALOGFILTER_ENABLE;
00226   I2C_InitStruct->DigitalFilter   = 0U;
00227   I2C_InitStruct->OwnAddress1     = 0U;
00228   I2C_InitStruct->TypeAcknowledge = LL_I2C_NACK;
00229   I2C_InitStruct->OwnAddrSize     = LL_I2C_OWNADDRESS1_7BIT;
00230 }
00231 
00232 /**
00233   * @}
00234   */
00235 
00236 /**
00237   * @}
00238   */
00239 
00240 /**
00241   * @}
00242   */
00243 
00244 #endif /* I2C1 || I2C2 || I2C3 || I2C4  || I2C5 */
00245 
00246 /**
00247   * @}
00248   */
00249 
00250 #endif /* USE_FULL_LL_DRIVER */