lcd.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 ) HiRatio = 100 ;
  143. TCON = TCON & (~(0xf<<8)) ; // clear manual update bit, stop Timer1
  144. TCFG0 &= 0xffffff00; // set Timer 0&1 prescaler 0
  145. TCFG0 |= 15; //prescaler = 15+1
  146. TCFG1 &= 0xffffff0f; // set Timer 1 MUX 1/16
  147. TCFG1 |= 0x00000030; // set Timer 1 MUX 1/16
  148. TCNTB1 = ( 100000000>>8 )/FREQ_PWM1; //if set inverter off, when TCNT2<=TCMP2, TOUT is high, TCNT2>TCMP2, TOUT is low
  149. TCMPB1 = ( TCNTB1*(100-HiRatio))/100 ; //if set inverter on, when TCNT2<=TCMP2, TOUT is low, TCNT2>TCMP2, TOUT is high
  150. TCON = TCON & (~(0xf<<8)) | (0x0e<<8) ;
  151. TCON = TCON & (~(0xf<<8)) | (0x0d<<8) ;
  152. }
  153. #ifdef RT_USING_RTGUI
  154. #include <rtgui/driver.h>
  155. #include <rtgui/color.h>
  156. void rt_hw_lcd_update(rtgui_rect_t *rect)
  157. {
  158. /* nothing */
  159. }
  160. rt_uint8_t * rt_hw_lcd_get_framebuffer(void)
  161. {
  162. return (rt_uint8_t *)_rt_hw_framebuffer;
  163. }
  164. void rt_hw_lcd_set_pixel(rtgui_color_t *c, rt_base_t x, rt_base_t y)
  165. {
  166. if (x < SCR_XSIZE_TFT_240320 && y < SCR_YSIZE_TFT_240320)
  167. {
  168. _rt_hw_framebuffer[(y)][(x)] = rtgui_color_to_565p(*c);
  169. }
  170. }
  171. void rt_hw_lcd_get_pixel(rtgui_color_t *c, rt_base_t x, rt_base_t y)
  172. {
  173. if (x < SCR_XSIZE_TFT_240320 && y < SCR_YSIZE_TFT_240320)
  174. {
  175. *c = rtgui_color_from_565p(_rt_hw_framebuffer[(y)][(x)]);
  176. }
  177. return ;
  178. }
  179. void rt_hw_lcd_draw_hline(rtgui_color_t *c, rt_base_t x1, rt_base_t x2, rt_base_t y)
  180. {
  181. rt_uint32_t idx;
  182. rt_uint16_t color;
  183. /* get color pixel */
  184. color = rtgui_color_to_565p(*c);
  185. for (idx = x1; idx < x2; idx ++)
  186. {
  187. _rt_hw_framebuffer[y][idx] = color;
  188. }
  189. }
  190. void rt_hw_lcd_draw_vline(rtgui_color_t *c, rt_base_t x, rt_base_t y1, rt_base_t y2)
  191. {
  192. rt_uint32_t idy;
  193. rt_uint16_t color;
  194. /* get color pixel */
  195. color = rtgui_color_to_565p(*c);
  196. for (idy = y1; idy < y2; idy ++)
  197. {
  198. _rt_hw_framebuffer[idy][x] = color;
  199. }
  200. }
  201. void rt_hw_lcd_draw_raw_hline(rt_uint8_t *pixels, rt_base_t x1, rt_base_t x2, rt_base_t y)
  202. {
  203. rt_memcpy((void*)&_rt_hw_framebuffer[y][x1], pixels, (x2 - x1) * 2);
  204. }
  205. struct rtgui_graphic_driver _rtgui_lcd_driver =
  206. {
  207. "lcd",
  208. 2,
  209. 240,
  210. 320,
  211. rt_hw_lcd_update,
  212. rt_hw_lcd_get_framebuffer,
  213. rt_hw_lcd_set_pixel,
  214. rt_hw_lcd_get_pixel,
  215. rt_hw_lcd_draw_hline,
  216. rt_hw_lcd_draw_vline,
  217. rt_hw_lcd_draw_raw_hline
  218. };
  219. #include "finsh.h"
  220. void hline(rt_uint32_t c, int x1, int x2, int y)
  221. {
  222. rtgui_color_t color = (rtgui_color_t)c;
  223. rt_hw_lcd_draw_hline(&color, x1, x2, y);
  224. }
  225. FINSH_FUNCTION_EXPORT(hline, draw a hline);
  226. void vline(rt_uint32_t c, int x, int y1, int y2)
  227. {
  228. rtgui_color_t color = (rtgui_color_t)c;
  229. rt_hw_lcd_draw_vline(&color, x, y1, y2);
  230. }
  231. FINSH_FUNCTION_EXPORT(vline, draw a vline);
  232. void clear()
  233. {
  234. int y;
  235. for (y = 0; y < 320; y ++)
  236. {
  237. rt_hw_lcd_draw_hline((rtgui_color_t*)&white, 0, 239, y);
  238. }
  239. }
  240. FINSH_FUNCTION_EXPORT(clear, clear screen);
  241. void rt_hw_lcd_init()
  242. {
  243. GPB1_TO_OUT();
  244. GPB1_TO_1();
  245. GPCUP = 0x00000000;
  246. GPCCON = 0xaaaa02a9;
  247. GPDUP = 0x00000000;
  248. GPDCON = 0xaaaaaaaa;
  249. LCDCON1 = LCDCON1_VALUE;
  250. LCDCON2 = LCDCON2_VALUE;
  251. LCDCON3 = LCDCON3_VALUE;
  252. LCDCON4 = LCDCON4_VALUE;
  253. LCDCON5 = LCDCON5_VALUE;
  254. LCDSADDR1=(((rt_uint32_t)_rt_hw_framebuffer>>22)<<21)|M5D((rt_uint32_t)_rt_hw_framebuffer>>1);
  255. LCDSADDR2=M5D( ((rt_uint32_t)_rt_hw_framebuffer+(SCR_XSIZE_TFT_240320*LCD_YSIZE_TFT_240320*2))>>1 );
  256. LCDSADDR3=(((SCR_XSIZE_TFT_240320-LCD_XSIZE_TFT_240320)/1)<<11)|(LCD_XSIZE_TFT_240320/1);
  257. LCDINTMSK|=(3);
  258. LPCSEL &= (~7) ;
  259. TPAL=0;
  260. LcdBkLtSet( 70 ) ;
  261. lcd_power_enable(0, 1);
  262. lcd_envid_on_off(1);
  263. /* add lcd driver into graphic driver */
  264. rtgui_graphic_driver_add(&_rtgui_lcd_driver);
  265. }
  266. #endif