TSC Touch capacitive example

Hi, I am trying to use 1 PA_2 pin from my NUCLEO-F072 ( Mbed 6.11.0) as a Touch capacitive.
I read all ST documentation carefuly about the TSC.

Using only the channel 1:

PA_2 (TSC_GROUP1_IO3) is the touch pad, pin 25
PA_3 (TSC_GROUP1_IO3) is the sampling pin (capacitor), pin 26
PA_0 not used.
PA_1 not used.

no success following this old code (HERE)

It’s weird that no body in this comunity asked about this very common function that the STM32 comes with. Mbed docs did’t provide some simple intruction on how to use it.

Could you share with us a simple working example for Mbed 6.11.0 ?

Any help will be appreciated.



You did not read carefully, because in the description of it you can found this note (on main page what you posted)

attempt at TSC on STM32L4. not working

However when you

Nice, so there is no issue and you know what you need to do. Mbed contains HALs for F0XX and you have access to all of them (HALs for TSC included).

You probably misunderstood something. Mbed’s API list exactly specified what APIs are provided. There no information about Display drivers , Touch screen drivers or similar things. So everything outside of this list is up to you and your implementation.
There are many more features that can be provided by any MCU but it is not possible to make support for everything.

BR, Jan

Hi @JohnnyK, I understand that Mbed can’t provide full info about all microcontrollers for all functions. What is weird is that the Mbed community seems to be huge, but nothing about TouchSensing is in the forum or somewhere there, touch pads are now days a very basic thing that too many projects have.
I know that touchSensing has to be handled in a lower layer using HAL, the thing is that I have not clue how to start with.
I even tried to setup the basis with CubeMX to see how to start but they use a library “STM32_TouchSensing_Library” that of course it doesn’t compile in Mbed envirionment :frowning:

That touch sensing it’s hopeless .

I will appreciate any additional info.

Thank you.

I do not think the Mbed community is “so huge”, to be honest it is very weak.

I usually saw projects with a resistive touch screen - Lib for ILI9341 8bit - Mbed OS - Arm Mbed OS support forum

About the library. I do not think it is about Mbed environment. When I look into STM32CubeF0/Middlewares/ST/STM32_TouchSensing_Library/inc at master · STMicroelectronics/STM32CubeF0 (github.com) I see the library does not have any preparation for STM32F0xx. So how you can see there are files for STM32L1xx. And also a tsl_conf.h must be prepared before usage.

BR, Jan

The STM32CubeMx generates a basic code including that “STM32_TouchSensing_Library” lib, of course without any example. I think that the only way to make it work is trying to follow this :

That will be a very long try and try…

Do you know the way (Mbed) to setup a pin as output and in OpenDrain mode?

Still not working this is my code, it always return TSC_GROUP_ONGOING= 8191

static void TSC_Init(void)

//1. Step
//2. Step

//Touch pin as PushPull
GPIO_InitStruct.Pin = PA_2;
GPIO_InitStruct.Mode = STM_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Alternate = GPIO_AF3_TSC;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

//Sampling pin (capacitor)
GPIO_InitStruct.Pin = PA_3;
GPIO_InitStruct.Mode = STM_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Alternate = GPIO_AF3_TSC;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

//3. interrupt configuration?

//4 TSC configuration

TscHandle.Instance = TSC;
TscHandle.Init.CTPulseHighLength = TSC_CTPH_8CYCLES;
TscHandle.Init.CTPulseLowLength = TSC_CTPL_8CYCLES;
TscHandle.Init.SpreadSpectrum = DISABLE;
TscHandle.Init.SpreadSpectrumDeviation = 1;
TscHandle.Init.SpreadSpectrumPrescaler = TSC_SS_PRESC_DIV1;
TscHandle.Init.PulseGeneratorPrescaler = TSC_PG_PRESC_DIV4;
TscHandle.Init.MaxCountValue = TSC_MCV_8191;
TscHandle.Init.IODefaultMode = TSC_IODEF_OUT_PP_LOW;
TscHandle.Init.SynchroPinPolarity = TSC_SYNC_POLARITY_FALLING;
TscHandle.Init.AcquisitionMode = TSC_ACQ_MODE_NORMAL;
TscHandle.Init.MaxCountInterrupt = DISABLE;
TscHandle.Init.ChannelIOs = 3;
TscHandle.Init.ShieldIOs = 0;
TscHandle.Init.SamplingIOs = TSC_GROUP1_IO4;

  if (HAL_TSC_Init(&TscHandle) != HAL_OK)
  	printf("ERROR  HAL_TSC_Init \n");

  //Dischange cap and wait
  HAL_TSC_IODischarge(&TscHandle, ENABLE);

  //Select Channel:
  IoConfig.ChannelIOs  = TSC_GROUP1_IO3; 
  IoConfig.SamplingIOs = TSC_GROUP1_IO4;
  IoConfig.ShieldIOs   = 0;

  if (HAL_TSC_IOConfig(&TscHandle, &IoConfig) != HAL_OK)
  	printf("ERROR  HAL_TSC_IOConfig \n");


  //if (HAL_TSC_Start_IT(&TscHandle) != HAL_OK)
  if (HAL_TSC_Start(&TscHandle) != HAL_OK)
  	printf("ERROR  HAL_TSC_Start_IT \n");

  while (1){

  	//Wait for the end of acquisition
  	while (HAL_TSC_GetState(&TscHandle) == HAL_TSC_STATE_BUSY){};

  	//Clear Flags

  	 if (HAL_TSC_GroupGetStatus(&TscHandle, TSC_GROUP1_IDX) == TSC_GROUP_COMPLETED){
  		 	uint32_t val = HAL_TSC_GroupGetValue(&TscHandle, TSC_GROUP1_IDX);
  	 else if (HAL_TSC_GroupGetStatus(&TscHandle, TSC_GROUP1_IDX) == TSC_GROUP_ONGOING){
  		 printf("TSC_GROUP_ONGOING= %d\n", HAL_TSC_GroupGetValue(&TscHandle, TSC_GROUP1_IDX) );


DigitalInOut:: mode(PinMode pull)

typedef enum {
    PullNone          = 0,
    PullUp            = 1,
    PullDown          = 2,
    OpenDrainPullUp   = 3,
    OpenDrainNoPull   = 4,
    OpenDrainPullDown = 5,
    PushPullNoPull    = PullNone,
    PushPullPullUp    = PullUp,
    PushPullPullDown  = PullDown,
    OpenDrain         = OpenDrainPullUp,
    PullDefault       = PullNone
} PinMode;

You can check ST’s TSC examples for STM32F072B-Discovery maybe.

BR, Jan