H753_CPP_SPI_Resistive_Touch_Screen_Controller 1.0
STM32H753 SPI resistive touchscreen (Adafruit 333 overlay + 5767 TSC2046 controller) driver and tutorial
Loading...
Searching...
No Matches
STM32SPIDevice Class Reference

Wraps one SPI peripheral + chip-select pin into read/write/transfer operations, mirroring the hardware-SPI subset of Adafruit_BusIO's Adafruit_SPIDevice API. More...

#include <STM32SPIDevice.hpp>

Public Member Functions

 STM32SPIDevice (SPI_HandleTypeDef *hspi, GPIO_TypeDef *csPort, uint16_t csPin, uint32_t timeoutMs=100)
 Constructs the device wrapper. Does not touch hardware – HAL_GPIO_Init()/HAL_SPI_Init() for the underlying pins/peripheral are assumed already done by CubeMX's generated MX_GPIO_Init() / MX_SPIx_Init(), called before this object is used.
 
void begin ()
 Deasserts chip-select (idle-high) so the bus starts in a known state. Call once before the first transaction.
 
bool read (uint8_t *buffer, size_t len, uint8_t sendValue=0xFF)
 Reads len bytes into buffer, clocking out sendValue for each byte read. Brackets the transfer with chip-select assert/deassert.
 
bool write (const uint8_t *buffer, size_t len, const uint8_t *prefixBuffer=nullptr, size_t prefixLen=0)
 Writes len bytes from buffer, optionally preceded by a separate prefix buffer (e.g. a command/address byte) in the same chip-select bracket. Brackets the transfer with chip-select assert/deassert.
 
bool writeThenRead (const uint8_t *writeBuffer, size_t writeLen, uint8_t *readBuffer, size_t readLen, uint8_t sendValue=0xFF)
 Writes writeLen bytes, then reads readLen bytes, all within a single chip-select assert/deassert bracket – the standard "send a command, then read the response" register-access pattern.
 
uint8_t transfer (uint8_t send)
 Transfers (sends and receives simultaneously) one byte, without chip-select management – the caller must bracket this with beginTransaction()/endTransaction() itself.
 
void transfer (uint8_t *buffer, size_t len)
 Transfers (sends and receives simultaneously) len bytes in place, without chip-select management – the caller must bracket this with beginTransaction()/endTransaction() itself.
 
void beginTransaction ()
 Asserts chip-select (drives it low). No SPI settings to apply.
 
void endTransaction ()
 Deasserts chip-select (drives it high).
 

Detailed Description

Wraps one SPI peripheral + chip-select pin into read/write/transfer operations, mirroring the hardware-SPI subset of Adafruit_BusIO's Adafruit_SPIDevice API.

SPI mode, clock speed, and bit order are NOT configured by this class – unlike Arduino's SPI.beginTransaction(), a single STM32 SPI_HandleTypeDef is already fully configured once by CubeMX's generated MX_SPIx_Init(), so there is nothing to reconfigure per-transaction. beginTransaction() / endTransaction() are kept only for API parity with the reference library and to bracket chip-select; they do not touch SPI peripheral settings.

Warning
Not thread-safe or ISR-safe. Each transaction method (read()/write()/writeThenRead()/etc.) asserts chip-select, calls HAL_SPI_TransmitReceive() or HAL_SPI_Transmit(), then deasserts chip-select, with no locking around that sequence. Calling this object from more than one context (e.g. main loop plus an ISR, or two RTOS tasks) without external synchronization can interleave two transactions on the same physical CS pin and corrupt both. Restrict use to a single calling context, or add a mutex/critical section around each call if that changes.

Definition at line 56 of file STM32SPIDevice.hpp.

Constructor & Destructor Documentation

◆ STM32SPIDevice()

STM32SPIDevice::STM32SPIDevice ( SPI_HandleTypeDef * hspi,
GPIO_TypeDef * csPort,
uint16_t csPin,
uint32_t timeoutMs = 100 )

Constructs the device wrapper. Does not touch hardware – HAL_GPIO_Init()/HAL_SPI_Init() for the underlying pins/peripheral are assumed already done by CubeMX's generated MX_GPIO_Init() / MX_SPIx_Init(), called before this object is used.

Parameters
hspiPointer to the already-initialized SPI handle to use for every transaction.
csPortGPIO port of the chip-select pin.
csPinGPIO pin number of the chip-select pin.
timeoutMsPer-transaction HAL_SPI_TransmitReceive()/ HAL_SPI_Transmit() timeout, in milliseconds. Defaults to 100ms.

Definition at line 11 of file STM32SPIDevice.cpp.

Member Function Documentation

◆ begin()

void STM32SPIDevice::begin ( )

Deasserts chip-select (idle-high) so the bus starts in a known state. Call once before the first transaction.

Definition at line 15 of file STM32SPIDevice.cpp.

Referenced by TSC2046::begin().

◆ beginTransaction()

void STM32SPIDevice::beginTransaction ( )

Asserts chip-select (drives it low). No SPI settings to apply.

Definition at line 19 of file STM32SPIDevice.cpp.

Referenced by read(), write(), and writeThenRead().

◆ endTransaction()

void STM32SPIDevice::endTransaction ( )

Deasserts chip-select (drives it high).

Definition at line 23 of file STM32SPIDevice.cpp.

Referenced by read(), write(), and writeThenRead().

◆ read()

bool STM32SPIDevice::read ( uint8_t * buffer,
size_t len,
uint8_t sendValue = 0xFF )

Reads len bytes into buffer, clocking out sendValue for each byte read. Brackets the transfer with chip-select assert/deassert.

Parameters
bufferDestination buffer, at least len bytes.
lenNumber of bytes to read.
sendValueByte clocked out on MOSI while reading. Defaults to 0xFF.
Return values
trueon success (the underlying HAL call returned HAL_OK).

Definition at line 41 of file STM32SPIDevice.cpp.

References beginTransaction(), and endTransaction().

◆ transfer() [1/2]

void STM32SPIDevice::transfer ( uint8_t * buffer,
size_t len )

Transfers (sends and receives simultaneously) len bytes in place, without chip-select management – the caller must bracket this with beginTransaction()/endTransaction() itself.

Parameters
bufferBuffer to send from and receive into, at least len bytes.
lenNumber of bytes to transfer.

Definition at line 33 of file STM32SPIDevice.cpp.

◆ transfer() [2/2]

uint8_t STM32SPIDevice::transfer ( uint8_t send)

Transfers (sends and receives simultaneously) one byte, without chip-select management – the caller must bracket this with beginTransaction()/endTransaction() itself.

Parameters
sendByte to send.
Return values
Thebyte received while sending.

Definition at line 27 of file STM32SPIDevice.cpp.

◆ write()

bool STM32SPIDevice::write ( const uint8_t * buffer,
size_t len,
const uint8_t * prefixBuffer = nullptr,
size_t prefixLen = 0 )

Writes len bytes from buffer, optionally preceded by a separate prefix buffer (e.g. a command/address byte) in the same chip-select bracket. Brackets the transfer with chip-select assert/deassert.

Unlike Adafruit_SPIDevice's byte-at-a-time transfer() loop, this calls HAL_SPI_Transmit() (not TransmitReceive) once per buffer, since the received data is never used here – STM32 HAL supports a transmit-only call directly, so there is no need for a dummy receive buffer or a byte loop.

Parameters
bufferData to write.
lenNumber of bytes from buffer to write.
prefixBufferOptional data written before buffer, in the same transaction. Pass nullptr (the default) if not needed.
prefixLenNumber of bytes from prefixBuffer to write.
Return values
trueon success (HAL_OK for every phase written).

Definition at line 51 of file STM32SPIDevice.cpp.

References beginTransaction(), and endTransaction().

◆ writeThenRead()

bool STM32SPIDevice::writeThenRead ( const uint8_t * writeBuffer,
size_t writeLen,
uint8_t * readBuffer,
size_t readLen,
uint8_t sendValue = 0xFF )

Writes writeLen bytes, then reads readLen bytes, all within a single chip-select assert/deassert bracket – the standard "send a command, then read the response" register-access pattern.

Unlike Adafruit_SPIDevice's byte-at-a-time Arduino implementation, this builds one combined TX buffer (the write bytes followed by readLen dummy sendValue bytes) and issues a single HAL_SPI_TransmitReceive() call, since STM32 HAL supports multi-byte buffer transfers directly. Limited to kMaxCombinedBytes total bytes (writeLen + readLen) since the combined buffer is a fixed-size stack array, not heap-allocated – more than enough for the register-access pattern this method targets (a handful of command/address/data bytes), but not intended for bulk transfers (use read()/write() for those instead, which have no such limit).

Parameters
writeBufferCommand/address bytes to write first.
writeLenNumber of bytes from writeBuffer to write.
readBufferDestination for the bytes read back afterward.
readLenNumber of bytes to read into readBuffer.
sendValueByte clocked out on MOSI during the read phase. Defaults to 0xFF.
Return values
trueon success. false if writeLen + readLen exceeds kMaxCombinedBytes, or the underlying HAL call did not return HAL_OK.

Definition at line 66 of file STM32SPIDevice.cpp.

References beginTransaction(), and endTransaction().

Referenced by STM32BusIORegister::read().


The documentation for this class was generated from the following files: