36#ifdef ADAFRUIT_RA8875_DEBUG
39#define RA8875_DEBUG_PRINTF(...) printf(__VA_ARGS__)
41#define RA8875_DEBUG_PRINTF(...) ((void)0)
45#define RA8875_SPI_TIMEOUT_MS 100
59__attribute__((weak)) uint32_t Adafruit_RA8875_GetSpiClockHz(SPI_HandleTypeDef* hspi) {
61 uint32_t periph = RCC_PERIPHCLK_SPI123;
63 if (hspi->Instance == SPI4 || hspi->Instance == SPI5)
64 periph = RCC_PERIPHCLK_SPI45;
67 if (hspi->Instance == SPI6)
68 periph = RCC_PERIPHCLK_SPI6;
70 return HAL_RCCEx_GetPeriphCLKFreq(periph);
74 apb2 = apb2 || (hspi->Instance == SPI1);
77 apb2 = apb2 || (hspi->Instance == SPI4);
80 apb2 = apb2 || (hspi->Instance == SPI5);
83 apb2 = apb2 || (hspi->Instance == SPI6);
85 return apb2 ? HAL_RCC_GetPCLK2Freq() : HAL_RCC_GetPCLK1Freq();
103 uint16_t csPin, GPIO_TypeDef* rstPort,
104 uint16_t rstPin, GPIO_TypeDef* waitPort,
106 :
Adafruit_GFX(800, 480), _spi(spi), _csPort(csPort), _rstPort(rstPort),
107 _waitPort(waitPort), _csPin(csPin), _rstPin(rstPin), _waitPin(waitPin),
108 _storageRead(nullptr), _storageWrite(nullptr), _storageCtx(nullptr),
112void Adafruit_RA8875::csLow(
void) {
113 HAL_GPIO_WritePin(_csPort, _csPin, GPIO_PIN_RESET);
116void Adafruit_RA8875::csHigh(
void) {
117 HAL_GPIO_WritePin(_csPort, _csPin, GPIO_PIN_SET);
122void Adafruit_RA8875::waitForReady(
void) {
123 if (_waitPort ==
nullptr)
125 while (!HAL_GPIO_ReadPin(_waitPort, _waitPin)) {
130uint8_t Adafruit_RA8875::spiTransfer(uint8_t d) {
136void Adafruit_RA8875::spiWrite(
const uint8_t* data, uint16_t len) {
140bool Adafruit_RA8875::spiConfigOk(
void) {
141 return _spi !=
nullptr && _spi->Init.Mode == SPI_MODE_MASTER &&
142 _spi->Init.Direction == SPI_DIRECTION_2LINES &&
143 _spi->Init.DataSize == SPI_DATASIZE_8BIT &&
144 _spi->Init.CLKPolarity == SPI_POLARITY_LOW &&
145 _spi->Init.CLKPhase == SPI_PHASE_1EDGE &&
146 _spi->Init.FirstBit == SPI_FIRSTBIT_MSB &&
147 _spi->Init.NSS == SPI_NSS_SOFT;
155void Adafruit_RA8875::spiSetSpeed(uint32_t hz) {
156 static const struct {
160 {2, SPI_BAUDRATEPRESCALER_2}, {4, SPI_BAUDRATEPRESCALER_4},
161 {8, SPI_BAUDRATEPRESCALER_8}, {16, SPI_BAUDRATEPRESCALER_16},
162 {32, SPI_BAUDRATEPRESCALER_32}, {64, SPI_BAUDRATEPRESCALER_64},
163 {128, SPI_BAUDRATEPRESCALER_128}, {256, SPI_BAUDRATEPRESCALER_256}};
164 uint32_t clk = Adafruit_RA8875_GetSpiClockHz(_spi);
167 uint32_t reg = SPI_BAUDRATEPRESCALER_256;
168 for (
unsigned i = 0; i <
sizeof(table) /
sizeof(table[0]); i++) {
169 if (clk / table[i].div <= hz) {
174 if (_spi->Init.BaudRatePrescaler != reg) {
175 _spi->Init.BaudRatePrescaler = reg;
212 if (!spiConfigOk()) {
218 HAL_GPIO_WritePin(_rstPort, _rstPin, GPIO_PIN_RESET);
220 HAL_GPIO_WritePin(_rstPort, _rstPin, GPIO_PIN_SET);
233 spiSetSpeed(4000000);
257void Adafruit_RA8875::PLLinit(
void) {
277void Adafruit_RA8875::initialize(
void) {
285 uint8_t hsync_finetune;
286 uint8_t hsync_nondisp;
288 uint16_t vsync_nondisp;
289 uint16_t vsync_start;
358 (uint16_t)(_height - 1 + _voffset) & 0xFF);
436 temp &= ~((1 << 7) | (1 << 5));
449 x = applyRotationX(x);
450 y = applyRotationY(y);
587 len = strlen(buffer);
589 for (uint16_t i = 0; i < len; i++) {
620 temp &= ~RA8875_MWCR0_TXTMODE;
637 uint8_t temp =
readReg(regname);
638 if (!(temp & waitflag))
672 uint32_t n = (num >
sizeof(buf) / 2) ?
sizeof(buf) / 2 : num;
673 for (uint32_t i = 0; i < n; i++) {
674 buf[2 * i] = (uint8_t)(p >> 8);
675 buf[2 * i + 1] = (uint8_t)p;
677 spiWrite(buf, (uint16_t)(n * 2));
702int16_t Adafruit_RA8875::applyRotationX(int16_t x) {
719int16_t Adafruit_RA8875::applyRotationY(int16_t y) {
739 x = applyRotationX(x);
740 y = applyRotationY(y);
749 spiWrite(px,
sizeof(px));
765 x = applyRotationX(x);
766 y = applyRotationY(y);
774 if (_rotation == 2) {
784 uint32_t n = (num >
sizeof(buf) / 2) ?
sizeof(buf) / 2 : num;
785 for (uint32_t i = 0; i < n; i++) {
786 buf[2 * i] = (uint8_t)(p[i] >> 8);
787 buf[2 * i + 1] = (uint8_t)p[i];
789 spiWrite(buf, (uint16_t)(n * 2));
809 x0 = applyRotationX(x0);
810 y0 = applyRotationY(y0);
811 x1 = applyRotationX(x1);
812 y1 = applyRotationY(y1);
897 rectHelper(x, y, x + w - 1, y + h - 1, color,
false);
913 rectHelper(x, y, x + w - 1, y + h - 1, color,
true);
924 rectHelper(0, 0, _width - 1, _height - 1, color,
true);
939 circleHelper(x, y, r, color,
false);
954 circleHelper(x, y, r, color,
true);
971 int16_t y1, int16_t x2, int16_t y2,
973 triangleHelper(x0, y0, x1, y1, x2, y2, color,
false);
990 int16_t y1, int16_t x2, int16_t y2,
992 triangleHelper(x0, y0, x1, y1, x2, y2, color,
true);
1007 int16_t longAxis, int16_t shortAxis,
1009 ellipseHelper(xCenter, yCenter, longAxis, shortAxis, color,
false);
1024 int16_t longAxis, int16_t shortAxis,
1026 ellipseHelper(xCenter, yCenter, longAxis, shortAxis, color,
true);
1046 int16_t longAxis, int16_t shortAxis,
1047 uint8_t curvePart, uint16_t color) {
1048 curveHelper(xCenter, yCenter, longAxis, shortAxis, curvePart, color,
false);
1068 int16_t longAxis, int16_t shortAxis,
1069 uint8_t curvePart, uint16_t color) {
1070 curveHelper(xCenter, yCenter, longAxis, shortAxis, curvePart, color,
true);
1086 int16_t r, uint16_t color) {
1087 roundRectHelper(x, y, x + w, y + h, r, color,
false);
1103 int16_t r, uint16_t color) {
1104 roundRectHelper(x, y, x + w, y + h, r, color,
true);
1112void Adafruit_RA8875::circleHelper(int16_t x, int16_t y, int16_t r,
1113 uint16_t color,
bool filled) {
1114 x = applyRotationX(x);
1115 y = applyRotationY(y);
1158void Adafruit_RA8875::rectHelper(int16_t x, int16_t y, int16_t w, int16_t h,
1159 uint16_t color,
bool filled) {
1160 x = applyRotationX(x);
1161 y = applyRotationY(y);
1162 w = applyRotationX(w);
1163 h = applyRotationY(h);
1214void Adafruit_RA8875::triangleHelper(int16_t x0, int16_t y0, int16_t x1,
1215 int16_t y1, int16_t x2, int16_t y2,
1216 uint16_t color,
bool filled) {
1217 x0 = applyRotationX(x0);
1218 y0 = applyRotationY(y0);
1219 x1 = applyRotationX(x1);
1220 y1 = applyRotationY(y1);
1221 x2 = applyRotationX(x2);
1222 y2 = applyRotationY(y2);
1279void Adafruit_RA8875::ellipseHelper(int16_t xCenter, int16_t yCenter,
1280 int16_t longAxis, int16_t shortAxis,
1281 uint16_t color,
bool filled) {
1282 xCenter = applyRotationX(xCenter);
1283 yCenter = applyRotationY(yCenter);
1330void Adafruit_RA8875::curveHelper(int16_t xCenter, int16_t yCenter,
1331 int16_t longAxis, int16_t shortAxis,
1332 uint8_t curvePart, uint16_t color,
1334 xCenter = applyRotationX(xCenter);
1335 yCenter = applyRotationY(yCenter);
1336 curvePart = (curvePart + _rotation) % 4;
1383void Adafruit_RA8875::roundRectHelper(int16_t x, int16_t y, int16_t w,
1384 int16_t h, int16_t r, uint16_t color,
1386 x = applyRotationX(x);
1387 y = applyRotationY(y);
1388 w = applyRotationX(w);
1389 h = applyRotationY(h);
1461 int16_t h, uint8_t mode) {
1660 ty |= (temp >> 2) & 0x03;
1760 uint8_t x = spiTransfer(0x0);
1800 uint8_t x = spiTransfer(0x0);
1821 StorageWriteFn write,
void* ctx,
1822 uint32_t sizeBytes) {
1823 _storageRead = read;
1824 _storageWrite =
write;
1826 _storageSize = sizeBytes;
1829bool Adafruit_RA8875::storageReadBytes(uint32_t address, uint8_t* data,
1831 return _storageRead && _storageRead(_storageCtx, address, data, length);
1834bool Adafruit_RA8875::storageWriteBytes(uint32_t address,
const uint8_t* data,
1836 return _storageWrite && _storageWrite(_storageCtx, address, data, length);
1849 uint8_t b[4] = {0, 0, 0, 0};
1850 if (!storageReadBytes((uint32_t)location, b, 4))
1852 return ((uint32_t)b[0] << 24) | ((uint32_t)b[1] << 16) |
1853 ((uint32_t)b[2] << 8) | (uint32_t)b[3];
1865 uint8_t b[4] = {(uint8_t)((value >> 24) & 0xff), (uint8_t)((value >> 16) & 0xff),
1866 (uint8_t)((value >> 8) & 0xff), (uint8_t)(value & 0xff)};
1867 storageWriteBytes((uint32_t)location, b, 4);
1881 if (location +
sizeof(
tsMatrix_t) > _storageSize) {
1884 uint8_t calibrated = 0;
1888 if (calibrated == 1) {
1912 if (location +
sizeof(
tsMatrix_t) < _storageSize) {
1921 const uint8_t one = 1;
#define RA8875_DEBUG_PRINTF(...)
Diagnostics compiled out.
__attribute__((weak)) uint32_t Adafruit_RA8875_GetSpiClockHz(SPI_HandleTypeDef *hspi)
Frequency of the clock feeding an SPI peripheral (before the baud prescaler). Weak: override it in yo...
#define RA8875_SPI_TIMEOUT_MS
SPI transfer timeout in milliseconds.
#define spi_end()
Not needed on STM32.
#define spi_begin()
Not needed on STM32.
#define RA8875_VSTR0
See datasheet.
#define RA8875_VSAW1
See datasheet.
#define RA8875_HDWR
See datasheet.
#define RA8875_INTC2_TP
See datasheet.
#define RA8875_TPCR1_DEBOUNCE
See datasheet.
#define RA8875_SYSR_MCU8
See datasheet.
#define RA8875_VNDR1
See datasheet.
#define RA8875_HEAW1
See datasheet.
#define RA8875_HNDFTR_DE_HIGH
See datasheet.
#define CFG_EEPROM_TOUCHSCREEN_CAL_FN
EEPROM Storage Location.
#define RA8875_DCR_DRAWSQUARE
See datasheet.
#define RA8875_HPWR
See datasheet.
#define RA8875_CURV0
See datasheet.
#define RA8875_TPCR0_ENABLE
See datasheet.
#define RA8875_TPCR0_ADCCLK_DIV4
See datasheet.
#define RA8875_VEAW1
See datasheet.
#define RA8875_MRWC
See datasheet.
#define RA8875_MWCR0_CURSOR
See datasheet.
#define RA8875_MWCR0_LRTD
Left->Right then Top->Down.
#define RA8875_DCR_FILL
See datasheet.
#define RA8875_DCR_CIRCLE_STATUS
See datasheet.
#define CFG_EEPROM_TOUCHSCREEN_CAL_BN
EEPROM Storage Location.
#define RA8875_P1CR_ENABLE
See datasheet.
#define RA8875_DATAREAD
See datasheet.
#define RA8875_TPYH
See datasheet.
#define RA8875_PWRR_SOFTRESET
See datasheet.
#define RA8875_GPIOX
See datasheet.
#define RA8875_MWCR0_RLTD
Right->Left then Top->Down.
#define RA8875_VNDR0
See datasheet.
#define RA8875_PCSR_4CLK
See datasheet.
#define RA8875_CURV1
See datasheet.
#define CFG_EEPROM_TOUCHSCREEN_CALIBRATED
EEPROM Storage Location.
#define RA8875_VSAW0
See datasheet.
#define RA8875_DCR_CIRCLE_START
See datasheet.
#define RA8875_DCR_NOFILL
See datasheet.
#define RA8875_VEAW0
See datasheet.
#define RA8875_HSAW1
See datasheet.
#define RA8875_INTC1
See datasheet.
#define RA8875_P1CR_DISABLE
See datasheet.
#define RA8875_PWRR_NORMAL
See datasheet.
#define RA8875_SYSR_16BPP
See datasheet.
#define RA8875_PWRR_SLEEP
See datasheet.
#define RA8875_VDHR0
See datasheet.
#define RA8875_DCR_LINESQUTRI_STATUS
See datasheet.
#define RA8875_PLLC1
See datasheet.
#define RA8875_ELLIPSE_STATUS
See datasheet.
#define RA8875_PWRR
See datasheet.
#define RA8875_ELLIPSE
See datasheet.
#define RA8875_MWCR0_DIRMASK
Bitmask for Write Direction.
#define RA8875_VSTR1
See datasheet.
#define RA8875_HSTR
See datasheet.
#define RA8875_P2CR
See datasheet.
#define RA8875_HEAW0
See datasheet.
#define RA8875_PLLC2
See datasheet.
#define CFG_EEPROM_TOUCHSCREEN_CAL_DN
EEPROM Storage Location.
#define RA8875_PCSR
See datasheet.
#define RA8875_PCSR_2CLK
See datasheet.
#define RA8875_HPWR_LOW
See datasheet.
#define RA8875_PLLC2_DIV4
See datasheet.
#define RA8875_CMDWRITE
See datasheet.
#define RA8875_DCR_LINESQUTRI_STOP
See datasheet.
#define RA8875_TPCR0_WAIT_4096CLK
See datasheet.
#define RA8875_TPCR0
See datasheet.
#define RA8875_CMDREAD
See datasheet.
#define RA8875_MWCR0_BLINK
See datasheet.
#define RA8875_PWRR_DISPOFF
See datasheet.
#define RA8875_P2CR_ENABLE
See datasheet.
#define RA8875_MWCR0_TXTMODE
See datasheet.
#define RA8875_PLLC1_PLLDIV1
See datasheet.
#define RA8875_MWCR0
See datasheet.
#define RA8875_DCR_LINESQUTRI_START
See datasheet.
#define RA8875_PCSR_PDATL
See datasheet.
#define RA8875_DCR
See datasheet.
#define RA8875_DATAWRITE
See datasheet.
#define CFG_EEPROM_TOUCHSCREEN_CAL_CN
EEPROM Storage Location.
#define RA8875_CURH0
See datasheet.
#define RA8875_TPCR1
See datasheet.
#define RA8875_P1CR
See datasheet.
#define RA8875_PWRR_DISPON
See datasheet.
#define RA8875_P2CR_DISABLE
See datasheet.
#define RA8875_HNDR
See datasheet.
#define RA8875_P1DCR
See datasheet.
#define CFG_EEPROM_TOUCHSCREEN_CAL_EN
EEPROM Storage Location.
#define RA8875_P2DCR
See datasheet.
#define RA8875_TPCR0_DISABLE
See datasheet.
#define RA8875_INTC1_TP
See datasheet.
#define RA8875_MCLR
See datasheet.
#define RA8875_TPCR0_WAKEENABLE
See datasheet.
#define RA8875_TPCR1_AUTO
See datasheet.
#define RA8875_SYSR
See datasheet.
#define RA8875_BTCR
See datasheet.
#define RA8875_INTC2
See datasheet.
#define CFG_EEPROM_TOUCHSCREEN_CAL_DIVIDER
EEPROM Storage Location.
#define RA8875_VDHR1
See datasheet.
#define RA8875_MCLR_FULL
See datasheet.
#define RA8875_VPWR_LOW
See datasheet.
#define RA8875_TPCR0_ADCCLK_DIV16
See datasheet.
#define RA8875_VPWR
See datasheet.
#define RA8875_TPXH
See datasheet.
#define RA8875_HNDFTR
See datasheet.
#define RA8875_HSAW0
See datasheet.
#define RA8875_TPXYL
See datasheet.
#define RA8875_MCLR_START
See datasheet.
#define RA8875_CURH1
See datasheet.
#define CFG_EEPROM_TOUCHSCREEN_CAL_AN
EEPROM Storage Location.
uint8_t rotation
Display rotation (0 thru 3)
void fillRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, uint16_t color)
bool readCalibration(int location, tsMatrix_t *matrixPtr)
boolean touchRead(uint16_t *x, uint16_t *y)
void drawPixel(int16_t x, int16_t y, uint16_t color)
void touchEnable(boolean on)
void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color)
void sleep(boolean sleep)
void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color)
void fillCircle(int16_t x, int16_t y, int16_t r, uint16_t color)
void fillScreen(uint16_t color)
void writeReg(uint8_t reg, uint8_t val)
void fillCurve(int16_t xCenter, int16_t yCenter, int16_t longAxis, int16_t shortAxis, uint8_t curvePart, uint16_t color)
Adafruit_RA8875(SPI_HandleTypeDef *spi, GPIO_TypeDef *csPort, uint16_t csPin, GPIO_TypeDef *rstPort, uint16_t rstPin, GPIO_TypeDef *waitPort=nullptr, uint16_t waitPin=0)
void PWM2config(boolean on, uint8_t clock)
void writeCalibration(int location, tsMatrix_t *matrixPtr)
void pushPixels(uint32_t num, uint16_t p)
void writeCommand(uint8_t d)
virtual size_t write(uint8_t b)
void setScrollWindow(int16_t x, int16_t y, int16_t w, int16_t h, uint8_t mode)
void drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
void fillEllipse(int16_t xCenter, int16_t yCenter, int16_t longAxis, int16_t shortAxis, uint16_t color)
void drawRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, uint16_t color)
uint32_t eepromReadS32(int location)
void cursorBlink(uint8_t rate)
boolean begin(enum RA8875sizes s)
void setXY(uint16_t x, uint16_t y)
void drawCircle(int16_t x, int16_t y, int16_t r, uint16_t color)
void textEnlarge(uint8_t scale)
void textSetCursor(uint16_t x, uint16_t y)
void fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color)
void drawCurve(int16_t xCenter, int16_t yCenter, int16_t longAxis, int16_t shortAxis, uint8_t curvePart, uint16_t color)
void scrollY(int16_t dist)
void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color)
void setCalibrationStorage(StorageReadFn read, StorageWriteFn write, void *ctx, uint32_t sizeBytes)
void textColor(uint16_t foreColor, uint16_t bgColor)
void drawPixels(uint16_t *p, uint32_t count, int16_t x, int16_t y)
void displayOn(boolean on)
uint8_t readReg(uint8_t reg)
boolean waitPoll(uint8_t r, uint8_t f)
void drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color)
void writeData(uint8_t d)
void textTransparent(uint16_t foreColor)
void PWM1config(boolean on, uint8_t clock)
void setRotation(int8_t rotation)
void textWrite(const char *buffer, uint16_t len=0)
void scrollX(int16_t dist)
void drawEllipse(int16_t xCenter, int16_t yCenter, int16_t longAxis, int16_t shortAxis, uint16_t color)
void eepromWriteS32(int location, int32_t value)