drv_lcd.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * File : drv_lcd.c
  3. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Change Logs:
  20. * Date Author Notes
  21. * 2017-08-08 Yang the first version
  22. */
  23. #include <board.h>
  24. #include <rtdevice.h>
  25. #include <drv_lcd.h>
  26. #include <string.h>
  27. #include "fsl_iocon.h"
  28. #include "fsl_sctimer.h"
  29. #include "fsl_lcdc.h"
  30. static rt_uint16_t *lcd_framebuffer = RT_NULL;
  31. static rt_uint16_t *_rt_framebuffer = RT_NULL;
  32. #define RT_HW_LCD_WIDTH 480
  33. #define RT_HW_LCD_HEIGHT 272
  34. static struct rt_device_graphic_info _lcd_info;
  35. static struct rt_device lcd;
  36. #define LCD_PANEL_CLK (9 * 1000000UL)
  37. #define LCD_PPL 480
  38. #define LCD_HSW 2
  39. #define LCD_HFP 8
  40. #define LCD_HBP 43
  41. #define LCD_LPP 272
  42. #define LCD_VSW 10
  43. #define LCD_VFP 4
  44. #define LCD_VBP 12
  45. #define LCD_POL_FLAGS kLCDC_InvertVsyncPolarity | kLCDC_InvertHsyncPolarity
  46. #define LCD_WIDTH 480
  47. #define LCD_HEIGHT 272
  48. #define LCD_BITS_PER_PIXEL 16
  49. static void lcd_gpio_init(void)
  50. {
  51. const uint32_t port2_pin11_config = (
  52. IOCON_PIO_FUNC1 | /* Pin is configured as LCD_PWR */
  53. IOCON_PIO_MODE_INACT | /* No addition pin function */
  54. IOCON_PIO_INV_DI | /* Input function is not inverted */
  55. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  56. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  57. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  58. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  59. );
  60. IOCON_PinMuxSet(IOCON, PORT2_IDX, PIN11_IDX, port2_pin11_config); /* PORT2 PIN11 (coords: K3) is configured as LCD_PWR */
  61. const uint32_t port2_pin13_config = (
  62. IOCON_PIO_FUNC1 | /* Pin is configured as LCD_DCLK */
  63. IOCON_PIO_MODE_INACT | /* No addition pin function */
  64. IOCON_PIO_INV_DI | /* Input function is not inverted */
  65. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  66. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  67. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  68. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  69. );
  70. IOCON_PinMuxSet(IOCON, PORT2_IDX, PIN13_IDX, port2_pin13_config); /* PORT2 PIN13 (coords: P7) is configured as LCD_DCLK */
  71. const uint32_t port2_pin14_config = (
  72. IOCON_PIO_FUNC1 | /* Pin is configured as LCD_FP */
  73. IOCON_PIO_MODE_INACT | /* No addition pin function */
  74. IOCON_PIO_INV_DI | /* Input function is not inverted */
  75. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  76. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  77. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  78. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  79. );
  80. IOCON_PinMuxSet(IOCON, PORT2_IDX, PIN14_IDX, port2_pin14_config); /* PORT2 PIN14 (coords: L7) is configured as LCD_FP */
  81. const uint32_t port2_pin15_config = (
  82. IOCON_PIO_FUNC1 | /* Pin is configured as LCD_AC */
  83. IOCON_PIO_MODE_INACT | /* No addition pin function */
  84. IOCON_PIO_INV_DI | /* Input function is not inverted */
  85. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  86. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  87. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  88. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  89. );
  90. IOCON_PinMuxSet(IOCON, PORT2_IDX, PIN15_IDX, port2_pin15_config); /* PORT2 PIN15 (coords: M8) is configured as LCD_AC */
  91. const uint32_t port2_pin16_config = (
  92. IOCON_PIO_FUNC1 | /* Pin is configured as LCD_LP */
  93. IOCON_PIO_MODE_INACT | /* No addition pin function */
  94. IOCON_PIO_INV_DI | /* Input function is not inverted */
  95. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  96. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  97. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  98. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  99. );
  100. IOCON_PinMuxSet(IOCON, PORT2_IDX, PIN16_IDX, port2_pin16_config); /* PORT2 PIN16 (coords: L8) is configured as LCD_LP */
  101. const uint32_t port2_pin21_config = (
  102. IOCON_PIO_FUNC1 | /* Pin is configured as LCD_VD(3) */
  103. IOCON_PIO_MODE_INACT | /* No addition pin function */
  104. IOCON_PIO_INV_DI | /* Input function is not inverted */
  105. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  106. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  107. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  108. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  109. );
  110. IOCON_PinMuxSet(IOCON, PORT2_IDX, PIN21_IDX, port2_pin21_config); /* PORT2 PIN21 (coords: L10) is configured as LCD_VD(3) */
  111. const uint32_t port2_pin22_config = (
  112. IOCON_PIO_FUNC1 | /* Pin is configured as LCD_VD(4) */
  113. IOCON_PIO_MODE_INACT | /* No addition pin function */
  114. IOCON_PIO_INV_DI | /* Input function is not inverted */
  115. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  116. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  117. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  118. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  119. );
  120. IOCON_PinMuxSet(IOCON, PORT2_IDX, PIN22_IDX, port2_pin22_config); /* PORT2 PIN22 (coords: K10) is configured as LCD_VD(4) */
  121. const uint32_t port2_pin23_config = (
  122. IOCON_PIO_FUNC1 | /* Pin is configured as LCD_VD(5) */
  123. IOCON_PIO_MODE_INACT | /* No addition pin function */
  124. IOCON_PIO_INV_DI | /* Input function is not inverted */
  125. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  126. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  127. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  128. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  129. );
  130. IOCON_PinMuxSet(IOCON, PORT2_IDX, PIN23_IDX, port2_pin23_config); /* PORT2 PIN23 (coords: M14) is configured as LCD_VD(5) */
  131. const uint32_t port2_pin24_config = (
  132. IOCON_PIO_FUNC1 | /* Pin is configured as LCD_VD(6) */
  133. IOCON_PIO_MODE_INACT | /* No addition pin function */
  134. IOCON_PIO_INV_DI | /* Input function is not inverted */
  135. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  136. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  137. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  138. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  139. );
  140. IOCON_PinMuxSet(IOCON, PORT2_IDX, PIN24_IDX, port2_pin24_config); /* PORT2 PIN24 (coords: K14) is configured as LCD_VD(6) */
  141. const uint32_t port2_pin25_config = (
  142. IOCON_PIO_FUNC1 | /* Pin is configured as LCD_VD(7) */
  143. IOCON_PIO_MODE_INACT | /* No addition pin function */
  144. IOCON_PIO_INV_DI | /* Input function is not inverted */
  145. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  146. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  147. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  148. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  149. );
  150. IOCON_PinMuxSet(IOCON, PORT2_IDX, PIN25_IDX, port2_pin25_config); /* PORT2 PIN25 (coords: J11) is configured as LCD_VD(7) */
  151. const uint32_t port2_pin27_config = (
  152. IOCON_PIO_FUNC0 | /* Pin is configured as PIO2_27 */
  153. IOCON_PIO_MODE_PULLUP | /* Selects pull-up function */
  154. IOCON_PIO_INV_DI | /* Input function is not inverted */
  155. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  156. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  157. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  158. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  159. );
  160. IOCON_PinMuxSet(IOCON, PORT2_IDX, PIN27_IDX, port2_pin27_config); /* PORT2 PIN27 (coords: H14) is configured as PIO2_27 */
  161. const uint32_t port2_pin28_config = (
  162. IOCON_PIO_FUNC1 | /* Pin is configured as LCD_VD(10) */
  163. IOCON_PIO_MODE_INACT | /* No addition pin function */
  164. IOCON_PIO_INV_DI | /* Input function is not inverted */
  165. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  166. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  167. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  168. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  169. );
  170. IOCON_PinMuxSet(IOCON, PORT2_IDX, PIN28_IDX, port2_pin28_config); /* PORT2 PIN28 (coords: G13) is configured as LCD_VD(10) */
  171. const uint32_t port2_pin29_config = (
  172. IOCON_PIO_FUNC1 | /* Pin is configured as LCD_VD(11) */
  173. IOCON_PIO_MODE_INACT | /* No addition pin function */
  174. IOCON_PIO_INV_DI | /* Input function is not inverted */
  175. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  176. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  177. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  178. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  179. );
  180. IOCON_PinMuxSet(IOCON, PORT2_IDX, PIN29_IDX, port2_pin29_config); /* PORT2 PIN29 (coords: G11) is configured as LCD_VD(11) */
  181. const uint32_t port2_pin30_config = (
  182. IOCON_PIO_FUNC1 | /* Pin is configured as LCD_VD(12) */
  183. IOCON_PIO_MODE_INACT | /* No addition pin function */
  184. IOCON_PIO_INV_DI | /* Input function is not inverted */
  185. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  186. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  187. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  188. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  189. );
  190. IOCON_PinMuxSet(IOCON, PORT2_IDX, PIN30_IDX, port2_pin30_config); /* PORT2 PIN30 (coords: F12) is configured as LCD_VD(12) */
  191. const uint32_t port2_pin31_config = (
  192. IOCON_PIO_FUNC1 | /* Pin is configured as LCD_VD(13) */
  193. IOCON_PIO_MODE_INACT | /* No addition pin function */
  194. IOCON_PIO_INV_DI | /* Input function is not inverted */
  195. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  196. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  197. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  198. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  199. );
  200. IOCON_PinMuxSet(IOCON, PORT2_IDX, PIN31_IDX, port2_pin31_config); /* PORT2 PIN31 (coords: D14) is configured as LCD_VD(13) */
  201. const uint32_t port3_pin0_config = (
  202. IOCON_PIO_FUNC1 | /* Pin is configured as LCD_VD(14) */
  203. IOCON_PIO_MODE_INACT | /* No addition pin function */
  204. IOCON_PIO_INV_DI | /* Input function is not inverted */
  205. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  206. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  207. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  208. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  209. );
  210. IOCON_PinMuxSet(IOCON, PORT3_IDX, PIN0_IDX, port3_pin0_config); /* PORT3 PIN0 (coords: D12) is configured as LCD_VD(14) */
  211. const uint32_t port3_pin1_config = (
  212. IOCON_PIO_FUNC1 | /* Pin is configured as LCD_VD(15) */
  213. IOCON_PIO_MODE_INACT | /* No addition pin function */
  214. IOCON_PIO_INV_DI | /* Input function is not inverted */
  215. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  216. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  217. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  218. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  219. );
  220. IOCON_PinMuxSet(IOCON, PORT3_IDX, PIN1_IDX, port3_pin1_config); /* PORT3 PIN1 (coords: D11) is configured as LCD_VD(15) */
  221. const uint32_t port3_pin5_config = (
  222. IOCON_PIO_FUNC1 | /* Pin is configured as LCD_VD(19) */
  223. IOCON_PIO_MODE_INACT | /* No addition pin function */
  224. IOCON_PIO_INV_DI | /* Input function is not inverted */
  225. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  226. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  227. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  228. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  229. );
  230. IOCON_PinMuxSet(IOCON, PORT3_IDX, PIN5_IDX, port3_pin5_config); /* PORT3 PIN5 (coords: B10) is configured as LCD_VD(19) */
  231. const uint32_t port3_pin6_config = (
  232. IOCON_PIO_FUNC1 | /* Pin is configured as LCD_VD(20) */
  233. IOCON_PIO_MODE_INACT | /* No addition pin function */
  234. IOCON_PIO_INV_DI | /* Input function is not inverted */
  235. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  236. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  237. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  238. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  239. );
  240. IOCON_PinMuxSet(IOCON, PORT3_IDX, PIN6_IDX, port3_pin6_config); /* PORT3 PIN6 (coords: C9) is configured as LCD_VD(20) */
  241. const uint32_t port3_pin7_config = (
  242. IOCON_PIO_FUNC1 | /* Pin is configured as LCD_VD(21) */
  243. IOCON_PIO_MODE_INACT | /* No addition pin function */
  244. IOCON_PIO_INV_DI | /* Input function is not inverted */
  245. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  246. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  247. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  248. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  249. );
  250. IOCON_PinMuxSet(IOCON, PORT3_IDX, PIN7_IDX, port3_pin7_config); /* PORT3 PIN7 (coords: B8) is configured as LCD_VD(21) */
  251. const uint32_t port3_pin8_config = (
  252. IOCON_PIO_FUNC1 | /* Pin is configured as LCD_VD(22) */
  253. IOCON_PIO_MODE_INACT | /* No addition pin function */
  254. IOCON_PIO_INV_DI | /* Input function is not inverted */
  255. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  256. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  257. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  258. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  259. );
  260. IOCON_PinMuxSet(IOCON, PORT3_IDX, PIN8_IDX, port3_pin8_config); /* PORT3 PIN8 (coords: A7) is configured as LCD_VD(22) */
  261. const uint32_t port3_pin9_config = (
  262. IOCON_PIO_FUNC1 | /* Pin is configured as LCD_VD(23) */
  263. IOCON_PIO_MODE_INACT | /* No addition pin function */
  264. IOCON_PIO_INV_DI | /* Input function is not inverted */
  265. IOCON_PIO_DIGITAL_EN | /* Enables digital function */
  266. IOCON_PIO_INPFILT_OFF | /* Input filter disabled */
  267. IOCON_PIO_SLEW_STANDARD | /* Standard mode, output slew rate control is enabled */
  268. IOCON_PIO_OPENDRAIN_DI /* Open drain is disabled */
  269. );
  270. IOCON_PinMuxSet(IOCON, PORT3_IDX, PIN9_IDX, port3_pin9_config); /* PORT3 PIN9 (coords: C7) is configured as LCD_VD(23) */
  271. }
  272. void LCD_IRQHandler(void)
  273. {
  274. uint32_t intStatus = LCDC_GetEnabledInterruptsPendingStatus(LCD);
  275. LCDC_ClearInterruptsStatus(LCD, intStatus);
  276. if (intStatus & kLCDC_VerticalCompareInterrupt)
  277. {
  278. //s_frameEndFlag = true;
  279. }
  280. //__DSB();
  281. }
  282. /* RT-Thread Device Interface */
  283. static rt_err_t rt_lcd_init(rt_device_t dev)
  284. {
  285. /* Route Main clock to LCD. */
  286. CLOCK_AttachClk(kMCLK_to_LCD_CLK);
  287. CLOCK_SetClkDiv(kCLOCK_DivLcdClk, 1, true);
  288. /*LCD管脚配置*/
  289. lcd_gpio_init();
  290. /* Set the back light PWM. */
  291. {
  292. sctimer_config_t config;
  293. sctimer_pwm_signal_param_t pwmParam;
  294. uint32_t event;
  295. CLOCK_AttachClk(kMCLK_to_SCT_CLK);
  296. CLOCK_SetClkDiv(kCLOCK_DivSctClk, 2, true);
  297. SCTIMER_GetDefaultConfig(&config);
  298. SCTIMER_Init(SCT0, &config);
  299. pwmParam.output = kSCTIMER_Out_5;
  300. pwmParam.level = kSCTIMER_HighTrue;
  301. pwmParam.dutyCyclePercent = 5;
  302. SCTIMER_SetupPwm(SCT0, &pwmParam, kSCTIMER_CenterAlignedPwm, 1000U, CLOCK_GetFreq(kCLOCK_Sct), &event);
  303. }
  304. lcd_framebuffer = rt_malloc_align(sizeof(rt_uint16_t) * RT_HW_LCD_HEIGHT * RT_HW_LCD_WIDTH, 32);
  305. rt_memset(lcd_framebuffer, 0, sizeof(rt_uint16_t) * RT_HW_LCD_HEIGHT * RT_HW_LCD_WIDTH);
  306. {
  307. /* Initialize the display. */
  308. lcdc_config_t lcdConfig;
  309. LCDC_GetDefaultConfig(&lcdConfig);
  310. lcdConfig.panelClock_Hz = LCD_PANEL_CLK;
  311. lcdConfig.ppl = LCD_PPL;
  312. lcdConfig.hsw = LCD_HSW;
  313. lcdConfig.hfp = LCD_HFP;
  314. lcdConfig.hbp = LCD_HBP;
  315. lcdConfig.lpp = LCD_LPP;
  316. lcdConfig.vsw = LCD_VSW;
  317. lcdConfig.vfp = LCD_VFP;
  318. lcdConfig.vbp = LCD_VBP;
  319. lcdConfig.polarityFlags = LCD_POL_FLAGS;
  320. lcdConfig.upperPanelAddr = (uint32_t)lcd_framebuffer;//VRAM_ADDR;
  321. lcdConfig.bpp = kLCDC_16BPP565;
  322. lcdConfig.display = kLCDC_DisplayTFT;
  323. lcdConfig.swapRedBlue = true;
  324. LCDC_Init(LCD, &lcdConfig, CLOCK_GetFreq(kCLOCK_LCD));
  325. /* Trigger interrupt at start of every vertical back porch. */
  326. LCDC_SetVerticalInterruptMode(LCD, kLCDC_StartOfBackPorch);
  327. LCDC_EnableInterrupts(LCD, kLCDC_VerticalCompareInterrupt);
  328. NVIC_EnableIRQ(LCD_IRQn);
  329. LCDC_Start(LCD);
  330. LCDC_PowerUp(LCD);
  331. }
  332. return RT_EOK;
  333. }
  334. static rt_err_t rt_lcd_control(rt_device_t dev, int cmd, void *args)
  335. {
  336. switch (cmd)
  337. {
  338. case RTGRAPHIC_CTRL_RECT_UPDATE:
  339. {
  340. struct rt_device_rect_info *rect_info = (struct rt_device_rect_info *)args;
  341. /* 先指向绘图 buff 显示 */
  342. LCD->UPBASE = (rt_uint32_t)_rt_framebuffer;
  343. /* 从绘图 buff copy 数据到显示 buff */
  344. if (rect_info->width * rect_info->height < RT_HW_LCD_WIDTH * RT_HW_LCD_HEIGHT / 5 * 3)
  345. {
  346. int index = 0;
  347. rt_uint8_t *p = (rt_uint8_t *)lcd_framebuffer;
  348. rt_uint8_t *q = (rt_uint8_t *)_rt_framebuffer;
  349. p += (rect_info->x + rect_info->y * RT_HW_LCD_WIDTH) * sizeof(rt_uint16_t);
  350. q += (rect_info->x + rect_info->y * RT_HW_LCD_WIDTH) * sizeof(rt_uint16_t);
  351. for (index = 0; index < rect_info->height; index ++)
  352. {
  353. memcpy((void *)p, (void *)q, sizeof(rt_uint16_t) * rect_info->width);
  354. p += RT_HW_LCD_WIDTH * sizeof(rt_uint16_t);
  355. q += RT_HW_LCD_WIDTH * sizeof(rt_uint16_t);
  356. }
  357. }
  358. else
  359. {
  360. memcpy((void *)lcd_framebuffer, _rt_framebuffer, sizeof(rt_uint16_t)*RT_HW_LCD_HEIGHT * RT_HW_LCD_WIDTH);
  361. }
  362. /* 指回显示 buff */
  363. LCD->UPBASE = (rt_uint32_t)lcd_framebuffer;
  364. rt_kprintf("====> rect_info : %d %d %d %d\n", rect_info->x, rect_info->y, rect_info->width, rect_info->height);
  365. }
  366. break;
  367. case RTGRAPHIC_CTRL_POWERON:
  368. break;
  369. case RTGRAPHIC_CTRL_POWEROFF:
  370. break;
  371. case RTGRAPHIC_CTRL_GET_INFO:
  372. memcpy(args, &_lcd_info, sizeof(_lcd_info));
  373. break;
  374. case RTGRAPHIC_CTRL_SET_MODE:
  375. break;
  376. }
  377. return RT_EOK;
  378. }
  379. rt_uint16_t* get_lcd_framebuffer(void)
  380. {
  381. return lcd_framebuffer;
  382. }
  383. void lcd_clear(rt_uint16_t color)
  384. {
  385. volatile rt_uint16_t *p = (rt_uint16_t *)lcd_framebuffer;
  386. int x, y;
  387. for (y = 0; y <= RT_HW_LCD_HEIGHT; y++)
  388. {
  389. for (x = 0; x <= RT_HW_LCD_WIDTH; x++)
  390. {
  391. *p++ = color; /* red */
  392. }
  393. }
  394. }
  395. void rt_hw_lcd_init(void)
  396. {
  397. _rt_framebuffer = rt_malloc_align(sizeof(rt_uint16_t) * RT_HW_LCD_HEIGHT * RT_HW_LCD_WIDTH, 32);
  398. if (_rt_framebuffer == RT_NULL) return; /* no memory yet */
  399. _lcd_info.bits_per_pixel = LCD_BITS_PER_PIXEL;
  400. _lcd_info.pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565; // RTGRAPHIC_PIXEL_FORMAT_ARGB888
  401. _lcd_info.framebuffer = (void *)_rt_framebuffer;
  402. _lcd_info.width = RT_HW_LCD_WIDTH;
  403. _lcd_info.height = RT_HW_LCD_HEIGHT;
  404. /* init device structure */
  405. lcd.type = RT_Device_Class_Graphic;
  406. lcd.init = rt_lcd_init;
  407. lcd.open = RT_NULL;
  408. lcd.close = RT_NULL;
  409. lcd.control = rt_lcd_control;
  410. lcd.user_data = (void *)&_lcd_info;
  411. /* register lcd device to RT-Thread */
  412. rt_device_register(&lcd, "lcd", RT_DEVICE_FLAG_RDWR);
  413. rt_lcd_init(&lcd);
  414. }
  415. void lcd_test(void)
  416. {
  417. lcd_clear(0xf800);
  418. rt_thread_delay(200);
  419. lcd_clear(0x07e0);
  420. rt_thread_delay(200);
  421. lcd_clear(0x001f);
  422. rt_thread_delay(200);
  423. }
  424. #ifdef RT_USING_FINSH
  425. #include <finsh.h>
  426. FINSH_FUNCTION_EXPORT(lcd_clear, lcd_clear);
  427. FINSH_FUNCTION_EXPORT(lcd_test, lcd_test);
  428. #ifdef FINSH_USING_MSH
  429. MSH_CMD_EXPORT(lcd_clear, lcd_clear);
  430. MSH_CMD_EXPORT(lcd_test, lcd_test);
  431. #endif
  432. #endif