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