1
0

lcd_at070.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * File : lcd.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Develop Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://openlab.rt-thread.com/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2007-11-17 Yi.Qiu
  13. */
  14. /**
  15. * @addtogroup mini2440
  16. */
  17. /*@{*/
  18. #include <rtthread.h>
  19. #include <s3c24x0.h>
  20. #define MVAL (13)
  21. #define MVAL_USED (0) //0=each frame 1=rate by MVAL
  22. #define INVVDEN (1) //0=normal 1=inverted
  23. #define BSWP (0) //Byte swap control
  24. #define HWSWP (1) //Half word swap control
  25. #define M5D(n) ((n) & 0x1fffff) // To get lower 21bits
  26. //TFT 800480
  27. #define LCD_XSIZE_TFT_800480 (800)
  28. #define LCD_YSIZE_TFT_800480 (480)
  29. #define SCR_XSIZE_TFT_800480 (800)
  30. #define SCR_YSIZE_TFT_800480 (480)
  31. //TFT 800480
  32. #define HOZVAL_TFT_800480 (LCD_XSIZE_TFT_800480-1)
  33. #define LINEVAL_TFT_800480 (LCD_YSIZE_TFT_800480-1)
  34. //Timing parameter for innolux 7" AT070TN84
  35. #define VBPD_800480 (25) //垂直同步信号的后肩
  36. #define VFPD_800480 (5) //垂直同步信号的前肩
  37. #define VSPW_800480 (1) //垂直同步信号的脉宽
  38. #define HBPD_800480 (15)//67 //水平同步信号的后肩
  39. #define HFPD_800480 (40) //水平同步信号的前肩
  40. #define HSPW_800480 (31) //水平同步信号的脉宽
  41. #define CLKVAL_TFT_800480 (4)
  42. #define GPB1_TO_OUT() (GPBUP &= 0xfffd, GPBCON &= 0xfffffff3, GPBCON |= 0x00000004)
  43. #define GPB1_TO_1() (GPBDAT |= 0x0002)
  44. #define GPB1_TO_0() (GPBDAT &= 0xfffd)
  45. #define RT_HW_LCD_WIDTH LCD_XSIZE_TFT_800480
  46. #define RT_HW_LCD_HEIGHT SCR_YSIZE_TFT_800480
  47. #define S3C2410_LCDCON1_CLKVAL(x) ((x) << 8)
  48. #define S3C2410_LCDCON1_MMODE (1<<7)
  49. #define S3C2410_LCDCON1_DSCAN4 (0<<5)
  50. #define S3C2410_LCDCON1_STN4 (1<<5)
  51. #define S3C2410_LCDCON1_STN8 (2<<5)
  52. #define S3C2410_LCDCON1_TFT (3<<5)
  53. #define S3C2410_LCDCON1_STN1BPP (0<<1)
  54. #define S3C2410_LCDCON1_STN2GREY (1<<1)
  55. #define S3C2410_LCDCON1_STN4GREY (2<<1)
  56. #define S3C2410_LCDCON1_STN8BPP (3<<1)
  57. #define S3C2410_LCDCON1_STN12BPP (4<<1)
  58. #define S3C2410_LCDCON1_TFT1BPP (8<<1)
  59. #define S3C2410_LCDCON1_TFT2BPP (9<<1)
  60. #define S3C2410_LCDCON1_TFT4BPP (10<<1)
  61. #define S3C2410_LCDCON1_TFT8BPP (11<<1)
  62. #define S3C2410_LCDCON1_TFT16BPP (12<<1)
  63. #define S3C2410_LCDCON1_TFT24BPP (13<<1)
  64. #define S3C2410_LCDCON1_ENVID (1)
  65. #define S3C2410_LCDCON1_MODEMASK 0x1E
  66. #define S3C2410_LCDCON2_VBPD(x) ((x) << 24)
  67. #define S3C2410_LCDCON2_LINEVAL(x) ((x) << 14)
  68. #define S3C2410_LCDCON2_VFPD(x) ((x) << 6)
  69. #define S3C2410_LCDCON2_VSPW(x) ((x) << 0)
  70. #define S3C2410_LCDCON2_GET_VBPD(x) ( ((x) >> 24) & 0xFF)
  71. #define S3C2410_LCDCON2_GET_VFPD(x) ( ((x) >> 6) & 0xFF)
  72. #define S3C2410_LCDCON2_GET_VSPW(x) ( ((x) >> 0) & 0x3F)
  73. #define S3C2410_LCDCON3_HBPD(x) ((x) << 19)
  74. #define S3C2410_LCDCON3_WDLY(x) ((x) << 19)
  75. #define S3C2410_LCDCON3_HOZVAL(x) ((x) << 8)
  76. #define S3C2410_LCDCON3_HFPD(x) ((x) << 0)
  77. #define S3C2410_LCDCON3_LINEBLANK(x)((x) << 0)
  78. #define S3C2410_LCDCON3_GET_HBPD(x) ( ((x) >> 19) & 0x7F)
  79. #define S3C2410_LCDCON3_GET_HFPD(x) ( ((x) >> 0) & 0xFF)
  80. #define S3C2410_LCDCON4_MVAL(x) ((x) << 8)
  81. #define S3C2410_LCDCON4_HSPW(x) ((x) << 0)
  82. #define S3C2410_LCDCON4_WLH(x) ((x) << 0)
  83. #define S3C2410_LCDCON4_GET_HSPW(x) ( ((x) >> 0) & 0xFF)
  84. #define S3C2410_LCDCON5_BPP24BL (1<<12)
  85. #define S3C2410_LCDCON5_FRM565 (1<<11)
  86. #define S3C2410_LCDCON5_INVVCLK (1<<10)
  87. #define S3C2410_LCDCON5_INVVLINE (1<<9)
  88. #define S3C2410_LCDCON5_INVVFRAME (1<<8)
  89. #define S3C2410_LCDCON5_INVVD (1<<7)
  90. #define S3C2410_LCDCON5_INVVDEN (1<<6)
  91. #define S3C2410_LCDCON5_INVPWREN (1<<5)
  92. #define S3C2410_LCDCON5_INVLEND (1<<4)
  93. #define S3C2410_LCDCON5_PWREN (1<<3)
  94. #define S3C2410_LCDCON5_ENLEND (1<<2)
  95. #define S3C2410_LCDCON5_BSWP (1<<1)
  96. #define S3C2410_LCDCON5_HWSWP (1<<0)
  97. #define LCDCON1_VALUE S3C2410_LCDCON1_TFT16BPP | \
  98. S3C2410_LCDCON1_TFT | \
  99. S3C2410_LCDCON1_CLKVAL(CLKVAL_TFT_800480)
  100. #define LCDCON2_VALUE S3C2410_LCDCON2_VBPD(VBPD_800480) | \
  101. S3C2410_LCDCON2_LINEVAL(LINEVAL_TFT_800480) | \
  102. S3C2410_LCDCON2_VFPD(VFPD_800480) | \
  103. S3C2410_LCDCON2_VSPW(VSPW_800480)
  104. #define LCDCON3_VALUE S3C2410_LCDCON3_HBPD(HBPD_800480) | \
  105. S3C2410_LCDCON3_HOZVAL(HOZVAL_TFT_800480) | \
  106. S3C2410_LCDCON3_HFPD(HFPD_800480)
  107. #define LCDCON4_VALUE S3C2410_LCDCON4_MVAL(MVAL) | \
  108. S3C2410_LCDCON4_HSPW(HSPW_800480)
  109. #define LCDCON5_VALUE S3C2410_LCDCON5_FRM565 | \
  110. S3C2410_LCDCON5_INVVLINE | \
  111. S3C2410_LCDCON5_INVVFRAME | \
  112. S3C2410_LCDCON5_PWREN | \
  113. S3C2410_LCDCON5_HWSWP
  114. #define S3C2410_LCDINT_FRSYNC (1<<1)
  115. volatile rt_uint16_t _rt_hw_framebuffer[RT_HW_LCD_HEIGHT][RT_HW_LCD_WIDTH];
  116. void lcd_power_enable(int invpwren,int pwren)
  117. {
  118. /* GPG4 is setted as LCD_PWREN */
  119. GPGUP=(GPGUP&(~(1<<4)))|(1<<4); /* Pull-up disable */
  120. GPGCON=(GPGCON&(~(3<<8)))|(3<<8); /* GPG4=LCD_PWREN */
  121. GPGDAT = GPGDAT | (1<<4) ;
  122. /* Enable LCD POWER ENABLE Function */
  123. LCDCON5=(LCDCON5&(~(1<<3)))|(pwren<<3); /* PWREN */
  124. LCDCON5=(LCDCON5&(~(1<<5)))|(invpwren<<5); /* INVPWREN */
  125. }
  126. void lcd_envid_on_off(int onoff)
  127. {
  128. if(onoff==1)
  129. /*ENVID=ON*/
  130. LCDCON1|=1;
  131. else
  132. /*ENVID Off*/
  133. LCDCON1 =LCDCON1 & 0x3fffe;
  134. }
  135. //********************** BOARD LCD backlight ****************************
  136. void LcdBkLtSet(rt_uint32_t HiRatio)
  137. {
  138. #define FREQ_PWM1 1000
  139. if(!HiRatio)
  140. {
  141. GPBCON = GPBCON & (~(3<<2)) | (1<<2) ; //GPB1设置为output
  142. GPBDAT &= ~(1<<1);
  143. return;
  144. }
  145. GPBCON = GPBCON & (~(3<<2)) | (2<<2) ;
  146. if( HiRatio > 100 )
  147. HiRatio = 100 ;
  148. TCON = TCON & (~(0xf<<8)) ; // clear manual update bit, stop Timer1
  149. TCFG0 &= 0xffffff00; // set Timer 0&1 prescaler 0
  150. TCFG0 |= 15; //prescaler = 15+1
  151. TCFG1 &= 0xffffff0f; // set Timer 1 MUX 1/16
  152. TCFG1 |= 0x00000030; // set Timer 1 MUX 1/16
  153. TCNTB1 = ( 100000000>>8 )/FREQ_PWM1; //if set inverter off, when TCNT2<=TCMP2, TOUT is high, TCNT2>TCMP2, TOUT is low
  154. TCMPB1 = ( TCNTB1*(100-HiRatio))/100 ; //if set inverter on, when TCNT2<=TCMP2, TOUT is low, TCNT2>TCMP2, TOUT is high
  155. TCON = TCON & (~(0xf<<8)) | (0x0e<<8) ;
  156. TCON = TCON & (~(0xf<<8)) | (0x0d<<8) ;
  157. }
  158. rt_uint16_t color2index565(rt_uint32_t color)
  159. {
  160. int r,g,b;
  161. r = (color>> (0+3)) & 0x1f;
  162. g = (color>> (8+2)) & 0x3f;
  163. b = (color>>(16+3)) & 0x1f;
  164. return (rt_uint16_t)(b+(g<<5)+(r<<11));
  165. }
  166. rt_uint32_t index2color565(int index)
  167. {
  168. unsigned int r,g,b;
  169. r = index & 0x1f;
  170. g = (index>>5) & 0x3f;
  171. b = ((unsigned)index >> 11) & 0x1f;
  172. r = r * 255 / 31;
  173. g = g * 255 / 63;
  174. b = b * 255 / 31;
  175. return r + (g<<8) + (((rt_uint32_t)b)<<16);
  176. }
  177. #ifdef RT_USING_RTGUI
  178. #include <rtgui/driver.h>
  179. #include <rtgui/color.h>
  180. void rt_hw_lcd_update(rtgui_rect_t* rect)
  181. {
  182. /* nothing */
  183. }
  184. rt_uint8_t * rt_hw_lcd_get_framebuffer(void)
  185. {
  186. return (rt_uint8_t *)_rt_hw_framebuffer;
  187. }
  188. void rt_hw_lcd_set_pixel(rtgui_color_t *c, rt_base_t x, rt_base_t y)
  189. {
  190. if (x < SCR_XSIZE_TFT_800480 && y < SCR_YSIZE_TFT_800480)
  191. {
  192. _rt_hw_framebuffer[(y)][(x)] = color2index565(*c);
  193. }
  194. }
  195. void rt_hw_lcd_get_pixel(rtgui_color_t *c, rt_base_t x, rt_base_t y)
  196. {
  197. return ;
  198. }
  199. void rt_hw_lcd_draw_hline(rtgui_color_t *c, rt_base_t x1, rt_base_t x2, rt_base_t y)
  200. {
  201. rt_uint32_t idx;
  202. rt_uint16_t color;
  203. color = color2index565(*c);
  204. for (idx = x1; idx < x2; idx ++)
  205. {
  206. _rt_hw_framebuffer[y][idx] = color;
  207. }
  208. }
  209. void rt_hw_lcd_draw_vline(rtgui_color_t *c, rt_base_t x, rt_base_t y1, rt_base_t y2)
  210. {
  211. rt_uint32_t idy;
  212. rt_uint16_t color;
  213. color = color2index565(*c);
  214. for (idy = y1; idy < y2; idy ++)
  215. {
  216. _rt_hw_framebuffer[idy][x] = color;
  217. }
  218. }
  219. struct rtgui_graphic_driver _rtgui_lcd_driver =
  220. {
  221. "lcd",
  222. 2,
  223. 800,
  224. 480,
  225. rt_hw_lcd_update,
  226. rt_hw_lcd_get_framebuffer,
  227. rt_hw_lcd_set_pixel,
  228. rt_hw_lcd_get_pixel,
  229. rt_hw_lcd_draw_hline,
  230. rt_hw_lcd_draw_vline
  231. };
  232. #include "finsh.h"
  233. void hline(rt_uint32_t c, int x1, int x2, int y)
  234. {
  235. rtgui_color_t color = (rtgui_color_t)c;
  236. rt_hw_lcd_draw_hline(&color, x1, x2, y);
  237. }
  238. void vline(rt_uint32_t c, int x, int y1, int y2)
  239. {
  240. rtgui_color_t color = (rtgui_color_t)c;
  241. rt_hw_lcd_draw_vline(&color, x, y1, y2);
  242. }
  243. void dump_vline(int x, int y1, int y2)
  244. {
  245. rt_uint32_t idy;
  246. for (idy = y1; idy < y2; idy ++)
  247. {
  248. rt_kprintf("0x%04x ", _rt_hw_framebuffer[idy][x]);
  249. if ((idy + 1) % 8 == 0)
  250. rt_kprintf("\n");
  251. }
  252. rt_kprintf("\n");
  253. }
  254. void rt_hw_lcd_init(void)
  255. {
  256. int x,y;
  257. GPB1_TO_OUT();
  258. GPB1_TO_1();
  259. GPCUP = 0x00000000;
  260. GPCCON = 0xaaaa02a9;
  261. GPDUP = 0x00000000;
  262. GPDCON = 0xaaaaaaaa;
  263. LCDCON1 = LCDCON1_VALUE;
  264. LCDCON2 = LCDCON2_VALUE;
  265. LCDCON3 = LCDCON3_VALUE;
  266. LCDCON4 = LCDCON4_VALUE;
  267. LCDCON5 = LCDCON5_VALUE;
  268. LCDSADDR1=(((rt_uint32_t)_rt_hw_framebuffer>>22)<<21)|M5D((rt_uint32_t)_rt_hw_framebuffer>>1);
  269. LCDSADDR2=M5D( ((rt_uint32_t)_rt_hw_framebuffer+(SCR_XSIZE_TFT_800480*LCD_YSIZE_TFT_800480*2))>>1 );
  270. LCDSADDR3=(((SCR_XSIZE_TFT_800480-LCD_XSIZE_TFT_800480)/1)<<11)|(LCD_XSIZE_TFT_800480/1);
  271. LCDINTMSK|=(3);
  272. LPCSEL &= (~7) ;
  273. TPAL=0;
  274. LcdBkLtSet( 70 ) ;
  275. lcd_power_enable(0, 1);
  276. lcd_envid_on_off(1);
  277. /* clear framebuffer */
  278. /* memset((void *)_rt_hw_framebuffer, 0, LCD_XSIZE_TFT_800480*LCD_YSIZE_TFT_800480*2); */
  279. for(y = 0; y < 480; y ++)
  280. for(x = 0; x < 800; x++)
  281. _rt_hw_framebuffer[y][x]=0x00FF00;
  282. /* add lcd driver into graphic driver */
  283. rtgui_graphic_driver_add(&_rtgui_lcd_driver);
  284. /* finsh debug */
  285. finsh_syscall_append("vline", (syscall_func)vline);
  286. finsh_syscall_append("hline", (syscall_func)hline);
  287. finsh_syscall_append("dump_vline", (syscall_func)dump_vline);
  288. /*
  289. extern void rtgui_topwin_dump();
  290. finsh_syscall_append("wins", (syscall_func)rtgui_topwin_dump);
  291. */
  292. }
  293. #endif
  294. /*@}*/