drv_spi_ili9488.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-08-10 zylx first version
  9. * 2022-02-01 Rudy Lo add lcd_fill_array function
  10. */
  11. #include <board.h>
  12. #ifdef BSP_USING_SPI_LCD_ILI9488
  13. #include <rtdevice.h>
  14. #include "drv_spi.h"
  15. #include "drv_spi_ili9488.h"
  16. #include <lcd_spi_port.h>
  17. #include "drv_lcd_font.h"
  18. #include <rttlogo.h>
  19. #include <string.h>
  20. #if defined(PKG_USING_GUIENGINE)
  21. #include <rtgui/driver.h>
  22. #endif
  23. #define DRV_DEBUG
  24. #define LOG_TAG "drv.spi_lcd"
  25. #include <drv_log.h>
  26. #define LCD_DC_PIN GET_PIN(C, 7)
  27. #define LCD_DEVICE(dev) (struct drv_lcd_device*)(dev)
  28. #define LCD_CLEAR_SEND_NUMBER (LCD_H * LCD_W *3)
  29. static rt_uint32_t BACK_COLOR = WHITE, FORE_COLOR = BLACK;
  30. static struct rt_spi_device *spi_dev_lcd;
  31. static int rt_hw_lcd_config(void)
  32. {
  33. spi_dev_lcd = (struct rt_spi_device *)rt_device_find("spi20");
  34. /* config spi */
  35. {
  36. struct rt_spi_configuration cfg;
  37. cfg.data_width = 8;
  38. cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB;
  39. cfg.max_hz = 50 * 1000 * 1000;
  40. rt_spi_configure(spi_dev_lcd, &cfg);
  41. }
  42. return RT_EOK;
  43. }
  44. static rt_err_t lcd_write_cmd(const rt_uint8_t cmd)
  45. {
  46. rt_size_t len;
  47. rt_pin_write(LCD_DC_PIN, PIN_LOW);
  48. len = rt_spi_send(spi_dev_lcd, &cmd, 1);
  49. if (len != 1)
  50. {
  51. LOG_I("lcd_write_cmd error. %d", len);
  52. return -RT_ERROR;
  53. }
  54. else
  55. {
  56. return RT_EOK;
  57. }
  58. }
  59. static rt_err_t lcd_write_data(const rt_uint8_t data)
  60. {
  61. rt_size_t len;
  62. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  63. len = rt_spi_send(spi_dev_lcd, &data, 1);
  64. if (len != 1)
  65. {
  66. LOG_I("lcd_write_data error. %d", len);
  67. return -RT_ERROR;
  68. }
  69. else
  70. {
  71. return RT_EOK;
  72. }
  73. }
  74. static rt_err_t lcd_write_half_word(const rt_uint16_t da)
  75. {
  76. rt_size_t len;
  77. char data[2] = {0};
  78. data[0] = da >> 8;
  79. data[1] = da;
  80. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  81. len = rt_spi_send(spi_dev_lcd, data, 2);
  82. if (len != 2)
  83. {
  84. LOG_I("lcd_write_half_word error. %d", len);
  85. return -RT_ERROR;
  86. }
  87. else
  88. {
  89. return RT_EOK;
  90. }
  91. }
  92. static rt_err_t lcd_write_three_bytes(const rt_uint32_t da)
  93. {
  94. rt_size_t len;
  95. char data[3] = {0};
  96. data[0] = da >> 16;
  97. data[1] = da >> 8;
  98. data[2] = da;
  99. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  100. len = rt_spi_send(spi_dev_lcd, data, 3);
  101. if (len != 3)
  102. {
  103. LOG_I("lcd_write_half_word error. %d", len);
  104. return -RT_ERROR;
  105. }
  106. else
  107. {
  108. return RT_EOK;
  109. }
  110. }
  111. static void lcd_gpio_init(void)
  112. {
  113. rt_hw_lcd_config();
  114. __HAL_RCC_GPIOC_CLK_ENABLE();
  115. rt_pin_mode(LCD_DC_PIN, PIN_MODE_OUTPUT);
  116. rt_pin_mode(LCD_BL_PIN, PIN_MODE_OUTPUT);
  117. rt_pin_mode(LCD_RES_PIN, PIN_MODE_OUTPUT);
  118. rt_pin_write(LCD_BL_PIN, PIN_LOW);
  119. }
  120. int rt_hw_spi_lcd_init(void)
  121. {
  122. __HAL_RCC_GPIOI_CLK_ENABLE();
  123. rt_hw_spi_device_attach("spi2", "spi20", GPIOI, GPIO_PIN_0);
  124. lcd_gpio_init();
  125. rt_pin_write(LCD_RES_PIN, PIN_HIGH);
  126. rt_thread_mdelay(10);
  127. rt_pin_write(LCD_RES_PIN, PIN_LOW);
  128. rt_thread_mdelay(50);
  129. rt_pin_write(LCD_RES_PIN, PIN_HIGH);
  130. rt_thread_mdelay(200);
  131. //************* Start Initial Sequence **********//
  132. lcd_write_cmd(0xE0);
  133. lcd_write_data(0x00);
  134. lcd_write_data(0x07);
  135. lcd_write_data(0x0f);
  136. lcd_write_data(0x0D);
  137. lcd_write_data(0x1B);
  138. lcd_write_data(0x0A);
  139. lcd_write_data(0x3c);
  140. lcd_write_data(0x78);
  141. lcd_write_data(0x4A);
  142. lcd_write_data(0x07);
  143. lcd_write_data(0x0E);
  144. lcd_write_data(0x09);
  145. lcd_write_data(0x1B);
  146. lcd_write_data(0x1e);
  147. lcd_write_data(0x0f);
  148. lcd_write_cmd(0xE1);
  149. lcd_write_data(0x00);
  150. lcd_write_data(0x22);
  151. lcd_write_data(0x24);
  152. lcd_write_data(0x06);
  153. lcd_write_data(0x12);
  154. lcd_write_data(0x07);
  155. lcd_write_data(0x36);
  156. lcd_write_data(0x47);
  157. lcd_write_data(0x47);
  158. lcd_write_data(0x06);
  159. lcd_write_data(0x0a);
  160. lcd_write_data(0x07);
  161. lcd_write_data(0x30);
  162. lcd_write_data(0x37);
  163. lcd_write_data(0x0f);
  164. lcd_write_cmd(0xC0);
  165. lcd_write_data(0x10);
  166. lcd_write_data(0x10);
  167. lcd_write_cmd(0xC1);
  168. lcd_write_data(0x41);
  169. lcd_write_cmd(0xC5);
  170. lcd_write_data(0x00);
  171. lcd_write_data(0x22);
  172. lcd_write_data(0x80);
  173. lcd_write_cmd(0x36);
  174. #ifndef LCD_HOR_SCREEN
  175. lcd_write_data(0x48);
  176. #else
  177. lcd_write_data(0x28);
  178. #endif
  179. lcd_write_cmd(0x3A); //Interface Mode Control
  180. lcd_write_data(0x66);
  181. lcd_write_cmd(0XB0); //Interface Mode Control
  182. lcd_write_data(0x00);
  183. lcd_write_cmd(0xB1); //Frame rate 70HZ
  184. lcd_write_data(0xB0);
  185. lcd_write_data(0x11);
  186. lcd_write_cmd(0xB4);
  187. lcd_write_data(0x02);
  188. lcd_write_cmd(0xB6); //RGB/MCU Interface Control
  189. lcd_write_data(0x02);
  190. lcd_write_data(0x02);
  191. lcd_write_cmd(0xB7);
  192. lcd_write_data(0xC6);
  193. lcd_write_cmd(0xE9);
  194. lcd_write_data(0x00);
  195. lcd_write_cmd(0XF7);
  196. lcd_write_data(0xA9);
  197. lcd_write_data(0x51);
  198. lcd_write_data(0x2C);
  199. lcd_write_data(0x82);
  200. lcd_fill(0, 0, LCD_WIDTH, LCD_HEIGHT, LCD_FULL_COLOR);
  201. lcd_write_cmd(0x11);
  202. rt_thread_mdelay(120);
  203. lcd_write_cmd(0x29);
  204. rt_thread_mdelay(50); //delay screen update to prevent screen appears white when the default color is black
  205. return RT_EOK;
  206. }
  207. /**
  208. * Set background color and foreground color
  209. *
  210. * @param back background color
  211. * @param fore fore color
  212. *
  213. * @return void
  214. */
  215. void lcd_set_color(rt_uint32_t back, rt_uint32_t fore)
  216. {
  217. BACK_COLOR = back;
  218. FORE_COLOR = fore;
  219. }
  220. void lcd_display_on(void)
  221. {
  222. rt_pin_write(LCD_BL_PIN, PIN_HIGH);
  223. }
  224. void lcd_display_off(void)
  225. {
  226. rt_pin_write(LCD_BL_PIN, PIN_LOW);
  227. }
  228. /* lcd enter the minimum power consumption mode and backlight off. */
  229. void lcd_enter_sleep(void)
  230. {
  231. lcd_display_off();
  232. rt_thread_mdelay(5);
  233. lcd_write_cmd(0x28);
  234. rt_thread_mdelay(10);
  235. lcd_write_cmd(0x10);
  236. rt_thread_mdelay(120);
  237. }
  238. /* lcd turn off sleep mode and backlight on. */
  239. void lcd_exit_sleep(void)
  240. {
  241. lcd_display_on();
  242. rt_thread_mdelay(5);
  243. lcd_write_cmd(0x11);
  244. rt_thread_mdelay(120);
  245. lcd_write_cmd(0x29);
  246. }
  247. /**
  248. * Set drawing area
  249. *
  250. * @param x1 start of x position
  251. * @param y1 start of y position
  252. * @param x2 end of x position
  253. * @param y2 end of y position
  254. *
  255. * @return void
  256. */
  257. void lcd_address_set(rt_uint16_t x1, rt_uint16_t y1, rt_uint16_t x2, rt_uint16_t y2)
  258. {
  259. lcd_write_cmd(0x2a);
  260. lcd_write_data(x1 >> 8);
  261. lcd_write_data(x1);
  262. lcd_write_data(x2 >> 8);
  263. lcd_write_data(x2);
  264. lcd_write_cmd(0x2b);
  265. lcd_write_data(y1 >> 8);
  266. lcd_write_data(y1);
  267. lcd_write_data(y2 >> 8);
  268. lcd_write_data(y2);
  269. lcd_write_cmd(0x2C);
  270. }
  271. /**
  272. * clear the lcd.
  273. *
  274. * @param color Fill color
  275. *
  276. * @return void
  277. */
  278. void lcd_clear(rt_uint32_t color)
  279. {
  280. rt_uint32_t i, j;
  281. rt_uint8_t data[3] = {0};
  282. rt_uint8_t *buf = RT_NULL;
  283. data[0] = color >> 16;
  284. data[1] = color >> 8;
  285. data[2] = color;
  286. lcd_address_set(0, 0, LCD_WIDTH - 1, LCD_HEIGHT - 1);
  287. buf = rt_malloc(LCD_BUF_SIZE);
  288. if (buf)
  289. {
  290. /* color is 16 bit */
  291. for (j = 0; j < LCD_BUF_SIZE / 3; j++)
  292. {
  293. buf[j * 3] = data[0];
  294. buf[j * 3 + 1] = data[1];
  295. buf[j * 3 + 2] = data[2];
  296. }
  297. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  298. rt_spi_send(spi_dev_lcd, buf, LCD_BUF_SIZE);
  299. rt_free(buf);
  300. }
  301. else
  302. {
  303. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  304. for (i = 0; i < LCD_WIDTH; i++)
  305. {
  306. for (j = 0; j < LCD_HEIGHT; j++)
  307. {
  308. rt_spi_send(spi_dev_lcd, data, 3);
  309. }
  310. }
  311. }
  312. }
  313. /**
  314. * display a point on the lcd using the given colour.
  315. *
  316. * @param x x position
  317. * @param y y position
  318. * @param color color of point
  319. *
  320. * @return void
  321. */
  322. void lcd_draw_point_color(rt_uint16_t x, rt_uint16_t y, rt_uint32_t color)
  323. {
  324. lcd_address_set(x, y, x, y);
  325. lcd_write_three_bytes(color);
  326. }
  327. /**
  328. * display a point on the lcd.
  329. *
  330. * @param x x position
  331. * @param y y position
  332. *
  333. * @return void
  334. */
  335. void lcd_draw_point(rt_uint16_t x, rt_uint16_t y)
  336. {
  337. lcd_draw_point_color(x, y, FORE_COLOR);
  338. }
  339. /**
  340. * full color on the lcd.
  341. *
  342. * @param x_start start of x position
  343. * @param y_start start of y position
  344. * @param x_end end of x position
  345. * @param y_end end of y position
  346. * @param color Fill color
  347. *
  348. * @return void
  349. */
  350. void lcd_fill(rt_uint16_t x_start, rt_uint16_t y_start, rt_uint16_t x_end, rt_uint16_t y_end, rt_uint32_t color)
  351. {
  352. rt_uint32_t i = 0, j = 0;
  353. rt_uint32_t size = 0, size_remain = 0;
  354. rt_uint8_t *fill_buf = RT_NULL;
  355. size = (x_end - x_start) * (y_end - y_start) * 3;
  356. if (size > LCD_CLEAR_SEND_NUMBER)
  357. {
  358. /* the number of remaining to be filled */
  359. size_remain = size - LCD_CLEAR_SEND_NUMBER;
  360. size = LCD_CLEAR_SEND_NUMBER;
  361. }
  362. lcd_address_set(x_start, y_start, x_end, y_end);
  363. fill_buf = (rt_uint8_t *)rt_malloc(LCD_CLEAR_SEND_NUMBER);
  364. if (fill_buf)
  365. {
  366. /* fast fill */
  367. while (1)
  368. {
  369. for (i = 0; i < size / 3; i++)
  370. {
  371. fill_buf[3 * i] = color >> 16;
  372. fill_buf[3 * i + 1] = color >> 8;
  373. fill_buf[3 * i + 2] = color;
  374. }
  375. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  376. rt_spi_send(spi_dev_lcd, fill_buf, size);
  377. /* Fill completed */
  378. if (size_remain == 0)
  379. break;
  380. /* calculate the number of fill next time */
  381. if (size_remain > LCD_CLEAR_SEND_NUMBER)
  382. {
  383. size_remain = size_remain - LCD_CLEAR_SEND_NUMBER;
  384. }
  385. else
  386. {
  387. size = size_remain;
  388. size_remain = 0;
  389. }
  390. }
  391. rt_free(fill_buf);
  392. }
  393. else
  394. {
  395. for (i = y_start; i <= y_end; i++)
  396. {
  397. for (j = x_start; j <= x_end; j++)lcd_write_three_bytes(color);
  398. }
  399. }
  400. }
  401. /**
  402. * full color array on the lcd.
  403. *
  404. * @param x_start start of x position
  405. * @param y_start start of y position
  406. * @param x_end end of x position
  407. * @param y_end end of y position
  408. * @param pcolor Fill color array's pointer
  409. *
  410. * @return void
  411. */
  412. void lcd_fill_array(rt_uint16_t x_start, rt_uint16_t y_start, rt_uint16_t x_end, rt_uint16_t y_end, void *pcolor)
  413. {
  414. rt_uint32_t size = 0;
  415. rt_uint8_t *array = RT_NULL;
  416. size = (x_end - x_start + 1) * (y_end - y_start + 1) * 3 /* 24bit */;
  417. array = (rt_uint8_t *)rt_malloc(size);
  418. if (!array) {
  419. LOG_E("not enough memory");
  420. return ;
  421. }
  422. rt_uint32_t *color_p = (rt_uint32_t *)pcolor;
  423. for (rt_uint16_t i = 0; i < size / 3; i++)
  424. {
  425. array[3 * i] = *color_p >> 16;
  426. array[3 * i + 1] = *color_p >> 8;
  427. array[3 * i + 2] = *color_p;
  428. color_p++;
  429. }
  430. lcd_address_set(x_start, y_start, x_end, y_end);
  431. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  432. rt_spi_send(spi_dev_lcd, array, size);
  433. rt_free(array);
  434. }
  435. /**
  436. * display a line on the lcd.
  437. *
  438. * @param x1 x1 position
  439. * @param y1 y1 position
  440. * @param x2 x2 position
  441. * @param y2 y2 position
  442. *
  443. * @return void
  444. */
  445. void lcd_draw_line(rt_uint16_t x1, rt_uint16_t y1, rt_uint16_t x2, rt_uint16_t y2)
  446. {
  447. rt_uint16_t t;
  448. rt_uint32_t i = 0;
  449. int xerr = 0, yerr = 0, delta_x, delta_y, distance;
  450. int incx, incy, row, col;
  451. if (y1 == y2)
  452. {
  453. /* fast draw transverse line */
  454. lcd_address_set(x1, y1, x2, y2);
  455. rt_uint8_t line_buf[960] = {0};
  456. for (i = 0; i < x2 - x1; i++)
  457. {
  458. line_buf[3 * i] = FORE_COLOR >> 16;
  459. line_buf[3 * i + 1] = FORE_COLOR >> 8;
  460. line_buf[3 * i + 2] = FORE_COLOR;
  461. }
  462. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  463. rt_spi_send(spi_dev_lcd, line_buf, (x2 - x1) * 3);
  464. return ;
  465. }
  466. delta_x = x2 - x1;
  467. delta_y = y2 - y1;
  468. row = x1;
  469. col = y1;
  470. if (delta_x > 0)incx = 1;
  471. else if (delta_x == 0)incx = 0;
  472. else
  473. {
  474. incx = -1;
  475. delta_x = -delta_x;
  476. }
  477. if (delta_y > 0)incy = 1;
  478. else if (delta_y == 0)incy = 0;
  479. else
  480. {
  481. incy = -1;
  482. delta_y = -delta_y;
  483. }
  484. if (delta_x > delta_y)distance = delta_x;
  485. else distance = delta_y;
  486. for (t = 0; t <= distance + 1; t++)
  487. {
  488. lcd_draw_point(row, col);
  489. xerr += delta_x ;
  490. yerr += delta_y ;
  491. if (xerr > distance)
  492. {
  493. xerr -= distance;
  494. row += incx;
  495. }
  496. if (yerr > distance)
  497. {
  498. yerr -= distance;
  499. col += incy;
  500. }
  501. }
  502. }
  503. /**
  504. * display a rectangle on the lcd.
  505. *
  506. * @param x1 x1 position
  507. * @param y1 y1 position
  508. * @param x2 x2 position
  509. * @param y2 y2 position
  510. *
  511. * @return void
  512. */
  513. void lcd_draw_rectangle(rt_uint16_t x1, rt_uint16_t y1, rt_uint16_t x2, rt_uint16_t y2)
  514. {
  515. lcd_draw_line(x1, y1, x2, y1);
  516. lcd_draw_line(x1, y1, x1, y2);
  517. lcd_draw_line(x1, y2, x2, y2);
  518. lcd_draw_line(x2, y1, x2, y2);
  519. }
  520. /**
  521. * display a circle on the lcd.
  522. *
  523. * @param x x position of Center
  524. * @param y y position of Center
  525. * @param r radius
  526. *
  527. * @return void
  528. */
  529. void lcd_draw_circle(rt_uint16_t x0, rt_uint16_t y0, rt_uint8_t r)
  530. {
  531. int a, b;
  532. int di;
  533. a = 0;
  534. b = r;
  535. di = 3 - (r << 1);
  536. while (a <= b)
  537. {
  538. lcd_draw_point(x0 - b, y0 - a);
  539. lcd_draw_point(x0 + b, y0 - a);
  540. lcd_draw_point(x0 - a, y0 + b);
  541. lcd_draw_point(x0 - b, y0 - a);
  542. lcd_draw_point(x0 - a, y0 - b);
  543. lcd_draw_point(x0 + b, y0 + a);
  544. lcd_draw_point(x0 + a, y0 - b);
  545. lcd_draw_point(x0 + a, y0 + b);
  546. lcd_draw_point(x0 - b, y0 + a);
  547. a++;
  548. //Bresenham
  549. if (di < 0)di += 4 * a + 6;
  550. else
  551. {
  552. di += 10 + 4 * (a - b);
  553. b--;
  554. }
  555. lcd_draw_point(x0 + a, y0 + b);
  556. }
  557. }
  558. static void lcd_show_char(rt_uint16_t x, rt_uint16_t y, rt_uint8_t data, rt_uint32_t size)
  559. {
  560. rt_uint8_t temp;
  561. rt_uint8_t num = 0;;
  562. rt_uint8_t pos, t;
  563. rt_uint32_t colortemp = FORE_COLOR;
  564. rt_uint8_t *font_buf = RT_NULL;
  565. if (x > LCD_WIDTH - size / 2 || y > LCD_HEIGHT - size)return;
  566. data = data - ' ';
  567. #ifdef ASC2_1608
  568. if (size == 16)
  569. {
  570. lcd_address_set(x, y, x + size / 2 - 1, y + size - 1);//(x,y,x+8-1,y+16-1)
  571. font_buf = (rt_uint8_t *)rt_malloc(size * size / 2 * 3);
  572. if (!font_buf)
  573. {
  574. /* fast show char */
  575. for (pos = 0; pos < size * (size / 2) / 8; pos++)
  576. {
  577. temp = asc2_1608[(rt_uint16_t)data * size * (size / 2) / 8 + pos];
  578. for (t = 0; t < 8; t++)
  579. {
  580. if (temp & 0x80)colortemp = FORE_COLOR;
  581. else colortemp = BACK_COLOR;
  582. lcd_write_three_bytes(colortemp);
  583. temp <<= 1;
  584. }
  585. }
  586. }
  587. else
  588. {
  589. for (pos = 0; pos < size * (size / 2) / 8; pos++)
  590. {
  591. temp = asc2_1608[(rt_uint16_t)data * size * (size / 2) / 8 + pos];
  592. for (t = 0; t < 8; t++)
  593. {
  594. if (temp & 0x80)colortemp = FORE_COLOR;
  595. else colortemp = BACK_COLOR;
  596. font_buf[3 * (8 * pos + t)] = colortemp >> 16;
  597. font_buf[3 * (8 * pos + t) + 1] = colortemp >> 8;
  598. font_buf[3 * (8 * pos + t) + 2] = colortemp;
  599. temp <<= 1;
  600. }
  601. }
  602. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  603. rt_spi_send(spi_dev_lcd, font_buf, size * size / 2 * 3);
  604. rt_free(font_buf);
  605. }
  606. }
  607. else
  608. #endif
  609. #ifdef ASC2_2412
  610. if (size == 24)
  611. {
  612. lcd_address_set(x, y, x + size / 2 - 1, y + size - 1);
  613. font_buf = (rt_uint8_t *)rt_malloc(size * size / 2 * 3);
  614. if (!font_buf)
  615. {
  616. /* fast show char */
  617. for (pos = 0; pos < (size * 16) / 8; pos++)
  618. {
  619. temp = asc2_2412[(rt_uint16_t)data * (size * 16) / 8 + pos];
  620. if (pos % 2 == 0)
  621. {
  622. num = 8;
  623. }
  624. else
  625. {
  626. num = 4;
  627. }
  628. for (t = 0; t < num; t++)
  629. {
  630. if (temp & 0x80)colortemp = FORE_COLOR;
  631. else colortemp = BACK_COLOR;
  632. lcd_write_three_bytes(colortemp);
  633. temp <<= 1;
  634. }
  635. }
  636. }
  637. else
  638. {
  639. for (pos = 0; pos < (size * 16) / 8; pos++)
  640. {
  641. temp = asc2_2412[(rt_uint16_t)data * (size * 16) / 8 + pos];
  642. if (pos % 2 == 0)
  643. {
  644. num = 8;
  645. }
  646. else
  647. {
  648. num = 4;
  649. }
  650. for (t = 0; t < num; t++)
  651. {
  652. if (temp & 0x80)colortemp = FORE_COLOR;
  653. else colortemp = BACK_COLOR;
  654. if (num == 8)
  655. {
  656. font_buf[3 * (12 * (pos / 2) + t)] = colortemp >> 16;
  657. font_buf[3 * (12 * (pos / 2) + t) + 1] = colortemp >> 8;
  658. font_buf[3 * (12 * (pos / 2) + t) + 2] = colortemp;
  659. }
  660. else
  661. {
  662. font_buf[3 * (8 + 12 * (pos / 2) + t)] = colortemp >> 16;
  663. font_buf[3 * (8 + 12 * (pos / 2) + t) + 1] = colortemp >> 8;
  664. font_buf[3 * (8 + 12 * (pos / 2) + t) + 2] = colortemp;
  665. }
  666. temp <<= 1;
  667. }
  668. }
  669. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  670. rt_spi_send(spi_dev_lcd, font_buf, size * size / 2 * 3);
  671. rt_free(font_buf);
  672. }
  673. }
  674. else
  675. #endif
  676. #ifdef ASC2_3216
  677. if (size == 32)
  678. {
  679. lcd_address_set(x, y, x + size / 2 - 1, y + size - 1);
  680. font_buf = (rt_uint8_t *)rt_malloc(size * size / 2 * 3);
  681. if (!font_buf)
  682. {
  683. /* fast show char */
  684. for (pos = 0; pos < size * (size / 2) / 8; pos++)
  685. {
  686. temp = asc2_3216[(rt_uint16_t)data * size * (size / 2) / 8 + pos];
  687. for (t = 0; t < 8; t++)
  688. {
  689. if (temp & 0x80)colortemp = FORE_COLOR;
  690. else colortemp = BACK_COLOR;
  691. lcd_write_three_bytes(colortemp);
  692. temp <<= 1;
  693. }
  694. }
  695. }
  696. else
  697. {
  698. for (pos = 0; pos < size * (size / 2) / 8; pos++)
  699. {
  700. temp = asc2_3216[(rt_uint16_t)data * size * (size / 2) / 8 + pos];
  701. for (t = 0; t < 8; t++)
  702. {
  703. if (temp & 0x80)colortemp = FORE_COLOR;
  704. else colortemp = BACK_COLOR;
  705. font_buf[3 * (8 * pos + t)] = colortemp >> 16;
  706. font_buf[3 * (8 * pos + t) + 1] = colortemp >> 8;
  707. font_buf[3 * (8 * pos + t) + 2] = colortemp;
  708. temp <<= 1;
  709. }
  710. }
  711. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  712. rt_spi_send(spi_dev_lcd, font_buf, size * size / 2 * 3);
  713. rt_free(font_buf);
  714. }
  715. }
  716. else
  717. #endif
  718. {
  719. LOG_E("There is no any define ASC2_1208 && ASC2_2412 && ASC2_2416 && ASC2_3216 !");
  720. }
  721. }
  722. /**
  723. * display the number on the lcd.
  724. *
  725. * @param x x position
  726. * @param y y position
  727. * @param num number
  728. * @param len length of number
  729. * @param size size of font
  730. *
  731. * @return void
  732. */
  733. void lcd_show_num(rt_uint16_t x, rt_uint16_t y, rt_uint32_t num, rt_uint8_t len, rt_uint32_t size)
  734. {
  735. lcd_show_string(x, y, size, "%d", num);
  736. }
  737. /**
  738. * display the string on the lcd.
  739. *
  740. * @param x x position
  741. * @param y y position
  742. * @param size size of font
  743. * @param p the string to be display
  744. *
  745. * @return 0: display success
  746. * -1: size of font is not support
  747. */
  748. rt_err_t lcd_show_string(rt_uint16_t x, rt_uint16_t y, rt_uint32_t size, const char *fmt, ...)
  749. {
  750. #define LCD_STRING_BUF_LEN 128
  751. va_list args;
  752. rt_uint8_t buf[LCD_STRING_BUF_LEN] = {0};
  753. rt_uint8_t *p = RT_NULL;
  754. if (size != 16 && size != 24 && size != 32)
  755. {
  756. LOG_E("font size(%d) is not support!", size);
  757. return -RT_ERROR;
  758. }
  759. va_start(args, fmt);
  760. rt_vsnprintf((char *)buf, 100, (const char *)fmt, args);
  761. va_end(args);
  762. p = buf;
  763. while (*p != '\0')
  764. {
  765. if (x > LCD_WIDTH - size / 2)
  766. {
  767. x = 0;
  768. y += size;
  769. }
  770. if (y > LCD_HEIGHT - size)
  771. {
  772. y = x = 0;
  773. lcd_clear(RED);
  774. }
  775. lcd_show_char(x, y, *p, size);
  776. x += size / 2;
  777. p++;
  778. }
  779. return RT_EOK;
  780. }
  781. /**
  782. * display the image on the lcd.
  783. *
  784. * @param x x position
  785. * @param y y position
  786. * @param length length of image
  787. * @param wide wide of image
  788. * @param p image
  789. *
  790. * @return 0: display success
  791. * -1: the image is too large
  792. */
  793. rt_err_t lcd_show_image(rt_uint16_t x, rt_uint16_t y, rt_uint16_t length, rt_uint16_t wide, const rt_uint8_t *p)
  794. {
  795. RT_ASSERT(p);
  796. if (x + length > LCD_WIDTH || y + wide > LCD_HEIGHT)
  797. {
  798. return -RT_ERROR;
  799. }
  800. lcd_address_set(x, y, x + length - 1, y + wide - 1);
  801. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  802. rt_spi_send(spi_dev_lcd, p, length * wide * 3);
  803. return RT_EOK;
  804. }
  805. struct drv_lcd_device
  806. {
  807. struct rt_device parent;
  808. struct rt_device_graphic_info lcd_info;
  809. struct rt_semaphore lcd_lock;
  810. /* 0:front_buf is being used 1: back_buf is being used*/
  811. rt_uint8_t cur_buf;
  812. rt_uint8_t *front_buf;
  813. rt_uint8_t *back_buf;
  814. };
  815. struct drv_lcd_device _lcd;
  816. static rt_err_t drv_lcd_init(struct rt_device *device)
  817. {
  818. struct drv_lcd_device *lcd = LCD_DEVICE(device);
  819. /* nothing, right now */
  820. lcd = lcd;
  821. return RT_EOK;
  822. }
  823. static rt_err_t drv_lcd_control(struct rt_device *device, int cmd, void *args)
  824. {
  825. struct drv_lcd_device *lcd = LCD_DEVICE(device);
  826. struct rt_device_rect_info *rect_info = RT_NULL;
  827. uint32_t data_start_addr = 0;
  828. uint32_t i = 0;
  829. switch (cmd)
  830. {
  831. case RTGRAPHIC_CTRL_RECT_UPDATE:
  832. {
  833. /* update */
  834. rect_info = (struct rt_device_rect_info *)args;
  835. if(rect_info != NULL)
  836. {
  837. data_start_addr = rect_info->y * LCD_BYTES_PER_PIXEL * LCD_WIDTH + rect_info->x * LCD_BYTES_PER_PIXEL;
  838. for(i = 0; i < rect_info->height; i++)
  839. {
  840. memcpy(&_lcd.front_buf[i * LCD_BYTES_PER_PIXEL * rect_info->width], &_lcd.lcd_info.framebuffer[data_start_addr + i * LCD_BYTES_PER_PIXEL * LCD_WIDTH], rect_info->width * LCD_BYTES_PER_PIXEL);
  841. }
  842. lcd_show_image(rect_info->x, rect_info->y, rect_info->width, rect_info->height, _lcd.front_buf);
  843. }
  844. else
  845. {
  846. memcpy(_lcd.front_buf, _lcd.lcd_info.framebuffer, LCD_BUF_SIZE);
  847. lcd_show_image(0, 0, LCD_WIDTH, LCD_HEIGHT, _lcd.front_buf);
  848. }
  849. }
  850. break;
  851. case RTGRAPHIC_CTRL_GET_INFO:
  852. {
  853. struct rt_device_graphic_info *info = (struct rt_device_graphic_info *)args;
  854. RT_ASSERT(info != RT_NULL);
  855. info->pixel_format = lcd->lcd_info.pixel_format;
  856. info->bits_per_pixel = lcd->lcd_info.bits_per_pixel;
  857. info->width = lcd->lcd_info.width;
  858. info->height = lcd->lcd_info.height;
  859. info->framebuffer = lcd->lcd_info.framebuffer;
  860. }
  861. break;
  862. }
  863. return RT_EOK;
  864. }
  865. #if defined(LCD_BACKLIGHT_USING_GPIO)
  866. void turn_on_lcd_backlight(void)
  867. {
  868. rt_pin_mode(LCD_BL_PIN, PIN_MODE_OUTPUT);
  869. rt_pin_write(LCD_BL_PIN, PIN_HIGH);
  870. }
  871. #else
  872. void turn_on_lcd_backlight(void)
  873. {
  874. }
  875. #endif
  876. #ifdef RT_USING_DEVICE_OPS
  877. const static struct rt_device_ops lcd_ops =
  878. {
  879. drv_lcd_init,
  880. RT_NULL,
  881. RT_NULL,
  882. RT_NULL,
  883. RT_NULL,
  884. drv_lcd_control
  885. };
  886. #endif
  887. int drv_lcd_ili9488_hw_init(void)
  888. {
  889. rt_err_t result = RT_EOK;
  890. struct rt_device *device = &_lcd.parent;
  891. /* memset _lcd to zero */
  892. memset(&_lcd, 0x00, sizeof(_lcd));
  893. /* init lcd_lock semaphore */
  894. result = rt_sem_init(&_lcd.lcd_lock, "lcd_lock", 0, RT_IPC_FLAG_FIFO);
  895. if (result != RT_EOK)
  896. {
  897. LOG_E("init semaphore failed!\n");
  898. result = -RT_ENOMEM;
  899. goto __exit;
  900. }
  901. /* config LCD dev info */
  902. _lcd.lcd_info.height = LCD_HEIGHT;
  903. _lcd.lcd_info.width = LCD_WIDTH;
  904. _lcd.lcd_info.bits_per_pixel = LCD_BITS_PER_PIXEL;
  905. _lcd.lcd_info.pixel_format = LCD_PIXEL_FORMAT;
  906. /* malloc memory for double Buffering */
  907. _lcd.lcd_info.framebuffer = rt_malloc(LCD_BUF_SIZE);
  908. _lcd.front_buf = rt_malloc(LCD_BUF_SIZE);
  909. if (_lcd.lcd_info.framebuffer == RT_NULL || _lcd.front_buf == RT_NULL)
  910. {
  911. LOG_E("init frame buffer failed!\n");
  912. result = -RT_ENOMEM;
  913. goto __exit;
  914. }
  915. /* memset buff to 0xFF */
  916. memset(_lcd.lcd_info.framebuffer, 0xFF, LCD_BUF_SIZE);
  917. memset(_lcd.front_buf, 0xFF, LCD_BUF_SIZE);
  918. device->type = RT_Device_Class_Graphic;
  919. #ifdef RT_USING_DEVICE_OPS
  920. device->ops = &lcd_ops;
  921. #else
  922. device->init = drv_lcd_init;
  923. device->control = drv_lcd_control;
  924. #endif
  925. /* register lcd device */
  926. rt_device_register(device, "lcd", RT_DEVICE_FLAG_RDWR);
  927. /* init spi lcd */
  928. if (rt_hw_spi_lcd_init() != RT_EOK)
  929. {
  930. result = -RT_ERROR;
  931. goto __exit;
  932. }
  933. else
  934. {
  935. turn_on_lcd_backlight();
  936. }
  937. #if defined(PKG_USING_GUIENGINE)
  938. rtgui_graphic_set_device(device);
  939. #endif
  940. __exit:
  941. if (result != RT_EOK)
  942. {
  943. rt_sem_detach(&_lcd.lcd_lock);
  944. if (_lcd.lcd_info.framebuffer)
  945. {
  946. rt_free(_lcd.lcd_info.framebuffer);
  947. }
  948. if (_lcd.back_buf)
  949. {
  950. rt_free(_lcd.back_buf);
  951. }
  952. if (_lcd.front_buf)
  953. {
  954. rt_free(_lcd.front_buf);
  955. }
  956. }
  957. return result;
  958. }
  959. INIT_COMPONENT_EXPORT(drv_lcd_ili9488_hw_init);
  960. #ifdef DRV_DEBUG
  961. #ifdef FINSH_USING_MSH
  962. int ili9488_test()
  963. {
  964. struct drv_lcd_device *lcd;
  965. lcd = (struct drv_lcd_device *)rt_device_find("lcd");
  966. struct rt_device_rect_info rect_info = {0, 0, LCD_WIDTH, LCD_HEIGHT};
  967. uint32_t i = 0;
  968. lcd_clear(WHITE);
  969. lcd_show_image(0, 0, 240, 69, image_rttlogo);
  970. lcd_set_color(WHITE, BLACK);
  971. lcd_show_string(10, 69, 16, "Hello, RT-Thread!");
  972. lcd_show_string(10, 69+16, 24, "RT-Thread");
  973. lcd_show_string(10, 69+16+24, 32, "RT-Thread");
  974. lcd_draw_line(0, 69+16+24+32, LCD_H, 69+16+24+32);
  975. lcd_draw_point(160, 310);
  976. for (int i = 0; i < 150; i += 4)
  977. {
  978. lcd_draw_circle(160, 310, i);
  979. }
  980. rt_thread_mdelay(2000);
  981. while (1)
  982. {
  983. i += 10;
  984. if(i >= 120)
  985. {
  986. i = 0;
  987. }
  988. rect_info.x = i;
  989. rect_info.y = i;
  990. rect_info.width =LCD_WIDTH - 2 * i;
  991. rect_info.height =LCD_HEIGHT - 2 * i;
  992. /* red */
  993. for (int i = 0; i < LCD_BUF_SIZE / 3; i++)
  994. {
  995. lcd->lcd_info.framebuffer[3 * i] = 0xFF;
  996. lcd->lcd_info.framebuffer[3 * i + 1] = 0x00;
  997. lcd->lcd_info.framebuffer[3 * i + 2] = 0x00;
  998. }
  999. lcd->parent.control(&lcd->parent, RTGRAPHIC_CTRL_RECT_UPDATE, &rect_info);
  1000. rt_thread_mdelay(1000);
  1001. /* green */
  1002. for (int i = 0; i < LCD_BUF_SIZE / 3; i++)
  1003. {
  1004. lcd->lcd_info.framebuffer[3 * i] = 0x00;
  1005. lcd->lcd_info.framebuffer[3 * i + 1] = 0xFF;
  1006. lcd->lcd_info.framebuffer[3 * i + 2] = 0x00;
  1007. }
  1008. lcd->parent.control(&lcd->parent, RTGRAPHIC_CTRL_RECT_UPDATE, &rect_info);
  1009. rt_thread_mdelay(1000);
  1010. /* blue */
  1011. for (int i = 0; i < LCD_BUF_SIZE / 3; i++)
  1012. {
  1013. lcd->lcd_info.framebuffer[3 * i] = 0x00;
  1014. lcd->lcd_info.framebuffer[3 * i + 1] = 0x00;
  1015. lcd->lcd_info.framebuffer[3 * i + 2] = 0xFF;
  1016. }
  1017. lcd->parent.control(&lcd->parent, RTGRAPHIC_CTRL_RECT_UPDATE, &rect_info);
  1018. rt_thread_mdelay(1000);
  1019. }
  1020. }
  1021. MSH_CMD_EXPORT(ili9488_test, test ili9488 driver);
  1022. #endif /* FINSH_USING_MSH */
  1023. #endif /* DRV_DEBUG */
  1024. #endif /* BSP_USING_SPI_LCD_ILI9488 */