|
H753_CPP_SPI_Resistive_Touch_Screen_Controller 1.0
STM32H753 SPI resistive touchscreen (Adafruit 333 overlay + 5767 TSC2046 controller) driver and tutorial
|
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). | |
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.
Definition at line 56 of file STM32SPIDevice.hpp.
| 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.
| hspi | Pointer to the already-initialized SPI handle to use for every transaction. |
| csPort | GPIO port of the chip-select pin. |
| csPin | GPIO pin number of the chip-select pin. |
| timeoutMs | Per-transaction HAL_SPI_TransmitReceive()/ HAL_SPI_Transmit() timeout, in milliseconds. Defaults to 100ms. |
Definition at line 11 of file STM32SPIDevice.cpp.
| 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().
| 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().
| void STM32SPIDevice::endTransaction | ( | ) |
Deasserts chip-select (drives it high).
Definition at line 23 of file STM32SPIDevice.cpp.
Referenced by read(), write(), and writeThenRead().
| 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.
| buffer | Destination buffer, at least len bytes. |
| len | Number of bytes to read. |
| sendValue | Byte clocked out on MOSI while reading. Defaults to 0xFF. |
| true | on success (the underlying HAL call returned HAL_OK). |
Definition at line 41 of file STM32SPIDevice.cpp.
References beginTransaction(), and endTransaction().
| 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.
| buffer | Buffer to send from and receive into, at least len bytes. |
| len | Number of bytes to transfer. |
Definition at line 33 of file STM32SPIDevice.cpp.
| 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.
| send | Byte to send. |
| The | byte received while sending. |
Definition at line 27 of file STM32SPIDevice.cpp.
| 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.
| buffer | Data to write. |
| len | Number of bytes from buffer to write. |
| prefixBuffer | Optional data written before buffer, in the same transaction. Pass nullptr (the default) if not needed. |
| prefixLen | Number of bytes from prefixBuffer to write. |
| true | on success (HAL_OK for every phase written). |
Definition at line 51 of file STM32SPIDevice.cpp.
References beginTransaction(), and endTransaction().
| 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).
| writeBuffer | Command/address bytes to write first. |
| writeLen | Number of bytes from writeBuffer to write. |
| readBuffer | Destination for the bytes read back afterward. |
| readLen | Number of bytes to read into readBuffer. |
| sendValue | Byte clocked out on MOSI during the read phase. Defaults to 0xFF. |
| true | on 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().