Adafruit_ILI9341.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*!
  2. * @file Adafruit_ILI9341.cpp
  3. *
  4. * @mainpage Adafruit ILI9341 TFT Displays
  5. *
  6. * @section intro_sec Introduction
  7. *
  8. * This is the documentation for Adafruit's ILI9341 driver for the
  9. * Arduino platform.
  10. *
  11. * This library works with the Adafruit 2.8" Touch Shield V2 (SPI)
  12. * http://www.adafruit.com/products/1651
  13. *
  14. * Adafruit 2.4" TFT LCD with Touchscreen Breakout w/MicroSD Socket - ILI9341
  15. * https://www.adafruit.com/product/2478
  16. *
  17. * 2.8" TFT LCD with Touchscreen Breakout Board w/MicroSD Socket - ILI9341
  18. * https://www.adafruit.com/product/1770
  19. *
  20. * 2.2" 18-bit color TFT LCD display with microSD card breakout - ILI9340
  21. * https://www.adafruit.com/product/1770
  22. *
  23. * TFT FeatherWing - 2.4" 320x240 Touchscreen For All Feathers
  24. * https://www.adafruit.com/product/3315
  25. *
  26. * These displays use SPI to communicate, 4 or 5 pins are required
  27. * to interface (RST is optional).
  28. *
  29. * Adafruit invests time and resources providing this open source code,
  30. * please support Adafruit and open-source hardware by purchasing
  31. * products from Adafruit!
  32. *
  33. * @section dependencies Dependencies
  34. *
  35. * This library depends on <a href="https://github.com/adafruit/Adafruit_GFX">
  36. * Adafruit_GFX</a> being present on your system. Please make sure you have
  37. * installed the latest version before using this library.
  38. *
  39. * @section author Author
  40. *
  41. * Written by Limor "ladyada" Fried for Adafruit Industries.
  42. *
  43. * @section license License
  44. *
  45. * BSD license, all text here must be included in any redistribution.
  46. *
  47. */
  48. #include "Adafruit_ILI9341.h"
  49. #ifndef ARDUINO_STM32_FEATHER
  50. #include "pins_arduino.h"
  51. #ifndef RASPI
  52. #include "wiring_private.h"
  53. #endif
  54. #endif
  55. #include <limits.h>
  56. #if defined (ARDUINO_ARCH_ARC32) || defined (ARDUINO_MAXIM)
  57. #define SPI_DEFAULT_FREQ 16000000
  58. #elif defined (__AVR__) || defined(TEENSYDUINO)
  59. #define SPI_DEFAULT_FREQ 8000000
  60. #elif defined(ESP8266) || defined(ESP32)
  61. #define SPI_DEFAULT_FREQ 40000000
  62. #elif defined(RASPI)
  63. #define SPI_DEFAULT_FREQ 80000000
  64. #elif defined(ARDUINO_ARCH_STM32F1)
  65. #define SPI_DEFAULT_FREQ 36000000
  66. #else
  67. #define SPI_DEFAULT_FREQ 24000000 ///< Default SPI data clock frequency
  68. #endif
  69. #define MADCTL_MY 0x80 ///< Bottom to top
  70. #define MADCTL_MX 0x40 ///< Right to left
  71. #define MADCTL_MV 0x20 ///< Reverse Mode
  72. #define MADCTL_ML 0x10 ///< LCD refresh Bottom to top
  73. #define MADCTL_RGB 0x00 ///< Red-Green-Blue pixel order
  74. #define MADCTL_BGR 0x08 ///< Blue-Green-Red pixel order
  75. #define MADCTL_MH 0x04 ///< LCD refresh right to left
  76. /**************************************************************************/
  77. /*!
  78. @brief Instantiate Adafruit ILI9341 driver with software SPI
  79. @param cs Chip select pin #
  80. @param dc Data/Command pin #
  81. @param mosi SPI MOSI pin #
  82. @param sclk SPI Clock pin #
  83. @param rst Reset pin # (optional, pass -1 if unused)
  84. @param miso SPI MISO pin # (optional, pass -1 if unused)
  85. */
  86. /**************************************************************************/
  87. Adafruit_ILI9341::Adafruit_ILI9341(int8_t cs, int8_t dc, int8_t mosi,
  88. int8_t sclk, int8_t rst, int8_t miso) : Adafruit_SPITFT(ILI9341_TFTWIDTH, ILI9341_TFTHEIGHT, cs, dc, mosi, sclk, rst, miso) {
  89. }
  90. /**************************************************************************/
  91. /*!
  92. @brief Instantiate Adafruit ILI9341 driver with hardware SPI
  93. @param cs Chip select pin #
  94. @param dc Data/Command pin #
  95. @param rst Reset pin # (optional, pass -1 if unused)
  96. */
  97. /**************************************************************************/
  98. Adafruit_ILI9341::Adafruit_ILI9341(int8_t cs, int8_t dc, int8_t rst) : Adafruit_SPITFT(ILI9341_TFTWIDTH, ILI9341_TFTHEIGHT, cs, dc, rst) {
  99. }
  100. static const uint8_t PROGMEM initcmd[] = {
  101. 0xEF, 3, 0x03, 0x80, 0x02,
  102. 0xCF, 3, 0x00, 0xC1, 0x30,
  103. 0xED, 4, 0x64, 0x03, 0x12, 0x81,
  104. 0xE8, 3, 0x85, 0x00, 0x78,
  105. 0xCB, 5, 0x39, 0x2C, 0x00, 0x34, 0x02,
  106. 0xF7, 1, 0x20,
  107. 0xEA, 2, 0x00, 0x00,
  108. ILI9341_PWCTR1 , 1, 0x23, // Power control VRH[5:0]
  109. ILI9341_PWCTR2 , 1, 0x10, // Power control SAP[2:0];BT[3:0]
  110. ILI9341_VMCTR1 , 2, 0x3e, 0x28, // VCM control
  111. ILI9341_VMCTR2 , 1, 0x86, // VCM control2
  112. ILI9341_MADCTL , 1, 0x48, // Memory Access Control
  113. ILI9341_VSCRSADD, 1, 0x00, // Vertical scroll zero
  114. ILI9341_PIXFMT , 1, 0x55,
  115. ILI9341_FRMCTR1 , 2, 0x00, 0x18,
  116. ILI9341_DFUNCTR , 3, 0x08, 0x82, 0x27, // Display Function Control
  117. 0xF2, 1, 0x00, // 3Gamma Function Disable
  118. ILI9341_GAMMASET , 1, 0x01, // Gamma curve selected
  119. ILI9341_GMCTRP1 , 15, 0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08, // Set Gamma
  120. 0x4E, 0xF1, 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00,
  121. ILI9341_GMCTRN1 , 15, 0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, // Set Gamma
  122. 0x31, 0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F,
  123. ILI9341_SLPOUT , 0x80, // Exit Sleep
  124. ILI9341_DISPON , 0x80, // Display on
  125. 0x00 // End of list
  126. };
  127. /**************************************************************************/
  128. /*!
  129. @brief Initialize ILI9341 chip
  130. Connects to the ILI9341 over SPI and sends initialization procedure commands
  131. @param freq Desired SPI clock frequency
  132. */
  133. /**************************************************************************/
  134. void Adafruit_ILI9341::begin(uint32_t freq) {
  135. if(!freq) freq = SPI_DEFAULT_FREQ;
  136. _freq = freq;
  137. initSPI(freq);
  138. startWrite();
  139. uint8_t cmd, x, numArgs;
  140. const uint8_t *addr = initcmd;
  141. while((cmd = pgm_read_byte(addr++)) > 0) {
  142. writeCommand(cmd);
  143. x = pgm_read_byte(addr++);
  144. numArgs = x & 0x7F;
  145. while(numArgs--) spiWrite(pgm_read_byte(addr++));
  146. if(x & 0x80) delay(120);
  147. }
  148. endWrite();
  149. _width = ILI9341_TFTWIDTH;
  150. _height = ILI9341_TFTHEIGHT;
  151. }
  152. /**************************************************************************/
  153. /*!
  154. @brief Set origin of (0,0) and orientation of TFT display
  155. @param m The index for rotation, from 0-3 inclusive
  156. */
  157. /**************************************************************************/
  158. void Adafruit_ILI9341::setRotation(uint8_t m) {
  159. rotation = m % 4; // can't be higher than 3
  160. switch (rotation) {
  161. case 0:
  162. m = (MADCTL_MX | MADCTL_BGR);
  163. _width = ILI9341_TFTWIDTH;
  164. _height = ILI9341_TFTHEIGHT;
  165. break;
  166. case 1:
  167. m = (MADCTL_MV | MADCTL_BGR);
  168. _width = ILI9341_TFTHEIGHT;
  169. _height = ILI9341_TFTWIDTH;
  170. break;
  171. case 2:
  172. m = (MADCTL_MY | MADCTL_BGR);
  173. _width = ILI9341_TFTWIDTH;
  174. _height = ILI9341_TFTHEIGHT;
  175. break;
  176. case 3:
  177. m = (MADCTL_MX | MADCTL_MY | MADCTL_MV | MADCTL_BGR);
  178. _width = ILI9341_TFTHEIGHT;
  179. _height = ILI9341_TFTWIDTH;
  180. break;
  181. }
  182. startWrite();
  183. writeCommand(ILI9341_MADCTL);
  184. spiWrite(m);
  185. endWrite();
  186. }
  187. /**************************************************************************/
  188. /*!
  189. @brief Enable/Disable display color inversion
  190. @param invert True to invert, False to have normal color
  191. */
  192. /**************************************************************************/
  193. void Adafruit_ILI9341::invertDisplay(boolean invert) {
  194. startWrite();
  195. writeCommand(invert ? ILI9341_INVON : ILI9341_INVOFF);
  196. endWrite();
  197. }
  198. /**************************************************************************/
  199. /*!
  200. @brief Scroll display memory
  201. @param y How many pixels to scroll display by
  202. */
  203. /**************************************************************************/
  204. void Adafruit_ILI9341::scrollTo(uint16_t y) {
  205. startWrite();
  206. writeCommand(ILI9341_VSCRSADD);
  207. SPI_WRITE16(y);
  208. endWrite();
  209. }
  210. /**************************************************************************/
  211. void Adafruit_ILI9341::setScrollMargins(uint16_t top, uint16_t bottom)
  212. {
  213. uint16_t height = _height - (top + bottom);
  214. startWrite();
  215. writeCommand(0x33);
  216. SPI_WRITE16(top);
  217. SPI_WRITE16(height);
  218. SPI_WRITE16(bottom);
  219. endWrite();
  220. }
  221. /**************************************************************************/
  222. /*!
  223. @brief Set the "address window" - the rectangle we will write to RAM with the next chunk of SPI data writes. The ILI9341 will automatically wrap the data as each row is filled
  224. @param x TFT memory 'x' origin
  225. @param y TFT memory 'y' origin
  226. @param w Width of rectangle
  227. @param h Height of rectangle
  228. */
  229. /**************************************************************************/
  230. void Adafruit_ILI9341::setAddrWindow(uint16_t x, uint16_t y, uint16_t w, uint16_t h) {
  231. uint32_t xa = ((uint32_t)x << 16) | (x+w-1);
  232. uint32_t ya = ((uint32_t)y << 16) | (y+h-1);
  233. writeCommand(ILI9341_CASET); // Column addr set
  234. SPI_WRITE32(xa);
  235. writeCommand(ILI9341_PASET); // Row addr set
  236. SPI_WRITE32(ya);
  237. writeCommand(ILI9341_RAMWR); // write to RAM
  238. }
  239. /**************************************************************************/
  240. /*!
  241. @brief Read 8 bits of data from ILI9341 configuration memory. NOT from RAM!
  242. This is highly undocumented/supported, it's really a hack but kinda works?
  243. @param command The command register to read data from
  244. @param index The byte index into the command to read from
  245. @return Unsigned 8-bit data read from ILI9341 register
  246. */
  247. /**************************************************************************/
  248. uint8_t Adafruit_ILI9341::readcommand8(uint8_t command, uint8_t index) {
  249. uint32_t freq = _freq;
  250. if(_freq > 24000000) _freq = 24000000;
  251. startWrite();
  252. writeCommand(0xD9); // woo sekret command?
  253. spiWrite(0x10 + index);
  254. writeCommand(command);
  255. uint8_t r = spiRead();
  256. endWrite();
  257. _freq = freq;
  258. return r;
  259. }