mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-16 11:21:18 +03:00
SD:
- optimize SPI usage 148% write speed (24kB/s -> 37kB/s) and 127% read speed (121kB/s -> 155kB/s) at 8MHz - add clock frequency as parameter for begin(csPin, frequency) - SD @80MHz write: 84kB/s read: 231kB/s SPI add functions: - void write32(uint32_t data); - void write32(uint32_t data, bool msb); - void transferBytes(uint8_t * out, uint8_t * in, uint32_t size); Adafruit_ILI9341: - code clean
This commit is contained in:
@ -216,8 +216,16 @@ void SPIClass::setClockDivider(uint32_t clockDiv) {
|
||||
SPI1CLK = clockDiv;
|
||||
}
|
||||
|
||||
inline void SPIClass::setDataBits(uint16_t bits) {
|
||||
const uint32_t mask = ~((SPIMMOSI << SPILMOSI) | (SPIMMISO << SPILMISO));
|
||||
bits--;
|
||||
SPI1U1 = ((SPI1U1 & mask) | ((bits << SPILMOSI) | (bits << SPILMISO)));
|
||||
}
|
||||
|
||||
uint8_t SPIClass::transfer(uint8_t data) {
|
||||
while(SPI1CMD & SPIBUSY) {}
|
||||
// reset to 8Bit mode
|
||||
setDataBits(8);
|
||||
SPI1W0 = data;
|
||||
SPI1CMD |= SPIBUSY;
|
||||
while(SPI1CMD & SPIBUSY) {}
|
||||
@ -248,19 +256,21 @@ uint16_t SPIClass::transfer16(uint16_t data) {
|
||||
|
||||
void SPIClass::write(uint8_t data) {
|
||||
while(SPI1CMD & SPIBUSY) {}
|
||||
// reset to 8Bit mode
|
||||
setDataBits(8);
|
||||
SPI1W0 = data;
|
||||
SPI1CMD |= SPIBUSY;
|
||||
while(SPI1CMD & SPIBUSY) {}
|
||||
}
|
||||
|
||||
void SPIClass::write16(uint16_t data) {
|
||||
write16(data, (SPI1C & (SPICWBO | SPICRBO)));
|
||||
write16(data, !(SPI1C & (SPICWBO | SPICRBO)));
|
||||
}
|
||||
|
||||
void SPIClass::write16(uint16_t data, bool msb) {
|
||||
while(SPI1CMD & SPIBUSY) {}
|
||||
// Set to 16Bits transfer
|
||||
SPI1U1 = (SPI1U1 & ~((SPIMMOSI << SPILMOSI))) | ((16 - 1) << SPILMOSI);
|
||||
setDataBits(16);
|
||||
if(msb) {
|
||||
// MSBFIRST Byte first
|
||||
SPI1W0 = (data >> 8) | (data << 8);
|
||||
@ -271,8 +281,31 @@ void SPIClass::write16(uint16_t data, bool msb) {
|
||||
SPI1CMD |= SPIBUSY;
|
||||
}
|
||||
while(SPI1CMD & SPIBUSY) {}
|
||||
// reset to 8Bit mode
|
||||
SPI1U1 = (SPI1U1 & ~((SPIMMOSI << SPILMOSI))) | ((8 - 1) << SPILMOSI);
|
||||
}
|
||||
|
||||
void SPIClass::write32(uint32_t data) {
|
||||
write32(data, !(SPI1C & (SPICWBO | SPICRBO)));
|
||||
}
|
||||
|
||||
void SPIClass::write32(uint32_t data, bool msb) {
|
||||
while(SPI1CMD & SPIBUSY) {}
|
||||
// Set to 32Bits transfer
|
||||
setDataBits(32);
|
||||
if(msb) {
|
||||
union {
|
||||
uint32_t l;
|
||||
uint8_t b[4];
|
||||
} data_;
|
||||
data_.l = data;
|
||||
// MSBFIRST Byte first
|
||||
SPI1W0 = (data_.b[3] | (data_.b[2] << 8) | (data_.b[1] << 16) | (data_.b[0] << 24));
|
||||
SPI1CMD |= SPIBUSY;
|
||||
} else {
|
||||
// LSBFIRST Byte first
|
||||
SPI1W0 = data;
|
||||
SPI1CMD |= SPIBUSY;
|
||||
}
|
||||
while(SPI1CMD & SPIBUSY) {}
|
||||
}
|
||||
|
||||
void SPIClass::writeBytes(uint8_t * data, uint32_t size) {
|
||||
@ -291,7 +324,7 @@ void SPIClass::writeBytes(uint8_t * data, uint32_t size) {
|
||||
void SPIClass::writeBytes_(uint8_t * data, uint8_t size) {
|
||||
while(SPI1CMD & SPIBUSY) {}
|
||||
// Set Bits to transfer
|
||||
SPI1U1 = (SPI1U1 & ~((SPIMMOSI << SPILMOSI))) | ((size * 8 - 1) << SPILMOSI);
|
||||
setDataBits(size * 8);
|
||||
|
||||
volatile uint32_t * fifoPtr = &SPI1W0;
|
||||
uint32_t * dataPtr = (uint32_t*) data;
|
||||
@ -305,8 +338,6 @@ void SPIClass::writeBytes_(uint8_t * data, uint8_t size) {
|
||||
|
||||
SPI1CMD |= SPIBUSY;
|
||||
while(SPI1CMD & SPIBUSY) {}
|
||||
// reset to 8Bit mode
|
||||
SPI1U1 = (SPI1U1 & ~((SPIMMOSI << SPILMOSI))) | ((8 - 1) << SPILMOSI);
|
||||
}
|
||||
|
||||
void SPIClass::writePattern(uint8_t * data, uint8_t size, uint32_t repeat) {
|
||||
@ -344,3 +375,56 @@ void SPIClass::writePattern_(uint8_t * data, uint8_t size, uint8_t repeat) {
|
||||
|
||||
writeBytes(&buffer[0], bytes);
|
||||
}
|
||||
|
||||
void SPIClass::transferBytes(uint8_t * out, uint8_t * in, uint32_t size) {
|
||||
while(size) {
|
||||
if(size > 64) {
|
||||
transferBytes_(out, in, 64);
|
||||
size -= 64;
|
||||
if(out) out += 64;
|
||||
if(in) in += 64;
|
||||
} else {
|
||||
transferBytes_(out, in, size);
|
||||
size = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SPIClass::transferBytes_(uint8_t * out, uint8_t * in, uint8_t size) {
|
||||
while(SPI1CMD & SPIBUSY) {}
|
||||
// Set in/out Bits to transfer
|
||||
|
||||
setDataBits(size * 8);
|
||||
|
||||
volatile uint32_t * fifoPtr = &SPI1W0;
|
||||
uint8_t dataSize = ((size + 3) / 4);
|
||||
|
||||
if(out) {
|
||||
uint32_t * dataPtr = (uint32_t*) out;
|
||||
while(dataSize--) {
|
||||
*fifoPtr = *dataPtr;
|
||||
dataPtr++;
|
||||
fifoPtr++;
|
||||
}
|
||||
} else {
|
||||
// no out data only read fill with dummy data!
|
||||
while(dataSize--) {
|
||||
*fifoPtr = 0xFFFFFFFF;
|
||||
fifoPtr++;
|
||||
}
|
||||
}
|
||||
|
||||
SPI1CMD |= SPIBUSY;
|
||||
while(SPI1CMD & SPIBUSY) {}
|
||||
|
||||
if(in) {
|
||||
volatile uint8_t * fifoPtr8 = (volatile uint8_t *) &SPI1W0;
|
||||
dataSize = size;
|
||||
while(dataSize--) {
|
||||
*in = *fifoPtr8;
|
||||
in++;
|
||||
fifoPtr8++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -75,13 +75,18 @@ public:
|
||||
void write(uint8_t data);
|
||||
void write16(uint16_t data);
|
||||
void write16(uint16_t data, bool msb);
|
||||
void write32(uint32_t data);
|
||||
void write32(uint32_t data, bool msb);
|
||||
void writeBytes(uint8_t * data, uint32_t size);
|
||||
void writePattern(uint8_t * data, uint8_t size, uint32_t repeat);
|
||||
void transferBytes(uint8_t * out, uint8_t * in, uint32_t size);
|
||||
void endTransaction(void);
|
||||
private:
|
||||
bool useHwCs;
|
||||
void writeBytes_(uint8_t * data, uint8_t size);
|
||||
void writePattern_(uint8_t * data, uint8_t size, uint8_t repeat);
|
||||
void transferBytes_(uint8_t * out, uint8_t * in, uint8_t size);
|
||||
inline void setDataBits(uint16_t bits);
|
||||
};
|
||||
|
||||
extern SPIClass SPI;
|
||||
|
@ -26,6 +26,16 @@
|
||||
#include "wiring_private.h"
|
||||
#include <SPI.h>
|
||||
|
||||
#ifdef ESP8266
|
||||
#define hwSPI true
|
||||
#endif
|
||||
|
||||
#define writeCmdDataTmp(cmd, ...) { \
|
||||
const uint8_t tmp##cmd##_[] = { __VA_ARGS__ }; \
|
||||
writeCmdData(cmd, (uint8_t *) &tmp##cmd##_[0], sizeof(tmp##cmd##_)); \
|
||||
}
|
||||
|
||||
|
||||
#ifndef ESP8266
|
||||
// Constructor when using software SPI. All output pins are configurable.
|
||||
Adafruit_ILI9341::Adafruit_ILI9341(int8_t cs, int8_t dc, int8_t mosi,
|
||||
@ -42,12 +52,15 @@ Adafruit_ILI9341::Adafruit_ILI9341(int8_t cs, int8_t dc, int8_t mosi,
|
||||
|
||||
// Constructor when using hardware SPI. Faster, but must use SPI pins
|
||||
// specific to each board type (e.g. 11,13 for Uno, 51,52 for Mega, etc.)
|
||||
#ifdef USE_HW_CS
|
||||
#if defined(ILI9341_USE_HW_CS) || defined(ILI9341_USE_NO_CS)
|
||||
Adafruit_ILI9341::Adafruit_ILI9341(int8_t dc, int8_t rst) : Adafruit_GFX(ILI9341_TFTWIDTH, ILI9341_TFTHEIGHT) {
|
||||
_dc = dc;
|
||||
_rst = rst;
|
||||
hwSPI = true;
|
||||
#ifndef ESP8266
|
||||
#ifdef ESP8266
|
||||
_dcMask = digitalPinToBitMask(_dc);
|
||||
_rstMask = digitalPinToBitMask(_rst);
|
||||
#else
|
||||
_mosi = _sclk = 0;
|
||||
#endif
|
||||
}
|
||||
@ -56,8 +69,12 @@ Adafruit_ILI9341::Adafruit_ILI9341(int8_t dc, int8_t rst) : Adafruit_GFX(ILI9341
|
||||
_cs = cs;
|
||||
_dc = dc;
|
||||
_rst = rst;
|
||||
#ifdef ESP8266
|
||||
_csMask = digitalPinToBitMask(_cs);
|
||||
_dcMask = digitalPinToBitMask(_dc);
|
||||
_rstMask = digitalPinToBitMask(_rst);
|
||||
#else
|
||||
hwSPI = true;
|
||||
#ifndef ESP8266
|
||||
_mosi = _sclk = 0;
|
||||
#endif
|
||||
}
|
||||
@ -140,12 +157,12 @@ void Adafruit_ILI9341::spiwritePattern(uint8_t * data, uint8_t size, uint32_t re
|
||||
|
||||
|
||||
inline void Adafruit_ILI9341::spiCsLow(void) {
|
||||
#ifdef USE_DIGITAL_WRITE
|
||||
#ifdef ILI9341_USE_DIGITAL_WRITE
|
||||
digitalWrite(_cs, LOW);
|
||||
#else
|
||||
#ifdef ESP8266
|
||||
#ifndef USE_HW_CS
|
||||
GPOC = digitalPinToBitMask(_cs);
|
||||
#if !defined(ILI9341_USE_HW_CS) && !defined(ILI9341_USE_NO_CS)
|
||||
GPOC = _csMask;
|
||||
#endif
|
||||
#else
|
||||
*csport &= ~cspinmask;
|
||||
@ -154,12 +171,12 @@ inline void Adafruit_ILI9341::spiCsLow(void) {
|
||||
}
|
||||
|
||||
inline void Adafruit_ILI9341::spiCsHigh(void) {
|
||||
#ifdef USE_DIGITAL_WRITE
|
||||
#ifdef ILI9341_USE_DIGITAL_WRITE
|
||||
digitalWrite(_cs, HIGH);
|
||||
#else
|
||||
#ifdef ESP8266
|
||||
#ifndef USE_HW_CS
|
||||
GPOS = digitalPinToBitMask(_cs);
|
||||
#if !defined(ILI9341_USE_HW_CS) && !defined(ILI9341_USE_NO_CS)
|
||||
GPOS = _csMask;
|
||||
#endif
|
||||
#else
|
||||
*csport |= cspinmask;
|
||||
@ -168,12 +185,12 @@ inline void Adafruit_ILI9341::spiCsHigh(void) {
|
||||
}
|
||||
|
||||
inline void Adafruit_ILI9341::spiDcLow(void){
|
||||
#ifdef USE_DIGITAL_WRITE
|
||||
#ifdef ILI9341_USE_DIGITAL_WRITE
|
||||
digitalWrite(_dc, LOW);
|
||||
#else
|
||||
#ifdef ESP8266
|
||||
#ifndef USE_HW_CS
|
||||
GPOC = digitalPinToBitMask(_dc);
|
||||
GPOC = _dcMask;
|
||||
#endif
|
||||
#else
|
||||
*dcport &= ~dcpinmask;
|
||||
@ -182,11 +199,11 @@ inline void Adafruit_ILI9341::spiDcLow(void){
|
||||
}
|
||||
|
||||
inline void Adafruit_ILI9341::spiDcHigh(void) {
|
||||
#ifdef USE_DIGITAL_WRITE
|
||||
#ifdef ILI9341_USE_DIGITAL_WRITE
|
||||
digitalWrite(_dc, HIGH);
|
||||
#else
|
||||
#ifdef ESP8266
|
||||
GPOS = digitalPinToBitMask(_dc);
|
||||
GPOS = _dcMask;
|
||||
#else
|
||||
*dcport |= dcpinmask;
|
||||
#endif
|
||||
@ -237,13 +254,16 @@ void Adafruit_ILI9341::writeCmdData(uint8_t cmd, uint8_t * data, uint8_t size) {
|
||||
// establish settings and protect from interference from other
|
||||
// libraries. Otherwise, they simply do nothing.
|
||||
#ifdef SPI_HAS_TRANSACTION
|
||||
|
||||
#ifdef ESP8266
|
||||
SPISettings spiSettings = SPISettings(F_CPU, MSBFIRST, SPI_MODE0);
|
||||
#else
|
||||
SPISettings spiSettings = SPISettings(8000000, MSBFIRST, SPI_MODE0);
|
||||
#endif
|
||||
|
||||
static inline void spi_begin(void) __attribute__((always_inline));
|
||||
static inline void spi_begin(void) {
|
||||
#ifdef ESP8266
|
||||
SPI.beginTransaction(SPISettings(F_CPU, MSBFIRST, SPI_MODE0));
|
||||
#else
|
||||
SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0));
|
||||
#endif
|
||||
SPI.beginTransaction(spiSettings);
|
||||
}
|
||||
static inline void spi_end(void) __attribute__((always_inline));
|
||||
static inline void spi_end(void) {
|
||||
@ -289,7 +309,7 @@ void Adafruit_ILI9341::commandList(uint8_t *addr) {
|
||||
|
||||
|
||||
void Adafruit_ILI9341::begin(void) {
|
||||
if (_rst > 0) {
|
||||
if (_rst > NOT_A_PIN) {
|
||||
pinMode(_rst, OUTPUT);
|
||||
digitalWrite(_rst, LOW);
|
||||
}
|
||||
@ -299,7 +319,7 @@ void Adafruit_ILI9341::begin(void) {
|
||||
pinMode(_cs, OUTPUT);
|
||||
#endif
|
||||
#ifndef ESP8266
|
||||
#ifndef USE_DIGITAL_WRITE
|
||||
#ifndef ILI9341_USE_DIGITAL_WRITE
|
||||
csport = portOutputRegister(digitalPinToPort(_cs));
|
||||
cspinmask = digitalPinToBitMask(_cs);
|
||||
dcport = portOutputRegister(digitalPinToPort(_dc));
|
||||
@ -343,7 +363,7 @@ void Adafruit_ILI9341::begin(void) {
|
||||
}
|
||||
#endif
|
||||
// toggle RST low to reset
|
||||
if (_rst > 0) {
|
||||
if (_rst > NOT_A_PIN) {
|
||||
digitalWrite(_rst, HIGH);
|
||||
delay(5);
|
||||
digitalWrite(_rst, LOW);
|
||||
@ -367,108 +387,47 @@ void Adafruit_ILI9341::begin(void) {
|
||||
//if(cmdList) commandList(cmdList);
|
||||
|
||||
if (hwSPI) spi_begin();
|
||||
writecommand(0xEF);
|
||||
writedata(0x03);
|
||||
writedata(0x80);
|
||||
writedata(0x02);
|
||||
|
||||
writecommand(0xCF);
|
||||
writedata(0x00);
|
||||
writedata(0XC1);
|
||||
writedata(0X30);
|
||||
writeCmdDataTmp(0xEF, 0x03, 0x80, 0x02);
|
||||
writeCmdDataTmp(0xCF, 0x00, 0XC1, 0X30);
|
||||
writeCmdDataTmp(0xED, 0x64, 0x03, 0X12, 0X81);
|
||||
writeCmdDataTmp(0xE8, 0x85, 0x00, 0x78);
|
||||
writeCmdDataTmp(0xCB, 0x39, 0x2C, 0x00, 0x34, 0x02);
|
||||
writeCmdDataTmp(0xF7, 0x20);
|
||||
writeCmdDataTmp(0xEA, 0x00, 0x00);
|
||||
|
||||
writecommand(0xED);
|
||||
writedata(0x64);
|
||||
writedata(0x03);
|
||||
writedata(0X12);
|
||||
writedata(0X81);
|
||||
//Powercontrol
|
||||
//VRH[5:0]
|
||||
writeCmdDataTmp(ILI9341_PWCTR1, 0x23);
|
||||
|
||||
writecommand(0xE8);
|
||||
writedata(0x85);
|
||||
writedata(0x00);
|
||||
writedata(0x78);
|
||||
//Powercontrol
|
||||
//SAP[2:0];BT[3:0]
|
||||
writeCmdDataTmp(ILI9341_PWCTR2, 0x10);
|
||||
|
||||
writecommand(0xCB);
|
||||
writedata(0x39);
|
||||
writedata(0x2C);
|
||||
writedata(0x00);
|
||||
writedata(0x34);
|
||||
writedata(0x02);
|
||||
//VCMcontrol
|
||||
writeCmdDataTmp(ILI9341_VMCTR1, 0x3e, 0x28);
|
||||
|
||||
writecommand(0xF7);
|
||||
writedata(0x20);
|
||||
//VCMcontrol2
|
||||
writeCmdDataTmp(ILI9341_VMCTR2, 0x86);
|
||||
|
||||
writecommand(0xEA);
|
||||
writedata(0x00);
|
||||
writedata(0x00);
|
||||
//MemoryAccessControl
|
||||
writeCmdDataTmp(ILI9341_MADCTL, 0x48);
|
||||
|
||||
writecommand(ILI9341_PWCTR1); //Power control
|
||||
writedata(0x23); //VRH[5:0]
|
||||
writeCmdDataTmp(ILI9341_PIXFMT, 0x55);
|
||||
writeCmdDataTmp(ILI9341_FRMCTR1, 0x00, 0x18);
|
||||
|
||||
writecommand(ILI9341_PWCTR2); //Power control
|
||||
writedata(0x10); //SAP[2:0];BT[3:0]
|
||||
//DisplayFunctionControl
|
||||
writeCmdDataTmp(ILI9341_DFUNCTR, 0x08, 0x82, 0x27);
|
||||
|
||||
writecommand(ILI9341_VMCTR1); //VCM control
|
||||
writedata(0x3e); //<2F>Աȶȵ<C8B6><C8B5><EFBFBD>
|
||||
writedata(0x28);
|
||||
//3GammaFunctionDisable
|
||||
writeCmdDataTmp(0xF2, 0x00);
|
||||
|
||||
writecommand(ILI9341_VMCTR2); //VCM control2
|
||||
writedata(0x86); //--
|
||||
//Gammacurveselected
|
||||
writeCmdDataTmp(ILI9341_GAMMASET, 0x01);
|
||||
|
||||
writecommand(ILI9341_MADCTL); // Memory Access Control
|
||||
writedata(0x48);
|
||||
|
||||
writecommand(ILI9341_PIXFMT);
|
||||
writedata(0x55);
|
||||
|
||||
writecommand(ILI9341_FRMCTR1);
|
||||
writedata(0x00);
|
||||
writedata(0x18);
|
||||
|
||||
writecommand(ILI9341_DFUNCTR); // Display Function Control
|
||||
writedata(0x08);
|
||||
writedata(0x82);
|
||||
writedata(0x27);
|
||||
|
||||
writecommand(0xF2); // 3Gamma Function Disable
|
||||
writedata(0x00);
|
||||
|
||||
writecommand(ILI9341_GAMMASET); //Gamma curve selected
|
||||
writedata(0x01);
|
||||
|
||||
writecommand(ILI9341_GMCTRP1); //Set Gamma
|
||||
writedata(0x0F);
|
||||
writedata(0x31);
|
||||
writedata(0x2B);
|
||||
writedata(0x0C);
|
||||
writedata(0x0E);
|
||||
writedata(0x08);
|
||||
writedata(0x4E);
|
||||
writedata(0xF1);
|
||||
writedata(0x37);
|
||||
writedata(0x07);
|
||||
writedata(0x10);
|
||||
writedata(0x03);
|
||||
writedata(0x0E);
|
||||
writedata(0x09);
|
||||
writedata(0x00);
|
||||
|
||||
writecommand(ILI9341_GMCTRN1); //Set Gamma
|
||||
writedata(0x00);
|
||||
writedata(0x0E);
|
||||
writedata(0x14);
|
||||
writedata(0x03);
|
||||
writedata(0x11);
|
||||
writedata(0x07);
|
||||
writedata(0x31);
|
||||
writedata(0xC1);
|
||||
writedata(0x48);
|
||||
writedata(0x08);
|
||||
writedata(0x0F);
|
||||
writedata(0x0C);
|
||||
writedata(0x31);
|
||||
writedata(0x36);
|
||||
writedata(0x0F);
|
||||
//SetGamma
|
||||
writeCmdDataTmp(ILI9341_GMCTRP1, 0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08, 0x4E, 0xF1, 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00);
|
||||
writeCmdDataTmp(ILI9341_GMCTRN1, 0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, 0x31, 0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F);
|
||||
|
||||
writecommand(ILI9341_SLPOUT); //Exit Sleep
|
||||
if (hwSPI) spi_end();
|
||||
@ -604,7 +563,7 @@ void Adafruit_ILI9341::drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t c
|
||||
}
|
||||
|
||||
void Adafruit_ILI9341::fillScreen(uint16_t color) {
|
||||
fillRect(0, 0, _width, _height, color);
|
||||
fillRect(0, 0, _width, _height, color);
|
||||
}
|
||||
|
||||
// fill a rectangle
|
||||
@ -711,9 +670,7 @@ uint8_t Adafruit_ILI9341::spiread(void) {
|
||||
SPI.setDataMode(SPI_MODE0);
|
||||
r = SPI.transfer(0x00);
|
||||
#else
|
||||
spi_begin();
|
||||
r = SPI.transfer(0x00);
|
||||
spi_end();
|
||||
#endif
|
||||
} else {
|
||||
#ifndef ESP8266
|
||||
@ -731,51 +688,41 @@ uint8_t Adafruit_ILI9341::spiread(void) {
|
||||
return r;
|
||||
}
|
||||
|
||||
uint8_t Adafruit_ILI9341::readdata(void) {
|
||||
digitalWrite(_dc, HIGH);
|
||||
#ifndef USE_HW_CS
|
||||
digitalWrite(_cs, LOW);
|
||||
#endif
|
||||
uint8_t r = spiread();
|
||||
#ifndef USE_HW_CS
|
||||
digitalWrite(_cs, HIGH);
|
||||
#endif
|
||||
return r;
|
||||
uint8_t Adafruit_ILI9341::readdata(void) {
|
||||
if(hwSPI) spi_begin();
|
||||
spiCsLow();
|
||||
spiDcLow();
|
||||
uint8_t r = spiread();
|
||||
spiCsHigh();
|
||||
if(hwSPI) spi_end();
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
uint8_t Adafruit_ILI9341::readcommand8(uint8_t c, uint8_t index) {
|
||||
if (hwSPI) spi_begin();
|
||||
digitalWrite(_dc, LOW); // command
|
||||
#ifndef USE_HW_CS
|
||||
digitalWrite(_cs, LOW);
|
||||
#endif
|
||||
spiwrite(0xD9); // woo sekret command?
|
||||
digitalWrite(_dc, HIGH); // data
|
||||
spiwrite(0x10 + index);
|
||||
#ifndef USE_HW_CS
|
||||
digitalWrite(_cs, HIGH);
|
||||
#endif
|
||||
digitalWrite(_dc, LOW);
|
||||
if(hwSPI) spi_begin();
|
||||
|
||||
spiCsLow();
|
||||
spiDcLow();
|
||||
|
||||
spiwrite(0xD9); // woo sekret command?
|
||||
spiDcHigh();
|
||||
spiwrite(0x10 + index);
|
||||
|
||||
#ifndef ESP8266
|
||||
digitalWrite(_sclk, LOW);
|
||||
digitalWrite(_sclk, LOW);
|
||||
#endif
|
||||
#ifndef USE_HW_CS
|
||||
digitalWrite(_cs, LOW);
|
||||
#endif
|
||||
spiwrite(c);
|
||||
|
||||
digitalWrite(_dc, HIGH);
|
||||
uint8_t r = spiread();
|
||||
#ifndef USE_HW_CS
|
||||
digitalWrite(_cs, HIGH);
|
||||
#endif
|
||||
if (hwSPI) spi_end();
|
||||
return r;
|
||||
spiDcLow();
|
||||
spiwrite(c);
|
||||
|
||||
spiDcHigh();
|
||||
uint8_t r = spiread();
|
||||
spiCsHigh();
|
||||
|
||||
if(hwSPI) spi_end();
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
|
||||
uint16_t Adafruit_ILI9341::readcommand16(uint8_t c) {
|
||||
|
@ -110,10 +110,11 @@
|
||||
#define ILI9341_GREENYELLOW 0xAFE5 /* 173, 255, 47 */
|
||||
#define ILI9341_PINK 0xF81F
|
||||
|
||||
//#define USE_DIGITAL_WRITE
|
||||
//#define ILI9341_USE_DIGITAL_WRITE
|
||||
//#define ILI9341_USE_NO_CS
|
||||
#ifdef ESP8266
|
||||
//not working
|
||||
//#define USE_HW_CS
|
||||
//#define ILI9341_USE_HW_CS
|
||||
#endif
|
||||
|
||||
class Adafruit_ILI9341 : public Adafruit_GFX {
|
||||
@ -123,7 +124,7 @@ class Adafruit_ILI9341 : public Adafruit_GFX {
|
||||
Adafruit_ILI9341(int8_t _CS, int8_t _DC, int8_t _MOSI, int8_t _SCLK,
|
||||
int8_t _RST, int8_t _MISO);
|
||||
#endif
|
||||
#ifdef USE_HW_CS
|
||||
#if defined(USE_HW_CS) || defined(USE_NO_CS)
|
||||
Adafruit_ILI9341(int8_t _DC, int8_t _RST = -1);
|
||||
#else
|
||||
Adafruit_ILI9341(int8_t _CS, int8_t _DC, int8_t _RST = -1);
|
||||
@ -183,8 +184,9 @@ class Adafruit_ILI9341 : public Adafruit_GFX {
|
||||
inline void spiDcLow(void);
|
||||
|
||||
uint8_t tabcolor;
|
||||
|
||||
#ifndef ESP8266
|
||||
boolean hwSPI;
|
||||
#endif
|
||||
#if defined (__AVR__) || defined(TEENSYDUINO)
|
||||
uint8_t mySPCR;
|
||||
volatile uint8_t *mosiport, *clkport, *dcport, *rsport, *csport;
|
||||
@ -197,8 +199,10 @@ class Adafruit_ILI9341 : public Adafruit_GFX {
|
||||
#elif defined (ESP8266)
|
||||
#ifndef USE_HW_CS
|
||||
int8_t _cs;
|
||||
uint32_t _csMask;
|
||||
#endif
|
||||
int8_t _dc, _rst;
|
||||
uint32_t _dcMask, _rstMask;
|
||||
#endif
|
||||
};
|
||||
|
||||
|
@ -326,13 +326,7 @@ boolean callback_rmdir(SdFile& parentDir, char *filePathComponent,
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Implementation of class used to create `SDCard` object. */
|
||||
|
||||
|
||||
|
||||
boolean SDClass::begin(uint8_t csPin) {
|
||||
boolean SDClass::begin(uint8_t csPin, uint32_t sckRateID) {
|
||||
/*
|
||||
|
||||
Performs the initialisation required by the sdfatlib library.
|
||||
@ -340,13 +334,11 @@ boolean SDClass::begin(uint8_t csPin) {
|
||||
Return true if initialization succeeds, false otherwise.
|
||||
|
||||
*/
|
||||
return card.init(SPI_HALF_SPEED, csPin) &&
|
||||
return card.init(sckRateID, csPin) &&
|
||||
volume.init(card) &&
|
||||
root.openRoot(volume);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// this little helper is used to traverse paths
|
||||
SdFile SDClass::getParentDir(const char *filepath, int *index) {
|
||||
// get parent directory
|
||||
|
@ -65,7 +65,7 @@ private:
|
||||
public:
|
||||
// This needs to be called to set up the connection to the SD card
|
||||
// before other methods are used.
|
||||
boolean begin(uint8_t csPin = SD_CHIP_SELECT_PIN);
|
||||
boolean begin(uint8_t csPin = SD_CHIP_SELECT_PIN, uint32_t sckRateID = SPI_FULL_SPEED);
|
||||
|
||||
// Open the specified file/directory with the supplied mode (e.g. read or
|
||||
// write, etc). Returns a File object for interacting with the file.
|
||||
|
@ -33,9 +33,13 @@ static void spiSend(uint8_t b) {
|
||||
SPDR = b;
|
||||
while (!(SPSR & (1 << SPIF)))
|
||||
;
|
||||
#else
|
||||
#ifdef ESP8266
|
||||
SPI.write(b);
|
||||
#else
|
||||
SPI.transfer(b);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
/** Receive a byte from the card */
|
||||
static uint8_t spiRec(void) {
|
||||
@ -116,8 +120,14 @@ uint8_t Sd2Card::cardCommand(uint8_t cmd, uint32_t arg) {
|
||||
// send command
|
||||
spiSend(cmd | 0x40);
|
||||
|
||||
#ifdef ESP8266
|
||||
// send argument
|
||||
SPI.write32(arg, true);
|
||||
#else
|
||||
// send argument
|
||||
for (int8_t s = 24; s >= 0; s -= 8) spiSend(arg >> s);
|
||||
#endif
|
||||
|
||||
|
||||
// send CRC
|
||||
uint8_t crc = 0xFF;
|
||||
@ -424,7 +434,14 @@ uint8_t Sd2Card::readData(uint32_t block,
|
||||
dst[n] = SPDR;
|
||||
|
||||
#else // OPTIMIZE_HARDWARE_SPI
|
||||
#ifdef ESP8266
|
||||
// skip data before offset
|
||||
SPI.transferBytes(NULL, NULL, offset_);
|
||||
|
||||
// transfer data
|
||||
SPI.transferBytes(NULL, dst, count);
|
||||
|
||||
#else
|
||||
// skip data before offset
|
||||
for (;offset_ < offset; offset_++) {
|
||||
spiRec();
|
||||
@ -433,6 +450,7 @@ uint8_t Sd2Card::readData(uint32_t block,
|
||||
for (uint16_t i = 0; i < count; i++) {
|
||||
dst[i] = spiRec();
|
||||
}
|
||||
#endif
|
||||
#endif // OPTIMIZE_HARDWARE_SPI
|
||||
|
||||
offset_ += count;
|
||||
@ -463,7 +481,11 @@ void Sd2Card::readEnd(void) {
|
||||
while (!(SPSR & (1 << SPIF)))
|
||||
;
|
||||
#else // OPTIMIZE_HARDWARE_SPI
|
||||
#ifdef ESP8266
|
||||
SPI.transferBytes(NULL, NULL, (514-offset_));
|
||||
#else
|
||||
while (offset_++ < 514) spiRec();
|
||||
#endif
|
||||
#endif // OPTIMIZE_HARDWARE_SPI
|
||||
chipSelectHigh();
|
||||
inBlock_ = 0;
|
||||
@ -479,7 +501,11 @@ uint8_t Sd2Card::readRegister(uint8_t cmd, void* buf) {
|
||||
}
|
||||
if (!waitStartBlock()) goto fail;
|
||||
// transfer data
|
||||
#ifdef ESP8266
|
||||
SPI.transferBytes(NULL, dst, 16);
|
||||
#else
|
||||
for (uint16_t i = 0; i < 16; i++) dst[i] = spiRec();
|
||||
#endif
|
||||
spiRec(); // get first crc byte
|
||||
spiRec(); // get second crc byte
|
||||
chipSelectHigh();
|
||||
@ -646,13 +672,21 @@ uint8_t Sd2Card::writeData(uint8_t token, const uint8_t* src) {
|
||||
|
||||
#else // OPTIMIZE_HARDWARE_SPI
|
||||
spiSend(token);
|
||||
#ifdef ESP8266
|
||||
// send argument
|
||||
SPI.writeBytes((uint8_t *)src, 512);
|
||||
#else
|
||||
for (uint16_t i = 0; i < 512; i++) {
|
||||
spiSend(src[i]);
|
||||
}
|
||||
#endif
|
||||
#endif // OPTIMIZE_HARDWARE_SPI
|
||||
#ifdef ESP8266
|
||||
SPI.write16(0xFFFF, true);
|
||||
#else
|
||||
spiSend(0xff); // dummy crc
|
||||
spiSend(0xff); // dummy crc
|
||||
|
||||
#endif
|
||||
status_ = spiRec();
|
||||
if ((status_ & DATA_RES_MASK) != DATA_RES_ACCEPTED) {
|
||||
error(SD_CARD_ERROR_WRITE);
|
||||
|
Reference in New Issue
Block a user