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