µGFX library fork

gdisp_lld_ILI93xx.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 /*|| defined(__DOXYGEN__)*/
  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_ILI93xx
  20. #include "gdisp_lld_config.h"
  21. #include "../../../src/gdisp/gdisp_driver.h"
  22. #include "board_ILI93xx.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. // Some common routines and macros
  45. #define dummy_read(g) { volatile uint16_t dummy; dummy = read_data(g); (void) dummy; }
  46. #define write_reg(g, reg, data) { write_index(g, reg); write_data(g, data); }
  47. static GFXINLINE uint16_t read_reg(GDisplay *g, uint32_t reg) {
  48. write_index(g, reg);
  49. return read_data(g);
  50. }
  51. static void set_cursor(GDisplay *g) {
  52. switch(g->g.Orientation) {
  53. default:
  54. case GDISP_ROTATE_0:
  55. case GDISP_ROTATE_180:
  56. write_reg(g, 0x20, g->p.x);
  57. write_reg(g, 0x21, g->p.y);
  58. break;
  59. case GDISP_ROTATE_90:
  60. case GDISP_ROTATE_270:
  61. write_reg(g, 0x20, g->p.y);
  62. write_reg(g, 0x21, g->p.x);
  63. break;
  64. }
  65. write_index(g, 0x22);
  66. }
  67. static void set_viewport(GDisplay* g) {
  68. switch(g->g.Orientation) {
  69. default:
  70. case GDISP_ROTATE_0:
  71. case GDISP_ROTATE_180:
  72. write_reg(g, 0x50, g->p.x);
  73. write_reg(g, 0x51, g->p.x + g->p.cx - 1);
  74. write_reg(g, 0x52, g->p.y);
  75. write_reg(g, 0x53, g->p.y + g->p.cy - 1);
  76. break;
  77. case GDISP_ROTATE_90:
  78. case GDISP_ROTATE_270:
  79. write_reg(g, 0x50, g->p.y);
  80. write_reg(g, 0x51, g->p.y + g->p.cy - 1);
  81. write_reg(g, 0x52, g->p.x);
  82. write_reg(g, 0x53, g->p.x + g->p.cx - 1);
  83. break;
  84. }
  85. }
  86. LLDSPEC bool_t gdisp_lld_init(GDisplay *g) {
  87. unsigned short DeviceCode;
  88. // No private area for this controller
  89. g->priv = 0;
  90. // Initialise the board interface
  91. init_board(g);
  92. /* Hardware reset */
  93. setpin_reset(g, TRUE);
  94. gfxSleepMilliseconds(1);
  95. setpin_reset(g, FALSE);
  96. gfxSleepMilliseconds(10);
  97. setpin_reset(g, TRUE);
  98. gfxSleepMilliseconds(50);
  99. acquire_bus(g);
  100. setreadmode(g);
  101. DeviceCode = read_reg(g, 0x00);
  102. setwritemode(g);
  103. if( DeviceCode == 0x9320 || DeviceCode == 0x9300 )
  104. {
  105. write_reg(g, 0x00, 0x0000);
  106. write_reg(g, 0x01, 0x0100); /* Driver Output Contral */
  107. write_reg(g, 0x02, 0x0700); /* LCD Driver Waveform Contral */
  108. write_reg(g, 0x03, 0x1038); /* Set the scan mode */
  109. write_reg(g, 0x04, 0x0000); /* Scalling Contral */
  110. write_reg(g, 0x08, 0x0202); /* Display Contral 2 */
  111. write_reg(g, 0x09, 0x0000); /* Display Contral 3 */
  112. write_reg(g, 0x0a, 0x0000); /* Frame Cycle Contal */
  113. write_reg(g, 0x0c, (1<<0)); /* Extern Display Interface Contral 1 */
  114. write_reg(g, 0x0d, 0x0000); /* Frame Maker Position */
  115. write_reg(g, 0x0f, 0x0000); /* Extern Display Interface Contral 2 */
  116. gfxSleepMilliseconds(50);
  117. write_reg(g, 0x07, 0x0101); /* Display Contral */
  118. gfxSleepMilliseconds(50);
  119. write_reg(g, 0x10, (1<<12)|(0<<8)|(1<<7)|(1<<6)|(0<<4)); /* Power Control 1 */
  120. write_reg(g, 0x11, 0x0007); /* Power Control 2 */
  121. write_reg(g, 0x12, (1<<8)|(1<<4)|(0<<0)); /* Power Control 3 */
  122. write_reg(g, 0x13, 0x0b00); /* Power Control 4 */
  123. write_reg(g, 0x29, 0x0000); /* Power Control 7 */
  124. write_reg(g, 0x2b, (1<<14)|(1<<4));
  125. write_reg(g, 0x50, 0); /* Set X Start */
  126. write_reg(g, 0x51, 239); /* Set X End */
  127. write_reg(g, 0x52, 0); /* Set Y Start */
  128. write_reg(g, 0x53, 319); /* Set Y End */
  129. gfxSleepMilliseconds(50);
  130. write_reg(g, 0x60, 0x2700); /* Driver Output Control */
  131. write_reg(g, 0x61, 0x0001); /* Driver Output Control */
  132. write_reg(g, 0x6a, 0x0000); /* Vertical Srcoll Control */
  133. write_reg(g, 0x80, 0x0000); /* Display Position? Partial Display 1 */
  134. write_reg(g, 0x81, 0x0000); /* RAM Address Start? Partial Display 1 */
  135. write_reg(g, 0x82, 0x0000); /* RAM Address End-Partial Display 1 */
  136. write_reg(g, 0x83, 0x0000); /* Displsy Position? Partial Display 2 */
  137. write_reg(g, 0x84, 0x0000); /* RAM Address Start? Partial Display 2 */
  138. write_reg(g, 0x85, 0x0000); /* RAM Address End? Partial Display 2 */
  139. write_reg(g, 0x90, (0<<7)|(16<<0)); /* Frame Cycle Contral */
  140. write_reg(g, 0x92, 0x0000); /* Panel Interface Contral 2 */
  141. write_reg(g, 0x93, 0x0001); /* Panel Interface Contral 3 */
  142. write_reg(g, 0x95, 0x0110); /* Frame Cycle Contral */
  143. write_reg(g, 0x97, (0<<8));
  144. write_reg(g, 0x98, 0x0000); /* Frame Cycle Contral */
  145. write_reg(g, 0x07, 0x0133);
  146. }
  147. else if( DeviceCode == 0x9325 || DeviceCode == 0x9328)
  148. {
  149. write_reg(g, 0x00e7, 0x0010);
  150. write_reg(g, 0x0000, 0x0001); /* start internal osc */
  151. write_reg(g, 0x0001, 0x0100);
  152. write_reg(g, 0x0002, 0x0700); /* power on sequence */
  153. write_reg(g, 0x0003, (1<<12)|(1<<5)|(1<<4)|(0<<3) ); /* importance */
  154. write_reg(g, 0x0004, 0x0000);
  155. write_reg(g, 0x0008, 0x0207);
  156. write_reg(g, 0x0009, 0x0000);
  157. write_reg(g, 0x000a, 0x0000); /* display setting */
  158. write_reg(g, 0x000c, 0x0001); /* display setting */
  159. write_reg(g, 0x000d, 0x0000);
  160. write_reg(g, 0x000f, 0x0000);
  161. /* Power On sequence */
  162. write_reg(g, 0x0010, 0x0000);
  163. write_reg(g, 0x0011, 0x0007);
  164. write_reg(g, 0x0012, 0x0000);
  165. write_reg(g, 0x0013, 0x0000);
  166. gfxSleepMilliseconds(50); /* delay 50 ms */
  167. write_reg(g, 0x0010, 0x1590);
  168. write_reg(g, 0x0011, 0x0227);
  169. gfxSleepMilliseconds(50); /* delay 50 ms */
  170. write_reg(g, 0x0012, 0x009c);
  171. gfxSleepMilliseconds(50); /* delay 50 ms */
  172. write_reg(g, 0x0013, 0x1900);
  173. write_reg(g, 0x0029, 0x0023);
  174. write_reg(g, 0x002b, 0x000e);
  175. gfxSleepMilliseconds(50); /* delay 50 ms */
  176. write_reg(g, 0x0020, 0x0000);
  177. write_reg(g, 0x0021, 0x0000);
  178. gfxSleepMilliseconds(50); /* delay 50 ms */
  179. write_reg(g, 0x0030, 0x0007);
  180. write_reg(g, 0x0031, 0x0707);
  181. write_reg(g, 0x0032, 0x0006);
  182. write_reg(g, 0x0035, 0x0704);
  183. write_reg(g, 0x0036, 0x1f04);
  184. write_reg(g, 0x0037, 0x0004);
  185. write_reg(g, 0x0038, 0x0000);
  186. write_reg(g, 0x0039, 0x0706);
  187. write_reg(g, 0x003c, 0x0701);
  188. write_reg(g, 0x003d, 0x000f);
  189. gfxSleepMilliseconds(50); /* delay 50 ms */
  190. write_reg(g, 0x0050, 0x0000);
  191. write_reg(g, 0x0051, 0x00ef);
  192. write_reg(g, 0x0052, 0x0000);
  193. write_reg(g, 0x0053, 0x013f);
  194. write_reg(g, 0x0060, 0xa700);
  195. write_reg(g, 0x0061, 0x0001);
  196. write_reg(g, 0x006a, 0x0000);
  197. write_reg(g, 0x0080, 0x0000);
  198. write_reg(g, 0x0081, 0x0000);
  199. write_reg(g, 0x0082, 0x0000);
  200. write_reg(g, 0x0083, 0x0000);
  201. write_reg(g, 0x0084, 0x0000);
  202. write_reg(g, 0x0085, 0x0000);
  203. write_reg(g, 0x0090, 0x0010);
  204. write_reg(g, 0x0092, 0x0000);
  205. write_reg(g, 0x0093, 0x0003);
  206. write_reg(g, 0x0095, 0x0110);
  207. write_reg(g, 0x0097, 0x0000);
  208. write_reg(g, 0x0098, 0x0000);
  209. /* display on sequence */
  210. write_reg(g, 0x0007, 0x0133);
  211. write_reg(g, 0x0020, 0x0000);
  212. write_reg(g, 0x0021, 0x0000);
  213. }
  214. gfxSleepMilliseconds(100); /* delay 50 ms */
  215. // Finish Init
  216. post_init_board(g);
  217. // Release the bus
  218. release_bus(g);
  219. // Turn on the backlight
  220. set_backlight(g, GDISP_INITIAL_BACKLIGHT);
  221. /* Initialise the GDISP structure */
  222. g->g.Width = GDISP_SCREEN_WIDTH;
  223. g->g.Height = GDISP_SCREEN_HEIGHT;
  224. g->g.Orientation = GDISP_ROTATE_0;
  225. g->g.Powermode = powerOn;
  226. g->g.Backlight = GDISP_INITIAL_BACKLIGHT;
  227. g->g.Contrast = GDISP_INITIAL_CONTRAST;
  228. return TRUE;
  229. }
  230. #if GDISP_HARDWARE_STREAM_WRITE
  231. LLDSPEC void gdisp_lld_write_start(GDisplay *g) {
  232. acquire_bus(g);
  233. set_viewport(g);
  234. }
  235. LLDSPEC void gdisp_lld_write_color(GDisplay *g) {
  236. write_data(g, gdispColor2Native(g->p.color));
  237. //write_data(g, COLOR2NATIVE(g->p.color));
  238. }
  239. LLDSPEC void gdisp_lld_write_stop(GDisplay *g) {
  240. release_bus(g);
  241. }
  242. LLDSPEC void gdisp_lld_write_pos(GDisplay *g) {
  243. set_cursor(g);
  244. }
  245. #endif
  246. #if GDISP_HARDWARE_STREAM_READ
  247. LLDSPEC void gdisp_lld_read_start(GDisplay *g) {
  248. acquire_bus(g);
  249. set_viewport(g);
  250. set_cursor(g);
  251. setreadmode(g);
  252. dummy_read(g);
  253. }
  254. LLDSPEC color_t gdisp_lld_read_color(GDisplay *g) {
  255. uint16_t data;
  256. data = read_data(g);
  257. return gdispNative2Color(data);
  258. }
  259. LLDSPEC void gdisp_lld_read_stop(GDisplay *g) {
  260. setwritemode(g);
  261. release_bus(g);
  262. }
  263. #endif
  264. #if GDISP_NEED_CONTROL && GDISP_HARDWARE_CONTROL
  265. LLDSPEC void gdisp_lld_control(GDisplay *g) {
  266. switch(g->p.x) {
  267. case GDISP_CONTROL_POWER:
  268. if (g->g.Powermode == (powermode_t)g->p.ptr)
  269. return;
  270. switch((powermode_t)g->p.ptr) {
  271. case powerOff:
  272. acquire_bus(g);
  273. write_reg(g, 0x07, 0x0000);
  274. write_reg(g, 0x10, 0x0000);
  275. write_reg(g, 0x11, 0x0000);
  276. write_reg(g, 0x12, 0x0000);
  277. write_reg(g, 0x13, 0x0000);
  278. release_bus(g);
  279. set_backlight(g, 0);
  280. break;
  281. case powerOn:
  282. //*************Power On sequence ******************//
  283. acquire_bus(g);
  284. write_reg(g, 0x10, 0x0000); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  285. write_reg(g, 0x11, 0x0000); /* DC1[2:0], DC0[2:0], VC[2:0] */
  286. write_reg(g, 0x12, 0x0000); /* VREG1OUT voltage */
  287. write_reg(g, 0x13, 0x0000); /* VDV[4:0] for VCOM amplitude */
  288. gfxSleepMilliseconds(200); /* Dis-charge capacitor power voltage */
  289. write_reg(g, 0x10, 0x17B0); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  290. write_reg(g, 0x11, 0x0147); /* DC1[2:0], DC0[2:0], VC[2:0] */
  291. gfxSleepMilliseconds(50);
  292. write_reg(g, 0x12, 0x013C); /* VREG1OUT voltage */
  293. gfxSleepMilliseconds(50);
  294. write_reg(g, 0x13, 0x0E00); /* VDV[4:0] for VCOM amplitude */
  295. write_reg(g, 0x29, 0x0009); /* VCM[4:0] for VCOMH */
  296. gfxSleepMilliseconds(50);
  297. write_reg(g, 0x07, 0x0173); /* 262K color and display ON */
  298. release_bus(g);
  299. set_backlight(g, g->g.Backlight);
  300. break;
  301. case powerSleep:
  302. acquire_bus(g);
  303. write_reg(g, 0x07, 0x0000); /* display OFF */
  304. write_reg(g, 0x10, 0x0000); /* SAP, BT[3:0], APE, AP, DSTB, SLP */
  305. write_reg(g, 0x11, 0x0000); /* DC1[2:0], DC0[2:0], VC[2:0] */
  306. write_reg(g, 0x12, 0x0000); /* VREG1OUT voltage */
  307. write_reg(g, 0x13, 0x0000); /* VDV[4:0] for VCOM amplitude */
  308. gfxSleepMilliseconds(200); /* Dis-charge capacitor power voltage */
  309. write_reg(g, 0x10, 0x0002); /* SAP, BT[3:0], APE, AP, DSTB, SLP */
  310. release_bus(g);
  311. set_backlight(g, g->g.Backlight);
  312. break;
  313. case powerDeepSleep:
  314. acquire_bus(g);
  315. write_reg(g, 0x07, 0x0000); /* display OFF */
  316. write_reg(g, 0x10, 0x0000); /* SAP, BT[3:0], APE, AP, DSTB, SLP */
  317. write_reg(g, 0x11, 0x0000); /* DC1[2:0], DC0[2:0], VC[2:0] */
  318. write_reg(g, 0x12, 0x0000); /* VREG1OUT voltage */
  319. write_reg(g, 0x13, 0x0000); /* VDV[4:0] for VCOM amplitude */
  320. gfxSleepMilliseconds(200); /* Dis-charge capacitor power voltage */
  321. write_reg(g, 0x10, 0x0004); /* SAP, BT[3:0], APE, AP, DSTB, SLP */
  322. release_bus(g);
  323. set_backlight(g, g->g.Backlight);
  324. break;
  325. default:
  326. return;
  327. }
  328. g->g.Powermode = (powermode_t)g->p.ptr;
  329. return;
  330. case GDISP_CONTROL_ORIENTATION:
  331. if (g->g.Orientation == (orientation_t)g->p.ptr)
  332. return;
  333. switch((orientation_t)g->p.ptr) {
  334. case GDISP_ROTATE_0:
  335. acquire_bus(g);
  336. write_reg(g, 0x01, 0x0100);
  337. write_reg(g, 0x03, 0x1030);
  338. write_reg(g, 0x60, 0xa700);
  339. release_bus(g);
  340. g->g.Height = GDISP_SCREEN_HEIGHT;
  341. g->g.Width = GDISP_SCREEN_WIDTH;
  342. break;
  343. case GDISP_ROTATE_90:
  344. acquire_bus(g);
  345. write_reg(g, 0x01, 0x0100);
  346. write_reg(g, 0x03, 0x1038);
  347. write_reg(g, 0x60, 0x2700);
  348. release_bus(g);
  349. g->g.Height = GDISP_SCREEN_WIDTH;
  350. g->g.Width = GDISP_SCREEN_HEIGHT;
  351. break;
  352. case GDISP_ROTATE_180:
  353. acquire_bus(g);
  354. write_reg(g, 0x01, 0x0000);
  355. write_reg(g, 0x03, 0x1030);
  356. write_reg(g, 0x60, 0x2700);
  357. release_bus(g);
  358. g->g.Height = GDISP_SCREEN_HEIGHT;
  359. g->g.Width = GDISP_SCREEN_WIDTH;
  360. break;
  361. case GDISP_ROTATE_270:
  362. acquire_bus(g);
  363. write_reg(g, 0x01, 0x0000);
  364. write_reg(g, 0x03, 0x1038);
  365. write_reg(g, 0x60, 0xa700);
  366. release_bus(g);
  367. g->g.Height = GDISP_SCREEN_WIDTH;
  368. g->g.Width = GDISP_SCREEN_HEIGHT;
  369. break;
  370. default:
  371. return;
  372. }
  373. g->g.Orientation = (orientation_t)g->p.ptr;
  374. return;
  375. case GDISP_CONTROL_BACKLIGHT:
  376. if ((unsigned)g->p.ptr > 100)
  377. g->p.ptr = (void *)100;
  378. set_backlight(g, (unsigned)g->p.ptr);
  379. g->g.Backlight = (unsigned)g->p.ptr;
  380. return;
  381. default:
  382. return;
  383. }
  384. }
  385. #endif
  386. #endif /* GFX_USE_GDISP */