2021-06-29 08:09 AM
I am using a STM32WB55xx controller for my project. I am trying to implement the capacitive touch behavior to detect button press on rising edge and button release on falling edge (please see document attached for details)
Q1. Will the Touch Sensing Controller (TSC) peripheral support this behavior ?
Q2. Can I use TSC in interrupt mode for this? If yes, which callback ID should I be using from the following:
typedef enum
{
HAL_TSC_CONV_COMPLETE_CB_ID = 0x00UL, /*!< TSC Conversion completed callback ID */
HAL_TSC_ERROR_CB_ID = 0x01UL, /*!< TSC Error callback ID */
HAL_TSC_MSPINIT_CB_ID = 0x02UL, /*!< TSC Msp Init callback ID */
HAL_TSC_MSPDEINIT_CB_ID = 0x03UL /*!< TSC Msp DeInit callback ID */
} HAL_TSC_CallbackIDTypeDef;
Thank you,
Arshiya Tabassum
2021-07-19 06:29 AM
Hello Arshiya,
The best is to use TOUCHSENSING library instead of HAL_TSC to do what you are looking for.
Regarding point Q1
Yes, StateId handle RELEASE and DETECT states. Something like this can be used:
#define TKEY_DET(NB) (MyTKeys[(NB)].p_Data->StateId == TSL_STATEID_DETECT)
#define TKEY_REL(NB) (MyTKeys[(NB)].p_Data->StateId == TSL_STATEID_RELEASE)
See pseudo code example bellow.
Keep in mind, debounce filter will delay release and detect (same as anti debounce on mechanical switches).
See TSLPRM_DEBOUNCE_DETECT and TSLPRM_DEBOUNCE_RELEASE values in tsl_conf.h file.
Regarding point Q2
Yes, you must used HAL_TSC_ConvCpltCallback api, see code example bellow.
Regarding Time out = 10s
TOUCHSENSING library include this feature, details in tsl_conf.h:
/*============================================================================*/
/* Detection Time Out (DTO) */
/*============================================================================*/
/** @defgroup Common_Parameters_DTO 11 - DTO
* @{ */
/** Detection Time Out delay in seconds (range=0..63)
- Value 0: DTO processing not compiled in the code (to gain size if not used).
- Value 1: Default time out infinite.
- Value between 2 and 63: Default time out between value n-1 and n.
- Examples:
- With a DTO equal to 2, the time out is between 1s and 2s.
- With a DTO equal to 63, the time out is between 62s and 63s.
@note The DTO can be changed in run-time by the application only if the
default value is between 1 and 63.
*/
#define TSLPRM_DTO (10)
Code example:
on main.c file:
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "touchsensing.h"
#include "tsc.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
#include "tsl.h"
#include "tsl_user.h"
/* Private typedef -----------------------------------------------------------*/
int TSCidxGroup=0;
int TSCAcqDone=0;
extern __IO TSL_tTick_ms_T ECSLastTick;
#define TKEY_DET(NB) (MyTKeys[(NB)].p_Data->StateId == TSL_STATEID_DETECT)
#define TKEY_REL(NB) (MyTKeys[(NB)].p_Data->StateId == TSL_STATEID_RELEASE)
int IdLed;
/**
* @brief Acquisition completed callback in non blocking mode
* @param htsc: pointer to a TSC_HandleTypeDef structure that contains
* the configuration information for the specified TSC.
* @retval None
*/
void HAL_TSC_ConvCpltCallback(TSC_HandleTypeDef* htsc)
{
TSL_acq_BankGetResult(TSCidxGroup, 0, 0);
TSCidxGroup++;
if (TSCidxGroup > TSLPRM_TOTAL_BANKS-1){
TSCidxGroup=0;
// Start process to handle all results, (this is a choice....)
TSCAcqDone++;
}else{
// We restart bank acquisition only if we have more than 1 bank (this is a choice....)
TSL_acq_BankConfig(TSCidxGroup);
TSL_acq_BankStartAcq_IT();
}
}
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TSC_Init();
MX_TOUCHSENSING_Init();
// Start First bank acquisition.
TSCidxGroup=0;
TSCAcqDone=0;
TSL_acq_BankConfig(TSCidxGroup);
TSL_acq_BankStartAcq_IT();
__WFI(); // For test purpose, to check Interrupt are working fine.
/* Infinite loop */
while (1){
if(TSCAcqDone != 0){
TSCAcqDone = 0;
// Get TSC/TSL keys status
TSL_obj_GroupProcess(&MyObjGroup);
// Point to first object
TSL_dxs_FirstObj(&MyObjGroup);
// Process ECS algo
if (TSL_tim_CheckDelay_ms(TSLPRM_ECS_DELAY, &ECSLastTick) == TSL_STATUS_OK){
TSL_ecs_Process(&MyObjGroup);
}
/*
for (IdLed=0; IdLed < TSLPRM_TOTAL_CHANNELS; IdLed++){
if(TKEY_DET(IdLed)){
/// Key press detection
time_stamp_press=get_systick
button_state=press
}
else if(TKEY_REL(IdLed)){
/// Key release detection
time_stamp_release=get_systick
button_state=release
}
}
*/
// Restart all bank acquisition
TSCidxGroup = 0;
TSCAcqDone = 0;
TSL_acq_BankConfig(TSCidxGroup);
TSL_acq_BankStartAcq_IT();
}else{
//__WFI();
HAL_Delay(1);
}
}
}
Regards, Olivier