Skip to main content

Modifying Generated Behavior

It is important to understand the structure of the generated code and how developers can use it to customize configurations and behavior.

Handwritten user code in files generated by STM32CubeMX are protected through the use of User Code sections placed strategically throughout the code generated by STM32CubeMX (C code). In the code generated by TouchGFX Generator (C++ code) this flexibility is accomplished through inheritance as described in the AL architecture.

TouchGFX Generator generates modifiable user classes in the TouchGFX/target folder that allows the developer to extend the behavior of the HAL and override functionality in the HAL configuration. The contents of these files are only generated once when generating code for the first time using STM32CubeMX. The user contents added to the modifiable classes is protected from being overwritten by STM32CubeMX when generating code again.

Extending behavior of HAL

For some drivers, TouchGFX Generator is not able to generate a fully working HAL because they are hardware specific. TouchGFX Generator will generate modifiable user classes, which inherits from the TouchGFX framework and has empty methods the developer must implement to have a certain functionality. These classes include:

  • STM32TouchController.cpp - Defines behavior of touch controller.
  • TouchGFXGPIO.cpp - Exposes signals to GPIO's for performances measuring.
  • OSWrappers.cpp - Defines OSAL (only generated in TouchGFX/tagret if Custom RTOS is chosen in TouchGFX Generator).
  • TouchGFXDMA.cpp - Defines behavior of custom DMA2D (only generated if Custom hardware acceleration is chosen in TouchGFX Generator).
  • TouchGFXDataReader.cpp - Defines behavior of Data Reader interface (only generated if External Data Reader is enabled in TouchGFX Generator).

For instance, the following empty definition of sampleTouch() is found in STM32TouchController.cpp for the developer to implement.

STM32TouchController.cpp
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);
*
*/
return false;
}

Overriding HAL configuration

Due to the class hierarchy of the HAL, users can override HAL configurations or behavior that was generated by STM32CubeMX inside TouchGFXHAL.cpp.

In the example below, developers can modify the initialize() method to configure TouchGFX additionally or to modify an existing configuration set in TouchGFXGeneratedHAL.

TouchGFXHAL.cpp
void TouchGFXHAL::initialize()
{
// Calling parent implementation of initialize().
//
// To overwrite the generated implementation, omit call to parent function
// and implemented needed functionality here.
// Please note, HAL::initialize() must be called to initialize the framework.

TouchGFXGeneratedHAL::initialize();

//Overriding configurations
hal.lockDMAToFrontPorch(true);
hal.setFingerSize(4);
hal...
}