Skip to main content

9. Touch Controller

Motivation

Touch coordinates must be readable from a touch controller for the user to be able to interact with the application. The code developed in this step will be used later to develop the TouchGFX abstraction layer at a later stage.

Note
Skip this step if a touch controller is not relevant for your board bring up.

Goal

The goal of this step is to ensure that touch coordinates can be read from the touch controller on your display.

Verification

Here are the verification points for this section:

Verification PointRationale
Touch controller and MCU are configuredMCU must be configured to read from the touch controller over e.g. I2C.
Touch controller registers can be readThe TouchGFX abstraction layer can use this code to get the touch coordinates from the controller.
Reading performs as expectedPolling is a part of application render time. If polling takes too long touch polling should be moved to a different thread or made interrupt based.

Prerequisites

  • Display with touch controller
  • Drivers to read from touch controller

Do

This step consists of two elements: Configure the MCU to communicate with the touch controller, and write driver code to talk to the touch controller.

Most touch controllers are connected to a I2C bus. The I2C communication is configured in STM32CubeMX under Connectivity -> I2C1:

Configuring I2C

Many STM32 MCUs have more than one I2C controller, so select the one connected to your touch controller. Remember to configure the relevant GPIOs also.

If you do not have driver code for your touch controller you need to write it from scratch. The STM32Cube Firmware for your MCU contains examples for I2C communication. These can be a start. Check the datasheet for the touch controller what registers to read to get the touch coordinates. The first thing to check is the I2C address of the touch controller and then read a "device id" registers for testing.

When you have the basic I2C running you need to develop a driver function that we will need when integrating with TouchGFX later. The function should return true if there is a touch, false if not, and also provide the coordinates.

The code example below shows how this code might look, driver code being abstracted by the function myTouchController_GetState:

main.c
    uint16_t x;
uint16_t y;

TouchControllerState state;
if (myTouchController_GetState(&state))
{
x = state.touchY;
y = state.touchX;
//break point here
}

Check with your debugger that the correct x and y values are received from the touch controller.

Some touch controllers are able to report multiple touch points. This is not supported by TouchGFX and can be ignored. Most often you just select the first touch point.

In the "TouchGFX AL Development" article Abstraction Layer it is explained how to send these values to TouchGFX.

Performance is as expected

Sampling touch should be possible within 1 ms if the code is executed in the same thread as the TouchGFX Application. If not fast enough, consider moving the code to a separate task, at a later stage.