• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2   ******************************************************************************
3   * @file    stm32mp1xx_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 STM32MP1xx
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) 2019 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 "stm32mp1xx_hal.h"
51 
52 /** @addtogroup STM32MP1xx_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 /**
265   * @brief Enable the I2C fast mode plus driving capability.
266   * @param ConfigFastModePlus Selects the pin.
267   *   This parameter can be one of the @ref I2CEx_FastModePlus values
268   * @note  For I2C1, fast mode plus driving capability can be enabled on all selected
269   *        I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
270   *        on each one of the following pins PB6, PB7, PB8 and PB9.
271   * @note  For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
272   *        can be enabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
273   * @note  For all I2C2 pins fast mode plus driving capability can be enabled
274   *        only by using I2C_FASTMODEPLUS_I2C2 parameter.
275   * @note  For all I2C3 pins fast mode plus driving capability can be enabled
276   *        only by using I2C_FASTMODEPLUS_I2C3 parameter.
277   * @note  For all I2C4 pins fast mode plus driving capability can be enabled
278   *        only by using I2C_FASTMODEPLUS_I2C4 parameter.
279   * @note  For all I2C5 pins fast mode plus driving capability can be enabled
280   *        only by using I2C_FASTMODEPLUS_I2C5 parameter.
281   * @note  For all I2C6 pins fast mode plus driving capability can be enabled
282   *        only by using I2C_FASTMODEPLUS_I2C6 parameter.
283   * @retval None
284   */
HAL_I2CEx_EnableFastModePlus(uint32_t ConfigFastModePlus)285 void HAL_I2CEx_EnableFastModePlus(uint32_t ConfigFastModePlus)
286 {
287   /* Check the parameter */
288   assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
289 
290   /* Enable SYSCFG clock */
291   __HAL_RCC_SYSCFG_CLK_ENABLE();
292 
293   /* Enable fast mode plus driving capability for selected pin */
294   SET_BIT(SYSCFG->PMCSETR, (uint32_t)ConfigFastModePlus);
295 }
296 
297 /**
298   * @brief Disable the I2C fast mode plus driving capability.
299   * @param ConfigFastModePlus Selects the pin.
300   *   This parameter can be one of the @ref I2CEx_FastModePlus values
301   * @note  For I2C1, fast mode plus driving capability can be disabled on all selected
302   *        I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
303   *        on each one of the following pins PB6, PB7, PB8 and PB9.
304   * @note  For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
305   *        can be disabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
306   * @note  For all I2C2 pins fast mode plus driving capability can be disabled
307   *        only by using I2C_FASTMODEPLUS_I2C2 parameter.
308   * @note  For all I2C3 pins fast mode plus driving capability can be disabled
309   *        only by using I2C_FASTMODEPLUS_I2C3 parameter.
310   * @note  For all I2C4 pins fast mode plus driving capability can be disabled
311   *        only by using I2C_FASTMODEPLUS_I2C4 parameter.
312   * @note  For all I2C5 pins fast mode plus driving capability can be disabled
313   *        only by using I2C_FASTMODEPLUS_I2C5 parameter.
314   * @note  For all I2C6 pins fast mode plus driving capability can be disabled
315   *        only by using I2C_FASTMODEPLUS_I2C6 parameter.
316   * @retval None
317   */
HAL_I2CEx_DisableFastModePlus(uint32_t ConfigFastModePlus)318 void HAL_I2CEx_DisableFastModePlus(uint32_t ConfigFastModePlus)
319 {
320   /* Check the parameter */
321   assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
322 
323   /* Enable SYSCFG clock */
324   __HAL_RCC_SYSCFG_CLK_ENABLE();
325 
326   /* Disable fast mode plus driving capability for selected pin */
327   CLEAR_BIT(SYSCFG->PMCCLRR, (uint32_t)ConfigFastModePlus);
328 }
329 
330 /**
331   * @}
332   */
333 
334 /**
335   * @}
336   */
337 
338 #endif /* HAL_I2C_MODULE_ENABLED */
339 /**
340   * @}
341   */
342 
343 /**
344   * @}
345   */
346 
347 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
348