001

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ganlikun 0:13413ea9a877 1 /**
ganlikun 0:13413ea9a877 2 ******************************************************************************
ganlikun 0:13413ea9a877 3 * @file stm32f4xx_ll_i2c.c
ganlikun 0:13413ea9a877 4 * @author MCD Application Team
ganlikun 0:13413ea9a877 5 * @version V1.7.1
ganlikun 0:13413ea9a877 6 * @date 14-April-2017
ganlikun 0:13413ea9a877 7 * @brief I2C LL module driver.
ganlikun 0:13413ea9a877 8 ******************************************************************************
ganlikun 0:13413ea9a877 9 * @attention
ganlikun 0:13413ea9a877 10 *
ganlikun 0:13413ea9a877 11 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
ganlikun 0:13413ea9a877 12 *
ganlikun 0:13413ea9a877 13 * Redistribution and use in source and binary forms, with or without modification,
ganlikun 0:13413ea9a877 14 * are permitted provided that the following conditions are met:
ganlikun 0:13413ea9a877 15 * 1. Redistributions of source code must retain the above copyright notice,
ganlikun 0:13413ea9a877 16 * this list of conditions and the following disclaimer.
ganlikun 0:13413ea9a877 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
ganlikun 0:13413ea9a877 18 * this list of conditions and the following disclaimer in the documentation
ganlikun 0:13413ea9a877 19 * and/or other materials provided with the distribution.
ganlikun 0:13413ea9a877 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
ganlikun 0:13413ea9a877 21 * may be used to endorse or promote products derived from this software
ganlikun 0:13413ea9a877 22 * without specific prior written permission.
ganlikun 0:13413ea9a877 23 *
ganlikun 0:13413ea9a877 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
ganlikun 0:13413ea9a877 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
ganlikun 0:13413ea9a877 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
ganlikun 0:13413ea9a877 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
ganlikun 0:13413ea9a877 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ganlikun 0:13413ea9a877 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
ganlikun 0:13413ea9a877 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
ganlikun 0:13413ea9a877 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
ganlikun 0:13413ea9a877 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
ganlikun 0:13413ea9a877 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ganlikun 0:13413ea9a877 34 *
ganlikun 0:13413ea9a877 35 ******************************************************************************
ganlikun 0:13413ea9a877 36 */
ganlikun 0:13413ea9a877 37 #if defined(USE_FULL_LL_DRIVER)
ganlikun 0:13413ea9a877 38
ganlikun 0:13413ea9a877 39 /* Includes ------------------------------------------------------------------*/
ganlikun 0:13413ea9a877 40 #include "stm32f4xx_ll_i2c.h"
ganlikun 0:13413ea9a877 41 #include "stm32f4xx_ll_bus.h"
ganlikun 0:13413ea9a877 42 #include "stm32f4xx_ll_rcc.h"
ganlikun 0:13413ea9a877 43 #ifdef USE_FULL_ASSERT
ganlikun 0:13413ea9a877 44 #include "stm32_assert.h"
ganlikun 0:13413ea9a877 45 #else
ganlikun 0:13413ea9a877 46 #define assert_param(expr) ((void)0U)
ganlikun 0:13413ea9a877 47 #endif
ganlikun 0:13413ea9a877 48
ganlikun 0:13413ea9a877 49 /** @addtogroup STM32F4xx_LL_Driver
ganlikun 0:13413ea9a877 50 * @{
ganlikun 0:13413ea9a877 51 */
ganlikun 0:13413ea9a877 52
ganlikun 0:13413ea9a877 53 #if defined (I2C1) || defined (I2C2) || defined (I2C3)
ganlikun 0:13413ea9a877 54
ganlikun 0:13413ea9a877 55 /** @defgroup I2C_LL I2C
ganlikun 0:13413ea9a877 56 * @{
ganlikun 0:13413ea9a877 57 */
ganlikun 0:13413ea9a877 58
ganlikun 0:13413ea9a877 59 /* Private types -------------------------------------------------------------*/
ganlikun 0:13413ea9a877 60 /* Private variables ---------------------------------------------------------*/
ganlikun 0:13413ea9a877 61 /* Private constants ---------------------------------------------------------*/
ganlikun 0:13413ea9a877 62 /* Private macros ------------------------------------------------------------*/
ganlikun 0:13413ea9a877 63 /** @addtogroup I2C_LL_Private_Macros
ganlikun 0:13413ea9a877 64 * @{
ganlikun 0:13413ea9a877 65 */
ganlikun 0:13413ea9a877 66
ganlikun 0:13413ea9a877 67 #define IS_LL_I2C_PERIPHERAL_MODE(__VALUE__) (((__VALUE__) == LL_I2C_MODE_I2C) || \
ganlikun 0:13413ea9a877 68 ((__VALUE__) == LL_I2C_MODE_SMBUS_HOST) || \
ganlikun 0:13413ea9a877 69 ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE) || \
ganlikun 0:13413ea9a877 70 ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE_ARP))
ganlikun 0:13413ea9a877 71
ganlikun 0:13413ea9a877 72 #define IS_I2C_CLOCK_SPEED(__VALUE__) (((__VALUE__) > 0U) && ((__VALUE__) <= LL_I2C_MAX_SPEED_FAST))
ganlikun 0:13413ea9a877 73
ganlikun 0:13413ea9a877 74 #define IS_I2C_DUTY_CYCLE(__VALUE__) (((__VALUE__) == LL_I2C_DUTYCYCLE_2) || \
ganlikun 0:13413ea9a877 75 ((__VALUE__) == LL_I2C_DUTYCYCLE_16_9))
ganlikun 0:13413ea9a877 76
ganlikun 0:13413ea9a877 77 #if defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF)
ganlikun 0:13413ea9a877 78 #define IS_LL_I2C_ANALOG_FILTER(__VALUE__) (((__VALUE__) == LL_I2C_ANALOGFILTER_ENABLE) || \
ganlikun 0:13413ea9a877 79 ((__VALUE__) == LL_I2C_ANALOGFILTER_DISABLE))
ganlikun 0:13413ea9a877 80
ganlikun 0:13413ea9a877 81 #define IS_LL_I2C_DIGITAL_FILTER(__VALUE__) ((__VALUE__) <= 0x0000000FU)
ganlikun 0:13413ea9a877 82
ganlikun 0:13413ea9a877 83 #endif
ganlikun 0:13413ea9a877 84 #define IS_LL_I2C_OWN_ADDRESS1(__VALUE__) ((__VALUE__) <= 0x000003FFU)
ganlikun 0:13413ea9a877 85
ganlikun 0:13413ea9a877 86 #define IS_LL_I2C_TYPE_ACKNOWLEDGE(__VALUE__) (((__VALUE__) == LL_I2C_ACK) || \
ganlikun 0:13413ea9a877 87 ((__VALUE__) == LL_I2C_NACK))
ganlikun 0:13413ea9a877 88
ganlikun 0:13413ea9a877 89 #define IS_LL_I2C_OWN_ADDRSIZE(__VALUE__) (((__VALUE__) == LL_I2C_OWNADDRESS1_7BIT) || \
ganlikun 0:13413ea9a877 90 ((__VALUE__) == LL_I2C_OWNADDRESS1_10BIT))
ganlikun 0:13413ea9a877 91 /**
ganlikun 0:13413ea9a877 92 * @}
ganlikun 0:13413ea9a877 93 */
ganlikun 0:13413ea9a877 94
ganlikun 0:13413ea9a877 95 /* Private function prototypes -----------------------------------------------*/
ganlikun 0:13413ea9a877 96
ganlikun 0:13413ea9a877 97 /* Exported functions --------------------------------------------------------*/
ganlikun 0:13413ea9a877 98 /** @addtogroup I2C_LL_Exported_Functions
ganlikun 0:13413ea9a877 99 * @{
ganlikun 0:13413ea9a877 100 */
ganlikun 0:13413ea9a877 101
ganlikun 0:13413ea9a877 102 /** @addtogroup I2C_LL_EF_Init
ganlikun 0:13413ea9a877 103 * @{
ganlikun 0:13413ea9a877 104 */
ganlikun 0:13413ea9a877 105
ganlikun 0:13413ea9a877 106 /**
ganlikun 0:13413ea9a877 107 * @brief De-initialize the I2C registers to their default reset values.
ganlikun 0:13413ea9a877 108 * @param I2Cx I2C Instance.
ganlikun 0:13413ea9a877 109 * @retval An ErrorStatus enumeration value:
ganlikun 0:13413ea9a877 110 * - SUCCESS: I2C registers are de-initialized
ganlikun 0:13413ea9a877 111 * - ERROR: I2C registers are not de-initialized
ganlikun 0:13413ea9a877 112 */
ganlikun 0:13413ea9a877 113 uint32_t LL_I2C_DeInit(I2C_TypeDef *I2Cx)
ganlikun 0:13413ea9a877 114 {
ganlikun 0:13413ea9a877 115 ErrorStatus status = SUCCESS;
ganlikun 0:13413ea9a877 116
ganlikun 0:13413ea9a877 117 /* Check the I2C Instance I2Cx */
ganlikun 0:13413ea9a877 118 assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
ganlikun 0:13413ea9a877 119
ganlikun 0:13413ea9a877 120 if (I2Cx == I2C1)
ganlikun 0:13413ea9a877 121 {
ganlikun 0:13413ea9a877 122 /* Force reset of I2C clock */
ganlikun 0:13413ea9a877 123 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C1);
ganlikun 0:13413ea9a877 124
ganlikun 0:13413ea9a877 125 /* Release reset of I2C clock */
ganlikun 0:13413ea9a877 126 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C1);
ganlikun 0:13413ea9a877 127 }
ganlikun 0:13413ea9a877 128 else if (I2Cx == I2C2)
ganlikun 0:13413ea9a877 129 {
ganlikun 0:13413ea9a877 130 /* Force reset of I2C clock */
ganlikun 0:13413ea9a877 131 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C2);
ganlikun 0:13413ea9a877 132
ganlikun 0:13413ea9a877 133 /* Release reset of I2C clock */
ganlikun 0:13413ea9a877 134 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C2);
ganlikun 0:13413ea9a877 135
ganlikun 0:13413ea9a877 136 }
ganlikun 0:13413ea9a877 137 #if defined(I2C3)
ganlikun 0:13413ea9a877 138 else if (I2Cx == I2C3)
ganlikun 0:13413ea9a877 139 {
ganlikun 0:13413ea9a877 140 /* Force reset of I2C clock */
ganlikun 0:13413ea9a877 141 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C3);
ganlikun 0:13413ea9a877 142
ganlikun 0:13413ea9a877 143 /* Release reset of I2C clock */
ganlikun 0:13413ea9a877 144 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C3);
ganlikun 0:13413ea9a877 145 }
ganlikun 0:13413ea9a877 146 #endif
ganlikun 0:13413ea9a877 147 else
ganlikun 0:13413ea9a877 148 {
ganlikun 0:13413ea9a877 149 status = ERROR;
ganlikun 0:13413ea9a877 150 }
ganlikun 0:13413ea9a877 151
ganlikun 0:13413ea9a877 152 return status;
ganlikun 0:13413ea9a877 153 }
ganlikun 0:13413ea9a877 154
ganlikun 0:13413ea9a877 155 /**
ganlikun 0:13413ea9a877 156 * @brief Initialize the I2C registers according to the specified parameters in I2C_InitStruct.
ganlikun 0:13413ea9a877 157 * @param I2Cx I2C Instance.
ganlikun 0:13413ea9a877 158 * @param I2C_InitStruct pointer to a @ref LL_I2C_InitTypeDef structure.
ganlikun 0:13413ea9a877 159 * @retval An ErrorStatus enumeration value:
ganlikun 0:13413ea9a877 160 * - SUCCESS: I2C registers are initialized
ganlikun 0:13413ea9a877 161 * - ERROR: Not applicable
ganlikun 0:13413ea9a877 162 */
ganlikun 0:13413ea9a877 163 uint32_t LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct)
ganlikun 0:13413ea9a877 164 {
ganlikun 0:13413ea9a877 165 LL_RCC_ClocksTypeDef rcc_clocks;
ganlikun 0:13413ea9a877 166
ganlikun 0:13413ea9a877 167 /* Check the I2C Instance I2Cx */
ganlikun 0:13413ea9a877 168 assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
ganlikun 0:13413ea9a877 169
ganlikun 0:13413ea9a877 170 /* Check the I2C parameters from I2C_InitStruct */
ganlikun 0:13413ea9a877 171 assert_param(IS_LL_I2C_PERIPHERAL_MODE(I2C_InitStruct->PeripheralMode));
ganlikun 0:13413ea9a877 172 assert_param(IS_I2C_CLOCK_SPEED(I2C_InitStruct->ClockSpeed));
ganlikun 0:13413ea9a877 173 assert_param(IS_I2C_DUTY_CYCLE(I2C_InitStruct->DutyCycle));
ganlikun 0:13413ea9a877 174 #if defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF)
ganlikun 0:13413ea9a877 175 assert_param(IS_LL_I2C_ANALOG_FILTER(I2C_InitStruct->AnalogFilter));
ganlikun 0:13413ea9a877 176 assert_param(IS_LL_I2C_DIGITAL_FILTER(I2C_InitStruct->DigitalFilter));
ganlikun 0:13413ea9a877 177 #endif
ganlikun 0:13413ea9a877 178 assert_param(IS_LL_I2C_OWN_ADDRESS1(I2C_InitStruct->OwnAddress1));
ganlikun 0:13413ea9a877 179 assert_param(IS_LL_I2C_TYPE_ACKNOWLEDGE(I2C_InitStruct->TypeAcknowledge));
ganlikun 0:13413ea9a877 180 assert_param(IS_LL_I2C_OWN_ADDRSIZE(I2C_InitStruct->OwnAddrSize));
ganlikun 0:13413ea9a877 181
ganlikun 0:13413ea9a877 182 /* Disable the selected I2Cx Peripheral */
ganlikun 0:13413ea9a877 183 LL_I2C_Disable(I2Cx);
ganlikun 0:13413ea9a877 184
ganlikun 0:13413ea9a877 185 /* Retrieve Clock frequencies */
ganlikun 0:13413ea9a877 186 LL_RCC_GetSystemClocksFreq(&rcc_clocks);
ganlikun 0:13413ea9a877 187
ganlikun 0:13413ea9a877 188 #if defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF)
ganlikun 0:13413ea9a877 189 /*---------------------------- I2Cx FLTR Configuration -----------------------
ganlikun 0:13413ea9a877 190 * Configure the analog and digital noise filters with parameters :
ganlikun 0:13413ea9a877 191 * - AnalogFilter: I2C_FLTR_ANFOFF bit
ganlikun 0:13413ea9a877 192 * - DigitalFilter: I2C_FLTR_DNF[3:0] bits
ganlikun 0:13413ea9a877 193 */
ganlikun 0:13413ea9a877 194 LL_I2C_ConfigFilters(I2Cx, I2C_InitStruct->AnalogFilter, I2C_InitStruct->DigitalFilter);
ganlikun 0:13413ea9a877 195
ganlikun 0:13413ea9a877 196 #endif
ganlikun 0:13413ea9a877 197 /*---------------------------- I2Cx SCL Clock Speed Configuration ------------
ganlikun 0:13413ea9a877 198 * Configure the SCL speed :
ganlikun 0:13413ea9a877 199 * - ClockSpeed: I2C_CR2_FREQ[5:0], I2C_TRISE_TRISE[5:0], I2C_CCR_FS,
ganlikun 0:13413ea9a877 200 * and I2C_CCR_CCR[11:0] bits
ganlikun 0:13413ea9a877 201 * - DutyCycle: I2C_CCR_DUTY[7:0] bits
ganlikun 0:13413ea9a877 202 */
ganlikun 0:13413ea9a877 203 LL_I2C_ConfigSpeed(I2Cx, rcc_clocks.PCLK1_Frequency, I2C_InitStruct->ClockSpeed, I2C_InitStruct->DutyCycle);
ganlikun 0:13413ea9a877 204
ganlikun 0:13413ea9a877 205 /*---------------------------- I2Cx OAR1 Configuration -----------------------
ganlikun 0:13413ea9a877 206 * Disable, Configure and Enable I2Cx device own address 1 with parameters :
ganlikun 0:13413ea9a877 207 * - OwnAddress1: I2C_OAR1_ADD[9:8], I2C_OAR1_ADD[7:1] and I2C_OAR1_ADD0 bits
ganlikun 0:13413ea9a877 208 * - OwnAddrSize: I2C_OAR1_ADDMODE bit
ganlikun 0:13413ea9a877 209 */
ganlikun 0:13413ea9a877 210 LL_I2C_SetOwnAddress1(I2Cx, I2C_InitStruct->OwnAddress1, I2C_InitStruct->OwnAddrSize);
ganlikun 0:13413ea9a877 211
ganlikun 0:13413ea9a877 212 /*---------------------------- I2Cx MODE Configuration -----------------------
ganlikun 0:13413ea9a877 213 * Configure I2Cx peripheral mode with parameter :
ganlikun 0:13413ea9a877 214 * - PeripheralMode: I2C_CR1_SMBUS, I2C_CR1_SMBTYPE and I2C_CR1_ENARP bits
ganlikun 0:13413ea9a877 215 */
ganlikun 0:13413ea9a877 216 LL_I2C_SetMode(I2Cx, I2C_InitStruct->PeripheralMode);
ganlikun 0:13413ea9a877 217
ganlikun 0:13413ea9a877 218 /* Enable the selected I2Cx Peripheral */
ganlikun 0:13413ea9a877 219 LL_I2C_Enable(I2Cx);
ganlikun 0:13413ea9a877 220
ganlikun 0:13413ea9a877 221 /*---------------------------- I2Cx CR2 Configuration ------------------------
ganlikun 0:13413ea9a877 222 * Configure the ACKnowledge or Non ACKnowledge condition
ganlikun 0:13413ea9a877 223 * after the address receive match code or next received byte with parameter :
ganlikun 0:13413ea9a877 224 * - TypeAcknowledge: I2C_CR2_NACK bit
ganlikun 0:13413ea9a877 225 */
ganlikun 0:13413ea9a877 226 LL_I2C_AcknowledgeNextData(I2Cx, I2C_InitStruct->TypeAcknowledge);
ganlikun 0:13413ea9a877 227
ganlikun 0:13413ea9a877 228 return SUCCESS;
ganlikun 0:13413ea9a877 229 }
ganlikun 0:13413ea9a877 230
ganlikun 0:13413ea9a877 231 /**
ganlikun 0:13413ea9a877 232 * @brief Set each @ref LL_I2C_InitTypeDef field to default value.
ganlikun 0:13413ea9a877 233 * @param I2C_InitStruct Pointer to a @ref LL_I2C_InitTypeDef structure.
ganlikun 0:13413ea9a877 234 * @retval None
ganlikun 0:13413ea9a877 235 */
ganlikun 0:13413ea9a877 236 void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct)
ganlikun 0:13413ea9a877 237 {
ganlikun 0:13413ea9a877 238 /* Set I2C_InitStruct fields to default values */
ganlikun 0:13413ea9a877 239 I2C_InitStruct->PeripheralMode = LL_I2C_MODE_I2C;
ganlikun 0:13413ea9a877 240 I2C_InitStruct->ClockSpeed = 5000U;
ganlikun 0:13413ea9a877 241 I2C_InitStruct->DutyCycle = LL_I2C_DUTYCYCLE_2;
ganlikun 0:13413ea9a877 242 #if defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF)
ganlikun 0:13413ea9a877 243 I2C_InitStruct->AnalogFilter = LL_I2C_ANALOGFILTER_ENABLE;
ganlikun 0:13413ea9a877 244 I2C_InitStruct->DigitalFilter = 0U;
ganlikun 0:13413ea9a877 245 #endif
ganlikun 0:13413ea9a877 246 I2C_InitStruct->OwnAddress1 = 0U;
ganlikun 0:13413ea9a877 247 I2C_InitStruct->TypeAcknowledge = LL_I2C_NACK;
ganlikun 0:13413ea9a877 248 I2C_InitStruct->OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
ganlikun 0:13413ea9a877 249 }
ganlikun 0:13413ea9a877 250
ganlikun 0:13413ea9a877 251 /**
ganlikun 0:13413ea9a877 252 * @}
ganlikun 0:13413ea9a877 253 */
ganlikun 0:13413ea9a877 254
ganlikun 0:13413ea9a877 255 /**
ganlikun 0:13413ea9a877 256 * @}
ganlikun 0:13413ea9a877 257 */
ganlikun 0:13413ea9a877 258
ganlikun 0:13413ea9a877 259 /**
ganlikun 0:13413ea9a877 260 * @}
ganlikun 0:13413ea9a877 261 */
ganlikun 0:13413ea9a877 262
ganlikun 0:13413ea9a877 263 #endif /* I2C1 || I2C2 || I2C3 */
ganlikun 0:13413ea9a877 264
ganlikun 0:13413ea9a877 265 /**
ganlikun 0:13413ea9a877 266 * @}
ganlikun 0:13413ea9a877 267 */
ganlikun 0:13413ea9a877 268
ganlikun 0:13413ea9a877 269 #endif /* USE_FULL_LL_DRIVER */
ganlikun 0:13413ea9a877 270
ganlikun 0:13413ea9a877 271 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
ganlikun 0:13413ea9a877 272