Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/dmaspi multi buffer #41

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
220 changes: 141 additions & 79 deletions Adafruit_RA8875.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,23 @@

#include <SPI.h>

void set_base_spi_speed(SpiDriver &driver);

void set_base_spi_speed(SpiDriver &driver) {
/// @cond DISABLE
#if defined(ARDUINO_ARCH_ARC32)
/// @endcond
driver.setClockSpeed(12000000L); /*!< 12MHz */
/// @cond DISABLE
#elif defined(ARDUINO_SAM_DUE)
/// @endcond
uint32_t spi_speed = 12000000; /*!< 12MHz */
driver.setClockSpeed(22000000L); /*!< For non-DMA uses, the highest recorded / usable SPI speed reached was 22MHz */
/// @cond DISABLE
#else
/// @endcond
uint32_t spi_speed = 4000000; /*!< 4MHz */
/// @cond DISABLE
/// @endcond
driver.setClockSpeed(4000000L); /*!< 4MHz */
#endif
/// @endcond

// If the SPI library has transaction support, these functions
// establish settings and protect from interference from other
// libraries. Otherwise, they simply do nothing.
#ifdef SPI_HAS_TRANSACTION
static inline void spi_begin(void) __attribute__((always_inline));
static inline void spi_begin(void) {
// max speed!
SPI.beginTransaction(SPISettings(spi_speed, MSBFIRST, SPI_MODE0));
}
static inline void spi_end(void) __attribute__((always_inline));
static inline void spi_end(void) { SPI.endTransaction(); }
#else
#define spi_begin() ///< Create dummy Macro Function
#define spi_end() ///< Create dummy Macro Function
#endif

/**************************************************************************/
/*!
Expand All @@ -80,10 +70,12 @@ static inline void spi_end(void) { SPI.endTransaction(); }
@param RST Location of the reset pin
*/
/**************************************************************************/
Adafruit_RA8875::Adafruit_RA8875(uint8_t CS, uint8_t RST)
: Adafruit_GFX(800, 480) {
Adafruit_RA8875::Adafruit_RA8875(uint8_t CS, uint8_t RST, bool use_interrupts)
: Adafruit_GFX(800, 480), spiDriver(CS, use_interrupts) {
_cs = CS;
_rst = RST;

set_base_spi_speed(spiDriver);
}

/**************************************************************************/
Expand Down Expand Up @@ -127,26 +119,20 @@ boolean Adafruit_RA8875::begin(enum RA8875sizes s) {
digitalWrite(_rst, HIGH);
delay(100);

SPI.begin();
spiDriver.begin();

#ifdef SPI_HAS_TRANSACTION
/// @cond DISABLE
#if defined(ARDUINO_ARCH_ARC32)
/// @endcond
spi_speed = 2000000;
/// @cond DISABLE
#else
/// @endcond
spi_speed = 125000;
/// @cond DISABLE
#endif
/// @endcond
#else
spiDriver.setClockSpeed(2000000);
#else // defined(ARDUINO_ARCH_ARC32)
spiDriver.setClockSpeed(125000);
#endif // defined(ARDUINO_ARCH_ARC32)
#else // SPI_HAS_TRANSACTION
#ifdef __AVR__
SPI.setClockDivider(SPI_CLOCK_DIV128);
SPI.setDataMode(SPI_MODE0);
#endif
#endif
#endif // __AVR__
#endif // SPI_HAS_TRANSACTION

uint8_t x = readReg(0);
// Serial.print("x = 0x"); Serial.println(x,HEX);
Expand All @@ -158,22 +144,20 @@ boolean Adafruit_RA8875::begin(enum RA8875sizes s) {
initialize();

#ifdef SPI_HAS_TRANSACTION
/// @cond DISABLE
#if defined(ARDUINO_ARCH_ARC32)
/// @endcond
spi_speed = 12000000L;
#else
spi_speed = 4000000L;
#endif
#else
set_base_spi_speed(spiDriver);
#else // SPI_HAS_TRANSACTION
#ifdef __AVR__
SPI.setClockDivider(SPI_CLOCK_DIV4);
#endif
#endif
#endif // __AVR__
#endif // SPI_HAS_TRANSACTION

return true;
}

void Adafruit_RA8875::setClockSpeed(uint32_t speed) {
spiDriver.setClockSpeed(speed);
}

/************************* Initialization *********************************/

/**************************************************************************/
Expand Down Expand Up @@ -268,11 +252,11 @@ void Adafruit_RA8875::initialize(void) {
writeReg(RA8875_HDWR, (_width / 8) - 1); // H width: (HDWR + 1) * 8 = 480
writeReg(RA8875_HNDFTR, RA8875_HNDFTR_DE_HIGH + hsync_finetune);
writeReg(RA8875_HNDR, (hsync_nondisp - hsync_finetune - 2) /
8); // H non-display: HNDR * 8 + HNDFTR + 2 = 10
8); // H non-display: HNDR * 8 + HNDFTR + 2 = 10
writeReg(RA8875_HSTR, hsync_start / 8 - 1); // Hsync start: (HSTR + 1)*8
writeReg(RA8875_HPWR,
RA8875_HPWR_LOW +
(hsync_pw / 8 - 1)); // HSync pulse width = (HPWR+1) * 8
(hsync_pw / 8 - 1)); // HSync pulse width = (HPWR+1) * 8

/* Vertical settings registers */
writeReg(RA8875_VDHR0, (uint16_t)(_height - 1 + _voffset) & 0xFF);
Expand Down Expand Up @@ -584,10 +568,36 @@ boolean Adafruit_RA8875::waitPoll(uint8_t regname, uint8_t waitflag) {
*/
/**************************************************************************/
void Adafruit_RA8875::setXY(uint16_t x, uint16_t y) {
writeReg(RA8875_CURH0, x);
writeReg(RA8875_CURH1, x >> 8);
writeReg(RA8875_CURV0, y);
writeReg(RA8875_CURV1, y >> 8);

digitalWrite(_cs, LOW);
spiDriver.activate();
spiDriver.send(RA8875_CMDWRITE);
spiDriver.send(RA8875_CURH0);
spiDriver.send(RA8875_DATAWRITE);
spiDriver.send(x);
digitalWrite(_cs, HIGH);

digitalWrite(_cs, LOW);
spiDriver.send(RA8875_CMDWRITE);
spiDriver.send(RA8875_CURH1);
spiDriver.send(RA8875_DATAWRITE);
spiDriver.send(x >> 8);
digitalWrite(_cs, HIGH);

digitalWrite(_cs, LOW);
spiDriver.send(RA8875_CMDWRITE);
spiDriver.send(RA8875_CURV0);
spiDriver.send(RA8875_DATAWRITE);
spiDriver.send(y);
digitalWrite(_cs, HIGH);

digitalWrite(_cs, LOW);
spiDriver.send(RA8875_CMDWRITE);
spiDriver.send(RA8875_CURV1);
spiDriver.send(RA8875_DATAWRITE);
spiDriver.send(y >> 8);
spiDriver.deactivate();
digitalWrite(_cs, HIGH);
}

/**************************************************************************/
Expand All @@ -600,10 +610,10 @@ void Adafruit_RA8875::setXY(uint16_t x, uint16_t y) {
/**************************************************************************/
void Adafruit_RA8875::pushPixels(uint32_t num, uint16_t p) {
digitalWrite(_cs, LOW);
SPI.transfer(RA8875_DATAWRITE);
spiDriver.send(RA8875_DATAWRITE);
while (num--) {
SPI.transfer(p >> 8);
SPI.transfer(p);
spiDriver.send(p >> 8);
spiDriver.send(p);
}
digitalWrite(_cs, HIGH);
}
Expand Down Expand Up @@ -673,9 +683,9 @@ void Adafruit_RA8875::drawPixel(int16_t x, int16_t y, uint16_t color) {
writeReg(RA8875_CURV1, y >> 8);
writeCommand(RA8875_MRWC);
digitalWrite(_cs, LOW);
SPI.transfer(RA8875_DATAWRITE);
SPI.transfer(color >> 8);
SPI.transfer(color);
spiDriver.send(RA8875_DATAWRITE);
spiDriver.send(color >> 8);
spiDriver.send(color);
digitalWrite(_cs, HIGH);
}

Expand Down Expand Up @@ -707,10 +717,60 @@ void Adafruit_RA8875::drawPixels(uint16_t *p, uint32_t num, int16_t x,

writeCommand(RA8875_MRWC);
digitalWrite(_cs, LOW);
SPI.transfer(RA8875_DATAWRITE);
spiDriver.send(RA8875_DATAWRITE);
while (num--) {
SPI.transfer16(*p++);
spiDriver.send16(*p++);
}
digitalWrite(_cs, HIGH);
}

/**************************************************************************/
/*!
Draws a series of pixels at the specified location without the overhead.
Allows programming of specified width, instead of full line width.

@param p An array of RGB565 color pixels
@param num The number of the pixels to draw
@param x The 0-based x location
@param y The 0-base y location
@param width The width of the rectangle to draw in
*/
/**************************************************************************/
void Adafruit_RA8875::drawPixelsArea(uint16_t *p, uint32_t num, int16_t x, int16_t y, int16_t width) {
x = applyRotationX(x);
y = applyRotationY(y);

setXY(x, y);

uint16_t pixels = 0;
uint16_t rows = 0;
uint8_t dir = RA8875_MWCR0_LRTD;
if (_rotation == 2) {
dir = RA8875_MWCR0_RLTD;
}
writeReg(RA8875_MWCR0, (readReg(RA8875_MWCR0) & ~RA8875_MWCR0_DIRMASK) | dir);
writeCommand(RA8875_MRWC);
digitalWrite(_cs, LOW);
spiDriver.send(RA8875_DATAWRITE);
while (num > 0) {
if (pixels == width) {
digitalWrite(_cs, HIGH);

rows++;
setXY(x, y + rows);
pixels = 0;

writeCommand(RA8875_MRWC);
digitalWrite(_cs, LOW);
spiDriver.send(RA8875_DATAWRITE);
}
uint16_t to_transfer = min(num, width);
auto start = (uint8_t *)(p + (rows * width));
spiDriver.send(start, sizeof(uint16_t) * to_transfer);
pixels += to_transfer;
num -= to_transfer;
}

digitalWrite(_cs, HIGH);
}

Expand Down Expand Up @@ -1521,14 +1581,16 @@ void Adafruit_RA8875::touchEnable(boolean on) {

if (on) {
/* Enable Touch Panel (Reg 0x70) */
spiDriver.setClockSpeed(10000000L);
writeReg(RA8875_TPCR0, RA8875_TPCR0_ENABLE | RA8875_TPCR0_WAIT_4096CLK |
RA8875_TPCR0_WAKEENABLE | adcClk); // 10mhz max!
RA8875_TPCR0_WAKEENABLE | adcClk); // 10mhz max!
/* Set Auto Mode (Reg 0x71) */
writeReg(RA8875_TPCR1, RA8875_TPCR1_AUTO |
// RA8875_TPCR1_VREFEXT |
RA8875_TPCR1_DEBOUNCE);
// RA8875_TPCR1_VREFEXT |
RA8875_TPCR1_DEBOUNCE);
/* Enable TP INT */
writeReg(RA8875_INTC1, readReg(RA8875_INTC1) | RA8875_INTC1_TP);
set_base_spi_speed(spiDriver);
} else {
/* Disable TP INT */
writeReg(RA8875_INTC1, readReg(RA8875_INTC1) & ~RA8875_INTC1_TP);
Expand Down Expand Up @@ -1651,10 +1713,10 @@ uint8_t Adafruit_RA8875::readReg(uint8_t reg) {
/**************************************************************************/
void Adafruit_RA8875::writeData(uint8_t d) {
digitalWrite(_cs, LOW);
spi_begin();
SPI.transfer(RA8875_DATAWRITE);
SPI.transfer(d);
spi_end();
spiDriver.activate();
spiDriver.send(RA8875_DATAWRITE);
spiDriver.send(d);
spiDriver.deactivate();
digitalWrite(_cs, HIGH);
}

Expand All @@ -1667,11 +1729,11 @@ void Adafruit_RA8875::writeData(uint8_t d) {
/**************************************************************************/
uint8_t Adafruit_RA8875::readData(void) {
digitalWrite(_cs, LOW);
spi_begin();
spiDriver.activate();

SPI.transfer(RA8875_DATAREAD);
uint8_t x = SPI.transfer(0x0);
spi_end();
spiDriver.send(RA8875_DATAREAD);
uint8_t x = spiDriver.receive();
spiDriver.deactivate();

digitalWrite(_cs, HIGH);
return x;
Expand All @@ -1686,11 +1748,11 @@ uint8_t Adafruit_RA8875::readData(void) {
/**************************************************************************/
void Adafruit_RA8875::writeCommand(uint8_t d) {
digitalWrite(_cs, LOW);
spi_begin();
spiDriver.activate();

SPI.transfer(RA8875_CMDWRITE);
SPI.transfer(d);
spi_end();
spiDriver.send(RA8875_CMDWRITE);
spiDriver.send(d);
spiDriver.deactivate();

digitalWrite(_cs, HIGH);
}
Expand All @@ -1704,10 +1766,10 @@ void Adafruit_RA8875::writeCommand(uint8_t d) {
/**************************************************************************/
uint8_t Adafruit_RA8875::readStatus(void) {
digitalWrite(_cs, LOW);
spi_begin();
SPI.transfer(RA8875_CMDREAD);
uint8_t x = SPI.transfer(0x0);
spi_end();
spiDriver.activate();
spiDriver.send(RA8875_CMDREAD);
uint8_t x = spiDriver.receive();
spiDriver.deactivate();

digitalWrite(_cs, HIGH);
return x;
Expand Down
Loading