F439_CPP_SPI_RA8875_TFT_LCD_04 1.0
STM32F439 SPI RA8875 7-inch TFT display with FRAM-backed touch calibration, built on the STM32_GFX, STM32_RA8875 and STM32_MB85RS64V libraries
Loading...
Searching...
No Matches
touchCalibrationStorage.hpp File Reference

Stores the touch calibration matrix in the MB85RS64V FRAM and checks it when loading. More...

#include "Adafruit_RA8875.h"
#include "MB85RS64V.hpp"

Go to the source code of this file.

Functions

void touchCalibrationAttachStorage (Adafruit_RA8875 *display, MB85RS64V *fram)
 Registers the FRAM read and write callbacks with the display library.
 
bool touchCalibrationLoad (Adafruit_RA8875 *display, MB85RS64V *fram, tsMatrix_t *matrix)
 Loads the calibration matrix from the FRAM if a valid one is stored.
 
bool touchCalibrationSave (Adafruit_RA8875 *display, MB85RS64V *fram, tsMatrix_t *matrix)
 Saves the calibration matrix to the FRAM and checks it by reading it back.
 

Variables

constexpr uint16_t TOUCH_CAL_ADDRESS = 0x0100
 FRAM address of the calibration matrix block written by the RA8875 library.
 
constexpr uint16_t TOUCH_CAL_HEADER_ADDRESS = 0x0120
 FRAM address of the 12-byte validity header.
 

Detailed Description

Stores the touch calibration matrix in the MB85RS64V FRAM and checks it when loading.

Project: F439_CPP_SPI_RA8875_TFT_LCD_04

The matrix itself is written and read by the RA8875 library (Adafruit_RA8875::writeCalibration()/readCalibration()), which calls back into this module to reach the FRAM. On top of that this module keeps a small header with a magic number, a version, the block length and a CRC-32 of the stored matrix bytes. The header is written last, after the matrix, so a power failure part-way through a save leaves a header that does not match the data and the next start simply calibrates again. A blank FRAM (all 00 or all FF), a damaged matrix, a different format version and a zero divider are all treated as "no valid calibration".

FRAM layout (all addresses inside the MB85RS64V's 8192 bytes):

  • TOUCH_CAL_ADDRESS (0x0100), 29 bytes: the library's matrix block (seven 32-bit values, most significant byte first, then a "calibrated" flag byte)
  • TOUCH_CAL_HEADER_ADDRESS (0x0120), 12 bytes: magic "CAL1", version, length, CRC-32
Warning
Thread safety: not thread-safe and not ISR-safe. Every function here is a sequence of separate FRAM transactions on a bus shared with the display; save() is several of them in a row and only the last one marks the data as valid. A second caller that used the FRAM or the SPI bus in between could corrupt the sequence. Call from one thread only, while nothing else uses SPI1.

Created on: Oct 5, 2026 Author: johng

Definition in file touchCalibrationStorage.hpp.

Function Documentation

◆ touchCalibrationAttachStorage()

void touchCalibrationAttachStorage ( Adafruit_RA8875 * display,
MB85RS64V * fram )

Registers the FRAM read and write callbacks with the display library.

Parameters
displayThe RA8875 display object.
framThe started MB85RS64V driver that holds the calibration.

Must be called once before touchCalibrationLoad() or touchCalibrationSave(). If either pointer is null it prints the reason and calls Error_Handler(), which does not return.

Definition at line 94 of file touchCalibrationStorage.cpp.

References Adafruit_RA8875::setCalibrationStorage(), and MB85RS64V::SIZE_BYTES.

Referenced by initTouchCalibration(), and testTouch().

◆ touchCalibrationLoad()

bool touchCalibrationLoad ( Adafruit_RA8875 * display,
MB85RS64V * fram,
tsMatrix_t * matrix )

Loads the calibration matrix from the FRAM if a valid one is stored.

Parameters
displayThe RA8875 display object, with the storage attached.
framThe started MB85RS64V driver.
matrixReceives the matrix when the function returns true.
Returns
true if a valid calibration was found (magic, version, length, CRC and the library's flag all agree and the divider is not zero); false if none is stored or it is damaged, in which case the reason is printed and the caller should calibrate again. A failed SPI transfer is not reported here: the FRAM driver calls Error_Handler().

Definition at line 114 of file touchCalibrationStorage.cpp.

References tsMatrix_t::Divider, MB85RS64V::read(), Adafruit_RA8875::readCalibration(), TOUCH_CAL_ADDRESS, and TOUCH_CAL_HEADER_ADDRESS.

Referenced by initTouchCalibration(), and touchCalibrationSave().

◆ touchCalibrationSave()

bool touchCalibrationSave ( Adafruit_RA8875 * display,
MB85RS64V * fram,
tsMatrix_t * matrix )

Saves the calibration matrix to the FRAM and checks it by reading it back.

Parameters
displayThe RA8875 display object, with the storage attached.
framThe started MB85RS64V driver.
matrixThe matrix to store.
Returns
true if the data and header were written and loading them back gave exactly the same matrix; false if the read-back did not match.

Definition at line 175 of file touchCalibrationStorage.cpp.

References MB85RS64V::read(), TOUCH_CAL_ADDRESS, TOUCH_CAL_HEADER_ADDRESS, touchCalibrationLoad(), MB85RS64V::write(), and Adafruit_RA8875::writeCalibration().

Referenced by testTouch().

Variable Documentation

◆ TOUCH_CAL_ADDRESS

constexpr uint16_t TOUCH_CAL_ADDRESS = 0x0100
constexpr

FRAM address of the calibration matrix block written by the RA8875 library.

Definition at line 39 of file touchCalibrationStorage.hpp.

Referenced by touchCalibrationLoad(), and touchCalibrationSave().

◆ TOUCH_CAL_HEADER_ADDRESS

constexpr uint16_t TOUCH_CAL_HEADER_ADDRESS = 0x0120
constexpr

FRAM address of the 12-byte validity header.

Definition at line 42 of file touchCalibrationStorage.hpp.

Referenced by touchCalibrationLoad(), and touchCalibrationSave().