dev_lcd.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /***************************************************************************//**
  2. * @file dev_lcd.c
  3. * @brief LCD driver of RT-Thread RTOS for EFM32
  4. * COPYRIGHT (C) 2011, RT-Thread Development Team
  5. * @author onelife
  6. * @version 0.4 beta
  7. *******************************************************************************
  8. * @section License
  9. * The license and distribution terms for this file may be found in the file
  10. * LICENSE in this distribution or at http://www.rt-thread.org/license/LICENSE
  11. *******************************************************************************
  12. * @section Change Logs
  13. * Date Author Notes
  14. * 2011-12-16 onelife Initial creation for EFM32
  15. ******************************************************************************/
  16. /***************************************************************************//**
  17. * @addtogroup efm32
  18. * @{
  19. ******************************************************************************/
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "board.h"
  22. #include "drv_usart.h"
  23. #include "dev_lcd.h"
  24. #if defined(EFM32_USING_LCD)
  25. #include <rtgui/rtgui.h>
  26. #include <rtgui/driver.h>
  27. #include <dmd_ssd2119.h>
  28. /* Private typedef -----------------------------------------------------------*/
  29. /* Private define ------------------------------------------------------------*/
  30. /* Private macro -------------------------------------------------------------*/
  31. #ifdef EFM32_LCD_DEBUG
  32. #define lcd_debug(format,args...) rt_kprintf(format, ##args)
  33. #else
  34. #define lcd_debug(format,args...)
  35. #endif
  36. /* Private function prototypes -----------------------------------------------*/
  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. /* Private variables ---------------------------------------------------------*/
  43. static rt_device_t lcd;
  44. static struct rt_device lcd_device;
  45. static struct rt_device_graphic_info lcd_info;
  46. static const struct rtgui_graphic_driver_ops lcd_ops =
  47. {
  48. efm32_spiLcd_setPixel,
  49. efm32_spiLcd_getPixel,
  50. efm32_spiLcd_drawHLine,
  51. efm32_spiLcd_drawVLine,
  52. efm32_spiLcd_drawRawHLine
  53. };
  54. static rt_bool_t lcdAutoCs = true;
  55. /* Private functions ---------------------------------------------------------*/
  56. /***************************************************************************//**
  57. * @brief
  58. * Set/Clear chip select
  59. *
  60. * @details
  61. *
  62. * @note
  63. *
  64. * @param[in] enable
  65. * Chip select pin setting
  66. ******************************************************************************/
  67. static void efm32_spiLcd_cs(rt_uint8_t enable)
  68. {
  69. if (!lcdAutoCs)
  70. {
  71. if (enable)
  72. {
  73. GPIO_PinOutClear(LCD_CS_PORT, LCD_CS_PIN);
  74. }
  75. else
  76. {
  77. GPIO_PinOutSet(LCD_CS_PORT, LCD_CS_PIN);
  78. }
  79. }
  80. }
  81. /***************************************************************************//**
  82. * @brief
  83. * Draw a pixel with specified color
  84. *
  85. * @details
  86. *
  87. * @note
  88. *
  89. * @param[in] c
  90. * Pointer to color
  91. *
  92. * @param[in] x
  93. * Horizontal position
  94. *
  95. * @param[in] y
  96. * Vertical position
  97. ******************************************************************************/
  98. static void efm32_spiLcd_setPixel(rtgui_color_t *c, int x, int y)
  99. {
  100. rt_uint32_t ret = RT_EOK;
  101. do
  102. {
  103. /* Check if pixel is outside clipping region */
  104. if ((x < 0) || (x > lcd_info.width))
  105. {
  106. break;
  107. }
  108. if ((y < 0) || (y > lcd_info.height))
  109. {
  110. break;
  111. }
  112. /* Write color */
  113. ret = DMD_writePixel((rt_uint16_t)x, (rt_uint16_t)y, (rt_uint16_t)*c, 1);
  114. if (ret != 0)
  115. {
  116. break;
  117. }
  118. return;
  119. } while(0);
  120. lcd_debug("LCD err: Set pixel at (%d,%d: %x) failed (%x)!\n",
  121. x, y, *c, ret);
  122. }
  123. /***************************************************************************//**
  124. * @brief
  125. * Get the color of a pixel
  126. *
  127. * @details
  128. *
  129. * @note
  130. *
  131. * @param[out] c
  132. * Pointer to color
  133. *
  134. * @param[in] x
  135. * Horizontal position
  136. *
  137. * @param[in] y
  138. * Vertical position
  139. ******************************************************************************/
  140. static void efm32_spiLcd_getPixel(rtgui_color_t *c, int x, int y)
  141. {
  142. rt_uint32_t ret = RT_EOK;
  143. do
  144. {
  145. /* Check if pixel is outside clipping region */
  146. if ((x < 0) || (x > lcd_info.width))
  147. {
  148. break;
  149. }
  150. if ((y < 0) || (y > lcd_info.height))
  151. {
  152. break;
  153. }
  154. /* Read color */
  155. ret = DMD_readPixel((rt_uint16_t)x, (rt_uint16_t)y, (rt_uint16_t *)c);
  156. if (ret != 0)
  157. {
  158. break;
  159. }
  160. return;
  161. } while(0);
  162. lcd_debug("LCD err: Get pixel at (%d,%d: %x) failed (%x)!\n",
  163. x, y, *c, ret);
  164. }
  165. /***************************************************************************//**
  166. * @brief
  167. * Draw a horizontal line with raw color
  168. *
  169. * @details
  170. *
  171. * @note
  172. *
  173. * @param[in] pixels
  174. * Pointer to raw 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_drawRawHLine(rt_uint8_t *pixels, int x1, int x2, int y)
  186. {
  187. lcd_debug("LCD: RAW H LINE!\n");
  188. }
  189. /***************************************************************************//**
  190. * @brief
  191. * Draw a horizontal line with specified color
  192. *
  193. * @details
  194. *
  195. * @note
  196. *
  197. * @param[in] c
  198. * Pointer to color
  199. *
  200. * @param[in] x1
  201. * Horizontal start position
  202. *
  203. * @param[in] x2
  204. * Horizontal end position
  205. *
  206. * @param[in] y
  207. * Vertical position
  208. ******************************************************************************/
  209. static void efm32_spiLcd_drawHLine(rtgui_color_t *c, int x1, int x2, int y)
  210. {
  211. rt_uint32_t ret = RT_EOK;
  212. do
  213. {
  214. /* Check if line is outside of clipping region */
  215. if ((y < 0) || (y > lcd_info.height))
  216. {
  217. break;
  218. }
  219. /* Swap the coordinates if x1 is larger than x2 */
  220. if (x1 > x2)
  221. {
  222. int swap;
  223. swap = x1;
  224. x1 = x2;
  225. x2 = swap;
  226. }
  227. /* Check if entire line is outside clipping region */
  228. if ((x1 > lcd_info.width) || (x2 < 0))
  229. {
  230. /* Nothing to draw */
  231. break;
  232. }
  233. /* Clip the line if necessary */
  234. if (x1 < 0)
  235. {
  236. x1 = 0;
  237. }
  238. if (x2 > lcd_info.width)
  239. {
  240. x2 = lcd_info.width;
  241. }
  242. /* Write color */
  243. rt_uint32_t length = x2 - x1 + 1;
  244. ret = DMD_writePixel((rt_uint16_t)x1, (rt_uint16_t)y,
  245. (rt_uint16_t)*c, length);
  246. if (ret != 0)
  247. {
  248. break;
  249. }
  250. return;
  251. } while(0);
  252. lcd_debug("LCD err: Draw hline at (%d-%d,%d: %x) failed (%x)!\n",
  253. x1, x2, y, *c, ret);
  254. }
  255. /***************************************************************************//**
  256. * @brief
  257. * Draw a vertical line with specified color
  258. *
  259. * @details
  260. *
  261. * @note
  262. *
  263. * @param[in] c
  264. * Pointer to color
  265. *
  266. * @param[in] x
  267. * Horizontal position
  268. *
  269. * @param[in] y1
  270. * Vertical start position
  271. *
  272. * @param[in] y2
  273. * Vertical end position
  274. ******************************************************************************/
  275. static void efm32_spiLcd_drawVLine(rtgui_color_t *c, int x , int y1, int y2)
  276. {
  277. rt_uint32_t ret = RT_EOK;
  278. do
  279. {
  280. /* Check if line is outside of clipping region */
  281. if ((x < 0) || (x > lcd_info.width))
  282. {
  283. break;
  284. }
  285. /* Swap the coordinates if y1 is larger than y2 */
  286. if (y1 > y2)
  287. {
  288. rt_uint16_t swap;
  289. swap = y1;
  290. y1 = y2;
  291. y2 = swap;
  292. }
  293. /* Check if entire line is outside clipping region */
  294. if ((y1 > lcd_info.height) || (y2 < 0))
  295. {
  296. /* Nothing to draw */
  297. break;
  298. }
  299. /* Clip the line if necessary */
  300. if (y1 < 0)
  301. {
  302. y1 = 0;
  303. }
  304. if (y2 > lcd_info.height)
  305. {
  306. y2 = lcd_info.height;
  307. }
  308. /* Set clipping area */
  309. rt_uint16_t length = y2 - y1 + 1;
  310. ret = DMD_setClippingArea((rt_uint16_t)x, (rt_uint16_t)y1, 1, length);
  311. if (ret != DMD_OK)
  312. {
  313. break;
  314. }
  315. /* Write color */
  316. ret= DMD_writePixel(0, 0, (rt_uint16_t)*c, length);
  317. if (ret != DMD_OK)
  318. {
  319. break;
  320. }
  321. /* Reset clipping area */
  322. ret = DMD_setClippingArea(0, 0, lcd_info.width, lcd_info.height);
  323. if (ret != DMD_OK)
  324. {
  325. break;
  326. }
  327. return;
  328. } while(0);
  329. lcd_debug("LCD err: Draw vline at (%d,%d-%d: %x) failed (%x)!\n",
  330. x, y1, y2, *c, ret);
  331. }
  332. /***************************************************************************//**
  333. * @brief
  334. * Configure LCD device
  335. *
  336. * @details
  337. *
  338. * @note
  339. *
  340. * @param[in] dev
  341. * Pointer to device descriptor
  342. *
  343. * @param[in] cmd
  344. * IIC control command
  345. *
  346. * @param[in] args
  347. * Arguments
  348. *
  349. * @return
  350. * Error code
  351. ******************************************************************************/
  352. static rt_err_t efm32_spiLcd_control (rt_device_t dev, rt_uint8_t cmd, void *args)
  353. {
  354. switch (cmd)
  355. {
  356. case RTGRAPHIC_CTRL_RECT_UPDATE:
  357. break;
  358. case RTGRAPHIC_CTRL_POWERON:
  359. break;
  360. case RTGRAPHIC_CTRL_POWEROFF:
  361. break;
  362. case RTGRAPHIC_CTRL_GET_INFO:
  363. rt_memcpy(args, &lcd_info, sizeof(struct rt_device_graphic_info));
  364. break;
  365. case RTGRAPHIC_CTRL_SET_MODE:
  366. break;
  367. }
  368. return RT_EOK;
  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_spiLed_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. /* Reconfig speed */
  475. usart = (struct efm32_usart_device_t *)(lcd->user_data);
  476. USART_BaudrateSyncSet(usart->usart_device, 0, EFM32_LCD_SPICLK);
  477. /* Config CS pin */
  478. if (!(usart->state & USART_STATE_AUTOCS))
  479. {
  480. GPIO_PinModeSet(LCD_CS_PORT, LCD_CS_PIN, gpioModePushPull, 1);
  481. lcdAutoCs = false;
  482. }
  483. // TODO: add another method
  484. /* TFT initialize or reinitialize to Address Mapped Mode
  485. Assumes EBI has been configured correctly in DVK_init(DVK_Init_EBI) */
  486. rt_uint32_t freq = SystemCoreClockGet();
  487. rt_uint32_t i;
  488. rt_bool_t warning = RT_FALSE;
  489. /* If we are in BC_UIF_AEM_EFM state, we can redraw graphics */
  490. while (DVK_readRegister(&BC_REGISTER->UIF_AEM) != BC_UIF_AEM_EFM)
  491. {
  492. if (!warning)
  493. {
  494. lcd_debug("LCD: Please press AEM button!!!\n");
  495. warning = RT_TRUE;
  496. }
  497. }
  498. lcd_debug("LCD: Got LCD control\n");
  499. /* If we're not BC_ARB_CTRL_EBI state, we need to reconfigure display controller */
  500. if (DVK_readRegister(&BC_REGISTER->ARB_CTRL) != BC_ARB_CTRL_EBI)
  501. {
  502. lcd_debug("LCD: Set to EBI mode\n");
  503. /* Configure for EBI mode and reset display */
  504. DVK_displayControl(DVK_Display_EBI);
  505. DVK_displayControl(DVK_Display_ResetAssert);
  506. DVK_displayControl(DVK_Display_PowerDisable);
  507. /* Short delay */
  508. freq = SystemCoreClockGet();
  509. for(i = 0; i < (freq / 100); i++)
  510. {
  511. __NOP();
  512. }
  513. /* Configure display for Direct Drive + SPI mode */
  514. DVK_displayControl(DVK_Display_Mode8080);
  515. DVK_displayControl(DVK_Display_PowerEnable);
  516. DVK_displayControl(DVK_Display_ResetRelease);
  517. /* Initialize graphics - abort on failure */
  518. ret = DMD_init(BC_SSD2119_BASE, BC_SSD2119_BASE + 2);
  519. if (ret == DMD_OK)
  520. {
  521. /* Make sure display is configured with correct rotation */
  522. DMD_flipDisplay(1, 1);
  523. }
  524. else if (ret != DMD_ERROR_DRIVER_ALREADY_INITIALIZED)
  525. {
  526. lcd_debug("LCD err: driver init failed %x\n", ret);
  527. break;
  528. }
  529. }
  530. /* Get LCD geometry */
  531. ret = DMD_getDisplayGeometry(&geometry);
  532. if (ret != DMD_OK)
  533. {
  534. lcd_debug("LCD err: get geometry failed!\n");
  535. break;
  536. }
  537. /* Init LCD info */
  538. lcd_info.pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565P;
  539. lcd_info.bits_per_pixel = 16;
  540. lcd_info.width = geometry->xSize - 1;
  541. lcd_info.height = geometry->ySize - 1;
  542. lcd_info.framebuffer = RT_NULL;
  543. flag = RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_DMA_TX;
  544. efm32_spiLcd_register(&lcd_device, LCD_DEVICE_NAME, flag, (void *)&lcd_ops);
  545. /* Set clipping area */
  546. ret = DMD_setClippingArea(0, 0, geometry->xSize, geometry->ySize);
  547. if (ret != DMD_OK)
  548. {
  549. lcd_debug("LCD err: set clipping area failed!\n");
  550. break;
  551. }
  552. /* Read device code */
  553. rt_uint16_t code = 0xFFFF;
  554. code = DMDIF_readDeviceCode();
  555. /* Set as rtgui graphic driver */
  556. rtgui_graphic_set_device(&lcd_device);
  557. lcd_debug("LCD: H/W (%x) init OK!\n", code);
  558. return;
  559. } while(0);
  560. lcd_debug("LCD err: H/W init failed!\n");
  561. }
  562. #endif /* defined(EFM32_USING_LCD) */
  563. /***************************************************************************//**
  564. * @}
  565. ******************************************************************************/