STM32F4xx_HAL_Driver  1.8.3
stm32f4xx_ll_i2c.c
Go to the documentation of this file.
1 
18 #if defined(USE_FULL_LL_DRIVER)
19 
20 /* Includes ------------------------------------------------------------------*/
21 #include "stm32f4xx_ll_i2c.h"
22 #include "stm32f4xx_ll_bus.h"
23 #include "stm32f4xx_ll_rcc.h"
24 #ifdef USE_FULL_ASSERT
25 #include "stm32_assert.h"
26 #else
27 #define assert_param(expr) ((void)0U)
28 #endif
29 
34 #if defined (I2C1) || defined (I2C2) || defined (I2C3)
35 
40 /* Private types -------------------------------------------------------------*/
41 /* Private variables ---------------------------------------------------------*/
42 /* Private constants ---------------------------------------------------------*/
43 /* Private macros ------------------------------------------------------------*/
48 #define IS_LL_I2C_PERIPHERAL_MODE(__VALUE__) (((__VALUE__) == LL_I2C_MODE_I2C) || \
49  ((__VALUE__) == LL_I2C_MODE_SMBUS_HOST) || \
50  ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE) || \
51  ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE_ARP))
52 
53 #define IS_LL_I2C_CLOCK_SPEED(__VALUE__) (((__VALUE__) > 0U) && ((__VALUE__) <= LL_I2C_MAX_SPEED_FAST))
54 
55 #define IS_LL_I2C_DUTY_CYCLE(__VALUE__) (((__VALUE__) == LL_I2C_DUTYCYCLE_2) || \
56  ((__VALUE__) == LL_I2C_DUTYCYCLE_16_9))
57 
58 #if defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF)
59 #define IS_LL_I2C_ANALOG_FILTER(__VALUE__) (((__VALUE__) == LL_I2C_ANALOGFILTER_ENABLE) || \
60  ((__VALUE__) == LL_I2C_ANALOGFILTER_DISABLE))
61 
62 #define IS_LL_I2C_DIGITAL_FILTER(__VALUE__) ((__VALUE__) <= 0x0000000FU)
63 
64 #endif
65 #define IS_LL_I2C_OWN_ADDRESS1(__VALUE__) ((__VALUE__) <= 0x000003FFU)
66 
67 #define IS_LL_I2C_TYPE_ACKNOWLEDGE(__VALUE__) (((__VALUE__) == LL_I2C_ACK) || \
68  ((__VALUE__) == LL_I2C_NACK))
69 
70 #define IS_LL_I2C_OWN_ADDRSIZE(__VALUE__) (((__VALUE__) == LL_I2C_OWNADDRESS1_7BIT) || \
71  ((__VALUE__) == LL_I2C_OWNADDRESS1_10BIT))
76 /* Private function prototypes -----------------------------------------------*/
77 
78 /* Exported functions --------------------------------------------------------*/
94 uint32_t LL_I2C_DeInit(I2C_TypeDef *I2Cx)
95 {
96  ErrorStatus status = SUCCESS;
97 
98  /* Check the I2C Instance I2Cx */
99  assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
100 
101  if (I2Cx == I2C1)
102  {
103  /* Force reset of I2C clock */
104  LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C1);
105 
106  /* Release reset of I2C clock */
107  LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C1);
108  }
109  else if (I2Cx == I2C2)
110  {
111  /* Force reset of I2C clock */
112  LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C2);
113 
114  /* Release reset of I2C clock */
115  LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C2);
116 
117  }
118 #if defined(I2C3)
119  else if (I2Cx == I2C3)
120  {
121  /* Force reset of I2C clock */
122  LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C3);
123 
124  /* Release reset of I2C clock */
125  LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C3);
126  }
127 #endif
128  else
129  {
130  status = ERROR;
131  }
132 
133  return status;
134 }
135 
144 uint32_t LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct)
145 {
146  LL_RCC_ClocksTypeDef rcc_clocks;
147 
148  /* Check the I2C Instance I2Cx */
149  assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
150 
151  /* Check the I2C parameters from I2C_InitStruct */
152  assert_param(IS_LL_I2C_PERIPHERAL_MODE(I2C_InitStruct->PeripheralMode));
153  assert_param(IS_LL_I2C_CLOCK_SPEED(I2C_InitStruct->ClockSpeed));
154  assert_param(IS_LL_I2C_DUTY_CYCLE(I2C_InitStruct->DutyCycle));
155 #if defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF)
156  assert_param(IS_LL_I2C_ANALOG_FILTER(I2C_InitStruct->AnalogFilter));
157  assert_param(IS_LL_I2C_DIGITAL_FILTER(I2C_InitStruct->DigitalFilter));
158 #endif
159  assert_param(IS_LL_I2C_OWN_ADDRESS1(I2C_InitStruct->OwnAddress1));
160  assert_param(IS_LL_I2C_TYPE_ACKNOWLEDGE(I2C_InitStruct->TypeAcknowledge));
161  assert_param(IS_LL_I2C_OWN_ADDRSIZE(I2C_InitStruct->OwnAddrSize));
162 
163  /* Disable the selected I2Cx Peripheral */
164  LL_I2C_Disable(I2Cx);
165 
166  /* Retrieve Clock frequencies */
167  LL_RCC_GetSystemClocksFreq(&rcc_clocks);
168 
169 #if defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF)
170  /*---------------------------- I2Cx FLTR Configuration -----------------------
171  * Configure the analog and digital noise filters with parameters :
172  * - AnalogFilter: I2C_FLTR_ANFOFF bit
173  * - DigitalFilter: I2C_FLTR_DNF[3:0] bits
174  */
175  LL_I2C_ConfigFilters(I2Cx, I2C_InitStruct->AnalogFilter, I2C_InitStruct->DigitalFilter);
176 
177 #endif
178  /*---------------------------- I2Cx SCL Clock Speed Configuration ------------
179  * Configure the SCL speed :
180  * - ClockSpeed: I2C_CR2_FREQ[5:0], I2C_TRISE_TRISE[5:0], I2C_CCR_FS,
181  * and I2C_CCR_CCR[11:0] bits
182  * - DutyCycle: I2C_CCR_DUTY[7:0] bits
183  */
184  LL_I2C_ConfigSpeed(I2Cx, rcc_clocks.PCLK1_Frequency, I2C_InitStruct->ClockSpeed, I2C_InitStruct->DutyCycle);
185 
186  /*---------------------------- I2Cx OAR1 Configuration -----------------------
187  * Disable, Configure and Enable I2Cx device own address 1 with parameters :
188  * - OwnAddress1: I2C_OAR1_ADD[9:8], I2C_OAR1_ADD[7:1] and I2C_OAR1_ADD0 bits
189  * - OwnAddrSize: I2C_OAR1_ADDMODE bit
190  */
191  LL_I2C_SetOwnAddress1(I2Cx, I2C_InitStruct->OwnAddress1, I2C_InitStruct->OwnAddrSize);
192 
193  /*---------------------------- I2Cx MODE Configuration -----------------------
194  * Configure I2Cx peripheral mode with parameter :
195  * - PeripheralMode: I2C_CR1_SMBUS, I2C_CR1_SMBTYPE and I2C_CR1_ENARP bits
196  */
197  LL_I2C_SetMode(I2Cx, I2C_InitStruct->PeripheralMode);
198 
199  /* Enable the selected I2Cx Peripheral */
200  LL_I2C_Enable(I2Cx);
201 
202  /*---------------------------- I2Cx CR2 Configuration ------------------------
203  * Configure the ACKnowledge or Non ACKnowledge condition
204  * after the address receive match code or next received byte with parameter :
205  * - TypeAcknowledge: I2C_CR2_NACK bit
206  */
207  LL_I2C_AcknowledgeNextData(I2Cx, I2C_InitStruct->TypeAcknowledge);
208 
209  return SUCCESS;
210 }
211 
218 {
219  /* Set I2C_InitStruct fields to default values */
220  I2C_InitStruct->PeripheralMode = LL_I2C_MODE_I2C;
221  I2C_InitStruct->ClockSpeed = 5000U;
222  I2C_InitStruct->DutyCycle = LL_I2C_DUTYCYCLE_2;
223 #if defined(I2C_FLTR_ANOFF)&&defined(I2C_FLTR_DNF)
224  I2C_InitStruct->AnalogFilter = LL_I2C_ANALOGFILTER_ENABLE;
225  I2C_InitStruct->DigitalFilter = 0U;
226 #endif
227  I2C_InitStruct->OwnAddress1 = 0U;
228  I2C_InitStruct->TypeAcknowledge = LL_I2C_NACK;
229  I2C_InitStruct->OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
230 }
231 
244 #endif /* I2C1 || I2C2 || I2C3 */
245 
250 #endif /* USE_FULL_LL_DRIVER */
251 
__STATIC_INLINE void LL_APB1_GRP1_ForceReset(uint32_t Periphs)
Force APB1 peripherals reset. @rmtoll APB1RSTR TIM2RST LL_APB1_GRP1_ForceReset APB1RSTR TIM3RST LL_A...
__STATIC_INLINE void LL_APB1_GRP1_ReleaseReset(uint32_t Periphs)
Release APB1 peripherals reset. @rmtoll APB1RSTR TIM2RST LL_APB1_GRP1_ReleaseReset APB1RSTR TIM3RST ...
__STATIC_INLINE void LL_I2C_Disable(I2C_TypeDef *I2Cx)
Disable I2C peripheral (PE = 0). @rmtoll CR1 PE LL_I2C_Disable.
__STATIC_INLINE void LL_I2C_ConfigFilters(I2C_TypeDef *I2Cx, uint32_t AnalogFilter, uint32_t DigitalFilter)
Configure Noise Filters (Analog and Digital).
__STATIC_INLINE void LL_I2C_ConfigSpeed(I2C_TypeDef *I2Cx, uint32_t PeriphClock, uint32_t ClockSpeed, uint32_t DutyCycle)
Configure the SCL speed.
__STATIC_INLINE void LL_I2C_Enable(I2C_TypeDef *I2Cx)
Enable I2C peripheral (PE = 1). @rmtoll CR1 PE LL_I2C_Enable.
__STATIC_INLINE void LL_I2C_SetMode(I2C_TypeDef *I2Cx, uint32_t PeripheralMode)
Configure peripheral mode.
__STATIC_INLINE void LL_I2C_SetOwnAddress1(I2C_TypeDef *I2Cx, uint32_t OwnAddress1, uint32_t OwnAddrSize)
Set the Own Address1. @rmtoll OAR1 ADD0 LL_I2C_SetOwnAddress1 OAR1 ADD1_7 LL_I2C_SetOwnAddress1 OAR...
__STATIC_INLINE void LL_I2C_AcknowledgeNextData(I2C_TypeDef *I2Cx, uint32_t TypeAcknowledge)
Prepare the generation of a ACKnowledge or Non ACKnowledge condition after the address receive match ...
uint32_t LL_I2C_DeInit(I2C_TypeDef *I2Cx)
De-initialize the I2C registers to their default reset values.
void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct)
Set each LL_I2C_InitTypeDef field to default value.
uint32_t LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct)
Initialize the I2C registers according to the specified parameters in I2C_InitStruct.
RCC Clocks Frequency Structure.
void LL_RCC_GetSystemClocksFreq(LL_RCC_ClocksTypeDef *RCC_Clocks)
Return the frequencies of different on chip clocks; System, AHB, APB1 and APB2 buses clocks.
Header file of BUS LL module.
Header file of I2C LL module.
Header file of RCC LL module.