drv_rgb_lcd.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (c) 2006-2022, Synwit Technology Co.,Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-02-25 lik first version
  9. */
  10. #include "drv_rgb_lcd.h"
  11. #ifdef BSP_USING_RGB_LCD
  12. #include "rgb_lcd_port.h"
  13. #define DRV_DEBUG
  14. #define LOG_TAG "drv.rgb_lcd"
  15. #include <drv_log.h>
  16. #define LCD_DEVICE(dev) (struct swm_rgb_lcd_device *)(dev)
  17. struct swm_rgb_lcd_device rgb_lcd_obj;
  18. static rt_err_t swm_rgb_lcd_configure(struct rt_device *device)
  19. {
  20. struct swm_rgb_lcd_device *rgb_lcd = LCD_DEVICE(device);
  21. /* nothing, right now */
  22. rgb_lcd = rgb_lcd;
  23. return RT_EOK;
  24. }
  25. static rt_err_t swm_rgb_lcd_control(struct rt_device *device, int cmd, void *args)
  26. {
  27. struct swm_rgb_lcd_device *rgb_lcd = LCD_DEVICE(device);
  28. switch (cmd)
  29. {
  30. case RTGRAPHIC_CTRL_GET_INFO:
  31. {
  32. struct rt_device_graphic_info *info = (struct rt_device_graphic_info *)args;
  33. RT_ASSERT(info != RT_NULL);
  34. info->pixel_format = rgb_lcd->lcd_info.pixel_format;
  35. info->bits_per_pixel = rgb_lcd->lcd_info.bits_per_pixel;
  36. info->width = rgb_lcd->lcd_info.width;
  37. info->height = rgb_lcd->lcd_info.height;
  38. info->framebuffer = rgb_lcd->lcd_info.framebuffer;
  39. }
  40. break;
  41. default:
  42. return -RT_EINVAL;
  43. }
  44. return RT_EOK;
  45. }
  46. #if defined(LCD_BACKLIGHT_USING_PWM)
  47. static void turn_on_lcd_backlight(void)
  48. {
  49. struct rt_device_pwm *pwm_dev;
  50. /* turn on the LCD backlight */
  51. pwm_dev = (struct rt_device_pwm *)rt_device_find(LCD_PWM_DEV_NAME);
  52. /* pwm frequency:100K = 10000ns */
  53. rt_pwm_set(pwm_dev, LCD_PWM_DEV_CHANNEL, 10000, 10000);
  54. rt_pwm_enable(pwm_dev, LCD_PWM_DEV_CHANNEL);
  55. }
  56. #elif defined(LCD_BACKLIGHT_USING_GPIO)
  57. static void turn_on_lcd_backlight(void)
  58. {
  59. rt_pin_mode(LCD_DISP_GPIO, PIN_MODE_OUTPUT);
  60. rt_pin_write(LCD_DISP_GPIO, PIN_HIGH);
  61. rt_pin_mode(LCD_BL_GPIO, PIN_MODE_OUTPUT);
  62. rt_pin_write(LCD_BL_GPIO, PIN_HIGH);
  63. }
  64. #else
  65. static void turn_on_lcd_backlight(void)
  66. {
  67. }
  68. #endif
  69. #ifdef RT_USING_DEVICE_OPS
  70. static const struct rt_device_ops swm_lcd_ops =
  71. {
  72. .init = swm_rgb_lcd_configure,
  73. .open = RT_NULL,
  74. .close = RT_NULL,
  75. .read = RT_NULL,
  76. .write = RT_NULL,
  77. .control = swm_rgb_lcd_control};
  78. #endif
  79. int swm_rgb_lcd_init(void)
  80. {
  81. rt_err_t result = RT_EOK;
  82. struct rt_device *device = &rgb_lcd_obj.parent;
  83. /* memset rgb_lcd_obj to zero */
  84. memset(&rgb_lcd_obj, 0x00, sizeof(rgb_lcd_obj));
  85. /* config LCD dev info */
  86. rgb_lcd_obj.lcd_info.height = LCD_HEIGHT;
  87. rgb_lcd_obj.lcd_info.width = LCD_WIDTH;
  88. rgb_lcd_obj.lcd_info.bits_per_pixel = LCD_BITS_PER_PIXEL;
  89. rgb_lcd_obj.lcd_info.pixel_format = LCD_PIXEL_FORMAT;
  90. /* malloc memory for Triple Buffering */
  91. rgb_lcd_obj.lcd_info.framebuffer = rt_malloc_align(LCD_BUF_SIZE,4);
  92. if (rgb_lcd_obj.lcd_info.framebuffer == RT_NULL)
  93. {
  94. LOG_E("init frame buffer failed!\n");
  95. result = -RT_ENOMEM;
  96. goto __exit;
  97. }
  98. /* memset buff to 0xFF */
  99. memset(rgb_lcd_obj.lcd_info.framebuffer, 0xFF, LCD_BUF_SIZE);
  100. device->type = RT_Device_Class_Graphic;
  101. #ifdef RT_USING_DEVICE_OPS
  102. device->ops = &swm_lcd_ops;
  103. #else
  104. device->init = swm_rgb_lcd_configure;
  105. device->control = swm_rgb_lcd_control;
  106. #endif
  107. /* register lcd device */
  108. rt_device_register(device, "rgb_lcd", RT_DEVICE_FLAG_RDWR);
  109. LCD_InitStructure LCD_initStruct;
  110. // PORT_Init(PORTB, PIN1, PORTB_PIN1_LCD_B0, 0);
  111. // PORT_Init(PORTB, PIN11, PORTB_PIN11_LCD_B1, 0);
  112. // PORT_Init(PORTB, PIN13, PORTB_PIN13_LCD_B2, 0);
  113. PORT_Init(PORTB, PIN15, PORTB_PIN15_LCD_B3, 0);
  114. PORT_Init(PORTA, PIN2, PORTA_PIN2_LCD_B4, 0);
  115. PORT_Init(PORTA, PIN9, PORTA_PIN9_LCD_B5, 0);
  116. PORT_Init(PORTA, PIN10, PORTA_PIN10_LCD_B6, 0);
  117. PORT_Init(PORTA, PIN11, PORTA_PIN11_LCD_B7, 0);
  118. // PORT_Init(PORTA, PIN12, PORTA_PIN12_LCD_G0, 0);
  119. // PORT_Init(PORTA, PIN13, PORTA_PIN13_LCD_G1, 0);
  120. PORT_Init(PORTA, PIN14, PORTA_PIN14_LCD_G2, 0);
  121. PORT_Init(PORTA, PIN15, PORTA_PIN15_LCD_G3, 0);
  122. PORT_Init(PORTC, PIN0, PORTC_PIN0_LCD_G4, 0);
  123. PORT_Init(PORTC, PIN1, PORTC_PIN1_LCD_G5, 0);
  124. PORT_Init(PORTC, PIN2, PORTC_PIN2_LCD_G6, 0);
  125. PORT_Init(PORTC, PIN3, PORTC_PIN3_LCD_G7, 0);
  126. // PORT_Init(PORTC, PIN4, PORTC_PIN4_LCD_R0, 0);
  127. // PORT_Init(PORTC, PIN5, PORTC_PIN5_LCD_R1, 0);
  128. // PORT_Init(PORTC, PIN8, PORTC_PIN8_LCD_R2, 0);
  129. PORT_Init(PORTC, PIN9, PORTC_PIN9_LCD_R3, 0);
  130. PORT_Init(PORTC, PIN10, PORTC_PIN10_LCD_R4, 0);
  131. PORT_Init(PORTC, PIN11, PORTC_PIN11_LCD_R5, 0);
  132. PORT_Init(PORTC, PIN12, PORTC_PIN12_LCD_R6, 0);
  133. PORT_Init(PORTC, PIN13, PORTC_PIN13_LCD_R7, 0);
  134. PORT_Init(PORTB, PIN2, PORTB_PIN2_LCD_VSYNC, 0);
  135. PORT_Init(PORTB, PIN3, PORTB_PIN3_LCD_HSYNC, 0);
  136. PORT_Init(PORTB, PIN4, PORTB_PIN4_LCD_DEN, 0);
  137. PORT_Init(PORTB, PIN5, PORTB_PIN5_LCD_DCLK, 0);
  138. LCD_initStruct.ClkDiv = LCD_CLK_DIV;
  139. LCD_initStruct.Format = ((LCD_BITS_PER_PIXEL == 16) ? LCD_FMT_RGB565 : LCD_FMT_RGB888);
  140. LCD_initStruct.HnPixel = LCD_WIDTH;
  141. LCD_initStruct.VnPixel = LCD_HEIGHT;
  142. LCD_initStruct.Hfp = LCD_HFP;
  143. LCD_initStruct.Hbp = LCD_HBP;
  144. LCD_initStruct.Vfp = LCD_VFP;
  145. LCD_initStruct.Vbp = LCD_VBP;
  146. LCD_initStruct.HsyncWidth = LCD_HSYNC_WIDTH;
  147. LCD_initStruct.VsyncWidth = LCD_VSYNC_WIDTH;
  148. LCD_initStruct.DataSource = (uint32_t)rgb_lcd_obj.lcd_info.framebuffer;
  149. LCD_initStruct.Background = 0xFFFFFF;
  150. LCD_initStruct.SampleEdge = LCD_SAMPLE_FALL;
  151. LCD_initStruct.IntEOTEn = 0;
  152. LCD_Init(LCD, &LCD_initStruct);
  153. LCD_Start(LCD);
  154. turn_on_lcd_backlight();
  155. __exit:
  156. if (result != RT_EOK)
  157. {
  158. if (rgb_lcd_obj.lcd_info.framebuffer)
  159. {
  160. rt_free(rgb_lcd_obj.lcd_info.framebuffer);
  161. }
  162. }
  163. return result;
  164. }
  165. INIT_BOARD_EXPORT(swm_rgb_lcd_init);
  166. int lcd_test(void)
  167. {
  168. struct swm_rgb_lcd_device *rgb_lcd;
  169. rgb_lcd = (struct swm_rgb_lcd_device *)rt_device_find("rgb_lcd");
  170. // while (1)
  171. {
  172. /* red */
  173. for (int i = 0; i < LCD_BUF_SIZE / 2; i++)
  174. {
  175. rgb_lcd->lcd_info.framebuffer[2 * i] = 0x00;
  176. rgb_lcd->lcd_info.framebuffer[2 * i + 1] = 0xF8;
  177. }
  178. rgb_lcd->parent.control(&rgb_lcd->parent, RTGRAPHIC_CTRL_RECT_UPDATE, RT_NULL);
  179. rt_thread_mdelay(1000);
  180. /* green */
  181. for (int i = 0; i < LCD_BUF_SIZE / 2; i++)
  182. {
  183. rgb_lcd->lcd_info.framebuffer[2 * i] = 0xE0;
  184. rgb_lcd->lcd_info.framebuffer[2 * i + 1] = 0x07;
  185. }
  186. rgb_lcd->parent.control(&rgb_lcd->parent, RTGRAPHIC_CTRL_RECT_UPDATE, RT_NULL);
  187. rt_thread_mdelay(1000);
  188. /* blue */
  189. for (int i = 0; i < LCD_BUF_SIZE / 2; i++)
  190. {
  191. rgb_lcd->lcd_info.framebuffer[2 * i] = 0x1F;
  192. rgb_lcd->lcd_info.framebuffer[2 * i + 1] = 0x00;
  193. }
  194. rgb_lcd->parent.control(&rgb_lcd->parent, RTGRAPHIC_CTRL_RECT_UPDATE, RT_NULL);
  195. rt_thread_mdelay(1000);
  196. }
  197. return 0;
  198. }
  199. MSH_CMD_EXPORT(lcd_test, lcd_test);
  200. #endif /* BSP_USING_RGB_LCD */