lcd.c 11.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * File      : lcd.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2011, RT-Thread Develop Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2011-03-05     lgnq         ZYMG12864C3 LCD driver
 */

#include <rtthread.h>
#include "lcd.h"
D
dzzxzz 已提交
17
#include "font.h"
18

19
static struct rt_device_graphic_info _lcd_info;
20
static rt_uint8_t gui_disp_buf[GUI_LCM_YMAX/8][GUI_LCM_XMAX];
D
dzzxzz 已提交
21 22 23 24
const unsigned char BIT_MASK[8] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
/* simple font: ' ', '0'~'9','a'~'z','A'~'Z' */
extern const unsigned char  FONTTYPE8_8[][8];

25 26 27
rt_uint32_t x;
rt_uint32_t y;

D
dzzxzz 已提交
28
void power_delay(void)
29
{
D
dzzxzz 已提交
30
    rt_uint32_t i = 0x4ffff;
31
    while(i--)
D
dzzxzz 已提交
32
        ;
33 34
}

D
dzzxzz 已提交
35
void delay(void)
36
{
D
dzzxzz 已提交
37
    rt_uint8_t i = 0x8;
38
    while(i--)
D
dzzxzz 已提交
39
        ;
40 41
}

D
dzzxzz 已提交
42
void reset_delay(void)
43
{
D
dzzxzz 已提交
44
    rt_uint8_t i = 0xff;
45
    while(i--)
D
dzzxzz 已提交
46
        ;
47 48
}

D
dzzxzz 已提交
49
void lcd_write_cmd(unsigned char command)
50 51 52 53 54 55
{
    rt_uint8_t i;

    LCD_PS_LOW();
    LCD_CS_LOW();
    LCD_CD_LOW();
D
dzzxzz 已提交
56
    for (i=0; i<8; i++)
57
    {
D
dzzxzz 已提交
58
        if (command & (0x80 >> i))
59 60 61 62 63
            LCD_DATA_HIGH();
        else
            LCD_DATA_LOW();
            
        LCD_CLK_LOW();
D
dzzxzz 已提交
64
        delay();
65
        LCD_CLK_HIGH();
D
dzzxzz 已提交
66
        delay();
67 68 69 70
    }
    LCD_CS_HIGH();
}

D
dzzxzz 已提交
71
void lcd_write_data(unsigned char data)
72 73 74 75 76 77
{
    rt_uint8_t i;

    LCD_PS_LOW();
    LCD_CS_LOW();
    LCD_CD_HIGH();
D
dzzxzz 已提交
78
    for (i=0; i<8; i++)
79
    {
D
dzzxzz 已提交
80
        if (data & (0x80 >> i))
81 82 83 84 85
            LCD_DATA_HIGH();
        else
            LCD_DATA_LOW();
        
        LCD_CLK_LOW();
D
dzzxzz 已提交
86
        delay();
87
        LCD_CLK_HIGH();
D
dzzxzz 已提交
88
        delay();
89 90 91 92 93 94 95 96
    }
    LCD_CS_HIGH();
}

#ifdef RT_USING_RTGUI
#include <rtgui/driver.h>
#include <rtgui/color.h>

D
dzzxzz 已提交
97
static void rt_hw_lcd_update(struct rt_device_rect_info *rect_info)
98 99 100 101
{
    rt_uint8_t i,j = GUI_LCM_XMAX;
    rt_uint8_t* p = (rt_uint8_t*)gui_disp_buf;  
    
D
dzzxzz 已提交
102
    for (i=0; i<GUI_LCM_PAGE; i++)
103
    { 
D
dzzxzz 已提交
104 105 106
        lcd_write_cmd(SET_PAGE_ADDR_0|i);    
        lcd_write_cmd(SET_COLH_ADDR_0);        
        lcd_write_cmd(SET_COLL_ADDR_0);
107
        j = GUI_LCM_XMAX;
D
dzzxzz 已提交
108
        while (j--)
109
        {
D
dzzxzz 已提交
110 111
            lcd_write_data(*p++);
            delay();
112 113 114 115 116 117 118 119 120
        }
    }
}

static rt_uint8_t * rt_hw_lcd_get_framebuffer(void)
{
    return(rt_uint8_t *)gui_disp_buf;
}

121
static void rt_hw_lcd_set_pixel(rtgui_color_t *c, int x, int y)
122 123 124 125
{
    rt_uint8_t page;
    page = y/8;

126
    if (*c == rtgui_color_to_565(black))
127 128
        gui_disp_buf[page][x] |= 1<<(y%8);
    else 
129
        if (*c == rtgui_color_to_565(white))
130 131 132
            gui_disp_buf[page][x] &= ~(1<<(y%8));
}

133
static void rt_hw_lcd_get_pixel(rtgui_color_t *c, int x, int y)
134 135 136 137
{
    rt_uint8_t page;
    page = y/8;
    
D
dzzxzz 已提交
138
    if (gui_disp_buf[page][x] & (1<<(y%8)))
139 140 141 142 143
        *c = black;
    else
        *c = white;
}

144
static void rt_hw_lcd_draw_hline(rtgui_color_t *c, int x1, int x2, int y)
145 146 147 148 149
{
    rt_uint8_t page;
	rt_uint8_t i;
    page = y/8;
  
D
dzzxzz 已提交
150
    for (i=x1; i<x2; i++)
151
    {
152
        if (*c == rtgui_color_to_565(black))
153 154
            gui_disp_buf[page][i] |= 1<<(y%8);
        else 
155
            if (*c == rtgui_color_to_565(white))
156 157 158 159
                gui_disp_buf[page][i] &= ~(1<<(y%8));      
    }
}

160
static void rt_hw_lcd_draw_vline(rtgui_color_t *c, int x, int y1, int y2)
161 162 163
{
    rt_uint8_t y;

D
dzzxzz 已提交
164
    for (y = y1; y < y2; y ++)
165 166 167 168 169
    {
        rt_hw_lcd_set_pixel(c, x, y);
    }
}

170
static void rt_hw_lcd_draw_raw_hline(rt_uint8_t *pixels, int x1, int x2, int y)
171 172 173 174 175 176 177 178
{
    rt_uint8_t coll; 
	rt_uint8_t colh; 
	rt_uint8_t page;
	rt_uint8_t i;

    page = y/8;
  
D
dzzxzz 已提交
179
    for (i=x1; i<x2; i++)
180 181 182 183
    {
        gui_disp_buf[page][i] |= 1<<(y%8);
        coll = i & 0x0f;
        colh = i >> 4;
D
dzzxzz 已提交
184 185 186 187
        lcd_write_cmd(SET_PAGE_ADDR_0 | page);
        lcd_write_cmd(SET_COLH_ADDR_0 | colh);
        lcd_write_cmd(SET_COLL_ADDR_0 | coll);
        lcd_write_data(gui_disp_buf[page][i]);
188 189 190
    }
}

D
dzzxzz 已提交
191
const struct rtgui_graphic_driver_ops _lcd_ops =
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
{
    rt_hw_lcd_set_pixel,
    rt_hw_lcd_get_pixel,
    rt_hw_lcd_draw_hline,
    rt_hw_lcd_draw_vline,
    rt_hw_lcd_draw_raw_hline
};
#endif

void lcd_io_init()
{
    /* Release the analog input function*/
    FM3_GPIO->ADE =0x03;
    /*Select CPIO function*/
    LCD_CS_PFR &= ~LCD_CS;
    /*Make pin output*/
    LCD_CS_DDR |= LCD_CS;
    /*Select CPIO function*/
    LCD_CD_PFR &= ~LCD_CD;
    /*Make pin output*/
    LCD_CD_DDR |= LCD_CD;
    /*Select CPIO function*/
    LCD_PS_PFR &= ~LCD_PS;
    /*Make pin output*/
    LCD_PS_DDR |= LCD_PS;    
    /*Select CPIO function*/
    LCD_CLK_PFR &= ~LCD_CLK;
    /*Make pin output*/
    LCD_CLK_DDR |= LCD_CLK;
    /*Select CPIO function*/
    LCD_DATA_PFR &= ~LCD_DATA;
    /*Make pin output*/
    LCD_DATA_DDR |= LCD_DATA;
}

/* RT-Thread Device Interface */
static rt_err_t rt_lcd_init (rt_device_t dev)
{
    lcd_io_init();
    
D
dzzxzz 已提交
232 233 234
    power_delay();
    lcd_write_cmd(DISPLAY_OFF);
    reset_delay();
235
    // Resetting circuit
D
dzzxzz 已提交
236 237
    lcd_write_cmd(RESET_LCD);
    reset_delay();
238
    // LCD bias setting
D
dzzxzz 已提交
239 240
    lcd_write_cmd(SET_LCD_BIAS_9);
    reset_delay();
241
    // ADC selection: display from left to right
D
dzzxzz 已提交
242 243
    lcd_write_cmd(SET_ADC_NORMAL);        
    reset_delay();
244
    // Common output state selection: display from up to down
D
dzzxzz 已提交
245 246
    lcd_write_cmd(COM_SCAN_DIR_REVERSE);
    reset_delay();
247
      
D
dzzxzz 已提交
248 249 250 251 252 253
    lcd_write_cmd(POWER_BOOSTER_ON);
    power_delay(); // 50ms requried
    lcd_write_cmd(POWER_REGULATOR_ON);
    power_delay(); // 50ms
    lcd_write_cmd(POWER_FOLLOWER_ON);
    power_delay(); // 50ms
254 255 256 257
      
    // Setting the built-in resistance radio for regulation of the V0 voltage
    // Electronic volume control
    // Power control setting
D
dzzxzz 已提交
258 259 260 261 262 263
    lcd_write_cmd(SET_ELECVOL_REG|0x05);
    delay();
    lcd_write_cmd(SET_ELECVOL_MODE);
    delay();
    lcd_write_cmd(SET_ELECVOL_REG);
    delay();
264
    //  LCD_Clear();
D
dzzxzz 已提交
265 266 267 268 269 270 271 272 273
    delay();
    lcd_write_cmd(SET_PAGE_ADDR_0);
    delay();
    lcd_write_cmd(SET_COLH_ADDR_0);
    delay();
    lcd_write_cmd(SET_COLL_ADDR_0);
    delay();
    lcd_write_cmd(DISPLAY_ON);
    delay();
274
      
D
dzzxzz 已提交
275 276 277 278 279 280 281 282
    lcd_write_cmd(DISPLAY_ALL_ON);
    delay();
    lcd_write_cmd(DISPLAY_OFF);
    delay();
    lcd_write_cmd(DISPLAY_ON);
    delay();
    lcd_write_cmd(DISPLAY_ALL_NORMAL);
    delay();
283 284 285 286
    
    return RT_EOK;
}

D
dzzxzz 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299 300
/*******************************************************************************
* Function Name  : LCD_FillAll
* Description    : Fill the whole LCD.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void LCD_FillAll(unsigned char*	buffer)
{
  unsigned char i,j = GUI_LCM_XMAX;
  unsigned char* p = buffer;  
	
  for (i=0; i<GUI_LCM_PAGE; i++)
  { 
D
dzzxzz 已提交
301 302 303
    lcd_write_cmd(SET_PAGE_ADDR_0|i);	
    lcd_write_cmd(SET_COLH_ADDR_0);		
    lcd_write_cmd(SET_COLL_ADDR_0);
D
dzzxzz 已提交
304 305 306
    j = GUI_LCM_XMAX;
    while (j--)
    {
D
dzzxzz 已提交
307 308
      lcd_write_data(*p++);
      delay();
D
dzzxzz 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321
    }
  }
}

/*******************************************************************************
* Function Name  : LCD_ClearSCR
* Description    : clean screen
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void LCD_ClearSCR(void)
{
322 323 324
  unsigned char i, j;

  for(i=0; i<GUI_LCM_PAGE; i++)
D
dzzxzz 已提交
325
  { 
326
    for(j = 0; j < GUI_LCM_XMAX; j++) 
D
dzzxzz 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
      gui_disp_buf[i][j] = 0;
  }
  LCD_FillAll((unsigned char*)gui_disp_buf);
}

/****************************************************************************
* Function Name  : LCD_UpdatePoint
* Description    : refresh the point in screen 
* Input          : x      X-coordinate
                   y      Y-coordinate
* Output         : None
* Return         : None
****************************************************************************/

void  LCD_UpdatePoint(unsigned int x, unsigned int y)
{
  unsigned char coll, colh, page;
  page = y / 8;
  coll = x & 0x0f;
  colh = x >> 4;
	
D
dzzxzz 已提交
348 349 350 351
  lcd_write_cmd(SET_PAGE_ADDR_0 | page);	        // page no.
  lcd_write_cmd(SET_COLH_ADDR_0 | colh);		// fixed col first addr
  lcd_write_cmd(SET_COLL_ADDR_0 | coll);
  lcd_write_data(gui_disp_buf[page][x]);
D
dzzxzz 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
}

/****************************************************************************
* Function Name  : LCD_PutChar
* Description    : output a char to screen 
                  (the char only can be ' ','0'~'9','A'~'Z','a'~'z')
* Input          : x      X-coordinate
                   y      Y-coordinate
                   ch     character
* Output         : None
* Return         : 1    Success
                   0    Fail
****************************************************************************/
unsigned char  LCD_PutChar(unsigned long x, unsigned long y, unsigned char ch)
{  
   unsigned char data;
368
   unsigned char i, j;
D
dzzxzz 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383

   if( x >=(GUI_LCM_XMAX-8) ) return(0);
   if( y >=(GUI_LCM_YMAX-8) ) return(0);
   
   if(ch == 0x20)
     ch -= 0x20;
     else if((ch >= 0x30)&&(ch <= 0x39))
       ch -= 0x2f;
       else if((ch >= 0x41)&&(ch <= 0x5a))
         ch -= 0x36;
         else if((ch >= 0x61)&&(ch <= 0x7a))
          ch -= 0x3C;
          else
            return(0);
    
384
   for(i = 0; i < 8; i++)
D
dzzxzz 已提交
385 386 387
   {  
      data = FONTTYPE8_8[ch][i];
      
388
      for(j = 0; j < 8; j++)
D
dzzxzz 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
      {  
         if( (data&BIT_MASK[j]) == 0)
           gui_disp_buf[y / 8][x] &= (~(0x01 << ( y % 8)));
         else  
           gui_disp_buf[y / 8][x] |= (0x01 <<( y % 8));
         LCD_UpdatePoint(x, y);
         x ++;
      }
      x -= 8;								
      y++;									
   }
   
   return(1);
}

/****************************************************************************
* Function Name  : LCD_PutString
* Description    : output string to screen 
* Input          : x      X-coordinate
                   y      Y-coordinate
                  str     pointer to string
* Output         : None
* Return         : None
****************************************************************************/
void  LCD_PutString(unsigned long x, unsigned long y, char *str)
{  
  while(1)
  {  
    if( (*str)=='\0' ) break;
    if( LCD_PutChar(x, y, *str++) == 0 ) break;
    x += 6;								
  }
}

423 424
static rt_err_t rt_lcd_control (rt_device_t dev, rt_uint8_t cmd, void *args)
{
425 426
	switch (cmd)
	{
427
#ifdef RT_USING_RTGUI    
428 429 430 431 432 433 434 435 436 437 438 439
	case RTGRAPHIC_CTRL_RECT_UPDATE:
        rt_hw_lcd_update(args);      
		break;
	case RTGRAPHIC_CTRL_POWERON:
		break;
	case RTGRAPHIC_CTRL_POWEROFF:
		break;
	case RTGRAPHIC_CTRL_GET_INFO:		
		rt_memcpy(args, &_lcd_info, sizeof(_lcd_info));
		break;
	case RTGRAPHIC_CTRL_SET_MODE:
		break;
440 441 442 443 444 445 446 447 448 449 450 451 452 453
#else
    case RT_DEVICE_CTRL_LCD_DISPLAY_ON:
        lcd_write_cmd(DISPLAY_ON);
        break;
    case RT_DEVICE_CTRL_LCD_DISPLAY_OFF:
        lcd_write_cmd(DISPLAY_OFF);
        break;
    case RT_DEVICE_CTRL_LCD_PUT_STRING:
        LCD_PutString(x, y, (char*)args);
        break;
    case RT_DEVICE_CTRL_LCD_CLEAR_SCR:
        LCD_ClearSCR();
        break;
#endif        
454 455 456
	}

	return RT_EOK;
457 458 459 460
}

void rt_hw_lcd_init(void)
{
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
	rt_device_t lcd = rt_malloc(sizeof(struct rt_device));
	if (lcd == RT_NULL) return; /* no memory yet */

	_lcd_info.bits_per_pixel = 16;
	_lcd_info.pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565;
	_lcd_info.framebuffer = RT_NULL;
	_lcd_info.width = LCD_WIDTH;
	_lcd_info.height = LCD_HEIGHT;

	/* init device structure */
	lcd->type = RT_Device_Class_Unknown;
	lcd->init = rt_lcd_init;
	lcd->open = RT_NULL;
	lcd->close = RT_NULL;
	lcd->control = rt_lcd_control;
476
#ifdef RT_USING_RTGUI
D
dzzxzz 已提交
477
	lcd->user_data = (void*)&_lcd_ops;
478 479 480
#endif	
	/* register lcd device to RT-Thread */
	rt_device_register(lcd, "lcd", RT_DEVICE_FLAG_RDWR);
481
}