xref: /btstack/port/stm32-l073rz-nucleo-em9304/Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_hal_i2c_ex.c (revision e838079242074edcbcbb400962776e15fe6ca6cb)
1 /**
2   ******************************************************************************
3   * @file    stm32l0xx_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   *           + Extended features functions
9   *
10   @verbatim
11   ==============================================================================
12                ##### I2C peripheral Extended features  #####
13   ==============================================================================
14 
15   [..] Comparing to other previous devices, the I2C interface for STM32L0xx
16        devices contains the following additional features
17 
18        (+) Possibility to disable or enable Analog Noise Filter
19        (+) Use of a configured Digital Noise Filter
20        (+) Disable or enable wakeup from Stop mode(s)
21        (+) Disable or enable Fast Mode Plus
22 
23                      ##### How to use this driver #####
24   ==============================================================================
25   [..] This driver provides functions to configure Noise Filter and Wake Up Feature
26     (#) Configure I2C Analog noise filter using the function HAL_I2CEx_ConfigAnalogFilter()
27     (#) Configure I2C Digital noise filter using the function HAL_I2CEx_ConfigDigitalFilter()
28     (#) Configure the enable or disable of I2C Wake Up Mode using the functions :
29           (++) HAL_I2CEx_EnableWakeUp()
30           (++) HAL_I2CEx_DisableWakeUp()
31     (#) Configure the enable or disable of fast mode plus driving capability using the functions :
32           (++) HAL_I2CEx_EnableFastModePlus()
33           (++) HAL_I2CEx_DisableFastModePlus()
34   @endverbatim
35   ******************************************************************************
36   * @attention
37   *
38   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
39   * All rights reserved.</center></h2>
40   *
41   * This software component is licensed by ST under BSD 3-Clause license,
42   * the "License"; You may not use this file except in compliance with the
43   * License. You may obtain a copy of the License at:
44   *                        opensource.org/licenses/BSD-3-Clause
45   *
46   ******************************************************************************
47   */
48 
49 /* Includes ------------------------------------------------------------------*/
50 #include "stm32l0xx_hal.h"
51 
52 /** @addtogroup STM32L0xx_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 Extended features functions
75   * @brief    Extended features functions
76  *
77 @verbatim
78  ===============================================================================
79                       ##### Extended features functions #####
80  ===============================================================================
81     [..] This section provides functions allowing to:
82       (+) Configure Noise Filters
83       (+) Configure Wake Up Feature
84       (+) Configure Fast Mode Plus
85 
86 @endverbatim
87   * @{
88   */
89 
90 /**
91   * @brief  Configure I2C Analog noise filter.
92   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
93   *                the configuration information for the specified I2Cx peripheral.
94   * @param  AnalogFilter New state of the Analog filter.
95   * @retval HAL status
96   */
HAL_I2CEx_ConfigAnalogFilter(I2C_HandleTypeDef * hi2c,uint32_t AnalogFilter)97 HAL_StatusTypeDef HAL_I2CEx_ConfigAnalogFilter(I2C_HandleTypeDef *hi2c, uint32_t AnalogFilter)
98 {
99   /* Check the parameters */
100   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
101   assert_param(IS_I2C_ANALOG_FILTER(AnalogFilter));
102 
103   if (hi2c->State == HAL_I2C_STATE_READY)
104   {
105     /* Process Locked */
106     __HAL_LOCK(hi2c);
107 
108     hi2c->State = HAL_I2C_STATE_BUSY;
109 
110     /* Disable the selected I2C peripheral */
111     __HAL_I2C_DISABLE(hi2c);
112 
113     /* Reset I2Cx ANOFF bit */
114     hi2c->Instance->CR1 &= ~(I2C_CR1_ANFOFF);
115 
116     /* Set analog filter bit*/
117     hi2c->Instance->CR1 |= AnalogFilter;
118 
119     __HAL_I2C_ENABLE(hi2c);
120 
121     hi2c->State = HAL_I2C_STATE_READY;
122 
123     /* Process Unlocked */
124     __HAL_UNLOCK(hi2c);
125 
126     return HAL_OK;
127   }
128   else
129   {
130     return HAL_BUSY;
131   }
132 }
133 
134 /**
135   * @brief  Configure I2C Digital noise filter.
136   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
137   *                the configuration information for the specified I2Cx peripheral.
138   * @param  DigitalFilter Coefficient of digital noise filter between Min_Data=0x00 and Max_Data=0x0F.
139   * @retval HAL status
140   */
HAL_I2CEx_ConfigDigitalFilter(I2C_HandleTypeDef * hi2c,uint32_t DigitalFilter)141 HAL_StatusTypeDef HAL_I2CEx_ConfigDigitalFilter(I2C_HandleTypeDef *hi2c, uint32_t DigitalFilter)
142 {
143   uint32_t tmpreg;
144 
145   /* Check the parameters */
146   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
147   assert_param(IS_I2C_DIGITAL_FILTER(DigitalFilter));
148 
149   if (hi2c->State == HAL_I2C_STATE_READY)
150   {
151     /* Process Locked */
152     __HAL_LOCK(hi2c);
153 
154     hi2c->State = HAL_I2C_STATE_BUSY;
155 
156     /* Disable the selected I2C peripheral */
157     __HAL_I2C_DISABLE(hi2c);
158 
159     /* Get the old register value */
160     tmpreg = hi2c->Instance->CR1;
161 
162     /* Reset I2Cx DNF bits [11:8] */
163     tmpreg &= ~(I2C_CR1_DNF);
164 
165     /* Set I2Cx DNF coefficient */
166     tmpreg |= DigitalFilter << 8U;
167 
168     /* Store the new register value */
169     hi2c->Instance->CR1 = tmpreg;
170 
171     __HAL_I2C_ENABLE(hi2c);
172 
173     hi2c->State = HAL_I2C_STATE_READY;
174 
175     /* Process Unlocked */
176     __HAL_UNLOCK(hi2c);
177 
178     return HAL_OK;
179   }
180   else
181   {
182     return HAL_BUSY;
183   }
184 }
185 
186 /**
187   * @brief  Enable I2C wakeup from Stop mode(s).
188   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
189   *                the configuration information for the specified I2Cx peripheral.
190   * @retval HAL status
191   */
HAL_I2CEx_EnableWakeUp(I2C_HandleTypeDef * hi2c)192 HAL_StatusTypeDef HAL_I2CEx_EnableWakeUp(I2C_HandleTypeDef *hi2c)
193 {
194   /* Check the parameters */
195   assert_param(IS_I2C_WAKEUP_FROMSTOP_INSTANCE(hi2c->Instance));
196 
197   if (hi2c->State == HAL_I2C_STATE_READY)
198   {
199     /* Process Locked */
200     __HAL_LOCK(hi2c);
201 
202     hi2c->State = HAL_I2C_STATE_BUSY;
203 
204     /* Disable the selected I2C peripheral */
205     __HAL_I2C_DISABLE(hi2c);
206 
207     /* Enable wakeup from stop mode */
208     hi2c->Instance->CR1 |= I2C_CR1_WUPEN;
209 
210     __HAL_I2C_ENABLE(hi2c);
211 
212     hi2c->State = HAL_I2C_STATE_READY;
213 
214     /* Process Unlocked */
215     __HAL_UNLOCK(hi2c);
216 
217     return HAL_OK;
218   }
219   else
220   {
221     return HAL_BUSY;
222   }
223 }
224 
225 /**
226   * @brief  Disable I2C wakeup from Stop mode(s).
227   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
228   *                the configuration information for the specified I2Cx peripheral.
229   * @retval HAL status
230   */
HAL_I2CEx_DisableWakeUp(I2C_HandleTypeDef * hi2c)231 HAL_StatusTypeDef HAL_I2CEx_DisableWakeUp(I2C_HandleTypeDef *hi2c)
232 {
233   /* Check the parameters */
234   assert_param(IS_I2C_WAKEUP_FROMSTOP_INSTANCE(hi2c->Instance));
235 
236   if (hi2c->State == HAL_I2C_STATE_READY)
237   {
238     /* Process Locked */
239     __HAL_LOCK(hi2c);
240 
241     hi2c->State = HAL_I2C_STATE_BUSY;
242 
243     /* Disable the selected I2C peripheral */
244     __HAL_I2C_DISABLE(hi2c);
245 
246     /* Enable wakeup from stop mode */
247     hi2c->Instance->CR1 &= ~(I2C_CR1_WUPEN);
248 
249     __HAL_I2C_ENABLE(hi2c);
250 
251     hi2c->State = HAL_I2C_STATE_READY;
252 
253     /* Process Unlocked */
254     __HAL_UNLOCK(hi2c);
255 
256     return HAL_OK;
257   }
258   else
259   {
260     return HAL_BUSY;
261   }
262 }
263 
264 #if  (defined(SYSCFG_CFGR2_I2C_PB6_FMP) || defined(SYSCFG_CFGR2_I2C_PB7_FMP)) || (defined(SYSCFG_CFGR2_I2C_PB8_FMP) || defined(SYSCFG_CFGR2_I2C_PB9_FMP)) || (defined(SYSCFG_CFGR2_I2C1_FMP)) || defined(SYSCFG_CFGR2_I2C2_FMP) || defined(SYSCFG_CFGR2_I2C3_FMP)
265 /**
266   * @brief Enable the I2C fast mode plus driving capability.
267   * @param ConfigFastModePlus Selects the pin.
268   *   This parameter can be one of the @ref I2CEx_FastModePlus values
269   * @note  For I2C1, fast mode plus driving capability can be enabled on all selected
270   *        I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
271   *        on each one of the following pins PB6, PB7, PB8 and PB9.
272   * @note  For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
273   *        can be enabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
274   * @note  For all I2C2 pins fast mode plus driving capability can be enabled
275   *        only by using I2C_FASTMODEPLUS_I2C2 parameter.
276   * @note  For all I2C3 pins fast mode plus driving capability can be enabled
277   *        only by using I2C_FASTMODEPLUS_I2C3 parameter.
278   * @retval None
279   */
HAL_I2CEx_EnableFastModePlus(uint32_t ConfigFastModePlus)280 void HAL_I2CEx_EnableFastModePlus(uint32_t ConfigFastModePlus)
281 {
282   /* Check the parameter */
283   assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
284 
285   /* Enable SYSCFG clock */
286   __HAL_RCC_SYSCFG_CLK_ENABLE();
287 
288   /* Enable fast mode plus driving capability for selected pin */
289   SET_BIT(SYSCFG->CFGR2, (uint32_t)ConfigFastModePlus);
290 }
291 
292 /**
293   * @brief Disable the I2C fast mode plus driving capability.
294   * @param ConfigFastModePlus Selects the pin.
295   *   This parameter can be one of the @ref I2CEx_FastModePlus values
296   * @note  For I2C1, fast mode plus driving capability can be disabled on all selected
297   *        I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
298   *        on each one of the following pins PB6, PB7, PB8 and PB9.
299   * @note  For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
300   *        can be disabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
301   * @note  For all I2C2 pins fast mode plus driving capability can be disabled
302   *        only by using I2C_FASTMODEPLUS_I2C2 parameter.
303   * @note  For all I2C3 pins fast mode plus driving capability can be disabled
304   *        only by using I2C_FASTMODEPLUS_I2C3 parameter.
305   * @retval None
306   */
HAL_I2CEx_DisableFastModePlus(uint32_t ConfigFastModePlus)307 void HAL_I2CEx_DisableFastModePlus(uint32_t ConfigFastModePlus)
308 {
309   /* Check the parameter */
310   assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
311 
312   /* Enable SYSCFG clock */
313   __HAL_RCC_SYSCFG_CLK_ENABLE();
314 
315   /* Disable fast mode plus driving capability for selected pin */
316   CLEAR_BIT(SYSCFG->CFGR2, (uint32_t)ConfigFastModePlus);
317 }
318 
319 #endif
320 /**
321   * @}
322   */
323 
324 /**
325   * @}
326   */
327 
328 #endif /* HAL_I2C_MODULE_ENABLED */
329 /**
330   * @}
331   */
332 
333 /**
334   * @}
335   */
336 
337 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
338