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),
114void Adafruit_RA8875::csLow(
void) {
115 HAL_GPIO_WritePin(_csPort, _csPin, GPIO_PIN_RESET);
120void Adafruit_RA8875::csHigh(
void) {
121 HAL_GPIO_WritePin(_csPort, _csPin, GPIO_PIN_SET);
129void Adafruit_RA8875::waitForReady(
void) {
130 if (_waitPort ==
nullptr)
132 while (!HAL_GPIO_ReadPin(_waitPort, _waitPin)) {
144uint8_t Adafruit_RA8875::spiTransfer(uint8_t d) {
156void Adafruit_RA8875::spiWrite(
const uint8_t* data, uint16_t len) {
164bool Adafruit_RA8875::spiConfigOk(
void) {
165 return _spi !=
nullptr && _spi->Init.Mode == SPI_MODE_MASTER &&
166 _spi->Init.Direction == SPI_DIRECTION_2LINES &&
167 _spi->Init.DataSize == SPI_DATASIZE_8BIT &&
168 _spi->Init.CLKPolarity == SPI_POLARITY_LOW &&
169 _spi->Init.CLKPhase == SPI_PHASE_1EDGE &&
170 _spi->Init.FirstBit == SPI_FIRSTBIT_MSB &&
171 _spi->Init.NSS == SPI_NSS_SOFT;
179void Adafruit_RA8875::spiSetSpeed(uint32_t hz) {
180 static const struct {
184 {2, SPI_BAUDRATEPRESCALER_2}, {4, SPI_BAUDRATEPRESCALER_4},
185 {8, SPI_BAUDRATEPRESCALER_8}, {16, SPI_BAUDRATEPRESCALER_16},
186 {32, SPI_BAUDRATEPRESCALER_32}, {64, SPI_BAUDRATEPRESCALER_64},
187 {128, SPI_BAUDRATEPRESCALER_128}, {256, SPI_BAUDRATEPRESCALER_256}};
188 uint32_t clk = Adafruit_RA8875_GetSpiClockHz(_spi);
191 uint32_t reg = SPI_BAUDRATEPRESCALER_256;
192 for (
unsigned i = 0; i <
sizeof(table) /
sizeof(table[0]); i++) {
193 if (clk / table[i].div <= hz) {
198 if (_spi->Init.BaudRatePrescaler != reg) {
199 _spi->Init.BaudRatePrescaler = reg;
236 if (!spiConfigOk()) {
242 HAL_GPIO_WritePin(_rstPort, _rstPin, GPIO_PIN_RESET);
244 HAL_GPIO_WritePin(_rstPort, _rstPin, GPIO_PIN_SET);
257 spiSetSpeed(4000000);
281void Adafruit_RA8875::PLLinit(
void) {
301void Adafruit_RA8875::initialize(
void) {
309 uint8_t hsync_finetune;
310 uint8_t hsync_nondisp;
312 uint16_t vsync_nondisp;
313 uint16_t vsync_start;
382 (uint16_t)(_height - 1 + _voffset) & 0xFF);
460 temp &= ~((1 << 7) | (1 << 5));
473 x = applyRotationX(x);
474 y = applyRotationY(y);
611 len = strlen(buffer);
613 for (uint16_t i = 0; i < len; i++) {
644 temp &= ~RA8875_MWCR0_TXTMODE;
661 uint8_t temp =
readReg(regname);
662 if (!(temp & waitflag))
696 uint32_t n = (num >
sizeof(buf) / 2) ?
sizeof(buf) / 2 : num;
697 for (uint32_t i = 0; i < n; i++) {
698 buf[2 * i] = (uint8_t)(p >> 8);
699 buf[2 * i + 1] = (uint8_t)p;
701 spiWrite(buf, (uint16_t)(n * 2));
726int16_t Adafruit_RA8875::applyRotationX(int16_t x) {
743int16_t Adafruit_RA8875::applyRotationY(int16_t y) {
763 x = applyRotationX(x);
764 y = applyRotationY(y);
773 spiWrite(px,
sizeof(px));
789 x = applyRotationX(x);
790 y = applyRotationY(y);
798 if (_rotation == 2) {
808 uint32_t n = (num >
sizeof(buf) / 2) ?
sizeof(buf) / 2 : num;
809 for (uint32_t i = 0; i < n; i++) {
810 buf[2 * i] = (uint8_t)(p[i] >> 8);
811 buf[2 * i + 1] = (uint8_t)p[i];
813 spiWrite(buf, (uint16_t)(n * 2));
833 x0 = applyRotationX(x0);
834 y0 = applyRotationY(y0);
835 x1 = applyRotationX(x1);
836 y1 = applyRotationY(y1);
921 rectHelper(x, y, x + w - 1, y + h - 1, color,
false);
937 rectHelper(x, y, x + w - 1, y + h - 1, color,
true);
948 rectHelper(0, 0, _width - 1, _height - 1, color,
true);
963 circleHelper(x, y, r, color,
false);
978 circleHelper(x, y, r, color,
true);
995 int16_t y1, int16_t x2, int16_t y2,
997 triangleHelper(x0, y0, x1, y1, x2, y2, color,
false);
1014 int16_t y1, int16_t x2, int16_t y2,
1016 triangleHelper(x0, y0, x1, y1, x2, y2, color,
true);
1031 int16_t longAxis, int16_t shortAxis,
1033 ellipseHelper(xCenter, yCenter, longAxis, shortAxis, color,
false);
1048 int16_t longAxis, int16_t shortAxis,
1050 ellipseHelper(xCenter, yCenter, longAxis, shortAxis, color,
true);
1070 int16_t longAxis, int16_t shortAxis,
1071 uint8_t curvePart, uint16_t color) {
1072 curveHelper(xCenter, yCenter, longAxis, shortAxis, curvePart, color,
false);
1092 int16_t longAxis, int16_t shortAxis,
1093 uint8_t curvePart, uint16_t color) {
1094 curveHelper(xCenter, yCenter, longAxis, shortAxis, curvePart, color,
true);
1110 int16_t r, uint16_t color) {
1111 roundRectHelper(x, y, x + w, y + h, r, color,
false);
1127 int16_t r, uint16_t color) {
1128 roundRectHelper(x, y, x + w, y + h, r, color,
true);
1136void Adafruit_RA8875::circleHelper(int16_t x, int16_t y, int16_t r,
1137 uint16_t color,
bool filled) {
1138 x = applyRotationX(x);
1139 y = applyRotationY(y);
1182void Adafruit_RA8875::rectHelper(int16_t x, int16_t y, int16_t w, int16_t h,
1183 uint16_t color,
bool filled) {
1184 x = applyRotationX(x);
1185 y = applyRotationY(y);
1186 w = applyRotationX(w);
1187 h = applyRotationY(h);
1238void Adafruit_RA8875::triangleHelper(int16_t x0, int16_t y0, int16_t x1,
1239 int16_t y1, int16_t x2, int16_t y2,
1240 uint16_t color,
bool filled) {
1241 x0 = applyRotationX(x0);
1242 y0 = applyRotationY(y0);
1243 x1 = applyRotationX(x1);
1244 y1 = applyRotationY(y1);
1245 x2 = applyRotationX(x2);
1246 y2 = applyRotationY(y2);
1303void Adafruit_RA8875::ellipseHelper(int16_t xCenter, int16_t yCenter,
1304 int16_t longAxis, int16_t shortAxis,
1305 uint16_t color,
bool filled) {
1306 xCenter = applyRotationX(xCenter);
1307 yCenter = applyRotationY(yCenter);
1354void Adafruit_RA8875::curveHelper(int16_t xCenter, int16_t yCenter,
1355 int16_t longAxis, int16_t shortAxis,
1356 uint8_t curvePart, uint16_t color,
1358 xCenter = applyRotationX(xCenter);
1359 yCenter = applyRotationY(yCenter);
1360 curvePart = (curvePart + _rotation) % 4;
1407void Adafruit_RA8875::roundRectHelper(int16_t x, int16_t y, int16_t w,
1408 int16_t h, int16_t r, uint16_t color,
1410 x = applyRotationX(x);
1411 y = applyRotationY(y);
1412 w = applyRotationX(w);
1413 h = applyRotationY(h);
1485 int16_t h, uint8_t mode) {
1684 ty |= (temp >> 2) & 0x03;
1784 uint8_t x = spiTransfer(0x0);
1824 uint8_t x = spiTransfer(0x0);
1845 StorageWriteFn write,
void* ctx,
1846 uint32_t sizeBytes) {
1847 _storageRead = read;
1848 _storageWrite =
write;
1850 _storageSize = sizeBytes;
1860bool Adafruit_RA8875::storageReadBytes(uint32_t address, uint8_t* data,
1862 return _storageRead && _storageRead(_storageCtx, address, data, length);
1872bool Adafruit_RA8875::storageWriteBytes(uint32_t address,
const uint8_t* data,
1874 return _storageWrite && _storageWrite(_storageCtx, address, data, length);
1887 uint8_t b[4] = {0, 0, 0, 0};
1888 if (!storageReadBytes((uint32_t)location, b, 4))
1890 return ((uint32_t)b[0] << 24) | ((uint32_t)b[1] << 16) |
1891 ((uint32_t)b[2] << 8) | (uint32_t)b[3];
1903 uint8_t b[4] = {(uint8_t)((value >> 24) & 0xff), (uint8_t)((value >> 16) & 0xff),
1904 (uint8_t)((value >> 8) & 0xff), (uint8_t)(value & 0xff)};
1905 storageWriteBytes((uint32_t)location, b, 4);
1919 if (location +
sizeof(
tsMatrix_t) > _storageSize) {
1922 uint8_t calibrated = 0;
1926 if (calibrated == 1) {
1950 if (location +
sizeof(
tsMatrix_t) < _storageSize) {
1959 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)