1 /**
2   ******************************************************************************
3   * @file    stm32u0xx_ll_i2c.c
4   * @author  MCD Application Team
5   * @brief   I2C LL module driver.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2023 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 "stm32u0xx_ll_i2c.h"
22 #include "stm32u0xx_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 STM32U0xx_LL_Driver
30   * @{
31   */
32 
33 #if defined (I2C1) || defined (I2C2) || defined (I2C3) || defined (I2C4)
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_ANALOG_FILTER(__VALUE__)      (((__VALUE__) == LL_I2C_ANALOGFILTER_ENABLE) || \
48                                                  ((__VALUE__) == LL_I2C_ANALOGFILTER_DISABLE))
49 
50 #define IS_LL_I2C_DIGITAL_FILTER(__VALUE__)     ((__VALUE__) <= 0x0000000FU)
51 
52 #define IS_LL_I2C_OWN_ADDRESS1(__VALUE__)       ((__VALUE__) <= 0x000003FFU)
53 
54 #define IS_LL_I2C_TYPE_ACKNOWLEDGE(__VALUE__)   (((__VALUE__) == LL_I2C_ACK) || \
55                                                  ((__VALUE__) == LL_I2C_NACK))
56 
57 #define IS_LL_I2C_OWN_ADDRSIZE(__VALUE__)       (((__VALUE__) == LL_I2C_OWNADDRESS1_7BIT) || \
58                                                  ((__VALUE__) == LL_I2C_OWNADDRESS1_10BIT))
59 /**
60   * @}
61   */
62 
63 /* Private function prototypes -----------------------------------------------*/
64 
65 /* Exported functions --------------------------------------------------------*/
66 /** @addtogroup I2C_LL_Exported_Functions
67   * @{
68   */
69 
70 /** @addtogroup I2C_LL_EF_Init
71   * @{
72   */
73 
74 /**
75   * @brief  De-initialize the I2C registers to their default reset values.
76   * @param  I2Cx I2C Instance.
77   * @retval An ErrorStatus enumeration value:
78   *          - SUCCESS: I2C registers are de-initialized
79   *          - ERROR: I2C registers are not de-initialized
80   */
LL_I2C_DeInit(const I2C_TypeDef * I2Cx)81 ErrorStatus LL_I2C_DeInit(const I2C_TypeDef *I2Cx)
82 {
83   ErrorStatus status = SUCCESS;
84 
85   /* Check the I2C Instance I2Cx */
86   assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
87 
88   if (I2Cx == I2C1)
89   {
90     /* Force reset of I2C clock */
91     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C1);
92 
93     /* Release reset of I2C clock */
94     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C1);
95   }
96   else if (I2Cx == I2C2)
97   {
98     /* Force reset of I2C clock */
99     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C2);
100 
101     /* Release reset of I2C clock */
102     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C2);
103 
104   }
105   else if (I2Cx == I2C3)
106   {
107     /* Force reset of I2C clock */
108     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C3);
109 
110     /* Release reset of I2C clock */
111     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C3);
112   }
113 #if defined(I2C4)
114   else if (I2Cx == I2C4)
115   {
116     /* Force reset of I2C clock */
117     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_I2C4);
118 
119     /* Release reset of I2C clock */
120     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_I2C4);
121   }
122 #endif /* I2C4 */
123   else
124   {
125     status = ERROR;
126   }
127 
128   return status;
129 }
130 
131 /**
132   * @brief  Initialize the I2C registers according to the specified parameters in I2C_InitStruct.
133   * @param  I2Cx I2C Instance.
134   * @param  I2C_InitStruct pointer to a @ref LL_I2C_InitTypeDef structure.
135   * @retval An ErrorStatus enumeration value:
136   *          - SUCCESS: I2C registers are initialized
137   *          - ERROR: Not applicable
138   */
LL_I2C_Init(I2C_TypeDef * I2Cx,const LL_I2C_InitTypeDef * I2C_InitStruct)139 ErrorStatus LL_I2C_Init(I2C_TypeDef *I2Cx, const LL_I2C_InitTypeDef *I2C_InitStruct)
140 {
141   /* Check the I2C Instance I2Cx */
142   assert_param(IS_I2C_ALL_INSTANCE(I2Cx));
143 
144   /* Check the I2C parameters from I2C_InitStruct */
145   assert_param(IS_LL_I2C_ANALOG_FILTER(I2C_InitStruct->AnalogFilter));
146   assert_param(IS_LL_I2C_DIGITAL_FILTER(I2C_InitStruct->DigitalFilter));
147   assert_param(IS_LL_I2C_OWN_ADDRESS1(I2C_InitStruct->OwnAddress1));
148   assert_param(IS_LL_I2C_TYPE_ACKNOWLEDGE(I2C_InitStruct->TypeAcknowledge));
149   assert_param(IS_LL_I2C_OWN_ADDRSIZE(I2C_InitStruct->OwnAddrSize));
150 
151   /* Disable the selected I2Cx Peripheral */
152   LL_I2C_Disable(I2Cx);
153 
154   /*---------------------------- I2Cx CR1 Configuration ------------------------
155    * Configure the analog and digital noise filters with parameters :
156    * - AnalogFilter: I2C_CR1_ANFOFF bit
157    * - DigitalFilter: I2C_CR1_DNF[3:0] bits
158    */
159   LL_I2C_ConfigFilters(I2Cx, I2C_InitStruct->AnalogFilter, I2C_InitStruct->DigitalFilter);
160 
161   /*---------------------------- I2Cx TIMINGR Configuration --------------------
162    * Configure the SDA setup, hold time and the SCL high, low period with parameter :
163    * - Timing: I2C_TIMINGR_PRESC[3:0], I2C_TIMINGR_SCLDEL[3:0], I2C_TIMINGR_SDADEL[3:0],
164    *           I2C_TIMINGR_SCLH[7:0] and I2C_TIMINGR_SCLL[7:0] bits
165    */
166   LL_I2C_SetTiming(I2Cx, I2C_InitStruct->Timing);
167 
168   /* Enable the selected I2Cx Peripheral */
169   LL_I2C_Enable(I2Cx);
170 
171   /*---------------------------- I2Cx OAR1 Configuration -----------------------
172    * Disable, Configure and Enable I2Cx device own address 1 with parameters :
173    * - OwnAddress1:  I2C_OAR1_OA1[9:0] bits
174    * - OwnAddrSize:  I2C_OAR1_OA1MODE bit
175    */
176   LL_I2C_DisableOwnAddress1(I2Cx);
177   LL_I2C_SetOwnAddress1(I2Cx, I2C_InitStruct->OwnAddress1, I2C_InitStruct->OwnAddrSize);
178 
179   /* OwnAdress1 == 0 is reserved for General Call address */
180   if (I2C_InitStruct->OwnAddress1 != 0U)
181   {
182     LL_I2C_EnableOwnAddress1(I2Cx);
183   }
184 
185   /*---------------------------- I2Cx CR2 Configuration ------------------------
186    * Configure the ACKnowledge or Non ACKnowledge condition
187    * after the address receive match code or next received byte with parameter :
188    * - TypeAcknowledge: I2C_CR2_NACK bit
189    */
190   LL_I2C_AcknowledgeNextData(I2Cx, I2C_InitStruct->TypeAcknowledge);
191 
192   return SUCCESS;
193 }
194 
195 /**
196   * @brief  Set each @ref LL_I2C_InitTypeDef field to default value.
197   * @param  I2C_InitStruct Pointer to a @ref LL_I2C_InitTypeDef structure.
198   * @retval None
199   */
LL_I2C_StructInit(LL_I2C_InitTypeDef * I2C_InitStruct)200 void LL_I2C_StructInit(LL_I2C_InitTypeDef *I2C_InitStruct)
201 {
202   /* Set I2C_InitStruct fields to default values */
203   I2C_InitStruct->Timing          = 0U;
204   I2C_InitStruct->AnalogFilter    = LL_I2C_ANALOGFILTER_ENABLE;
205   I2C_InitStruct->DigitalFilter   = 0U;
206   I2C_InitStruct->OwnAddress1     = 0U;
207   I2C_InitStruct->TypeAcknowledge = LL_I2C_NACK;
208   I2C_InitStruct->OwnAddrSize     = LL_I2C_OWNADDRESS1_7BIT;
209 }
210 
211 /**
212   * @}
213   */
214 
215 /**
216   * @}
217   */
218 
219 /**
220   * @}
221   */
222 
223 #endif /* I2C1 || I2C2 || I2C3 || I2C4 */
224 
225 /**
226   * @}
227   */
228 
229 #endif /* USE_FULL_LL_DRIVER */
230