display_controller.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2011-08-09 lgnq first version for LS1B DC
  9. * 2015-07-06 chinesebear modified for loongson 1c
  10. * 2018-01-06 sundm75 modified for smartloong
  11. */
  12. #include <rtthread.h>
  13. #include "display_controller.h"
  14. #include "../../libraries/ls1c_pwm.h"
  15. #include "../../libraries/ls1c_public.h"
  16. #include "../../libraries/ls1c_gpio.h"
  17. #include "../../libraries/ls1c_pin.h"
  18. #if (defined PKG_USING_GUIENGINE) || (defined RT_USING_RTGUI)
  19. struct vga_struct vga_mode[] =
  20. {
  21. {/*"480x272_60.00"*/ 111000, 480, 482, 523, 525, 272, 274, 284, 286, },
  22. {/*"640x480_70.00"*/ 28560, 640, 664, 728, 816, 480, 481, 484, 500, },
  23. {/*"640x640_60.00"*/ 33100, 640, 672, 736, 832, 640, 641, 644, 663, },
  24. {/*"640x768_60.00"*/ 39690, 640, 672, 736, 832, 768, 769, 772, 795, },
  25. {/*"640x800_60.00"*/ 42130, 640, 680, 744, 848, 800, 801, 804, 828, },
  26. {/*"800x480_70.00"*/ 35840, 800, 832, 912, 1024, 480, 481, 484, 500, },
  27. {/*"800x600_60.00"*/ 38220, 800, 832, 912, 1024, 600, 601, 604, 622, },
  28. {/*"800x640_60.00"*/ 40730, 800, 832, 912, 1024, 640, 641, 644, 663, },
  29. {/*"832x600_60.00"*/ 40010, 832, 864, 952, 1072, 600, 601, 604, 622, },
  30. {/*"832x608_60.00"*/ 40520, 832, 864, 952, 1072, 608, 609, 612, 630, },
  31. {/*"1024x480_60.00"*/ 38170, 1024, 1048, 1152, 1280, 480, 481, 484, 497, },
  32. {/*"1024x600_60.00"*/ 48960, 1024, 1064, 1168, 1312, 600, 601, 604, 622, },
  33. {/*"1024x640_60.00"*/ 52830, 1024, 1072, 1176, 1328, 640, 641, 644, 663, },
  34. {/*"1024x768_60.00"*/ 64110, 1024, 1080, 1184, 1344, 768, 769, 772, 795, },
  35. {/*"1152x764_60.00"*/ 71380, 1152, 1208, 1328, 1504, 764, 765, 768, 791, },
  36. {/*"1280x800_60.00"*/ 83460, 1280, 1344, 1480, 1680, 800, 801, 804, 828, },
  37. {/*"1280x1024_55.00"*/ 98600, 1280, 1352, 1488, 1696, 1024, 1025, 1028, 1057, },
  38. {/*"1440x800_60.00"*/ 93800, 1440, 1512, 1664, 1888, 800, 801, 804, 828, },
  39. {/*"1440x900_67.00"*/ 120280, 1440, 1528, 1680, 1920, 900, 901, 904, 935, },
  40. };
  41. ALIGN(16)
  42. volatile rt_uint16_t _rt_framebuffer[FB_YSIZE][FB_XSIZE];
  43. static struct rt_device_graphic_info _dc_info;
  44. static void pwminit(void)
  45. {
  46. pwm_info_t pwm_info;
  47. pwm_info.gpio = LS1C_PWM0_GPIO06; // pwm引脚位gpio06
  48. pwm_info.mode = PWM_MODE_NORMAL; // 正常模式--连续输出pwm波形
  49. pwm_info.duty = 0.85; // pwm占空比 85%
  50. pwm_info.period_ns = 5*1000*1000; // pwm周期5ms
  51. /*pwm初始化,初始化后立即产生pwm波形*/
  52. pwm_init(&pwm_info);
  53. /* 使能pwm */
  54. pwm_enable(&pwm_info);
  55. }
  56. int caclulate_freq(rt_uint32_t XIN, rt_uint32_t PCLK)
  57. {
  58. rt_uint32_t divider_int;
  59. rt_uint32_t needed_pixclk;
  60. rt_uint32_t pll_clk, pix_div;
  61. rt_uint32_t regval;
  62. pll_clk = PLL_FREQ; // 读CPU的 PLL及SDRAM 分频系数
  63. pll_clk =( pll_clk>>8 )& 0xff;
  64. pll_clk = XIN * pll_clk / 4 ;
  65. pix_div = PLL_DIV_PARAM;//读CPU的 CPU/CAMERA/DC 分频系数
  66. pix_div = (pix_div>>24)&0xff;
  67. rt_kprintf("old pll_clk=%d, pix_div=%d\n", pll_clk, pix_div);
  68. divider_int = pll_clk/(1000000) *PCLK/1000;
  69. if(divider_int%1000>=500)
  70. divider_int = divider_int/1000+1;
  71. else
  72. divider_int = divider_int/1000;
  73. rt_kprintf("divider_int = %d\n", divider_int);
  74. /* check whether divisor is too small. */
  75. if (divider_int < 1) {
  76. rt_kprintf("Warning: clock source is too slow.Try smaller resolution\n");
  77. divider_int = 1;
  78. }
  79. else if(divider_int > 100) {
  80. rt_kprintf("Warning: clock source is too fast.Try smaller resolution\n");
  81. divider_int = 100;
  82. }
  83. /* 配置分频寄存器 */
  84. {
  85. rt_uint32_t regval = 0;
  86. regval = PLL_DIV_PARAM;
  87. /*首先需要把分频使能位清零 */
  88. regval &= ~0x80000030; //PIX_DIV_VALID PIX_SEL 置0
  89. regval &= ~(0x3f<<24); //PIX_DIV 清零
  90. regval |= divider_int << 24;
  91. PLL_DIV_PARAM = regval;
  92. regval |= 0x80000030; //PIX_DIV_VALID PIX_SEL 置1
  93. PLL_DIV_PARAM = regval;
  94. }
  95. rt_kprintf("new PLL_FREQ=0x%x, PLL_DIV_PARAM=0x%x\n", PLL_FREQ, PLL_DIV_PARAM);
  96. rt_thread_delay(10);
  97. return 0;
  98. }
  99. static rt_err_t rt_dc_init(rt_device_t dev)
  100. {
  101. int i, out, mode=-1;
  102. int val;
  103. rt_kprintf("PWM initied\n");
  104. /* Set the back light PWM. */
  105. pwminit();
  106. for (i=0; i<sizeof(vga_mode)/sizeof(struct vga_struct); i++)
  107. {
  108. if (vga_mode[i].hr == FB_XSIZE && vga_mode[i].vr == FB_YSIZE)
  109. {
  110. mode=i;
  111. /* 计算时钟 配置频率*/
  112. caclulate_freq(OSC, vga_mode[i].pclk);
  113. break;
  114. }
  115. }
  116. if (mode<0)
  117. {
  118. rt_kprintf("\n\n\nunsupported framebuffer resolution\n\n\n");
  119. return;
  120. }
  121. DC_FB_CONFIG = 0x0;
  122. DC_FB_CONFIG = 0x3; // // framebuffer configuration RGB565
  123. DC_FB_BUFFER_ADDR0 = (rt_uint32_t)_rt_framebuffer - 0x80000000;
  124. DC_FB_BUFFER_ADDR1 = (rt_uint32_t)_rt_framebuffer - 0x80000000;
  125. DC_DITHER_CONFIG = 0x0; //颜色抖动配置寄存器
  126. DC_DITHER_TABLE_LOW = 0x0; //颜色抖动查找表低位寄存器
  127. DC_DITHER_TABLE_HIGH = 0x0; //颜色抖动查找表高位寄存器
  128. DC_PANEL_CONFIG = 0x80001311; //液晶面板配置寄存器
  129. DC_PANEL_TIMING = 0x0;
  130. DC_HDISPLAY = (vga_mode[mode].hfl<<16) | vga_mode[mode].hr;
  131. DC_HSYNC = 0x40000000 | (vga_mode[mode].hse<<16) | vga_mode[mode].hss;
  132. DC_VDISPLAY = (vga_mode[mode].vfl<<16) | vga_mode[mode].vr;
  133. DC_VSYNC = 0x40000000 | (vga_mode[mode].vse<<16) | vga_mode[mode].vss;
  134. #if defined(CONFIG_VIDEO_32BPP)
  135. DC_FB_CONFIG = 0x00100105;
  136. DC_FB_BUFFER_STRIDE = FB_XSIZE*4;
  137. #elif defined(CONFIG_VIDEO_24BPP)
  138. DC_FB_CONFIG = 0x00100104;
  139. DC_FB_BUFFER_STRIDE = (FB_XSIZE*4+255)&(~255);
  140. #elif defined(CONFIG_VIDEO_16BPP)// 使用这个选项
  141. DC_FB_CONFIG = 0x00100103;
  142. DC_FB_BUFFER_STRIDE = (FB_XSIZE*2+0x7f)&(~0x7f);
  143. #elif defined(CONFIG_VIDEO_15BPP)
  144. DC_FB_CONFIG = 0x00100102;
  145. DC_FB_BUFFER_STRIDE = (FB_XSIZE*2+255)&(~255);
  146. #elif defined(CONFIG_VIDEO_12BPP)
  147. DC_FB_CONFIG = 0x00100101;
  148. DC_FB_BUFFER_STRIDE = (FB_XSIZE*2+255)&(~255);
  149. #else
  150. DC_FB_CONFIG = 0x00100104;
  151. DC_FB_BUFFER_STRIDE = (FB_XSIZE*4+255)&(~255);
  152. #endif
  153. return RT_EOK;
  154. }
  155. static rt_err_t rt_dc_control(rt_device_t dev, int cmd, void *args)
  156. {
  157. switch (cmd)
  158. {
  159. case RTGRAPHIC_CTRL_RECT_UPDATE:
  160. break;
  161. case RTGRAPHIC_CTRL_POWERON:
  162. break;
  163. case RTGRAPHIC_CTRL_POWEROFF:
  164. break;
  165. case RTGRAPHIC_CTRL_GET_INFO:
  166. rt_memcpy(args, &_dc_info, sizeof(_dc_info));
  167. break;
  168. case RTGRAPHIC_CTRL_SET_MODE:
  169. break;
  170. }
  171. return RT_EOK;
  172. }
  173. void rt_hw_dc_init(void)
  174. {
  175. rt_device_t dc = rt_malloc(sizeof(struct rt_device));
  176. if (dc == RT_NULL)
  177. {
  178. rt_kprintf("dc == RT_NULL\n");
  179. return; /* no memory yet */
  180. }
  181. _dc_info.bits_per_pixel = 16;
  182. _dc_info.pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565P;
  183. _dc_info.framebuffer = (rt_uint8_t*)HW_FB_ADDR;
  184. _dc_info.width = FB_XSIZE;
  185. _dc_info.height = FB_YSIZE;
  186. /* init device structure */
  187. dc->type = RT_Device_Class_Graphic;
  188. dc->init = rt_dc_init;
  189. dc->open = RT_NULL;
  190. dc->close = RT_NULL;
  191. dc->control = rt_dc_control;
  192. dc->user_data = (void*)&_dc_info;
  193. /* register Display Controller device to RT-Thread */
  194. rt_device_register(dc, "dc", RT_DEVICE_FLAG_RDWR);
  195. rt_device_init(dc);
  196. }
  197. #include <rtgui/driver.h>
  198. #include "display_controller.h"
  199. /* initialize for gui driver */
  200. int rtgui_lcd_init(void)
  201. {
  202. rt_device_t dc;
  203. rt_kprintf("DC initied\n");
  204. pin_set_purpose(76, PIN_PURPOSE_OTHER);
  205. pin_set_remap(76, PIN_REMAP_DEFAULT);
  206. /* init Display Controller */
  207. rt_hw_dc_init();
  208. /* find Display Controller device */
  209. dc = rt_device_find("dc");
  210. /* set Display Controller device as rtgui graphic driver */
  211. rtgui_graphic_set_device(dc);
  212. return 0;
  213. }
  214. INIT_DEVICE_EXPORT(rtgui_lcd_init);
  215. #endif