1 /**
2   ******************************************************************************
3   * @file    stm32wb0x_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   *           + FastModePlus Functions
10   *
11   ******************************************************************************
12   * @attention
13   *
14   * Copyright (c) 2024 STMicroelectronics.
15   * All rights reserved.
16   *
17   * This software is licensed under terms that can be found in the LICENSE file
18   * in the root directory of this software component.
19   * If no LICENSE file comes with this software, it is provided AS-IS.
20   *
21   ******************************************************************************
22   @verbatim
23   ==============================================================================
24                ##### I2C peripheral Extended features  #####
25   ==============================================================================
26 
27   [..] Comparing to other previous devices, the I2C interface for STM32WB0x
28        devices contains the following additional features
29 
30        (+) Possibility to disable or enable Analog Noise Filter
31        (+) Use of a configured Digital Noise Filter
32        (+) Disable or enable Fast Mode Plus
33 
34                      ##### How to use this driver #####
35   ==============================================================================
36   [..] This driver provides functions to:
37     (#) Configure I2C Analog noise filter using the function HAL_I2CEx_ConfigAnalogFilter()
38     (#) Configure I2C Digital noise filter using the function HAL_I2CEx_ConfigDigitalFilter()
39     (#) Configure the enable or disable of fast mode plus driving capability using the functions :
40           (++) HAL_I2CEx_EnableFastModePlus()
41           (++) HAL_I2CEx_DisableFastModePlus()
42   @endverbatim
43   */
44 
45 /* Includes ------------------------------------------------------------------*/
46 #include "stm32wb0x_hal.h"
47 
48 /** @addtogroup STM32WB0x_HAL_Driver
49   * @{
50   */
51 
52 /** @defgroup I2CEx I2CEx
53   * @brief I2C Extended HAL module driver
54   * @{
55   */
56 
57 #ifdef HAL_I2C_MODULE_ENABLED
58 
59 /* Private typedef -----------------------------------------------------------*/
60 /* Private define ------------------------------------------------------------*/
61 /* Private macro -------------------------------------------------------------*/
62 /* Private variables ---------------------------------------------------------*/
63 /* Private function prototypes -----------------------------------------------*/
64 /* Private functions ---------------------------------------------------------*/
65 
66 /** @defgroup I2CEx_Exported_Functions I2C Extended Exported Functions
67   * @{
68   */
69 
70 /** @defgroup I2CEx_Exported_Functions_Group1 Filter Mode Functions
71   * @brief    Filter Mode Functions
72   *
73 @verbatim
74  ===============================================================================
75                       ##### Filter Mode Functions #####
76  ===============================================================================
77     [..] This section provides functions allowing to:
78       (+) Configure Noise Filters
79 
80 @endverbatim
81   * @{
82   */
83 
84 /**
85   * @brief  Configure I2C Analog noise filter.
86   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
87   *                the configuration information for the specified I2Cx peripheral.
88   * @param  AnalogFilter New state of the Analog filter.
89   * @retval HAL status
90   */
HAL_I2CEx_ConfigAnalogFilter(I2C_HandleTypeDef * hi2c,uint32_t AnalogFilter)91 HAL_StatusTypeDef HAL_I2CEx_ConfigAnalogFilter(I2C_HandleTypeDef *hi2c, uint32_t AnalogFilter)
92 {
93   /* Check the parameters */
94   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
95   assert_param(IS_I2C_ANALOG_FILTER(AnalogFilter));
96 
97   if (hi2c->State == HAL_I2C_STATE_READY)
98   {
99     /* Process Locked */
100     __HAL_LOCK(hi2c);
101 
102     hi2c->State = HAL_I2C_STATE_BUSY;
103 
104     /* Disable the selected I2C peripheral */
105     __HAL_I2C_DISABLE(hi2c);
106 
107     /* Reset I2Cx ANOFF bit */
108     hi2c->Instance->CR1 &= ~(I2C_CR1_ANFOFF);
109 
110     /* Set analog filter bit*/
111     hi2c->Instance->CR1 |= AnalogFilter;
112 
113     __HAL_I2C_ENABLE(hi2c);
114 
115     hi2c->State = HAL_I2C_STATE_READY;
116 
117     /* Process Unlocked */
118     __HAL_UNLOCK(hi2c);
119 
120     return HAL_OK;
121   }
122   else
123   {
124     return HAL_BUSY;
125   }
126 }
127 
128 /**
129   * @brief  Configure I2C Digital noise filter.
130   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
131   *                the configuration information for the specified I2Cx peripheral.
132   * @param  DigitalFilter Coefficient of digital noise filter between Min_Data=0x00 and Max_Data=0x0F.
133   * @retval HAL status
134   */
HAL_I2CEx_ConfigDigitalFilter(I2C_HandleTypeDef * hi2c,uint32_t DigitalFilter)135 HAL_StatusTypeDef HAL_I2CEx_ConfigDigitalFilter(I2C_HandleTypeDef *hi2c, uint32_t DigitalFilter)
136 {
137   uint32_t tmpreg;
138 
139   /* Check the parameters */
140   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
141   assert_param(IS_I2C_DIGITAL_FILTER(DigitalFilter));
142 
143   if (hi2c->State == HAL_I2C_STATE_READY)
144   {
145     /* Process Locked */
146     __HAL_LOCK(hi2c);
147 
148     hi2c->State = HAL_I2C_STATE_BUSY;
149 
150     /* Disable the selected I2C peripheral */
151     __HAL_I2C_DISABLE(hi2c);
152 
153     /* Get the old register value */
154     tmpreg = hi2c->Instance->CR1;
155 
156     /* Reset I2Cx DNF bits [11:8] */
157     tmpreg &= ~(I2C_CR1_DNF);
158 
159     /* Set I2Cx DNF coefficient */
160     tmpreg |= DigitalFilter << 8U;
161 
162     /* Store the new register value */
163     hi2c->Instance->CR1 = tmpreg;
164 
165     __HAL_I2C_ENABLE(hi2c);
166 
167     hi2c->State = HAL_I2C_STATE_READY;
168 
169     /* Process Unlocked */
170     __HAL_UNLOCK(hi2c);
171 
172     return HAL_OK;
173   }
174   else
175   {
176     return HAL_BUSY;
177   }
178 }
179 /**
180   * @}
181   */
182 
183 /** @defgroup I2CEx_Exported_Functions_Group3 Fast Mode Plus Functions
184   * @brief    Fast Mode Plus Functions
185   *
186 @verbatim
187  ===============================================================================
188                       ##### Fast Mode Plus Functions #####
189  ===============================================================================
190     [..] This section provides functions allowing to:
191       (+) Configure Fast Mode Plus
192 
193 @endverbatim
194   * @{
195   */
196 
197 /**
198   * @brief Enable the I2C fast mode plus driving capability.
199   * @param ConfigFastModePlus Selects the pin.
200   *   This parameter can be one of the @ref I2CEx_FastModePlus values
201   * @retval None
202   */
HAL_I2CEx_EnableFastModePlus(uint32_t ConfigFastModePlus)203 void HAL_I2CEx_EnableFastModePlus(uint32_t ConfigFastModePlus)
204 {
205   /* Check the parameter */
206   assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
207 
208   /* Enable fast mode plus driving capability for selected pin */
209   SET_BIT(SYSCFG->I2C_FMP_CTRL, (uint32_t)ConfigFastModePlus);
210 }
211 
212 /**
213   * @brief Disable the I2C fast mode plus driving capability.
214   * @param ConfigFastModePlus Selects the pin.
215   *   This parameter can be one of the @ref I2CEx_FastModePlus values
216   * @retval None
217   */
HAL_I2CEx_DisableFastModePlus(uint32_t ConfigFastModePlus)218 void HAL_I2CEx_DisableFastModePlus(uint32_t ConfigFastModePlus)
219 {
220   /* Check the parameter */
221   assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
222 
223   /* Disable fast mode plus driving capability for selected pin */
224   CLEAR_BIT(SYSCFG->I2C_FMP_CTRL, (uint32_t)ConfigFastModePlus);
225 }
226 /**
227   * @}
228   */
229 /**
230   * @}
231   */
232 
233 #endif /* HAL_I2C_MODULE_ENABLED */
234 /**
235   * @}
236   */
237 
238 /**
239   * @}
240   */
241