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