PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

419 rindas
15KB

  1. /*
  2. ILI9163C - A fast SPI driver for TFT that use Ilitek ILI9163C.
  3. Features:
  4. - Very FAST!, expecially with Teensy 3.x where uses hyper optimized SPI.
  5. - It uses just 4 or 5 wires.
  6. - Compatible at command level with Adafruit display series so it's easy to adapt existing code.
  7. - It uses the standard Adafruit_GFX Library (you need to install).
  8. Background:
  9. I got one of those displays from a chinese ebay seller but unfortunatly I cannot get
  10. any working library so I decided to hack it. ILI9163C looks pretty similar to other
  11. display driver but it uses it's own commands so it's tricky to work with it unlsess you
  12. carefully fight with his gigantic and not so clever datasheet.
  13. My display it's a 1.44"", 128x128 that suppose to substitute Nokia 5110 LCD and here's the
  14. first confusion! Many sellers claim that it's compatible with Nokia 5110 (that use a philips
  15. controller) but the only similarity it's the pin names since that this one it's color and
  16. have totally different controller that's not compatible.
  17. http://www.ebay.com/itm/Replace-Nokia-5110-LCD-1-44-Red-Serial-128X128-SPI-Color-TFT-LCD-Display-Module-/141196897388
  18. http://www.elecrow.com/144-128x-128-tft-lcd-with-spi-interface-p-855.html
  19. Pay attention that can drive different resolutions and your display can be
  20. 160*128 or whatever, also there's a strain of this display with a black PCB that a friend of mine
  21. got some weeks ago and need some small changes in library to get working.
  22. If you look at TFT_ILI9163C.h file you can add your modifications and let me know so I
  23. can include for future versions.
  24. Code Optimizations:
  25. The purpose of this library it's SPEED. I have tried to use hardware optimized calls
  26. where was possible and results are quite good for most applications, actually nly filled circles
  27. are still a bit slow. Many SPI call has been optimized by reduce un-needed triggers to RS and CS
  28. lines. Of course it can be improved so feel free to add suggestions.
  29. -------------------------------------------------------------------------------
  30. Copyright (c) 2014, .S.U.M.O.T.O.Y., coded by Max MC Costa.
  31. TFT_ILI9163C Library is free software: you can redistribute it and/or modify
  32. it under the terms of the GNU General Public License as published by
  33. the Free Software Foundation, either version 3 of the License, or
  34. (at your option) any later version.
  35. TFT_ILI9163C Library is distributed in the hope that it will be useful,
  36. but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. GNU General Public License for more details.
  39. You should have received a copy of the GNU General Public License
  40. along with Foobar. If not, see <http://www.gnu.org/licenses/>.
  41. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  42. This file needs the following Libraries:
  43. Adafruit_GFX by Adafruit:
  44. https://github.com/adafruit/Adafruit-GFX-Library
  45. Remember to update GFX library often to have more features with this library!
  46. From this version I'm using my version of Adafruit_GFX library:
  47. https://github.com/sumotoy/Adafruit-GFX-Library
  48. It has faster char rendering and some small little optimizations but you can
  49. choose one of the two freely since are both fully compatible.
  50. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  51. Special Thanks:
  52. Thanks Adafruit for his Adafruit_GFX!
  53. Thanks to Paul Stoffregen for his beautiful Teensy3 and DMA SPI.
  54. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  55. Version:
  56. 0.1a1: First release, compile correctly. Altrough not fully working!
  57. 0.1a3: Better but still some addressing problems.
  58. 0.1b1: Beta! Addressing solved, now rotation works and boundaries ok.
  59. 0.2b1: Cleaned up.
  60. 0.2b3: Added 2.2" Red PCB parameters
  61. 0.2b4: Bug fixes, added colorSpace (for future send image)
  62. 0.2b5: Cleaning
  63. 0.3b1: Complete rework on Teensy SPI based on Paul Stoffregen work
  64. SPI transaction,added BLACK TAG 2.2 display
  65. 0.3b2: Minor fix, load 24bit image, Added conversion utility
  66. 0.4: some improvement, new ballistic gauge example!
  67. 0.5: Added scroll and more commands, optimizations
  68. 0.6: Small fix, added SD example and subroutines
  69. 0.6b1: Fix clearscreen, missed a parameter.
  70. 0.6b2: Scroll completed. (thanks Masuda)
  71. 0.6b3: Clear Screen fix v2. Added Idle mode.
  72. 0.7: Init correction.Clear Screen fix v3 (last time?)
  73. 0.75: SPI transactions for arduino's (beta)
  74. 0.8: Compatiblke with IDE 1.0.6 (teensyduino 1.20) and IDE 1.6.x (teensyduino 1.21b)
  75. 0.9: Many changes! Now works with more CPU's, alternative pins for Teensy and Teensy LC
  76. Works (in standard SPI) with Teensy LC.
  77. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  78. BugList of the current version:
  79. Please report any!
  80. Here's the speed test between 0.2b5 and 0.3b1 on Teensy3.1 (major SPI changes)
  81. ------------------------------------------------------------------------
  82. Lines 17024 16115 BETTER
  83. Horiz/Vert Lines 5360 5080 BETTER
  84. Rectangles (outline) 4384 4217 BETTER
  85. Rectangles (filled) 96315 91265 BETTER
  86. Circles (filled) 16053 15829 LITTLE BETTER
  87. Circles (outline) 11540 20320 WORST!
  88. Triangles (outline) 5359 5143 BETTER
  89. Triangles (filled) 19088 18741 BETTER
  90. Rounded rects (outline) 8681 12498 LITTLE WORST
  91. Rounded rects (filled) 105453 100213 BETTER
  92. Done!
  93. */
  94. #ifndef _TFT_ILI9163CLIB_H_
  95. #define _TFT_ILI9163CLIB_H_
  96. //defined(__MKL26Z64__)
  97. #include "Arduino.h"
  98. #include "Print.h"
  99. #include <Adafruit_GFX.h>
  100. #include "_settings/TFT_ILI9163C_settings.h"
  101. #if !defined(_ADAFRUIT_GFX_VARIANT)
  102. #ifdef __AVR__
  103. #include <avr/pgmspace.h>
  104. #elif defined(__SAM3X8E__)
  105. #include <include/pio.h>
  106. #define PROGMEM
  107. #define pgm_read_byte(addr) (*(const unsigned char *)(addr))
  108. #define pgm_read_word(addr) (*(const unsigned short *)(addr))
  109. typedef unsigned char prog_uchar;
  110. #endif
  111. #endif
  112. //--------- Keep out hands from here!-------------
  113. #define BLACK 0x0000
  114. #define WHITE 0xFFFF
  115. #include "_settings/TFT_ILI9163C_registers.h"
  116. class TFT_ILI9163C : public Adafruit_GFX {
  117. public:
  118. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  119. TFT_ILI9163C(uint8_t cspin,uint8_t dcpin,uint8_t rstpin=255,uint8_t mosi=11,uint8_t sclk=13);
  120. #elif defined(__MKL26Z64__)
  121. TFT_ILI9163C(uint8_t cspin,uint8_t dcpin,uint8_t rstpin=255,uint8_t mosi=11,uint8_t sclk=13);
  122. #else
  123. TFT_ILI9163C(uint8_t cspin,uint8_t dcpin,uint8_t rstpin=255);
  124. #endif
  125. //TFT_ILI9163C(uint8_t CS, uint8_t DC);//connect rst pin to VDD
  126. void begin(void),
  127. setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1),//graphic Addressing
  128. setCursor(int16_t x,int16_t y),//char addressing
  129. pushColor(uint16_t color),
  130. fillScreen(uint16_t color=0x0000),
  131. clearScreen(uint16_t color=0x0000),//same as fillScreen
  132. drawPixel(int16_t x, int16_t y, uint16_t color),
  133. drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color),
  134. drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color),
  135. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__) //workaround to get more speed from Teensy
  136. drawLine(int16_t x0, int16_t y0,int16_t x1, int16_t y1, uint16_t color),
  137. drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color),
  138. #endif
  139. fillRect(int16_t x, int16_t y, int16_t w, int16_t h,uint16_t color),
  140. setRotation(uint8_t r),
  141. invertDisplay(boolean i);
  142. uint8_t errorCode(void);
  143. void idleMode(boolean onOff);
  144. void display(boolean onOff);
  145. void sleepMode(boolean mode);
  146. void defineScrollArea(uint16_t tfa, uint16_t bfa);
  147. void scroll(uint16_t adrs);
  148. void startPushData(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1);
  149. void pushData(uint16_t color);
  150. void endPushData();
  151. void writeScreen24(const uint32_t *bitmap,uint16_t size=_TFTWIDTH*_TFTHEIGHT);
  152. inline uint16_t Color565(uint8_t r, uint8_t g, uint8_t b) {return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);};
  153. //convert 24bit color into packet 16 bit one (credits for this are all mine)
  154. inline uint16_t Color24To565(int32_t color_) { return ((((color_ >> 16) & 0xFF) / 8) << 11) | ((((color_ >> 8) & 0xFF) / 4) << 5) | (((color_) & 0xFF) / 8);}
  155. void setBitrate(uint32_t n);
  156. protected:
  157. volatile uint8_t _Mactrl_Data;//container for the memory access control data
  158. uint8_t _colorspaceData;
  159. #if defined(__AVR__)
  160. void spiwrite(uint8_t);
  161. volatile uint8_t *dataport, *clkport, *csport, *rsport;
  162. uint8_t _cs,_rs,_rst;
  163. uint8_t datapinmask, clkpinmask, cspinmask, rspinmask;
  164. #elif defined(__SAM3X8E__)
  165. void spiwrite(uint8_t);
  166. Pio *dataport, *clkport, *csport, *rsport;
  167. uint8_t _cs,_rs,_rst;
  168. uint32_t datapinmask, clkpinmask, cspinmask, rspinmask;
  169. #elif defined(__MKL26Z64__)
  170. uint8_t _cs,_rs,_rst;
  171. uint8_t _mosi, _sclk;
  172. bool _useSPI1;
  173. #elif defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  174. uint8_t _cs, _rs, _rst;
  175. uint8_t pcs_data, pcs_command;
  176. uint8_t _mosi, _sclk;
  177. void _setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1);//graphic Addressing for Teensy
  178. //Here's Paul Stoffregen magic in action...
  179. void waitFifoNotFull(void) {
  180. uint32_t sr;
  181. uint32_t tmp __attribute__((unused));
  182. do {
  183. #if ARDUINO >= 160
  184. sr = KINETISK_SPI0.SR;
  185. #else
  186. sr = SPI0.SR;
  187. #endif
  188. if (sr & 0xF0) tmp = SPI0_POPR; // drain RX FIFO
  189. } while ((sr & (15 << 12)) > (3 << 12));
  190. }
  191. void waitFifoEmpty(void) {
  192. uint32_t sr;
  193. uint32_t tmp __attribute__((unused));
  194. do {
  195. #if ARDUINO >= 160
  196. sr = KINETISK_SPI0.SR;
  197. if (sr & 0xF0) tmp = KINETISK_SPI0.POPR; // drain RX FIFO
  198. #else
  199. sr = SPI0.SR;
  200. if (sr & 0xF0) tmp = SPI0_POPR; // drain RX FIFO
  201. #endif
  202. } while ((sr & 0xF0F0) > 0); // wait both RX & TX empty
  203. }
  204. #if !defined(__FORCECOMPAT_SPI)//faster
  205. void waitTransmitComplete(void)
  206. __attribute__((always_inline)) {
  207. uint32_t tmp __attribute__((unused));
  208. #if ARDUINO >= 160
  209. while (!(KINETISK_SPI0.SR & SPI_SR_TCF)) ; // wait until final output done
  210. #else
  211. while (!(SPI0.SR & SPI_SR_TCF)) ; // wait until final output done
  212. #endif
  213. tmp = SPI0_POPR; // drain the final RX FIFO word
  214. }
  215. #else
  216. void waitTransmitComplete(uint32_t mcr)
  217. __attribute__((always_inline)) {
  218. uint32_t tmp __attribute__((unused));
  219. #if ARDUINO >= 160
  220. while (1) {
  221. uint32_t sr = KINETISK_SPI0.SR;
  222. if (sr & SPI_SR_EOQF) break; // wait for last transmit
  223. if (sr & 0xF0) tmp = KINETISK_SPI0.POPR;
  224. }
  225. KINETISK_SPI0.SR = SPI_SR_EOQF;
  226. SPI0_MCR = mcr;
  227. while (KINETISK_SPI0.SR & 0xF0) {
  228. tmp = KINETISK_SPI0.POPR;
  229. }
  230. #else
  231. while (1) {
  232. uint32_t sr = SPI0.SR;
  233. if (sr & SPI_SR_EOQF) break; // wait for last transmit
  234. if (sr & 0xF0) tmp = SPI0_POPR;
  235. }
  236. SPI0.SR = SPI_SR_EOQF;
  237. SPI0_MCR = mcr;
  238. while (SPI0.SR & 0xF0) {
  239. tmp = SPI0_POPR;
  240. }
  241. #endif
  242. }
  243. #endif
  244. void writecommand_cont(uint8_t c)
  245. __attribute__((always_inline)) {
  246. #if ARDUINO >= 160
  247. KINETISK_SPI0.PUSHR = c | (pcs_command << 16) | SPI_PUSHR_CTAS(0) | SPI_PUSHR_CONT;
  248. #else
  249. SPI0.PUSHR = c | (pcs_command << 16) | SPI_PUSHR_CTAS(0) | SPI_PUSHR_CONT;
  250. #endif
  251. waitFifoNotFull();
  252. }
  253. void writedata8_cont(uint8_t c)
  254. __attribute__((always_inline)) {
  255. #if ARDUINO >= 160
  256. KINETISK_SPI0.PUSHR = c | (pcs_data << 16) | SPI_PUSHR_CTAS(0) | SPI_PUSHR_CONT;
  257. #else
  258. SPI0.PUSHR = c | (pcs_data << 16) | SPI_PUSHR_CTAS(0) | SPI_PUSHR_CONT;
  259. #endif
  260. waitFifoNotFull();
  261. }
  262. void writedata16_cont(uint16_t d)
  263. __attribute__((always_inline)) {
  264. #if ARDUINO >= 160
  265. KINETISK_SPI0.PUSHR = d | (pcs_data << 16) | SPI_PUSHR_CTAS(1) | SPI_PUSHR_CONT;
  266. #else
  267. SPI0.PUSHR = d | (pcs_data << 16) | SPI_PUSHR_CTAS(1) | SPI_PUSHR_CONT;
  268. #endif
  269. waitFifoNotFull();
  270. }
  271. #if !defined(__FORCECOMPAT_SPI)
  272. void writecommand_last(uint8_t c)
  273. __attribute__((always_inline)) {
  274. waitFifoEmpty();
  275. #if ARDUINO >= 160
  276. KINETISK_SPI0.SR = SPI_SR_TCF;
  277. KINETISK_SPI0.PUSHR = c | (pcs_command << 16) | SPI_PUSHR_CTAS(0);
  278. #else
  279. SPI0.SR = SPI_SR_TCF;
  280. SPI0.PUSHR = c | (pcs_command << 16) | SPI_PUSHR_CTAS(0);
  281. #endif
  282. waitTransmitComplete();
  283. }
  284. void writedata8_last(uint8_t c)
  285. __attribute__((always_inline)) {
  286. waitFifoEmpty();
  287. #if ARDUINO >= 160
  288. KINETISK_SPI0.SR = SPI_SR_TCF;
  289. KINETISK_SPI0.PUSHR = c | (pcs_data << 16) | SPI_PUSHR_CTAS(0);
  290. #else
  291. SPI0.SR = SPI_SR_TCF;
  292. SPI0.PUSHR = c | (pcs_data << 16) | SPI_PUSHR_CTAS(0);
  293. #endif
  294. waitTransmitComplete();
  295. }
  296. void writedata16_last(uint16_t d)
  297. __attribute__((always_inline)) {
  298. waitFifoEmpty();
  299. #if ARDUINO >= 160
  300. KINETISK_SPI0.SR = SPI_SR_TCF;
  301. KINETISK_SPI0.PUSHR = d | (pcs_data << 16) | SPI_PUSHR_CTAS(1);
  302. #else
  303. SPI0.SR = SPI_SR_TCF;
  304. SPI0.PUSHR = d | (pcs_data << 16) | SPI_PUSHR_CTAS(1);
  305. #endif
  306. waitTransmitComplete();
  307. }
  308. #else
  309. void writecommand_last(uint8_t c)
  310. __attribute__((always_inline)) {
  311. uint32_t mcr = SPI0_MCR;
  312. #if ARDUINO >= 160
  313. KINETISK_SPI0.PUSHR = c | (pcs_command << 16) | SPI_PUSHR_CTAS(0) | SPI_PUSHR_EOQ;
  314. #else
  315. SPI0.PUSHR = c | (pcs_command << 16) | SPI_PUSHR_CTAS(0) | SPI_PUSHR_EOQ;
  316. #endif
  317. waitTransmitComplete(mcr);
  318. }
  319. void writedata8_last(uint8_t c)
  320. __attribute__((always_inline)) {
  321. uint32_t mcr = SPI0_MCR;
  322. #if ARDUINO >= 160
  323. KINETISK_SPI0.PUSHR = c | (pcs_data << 16) | SPI_PUSHR_CTAS(0) | SPI_PUSHR_EOQ;
  324. #else
  325. SPI0.PUSHR = c | (pcs_data << 16) | SPI_PUSHR_CTAS(0) | SPI_PUSHR_EOQ;
  326. #endif
  327. waitTransmitComplete(mcr);
  328. }
  329. void writedata16_last(uint16_t d)
  330. __attribute__((always_inline)) {
  331. uint32_t mcr = SPI0_MCR;
  332. #if ARDUINO >= 160
  333. KINETISK_SPI0.PUSHR = d | (pcs_data << 16) | SPI_PUSHR_CTAS(1) | SPI_PUSHR_EOQ;
  334. #else
  335. SPI0.PUSHR = d | (pcs_data << 16) | SPI_PUSHR_CTAS(1) | SPI_PUSHR_EOQ;
  336. #endif
  337. waitTransmitComplete(mcr);
  338. }
  339. #endif
  340. void HLine(int16_t x, int16_t y, int16_t w, uint16_t color)
  341. __attribute__((always_inline)) {
  342. _setAddrWindow(x, y, x+w-1, y);
  343. do { writedata16_cont(color); } while (--w > 0);
  344. }
  345. void VLine(int16_t x, int16_t y, int16_t h, uint16_t color)
  346. __attribute__((always_inline)) {
  347. _setAddrWindow(x, y, x, y+h-1);
  348. do { writedata16_cont(color); } while (--h > 0);
  349. }
  350. void Pixel(int16_t x, int16_t y, uint16_t color)
  351. __attribute__((always_inline)) {
  352. _setAddrWindow(x, y, x, y);
  353. writedata16_cont(color);
  354. }
  355. #else
  356. uint8_t _cs,_rs,_rst;
  357. #endif
  358. #if !defined(__MK20DX128__) && !defined(__MK20DX256__) && !defined(__MK64FX512__) && !defined(__MK66FX1M0__)
  359. void writecommand(uint8_t c);
  360. void writedata(uint8_t d);
  361. void writedata16(uint16_t d);
  362. #endif
  363. private:
  364. void colorSpace(uint8_t cspace);
  365. void setAddr(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1);
  366. uint8_t sleep;
  367. void chipInit();
  368. bool boundaryCheck(int16_t x,int16_t y);
  369. void homeAddress();
  370. uint8_t _initError;
  371. };
  372. #endif