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
STM32_TSC2046

STM32 HAL (C++) driver for the TI TSC2046 resistive touchscreen controller (e.g. Adafruit #5767): reads raw 12-bit touch X/Y coordinates and a pressure-proportional resistance over SPI, plus the chip's temperature, VBAT and AUX readings.

Modeled on Adafruit_TSC2046 as a design reference for the command byte layout and pressure formula. It is an independent STM32 HAL implementation, not a direct port.

Dependency

Built on STM32_BusIO. Add both to your project as git submodules:

git submodule add git@github.com:jpgroulx/STM32_BusIO.git STM32BusIO
git submodule add git@github.com:jpgroulx/STM32_TSC2046.git TSC2046

Cloning a project that already includes them:

git clone --recurse-submodules <project-url>

If you forgot the flag the folders are empty; run git submodule update --init.

In STM32CubeIDE, add both folders as source locations and add ../STM32BusIO and ../TSC2046 to the include paths for both C and C++.

Usage

Configure an SPI peripheral in CubeMX (mode 0, 8-bit, MSB first, SCK at or below ~2 MHz) and a GPIO output for chip select, then:

TSC2046 touch(&hspi1, TSC2046_CS_GPIO_Port, TSC2046_CS_Pin);
touch.begin(); // optional: begin(plateOhms) if you measured it
TSC2046TouchPoint p = touch.getPoint();
if (touch.isTouched()) { /* p.x, p.y are 0-4095 raw; p.pressure is ohms, lower = harder */ }
STM32 HAL driver for a TI/Adafruit TSC2046 resistive touchscreen controller, reached over SPI via an ...
Definition TSC2046.hpp:68
One touch reading: raw X/Y position and a pressure-proportional resistance value.
Definition TSC2046.hpp:32

Construct the object after MX_SPIx_Init(), not as a global, so the peripheral is configured first. The PENIRQ pin can be polled as a GPIO input (active low while pressed).

Using it from a CubeIDE main.c

The classes in this library are C++, but STM32CubeIDE generates main.c as plain C, and you should not rename it to main.cpp (CubeMX regenerates main.c, not main.cpp). Keep main.c as C and reach the driver through a small C-callable wrapper:

  1. Enable C++ in the project first (right-click the project, Convert to C++).
  2. Declare wrapper functions inside extern "C" in a header.
  3. Implement them in a .cpp file that creates the TSC2046 object with new after MX_SPIx_Init() has run (not as a global, which would construct too early).
  4. Call the wrapper functions from main.c, and put those calls only inside CubeMX's "safe areas": the code between the /* USER CODE BEGIN ... */ and /* USER CODE END ... */ markers. CubeMX overwrites everything outside those markers whenever it regenerates main.c.

The tutorial STM32 C++ Code Integration with STM32CubeIDE walks through this step by step.

Notes

  • Touch coordinates use differential mode and don't need VRef, VBAT or AUX connected.
  • The temperature reading (readTemperatureC() / readTemperatureF()) is not accurate. It is the chip's own on-die temperature from a two-diode measurement; TI specifies only about 2 C accuracy under ideal conditions, real readings can be off by more, and it has not been checked against a reference thermometer here. It exists because the TSC2046 has the sensor (alongside the VBAT and AUX inputs) and the Adafruit reference library exposes it, so it is ported for parity. Touch handling never uses it. Treat it as a rough indication only.
  • Not thread-safe or ISR-safe. getPoint() disables interrupts around four SPI reads; in an RTOS project replace that with a mutex.

Building the documentation

The API docs are generated with Doxygen and are not committed to the repo. From this folder, run:

doxygen

The HTML is written to docs/ (open docs/html/index.html). Graphviz is optional, for call graphs; see the commented HAVE_DOT lines in Doxyfile.

License

MIT. Design derived from Adafruit_TSC2046, Copyright (c) 2023 Adafruit Industries and Mikaela Szekely (MIT). See LICENSE.