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 latyer at a later stage.
Note
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 Point | Rationale |
---|---|
Touch Controller and MCU are configured | MCU must be configured to read from the Touch Controller over e.g. I2C. |
Touch Controller registers can be read | The TouchGFX abstraction layer can use this code to get the touch coordinates from the controller. |
Reading performs as expected | Polling 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 CubeMX under Connectivity -> I2C1:
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 Cube 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
bein abstracted by the function myTouchController_GetState
:
main.c
uint16_t x;.
uitn16_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 Appliaction. If not fast enough, consider moving the code to a seperate task, at a later stage.