lcd_x35.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * File : lcd_x35.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. * 2011-07-13 Manoel Modifications on lcd_n35.c to support X35 LCD
  14. * ACX502BMU - 7522J16D SONY
  15. */
  16. #include <rtthread.h>
  17. #include <s3c24x0.h>
  18. /* LCD driver for X3'5 */
  19. #define LCD_WIDTH 240 // xres
  20. #define LCD_HEIGHT 320 // yres
  21. #define LCD_PIXCLOCK 7 // clock
  22. #define LCD_RIGHT_MARGIN 23 // margin_right
  23. #define LCD_LEFT_MARGIN 0 // margin_left
  24. #define LCD_HSYNC_LEN 5 // hsync
  25. #define LCD_UPPER_MARGIN 1 // margin_top
  26. #define LCD_LOWER_MARGIN 3 // margin_botton
  27. #define LCD_VSYNC_LEN 9 // vsync
  28. #define LCD_XSIZE LCD_WIDTH
  29. #define LCD_YSIZE LCD_HEIGHT
  30. #define SCR_XSIZE LCD_WIDTH
  31. #define SCR_YSIZE LCD_HEIGHT
  32. #define RT_HW_LCD_WIDTH LCD_WIDTH
  33. #define RT_HW_LCD_HEIGHT LCD_HEIGHT
  34. #define MVAL (13)
  35. #define MVAL_USED (0) //0=each frame 1=rate by MVAL
  36. #define INVVDEN (1) //0=normal 1=inverted
  37. #define BSWP (0) //Byte swap control
  38. #define HWSWP (1) //Half word swap control
  39. #define GPB1_TO_OUT() (GPBUP &= 0xfffd, GPBCON &= 0xfffffff3, GPBCON |= 0x00000004)
  40. #define GPB1_TO_1() (GPBDAT |= 0x0002)
  41. #define GPB1_TO_0() (GPBDAT &= 0xfffd)
  42. #define S3C2410_LCDCON1_CLKVAL(x) ((x) << 8)
  43. #define S3C2410_LCDCON1_MMODE (1<<7)
  44. #define S3C2410_LCDCON1_DSCAN4 (0<<5)
  45. #define S3C2410_LCDCON1_STN4 (1<<5)
  46. #define S3C2410_LCDCON1_STN8 (2<<5)
  47. #define S3C2410_LCDCON1_TFT (3<<5)
  48. #define S3C2410_LCDCON1_STN1BPP (0<<1)
  49. #define S3C2410_LCDCON1_STN2GREY (1<<1)
  50. #define S3C2410_LCDCON1_STN4GREY (2<<1)
  51. #define S3C2410_LCDCON1_STN8BPP (3<<1)
  52. #define S3C2410_LCDCON1_STN12BPP (4<<1)
  53. #define S3C2410_LCDCON1_TFT1BPP (8<<1)
  54. #define S3C2410_LCDCON1_TFT2BPP (9<<1)
  55. #define S3C2410_LCDCON1_TFT4BPP (10<<1)
  56. #define S3C2410_LCDCON1_TFT8BPP (11<<1)
  57. #define S3C2410_LCDCON1_TFT16BPP (12<<1)
  58. #define S3C2410_LCDCON1_TFT24BPP (13<<1)
  59. #define S3C2410_LCDCON1_ENVID (1)
  60. #define S3C2410_LCDCON1_MODEMASK 0x1E
  61. #define S3C2410_LCDCON2_VBPD(x) ((x) << 24)
  62. #define S3C2410_LCDCON2_LINEVAL(x) ((x) << 14)
  63. #define S3C2410_LCDCON2_VFPD(x) ((x) << 6)
  64. #define S3C2410_LCDCON2_VSPW(x) ((x) << 0)
  65. #define S3C2410_LCDCON2_GET_VBPD(x) ( ((x) >> 24) & 0xFF)
  66. #define S3C2410_LCDCON2_GET_VFPD(x) ( ((x) >> 6) & 0xFF)
  67. #define S3C2410_LCDCON2_GET_VSPW(x) ( ((x) >> 0) & 0x3F)
  68. #define S3C2410_LCDCON3_HBPD(x) ((x) << 19)
  69. #define S3C2410_LCDCON3_WDLY(x) ((x) << 19)
  70. #define S3C2410_LCDCON3_HOZVAL(x) ((x) << 8)
  71. #define S3C2410_LCDCON3_HFPD(x) ((x) << 0)
  72. #define S3C2410_LCDCON3_LINEBLANK(x)((x) << 0)
  73. #define S3C2410_LCDCON3_GET_HBPD(x) ( ((x) >> 19) & 0x7F)
  74. #define S3C2410_LCDCON3_GET_HFPD(x) ( ((x) >> 0) & 0xFF)
  75. #define S3C2410_LCDCON4_MVAL(x) ((x) << 8)
  76. #define S3C2410_LCDCON4_HSPW(x) ((x) << 0)
  77. #define S3C2410_LCDCON4_WLH(x) ((x) << 0)
  78. #define S3C2410_LCDCON4_GET_HSPW(x) ( ((x) >> 0) & 0xFF)
  79. #define S3C2410_LCDCON5_BPP24BL (1<<12)
  80. #define S3C2410_LCDCON5_FRM565 (1<<11)
  81. #define S3C2410_LCDCON5_INVVCLK (1<<10)
  82. #define S3C2410_LCDCON5_INVVLINE (1<<9)
  83. #define S3C2410_LCDCON5_INVVFRAME (1<<8)
  84. #define S3C2410_LCDCON5_INVVD (1<<7)
  85. #define S3C2410_LCDCON5_INVVDEN (1<<6)
  86. #define S3C2410_LCDCON5_INVPWREN (1<<5)
  87. #define S3C2410_LCDCON5_INVLEND (1<<4)
  88. #define S3C2410_LCDCON5_PWREN (1<<3)
  89. #define S3C2410_LCDCON5_ENLEND (1<<2)
  90. #define S3C2410_LCDCON5_BSWP (1<<1)
  91. #define S3C2410_LCDCON5_HWSWP (1<<0)
  92. #define S3C2410_LCDINT_FRSYNC (1<<1)
  93. static volatile rt_uint16_t _rt_framebuffer[RT_HW_LCD_HEIGHT][RT_HW_LCD_WIDTH];
  94. //static volatile rt_uint16_t _rt_hw_framebuffer[RT_HW_LCD_HEIGHT][RT_HW_LCD_WIDTH];
  95. static struct rt_device_graphic_info _lcd_info;
  96. static void lcd_power_enable(int invpwren, int pwren)
  97. {
  98. //GPG4 is setted as LCD_PWREN
  99. GPGUP = GPGUP | (1<<4); // Pull-up disable
  100. GPGCON = GPGCON | (3<<8); //GPG4=LCD_PWREN
  101. //Enable LCD POWER ENABLE Function
  102. LCDCON5 = LCDCON5&(~(1<<3))|(pwren<<3); // PWREN
  103. LCDCON5 = LCDCON5&(~(1<<5))|(invpwren<<5); // INVPWREN
  104. }
  105. static void lcd_envid_on_off(int onoff)
  106. {
  107. if(onoff==1)
  108. /*ENVID=ON*/
  109. LCDCON1|=1;
  110. else
  111. /*ENVID Off*/
  112. LCDCON1 =LCDCON1 & 0x3fffe;
  113. }
  114. //********************** BOARD LCD backlight ****************************
  115. static void LcdBkLtSet(rt_uint32_t HiRatio)
  116. {
  117. #define FREQ_PWM1 1000
  118. if(!HiRatio)
  119. {
  120. GPBCON = GPBCON & (~(3<<2)) | (1<<2) ;
  121. GPBDAT &= ~(1<<1);
  122. return;
  123. }
  124. GPBCON = GPBCON & (~(3<<2)) | (2<<2) ;
  125. if( HiRatio > 100 ) HiRatio = 100 ;
  126. TCON = TCON & (~(0xf<<8)) ; // clear manual update bit, stop Timer1
  127. TCFG0 &= 0xffffff00; // set Timer 0&1 prescaler 0
  128. TCFG0 |= 15; //prescaler = 15+1
  129. TCFG1 &= 0xffffff0f; // set Timer 1 MUX 1/16
  130. TCFG1 |= 0x00000030; // set Timer 1 MUX 1/16
  131. TCNTB1 = ( 100000000>>8 )/FREQ_PWM1; //if set inverter off, when TCNT2<=TCMP2, TOUT is high, TCNT2>TCMP2, TOUT is low
  132. TCMPB1 = ( TCNTB1*(100-HiRatio))/100 ; //if set inverter on, when TCNT2<=TCMP2, TOUT is low, TCNT2>TCMP2, TOUT is high
  133. TCON = TCON & (~(0xf<<8)) | (0x0e<<8) ;
  134. TCON = TCON & (~(0xf<<8)) | (0x0d<<8) ;
  135. }
  136. /* RT-Thread Device Interface */
  137. static rt_err_t rt_lcd_init (rt_device_t dev)
  138. {
  139. GPB1_TO_OUT();
  140. GPB1_TO_1();
  141. GPCUP = 0x00000000;
  142. GPCCON = 0xaaaa02a9;
  143. GPDUP = 0x00000000;
  144. GPDCON = 0xaaaaaaaa;
  145. #define M5D(n) ((n)&0x1fffff)
  146. #define LCD_ADDR ((rt_uint32_t)_rt_framebuffer)
  147. LCDCON1 = (LCD_PIXCLOCK << 8) | (3 << 5) | (12 << 1);
  148. LCDCON2 = (LCD_UPPER_MARGIN << 24) | ((LCD_HEIGHT - 1) << 14) | (LCD_LOWER_MARGIN << 6) | (LCD_VSYNC_LEN << 0);
  149. LCDCON3 = (LCD_RIGHT_MARGIN << 19) | ((LCD_WIDTH - 1) << 8) | (LCD_LEFT_MARGIN << 0);
  150. LCDCON4 = (13 << 8) | (LCD_HSYNC_LEN << 0);
  151. #define LCD_CON5 (S3C2410_LCDCON5_FRM565 | S3C2410_LCDCON5_INVVDEN | S3C2410_LCDCON5_INVVFRAME | \
  152. S3C2410_LCDCON5_INVVLINE | S3C2410_LCDCON5_INVVCLK | S3C2410_LCDCON5_PWREN| S3C2410_LCDCON5_HWSWP)
  153. #if !defined(LCD_CON5)
  154. #define LCD_CON5 ((1<<11) | (1 << 9) | (1 << 8) | (1 << 3) | (1 << 0))
  155. #endif
  156. LCDCON5 = LCD_CON5;
  157. LCDSADDR1 = ((LCD_ADDR >> 22) << 21) | ((M5D(LCD_ADDR >> 1)) << 0);
  158. LCDSADDR2 = M5D((LCD_ADDR + LCD_WIDTH * LCD_HEIGHT * 2) >> 1);
  159. LCDSADDR3 = LCD_WIDTH;
  160. LCDINTMSK |= (3);
  161. LPCSEL &= (~7) ;
  162. TPAL=0;
  163. LcdBkLtSet(70) ;
  164. lcd_power_enable(0, 1);
  165. lcd_envid_on_off(1);
  166. return RT_EOK;
  167. }
  168. static rt_err_t rt_lcd_control (rt_device_t dev, int cmd, void *args)
  169. {
  170. switch (cmd)
  171. {
  172. case RTGRAPHIC_CTRL_RECT_UPDATE:
  173. break;
  174. case RTGRAPHIC_CTRL_POWERON:
  175. break;
  176. case RTGRAPHIC_CTRL_POWEROFF:
  177. break;
  178. case RTGRAPHIC_CTRL_GET_INFO:
  179. rt_memcpy(args, &_lcd_info, sizeof(_lcd_info));
  180. break;
  181. case RTGRAPHIC_CTRL_SET_MODE:
  182. break;
  183. }
  184. return RT_EOK;
  185. }
  186. int rt_hw_lcd_init(void)
  187. {
  188. rt_device_t lcd = rt_malloc(sizeof(struct rt_device));
  189. if (lcd == RT_NULL)
  190. return -RT_ERROR; /* no memory yet */
  191. _lcd_info.bits_per_pixel = 16;
  192. _lcd_info.pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565P;
  193. _lcd_info.framebuffer = (void*)_rt_framebuffer;
  194. _lcd_info.width = LCD_WIDTH;
  195. _lcd_info.height = LCD_HEIGHT;
  196. /* init device structure */
  197. lcd->type = RT_Device_Class_Unknown;
  198. lcd->init = rt_lcd_init;
  199. lcd->open = RT_NULL;
  200. lcd->close = RT_NULL;
  201. lcd->control = rt_lcd_control;
  202. lcd->user_data = (void*)&_lcd_info;
  203. /* register lcd device to RT-Thread */
  204. rt_device_register(lcd, "lcd", RT_DEVICE_FLAG_RDWR);
  205. }
  206. INIT_BOARD_EXPORT(rt_hw_lcd_init);