1 /**
2   ******************************************************************************
3   * @file    stm32u0xx_hal_i2c_ex.c
4   * @author  MCD Application Team
5   * @brief   I2C Extended HAL module driver.
6   *          This file provides firmware functions to manage the following
7   *          functionalities of I2C Extended peripheral:
8   *           + Filter Mode Functions
9   *           + WakeUp Mode Functions
10   *           + FastModePlus Functions
11   *
12   ******************************************************************************
13   * @attention
14   *
15   * Copyright (c) 2023 STMicroelectronics.
16   * All rights reserved.
17   *
18   * This software is licensed under terms that can be found in the LICENSE file
19   * in the root directory of this software component.
20   * If no LICENSE file comes with this software, it is provided AS-IS.
21   *
22   ******************************************************************************
23   @verbatim
24   ==============================================================================
25                ##### I2C peripheral Extended features  #####
26   ==============================================================================
27 
28   [..] Comparing to other previous devices, the I2C interface for STM32U0xx
29        devices contains the following additional features
30 
31        (+) Possibility to disable or enable Analog Noise Filter
32        (+) Use of a configured Digital Noise Filter
33        (+) Disable or enable wakeup from Stop mode(s)
34        (+) Disable or enable Fast Mode Plus
35 
36                      ##### How to use this driver #####
37   ==============================================================================
38   [..] This driver provides functions to configure Noise Filter and Wake Up Feature
39     (#) Configure I2C Analog noise filter using the function HAL_I2CEx_ConfigAnalogFilter()
40     (#) Configure I2C Digital noise filter using the function HAL_I2CEx_ConfigDigitalFilter()
41     (#) Configure the enable or disable of I2C Wake Up Mode using the functions :
42           (++) HAL_I2CEx_EnableWakeUp()
43           (++) HAL_I2CEx_DisableWakeUp()
44     (#) Configure the enable or disable of fast mode plus driving capability using the functions :
45           (++) HAL_I2CEx_ConfigFastModePlus()
46   @endverbatim
47   */
48 
49 /* Includes ------------------------------------------------------------------*/
50 #include "stm32u0xx_hal.h"
51 
52 /** @addtogroup STM32U0xx_HAL_Driver
53   * @{
54   */
55 
56 /** @defgroup I2CEx I2CEx
57   * @brief I2C Extended HAL module driver
58   * @{
59   */
60 
61 #ifdef HAL_I2C_MODULE_ENABLED
62 
63 /* Private typedef -----------------------------------------------------------*/
64 /* Private define ------------------------------------------------------------*/
65 /* Private macro -------------------------------------------------------------*/
66 /* Private variables ---------------------------------------------------------*/
67 /* Private function prototypes -----------------------------------------------*/
68 /* Private functions ---------------------------------------------------------*/
69 
70 /** @defgroup I2CEx_Exported_Functions I2C Extended Exported Functions
71   * @{
72   */
73 
74 /** @defgroup I2CEx_Exported_Functions_Group1 Filter Mode Functions
75   * @brief    Filter Mode Functions
76   *
77 @verbatim
78  ===============================================================================
79                       ##### Filter Mode Functions #####
80  ===============================================================================
81     [..] This section provides functions allowing to:
82       (+) Configure Noise Filters
83 
84 @endverbatim
85   * @{
86   */
87 
88 /**
89   * @brief  Configure I2C Analog noise filter.
90   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
91   *                the configuration information for the specified I2Cx peripheral.
92   * @param  AnalogFilter New state of the Analog filter.
93   * @retval HAL status
94   */
HAL_I2CEx_ConfigAnalogFilter(I2C_HandleTypeDef * hi2c,uint32_t AnalogFilter)95 HAL_StatusTypeDef HAL_I2CEx_ConfigAnalogFilter(I2C_HandleTypeDef *hi2c, uint32_t AnalogFilter)
96 {
97   /* Check the parameters */
98   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
99   assert_param(IS_I2C_ANALOG_FILTER(AnalogFilter));
100 
101   if (hi2c->State == HAL_I2C_STATE_READY)
102   {
103     /* Process Locked */
104     __HAL_LOCK(hi2c);
105 
106     hi2c->State = HAL_I2C_STATE_BUSY;
107 
108     /* Disable the selected I2C peripheral */
109     __HAL_I2C_DISABLE(hi2c);
110 
111     /* Reset I2Cx ANOFF bit */
112     hi2c->Instance->CR1 &= ~(I2C_CR1_ANFOFF);
113 
114     /* Set analog filter bit*/
115     hi2c->Instance->CR1 |= AnalogFilter;
116 
117     __HAL_I2C_ENABLE(hi2c);
118 
119     hi2c->State = HAL_I2C_STATE_READY;
120 
121     /* Process Unlocked */
122     __HAL_UNLOCK(hi2c);
123 
124     return HAL_OK;
125   }
126   else
127   {
128     return HAL_BUSY;
129   }
130 }
131 
132 /**
133   * @brief  Configure I2C Digital noise filter.
134   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
135   *                the configuration information for the specified I2Cx peripheral.
136   * @param  DigitalFilter Coefficient of digital noise filter between Min_Data=0x00 and Max_Data=0x0F.
137   * @retval HAL status
138   */
HAL_I2CEx_ConfigDigitalFilter(I2C_HandleTypeDef * hi2c,uint32_t DigitalFilter)139 HAL_StatusTypeDef HAL_I2CEx_ConfigDigitalFilter(I2C_HandleTypeDef *hi2c, uint32_t DigitalFilter)
140 {
141   uint32_t tmpreg;
142 
143   /* Check the parameters */
144   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
145   assert_param(IS_I2C_DIGITAL_FILTER(DigitalFilter));
146 
147   if (hi2c->State == HAL_I2C_STATE_READY)
148   {
149     /* Process Locked */
150     __HAL_LOCK(hi2c);
151 
152     hi2c->State = HAL_I2C_STATE_BUSY;
153 
154     /* Disable the selected I2C peripheral */
155     __HAL_I2C_DISABLE(hi2c);
156 
157     /* Get the old register value */
158     tmpreg = hi2c->Instance->CR1;
159 
160     /* Reset I2Cx DNF bits [11:8] */
161     tmpreg &= ~(I2C_CR1_DNF);
162 
163     /* Set I2Cx DNF coefficient */
164     tmpreg |= DigitalFilter << 8U;
165 
166     /* Store the new register value */
167     hi2c->Instance->CR1 = tmpreg;
168 
169     __HAL_I2C_ENABLE(hi2c);
170 
171     hi2c->State = HAL_I2C_STATE_READY;
172 
173     /* Process Unlocked */
174     __HAL_UNLOCK(hi2c);
175 
176     return HAL_OK;
177   }
178   else
179   {
180     return HAL_BUSY;
181   }
182 }
183 /**
184   * @}
185   */
186 #if defined(I2C_CR1_WUPEN)
187 
188 /** @defgroup I2CEx_Exported_Functions_Group2 WakeUp Mode Functions
189   * @brief    WakeUp Mode Functions
190   *
191 @verbatim
192  ===============================================================================
193                       ##### WakeUp Mode Functions #####
194  ===============================================================================
195     [..] This section provides functions allowing to:
196       (+) Configure Wake Up Feature
197 
198 @endverbatim
199   * @{
200   */
201 
202 /**
203   * @brief  Enable I2C wakeup from Stop mode(s).
204   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
205   *                the configuration information for the specified I2Cx peripheral.
206   * @retval HAL status
207   */
HAL_I2CEx_EnableWakeUp(I2C_HandleTypeDef * hi2c)208 HAL_StatusTypeDef HAL_I2CEx_EnableWakeUp(I2C_HandleTypeDef *hi2c)
209 {
210   /* Check the parameters */
211   assert_param(IS_I2C_WAKEUP_FROMSTOP_INSTANCE(hi2c->Instance));
212 
213   if (hi2c->State == HAL_I2C_STATE_READY)
214   {
215     /* Process Locked */
216     __HAL_LOCK(hi2c);
217 
218     hi2c->State = HAL_I2C_STATE_BUSY;
219 
220     /* Disable the selected I2C peripheral */
221     __HAL_I2C_DISABLE(hi2c);
222 
223     /* Enable wakeup from stop mode */
224     hi2c->Instance->CR1 |= I2C_CR1_WUPEN;
225 
226     __HAL_I2C_ENABLE(hi2c);
227 
228     hi2c->State = HAL_I2C_STATE_READY;
229 
230     /* Process Unlocked */
231     __HAL_UNLOCK(hi2c);
232 
233     return HAL_OK;
234   }
235   else
236   {
237     return HAL_BUSY;
238   }
239 }
240 
241 /**
242   * @brief  Disable I2C wakeup from Stop mode(s).
243   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
244   *                the configuration information for the specified I2Cx peripheral.
245   * @retval HAL status
246   */
HAL_I2CEx_DisableWakeUp(I2C_HandleTypeDef * hi2c)247 HAL_StatusTypeDef HAL_I2CEx_DisableWakeUp(I2C_HandleTypeDef *hi2c)
248 {
249   /* Check the parameters */
250   assert_param(IS_I2C_WAKEUP_FROMSTOP_INSTANCE(hi2c->Instance));
251 
252   if (hi2c->State == HAL_I2C_STATE_READY)
253   {
254     /* Process Locked */
255     __HAL_LOCK(hi2c);
256 
257     hi2c->State = HAL_I2C_STATE_BUSY;
258 
259     /* Disable the selected I2C peripheral */
260     __HAL_I2C_DISABLE(hi2c);
261 
262     /* Enable wakeup from stop mode */
263     hi2c->Instance->CR1 &= ~(I2C_CR1_WUPEN);
264 
265     __HAL_I2C_ENABLE(hi2c);
266 
267     hi2c->State = HAL_I2C_STATE_READY;
268 
269     /* Process Unlocked */
270     __HAL_UNLOCK(hi2c);
271 
272     return HAL_OK;
273   }
274   else
275   {
276     return HAL_BUSY;
277   }
278 }
279 /**
280   * @}
281   */
282 #endif /* I2C_CR1_WUPEN */
283 
284 /** @defgroup I2CEx_Exported_Functions_Group3 Fast Mode Plus Functions
285   * @brief    Fast Mode Plus Functions
286   *
287 @verbatim
288  ===============================================================================
289                       ##### Fast Mode Plus Functions #####
290  ===============================================================================
291     [..] This section provides functions allowing to:
292       (+) Configure Fast Mode Plus
293 
294 @endverbatim
295   * @{
296   */
297 
298 /**
299   * @brief  Configure I2C Fast Mode Plus.
300   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
301   *                the configuration information for the specified I2Cx peripheral.
302   * @param  FastModePlus New state of the Fast Mode Plus.
303   * @retval HAL status
304   */
HAL_I2CEx_ConfigFastModePlus(I2C_HandleTypeDef * hi2c,uint32_t FastModePlus)305 HAL_StatusTypeDef HAL_I2CEx_ConfigFastModePlus(I2C_HandleTypeDef *hi2c, uint32_t FastModePlus)
306 {
307   /* Check the parameters */
308   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
309   assert_param(IS_I2C_FASTMODEPLUS(FastModePlus));
310 
311   if (hi2c->State == HAL_I2C_STATE_READY)
312   {
313     /* Process Locked */
314     __HAL_LOCK(hi2c);
315 
316     hi2c->State = HAL_I2C_STATE_BUSY;
317 
318     /* Disable the selected I2C peripheral */
319     __HAL_I2C_DISABLE(hi2c);
320 
321     if (FastModePlus == I2C_FASTMODEPLUS_ENABLE)
322     {
323       /* Set I2Cx FMP bit */
324       hi2c->Instance->CR1 |= (I2C_CR1_FMP);
325     }
326     else
327     {
328       /* Reset I2Cx FMP bit */
329       hi2c->Instance->CR1 &= ~(I2C_CR1_FMP);
330     }
331 
332     __HAL_I2C_ENABLE(hi2c);
333 
334     hi2c->State = HAL_I2C_STATE_READY;
335 
336     /* Process Unlocked */
337     __HAL_UNLOCK(hi2c);
338 
339     return HAL_OK;
340   }
341   else
342   {
343     return HAL_BUSY;
344   }
345 }
346 
347 /**
348   * @}
349   */
350 /**
351   * @}
352   */
353 
354 #endif /* HAL_I2C_MODULE_ENABLED */
355 /**
356   * @}
357   */
358 
359 /**
360   * @}
361   */
362