µGFX library fork

gdisp_lld_ILI9342.c 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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_ILI9342
  19. #include "gdisp_lld_config.h"
  20. #include "../../../src/gdisp/gdisp_driver.h"
  21. #include "board_ILI9342.h"
  22. /*===========================================================================*/
  23. /* Driver local definitions. */
  24. /*===========================================================================*/
  25. #ifndef GDISP_SCREEN_HEIGHT
  26. #define GDISP_SCREEN_HEIGHT 240
  27. #endif
  28. #ifndef GDISP_SCREEN_WIDTH
  29. #define GDISP_SCREEN_WIDTH 320
  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 "drivers/gdisp/ILI9342/ILI9342.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. static void set_viewport(GDisplay *g) {
  45. write_index(g, 0x2A);
  46. write_data(g, (g->p.x >> 8));
  47. write_data(g, (uint8_t) g->p.x);
  48. write_data(g, (g->p.x + g->p.cx - 1) >> 8);
  49. write_data(g, (uint8_t) (g->p.x + g->p.cx - 1));
  50. write_index(g, 0x2B);
  51. write_data(g, (g->p.y >> 8));
  52. write_data(g, (uint8_t) g->p.y);
  53. write_data(g, (g->p.y + g->p.cy - 1) >> 8);
  54. write_data(g, (uint8_t) (g->p.y + g->p.cy - 1));
  55. }
  56. /*===========================================================================*/
  57. /* Driver exported functions. */
  58. /*===========================================================================*/
  59. LLDSPEC bool_t gdisp_lld_init(GDisplay *g) {
  60. // No private area for this controller
  61. g->priv = 0;
  62. // Initialise the board interface
  63. init_board(g);
  64. // Hardware reset
  65. setpin_reset(g, TRUE);
  66. gfxSleepMilliseconds(20);
  67. setpin_reset(g, FALSE);
  68. gfxSleepMilliseconds(20);
  69. // Get the bus for the following initialisation commands
  70. acquire_bus(g);
  71. // Software reset
  72. write_index(g, 0x01);
  73. gfxSleepMilliseconds(5);
  74. write_index(g, 0x28);
  75. // display off
  76. //---------------------------------------------------------
  77. // magic?
  78. write_index(g, 0xcf);
  79. write_data(g, 0x00);
  80. write_data(g, 0x83);
  81. write_data(g, 0x30);
  82. write_index(g, 0xed);
  83. write_data(g, 0x64);
  84. write_data(g, 0x03);
  85. write_data(g, 0x12);
  86. write_data(g, 0x81);
  87. write_index(g, 0xe8);
  88. write_data(g, 0x85);
  89. write_data(g, 0x01);
  90. write_data(g, 0x79);
  91. write_index(g, 0xcb);
  92. write_data(g, 0x39);
  93. write_data(g, 0x2c);
  94. write_data(g, 0x00);
  95. write_data(g, 0x34);
  96. write_data(g, 0x02);
  97. write_index(g, 0xf7);
  98. write_data(g, 0x20);
  99. write_index(g, 0xea);
  100. write_data(g, 0x00);
  101. write_data(g, 0x00);
  102. //------------power control------------------------------
  103. write_index(g, 0xc0); //power control
  104. write_data(g, 0x26);
  105. write_index(g, 0xc1); //power control
  106. write_data(g, 0x11);
  107. //--------------VCOM
  108. write_index(g, 0xc5); //vcom control
  109. write_data(g, 0x35);//35
  110. write_data(g, 0x3e);//3E
  111. write_index(g, 0xc7); //vcom control
  112. write_data(g, 0xbe); // 0x94
  113. //------------memory access control------------------------
  114. write_index(g, 0x36);
  115. write_data(g, 0x08); // scan direction (aka orientation and mirroring)
  116. write_index(g, 0x3a); // pixel format set
  117. write_data(g, 0x55);//16bit /pixel
  118. //----------------- frame rate------------------------------
  119. write_index(g, 0xb1);
  120. // frame rate
  121. write_data(g, 0x00);
  122. write_data(g, 0x1B); //70
  123. //----------------Gamma---------------------------------
  124. write_index(g, 0xf2); // 3Gamma Function Disable
  125. write_data(g, 0x08);
  126. write_index(g, 0x26);
  127. write_data(g, 0x01); // gamma set 4 gamma curve 01/02/04/08
  128. write_index(g, 0xE0); //positive gamma correction
  129. write_data(g, 0x1f);
  130. write_data(g, 0x1a);
  131. write_data(g, 0x18);
  132. write_data(g, 0x0a);
  133. write_data(g, 0x0f);
  134. write_data(g, 0x06);
  135. write_data(g, 0x45);
  136. write_data(g, 0x87);
  137. write_data(g, 0x32);
  138. write_data(g, 0x0a);
  139. write_data(g, 0x07);
  140. write_data(g, 0x02);
  141. write_data(g, 0x07);
  142. write_data(g, 0x05);
  143. write_data(g, 0x00);
  144. write_index(g, 0xE1); //negamma correction
  145. write_data(g, 0x00);
  146. write_data(g, 0x25);
  147. write_data(g, 0x27);
  148. write_data(g, 0x05);
  149. write_data(g, 0x10);
  150. write_data(g, 0x09);
  151. write_data(g, 0x3a);
  152. write_data(g, 0x78);
  153. write_data(g, 0x4d);
  154. write_data(g, 0x05);
  155. write_data(g, 0x18);
  156. write_data(g, 0x0d);
  157. write_data(g, 0x38);
  158. write_data(g, 0x3a);
  159. write_data(g, 0x1f);
  160. //--------------ddram ---------------------
  161. write_index(g, 0x2a);
  162. // column set
  163. // size = 239
  164. write_data(g, 0x00);
  165. write_data(g, 0x00);
  166. write_data(g, 0x00);
  167. write_data(g, 0xEF);
  168. write_index(g, 0x2b);
  169. // page address set
  170. // size = 319
  171. write_data(g, 0x00);
  172. write_data(g, 0x00);
  173. write_data(g, 0x01);
  174. write_data(g, 0x3F);
  175. // write_index(g, 0x34);
  176. //write_index(g, 0x35);
  177. // tearing effect off
  178. // tearing effect on
  179. // write_index(g, 0xb4); // display inversion
  180. // write_data(g, 0x00);
  181. write_index(g, 0xb7); //entry mode set
  182. write_data(g, 0x07);
  183. //-----------------display---------------------
  184. write_index(g, 0xb6);
  185. // display function control
  186. write_data(g, 0x0a);
  187. write_data(g, 0x82);
  188. write_data(g, 0x27);
  189. write_data(g, 0x00);
  190. write_index(g, 0x11); //sleep out
  191. gfxSleepMilliseconds(100);
  192. write_index(g, 0x29); // display on
  193. gfxSleepMilliseconds(100);
  194. // Finish Init
  195. post_init_board(g);
  196. // Release the bus
  197. release_bus(g);
  198. /* Turn on the back-light */
  199. set_backlight(g, GDISP_INITIAL_BACKLIGHT);
  200. /* Initialise the GDISP structure */
  201. g->g.Width = GDISP_SCREEN_WIDTH;
  202. g->g.Height = GDISP_SCREEN_HEIGHT;
  203. g->g.Orientation = GDISP_ROTATE_0;
  204. g->g.Powermode = powerOn;
  205. g->g.Backlight = GDISP_INITIAL_BACKLIGHT;
  206. g->g.Contrast = GDISP_INITIAL_CONTRAST;
  207. return TRUE;
  208. }
  209. #if GDISP_HARDWARE_STREAM_WRITE
  210. LLDSPEC void gdisp_lld_write_start(GDisplay *g) {
  211. acquire_bus(g);
  212. set_viewport(g);
  213. write_index(g, 0x2C);
  214. }
  215. LLDSPEC void gdisp_lld_write_color(GDisplay *g) {
  216. write_pixel(g, gdispColor2Native(g->p.color));
  217. }
  218. LLDSPEC void gdisp_lld_write_stop(GDisplay *g) {
  219. release_bus(g);
  220. }
  221. #endif
  222. #if GDISP_HARDWARE_STREAM_READ
  223. LLDSPEC void gdisp_lld_read_start(GDisplay *g) {
  224. acquire_bus(g);
  225. set_viewport(g);
  226. write_index(g, 0x2E);
  227. setreadmode(g);
  228. dummy_read(g);
  229. }
  230. LLDSPEC color_t gdisp_lld_read_color(GDisplay *g) {
  231. uint16_t data;
  232. data = read_data(g);
  233. return gdispNative2Color(data);
  234. }
  235. LLDSPEC void gdisp_lld_read_stop(GDisplay *g) {
  236. setwritemode(g);
  237. release_bus(g);
  238. }
  239. #endif
  240. #if GDISP_NEED_CONTROL && GDISP_HARDWARE_CONTROL
  241. LLDSPEC void gdisp_lld_control(GDisplay *g) {
  242. switch(g->p.x) {
  243. case GDISP_CONTROL_POWER:
  244. if (g->g.Powermode == (powermode_t)g->p.ptr)
  245. return;
  246. switch((powermode_t)g->p.ptr) {
  247. case powerOff:
  248. case powerSleep:
  249. case powerDeepSleep:
  250. acquire_bus(g);
  251. write_reg(g, 0x0010, 0x0001); /* enter sleep mode */
  252. release_bus(g);
  253. break;
  254. case powerOn:
  255. acquire_bus(g);
  256. write_reg(g, 0x0010, 0x0000); /* leave sleep mode */
  257. release_bus(g);
  258. break;
  259. default:
  260. return;
  261. }
  262. g->g.Powermode = (powermode_t)g->p.ptr;
  263. return;
  264. case GDISP_CONTROL_ORIENTATION:
  265. if (g->g.Orientation == (orientation_t)g->p.ptr)
  266. return;
  267. switch((orientation_t)g->p.ptr) {
  268. case GDISP_ROTATE_0:
  269. acquire_bus(g);
  270. write_reg(g, 0x36, 0x08);
  271. release_bus(g);
  272. g->g.Height = GDISP_SCREEN_HEIGHT;
  273. g->g.Width = GDISP_SCREEN_WIDTH;
  274. break;
  275. case GDISP_ROTATE_90:
  276. acquire_bus(g);
  277. write_reg(g, 0x36, 0x68);
  278. release_bus(g);
  279. g->g.Height = GDISP_SCREEN_WIDTH;
  280. g->g.Width = GDISP_SCREEN_HEIGHT;
  281. break;
  282. case GDISP_ROTATE_180:
  283. acquire_bus(g);
  284. write_reg(g, 0x36, 0xC8);
  285. release_bus(g);
  286. g->g.Height = GDISP_SCREEN_HEIGHT;
  287. g->g.Width = GDISP_SCREEN_WIDTH;
  288. break;
  289. case GDISP_ROTATE_270:
  290. acquire_bus(g);
  291. write_reg(g, 0x36, 0xA8);
  292. release_bus(g);
  293. g->g.Height = GDISP_SCREEN_WIDTH;
  294. g->g.Width = GDISP_SCREEN_HEIGHT;
  295. break;
  296. default:
  297. return;
  298. }
  299. g->g.Orientation = (orientation_t)g->p.ptr;
  300. return;
  301. case GDISP_CONTROL_BACKLIGHT:
  302. if ((unsigned)g->p.ptr > 100)
  303. g->p.ptr = (void *)100;
  304. set_backlight(g, (unsigned)g->p.ptr);
  305. g->g.Backlight = (unsigned)g->p.ptr;
  306. return;
  307. //case GDISP_CONTROL_CONTRAST:
  308. default:
  309. return;
  310. }
  311. }
  312. #endif
  313. #endif /* GFX_USE_GDISP */