STM32F479xx HAL User Manual
|
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>© 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 "stm32f4xx_ll_i2c.h" 00023 #include "stm32f4xx_ll_bus.h" 00024 #include "stm32f4xx_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 STM32F4xx_LL_Driver 00032 * @{ 00033 */ 00034 00035 #if defined (I2C1) || defined (I2C2) || defined (I2C3) 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 #if defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF) 00060 #define IS_LL_I2C_ANALOG_FILTER(__VALUE__) (((__VALUE__) == LL_I2C_ANALOGFILTER_ENABLE) || \ 00061 ((__VALUE__) == LL_I2C_ANALOGFILTER_DISABLE)) 00062 00063 #define IS_LL_I2C_DIGITAL_FILTER(__VALUE__) ((__VALUE__) <= 0x0000000FU) 00064 00065 #endif 00066 #define IS_LL_I2C_OWN_ADDRESS1(__VALUE__) ((__VALUE__) <= 0x000003FFU) 00067 00068 #define IS_LL_I2C_TYPE_ACKNOWLEDGE(__VALUE__) (((__VALUE__) == LL_I2C_ACK) || \ 00069 ((__VALUE__) == LL_I2C_NACK)) 00070 00071 #define IS_LL_I2C_OWN_ADDRSIZE(__VALUE__) (((__VALUE__) == LL_I2C_OWNADDRESS1_7BIT) || \ 00072 ((__VALUE__) == LL_I2C_OWNADDRESS1_10BIT)) 00073 /** 00074 * @} 00075 */ 00076 00077 /* Private function prototypes -----------------------------------------------*/ 00078 00079 /* Exported functions --------------------------------------------------------*/ 00080 /** @addtogroup I2C_LL_Exported_Functions 00081 * @{ 00082 */ 00083 00084 /** @addtogroup I2C_LL_EF_Init 00085 * @{ 00086 */ 00087 00088 /** 00089 * @brief De-initialize the I2C registers to their default reset values. 00090 * @param I2Cx I2C Instance. 00091 * @retval An ErrorStatus enumeration value: 00092 * - SUCCESS I2C registers are de-initialized 00093 * - ERROR I2C registers are not de-initialized 00094 */ 00095 uint32_t LL_I2C_DeInit(I2C_TypeDef *I2Cx) 00096 { 00097 ErrorStatus status = SUCCESS; 00098 00099 /* Check the I2C Instance I2Cx */ 00100 assert_param(IS_I2C_ALL_INSTANCE(I2Cx)); 00101 00102 if (I2Cx == I2C1) 00103 { 00104 /* Force reset of I2C clock */ 00105 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C1); 00106 00107 /* Release reset of I2C clock */ 00108 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C1); 00109 } 00110 else if (I2Cx == I2C2) 00111 { 00112 /* Force reset of I2C clock */ 00113 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C2); 00114 00115 /* Release reset of I2C clock */ 00116 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C2); 00117 00118 } 00119 #if defined(I2C3) 00120 else if (I2Cx == I2C3) 00121 { 00122 /* Force reset of I2C clock */ 00123 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C3); 00124 00125 /* Release reset of I2C clock */ 00126 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C3); 00127 } 00128 #endif 00129 else 00130 { 00131 status = ERROR; 00132 } 00133 00134 return status; 00135 } 00136 00137 /** 00138 * @brief Initialize the I2C registers according to the specified parameters in I2C_InitStruct. 00139 * @param I2Cx I2C Instance. 00140 * @param I2C_InitStruct pointer to a @ref LL_I2C_InitTypeDef structure. 00141 * @retval An ErrorStatus enumeration value: 00142 * - SUCCESS I2C registers are initialized 00143 * - ERROR Not applicable 00144 */ 00145 uint32_t LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct) 00146 { 00147 LL_RCC_ClocksTypeDef rcc_clocks; 00148 00149 /* Check the I2C Instance I2Cx */ 00150 assert_param(IS_I2C_ALL_INSTANCE(I2Cx)); 00151 00152 /* Check the I2C parameters from I2C_InitStruct */ 00153 assert_param(IS_LL_I2C_PERIPHERAL_MODE(I2C_InitStruct->PeripheralMode)); 00154 assert_param(IS_LL_I2C_CLOCK_SPEED(I2C_InitStruct->ClockSpeed)); 00155 assert_param(IS_LL_I2C_DUTY_CYCLE(I2C_InitStruct->DutyCycle)); 00156 #if defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF) 00157 assert_param(IS_LL_I2C_ANALOG_FILTER(I2C_InitStruct->AnalogFilter)); 00158 assert_param(IS_LL_I2C_DIGITAL_FILTER(I2C_InitStruct->DigitalFilter)); 00159 #endif 00160 assert_param(IS_LL_I2C_OWN_ADDRESS1(I2C_InitStruct->OwnAddress1)); 00161 assert_param(IS_LL_I2C_TYPE_ACKNOWLEDGE(I2C_InitStruct->TypeAcknowledge)); 00162 assert_param(IS_LL_I2C_OWN_ADDRSIZE(I2C_InitStruct->OwnAddrSize)); 00163 00164 /* Disable the selected I2Cx Peripheral */ 00165 LL_I2C_Disable(I2Cx); 00166 00167 /* Retrieve Clock frequencies */ 00168 LL_RCC_GetSystemClocksFreq(&rcc_clocks); 00169 00170 #if defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF) 00171 /*---------------------------- I2Cx FLTR Configuration ----------------------- 00172 * Configure the analog and digital noise filters with parameters : 00173 * - AnalogFilter: I2C_FLTR_ANFOFF bit 00174 * - DigitalFilter: I2C_FLTR_DNF[3:0] bits 00175 */ 00176 LL_I2C_ConfigFilters(I2Cx, I2C_InitStruct->AnalogFilter, I2C_InitStruct->DigitalFilter); 00177 00178 #endif 00179 /*---------------------------- I2Cx SCL Clock Speed Configuration ------------ 00180 * Configure the SCL speed : 00181 * - ClockSpeed: I2C_CR2_FREQ[5:0], I2C_TRISE_TRISE[5:0], I2C_CCR_FS, 00182 * and I2C_CCR_CCR[11:0] bits 00183 * - DutyCycle: I2C_CCR_DUTY[7:0] bits 00184 */ 00185 LL_I2C_ConfigSpeed(I2Cx, rcc_clocks.PCLK1_Frequency, I2C_InitStruct->ClockSpeed, I2C_InitStruct->DutyCycle); 00186 00187 /*---------------------------- I2Cx OAR1 Configuration ----------------------- 00188 * Disable, Configure and Enable I2Cx device own address 1 with parameters : 00189 * - OwnAddress1: I2C_OAR1_ADD[9:8], I2C_OAR1_ADD[7:1] and I2C_OAR1_ADD0 bits 00190 * - OwnAddrSize: I2C_OAR1_ADDMODE bit 00191 */ 00192 LL_I2C_SetOwnAddress1(I2Cx, I2C_InitStruct->OwnAddress1, I2C_InitStruct->OwnAddrSize); 00193 00194 /*---------------------------- I2Cx MODE Configuration ----------------------- 00195 * Configure I2Cx peripheral mode with parameter : 00196 * - PeripheralMode: I2C_CR1_SMBUS, I2C_CR1_SMBTYPE and I2C_CR1_ENARP bits 00197 */ 00198 LL_I2C_SetMode(I2Cx, I2C_InitStruct->PeripheralMode); 00199 00200 /* Enable the selected I2Cx Peripheral */ 00201 LL_I2C_Enable(I2Cx); 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->ClockSpeed = 5000U; 00223 I2C_InitStruct->DutyCycle = LL_I2C_DUTYCYCLE_2; 00224 #if defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF) 00225 I2C_InitStruct->AnalogFilter = LL_I2C_ANALOGFILTER_ENABLE; 00226 I2C_InitStruct->DigitalFilter = 0U; 00227 #endif 00228 I2C_InitStruct->OwnAddress1 = 0U; 00229 I2C_InitStruct->TypeAcknowledge = LL_I2C_NACK; 00230 I2C_InitStruct->OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT; 00231 } 00232 00233 /** 00234 * @} 00235 */ 00236 00237 /** 00238 * @} 00239 */ 00240 00241 /** 00242 * @} 00243 */ 00244 00245 #endif /* I2C1 || I2C2 || I2C3 */ 00246 00247 /** 00248 * @} 00249 */ 00250 00251 #endif /* USE_FULL_LL_DRIVER */ 00252 00253 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/