drv_lcd.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-11-24 Rbb666 the first version
  9. */
  10. #include <rtthread.h>
  11. #if (defined(BSP_USING_LCD)) || (defined(SOC_SERIES_R7FA6M3))
  12. #include <lcd_port.h>
  13. #include "r_display_api.h"
  14. #include "hal_data.h"
  15. //#define DRV_DEBUG
  16. //#define LOG_TAG "drv.lcd"
  17. //#include <drv_log.h>
  18. struct drv_lcd_device
  19. {
  20. struct rt_device parent;
  21. struct rt_device_graphic_info lcd_info;
  22. void *framebuffer;
  23. };
  24. struct drv_lcd_device _lcd;
  25. uint16_t screen_rotation;
  26. uint16_t *lcd_current_working_buffer = (uint16_t *) &fb_background[0];
  27. // jpeg and lvgl can only select one
  28. __WEAK void _ra_port_display_callback(display_callback_args_t *p_args)
  29. {
  30. if (DISPLAY_EVENT_LINE_DETECTION == p_args->event)
  31. {
  32. }
  33. }
  34. void turn_on_lcd_backlight(void)
  35. {
  36. #ifdef BSP_USING_PWM5
  37. #define LCD_PWM_DEV_NAME "pwm5"
  38. #define LCD_PWM_DEV_CHANNEL 0
  39. struct rt_device_pwm *pwm_dev;
  40. /* turn on the LCD backlight */
  41. pwm_dev = (struct rt_device_pwm *)rt_device_find(LCD_PWM_DEV_NAME);
  42. /* pwm frequency:100K = 10000ns */
  43. rt_pwm_set(pwm_dev, LCD_PWM_DEV_CHANNEL, 10000, 7000);
  44. rt_pwm_enable(pwm_dev, LCD_PWM_DEV_CHANNEL);
  45. #endif
  46. rt_pin_mode(LCD_BL_PIN, PIN_MODE_OUTPUT); /* LCD_BL */
  47. rt_pin_write(LCD_BL_PIN, PIN_HIGH);
  48. }
  49. void ra_bsp_lcd_init(void)
  50. {
  51. fsp_err_t error;
  52. // Set screen rotation to default view
  53. screen_rotation = ROTATION_ZERO;
  54. /** Display driver open */
  55. error = R_GLCDC_Open(&g_display0_ctrl, &g_display0_cfg);
  56. if (FSP_SUCCESS == error)
  57. {
  58. /** Display driver start */
  59. error = R_GLCDC_Start(&g_display0_ctrl);
  60. }
  61. }
  62. void ra_bsp_lcd_set_display_buffer(uint8_t index)
  63. {
  64. R_GLCDC_BufferChange(&g_display0_ctrl, fb_background[index - 1], DISPLAY_FRAME_LAYER_1);
  65. }
  66. void ra_bsp_lcd_set_working_buffer(uint8_t index)
  67. {
  68. if (index >= 1 && index <= 2)
  69. {
  70. lcd_current_working_buffer = (uint16_t *)fb_background[index - 1];
  71. }
  72. }
  73. void ra_bsp_lcd_enable_double_buffer(void)
  74. {
  75. ra_bsp_lcd_set_display_buffer(1);
  76. ra_bsp_lcd_set_working_buffer(2);
  77. }
  78. void ra_bsp_lcd_clear(uint16_t color)
  79. {
  80. for (uint32_t i = 0; i < (LCD_WIDTH * LCD_HEIGHT); i++)
  81. {
  82. lcd_current_working_buffer[i] = color;
  83. }
  84. }
  85. void ra_bsp_lcd_swap_buffer(void)
  86. {
  87. if (lcd_current_working_buffer == (uint16_t *)fb_background[0])
  88. {
  89. ra_bsp_lcd_set_display_buffer(1);
  90. ra_bsp_lcd_set_working_buffer(2);
  91. }
  92. else
  93. {
  94. ra_bsp_lcd_set_display_buffer(2);
  95. ra_bsp_lcd_set_working_buffer(1);
  96. }
  97. }
  98. void bsp_lcd_draw_pixel(uint32_t x, uint32_t y, uint16_t color)
  99. {
  100. // Verify pixel is within LCD range
  101. if ((x <= LCD_WIDTH) && (y <= LCD_HEIGHT))
  102. {
  103. switch (screen_rotation)
  104. {
  105. case ROTATION_ZERO:
  106. {
  107. lcd_current_working_buffer[(y * LCD_WIDTH) + x] = color;
  108. break;
  109. }
  110. case ROTATION_180:
  111. {
  112. lcd_current_working_buffer[((LCD_HEIGHT - y) * LCD_WIDTH) + (LCD_WIDTH - x)] = color;
  113. break;
  114. }
  115. default:
  116. {
  117. lcd_current_working_buffer[(y * LCD_WIDTH) + x] = color;
  118. break;
  119. }
  120. }
  121. }
  122. else
  123. {
  124. rt_kprintf("draw pixel outof range:%d,%d\n", x, y);
  125. }
  126. }
  127. void lcd_fill_array(uint16_t x_start, uint16_t y_start, uint16_t x_end, uint16_t y_end, void *pcolor)
  128. {
  129. uint16_t *pixel = RT_NULL;
  130. uint16_t cycle_y, x_offset = 0;
  131. pixel = (uint16_t *)pcolor;
  132. for (cycle_y = y_start; cycle_y <= y_end;)
  133. {
  134. for (x_offset = 0; x_start + x_offset <= x_end; x_offset++)
  135. {
  136. bsp_lcd_draw_pixel(x_start + x_offset, cycle_y, *pixel++);
  137. }
  138. cycle_y++;
  139. }
  140. }
  141. static rt_err_t ra_lcd_init(rt_device_t device)
  142. {
  143. RT_ASSERT(device != RT_NULL);
  144. ra_bsp_lcd_init();
  145. return RT_EOK;
  146. }
  147. static rt_err_t ra_lcd_control(rt_device_t device, int cmd, void *args)
  148. {
  149. struct drv_lcd_device *lcd = (struct drv_lcd_device *)device;
  150. switch (cmd)
  151. {
  152. case RTGRAPHIC_CTRL_RECT_UPDATE:
  153. {
  154. struct rt_device_rect_info *info = (struct rt_device_rect_info *)args;
  155. (void)info; /* nothing, right now */
  156. rt_kprintf("update screen...\n");
  157. }
  158. break;
  159. case RTGRAPHIC_CTRL_POWERON:
  160. rt_pin_write(LCD_BL_PIN, PIN_HIGH);
  161. break;
  162. case RTGRAPHIC_CTRL_POWEROFF:
  163. rt_pin_write(LCD_BL_PIN, PIN_LOW);
  164. break;
  165. case RTGRAPHIC_CTRL_GET_INFO:
  166. {
  167. struct rt_device_graphic_info *info = (struct rt_device_graphic_info *)args;
  168. RT_ASSERT(info != RT_NULL);
  169. info->pixel_format = lcd->lcd_info.pixel_format;
  170. info->bits_per_pixel = 16;
  171. info->width = lcd->lcd_info.width;
  172. info->height = lcd->lcd_info.height;
  173. info->framebuffer = lcd->lcd_info.framebuffer;
  174. }
  175. break;
  176. case RTGRAPHIC_CTRL_SET_MODE:
  177. break;
  178. }
  179. return RT_EOK;
  180. }
  181. int rt_hw_lcd_init(void)
  182. {
  183. rt_err_t result = RT_EOK;
  184. struct rt_device *device = &_lcd.parent;
  185. /* memset _lcd to zero */
  186. memset(&_lcd, 0x00, sizeof(_lcd));
  187. /* config LCD dev info */
  188. _lcd.lcd_info.height = LCD_HEIGHT;
  189. _lcd.lcd_info.width = LCD_WIDTH;
  190. _lcd.lcd_info.bits_per_pixel = LCD_BITS_PER_PIXEL;
  191. _lcd.lcd_info.pixel_format = LCD_PIXEL_FORMAT;
  192. _lcd.lcd_info.framebuffer = (uint8_t *)&fb_background[0];
  193. device->type = RT_Device_Class_Graphic;
  194. #ifdef RT_USING_DEVICE_OPS
  195. device->ops = &lcd_ops;
  196. #else
  197. device->init = ra_lcd_init;
  198. device->control = ra_lcd_control;
  199. #endif
  200. /* register lcd device */
  201. rt_device_register(device, "lcd", RT_DEVICE_FLAG_RDWR);
  202. turn_on_lcd_backlight();
  203. // Initialize lcd controller
  204. ra_lcd_init(device);
  205. ra_bsp_lcd_set_display_buffer(1);
  206. screen_rotation = ROTATION_ZERO;
  207. return result;
  208. }
  209. INIT_DEVICE_EXPORT(rt_hw_lcd_init);
  210. int lcd_test()
  211. {
  212. struct drv_lcd_device *lcd;
  213. lcd = (struct drv_lcd_device *)rt_device_find("lcd");
  214. while (1)
  215. {
  216. /* red */
  217. for (int i = 0; i < LCD_BUF_SIZE / 2; i++)
  218. {
  219. lcd->lcd_info.framebuffer[2 * i] = 0x00;
  220. lcd->lcd_info.framebuffer[2 * i + 1] = 0xF8;
  221. }
  222. lcd->parent.control(&lcd->parent, RTGRAPHIC_CTRL_RECT_UPDATE, RT_NULL);
  223. rt_thread_mdelay(1000);
  224. /* green */
  225. for (int i = 0; i < LCD_BUF_SIZE / 2; i++)
  226. {
  227. lcd->lcd_info.framebuffer[2 * i] = 0xE0;
  228. lcd->lcd_info.framebuffer[2 * i + 1] = 0x07;
  229. }
  230. lcd->parent.control(&lcd->parent, RTGRAPHIC_CTRL_RECT_UPDATE, RT_NULL);
  231. rt_thread_mdelay(1000);
  232. /* blue */
  233. for (int i = 0; i < LCD_BUF_SIZE / 2; i++)
  234. {
  235. lcd->lcd_info.framebuffer[2 * i] = 0x1F;
  236. lcd->lcd_info.framebuffer[2 * i + 1] = 0x00;
  237. }
  238. lcd->parent.control(&lcd->parent, RTGRAPHIC_CTRL_RECT_UPDATE, RT_NULL);
  239. rt_thread_mdelay(1000);
  240. }
  241. }
  242. MSH_CMD_EXPORT(lcd_test, lcd_test);
  243. #endif /* BSP_USING_LCD */