µGFX library fork

gdisp_lld_ILI9341.c 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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.org/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 uint16_t 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, (uint8_t)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, (uint8_t) g->p.x);
  51. write_data(g, (g->p.x + g->p.cx - 1) >> 8);
  52. write_data(g, (uint8_t) (g->p.x + g->p.cx - 1));
  53. write_index(g, 0x2B);
  54. write_data(g, (g->p.y >> 8));
  55. write_data(g, (uint8_t) g->p.y);
  56. write_data(g, (g->p.y + g->p.cy - 1) >> 8);
  57. write_data(g, (uint8_t) (g->p.y + g->p.cy - 1));
  58. }
  59. /*===========================================================================*/
  60. /* Driver exported functions. */
  61. /*===========================================================================*/
  62. LLDSPEC bool_t 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, TRUE);
  69. gfxSleepMilliseconds(20);
  70. setpin_reset(g, FALSE);
  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. // magic?
  80. write_index(g, 0xcf);
  81. write_data(g, 0x00);
  82. write_data(g, 0x83);
  83. write_data(g, 0x30);
  84. write_index(g, 0xed);
  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);
  90. write_data(g, 0x85);
  91. write_data(g, 0x01);
  92. write_data(g, 0x79);
  93. write_index(g, 0xcb);
  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);
  100. write_data(g, 0x20);
  101. write_index(g, 0xea);
  102. write_data(g, 0x00);
  103. write_data(g, 0x00);
  104. //------------power control------------------------------
  105. write_index(g, 0xc0); //power control
  106. write_data(g, 0x26);
  107. write_index(g, 0xc1); //power control
  108. write_data(g, 0x11);
  109. //--------------VCOM
  110. write_index(g, 0xc5); //vcom control
  111. write_data(g, 0x35);//35
  112. write_data(g, 0x3e);//3E
  113. write_index(g, 0xc7); //vcom control
  114. write_data(g, 0xbe); // 0x94
  115. //------------memory access control------------------------
  116. write_index(g, 0x36);
  117. // memory access control
  118. write_data(g, 0x48); //0048 my,mx,mv,ml,BGR,mh,0.0
  119. write_index(g, 0x3a); // pixel format set
  120. write_data(g, 0x55);//16bit /pixel
  121. //----------------- frame rate------------------------------
  122. write_index(g, 0xb1);
  123. // frame rate
  124. write_data(g, 0x00);
  125. write_data(g, 0x1B); //70
  126. //----------------Gamma---------------------------------
  127. write_index(g, 0xf2); // 3Gamma Function Disable
  128. write_data(g, 0x08);
  129. write_index(g, 0x26);
  130. write_data(g, 0x01); // gamma set 4 gamma curve 01/02/04/08
  131. write_index(g, 0xE0); //positive gamma correction
  132. write_data(g, 0x1f);
  133. write_data(g, 0x1a);
  134. write_data(g, 0x18);
  135. write_data(g, 0x0a);
  136. write_data(g, 0x0f);
  137. write_data(g, 0x06);
  138. write_data(g, 0x45);
  139. write_data(g, 0x87);
  140. write_data(g, 0x32);
  141. write_data(g, 0x0a);
  142. write_data(g, 0x07);
  143. write_data(g, 0x02);
  144. write_data(g, 0x07);
  145. write_data(g, 0x05);
  146. write_data(g, 0x00);
  147. write_index(g, 0xE1); //negamma correction
  148. write_data(g, 0x00);
  149. write_data(g, 0x25);
  150. write_data(g, 0x27);
  151. write_data(g, 0x05);
  152. write_data(g, 0x10);
  153. write_data(g, 0x09);
  154. write_data(g, 0x3a);
  155. write_data(g, 0x78);
  156. write_data(g, 0x4d);
  157. write_data(g, 0x05);
  158. write_data(g, 0x18);
  159. write_data(g, 0x0d);
  160. write_data(g, 0x38);
  161. write_data(g, 0x3a);
  162. write_data(g, 0x1f);
  163. //--------------ddram ---------------------
  164. write_index(g, 0x2a);
  165. // column set
  166. // 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, 0x2b);
  172. // page address set
  173. // size = 319
  174. write_data(g, 0x00);
  175. write_data(g, 0x00);
  176. write_data(g, 0x01);
  177. write_data(g, 0x3F);
  178. // write_index(g, 0x34);
  179. //write_index(g, 0x35);
  180. // tearing effect off
  181. // tearing effect on
  182. // write_index(g, 0xb4); // display inversion
  183. // write_data(g, 0x00);
  184. write_index(g, 0xb7); //entry mode set
  185. write_data(g, 0x07);
  186. //-----------------display---------------------
  187. write_index(g, 0xb6);
  188. // display function control
  189. write_data(g, 0x0a);
  190. write_data(g, 0x82);
  191. write_data(g, 0x27);
  192. write_data(g, 0x00);
  193. write_index(g, 0x11); //sleep out
  194. gfxSleepMilliseconds(100);
  195. write_index(g, 0x29); // display on
  196. gfxSleepMilliseconds(100);
  197. // Finish Init
  198. post_init_board(g);
  199. // Release the bus
  200. release_bus(g);
  201. /* Turn on the back-light */
  202. set_backlight(g, GDISP_INITIAL_BACKLIGHT);
  203. /* Initialise the GDISP structure */
  204. g->g.Width = GDISP_SCREEN_WIDTH;
  205. g->g.Height = GDISP_SCREEN_HEIGHT;
  206. g->g.Orientation = GDISP_ROTATE_0;
  207. g->g.Powermode = powerOn;
  208. g->g.Backlight = GDISP_INITIAL_BACKLIGHT;
  209. g->g.Contrast = GDISP_INITIAL_CONTRAST;
  210. return TRUE;
  211. }
  212. #if GDISP_HARDWARE_STREAM_WRITE
  213. LLDSPEC void gdisp_lld_write_start(GDisplay *g) {
  214. acquire_bus(g);
  215. set_viewport(g);
  216. write_index(g, 0x2C);
  217. }
  218. LLDSPEC void gdisp_lld_write_color(GDisplay *g) {
  219. write_data16(g, gdispColor2Native(g->p.color));
  220. }
  221. LLDSPEC void gdisp_lld_write_stop(GDisplay *g) {
  222. release_bus(g);
  223. }
  224. #endif
  225. #if GDISP_HARDWARE_STREAM_READ
  226. LLDSPEC void gdisp_lld_read_start(GDisplay *g) {
  227. acquire_bus(g);
  228. set_viewport(g);
  229. write_index(g, 0x2E);
  230. setreadmode(g);
  231. dummy_read(g);
  232. }
  233. LLDSPEC color_t gdisp_lld_read_color(GDisplay *g) {
  234. uint16_t data;
  235. data = read_data(g);
  236. return gdispNative2Color(data);
  237. }
  238. LLDSPEC void gdisp_lld_read_stop(GDisplay *g) {
  239. setwritemode(g);
  240. release_bus(g);
  241. }
  242. #endif
  243. #if GDISP_NEED_CONTROL && GDISP_HARDWARE_CONTROL
  244. LLDSPEC void gdisp_lld_control(GDisplay *g) {
  245. switch(g->p.x) {
  246. case GDISP_CONTROL_POWER:
  247. if (g->g.Powermode == (powermode_t)g->p.ptr)
  248. return;
  249. switch((powermode_t)g->p.ptr) {
  250. case powerOff:
  251. case powerSleep:
  252. case powerDeepSleep:
  253. acquire_bus(g);
  254. write_reg(g, 0x0010, 0x0001); /* enter sleep mode */
  255. release_bus(g);
  256. break;
  257. case powerOn:
  258. acquire_bus(g);
  259. write_reg(g, 0x0010, 0x0000); /* leave sleep mode */
  260. release_bus(g);
  261. break;
  262. default:
  263. return;
  264. }
  265. g->g.Powermode = (powermode_t)g->p.ptr;
  266. return;
  267. case GDISP_CONTROL_ORIENTATION:
  268. if (g->g.Orientation == (orientation_t)g->p.ptr)
  269. return;
  270. switch((orientation_t)g->p.ptr) {
  271. case GDISP_ROTATE_0:
  272. acquire_bus(g);
  273. write_reg(g, 0x36, 0x48); /* X and Y axes non-inverted */
  274. release_bus(g);
  275. g->g.Height = GDISP_SCREEN_HEIGHT;
  276. g->g.Width = GDISP_SCREEN_WIDTH;
  277. break;
  278. case GDISP_ROTATE_90:
  279. acquire_bus(g);
  280. write_reg(g, 0x36, 0xE8); /* Invert X and Y axes */
  281. release_bus(g);
  282. g->g.Height = GDISP_SCREEN_WIDTH;
  283. g->g.Width = GDISP_SCREEN_HEIGHT;
  284. break;
  285. case GDISP_ROTATE_180:
  286. acquire_bus(g);
  287. write_reg(g, 0x36, 0x88); /* X and Y axes non-inverted */
  288. release_bus(g);
  289. g->g.Height = GDISP_SCREEN_HEIGHT;
  290. g->g.Width = GDISP_SCREEN_WIDTH;
  291. break;
  292. case GDISP_ROTATE_270:
  293. acquire_bus(g);
  294. write_reg(g, 0x36, 0x28); /* Invert X and Y axes */
  295. release_bus(g);
  296. g->g.Height = GDISP_SCREEN_WIDTH;
  297. g->g.Width = GDISP_SCREEN_HEIGHT;
  298. break;
  299. default:
  300. return;
  301. }
  302. g->g.Orientation = (orientation_t)g->p.ptr;
  303. return;
  304. case GDISP_CONTROL_BACKLIGHT:
  305. if ((unsigned)g->p.ptr > 100)
  306. g->p.ptr = (void *)100;
  307. set_backlight(g, (unsigned)g->p.ptr);
  308. g->g.Backlight = (unsigned)g->p.ptr;
  309. return;
  310. //case GDISP_CONTROL_CONTRAST:
  311. default:
  312. return;
  313. }
  314. }
  315. #endif
  316. #endif /* GFX_USE_GDISP */