dev_lcd.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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. * 2011-12-16 onelife Initial creation of address mapped method (pixel
  9. * drive) for EFM32GG_DK3750 board
  10. * 2011-12-29 onelife Add direct drive method (frame buffer) support
  11. */
  12. /***************************************************************************//**
  13. * @addtogroup EFM32GG_DK3750
  14. * @{
  15. ******************************************************************************/
  16. /* Includes ------------------------------------------------------------------*/
  17. #include "board.h"
  18. #include "drv_usart.h"
  19. #include "dev_lcd.h"
  20. #if defined(EFM32_USING_LCD)
  21. #if (!defined(LCD_MAPPED) && !defined(LCD_DIRECT))
  22. #error "Unknown LCD access mode"
  23. #endif
  24. #include <rtgui/rtgui.h>
  25. #include <rtgui/driver.h>
  26. #include <dmd_ssd2119.h>
  27. /* Private typedef -----------------------------------------------------------*/
  28. /* Private define ------------------------------------------------------------*/
  29. /* Private macro -------------------------------------------------------------*/
  30. #ifdef EFM32_LCD_DEBUG
  31. #define lcd_debug(format,args...) rt_kprintf(format, ##args)
  32. #else
  33. #define lcd_debug(format,args...)
  34. #endif
  35. /* Private function prototypes -----------------------------------------------*/
  36. #if defined(LCD_MAPPED)
  37. static void efm32_spiLcd_setPixel(rtgui_color_t *c, int x, int y);
  38. static void efm32_spiLcd_getPixel(rtgui_color_t *c, int x, int y);
  39. static void efm32_spiLcd_drawRawHLine(rt_uint8_t *pixels, int x1, int x2, int y);
  40. static void efm32_spiLcd_drawHLine(rtgui_color_t *c, int x1, int x2, int y);
  41. static void efm32_spiLcd_drawVLine(rtgui_color_t *c, int x1, int x2, int y);
  42. #endif
  43. /* Private variables ---------------------------------------------------------*/
  44. static rt_device_t lcd;
  45. static struct rt_device lcd_device;
  46. static rt_bool_t lcdAutoCs = true;
  47. static struct rt_device_graphic_info lcd_info;
  48. #if defined(LCD_MAPPED)
  49. static const struct rtgui_graphic_driver_ops lcd_ops =
  50. {
  51. efm32_spiLcd_setPixel,
  52. efm32_spiLcd_getPixel,
  53. efm32_spiLcd_drawHLine,
  54. efm32_spiLcd_drawVLine,
  55. efm32_spiLcd_drawRawHLine
  56. };
  57. /* Private functions ---------------------------------------------------------*/
  58. /***************************************************************************//**
  59. * @brief
  60. * Draw a pixel with specified color
  61. *
  62. * @details
  63. *
  64. * @note
  65. *
  66. * @param[in] c
  67. * Pointer to color
  68. *
  69. * @param[in] x
  70. * Horizontal position
  71. *
  72. * @param[in] y
  73. * Vertical position
  74. ******************************************************************************/
  75. static void efm32_spiLcd_setPixel(rtgui_color_t *c, int x, int y)
  76. {
  77. rt_uint32_t ret = RT_EOK;
  78. do
  79. {
  80. /* Check if pixel is outside clipping region */
  81. if ((x < 0) || (x > lcd_info.width))
  82. {
  83. break;
  84. }
  85. if ((y < 0) || (y > lcd_info.height))
  86. {
  87. break;
  88. }
  89. /* Write color */
  90. ret = DMD_writePixel((rt_uint16_t)x, (rt_uint16_t)y, (rt_uint16_t)*c, 1);
  91. if (ret != 0)
  92. {
  93. break;
  94. }
  95. return;
  96. } while(0);
  97. // lcd_debug("LCD err: Set pixel at (%d,%d: %x) failed (%x)!\n", x, y, *c, ret);
  98. }
  99. /***************************************************************************//**
  100. * @brief
  101. * Get the color of a pixel
  102. *
  103. * @details
  104. *
  105. * @note
  106. *
  107. * @param[out] c
  108. * Pointer to color
  109. *
  110. * @param[in] x
  111. * Horizontal position
  112. *
  113. * @param[in] y
  114. * Vertical position
  115. ******************************************************************************/
  116. static void efm32_spiLcd_getPixel(rtgui_color_t *c, int x, int y)
  117. {
  118. rt_uint32_t ret = RT_EOK;
  119. do
  120. {
  121. /* Check if pixel is outside clipping region */
  122. if ((x < 0) || (x > lcd_info.width))
  123. {
  124. break;
  125. }
  126. if ((y < 0) || (y > lcd_info.height))
  127. {
  128. break;
  129. }
  130. /* Read color */
  131. ret = DMD_readPixel((rt_uint16_t)x, (rt_uint16_t)y, (rt_uint16_t *)c);
  132. if (ret != 0)
  133. {
  134. break;
  135. }
  136. return;
  137. } while(0);
  138. lcd_debug("LCD err: Get pixel at (%d,%d: %x) failed (%x)!\n",
  139. x, y, *c, ret);
  140. }
  141. /***************************************************************************//**
  142. * @brief
  143. * Draw a horizontal line with raw color
  144. *
  145. * @details
  146. *
  147. * @note
  148. *
  149. * @param[in] pixels
  150. * Pointer to raw color
  151. *
  152. * @param[in] x1
  153. * Horizontal start position
  154. *
  155. * @param[in] x2
  156. * Horizontal end position
  157. *
  158. * @param[in] y
  159. * Vertical position
  160. ******************************************************************************/
  161. static void efm32_spiLcd_drawRawHLine(rt_uint8_t *pixels, int x1, int x2, int y)
  162. {
  163. lcd_debug("LCD: RAW H LINE!\n");
  164. }
  165. /***************************************************************************//**
  166. * @brief
  167. * Draw a horizontal line with specified color
  168. *
  169. * @details
  170. *
  171. * @note
  172. *
  173. * @param[in] c
  174. * Pointer to color
  175. *
  176. * @param[in] x1
  177. * Horizontal start position
  178. *
  179. * @param[in] x2
  180. * Horizontal end position
  181. *
  182. * @param[in] y
  183. * Vertical position
  184. ******************************************************************************/
  185. static void efm32_spiLcd_drawHLine(rtgui_color_t *c, int x1, int x2, int y)
  186. {
  187. rt_uint32_t ret = RT_EOK;
  188. do
  189. {
  190. /* Check if line is outside of clipping region */
  191. if ((y < 0) || (y > lcd_info.height))
  192. {
  193. break;
  194. }
  195. /* Swap the coordinates if x1 is larger than x2 */
  196. if (x1 > x2)
  197. {
  198. int swap;
  199. swap = x1;
  200. x1 = x2;
  201. x2 = swap;
  202. }
  203. /* Check if entire line is outside clipping region */
  204. if ((x1 > lcd_info.width) || (x2 < 0))
  205. {
  206. /* Nothing to draw */
  207. break;
  208. }
  209. /* Clip the line if necessary */
  210. if (x1 < 0)
  211. {
  212. x1 = 0;
  213. }
  214. if (x2 > lcd_info.width)
  215. {
  216. x2 = lcd_info.width;
  217. }
  218. /* Write color */
  219. rt_uint32_t length = x2 - x1 + 1;
  220. ret = DMD_writePixel((rt_uint16_t)x1, (rt_uint16_t)y,
  221. (rt_uint16_t)*c, length);
  222. if (ret != 0)
  223. {
  224. break;
  225. }
  226. return;
  227. } while(0);
  228. // lcd_debug("LCD err: Draw hline at (%d-%d,%d: %x) failed (%x)!\n", x1, x2, y, *c, ret);
  229. }
  230. /***************************************************************************//**
  231. * @brief
  232. * Draw a vertical line with specified color
  233. *
  234. * @details
  235. *
  236. * @note
  237. *
  238. * @param[in] c
  239. * Pointer to color
  240. *
  241. * @param[in] x
  242. * Horizontal position
  243. *
  244. * @param[in] y1
  245. * Vertical start position
  246. *
  247. * @param[in] y2
  248. * Vertical end position
  249. ******************************************************************************/
  250. static void efm32_spiLcd_drawVLine(rtgui_color_t *c, int x , int y1, int y2)
  251. {
  252. rt_uint32_t ret = RT_EOK;
  253. do
  254. {
  255. /* Check if line is outside of clipping region */
  256. if ((x < 0) || (x > lcd_info.width))
  257. {
  258. break;
  259. }
  260. /* Swap the coordinates if y1 is larger than y2 */
  261. if (y1 > y2)
  262. {
  263. rt_uint16_t swap;
  264. swap = y1;
  265. y1 = y2;
  266. y2 = swap;
  267. }
  268. /* Check if entire line is outside clipping region */
  269. if ((y1 > lcd_info.height) || (y2 < 0))
  270. {
  271. /* Nothing to draw */
  272. break;
  273. }
  274. /* Clip the line if necessary */
  275. if (y1 < 0)
  276. {
  277. y1 = 0;
  278. }
  279. if (y2 > lcd_info.height)
  280. {
  281. y2 = lcd_info.height;
  282. }
  283. /* Set clipping area */
  284. rt_uint16_t length = y2 - y1 + 1;
  285. ret = DMD_setClippingArea((rt_uint16_t)x, (rt_uint16_t)y1, 1, length);
  286. if (ret != DMD_OK)
  287. {
  288. break;
  289. }
  290. /* Write color */
  291. ret= DMD_writePixel(0, 0, (rt_uint16_t)*c, length);
  292. if (ret != DMD_OK)
  293. {
  294. break;
  295. }
  296. /* Reset clipping area */
  297. ret = DMD_setClippingArea(0, 0, lcd_info.width, lcd_info.height);
  298. if (ret != DMD_OK)
  299. {
  300. break;
  301. }
  302. return;
  303. } while(0);
  304. // lcd_debug("LCD err: Draw vline at (%d,%d-%d: %x) failed (%x)!\n", x, y1, y2, *c, ret);
  305. }
  306. #endif
  307. /***************************************************************************//**
  308. * @brief
  309. * Configure LCD device
  310. *
  311. * @details
  312. *
  313. * @note
  314. *
  315. * @param[in] dev
  316. * Pointer to device descriptor
  317. *
  318. * @param[in] cmd
  319. * IIC control command
  320. *
  321. * @param[in] args
  322. * Arguments
  323. *
  324. * @return
  325. * Error code
  326. ******************************************************************************/
  327. static rt_err_t efm32_spiLcd_control (rt_device_t dev, int cmd, void *args)
  328. {
  329. switch (cmd)
  330. {
  331. case RTGRAPHIC_CTRL_RECT_UPDATE:
  332. break;
  333. case RTGRAPHIC_CTRL_POWERON:
  334. break;
  335. case RTGRAPHIC_CTRL_POWEROFF:
  336. break;
  337. case RTGRAPHIC_CTRL_GET_INFO:
  338. rt_memcpy(args, &lcd_info, sizeof(struct rt_device_graphic_info));
  339. break;
  340. case RTGRAPHIC_CTRL_SET_MODE:
  341. break;
  342. }
  343. return RT_EOK;
  344. }
  345. /***************************************************************************//**
  346. * @brief
  347. * Set/Clear chip select
  348. *
  349. * @details
  350. *
  351. * @note
  352. *
  353. * @param[in] enable
  354. * Chip select pin setting
  355. ******************************************************************************/
  356. static void efm32_spiLcd_cs(rt_uint8_t enable)
  357. {
  358. if (!lcdAutoCs)
  359. {
  360. if (enable)
  361. {
  362. GPIO_PinOutClear(LCD_CS_PORT, LCD_CS_PIN);
  363. }
  364. else
  365. {
  366. GPIO_PinOutSet(LCD_CS_PORT, LCD_CS_PIN);
  367. }
  368. }
  369. }
  370. /***************************************************************************//**
  371. * @brief
  372. * Write data to SSD2119 controller
  373. *
  374. * @param[in] reg
  375. * Register to write to
  376. *
  377. * @param[in] data
  378. * 16-bit data to write into register
  379. *
  380. * @note
  381. * It's not possible to read back register value through SSD2119 SPI interface
  382. ******************************************************************************/
  383. rt_err_t efm32_spiLcd_writeRegister(rt_uint8_t reg, rt_uint16_t data)
  384. {
  385. struct efm32_usart_device_t *usart;
  386. rt_uint8_t buf_ins[3];
  387. rt_uint8_t buf_res[3];
  388. RT_ASSERT(lcd != RT_NULL);
  389. usart = (struct efm32_usart_device_t *)(lcd->user_data);
  390. /* Build instruction buffer */
  391. buf_res[0] = (data & 0xff00) >> 8;
  392. buf_res[1] = data & 0x00ff;
  393. buf_ins[0] = 1; /* Instruction length */
  394. buf_ins[1] = reg; /* Instruction */
  395. *(rt_uint8_t **)(&buf_ins[2]) = buf_res; /* Data */
  396. efm32_spiLcd_cs(1);
  397. if (lcd->write(lcd, EFM32_NO_DATA, buf_ins, 2) == 0)
  398. {
  399. lcd_debug("LCD: Write data failed!\n");
  400. return -RT_ERROR;
  401. }
  402. efm32_spiLcd_cs(0);
  403. return RT_EOK;
  404. }
  405. /***************************************************************************//**
  406. * @brief
  407. * Register LCD device
  408. *
  409. * @details
  410. *
  411. * @note
  412. *
  413. * @param[in] device
  414. * Pointer to device descriptor
  415. *
  416. * @param[in] name
  417. * Device name
  418. *
  419. * @param[in] flag
  420. * Configuration flags
  421. *
  422. * @param[in] iic
  423. * Pointer to IIC device descriptor
  424. *
  425. * @return
  426. * Error code
  427. ******************************************************************************/
  428. rt_err_t efm32_spiLcd_register(
  429. rt_device_t device,
  430. const char *name,
  431. rt_uint32_t flag,
  432. void *data)
  433. {
  434. RT_ASSERT(device != RT_NULL);
  435. device->type = RT_Device_Class_Graphic;
  436. device->rx_indicate = RT_NULL;
  437. device->tx_complete = RT_NULL;
  438. device->init = RT_NULL;
  439. device->open = RT_NULL;
  440. device->close = RT_NULL;
  441. device->read = RT_NULL;
  442. device->write = RT_NULL;
  443. device->control = efm32_spiLcd_control;
  444. device->user_data = data;
  445. /* register a character device */
  446. return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | flag);
  447. }
  448. /***************************************************************************//**
  449. * @brief
  450. * Initialize LCD device
  451. *
  452. * @details
  453. *
  454. * @note
  455. *
  456. ******************************************************************************/
  457. void efm32_spiLcd_init(void)
  458. {
  459. struct efm32_usart_device_t *usart;
  460. rt_uint32_t flag;
  461. DMD_DisplayGeometry *geometry;
  462. rt_uint32_t ret;
  463. do
  464. {
  465. USART_InitSync_TypeDef init = USART_INITSYNC_DEFAULT;
  466. /* Find SPI device */
  467. lcd = rt_device_find(LCD_USING_DEVICE_NAME);
  468. if (lcd == RT_NULL)
  469. {
  470. lcd_debug("LCD err: Can't find %s!\n", LCD_USING_DEVICE_NAME);
  471. break;
  472. }
  473. lcd_debug("LCD: Find device %s\n", LCD_USING_DEVICE_NAME);
  474. /* Config CS pin */
  475. usart = (struct efm32_usart_device_t *)(lcd->user_data);
  476. if (!(usart->state & USART_STATE_AUTOCS))
  477. {
  478. GPIO_PinModeSet(LCD_CS_PORT, LCD_CS_PIN, gpioModePushPull, 1);
  479. lcdAutoCs = false;
  480. }
  481. /* TFT initialize or reinitialize. Assumes EBI has been configured
  482. correctly in DVK_init(DVK_Init_EBI) */
  483. rt_uint32_t freq = SystemCoreClockGet();
  484. rt_uint32_t i;
  485. rt_bool_t warning = RT_FALSE;
  486. /* If we are in BC_UIF_AEM_EFM state, we can redraw graphics */
  487. while (DVK_readRegister(&BC_REGISTER->UIF_AEM) != BC_UIF_AEM_EFM)
  488. {
  489. if (!warning)
  490. {
  491. lcd_debug("LCD: Please press AEM button!!!\n");
  492. warning = RT_TRUE;
  493. }
  494. }
  495. lcd_debug("LCD: Got LCD control\n");
  496. /* If we're not BC_ARB_CTRL_EBI state, we need to reconfigure display controller */
  497. if (DVK_readRegister(&BC_REGISTER->ARB_CTRL) != BC_ARB_CTRL_EBI)
  498. {
  499. lcd_debug("LCD: Set to EBI mode\n");
  500. /* Configure for EBI mode and reset display */
  501. DVK_displayControl(DVK_Display_EBI);
  502. DVK_displayControl(DVK_Display_ResetAssert);
  503. DVK_displayControl(DVK_Display_PowerDisable);
  504. /* Short delay */
  505. freq = SystemCoreClockGet();
  506. for(i = 0; i < (freq / 100); i++)
  507. {
  508. __NOP();
  509. }
  510. #if defined(LCD_MAPPED)
  511. /* Configure display for address mapped method + 3-wire SPI mode */
  512. DVK_displayControl(DVK_Display_Mode8080);
  513. DVK_displayControl(DVK_Display_PowerEnable);
  514. DVK_displayControl(DVK_Display_ResetRelease);
  515. /* Initialize graphics - abort on failure */
  516. ret = DMD_init(BC_SSD2119_BASE, BC_SSD2119_BASE + 2);
  517. if (ret == DMD_OK)
  518. {
  519. /* Make sure display is configured with correct rotation */
  520. DMD_flipDisplay(1, 1);
  521. }
  522. else if (ret != DMD_ERROR_DRIVER_ALREADY_INITIALIZED)
  523. {
  524. lcd_debug("LCD err: driver init failed %x\n", ret);
  525. break;
  526. }
  527. #elif defined(LCD_DIRECT)
  528. /* Configure TFT direct drive method from EBI BANK2 */
  529. const EBI_TFTInit_TypeDef tftInit =
  530. {
  531. ebiTFTBank2, /* Select EBI Bank 2 */
  532. ebiTFTWidthHalfWord, /* Select 2-byte (16-bit RGB565) increments */
  533. ebiTFTColorSrcMem, /* Use memory as source for mask/blending */
  534. ebiTFTInterleaveUnlimited, /* Unlimited interleaved accesses */
  535. ebiTFTFrameBufTriggerVSync, /* VSYNC as frame buffer update trigger */
  536. false, /* Drive DCLK from negative edge of internal clock */
  537. ebiTFTMBDisabled, /* No masking and alpha blending enabled */
  538. ebiTFTDDModeExternal, /* Drive from external memory */
  539. ebiActiveLow, /* CS Active Low polarity */
  540. ebiActiveHigh, /* DCLK Active High polarity */
  541. ebiActiveLow, /* DATAEN Active Low polarity */
  542. ebiActiveLow, /* HSYNC Active Low polarity */
  543. ebiActiveLow, /* VSYNC Active Low polarity */
  544. 320, /* Horizontal size in pixels */
  545. 1, /* Horizontal Front Porch */
  546. 30, /* Horizontal Back Porch */
  547. 2, /* Horizontal Synchronization Pulse Width */
  548. 240, /* Vertical size in pixels */
  549. 1, /* Vertical Front Porch */
  550. 4, /* Vertical Back Porch */
  551. 2, /* Vertical Synchronization Pulse Width */
  552. 0x0000, /* Frame Address pointer offset to EBI memory base */
  553. 4, /* DCLK Period */
  554. 0, /* DCLK Start cycles */
  555. 0, /* DCLK Setup cycles */
  556. 0, /* DCLK Hold cycles */
  557. };
  558. DVK_enablePeripheral(DVK_TFT);
  559. /* Configure display for Direct Drive + 3-wire SPI mode */
  560. DVK_displayControl(DVK_Display_ModeGeneric);
  561. DVK_displayControl(DVK_Display_PowerEnable);
  562. DVK_displayControl(DVK_Display_ResetRelease);
  563. /* Configure GPIO for EBI and TFT */
  564. /* EBI TFT DCLK/Dot Clock */
  565. GPIO_PinModeSet(gpioPortA, 8, gpioModePushPull, 0);
  566. /* EBI TFT DATAEN */
  567. GPIO_PinModeSet(gpioPortA, 9, gpioModePushPull, 0);
  568. /* EBI TFT VSYNC */
  569. GPIO_PinModeSet(gpioPortA, 10, gpioModePushPull, 0);
  570. /* EBI TFT HSYNC */
  571. GPIO_PinModeSet(gpioPortA, 11, gpioModePushPull, 0);
  572. /* Initialize display */
  573. DMD_init(0, (rt_uint32_t)EBI_BankAddress(EBI_BANK2));
  574. /* Configure EBI TFT direct drive */
  575. EBI_TFTInit(&tftInit);
  576. #endif
  577. }
  578. /* Get LCD geometry */
  579. ret = DMD_getDisplayGeometry(&geometry);
  580. if (ret != DMD_OK)
  581. {
  582. lcd_debug("LCD err: get geometry failed!\n");
  583. break;
  584. }
  585. /* Init LCD info */
  586. flag = RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_DMA_TX;
  587. lcd_info.pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565P;
  588. lcd_info.bits_per_pixel = 16;
  589. lcd_info.width = geometry->xSize;
  590. lcd_info.height = geometry->ySize;
  591. #if defined(LCD_MAPPED)
  592. lcd_info.framebuffer = RT_NULL;
  593. efm32_spiLcd_register(&lcd_device, LCD_DEVICE_NAME, flag, (void *)&lcd_ops);
  594. #elif defined(LCD_DIRECT)
  595. lcd_info.framebuffer = (rt_uint8_t *)EBI_BankAddress(EBI_BANK2);
  596. efm32_spiLcd_register(&lcd_device, LCD_DEVICE_NAME, flag, RT_NULL);
  597. #endif
  598. /* Set clipping area */
  599. ret = DMD_setClippingArea(0, 0, geometry->xSize, geometry->ySize);
  600. if (ret != DMD_OK)
  601. {
  602. lcd_debug("LCD err: set clipping area failed!\n");
  603. break;
  604. }
  605. /* Read device code */
  606. rt_uint16_t code = 0xFFFF;
  607. #if defined(LCD_MAPPED)
  608. code = DMDIF_readDeviceCode();
  609. #endif
  610. /* Set as rtgui graphic driver */
  611. rtgui_graphic_set_device(&lcd_device);
  612. lcd_debug("LCD: H/W init OK!\n");
  613. return;
  614. } while(0);
  615. lcd_debug("LCD err: H/W init failed!\n");
  616. }
  617. #endif /* defined(EFM32_USING_LCD) */
  618. /***************************************************************************//**
  619. * @}
  620. ******************************************************************************/