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