1 /**
2   ******************************************************************************
3   * @file    stm32u5xx_ll_i2c.c
4   * @author  MCD Application Team
5   * @brief   I2C LL module driver.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2021 STMicroelectronics.
10   * All rights reserved.
11   *
12   * This software is licensed under terms that can be found in the LICENSE file
13   * in the root directory of this software component.
14   * If no LICENSE file comes with this software, it is provided AS-IS.
15   *
16   ******************************************************************************
17   */
18 #if defined(USE_FULL_LL_DRIVER)
19 
20 /* Includes ------------------------------------------------------------------*/
21 #include "stm32u5xx_ll_i2c.h"
22 #include "stm32u5xx_ll_bus.h"
23 #ifdef  USE_FULL_ASSERT
24 #include "stm32_assert.h"
25 #else
26 #define assert_param(expr) ((void)0U)
27 #endif /* USE_FULL_ASSERT */
28 
29 /** @addtogroup STM32U5xx_LL_Driver
30   * @{
31   */
32 
33 #if defined (I2C1) || defined (I2C2) || defined (I2C3) || defined (I2C4) || defined (I2C5) || defined (I2C6)
34 
35 /** @defgroup I2C_LL I2C
36   * @{
37   */
38 
39 /* Private types -------------------------------------------------------------*/
40 /* Private variables ---------------------------------------------------------*/
41 /* Private constants ---------------------------------------------------------*/
42 /* Private macros ------------------------------------------------------------*/
43 /** @addtogroup I2C_LL_Private_Macros
44   * @{
45   */
46 
47 #define IS_LL_I2C_PERIPHERAL_MODE(__VALUE__)    (((__VALUE__) == LL_I2C_MODE_I2C)          || \
48                                                  ((__VALUE__) == LL_I2C_MODE_SMBUS_HOST)   || \
49                                                  ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE) || \
50                                                  ((__VALUE__) == LL_I2C_MODE_SMBUS_DEVICE_ARP))
51 
52 #define IS_LL_I2C_ANALOG_FILTER(__VALUE__)      (((__VALUE__) == LL_I2C_ANALOGFILTER_ENABLE) || \
53                                                  ((__VALUE__) == LL_I2C_ANALOGFILTER_DISABLE))
54 
55 #define IS_LL_I2C_DIGITAL_FILTER(__VALUE__)     ((__VALUE__) <= 0x0000000FU)
56 
57 #define IS_LL_I2C_OWN_ADDRESS1(__VALUE__)       ((__VALUE__) <= 0x000003FFU)
58 
59 #define IS_LL_I2C_TYPE_ACKNOWLEDGE(__VALUE__)   (((__VALUE__) == LL_I2C_ACK) || \
60                                                  ((__VALUE__) == LL_I2C_NACK))
61 
62 #define IS_LL_I2C_OWN_ADDRSIZE(__VALUE__)       (((__VALUE__) == LL_I2C_OWNADDRESS1_7BIT) || \
63                                                  ((__VALUE__) == LL_I2C_OWNADDRESS1_10BIT))
64 /**
65   * @}
66   */
67 
68 /* Private function prototypes -----------------------------------------------*/
69 
70 /* Exported functions --------------------------------------------------------*/
71 /** @addtogroup I2C_LL_Exported_Functions
72   * @{
73   */
74 
75 /** @addtogroup I2C_LL_EF_Init
76   * @{
77   */
78 
79 /**
80   * @brief  De-initialize the I2C registers to their default reset values.
81   * @param  I2Cx I2C Instance.
82   * @retval An ErrorStatus enumeration value:
83   *          - SUCCESS: I2C registers are de-initialized
84   *          - ERROR: I2C registers are not de-initialized
85   */
LL_I2C_DeInit(const I2C_TypeDef * I2Cx)86 ErrorStatus LL_I2C_DeInit(const I2C_TypeDef *I2Cx)
87 {
88   ErrorStatus status = SUCCESS;
89 
90   /* Check the I2C Instance I2Cx */
91   assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
92 
93   if (I2Cx == I2C1)
94   {
95     /* Force reset of I2C clock */
96     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C1);
97 
98     /* Release reset of I2C clock */
99     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C1);
100   }
101   else if (I2Cx == I2C2)
102   {
103     /* Force reset of I2C clock */
104     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C2);
105 
106     /* Release reset of I2C clock */
107     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C2);
108 
109   }
110   else if (I2Cx == I2C3)
111   {
112     /* Force reset of I2C clock */
113     LL_APB3_GRP1_ForceReset(LL_APB3_GRP1_PERIPH_I2C3);
114 
115     /* Release reset of I2C clock */
116     LL_APB3_GRP1_ReleaseReset(LL_APB3_GRP1_PERIPH_I2C3);
117   }
118   else if (I2Cx == I2C4)
119   {
120     /* Force reset of I2C clock */
121     LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_I2C4);
122 
123     /* Release reset of I2C clock */
124     LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_I2C4);
125   }
126 #if defined(I2C5)
127   else if (I2Cx == I2C5)
128   {
129     /* Force reset of I2C clock */
130     LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_I2C5);
131 
132     /* Release reset of I2C clock */
133     LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_I2C5);
134   }
135 #endif /* I2C5 */
136 #if defined(I2C6)
137   else if (I2Cx == I2C6)
138   {
139     /* Force reset of I2C clock */
140     LL_APB1_GRP2_ForceReset(LL_APB1_GRP2_PERIPH_I2C6);
141 
142     /* Release reset of I2C clock */
143     LL_APB1_GRP2_ReleaseReset(LL_APB1_GRP2_PERIPH_I2C6);
144   }
145 #endif /* I2C6 */
146   else
147   {
148     status = ERROR;
149   }
150 
151   return status;
152 }
153 
154 /**
155   * @brief  Initialize the I2C registers according to the specified parameters in I2C_InitStruct.
156   * @param  I2Cx I2C Instance.
157   * @param  I2C_InitStruct pointer to a @ref LL_I2C_InitTypeDef structure.
158   * @retval An ErrorStatus enumeration value:
159   *          - SUCCESS: I2C registers are initialized
160   *          - ERROR: Not applicable
161   */
LL_I2C_Init(I2C_TypeDef * I2Cx,const LL_I2C_InitTypeDef * I2C_InitStruct)162 ErrorStatus LL_I2C_Init(I2C_TypeDef *I2Cx, const LL_I2C_InitTypeDef *I2C_InitStruct)
163 {
164   /* Check the I2C Instance I2Cx */
165   assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
166 
167   /* Check the I2C parameters from I2C_InitStruct */
168   assert_param(IS_LL_I2C_PERIPHERAL_MODE(I2C_InitStruct->PeripheralMode));
169   assert_param(IS_LL_I2C_ANALOG_FILTER(I2C_InitStruct->AnalogFilter));
170   assert_param(IS_LL_I2C_DIGITAL_FILTER(I2C_InitStruct->DigitalFilter));
171   assert_param(IS_LL_I2C_OWN_ADDRESS1(I2C_InitStruct->OwnAddress1));
172   assert_param(IS_LL_I2C_TYPE_ACKNOWLEDGE(I2C_InitStruct->TypeAcknowledge));
173   assert_param(IS_LL_I2C_OWN_ADDRSIZE(I2C_InitStruct->OwnAddrSize));
174 
175   /* Disable the selected I2Cx Peripheral */
176   LL_I2C_Disable(I2Cx);
177 
178   /*---------------------------- I2Cx CR1 Configuration ------------------------
179    * Configure the analog and digital noise filters with parameters :
180    * - AnalogFilter: I2C_CR1_ANFOFF bit
181    * - DigitalFilter: I2C_CR1_DNF[3:0] bits
182    */
183   LL_I2C_ConfigFilters(I2Cx, I2C_InitStruct->AnalogFilter, I2C_InitStruct->DigitalFilter);
184 
185   /*---------------------------- I2Cx TIMINGR Configuration --------------------
186    * Configure the SDA setup, hold time and the SCL high, low period with parameter :
187    * - Timing: I2C_TIMINGR_PRESC[3:0], I2C_TIMINGR_SCLDEL[3:0], I2C_TIMINGR_SDADEL[3:0],
188    *           I2C_TIMINGR_SCLH[7:0] and I2C_TIMINGR_SCLL[7:0] bits
189    */
190   LL_I2C_SetTiming(I2Cx, I2C_InitStruct->Timing);
191 
192   /* Enable the selected I2Cx Peripheral */
193   LL_I2C_Enable(I2Cx);
194 
195   /*---------------------------- I2Cx OAR1 Configuration -----------------------
196    * Disable, Configure and Enable I2Cx device own address 1 with parameters :
197    * - OwnAddress1:  I2C_OAR1_OA1[9:0] bits
198    * - OwnAddrSize:  I2C_OAR1_OA1MODE bit
199    */
200   LL_I2C_DisableOwnAddress1(I2Cx);
201   LL_I2C_SetOwnAddress1(I2Cx, I2C_InitStruct->OwnAddress1, I2C_InitStruct->OwnAddrSize);
202 
203   /* OwnAdress1 == 0 is reserved for General Call address */
204   if (I2C_InitStruct->OwnAddress1 != 0U)
205   {
206     LL_I2C_EnableOwnAddress1(I2Cx);
207   }
208 
209   /*---------------------------- I2Cx MODE Configuration -----------------------
210   * Configure I2Cx peripheral mode with parameter :
211    * - PeripheralMode: I2C_CR1_SMBDEN and I2C_CR1_SMBHEN bits
212    */
213   LL_I2C_SetMode(I2Cx, I2C_InitStruct->PeripheralMode);
214 
215   /*---------------------------- I2Cx CR2 Configuration ------------------------
216    * Configure the ACKnowledge or Non ACKnowledge condition
217    * after the address receive match code or next received byte with parameter :
218    * - TypeAcknowledge: I2C_CR2_NACK bit
219    */
220   LL_I2C_AcknowledgeNextData(I2Cx, I2C_InitStruct->TypeAcknowledge);
221 
222   return SUCCESS;
223 }
224 
225 /**
226   * @brief  Set each @ref LL_I2C_InitTypeDef field to default value.
227   * @param  I2C_InitStruct Pointer to a @ref LL_I2C_InitTypeDef structure.
228   * @retval None
229   */
LL_I2C_StructInit(LL_I2C_InitTypeDef * I2C_InitStruct)230 void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct)
231 {
232   /* Set I2C_InitStruct fields to default values */
233   I2C_InitStruct->PeripheralMode  = LL_I2C_MODE_I2C;
234   I2C_InitStruct->Timing          = 0U;
235   I2C_InitStruct->AnalogFilter    = LL_I2C_ANALOGFILTER_ENABLE;
236   I2C_InitStruct->DigitalFilter   = 0U;
237   I2C_InitStruct->OwnAddress1     = 0U;
238   I2C_InitStruct->TypeAcknowledge = LL_I2C_NACK;
239   I2C_InitStruct->OwnAddrSize     = LL_I2C_OWNADDRESS1_7BIT;
240 }
241 
242 /**
243   * @}
244   */
245 
246 /**
247   * @}
248   */
249 
250 /**
251   * @}
252   */
253 
254 #endif /* I2C1 || I2C2 || I2C3 || I2C4 || I2C5 || I2C6 */
255 
256 /**
257   * @}
258   */
259 
260 #endif /* USE_FULL_LL_DRIVER */
261