]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32L0/stm32l0xx_hal_i2c_ex.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32L0 / stm32l0xx_hal_i2c_ex.c
1 /**
2 ******************************************************************************
3 * @file stm32l0xx_hal_i2c_ex.c
4 * @author MCD Application Team
5 * @version V1.2.0
6 * @date 06-February-2015
7 * @brief I2C Extended HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of I2C Extended peripheral:
10 * + Extended features functions
11 *
12 @verbatim
13 ==============================================================================
14 ##### I2C peripheral Extended features #####
15 ==============================================================================
16
17 [..] Comparing to other previous devices, the I2C interface for STM32L0XX
18 devices contains the following additional features
19
20 (+) Possibility to disable or enable Analog Noise Filter
21 (+) Use of a configured Digital Noise Filter
22 (+) Disable or enable wakeup from Stop mode
23
24 ##### How to use this driver #####
25 ==============================================================================
26 [..] This driver provides functions to configure Noise Filter
27 (#) Configure I2C Analog noise filter using the function HAL_I2CEx_ConfigAnalogFilter()
28 (#) Configure I2C Digital noise filter using the function HAL_I2CEx_ConfigDigitalFilter()
29 (#) Configure the enable or disable of I2C Wake Up Mode using the functions :
30 + HAL_I2CEx_EnableWakeUp()
31 + HAL_I2CEx_DisableWakeUp()
32 (#) Configure the enable or disable of fast mode plus driving capability using the functions :
33 + HAL_I2CEx_EnableFastModePlus()
34 + HAL_I2CEx_DisbleFastModePlus()
35 @endverbatim
36 ******************************************************************************
37 * @attention
38 *
39 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
40 *
41 * Redistribution and use in source and binary forms, with or without modification,
42 * are permitted provided that the following conditions are met:
43 * 1. Redistributions of source code must retain the above copyright notice,
44 * this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright notice,
46 * this list of conditions and the following disclaimer in the documentation
47 * and/or other materials provided with the distribution.
48 * 3. Neither the name of STMicroelectronics nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
53 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
55 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
58 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
59 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
60 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 *
63 ******************************************************************************
64 */
65
66 /* Includes ------------------------------------------------------------------*/
67 #include "stm32l0xx_hal.h"
68
69 /** @addtogroup STM32L0xx_HAL_Driver
70 * @{
71 */
72
73 /** @defgroup I2CEx I2C Extended HAL module driver
74 * @brief I2C Extended HAL module driver
75 * @{
76 */
77
78 #ifdef HAL_I2C_MODULE_ENABLED
79
80 /* Private typedef -----------------------------------------------------------*/
81 /* Private define ------------------------------------------------------------*/
82 /* Private macro -------------------------------------------------------------*/
83 /* Private variables ---------------------------------------------------------*/
84 /* Private function prototypes -----------------------------------------------*/
85 /* Private functions ---------------------------------------------------------*/
86
87 /** @defgroup I2CEx_Exported_Functions I2C Extended Exported Functions
88 * @{
89 */
90
91 /** @defgroup I2CEx_Exported_Functions_Group1 Extended features functions
92 * @brief Extended features functions
93 *
94 @verbatim
95 ===============================================================================
96 ##### Extended features functions #####
97 ===============================================================================
98 [..] This section provides functions allowing to:
99 (+) Configure Noise Filters
100
101 @endverbatim
102 * @{
103 */
104
105 /**
106 * @brief Configures I2C Analog noise filter.
107 * @param hi2c : pointer to a I2C_HandleTypeDef structure that contains
108 * the configuration information for the specified I2Cx peripheral.
109 * @param AnalogFilter : new state of the Analog filter.
110 * @retval HAL status
111 */
112 HAL_StatusTypeDef HAL_I2CEx_ConfigAnalogFilter(I2C_HandleTypeDef *hi2c, uint32_t AnalogFilter)
113 {
114 /* Check the parameters */
115 assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
116 assert_param(IS_I2C_ANALOG_FILTER(AnalogFilter));
117
118 if((hi2c->State == HAL_I2C_STATE_BUSY) || (hi2c->State == HAL_I2C_STATE_MASTER_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_MASTER_BUSY_RX)
119 || (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_RX))
120 {
121 return HAL_BUSY;
122 }
123
124 /* Process Locked */
125 __HAL_LOCK(hi2c);
126
127 hi2c->State = HAL_I2C_STATE_BUSY;
128
129 /* Disable the selected I2C peripheral */
130 __HAL_I2C_DISABLE(hi2c);
131
132 /* Reset I2Cx ANOFF bit */
133 hi2c->Instance->CR1 &= ~(I2C_CR1_ANFOFF);
134
135 /* Set analog filter bit*/
136 hi2c->Instance->CR1 |= AnalogFilter;
137
138 __HAL_I2C_ENABLE(hi2c);
139
140 hi2c->State = HAL_I2C_STATE_READY;
141
142 /* Process Unlocked */
143 __HAL_UNLOCK(hi2c);
144
145 return HAL_OK;
146 }
147
148 /**
149 * @brief Configures I2C Digital noise filter.
150 * @param hi2c : pointer to a I2C_HandleTypeDef structure that contains
151 * the configuration information for the specified I2Cx peripheral.
152 * @param DigitalFilter : Coefficient of digital noise filter between 0x00 and 0x0F.
153 * @retval HAL status
154 */
155 HAL_StatusTypeDef HAL_I2CEx_ConfigDigitalFilter(I2C_HandleTypeDef *hi2c, uint32_t DigitalFilter)
156 {
157 uint32_t tmpreg = 0;
158
159 /* Check the parameters */
160 assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
161 assert_param(IS_I2C_DIGITAL_FILTER(DigitalFilter));
162
163 if((hi2c->State == HAL_I2C_STATE_BUSY) || (hi2c->State == HAL_I2C_STATE_MASTER_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_MASTER_BUSY_RX)
164 || (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_RX))
165 {
166 return HAL_BUSY;
167 }
168
169 /* Process Locked */
170 __HAL_LOCK(hi2c);
171
172 hi2c->State = HAL_I2C_STATE_BUSY;
173
174 /* Disable the selected I2C peripheral */
175 __HAL_I2C_DISABLE(hi2c);
176
177 /* Get the old register value */
178 tmpreg = hi2c->Instance->CR1;
179
180 /* Reset I2Cx DNF bits [11:8] */
181 tmpreg &= ~(I2C_CR1_DFN);
182
183 /* Set I2Cx DNF coefficient */
184 tmpreg |= DigitalFilter << 8;
185
186 /* Store the new register value */
187 hi2c->Instance->CR1 = tmpreg;
188
189 __HAL_I2C_ENABLE(hi2c);
190
191 hi2c->State = HAL_I2C_STATE_READY;
192
193 /* Process Unlocked */
194 __HAL_UNLOCK(hi2c);
195
196 return HAL_OK;
197 }
198
199 /**
200 * @brief Enables I2C wakeup from stop mode.
201 * @param hi2c : pointer to a I2C_HandleTypeDef structure that contains
202 * the configuration information for the specified I2Cx peripheral.
203 * @retval HAL status
204 */
205 HAL_StatusTypeDef HAL_I2CEx_EnableWakeUp (I2C_HandleTypeDef *hi2c)
206 {
207 /* Check the parameters */
208 assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
209
210 if((hi2c->State == HAL_I2C_STATE_BUSY) || (hi2c->State == HAL_I2C_STATE_MASTER_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_MASTER_BUSY_RX)
211 || (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_RX))
212 {
213 return HAL_BUSY;
214 }
215
216 /* Process Locked */
217 __HAL_LOCK(hi2c);
218
219 hi2c->State = HAL_I2C_STATE_BUSY;
220
221 /* Disable the selected I2C peripheral */
222 __HAL_I2C_DISABLE(hi2c);
223
224 /* Enable wakeup from stop mode */
225 hi2c->Instance->CR1 |= I2C_CR1_WUPEN;
226
227 __HAL_I2C_ENABLE(hi2c);
228
229 hi2c->State = HAL_I2C_STATE_READY;
230
231 /* Process Unlocked */
232 __HAL_UNLOCK(hi2c);
233
234 return HAL_OK;
235 }
236
237
238 /**
239 * @brief Disables I2C wakeup from stop mode.
240 * @param hi2c : pointer to a I2C_HandleTypeDef structure that contains
241 * the configuration information for the specified I2Cx peripheral.
242 * @retval HAL status
243 */
244 HAL_StatusTypeDef HAL_I2CEx_DisableWakeUp (I2C_HandleTypeDef *hi2c)
245 {
246 /* Check the parameters */
247 assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
248
249 if((hi2c->State == HAL_I2C_STATE_BUSY) || (hi2c->State == HAL_I2C_STATE_MASTER_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_MASTER_BUSY_RX)
250 || (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_RX))
251 {
252 return HAL_BUSY;
253 }
254
255 /* Process Locked */
256 __HAL_LOCK(hi2c);
257
258 hi2c->State = HAL_I2C_STATE_BUSY;
259
260 /* Disable the selected I2C peripheral */
261 __HAL_I2C_DISABLE(hi2c);
262
263 /* Enable wakeup from stop mode */
264 hi2c->Instance->CR1 &= ~(I2C_CR1_WUPEN);
265
266 __HAL_I2C_ENABLE(hi2c);
267
268 hi2c->State = HAL_I2C_STATE_READY;
269
270 /* Process Unlocked */
271 __HAL_UNLOCK(hi2c);
272
273 return HAL_OK;
274 }
275
276 /**
277 * @brief Enable the I2C fast mode plus driving capability.
278 * @param ConfigFastModePlus: selects the pin.
279 * This parameter can be one of the @ref I2CEx_FastModePlus values
280 * @note For I2C1, fast mode plus driving capability can be enabled on all selected
281 * I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
282 * on each one of the following pins PB6, PB7, PB8 and PB9.
283 * @note For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
284 * can be enabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
285 * @note For all I2C2 pins fast mode plus driving capability can be enabled
286 * only by using I2C_FASTMODEPLUS_I2C2 parameter.
287 * @note For all I2C3 pins fast mode plus driving capability can be enabled
288 * only by using I2C_FASTMODEPLUS_I2C3 parameter.
289 * @retval None
290 */
291 void HAL_I2CEx_EnableFastModePlus(uint32_t ConfigFastModePlus)
292 {
293 /* Check the parameter */
294 assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
295
296 /* Enable SYSCFG clock */
297 __HAL_RCC_SYSCFG_CLK_ENABLE();
298
299 /* Enable fast mode plus driving capability for selected pin */
300 SET_BIT(SYSCFG->CFGR1, (uint32_t)ConfigFastModePlus);
301 }
302
303 /**
304 * @brief Disable the I2C fast mode plus driving capability.
305 * @param ConfigFastModePlus: selects the pin.
306 * This parameter can be one of the @ref I2CEx_FastModePlus values
307 * @note For I2C1, fast mode plus driving capability can be disabled on all selected
308 * I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
309 * on each one of the following pins PB6, PB7, PB8 and PB9.
310 * @note For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
311 * can be disabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
312 * @note For all I2C2 pins fast mode plus driving capability can be disabled
313 * only by using I2C_FASTMODEPLUS_I2C2 parameter.
314 * @note For all I2C3 pins fast mode plus driving capability can be disabled
315 * only by using I2C_FASTMODEPLUS_I2C3 parameter.
316 * @retval None
317 */
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->CFGR1, (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
Imprint / Impressum