1 /**
2 ******************************************************************************
3 * @file stm32wbxx_hal_tsc.c
4 * @author MCD Application Team
5 * @brief This file provides firmware functions to manage the following
6 * functionalities of the Touch Sensing Controller (TSC) peripheral:
7 * + Initialization and De-initialization
8 * + Channel IOs, Shield IOs and Sampling IOs configuration
9 * + Start and Stop an acquisition
10 * + Read acquisition result
11 * + Interrupts and flags management
12 *
13 ******************************************************************************
14 * @attention
15 *
16 * Copyright (c) 2019 STMicroelectronics.
17 * All rights reserved.
18 *
19 * This software is licensed under terms that can be found in the LICENSE file
20 * in the root directory of this software component.
21 * If no LICENSE file comes with this software, it is provided AS-IS.
22 *
23 ******************************************************************************
24 @verbatim
25 ================================================================================
26 ##### TSC specific features #####
27 ================================================================================
28 [..]
29 (#) Proven and robust surface charge transfer acquisition principle
30
31 (#) Supports up to 3 capacitive sensing channels per group
32
33 (#) Capacitive sensing channels can be acquired in parallel offering a very good
34 response time
35
36 (#) Spread spectrum feature to improve system robustness in noisy environments
37
38 (#) Full hardware management of the charge transfer acquisition sequence
39
40 (#) Programmable charge transfer frequency
41
42 (#) Programmable sampling capacitor I/O pin
43
44 (#) Programmable channel I/O pin
45
46 (#) Programmable max count value to avoid long acquisition when a channel is faulty
47
48 (#) Dedicated end of acquisition and max count error flags with interrupt capability
49
50 (#) One sampling capacitor for up to 3 capacitive sensing channels to reduce the system
51 components
52
53 (#) Compatible with proximity, touchkey, linear and rotary touch sensor implementation
54
55 ##### How to use this driver #####
56 ================================================================================
57 [..]
58 (#) Enable the TSC interface clock using __HAL_RCC_TSC_CLK_ENABLE() macro.
59
60 (#) GPIO pins configuration
61 (++) Enable the clock for the TSC GPIOs using __HAL_RCC_GPIOx_CLK_ENABLE() macro.
62 (++) Configure the TSC pins used as sampling IOs in alternate function output Open-Drain mode,
63 and TSC pins used as channel/shield IOs in alternate function output Push-Pull mode
64 using HAL_GPIO_Init() function.
65
66 (#) Interrupts configuration
67 (++) Configure the NVIC (if the interrupt model is used) using HAL_NVIC_SetPriority()
68 and HAL_NVIC_EnableIRQ() and function.
69
70 (#) TSC configuration
71 (++) Configure all TSC parameters and used TSC IOs using HAL_TSC_Init() function.
72
73 [..] TSC peripheral alternate functions are mapped on AF9.
74
75 *** Acquisition sequence ***
76 ===================================
77 [..]
78 (+) Discharge all IOs using HAL_TSC_IODischarge() function.
79 (+) Wait a certain time allowing a good discharge of all capacitors. This delay depends
80 of the sampling capacitor and electrodes design.
81 (+) Select the channel IOs to be acquired using HAL_TSC_IOConfig() function.
82 (+) Launch the acquisition using either HAL_TSC_Start() or HAL_TSC_Start_IT() function.
83 If the synchronized mode is selected, the acquisition will start as soon as the signal
84 is received on the synchro pin.
85 (+) Wait the end of acquisition using either HAL_TSC_PollForAcquisition() or
86 HAL_TSC_GetState() function or using WFI instruction for example.
87 (+) Check the group acquisition status using HAL_TSC_GroupGetStatus() function.
88 (+) Read the acquisition value using HAL_TSC_GroupGetValue() function.
89
90 *** Callback registration ***
91 =============================================
92
93 [..]
94 The compilation flag USE_HAL_TSC_REGISTER_CALLBACKS when set to 1
95 allows the user to configure dynamically the driver callbacks.
96 Use Functions HAL_TSC_RegisterCallback() to register an interrupt callback.
97
98 [..]
99 Function HAL_TSC_RegisterCallback() allows to register following callbacks:
100 (+) ConvCpltCallback : callback for conversion complete process.
101 (+) ErrorCallback : callback for error detection.
102 (+) MspInitCallback : callback for Msp Init.
103 (+) MspDeInitCallback : callback for Msp DeInit.
104 [..]
105 This function takes as parameters the HAL peripheral handle, the Callback ID
106 and a pointer to the user callback function.
107
108 [..]
109 Use function HAL_TSC_UnRegisterCallback to reset a callback to the default
110 weak function.
111 HAL_TSC_UnRegisterCallback takes as parameters the HAL peripheral handle,
112 and the Callback ID.
113 [..]
114 This function allows to reset following callbacks:
115 (+) ConvCpltCallback : callback for conversion complete process.
116 (+) ErrorCallback : callback for error detection.
117 (+) MspInitCallback : callback for Msp Init.
118 (+) MspDeInitCallback : callback for Msp DeInit.
119
120 [..]
121 By default, after the HAL_TSC_Init() and when the state is HAL_TSC_STATE_RESET
122 all callbacks are set to the corresponding weak functions:
123 examples HAL_TSC_ConvCpltCallback(), HAL_TSC_ErrorCallback().
124 Exception done for MspInit and MspDeInit functions that are
125 reset to the legacy weak functions in the HAL_TSC_Init()/ HAL_TSC_DeInit() only when
126 these callbacks are null (not registered beforehand).
127 If MspInit or MspDeInit are not null, the HAL_TSC_Init()/ HAL_TSC_DeInit()
128 keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
129
130 [..]
131 Callbacks can be registered/unregistered in HAL_TSC_STATE_READY state only.
132 Exception done MspInit/MspDeInit functions that can be registered/unregistered
133 in HAL_TSC_STATE_READY or HAL_TSC_STATE_RESET state,
134 thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
135 Then, the user first registers the MspInit/MspDeInit user callbacks
136 using HAL_TSC_RegisterCallback() before calling HAL_TSC_DeInit()
137 or HAL_TSC_Init() function.
138
139 [..]
140 When the compilation flag USE_HAL_TSC_REGISTER_CALLBACKS is set to 0 or
141 not defined, the callback registration feature is not available and all callbacks
142 are set to the corresponding weak functions.
143
144 @endverbatim
145 ******************************************************************************
146
147 Table 1. IOs for the STM32WBxx devices
148 +--------------------------------+
149 | IOs | TSC functions |
150 |--------------|-----------------|
151 | PB12 (AF) | TSC_G1_IO1 |
152 | PB13 (AF) | TSC_G1_IO2 |
153 | PB14 (AF) | TSC_G1_IO3 |
154 | PB15 (AF) | TSC_G1_IO4 |
155 |--------------|-----------------|
156 | PB4 (AF) | TSC_G2_IO1 |
157 | PB5 (AF) | TSC_G2_IO2 |
158 | PB6 (AF) | TSC_G2_IO3 |
159 | PB7 (AF) | TSC_G2_IO4 |
160 |--------------|-----------------|
161 | PA15 (AF) | TSC_G3_IO1 |
162 | PC10 (AF) | TSC_G3_IO2 |
163 | PC11 (AF) | TSC_G3_IO3 |
164 | PC12 (AF) | TSC_G3_IO4 |
165 |--------------|-----------------|
166 | PC6 (AF) | TSC_G4_IO1 |
167 | PC7 (AF) | TSC_G4_IO2 |
168 | PC8 (AF) | TSC_G4_IO3 |
169 | PC9 (AF) | TSC_G4_IO4 |
170 |--------------|-----------------|
171 | PD4 (AF) | TSC_G5_IO1 |
172 | PD5 (AF) | TSC_G5_IO2 |
173 | PD6 (AF) | TSC_G5_IO3 |
174 | PD7 (AF) | TSC_G5_IO4 |
175 |--------------|-----------------|
176 | PD10 (AF) | TSC_G6_IO1 |
177 | PD11 (AF) | TSC_G6_IO2 |
178 | PD12 (AF) | TSC_G6_IO3 |
179 | PD13 (AF) | TSC_G6_IO4 |
180 |--------------|-----------------|
181 | PE2 (AF) | TSC_G7_IO1 |
182 | PE1 (AF) | TSC_G7_IO2 |
183 | PE0 (AF) | TSC_G7_IO3 |
184 | PB9 (AF) | TSC_G7_IO4 |
185 |--------------|-----------------|
186 | PB10 (AF) | TSC_SYNC |
187 | PD2 (AF) | |
188 +--------------------------------+
189
190
191 */
192
193 /* Includes ------------------------------------------------------------------*/
194 #include "stm32wbxx_hal.h"
195
196 #if defined(TSC)
197 /** @addtogroup STM32WBxx_HAL_Driver
198 * @{
199 */
200
201 /** @defgroup TSC TSC
202 * @brief HAL TSC module driver
203 * @{
204 */
205
206 #ifdef HAL_TSC_MODULE_ENABLED
207
208 /* Private typedef -----------------------------------------------------------*/
209 /* Private define ------------------------------------------------------------*/
210 /* Private macro -------------------------------------------------------------*/
211 /* Private variables ---------------------------------------------------------*/
212 /* Private function prototypes -----------------------------------------------*/
213 static uint32_t TSC_extract_groups(uint32_t iomask);
214
215 /* Exported functions --------------------------------------------------------*/
216
217 /** @defgroup TSC_Exported_Functions TSC Exported Functions
218 * @{
219 */
220
221 /** @defgroup TSC_Exported_Functions_Group1 Initialization and de-initialization functions
222 * @brief Initialization and Configuration functions
223 *
224 @verbatim
225 ===============================================================================
226 ##### Initialization and de-initialization functions #####
227 ===============================================================================
228 [..] This section provides functions allowing to:
229 (+) Initialize and configure the TSC.
230 (+) De-initialize the TSC.
231 @endverbatim
232 * @{
233 */
234
235 /**
236 * @brief Initialize the TSC peripheral according to the specified parameters
237 * in the TSC_InitTypeDef structure and initialize the associated handle.
238 * @param htsc TSC handle
239 * @retval HAL status
240 */
HAL_TSC_Init(TSC_HandleTypeDef * htsc)241 HAL_StatusTypeDef HAL_TSC_Init(TSC_HandleTypeDef *htsc)
242 {
243 /* Check TSC handle allocation */
244 if (htsc == NULL)
245 {
246 return HAL_ERROR;
247 }
248
249 /* Check the parameters */
250 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
251 assert_param(IS_TSC_CTPH(htsc->Init.CTPulseHighLength));
252 assert_param(IS_TSC_CTPL(htsc->Init.CTPulseLowLength));
253 assert_param(IS_TSC_SS(htsc->Init.SpreadSpectrum));
254 assert_param(IS_TSC_SSD(htsc->Init.SpreadSpectrumDeviation));
255 assert_param(IS_TSC_SS_PRESC(htsc->Init.SpreadSpectrumPrescaler));
256 assert_param(IS_TSC_PG_PRESC(htsc->Init.PulseGeneratorPrescaler));
257 assert_param(IS_TSC_PG_PRESC_VS_CTPL(htsc->Init.PulseGeneratorPrescaler, htsc->Init.CTPulseLowLength));
258 assert_param(IS_TSC_MCV(htsc->Init.MaxCountValue));
259 assert_param(IS_TSC_IODEF(htsc->Init.IODefaultMode));
260 assert_param(IS_TSC_SYNC_POL(htsc->Init.SynchroPinPolarity));
261 assert_param(IS_TSC_ACQ_MODE(htsc->Init.AcquisitionMode));
262 assert_param(IS_TSC_MCE_IT(htsc->Init.MaxCountInterrupt));
263 assert_param(IS_TSC_GROUP(htsc->Init.ChannelIOs));
264 assert_param(IS_TSC_GROUP(htsc->Init.ShieldIOs));
265 assert_param(IS_TSC_GROUP(htsc->Init.SamplingIOs));
266
267 if (htsc->State == HAL_TSC_STATE_RESET)
268 {
269 /* Allocate lock resource and initialize it */
270 htsc->Lock = HAL_UNLOCKED;
271
272 #if (USE_HAL_TSC_REGISTER_CALLBACKS == 1)
273 /* Init the TSC Callback settings */
274 htsc->ConvCpltCallback = HAL_TSC_ConvCpltCallback; /* Legacy weak ConvCpltCallback */
275 htsc->ErrorCallback = HAL_TSC_ErrorCallback; /* Legacy weak ErrorCallback */
276
277 if (htsc->MspInitCallback == NULL)
278 {
279 htsc->MspInitCallback = HAL_TSC_MspInit; /* Legacy weak MspInit */
280 }
281
282 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
283 htsc->MspInitCallback(htsc);
284 #else
285 /* Init the low level hardware : GPIO, CLOCK, CORTEX */
286 HAL_TSC_MspInit(htsc);
287 #endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
288 }
289
290 /* Initialize the TSC state */
291 htsc->State = HAL_TSC_STATE_BUSY;
292
293 /*--------------------------------------------------------------------------*/
294 /* Set TSC parameters */
295
296 /* Enable TSC */
297 htsc->Instance->CR = TSC_CR_TSCE;
298
299 /* Set all functions */
300 htsc->Instance->CR |= (htsc->Init.CTPulseHighLength |
301 htsc->Init.CTPulseLowLength |
302 (htsc->Init.SpreadSpectrumDeviation << TSC_CR_SSD_Pos) |
303 htsc->Init.SpreadSpectrumPrescaler |
304 htsc->Init.PulseGeneratorPrescaler |
305 htsc->Init.MaxCountValue |
306 htsc->Init.SynchroPinPolarity |
307 htsc->Init.AcquisitionMode);
308
309 /* Spread spectrum */
310 if (htsc->Init.SpreadSpectrum == ENABLE)
311 {
312 htsc->Instance->CR |= TSC_CR_SSE;
313 }
314
315 /* Disable Schmitt trigger hysteresis on all used TSC IOs */
316 htsc->Instance->IOHCR = (~(htsc->Init.ChannelIOs | htsc->Init.ShieldIOs | htsc->Init.SamplingIOs));
317
318 /* Set channel and shield IOs */
319 htsc->Instance->IOCCR = (htsc->Init.ChannelIOs | htsc->Init.ShieldIOs);
320
321 /* Set sampling IOs */
322 htsc->Instance->IOSCR = htsc->Init.SamplingIOs;
323
324 /* Set the groups to be acquired */
325 htsc->Instance->IOGCSR = TSC_extract_groups(htsc->Init.ChannelIOs);
326
327 /* Disable interrupts */
328 htsc->Instance->IER &= (~(TSC_IT_EOA | TSC_IT_MCE));
329
330 /* Clear flags */
331 htsc->Instance->ICR = (TSC_FLAG_EOA | TSC_FLAG_MCE);
332
333 /*--------------------------------------------------------------------------*/
334
335 /* Initialize the TSC state */
336 htsc->State = HAL_TSC_STATE_READY;
337
338 /* Return function status */
339 return HAL_OK;
340 }
341
342 /**
343 * @brief Deinitialize the TSC peripheral registers to their default reset values.
344 * @param htsc TSC handle
345 * @retval HAL status
346 */
HAL_TSC_DeInit(TSC_HandleTypeDef * htsc)347 HAL_StatusTypeDef HAL_TSC_DeInit(TSC_HandleTypeDef *htsc)
348 {
349 /* Check TSC handle allocation */
350 if (htsc == NULL)
351 {
352 return HAL_ERROR;
353 }
354
355 /* Check the parameters */
356 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
357
358 /* Change TSC state */
359 htsc->State = HAL_TSC_STATE_BUSY;
360
361 #if (USE_HAL_TSC_REGISTER_CALLBACKS == 1)
362 if (htsc->MspDeInitCallback == NULL)
363 {
364 htsc->MspDeInitCallback = HAL_TSC_MspDeInit; /* Legacy weak MspDeInit */
365 }
366
367 /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
368 htsc->MspDeInitCallback(htsc);
369 #else
370 /* DeInit the low level hardware */
371 HAL_TSC_MspDeInit(htsc);
372 #endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
373
374 /* Change TSC state */
375 htsc->State = HAL_TSC_STATE_RESET;
376
377 /* Process unlocked */
378 __HAL_UNLOCK(htsc);
379
380 /* Return function status */
381 return HAL_OK;
382 }
383
384 /**
385 * @brief Initialize the TSC MSP.
386 * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
387 * the configuration information for the specified TSC.
388 * @retval None
389 */
HAL_TSC_MspInit(TSC_HandleTypeDef * htsc)390 __weak void HAL_TSC_MspInit(TSC_HandleTypeDef *htsc)
391 {
392 /* Prevent unused argument(s) compilation warning */
393 UNUSED(htsc);
394
395 /* NOTE : This function should not be modified, when the callback is needed,
396 the HAL_TSC_MspInit could be implemented in the user file.
397 */
398 }
399
400 /**
401 * @brief DeInitialize the TSC MSP.
402 * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
403 * the configuration information for the specified TSC.
404 * @retval None
405 */
HAL_TSC_MspDeInit(TSC_HandleTypeDef * htsc)406 __weak void HAL_TSC_MspDeInit(TSC_HandleTypeDef *htsc)
407 {
408 /* Prevent unused argument(s) compilation warning */
409 UNUSED(htsc);
410
411 /* NOTE : This function should not be modified, when the callback is needed,
412 the HAL_TSC_MspDeInit could be implemented in the user file.
413 */
414 }
415
416 #if (USE_HAL_TSC_REGISTER_CALLBACKS == 1)
417 /**
418 * @brief Register a User TSC Callback
419 * To be used instead of the weak predefined callback
420 * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
421 * the configuration information for the specified TSC.
422 * @param CallbackID ID of the callback to be registered
423 * This parameter can be one of the following values:
424 * @arg @ref HAL_TSC_CONV_COMPLETE_CB_ID Conversion completed callback ID
425 * @arg @ref HAL_TSC_ERROR_CB_ID Error callback ID
426 * @arg @ref HAL_TSC_MSPINIT_CB_ID MspInit callback ID
427 * @arg @ref HAL_TSC_MSPDEINIT_CB_ID MspDeInit callback ID
428 * @param pCallback pointer to the Callback function
429 * @retval HAL status
430 */
HAL_TSC_RegisterCallback(TSC_HandleTypeDef * htsc,HAL_TSC_CallbackIDTypeDef CallbackID,pTSC_CallbackTypeDef pCallback)431 HAL_StatusTypeDef HAL_TSC_RegisterCallback(TSC_HandleTypeDef *htsc, HAL_TSC_CallbackIDTypeDef CallbackID,
432 pTSC_CallbackTypeDef pCallback)
433 {
434 HAL_StatusTypeDef status = HAL_OK;
435
436 if (pCallback == NULL)
437 {
438 /* Update the error code */
439 htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
440
441 return HAL_ERROR;
442 }
443 /* Process locked */
444 __HAL_LOCK(htsc);
445
446 if (HAL_TSC_STATE_READY == htsc->State)
447 {
448 switch (CallbackID)
449 {
450 case HAL_TSC_CONV_COMPLETE_CB_ID :
451 htsc->ConvCpltCallback = pCallback;
452 break;
453
454 case HAL_TSC_ERROR_CB_ID :
455 htsc->ErrorCallback = pCallback;
456 break;
457
458 case HAL_TSC_MSPINIT_CB_ID :
459 htsc->MspInitCallback = pCallback;
460 break;
461
462 case HAL_TSC_MSPDEINIT_CB_ID :
463 htsc->MspDeInitCallback = pCallback;
464 break;
465
466 default :
467 /* Update the error code */
468 htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
469
470 /* Return error status */
471 status = HAL_ERROR;
472 break;
473 }
474 }
475 else if (HAL_TSC_STATE_RESET == htsc->State)
476 {
477 switch (CallbackID)
478 {
479 case HAL_TSC_MSPINIT_CB_ID :
480 htsc->MspInitCallback = pCallback;
481 break;
482
483 case HAL_TSC_MSPDEINIT_CB_ID :
484 htsc->MspDeInitCallback = pCallback;
485 break;
486
487 default :
488 /* Update the error code */
489 htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
490
491 /* Return error status */
492 status = HAL_ERROR;
493 break;
494 }
495 }
496 else
497 {
498 /* Update the error code */
499 htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
500
501 /* Return error status */
502 status = HAL_ERROR;
503 }
504
505 /* Release Lock */
506 __HAL_UNLOCK(htsc);
507 return status;
508 }
509
510 /**
511 * @brief Unregister an TSC Callback
512 * TSC callback is redirected to the weak predefined callback
513 * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
514 * the configuration information for the specified TSC.
515 * @param CallbackID ID of the callback to be unregistered
516 * This parameter can be one of the following values:
517 * This parameter can be one of the following values:
518 * @arg @ref HAL_TSC_CONV_COMPLETE_CB_ID Conversion completed callback ID
519 * @arg @ref HAL_TSC_ERROR_CB_ID Error callback ID
520 * @arg @ref HAL_TSC_MSPINIT_CB_ID MspInit callback ID
521 * @arg @ref HAL_TSC_MSPDEINIT_CB_ID MspDeInit callback ID
522 * @retval HAL status
523 */
HAL_TSC_UnRegisterCallback(TSC_HandleTypeDef * htsc,HAL_TSC_CallbackIDTypeDef CallbackID)524 HAL_StatusTypeDef HAL_TSC_UnRegisterCallback(TSC_HandleTypeDef *htsc, HAL_TSC_CallbackIDTypeDef CallbackID)
525 {
526 HAL_StatusTypeDef status = HAL_OK;
527
528 /* Process locked */
529 __HAL_LOCK(htsc);
530
531 if (HAL_TSC_STATE_READY == htsc->State)
532 {
533 switch (CallbackID)
534 {
535 case HAL_TSC_CONV_COMPLETE_CB_ID :
536 htsc->ConvCpltCallback = HAL_TSC_ConvCpltCallback; /* Legacy weak ConvCpltCallback */
537 break;
538
539 case HAL_TSC_ERROR_CB_ID :
540 htsc->ErrorCallback = HAL_TSC_ErrorCallback; /* Legacy weak ErrorCallback */
541 break;
542
543 case HAL_TSC_MSPINIT_CB_ID :
544 htsc->MspInitCallback = HAL_TSC_MspInit; /* Legacy weak MspInit */
545 break;
546
547 case HAL_TSC_MSPDEINIT_CB_ID :
548 htsc->MspDeInitCallback = HAL_TSC_MspDeInit; /* Legacy weak MspDeInit */
549 break;
550
551 default :
552 /* Update the error code */
553 htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
554
555 /* Return error status */
556 status = HAL_ERROR;
557 break;
558 }
559 }
560 else if (HAL_TSC_STATE_RESET == htsc->State)
561 {
562 switch (CallbackID)
563 {
564 case HAL_TSC_MSPINIT_CB_ID :
565 htsc->MspInitCallback = HAL_TSC_MspInit; /* Legacy weak MspInit */
566 break;
567
568 case HAL_TSC_MSPDEINIT_CB_ID :
569 htsc->MspDeInitCallback = HAL_TSC_MspDeInit; /* Legacy weak MspDeInit */
570 break;
571
572 default :
573 /* Update the error code */
574 htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
575
576 /* Return error status */
577 status = HAL_ERROR;
578 break;
579 }
580 }
581 else
582 {
583 /* Update the error code */
584 htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
585
586 /* Return error status */
587 status = HAL_ERROR;
588 }
589
590 /* Release Lock */
591 __HAL_UNLOCK(htsc);
592 return status;
593 }
594
595 #endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
596
597 /**
598 * @}
599 */
600
601 /** @defgroup TSC_Exported_Functions_Group2 Input and Output operation functions
602 * @brief Input and Output operation functions
603 *
604 @verbatim
605 ===============================================================================
606 ##### IO Operation functions #####
607 ===============================================================================
608 [..] This section provides functions allowing to:
609 (+) Start acquisition in polling mode.
610 (+) Start acquisition in interrupt mode.
611 (+) Stop conversion in polling mode.
612 (+) Stop conversion in interrupt mode.
613 (+) Poll for acquisition completed.
614 (+) Get group acquisition status.
615 (+) Get group acquisition value.
616 @endverbatim
617 * @{
618 */
619
620 /**
621 * @brief Start the acquisition.
622 * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
623 * the configuration information for the specified TSC.
624 * @retval HAL status
625 */
HAL_TSC_Start(TSC_HandleTypeDef * htsc)626 HAL_StatusTypeDef HAL_TSC_Start(TSC_HandleTypeDef *htsc)
627 {
628 /* Check the parameters */
629 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
630
631 /* Process locked */
632 __HAL_LOCK(htsc);
633
634 /* Change TSC state */
635 htsc->State = HAL_TSC_STATE_BUSY;
636
637 /* Clear interrupts */
638 __HAL_TSC_DISABLE_IT(htsc, (TSC_IT_EOA | TSC_IT_MCE));
639
640 /* Clear flags */
641 __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
642
643 /* Set touch sensing IOs not acquired to the specified IODefaultMode */
644 if (htsc->Init.IODefaultMode == TSC_IODEF_OUT_PP_LOW)
645 {
646 __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
647 }
648 else
649 {
650 __HAL_TSC_SET_IODEF_INFLOAT(htsc);
651 }
652
653 /* Launch the acquisition */
654 __HAL_TSC_START_ACQ(htsc);
655
656 /* Process unlocked */
657 __HAL_UNLOCK(htsc);
658
659 /* Return function status */
660 return HAL_OK;
661 }
662
663 /**
664 * @brief Start the acquisition in interrupt mode.
665 * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
666 * the configuration information for the specified TSC.
667 * @retval HAL status.
668 */
HAL_TSC_Start_IT(TSC_HandleTypeDef * htsc)669 HAL_StatusTypeDef HAL_TSC_Start_IT(TSC_HandleTypeDef *htsc)
670 {
671 /* Check the parameters */
672 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
673 assert_param(IS_TSC_MCE_IT(htsc->Init.MaxCountInterrupt));
674
675 /* Process locked */
676 __HAL_LOCK(htsc);
677
678 /* Change TSC state */
679 htsc->State = HAL_TSC_STATE_BUSY;
680
681 /* Enable end of acquisition interrupt */
682 __HAL_TSC_ENABLE_IT(htsc, TSC_IT_EOA);
683
684 /* Enable max count error interrupt (optional) */
685 if (htsc->Init.MaxCountInterrupt == ENABLE)
686 {
687 __HAL_TSC_ENABLE_IT(htsc, TSC_IT_MCE);
688 }
689 else
690 {
691 __HAL_TSC_DISABLE_IT(htsc, TSC_IT_MCE);
692 }
693
694 /* Clear flags */
695 __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
696
697 /* Set touch sensing IOs not acquired to the specified IODefaultMode */
698 if (htsc->Init.IODefaultMode == TSC_IODEF_OUT_PP_LOW)
699 {
700 __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
701 }
702 else
703 {
704 __HAL_TSC_SET_IODEF_INFLOAT(htsc);
705 }
706
707 /* Launch the acquisition */
708 __HAL_TSC_START_ACQ(htsc);
709
710 /* Process unlocked */
711 __HAL_UNLOCK(htsc);
712
713 /* Return function status */
714 return HAL_OK;
715 }
716
717 /**
718 * @brief Stop the acquisition previously launched in polling mode.
719 * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
720 * the configuration information for the specified TSC.
721 * @retval HAL status
722 */
HAL_TSC_Stop(TSC_HandleTypeDef * htsc)723 HAL_StatusTypeDef HAL_TSC_Stop(TSC_HandleTypeDef *htsc)
724 {
725 /* Check the parameters */
726 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
727
728 /* Process locked */
729 __HAL_LOCK(htsc);
730
731 /* Stop the acquisition */
732 __HAL_TSC_STOP_ACQ(htsc);
733
734 /* Set touch sensing IOs in low power mode (output push-pull) */
735 __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
736
737 /* Clear flags */
738 __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
739
740 /* Change TSC state */
741 htsc->State = HAL_TSC_STATE_READY;
742
743 /* Process unlocked */
744 __HAL_UNLOCK(htsc);
745
746 /* Return function status */
747 return HAL_OK;
748 }
749
750 /**
751 * @brief Stop the acquisition previously launched in interrupt mode.
752 * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
753 * the configuration information for the specified TSC.
754 * @retval HAL status
755 */
HAL_TSC_Stop_IT(TSC_HandleTypeDef * htsc)756 HAL_StatusTypeDef HAL_TSC_Stop_IT(TSC_HandleTypeDef *htsc)
757 {
758 /* Check the parameters */
759 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
760
761 /* Process locked */
762 __HAL_LOCK(htsc);
763
764 /* Stop the acquisition */
765 __HAL_TSC_STOP_ACQ(htsc);
766
767 /* Set touch sensing IOs in low power mode (output push-pull) */
768 __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
769
770 /* Disable interrupts */
771 __HAL_TSC_DISABLE_IT(htsc, (TSC_IT_EOA | TSC_IT_MCE));
772
773 /* Clear flags */
774 __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
775
776 /* Change TSC state */
777 htsc->State = HAL_TSC_STATE_READY;
778
779 /* Process unlocked */
780 __HAL_UNLOCK(htsc);
781
782 /* Return function status */
783 return HAL_OK;
784 }
785
786 /**
787 * @brief Start acquisition and wait until completion.
788 * @note There is no need of a timeout parameter as the max count error is already
789 * managed by the TSC peripheral.
790 * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
791 * the configuration information for the specified TSC.
792 * @retval HAL state
793 */
HAL_TSC_PollForAcquisition(TSC_HandleTypeDef * htsc)794 HAL_StatusTypeDef HAL_TSC_PollForAcquisition(TSC_HandleTypeDef *htsc)
795 {
796 /* Check the parameters */
797 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
798
799 /* Process locked */
800 __HAL_LOCK(htsc);
801
802 /* Check end of acquisition */
803 while (HAL_TSC_GetState(htsc) == HAL_TSC_STATE_BUSY)
804 {
805 /* The timeout (max count error) is managed by the TSC peripheral itself. */
806 }
807
808 /* Process unlocked */
809 __HAL_UNLOCK(htsc);
810
811 return HAL_OK;
812 }
813
814 /**
815 * @brief Get the acquisition status for a group.
816 * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
817 * the configuration information for the specified TSC.
818 * @param gx_index Index of the group
819 * @retval Group status
820 */
HAL_TSC_GroupGetStatus(const TSC_HandleTypeDef * htsc,uint32_t gx_index)821 TSC_GroupStatusTypeDef HAL_TSC_GroupGetStatus(const TSC_HandleTypeDef *htsc, uint32_t gx_index)
822 {
823 /* Check the parameters */
824 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
825 assert_param(IS_TSC_GROUP_INDEX(gx_index));
826
827 /* Return the group status */
828 return (__HAL_TSC_GET_GROUP_STATUS(htsc, gx_index));
829 }
830
831 /**
832 * @brief Get the acquisition measure for a group.
833 * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
834 * the configuration information for the specified TSC.
835 * @param gx_index Index of the group
836 * @retval Acquisition measure
837 */
HAL_TSC_GroupGetValue(const TSC_HandleTypeDef * htsc,uint32_t gx_index)838 uint32_t HAL_TSC_GroupGetValue(const TSC_HandleTypeDef *htsc, uint32_t gx_index)
839 {
840 /* Check the parameters */
841 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
842 assert_param(IS_TSC_GROUP_INDEX(gx_index));
843
844 /* Return the group acquisition counter */
845 return htsc->Instance->IOGXCR[gx_index];
846 }
847
848 /**
849 * @}
850 */
851
852 /** @defgroup TSC_Exported_Functions_Group3 Peripheral Control functions
853 * @brief Peripheral Control functions
854 *
855 @verbatim
856 ===============================================================================
857 ##### Peripheral Control functions #####
858 ===============================================================================
859 [..] This section provides functions allowing to:
860 (+) Configure TSC IOs
861 (+) Discharge TSC IOs
862 @endverbatim
863 * @{
864 */
865
866 /**
867 * @brief Configure TSC IOs.
868 * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
869 * the configuration information for the specified TSC.
870 * @param config Pointer to the configuration structure.
871 * @retval HAL status
872 */
HAL_TSC_IOConfig(TSC_HandleTypeDef * htsc,const TSC_IOConfigTypeDef * config)873 HAL_StatusTypeDef HAL_TSC_IOConfig(TSC_HandleTypeDef *htsc, const TSC_IOConfigTypeDef *config)
874 {
875 /* Check the parameters */
876 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
877 assert_param(IS_TSC_GROUP(config->ChannelIOs));
878 assert_param(IS_TSC_GROUP(config->ShieldIOs));
879 assert_param(IS_TSC_GROUP(config->SamplingIOs));
880
881 /* Process locked */
882 __HAL_LOCK(htsc);
883
884 /* Stop acquisition */
885 __HAL_TSC_STOP_ACQ(htsc);
886
887 /* Disable Schmitt trigger hysteresis on all used TSC IOs */
888 htsc->Instance->IOHCR = (~(config->ChannelIOs | config->ShieldIOs | config->SamplingIOs));
889
890 /* Set channel and shield IOs */
891 htsc->Instance->IOCCR = (config->ChannelIOs | config->ShieldIOs);
892
893 /* Set sampling IOs */
894 htsc->Instance->IOSCR = config->SamplingIOs;
895
896 /* Set groups to be acquired */
897 htsc->Instance->IOGCSR = TSC_extract_groups(config->ChannelIOs);
898
899 /* Process unlocked */
900 __HAL_UNLOCK(htsc);
901
902 /* Return function status */
903 return HAL_OK;
904 }
905
906 /**
907 * @brief Discharge TSC IOs.
908 * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
909 * the configuration information for the specified TSC.
910 * @param choice This parameter can be set to ENABLE or DISABLE.
911 * @retval HAL status
912 */
HAL_TSC_IODischarge(TSC_HandleTypeDef * htsc,FunctionalState choice)913 HAL_StatusTypeDef HAL_TSC_IODischarge(TSC_HandleTypeDef *htsc, FunctionalState choice)
914 {
915 /* Check the parameters */
916 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
917
918 /* Process locked */
919 __HAL_LOCK(htsc);
920
921 if (choice == ENABLE)
922 {
923 __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
924 }
925 else
926 {
927 __HAL_TSC_SET_IODEF_INFLOAT(htsc);
928 }
929
930 /* Process unlocked */
931 __HAL_UNLOCK(htsc);
932
933 /* Return the group acquisition counter */
934 return HAL_OK;
935 }
936
937 /**
938 * @}
939 */
940
941 /** @defgroup TSC_Exported_Functions_Group4 Peripheral State and Errors functions
942 * @brief Peripheral State and Errors functions
943 *
944 @verbatim
945 ===============================================================================
946 ##### State and Errors functions #####
947 ===============================================================================
948 [..]
949 This subsection provides functions allowing to
950 (+) Get TSC state.
951
952 @endverbatim
953 * @{
954 */
955
956 /**
957 * @brief Return the TSC handle state.
958 * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
959 * the configuration information for the specified TSC.
960 * @retval HAL state
961 */
HAL_TSC_GetState(TSC_HandleTypeDef * htsc)962 HAL_TSC_StateTypeDef HAL_TSC_GetState(TSC_HandleTypeDef *htsc)
963 {
964 /* Check the parameters */
965 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
966
967 if (htsc->State == HAL_TSC_STATE_BUSY)
968 {
969 /* Check end of acquisition flag */
970 if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_EOA) != RESET)
971 {
972 /* Check max count error flag */
973 if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_MCE) != RESET)
974 {
975 /* Change TSC state */
976 htsc->State = HAL_TSC_STATE_ERROR;
977 }
978 else
979 {
980 /* Change TSC state */
981 htsc->State = HAL_TSC_STATE_READY;
982 }
983 }
984 }
985
986 /* Return TSC state */
987 return htsc->State;
988 }
989
990 /**
991 * @}
992 */
993
994 /** @defgroup TSC_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
995 * @{
996 */
997
998 /**
999 * @brief Handle TSC interrupt request.
1000 * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
1001 * the configuration information for the specified TSC.
1002 * @retval None
1003 */
HAL_TSC_IRQHandler(TSC_HandleTypeDef * htsc)1004 void HAL_TSC_IRQHandler(TSC_HandleTypeDef *htsc)
1005 {
1006 /* Check the parameters */
1007 assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
1008
1009 /* Check if the end of acquisition occurred */
1010 if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_EOA) != RESET)
1011 {
1012 /* Clear EOA flag */
1013 __HAL_TSC_CLEAR_FLAG(htsc, TSC_FLAG_EOA);
1014 }
1015
1016 /* Check if max count error occurred */
1017 if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_MCE) != RESET)
1018 {
1019 /* Clear MCE flag */
1020 __HAL_TSC_CLEAR_FLAG(htsc, TSC_FLAG_MCE);
1021 /* Change TSC state */
1022 htsc->State = HAL_TSC_STATE_ERROR;
1023 #if (USE_HAL_TSC_REGISTER_CALLBACKS == 1)
1024 htsc->ErrorCallback(htsc);
1025 #else
1026 /* Conversion completed callback */
1027 HAL_TSC_ErrorCallback(htsc);
1028 #endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
1029 }
1030 else
1031 {
1032 /* Change TSC state */
1033 htsc->State = HAL_TSC_STATE_READY;
1034 #if (USE_HAL_TSC_REGISTER_CALLBACKS == 1)
1035 htsc->ConvCpltCallback(htsc);
1036 #else
1037 /* Conversion completed callback */
1038 HAL_TSC_ConvCpltCallback(htsc);
1039 #endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
1040 }
1041 }
1042
1043 /**
1044 * @brief Acquisition completed callback in non-blocking mode.
1045 * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
1046 * the configuration information for the specified TSC.
1047 * @retval None
1048 */
HAL_TSC_ConvCpltCallback(TSC_HandleTypeDef * htsc)1049 __weak void HAL_TSC_ConvCpltCallback(TSC_HandleTypeDef *htsc)
1050 {
1051 /* Prevent unused argument(s) compilation warning */
1052 UNUSED(htsc);
1053
1054 /* NOTE : This function should not be modified, when the callback is needed,
1055 the HAL_TSC_ConvCpltCallback could be implemented in the user file.
1056 */
1057 }
1058
1059 /**
1060 * @brief Error callback in non-blocking mode.
1061 * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
1062 * the configuration information for the specified TSC.
1063 * @retval None
1064 */
HAL_TSC_ErrorCallback(TSC_HandleTypeDef * htsc)1065 __weak void HAL_TSC_ErrorCallback(TSC_HandleTypeDef *htsc)
1066 {
1067 /* Prevent unused argument(s) compilation warning */
1068 UNUSED(htsc);
1069
1070 /* NOTE : This function should not be modified, when the callback is needed,
1071 the HAL_TSC_ErrorCallback could be implemented in the user file.
1072 */
1073 }
1074
1075 /**
1076 * @}
1077 */
1078
1079 /**
1080 * @}
1081 */
1082
1083 /* Private functions ---------------------------------------------------------*/
1084 /** @defgroup TSC_Private_Functions TSC Private Functions
1085 * @{
1086 */
1087
1088 /**
1089 * @brief Utility function used to set the acquired groups mask.
1090 * @param iomask Channels IOs mask
1091 * @retval Acquired groups mask
1092 */
TSC_extract_groups(uint32_t iomask)1093 static uint32_t TSC_extract_groups(uint32_t iomask)
1094 {
1095 uint32_t groups = 0UL;
1096 uint32_t idx;
1097
1098 for (idx = 0UL; idx < (uint32_t)TSC_NB_OF_GROUPS; idx++)
1099 {
1100 if ((iomask & (0x0FUL << (idx * 4UL))) != 0UL)
1101 {
1102 groups |= (1UL << idx);
1103 }
1104 }
1105
1106 return groups;
1107 }
1108
1109 /**
1110 * @}
1111 */
1112
1113 #endif /* HAL_TSC_MODULE_ENABLED */
1114
1115 /**
1116 * @}
1117 */
1118
1119 /**
1120 * @}
1121 */
1122
1123 #endif /* TSC */
1124