drv_clcd.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020/12/31 Bernard Add license info
  9. */
  10. #include <stdint.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <rtthread.h>
  14. #include "drv_clcd.h"
  15. #define CLCD_WIDTH (BSP_LCD_WIDTH)
  16. #define CLCD_HEIGHT (BSP_LCD_HEIGHT)
  17. #define CLCD_DEVICE(dev) (struct drv_clcd_device*)(dev)
  18. #define PL111_CR_EN 0x001
  19. #define PL111_CR_PWR 0x800
  20. #define PL111_IOBASE 0x10020000
  21. #define PL111_PALBASE (PL111_IOBASE + 0x200)
  22. typedef struct _PL111MMIO
  23. {
  24. uint32_t volatile tim0; //0
  25. uint32_t volatile tim1; //4
  26. uint32_t volatile tim2; //8
  27. uint32_t volatile tim3; //c
  28. uint32_t volatile upbase; //10
  29. uint32_t volatile f; //14
  30. uint32_t volatile control; //18
  31. uint32_t volatile g; //1c
  32. } PL111MMIO;
  33. struct drv_clcd_device
  34. {
  35. struct rt_device parent;
  36. int width;
  37. int height;
  38. uint8_t *fb;
  39. };
  40. struct drv_clcd_device _lcd;
  41. static rt_err_t drv_clcd_init(struct rt_device *device)
  42. {
  43. struct drv_clcd_device *lcd = CLCD_DEVICE(device);
  44. lcd = lcd; /* nothing, right now */
  45. return RT_EOK;
  46. }
  47. static rt_err_t drv_clcd_control(struct rt_device *device, int cmd, void *args)
  48. {
  49. struct drv_clcd_device *lcd = CLCD_DEVICE(device);
  50. switch (cmd)
  51. {
  52. case RTGRAPHIC_CTRL_RECT_UPDATE:
  53. {
  54. struct rt_device_rect_info *info = (struct rt_device_rect_info*)args;
  55. info = info; /* nothing, right now */
  56. }
  57. break;
  58. case RTGRAPHIC_CTRL_GET_INFO:
  59. {
  60. struct rt_device_graphic_info* info = (struct rt_device_graphic_info*)args;
  61. RT_ASSERT(info != RT_NULL);
  62. info->pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565;
  63. // info->pixel_format = RTGRAPHIC_PIXEL_FORMAT_ARGB888;
  64. info->bits_per_pixel= 16;
  65. info->width = lcd->width;
  66. info->height = lcd->height;
  67. info->framebuffer = lcd->fb;
  68. }
  69. break;
  70. }
  71. return RT_EOK;
  72. }
  73. #ifdef RT_USING_DEVICE_OPS
  74. const static struct rt_device_ops clcd_ops =
  75. {
  76. drv_clcd_init,
  77. RT_NULL,
  78. RT_NULL,
  79. RT_NULL,
  80. RT_NULL,
  81. drv_clcd_control
  82. };
  83. #endif
  84. int drv_clcd_hw_init(void)
  85. {
  86. PL111MMIO *plio;
  87. struct rt_device *device = &_lcd.parent;
  88. /* memset _lcd to zero */
  89. memset(&_lcd, 0x0, sizeof(_lcd));
  90. _lcd.width = CLCD_WIDTH;
  91. _lcd.height = CLCD_HEIGHT;
  92. _lcd.fb = rt_malloc(_lcd.width * _lcd.height * 2);
  93. if (_lcd.fb == NULL)
  94. {
  95. rt_kprintf("initialize frame buffer failed!\n");
  96. return -1;
  97. }
  98. memset(_lcd.fb, 0xff, _lcd.width * _lcd.height * 2);
  99. plio = (PL111MMIO*)PL111_IOBASE;
  100. plio->tim0 = 0x3F1F3C00 | ((CLCD_WIDTH/16 - 1) << 2);
  101. plio->tim1 = 0x080B6000 | (CLCD_HEIGHT - 1);
  102. plio->upbase = (uint32_t)_lcd.fb;
  103. /* 16-bit 565 color */
  104. plio->control = 0x1921 | (0x6 << 1);
  105. device->type = RT_Device_Class_Graphic;
  106. #ifdef RT_USING_DEVICE_OPS
  107. device->ops = &clcd_ops;
  108. #else
  109. device->init = drv_clcd_init;
  110. device->control = drv_clcd_control;
  111. #endif
  112. rt_device_register(device, "lcd", RT_DEVICE_FLAG_RDWR);
  113. return 0;
  114. }
  115. INIT_DEVICE_EXPORT(drv_clcd_hw_init);