Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
stm32l4xx_hal_tsc.c
00001 /** 00002 ****************************************************************************** 00003 * @file stm32l4xx_hal_tsc.c 00004 * @author MCD Application Team 00005 * @version V1.5.1 00006 * @date 31-May-2016 00007 * @brief This file provides firmware functions to manage the following 00008 * functionalities of the Touch Sensing Controller (TSC) peripheral: 00009 * + Initialization and De-initialization 00010 * + Channel IOs, Shield IOs and Sampling IOs configuration 00011 * + Start and Stop an acquisition 00012 * + Read acquisition result 00013 * + Interrupts and flags management 00014 * 00015 @verbatim 00016 ================================================================================ 00017 ##### TSC specific features ##### 00018 ================================================================================ 00019 [..] 00020 (#) Proven and robust surface charge transfer acquisition principle 00021 00022 (#) Supports up to 3 capacitive sensing channels per group 00023 00024 (#) Capacitive sensing channels can be acquired in parallel offering a very good 00025 response time 00026 00027 (#) Spread spectrum feature to improve system robustness in noisy environments 00028 00029 (#) Full hardware management of the charge transfer acquisition sequence 00030 00031 (#) Programmable charge transfer frequency 00032 00033 (#) Programmable sampling capacitor I/O pin 00034 00035 (#) Programmable channel I/O pin 00036 00037 (#) Programmable max count value to avoid long acquisition when a channel is faulty 00038 00039 (#) Dedicated end of acquisition and max count error flags with interrupt capability 00040 00041 (#) One sampling capacitor for up to 3 capacitive sensing channels to reduce the system 00042 components 00043 00044 (#) Compatible with proximity, touchkey, linear and rotary touch sensor implementation 00045 00046 00047 ##### How to use this driver ##### 00048 ================================================================================ 00049 [..] 00050 (#) Enable the TSC interface clock using __HAL_RCC_TSC_CLK_ENABLE() macro. 00051 00052 (#) GPIO pins configuration 00053 (++) Enable the clock for the TSC GPIOs using __HAL_RCC_GPIOx_CLK_ENABLE() macro. 00054 (++) Configure the TSC pins used as sampling IOs in alternate function output Open-Drain mode, 00055 and TSC pins used as channel/shield IOs in alternate function output Push-Pull mode 00056 using HAL_GPIO_Init() function. 00057 00058 (#) Interrupts configuration 00059 (++) Configure the NVIC (if the interrupt model is used) using HAL_NVIC_SetPriority() 00060 and HAL_NVIC_EnableIRQ() and function. 00061 00062 (#) TSC configuration 00063 (++) Configure all TSC parameters and used TSC IOs using HAL_TSC_Init() function. 00064 00065 [..] TSC peripheral alternate functions are mapped on AF9. 00066 00067 *** Acquisition sequence *** 00068 =================================== 00069 [..] 00070 (+) Discharge all IOs using HAL_TSC_IODischarge() function. 00071 (+) Wait a certain time allowing a good discharge of all capacitors. This delay depends 00072 of the sampling capacitor and electrodes design. 00073 (+) Select the channel IOs to be acquired using HAL_TSC_IOConfig() function. 00074 (+) Launch the acquisition using either HAL_TSC_Start() or HAL_TSC_Start_IT() function. 00075 If the synchronized mode is selected, the acquisition will start as soon as the signal 00076 is received on the synchro pin. 00077 (+) Wait the end of acquisition using either HAL_TSC_PollForAcquisition() or 00078 HAL_TSC_GetState() function or using WFI instruction for example. 00079 (+) Check the group acquisition status using HAL_TSC_GroupGetStatus() function. 00080 (+) Read the acquisition value using HAL_TSC_GroupGetValue() function. 00081 00082 @endverbatim 00083 ****************************************************************************** 00084 00085 Table 1. IOs for the STM32L4xx devices 00086 +--------------------------------+ 00087 | IOs | TSC functions | 00088 |--------------|-----------------| 00089 | PB12 (AF) | TSC_G1_IO1 | 00090 | PB13 (AF) | TSC_G1_IO2 | 00091 | PB14 (AF) | TSC_G1_IO3 | 00092 | PB15 (AF) | TSC_G1_IO4 | 00093 |--------------|-----------------| 00094 | PB4 (AF) | TSC_G2_IO1 | 00095 | PB5 (AF) | TSC_G2_IO2 | 00096 | PB6 (AF) | TSC_G2_IO3 | 00097 | PB7 (AF) | TSC_G2_IO4 | 00098 |--------------|-----------------| 00099 | PA15 (AF) | TSC_G3_IO1 | 00100 | PC10 (AF) | TSC_G3_IO2 | 00101 | PC11 (AF) | TSC_G3_IO3 | 00102 | PC12 (AF) | TSC_G3_IO4 | 00103 |--------------|-----------------| 00104 | PC6 (AF) | TSC_G4_IO1 | 00105 | PC7 (AF) | TSC_G4_IO2 | 00106 | PC8 (AF) | TSC_G4_IO3 | 00107 | PC9 (AF) | TSC_G4_IO4 | 00108 |--------------|-----------------| 00109 | PE10 (AF) | TSC_G5_IO1 | 00110 | PE11 (AF) | TSC_G5_IO2 | 00111 | PE12 (AF) | TSC_G5_IO3 | 00112 | PE13 (AF) | TSC_G5_IO4 | 00113 |--------------|-----------------| 00114 | PD10 (AF) | TSC_G6_IO1 | 00115 | PD11 (AF) | TSC_G6_IO2 | 00116 | PD12 (AF) | TSC_G6_IO3 | 00117 | PD13 (AF) | TSC_G6_IO4 | 00118 |--------------|-----------------| 00119 | PE2 (AF) | TSC_G7_IO1 | 00120 | PE3 (AF) | TSC_G7_IO2 | 00121 | PE4 (AF) | TSC_G7_IO3 | 00122 | PE5 (AF) | TSC_G7_IO4 | 00123 |--------------|-----------------| 00124 | PF14 (AF) | TSC_G8_IO1 | 00125 | PF15 (AF) | TSC_G8_IO2 | 00126 | PG0 (AF) | TSC_G8_IO3 | 00127 | PG1 (AF) | TSC_G8_IO4 | 00128 |--------------|-----------------| 00129 | PB10 (AF) | TSC_SYNC | 00130 | PD2 (AF) | | 00131 +--------------------------------+ 00132 00133 ****************************************************************************** 00134 * @attention 00135 * 00136 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> 00137 * 00138 * Redistribution and use in source and binary forms, with or without modification, 00139 * are permitted provided that the following conditions are met: 00140 * 1. Redistributions of source code must retain the above copyright notice, 00141 * this list of conditions and the following disclaimer. 00142 * 2. Redistributions in binary form must reproduce the above copyright notice, 00143 * this list of conditions and the following disclaimer in the documentation 00144 * and/or other materials provided with the distribution. 00145 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00146 * may be used to endorse or promote products derived from this software 00147 * without specific prior written permission. 00148 * 00149 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00150 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00151 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00152 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00153 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00154 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00155 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00156 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00157 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00158 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00159 * 00160 ****************************************************************************** 00161 */ 00162 00163 /* Includes ------------------------------------------------------------------*/ 00164 #include "stm32l4xx_hal.h" 00165 00166 /** @addtogroup STM32L4xx_HAL_Driver 00167 * @{ 00168 */ 00169 00170 /** @defgroup TSC TSC 00171 * @brief HAL TSC module driver 00172 * @{ 00173 */ 00174 00175 #ifdef HAL_TSC_MODULE_ENABLED 00176 00177 /* Private typedef -----------------------------------------------------------*/ 00178 /* Private define ------------------------------------------------------------*/ 00179 /* Private macro -------------------------------------------------------------*/ 00180 /* Private variables ---------------------------------------------------------*/ 00181 /* Private function prototypes -----------------------------------------------*/ 00182 static uint32_t TSC_extract_groups(uint32_t iomask); 00183 00184 /* Exported functions --------------------------------------------------------*/ 00185 00186 /** @defgroup TSC_Exported_Functions Exported Functions 00187 * @{ 00188 */ 00189 00190 /** @defgroup TSC_Exported_Functions_Group1 Initialization and de-initialization functions 00191 * @brief Initialization and Configuration functions 00192 * 00193 @verbatim 00194 =============================================================================== 00195 ##### Initialization and de-initialization functions ##### 00196 =============================================================================== 00197 [..] This section provides functions allowing to: 00198 (+) Initialize and configure the TSC. 00199 (+) De-initialize the TSC. 00200 @endverbatim 00201 * @{ 00202 */ 00203 00204 /** 00205 * @brief Initialize the TSC peripheral according to the specified parameters 00206 * in the TSC_InitTypeDef structure and initialize the associated handle. 00207 * @param htsc: TSC handle 00208 * @retval HAL status 00209 */ 00210 HAL_StatusTypeDef HAL_TSC_Init(TSC_HandleTypeDef* htsc) 00211 { 00212 /* Check TSC handle allocation */ 00213 if (htsc == NULL) 00214 { 00215 return HAL_ERROR; 00216 } 00217 00218 /* Check the parameters */ 00219 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance)); 00220 assert_param(IS_TSC_CTPH(htsc->Init.CTPulseHighLength)); 00221 assert_param(IS_TSC_CTPL(htsc->Init.CTPulseLowLength)); 00222 assert_param(IS_TSC_SS(htsc->Init.SpreadSpectrum)); 00223 assert_param(IS_TSC_SSD(htsc->Init.SpreadSpectrumDeviation)); 00224 assert_param(IS_TSC_SS_PRESC(htsc->Init.SpreadSpectrumPrescaler)); 00225 assert_param(IS_TSC_PG_PRESC(htsc->Init.PulseGeneratorPrescaler)); 00226 assert_param(IS_TSC_MCV(htsc->Init.MaxCountValue)); 00227 assert_param(IS_TSC_IODEF(htsc->Init.IODefaultMode)); 00228 assert_param(IS_TSC_SYNC_POL(htsc->Init.SynchroPinPolarity)); 00229 assert_param(IS_TSC_ACQ_MODE(htsc->Init.AcquisitionMode)); 00230 assert_param(IS_TSC_MCE_IT(htsc->Init.MaxCountInterrupt)); 00231 00232 if(htsc->State == HAL_TSC_STATE_RESET) 00233 { 00234 /* Allocate lock resource and initialize it */ 00235 htsc->Lock = HAL_UNLOCKED; 00236 } 00237 00238 /* Initialize the TSC state */ 00239 htsc->State = HAL_TSC_STATE_BUSY; 00240 00241 /* Init the low level hardware : GPIO, CLOCK, CORTEX */ 00242 HAL_TSC_MspInit(htsc); 00243 00244 /*--------------------------------------------------------------------------*/ 00245 /* Set TSC parameters */ 00246 00247 /* Enable TSC */ 00248 htsc->Instance->CR = TSC_CR_TSCE; 00249 00250 /* Set all functions */ 00251 htsc->Instance->CR |= (htsc->Init.CTPulseHighLength | 00252 htsc->Init.CTPulseLowLength | 00253 (uint32_t)(htsc->Init.SpreadSpectrumDeviation << 17) | 00254 htsc->Init.SpreadSpectrumPrescaler | 00255 htsc->Init.PulseGeneratorPrescaler | 00256 htsc->Init.MaxCountValue | 00257 htsc->Init.SynchroPinPolarity | 00258 htsc->Init.AcquisitionMode); 00259 00260 /* Spread spectrum */ 00261 if (htsc->Init.SpreadSpectrum == ENABLE) 00262 { 00263 htsc->Instance->CR |= TSC_CR_SSE; 00264 } 00265 00266 /* Disable Schmitt trigger hysteresis on all used TSC IOs */ 00267 htsc->Instance->IOHCR = (uint32_t)(~(htsc->Init.ChannelIOs | htsc->Init.ShieldIOs | htsc->Init.SamplingIOs)); 00268 00269 /* Set channel and shield IOs */ 00270 htsc->Instance->IOCCR = (htsc->Init.ChannelIOs | htsc->Init.ShieldIOs); 00271 00272 /* Set sampling IOs */ 00273 htsc->Instance->IOSCR = htsc->Init.SamplingIOs; 00274 00275 /* Set the groups to be acquired */ 00276 htsc->Instance->IOGCSR = TSC_extract_groups(htsc->Init.ChannelIOs); 00277 00278 /* Disable interrupts */ 00279 htsc->Instance->IER &= (uint32_t)(~(TSC_IT_EOA | TSC_IT_MCE)); 00280 00281 /* Clear flags */ 00282 htsc->Instance->ICR = (TSC_FLAG_EOA | TSC_FLAG_MCE); 00283 00284 /*--------------------------------------------------------------------------*/ 00285 00286 /* Initialize the TSC state */ 00287 htsc->State = HAL_TSC_STATE_READY; 00288 00289 /* Return function status */ 00290 return HAL_OK; 00291 } 00292 00293 /** 00294 * @brief Deinitialize the TSC peripheral registers to their default reset values. 00295 * @param htsc: TSC handle 00296 * @retval HAL status 00297 */ 00298 HAL_StatusTypeDef HAL_TSC_DeInit(TSC_HandleTypeDef* htsc) 00299 { 00300 /* Check TSC handle allocation */ 00301 if (htsc == NULL) 00302 { 00303 return HAL_ERROR; 00304 } 00305 00306 /* Check the parameters */ 00307 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance)); 00308 00309 /* Change TSC state */ 00310 htsc->State = HAL_TSC_STATE_BUSY; 00311 00312 /* DeInit the low level hardware */ 00313 HAL_TSC_MspDeInit(htsc); 00314 00315 /* Change TSC state */ 00316 htsc->State = HAL_TSC_STATE_RESET; 00317 00318 /* Process unlocked */ 00319 __HAL_UNLOCK(htsc); 00320 00321 /* Return function status */ 00322 return HAL_OK; 00323 } 00324 00325 /** 00326 * @brief Initialize the TSC MSP. 00327 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains 00328 * the configuration information for the specified TSC. 00329 * @retval None 00330 */ 00331 __weak void HAL_TSC_MspInit(TSC_HandleTypeDef* htsc) 00332 { 00333 /* Prevent unused argument(s) compilation warning */ 00334 UNUSED(htsc); 00335 00336 /* NOTE : This function should not be modified, when the callback is needed, 00337 the HAL_TSC_MspInit could be implemented in the user file. 00338 */ 00339 } 00340 00341 /** 00342 * @brief DeInitialize the TSC MSP. 00343 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains 00344 * the configuration information for the specified TSC. 00345 * @retval None 00346 */ 00347 __weak void HAL_TSC_MspDeInit(TSC_HandleTypeDef* htsc) 00348 { 00349 /* Prevent unused argument(s) compilation warning */ 00350 UNUSED(htsc); 00351 00352 /* NOTE : This function should not be modified, when the callback is needed, 00353 the HAL_TSC_MspDeInit could be implemented in the user file. 00354 */ 00355 } 00356 00357 /** 00358 * @} 00359 */ 00360 00361 /** @defgroup TSC_Exported_Functions_Group2 Input and Output operation functions 00362 * @brief Input and Output operation functions 00363 * 00364 @verbatim 00365 =============================================================================== 00366 ##### IO Operation functions ##### 00367 =============================================================================== 00368 [..] This section provides functions allowing to: 00369 (+) Start acquisition in polling mode. 00370 (+) Start acquisition in interrupt mode. 00371 (+) Stop conversion in polling mode. 00372 (+) Stop conversion in interrupt mode. 00373 (+) Poll for acquisition completed. 00374 (+) Get group acquisition status. 00375 (+) Get group acquisition value. 00376 @endverbatim 00377 * @{ 00378 */ 00379 00380 /** 00381 * @brief Start the acquisition. 00382 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains 00383 * the configuration information for the specified TSC. 00384 * @retval HAL status 00385 */ 00386 HAL_StatusTypeDef HAL_TSC_Start(TSC_HandleTypeDef* htsc) 00387 { 00388 /* Check the parameters */ 00389 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance)); 00390 00391 /* Process locked */ 00392 __HAL_LOCK(htsc); 00393 00394 /* Change TSC state */ 00395 htsc->State = HAL_TSC_STATE_BUSY; 00396 00397 /* Clear interrupts */ 00398 __HAL_TSC_DISABLE_IT(htsc, (TSC_IT_EOA | TSC_IT_MCE)); 00399 00400 /* Clear flags */ 00401 __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE)); 00402 00403 /* Set touch sensing IOs not acquired to the specified IODefaultMode */ 00404 if (htsc->Init.IODefaultMode == TSC_IODEF_OUT_PP_LOW) 00405 { 00406 __HAL_TSC_SET_IODEF_OUTPPLOW(htsc); 00407 } 00408 else 00409 { 00410 __HAL_TSC_SET_IODEF_INFLOAT(htsc); 00411 } 00412 00413 /* Launch the acquisition */ 00414 __HAL_TSC_START_ACQ(htsc); 00415 00416 /* Process unlocked */ 00417 __HAL_UNLOCK(htsc); 00418 00419 /* Return function status */ 00420 return HAL_OK; 00421 } 00422 00423 /** 00424 * @brief Start the acquisition in interrupt mode. 00425 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains 00426 * the configuration information for the specified TSC. 00427 * @retval HAL status. 00428 */ 00429 HAL_StatusTypeDef HAL_TSC_Start_IT(TSC_HandleTypeDef* htsc) 00430 { 00431 /* Check the parameters */ 00432 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance)); 00433 assert_param(IS_TSC_MCE_IT(htsc->Init.MaxCountInterrupt)); 00434 00435 /* Process locked */ 00436 __HAL_LOCK(htsc); 00437 00438 /* Change TSC state */ 00439 htsc->State = HAL_TSC_STATE_BUSY; 00440 00441 /* Enable end of acquisition interrupt */ 00442 __HAL_TSC_ENABLE_IT(htsc, TSC_IT_EOA); 00443 00444 /* Enable max count error interrupt (optional) */ 00445 if (htsc->Init.MaxCountInterrupt == ENABLE) 00446 { 00447 __HAL_TSC_ENABLE_IT(htsc, TSC_IT_MCE); 00448 } 00449 else 00450 { 00451 __HAL_TSC_DISABLE_IT(htsc, TSC_IT_MCE); 00452 } 00453 00454 /* Clear flags */ 00455 __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE)); 00456 00457 /* Set touch sensing IOs not acquired to the specified IODefaultMode */ 00458 if (htsc->Init.IODefaultMode == TSC_IODEF_OUT_PP_LOW) 00459 { 00460 __HAL_TSC_SET_IODEF_OUTPPLOW(htsc); 00461 } 00462 else 00463 { 00464 __HAL_TSC_SET_IODEF_INFLOAT(htsc); 00465 } 00466 00467 /* Launch the acquisition */ 00468 __HAL_TSC_START_ACQ(htsc); 00469 00470 /* Process unlocked */ 00471 __HAL_UNLOCK(htsc); 00472 00473 /* Return function status */ 00474 return HAL_OK; 00475 } 00476 00477 /** 00478 * @brief Stop the acquisition previously launched in polling mode. 00479 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains 00480 * the configuration information for the specified TSC. 00481 * @retval HAL status 00482 */ 00483 HAL_StatusTypeDef HAL_TSC_Stop(TSC_HandleTypeDef* htsc) 00484 { 00485 /* Check the parameters */ 00486 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance)); 00487 00488 /* Process locked */ 00489 __HAL_LOCK(htsc); 00490 00491 /* Stop the acquisition */ 00492 __HAL_TSC_STOP_ACQ(htsc); 00493 00494 /* Set touch sensing IOs in low power mode (output push-pull) */ 00495 __HAL_TSC_SET_IODEF_OUTPPLOW(htsc); 00496 00497 /* Clear flags */ 00498 __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE)); 00499 00500 /* Change TSC state */ 00501 htsc->State = HAL_TSC_STATE_READY; 00502 00503 /* Process unlocked */ 00504 __HAL_UNLOCK(htsc); 00505 00506 /* Return function status */ 00507 return HAL_OK; 00508 } 00509 00510 /** 00511 * @brief Stop the acquisition previously launched in interrupt mode. 00512 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains 00513 * the configuration information for the specified TSC. 00514 * @retval HAL status 00515 */ 00516 HAL_StatusTypeDef HAL_TSC_Stop_IT(TSC_HandleTypeDef* htsc) 00517 { 00518 /* Check the parameters */ 00519 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance)); 00520 00521 /* Process locked */ 00522 __HAL_LOCK(htsc); 00523 00524 /* Stop the acquisition */ 00525 __HAL_TSC_STOP_ACQ(htsc); 00526 00527 /* Set touch sensing IOs in low power mode (output push-pull) */ 00528 __HAL_TSC_SET_IODEF_OUTPPLOW(htsc); 00529 00530 /* Disable interrupts */ 00531 __HAL_TSC_DISABLE_IT(htsc, (TSC_IT_EOA | TSC_IT_MCE)); 00532 00533 /* Clear flags */ 00534 __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE)); 00535 00536 /* Change TSC state */ 00537 htsc->State = HAL_TSC_STATE_READY; 00538 00539 /* Process unlocked */ 00540 __HAL_UNLOCK(htsc); 00541 00542 /* Return function status */ 00543 return HAL_OK; 00544 } 00545 00546 /** 00547 * @brief Start acquisition and wait until completion. 00548 * @note There is no need of a timeout parameter as the max count error is already 00549 * managed by the TSC peripheral. 00550 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains 00551 * the configuration information for the specified TSC. 00552 * @retval HAL state 00553 */ 00554 HAL_StatusTypeDef HAL_TSC_PollForAcquisition(TSC_HandleTypeDef* htsc) 00555 { 00556 /* Check the parameters */ 00557 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance)); 00558 00559 /* Process locked */ 00560 __HAL_LOCK(htsc); 00561 00562 /* Check end of acquisition */ 00563 while (HAL_TSC_GetState(htsc) == HAL_TSC_STATE_BUSY) 00564 { 00565 /* The timeout (max count error) is managed by the TSC peripheral itself. */ 00566 } 00567 00568 /* Process unlocked */ 00569 __HAL_UNLOCK(htsc); 00570 00571 return HAL_OK; 00572 } 00573 00574 /** 00575 * @brief Get the acquisition status for a group. 00576 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains 00577 * the configuration information for the specified TSC. 00578 * @param gx_index: Index of the group 00579 * @retval Group status 00580 */ 00581 TSC_GroupStatusTypeDef HAL_TSC_GroupGetStatus(TSC_HandleTypeDef* htsc, uint32_t gx_index) 00582 { 00583 /* Check the parameters */ 00584 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance)); 00585 assert_param(IS_TSC_GROUP_INDEX(gx_index)); 00586 00587 /* Return the group status */ 00588 return(__HAL_TSC_GET_GROUP_STATUS(htsc, gx_index)); 00589 } 00590 00591 /** 00592 * @brief Get the acquisition measure for a group. 00593 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains 00594 * the configuration information for the specified TSC. 00595 * @param gx_index: Index of the group 00596 * @retval Acquisition measure 00597 */ 00598 uint32_t HAL_TSC_GroupGetValue(TSC_HandleTypeDef* htsc, uint32_t gx_index) 00599 { 00600 /* Check the parameters */ 00601 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance)); 00602 assert_param(IS_TSC_GROUP_INDEX(gx_index)); 00603 00604 /* Return the group acquisition counter */ 00605 return htsc->Instance->IOGXCR[gx_index]; 00606 } 00607 00608 /** 00609 * @} 00610 */ 00611 00612 /** @defgroup TSC_Exported_Functions_Group3 Peripheral Control functions 00613 * @brief Peripheral Control functions 00614 * 00615 @verbatim 00616 =============================================================================== 00617 ##### Peripheral Control functions ##### 00618 =============================================================================== 00619 [..] This section provides functions allowing to: 00620 (+) Configure TSC IOs 00621 (+) Discharge TSC IOs 00622 @endverbatim 00623 * @{ 00624 */ 00625 00626 /** 00627 * @brief Configure TSC IOs. 00628 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains 00629 * the configuration information for the specified TSC. 00630 * @param config: pointer to the configuration structure. 00631 * @retval HAL status 00632 */ 00633 HAL_StatusTypeDef HAL_TSC_IOConfig(TSC_HandleTypeDef* htsc, TSC_IOConfigTypeDef* config) 00634 { 00635 /* Check the parameters */ 00636 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance)); 00637 00638 /* Process locked */ 00639 __HAL_LOCK(htsc); 00640 00641 /* Stop acquisition */ 00642 __HAL_TSC_STOP_ACQ(htsc); 00643 00644 /* Disable Schmitt trigger hysteresis on all used TSC IOs */ 00645 htsc->Instance->IOHCR = (uint32_t)(~(config->ChannelIOs | config->ShieldIOs | config->SamplingIOs)); 00646 00647 /* Set channel and shield IOs */ 00648 htsc->Instance->IOCCR = (config->ChannelIOs | config->ShieldIOs); 00649 00650 /* Set sampling IOs */ 00651 htsc->Instance->IOSCR = config->SamplingIOs; 00652 00653 /* Set groups to be acquired */ 00654 htsc->Instance->IOGCSR = TSC_extract_groups(config->ChannelIOs); 00655 00656 /* Process unlocked */ 00657 __HAL_UNLOCK(htsc); 00658 00659 /* Return function status */ 00660 return HAL_OK; 00661 } 00662 00663 /** 00664 * @brief Discharge TSC IOs. 00665 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains 00666 * the configuration information for the specified TSC. 00667 * @param choice: enable or disable 00668 * @retval HAL status 00669 */ 00670 HAL_StatusTypeDef HAL_TSC_IODischarge(TSC_HandleTypeDef* htsc, uint32_t choice) 00671 { 00672 /* Check the parameters */ 00673 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance)); 00674 00675 /* Process locked */ 00676 __HAL_LOCK(htsc); 00677 00678 if (choice == ENABLE) 00679 { 00680 __HAL_TSC_SET_IODEF_OUTPPLOW(htsc); 00681 } 00682 else 00683 { 00684 __HAL_TSC_SET_IODEF_INFLOAT(htsc); 00685 } 00686 00687 /* Process unlocked */ 00688 __HAL_UNLOCK(htsc); 00689 00690 /* Return the group acquisition counter */ 00691 return HAL_OK; 00692 } 00693 00694 /** 00695 * @} 00696 */ 00697 00698 /** @defgroup TSC_Exported_Functions_Group4 Peripheral State and Errors functions 00699 * @brief Peripheral State and Errors functions 00700 * 00701 @verbatim 00702 =============================================================================== 00703 ##### State and Errors functions ##### 00704 =============================================================================== 00705 [..] 00706 This subsection provides functions allowing to 00707 (+) Get TSC state. 00708 00709 @endverbatim 00710 * @{ 00711 */ 00712 00713 /** 00714 * @brief Return the TSC handle state. 00715 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains 00716 * the configuration information for the specified TSC. 00717 * @retval HAL state 00718 */ 00719 HAL_TSC_StateTypeDef HAL_TSC_GetState(TSC_HandleTypeDef* htsc) 00720 { 00721 /* Check the parameters */ 00722 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance)); 00723 00724 if (htsc->State == HAL_TSC_STATE_BUSY) 00725 { 00726 /* Check end of acquisition flag */ 00727 if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_EOA) != RESET) 00728 { 00729 /* Check max count error flag */ 00730 if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_MCE) != RESET) 00731 { 00732 /* Change TSC state */ 00733 htsc->State = HAL_TSC_STATE_ERROR; 00734 } 00735 else 00736 { 00737 /* Change TSC state */ 00738 htsc->State = HAL_TSC_STATE_READY; 00739 } 00740 } 00741 } 00742 00743 /* Return TSC state */ 00744 return htsc->State; 00745 } 00746 00747 /** 00748 * @} 00749 */ 00750 00751 /** @defgroup TSC_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks 00752 * @{ 00753 */ 00754 00755 /** 00756 * @brief Handle TSC interrupt request. 00757 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains 00758 * the configuration information for the specified TSC. 00759 * @retval None 00760 */ 00761 void HAL_TSC_IRQHandler(TSC_HandleTypeDef* htsc) 00762 { 00763 /* Check the parameters */ 00764 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance)); 00765 00766 /* Check if the end of acquisition occurred */ 00767 if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_EOA) != RESET) 00768 { 00769 /* Clear EOA flag */ 00770 __HAL_TSC_CLEAR_FLAG(htsc, TSC_FLAG_EOA); 00771 } 00772 00773 /* Check if max count error occurred */ 00774 if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_MCE) != RESET) 00775 { 00776 /* Clear MCE flag */ 00777 __HAL_TSC_CLEAR_FLAG(htsc, TSC_FLAG_MCE); 00778 /* Change TSC state */ 00779 htsc->State = HAL_TSC_STATE_ERROR; 00780 /* Conversion completed callback */ 00781 HAL_TSC_ErrorCallback(htsc); 00782 } 00783 else 00784 { 00785 /* Change TSC state */ 00786 htsc->State = HAL_TSC_STATE_READY; 00787 /* Conversion completed callback */ 00788 HAL_TSC_ConvCpltCallback(htsc); 00789 } 00790 } 00791 00792 /** 00793 * @brief Acquisition completed callback in non-blocking mode. 00794 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains 00795 * the configuration information for the specified TSC. 00796 * @retval None 00797 */ 00798 __weak void HAL_TSC_ConvCpltCallback(TSC_HandleTypeDef* htsc) 00799 { 00800 /* Prevent unused argument(s) compilation warning */ 00801 UNUSED(htsc); 00802 00803 /* NOTE : This function should not be modified, when the callback is needed, 00804 the HAL_TSC_ConvCpltCallback could be implemented in the user file. 00805 */ 00806 } 00807 00808 /** 00809 * @brief Error callback in non-blocking mode. 00810 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains 00811 * the configuration information for the specified TSC. 00812 * @retval None 00813 */ 00814 __weak void HAL_TSC_ErrorCallback(TSC_HandleTypeDef* htsc) 00815 { 00816 /* Prevent unused argument(s) compilation warning */ 00817 UNUSED(htsc); 00818 00819 /* NOTE : This function should not be modified, when the callback is needed, 00820 the HAL_TSC_ErrorCallback could be implemented in the user file. 00821 */ 00822 } 00823 00824 /** 00825 * @} 00826 */ 00827 00828 /** 00829 * @} 00830 */ 00831 00832 /* Private functions ---------------------------------------------------------*/ 00833 /** @defgroup TSC_Private_Functions Private Functions 00834 * @{ 00835 */ 00836 00837 /** 00838 * @brief Utility function used to set the acquired groups mask. 00839 * @param iomask: Channels IOs mask 00840 * @retval Acquired groups mask 00841 */ 00842 static uint32_t TSC_extract_groups(uint32_t iomask) 00843 { 00844 uint32_t groups = 0; 00845 uint32_t idx; 00846 00847 for (idx = 0; idx < TSC_NB_OF_GROUPS; idx++) 00848 { 00849 if ((iomask & ((uint32_t)0x0F << (idx * 4))) != RESET) 00850 { 00851 groups |= ((uint32_t)1 << idx); 00852 } 00853 } 00854 00855 return groups; 00856 } 00857 00858 /** 00859 * @} 00860 */ 00861 00862 #endif /* HAL_TSC_MODULE_ENABLED */ 00863 00864 /** 00865 * @} 00866 */ 00867 00868 /** 00869 * @} 00870 */ 00871 00872 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Tue Jul 12 2022 10:59:59 by
