1 /**
2   ******************************************************************************
3   * @file    stm32h5xx_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) 2022 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 STM32H5xx
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 "stm32h5xx_hal.h"
51 
52 /** @addtogroup STM32H5xx_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 
187 /** @defgroup I2CEx_Exported_Functions_Group2 WakeUp Mode Functions
188   * @brief    WakeUp Mode Functions
189   *
190 @verbatim
191  ===============================================================================
192                       ##### WakeUp Mode Functions #####
193  ===============================================================================
194     [..] This section provides functions allowing to:
195       (+) Configure Wake Up Feature
196 
197 @endverbatim
198   * @{
199   */
200 
201 /**
202   * @brief  Enable I2C wakeup from Stop mode(s).
203   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
204   *                the configuration information for the specified I2Cx peripheral.
205   * @retval HAL status
206   */
HAL_I2CEx_EnableWakeUp(I2C_HandleTypeDef * hi2c)207 HAL_StatusTypeDef HAL_I2CEx_EnableWakeUp(I2C_HandleTypeDef *hi2c)
208 {
209   /* Check the parameters */
210   assert_param(IS_I2C_WAKEUP_FROMSTOP_INSTANCE(hi2c->Instance));
211 
212   if (hi2c->State == HAL_I2C_STATE_READY)
213   {
214     /* Process Locked */
215     __HAL_LOCK(hi2c);
216 
217     hi2c->State = HAL_I2C_STATE_BUSY;
218 
219     /* Disable the selected I2C peripheral */
220     __HAL_I2C_DISABLE(hi2c);
221 
222     /* Enable wakeup from stop mode */
223     hi2c->Instance->CR1 |= I2C_CR1_WUPEN;
224 
225     __HAL_I2C_ENABLE(hi2c);
226 
227     hi2c->State = HAL_I2C_STATE_READY;
228 
229     /* Process Unlocked */
230     __HAL_UNLOCK(hi2c);
231 
232     return HAL_OK;
233   }
234   else
235   {
236     return HAL_BUSY;
237   }
238 }
239 
240 /**
241   * @brief  Disable I2C wakeup from Stop mode(s).
242   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
243   *                the configuration information for the specified I2Cx peripheral.
244   * @retval HAL status
245   */
HAL_I2CEx_DisableWakeUp(I2C_HandleTypeDef * hi2c)246 HAL_StatusTypeDef HAL_I2CEx_DisableWakeUp(I2C_HandleTypeDef *hi2c)
247 {
248   /* Check the parameters */
249   assert_param(IS_I2C_WAKEUP_FROMSTOP_INSTANCE(hi2c->Instance));
250 
251   if (hi2c->State == HAL_I2C_STATE_READY)
252   {
253     /* Process Locked */
254     __HAL_LOCK(hi2c);
255 
256     hi2c->State = HAL_I2C_STATE_BUSY;
257 
258     /* Disable the selected I2C peripheral */
259     __HAL_I2C_DISABLE(hi2c);
260 
261     /* Enable wakeup from stop mode */
262     hi2c->Instance->CR1 &= ~(I2C_CR1_WUPEN);
263 
264     __HAL_I2C_ENABLE(hi2c);
265 
266     hi2c->State = HAL_I2C_STATE_READY;
267 
268     /* Process Unlocked */
269     __HAL_UNLOCK(hi2c);
270 
271     return HAL_OK;
272   }
273   else
274   {
275     return HAL_BUSY;
276   }
277 }
278 /**
279   * @}
280   */
281 
282 /** @defgroup I2CEx_Exported_Functions_Group3 Fast Mode Plus Functions
283   * @brief    Fast Mode Plus Functions
284   *
285 @verbatim
286  ===============================================================================
287                       ##### Fast Mode Plus Functions #####
288  ===============================================================================
289     [..] This section provides functions allowing to:
290       (+) Configure Fast Mode Plus
291 
292 @endverbatim
293   * @{
294   */
295 
296 /**
297   * @brief  Configure I2C Fast Mode Plus.
298   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
299   *                the configuration information for the specified I2Cx peripheral.
300   * @param  FastModePlus New state of the Fast Mode Plus.
301   * @retval HAL status
302   */
HAL_I2CEx_ConfigFastModePlus(I2C_HandleTypeDef * hi2c,uint32_t FastModePlus)303 HAL_StatusTypeDef HAL_I2CEx_ConfigFastModePlus(I2C_HandleTypeDef *hi2c, uint32_t FastModePlus)
304 {
305   /* Check the parameters */
306   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
307   assert_param(IS_I2C_FASTMODEPLUS(FastModePlus));
308 
309   if (hi2c->State == HAL_I2C_STATE_READY)
310   {
311     /* Process Locked */
312     __HAL_LOCK(hi2c);
313 
314     hi2c->State = HAL_I2C_STATE_BUSY;
315 
316     /* Disable the selected I2C peripheral */
317     __HAL_I2C_DISABLE(hi2c);
318 
319     if (FastModePlus == I2C_FASTMODEPLUS_ENABLE)
320     {
321       /* Set I2Cx FMP bit */
322       hi2c->Instance->CR1 |= (I2C_CR1_FMP);
323     }
324     else
325     {
326       /* Reset I2Cx FMP bit */
327       hi2c->Instance->CR1 &= ~(I2C_CR1_FMP);
328     }
329 
330     __HAL_I2C_ENABLE(hi2c);
331 
332     hi2c->State = HAL_I2C_STATE_READY;
333 
334     /* Process Unlocked */
335     __HAL_UNLOCK(hi2c);
336 
337     return HAL_OK;
338   }
339   else
340   {
341     return HAL_BUSY;
342   }
343 }
344 
345 /**
346   * @}
347   */
348 /**
349   * @}
350   */
351 
352 #endif /* HAL_I2C_MODULE_ENABLED */
353 /**
354   * @}
355   */
356 
357 /**
358   * @}
359   */
360