]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F0/stm32f0xx_hal_tsc.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_tsc.c
1 /**
2   ******************************************************************************
3   * @file    stm32f0xx_hal_tsc.c
4   * @author  MCD Application Team
5   * @version V1.2.0
6   * @date    11-December-2014
7   * @brief   This file provides firmware functions to manage the following 
8   *          functionalities of the Touch Sensing Controller (TSC) peripheral:
9   *           + Initialization and DeInitialization
10   *           + Channel IOs, Shield IOs and Sampling IOs configuration
11   *           + Start and Stop an acquisition
12   *           + Read acquisition result
13   *           + Interrupts and flags management
14   *         
15   @verbatim
16 ================================================================================
17                        ##### TSC specific features #####
18 ================================================================================
19   [..]
20   (#) Proven and robust surface charge transfer acquisition principle
21     
22   (#) Supports up to 3 capacitive sensing channels per group
23     
24   (#) Capacitive sensing channels can be acquired in parallel offering a very good
25       response time
26       
27   (#) Spread spectrum feature to improve system robustness in noisy environments
28    
29   (#) Full hardware management of the charge transfer acquisition sequence
30    
31   (#) Programmable charge transfer frequency
32    
33   (#) Programmable sampling capacitor I/O pin
34    
35   (#) Programmable channel I/O pin
36    
37   (#) Programmable max count value to avoid long acquisition when a channel is faulty
38    
39   (#) Dedicated end of acquisition and max count error flags with interrupt capability
40    
41   (#) One sampling capacitor for up to 3 capacitive sensing channels to reduce the system
42       components
43    
44   (#) Compatible with proximity, touchkey, linear and rotary touch sensor implementation
45
46    
47                           ##### How to use this driver #####
48 ================================================================================
49   [..]
50     (#) Enable the TSC interface clock using __TSC_CLK_ENABLE() macro.
51
52     (#) GPIO pins configuration
53       (++) Enable the clock for the TSC GPIOs using __GPIOx_CLK_ENABLE() macro.
54       (++) Configure the TSC pins used as sampling IOs in alternate function output Open-Drain mode,
55            and TSC pins used as channel/shield IOs in alternate function output Push-Pull mode
56            using HAL_GPIO_Init() function.
57       (++) Configure the alternate function on all the TSC pins using HAL_xxxx() function.
58
59     (#) Interrupts configuration
60       (++) Configure the NVIC (if the interrupt model is used) using HAL_xxx() function.
61
62     (#) TSC configuration
63       (++) Configure all TSC parameters and used TSC IOs using HAL_TSC_Init() function.
64
65   *** Acquisition sequence ***
66   ===================================
67   [..]
68     (+) Discharge all IOs using HAL_TSC_IODischarge() function.
69     (+) Wait a certain time allowing a good discharge of all capacitors. This delay depends
70         of the sampling capacitor and electrodes design.
71     (+) Select the channel IOs to be acquired using HAL_TSC_IOConfig() function.
72     (+) Launch the acquisition using either HAL_TSC_Start() or HAL_TSC_Start_IT() function.
73         If the synchronized mode is selected, the acquisition will start as soon as the signal
74         is received on the synchro pin.
75     (+) Wait the end of acquisition using either HAL_TSC_PollForAcquisition() or
76         HAL_TSC_GetState() function or using WFI instruction for example.
77     (+) Check the group acquisition status using HAL_TSC_GroupGetStatus() function.
78     (+) Read the acquisition value using HAL_TSC_GroupGetValue() function.
79       
80   @endverbatim
81   ******************************************************************************
82   * @attention
83   *
84   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
85   *
86   * Redistribution and use in source and binary forms, with or without modification,
87   * are permitted provided that the following conditions are met:
88   *   1. Redistributions of source code must retain the above copyright notice,
89   *      this list of conditions and the following disclaimer.
90   *   2. Redistributions in binary form must reproduce the above copyright notice,
91   *      this list of conditions and the following disclaimer in the documentation
92   *      and/or other materials provided with the distribution.
93   *   3. Neither the name of STMicroelectronics nor the names of its contributors
94   *      may be used to endorse or promote products derived from this software
95   *      without specific prior written permission.
96   *
97   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
98   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
99   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
100   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
101   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
102   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
103   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
104   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
105   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
106   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
107   *
108   ******************************************************************************
109   */
110
111 /* Includes ------------------------------------------------------------------*/
112 #include "stm32f0xx_hal.h"
113
114 #ifdef HAL_TSC_MODULE_ENABLED
115
116 #if defined(STM32F051x8) || defined(STM32F071xB) || defined(STM32F091xC) || \
117     defined(STM32F042x6) || defined(STM32F072xB) ||                         \
118     defined(STM32F048xx) || defined(STM32F058xx) || defined(STM32F078xx) || defined(STM32F098xx)
119
120 /** @addtogroup STM32F0xx_HAL_Driver
121   * @{
122   */
123
124 /** @defgroup TSC TSC HAL module driver
125   * @brief TSC HAL module driver
126   * @{
127   */
128
129 /* Private typedef -----------------------------------------------------------*/
130 /* Private define ------------------------------------------------------------*/
131 /* Private macro -------------------------------------------------------------*/
132 /* Private variables ---------------------------------------------------------*/
133 /* Private function prototypes -----------------------------------------------*/
134 static uint32_t TSC_extract_groups(uint32_t iomask);
135 /* Exported functions ---------------------------------------------------------*/
136
137 /** @defgroup TSC_Exported_Functions TSC Exported Functions
138   * @{
139   */ 
140
141 /** @defgroup TSC_Exported_Functions_Group1 Initialization/de-initialization functions 
142  *  @brief    Initialization and Configuration functions 
143  *
144 @verbatim    
145  ===============================================================================
146               ##### Initialization and de-initialization functions #####
147  ===============================================================================
148     [..]  This section provides functions allowing to:
149       (+) Initialize and configure the TSC.
150       (+) De-initialize the TSC.
151 @endverbatim
152   * @{
153   */
154
155 /**
156   * @brief  Initializes the TSC peripheral according to the specified parameters 
157   *         in the TSC_InitTypeDef structure.           
158   * @param  htsc: TSC handle
159   * @retval HAL status
160   */
161 HAL_StatusTypeDef HAL_TSC_Init(TSC_HandleTypeDef* htsc)
162 {
163   /* Check TSC handle allocation */
164   if (htsc == NULL)
165   {
166     return HAL_ERROR;
167   }
168
169   /* Check the parameters */
170   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
171   assert_param(IS_TSC_CTPH(htsc->Init.CTPulseHighLength));
172   assert_param(IS_TSC_CTPL(htsc->Init.CTPulseLowLength));
173   assert_param(IS_TSC_SS(htsc->Init.SpreadSpectrum));
174   assert_param(IS_TSC_SSD(htsc->Init.SpreadSpectrumDeviation));
175   assert_param(IS_TSC_SS_PRESC(htsc->Init.SpreadSpectrumPrescaler));
176   assert_param(IS_TSC_PG_PRESC(htsc->Init.PulseGeneratorPrescaler));
177   assert_param(IS_TSC_MCV(htsc->Init.MaxCountValue));
178   assert_param(IS_TSC_IODEF(htsc->Init.IODefaultMode));
179   assert_param(IS_TSC_SYNC_POL(htsc->Init.SynchroPinPolarity));
180   assert_param(IS_TSC_ACQ_MODE(htsc->Init.AcquisitionMode));
181   assert_param(IS_TSC_MCE_IT(htsc->Init.MaxCountInterrupt));
182     
183   /* Initialize the TSC state */
184   htsc->State = HAL_TSC_STATE_BUSY;
185
186   /* Init the low level hardware : GPIO, CLOCK, CORTEX */
187   HAL_TSC_MspInit(htsc);
188
189   /*--------------------------------------------------------------------------*/  
190   /* Set TSC parameters */
191
192   /* Enable TSC */
193   htsc->Instance->CR = TSC_CR_TSCE;
194   
195   /* Set all functions */
196   htsc->Instance->CR |= (htsc->Init.CTPulseHighLength |
197                          htsc->Init.CTPulseLowLength |
198                          (uint32_t)(htsc->Init.SpreadSpectrumDeviation << 17) |
199                          htsc->Init.SpreadSpectrumPrescaler |
200                          htsc->Init.PulseGeneratorPrescaler |
201                          htsc->Init.MaxCountValue |
202                          htsc->Init.IODefaultMode |
203                          htsc->Init.SynchroPinPolarity |
204                          htsc->Init.AcquisitionMode);
205
206   /* Spread spectrum */
207   if (htsc->Init.SpreadSpectrum == ENABLE)
208   {
209     htsc->Instance->CR |= TSC_CR_SSE;
210   }
211   
212   /* Disable Schmitt trigger hysteresis on all used TSC IOs */
213   htsc->Instance->IOHCR = (uint32_t)(~(htsc->Init.ChannelIOs | htsc->Init.ShieldIOs | htsc->Init.SamplingIOs));
214
215   /* Set channel and shield IOs */
216   htsc->Instance->IOCCR = (htsc->Init.ChannelIOs | htsc->Init.ShieldIOs);
217   
218   /* Set sampling IOs */
219   htsc->Instance->IOSCR = htsc->Init.SamplingIOs;
220   
221   /* Set the groups to be acquired */
222   htsc->Instance->IOGCSR = TSC_extract_groups(htsc->Init.ChannelIOs);
223   
224   /* Clear interrupts */
225   htsc->Instance->IER &= (uint32_t)(~(TSC_IT_EOA | TSC_IT_MCE));
226   
227   /* Clear flags */
228   htsc->Instance->ICR = (TSC_FLAG_EOA | TSC_FLAG_MCE);
229
230   /*--------------------------------------------------------------------------*/
231   
232   /* Initialize the TSC state */
233   htsc->State = HAL_TSC_STATE_READY;
234   
235   /* Return function status */
236   return HAL_OK;
237 }
238
239 /**
240   * @brief  Deinitializes the TSC peripheral registers to their default reset values.
241   * @param  htsc: TSC handle  
242   * @retval HAL status
243   */
244 HAL_StatusTypeDef HAL_TSC_DeInit(TSC_HandleTypeDef* htsc)
245 {
246   /* Check TSC handle allocation */
247   if (htsc == NULL)
248   {
249     return HAL_ERROR;
250   }
251
252   /* Check the parameters */
253   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
254    
255   /* Change TSC state */
256   htsc->State = HAL_TSC_STATE_BUSY;
257  
258   /* DeInit the low level hardware */
259   HAL_TSC_MspDeInit(htsc);
260   
261   /* Change TSC state */
262   htsc->State = HAL_TSC_STATE_RESET;
263
264   /* Process unlocked */
265   __HAL_UNLOCK(htsc);
266
267   /* Return function status */
268   return HAL_OK;
269 }
270
271 /**
272   * @brief  Initializes the TSC MSP.
273   * @param  htsc: pointer to a TSC_HandleTypeDef structure that contains
274   *         the configuration information for the specified TSC.  
275   * @retval None
276   */
277 __weak void HAL_TSC_MspInit(TSC_HandleTypeDef* htsc)
278 {
279   /* NOTE : This function should not be modified, when the callback is needed,
280             the HAL_TSC_MspInit could be implemented in the user file.
281    */ 
282 }
283
284 /**
285   * @brief  DeInitializes the TSC MSP.
286   * @param  htsc: pointer to a TSC_HandleTypeDef structure that contains
287   *         the configuration information for the specified TSC.  
288   * @retval None
289   */
290 __weak void HAL_TSC_MspDeInit(TSC_HandleTypeDef* htsc)
291 {
292   /* NOTE : This function should not be modified, when the callback is needed,
293             the HAL_TSC_MspDeInit could be implemented in the user file.
294    */ 
295 }
296
297 /**
298   * @}
299   */
300
301 /** @defgroup TSC_Exported_Functions_Group2 IO operation functions
302  *  @brief    IO operation functions 
303  *
304 @verbatim   
305  ===============================================================================
306              ##### IO operation functions #####
307  ===============================================================================  
308     [..]  This section provides functions allowing to:
309       (+) Start acquisition in polling mode.
310       (+) Start acquisition in interrupt mode.
311       (+) Stop conversion in polling mode.
312       (+) Stop conversion in interrupt mode.
313       (+) Get group acquisition status.
314       (+) Get group acquisition value.
315 @endverbatim
316   * @{
317   */
318
319 /**
320   * @brief  Starts the acquisition.
321   * @param  htsc: pointer to a TSC_HandleTypeDef structure that contains
322   *         the configuration information for the specified TSC.
323   * @retval HAL status
324   */
325 HAL_StatusTypeDef HAL_TSC_Start(TSC_HandleTypeDef* htsc)
326 {
327   /* Check the parameters */
328   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
329   
330   /* Process locked */
331   __HAL_LOCK(htsc);
332   
333   /* Change TSC state */
334   htsc->State = HAL_TSC_STATE_BUSY;
335
336   /* Clear interrupts */
337   __HAL_TSC_DISABLE_IT(htsc, (TSC_IT_EOA | TSC_IT_MCE));
338
339   /* Clear flags */
340   __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
341
342   /* Stop discharging the IOs */
343   __HAL_TSC_SET_IODEF_INFLOAT(htsc);
344   
345   /* Launch the acquisition */
346   __HAL_TSC_START_ACQ(htsc);
347   
348   /* Process unlocked */
349   __HAL_UNLOCK(htsc);
350   
351   /* Return function status */
352   return HAL_OK;
353 }
354
355 /**
356   * @brief  Enables the interrupt and starts the acquisition
357   * @param  htsc: pointer to a TSC_HandleTypeDef structure that contains
358   *         the configuration information for the specified TSC.
359   * @retval HAL status.
360   */
361 HAL_StatusTypeDef HAL_TSC_Start_IT(TSC_HandleTypeDef* htsc)
362 {
363   /* Check the parameters */
364   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
365   assert_param(IS_TSC_MCE_IT(htsc->Init.MaxCountInterrupt));
366
367   /* Process locked */
368   __HAL_LOCK(htsc);
369   
370   /* Change TSC state */
371   htsc->State = HAL_TSC_STATE_BUSY;
372   
373   /* Enable end of acquisition interrupt */
374   __HAL_TSC_ENABLE_IT(htsc, TSC_IT_EOA);
375
376   /* Enable max count error interrupt (optional) */
377   if (htsc->Init.MaxCountInterrupt == ENABLE)
378   {
379     __HAL_TSC_ENABLE_IT(htsc, TSC_IT_MCE);
380   }
381   else
382   {
383     __HAL_TSC_DISABLE_IT(htsc, TSC_IT_MCE);
384   }
385
386   /* Clear flags */
387   __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
388   
389   /* Stop discharging the IOs */
390   __HAL_TSC_SET_IODEF_INFLOAT(htsc);
391   
392   /* Launch the acquisition */
393   __HAL_TSC_START_ACQ(htsc);
394
395   /* Process unlocked */
396   __HAL_UNLOCK(htsc);
397   
398   /* Return function status */
399   return HAL_OK;
400 }
401
402 /**
403   * @brief  Stops the acquisition previously launched in polling mode
404   * @param  htsc: pointer to a TSC_HandleTypeDef structure that contains
405   *         the configuration information for the specified TSC.
406   * @retval HAL status
407   */
408 HAL_StatusTypeDef HAL_TSC_Stop(TSC_HandleTypeDef* htsc)
409 {
410   /* Check the parameters */
411   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
412
413   /* Process locked */
414   __HAL_LOCK(htsc);
415   
416   /* Stop the acquisition */
417   __HAL_TSC_STOP_ACQ(htsc);
418
419   /* Clear flags */
420   __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
421   
422   /* Change TSC state */
423   htsc->State = HAL_TSC_STATE_READY;
424
425   /* Process unlocked */
426   __HAL_UNLOCK(htsc);
427   
428   /* Return function status */
429   return HAL_OK;
430 }
431
432 /**
433   * @brief  Stops the acquisition previously launched in interrupt mode
434   * @param  htsc: pointer to a TSC_HandleTypeDef structure that contains
435   *         the configuration information for the specified TSC.
436   * @retval HAL status
437   */
438 HAL_StatusTypeDef HAL_TSC_Stop_IT(TSC_HandleTypeDef* htsc)
439 {
440   /* Check the parameters */
441   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
442
443   /* Process locked */
444   __HAL_LOCK(htsc);
445   
446   /* Stop the acquisition */
447   __HAL_TSC_STOP_ACQ(htsc);
448   
449   /* Disable interrupts */
450   __HAL_TSC_DISABLE_IT(htsc, (TSC_IT_EOA | TSC_IT_MCE));
451
452   /* Clear flags */
453   __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
454   
455   /* Change TSC state */
456   htsc->State = HAL_TSC_STATE_READY;
457
458   /* Process unlocked */
459   __HAL_UNLOCK(htsc);
460   
461   /* Return function status */
462   return HAL_OK;
463 }
464
465 /**
466   * @brief  Gets the acquisition status for a group
467   * @param  htsc: pointer to a TSC_HandleTypeDef structure that contains
468   *         the configuration information for the specified TSC.
469   * @param  gx_index: Index of the group
470   * @retval Group status
471   */
472 TSC_GroupStatusTypeDef HAL_TSC_GroupGetStatus(TSC_HandleTypeDef* htsc, uint32_t gx_index)
473 {
474   /* Check the parameters */
475   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
476   assert_param(IS_GROUP_INDEX(gx_index));
477
478   /* Return the group status */ 
479   return(__HAL_TSC_GET_GROUP_STATUS(htsc, gx_index));
480 }
481
482 /**
483   * @brief  Gets the acquisition measure for a group
484   * @param  htsc: pointer to a TSC_HandleTypeDef structure that contains
485   *         the configuration information for the specified TSC.
486   * @param  gx_index: Index of the group
487   * @retval Acquisition measure
488   */
489 uint32_t HAL_TSC_GroupGetValue(TSC_HandleTypeDef* htsc, uint32_t gx_index)
490 {       
491   /* Check the parameters */
492   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
493   assert_param(IS_GROUP_INDEX(gx_index));
494
495   /* Return the group acquisition counter */ 
496   return htsc->Instance->IOGXCR[gx_index];
497 }
498
499 /**
500   * @}
501   */
502   
503 /** @defgroup TSC_Exported_Functions_Group3 Peripheral Control functions
504  *  @brief    Peripheral Control functions 
505  *
506 @verbatim   
507  ===============================================================================
508              ##### Peripheral Control functions #####
509  ===============================================================================  
510     [..]  This section provides functions allowing to:
511       (+) Configure TSC IOs
512       (+) Discharge TSC IOs
513 @endverbatim
514   * @{
515   */
516
517 /**
518   * @brief  Configures TSC IOs
519   * @param  htsc: pointer to a TSC_HandleTypeDef structure that contains
520   *         the configuration information for the specified TSC.
521   * @param  config: pointer to the configuration structure.
522   * @retval HAL status
523   */
524 HAL_StatusTypeDef HAL_TSC_IOConfig(TSC_HandleTypeDef* htsc, TSC_IOConfigTypeDef* config)
525 {
526   /* Check the parameters */
527   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
528  
529   /* Process locked */
530   __HAL_LOCK(htsc);
531
532   /* Stop acquisition */
533   __HAL_TSC_STOP_ACQ(htsc);
534
535   /* Disable Schmitt trigger hysteresis on all used TSC IOs */
536   htsc->Instance->IOHCR = (uint32_t)(~(config->ChannelIOs | config->ShieldIOs | config->SamplingIOs));
537
538   /* Set channel and shield IOs */
539   htsc->Instance->IOCCR = (config->ChannelIOs | config->ShieldIOs);
540   
541   /* Set sampling IOs */
542   htsc->Instance->IOSCR = config->SamplingIOs;
543   
544   /* Set groups to be acquired */
545   htsc->Instance->IOGCSR = TSC_extract_groups(config->ChannelIOs);
546     
547   /* Process unlocked */
548   __HAL_UNLOCK(htsc);
549   
550   /* Return function status */
551   return HAL_OK;
552 }
553
554 /**
555   * @brief  Discharge TSC IOs
556   * @param  htsc: pointer to a TSC_HandleTypeDef structure that contains
557   *         the configuration information for the specified TSC.
558   * @param  choice: enable or disable
559   * @retval HAL status
560   */
561 HAL_StatusTypeDef HAL_TSC_IODischarge(TSC_HandleTypeDef* htsc, uint32_t choice)
562 {       
563   /* Check the parameters */
564   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
565
566   /* Process locked */
567   __HAL_LOCK(htsc);
568   
569   if (choice == ENABLE)
570   {
571     __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
572   }
573   else
574   {
575     __HAL_TSC_SET_IODEF_INFLOAT(htsc);
576   }
577
578   /* Process unlocked */
579   __HAL_UNLOCK(htsc);
580   
581   /* Return the group acquisition counter */ 
582   return HAL_OK;
583 }
584
585 /**
586   * @}
587   */
588
589 /** @defgroup TSC_Exported_Functions_Group4 State functions
590  *  @brief   State functions 
591  *
592 @verbatim   
593  ===============================================================================
594             ##### State functions #####
595  ===============================================================================  
596     [..]
597     This subsection provides functions allowing to
598       (+) Get TSC state.
599       (+) Poll for acquisition completed.
600       (+) Handles TSC interrupt request.
601          
602 @endverbatim
603   * @{
604   */
605
606 /**
607   * @brief  Return the TSC state
608   * @param  htsc: pointer to a TSC_HandleTypeDef structure that contains
609   *         the configuration information for the specified TSC.
610   * @retval HAL state
611   */
612 HAL_TSC_StateTypeDef HAL_TSC_GetState(TSC_HandleTypeDef* htsc)
613 {
614   /* Check the parameters */
615   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
616   
617   if (htsc->State == HAL_TSC_STATE_BUSY)
618   {
619     /* Check end of acquisition flag */
620     if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_EOA) != RESET)
621     {
622       /* Check max count error flag */
623       if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_MCE) != RESET)
624       {
625         /* Change TSC state */
626         htsc->State = HAL_TSC_STATE_ERROR;
627       }
628       else
629       {
630         /* Change TSC state */
631         htsc->State = HAL_TSC_STATE_READY;
632       }
633     }
634   }
635   
636   /* Return TSC state */
637   return htsc->State;
638 }
639
640 /**
641   * @brief  Start acquisition and wait until completion
642   * @note   There is no need of a timeout parameter as the max count error is already
643   *         managed by the TSC peripheral.
644   * @param  htsc: pointer to a TSC_HandleTypeDef structure that contains
645   *         the configuration information for the specified TSC.
646   * @retval HAL state
647   */
648 HAL_StatusTypeDef HAL_TSC_PollForAcquisition(TSC_HandleTypeDef* htsc)
649 {
650   /* Check the parameters */
651   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
652
653   /* Process locked */
654   __HAL_LOCK(htsc);
655   
656   /* Check end of acquisition */
657   while (HAL_TSC_GetState(htsc) == HAL_TSC_STATE_BUSY)
658   {
659     /* The timeout (max count error) is managed by the TSC peripheral itself. */
660   }
661
662   /* Process unlocked */
663   __HAL_UNLOCK(htsc);
664   
665   return HAL_OK;
666 }
667
668 /**
669   * @brief  Handles TSC interrupt request  
670   * @param  htsc: pointer to a TSC_HandleTypeDef structure that contains
671   *         the configuration information for the specified TSC.
672   * @retval None
673   */
674 void HAL_TSC_IRQHandler(TSC_HandleTypeDef* htsc)
675 {
676   /* Check the parameters */
677   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
678
679   /* Check if the end of acquisition occured */
680   if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_EOA) != RESET)
681   {
682     /* Clear EOA flag */
683     __HAL_TSC_CLEAR_FLAG(htsc, TSC_FLAG_EOA);
684   }
685   
686   /* Check if max count error occured */
687   if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_MCE) != RESET)
688   {
689     /* Clear MCE flag */
690     __HAL_TSC_CLEAR_FLAG(htsc, TSC_FLAG_MCE);
691     /* Change TSC state */
692     htsc->State = HAL_TSC_STATE_ERROR;
693     /* Conversion completed callback */
694     HAL_TSC_ErrorCallback(htsc);
695   }
696   else
697   {
698     /* Change TSC state */
699     htsc->State = HAL_TSC_STATE_READY;
700     /* Conversion completed callback */
701     HAL_TSC_ConvCpltCallback(htsc);
702   }
703 }
704
705 /**
706   * @}
707   */
708
709 /** @defgroup TSC_Exported_Functions_Group5 Callback functions
710  *  @brief   Callback functions 
711  *  @{
712  */
713  
714 /**
715   * @brief  Acquisition completed callback in non blocking mode 
716   * @param  htsc: pointer to a TSC_HandleTypeDef structure that contains
717   *         the configuration information for the specified TSC.
718   * @retval None
719   */
720 __weak void HAL_TSC_ConvCpltCallback(TSC_HandleTypeDef* htsc)
721 {
722   /* NOTE : This function should not be modified, when the callback is needed,
723             the HAL_TSC_ConvCpltCallback could be implemented in the user file.
724    */
725 }
726
727 /**
728   * @brief  Error callback in non blocking mode
729   * @param  htsc: pointer to a TSC_HandleTypeDef structure that contains
730   *         the configuration information for the specified TSC.
731   * @retval None
732   */
733 __weak void HAL_TSC_ErrorCallback(TSC_HandleTypeDef* htsc)
734 {
735   /* NOTE : This function should not be modified, when the callback is needed,
736             the HAL_TSC_ErrorCallback could be implemented in the user file.
737    */
738 }
739
740 /**
741   * @}
742   */
743
744 /**
745   * @}
746   */
747
748 /** @defgroup TSC_Private_Functions TSC Private Functions
749  *  @{
750  */
751       
752 /**
753   * @brief  Utility function used to set the acquired groups mask
754   * @param  iomask: Channels IOs mask
755   * @retval Acquired groups mask
756   */
757 static uint32_t TSC_extract_groups(uint32_t iomask)
758 {
759   uint32_t groups = 0;
760   uint32_t idx;
761   
762   for (idx = 0; idx < TSC_NB_OF_GROUPS; idx++)
763   {
764     if ((iomask & ((uint32_t)0x0F << (idx * 4))) != RESET)
765     {
766       groups |= ((uint32_t)1 << idx);
767     }
768   }
769   
770   return groups;
771 }
772
773 /**
774   * @}
775   */
776
777 /**
778   * @}
779   */ 
780
781 /**
782   * @}
783   */ 
784
785 #endif /* defined(STM32F051x8) || defined(STM32F071xB) || defined(STM32F091xC) || */
786        /* defined(STM32F042x6) || defined(STM32F072xB) ||                         */
787        /* defined(STM32F048xx) || defined(STM32F058xx) || defined(STM32F078xx) || defined(STM32F098xx) */
788
789 #endif /* HAL_TSC_MODULE_ENABLED */
790
791 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/