STM32L4xx_HAL_Driver  1.14.0
stm32l4xx_ll_i2c.c
Go to the documentation of this file.
1 
19 #if defined(USE_FULL_LL_DRIVER)
20 
21 /* Includes ------------------------------------------------------------------*/
22 #include "stm32l4xx_ll_i2c.h"
23 #include "stm32l4xx_ll_bus.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) || defined (I2C4)
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_ANALOG_FILTER(__VALUE__) (((__VALUE__) == LL_I2C_ANALOGFILTER_ENABLE) || \
54  ((__VALUE__) == LL_I2C_ANALOGFILTER_DISABLE))
55 
56 #define IS_LL_I2C_DIGITAL_FILTER(__VALUE__) ((__VALUE__) <= 0x0000000FU)
57 
58 #define IS_LL_I2C_OWN_ADDRESS1(__VALUE__) ((__VALUE__) <= 0x000003FFU)
59 
60 #define IS_LL_I2C_TYPE_ACKNOWLEDGE(__VALUE__) (((__VALUE__) == LL_I2C_ACK) || \
61  ((__VALUE__) == LL_I2C_NACK))
62 
63 #define IS_LL_I2C_OWN_ADDRSIZE(__VALUE__) (((__VALUE__) == LL_I2C_OWNADDRESS1_7BIT) || \
64  ((__VALUE__) == LL_I2C_OWNADDRESS1_10BIT))
65 
69 /* Private function prototypes -----------------------------------------------*/
70 
71 /* Exported functions --------------------------------------------------------*/
87 ErrorStatus LL_I2C_DeInit(I2C_TypeDef *I2Cx)
88 {
89  ErrorStatus status = SUCCESS;
90 
91  /* Check the I2C Instance I2Cx */
92  assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
93 
94  if (I2Cx == I2C1)
95  {
96  /* Force reset of I2C clock */
97  LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C1);
98 
99  /* Release reset of I2C clock */
100  LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C1);
101  }
102 #if defined(I2C2)
103  else if (I2Cx == I2C2)
104  {
105  /* Force reset of I2C clock */
106  LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C2);
107 
108  /* Release reset of I2C clock */
109  LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C2);
110 
111  }
112 #endif
113  else if (I2Cx == I2C3)
114  {
115  /* Force reset of I2C clock */
116  LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C3);
117 
118  /* Release reset of I2C clock */
119  LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C3);
120  }
121 #if defined(I2C4)
122  else if (I2Cx == I2C4)
123  {
124  /* Force reset of I2C clock */
125  LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_I2C4);
126 
127  /* Release reset of I2C clock */
128  LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_I2C4);
129  }
130 #endif
131  else
132  {
133  status = ERROR;
134  }
135 
136  return status;
137 }
138 
147 ErrorStatus LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct)
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_ANALOG_FILTER(I2C_InitStruct->AnalogFilter));
155  assert_param(IS_LL_I2C_DIGITAL_FILTER(I2C_InitStruct->DigitalFilter));
156  assert_param(IS_LL_I2C_OWN_ADDRESS1(I2C_InitStruct->OwnAddress1));
157  assert_param(IS_LL_I2C_TYPE_ACKNOWLEDGE(I2C_InitStruct->TypeAcknowledge));
158  assert_param(IS_LL_I2C_OWN_ADDRSIZE(I2C_InitStruct->OwnAddrSize));
159 
160  /* Disable the selected I2Cx Peripheral */
161  LL_I2C_Disable(I2Cx);
162 
163  /*---------------------------- I2Cx CR1 Configuration ------------------------
164  * Configure the analog and digital noise filters with parameters :
165  * - AnalogFilter: I2C_CR1_ANFOFF bit
166  * - DigitalFilter: I2C_CR1_DNF[3:0] bits
167  */
168  LL_I2C_ConfigFilters(I2Cx, I2C_InitStruct->AnalogFilter, I2C_InitStruct->DigitalFilter);
169 
170  /*---------------------------- I2Cx TIMINGR Configuration --------------------
171  * Configure the SDA setup, hold time and the SCL high, low period with parameter :
172  * - Timing: I2C_TIMINGR_PRESC[3:0], I2C_TIMINGR_SCLDEL[3:0], I2C_TIMINGR_SDADEL[3:0],
173  * I2C_TIMINGR_SCLH[7:0] and I2C_TIMINGR_SCLL[7:0] bits
174  */
175  LL_I2C_SetTiming(I2Cx, I2C_InitStruct->Timing);
176 
177  /* Enable the selected I2Cx Peripheral */
178  LL_I2C_Enable(I2Cx);
179 
180  /*---------------------------- I2Cx OAR1 Configuration -----------------------
181  * Disable, Configure and Enable I2Cx device own address 1 with parameters :
182  * - OwnAddress1: I2C_OAR1_OA1[9:0] bits
183  * - OwnAddrSize: I2C_OAR1_OA1MODE bit
184  */
186  LL_I2C_SetOwnAddress1(I2Cx, I2C_InitStruct->OwnAddress1, I2C_InitStruct->OwnAddrSize);
187 
188  /* OwnAdress1 == 0 is reserved for General Call address */
189  if (I2C_InitStruct->OwnAddress1 != 0U)
190  {
192  }
193 
194  /*---------------------------- I2Cx MODE Configuration -----------------------
195  * Configure I2Cx peripheral mode with parameter :
196  * - PeripheralMode: I2C_CR1_SMBDEN and I2C_CR1_SMBHEN bits
197  */
198  LL_I2C_SetMode(I2Cx, I2C_InitStruct->PeripheralMode);
199 
200  /*---------------------------- I2Cx CR2 Configuration ------------------------
201  * Configure the ACKnowledge or Non ACKnowledge condition
202  * after the address receive match code or next received byte with parameter :
203  * - TypeAcknowledge: I2C_CR2_NACK bit
204  */
205  LL_I2C_AcknowledgeNextData(I2Cx, I2C_InitStruct->TypeAcknowledge);
206 
207  return SUCCESS;
208 }
209 
216 {
217  /* Set I2C_InitStruct fields to default values */
218  I2C_InitStruct->PeripheralMode = LL_I2C_MODE_I2C;
219  I2C_InitStruct->Timing = 0U;
220  I2C_InitStruct->AnalogFilter = LL_I2C_ANALOGFILTER_ENABLE;
221  I2C_InitStruct->DigitalFilter = 0U;
222  I2C_InitStruct->OwnAddress1 = 0U;
223  I2C_InitStruct->TypeAcknowledge = LL_I2C_NACK;
224  I2C_InitStruct->OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
225 }
226 
239 #endif /* I2C1 || I2C2 || I2C3 || I2C4 */
240 
245 #endif /* USE_FULL_LL_DRIVER */
246 
247 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
__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_SetMode(I2C_TypeDef *I2Cx, uint32_t PeripheralMode)
Configure peripheral mode.
ErrorStatus LL_I2C_DeInit(I2C_TypeDef *I2Cx)
De-initialize the I2C registers to their default reset values.
Header file of I2C LL module.
__STATIC_INLINE void LL_I2C_Enable(I2C_TypeDef *I2Cx)
Enable I2C peripheral (PE = 1). CR1 PE LL_I2C_Enable.
__STATIC_INLINE void LL_APB1_GRP1_ReleaseReset(uint32_t Periphs)
Release APB1 peripherals reset. APB1RSTR1 TIM2RST LL_APB1_GRP1_ReleaseReset APB1RSTR1 TIM3RST LL_AP...
__STATIC_INLINE void LL_I2C_SetOwnAddress1(I2C_TypeDef *I2Cx, uint32_t OwnAddress1, uint32_t OwnAddrSize)
Set the Own Address1. OAR1 OA1 LL_I2C_SetOwnAddress1 OAR1 OA1MODE LL_I2C_SetOwnAddress1.
__STATIC_INLINE void LL_APB1_GRP2_ForceReset(uint32_t Periphs)
Force APB1 peripherals reset. APB1RSTR2 LPUART1RST LL_APB1_GRP2_ForceReset APB1RSTR2 I2C4RST LL_APB...
__STATIC_INLINE void LL_I2C_EnableOwnAddress1(I2C_TypeDef *I2Cx)
Enable acknowledge on Own Address1 match address. OAR1 OA1EN LL_I2C_EnableOwnAddress1.
__STATIC_INLINE void LL_I2C_DisableOwnAddress1(I2C_TypeDef *I2Cx)
Disable acknowledge on Own Address1 match address. OAR1 OA1EN LL_I2C_DisableOwnAddress1.
__STATIC_INLINE void LL_APB1_GRP2_ReleaseReset(uint32_t Periphs)
Release APB1 peripherals reset. APB1RSTR2 LPUART1RST LL_APB1_GRP2_ReleaseReset APB1RSTR2 I2C4RST LL...
ErrorStatus LL_I2C_Init(I2C_TypeDef *I2Cx, LL_I2C_InitTypeDef *I2C_InitStruct)
Initialize the I2C registers according to the specified parameters in I2C_InitStruct.
Header file of BUS LL module.
__STATIC_INLINE void LL_I2C_Disable(I2C_TypeDef *I2Cx)
Disable I2C peripheral (PE = 0).
__STATIC_INLINE void LL_APB1_GRP1_ForceReset(uint32_t Periphs)
Force APB1 peripherals reset. APB1RSTR1 TIM2RST LL_APB1_GRP1_ForceReset APB1RSTR1 TIM3RST LL_APB1_G...
__STATIC_INLINE void LL_I2C_SetTiming(I2C_TypeDef *I2Cx, uint32_t Timing)
Configure the SDA setup, hold time and the SCL high, low period.
__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 ...
void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct)
Set each LL_I2C_InitTypeDef field to default value.
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))