STM32L443xx HAL User Manual
stm32l4xx_hal_tsc.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32l4xx_hal_tsc.c
00004   * @author  MCD Application Team
00005   * @brief   This file provides firmware functions to manage the following
00006   *          functionalities of the Touch Sensing Controller (TSC) peripheral:
00007   *           + Initialization and De-initialization
00008   *           + Channel IOs, Shield IOs and Sampling IOs configuration
00009   *           + Start and Stop an acquisition
00010   *           + Read acquisition result
00011   *           + Interrupts and flags management
00012   *
00013   ******************************************************************************
00014   * @attention
00015   *
00016   * Copyright (c) 2017 STMicroelectronics.
00017   * All rights reserved.
00018   *
00019   * This software is licensed under terms that can be found in the LICENSE file
00020   * in the root directory of this software component.
00021   * If no LICENSE file comes with this software, it is provided AS-IS.
00022   *
00023   ******************************************************************************
00024   @verbatim
00025 ================================================================================
00026                        ##### TSC specific features #####
00027 ================================================================================
00028   [..]
00029   (#) Proven and robust surface charge transfer acquisition principle
00030 
00031   (#) Supports up to 3 capacitive sensing channels per group
00032 
00033   (#) Capacitive sensing channels can be acquired in parallel offering a very good
00034       response time
00035 
00036   (#) Spread spectrum feature to improve system robustness in noisy environments
00037 
00038   (#) Full hardware management of the charge transfer acquisition sequence
00039 
00040   (#) Programmable charge transfer frequency
00041 
00042   (#) Programmable sampling capacitor I/O pin
00043 
00044   (#) Programmable channel I/O pin
00045 
00046   (#) Programmable max count value to avoid long acquisition when a channel is faulty
00047 
00048   (#) Dedicated end of acquisition and max count error flags with interrupt capability
00049 
00050   (#) One sampling capacitor for up to 3 capacitive sensing channels to reduce the system
00051       components
00052 
00053   (#) Compatible with proximity, touchkey, linear and rotary touch sensor implementation
00054 
00055                           ##### How to use this driver #####
00056 ================================================================================
00057   [..]
00058     (#) Enable the TSC interface clock using __HAL_RCC_TSC_CLK_ENABLE() macro.
00059 
00060     (#) GPIO pins configuration
00061       (++) Enable the clock for the TSC GPIOs using __HAL_RCC_GPIOx_CLK_ENABLE() macro.
00062       (++) Configure the TSC pins used as sampling IOs in alternate function output Open-Drain mode,
00063            and TSC pins used as channel/shield IOs in alternate function output Push-Pull mode
00064            using HAL_GPIO_Init() function.
00065 
00066     (#) Interrupts configuration
00067       (++) Configure the NVIC (if the interrupt model is used) using HAL_NVIC_SetPriority()
00068            and HAL_NVIC_EnableIRQ() and function.
00069 
00070     (#) TSC configuration
00071       (++) Configure all TSC parameters and used TSC IOs using HAL_TSC_Init() function.
00072 
00073  [..]   TSC peripheral alternate functions are mapped on AF9.
00074 
00075   *** Acquisition sequence ***
00076   ===================================
00077   [..]
00078     (+) Discharge all IOs using HAL_TSC_IODischarge() function.
00079     (+) Wait a certain time allowing a good discharge of all capacitors. This delay depends
00080         of the sampling capacitor and electrodes design.
00081     (+) Select the channel IOs to be acquired using HAL_TSC_IOConfig() function.
00082     (+) Launch the acquisition using either HAL_TSC_Start() or HAL_TSC_Start_IT() function.
00083         If the synchronized mode is selected, the acquisition will start as soon as the signal
00084         is received on the synchro pin.
00085     (+) Wait the end of acquisition using either HAL_TSC_PollForAcquisition() or
00086         HAL_TSC_GetState() function or using WFI instruction for example.
00087     (+) Check the group acquisition status using HAL_TSC_GroupGetStatus() function.
00088     (+) Read the acquisition value using HAL_TSC_GroupGetValue() function.
00089 
00090      *** Callback registration ***
00091      =============================================
00092 
00093   [..]
00094      The compilation flag USE_HAL_TSC_REGISTER_CALLBACKS when set to 1
00095      allows the user to configure dynamically the driver callbacks.
00096      Use Functions HAL_TSC_RegisterCallback() to register an interrupt callback.
00097 
00098   [..]
00099      Function HAL_TSC_RegisterCallback() allows to register following callbacks:
00100        (+) ConvCpltCallback   : callback for conversion complete process.
00101        (+) ErrorCallback      : callback for error detection.
00102        (+) MspInitCallback    : callback for Msp Init.
00103        (+) MspDeInitCallback  : callback for Msp DeInit.
00104   [..]
00105      This function takes as parameters the HAL peripheral handle, the Callback ID
00106      and a pointer to the user callback function.
00107 
00108   [..]
00109      Use function HAL_TSC_UnRegisterCallback to reset a callback to the default
00110      weak function.
00111      HAL_TSC_UnRegisterCallback takes as parameters the HAL peripheral handle,
00112      and the Callback ID.
00113   [..]
00114      This function allows to reset following callbacks:
00115        (+) ConvCpltCallback   : callback for conversion complete process.
00116        (+) ErrorCallback      : callback for error detection.
00117        (+) MspInitCallback    : callback for Msp Init.
00118        (+) MspDeInitCallback  : callback for Msp DeInit.
00119 
00120   [..]
00121      By default, after the HAL_TSC_Init() and when the state is HAL_TSC_STATE_RESET
00122      all callbacks are set to the corresponding weak functions:
00123      examples HAL_TSC_ConvCpltCallback(), HAL_TSC_ErrorCallback().
00124      Exception done for MspInit and MspDeInit functions that are
00125      reset to the legacy weak functions in the HAL_TSC_Init()/ HAL_TSC_DeInit() only when
00126      these callbacks are null (not registered beforehand).
00127      If MspInit or MspDeInit are not null, the HAL_TSC_Init()/ HAL_TSC_DeInit()
00128      keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
00129 
00130   [..]
00131      Callbacks can be registered/unregistered in HAL_TSC_STATE_READY state only.
00132      Exception done MspInit/MspDeInit functions that can be registered/unregistered
00133      in HAL_TSC_STATE_READY or HAL_TSC_STATE_RESET state,
00134      thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
00135      Then, the user first registers the MspInit/MspDeInit user callbacks
00136      using HAL_TSC_RegisterCallback() before calling HAL_TSC_DeInit()
00137      or HAL_TSC_Init() function.
00138 
00139   [..]
00140      When the compilation flag USE_HAL_TSC_REGISTER_CALLBACKS is set to 0 or
00141      not defined, the callback registration feature is not available and all callbacks
00142      are set to the corresponding weak functions.
00143 
00144   @endverbatim
00145   ******************************************************************************
00146 
00147     Table 1. IOs for the STM32L4xx devices
00148     +--------------------------------+
00149     |       IOs    |   TSC functions |
00150     |--------------|-----------------|
00151     |   PB12 (AF)  |   TSC_G1_IO1    |
00152     |   PB13 (AF)  |   TSC_G1_IO2    |
00153     |   PB14 (AF)  |   TSC_G1_IO3    |
00154     |   PB15 (AF)  |   TSC_G1_IO4    |
00155     |--------------|-----------------|
00156     |   PB4 (AF)   |   TSC_G2_IO1    |
00157     |   PB5 (AF)   |   TSC_G2_IO2    |
00158     |   PB6 (AF)   |   TSC_G2_IO3    |
00159     |   PB7 (AF)   |   TSC_G2_IO4    |
00160     |--------------|-----------------|
00161     |   PA15 (AF)  |   TSC_G3_IO1    |
00162     |   PC10 (AF)  |   TSC_G3_IO2    |
00163     |   PC11 (AF)  |   TSC_G3_IO3    |
00164     |   PC12 (AF)  |   TSC_G3_IO4    |
00165     |--------------|-----------------|
00166     |   PC6 (AF)   |   TSC_G4_IO1    |
00167     |   PC7 (AF)   |   TSC_G4_IO2    |
00168     |   PC8 (AF)   |   TSC_G4_IO3    |
00169     |   PC9 (AF)   |   TSC_G4_IO4    |
00170     |--------------|-----------------|
00171     |   PE10 (AF)  |   TSC_G5_IO1    |
00172     |   PE11 (AF)  |   TSC_G5_IO2    |
00173     |   PE12 (AF)  |   TSC_G5_IO3    |
00174     |   PE13 (AF)  |   TSC_G5_IO4    |
00175     |--------------|-----------------|
00176     |   PD10 (AF)  |   TSC_G6_IO1    |
00177     |   PD11 (AF)  |   TSC_G6_IO2    |
00178     |   PD12 (AF)  |   TSC_G6_IO3    |
00179     |   PD13 (AF)  |   TSC_G6_IO4    |
00180     |--------------|-----------------|
00181     |   PE2 (AF)   |   TSC_G7_IO1    |
00182     |   PE3 (AF)   |   TSC_G7_IO2    |
00183     |   PE4 (AF)   |   TSC_G7_IO3    |
00184     |   PE5 (AF)   |   TSC_G7_IO4    |
00185     |--------------|-----------------|
00186     |   PF14 (AF)  |   TSC_G8_IO1    |
00187     |   PF15 (AF)  |   TSC_G8_IO2    |
00188     |   PG0 (AF)   |   TSC_G8_IO3    |
00189     |   PG1 (AF)   |   TSC_G8_IO4    |
00190     |--------------|-----------------|
00191     |   PB10 (AF)  |   TSC_SYNC      |
00192     |   PD2 (AF)   |                 |
00193     +--------------------------------+
00194 
00195 
00196   */
00197 
00198 /* Includes ------------------------------------------------------------------*/
00199 #include "stm32l4xx_hal.h"
00200 
00201 /** @addtogroup STM32L4xx_HAL_Driver
00202   * @{
00203   */
00204 
00205 /** @defgroup TSC TSC
00206   * @brief HAL TSC module driver
00207   * @{
00208   */
00209 
00210 #ifdef HAL_TSC_MODULE_ENABLED
00211 
00212 /* Private typedef -----------------------------------------------------------*/
00213 /* Private define ------------------------------------------------------------*/
00214 /* Private macro -------------------------------------------------------------*/
00215 /* Private variables ---------------------------------------------------------*/
00216 /* Private function prototypes -----------------------------------------------*/
00217 static uint32_t TSC_extract_groups(uint32_t iomask);
00218 
00219 /* Exported functions --------------------------------------------------------*/
00220 
00221 /** @defgroup TSC_Exported_Functions TSC Exported Functions
00222   * @{
00223   */
00224 
00225 /** @defgroup TSC_Exported_Functions_Group1 Initialization and de-initialization functions
00226   *  @brief    Initialization and Configuration functions
00227   *
00228 @verbatim
00229  ===============================================================================
00230               ##### Initialization and de-initialization functions #####
00231  ===============================================================================
00232     [..]  This section provides functions allowing to:
00233       (+) Initialize and configure the TSC.
00234       (+) De-initialize the TSC.
00235 @endverbatim
00236   * @{
00237   */
00238 
00239 /**
00240   * @brief  Initialize the TSC peripheral according to the specified parameters
00241   *         in the TSC_InitTypeDef structure and initialize the associated handle.
00242   * @param  htsc TSC handle
00243   * @retval HAL status
00244   */
00245 HAL_StatusTypeDef HAL_TSC_Init(TSC_HandleTypeDef *htsc)
00246 {
00247   /* Check TSC handle allocation */
00248   if (htsc == NULL)
00249   {
00250     return HAL_ERROR;
00251   }
00252 
00253   /* Check the parameters */
00254   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
00255   assert_param(IS_TSC_CTPH(htsc->Init.CTPulseHighLength));
00256   assert_param(IS_TSC_CTPL(htsc->Init.CTPulseLowLength));
00257   assert_param(IS_TSC_SS(htsc->Init.SpreadSpectrum));
00258   assert_param(IS_TSC_SSD(htsc->Init.SpreadSpectrumDeviation));
00259   assert_param(IS_TSC_SS_PRESC(htsc->Init.SpreadSpectrumPrescaler));
00260   assert_param(IS_TSC_PG_PRESC(htsc->Init.PulseGeneratorPrescaler));
00261   assert_param(IS_TSC_PG_PRESC_VS_CTPL(htsc->Init.PulseGeneratorPrescaler, htsc->Init.CTPulseLowLength));
00262   assert_param(IS_TSC_MCV(htsc->Init.MaxCountValue));
00263   assert_param(IS_TSC_IODEF(htsc->Init.IODefaultMode));
00264   assert_param(IS_TSC_SYNC_POL(htsc->Init.SynchroPinPolarity));
00265   assert_param(IS_TSC_ACQ_MODE(htsc->Init.AcquisitionMode));
00266   assert_param(IS_TSC_MCE_IT(htsc->Init.MaxCountInterrupt));
00267   assert_param(IS_TSC_GROUP(htsc->Init.ChannelIOs));
00268   assert_param(IS_TSC_GROUP(htsc->Init.ShieldIOs));
00269   assert_param(IS_TSC_GROUP(htsc->Init.SamplingIOs));
00270 
00271   if (htsc->State == HAL_TSC_STATE_RESET)
00272   {
00273     /* Allocate lock resource and initialize it */
00274     htsc->Lock = HAL_UNLOCKED;
00275 
00276 #if (USE_HAL_TSC_REGISTER_CALLBACKS == 1)
00277     /* Init the TSC Callback settings */
00278     htsc->ConvCpltCallback  = HAL_TSC_ConvCpltCallback; /* Legacy weak ConvCpltCallback     */
00279     htsc->ErrorCallback     = HAL_TSC_ErrorCallback;    /* Legacy weak ErrorCallback        */
00280 
00281     if (htsc->MspInitCallback == NULL)
00282     {
00283       htsc->MspInitCallback = HAL_TSC_MspInit; /* Legacy weak MspInit  */
00284     }
00285 
00286     /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
00287     htsc->MspInitCallback(htsc);
00288 #else
00289     /* Init the low level hardware : GPIO, CLOCK, CORTEX */
00290     HAL_TSC_MspInit(htsc);
00291 #endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
00292   }
00293 
00294   /* Initialize the TSC state */
00295   htsc->State = HAL_TSC_STATE_BUSY;
00296 
00297   /*--------------------------------------------------------------------------*/
00298   /* Set TSC parameters */
00299 
00300   /* Enable TSC */
00301   htsc->Instance->CR = TSC_CR_TSCE;
00302 
00303   /* Set all functions */
00304   htsc->Instance->CR |= (htsc->Init.CTPulseHighLength |
00305                          htsc->Init.CTPulseLowLength |
00306                          (htsc->Init.SpreadSpectrumDeviation << TSC_CR_SSD_Pos) |
00307                          htsc->Init.SpreadSpectrumPrescaler |
00308                          htsc->Init.PulseGeneratorPrescaler |
00309                          htsc->Init.MaxCountValue |
00310                          htsc->Init.SynchroPinPolarity |
00311                          htsc->Init.AcquisitionMode);
00312 
00313   /* Spread spectrum */
00314   if (htsc->Init.SpreadSpectrum == ENABLE)
00315   {
00316     htsc->Instance->CR |= TSC_CR_SSE;
00317   }
00318 
00319   /* Disable Schmitt trigger hysteresis on all used TSC IOs */
00320   htsc->Instance->IOHCR = (~(htsc->Init.ChannelIOs | htsc->Init.ShieldIOs | htsc->Init.SamplingIOs));
00321 
00322   /* Set channel and shield IOs */
00323   htsc->Instance->IOCCR = (htsc->Init.ChannelIOs | htsc->Init.ShieldIOs);
00324 
00325   /* Set sampling IOs */
00326   htsc->Instance->IOSCR = htsc->Init.SamplingIOs;
00327 
00328   /* Set the groups to be acquired */
00329   htsc->Instance->IOGCSR = TSC_extract_groups(htsc->Init.ChannelIOs);
00330 
00331   /* Disable interrupts */
00332   htsc->Instance->IER &= (~(TSC_IT_EOA | TSC_IT_MCE));
00333 
00334   /* Clear flags */
00335   htsc->Instance->ICR = (TSC_FLAG_EOA | TSC_FLAG_MCE);
00336 
00337   /*--------------------------------------------------------------------------*/
00338 
00339   /* Initialize the TSC state */
00340   htsc->State = HAL_TSC_STATE_READY;
00341 
00342   /* Return function status */
00343   return HAL_OK;
00344 }
00345 
00346 /**
00347   * @brief  Deinitialize the TSC peripheral registers to their default reset values.
00348   * @param  htsc TSC handle
00349   * @retval HAL status
00350   */
00351 HAL_StatusTypeDef HAL_TSC_DeInit(TSC_HandleTypeDef *htsc)
00352 {
00353   /* Check TSC handle allocation */
00354   if (htsc == NULL)
00355   {
00356     return HAL_ERROR;
00357   }
00358 
00359   /* Check the parameters */
00360   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
00361 
00362   /* Change TSC state */
00363   htsc->State = HAL_TSC_STATE_BUSY;
00364 
00365 #if (USE_HAL_TSC_REGISTER_CALLBACKS == 1)
00366   if (htsc->MspDeInitCallback == NULL)
00367   {
00368     htsc->MspDeInitCallback = HAL_TSC_MspDeInit; /* Legacy weak MspDeInit  */
00369   }
00370 
00371   /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
00372   htsc->MspDeInitCallback(htsc);
00373 #else
00374   /* DeInit the low level hardware */
00375   HAL_TSC_MspDeInit(htsc);
00376 #endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
00377 
00378   /* Change TSC state */
00379   htsc->State = HAL_TSC_STATE_RESET;
00380 
00381   /* Process unlocked */
00382   __HAL_UNLOCK(htsc);
00383 
00384   /* Return function status */
00385   return HAL_OK;
00386 }
00387 
00388 /**
00389   * @brief  Initialize the TSC MSP.
00390   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
00391   *         the configuration information for the specified TSC.
00392   * @retval None
00393   */
00394 __weak void HAL_TSC_MspInit(TSC_HandleTypeDef *htsc)
00395 {
00396   /* Prevent unused argument(s) compilation warning */
00397   UNUSED(htsc);
00398 
00399   /* NOTE : This function should not be modified, when the callback is needed,
00400             the HAL_TSC_MspInit could be implemented in the user file.
00401    */
00402 }
00403 
00404 /**
00405   * @brief  DeInitialize the TSC MSP.
00406   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
00407   *         the configuration information for the specified TSC.
00408   * @retval None
00409   */
00410 __weak void HAL_TSC_MspDeInit(TSC_HandleTypeDef *htsc)
00411 {
00412   /* Prevent unused argument(s) compilation warning */
00413   UNUSED(htsc);
00414 
00415   /* NOTE : This function should not be modified, when the callback is needed,
00416             the HAL_TSC_MspDeInit could be implemented in the user file.
00417    */
00418 }
00419 
00420 #if (USE_HAL_TSC_REGISTER_CALLBACKS == 1)
00421 /**
00422   * @brief  Register a User TSC Callback
00423   *         To be used instead of the weak predefined callback
00424   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
00425   *                the configuration information for the specified TSC.
00426   * @param  CallbackID ID of the callback to be registered
00427   *         This parameter can be one of the following values:
00428   *          @arg @ref HAL_TSC_CONV_COMPLETE_CB_ID Conversion completed callback ID
00429   *          @arg @ref HAL_TSC_ERROR_CB_ID Error callback ID
00430   *          @arg @ref HAL_TSC_MSPINIT_CB_ID MspInit callback ID
00431   *          @arg @ref HAL_TSC_MSPDEINIT_CB_ID MspDeInit callback ID
00432   * @param  pCallback pointer to the Callback function
00433   * @retval HAL status
00434   */
00435 HAL_StatusTypeDef HAL_TSC_RegisterCallback(TSC_HandleTypeDef *htsc, HAL_TSC_CallbackIDTypeDef CallbackID,
00436                                            pTSC_CallbackTypeDef pCallback)
00437 {
00438   HAL_StatusTypeDef status = HAL_OK;
00439 
00440   if (pCallback == NULL)
00441   {
00442     /* Update the error code */
00443     htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
00444 
00445     return HAL_ERROR;
00446   }
00447   /* Process locked */
00448   __HAL_LOCK(htsc);
00449 
00450   if (HAL_TSC_STATE_READY == htsc->State)
00451   {
00452     switch (CallbackID)
00453     {
00454       case HAL_TSC_CONV_COMPLETE_CB_ID :
00455         htsc->ConvCpltCallback = pCallback;
00456         break;
00457 
00458       case HAL_TSC_ERROR_CB_ID :
00459         htsc->ErrorCallback = pCallback;
00460         break;
00461 
00462       case HAL_TSC_MSPINIT_CB_ID :
00463         htsc->MspInitCallback = pCallback;
00464         break;
00465 
00466       case HAL_TSC_MSPDEINIT_CB_ID :
00467         htsc->MspDeInitCallback = pCallback;
00468         break;
00469 
00470       default :
00471         /* Update the error code */
00472         htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
00473 
00474         /* Return error status */
00475         status =  HAL_ERROR;
00476         break;
00477     }
00478   }
00479   else if (HAL_TSC_STATE_RESET == htsc->State)
00480   {
00481     switch (CallbackID)
00482     {
00483       case HAL_TSC_MSPINIT_CB_ID :
00484         htsc->MspInitCallback = pCallback;
00485         break;
00486 
00487       case HAL_TSC_MSPDEINIT_CB_ID :
00488         htsc->MspDeInitCallback = pCallback;
00489         break;
00490 
00491       default :
00492         /* Update the error code */
00493         htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
00494 
00495         /* Return error status */
00496         status =  HAL_ERROR;
00497         break;
00498     }
00499   }
00500   else
00501   {
00502     /* Update the error code */
00503     htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
00504 
00505     /* Return error status */
00506     status =  HAL_ERROR;
00507   }
00508 
00509   /* Release Lock */
00510   __HAL_UNLOCK(htsc);
00511   return status;
00512 }
00513 
00514 /**
00515   * @brief  Unregister an TSC Callback
00516   *         TSC callback is redirected to the weak predefined callback
00517   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
00518   *                the configuration information for the specified TSC.
00519   * @param  CallbackID ID of the callback to be unregistered
00520   *         This parameter can be one of the following values:
00521   *         This parameter can be one of the following values:
00522   *          @arg @ref HAL_TSC_CONV_COMPLETE_CB_ID Conversion completed callback ID
00523   *          @arg @ref HAL_TSC_ERROR_CB_ID Error callback ID
00524   *          @arg @ref HAL_TSC_MSPINIT_CB_ID MspInit callback ID
00525   *          @arg @ref HAL_TSC_MSPDEINIT_CB_ID MspDeInit callback ID
00526   * @retval HAL status
00527   */
00528 HAL_StatusTypeDef HAL_TSC_UnRegisterCallback(TSC_HandleTypeDef *htsc, HAL_TSC_CallbackIDTypeDef CallbackID)
00529 {
00530   HAL_StatusTypeDef status = HAL_OK;
00531 
00532   /* Process locked */
00533   __HAL_LOCK(htsc);
00534 
00535   if (HAL_TSC_STATE_READY == htsc->State)
00536   {
00537     switch (CallbackID)
00538     {
00539       case HAL_TSC_CONV_COMPLETE_CB_ID :
00540         htsc->ConvCpltCallback = HAL_TSC_ConvCpltCallback;       /* Legacy weak ConvCpltCallback      */
00541         break;
00542 
00543       case HAL_TSC_ERROR_CB_ID :
00544         htsc->ErrorCallback = HAL_TSC_ErrorCallback;               /* Legacy weak ErrorCallback        */
00545         break;
00546 
00547       case HAL_TSC_MSPINIT_CB_ID :
00548         htsc->MspInitCallback = HAL_TSC_MspInit;                   /* Legacy weak MspInit              */
00549         break;
00550 
00551       case HAL_TSC_MSPDEINIT_CB_ID :
00552         htsc->MspDeInitCallback = HAL_TSC_MspDeInit;               /* Legacy weak MspDeInit            */
00553         break;
00554 
00555       default :
00556         /* Update the error code */
00557         htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
00558 
00559         /* Return error status */
00560         status =  HAL_ERROR;
00561         break;
00562     }
00563   }
00564   else if (HAL_TSC_STATE_RESET == htsc->State)
00565   {
00566     switch (CallbackID)
00567     {
00568       case HAL_TSC_MSPINIT_CB_ID :
00569         htsc->MspInitCallback = HAL_TSC_MspInit;                   /* Legacy weak MspInit              */
00570         break;
00571 
00572       case HAL_TSC_MSPDEINIT_CB_ID :
00573         htsc->MspDeInitCallback = HAL_TSC_MspDeInit;               /* Legacy weak MspDeInit            */
00574         break;
00575 
00576       default :
00577         /* Update the error code */
00578         htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
00579 
00580         /* Return error status */
00581         status =  HAL_ERROR;
00582         break;
00583     }
00584   }
00585   else
00586   {
00587     /* Update the error code */
00588     htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
00589 
00590     /* Return error status */
00591     status =  HAL_ERROR;
00592   }
00593 
00594   /* Release Lock */
00595   __HAL_UNLOCK(htsc);
00596   return status;
00597 }
00598 
00599 #endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
00600 
00601 /**
00602   * @}
00603   */
00604 
00605 /** @defgroup TSC_Exported_Functions_Group2 Input and Output operation functions
00606   *  @brief    Input and Output operation functions
00607   *
00608 @verbatim
00609  ===============================================================================
00610              ##### IO Operation functions #####
00611  ===============================================================================
00612     [..]  This section provides functions allowing to:
00613       (+) Start acquisition in polling mode.
00614       (+) Start acquisition in interrupt mode.
00615       (+) Stop conversion in polling mode.
00616       (+) Stop conversion in interrupt mode.
00617       (+) Poll for acquisition completed.
00618       (+) Get group acquisition status.
00619       (+) Get group acquisition value.
00620 @endverbatim
00621   * @{
00622   */
00623 
00624 /**
00625   * @brief  Start the acquisition.
00626   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
00627   *         the configuration information for the specified TSC.
00628   * @retval HAL status
00629   */
00630 HAL_StatusTypeDef HAL_TSC_Start(TSC_HandleTypeDef *htsc)
00631 {
00632   /* Check the parameters */
00633   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
00634 
00635   /* Process locked */
00636   __HAL_LOCK(htsc);
00637 
00638   /* Change TSC state */
00639   htsc->State = HAL_TSC_STATE_BUSY;
00640 
00641   /* Clear interrupts */
00642   __HAL_TSC_DISABLE_IT(htsc, (TSC_IT_EOA | TSC_IT_MCE));
00643 
00644   /* Clear flags */
00645   __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
00646 
00647   /* Set touch sensing IOs not acquired to the specified IODefaultMode */
00648   if (htsc->Init.IODefaultMode == TSC_IODEF_OUT_PP_LOW)
00649   {
00650     __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
00651   }
00652   else
00653   {
00654     __HAL_TSC_SET_IODEF_INFLOAT(htsc);
00655   }
00656 
00657   /* Launch the acquisition */
00658   __HAL_TSC_START_ACQ(htsc);
00659 
00660   /* Process unlocked */
00661   __HAL_UNLOCK(htsc);
00662 
00663   /* Return function status */
00664   return HAL_OK;
00665 }
00666 
00667 /**
00668   * @brief  Start the acquisition in interrupt mode.
00669   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
00670   *         the configuration information for the specified TSC.
00671   * @retval HAL status.
00672   */
00673 HAL_StatusTypeDef HAL_TSC_Start_IT(TSC_HandleTypeDef *htsc)
00674 {
00675   /* Check the parameters */
00676   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
00677   assert_param(IS_TSC_MCE_IT(htsc->Init.MaxCountInterrupt));
00678 
00679   /* Process locked */
00680   __HAL_LOCK(htsc);
00681 
00682   /* Change TSC state */
00683   htsc->State = HAL_TSC_STATE_BUSY;
00684 
00685   /* Enable end of acquisition interrupt */
00686   __HAL_TSC_ENABLE_IT(htsc, TSC_IT_EOA);
00687 
00688   /* Enable max count error interrupt (optional) */
00689   if (htsc->Init.MaxCountInterrupt == ENABLE)
00690   {
00691     __HAL_TSC_ENABLE_IT(htsc, TSC_IT_MCE);
00692   }
00693   else
00694   {
00695     __HAL_TSC_DISABLE_IT(htsc, TSC_IT_MCE);
00696   }
00697 
00698   /* Clear flags */
00699   __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
00700 
00701   /* Set touch sensing IOs not acquired to the specified IODefaultMode */
00702   if (htsc->Init.IODefaultMode == TSC_IODEF_OUT_PP_LOW)
00703   {
00704     __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
00705   }
00706   else
00707   {
00708     __HAL_TSC_SET_IODEF_INFLOAT(htsc);
00709   }
00710 
00711   /* Launch the acquisition */
00712   __HAL_TSC_START_ACQ(htsc);
00713 
00714   /* Process unlocked */
00715   __HAL_UNLOCK(htsc);
00716 
00717   /* Return function status */
00718   return HAL_OK;
00719 }
00720 
00721 /**
00722   * @brief  Stop the acquisition previously launched in polling mode.
00723   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
00724   *         the configuration information for the specified TSC.
00725   * @retval HAL status
00726   */
00727 HAL_StatusTypeDef HAL_TSC_Stop(TSC_HandleTypeDef *htsc)
00728 {
00729   /* Check the parameters */
00730   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
00731 
00732   /* Process locked */
00733   __HAL_LOCK(htsc);
00734 
00735   /* Stop the acquisition */
00736   __HAL_TSC_STOP_ACQ(htsc);
00737 
00738   /* Set touch sensing IOs in low power mode (output push-pull) */
00739   __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
00740 
00741   /* Clear flags */
00742   __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
00743 
00744   /* Change TSC state */
00745   htsc->State = HAL_TSC_STATE_READY;
00746 
00747   /* Process unlocked */
00748   __HAL_UNLOCK(htsc);
00749 
00750   /* Return function status */
00751   return HAL_OK;
00752 }
00753 
00754 /**
00755   * @brief  Stop the acquisition previously launched in interrupt mode.
00756   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
00757   *         the configuration information for the specified TSC.
00758   * @retval HAL status
00759   */
00760 HAL_StatusTypeDef HAL_TSC_Stop_IT(TSC_HandleTypeDef *htsc)
00761 {
00762   /* Check the parameters */
00763   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
00764 
00765   /* Process locked */
00766   __HAL_LOCK(htsc);
00767 
00768   /* Stop the acquisition */
00769   __HAL_TSC_STOP_ACQ(htsc);
00770 
00771   /* Set touch sensing IOs in low power mode (output push-pull) */
00772   __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
00773 
00774   /* Disable interrupts */
00775   __HAL_TSC_DISABLE_IT(htsc, (TSC_IT_EOA | TSC_IT_MCE));
00776 
00777   /* Clear flags */
00778   __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
00779 
00780   /* Change TSC state */
00781   htsc->State = HAL_TSC_STATE_READY;
00782 
00783   /* Process unlocked */
00784   __HAL_UNLOCK(htsc);
00785 
00786   /* Return function status */
00787   return HAL_OK;
00788 }
00789 
00790 /**
00791   * @brief  Start acquisition and wait until completion.
00792   * @note   There is no need of a timeout parameter as the max count error is already
00793   *         managed by the TSC peripheral.
00794   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
00795   *         the configuration information for the specified TSC.
00796   * @retval HAL state
00797   */
00798 HAL_StatusTypeDef HAL_TSC_PollForAcquisition(TSC_HandleTypeDef *htsc)
00799 {
00800   /* Check the parameters */
00801   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
00802 
00803   /* Process locked */
00804   __HAL_LOCK(htsc);
00805 
00806   /* Check end of acquisition */
00807   while (HAL_TSC_GetState(htsc) == HAL_TSC_STATE_BUSY)
00808   {
00809     /* The timeout (max count error) is managed by the TSC peripheral itself. */
00810   }
00811 
00812   /* Process unlocked */
00813   __HAL_UNLOCK(htsc);
00814 
00815   return HAL_OK;
00816 }
00817 
00818 /**
00819   * @brief  Get the acquisition status for a group.
00820   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
00821   *         the configuration information for the specified TSC.
00822   * @param  gx_index Index of the group
00823   * @retval Group status
00824   */
00825 TSC_GroupStatusTypeDef HAL_TSC_GroupGetStatus(TSC_HandleTypeDef *htsc, uint32_t gx_index)
00826 {
00827   /* Check the parameters */
00828   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
00829   assert_param(IS_TSC_GROUP_INDEX(gx_index));
00830 
00831   /* Return the group status */
00832   return (__HAL_TSC_GET_GROUP_STATUS(htsc, gx_index));
00833 }
00834 
00835 /**
00836   * @brief  Get the acquisition measure for a group.
00837   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
00838   *         the configuration information for the specified TSC.
00839   * @param  gx_index Index of the group
00840   * @retval Acquisition measure
00841   */
00842 uint32_t HAL_TSC_GroupGetValue(TSC_HandleTypeDef *htsc, uint32_t gx_index)
00843 {
00844   /* Check the parameters */
00845   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
00846   assert_param(IS_TSC_GROUP_INDEX(gx_index));
00847 
00848   /* Return the group acquisition counter */
00849   return htsc->Instance->IOGXCR[gx_index];
00850 }
00851 
00852 /**
00853   * @}
00854   */
00855 
00856 /** @defgroup TSC_Exported_Functions_Group3 Peripheral Control functions
00857   *  @brief    Peripheral Control functions
00858   *
00859 @verbatim
00860  ===============================================================================
00861              ##### Peripheral Control functions #####
00862  ===============================================================================
00863     [..]  This section provides functions allowing to:
00864       (+) Configure TSC IOs
00865       (+) Discharge TSC IOs
00866 @endverbatim
00867   * @{
00868   */
00869 
00870 /**
00871   * @brief  Configure TSC IOs.
00872   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
00873   *         the configuration information for the specified TSC.
00874   * @param  config Pointer to the configuration structure.
00875   * @retval HAL status
00876   */
00877 HAL_StatusTypeDef HAL_TSC_IOConfig(TSC_HandleTypeDef *htsc, TSC_IOConfigTypeDef *config)
00878 {
00879   /* Check the parameters */
00880   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
00881   assert_param(IS_TSC_GROUP(config->ChannelIOs));
00882   assert_param(IS_TSC_GROUP(config->ShieldIOs));
00883   assert_param(IS_TSC_GROUP(config->SamplingIOs));
00884 
00885   /* Process locked */
00886   __HAL_LOCK(htsc);
00887 
00888   /* Stop acquisition */
00889   __HAL_TSC_STOP_ACQ(htsc);
00890 
00891   /* Disable Schmitt trigger hysteresis on all used TSC IOs */
00892   htsc->Instance->IOHCR = (~(config->ChannelIOs | config->ShieldIOs | config->SamplingIOs));
00893 
00894   /* Set channel and shield IOs */
00895   htsc->Instance->IOCCR = (config->ChannelIOs | config->ShieldIOs);
00896 
00897   /* Set sampling IOs */
00898   htsc->Instance->IOSCR = config->SamplingIOs;
00899 
00900   /* Set groups to be acquired */
00901   htsc->Instance->IOGCSR = TSC_extract_groups(config->ChannelIOs);
00902 
00903   /* Process unlocked */
00904   __HAL_UNLOCK(htsc);
00905 
00906   /* Return function status */
00907   return HAL_OK;
00908 }
00909 
00910 /**
00911   * @brief  Discharge TSC IOs.
00912   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
00913   *         the configuration information for the specified TSC.
00914   * @param  choice This parameter can be set to ENABLE or DISABLE.
00915   * @retval HAL status
00916   */
00917 HAL_StatusTypeDef HAL_TSC_IODischarge(TSC_HandleTypeDef *htsc, FunctionalState choice)
00918 {
00919   /* Check the parameters */
00920   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
00921 
00922   /* Process locked */
00923   __HAL_LOCK(htsc);
00924 
00925   if (choice == ENABLE)
00926   {
00927     __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
00928   }
00929   else
00930   {
00931     __HAL_TSC_SET_IODEF_INFLOAT(htsc);
00932   }
00933 
00934   /* Process unlocked */
00935   __HAL_UNLOCK(htsc);
00936 
00937   /* Return the group acquisition counter */
00938   return HAL_OK;
00939 }
00940 
00941 /**
00942   * @}
00943   */
00944 
00945 /** @defgroup TSC_Exported_Functions_Group4 Peripheral State and Errors functions
00946   *  @brief   Peripheral State and Errors functions
00947   *
00948 @verbatim
00949  ===============================================================================
00950             ##### State and Errors functions #####
00951  ===============================================================================
00952     [..]
00953     This subsection provides functions allowing to
00954       (+) Get TSC state.
00955 
00956 @endverbatim
00957   * @{
00958   */
00959 
00960 /**
00961   * @brief  Return the TSC handle state.
00962   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
00963   *         the configuration information for the specified TSC.
00964   * @retval HAL state
00965   */
00966 HAL_TSC_StateTypeDef HAL_TSC_GetState(TSC_HandleTypeDef *htsc)
00967 {
00968   /* Check the parameters */
00969   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
00970 
00971   if (htsc->State == HAL_TSC_STATE_BUSY)
00972   {
00973     /* Check end of acquisition flag */
00974     if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_EOA) != RESET)
00975     {
00976       /* Check max count error flag */
00977       if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_MCE) != RESET)
00978       {
00979         /* Change TSC state */
00980         htsc->State = HAL_TSC_STATE_ERROR;
00981       }
00982       else
00983       {
00984         /* Change TSC state */
00985         htsc->State = HAL_TSC_STATE_READY;
00986       }
00987     }
00988   }
00989 
00990   /* Return TSC state */
00991   return htsc->State;
00992 }
00993 
00994 /**
00995   * @}
00996   */
00997 
00998 /** @defgroup TSC_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
00999   * @{
01000   */
01001 
01002 /**
01003   * @brief  Handle TSC interrupt request.
01004   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
01005   *         the configuration information for the specified TSC.
01006   * @retval None
01007   */
01008 void HAL_TSC_IRQHandler(TSC_HandleTypeDef *htsc)
01009 {
01010   /* Check the parameters */
01011   assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
01012 
01013   /* Check if the end of acquisition occurred */
01014   if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_EOA) != RESET)
01015   {
01016     /* Clear EOA flag */
01017     __HAL_TSC_CLEAR_FLAG(htsc, TSC_FLAG_EOA);
01018   }
01019 
01020   /* Check if max count error occurred */
01021   if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_MCE) != RESET)
01022   {
01023     /* Clear MCE flag */
01024     __HAL_TSC_CLEAR_FLAG(htsc, TSC_FLAG_MCE);
01025     /* Change TSC state */
01026     htsc->State = HAL_TSC_STATE_ERROR;
01027 #if (USE_HAL_TSC_REGISTER_CALLBACKS == 1)
01028     htsc->ErrorCallback(htsc);
01029 #else
01030     /* Conversion completed callback */
01031     HAL_TSC_ErrorCallback(htsc);
01032 #endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
01033   }
01034   else
01035   {
01036     /* Change TSC state */
01037     htsc->State = HAL_TSC_STATE_READY;
01038 #if (USE_HAL_TSC_REGISTER_CALLBACKS == 1)
01039     htsc->ConvCpltCallback(htsc);
01040 #else
01041     /* Conversion completed callback */
01042     HAL_TSC_ConvCpltCallback(htsc);
01043 #endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
01044   }
01045 }
01046 
01047 /**
01048   * @brief  Acquisition completed callback in non-blocking mode.
01049   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
01050   *         the configuration information for the specified TSC.
01051   * @retval None
01052   */
01053 __weak void HAL_TSC_ConvCpltCallback(TSC_HandleTypeDef *htsc)
01054 {
01055   /* Prevent unused argument(s) compilation warning */
01056   UNUSED(htsc);
01057 
01058   /* NOTE : This function should not be modified, when the callback is needed,
01059             the HAL_TSC_ConvCpltCallback could be implemented in the user file.
01060    */
01061 }
01062 
01063 /**
01064   * @brief  Error callback in non-blocking mode.
01065   * @param  htsc Pointer to a TSC_HandleTypeDef structure that contains
01066   *         the configuration information for the specified TSC.
01067   * @retval None
01068   */
01069 __weak void HAL_TSC_ErrorCallback(TSC_HandleTypeDef *htsc)
01070 {
01071   /* Prevent unused argument(s) compilation warning */
01072   UNUSED(htsc);
01073 
01074   /* NOTE : This function should not be modified, when the callback is needed,
01075             the HAL_TSC_ErrorCallback could be implemented in the user file.
01076    */
01077 }
01078 
01079 /**
01080   * @}
01081   */
01082 
01083 /**
01084   * @}
01085   */
01086 
01087 /* Private functions ---------------------------------------------------------*/
01088 /** @defgroup TSC_Private_Functions TSC Private Functions
01089   * @{
01090   */
01091 
01092 /**
01093   * @brief  Utility function used to set the acquired groups mask.
01094   * @param  iomask Channels IOs mask
01095   * @retval Acquired groups mask
01096   */
01097 static uint32_t TSC_extract_groups(uint32_t iomask)
01098 {
01099   uint32_t groups = 0UL;
01100   uint32_t idx;
01101 
01102   for (idx = 0UL; idx < (uint32_t)TSC_NB_OF_GROUPS; idx++)
01103   {
01104     if ((iomask & (0x0FUL << (idx * 4UL))) != 0UL)
01105     {
01106       groups |= (1UL << idx);
01107     }
01108   }
01109 
01110   return groups;
01111 }
01112 
01113 /**
01114   * @}
01115   */
01116 
01117 #endif /* HAL_TSC_MODULE_ENABLED */
01118 
01119 /**
01120   * @}
01121   */
01122 
01123 /**
01124   * @}
01125   */
01126