gdisp_lld_ILI9341.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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, 0xcb); //Power control A, default values:
  94. write_data(g, 0x39);
  95. write_data(g, 0x2c);
  96. write_data(g, 0x00);
  97. write_data(g, 0x34);
  98. write_data(g, 0x02);
  99. write_index(g, 0xf7); //Pump ratio control
  100. write_data(g, 0x20);
  101. write_index(g, 0xea); //Driver timing control B
  102. write_data(g, 0x00);
  103. write_data(g, 0x00);
  104. write_index(g, 0xc0); //Power control 1
  105. write_data(g, 0x1b); // 26
  106. write_index(g, 0xc1); //Power control 2
  107. write_data(g, 0x01); // 11
  108. write_index(g, 0xc5); //Vcom control 1
  109. write_data(g, 0x30); // 35
  110. write_data(g, 0x30); // 3E
  111. write_index(g, 0xc7); //Vcom control 2
  112. write_data(g, 0xb7); // 0xbe
  113. write_index(g, 0x36); //Memory access control
  114. write_data(g, 0x48); // 0048 my,mx,mv,ml,BGR,mh,0.0
  115. write_index(g, 0x3a); //Pixel format set
  116. write_data(g, 0x55); // 16 bit/pixel
  117. write_index(g, 0xb1); //Frame Rate Control
  118. write_data(g, 0x00);
  119. write_data(g, 0x1a); // 1b
  120. write_index(g, 0xb6); //Display function control
  121. write_data(g, 0x0a);
  122. write_data(g, 0xa2); //82
  123. // write_data(g, 0x27);
  124. // write_data(g, 0x00);
  125. write_index(g, 0xf2); //3Gamma Function Disable
  126. write_data(g, 0x00); // 08
  127. write_index(g, 0x26); //Gamma Set
  128. write_data(g, 0x01); //default value
  129. write_index(g, 0xE0); //Positive Gamma Correction
  130. write_data(g, 0x0f); // 1a
  131. write_data(g, 0x2a); // 1f
  132. write_data(g, 0x28); // 18
  133. write_data(g, 0x08); // 0a
  134. write_data(g, 0x0e); // 0f
  135. write_data(g, 0x08); // 06
  136. write_data(g, 0x54); // 45
  137. write_data(g, 0xa9); // 87
  138. write_data(g, 0x43); // 32
  139. write_data(g, 0x0a); // 0a
  140. write_data(g, 0x0f); // 07
  141. write_data(g, 0x00); // 02
  142. write_data(g, 0x00); // 07
  143. write_data(g, 0x00); // 05
  144. write_data(g, 0x00);
  145. write_index(g, 0xE1); //Negative Gamma Correction
  146. write_data(g, 0x00);
  147. write_data(g, 0x15); // 25
  148. write_data(g, 0x17); // 27
  149. write_data(g, 0x07); // 05
  150. write_data(g, 0x11); // 10
  151. write_data(g, 0x06); // 09
  152. write_data(g, 0x2b); // 3a
  153. write_data(g, 0x56); // 78
  154. write_data(g, 0x3c); // 4d
  155. write_data(g, 0x05);
  156. write_data(g, 0x10); // 18
  157. write_data(g, 0x0f); // 0d
  158. write_data(g, 0x3f); // 38
  159. write_data(g, 0x3f); // 3a
  160. write_data(g, 0x0f); // 1f
  161. write_index(g, 0x2b); //Page Address Set, size = 319
  162. write_data(g, 0x00);
  163. write_data(g, 0x00);
  164. write_data(g, 0x01);
  165. write_data(g, 0x3F);
  166. write_index(g, 0x2a); //Column Address Set, size = 239
  167. write_data(g, 0x00);
  168. write_data(g, 0x00);
  169. write_data(g, 0x00);
  170. write_data(g, 0xEF);
  171. write_index(g, 0x11); //Sleep Out
  172. gfxSleepMilliseconds(5); // 120 ?
  173. write_index(g, 0x29); //Display ON
  174. // Finish Init
  175. post_init_board(g);
  176. // Release the bus
  177. release_bus(g);
  178. /* Turn on the back-light */
  179. set_backlight(g, GDISP_INITIAL_BACKLIGHT);
  180. /* Initialise the GDISP structure */
  181. g->g.Width = GDISP_SCREEN_WIDTH;
  182. g->g.Height = GDISP_SCREEN_HEIGHT;
  183. g->g.Orientation = gOrientation0;
  184. g->g.Powermode = gPowerOn;
  185. g->g.Backlight = GDISP_INITIAL_BACKLIGHT;
  186. g->g.Contrast = GDISP_INITIAL_CONTRAST;
  187. return gTrue;
  188. }
  189. #if GDISP_HARDWARE_STREAM_WRITE
  190. LLDSPEC void gdisp_lld_write_start(GDisplay *g) {
  191. acquire_bus(g);
  192. set_viewport(g);
  193. write_index(g, 0x2C);
  194. }
  195. LLDSPEC void gdisp_lld_write_color(GDisplay *g) {
  196. //write_data16(g, gdispColor2Native(g->p.color));
  197. write_data(g, gdispColor2Native(g->p.color));
  198. }
  199. LLDSPEC void gdisp_lld_write_stop(GDisplay *g) {
  200. release_bus(g);
  201. }
  202. #endif
  203. #if GDISP_HARDWARE_STREAM_READ
  204. LLDSPEC void gdisp_lld_read_start(GDisplay *g) {
  205. acquire_bus(g);
  206. set_viewport(g);
  207. write_index(g, 0x2E);
  208. setreadmode(g);
  209. dummy_read(g);
  210. }
  211. LLDSPEC gColor gdisp_lld_read_color(GDisplay *g) {
  212. gU16 data;
  213. data = read_data(g);
  214. return gdispNative2Color(data);
  215. }
  216. LLDSPEC void gdisp_lld_read_stop(GDisplay *g) {
  217. setwritemode(g);
  218. release_bus(g);
  219. }
  220. #endif
  221. #if GDISP_NEED_CONTROL && GDISP_HARDWARE_CONTROL
  222. LLDSPEC void gdisp_lld_control(GDisplay *g) {
  223. switch(g->p.x) {
  224. case GDISP_CONTROL_POWER:
  225. if (g->g.Powermode == (gPowermode)g->p.ptr)
  226. return;
  227. switch((gPowermode)g->p.ptr) {
  228. case gPowerOff:
  229. case gPowerSleep:
  230. case gPowerDeepSleep:
  231. acquire_bus(g);
  232. write_reg(g, 0x0010, 0x0001); /* enter sleep mode */
  233. release_bus(g);
  234. break;
  235. case gPowerOn:
  236. acquire_bus(g);
  237. write_reg(g, 0x0011, 0x0000); /* leave sleep mode */
  238. gfxSleepMilliseconds(5);
  239. release_bus(g);
  240. break;
  241. default:
  242. return;
  243. }
  244. g->g.Powermode = (gPowermode)g->p.ptr;
  245. return;
  246. case GDISP_CONTROL_ORIENTATION:
  247. if (g->g.Orientation == (gOrientation)g->p.ptr)
  248. return;
  249. switch((gOrientation)g->p.ptr) {
  250. case gOrientation0:
  251. acquire_bus(g);
  252. write_reg(g, 0x36, 0x48); /* X and Y axes non-inverted */
  253. release_bus(g);
  254. g->g.Height = GDISP_SCREEN_HEIGHT;
  255. g->g.Width = GDISP_SCREEN_WIDTH;
  256. break;
  257. case gOrientation90:
  258. acquire_bus(g);
  259. write_reg(g, 0x36, 0xE8); /* Invert X and Y axes */
  260. release_bus(g);
  261. g->g.Height = GDISP_SCREEN_WIDTH;
  262. g->g.Width = GDISP_SCREEN_HEIGHT;
  263. break;
  264. case gOrientation180:
  265. acquire_bus(g);
  266. write_reg(g, 0x36, 0x88); /* X and Y axes non-inverted */
  267. release_bus(g);
  268. g->g.Height = GDISP_SCREEN_HEIGHT;
  269. g->g.Width = GDISP_SCREEN_WIDTH;
  270. break;
  271. case gOrientation270:
  272. acquire_bus(g);
  273. write_reg(g, 0x36, 0x28); /* Invert X and Y axes */
  274. release_bus(g);
  275. g->g.Height = GDISP_SCREEN_WIDTH;
  276. g->g.Width = GDISP_SCREEN_HEIGHT;
  277. break;
  278. default:
  279. return;
  280. }
  281. g->g.Orientation = (gOrientation)g->p.ptr;
  282. return;
  283. case GDISP_CONTROL_BACKLIGHT:
  284. if ((unsigned)g->p.ptr > 100)
  285. g->p.ptr = (void *)100;
  286. set_backlight(g, (unsigned)g->p.ptr);
  287. g->g.Backlight = (unsigned)g->p.ptr;
  288. return;
  289. //case GDISP_CONTROL_CONTRAST:
  290. default:
  291. return;
  292. }
  293. }
  294. #endif
  295. #endif /* GFX_USE_GDISP */