drv_lcd_spi.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. * 2020-08-07 NU-LL first version
  9. */
  10. #include <board.h>
  11. #ifdef BSP_USING_LCD_SPI
  12. #include <lcd.h>
  13. #include <lcd_port.h>
  14. #include <string.h>
  15. #define DRV_DEBUG
  16. #define LOG_TAG "drv.lcd"
  17. #include <drv_log.h>
  18. #define LCD_DEVICE(dev) (struct drv_lcd_device*)(dev)
  19. struct drv_lcd_device
  20. {
  21. struct rt_device parent;
  22. struct rt_device_graphic_info lcd_info;
  23. struct rt_semaphore lcd_lock;
  24. /* 0:front_buf is being used 1: back_buf is being used*/
  25. rt_uint8_t cur_buf;
  26. rt_uint8_t *front_buf;
  27. rt_uint8_t *back_buf;
  28. };
  29. struct drv_lcd_device _lcd;
  30. static rt_err_t drv_lcd_init(struct rt_device *device)
  31. {
  32. struct drv_lcd_device *lcd = LCD_DEVICE(device);
  33. /* nothing, right now */
  34. lcd = lcd;
  35. return RT_EOK;
  36. }
  37. static rt_err_t drv_lcd_control(struct rt_device *device, int cmd, void *args)
  38. {
  39. struct drv_lcd_device *lcd = LCD_DEVICE(device);
  40. switch (cmd)
  41. {
  42. case RTGRAPHIC_CTRL_RECT_UPDATE:
  43. {
  44. LCD_FillRGBRect(0, 0, _lcd.lcd_info.framebuffer, _lcd.lcd_info.width, _lcd.lcd_info.height);
  45. }
  46. break;
  47. case RTGRAPHIC_CTRL_GET_INFO:
  48. {
  49. struct rt_device_graphic_info *info = (struct rt_device_graphic_info *)args;
  50. RT_ASSERT(info != RT_NULL);
  51. info->pixel_format = lcd->lcd_info.pixel_format;
  52. info->bits_per_pixel = 16;
  53. info->width = lcd->lcd_info.width;
  54. info->height = lcd->lcd_info.height;
  55. info->framebuffer = lcd->lcd_info.framebuffer;
  56. }
  57. break;
  58. }
  59. return RT_EOK;
  60. }
  61. rt_err_t stm32_lcd_init(struct drv_lcd_device *lcd)
  62. {
  63. rt_err_t result;
  64. struct rt_spi_device *spi_device;
  65. struct rt_spi_configuration cfg;
  66. /* attach the device to spi bus*/
  67. spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  68. RT_ASSERT(spi_device != RT_NULL);
  69. result = rt_spi_bus_attach_device(spi_device, LCD_SPI_DEV_NAME, LCD_SPI_BUS_NAME, (void *)RT_NULL);
  70. if (result != RT_EOK)
  71. {
  72. LOG_E("%s attach to %s faild, %d\n", LCD_SPI_DEV_NAME, LCD_SPI_BUS_NAME, result);
  73. }
  74. RT_ASSERT(result == RT_EOK);
  75. cfg.data_width = 8;
  76. cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB | RT_SPI_NO_CS | RT_SPI_3WIRE;
  77. cfg.max_hz = 20 * 1000 *1000; /* 20M */
  78. rt_spi_configure(spi_device, &cfg);
  79. return result;
  80. }
  81. #ifdef RT_USING_DEVICE_OPS
  82. const static struct rt_device_ops lcd_ops =
  83. {
  84. drv_lcd_init,
  85. RT_NULL,
  86. RT_NULL,
  87. RT_NULL,
  88. RT_NULL,
  89. drv_lcd_control
  90. };
  91. #endif
  92. int drv_lcd_hw_init(void)
  93. {
  94. rt_err_t result = RT_EOK;
  95. struct rt_device *device = &_lcd.parent;
  96. /* memset _lcd to zero */
  97. memset(&_lcd, 0x00, sizeof(_lcd));
  98. /* init lcd_lock semaphore */
  99. result = rt_sem_init(&_lcd.lcd_lock, "lcd_lock", 0, RT_IPC_FLAG_FIFO);
  100. if (result != RT_EOK)
  101. {
  102. LOG_E("init semaphore failed!\n");
  103. result = -RT_ENOMEM;
  104. goto __exit;
  105. }
  106. /* config LCD dev info */
  107. _lcd.lcd_info.height = LCD_HEIGHT;
  108. _lcd.lcd_info.width = LCD_WIDTH;
  109. _lcd.lcd_info.bits_per_pixel = LCD_BITS_PER_PIXEL;
  110. _lcd.lcd_info.pixel_format = LCD_PIXEL_FORMAT;
  111. /* malloc memory for Triple Buffering */
  112. _lcd.lcd_info.framebuffer = rt_malloc(LCD_BUF_SIZE);
  113. _lcd.back_buf = rt_malloc(LCD_BUF_SIZE);
  114. _lcd.front_buf = rt_malloc(LCD_BUF_SIZE);
  115. if (_lcd.lcd_info.framebuffer == RT_NULL || _lcd.back_buf == RT_NULL || _lcd.front_buf == RT_NULL)
  116. {
  117. LOG_E("init frame buffer failed!\n");
  118. result = -RT_ENOMEM;
  119. goto __exit;
  120. }
  121. /* memset buff to 0xFF */
  122. memset(_lcd.lcd_info.framebuffer, 0xFF, LCD_BUF_SIZE);
  123. memset(_lcd.back_buf, 0xFF, LCD_BUF_SIZE);
  124. memset(_lcd.front_buf, 0xFF, LCD_BUF_SIZE);
  125. device->type = RT_Device_Class_Graphic;
  126. #ifdef RT_USING_DEVICE_OPS
  127. device->ops = &lcd_ops;
  128. #else
  129. device->init = drv_lcd_init;
  130. device->control = drv_lcd_control;
  131. #endif
  132. /* register lcd device */
  133. rt_device_register(device, "lcd", RT_DEVICE_FLAG_RDWR);
  134. /* init stm32 LTDC */
  135. if (stm32_lcd_init(&_lcd) != RT_EOK)
  136. {
  137. result = -RT_ERROR;
  138. goto __exit;
  139. }
  140. else
  141. {
  142. LCD_SetBrightness(MAX_BRIGHTNESS);
  143. }
  144. __exit:
  145. if (result != RT_EOK)
  146. {
  147. rt_sem_delete(&_lcd.lcd_lock);
  148. if (_lcd.lcd_info.framebuffer)
  149. {
  150. rt_free(_lcd.lcd_info.framebuffer);
  151. }
  152. if (_lcd.back_buf)
  153. {
  154. rt_free(_lcd.back_buf);
  155. }
  156. if (_lcd.front_buf)
  157. {
  158. rt_free(_lcd.front_buf);
  159. }
  160. }
  161. return result;
  162. }
  163. INIT_DEVICE_EXPORT(drv_lcd_hw_init);
  164. #ifdef DRV_DEBUG
  165. #ifdef FINSH_USING_MSH
  166. int lcd_test()
  167. {
  168. struct drv_lcd_device *lcd;
  169. lcd = (struct drv_lcd_device *)rt_device_find("lcd");
  170. while (1)
  171. {
  172. /* red */
  173. for (int i = 0; i < LCD_BUF_SIZE / 2; i++)
  174. {
  175. lcd->lcd_info.framebuffer[2 * i] = 0x00;
  176. lcd->lcd_info.framebuffer[2 * i + 1] = 0xF8;
  177. }
  178. lcd->parent.control(&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. lcd->lcd_info.framebuffer[2 * i] = 0xE0;
  184. lcd->lcd_info.framebuffer[2 * i + 1] = 0x07;
  185. }
  186. lcd->parent.control(&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. lcd->lcd_info.framebuffer[2 * i] = 0x1F;
  192. lcd->lcd_info.framebuffer[2 * i + 1] = 0x00;
  193. }
  194. lcd->parent.control(&lcd->parent, RTGRAPHIC_CTRL_RECT_UPDATE, RT_NULL);
  195. rt_thread_mdelay(1000);
  196. }
  197. }
  198. MSH_CMD_EXPORT(lcd_test, lcd_test);
  199. #endif /* FINSH_USING_MSH */
  200. #endif /* DRV_DEBUG */
  201. #endif /* BSP_USING_LCD */