STM32L486xx HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32l4xx_ll_i2c.c 00004 * @author MCD Application Team 00005 * @brief I2C LL module driver. 00006 ****************************************************************************** 00007 * @attention 00008 * 00009 * <h2><center>© 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 "stm32l4xx_ll_i2c.h" 00039 #include "stm32l4xx_ll_bus.h" 00040 #ifdef USE_FULL_ASSERT 00041 #include "stm32_assert.h" 00042 #else 00043 #define assert_param(expr) ((void)0U) 00044 #endif 00045 00046 /** @addtogroup STM32L4xx_LL_Driver 00047 * @{ 00048 */ 00049 00050 #if defined (I2C1) || defined (I2C2) || defined (I2C3) || defined (I2C4) 00051 00052 /** @defgroup I2C_LL I2C 00053 * @{ 00054 */ 00055 00056 /* Private types -------------------------------------------------------------*/ 00057 /* Private variables ---------------------------------------------------------*/ 00058 /* Private constants ---------------------------------------------------------*/ 00059 /* Private macros ------------------------------------------------------------*/ 00060 /** @addtogroup I2C_LL_Private_Macros 00061 * @{ 00062 */ 00063 00064 #define IS_LL_I2C_PERIPHERAL_MODE(__VALUE__) (((__VALUE__) == LL_I2C_MODE_I2C) || \ 00065 ((__VALUE__) == LL_I2C_MODE_SMBUS_HOST) || \ 00066 ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE) || \ 00067 ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE_ARP)) 00068 00069 #define IS_LL_I2C_ANALOG_FILTER(__VALUE__) (((__VALUE__) == LL_I2C_ANALOGFILTER_ENABLE) || \ 00070 ((__VALUE__) == LL_I2C_ANALOGFILTER_DISABLE)) 00071 00072 #define IS_LL_I2C_DIGITAL_FILTER(__VALUE__) ((__VALUE__) <= 0x0000000FU) 00073 00074 #define IS_LL_I2C_OWN_ADDRESS1(__VALUE__) ((__VALUE__) <= 0x000003FFU) 00075 00076 #define IS_LL_I2C_TYPE_ACKNOWLEDGE(__VALUE__) (((__VALUE__) == LL_I2C_ACK) || \ 00077 ((__VALUE__) == LL_I2C_NACK)) 00078 00079 #define IS_LL_I2C_OWN_ADDRSIZE(__VALUE__) (((__VALUE__) == LL_I2C_OWNADDRESS1_7BIT) || \ 00080 ((__VALUE__) == LL_I2C_OWNADDRESS1_10BIT)) 00081 /** 00082 * @} 00083 */ 00084 00085 /* Private function prototypes -----------------------------------------------*/ 00086 00087 /* Exported functions --------------------------------------------------------*/ 00088 /** @addtogroup I2C_LL_Exported_Functions 00089 * @{ 00090 */ 00091 00092 /** @addtogroup I2C_LL_EF_Init 00093 * @{ 00094 */ 00095 00096 /** 00097 * @brief De-initialize the I2C registers to their default reset values. 00098 * @param I2Cx I2C Instance. 00099 * @retval An ErrorStatus enumeration value: 00100 * - SUCCESS: I2C registers are de-initialized 00101 * - ERROR: I2C registers are not de-initialized 00102 */ 00103 uint32_t LL_I2C_DeInit(I2C_TypeDef *I2Cx) 00104 { 00105 ErrorStatus status = SUCCESS; 00106 00107 /* Check the I2C Instance I2Cx */ 00108 assert_param(IS_I2C_ALL_INSTANCE(I2Cx)); 00109 00110 if (I2Cx == I2C1) 00111 { 00112 /* Force reset of I2C clock */ 00113 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C1); 00114 00115 /* Release reset of I2C clock */ 00116 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C1); 00117 } 00118 #if defined(I2C2) 00119 else if (I2Cx == I2C2) 00120 { 00121 /* Force reset of I2C clock */ 00122 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C2); 00123 00124 /* Release reset of I2C clock */ 00125 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C2); 00126 00127 } 00128 #endif 00129 else if (I2Cx == I2C3) 00130 { 00131 /* Force reset of I2C clock */ 00132 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C3); 00133 00134 /* Release reset of I2C clock */ 00135 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C3); 00136 } 00137 #if defined(I2C4) 00138 else if (I2Cx == I2C4) 00139 { 00140 /* Force reset of I2C clock */ 00141 LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_I2C4); 00142 00143 /* Release reset of I2C clock */ 00144 LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_I2C4); 00145 } 00146 #endif 00147 else 00148 { 00149 status = ERROR; 00150 } 00151 00152 return status; 00153 } 00154 00155 /** 00156 * @brief Initialize the I2C registers according to the specified parameters in I2C_InitStruct. 00157 * @param I2Cx I2C Instance. 00158 * @param I2C_InitStruct pointer to a @ref LL_I2C_InitTypeDef structure. 00159 * @retval An ErrorStatus enumeration value: 00160 * - SUCCESS: I2C registers are initialized 00161 * - ERROR: Not applicable 00162 */ 00163 uint32_t LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct) 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_ANALOG_FILTER(I2C_InitStruct->AnalogFilter)); 00171 assert_param(IS_LL_I2C_DIGITAL_FILTER(I2C_InitStruct->DigitalFilter)); 00172 assert_param(IS_LL_I2C_OWN_ADDRESS1(I2C_InitStruct->OwnAddress1)); 00173 assert_param(IS_LL_I2C_TYPE_ACKNOWLEDGE(I2C_InitStruct->TypeAcknowledge)); 00174 assert_param(IS_LL_I2C_OWN_ADDRSIZE(I2C_InitStruct->OwnAddrSize)); 00175 00176 /* Disable the selected I2Cx Peripheral */ 00177 LL_I2C_Disable(I2Cx); 00178 00179 /*---------------------------- I2Cx CR1 Configuration ------------------------ 00180 * Configure the analog and digital noise filters with parameters : 00181 * - AnalogFilter: I2C_CR1_ANFOFF bit 00182 * - DigitalFilter: I2C_CR1_DNF[3:0] bits 00183 */ 00184 LL_I2C_ConfigFilters(I2Cx, I2C_InitStruct->AnalogFilter, I2C_InitStruct->DigitalFilter); 00185 00186 /*---------------------------- I2Cx TIMINGR Configuration -------------------- 00187 * Configure the SDA setup, hold time and the SCL high, low period with parameter : 00188 * - Timing: I2C_TIMINGR_PRESC[3:0], I2C_TIMINGR_SCLDEL[3:0], I2C_TIMINGR_SDADEL[3:0], 00189 * I2C_TIMINGR_SCLH[7:0] and I2C_TIMINGR_SCLL[7:0] bits 00190 */ 00191 LL_I2C_SetTiming(I2Cx, I2C_InitStruct->Timing); 00192 00193 /* Enable the selected I2Cx Peripheral */ 00194 LL_I2C_Enable(I2Cx); 00195 00196 /*---------------------------- I2Cx OAR1 Configuration ----------------------- 00197 * Disable, Configure and Enable I2Cx device own address 1 with parameters : 00198 * - OwnAddress1: I2C_OAR1_OA1[9:0] bits 00199 * - OwnAddrSize: I2C_OAR1_OA1MODE bit 00200 */ 00201 LL_I2C_DisableOwnAddress1(I2Cx); 00202 LL_I2C_SetOwnAddress1(I2Cx, I2C_InitStruct->OwnAddress1, I2C_InitStruct->OwnAddrSize); 00203 00204 /* OwnAdress1 == 0 is reserved for General Call address */ 00205 if (I2C_InitStruct->OwnAddress1 != 0U) 00206 { 00207 LL_I2C_EnableOwnAddress1(I2Cx); 00208 } 00209 00210 /*---------------------------- I2Cx MODE Configuration ----------------------- 00211 * Configure I2Cx peripheral mode with parameter : 00212 * - PeripheralMode: I2C_CR1_SMBDEN and I2C_CR1_SMBHEN bits 00213 */ 00214 LL_I2C_SetMode(I2Cx, I2C_InitStruct->PeripheralMode); 00215 00216 /*---------------------------- I2Cx CR2 Configuration ------------------------ 00217 * Configure the ACKnowledge or Non ACKnowledge condition 00218 * after the address receive match code or next received byte with parameter : 00219 * - TypeAcknowledge: I2C_CR2_NACK bit 00220 */ 00221 LL_I2C_AcknowledgeNextData(I2Cx, I2C_InitStruct->TypeAcknowledge); 00222 00223 return SUCCESS; 00224 } 00225 00226 /** 00227 * @brief Set each @ref LL_I2C_InitTypeDef field to default value. 00228 * @param I2C_InitStruct Pointer to a @ref LL_I2C_InitTypeDef structure. 00229 * @retval None 00230 */ 00231 void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct) 00232 { 00233 /* Set I2C_InitStruct fields to default values */ 00234 I2C_InitStruct->PeripheralMode = LL_I2C_MODE_I2C; 00235 I2C_InitStruct->Timing = 0U; 00236 I2C_InitStruct->AnalogFilter = LL_I2C_ANALOGFILTER_ENABLE; 00237 I2C_InitStruct->DigitalFilter = 0U; 00238 I2C_InitStruct->OwnAddress1 = 0U; 00239 I2C_InitStruct->TypeAcknowledge = LL_I2C_NACK; 00240 I2C_InitStruct->OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT; 00241 } 00242 00243 /** 00244 * @} 00245 */ 00246 00247 /** 00248 * @} 00249 */ 00250 00251 /** 00252 * @} 00253 */ 00254 00255 #endif /* I2C1 || I2C2 || I2C3 || I2C4 */ 00256 00257 /** 00258 * @} 00259 */ 00260 00261 #endif /* USE_FULL_LL_DRIVER */ 00262 00263 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/