H753_CPP_SPI_Resistive_Touch_Screen_Controller 1.0
STM32H753 SPI resistive touchscreen (Adafruit 333 overlay + 5767 TSC2046 controller) driver and tutorial
Loading...
Searching...
No Matches
TSC2046 Class Reference

STM32 HAL driver for a TI/Adafruit TSC2046 resistive touchscreen controller, reached over SPI via an owned STM32SPIDevice. More...

#include <TSC2046.hpp>

Public Member Functions

 TSC2046 (SPI_HandleTypeDef *hspi, GPIO_TypeDef *csPort, uint16_t csPin)
 Constructs the driver and its underlying STM32SPIDevice. Does not touch hardware – call begin() before any other method.
 
void begin (uint32_t xPlateResistance=400)
 Initializes the driver. Call once before any other method.
 
void setVRef (float vref)
 Sets the voltage connected to the TSC2046's VRef pin, if any.
 
void setTouchedThreshold (float touchedThresholdOhms)
 Sets the pressure threshold used by isTouched().
 
TSC2046TouchPoint getPoint ()
 Reads the current touch position and pressure.
 
bool isTouched ()
 Determines if the touchscreen is currently being touched, based on the pressure value from getPoint() and the threshold set by setTouchedThreshold().
 
void enableInterrupts (bool enable)
 Enables or disables the TSC2046's own touch interrupt (IRQ pin pulled low on touch). See the class-level.
 
float readTemperatureC ()
 Reads the TSC2046's on-die temperature sensor, in degrees Celsius.
 
float readTemperatureF ()
 Same reading as readTemperatureC(), converted to degrees Fahrenheit.
 
float readBatteryVoltage ()
 
float readAuxiliaryVoltage ()
 

Detailed Description

STM32 HAL driver for a TI/Adafruit TSC2046 resistive touchscreen controller, reached over SPI via an owned STM32SPIDevice.

Note
Power behavior, carried over from the reference library: while interrupts are enabled (the default), the TSC2046 auto-powers-down between reads and auto-powers-up at the start of the next one, with no measurable delay per the datasheet. If the IRQ pin is connected to an STM32 EXTI line you don't want firing, call enableInterrupts(false) instead of just ignoring the pin – leaving interrupts enabled on the TSC2046 side means its IRQ output still goes low on every touch even if nothing on the STM32 side is listening for it.
Warning
Not thread-safe or ISR-safe. getPoint() performs four sequential register reads (X, Y, Z1, Z2) that must all reflect the same physical touch to produce a sane pressure value – if another context reads this same TSC2046 instance in between, the coordinates and pressure end up mismatched. To guard against that on this single-core, bare-metal (non-RTOS) project, getPoint() brackets those four reads with __disable_irq()/__enable_irq() when TSC2046-side interrupts are enabled, matching the reference library's noInterrupts()/ interrupts() calls. If this driver is ever reused in a FreeRTOS project, replace that global interrupt disable with a mutex – disabling all interrupts for the duration of four SPI transactions will delay every other interrupt-driven subsystem (including the RTOS tick) for that whole window.

Definition at line 68 of file TSC2046.hpp.

Constructor & Destructor Documentation

◆ TSC2046()

TSC2046::TSC2046 ( SPI_HandleTypeDef * hspi,
GPIO_TypeDef * csPort,
uint16_t csPin )

Constructs the driver and its underlying STM32SPIDevice. Does not touch hardware – call begin() before any other method.

Parameters
hspiPointer to the already-initialized SPI handle to use.
csPortGPIO port of the TSC2046's chip-select pin.
csPinGPIO pin number of the TSC2046's chip-select pin.

Definition at line 53 of file TSC2046.cpp.

Member Function Documentation

◆ begin()

void TSC2046::begin ( uint32_t xPlateResistance = 400)

Initializes the driver. Call once before any other method.

Parameters
xPlateResistanceThe resistance, in Ohms, measured across the touchscreen's X- and X+ pins with a multimeter – see the reference library's documentation for how to measure this. Defaults to 400 (a reasonable placeholder if not measured, though getPoint()'s pressure value will be less accurate).

Definition at line 58 of file TSC2046.cpp.

References STM32SPIDevice::begin().

Referenced by initTSC2046().

◆ enableInterrupts()

void TSC2046::enableInterrupts ( bool enable)

Enables or disables the TSC2046's own touch interrupt (IRQ pin pulled low on touch). See the class-level.

Note
on why disabling this matters even if the IRQ pin isn't wired up.
Parameters
enabletrue to enable (the default state), false to disable.

Definition at line 144 of file TSC2046.cpp.

Referenced by tsc2046EnableInterrupts().

◆ getPoint()

TSC2046TouchPoint TSC2046::getPoint ( )

Reads the current touch position and pressure.

Return values
TheX, Y, and pressure reading. Meaningless if isTouched() (or an equivalent check on the returned pressure) is false.

Definition at line 114 of file TSC2046.cpp.

References TSC2046TouchPoint::x.

Referenced by isTouched(), and tsc2046GetPoint().

◆ isTouched()

bool TSC2046::isTouched ( )

Determines if the touchscreen is currently being touched, based on the pressure value from getPoint() and the threshold set by setTouchedThreshold().

Return values
trueif touched.

Definition at line 139 of file TSC2046.cpp.

References getPoint(), and TSC2046TouchPoint::pressure.

Referenced by tsc2046IsTouched().

◆ readAuxiliaryVoltage()

float TSC2046::readAuxiliaryVoltage ( )
Return values
Thevoltage on the AUX pin, in volts (0-effectiveVRef() range).

Definition at line 176 of file TSC2046.cpp.

Referenced by tsc2046ReadAuxiliaryVoltage().

◆ readBatteryVoltage()

float TSC2046::readBatteryVoltage ( )
Return values
Thevoltage on the VBAT pin, in volts (0-6V range regardless of Vin/VRef).

Definition at line 170 of file TSC2046.cpp.

Referenced by tsc2046ReadBatteryVoltage().

◆ readTemperatureC()

float TSC2046::readTemperatureC ( )

Reads the TSC2046's on-die temperature sensor, in degrees Celsius.

Warning
NOT an accurate temperature measurement. Treat it as a rough indication only, never as a calibrated reading. The chip derives it from the difference between two diode-voltage conversions (TEMP0 and TEMP1) using a datasheet approximation (2.573 K/mV). TI's datasheet claims only about 2 degrees C accuracy even under ideal conditions, the result depends on the reference voltage being settled, and real-world readings can be off by more (TI's own support forum has reports of the sensor output being wrong). It measures the chip's own die temperature, which runs warmer than the surrounding air, not the touch panel or the board. This port has not been checked against a reference thermometer, so the projects that use this library do not base any pass/fail test on it.
Note
Why it exists at all: the TSC2046 includes the sensor, alongside its VBAT and AUX inputs, as a convenience feature for system monitoring, and the Adafruit reference library this driver follows exposes it. It is ported here for parity with that library, not because touch handling needs it. Touch coordinates never use it.
Return values
Thechip temperature in degrees Celsius (approximate).

Definition at line 152 of file TSC2046.cpp.

Referenced by readTemperatureF(), and tsc2046ReadTemperatureC().

◆ readTemperatureF()

float TSC2046::readTemperatureF ( )

Same reading as readTemperatureC(), converted to degrees Fahrenheit.

Warning
Not accurate – see the warning on readTemperatureC(); this is a rough on-die indication only.
Return values
Thechip temperature in degrees Fahrenheit (approximate).

Definition at line 156 of file TSC2046.cpp.

References readTemperatureC().

◆ setTouchedThreshold()

void TSC2046::setTouchedThreshold ( float touchedThresholdOhms)

Sets the pressure threshold used by isTouched().

Parameters
touchedThresholdOhmsResistance readings above this value (in Ohms) are considered "not touching". Defaults to 100000 (100 kOhm) if never called.

Definition at line 67 of file TSC2046.cpp.

Referenced by tsc2046SetTouchedThreshold().

◆ setVRef()

void TSC2046::setVRef ( float vref)

Sets the voltage connected to the TSC2046's VRef pin, if any.

Parameters
vrefVoltage in volts, or a negative value (the default) if nothing is connected to VRef – in which case the chip's internal 2.5V reference is used for readTemperatureC/F(), readBatteryVoltage(), and readAuxiliaryVoltage(). Has no effect on touch coordinate reads, which always use differential mode.

Definition at line 63 of file TSC2046.cpp.

Referenced by tsc2046SetVRef().


The documentation for this class was generated from the following files: