2 ******************************************************************************
3 * @file stm32f0xx_hal_tsc.c
4 * @author MCD Application Team
6 * @date 11-December-2014
7 * @brief This file provides firmware functions to manage the following
8 * functionalities of the Touch Sensing Controller (TSC) peripheral:
9 * + Initialization and DeInitialization
10 * + Channel IOs, Shield IOs and Sampling IOs configuration
11 * + Start and Stop an acquisition
12 * + Read acquisition result
13 * + Interrupts and flags management
16 ================================================================================
17 ##### TSC specific features #####
18 ================================================================================
20 (#) Proven and robust surface charge transfer acquisition principle
22 (#) Supports up to 3 capacitive sensing channels per group
24 (#) Capacitive sensing channels can be acquired in parallel offering a very good
27 (#) Spread spectrum feature to improve system robustness in noisy environments
29 (#) Full hardware management of the charge transfer acquisition sequence
31 (#) Programmable charge transfer frequency
33 (#) Programmable sampling capacitor I/O pin
35 (#) Programmable channel I/O pin
37 (#) Programmable max count value to avoid long acquisition when a channel is faulty
39 (#) Dedicated end of acquisition and max count error flags with interrupt capability
41 (#) One sampling capacitor for up to 3 capacitive sensing channels to reduce the system
44 (#) Compatible with proximity, touchkey, linear and rotary touch sensor implementation
47 ##### How to use this driver #####
48 ================================================================================
50 (#) Enable the TSC interface clock using __TSC_CLK_ENABLE() macro.
52 (#) GPIO pins configuration
53 (++) Enable the clock for the TSC GPIOs using __GPIOx_CLK_ENABLE() macro.
54 (++) Configure the TSC pins used as sampling IOs in alternate function output Open-Drain mode,
55 and TSC pins used as channel/shield IOs in alternate function output Push-Pull mode
56 using HAL_GPIO_Init() function.
57 (++) Configure the alternate function on all the TSC pins using HAL_xxxx() function.
59 (#) Interrupts configuration
60 (++) Configure the NVIC (if the interrupt model is used) using HAL_xxx() function.
63 (++) Configure all TSC parameters and used TSC IOs using HAL_TSC_Init() function.
65 *** Acquisition sequence ***
66 ===================================
68 (+) Discharge all IOs using HAL_TSC_IODischarge() function.
69 (+) Wait a certain time allowing a good discharge of all capacitors. This delay depends
70 of the sampling capacitor and electrodes design.
71 (+) Select the channel IOs to be acquired using HAL_TSC_IOConfig() function.
72 (+) Launch the acquisition using either HAL_TSC_Start() or HAL_TSC_Start_IT() function.
73 If the synchronized mode is selected, the acquisition will start as soon as the signal
74 is received on the synchro pin.
75 (+) Wait the end of acquisition using either HAL_TSC_PollForAcquisition() or
76 HAL_TSC_GetState() function or using WFI instruction for example.
77 (+) Check the group acquisition status using HAL_TSC_GroupGetStatus() function.
78 (+) Read the acquisition value using HAL_TSC_GroupGetValue() function.
81 ******************************************************************************
84 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
86 * Redistribution and use in source and binary forms, with or without modification,
87 * are permitted provided that the following conditions are met:
88 * 1. Redistributions of source code must retain the above copyright notice,
89 * this list of conditions and the following disclaimer.
90 * 2. Redistributions in binary form must reproduce the above copyright notice,
91 * this list of conditions and the following disclaimer in the documentation
92 * and/or other materials provided with the distribution.
93 * 3. Neither the name of STMicroelectronics nor the names of its contributors
94 * may be used to endorse or promote products derived from this software
95 * without specific prior written permission.
97 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
98 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
99 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
100 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
101 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
102 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
103 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
104 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
105 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
106 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
108 ******************************************************************************
111 /* Includes ------------------------------------------------------------------*/
112 #include "stm32f0xx_hal.h"
114 #ifdef HAL_TSC_MODULE_ENABLED
116 #if defined(STM32F051x8) || defined(STM32F071xB) || defined(STM32F091xC) || \
117 defined(STM32F042x6) || defined(STM32F072xB) || \
118 defined(STM32F048xx) || defined(STM32F058xx) || defined(STM32F078xx) || defined(STM32F098xx)
120 /** @addtogroup STM32F0xx_HAL_Driver
124 /** @defgroup TSC TSC HAL module driver
125 * @brief TSC HAL module driver
129 /* Private typedef -----------------------------------------------------------*/
130 /* Private define ------------------------------------------------------------*/
131 /* Private macro -------------------------------------------------------------*/
132 /* Private variables ---------------------------------------------------------*/
133 /* Private function prototypes -----------------------------------------------*/
134 static uint32_t TSC_extract_groups(uint32_t iomask);
135 /* Exported functions ---------------------------------------------------------*/
137 /** @defgroup TSC_Exported_Functions TSC Exported Functions
141 /** @defgroup TSC_Exported_Functions_Group1 Initialization/de-initialization functions
142 * @brief Initialization and Configuration functions
145 ===============================================================================
146 ##### Initialization and de-initialization functions #####
147 ===============================================================================
148 [..] This section provides functions allowing to:
149 (+) Initialize and configure the TSC.
150 (+) De-initialize the TSC.
156 * @brief Initializes the TSC peripheral according to the specified parameters
157 * in the TSC_InitTypeDef structure.
158 * @param htsc: TSC handle
161 HAL_StatusTypeDef HAL_TSC_Init(TSC_HandleTypeDef* htsc)
163 /* Check TSC handle allocation */
169 /* Check the parameters */
170 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
171 assert_param(IS_TSC_CTPH(htsc->Init.CTPulseHighLength));
172 assert_param(IS_TSC_CTPL(htsc->Init.CTPulseLowLength));
173 assert_param(IS_TSC_SS(htsc->Init.SpreadSpectrum));
174 assert_param(IS_TSC_SSD(htsc->Init.SpreadSpectrumDeviation));
175 assert_param(IS_TSC_SS_PRESC(htsc->Init.SpreadSpectrumPrescaler));
176 assert_param(IS_TSC_PG_PRESC(htsc->Init.PulseGeneratorPrescaler));
177 assert_param(IS_TSC_MCV(htsc->Init.MaxCountValue));
178 assert_param(IS_TSC_IODEF(htsc->Init.IODefaultMode));
179 assert_param(IS_TSC_SYNC_POL(htsc->Init.SynchroPinPolarity));
180 assert_param(IS_TSC_ACQ_MODE(htsc->Init.AcquisitionMode));
181 assert_param(IS_TSC_MCE_IT(htsc->Init.MaxCountInterrupt));
183 /* Initialize the TSC state */
184 htsc->State = HAL_TSC_STATE_BUSY;
186 /* Init the low level hardware : GPIO, CLOCK, CORTEX */
187 HAL_TSC_MspInit(htsc);
189 /*--------------------------------------------------------------------------*/
190 /* Set TSC parameters */
193 htsc->Instance->CR = TSC_CR_TSCE;
195 /* Set all functions */
196 htsc->Instance->CR |= (htsc->Init.CTPulseHighLength |
197 htsc->Init.CTPulseLowLength |
198 (uint32_t)(htsc->Init.SpreadSpectrumDeviation << 17) |
199 htsc->Init.SpreadSpectrumPrescaler |
200 htsc->Init.PulseGeneratorPrescaler |
201 htsc->Init.MaxCountValue |
202 htsc->Init.IODefaultMode |
203 htsc->Init.SynchroPinPolarity |
204 htsc->Init.AcquisitionMode);
206 /* Spread spectrum */
207 if (htsc->Init.SpreadSpectrum == ENABLE)
209 htsc->Instance->CR |= TSC_CR_SSE;
212 /* Disable Schmitt trigger hysteresis on all used TSC IOs */
213 htsc->Instance->IOHCR = (uint32_t)(~(htsc->Init.ChannelIOs | htsc->Init.ShieldIOs | htsc->Init.SamplingIOs));
215 /* Set channel and shield IOs */
216 htsc->Instance->IOCCR = (htsc->Init.ChannelIOs | htsc->Init.ShieldIOs);
218 /* Set sampling IOs */
219 htsc->Instance->IOSCR = htsc->Init.SamplingIOs;
221 /* Set the groups to be acquired */
222 htsc->Instance->IOGCSR = TSC_extract_groups(htsc->Init.ChannelIOs);
224 /* Clear interrupts */
225 htsc->Instance->IER &= (uint32_t)(~(TSC_IT_EOA | TSC_IT_MCE));
228 htsc->Instance->ICR = (TSC_FLAG_EOA | TSC_FLAG_MCE);
230 /*--------------------------------------------------------------------------*/
232 /* Initialize the TSC state */
233 htsc->State = HAL_TSC_STATE_READY;
235 /* Return function status */
240 * @brief Deinitializes the TSC peripheral registers to their default reset values.
241 * @param htsc: TSC handle
244 HAL_StatusTypeDef HAL_TSC_DeInit(TSC_HandleTypeDef* htsc)
246 /* Check TSC handle allocation */
252 /* Check the parameters */
253 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
255 /* Change TSC state */
256 htsc->State = HAL_TSC_STATE_BUSY;
258 /* DeInit the low level hardware */
259 HAL_TSC_MspDeInit(htsc);
261 /* Change TSC state */
262 htsc->State = HAL_TSC_STATE_RESET;
264 /* Process unlocked */
267 /* Return function status */
272 * @brief Initializes the TSC MSP.
273 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
274 * the configuration information for the specified TSC.
277 __weak void HAL_TSC_MspInit(TSC_HandleTypeDef* htsc)
279 /* NOTE : This function should not be modified, when the callback is needed,
280 the HAL_TSC_MspInit could be implemented in the user file.
285 * @brief DeInitializes the TSC MSP.
286 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
287 * the configuration information for the specified TSC.
290 __weak void HAL_TSC_MspDeInit(TSC_HandleTypeDef* htsc)
292 /* NOTE : This function should not be modified, when the callback is needed,
293 the HAL_TSC_MspDeInit could be implemented in the user file.
301 /** @defgroup TSC_Exported_Functions_Group2 IO operation functions
302 * @brief IO operation functions
305 ===============================================================================
306 ##### IO operation functions #####
307 ===============================================================================
308 [..] This section provides functions allowing to:
309 (+) Start acquisition in polling mode.
310 (+) Start acquisition in interrupt mode.
311 (+) Stop conversion in polling mode.
312 (+) Stop conversion in interrupt mode.
313 (+) Get group acquisition status.
314 (+) Get group acquisition value.
320 * @brief Starts the acquisition.
321 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
322 * the configuration information for the specified TSC.
325 HAL_StatusTypeDef HAL_TSC_Start(TSC_HandleTypeDef* htsc)
327 /* Check the parameters */
328 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
333 /* Change TSC state */
334 htsc->State = HAL_TSC_STATE_BUSY;
336 /* Clear interrupts */
337 __HAL_TSC_DISABLE_IT(htsc, (TSC_IT_EOA | TSC_IT_MCE));
340 __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
342 /* Stop discharging the IOs */
343 __HAL_TSC_SET_IODEF_INFLOAT(htsc);
345 /* Launch the acquisition */
346 __HAL_TSC_START_ACQ(htsc);
348 /* Process unlocked */
351 /* Return function status */
356 * @brief Enables the interrupt and starts the acquisition
357 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
358 * the configuration information for the specified TSC.
359 * @retval HAL status.
361 HAL_StatusTypeDef HAL_TSC_Start_IT(TSC_HandleTypeDef* htsc)
363 /* Check the parameters */
364 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
365 assert_param(IS_TSC_MCE_IT(htsc->Init.MaxCountInterrupt));
370 /* Change TSC state */
371 htsc->State = HAL_TSC_STATE_BUSY;
373 /* Enable end of acquisition interrupt */
374 __HAL_TSC_ENABLE_IT(htsc, TSC_IT_EOA);
376 /* Enable max count error interrupt (optional) */
377 if (htsc->Init.MaxCountInterrupt == ENABLE)
379 __HAL_TSC_ENABLE_IT(htsc, TSC_IT_MCE);
383 __HAL_TSC_DISABLE_IT(htsc, TSC_IT_MCE);
387 __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
389 /* Stop discharging the IOs */
390 __HAL_TSC_SET_IODEF_INFLOAT(htsc);
392 /* Launch the acquisition */
393 __HAL_TSC_START_ACQ(htsc);
395 /* Process unlocked */
398 /* Return function status */
403 * @brief Stops the acquisition previously launched in polling mode
404 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
405 * the configuration information for the specified TSC.
408 HAL_StatusTypeDef HAL_TSC_Stop(TSC_HandleTypeDef* htsc)
410 /* Check the parameters */
411 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
416 /* Stop the acquisition */
417 __HAL_TSC_STOP_ACQ(htsc);
420 __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
422 /* Change TSC state */
423 htsc->State = HAL_TSC_STATE_READY;
425 /* Process unlocked */
428 /* Return function status */
433 * @brief Stops the acquisition previously launched in interrupt mode
434 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
435 * the configuration information for the specified TSC.
438 HAL_StatusTypeDef HAL_TSC_Stop_IT(TSC_HandleTypeDef* htsc)
440 /* Check the parameters */
441 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
446 /* Stop the acquisition */
447 __HAL_TSC_STOP_ACQ(htsc);
449 /* Disable interrupts */
450 __HAL_TSC_DISABLE_IT(htsc, (TSC_IT_EOA | TSC_IT_MCE));
453 __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
455 /* Change TSC state */
456 htsc->State = HAL_TSC_STATE_READY;
458 /* Process unlocked */
461 /* Return function status */
466 * @brief Gets the acquisition status for a group
467 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
468 * the configuration information for the specified TSC.
469 * @param gx_index: Index of the group
470 * @retval Group status
472 TSC_GroupStatusTypeDef HAL_TSC_GroupGetStatus(TSC_HandleTypeDef* htsc, uint32_t gx_index)
474 /* Check the parameters */
475 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
476 assert_param(IS_GROUP_INDEX(gx_index));
478 /* Return the group status */
479 return(__HAL_TSC_GET_GROUP_STATUS(htsc, gx_index));
483 * @brief Gets the acquisition measure for a group
484 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
485 * the configuration information for the specified TSC.
486 * @param gx_index: Index of the group
487 * @retval Acquisition measure
489 uint32_t HAL_TSC_GroupGetValue(TSC_HandleTypeDef* htsc, uint32_t gx_index)
491 /* Check the parameters */
492 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
493 assert_param(IS_GROUP_INDEX(gx_index));
495 /* Return the group acquisition counter */
496 return htsc->Instance->IOGXCR[gx_index];
503 /** @defgroup TSC_Exported_Functions_Group3 Peripheral Control functions
504 * @brief Peripheral Control functions
507 ===============================================================================
508 ##### Peripheral Control functions #####
509 ===============================================================================
510 [..] This section provides functions allowing to:
511 (+) Configure TSC IOs
512 (+) Discharge TSC IOs
518 * @brief Configures TSC IOs
519 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
520 * the configuration information for the specified TSC.
521 * @param config: pointer to the configuration structure.
524 HAL_StatusTypeDef HAL_TSC_IOConfig(TSC_HandleTypeDef* htsc, TSC_IOConfigTypeDef* config)
526 /* Check the parameters */
527 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
532 /* Stop acquisition */
533 __HAL_TSC_STOP_ACQ(htsc);
535 /* Disable Schmitt trigger hysteresis on all used TSC IOs */
536 htsc->Instance->IOHCR = (uint32_t)(~(config->ChannelIOs | config->ShieldIOs | config->SamplingIOs));
538 /* Set channel and shield IOs */
539 htsc->Instance->IOCCR = (config->ChannelIOs | config->ShieldIOs);
541 /* Set sampling IOs */
542 htsc->Instance->IOSCR = config->SamplingIOs;
544 /* Set groups to be acquired */
545 htsc->Instance->IOGCSR = TSC_extract_groups(config->ChannelIOs);
547 /* Process unlocked */
550 /* Return function status */
555 * @brief Discharge TSC IOs
556 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
557 * the configuration information for the specified TSC.
558 * @param choice: enable or disable
561 HAL_StatusTypeDef HAL_TSC_IODischarge(TSC_HandleTypeDef* htsc, uint32_t choice)
563 /* Check the parameters */
564 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
569 if (choice == ENABLE)
571 __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
575 __HAL_TSC_SET_IODEF_INFLOAT(htsc);
578 /* Process unlocked */
581 /* Return the group acquisition counter */
589 /** @defgroup TSC_Exported_Functions_Group4 State functions
590 * @brief State functions
593 ===============================================================================
594 ##### State functions #####
595 ===============================================================================
597 This subsection provides functions allowing to
599 (+) Poll for acquisition completed.
600 (+) Handles TSC interrupt request.
607 * @brief Return the TSC state
608 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
609 * the configuration information for the specified TSC.
612 HAL_TSC_StateTypeDef HAL_TSC_GetState(TSC_HandleTypeDef* htsc)
614 /* Check the parameters */
615 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
617 if (htsc->State == HAL_TSC_STATE_BUSY)
619 /* Check end of acquisition flag */
620 if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_EOA) != RESET)
622 /* Check max count error flag */
623 if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_MCE) != RESET)
625 /* Change TSC state */
626 htsc->State = HAL_TSC_STATE_ERROR;
630 /* Change TSC state */
631 htsc->State = HAL_TSC_STATE_READY;
636 /* Return TSC state */
641 * @brief Start acquisition and wait until completion
642 * @note There is no need of a timeout parameter as the max count error is already
643 * managed by the TSC peripheral.
644 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
645 * the configuration information for the specified TSC.
648 HAL_StatusTypeDef HAL_TSC_PollForAcquisition(TSC_HandleTypeDef* htsc)
650 /* Check the parameters */
651 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
656 /* Check end of acquisition */
657 while (HAL_TSC_GetState(htsc) == HAL_TSC_STATE_BUSY)
659 /* The timeout (max count error) is managed by the TSC peripheral itself. */
662 /* Process unlocked */
669 * @brief Handles TSC interrupt request
670 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
671 * the configuration information for the specified TSC.
674 void HAL_TSC_IRQHandler(TSC_HandleTypeDef* htsc)
676 /* Check the parameters */
677 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
679 /* Check if the end of acquisition occured */
680 if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_EOA) != RESET)
683 __HAL_TSC_CLEAR_FLAG(htsc, TSC_FLAG_EOA);
686 /* Check if max count error occured */
687 if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_MCE) != RESET)
690 __HAL_TSC_CLEAR_FLAG(htsc, TSC_FLAG_MCE);
691 /* Change TSC state */
692 htsc->State = HAL_TSC_STATE_ERROR;
693 /* Conversion completed callback */
694 HAL_TSC_ErrorCallback(htsc);
698 /* Change TSC state */
699 htsc->State = HAL_TSC_STATE_READY;
700 /* Conversion completed callback */
701 HAL_TSC_ConvCpltCallback(htsc);
709 /** @defgroup TSC_Exported_Functions_Group5 Callback functions
710 * @brief Callback functions
715 * @brief Acquisition completed callback in non blocking mode
716 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
717 * the configuration information for the specified TSC.
720 __weak void HAL_TSC_ConvCpltCallback(TSC_HandleTypeDef* htsc)
722 /* NOTE : This function should not be modified, when the callback is needed,
723 the HAL_TSC_ConvCpltCallback could be implemented in the user file.
728 * @brief Error callback in non blocking mode
729 * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
730 * the configuration information for the specified TSC.
733 __weak void HAL_TSC_ErrorCallback(TSC_HandleTypeDef* htsc)
735 /* NOTE : This function should not be modified, when the callback is needed,
736 the HAL_TSC_ErrorCallback could be implemented in the user file.
748 /** @defgroup TSC_Private_Functions TSC Private Functions
753 * @brief Utility function used to set the acquired groups mask
754 * @param iomask: Channels IOs mask
755 * @retval Acquired groups mask
757 static uint32_t TSC_extract_groups(uint32_t iomask)
762 for (idx = 0; idx < TSC_NB_OF_GROUPS; idx++)
764 if ((iomask & ((uint32_t)0x0F << (idx * 4))) != RESET)
766 groups |= ((uint32_t)1 << idx);
785 #endif /* defined(STM32F051x8) || defined(STM32F071xB) || defined(STM32F091xC) || */
786 /* defined(STM32F042x6) || defined(STM32F072xB) || */
787 /* defined(STM32F048xx) || defined(STM32F058xx) || defined(STM32F078xx) || defined(STM32F098xx) */
789 #endif /* HAL_TSC_MODULE_ENABLED */
791 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/