lcd.c 9.3 KB

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