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.cpp
1/*
2 * STM32SPIDevice.cpp
3 *
4 * Created on: Sep 15, 2026
5 * Author: johng
6 */
7
8#include "STM32SPIDevice.hpp"
9#include <cstring>
10
11STM32SPIDevice::STM32SPIDevice(SPI_HandleTypeDef *hspi, GPIO_TypeDef *csPort, uint16_t csPin, uint32_t timeoutMs)
12 : _hspi(hspi), _csPort(csPort), _csPin(csPin), _timeoutMs(timeoutMs) {
13}
14
16 HAL_GPIO_WritePin(_csPort, _csPin, GPIO_PIN_SET);
17}
18
20 HAL_GPIO_WritePin(_csPort, _csPin, GPIO_PIN_RESET);
21}
22
24 HAL_GPIO_WritePin(_csPort, _csPin, GPIO_PIN_SET);
25}
26
27uint8_t STM32SPIDevice::transfer(uint8_t send) {
28 uint8_t received = 0xFF;
29 HAL_SPI_TransmitReceive(_hspi, &send, &received, 1, _timeoutMs);
30 return received;
31}
32
33void STM32SPIDevice::transfer(uint8_t *buffer, size_t len) {
34 // Same buffer for TX and RX: safe here because HAL's blocking
35 // implementation processes one byte at a time, fully transmitting
36 // buffer[i] before overwriting it with the received byte -- by the time
37 // index i is written, its transmitted value has already gone out.
38 HAL_SPI_TransmitReceive(_hspi, buffer, buffer, len, _timeoutMs);
39}
40
41bool STM32SPIDevice::read(uint8_t *buffer, size_t len, uint8_t sendValue) {
42 memset(buffer, sendValue, len);
43
45 HAL_StatusTypeDef status = HAL_SPI_TransmitReceive(_hspi, buffer, buffer, len, _timeoutMs);
47
48 return status == HAL_OK;
49}
50
51bool STM32SPIDevice::write(const uint8_t *buffer, size_t len, const uint8_t *prefixBuffer, size_t prefixLen) {
53
54 HAL_StatusTypeDef status = HAL_OK;
55 if (prefixLen > 0) {
56 status = HAL_SPI_Transmit(_hspi, const_cast<uint8_t *>(prefixBuffer), prefixLen, _timeoutMs);
57 }
58 if (status == HAL_OK && len > 0) {
59 status = HAL_SPI_Transmit(_hspi, const_cast<uint8_t *>(buffer), len, _timeoutMs);
60 }
61
63 return status == HAL_OK;
64}
65
66bool STM32SPIDevice::writeThenRead(const uint8_t *writeBuffer, size_t writeLen, uint8_t *readBuffer, size_t readLen, uint8_t sendValue) {
67 if (writeLen + readLen > kMaxCombinedBytes) {
68 return false;
69 }
70
71 uint8_t txBuffer[kMaxCombinedBytes];
72 uint8_t rxBuffer[kMaxCombinedBytes];
73
74 memcpy(txBuffer, writeBuffer, writeLen);
75 memset(txBuffer + writeLen, sendValue, readLen);
76
78 HAL_StatusTypeDef status = HAL_SPI_TransmitReceive(_hspi, txBuffer, rxBuffer, writeLen + readLen, _timeoutMs);
80
81 if (status != HAL_OK) {
82 return false;
83 }
84
85 memcpy(readBuffer, rxBuffer + writeLen, readLen);
86 return true;
87}
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....
void begin()
Deasserts chip-select (idle-high) so the bus starts in a known state. Call once before the first tran...
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 ch...
void endTransaction()
Deasserts chip-select (drives it high).
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 brac...
void beginTransaction()
Asserts chip-select (drives it low). No SPI settings to apply.
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 under...
uint8_t transfer(uint8_t send)
Transfers (sends and receives simultaneously) one byte, without chip-select management – the caller m...