µGFX library fork

gdisp_lld_ILI9325.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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_ILI9325
  20. #include "gdisp_lld_config.h"
  21. #include "../../../src/gdisp/gdisp_driver.h"
  22. #include "board_ILI9325.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 void set_cursor(GDisplay *g) {
  48. switch(g->g.Orientation) {
  49. default:
  50. case GDISP_ROTATE_0:
  51. case GDISP_ROTATE_180:
  52. write_reg(g, 0x20, g->p.x);
  53. write_reg(g, 0x21, g->p.y);
  54. break;
  55. case GDISP_ROTATE_90:
  56. case GDISP_ROTATE_270:
  57. write_reg(g, 0x20, g->p.y);
  58. write_reg(g, 0x21, g->p.x);
  59. break;
  60. }
  61. write_index(g, 0x22);
  62. }
  63. static void set_viewport(GDisplay* g) {
  64. switch(g->g.Orientation) {
  65. default:
  66. case GDISP_ROTATE_0:
  67. case GDISP_ROTATE_180:
  68. write_reg(g, 0x50, g->p.x);
  69. write_reg(g, 0x51, g->p.x + g->p.cx - 1);
  70. write_reg(g, 0x52, g->p.y);
  71. write_reg(g, 0x53, g->p.y + g->p.cy - 1);
  72. break;
  73. case GDISP_ROTATE_90:
  74. case GDISP_ROTATE_270:
  75. write_reg(g, 0x50, g->p.y);
  76. write_reg(g, 0x51, g->p.y + g->p.cy - 1);
  77. write_reg(g, 0x52, g->p.x);
  78. write_reg(g, 0x53, g->p.x + g->p.cx - 1);
  79. break;
  80. }
  81. }
  82. LLDSPEC bool_t gdisp_lld_init(GDisplay *g) {
  83. uint16_t cver;
  84. // No private area for this controller
  85. g->priv = 0;
  86. // Initialise the board interface
  87. init_board(g);
  88. /* Hardware reset */
  89. setpin_reset(g, TRUE);
  90. gfxSleepMicroseconds(1000);
  91. setpin_reset(g, FALSE);
  92. gfxSleepMicroseconds(1000);
  93. acquire_bus(g);
  94. write_index(g, 0); // Get controller version
  95. setreadmode(g);
  96. dummy_read(g);
  97. cver = read_data(g);
  98. setwritemode(g);
  99. // chinese code starts here
  100. write_reg(g, 0x00, 0x0001);
  101. gfxSleepMilliseconds(10);
  102. write_reg(g, 0x15, 0x0030);
  103. write_reg(g, 0x11, 0x0040);
  104. write_reg(g, 0x10, 0x1628);
  105. write_reg(g, 0x12, 0x0000);
  106. write_reg(g, 0x13, 0x104d);
  107. gfxSleepMilliseconds(10);
  108. write_reg(g, 0x12, 0x0010);
  109. gfxSleepMilliseconds(10);
  110. write_reg(g, 0x10, 0x2620);
  111. write_reg(g, 0x13, 0x344d); //304d
  112. gfxSleepMilliseconds(10);
  113. write_reg(g, 0x01, 0x0100);
  114. write_reg(g, 0x02, 0x0300);
  115. write_reg(g, 0x03, 0x1038);//0x1030
  116. write_reg(g, 0x08, 0x0604);
  117. write_reg(g, 0x09, 0x0000);
  118. write_reg(g, 0x0A, 0x0008);
  119. write_reg(g, 0x41, 0x0002);
  120. write_reg(g, 0x60, 0x2700);
  121. write_reg(g, 0x61, 0x0001);
  122. write_reg(g, 0x90, 0x0182);
  123. write_reg(g, 0x93, 0x0001);
  124. write_reg(g, 0xa3, 0x0010);
  125. gfxSleepMilliseconds(10);
  126. //################# void Gamma_Set(void) ####################//
  127. write_reg(g, 0x30, 0x0000);
  128. write_reg(g, 0x31, 0x0502);
  129. write_reg(g, 0x32, 0x0307);
  130. write_reg(g, 0x33, 0x0305);
  131. write_reg(g, 0x34, 0x0004);
  132. write_reg(g, 0x35, 0x0402);
  133. write_reg(g, 0x36, 0x0707);
  134. write_reg(g, 0x37, 0x0503);
  135. write_reg(g, 0x38, 0x1505);
  136. write_reg(g, 0x39, 0x1505);
  137. gfxSleepMilliseconds(10);
  138. //################## void Display_ON(void) ####################//
  139. write_reg(g, 0x07, 0x0001);
  140. gfxSleepMilliseconds(10);
  141. write_reg(g, 0x07, 0x0021);
  142. write_reg(g, 0x07, 0x0023);
  143. gfxSleepMilliseconds(10);
  144. write_reg(g, 0x07, 0x0033);
  145. gfxSleepMilliseconds(10);
  146. write_reg(g, 0x07, 0x0133);
  147. // chinese code ends here
  148. // Finish Init
  149. post_init_board(g);
  150. // Release the bus
  151. release_bus(g);
  152. // Turn on the backlight
  153. set_backlight(g, GDISP_INITIAL_BACKLIGHT);
  154. /* Initialise the GDISP structure */
  155. g->g.Width = GDISP_SCREEN_WIDTH;
  156. g->g.Height = GDISP_SCREEN_HEIGHT;
  157. g->g.Orientation = GDISP_ROTATE_0;
  158. g->g.Powermode = powerOn;
  159. g->g.Backlight = GDISP_INITIAL_BACKLIGHT;
  160. g->g.Contrast = GDISP_INITIAL_CONTRAST;
  161. return TRUE;
  162. }
  163. #if GDISP_HARDWARE_STREAM_WRITE
  164. LLDSPEC void gdisp_lld_write_start(GDisplay *g) {
  165. acquire_bus(g);
  166. set_viewport(g);
  167. }
  168. LLDSPEC void gdisp_lld_write_color(GDisplay *g) {
  169. write_data(g, gdispColor2Native(g->p.color));
  170. }
  171. LLDSPEC void gdisp_lld_write_stop(GDisplay *g) {
  172. release_bus(g);
  173. }
  174. LLDSPEC void gdisp_lld_write_pos(GDisplay *g) {
  175. set_cursor(g);
  176. }
  177. #endif
  178. #if GDISP_HARDWARE_STREAM_READ
  179. LLDSPEC void gdisp_lld_read_start(GDisplay *g) {
  180. acquire_bus(g);
  181. set_viewport(g);
  182. set_cursor(g);
  183. setreadmode(g);
  184. dummy_read(g);
  185. }
  186. LLDSPEC color_t gdisp_lld_read_color(GDisplay *g) {
  187. uint16_t data;
  188. data = read_data(g);
  189. return gdispNative2Color(data);
  190. }
  191. LLDSPEC void gdisp_lld_read_stop(GDisplay *g) {
  192. setwritemode(g);
  193. release_bus(g);
  194. }
  195. #endif
  196. #if GDISP_NEED_CONTROL && GDISP_HARDWARE_CONTROL
  197. LLDSPEC void gdisp_lld_control(GDisplay *g) {
  198. switch(g->p.x) {
  199. case GDISP_CONTROL_POWER:
  200. if (g->g.Powermode == (powermode_t)g->p.ptr)
  201. return;
  202. switch((powermode_t)g->p.ptr) {
  203. case powerOff:
  204. acquire_bus(g);
  205. write_reg(g, 0x07, 0x0000);
  206. write_reg(g, 0x10, 0x0000);
  207. write_reg(g, 0x11, 0x0000);
  208. write_reg(g, 0x12, 0x0000);
  209. write_reg(g, 0x13, 0x0000);
  210. release_bus(g);
  211. set_backlight(g, 0);
  212. break;
  213. case powerOn:
  214. //*************Power On sequence ******************//
  215. acquire_bus(g);
  216. write_reg(g, 0x10, 0x0000); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  217. write_reg(g, 0x11, 0x0000); /* DC1[2:0], DC0[2:0], VC[2:0] */
  218. write_reg(g, 0x12, 0x0000); /* VREG1OUT voltage */
  219. write_reg(g, 0x13, 0x0000); /* VDV[4:0] for VCOM amplitude */
  220. gfxSleepMilliseconds(200); /* Dis-charge capacitor power voltage */
  221. write_reg(g, 0x10, 0x17B0); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  222. write_reg(g, 0x11, 0x0147); /* DC1[2:0], DC0[2:0], VC[2:0] */
  223. gfxSleepMilliseconds(50);
  224. write_reg(g, 0x12, 0x013C); /* VREG1OUT voltage */
  225. gfxSleepMilliseconds(50);
  226. write_reg(g, 0x13, 0x0E00); /* VDV[4:0] for VCOM amplitude */
  227. write_reg(g, 0x29, 0x0009); /* VCM[4:0] for VCOMH */
  228. gfxSleepMilliseconds(50);
  229. write_reg(g, 0x07, 0x0173); /* 262K color and display ON */
  230. release_bus(g);
  231. set_backlight(g, g->g.Backlight);
  232. break;
  233. case powerSleep:
  234. acquire_bus(g);
  235. write_reg(g, 0x07, 0x0000); /* display OFF */
  236. write_reg(g, 0x10, 0x0000); /* SAP, BT[3:0], APE, AP, DSTB, SLP */
  237. write_reg(g, 0x11, 0x0000); /* DC1[2:0], DC0[2:0], VC[2:0] */
  238. write_reg(g, 0x12, 0x0000); /* VREG1OUT voltage */
  239. write_reg(g, 0x13, 0x0000); /* VDV[4:0] for VCOM amplitude */
  240. gfxSleepMilliseconds(200); /* Dis-charge capacitor power voltage */
  241. write_reg(g, 0x10, 0x0002); /* SAP, BT[3:0], APE, AP, DSTB, SLP */
  242. release_bus(g);
  243. gdisp_lld_backlight(g, 0);
  244. break;
  245. case powerDeepSleep:
  246. acquire_bus(g);
  247. write_reg(g, 0x07, 0x0000); /* display OFF */
  248. write_reg(g, 0x10, 0x0000); /* SAP, BT[3:0], APE, AP, DSTB, SLP */
  249. write_reg(g, 0x11, 0x0000); /* DC1[2:0], DC0[2:0], VC[2:0] */
  250. write_reg(g, 0x12, 0x0000); /* VREG1OUT voltage */
  251. write_reg(g, 0x13, 0x0000); /* VDV[4:0] for VCOM amplitude */
  252. gfxSleepMilliseconds(200); /* Dis-charge capacitor power voltage */
  253. write_reg(g, 0x10, 0x0004); /* SAP, BT[3:0], APE, AP, DSTB, SLP */
  254. release_bus(g);
  255. gdisp_lld_backlight(g, 0);
  256. break;
  257. default:
  258. return;
  259. }
  260. g->g.Powermode = (powermode_t)g->p.ptr;
  261. return;
  262. case GDISP_CONTROL_ORIENTATION:
  263. if (g->g.Orientation == (orientation_t)g->p.ptr)
  264. return;
  265. switch((orientation_t)g->p.ptr) {
  266. case GDISP_ROTATE_0:
  267. acquire_bus(g);
  268. write_reg(g, 0x01, 0x0100);
  269. write_reg(g, 0x03, 0x1038);
  270. write_reg(g, 0x60, 0x2700);
  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, 0x01, 0x0000);
  278. write_reg(g, 0x03, 0x1030);
  279. write_reg(g, 0x60, 0x2700);
  280. release_bus(g);
  281. g->g.Height = GDISP_SCREEN_WIDTH;
  282. g->g.Width = GDISP_SCREEN_HEIGHT;
  283. break;
  284. case GDISP_ROTATE_180:
  285. acquire_bus(g);
  286. write_reg(g, 0x01, 0x0000);
  287. write_reg(g, 0x03, 0x1038);
  288. write_reg(g, 0x60, 0xa700);
  289. release_bus(g);
  290. g->g.Height = GDISP_SCREEN_HEIGHT;
  291. g->g.Width = GDISP_SCREEN_WIDTH;
  292. break;
  293. case GDISP_ROTATE_270:
  294. acquire_bus(g);
  295. write_reg(g, 0x01, 0x0100);
  296. write_reg(g, 0x03, 0x1030);
  297. write_reg(g, 0x60, 0xA700);
  298. release_bus(g);
  299. g->g.Height = GDISP_SCREEN_WIDTH;
  300. g->g.Width = GDISP_SCREEN_HEIGHT;
  301. break;
  302. default:
  303. return;
  304. }
  305. g->g.Orientation = (orientation_t)g->p.ptr;
  306. return;
  307. case GDISP_CONTROL_BACKLIGHT:
  308. if ((unsigned)g->p.ptr > 100)
  309. g->p.ptr = (void *)100;
  310. set_backlight(g, (unsigned)g->p.ptr);
  311. g->g.Backlight = (unsigned)g->p.ptr;
  312. return;
  313. default:
  314. return;
  315. }
  316. }
  317. #endif
  318. #endif /* GFX_USE_GDISP */