STM32F439xx HAL User Manual
stm32f4xx_ll_i2c.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f4xx_ll_i2c.c
00004   * @author  MCD Application Team
00005   * @brief   I2C LL module driver.
00006   ******************************************************************************
00007   * @attention
00008   *
00009   * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
00010   *
00011   * Redistribution and use in source and binary forms, with or without modification,
00012   * are permitted provided that the following conditions are met:
00013   *   1. Redistributions of source code must retain the above copyright notice,
00014   *      this list of conditions and the following disclaimer.
00015   *   2. Redistributions in binary form must reproduce the above copyright notice,
00016   *      this list of conditions and the following disclaimer in the documentation
00017   *      and/or other materials provided with the distribution.
00018   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00019   *      may be used to endorse or promote products derived from this software
00020   *      without specific prior written permission.
00021   *
00022   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00023   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00025   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00026   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00028   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00030   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00031   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032   *
00033   ******************************************************************************
00034   */
00035 #if defined(USE_FULL_LL_DRIVER)
00036 
00037 /* Includes ------------------------------------------------------------------*/
00038 #include "stm32f4xx_ll_i2c.h"
00039 #include "stm32f4xx_ll_bus.h"
00040 #include "stm32f4xx_ll_rcc.h"
00041 #ifdef  USE_FULL_ASSERT
00042 #include "stm32_assert.h"
00043 #else
00044 #define assert_param(expr) ((void)0U)
00045 #endif
00046 
00047 /** @addtogroup STM32F4xx_LL_Driver
00048   * @{
00049   */
00050 
00051 #if defined (I2C1) || defined (I2C2) || defined (I2C3)
00052 
00053 /** @defgroup I2C_LL I2C
00054   * @{
00055   */
00056 
00057 /* Private types -------------------------------------------------------------*/
00058 /* Private variables ---------------------------------------------------------*/
00059 /* Private constants ---------------------------------------------------------*/
00060 /* Private macros ------------------------------------------------------------*/
00061 /** @addtogroup I2C_LL_Private_Macros
00062   * @{
00063   */
00064 
00065 #define IS_LL_I2C_PERIPHERAL_MODE(__VALUE__)    (((__VALUE__) == LL_I2C_MODE_I2C)          || \
00066                                                  ((__VALUE__) == LL_I2C_MODE_SMBUS_HOST)   || \
00067                                                  ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE) || \
00068                                                  ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE_ARP))
00069 
00070 #define IS_LL_I2C_CLOCK_SPEED(__VALUE__)        (((__VALUE__) > 0U) && ((__VALUE__) <= LL_I2C_MAX_SPEED_FAST))
00071 
00072 #define IS_LL_I2C_DUTY_CYCLE(__VALUE__)         (((__VALUE__) == LL_I2C_DUTYCYCLE_2) || \
00073                                                  ((__VALUE__) == LL_I2C_DUTYCYCLE_16_9))
00074 
00075 #if  defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF)
00076 #define IS_LL_I2C_ANALOG_FILTER(__VALUE__)      (((__VALUE__) == LL_I2C_ANALOGFILTER_ENABLE) || \
00077                                                  ((__VALUE__) == LL_I2C_ANALOGFILTER_DISABLE))
00078 
00079 #define IS_LL_I2C_DIGITAL_FILTER(__VALUE__)     ((__VALUE__) <= 0x0000000FU)
00080 
00081 #endif
00082 #define IS_LL_I2C_OWN_ADDRESS1(__VALUE__)       ((__VALUE__) <= 0x000003FFU)
00083 
00084 #define IS_LL_I2C_TYPE_ACKNOWLEDGE(__VALUE__)   (((__VALUE__) == LL_I2C_ACK) || \
00085                                                  ((__VALUE__) == LL_I2C_NACK))
00086 
00087 #define IS_LL_I2C_OWN_ADDRSIZE(__VALUE__)       (((__VALUE__) == LL_I2C_OWNADDRESS1_7BIT) || \
00088                                                  ((__VALUE__) == LL_I2C_OWNADDRESS1_10BIT))
00089 /**
00090   * @}
00091   */
00092 
00093 /* Private function prototypes -----------------------------------------------*/
00094 
00095 /* Exported functions --------------------------------------------------------*/
00096 /** @addtogroup I2C_LL_Exported_Functions
00097   * @{
00098   */
00099 
00100 /** @addtogroup I2C_LL_EF_Init
00101   * @{
00102   */
00103 
00104 /**
00105   * @brief  De-initialize the I2C registers to their default reset values.
00106   * @param  I2Cx I2C Instance.
00107   * @retval An ErrorStatus enumeration value:
00108   *          - SUCCESS: I2C registers are de-initialized
00109   *          - ERROR: I2C registers are not de-initialized
00110   */
00111 uint32_t LL_I2C_DeInit(I2C_TypeDef *I2Cx)
00112 {
00113   ErrorStatus status = SUCCESS;
00114 
00115   /* Check the I2C Instance I2Cx */
00116   assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
00117 
00118   if (I2Cx == I2C1)
00119   {
00120     /* Force reset of I2C clock */
00121     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C1);
00122 
00123     /* Release reset of I2C clock */
00124     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C1);
00125   }
00126   else if (I2Cx == I2C2)
00127   {
00128     /* Force reset of I2C clock */
00129     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C2);
00130 
00131     /* Release reset of I2C clock */
00132     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C2);
00133 
00134   }
00135 #if defined(I2C3)
00136   else if (I2Cx == I2C3)
00137   {
00138     /* Force reset of I2C clock */
00139     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C3);
00140 
00141     /* Release reset of I2C clock */
00142     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C3);
00143   }
00144 #endif
00145   else
00146   {
00147     status = ERROR;
00148   }
00149 
00150   return status;
00151 }
00152 
00153 /**
00154   * @brief  Initialize the I2C registers according to the specified parameters in I2C_InitStruct.
00155   * @param  I2Cx I2C Instance.
00156   * @param  I2C_InitStruct pointer to a @ref LL_I2C_InitTypeDef structure.
00157   * @retval An ErrorStatus enumeration value:
00158   *          - SUCCESS: I2C registers are initialized
00159   *          - ERROR: Not applicable
00160   */
00161 uint32_t LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct)
00162 {
00163   LL_RCC_ClocksTypeDef rcc_clocks;
00164 
00165   /* Check the I2C Instance I2Cx */
00166   assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
00167 
00168   /* Check the I2C parameters from I2C_InitStruct */
00169   assert_param(IS_LL_I2C_PERIPHERAL_MODE(I2C_InitStruct->PeripheralMode));
00170   assert_param(IS_LL_I2C_CLOCK_SPEED(I2C_InitStruct->ClockSpeed));
00171   assert_param(IS_LL_I2C_DUTY_CYCLE(I2C_InitStruct->DutyCycle));
00172 #if  defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF)
00173   assert_param(IS_LL_I2C_ANALOG_FILTER(I2C_InitStruct->AnalogFilter));
00174   assert_param(IS_LL_I2C_DIGITAL_FILTER(I2C_InitStruct->DigitalFilter));
00175 #endif
00176   assert_param(IS_LL_I2C_OWN_ADDRESS1(I2C_InitStruct->OwnAddress1));
00177   assert_param(IS_LL_I2C_TYPE_ACKNOWLEDGE(I2C_InitStruct->TypeAcknowledge));
00178   assert_param(IS_LL_I2C_OWN_ADDRSIZE(I2C_InitStruct->OwnAddrSize));
00179 
00180   /* Disable the selected I2Cx Peripheral */
00181   LL_I2C_Disable(I2Cx);
00182 
00183   /* Retrieve Clock frequencies */
00184   LL_RCC_GetSystemClocksFreq(&rcc_clocks);
00185 
00186 #if  defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF)
00187   /*---------------------------- I2Cx FLTR Configuration -----------------------
00188    * Configure the analog and digital noise filters with parameters :
00189    * - AnalogFilter: I2C_FLTR_ANFOFF bit
00190    * - DigitalFilter: I2C_FLTR_DNF[3:0] bits
00191    */
00192   LL_I2C_ConfigFilters(I2Cx, I2C_InitStruct->AnalogFilter, I2C_InitStruct->DigitalFilter);
00193 
00194 #endif
00195   /*---------------------------- I2Cx SCL Clock Speed Configuration ------------
00196    * Configure the SCL speed :
00197    * - ClockSpeed: I2C_CR2_FREQ[5:0], I2C_TRISE_TRISE[5:0], I2C_CCR_FS,
00198    *           and I2C_CCR_CCR[11:0] bits
00199    * - DutyCycle: I2C_CCR_DUTY[7:0] bits
00200    */
00201   LL_I2C_ConfigSpeed(I2Cx, rcc_clocks.PCLK1_Frequency, I2C_InitStruct->ClockSpeed, I2C_InitStruct->DutyCycle);
00202 
00203   /*---------------------------- I2Cx OAR1 Configuration -----------------------
00204    * Disable, Configure and Enable I2Cx device own address 1 with parameters :
00205    * - OwnAddress1:  I2C_OAR1_ADD[9:8], I2C_OAR1_ADD[7:1] and I2C_OAR1_ADD0 bits
00206    * - OwnAddrSize:  I2C_OAR1_ADDMODE bit
00207    */
00208   LL_I2C_SetOwnAddress1(I2Cx, I2C_InitStruct->OwnAddress1, I2C_InitStruct->OwnAddrSize);
00209 
00210   /*---------------------------- I2Cx MODE Configuration -----------------------
00211   * Configure I2Cx peripheral mode with parameter :
00212    * - PeripheralMode: I2C_CR1_SMBUS, I2C_CR1_SMBTYPE and I2C_CR1_ENARP bits
00213    */
00214   LL_I2C_SetMode(I2Cx, I2C_InitStruct->PeripheralMode);
00215 
00216   /* Enable the selected I2Cx Peripheral */
00217   LL_I2C_Enable(I2Cx);
00218 
00219   /*---------------------------- I2Cx CR2 Configuration ------------------------
00220    * Configure the ACKnowledge or Non ACKnowledge condition
00221    * after the address receive match code or next received byte with parameter :
00222    * - TypeAcknowledge: I2C_CR2_NACK bit
00223    */
00224   LL_I2C_AcknowledgeNextData(I2Cx, I2C_InitStruct->TypeAcknowledge);
00225 
00226   return SUCCESS;
00227 }
00228 
00229 /**
00230   * @brief  Set each @ref LL_I2C_InitTypeDef field to default value.
00231   * @param  I2C_InitStruct Pointer to a @ref LL_I2C_InitTypeDef structure.
00232   * @retval None
00233   */
00234 void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct)
00235 {
00236   /* Set I2C_InitStruct fields to default values */
00237   I2C_InitStruct->PeripheralMode  = LL_I2C_MODE_I2C;
00238   I2C_InitStruct->ClockSpeed      = 5000U;
00239   I2C_InitStruct->DutyCycle       = LL_I2C_DUTYCYCLE_2;
00240 #if  defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF)
00241   I2C_InitStruct->AnalogFilter    = LL_I2C_ANALOGFILTER_ENABLE;
00242   I2C_InitStruct->DigitalFilter   = 0U;
00243 #endif
00244   I2C_InitStruct->OwnAddress1     = 0U;
00245   I2C_InitStruct->TypeAcknowledge = LL_I2C_NACK;
00246   I2C_InitStruct->OwnAddrSize     = LL_I2C_OWNADDRESS1_7BIT;
00247 }
00248 
00249 /**
00250   * @}
00251   */
00252 
00253 /**
00254   * @}
00255   */
00256 
00257 /**
00258   * @}
00259   */
00260 
00261 #endif /* I2C1 || I2C2 || I2C3 */
00262 
00263 /**
00264   * @}
00265   */
00266 
00267 #endif /* USE_FULL_LL_DRIVER */
00268 
00269 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/