STM32F103xB HAL User Manual
stm32f1xx_ll_i2c.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f1xx_ll_i2c.c
00004   * @author  MCD Application Team
00005   * @brief   I2C LL module driver.
00006   ******************************************************************************
00007   * @attention
00008   *
00009   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
00010   * All rights reserved.</center></h2>
00011   *
00012   * This software component is licensed by ST under BSD 3-Clause license,
00013   * the "License"; You may not use this file except in compliance with the
00014   * License. You may obtain a copy of the License at:
00015   *                        opensource.org/licenses/BSD-3-Clause
00016   *
00017   ******************************************************************************
00018   */
00019 #if defined(USE_FULL_LL_DRIVER)
00020 
00021 /* Includes ------------------------------------------------------------------*/
00022 #include "stm32f1xx_ll_i2c.h"
00023 #include "stm32f1xx_ll_bus.h"
00024 #include "stm32f1xx_ll_rcc.h"
00025 #ifdef  USE_FULL_ASSERT
00026 #include "stm32_assert.h"
00027 #else
00028 #define assert_param(expr) ((void)0U)
00029 #endif
00030 
00031 /** @addtogroup STM32F1xx_LL_Driver
00032   * @{
00033   */
00034 
00035 #if defined (I2C1) || defined (I2C2)
00036 
00037 /** @defgroup I2C_LL I2C
00038   * @{
00039   */
00040 
00041 /* Private types -------------------------------------------------------------*/
00042 /* Private variables ---------------------------------------------------------*/
00043 /* Private constants ---------------------------------------------------------*/
00044 /* Private macros ------------------------------------------------------------*/
00045 /** @addtogroup I2C_LL_Private_Macros
00046   * @{
00047   */
00048 
00049 #define IS_LL_I2C_PERIPHERAL_MODE(__VALUE__)    (((__VALUE__) == LL_I2C_MODE_I2C)          || \
00050                                                  ((__VALUE__) == LL_I2C_MODE_SMBUS_HOST)   || \
00051                                                  ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE) || \
00052                                                  ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE_ARP))
00053 
00054 #define IS_LL_I2C_CLOCK_SPEED(__VALUE__)           (((__VALUE__) > 0U) && ((__VALUE__) <= LL_I2C_MAX_SPEED_FAST))
00055 
00056 #define IS_LL_I2C_DUTY_CYCLE(__VALUE__)            (((__VALUE__) == LL_I2C_DUTYCYCLE_2) || \
00057                                                  ((__VALUE__) == LL_I2C_DUTYCYCLE_16_9))
00058 
00059 #define IS_LL_I2C_OWN_ADDRESS1(__VALUE__)       ((__VALUE__) <= 0x000003FFU)
00060 
00061 #define IS_LL_I2C_TYPE_ACKNOWLEDGE(__VALUE__)   (((__VALUE__) == LL_I2C_ACK) || \
00062                                                  ((__VALUE__) == LL_I2C_NACK))
00063 
00064 #define IS_LL_I2C_OWN_ADDRSIZE(__VALUE__)       (((__VALUE__) == LL_I2C_OWNADDRESS1_7BIT) || \
00065                                                  ((__VALUE__) == LL_I2C_OWNADDRESS1_10BIT))
00066 /**
00067   * @}
00068   */
00069 
00070 /* Private function prototypes -----------------------------------------------*/
00071 
00072 /* Exported functions --------------------------------------------------------*/
00073 /** @addtogroup I2C_LL_Exported_Functions
00074   * @{
00075   */
00076 
00077 /** @addtogroup I2C_LL_EF_Init
00078   * @{
00079   */
00080 
00081 /**
00082   * @brief  De-initialize the I2C registers to their default reset values.
00083   * @param  I2Cx I2C Instance.
00084   * @retval An ErrorStatus enumeration value:
00085   *          - SUCCESS  I2C registers are de-initialized
00086   *          - ERROR  I2C registers are not de-initialized
00087   */
00088 uint32_t LL_I2C_DeInit(I2C_TypeDef *I2Cx)
00089 {
00090   ErrorStatus status = SUCCESS;
00091 
00092   /* Check the I2C Instance I2Cx */
00093   assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
00094 
00095   if (I2Cx == I2C1)
00096   {
00097     /* Force reset of I2C clock */
00098     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C1);
00099 
00100     /* Release reset of I2C clock */
00101     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C1);
00102   }
00103 #if defined(I2C2)
00104   else if (I2Cx == I2C2)
00105   {
00106     /* Force reset of I2C clock */
00107     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C2);
00108 
00109     /* Release reset of I2C clock */
00110     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C2);
00111 
00112   }
00113 #endif /* I2C2 */
00114   else
00115   {
00116     status = ERROR;
00117   }
00118 
00119   return status;
00120 }
00121 
00122 /**
00123   * @brief  Initialize the I2C registers according to the specified parameters in I2C_InitStruct.
00124   * @param  I2Cx I2C Instance.
00125   * @param  I2C_InitStruct pointer to a @ref LL_I2C_InitTypeDef structure.
00126   * @retval An ErrorStatus enumeration value:
00127   *          - SUCCESS  I2C registers are initialized
00128   *          - ERROR  Not applicable
00129   */
00130 uint32_t LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct)
00131 {
00132   LL_RCC_ClocksTypeDef rcc_clocks;
00133 
00134   /* Check the I2C Instance I2Cx */
00135   assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
00136 
00137   /* Check the I2C parameters from I2C_InitStruct */
00138   assert_param(IS_LL_I2C_PERIPHERAL_MODE(I2C_InitStruct->PeripheralMode));
00139   assert_param(IS_LL_I2C_CLOCK_SPEED(I2C_InitStruct->ClockSpeed));
00140   assert_param(IS_LL_I2C_DUTY_CYCLE(I2C_InitStruct->DutyCycle));
00141   assert_param(IS_LL_I2C_OWN_ADDRESS1(I2C_InitStruct->OwnAddress1));
00142   assert_param(IS_LL_I2C_TYPE_ACKNOWLEDGE(I2C_InitStruct->TypeAcknowledge));
00143   assert_param(IS_LL_I2C_OWN_ADDRSIZE(I2C_InitStruct->OwnAddrSize));
00144 
00145   /* Disable the selected I2Cx Peripheral */
00146   LL_I2C_Disable(I2Cx);
00147 
00148   /* Retrieve Clock frequencies */
00149   LL_RCC_GetSystemClocksFreq(&rcc_clocks);
00150 
00151   /*---------------------------- I2Cx SCL Clock Speed Configuration ------------
00152    * Configure the SCL speed :
00153    * - ClockSpeed: I2C_CR2_FREQ[5:0], I2C_TRISE_TRISE[5:0], I2C_CCR_FS,
00154    *           and I2C_CCR_CCR[11:0] bits
00155    * - DutyCycle: I2C_CCR_DUTY[7:0] bits
00156    */
00157   LL_I2C_ConfigSpeed(I2Cx, rcc_clocks.PCLK1_Frequency, I2C_InitStruct->ClockSpeed, I2C_InitStruct->DutyCycle);
00158 
00159   /*---------------------------- I2Cx OAR1 Configuration -----------------------
00160    * Disable, Configure and Enable I2Cx device own address 1 with parameters :
00161    * - OwnAddress1:  I2C_OAR1_ADD[9:8], I2C_OAR1_ADD[7:1] and I2C_OAR1_ADD0 bits
00162    * - OwnAddrSize:  I2C_OAR1_ADDMODE bit
00163    */
00164   LL_I2C_SetOwnAddress1(I2Cx, I2C_InitStruct->OwnAddress1, I2C_InitStruct->OwnAddrSize);
00165 
00166   /*---------------------------- I2Cx MODE Configuration -----------------------
00167   * Configure I2Cx peripheral mode with parameter :
00168    * - PeripheralMode: I2C_CR1_SMBUS, I2C_CR1_SMBTYPE and I2C_CR1_ENARP bits
00169    */
00170   LL_I2C_SetMode(I2Cx, I2C_InitStruct->PeripheralMode);
00171 
00172   /* Enable the selected I2Cx Peripheral */
00173   LL_I2C_Enable(I2Cx);
00174 
00175   /*---------------------------- I2Cx CR2 Configuration ------------------------
00176    * Configure the ACKnowledge or Non ACKnowledge condition
00177    * after the address receive match code or next received byte with parameter :
00178    * - TypeAcknowledge: I2C_CR2_NACK bit
00179    */
00180   LL_I2C_AcknowledgeNextData(I2Cx, I2C_InitStruct->TypeAcknowledge);
00181 
00182   return SUCCESS;
00183 }
00184 
00185 /**
00186   * @brief  Set each @ref LL_I2C_InitTypeDef field to default value.
00187   * @param  I2C_InitStruct Pointer to a @ref LL_I2C_InitTypeDef structure.
00188   * @retval None
00189   */
00190 void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct)
00191 {
00192   /* Set I2C_InitStruct fields to default values */
00193   I2C_InitStruct->PeripheralMode  = LL_I2C_MODE_I2C;
00194   I2C_InitStruct->ClockSpeed      = 5000U;
00195   I2C_InitStruct->DutyCycle       = LL_I2C_DUTYCYCLE_2;
00196   I2C_InitStruct->OwnAddress1     = 0U;
00197   I2C_InitStruct->TypeAcknowledge = LL_I2C_NACK;
00198   I2C_InitStruct->OwnAddrSize     = LL_I2C_OWNADDRESS1_7BIT;
00199 }
00200 
00201 /**
00202   * @}
00203   */
00204 
00205 /**
00206   * @}
00207   */
00208 
00209 /**
00210   * @}
00211   */
00212 
00213 #endif /* I2C1 || I2C2 */
00214 
00215 /**
00216   * @}
00217   */
00218 
00219 #endif /* USE_FULL_LL_DRIVER */
00220 
00221 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/