TUKS MCU Introductory course / TUKS-COURSE-THERMOMETER

Fork of TUKS-COURSE-TIMER by TUKS MCU Introductory course

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32l4xx_ll_i2c.c Source File

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