Added first pas at SPI::DMATransfer(byte *buf, int length) - and updated ILI9341 lib to use DMA Transfer for fill rect

This commit is contained in:
Roger Clark 2015-03-30 03:31:41 +11:00
parent a47caee80d
commit 5f9e8a04dd
3 changed files with 85 additions and 21 deletions

View File

@ -327,9 +327,11 @@ void Adafruit_ILI9341::begin(void) {
void Adafruit_ILI9341::setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, void Adafruit_ILI9341::setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1,
uint16_t y1) { uint16_t y1) {
byte buf[4];
writecommand(ILI9341_CASET); // Column addr set writecommand(ILI9341_CASET); // Column addr set
*dcport |= dcpinmask; *dcport |= dcpinmask;
*csport &= ~cspinmask; *csport &= ~cspinmask;
SPI.write(x0 >> 8); SPI.write(x0 >> 8);
SPI.write(x0 & 0xFF); // XSTART SPI.write(x0 & 0xFF); // XSTART
SPI.write(x1 >> 8); SPI.write(x1 >> 8);
@ -338,11 +340,13 @@ void Adafruit_ILI9341::setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1,
writecommand(ILI9341_PASET); // Row addr set writecommand(ILI9341_PASET); // Row addr set
*dcport |= dcpinmask; *dcport |= dcpinmask;
*csport &= ~cspinmask; *csport &= ~cspinmask;
SPI.write(y0>>8); SPI.write(y0>>8);
SPI.write(y0); // YSTART SPI.write(y0); // YSTART
SPI.write(y1>>8); SPI.write(y1>>8);
SPI.write(y1); // YEND SPI.write(y1); // YEND
writecommand(ILI9341_RAMWR); // write to RAM writecommand(ILI9341_RAMWR); // write to RAM
} }
@ -433,9 +437,11 @@ void Adafruit_ILI9341::fillScreen(uint16_t color) {
} }
// fill a rectangle // fill a rectangle
void Adafruit_ILI9341::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, void Adafruit_ILI9341::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color) {
uint16_t color) {
int numPixels; int numPixels;
unsigned char *buff;
// rudimentary clipping (drawChar w/big text requires this) // rudimentary clipping (drawChar w/big text requires this)
if((x >= _width) || (y >= _height)) return; if((x >= _width) || (y >= _height)) return;
if((x + w - 1) >= _width) w = _width - x; if((x + w - 1) >= _width) w = _width - x;
@ -448,7 +454,27 @@ void Adafruit_ILI9341::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
*dcport |= dcpinmask; *dcport |= dcpinmask;
*csport &= ~cspinmask; *csport &= ~cspinmask;
if (true)
{
// Use DMA
byte lineBuffer[h*2];// Buffer to be sent via DMA
// need to build a buffer of the required height (h)
// Note I suspect there is a faster way to do this
for(int i=0;i<h*2;i++)
{
lineBuffer[i++] = hi&0xff;
lineBuffer[i] = lo&0xff;
}
// Tansfer each line by DMA
for(int i=0;i<w;i++)
{
SPI.DMATransfer(lineBuffer,h*2);
}
}
else
{
// Non DMA method (currently not used)
for(y=h; y>0; y--) for(y=h; y>0; y--)
{ {
for(x=w; x>0; x--) for(x=w; x>0; x--)
@ -457,6 +483,7 @@ void Adafruit_ILI9341::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
SPI.write(lo); SPI.write(lo);
} }
} }
}
if (hwSPI) spi_end(); if (hwSPI) spi_end();
*csport |= cspinmask; *csport |= cspinmask;

View File

@ -330,6 +330,29 @@ uint8 SPIClass::transfer(uint8 byte) {
return b; return b;
} }
uint8 SPIClass::DMATransfer(uint8 *data, uint32 length) {
uint8 b;
dma1_ch3_Active=true;
spi_tx_dma_enable(SPI1);
dma_init(DMA1);
dma_attach_interrupt(DMA1, DMA_CH3, &SPIClass::DMA1_CH3_Event);
dma_setup_transfer(DMA1, DMA_CH3, &SPI1->regs->DR, DMA_SIZE_8BITS,
data, DMA_SIZE_8BITS, (DMA_MINC_MODE | DMA_FROM_MEM | DMA_TRNS_CMPLT));
dma_set_num_transfers(DMA1, DMA_CH3, length); // 2 bytes per pixel
dma_enable(DMA1, DMA_CH3);
while (dma1_ch3_Active);
return b;
}
void SPIClass::attachInterrupt(void) { void SPIClass::attachInterrupt(void) {
// Should be enableInterrupt() // Should be enableInterrupt()
} }

View File

@ -40,6 +40,7 @@
#include <libmaple/libmaple_types.h> #include <libmaple/libmaple_types.h>
#include <libmaple/spi.h> #include <libmaple/spi.h>
#include <libmaple/dma.h>
#include <boards.h> #include <boards.h>
#include <stdint.h> #include <stdint.h>
@ -121,6 +122,7 @@ private:
}; };
volatile static bool dma1_ch3_Active;
/** /**
* @brief Wirish SPI interface. * @brief Wirish SPI interface.
@ -130,6 +132,9 @@ private:
*/ */
class SPIClass { class SPIClass {
public: public:
/** /**
* @param spiPortNumber Number of the SPI port to manage. * @param spiPortNumber Number of the SPI port to manage.
*/ */
@ -223,6 +228,8 @@ public:
*/ */
uint8 transfer(uint8 data); uint8 transfer(uint8 data);
uint8 DMATransfer(uint8 *data, uint32 length);
/* /*
* Pin accessors * Pin accessors
*/ */
@ -289,7 +296,14 @@ public:
spi_dev *dev(){ return spi_d;} spi_dev *dev(){ return spi_d;}
private: private:
static inline void DMA1_CH3_Event() {
dma1_ch3_Active = 0;
dma_disable(DMA1, DMA_CH3);
}
spi_dev *spi_d; spi_dev *spi_d;
uint8_t _SSPin; uint8_t _SSPin;
uint32_t clockDivider; uint32_t clockDivider;