gdisp_lld_ILI9341.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * This file is subject to the terms of the GFX License. If a copy of
  3. * the license was not distributed with this file, you can obtain one at:
  4. *
  5. * http://ugfx.io/license.html
  6. */
  7. #include "gfx.h"
  8. #if GFX_USE_GDISP
  9. #if defined(GDISP_SCREEN_HEIGHT) || defined(GDISP_SCREEN_HEIGHT)
  10. #if GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_DIRECT
  11. #warning "GDISP: This low level driver does not support setting a screen size. It is being ignored."
  12. #elif GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_MACRO
  13. COMPILER_WARNING("GDISP: This low level driver does not support setting a screen size. It is being ignored.")
  14. #endif
  15. #undef GDISP_SCREEN_WIDTH
  16. #undef GDISP_SCREEN_HEIGHT
  17. #endif
  18. #define GDISP_DRIVER_VMT GDISPVMT_ILI9341
  19. #include "gdisp_lld_config.h"
  20. #include "src/gdisp/gdisp_driver.h"
  21. #include "board_ILI9341.h"
  22. /*===========================================================================*/
  23. /* Driver local definitions. */
  24. /*===========================================================================*/
  25. #ifndef GDISP_SCREEN_HEIGHT
  26. #define GDISP_SCREEN_HEIGHT 320
  27. #endif
  28. #ifndef GDISP_SCREEN_WIDTH
  29. #define GDISP_SCREEN_WIDTH 240
  30. #endif
  31. #ifndef GDISP_INITIAL_CONTRAST
  32. #define GDISP_INITIAL_CONTRAST 50
  33. #endif
  34. #ifndef GDISP_INITIAL_BACKLIGHT
  35. #define GDISP_INITIAL_BACKLIGHT 100
  36. #endif
  37. #include "ILI9341.h"
  38. /*===========================================================================*/
  39. /* Driver local functions. */
  40. /*===========================================================================*/
  41. // Some common routines and macros
  42. #define dummy_read(g) { volatile gU16 dummy; dummy = read_data(g); (void) dummy; }
  43. #define write_reg(g, reg, data) { write_index(g, reg); write_data(g, data); }
  44. #define write_data16(g, data) { write_data(g, data >> 8); write_data(g, (gU8)data); }
  45. #define delay(us) gfxSleepMicroseconds(us)
  46. #define delayms(ms) gfxSleepMilliseconds(ms)
  47. static void set_viewport(GDisplay *g) {
  48. write_index(g, 0x2A);
  49. write_data(g, (g->p.x >> 8));
  50. write_data(g, (gU8) g->p.x);
  51. write_data(g, (g->p.x + g->p.cx - 1) >> 8);
  52. write_data(g, (gU8) (g->p.x + g->p.cx - 1));
  53. write_index(g, 0x2B);
  54. write_data(g, (g->p.y >> 8));
  55. write_data(g, (gU8) g->p.y);
  56. write_data(g, (g->p.y + g->p.cy - 1) >> 8);
  57. write_data(g, (gU8) (g->p.y + g->p.cy - 1));
  58. }
  59. /*===========================================================================*/
  60. /* Driver exported functions. */
  61. /*===========================================================================*/
  62. LLDSPEC gBool gdisp_lld_init(GDisplay *g) {
  63. // No private area for this controller
  64. g->priv = 0;
  65. // Initialise the board interface
  66. init_board(g);
  67. // Hardware reset
  68. setpin_reset(g, gTrue);
  69. gfxSleepMilliseconds(20);
  70. setpin_reset(g, gFalse);
  71. gfxSleepMilliseconds(20);
  72. // Get the bus for the following initialisation commands
  73. acquire_bus(g);
  74. write_index(g, 0x01); //software reset
  75. gfxSleepMilliseconds(5);
  76. write_index(g, 0x28);
  77. // display off
  78. //---------------------------------------------------------
  79. write_index(g, 0xcf); //Power control B
  80. write_data(g, 0x00);
  81. __NOP();
  82. write_data(g, 0xc1); // 0x83 + PCEQ (Power Saving)
  83. write_data(g, 0x30);
  84. write_index(g, 0xed); //Power on sequence control
  85. write_data(g, 0x64);
  86. write_data(g, 0x03);
  87. write_data(g, 0x12);
  88. write_data(g, 0x81);
  89. write_index(g, 0xe8); //Driver timing control A
  90. write_data(g, 0x85);
  91. write_data(g, 0x10); // 01
  92. write_data(g, 0x7a); // 79
  93. write_index(g, 0xf7); //Pump ratio control
  94. write_data(g, 0x20);
  95. write_index(g, 0xea); //Driver timing control B
  96. write_data(g, 0x00);
  97. write_data(g, 0x00);
  98. write_index(g, 0xc0); //Power control 1
  99. write_data(g, 0x1b); // 26
  100. write_index(g, 0xc1); //Power control 2
  101. write_data(g, 0x01); // 11
  102. write_index(g, 0xc5); //Vcom control 1
  103. write_data(g, 0x30); // 35
  104. write_data(g, 0x30); // 3E
  105. write_index(g, 0xc7); //Vcom control 2
  106. write_data(g, 0xb7); // 0xbe
  107. write_index(g, 0x36); //Memory access control
  108. write_data(g, 0x48); // 0048 my,mx,mv,ml,BGR,mh,0.0
  109. write_index(g, 0x3a); //Pixel format set
  110. write_data(g, 0x55); // 16 bit/pixel
  111. write_index(g, 0xb1); //Frame Rate Control
  112. write_data(g, 0x00);
  113. write_data(g, 0x1a); // 1b
  114. write_index(g, 0xb6); //Display function control
  115. write_data(g, 0x0a);
  116. write_data(g, 0x82);
  117. write_index(g, 0xf2); //3Gamma Function Disable
  118. write_data(g, 0x00); // 08
  119. write_index(g, 0xE0); //Positive Gamma Correction
  120. write_data(g, 0x0f); // 1a
  121. write_data(g, 0x2a); // 1f
  122. write_data(g, 0x28); // 18
  123. write_data(g, 0x08); // 0a
  124. write_data(g, 0x0e); // 0f
  125. write_data(g, 0x08); // 06
  126. write_data(g, 0x54); // 45
  127. write_data(g, 0xa9); // 87
  128. write_data(g, 0x43); // 32
  129. write_data(g, 0x0a); // 0a
  130. write_data(g, 0x0f); // 07
  131. write_data(g, 0x00); // 02
  132. write_data(g, 0x00); // 07
  133. write_data(g, 0x00); // 05
  134. write_data(g, 0x00);
  135. write_index(g, 0xE1); //Negative Gamma Correction
  136. write_data(g, 0x00);
  137. write_data(g, 0x15); // 25
  138. write_data(g, 0x17); // 27
  139. write_data(g, 0x07); // 05
  140. write_data(g, 0x11); // 10
  141. write_data(g, 0x06); // 09
  142. write_data(g, 0x2b); // 3a
  143. write_data(g, 0x56); // 78
  144. write_data(g, 0x3c); // 4d
  145. write_data(g, 0x05);
  146. write_data(g, 0x10); // 18
  147. write_data(g, 0x0f); // 0d
  148. write_data(g, 0x3f); // 38
  149. write_data(g, 0x3f); // 3a
  150. write_data(g, 0x0f); // 1f
  151. write_index(g, 0x2b); //Page Address Set, size = 319
  152. write_data(g, 0x00);
  153. write_data(g, 0x00);
  154. write_data(g, 0x01);
  155. write_data(g, 0x3F);
  156. write_index(g, 0x2a); //Column Address Set, size = 239
  157. write_data(g, 0x00);
  158. write_data(g, 0x00);
  159. write_data(g, 0x00);
  160. write_data(g, 0xEF);
  161. write_index(g, 0x11); //Sleep Out
  162. gfxSleepMilliseconds(5);
  163. write_index(g, 0x29); //Display ON
  164. // Finish Init
  165. post_init_board(g);
  166. // Release the bus
  167. release_bus(g);
  168. /* Turn on the back-light */
  169. set_backlight(g, GDISP_INITIAL_BACKLIGHT);
  170. /* Initialise the GDISP structure */
  171. g->g.Width = GDISP_SCREEN_WIDTH;
  172. g->g.Height = GDISP_SCREEN_HEIGHT;
  173. g->g.Orientation = gOrientation0;
  174. g->g.Powermode = gPowerOn;
  175. g->g.Backlight = GDISP_INITIAL_BACKLIGHT;
  176. g->g.Contrast = GDISP_INITIAL_CONTRAST;
  177. return gTrue;
  178. }
  179. #if GDISP_HARDWARE_STREAM_WRITE
  180. LLDSPEC void gdisp_lld_write_start(GDisplay *g) {
  181. acquire_bus(g);
  182. set_viewport(g);
  183. write_index(g, 0x2C);
  184. }
  185. LLDSPEC void gdisp_lld_write_color(GDisplay *g) {
  186. //write_data16(g, gdispColor2Native(g->p.color));
  187. write_data(g, gdispColor2Native(g->p.color));
  188. }
  189. LLDSPEC void gdisp_lld_write_stop(GDisplay *g) {
  190. release_bus(g);
  191. }
  192. #endif
  193. #if GDISP_HARDWARE_STREAM_READ
  194. LLDSPEC void gdisp_lld_read_start(GDisplay *g) {
  195. acquire_bus(g);
  196. set_viewport(g);
  197. write_index(g, 0x2E);
  198. setreadmode(g);
  199. dummy_read(g);
  200. }
  201. LLDSPEC gColor gdisp_lld_read_color(GDisplay *g) {
  202. gU16 data;
  203. data = read_data(g);
  204. return gdispNative2Color(data);
  205. }
  206. LLDSPEC void gdisp_lld_read_stop(GDisplay *g) {
  207. setwritemode(g);
  208. release_bus(g);
  209. }
  210. #endif
  211. #if GDISP_NEED_CONTROL && GDISP_HARDWARE_CONTROL
  212. LLDSPEC void gdisp_lld_control(GDisplay *g) {
  213. switch(g->p.x) {
  214. case GDISP_CONTROL_POWER:
  215. if (g->g.Powermode == (gPowermode)g->p.ptr)
  216. return;
  217. switch((gPowermode)g->p.ptr) {
  218. case gPowerOff:
  219. case gPowerSleep:
  220. case gPowerDeepSleep:
  221. acquire_bus(g);
  222. write_reg(g, 0x0010, 0x0001); /* enter sleep mode */
  223. release_bus(g);
  224. break;
  225. case gPowerOn:
  226. acquire_bus(g);
  227. write_reg(g, 0x0011, 0x0000); /* leave sleep mode */
  228. gfxSleepMilliseconds(5);
  229. release_bus(g);
  230. break;
  231. default:
  232. return;
  233. }
  234. g->g.Powermode = (gPowermode)g->p.ptr;
  235. return;
  236. case GDISP_CONTROL_ORIENTATION:
  237. if (g->g.Orientation == (gOrientation)g->p.ptr)
  238. return;
  239. switch((gOrientation)g->p.ptr) {
  240. case gOrientation0:
  241. acquire_bus(g);
  242. write_reg(g, 0x36, 0x48); /* X and Y axes non-inverted */
  243. release_bus(g);
  244. g->g.Height = GDISP_SCREEN_HEIGHT;
  245. g->g.Width = GDISP_SCREEN_WIDTH;
  246. break;
  247. case gOrientation90:
  248. acquire_bus(g);
  249. write_reg(g, 0x36, 0xE8); /* Invert X and Y axes */
  250. release_bus(g);
  251. g->g.Height = GDISP_SCREEN_WIDTH;
  252. g->g.Width = GDISP_SCREEN_HEIGHT;
  253. break;
  254. case gOrientation180:
  255. acquire_bus(g);
  256. write_reg(g, 0x36, 0x88); /* X and Y axes non-inverted */
  257. release_bus(g);
  258. g->g.Height = GDISP_SCREEN_HEIGHT;
  259. g->g.Width = GDISP_SCREEN_WIDTH;
  260. break;
  261. case gOrientation270:
  262. acquire_bus(g);
  263. write_reg(g, 0x36, 0x28); /* Invert X and Y axes */
  264. release_bus(g);
  265. g->g.Height = GDISP_SCREEN_WIDTH;
  266. g->g.Width = GDISP_SCREEN_HEIGHT;
  267. break;
  268. default:
  269. return;
  270. }
  271. g->g.Orientation = (gOrientation)g->p.ptr;
  272. return;
  273. case GDISP_CONTROL_BACKLIGHT:
  274. if ((unsigned)g->p.ptr > 100)
  275. g->p.ptr = (void *)100;
  276. set_backlight(g, (unsigned)g->p.ptr);
  277. g->g.Backlight = (unsigned)g->p.ptr;
  278. return;
  279. //case GDISP_CONTROL_CONTRAST:
  280. default:
  281. return;
  282. }
  283. }
  284. #endif
  285. #endif /* GFX_USE_GDISP */