display_controller.c 8.3 KB

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