ucg_dev_ic_ili9325.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. ucg_dev_ic_ili9325.c
  3. Specific code for the ili9325 controller (TFT displays)
  4. Universal uC Color Graphics Library
  5. Copyright (c) 2014, olikraus@gmail.com
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without modification,
  8. are permitted provided that the following conditions are met:
  9. * Redistributions of source code must retain the above copyright notice, this list
  10. of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above copyright notice, this
  12. list of conditions and the following disclaimer in the documentation and/or other
  13. materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  15. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  16. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  17. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  26. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "ucg.h"
  29. static const ucg_pgm_uint8_t ucg_ili9325_set_pos_seq[] =
  30. {
  31. UCG_CS(0), /* enable chip */
  32. UCG_C10(0x020), UCG_VARX(0,0x00, 0), UCG_VARX(0,0x0ff, 0), /* set x position */
  33. UCG_C10(0x021), UCG_VARY(8,0x01, 0), UCG_VARY(0,0x0ff, 0), /* set y position */
  34. UCG_C10(0x022), /* write to RAM */
  35. UCG_DATA(), /* change to data mode */
  36. UCG_END()
  37. };
  38. static const ucg_pgm_uint8_t ucg_ili9325_set_pos_dir0_seq[] =
  39. {
  40. UCG_CS(0), /* enable chip */
  41. /* last byte: 0x030 horizontal increment (dir = 0) */
  42. /* last byte: 0x038 vertical increment (dir = 1) */
  43. /* last byte: 0x000 horizontal deccrement (dir = 2) */
  44. /* last byte: 0x008 vertical deccrement (dir = 3) */
  45. UCG_C22(0x000, 0x003, 0xc0 | 0x010, 0x030), /* Entry Mode, GRAM write direction and BGR (Bit 12)=1, set TRI (Bit 15) and DFM (Bit 14) --> three byte transfer */
  46. UCG_C10(0x020), UCG_VARX(0,0x00, 0), UCG_VARX(0,0x0ff, 0), /* set x position */
  47. UCG_C10(0x021), UCG_VARY(8,0x01, 0), UCG_VARY(0,0x0ff, 0), /* set y position */
  48. UCG_C10(0x022), /* write to RAM */
  49. UCG_DATA(), /* change to data mode */
  50. UCG_END()
  51. };
  52. static const ucg_pgm_uint8_t ucg_ili9325_set_pos_dir1_seq[] =
  53. {
  54. UCG_CS(0), /* enable chip */
  55. /* last byte: 0x030 horizontal increment (dir = 0) */
  56. /* last byte: 0x038 vertical increment (dir = 1) */
  57. /* last byte: 0x000 horizontal deccrement (dir = 2) */
  58. /* last byte: 0x008 vertical deccrement (dir = 3) */
  59. UCG_C22(0x000, 0x003, 0xc0 | 0x010, 0x038), /* Entry Mode, GRAM write direction and BGR (Bit 12)=1, set TRI (Bit 15) and DFM (Bit 14) --> three byte transfer */
  60. UCG_C10(0x020), UCG_VARX(0,0x00, 0), UCG_VARX(0,0x0ff, 0), /* set x position */
  61. UCG_C10(0x021), UCG_VARY(8,0x01, 0), UCG_VARY(0,0x0ff, 0), /* set y position */
  62. UCG_C10(0x022), /* write to RAM */
  63. UCG_DATA(), /* change to data mode */
  64. UCG_END()
  65. };
  66. static const ucg_pgm_uint8_t ucg_ili9325_set_pos_dir2_seq[] =
  67. {
  68. UCG_CS(0), /* enable chip */
  69. /* last byte: 0x030 horizontal increment (dir = 0) */
  70. /* last byte: 0x038 vertical increment (dir = 1) */
  71. /* last byte: 0x000 horizontal deccrement (dir = 2) */
  72. /* last byte: 0x008 vertical deccrement (dir = 3) */
  73. UCG_C22(0x000, 0x003, 0xc0 | 0x010, 0x000), /* Entry Mode, GRAM write direction and BGR (Bit 12)=1, set TRI (Bit 15) and DFM (Bit 14) --> three byte transfer */
  74. UCG_C10(0x020), UCG_VARX(0,0x00, 0), UCG_VARX(0,0x0ff, 0), /* set x position */
  75. UCG_C10(0x021), UCG_VARY(8,0x01, 0), UCG_VARY(0,0x0ff, 0), /* set y position */
  76. UCG_C10(0x022), /* write to RAM */
  77. UCG_DATA(), /* change to data mode */
  78. UCG_END()
  79. };
  80. static const ucg_pgm_uint8_t ucg_ili9325_set_pos_dir3_seq[] =
  81. {
  82. UCG_CS(0), /* enable chip */
  83. /* last byte: 0x030 horizontal increment (dir = 0) */
  84. /* last byte: 0x038 vertical increment (dir = 1) */
  85. /* last byte: 0x000 horizontal deccrement (dir = 2) */
  86. /* last byte: 0x008 vertical deccrement (dir = 3) */
  87. UCG_C22(0x000, 0x003, 0xc0 | 0x010, 0x008), /* Entry Mode, GRAM write direction and BGR (Bit 12)=1, set TRI (Bit 15) and DFM (Bit 14) --> three byte transfer */
  88. UCG_C10(0x020), UCG_VARX(0,0x00, 0), UCG_VARX(0,0x0ff, 0), /* set x position */
  89. UCG_C10(0x021), UCG_VARY(8,0x01, 0), UCG_VARY(0,0x0ff, 0), /* set y position */
  90. UCG_C10(0x022), /* write to RAM */
  91. UCG_DATA(), /* change to data mode */
  92. UCG_END()
  93. };
  94. static ucg_int_t ucg_handle_ili9325_l90fx(ucg_t *ucg)
  95. {
  96. uint8_t c[3];
  97. if ( ucg_clip_l90fx(ucg) != 0 )
  98. {
  99. switch(ucg->arg.dir)
  100. {
  101. case 0:
  102. ucg_com_SendCmdSeq(ucg, ucg_ili9325_set_pos_dir0_seq);
  103. break;
  104. case 1:
  105. ucg_com_SendCmdSeq(ucg, ucg_ili9325_set_pos_dir1_seq);
  106. break;
  107. case 2:
  108. ucg_com_SendCmdSeq(ucg, ucg_ili9325_set_pos_dir2_seq);
  109. break;
  110. case 3:
  111. default:
  112. ucg_com_SendCmdSeq(ucg, ucg_ili9325_set_pos_dir3_seq);
  113. break;
  114. }
  115. c[0] = ucg->arg.pixel.rgb.color[0];
  116. c[1] = ucg->arg.pixel.rgb.color[1];
  117. c[2] = ucg->arg.pixel.rgb.color[2];
  118. ucg_com_SendRepeat3Bytes(ucg, ucg->arg.len, c);
  119. ucg_com_SetCSLineStatus(ucg, 1); /* disable chip */
  120. return 1;
  121. }
  122. return 0;
  123. }
  124. /*
  125. L2TC (Glyph Output)
  126. Because of this for vertical lines the x min and max values in
  127. "ucg_ili9325_set_pos_for_y_seq" are identical to avoid changes of the x position
  128. */
  129. static const ucg_pgm_uint8_t ucg_ili9325_set_x_pos_seq[] =
  130. {
  131. UCG_C10(0x020), UCG_VARX(0,0x00, 0), UCG_VARX(0,0x0ff, 0), /* set x position */
  132. UCG_C10(0x022), /* write to RAM */
  133. UCG_DATA(), /* change to data mode */
  134. UCG_END()
  135. };
  136. static const ucg_pgm_uint8_t ucg_ili9325_set_y_pos_seq[] =
  137. {
  138. UCG_C10(0x021), UCG_VARY(8,0x01, 0), UCG_VARY(0,0x0ff, 0), /* set y position */
  139. UCG_C10(0x022), /* write to RAM */
  140. UCG_DATA(), /* change to data mode */
  141. UCG_END()
  142. };
  143. /* without CmdDataSequence */
  144. ucg_int_t xxxxxx_ucg_handle_ili9325_l90tc(ucg_t *ucg)
  145. {
  146. if ( ucg_clip_l90tc(ucg) != 0 )
  147. {
  148. uint8_t buf[3];
  149. ucg_int_t dx, dy;
  150. ucg_int_t i;
  151. const uint8_t *seq;
  152. unsigned char pixmap;
  153. uint8_t bitcnt;
  154. ucg_com_SetCSLineStatus(ucg, 0); /* enable chip */
  155. ucg_com_SendCmdSeq(ucg, ucg_ili9325_set_pos_seq);
  156. switch(ucg->arg.dir)
  157. {
  158. case 0:
  159. dx = 1; dy = 0;
  160. seq = ucg_ili9325_set_x_pos_seq;
  161. break;
  162. case 1:
  163. dx = 0; dy = 1;
  164. seq = ucg_ili9325_set_y_pos_seq;
  165. break;
  166. case 2:
  167. dx = -1; dy = 0;
  168. seq = ucg_ili9325_set_x_pos_seq;
  169. break;
  170. case 3:
  171. default:
  172. dx = 0; dy = -1;
  173. seq = ucg_ili9325_set_y_pos_seq;
  174. break;
  175. }
  176. pixmap = ucg_pgm_read(ucg->arg.bitmap);
  177. bitcnt = ucg->arg.pixel_skip;
  178. pixmap <<= bitcnt;
  179. buf[0] = ucg->arg.pixel.rgb.color[0];
  180. buf[1] = ucg->arg.pixel.rgb.color[1];
  181. buf[2] = ucg->arg.pixel.rgb.color[2];
  182. //ucg_com_SetCSLineStatus(ucg, 0); /* enable chip */
  183. for( i = 0; i < ucg->arg.len; i++ )
  184. {
  185. if ( (pixmap & 128) != 0 )
  186. {
  187. ucg_com_SendCmdSeq(ucg, seq);
  188. ucg_com_SendRepeat3Bytes(ucg, 1, buf);
  189. }
  190. pixmap<<=1;
  191. ucg->arg.pixel.pos.x+=dx;
  192. ucg->arg.pixel.pos.y+=dy;
  193. bitcnt++;
  194. if ( bitcnt >= 8 )
  195. {
  196. ucg->arg.bitmap++;
  197. pixmap = ucg_pgm_read(ucg->arg.bitmap);
  198. bitcnt = 0;
  199. }
  200. }
  201. ucg_com_SetCSLineStatus(ucg, 1); /* disable chip */
  202. return 1;
  203. }
  204. return 0;
  205. }
  206. /* with CmdDataSequence */
  207. static ucg_int_t ucg_handle_ili9325_l90tc(ucg_t *ucg)
  208. {
  209. if ( ucg_clip_l90tc(ucg) != 0 )
  210. {
  211. uint8_t buf[20];
  212. ucg_int_t dx, dy;
  213. ucg_int_t i;
  214. unsigned char pixmap;
  215. uint8_t bitcnt;
  216. ucg_com_SetCSLineStatus(ucg, 0); /* enable chip */
  217. ucg_com_SendCmdSeq(ucg, ucg_ili9325_set_pos_seq);
  218. buf[0] = 0x001; // change to 0 (cmd mode)
  219. buf[1] = 0x020; // set x
  220. buf[2] = 0x002; // change to 1 (arg mode)
  221. buf[3] = 0x000; // upper part x
  222. buf[4] = 0x000; // no change
  223. buf[5] = 0x000; // will be overwritten by x value
  224. switch(ucg->arg.dir)
  225. {
  226. case 0:
  227. dx = 1; dy = 0;
  228. buf[6] = 0x001; // change to 0 (cmd mode)
  229. buf[7] = 0x022; // write data
  230. buf[8] = 0x002; // change to 1 (data mode)
  231. buf[9] = 0x000; // red value
  232. buf[10] = 0x000; // no change
  233. buf[11] = 0x000; // green value
  234. buf[12] = 0x000; // no change
  235. buf[13] = 0x000; // blue value
  236. break;
  237. case 1:
  238. dx = 0; dy = 1;
  239. buf[6] = 0x001; // change to 0 (cmd mode)
  240. buf[7] = 0x021; // set y
  241. buf[8] = 0x002; // change to 1 (arg mode)
  242. buf[9] = 0x000; // upper part y
  243. buf[10] = 0x000; // no change
  244. buf[11] = 0x000; // will be overwritten by y value
  245. buf[12] = 0x001; // change to 0 (cmd mode)
  246. buf[13] = 0x022; // write data
  247. buf[14] = 0x002; // change to 1 (data mode)
  248. buf[15] = 0x000; // red value
  249. buf[16] = 0x000; // no change
  250. buf[17] = 0x000; // green value
  251. buf[18] = 0x000; // no change
  252. buf[19] = 0x000; // blue value
  253. break;
  254. case 2:
  255. dx = -1; dy = 0;
  256. buf[6] = 0x001; // change to 0 (cmd mode)
  257. buf[7] = 0x022; // write data
  258. buf[8] = 0x002; // change to 1 (data mode)
  259. buf[9] = 0x000; // red value
  260. buf[10] = 0x000; // no change
  261. buf[11] = 0x000; // green value
  262. buf[12] = 0x000; // no change
  263. buf[13] = 0x000; // blue value
  264. break;
  265. case 3:
  266. default:
  267. dx = 0; dy = -1;
  268. buf[6] = 0x001; // change to 0 (cmd mode)
  269. buf[7] = 0x021; // set y
  270. buf[8] = 0x002; // change to 1 (arg mode)
  271. buf[9] = 0x000; // upper part y
  272. buf[10] = 0x000; // no change
  273. buf[11] = 0x000; // will be overwritten by y value
  274. buf[12] = 0x001; // change to 0 (cmd mode)
  275. buf[13] = 0x022; // write data
  276. buf[14] = 0x002; // change to 1 (data mode)
  277. buf[15] = 0x000; // red value
  278. buf[16] = 0x000; // no change
  279. buf[17] = 0x000; // green value
  280. buf[18] = 0x000; // no change
  281. buf[19] = 0x000; // blue value
  282. break;
  283. }
  284. pixmap = ucg_pgm_read(ucg->arg.bitmap);
  285. bitcnt = ucg->arg.pixel_skip;
  286. pixmap <<= bitcnt;
  287. if ( (ucg->arg.dir&1) == 0 )
  288. {
  289. buf[9] = ucg->arg.pixel.rgb.color[0];
  290. buf[11] = ucg->arg.pixel.rgb.color[1];
  291. buf[13] = ucg->arg.pixel.rgb.color[2];
  292. }
  293. else
  294. {
  295. buf[15] = ucg->arg.pixel.rgb.color[0];
  296. buf[17] = ucg->arg.pixel.rgb.color[1];
  297. buf[19] = ucg->arg.pixel.rgb.color[2];
  298. }
  299. //ucg_com_SetCSLineStatus(ucg, 0); /* enable chip */
  300. for( i = 0; i < ucg->arg.len; i++ )
  301. {
  302. if ( (pixmap & 128) != 0 )
  303. {
  304. if ( (ucg->arg.dir&1) == 0 )
  305. {
  306. buf[5] = ucg->arg.pixel.pos.x;
  307. ucg_com_SendCmdDataSequence(ucg, 7, buf, 1);
  308. }
  309. else
  310. {
  311. buf[5] = ucg->arg.pixel.pos.x;
  312. buf[9] = (ucg->arg.pixel.pos.y>>8)&1;
  313. buf[11] = ucg->arg.pixel.pos.y&255;
  314. ucg_com_SendCmdDataSequence(ucg, 10, buf, 1);
  315. }
  316. }
  317. pixmap<<=1;
  318. ucg->arg.pixel.pos.x+=dx;
  319. ucg->arg.pixel.pos.y+=dy;
  320. bitcnt++;
  321. if ( bitcnt >= 8 )
  322. {
  323. ucg->arg.bitmap++;
  324. pixmap = ucg_pgm_read(ucg->arg.bitmap);
  325. bitcnt = 0;
  326. }
  327. }
  328. ucg_com_SetCSLineStatus(ucg, 1); /* disable chip */
  329. return 1;
  330. }
  331. return 0;
  332. }
  333. static ucg_int_t ucg_handle_ili9325_l90se(ucg_t *ucg)
  334. {
  335. uint8_t i;
  336. uint8_t c[3];
  337. /* Setup ccs for l90se. This will be updated by ucg_clip_l90se if required */
  338. for ( i = 0; i < 3; i++ )
  339. {
  340. ucg_ccs_init(ucg->arg.ccs_line+i, ucg->arg.rgb[0].color[i], ucg->arg.rgb[1].color[i], ucg->arg.len);
  341. }
  342. /* check if the line is visible */
  343. if ( ucg_clip_l90se(ucg) != 0 )
  344. {
  345. ucg_int_t i;
  346. switch(ucg->arg.dir)
  347. {
  348. case 0:
  349. ucg_com_SendCmdSeq(ucg, ucg_ili9325_set_pos_dir0_seq);
  350. break;
  351. case 1:
  352. ucg_com_SendCmdSeq(ucg, ucg_ili9325_set_pos_dir1_seq);
  353. break;
  354. case 2:
  355. ucg_com_SendCmdSeq(ucg, ucg_ili9325_set_pos_dir2_seq);
  356. break;
  357. case 3:
  358. default:
  359. ucg_com_SendCmdSeq(ucg, ucg_ili9325_set_pos_dir3_seq);
  360. break;
  361. }
  362. for( i = 0; i < ucg->arg.len; i++ )
  363. {
  364. c[0] = ucg->arg.ccs_line[0].current;
  365. c[1] = ucg->arg.ccs_line[1].current;
  366. c[2] = ucg->arg.ccs_line[2].current;
  367. ucg_com_SendRepeat3Bytes(ucg, 1, c);
  368. ucg_ccs_step(ucg->arg.ccs_line+0);
  369. ucg_ccs_step(ucg->arg.ccs_line+1);
  370. ucg_ccs_step(ucg->arg.ccs_line+2);
  371. }
  372. ucg_com_SetCSLineStatus(ucg, 1); /* disable chip */
  373. return 1;
  374. }
  375. return 0;
  376. }
  377. ucg_int_t ucg_dev_ic_ili9325_18(ucg_t *ucg, ucg_int_t msg, void *data)
  378. {
  379. switch(msg)
  380. {
  381. case UCG_MSG_DEV_POWER_UP:
  382. /* setup com interface and provide information on the clock speed */
  383. /* of the serial and parallel interface. Values are nanoseconds. */
  384. return ucg_com_PowerUp(ucg, 40, 100);
  385. case UCG_MSG_DEV_POWER_DOWN:
  386. /* not yet implemented */
  387. return 1;
  388. case UCG_MSG_GET_DIMENSION:
  389. ((ucg_wh_t *)data)->w = 240;
  390. ((ucg_wh_t *)data)->h = 320;
  391. return 1;
  392. case UCG_MSG_DRAW_PIXEL:
  393. if ( ucg_clip_is_pixel_visible(ucg) !=0 )
  394. {
  395. uint8_t c[3];
  396. ucg_com_SendCmdSeq(ucg, ucg_ili9325_set_pos_seq);
  397. c[0] = ucg->arg.pixel.rgb.color[0];
  398. c[1] = ucg->arg.pixel.rgb.color[1];
  399. c[2] = ucg->arg.pixel.rgb.color[2];
  400. ucg_com_SendRepeat3Bytes(ucg, 1, c);
  401. ucg_com_SetCSLineStatus(ucg, 1); /* disable chip */
  402. }
  403. return 1;
  404. case UCG_MSG_DRAW_L90FX:
  405. //ucg_handle_l90fx(ucg, ucg_dev_ic_ili9325_18);
  406. ucg_handle_ili9325_l90fx(ucg);
  407. return 1;
  408. #ifdef UCG_MSG_DRAW_L90TC
  409. case UCG_MSG_DRAW_L90TC:
  410. //ucg_handle_l90tc(ucg, ucg_dev_ic_ili9325);
  411. ucg_handle_ili9325_l90tc(ucg);
  412. return 1;
  413. #endif /* UCG_MSG_DRAW_L90TC */
  414. #ifdef UCG_MSG_DRAW_L90BF
  415. case UCG_MSG_DRAW_L90BF:
  416. ucg_handle_l90bf(ucg, ucg_dev_ic_ili9325_18);
  417. return 1;
  418. #endif /* UCG_MSG_DRAW_L90BF */
  419. /* msg UCG_MSG_DRAW_L90SE is handled by ucg_dev_default_cb */
  420. /*
  421. case UCG_MSG_DRAW_L90SE:
  422. return ucg->ext_cb(ucg, msg, data);
  423. */
  424. }
  425. return ucg_dev_default_cb(ucg, msg, data);
  426. }
  427. ucg_int_t ucg_ext_ili9325_18(ucg_t *ucg, ucg_int_t msg, void *data)
  428. {
  429. switch(msg)
  430. {
  431. case UCG_MSG_DRAW_L90SE:
  432. //ucg_handle_l90se(ucg, ucg_dev_ic_ili9325);
  433. ucg_handle_ili9325_l90se(ucg);
  434. break;
  435. }
  436. return 1;
  437. }