drv_lcd.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * File : drv_lcd.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006-2013, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2017-10-30 Tanek the first version
  13. * 2018-04-05 Liu2guang export LCD config parameters.
  14. */
  15. #include <rtthread.h>
  16. #ifdef BSP_USING_LCD
  17. #include "drv_lcd.h"
  18. #include "fsl_common.h"
  19. #include "fsl_iomuxc.h"
  20. #include "fsl_elcdif.h"
  21. #include "drv_gpio.h"
  22. #define LOG_TAG "drv.lcd"
  23. #include <drv_log.h>
  24. #if !defined(LCD_WIDTH) || !defined(LCD_HEIGHT)
  25. #error "Please config lcd pixel parameters."
  26. #endif
  27. #if !defined(LCD_HFP) || !defined(LCD_HBP) || !defined(LCD_HSW) || \
  28. !defined(LCD_VFP) || !defined(LCD_VBP) || !defined(LCD_VSW)
  29. #error "Please config lcd timing parameters."
  30. #endif
  31. #if !defined(LCD_BL_PIN) || !defined(LCD_RST_PIN)
  32. #error "Please config lcd backlight or reset pin."
  33. #endif
  34. struct imxrt_lcd
  35. {
  36. struct rt_device device;
  37. struct rt_device_graphic_info info;
  38. };
  39. static struct imxrt_lcd lcd;
  40. ALIGN(64) static uint16_t frame_buffer[LCD_HEIGHT][LCD_WIDTH] RT_SECTION("NonCacheable");
  41. static rt_err_t imxrt_lcd_init(rt_device_t device)
  42. {
  43. RT_ASSERT(device != RT_NULL);
  44. rt_memset(frame_buffer, 0x00, sizeof(frame_buffer));
  45. clock_video_pll_config_t pll_config;
  46. pll_config.loopDivider = 43;
  47. pll_config.postDivider = 4;
  48. pll_config.numerator = 0;
  49. pll_config.denominator = 0;
  50. CLOCK_InitVideoPll(&pll_config);
  51. rt_pin_mode(LCD_RST_PIN, PIN_MODE_OUTPUT); /* LCD_RESET */
  52. rt_pin_write(LCD_RST_PIN, PIN_LOW);
  53. rt_thread_delay(RT_TICK_PER_SECOND / 100);
  54. rt_pin_write(LCD_RST_PIN, PIN_HIGH);
  55. rt_pin_mode (LCD_BL_PIN, PIN_MODE_OUTPUT); /* LCD_BL */
  56. rt_pin_write(LCD_BL_PIN, PIN_HIGH);
  57. /* LCD */
  58. elcdif_rgb_mode_config_t lcd_config;
  59. lcd_config.hfp = LCD_HFP;
  60. lcd_config.vfp = LCD_VFP;
  61. lcd_config.hbp = LCD_HBP;
  62. lcd_config.vbp = LCD_VBP;
  63. lcd_config.hsw = LCD_HSW;
  64. lcd_config.vsw = LCD_VSW;
  65. lcd_config.polarityFlags = kELCDIF_DataEnableActiveHigh |
  66. kELCDIF_VsyncActiveHigh |
  67. kELCDIF_HsyncActiveLow |
  68. kELCDIF_DriveDataOnRisingClkEdge;
  69. lcd_config.panelWidth = LCD_WIDTH;
  70. lcd_config.panelHeight = LCD_HEIGHT;
  71. lcd_config.pixelFormat = kELCDIF_PixelFormatRGB565;
  72. lcd_config.dataBus = kELCDIF_DataBus16Bit;
  73. lcd_config.bufferAddr = (uint32_t)frame_buffer;
  74. ELCDIF_RgbModeInit (LCDIF, &lcd_config);
  75. ELCDIF_RgbModeStart(LCDIF);
  76. /* LCD DEVICE */
  77. lcd.info.width = LCD_WIDTH;
  78. lcd.info.height = LCD_HEIGHT;
  79. lcd.info.pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565;
  80. lcd.info.bits_per_pixel = 16;
  81. lcd.info.framebuffer = (void *)frame_buffer;
  82. return RT_EOK;
  83. }
  84. static rt_err_t imxrt_lcd_control(rt_device_t device, int cmd, void *args)
  85. {
  86. switch(cmd)
  87. {
  88. case RTGRAPHIC_CTRL_RECT_UPDATE:
  89. break;
  90. case RTGRAPHIC_CTRL_POWERON:
  91. rt_pin_write(LCD_BL_PIN, PIN_HIGH);
  92. break;
  93. case RTGRAPHIC_CTRL_POWEROFF:
  94. rt_pin_write(LCD_BL_PIN, PIN_LOW);
  95. break;
  96. case RTGRAPHIC_CTRL_GET_INFO:
  97. rt_memcpy(args, &lcd.info, sizeof(lcd.info));
  98. break;
  99. case RTGRAPHIC_CTRL_SET_MODE:
  100. break;
  101. }
  102. return RT_EOK;
  103. }
  104. int rt_hw_lcd_init(void)
  105. {
  106. rt_err_t ret;
  107. lcd.device.type = RT_Device_Class_Graphic;
  108. lcd.device.init = imxrt_lcd_init;
  109. lcd.device.open = RT_NULL;
  110. lcd.device.close = RT_NULL;
  111. lcd.device.read = RT_NULL;
  112. lcd.device.write = RT_NULL;
  113. lcd.device.control = imxrt_lcd_control;
  114. lcd.device.user_data = (void *)&lcd.info;
  115. ret = rt_device_register(&lcd.device, "lcd", RT_DEVICE_FLAG_RDWR);
  116. return ret;
  117. }
  118. INIT_DEVICE_EXPORT(rt_hw_lcd_init);
  119. #endif /* BSP_USING_LCD */