STM32L443xx HAL User Manual
stm32l4xx_ll_i2c.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32l4xx_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 "stm32l4xx_ll_i2c.h"
00022 #include "stm32l4xx_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 STM32L4xx_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(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 #if defined(I2C2)
00102   else if (I2Cx == I2C2)
00103   {
00104     /* Force reset of I2C clock */
00105     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C2);
00106 
00107     /* Release reset of I2C clock */
00108     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C2);
00109 
00110   }
00111 #endif /* I2C2 */
00112   else if (I2Cx == I2C3)
00113   {
00114     /* Force reset of I2C clock */
00115     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C3);
00116 
00117     /* Release reset of I2C clock */
00118     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C3);
00119   }
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, 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));
00156   assert_param(IS_LL_I2C_TYPE_ACKNOWLEDGE(I2C_InitStruct->TypeAcknowledge));
00157   assert_param(IS_LL_I2C_OWN_ADDRSIZE(I2C_InitStruct->OwnAddrSize));
00158 
00159   /* Disable the selected I2Cx Peripheral */
00160   LL_I2C_Disable(I2Cx);
00161 
00162   /*---------------------------- I2Cx CR1 Configuration ------------------------
00163    * Configure the analog and digital noise filters with parameters :
00164    * - AnalogFilter: I2C_CR1_ANFOFF bit
00165    * - DigitalFilter: I2C_CR1_DNF[3:0] bits
00166    */
00167   LL_I2C_ConfigFilters(I2Cx, I2C_InitStruct->AnalogFilter, I2C_InitStruct->DigitalFilter);
00168 
00169   /*---------------------------- I2Cx TIMINGR Configuration --------------------
00170    * Configure the SDA setup, hold time and the SCL high, low period with parameter :
00171    * - Timing: I2C_TIMINGR_PRESC[3:0], I2C_TIMINGR_SCLDEL[3:0], I2C_TIMINGR_SDADEL[3:0],
00172    *           I2C_TIMINGR_SCLH[7:0] and I2C_TIMINGR_SCLL[7:0] bits
00173    */
00174   LL_I2C_SetTiming(I2Cx, I2C_InitStruct->Timing);
00175 
00176   /* Enable the selected I2Cx Peripheral */
00177   LL_I2C_Enable(I2Cx);
00178 
00179   /*---------------------------- I2Cx OAR1 Configuration -----------------------
00180    * Disable, Configure and Enable I2Cx device own address 1 with parameters :
00181    * - OwnAddress1:  I2C_OAR1_OA1[9:0] bits
00182    * - OwnAddrSize:  I2C_OAR1_OA1MODE bit
00183    */
00184   LL_I2C_DisableOwnAddress1(I2Cx);
00185   LL_I2C_SetOwnAddress1(I2Cx, I2C_InitStruct->OwnAddress1, I2C_InitStruct->OwnAddrSize);
00186 
00187   /* OwnAdress1 == 0 is reserved for General Call address */
00188   if (I2C_InitStruct->OwnAddress1 != 0U)
00189   {
00190     LL_I2C_EnableOwnAddress1(I2Cx);
00191   }
00192 
00193   /*---------------------------- I2Cx MODE Configuration -----------------------
00194   * Configure I2Cx peripheral mode with parameter :
00195    * - PeripheralMode: I2C_CR1_SMBDEN and I2C_CR1_SMBHEN bits
00196    */
00197   LL_I2C_SetMode(I2Cx, I2C_InitStruct->PeripheralMode);
00198 
00199   /*---------------------------- I2Cx CR2 Configuration ------------------------
00200    * Configure the ACKnowledge or Non ACKnowledge condition
00201    * after the address receive match code or next received byte with parameter :
00202    * - TypeAcknowledge: I2C_CR2_NACK bit
00203    */
00204   LL_I2C_AcknowledgeNextData(I2Cx, I2C_InitStruct->TypeAcknowledge);
00205 
00206   return SUCCESS;
00207 }
00208 
00209 /**
00210   * @brief  Set each @ref LL_I2C_InitTypeDef field to default value.
00211   * @param  I2C_InitStruct Pointer to a @ref LL_I2C_InitTypeDef structure.
00212   * @retval None
00213   */
00214 void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct)
00215 {
00216   /* Set I2C_InitStruct fields to default values */
00217   I2C_InitStruct->PeripheralMode  = LL_I2C_MODE_I2C;
00218   I2C_InitStruct->Timing          = 0U;
00219   I2C_InitStruct->AnalogFilter    = LL_I2C_ANALOGFILTER_ENABLE;
00220   I2C_InitStruct->DigitalFilter   = 0U;
00221   I2C_InitStruct->OwnAddress1     = 0U;
00222   I2C_InitStruct->TypeAcknowledge = LL_I2C_NACK;
00223   I2C_InitStruct->OwnAddrSize     = LL_I2C_OWNADDRESS1_7BIT;
00224 }
00225 
00226 /**
00227   * @}
00228   */
00229 
00230 /**
00231   * @}
00232   */
00233 
00234 /**
00235   * @}
00236   */
00237 
00238 #endif /* I2C1 || I2C2 || I2C3 || I2C4 */
00239 
00240 /**
00241   * @}
00242   */
00243 
00244 #endif /* USE_FULL_LL_DRIVER */