lcd_n35.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * File : lcd_t35.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2010, 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://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2010-01-01 bernard first version from QiuYi's driver
  13. */
  14. #include <rtthread.h>
  15. #include <s3c24x0.h>
  16. #include "lcd.h"
  17. /* LCD driver for N3'5 */
  18. #define LCD_WIDTH 240
  19. #define LCD_HEIGHT 320
  20. #define LCD_PIXCLOCK 4
  21. #define LCD_RIGHT_MARGIN 36
  22. #define LCD_LEFT_MARGIN 19
  23. #define LCD_HSYNC_LEN 5
  24. #define LCD_UPPER_MARGIN 1
  25. #define LCD_LOWER_MARGIN 5
  26. #define LCD_VSYNC_LEN 1
  27. #define LCD_XSIZE LCD_WIDTH
  28. #define LCD_YSIZE LCD_HEIGHT
  29. #define SCR_XSIZE LCD_WIDTH
  30. #define SCR_YSIZE LCD_HEIGHT
  31. #define RT_HW_LCD_WIDTH LCD_WIDTH
  32. #define RT_HW_LCD_HEIGHT LCD_HEIGHT
  33. #define MVAL (13)
  34. #define MVAL_USED (0) //0=each frame 1=rate by MVAL
  35. #define INVVDEN (1) //0=normal 1=inverted
  36. #define BSWP (0) //Byte swap control
  37. #define HWSWP (1) //Half word swap control
  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 S3C2410_LCDCON1_CLKVAL(x) ((x) << 8)
  42. #define S3C2410_LCDCON1_MMODE (1<<7)
  43. #define S3C2410_LCDCON1_DSCAN4 (0<<5)
  44. #define S3C2410_LCDCON1_STN4 (1<<5)
  45. #define S3C2410_LCDCON1_STN8 (2<<5)
  46. #define S3C2410_LCDCON1_TFT (3<<5)
  47. #define S3C2410_LCDCON1_STN1BPP (0<<1)
  48. #define S3C2410_LCDCON1_STN2GREY (1<<1)
  49. #define S3C2410_LCDCON1_STN4GREY (2<<1)
  50. #define S3C2410_LCDCON1_STN8BPP (3<<1)
  51. #define S3C2410_LCDCON1_STN12BPP (4<<1)
  52. #define S3C2410_LCDCON1_TFT1BPP (8<<1)
  53. #define S3C2410_LCDCON1_TFT2BPP (9<<1)
  54. #define S3C2410_LCDCON1_TFT4BPP (10<<1)
  55. #define S3C2410_LCDCON1_TFT8BPP (11<<1)
  56. #define S3C2410_LCDCON1_TFT16BPP (12<<1)
  57. #define S3C2410_LCDCON1_TFT24BPP (13<<1)
  58. #define S3C2410_LCDCON1_ENVID (1)
  59. #define S3C2410_LCDCON1_MODEMASK 0x1E
  60. #define S3C2410_LCDCON2_VBPD(x) ((x) << 24)
  61. #define S3C2410_LCDCON2_LINEVAL(x) ((x) << 14)
  62. #define S3C2410_LCDCON2_VFPD(x) ((x) << 6)
  63. #define S3C2410_LCDCON2_VSPW(x) ((x) << 0)
  64. #define S3C2410_LCDCON2_GET_VBPD(x) ( ((x) >> 24) & 0xFF)
  65. #define S3C2410_LCDCON2_GET_VFPD(x) ( ((x) >> 6) & 0xFF)
  66. #define S3C2410_LCDCON2_GET_VSPW(x) ( ((x) >> 0) & 0x3F)
  67. #define S3C2410_LCDCON3_HBPD(x) ((x) << 19)
  68. #define S3C2410_LCDCON3_WDLY(x) ((x) << 19)
  69. #define S3C2410_LCDCON3_HOZVAL(x) ((x) << 8)
  70. #define S3C2410_LCDCON3_HFPD(x) ((x) << 0)
  71. #define S3C2410_LCDCON3_LINEBLANK(x)((x) << 0)
  72. #define S3C2410_LCDCON3_GET_HBPD(x) ( ((x) >> 19) & 0x7F)
  73. #define S3C2410_LCDCON3_GET_HFPD(x) ( ((x) >> 0) & 0xFF)
  74. #define S3C2410_LCDCON4_MVAL(x) ((x) << 8)
  75. #define S3C2410_LCDCON4_HSPW(x) ((x) << 0)
  76. #define S3C2410_LCDCON4_WLH(x) ((x) << 0)
  77. #define S3C2410_LCDCON4_GET_HSPW(x) ( ((x) >> 0) & 0xFF)
  78. #define S3C2410_LCDCON5_BPP24BL (1<<12)
  79. #define S3C2410_LCDCON5_FRM565 (1<<11)
  80. #define S3C2410_LCDCON5_INVVCLK (1<<10)
  81. #define S3C2410_LCDCON5_INVVLINE (1<<9)
  82. #define S3C2410_LCDCON5_INVVFRAME (1<<8)
  83. #define S3C2410_LCDCON5_INVVD (1<<7)
  84. #define S3C2410_LCDCON5_INVVDEN (1<<6)
  85. #define S3C2410_LCDCON5_INVPWREN (1<<5)
  86. #define S3C2410_LCDCON5_INVLEND (1<<4)
  87. #define S3C2410_LCDCON5_PWREN (1<<3)
  88. #define S3C2410_LCDCON5_ENLEND (1<<2)
  89. #define S3C2410_LCDCON5_BSWP (1<<1)
  90. #define S3C2410_LCDCON5_HWSWP (1<<0)
  91. #define S3C2410_LCDINT_FRSYNC (1<<1)
  92. static volatile rt_uint16_t _rt_framebuffer[RT_HW_LCD_HEIGHT][RT_HW_LCD_WIDTH];
  93. static volatile rt_uint16_t _rt_hw_framebuffer[RT_HW_LCD_HEIGHT][RT_HW_LCD_WIDTH];
  94. struct rtgui_lcd_device
  95. {
  96. struct rt_device parent;
  97. /* byte per pixel */
  98. rt_uint16_t byte_per_pixel;
  99. /* screen width and height */
  100. rt_uint16_t width;
  101. rt_uint16_t height;
  102. void* hw_framebuffer;
  103. };
  104. static struct rtgui_lcd_device *lcd = RT_NULL;
  105. static void lcd_power_enable(int invpwren, int pwren)
  106. {
  107. //GPG4 is setted as LCD_PWREN
  108. GPGUP = GPGUP | (1<<4); // Pull-up disable
  109. GPGCON = GPGCON | (3<<8); //GPG4=LCD_PWREN
  110. //Enable LCD POWER ENABLE Function
  111. LCDCON5 = LCDCON5&(~(1<<3))|(pwren<<3); // PWREN
  112. LCDCON5 = LCDCON5&(~(1<<5))|(invpwren<<5); // INVPWREN
  113. }
  114. static void lcd_envid_on_off(int onoff)
  115. {
  116. if(onoff==1)
  117. /*ENVID=ON*/
  118. LCDCON1|=1;
  119. else
  120. /*ENVID Off*/
  121. LCDCON1 =LCDCON1 & 0x3fffe;
  122. }
  123. //********************** BOARD LCD backlight ****************************
  124. static void LcdBkLtSet(rt_uint32_t HiRatio)
  125. {
  126. #define FREQ_PWM1 1000
  127. if(!HiRatio)
  128. {
  129. GPBCON = GPBCON & (~(3<<2)) | (1<<2) ; //GPB1ÉèÖÃΪoutput
  130. GPBDAT &= ~(1<<1);
  131. return;
  132. }
  133. GPBCON = GPBCON & (~(3<<2)) | (2<<2) ;
  134. if( HiRatio > 100 ) HiRatio = 100 ;
  135. TCON = TCON & (~(0xf<<8)) ; // clear manual update bit, stop Timer1
  136. TCFG0 &= 0xffffff00; // set Timer 0&1 prescaler 0
  137. TCFG0 |= 15; //prescaler = 15+1
  138. TCFG1 &= 0xffffff0f; // set Timer 1 MUX 1/16
  139. TCFG1 |= 0x00000030; // set Timer 1 MUX 1/16
  140. TCNTB1 = ( 100000000>>8 )/FREQ_PWM1; //if set inverter off, when TCNT2<=TCMP2, TOUT is high, TCNT2>TCMP2, TOUT is low
  141. TCMPB1 = ( TCNTB1*(100-HiRatio))/100 ; //if set inverter on, when TCNT2<=TCMP2, TOUT is low, TCNT2>TCMP2, TOUT is high
  142. TCON = TCON & (~(0xf<<8)) | (0x0e<<8) ;
  143. TCON = TCON & (~(0xf<<8)) | (0x0d<<8) ;
  144. }
  145. #ifdef RT_USING_RTGUI
  146. #include <rtgui/driver.h>
  147. #include <rtgui/color.h>
  148. static void rt_hw_lcd_update(rtgui_rect_t *rect)
  149. {
  150. rt_uint32_t i, j;
  151. for (i = rect->y1; i < rect->y2; i ++)
  152. {
  153. for(j = rect->x1; j < rect->x2; j++)
  154. _rt_hw_framebuffer[i][j] = _rt_framebuffer[i][j];
  155. }
  156. }
  157. static rt_uint8_t * rt_hw_lcd_get_framebuffer(void)
  158. {
  159. return (rt_uint8_t *)_rt_framebuffer;
  160. }
  161. static void rt_hw_lcd_set_pixel(rtgui_color_t *c, rt_base_t x, rt_base_t y)
  162. {
  163. if (x < RT_HW_LCD_WIDTH && y < RT_HW_LCD_HEIGHT)
  164. {
  165. _rt_framebuffer[(y)][(x)] = rtgui_color_to_565p(*c);
  166. }
  167. }
  168. static void rt_hw_lcd_get_pixel(rtgui_color_t *c, rt_base_t x, rt_base_t y)
  169. {
  170. if (x < RT_HW_LCD_WIDTH && y < RT_HW_LCD_HEIGHT)
  171. {
  172. *c = rtgui_color_from_565p(_rt_framebuffer[(y)][(x)]);
  173. }
  174. }
  175. static void rt_hw_lcd_draw_hline(rtgui_color_t *c, rt_base_t x1, rt_base_t x2, rt_base_t y)
  176. {
  177. rt_uint32_t idx;
  178. rt_uint16_t color;
  179. /* get color pixel */
  180. color = rtgui_color_to_565p(*c);
  181. for (idx = x1; idx < x2; idx ++)
  182. {
  183. _rt_framebuffer[y][idx] = color;
  184. }
  185. }
  186. static void rt_hw_lcd_draw_vline(rtgui_color_t *c, rt_base_t x, rt_base_t y1, rt_base_t y2)
  187. {
  188. rt_uint32_t idy;
  189. rt_uint16_t color;
  190. /* get color pixel */
  191. color = rtgui_color_to_565p(*c);
  192. for (idy = y1; idy < y2; idy ++)
  193. {
  194. _rt_framebuffer[idy][x] = color;
  195. }
  196. }
  197. static void rt_hw_lcd_draw_raw_hline(rt_uint8_t *pixels, rt_base_t x1, rt_base_t x2, rt_base_t y)
  198. {
  199. rt_memcpy((void*)&_rt_framebuffer[y][x1], pixels, (x2 - x1) * 2);
  200. }
  201. struct rtgui_graphic_driver _rtgui_lcd_driver =
  202. {
  203. "lcd",
  204. 2,
  205. RT_HW_LCD_WIDTH,
  206. RT_HW_LCD_HEIGHT,
  207. rt_hw_lcd_update,
  208. rt_hw_lcd_get_framebuffer,
  209. rt_hw_lcd_set_pixel,
  210. rt_hw_lcd_get_pixel,
  211. rt_hw_lcd_draw_hline,
  212. rt_hw_lcd_draw_vline,
  213. rt_hw_lcd_draw_raw_hline
  214. };
  215. #include "finsh.h"
  216. void hline(rt_uint32_t c, int x1, int x2, int y)
  217. {
  218. rtgui_color_t color = (rtgui_color_t)c;
  219. rt_hw_lcd_draw_hline(&color, x1, x2, y);
  220. }
  221. FINSH_FUNCTION_EXPORT(hline, draw a hline);
  222. void vline(rt_uint32_t c, int x, int y1, int y2)
  223. {
  224. rtgui_color_t color = (rtgui_color_t)c;
  225. rt_hw_lcd_draw_vline(&color, x, y1, y2);
  226. }
  227. FINSH_FUNCTION_EXPORT(vline, draw a vline);
  228. void clear()
  229. {
  230. int y;
  231. for (y = 0; y < LCD_HEIGHT; y ++)
  232. {
  233. rt_hw_lcd_draw_hline((rtgui_color_t*)&white, 0, LCD_WIDTH, y);
  234. }
  235. }
  236. FINSH_FUNCTION_EXPORT(clear, clear screen);
  237. #endif
  238. /* RT-Thread Device Interface */
  239. static rt_err_t rt_lcd_init (rt_device_t dev)
  240. {
  241. GPB1_TO_OUT();
  242. GPB1_TO_1();
  243. GPCUP = 0x00000000;
  244. GPCCON = 0xaaaa02a9;
  245. GPDUP = 0x00000000;
  246. GPDCON = 0xaaaaaaaa;
  247. #define M5D(n) ((n)&0x1fffff)
  248. #define LCD_ADDR ((rt_uint32_t)_rt_hw_framebuffer)
  249. LCDCON1 = (LCD_PIXCLOCK << 8) | (3 << 5) | (12 << 1);
  250. LCDCON2 = (LCD_UPPER_MARGIN << 24) | ((LCD_HEIGHT - 1) << 14) | (LCD_LOWER_MARGIN << 6) | (LCD_VSYNC_LEN << 0);
  251. LCDCON3 = (LCD_RIGHT_MARGIN << 19) | ((LCD_WIDTH - 1) << 8) | (LCD_LEFT_MARGIN << 0);
  252. LCDCON4 = (13 << 8) | (LCD_HSYNC_LEN << 0);
  253. #if !defined(LCD_CON5)
  254. #define LCD_CON5 ((1<<11) | (1 << 9) | (1 << 8) | (1 << 3) | (1 << 0))
  255. #endif
  256. LCDCON5 = LCD_CON5;
  257. LCDSADDR1 = ((LCD_ADDR >> 22) << 21) | ((M5D(LCD_ADDR >> 1)) << 0);
  258. LCDSADDR2 = M5D((LCD_ADDR + LCD_WIDTH * LCD_HEIGHT * 2) >> 1);
  259. LCDSADDR3 = LCD_WIDTH;
  260. LCDINTMSK |= (3);
  261. LPCSEL &= (~7) ;
  262. TPAL=0;
  263. LcdBkLtSet(70) ;
  264. lcd_power_enable(0, 1);
  265. lcd_envid_on_off(1);
  266. return RT_EOK;
  267. }
  268. static rt_err_t rt_lcd_control (rt_device_t dev, rt_uint8_t cmd, void *args)
  269. {
  270. switch (cmd)
  271. {
  272. case RT_DEVICE_CTRL_LCD_GET_WIDTH:
  273. *((rt_uint16_t*)args) = lcd->width;
  274. break;
  275. case RT_DEVICE_CTRL_LCD_GET_HEIGHT:
  276. *((rt_uint16_t*)args) = lcd->height;
  277. break;
  278. case RT_DEVICE_CTRL_LCD_GET_BPP:
  279. *((rt_uint16_t*)args) = lcd->byte_per_pixel;
  280. break;
  281. case RT_DEVICE_CTRL_LCD_GET_FRAMEBUFFER:
  282. *((rt_uint16_t*)args) = lcd->hw_framebuffer;
  283. break;
  284. }
  285. return RT_EOK;
  286. }
  287. void rtgui_lcd_hw_init(void)
  288. {
  289. lcd = (struct rtgui_lcd_device*)rt_malloc(sizeof(struct rtgui_lcd_device));
  290. if (lcd == RT_NULL) return; /* no memory yet */
  291. /* init device structure */
  292. lcd->parent.type = RT_Device_Class_Unknown;
  293. lcd->parent.init = rt_lcd_init;
  294. lcd->parent.control = rt_lcd_control;
  295. lcd->parent.user_data = RT_NULL;
  296. lcd->byte_per_pixel = 2;
  297. lcd->width = LCD_WIDTH;
  298. lcd->height = LCD_HEIGHT;
  299. lcd->hw_framebuffer = (void*)_rt_hw_framebuffer;
  300. /* register touch device to RT-Thread */
  301. rt_device_register(&(lcd->parent), "lcd", RT_DEVICE_FLAG_RDWR);
  302. #ifdef RT_USING_RTGUI
  303. /* add lcd driver into graphic driver */
  304. rtgui_graphic_driver_add(&_rtgui_lcd_driver);
  305. #endif
  306. }