Continued from Pt 1: Screen Works.

We left off at the point where the LCD shows out TouchGFX design, but touch screen doesn't work yet.

We'll fix that.


5. Add Touch Screen init and handling


The two methods in TouchGFX/target/STM32TouchController.cpp aren't implemented by the wizards.

Open the file and implement them as below:


#include <stm32h7b3i_discovery_ts.h>
#include <TouchGFXHAL.hpp>
#include <cmsis_os.h>


void STM32TouchController::init()
     * Initialize touch controller and driver
    TS_Init_t hTS;

    hTS.Orientation = TS_SWAP_XY;
    hTS.Accuracy = 0;
    hTS.Width = touchgfx::HAL::FRAME_BUFFER_WIDTH;
    hTS.Height = touchgfx::HAL::FRAME_BUFFER_HEIGHT;
    BSP_TS_Init(0, &hTS);


bool STM32TouchController::sampleTouch(int32_t& x, int32_t& y)
     * By default sampleTouch returns false,
     * return true if a touch has been detected, otherwise false.
     * Coordinates are passed to the caller by reference by x and y.
     * This function is called by the TouchGFX framework.
     * By default sampleTouch is called every tick, this can be adjusted by HAL::setTouchSampleRate(int8_t);
    TS_State_t TS_State = { 0 };

    /* This should never fail !! */
    if (BSP_TS_GetState(0, &TS_State) != BSP_ERROR_NONE)

    if (TS_State.TouchDetected)
        x = TS_State.TouchX;
        y = TS_State.TouchY;

        return true;

    return false;


The logic is dependent on the board definition files. We can import them from the firmware support package.

If you don't know where that is installed, check via the IDE  Windows -> Preferences menu


Right-click on the Drivers folder and select Import -> File System

Navigate to where you installed the firmware - In our case, it's version 1.6.0 because that's the one used in the TouchGFX .ioc file. Look for the folder Drivers and select these:


Then select the project's BSP folder, right-click and select Import


Rename Drivers/BSP/STM32H7B3I-DK/stm32h7b3i_discovery_conf_template.h to stm32h7b3i_discovery_conf.h.

Rename Drivers/BSP/Components/mx25lm51245g/mx25lm51245g_conf_template.h to mx25lm51245g_template.h.


Rename Drivers/BSP/Components/ft5336/ft5336_conf_template.h to ft5336_conf.h.

Then edit it:

#define FT5336_MAX_X_LENGTH                  480U
#define FT5336_MAX_Y_LENGTH                  272U


Create a new header file Drivers/STM32H7xx_HAL_Driver/Inc/stm32xxxx_hal.h.

Enter these contents:


#include "stm32h7xx_hal.h"




Adapt the include settings, by right-clicking the project, then select properties:


Build and Debug again. The touch screen should now work. It does for me .

Again, the project, now with touch screen working, is attached.


Related Posts
First Experience with CubeIDE
Create a TouchGFX Project with support for the Designer, CubeIDE and Debugger - Pt 1: Screen Works
Create a TouchGFX Project with support for the Designer, CubeIDE and Debugger - Pt 2: Touch Works
TouchGFX Simple Example: react on a button click
USB, freeRTOS ,Task Notifications and Interrupts
the Development Kit STMod+ Connector and Using UART2
TouchGFX Application Framework: Model, View, Presentation, Message Queue
TouchGFX Application Framework: A Mock GUI - Show Statuses, Switch Screens
TouchGFX Application Framework: MVP and the ModelListener
Write a CubeIDE MX application: Hardware Cryptography with DMA