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.hpp
1/*
2 * STM32SPIDevice.hpp
3 *
4 * STM32 HAL wrapper for a single SPI peripheral + chip-select pin, providing
5 * read/write/transfer helpers with automatic chip-select assert/deassert
6 * bracketing.
7 *
8 * Adapted from Adafruit_BusIO's Adafruit_SPIDevice
9 * (https://github.com/adafruit/Adafruit_BusIO, MIT License, Copyright (c)
10 * 2017 Adafruit Industries) as a design reference for the transaction API
11 * shape -- this is an independent STM32 HAL implementation, not a direct
12 * port of Adafruit's Arduino source (which depends on Arduino-only types
13 * this toolchain doesn't have). Only the hardware-SPI subset is ported:
14 * Adafruit_SPIDevice's software/bit-banged-SPI fallback path has no STM32
15 * HAL equivalent and is intentionally not included here.
16 *
17 * Unlike this project's other per-project drivers (e.g. W25QFlashSPI, which
18 * references its SPI handle and CS pin via CubeMX-generated externs), this
19 * class takes its SPI_HandleTypeDef and CS GPIO port/pin as constructor
20 * parameters instead. STM32BusIO is meant to be reused as-is by future
21 * Adafruit-derived tutorials on this site, each wiring up whatever
22 * peripheral instance and CS pin their own CubeMX config generates.
23 *
24 * Created on: Sep 15, 2026
25 * Author: johng
26 */
27
28#ifndef STM32BUSIO_STM32SPIDEVICE_HPP_
29#define STM32BUSIO_STM32SPIDEVICE_HPP_
30
31#include "main.h"
32#include <cstddef>
33
57public:
70 STM32SPIDevice(SPI_HandleTypeDef *hspi, GPIO_TypeDef *csPort, uint16_t csPin, uint32_t timeoutMs = 100);
71
76 void begin();
77
88 bool read(uint8_t *buffer, size_t len, uint8_t sendValue = 0xFF);
89
109 bool write(const uint8_t *buffer, size_t len, const uint8_t *prefixBuffer = nullptr, size_t prefixLen = 0);
110
138 bool writeThenRead(const uint8_t *writeBuffer, size_t writeLen, uint8_t *readBuffer, size_t readLen, uint8_t sendValue = 0xFF);
139
147 uint8_t transfer(uint8_t send);
148
157 void transfer(uint8_t *buffer, size_t len);
158
160 void beginTransaction();
161
163 void endTransaction();
164
165private:
169 static constexpr size_t kMaxCombinedBytes = 32;
170
171 SPI_HandleTypeDef *_hspi;
172 GPIO_TypeDef *_csPort;
173 uint16_t _csPin;
174 uint32_t _timeoutMs;
175};
176
177#endif /* STM32BUSIO_STM32SPIDEVICE_HPP_ */
Wraps one SPI peripheral + chip-select pin into read/write/transfer operations, mirroring the hardwar...
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...
: Header for main.c file. This file contains the common defines of the application.