µGFX library fork

gdisp_lld_ILI9320.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. /* This controller is only ever used with a 240 x 320 display */
  10. #if defined(GDISP_SCREEN_HEIGHT) || defined(GDISP_SCREEN_HEIGHT)
  11. #if GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_DIRECT
  12. #warning "GDISP: This low level driver does not support setting a screen size. It is being ignored."
  13. #elif GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_MACRO
  14. COMPILER_WARNING("GDISP: This low level driver does not support setting a screen size. It is being ignored.")
  15. #endif
  16. #undef GDISP_SCREEN_WIDTH
  17. #undef GDISP_SCREEN_HEIGHT
  18. #endif
  19. #define GDISP_DRIVER_VMT GDISPVMT_ILI9320
  20. #include "gdisp_lld_config.h"
  21. #include "../../../src/gdisp/gdisp_driver.h"
  22. #include "board_ILI9320.h"
  23. /*===========================================================================*/
  24. /* Driver local definitions. */
  25. /*===========================================================================*/
  26. #ifndef GDISP_SCREEN_HEIGHT
  27. #define GDISP_SCREEN_HEIGHT 320
  28. #endif
  29. #ifndef GDISP_SCREEN_WIDTH
  30. #define GDISP_SCREEN_WIDTH 240
  31. #endif
  32. #ifndef GDISP_INITIAL_CONTRAST
  33. #define GDISP_INITIAL_CONTRAST 50
  34. #endif
  35. #ifndef GDISP_INITIAL_BACKLIGHT
  36. #define GDISP_INITIAL_BACKLIGHT 100
  37. #endif
  38. /*===========================================================================*/
  39. /* Driver local variables. */
  40. /*===========================================================================*/
  41. /*===========================================================================*/
  42. /* Driver local functions. */
  43. /*===========================================================================*/
  44. #define dummy_read(g) { volatile uint16_t dummy; dummy = read_data(g); (void) dummy; }
  45. #define write_reg(g, reg, data) { write_index(g, reg); write_data(g, data); }
  46. static void set_cursor(GDisplay *g) {
  47. switch(g->g.Orientation) {
  48. default:
  49. case GDISP_ROTATE_0:
  50. case GDISP_ROTATE_180:
  51. write_reg(g, 0x20, g->p.x);
  52. write_reg(g, 0x21, g->p.y);
  53. break;
  54. case GDISP_ROTATE_90:
  55. case GDISP_ROTATE_270:
  56. write_reg(g, 0x20, g->p.y);
  57. write_reg(g, 0x21, g->p.x);
  58. break;
  59. }
  60. write_index(g, 0x22);
  61. }
  62. static void set_viewport(GDisplay *g) {
  63. switch(g->g.Orientation) {
  64. default:
  65. case GDISP_ROTATE_0:
  66. case GDISP_ROTATE_180:
  67. write_reg(g, 0x50, g->p.x);
  68. write_reg(g, 0x51, g->p.x + g->p.cx - 1);
  69. write_reg(g, 0x52, g->p.y);
  70. write_reg(g, 0x53, g->p.y + g->p.cy - 1);
  71. break;
  72. case GDISP_ROTATE_90:
  73. case GDISP_ROTATE_270:
  74. write_reg(g, 0x50, g->p.y);
  75. write_reg(g, 0x51, g->p.y + g->p.cy - 1);
  76. write_reg(g, 0x52, g->p.x);
  77. write_reg(g, 0x53, g->p.x + g->p.cx - 1);
  78. break;
  79. }
  80. }
  81. LLDSPEC bool_t gdisp_lld_init(GDisplay *g) {
  82. uint16_t cver;
  83. // No private area for this controller
  84. g->priv = 0;
  85. // Initialise the board interface
  86. init_board(g);
  87. /* Hardware reset */
  88. setpin_reset(g, TRUE);
  89. gfxSleepMicroseconds(1000);
  90. setpin_reset(g, FALSE);
  91. gfxSleepMicroseconds(1000);
  92. acquire_bus(g);
  93. write_index(g, 0); // Get controller version
  94. setreadmode(g);
  95. dummy_read(g);
  96. cver = read_data(g);
  97. setwritemode(g);
  98. write_reg(g, 0x00, 0x0001); //start Int. osc
  99. gfxSleepMicroseconds(500);
  100. write_reg(g, 0x01, 0x0100); //Set SS bit (shift direction of outputs is from S720 to S1)
  101. write_reg(g, 0x02, 0x0700); //select the line inversion
  102. write_reg(g, 0x03, 0x1038); //Entry mode(Horizontal : increment,Vertical : increment, AM=1)
  103. write_reg(g, 0x04, 0x0000); //Resize control(No resizing)
  104. write_reg(g, 0x08, 0x0202); //front and back porch 2 lines
  105. write_reg(g, 0x09, 0x0000); //select normal scan
  106. write_reg(g, 0x0A, 0x0000); //display control 4
  107. write_reg(g, 0x0C, 0x0000); //system interface(2 transfer /pixel), internal sys clock,
  108. write_reg(g, 0x0D, 0x0000); //Frame marker position
  109. write_reg(g, 0x0F, 0x0000); //selects clk, enable and sync signal polarity,
  110. write_reg(g, 0x10, 0x0000); //
  111. write_reg(g, 0x11, 0x0000); //power control 2 reference voltages = 1:1,
  112. write_reg(g, 0x12, 0x0000); //power control 3 VRH
  113. write_reg(g, 0x13, 0x0000); //power control 4 VCOM amplitude
  114. gfxSleepMicroseconds(500);
  115. write_reg(g, 0x10, 0x17B0); //power control 1 BT,AP
  116. write_reg(g, 0x11, 0x0137); //power control 2 DC,VC
  117. gfxSleepMicroseconds(500);
  118. write_reg(g, 0x12, 0x0139); //power control 3 VRH
  119. gfxSleepMicroseconds(500);
  120. write_reg(g, 0x13, 0x1d00); //power control 4 vcom amplitude
  121. write_reg(g, 0x29, 0x0011); //power control 7 VCOMH
  122. gfxSleepMicroseconds(500);
  123. write_reg(g, 0x30, 0x0007);
  124. write_reg(g, 0x31, 0x0403);
  125. write_reg(g, 0x32, 0x0404);
  126. write_reg(g, 0x35, 0x0002);
  127. write_reg(g, 0x36, 0x0707);
  128. write_reg(g, 0x37, 0x0606);
  129. write_reg(g, 0x38, 0x0106);
  130. write_reg(g, 0x39, 0x0007);
  131. write_reg(g, 0x3c, 0x0700);
  132. write_reg(g, 0x3d, 0x0707);
  133. write_reg(g, 0x20, 0x0000); //starting Horizontal GRAM Address
  134. write_reg(g, 0x21, 0x0000); //starting Vertical GRAM Address
  135. write_reg(g, 0x50, 0x0000); //Horizontal GRAM Start Position
  136. write_reg(g, 0x51, 0x00EF); //Horizontal GRAM end Position
  137. write_reg(g, 0x52, 0x0000); //Vertical GRAM Start Position
  138. write_reg(g, 0x53, 0x013F); //Vertical GRAM end Position
  139. switch (cver) {
  140. case 0x9320:
  141. write_reg(g, 0x60, 0x2700); //starts scanning from G1, and 320 drive lines
  142. break;
  143. case 0x9325:
  144. write_reg(g, 0x60, 0xA700); //starts scanning from G1, and 320 drive lines
  145. break;
  146. }
  147. write_reg(g, 0x61, 0x0001); //fixed base display
  148. write_reg(g, 0x6a, 0x0000); //no scroll
  149. write_reg(g, 0x90, 0x0010); //set Clocks/Line =16, Internal Operation Clock Frequency=fosc/1,
  150. write_reg(g, 0x92, 0x0000); //set gate output non-overlap period=0
  151. write_reg(g, 0x93, 0x0003); //set Source Output Position=3
  152. write_reg(g, 0x95, 0x0110); //RGB interface(Clocks per line period=16 clocks)
  153. write_reg(g, 0x97, 0x0110); //set Gate Non-overlap Period 0 locksc
  154. write_reg(g, 0x98, 0x0110); //
  155. write_reg(g, 0x07, 0x0173); //display On
  156. // Finish Init
  157. post_init_board(g);
  158. // Release the bus
  159. release_bus(g);
  160. // Turn on the backlight
  161. set_backlight(g, GDISP_INITIAL_BACKLIGHT);
  162. /* Initialise the GDISP structure */
  163. g->g.Width = GDISP_SCREEN_WIDTH;
  164. g->g.Height = GDISP_SCREEN_HEIGHT;
  165. g->g.Orientation = GDISP_ROTATE_0;
  166. g->g.Powermode = powerOn;
  167. g->g.Backlight = GDISP_INITIAL_BACKLIGHT;
  168. g->g.Contrast = GDISP_INITIAL_CONTRAST;
  169. return TRUE;
  170. }
  171. #if GDISP_HARDWARE_STREAM_WRITE
  172. LLDSPEC void gdisp_lld_write_start(GDisplay *g) {
  173. acquire_bus(g);
  174. set_viewport(g);
  175. #if !GDISP_HARDWARE_STREAM_POS
  176. set_cursor(g);
  177. #endif
  178. }
  179. LLDSPEC void gdisp_lld_write_color(GDisplay *g) {
  180. write_data(g, gdispColor2Native(g->p.color));
  181. }
  182. LLDSPEC void gdisp_lld_write_stop(GDisplay *g) {
  183. release_bus(g);
  184. }
  185. #if GDISP_HARDWARE_STREAM_POS
  186. LLDSPEC void gdisp_lld_write_pos(GDisplay *g) {
  187. set_cursor(g);
  188. }
  189. #endif
  190. #endif
  191. #if GDISP_HARDWARE_STREAM_READ
  192. LLDSPEC void gdisp_lld_read_start(GDisplay *g) {
  193. acquire_bus(g);
  194. set_viewport(g);
  195. set_cursor(g);
  196. setreadmode(g);
  197. dummy_read(g);
  198. }
  199. LLDSPEC color_t gdisp_lld_read_color(GDisplay *g) {
  200. uint16_t data;
  201. data = read_data(g);
  202. return gdispNative2Color(data);
  203. }
  204. LLDSPEC void gdisp_lld_read_stop(GDisplay *g) {
  205. setwritemode(g);
  206. release_bus(g);
  207. }
  208. #endif
  209. #if GDISP_NEED_CONTROL && GDISP_HARDWARE_CONTROL
  210. LLDSPEC void gdisp_lld_control(GDisplay *g) {
  211. switch(g->p.x) {
  212. case GDISP_CONTROL_POWER:
  213. if (g->g.Powermode == (powermode_t)g->p.ptr)
  214. return;
  215. switch((powermode_t)g->p.ptr) {
  216. case powerOff:
  217. acquire_bus(g);
  218. write_reg(g, 0x07, 0x0000);
  219. write_reg(g, 0x10, 0x0000);
  220. write_reg(g, 0x11, 0x0000);
  221. write_reg(g, 0x12, 0x0000);
  222. write_reg(g, 0x13, 0x0000);
  223. release_bus(g);
  224. set_backlight(g, 0);
  225. break;
  226. case powerOn:
  227. //*************Power On sequence ******************//
  228. acquire_bus(g);
  229. write_reg(g, 0x10, 0x0000); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  230. write_reg(g, 0x11, 0x0000); /* DC1[2:0], DC0[2:0], VC[2:0] */
  231. write_reg(g, 0x12, 0x0000); /* VREG1OUT voltage */
  232. write_reg(g, 0x13, 0x0000); /* VDV[4:0] for VCOM amplitude */
  233. gfxSleepMicroseconds(2000); /* Dis-charge capacitor power voltage */
  234. write_reg(g, 0x10, 0x17B0); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  235. write_reg(g, 0x11, 0x0147); /* DC1[2:0], DC0[2:0], VC[2:0] */
  236. gfxSleepMicroseconds(500);
  237. write_reg(g, 0x12, 0x013C); /* VREG1OUT voltage */
  238. gfxSleepMicroseconds(500);
  239. write_reg(g, 0x13, 0x0E00); /* VDV[4:0] for VCOM amplitude */
  240. write_reg(g, 0x29, 0x0009); /* VCM[4:0] for VCOMH */
  241. gfxSleepMicroseconds(500);
  242. write_reg(g, 0x07, 0x0173); /* 262K color and display ON */
  243. release_bus(g);
  244. set_backlight(g, g->g.Backlight);
  245. break;
  246. case powerSleep:
  247. acquire_bus(g);
  248. write_reg(g, 0x07, 0x0000); /* display OFF */
  249. write_reg(g, 0x10, 0x0000); /* SAP, BT[3:0], APE, AP, DSTB, SLP */
  250. write_reg(g, 0x11, 0x0000); /* DC1[2:0], DC0[2:0], VC[2:0] */
  251. write_reg(g, 0x12, 0x0000); /* VREG1OUT voltage */
  252. write_reg(g, 0x13, 0x0000); /* VDV[4:0] for VCOM amplitude */
  253. gfxSleepMicroseconds(2000); /* Dis-charge capacitor power voltage */
  254. write_reg(g, 0x10, 0x0002); /* SAP, BT[3:0], APE, AP, DSTB, SLP */
  255. release_bus(g);
  256. set_backlight(g, 0);
  257. break;
  258. case powerDeepSleep:
  259. acquire_bus(g);
  260. write_reg(g, 0x07, 0x0000); /* display OFF */
  261. write_reg(g, 0x10, 0x0000); /* SAP, BT[3:0], APE, AP, DSTB, SLP */
  262. write_reg(g, 0x11, 0x0000); /* DC1[2:0], DC0[2:0], VC[2:0] */
  263. write_reg(g, 0x12, 0x0000); /* VREG1OUT voltage */
  264. write_reg(g, 0x13, 0x0000); /* VDV[4:0] for VCOM amplitude */
  265. gfxSleepMicroseconds(2000); /* Dis-charge capacitor power voltage */
  266. write_reg(g, 0x10, 0x0004); /* SAP, BT[3:0], APE, AP, DSTB, SLP */
  267. release_bus(g);
  268. set_backlight(g, 0);
  269. break;
  270. default:
  271. return;
  272. }
  273. g->g.Powermode = (powermode_t)g->p.ptr;
  274. return;
  275. case GDISP_CONTROL_ORIENTATION:
  276. if (g->g.Orientation == (orientation_t)g->p.ptr)
  277. return;
  278. switch((orientation_t)g->p.ptr) {
  279. case GDISP_ROTATE_0:
  280. acquire_bus(g);
  281. write_reg(g, 0x01, 0x0100);
  282. write_reg(g, 0x03, 0x1038);
  283. write_reg(g, 0x60, 0x2700);
  284. release_bus(g);
  285. g->g.Height = GDISP_SCREEN_HEIGHT;
  286. g->g.Width = GDISP_SCREEN_WIDTH;
  287. break;
  288. case GDISP_ROTATE_90:
  289. acquire_bus(g);
  290. write_reg(g, 0x01, 0x0000);
  291. write_reg(g, 0x03, 0x1030);
  292. write_reg(g, 0x60, 0x2700);
  293. release_bus(g);
  294. g->g.Height = GDISP_SCREEN_WIDTH;
  295. g->g.Width = GDISP_SCREEN_HEIGHT;
  296. break;
  297. case GDISP_ROTATE_180:
  298. acquire_bus(g);
  299. write_reg(g, 0x01, 0x0000);
  300. write_reg(g, 0x03, 0x1030);
  301. write_reg(g, 0x60, 0x2700);
  302. release_bus(g);
  303. g->g.Height = GDISP_SCREEN_HEIGHT;
  304. g->g.Width = GDISP_SCREEN_WIDTH;
  305. break;
  306. case GDISP_ROTATE_270:
  307. acquire_bus(g);
  308. write_reg(g, 0x01, 0x0100);
  309. write_reg(g, 0x03, 0x1038);
  310. write_reg(g, 0x60, 0xA700);
  311. release_bus(g);
  312. g->g.Height = GDISP_SCREEN_WIDTH;
  313. g->g.Width = GDISP_SCREEN_HEIGHT;
  314. break;
  315. default:
  316. return;
  317. }
  318. g->g.Orientation = (orientation_t)g->p.ptr;
  319. return;
  320. case GDISP_CONTROL_BACKLIGHT:
  321. if ((unsigned)g->p.ptr > 100)
  322. g->p.ptr = (void *)100;
  323. set_backlight(g, (unsigned)g->p.ptr);
  324. g->g.Backlight = (unsigned)g->p.ptr;
  325. return;
  326. default:
  327. return;
  328. }
  329. }
  330. #endif
  331. #endif /* GFX_USE_GDISP */