Skip to main content
Explorer
January 20, 2025
Question

STM32 TSC Multi group + multi pin detection problem

  • January 20, 2025
  • 1 reply
  • 529 views

 

Hello,
I am a student currently studying STM32 products.
I am trying to implement a function using the TSC feature of STM32 to read a total of 10 touch values.

I have been following various resources, but I keep encountering an issue where touching one port also causes the values of other ports to drop simultaneously.
When I checked in debug mode, I noticed that both IOGXCR[1] and IOGXCR[5] respond at the same time.
As far as I know, touch detection is shared within the same group if there are no specific pin settings. However, in this case, the groups are different, so I’m unsure why this is happening.

The board I’m using is the Nucleof303k8.
I would appreciate any help.

 

#include "main.h"
#include "tim.h"
#include "tsc.h"
#include "usart.h"
#include "gpio.h"

#define MAX_TOUCH_VALUE 250
#define DATA_SIZE 14
#define NUM_CHANNELS 5
#define CHANNEL_MASK 0x1F
#define NUM_GROUPS (sizeof(tch_group) / sizeof(Group_touch))


uint8_t trigger_tim3;
uint16_t tpt[10] = {0,};
uint8_t idx_tsc;

typedef struct {
uint8_t seq;
uint8_t pin_count;
uint8_t addr[4];
uint8_t used_group[4];
uint32_t touch_pins;
} tsc_mod;

tsc_mod TModule[] = {
{0, 3, {0, 1, 2}, {0, 1, 3}, TSC_GROUP1_IO2 | TSC_GROUP2_IO2 | TSC_GROUP4_IO2},
{1, 4, {3, 4, 5, 6}, {0, 1, 2, 4}, TSC_GROUP1_IO3 | TSC_GROUP2_IO3 | TSC_GROUP3_IO3| TSC_GROUP5_IO3},
{2, 3, {7, 8, 9}, {0, 1, 4}, TSC_GROUP1_IO4 | TSC_GROUP2_IO4 | TSC_GROUP5_IO4}
};

uint8_t pck[DATA_SIZE] = {0,};

void SystemClock_Config(void);

int main(void) {
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_TSC_Init();
MX_TIM3_Init();

HAL_TIM_Base_Start_IT(&htim3);
HAL_TSC_Init(&htsc);
HAL_TSC_Start_IT(&htsc);

while (1) {
if(trigger_tim3 == 1) {
trigger_tim3 = 0;
tsc_mod cur_seq_mod = TModule[idx_tsc];
HAL_TSC_IODischarge(&htsc, ENABLE);
TSC->IOHCR = ~cur_seq_mod.touch_pins;

if(HAL_TSC_PollForAcquisition(&htsc) == HAL_OK) {
if (HAL_TSC_Start(&htsc) == HAL_OK) {
if (HAL_TSC_PollForAcquisition(&htsc) == HAL_OK) {
for(int i = 0; i < cur_seq_mod.pin_count; i++) {
tpt[cur_seq_mod.addr[i]] = TSC->IOGXCR[cur_seq_mod.used_group[i]];
}
}
}
}
idx_tsc++;
if(idx_tsc > 2) {
idx_tsc = 0;
}
}
}
}

void SystemClock_Config(void) {
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}

RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
Error_Handler();
}
}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
if (htim->Instance == htim3.Instance) {
if(trigger_tim3 == 0) {
trigger_tim3 = 1;
}
}
}

void Error_Handler(void) {
__disable_irq();
while (1) {}
}
    This topic has been closed for replies.

    1 reply

    ST Employee
    February 11, 2025

    Hello Remoter,

    I suggest you base yourself from an existing example, in STM32CubeF3 package for example you have examples for both polling and interrupt mode for the TSC feature on the eval boards examples. It is easier to follow or debug.

    If you want you can also generate the required code from CubeMx as well.

    I think the configuration is not correct in your code.

    Regards,
    Stassen