source: S-port/trunk/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_i2c.c

Last change on this file was 1, checked in by AlexLir, 3 years ago
File size: 8.7 KB
Line 
1/**
2 ******************************************************************************
3 * @file stm32f4xx_ll_i2c.c
4 * @author MCD Application Team
5 * @brief I2C LL module driver.
6 ******************************************************************************
7 * @attention
8 *
9 * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
10 * All rights reserved.</center></h2>
11 *
12 * This software component is licensed by ST under BSD 3-Clause license,
13 * the "License"; You may not use this file except in compliance with the
14 * License. You may obtain a copy of the License at:
15 * opensource.org/licenses/BSD-3-Clause
16 *
17 ******************************************************************************
18 */
19#if defined(USE_FULL_LL_DRIVER)
20
21/* Includes ------------------------------------------------------------------*/
22#include "stm32f4xx_ll_i2c.h"
23#include "stm32f4xx_ll_bus.h"
24#include "stm32f4xx_ll_rcc.h"
25#ifdef USE_FULL_ASSERT
26#include "stm32_assert.h"
27#else
28#define assert_param(expr) ((void)0U)
29#endif
30
31/** @addtogroup STM32F4xx_LL_Driver
32 * @{
33 */
34
35#if defined (I2C1) || defined (I2C2) || defined (I2C3)
36
37/** @defgroup I2C_LL I2C
38 * @{
39 */
40
41/* Private types -------------------------------------------------------------*/
42/* Private variables ---------------------------------------------------------*/
43/* Private constants ---------------------------------------------------------*/
44/* Private macros ------------------------------------------------------------*/
45/** @addtogroup I2C_LL_Private_Macros
46 * @{
47 */
48
49#define IS_LL_I2C_PERIPHERAL_MODE(__VALUE__) (((__VALUE__) == LL_I2C_MODE_I2C) || \
50 ((__VALUE__) == LL_I2C_MODE_SMBUS_HOST) || \
51 ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE) || \
52 ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE_ARP))
53
54#define IS_LL_I2C_CLOCK_SPEED(__VALUE__) (((__VALUE__) > 0U) && ((__VALUE__) <= LL_I2C_MAX_SPEED_FAST))
55
56#define IS_LL_I2C_DUTY_CYCLE(__VALUE__) (((__VALUE__) == LL_I2C_DUTYCYCLE_2) || \
57 ((__VALUE__) == LL_I2C_DUTYCYCLE_16_9))
58
59#if defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF)
60#define IS_LL_I2C_ANALOG_FILTER(__VALUE__) (((__VALUE__) == LL_I2C_ANALOGFILTER_ENABLE) || \
61 ((__VALUE__) == LL_I2C_ANALOGFILTER_DISABLE))
62
63#define IS_LL_I2C_DIGITAL_FILTER(__VALUE__) ((__VALUE__) <= 0x0000000FU)
64
65#endif
66#define IS_LL_I2C_OWN_ADDRESS1(__VALUE__) ((__VALUE__) <= 0x000003FFU)
67
68#define IS_LL_I2C_TYPE_ACKNOWLEDGE(__VALUE__) (((__VALUE__) == LL_I2C_ACK) || \
69 ((__VALUE__) == LL_I2C_NACK))
70
71#define IS_LL_I2C_OWN_ADDRSIZE(__VALUE__) (((__VALUE__) == LL_I2C_OWNADDRESS1_7BIT) || \
72 ((__VALUE__) == LL_I2C_OWNADDRESS1_10BIT))
73/**
74 * @}
75 */
76
77/* Private function prototypes -----------------------------------------------*/
78
79/* Exported functions --------------------------------------------------------*/
80/** @addtogroup I2C_LL_Exported_Functions
81 * @{
82 */
83
84/** @addtogroup I2C_LL_EF_Init
85 * @{
86 */
87
88/**
89 * @brief De-initialize the I2C registers to their default reset values.
90 * @param I2Cx I2C Instance.
91 * @retval An ErrorStatus enumeration value:
92 * - SUCCESS I2C registers are de-initialized
93 * - ERROR I2C registers are not de-initialized
94 */
95uint32_t LL_I2C_DeInit(I2C_TypeDef *I2Cx)
96{
97 ErrorStatus status = SUCCESS;
98
99 /* Check the I2C Instance I2Cx */
100 assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
101
102 if (I2Cx == I2C1)
103 {
104 /* Force reset of I2C clock */
105 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C1);
106
107 /* Release reset of I2C clock */
108 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C1);
109 }
110 else if (I2Cx == I2C2)
111 {
112 /* Force reset of I2C clock */
113 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C2);
114
115 /* Release reset of I2C clock */
116 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C2);
117
118 }
119#if defined(I2C3)
120 else if (I2Cx == I2C3)
121 {
122 /* Force reset of I2C clock */
123 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C3);
124
125 /* Release reset of I2C clock */
126 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C3);
127 }
128#endif
129 else
130 {
131 status = ERROR;
132 }
133
134 return status;
135}
136
137/**
138 * @brief Initialize the I2C registers according to the specified parameters in I2C_InitStruct.
139 * @param I2Cx I2C Instance.
140 * @param I2C_InitStruct pointer to a @ref LL_I2C_InitTypeDef structure.
141 * @retval An ErrorStatus enumeration value:
142 * - SUCCESS I2C registers are initialized
143 * - ERROR Not applicable
144 */
145uint32_t LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct)
146{
147 LL_RCC_ClocksTypeDef rcc_clocks;
148
149 /* Check the I2C Instance I2Cx */
150 assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
151
152 /* Check the I2C parameters from I2C_InitStruct */
153 assert_param(IS_LL_I2C_PERIPHERAL_MODE(I2C_InitStruct->PeripheralMode));
154 assert_param(IS_LL_I2C_CLOCK_SPEED(I2C_InitStruct->ClockSpeed));
155 assert_param(IS_LL_I2C_DUTY_CYCLE(I2C_InitStruct->DutyCycle));
156#if defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF)
157 assert_param(IS_LL_I2C_ANALOG_FILTER(I2C_InitStruct->AnalogFilter));
158 assert_param(IS_LL_I2C_DIGITAL_FILTER(I2C_InitStruct->DigitalFilter));
159#endif
160 assert_param(IS_LL_I2C_OWN_ADDRESS1(I2C_InitStruct->OwnAddress1));
161 assert_param(IS_LL_I2C_TYPE_ACKNOWLEDGE(I2C_InitStruct->TypeAcknowledge));
162 assert_param(IS_LL_I2C_OWN_ADDRSIZE(I2C_InitStruct->OwnAddrSize));
163
164 /* Disable the selected I2Cx Peripheral */
165 LL_I2C_Disable(I2Cx);
166
167 /* Retrieve Clock frequencies */
168 LL_RCC_GetSystemClocksFreq(&rcc_clocks);
169
170#if defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF)
171 /*---------------------------- I2Cx FLTR Configuration -----------------------
172 * Configure the analog and digital noise filters with parameters :
173 * - AnalogFilter: I2C_FLTR_ANFOFF bit
174 * - DigitalFilter: I2C_FLTR_DNF[3:0] bits
175 */
176 LL_I2C_ConfigFilters(I2Cx, I2C_InitStruct->AnalogFilter, I2C_InitStruct->DigitalFilter);
177
178#endif
179 /*---------------------------- I2Cx SCL Clock Speed Configuration ------------
180 * Configure the SCL speed :
181 * - ClockSpeed: I2C_CR2_FREQ[5:0], I2C_TRISE_TRISE[5:0], I2C_CCR_FS,
182 * and I2C_CCR_CCR[11:0] bits
183 * - DutyCycle: I2C_CCR_DUTY[7:0] bits
184 */
185 LL_I2C_ConfigSpeed(I2Cx, rcc_clocks.PCLK1_Frequency, I2C_InitStruct->ClockSpeed, I2C_InitStruct->DutyCycle);
186
187 /*---------------------------- I2Cx OAR1 Configuration -----------------------
188 * Disable, Configure and Enable I2Cx device own address 1 with parameters :
189 * - OwnAddress1: I2C_OAR1_ADD[9:8], I2C_OAR1_ADD[7:1] and I2C_OAR1_ADD0 bits
190 * - OwnAddrSize: I2C_OAR1_ADDMODE bit
191 */
192 LL_I2C_SetOwnAddress1(I2Cx, I2C_InitStruct->OwnAddress1, I2C_InitStruct->OwnAddrSize);
193
194 /*---------------------------- I2Cx MODE Configuration -----------------------
195 * Configure I2Cx peripheral mode with parameter :
196 * - PeripheralMode: I2C_CR1_SMBUS, I2C_CR1_SMBTYPE and I2C_CR1_ENARP bits
197 */
198 LL_I2C_SetMode(I2Cx, I2C_InitStruct->PeripheralMode);
199
200 /* Enable the selected I2Cx Peripheral */
201 LL_I2C_Enable(I2Cx);
202
203 /*---------------------------- I2Cx CR2 Configuration ------------------------
204 * Configure the ACKnowledge or Non ACKnowledge condition
205 * after the address receive match code or next received byte with parameter :
206 * - TypeAcknowledge: I2C_CR2_NACK bit
207 */
208 LL_I2C_AcknowledgeNextData(I2Cx, I2C_InitStruct->TypeAcknowledge);
209
210 return SUCCESS;
211}
212
213/**
214 * @brief Set each @ref LL_I2C_InitTypeDef field to default value.
215 * @param I2C_InitStruct Pointer to a @ref LL_I2C_InitTypeDef structure.
216 * @retval None
217 */
218void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct)
219{
220 /* Set I2C_InitStruct fields to default values */
221 I2C_InitStruct->PeripheralMode = LL_I2C_MODE_I2C;
222 I2C_InitStruct->ClockSpeed = 5000U;
223 I2C_InitStruct->DutyCycle = LL_I2C_DUTYCYCLE_2;
224#if defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF)
225 I2C_InitStruct->AnalogFilter = LL_I2C_ANALOGFILTER_ENABLE;
226 I2C_InitStruct->DigitalFilter = 0U;
227#endif
228 I2C_InitStruct->OwnAddress1 = 0U;
229 I2C_InitStruct->TypeAcknowledge = LL_I2C_NACK;
230 I2C_InitStruct->OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
231}
232
233/**
234 * @}
235 */
236
237/**
238 * @}
239 */
240
241/**
242 * @}
243 */
244
245#endif /* I2C1 || I2C2 || I2C3 */
246
247/**
248 * @}
249 */
250
251#endif /* USE_FULL_LL_DRIVER */
252
253/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Note: See TracBrowser for help on using the repository browser.