drv_lcd.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (c) 2006-2022, 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. void turn_on_lcd_backlight(void)
  28. {
  29. rt_pin_mode(LCD_BL_PIN, PIN_MODE_OUTPUT); /* LCD_BL */
  30. rt_pin_write(LCD_BL_PIN, PIN_HIGH);
  31. }
  32. void ra_bsp_lcd_init(void)
  33. {
  34. fsp_err_t error;
  35. // Set screen rotation to default view
  36. screen_rotation = ROTATION_ZERO;
  37. /** Display driver open */
  38. error = R_GLCDC_Open(&g_display0_ctrl, &g_display0_cfg);
  39. if (FSP_SUCCESS == error)
  40. {
  41. /** Display driver start */
  42. error = R_GLCDC_Start(&g_display0_ctrl);
  43. }
  44. }
  45. void ra_bsp_lcd_set_display_buffer(uint8_t index)
  46. {
  47. R_GLCDC_BufferChange(&g_display0_ctrl, fb_background[index - 1], DISPLAY_FRAME_LAYER_1);
  48. }
  49. void ra_bsp_lcd_set_working_buffer(uint8_t index)
  50. {
  51. if (index >= 1 && index <= 2)
  52. {
  53. lcd_current_working_buffer = (uint16_t *)fb_background[index - 1];
  54. }
  55. }
  56. void ra_bsp_lcd_enable_double_buffer(void)
  57. {
  58. ra_bsp_lcd_set_display_buffer(1);
  59. ra_bsp_lcd_set_working_buffer(2);
  60. }
  61. void ra_bsp_lcd_clear(uint16_t color)
  62. {
  63. for (uint32_t i = 0; i < (LCD_WIDTH * LCD_HEIGHT); i++)
  64. {
  65. lcd_current_working_buffer[i] = color;
  66. }
  67. }
  68. void ra_bsp_lcd_swap_buffer(void)
  69. {
  70. if (lcd_current_working_buffer == (uint16_t *)fb_background[0])
  71. {
  72. ra_bsp_lcd_set_display_buffer(1);
  73. ra_bsp_lcd_set_working_buffer(2);
  74. }
  75. else
  76. {
  77. ra_bsp_lcd_set_display_buffer(2);
  78. ra_bsp_lcd_set_working_buffer(1);
  79. }
  80. }
  81. void bsp_lcd_draw_pixel(uint32_t x, uint32_t y, uint16_t color)
  82. {
  83. // Verify pixel is within LCD range
  84. if ((x < LCD_WIDTH) && (y < LCD_HEIGHT))
  85. {
  86. switch (screen_rotation)
  87. {
  88. case ROTATION_ZERO:
  89. {
  90. lcd_current_working_buffer[(y * LCD_WIDTH) + x] = color;
  91. break;
  92. }
  93. case ROTATION_180:
  94. {
  95. lcd_current_working_buffer[((LCD_HEIGHT - y) * LCD_WIDTH) + (LCD_WIDTH - x)] = color;
  96. break;
  97. }
  98. default:
  99. {
  100. lcd_current_working_buffer[(y * LCD_WIDTH) + x] = color;
  101. break;
  102. }
  103. }
  104. }
  105. else
  106. {
  107. rt_kprintf("draw pixel outof range:%d,%d\n", x, y);
  108. }
  109. }
  110. void lcd_fill_array(uint16_t x_start, uint16_t y_start, uint16_t x_end, uint16_t y_end, void *pcolor)
  111. {
  112. uint16_t *pixel = RT_NULL;
  113. uint16_t cycle_y, x_offset = 0;
  114. pixel = (uint16_t *)pcolor;
  115. for (cycle_y = y_start; cycle_y <= y_end;)
  116. {
  117. for (x_offset = 0; x_start + x_offset <= x_end; x_offset++)
  118. {
  119. bsp_lcd_draw_pixel(x_start + x_offset, cycle_y, *pixel++);
  120. }
  121. cycle_y++;
  122. }
  123. }
  124. static rt_err_t ra_lcd_init(rt_device_t device)
  125. {
  126. RT_ASSERT(device != RT_NULL);
  127. ra_bsp_lcd_init();
  128. return RT_EOK;
  129. }
  130. static rt_err_t ra_lcd_control(rt_device_t device, int cmd, void *args)
  131. {
  132. struct drv_lcd_device *lcd = (struct drv_lcd_device *)device;
  133. switch (cmd)
  134. {
  135. case RTGRAPHIC_CTRL_RECT_UPDATE:
  136. {
  137. struct rt_device_rect_info *info = (struct rt_device_rect_info *)args;
  138. (void)info; /* nothing, right now */
  139. rt_kprintf("update screen...\n");
  140. }
  141. break;
  142. case RTGRAPHIC_CTRL_POWERON:
  143. rt_pin_write(LCD_BL_PIN, PIN_HIGH);
  144. break;
  145. case RTGRAPHIC_CTRL_POWEROFF:
  146. rt_pin_write(LCD_BL_PIN, PIN_LOW);
  147. break;
  148. case RTGRAPHIC_CTRL_GET_INFO:
  149. {
  150. struct rt_device_graphic_info *info = (struct rt_device_graphic_info *)args;
  151. RT_ASSERT(info != RT_NULL);
  152. info->pixel_format = lcd->lcd_info.pixel_format;
  153. info->bits_per_pixel = 16;
  154. info->width = lcd->lcd_info.width;
  155. info->height = lcd->lcd_info.height;
  156. info->framebuffer = lcd->lcd_info.framebuffer;
  157. }
  158. break;
  159. case RTGRAPHIC_CTRL_SET_MODE:
  160. break;
  161. }
  162. return RT_EOK;
  163. }
  164. int rt_hw_lcd_init(void)
  165. {
  166. rt_err_t result = RT_EOK;
  167. struct rt_device *device = &_lcd.parent;
  168. /* memset _lcd to zero */
  169. memset(&_lcd, 0x00, sizeof(_lcd));
  170. /* config LCD dev info */
  171. _lcd.lcd_info.height = LCD_HEIGHT;
  172. _lcd.lcd_info.width = LCD_WIDTH;
  173. _lcd.lcd_info.bits_per_pixel = LCD_BITS_PER_PIXEL;
  174. _lcd.lcd_info.pixel_format = LCD_PIXEL_FORMAT;
  175. _lcd.lcd_info.framebuffer = (uint8_t *)&fb_background[0];
  176. device->type = RT_Device_Class_Graphic;
  177. #ifdef RT_USING_DEVICE_OPS
  178. device->ops = &lcd_ops;
  179. #else
  180. device->init = ra_lcd_init;
  181. device->control = ra_lcd_control;
  182. #endif
  183. /* register lcd device */
  184. rt_device_register(device, "lcd", RT_DEVICE_FLAG_RDWR);
  185. turn_on_lcd_backlight();
  186. // Initialize lcd controller
  187. ra_lcd_init(device);
  188. ra_bsp_lcd_set_display_buffer(1);
  189. screen_rotation = ROTATION_ZERO;
  190. return result;
  191. }
  192. INIT_DEVICE_EXPORT(rt_hw_lcd_init);
  193. int lcd_test()
  194. {
  195. struct drv_lcd_device *lcd;
  196. lcd = (struct drv_lcd_device *)rt_device_find("lcd");
  197. while (1)
  198. {
  199. /* red */
  200. for (int i = 0; i < LCD_BUF_SIZE / 2; i++)
  201. {
  202. lcd->lcd_info.framebuffer[2 * i] = 0x00;
  203. lcd->lcd_info.framebuffer[2 * i + 1] = 0xF8;
  204. }
  205. lcd->parent.control(&lcd->parent, RTGRAPHIC_CTRL_RECT_UPDATE, RT_NULL);
  206. rt_thread_mdelay(1000);
  207. /* green */
  208. for (int i = 0; i < LCD_BUF_SIZE / 2; i++)
  209. {
  210. lcd->lcd_info.framebuffer[2 * i] = 0xE0;
  211. lcd->lcd_info.framebuffer[2 * i + 1] = 0x07;
  212. }
  213. lcd->parent.control(&lcd->parent, RTGRAPHIC_CTRL_RECT_UPDATE, RT_NULL);
  214. rt_thread_mdelay(1000);
  215. /* blue */
  216. for (int i = 0; i < LCD_BUF_SIZE / 2; i++)
  217. {
  218. lcd->lcd_info.framebuffer[2 * i] = 0x1F;
  219. lcd->lcd_info.framebuffer[2 * i + 1] = 0x00;
  220. }
  221. lcd->parent.control(&lcd->parent, RTGRAPHIC_CTRL_RECT_UPDATE, RT_NULL);
  222. rt_thread_mdelay(1000);
  223. }
  224. }
  225. MSH_CMD_EXPORT(lcd_test, lcd_test);
  226. #endif /* BSP_USING_LCD */