Skip to content

Commit aa79ea1

Browse files
committed
Adafruit_ILI9341_STM - reworked for 16 bit SPI register accesses
1 parent f1608ab commit aa79ea1

2 files changed

Lines changed: 11 additions & 21 deletions

File tree

STM32F1/libraries/Adafruit_ILI9341_STM/Adafruit_ILI9341_STM.cpp

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Adafruit_ILI9341_STM::Adafruit_ILI9341_STM(int8_t cs, int8_t dc, int8_t rst) : A
3636
}
3737

3838

39-
void Adafruit_ILI9341_STM::spiwrite(uint8_t c) {
39+
void Adafruit_ILI9341_STM::spiwrite(uint16_t c) {
4040

4141
//Serial.print("0x"); Serial.print(c, HEX); Serial.print(", ");
4242

@@ -178,10 +178,7 @@ void Adafruit_ILI9341_STM::begin(void) {
178178
SPI.setBitOrder(MSBFIRST);
179179
SPI.setDataMode(SPI_MODE0);
180180
#elif defined (__STM32F1__)
181-
SPI.begin();
182-
SPI.setClockDivider(SPI_CLOCK_DIV2);
183-
SPI.setBitOrder(MSBFIRST);
184-
SPI.setDataMode(SPI_MODE0);
181+
SPI.beginTransaction(SPISettings(36000000));
185182

186183
#elif defined (__arm__)
187184
SPI.begin();
@@ -335,6 +332,7 @@ void Adafruit_ILI9341_STM::begin(void) {
335332
if (hwSPI) spi_begin();
336333
writecommand(ILI9341_DISPON); //Display on
337334
if (hwSPI) spi_end();
335+
if (hwSPI) SPI.setDataSize(SPI_CR1_DFF);
338336

339337
}
340338

@@ -345,18 +343,14 @@ void Adafruit_ILI9341_STM::setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1,
345343
writecommand(ILI9341_CASET); // Column addr set
346344
*dcport |= dcpinmask;
347345
*csport &= ~cspinmask;
348-
SPI.setDataSize (SPI_CR1_DFF);
349346
SPI.write(x0);
350347
SPI.write(x1);
351-
// SPI.setDataSize (0);
352348

353349
writecommand(ILI9341_PASET); // Row addr set
354350
*dcport |= dcpinmask;
355351
*csport &= ~cspinmask;
356-
// SPI.setDataSize (SPI_CR1_DFF);
357352
SPI.write(y0);
358353
SPI.write(y1);
359-
SPI.setDataSize (0);
360354

361355
writecommand(ILI9341_RAMWR); // write to RAM
362356

@@ -385,7 +379,6 @@ void Adafruit_ILI9341_STM::pushColor(uint16_t color) {
385379
//digitalWrite(_cs, LOW);
386380
*csport &= ~cspinmask;
387381

388-
spiwrite(color >> 8);
389382
spiwrite(color);
390383

391384
*csport |= cspinmask;
@@ -403,7 +396,6 @@ void Adafruit_ILI9341_STM::drawPixel(int16_t x, int16_t y, uint16_t color) {
403396
*dcport |= dcpinmask;
404397
*csport &= ~cspinmask;
405398

406-
spiwrite(color >> 8);
407399
spiwrite(color);
408400

409401
*csport |= cspinmask;
@@ -431,10 +423,8 @@ void Adafruit_ILI9341_STM::drawFastVLine(int16_t x, int16_t y, int16_t h,
431423
*csport &= ~cspinmask;
432424

433425
#if defined (__STM32F1__)
434-
SPI.setDataSize (SPI_CR1_DFF); // Set SPI 16bit mode
435426
lineBuffer[0] = color;
436427
SPI.dmaSend(lineBuffer, h, 0);
437-
SPI.setDataSize (0);
438428
#else
439429
uint8_t hi = color >> 8, lo = color;
440430
while (h--) {
@@ -464,10 +454,8 @@ void Adafruit_ILI9341_STM::drawFastHLine(int16_t x, int16_t y, int16_t w,
464454
*csport &= ~cspinmask;
465455

466456
#if defined (__STM32F1__)
467-
SPI.setDataSize (SPI_CR1_DFF); // Set spi 16bit mode
468457
lineBuffer[0] = color;
469458
SPI.dmaSend(lineBuffer, w, 0);
470-
SPI.setDataSize (0);
471459
#else
472460
uint8_t hi = color >> 8, lo = color;
473461
while (w--) {
@@ -485,11 +473,9 @@ void Adafruit_ILI9341_STM::fillScreen(uint16_t color) {
485473
setAddrWindow(0, 0, _width - 1, _height - 1);
486474
*dcport |= dcpinmask;
487475
*csport &= ~cspinmask;
488-
SPI.setDataSize (SPI_CR1_DFF); // Set spi 16bit mode
489476
lineBuffer[0] = color;
490477
SPI.dmaSend(lineBuffer, (65535), 0);
491478
SPI.dmaSend(lineBuffer, ((_width * _height) - 65535), 0);
492-
SPI.setDataSize (0);
493479

494480
#else
495481
fillRect(0, 0, _width, _height, color);
@@ -515,7 +501,6 @@ void Adafruit_ILI9341_STM::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
515501
*dcport |= dcpinmask;
516502
*csport &= ~cspinmask;
517503
#if defined (__STM32F1__)
518-
SPI.setDataSize (SPI_CR1_DFF); // Set spi 16bit mode
519504
lineBuffer[0] = color;
520505
if (w*h <= 65535) {
521506
SPI.dmaSend(lineBuffer, (w*h), 0);
@@ -524,7 +509,6 @@ void Adafruit_ILI9341_STM::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
524509
SPI.dmaSend(lineBuffer, (65535), 0);
525510
SPI.dmaSend(lineBuffer, ((w*h) - 65535), 0);
526511
}
527-
SPI.setDataSize (0);
528512
#else
529513
uint8_t hi = color >> 8, lo = color;
530514
for(y=h; y>0; y--)
@@ -672,6 +656,7 @@ uint16_t Adafruit_ILI9341_STM::color565(uint8_t r, uint8_t g, uint8_t b) {
672656
void Adafruit_ILI9341_STM::setRotation(uint8_t m) {
673657

674658
if (hwSPI) spi_begin();
659+
if (hwSPI) SPI.setDataSize(0);
675660
writecommand(ILI9341_MADCTL);
676661
rotation = m % 4; // can't be higher than 3
677662
switch (rotation) {
@@ -696,6 +681,7 @@ void Adafruit_ILI9341_STM::setRotation(uint8_t m) {
696681
_height = ILI9341_TFTWIDTH;
697682
break;
698683
}
684+
if (hwSPI) SPI.setDataSize(SPI_CR1_DFF);
699685
if (hwSPI) spi_end();
700686
}
701687

STM32F1/libraries/Adafruit_ILI9341_STM/Adafruit_ILI9341_STM.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ This library has been modified for the Maple Mini
88

99
#include "Arduino.h"
1010
#include "Print.h"
11-
#include <Adafruit_GFX_AS.h>
11+
#include <Adafruit_GFX.h>
1212
#include <avr/pgmspace.h>
1313

14+
#ifndef swap
15+
#define swap(a, b) { int16_t t = a; a = b; b = t; }
16+
#endif
17+
1418
#define ILI9341_TFTWIDTH 240
1519
#define ILI9341_TFTHEIGHT 320
1620

@@ -125,7 +129,7 @@ class Adafruit_ILI9341_STM : public Adafruit_GFX {
125129
void dummyclock(void);
126130
*/
127131

128-
void spiwrite(uint8_t),
132+
void spiwrite(uint16_t),
129133
writecommand(uint8_t c),
130134
writedata(uint8_t d),
131135
commandList(uint8_t *addr);

0 commit comments

Comments
 (0)