µGFX library fork

gdisp_lld_ILI9481.c 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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_ILI9481
  19. #include "gdisp_lld_config.h"
  20. #include "../../../src/gdisp/gdisp_driver.h"
  21. #include "board_ILI9481.h"
  22. /*===========================================================================*/
  23. /* Driver local definitions. */
  24. /*===========================================================================*/
  25. #ifndef GDISP_SCREEN_HEIGHT
  26. #define GDISP_SCREEN_HEIGHT 480
  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. /*===========================================================================*/
  38. /* Driver local functions. */
  39. /*===========================================================================*/
  40. // Some common routines and macros
  41. #define dummy_read(g) { volatile uint16_t dummy; dummy = read_data(g); (void) dummy; }
  42. #define write_reg(g, reg, data) { write_index(g, reg); write_data(g, data); }
  43. #define write_reg2x16(g, reg, data1, data2) { write_index(g, reg); write_data(g, (data1)>>8); write_data(g, (uint8_t)(data1)); write_data(g, (data2)>>8); write_data(g, (uint8_t)(data2));}
  44. static void set_viewport(GDisplay* g) {
  45. write_reg2x16(g, 0x2A, g->p.x, g->p.x + g->p.cx - 1);
  46. write_reg2x16(g, 0x2B, g->p.y, g->p.y + g->p.cy - 1);
  47. write_index(g, 0x2C);
  48. }
  49. /*===========================================================================*/
  50. /* Driver interrupt handlers. */
  51. /*===========================================================================*/
  52. /*===========================================================================*/
  53. /* Driver exported functions. */
  54. /*===========================================================================*/
  55. LLDSPEC bool_t gdisp_lld_init(GDisplay *g) {
  56. // No private area for this controller
  57. g->priv = 0;
  58. // Initialise the board interface
  59. init_board(g);
  60. /* Hardware reset */
  61. setpin_reset(g, TRUE);
  62. gfxSleepMilliseconds(20);
  63. setpin_reset(g, FALSE);
  64. gfxSleepMilliseconds(20);
  65. /* Get the bus for the following initialisation commands */
  66. acquire_bus(g);
  67. /* Enable Access to all Manufacturer commands (0xB0 and higher opcodes) */
  68. write_reg(g, 0xB0, 0x00);
  69. /* Frame Memory Access and Interface Setting */
  70. write_index(g, 0xB3);
  71. write_data(g, 0x02);
  72. write_data(g, 0x00);
  73. write_data(g, 0x00);
  74. write_data(g, 0x10);
  75. /* Display Mode and Frame Memory Write Mode Setting (B4h) */
  76. /* Use internal clock for synchronization */
  77. /* Use DBI interface (only DB0-17, no HSYNC, VSYNC or CLK) */
  78. write_reg(g, 0xB4, 0x00);
  79. /* Internal Backlight Control */
  80. /* write_index(g, 0xB9); // PWM Settings for Brightness Control
  81. write_data(g, 0x01); // Disabled by default.
  82. write_data(g, 0xFF); // 0xFF = Max brightness
  83. write_data(g, 0xFF);
  84. write_data(g, 0x18); */
  85. /* Panel Driving settings */
  86. write_index(g, 0xC0);
  87. write_data(g, 0x03);
  88. write_data(g, 0x3B);
  89. write_data(g, 0x00);
  90. write_data(g, 0x00);
  91. write_data(g, 0x00);
  92. write_data(g, 0x01);
  93. write_data(g, 0x00); /* NW */
  94. write_data(g, 0x43);
  95. /* Display timings in Operating Mode */
  96. write_index(g, 0xC1);
  97. write_data(g, 0x08);
  98. write_data(g, 0x15); /* CLOCK */
  99. write_data(g, 0x08);
  100. write_data(g, 0x08);
  101. /* S/VCOM/Gate Driving timing setting */
  102. write_index(g, 0xC4);
  103. write_data(g, 0x15);
  104. write_data(g, 0x03);
  105. write_data(g, 0x03);
  106. write_data(g, 0x01);
  107. /* Interface Setting */
  108. write_reg(g, 0xC6, 0x02);
  109. /* Gamma Setting - should be changed if using a different panel */
  110. write_index(g, 0xC8);
  111. write_data(g, 0x0C);
  112. write_data(g, 0x05);
  113. write_data(g, 0x0A); /*0X12 */
  114. write_data(g, 0x6B); /*0x7D */
  115. write_data(g, 0x04);
  116. write_data(g, 0x06); /*0x08 */
  117. write_data(g, 0x15); /*0x0A */
  118. write_data(g, 0x10);
  119. write_data(g, 0x00);
  120. write_data(g, 0x60); /*0x23 */
  121. /* Address Mode setting */
  122. write_reg(g, 0x36, 0x00);
  123. /* Set Pixel Format = 16 bits per pixel */
  124. /* The driver supports upto 24 bits per pixel, with dither */
  125. write_reg(g, 0x3A, 0x55);
  126. /* Exit Idle Mode */
  127. write_index(g, 0x38);
  128. /* Power Setting */
  129. write_index(g, 0xD0);
  130. write_data(g, 0x07);
  131. write_data(g, 0x07); /* VCI = VCI1 */
  132. write_data(g, 0x14); /* VRH 0x1D */
  133. write_data(g, 0xA2); /* BT 0x06 */
  134. /* VCOM Setting */
  135. write_index(g, 0xD1);
  136. write_data(g, 0x03);
  137. write_data(g, 0x5A); /* VCM 0x5A */
  138. write_data(g, 0x10); /* VDV */
  139. /* Power Setting for Normal Mode */
  140. write_index(g, 0xD2);
  141. write_data(g, 0x03);
  142. write_data(g, 0x04); /* 0x24 */
  143. write_data(g, 0x04);
  144. /* Exit Sleep Mode */
  145. write_index(g, 0x11);
  146. gfxSleepMilliseconds(150);
  147. /* Display ON */
  148. write_index(g, 0x29);
  149. gfxSleepMilliseconds(30);
  150. // Finish Init
  151. post_init_board(g);
  152. // Release the bus
  153. release_bus(g);
  154. /* Turn on the back-light */
  155. set_backlight(g, GDISP_INITIAL_BACKLIGHT);
  156. /* Initialise the GDISP structure */
  157. g->g.Width = GDISP_SCREEN_WIDTH;
  158. g->g.Height = GDISP_SCREEN_HEIGHT;
  159. g->g.Orientation = GDISP_ROTATE_0;
  160. g->g.Powermode = powerOn;
  161. g->g.Backlight = GDISP_INITIAL_BACKLIGHT;
  162. g->g.Contrast = GDISP_INITIAL_CONTRAST;
  163. return TRUE;
  164. }
  165. #if GDISP_HARDWARE_STREAM_WRITE
  166. LLDSPEC void gdisp_lld_write_start(GDisplay *g) {
  167. acquire_bus(g);
  168. set_viewport(g);
  169. }
  170. LLDSPEC void gdisp_lld_write_color(GDisplay *g) {
  171. write_data(g, gdispColor2Native(g->p.color));
  172. }
  173. LLDSPEC void gdisp_lld_write_stop(GDisplay *g) {
  174. release_bus(g);
  175. }
  176. #endif
  177. #if GDISP_HARDWARE_STREAM_READ
  178. LLDSPEC void gdisp_lld_read_start(GDisplay *g) {
  179. acquire_bus(g);
  180. set_viewport(g);
  181. setreadmode(g);
  182. dummy_read(g);
  183. }
  184. LLDSPEC color_t gdisp_lld_read_color(GDisplay *g) {
  185. uint16_t data;
  186. data = read_data(g);
  187. return gdispNative2Color(data);
  188. }
  189. LLDSPEC void gdisp_lld_read_stop(GDisplay *g) {
  190. setwritemode(g);
  191. release_bus(g);
  192. }
  193. #endif
  194. #if GDISP_NEED_CONTROL && GDISP_HARDWARE_CONTROL
  195. LLDSPEC void gdisp_lld_control(GDisplay *g) {
  196. switch(g->p.x) {
  197. case GDISP_CONTROL_POWER:
  198. if (g->g.Powermode == (powermode_t)g->p.ptr)
  199. return;
  200. switch((powermode_t)g->p.ptr) {
  201. case powerOff:
  202. acquire_bus(g);
  203. write_reg(g, 0x0010, 0x0001); /* enter sleep mode */
  204. release_bus(g);
  205. break;
  206. case powerOn:
  207. acquire_bus(g);
  208. write_reg(g, 0x0010, 0x0000); /* leave sleep mode */
  209. release_bus(g);
  210. if (g->g.Powermode != powerSleep)
  211. gdisp_lld_init(g);
  212. break;
  213. case powerSleep:
  214. acquire_bus(g);
  215. write_reg(g, 0x0010, 0x0001); /* enter sleep mode */
  216. release_bus(g);
  217. break;
  218. default:
  219. return;
  220. }
  221. g->g.Powermode = (powermode_t)g->p.ptr;
  222. return;
  223. case GDISP_CONTROL_ORIENTATION:
  224. if (g->g.Orientation == (orientation_t)g->p.ptr)
  225. return;
  226. switch((orientation_t)g->p.ptr) {
  227. case GDISP_ROTATE_0:
  228. acquire_bus(g);
  229. write_reg(g, 0xC0, 0x03);
  230. write_reg(g, 0x36, 0x00); /* X and Y axes non-inverted */
  231. release_bus(g);
  232. g->g.Height = GDISP_SCREEN_HEIGHT;
  233. g->g.Width = GDISP_SCREEN_WIDTH;
  234. break;
  235. case GDISP_ROTATE_90:
  236. acquire_bus(g);
  237. write_reg(g, 0xC0, 0x02);
  238. write_reg(g, 0x36, 0x20); /* Invert X and Y axes */
  239. release_bus(g);
  240. g->g.Height = GDISP_SCREEN_WIDTH;
  241. g->g.Width = GDISP_SCREEN_HEIGHT;
  242. break;
  243. case GDISP_ROTATE_180:
  244. acquire_bus(g);
  245. write_reg(g, 0xC0, 0x06);
  246. write_reg(g, 0x36, 0x00); /* X and Y axes non-inverted */
  247. release_bus(g);
  248. g->g.Height = GDISP_SCREEN_HEIGHT;
  249. g->g.Width = GDISP_SCREEN_WIDTH;
  250. break;
  251. case GDISP_ROTATE_270:
  252. acquire_bus(g);
  253. write_reg(g, 0xC0, 0x07);
  254. write_reg(g, 0x36, 0x20); /* Invert X and Y axes */
  255. release_bus(g);
  256. g->g.Height = GDISP_SCREEN_WIDTH;
  257. g->g.Width = GDISP_SCREEN_HEIGHT;
  258. break;
  259. default:
  260. return;
  261. }
  262. g->g.Orientation = (orientation_t)g->p.ptr;
  263. return;
  264. //case GDISP_CONTROL_BACKLIGHT:
  265. //case GDISP_CONTROL_CONTRAST:
  266. default:
  267. return;
  268. }
  269. }
  270. #endif
  271. #endif /* GFX_USE_GDISP */