/*
* FILE : ra8875.c
* DESCRIPTION : This file is PLAM ra8875 driver demo.
* Author : momo
* Copyright :
*
* History
* --------------------
* Rev : 0.00
* Date : 07/17/2013
*
* create.
* --------------------
*/
//---------------- Include files ------------------------//
#include "..\include\hardware.h"
#include "..\include\ra8875.h"
#include "..\include\fpga.h"
#include "stm32f4xx.h"
//---------------- Function Prototype -------------------//
static int LCD_Initial(void);
static int LCD_CmdWrite(int Cmd);
static int LCD_DataWrite(int Data);
static int Write_Dir(int Cmd,int Data);
static int LCD_StatusRead(void);
static int LCD_DataRead(void);
static int LCD_Reset(void);
static int Chk_Busy(void);
static int Chk_BTE_Busy(void);
static int Chk_DMA_Busy(void);
static int Active_Window(int XL,int XR ,int YT ,int YB);
static int Text_Background_Color1(unsigned int b_color);
static int Text_Foreground_Color1(unsigned int b_color);
static int Draw_Circle(int X,int Y,int R);
static int Draw_Ellipse(int X,int Y,int R1,int R2);
static int Draw_Line(int XS,int XE ,int YS,int YE);
static int Text_Background_Color(unsigned int setR,unsigned int setG,unsigned int setB);
static int Text_Foreground_Color(unsigned int setR,unsigned int setG,unsigned int setB);
#define color_brown 0x40c0
#define color_black 0x0000
#define color_white 0xffff
#define color_red 0xf800
#define color_green 0x07e0
#define color_blue 0x001f
#define color_yellow color_red|color_green
#define color_cyan color_green|color_blue
#define color_purple color_red|color_blue
//---------------- Variable -----------------------------//
SYS_RA8875_T ra8875 = {
.LCD_Initial = LCD_Initial,
.LCD_CmdWrite = LCD_CmdWrite,
.LCD_DataWrite = LCD_DataWrite,
.Write_Dir = Write_Dir,
.LCD_StatusRead = LCD_StatusRead,
.LCD_DataRead = LCD_DataRead,
.LCD_Reset = LCD_Reset,
.Chk_Busy = Chk_Busy,
.Chk_BTE_Busy = Chk_BTE_Busy,
.Chk_DMA_Busy = Chk_DMA_Busy,
.Active_Window = Active_Window,
.Text_Background_Color1 = Text_Background_Color1,
.Text_Foreground_Color1 = Text_Foreground_Color1,
.Write_Dir = Write_Dir,
.Draw_Circle = Draw_Circle,
.Draw_Ellipse = Draw_Ellipse,
.Draw_Line = Draw_Line,
.Text_Background_Color = Text_Background_Color,
.Text_Foreground_Color = Text_Foreground_Color
};
//-----------------Function------------------------------//
/*
* Name : LCD_CmdWrite
* Description : ---
* Author : momo.
*
* History
* ----------------------
* Rev : 0.00
* Date : 07/19/2013
*
* create.
* ----------------------
* 写指令函数
*/
static int LCD_CmdWrite(int Cmd)
{
tft_write(1, Cmd);
return 0;
}
/*
* Name : LCD_DataWrite
* Description : ---
* Author : momo.
*
* History
* ----------------------
* Rev : 0.00
* Date : 07/19/2013
*
* create.
* ----------------------
* 写数据函数
*/
static int LCD_DataWrite(int Data)
{
tft_write(0, Data);
return 0;
}
/*
* Name : Write_Dir
* Description : ---
* Author : momo.
*
* History
* ----------------------
* Rev : 0.00
* Date : 07/19/2013
*
* create.
* ----------------------
* 写指令和写数据函数
*/
static int Write_Dir(int Cmd,int Data) //写指令
{
LCD_CmdWrite(Cmd);
LCD_DataWrite(Data);
return 0;
}
// /*
// * Name : read
// * Description : ---
// * Author : momo.
// *
// * History
// * ----------------------
// * Rev : 0.00
// * Date : 07/19/2013
// *
// * create.
// * ----------------------
// * 读tft数据
// */
// static int read(int offset)
// {
// tft_write(1,offset);
//
// return tft_read(0);
// }
/*
* Name : LCD_StatusRead
* Description : ---
* Author : momo.
*
* History
* ----------------------
* Rev : 0.00
* Date : 07/19/2013
*
* create.
* ----------------------
* 读寄存器的状态
*/
static int LCD_StatusRead(void) //读状态
{
unsigned char Data;
Data = tft_read(1);
return Data;
}
/*
* Name : LCD_DataRead
* Description : ---
* Author : momo.
*
* History
* ----------------------
* Rev : 0.00
* Date : 07/19/2013
*
* create.
* ----------------------
* 读寄存器的数据
*/
static int LCD_DataRead(void) //读数据
{
unsigned Data;
Data = tft_read(0);
return Data;
}
/*
* Name : LCD_Reset
* Description : ---
* Author : momo.
*
* History
* ----------------------
* Rev : 0.00
* Date : 07/19/2013
*
* create.
* ----------------------
* LCD复位函数
*/
static int LCD_Reset(void) //LCD复位
{
int i = 0;
RA8875_RST_OUTPUT;
RA8875_RST_OFF;
for(i=0;i<1000000;i++);
RA8875_RST_ON;
for(i=0;i<1000000;i++);
return 0;
}
/*
* Name : Chk_Busy
* Description : ---
* Author : momo.
*
* History
* ----------------------
* Rev : 0.00
* Date : 07/19/2013
*
* create.
* ----------------------
* 测忙函数组
*/
static int Chk_Busy(void)
{
char temp;
do
{
temp = LCD_StatusRead();
}while((temp&0x80) == 0x80);
return 0;
}
/*
* Name : Chk_BTE_Busy
* Description : ---
* Author : momo.
*
* History
* ----------------------
* Rev : 0.00
* Date : 07/19/2013
*
* create.
* ----------------------
* 测忙函数组
*/
static int Chk_BTE_Busy(void)
{
char temp;
do
{
temp = LCD_StatusRead();
}while((temp&0x40) == 0x40);
return 0;
}
/*
* Name : Chk_DMA_Busy
* Description : ---
* Author : momo.
*
* History
* ----------------------
* Rev : 0.00
* Date : 07/19/2013
*
* create.
* ----------------------
* 测忙函数组
*/
static int Chk_DMA_Busy(void)
{
char temp;
do
{
LCD_CmdWrite(0xBF);
temp = LCD_DataRead();
}while((temp&0x01) == 0x01);
return 0;
}
/*
* Name : PLL_ini
* Description : ---
* Author : momo.
*
* History
* ----------------------
* Rev : 0.00
* Date : 07/19/2013
*
* create.
* ----------------------
* 倍频设置
*/
static int PLL_ini(void)
{
int i = 0;
LCD_CmdWrite(0x88);
for(i = 0; i < 1000; i ++);
LCD_DataWrite(0x08);
for(i=0;i<10000;i++);
LCD_CmdWrite(0x89);
for(i = 0; i < 1000; i ++);
LCD_DataWrite(0x04);
for(i=0;i<10000;i++);
return 0;
}
/*
* Name : LCD_Initial
* Description : ---
* Author : momo.
*
* History
* ----------------------
* Rev : 0.00
* Date : 07/19/2013
*
* create.
* ----------------------
* 初始化设置
*/
static int LCD_Initial(void)
{
int i = 0;
for(int k = 0; k < 10000000; k ++);
LCD_Reset();
PLL_ini();
LCD_CmdWrite(0x10); //SYSR bit[4:3]=00 256 color bit[2:1]= 00 8bit MPU interface
LCD_DataWrite(0x0f); // 1x 64k color 1x 16bit
LCD_CmdWrite(0x04); //PCLK
LCD_DataWrite(0x00); //PCLK频率周期 = 1*系统频率周期
for(i = 0; i<100000;i++);
//水平设置
LCD_CmdWrite(0x14); //HDWR//Horizontal Display Width Setting Bit[6:0]
LCD_DataWrite(0x3b); //Horizontal display width(pixels) = (HDWR + 1)*8 = (59+1)*8 = 480 0x27
LCD_CmdWrite(0x15); //HNDFCR//Horizontal Non-Display Period fine tune Bit[3:0]
LCD_DataWrite(0x00); //(HNDR + 1)*8 +HNDFCR
LCD_CmdWrite(0x16); //HNDR//Horizontal Non-Display Period Bit[4:0]
LCD_DataWrite(0x01); //Horizontal Non-Display Period (pixels) = (HNDR + 1)*8
LCD_CmdWrite(0x17); //HSTR//HSYNC Start Position[4:0]
LCD_DataWrite(0x00); //HSYNC Start Position(PCLK) = (HSTR + 1)*8
LCD_CmdWrite(0x18); //HPWR//HSYNC Polarity ,The period width of HSYNC.
LCD_DataWrite(0x05); //HSYNC Width [4:0] HSYNC Pulse width(PCLK) = (HPWR + 1)*8
//垂直设置
LCD_CmdWrite(0x19); //VDHR0 //Vertical Display Height Bit [7:0]
LCD_DataWrite(0x0f); //Vertical pixels = VDHR + 1 = 10f+1=272
LCD_CmdWrite(0x1a); //VDHR1 //Vertical Display Height Bit [8]
LCD_DataWrite(0x01); //Vertical pixels = VDHR + 1 0x00
LCD_CmdWrite(0x1b); //VNDR0 //Vertical Non-Display Period Bit [7:0]
LCD_DataWrite(0x02); //Vertical Non-Display area = (VNDR + 1)
LCD_CmdWrite(0x1c); //VNDR1 //Vertical Non-Display Period Bit [8]
LCD_DataWrite(0x00); //Vertical Non-Display area = (VNDR + 1)
LCD_CmdWrite(0x1d); //VSTR0 //VSYNC Start Position[7:0]
LCD_DataWrite(0x07); //VSYNC Start Position(PCLK) = (VSTR + 1)
LCD_CmdWrite(0x1e); //VSTR1 //VSYNC Start Position[8]
LCD_DataWrite(0x00); //VSYNC Start Position(PCLK) = (VSTR + 1)
LCD_CmdWrite(0x1f); //VPWR //VSYNC Polarity ,VSYNC Pulse Width[6:0]
LCD_DataWrite(0x09); //VSYNC Pulse Width(PCLK) = (VPWR + 1)
//Active window set
//setting active window X
Write_Dir(0x30,0x00);
Write_Dir(0x31,0x00);
Write_Dir(0x34,0xdf);
Write_Dir(0x35,0x01);
//setting active window Y
Write_Dir(0x32,0x00);
Write_Dir(0x33,0x00);
Write_Dir(0x36,0x0f);
Write_Dir(0x37,0x01);
for(i = 0; i<10000;i++);
LCD_CmdWrite(0x04); //PCLK
LCD_DataWrite(0x00); //PCLK频率周期 = 系统频率周期
LCD_CmdWrite(0x8a);//PWM控制设置
LCD_DataWrite(0x80);//打开PWM
LCD_CmdWrite(0x8a);//PWM控制设置
LCD_DataWrite(0x81);//开启PWM
LCD_CmdWrite(0x8b);//背光亮度
LCD_DataWrite(0x7f);//亮度参数0xff-0x00
Write_Dir(0x40,0x00); //绘图模式 0为→↓,1为←↓,2为↑→,3为↓→
Write_Dir(0x41,0x00); //图形光标,图层选择
// ra8875.Write_Dir(0x8b,0x7f);//背光亮度调节 0x3f:25% ;ox7f:50% ;0xff:100%
// for(i = 0; i<10000;i++);
return 0;
}
/*
* Name : Active_Window
* Description : ---
* Author : momo.
*
* History
* ----------------------
* Rev : 0.00
* Date : 07/19/2013
*
* create.
* ----------------------
* 设置工作窗口区域
*/
static int Active_Window(int XL,int XR ,int YT ,int YB)
{
int temp;
//setting active window X
temp=XL;
LCD_CmdWrite(0x30);//HSAW0 设置开始X
LCD_DataWrite(temp);
temp=XL>>8;
LCD_CmdWrite(0x31);//HSAW1
LCD_DataWrite(temp);
temp=XR;
LCD_CmdWrite(0x34);//HEAW0 设置结束Y
LCD_DataWrite(temp);
temp=XR>>8;
LCD_CmdWrite(0x35);//HEAW1
LCD_DataWrite(temp);
//setting active window Y
temp=YT;
LCD_CmdWrite(0x32);//VSAW0 设置开始Y
LCD_DataWrite(temp);
temp=YT>>8;
LCD_CmdWrite(0x33);//VSAW1
LCD_DataWrite(temp);
temp=YB;
LCD_CmdWrite(0x36);//VEAW0 设置结束Y
LCD_DataWrite(temp);
temp=YB>>8;
LCD_CmdWrite(0x37);//VEAW1
LCD_DataWrite(temp);
return 0;
}
/*
* Name : Text_Background_Color1
* Description : ---
* Author : momo.
*
* History
* ----------------------
* Rev : 0.00
* Date : 07/19/2013
*
* create.
* ----------------------
* 背景颜色设置
*/
static int Text_Background_Color1(unsigned int b_color)
{
LCD_CmdWrite(0x60);//BGCR0
LCD_DataWrite((int)((b_color & 0xf800 )>>11));
LCD_CmdWrite(0x61);//BGCR0
LCD_DataWrite((int)((b_color & 0x07e0 )>>5));
LCD_CmdWrite(0x62);//BGCR0
LCD_DataWrite((int)((b_color & 0x001f)));
return 0;
}
static int Text_Background_Color(unsigned int setR,unsigned int setG,unsigned int setB)
{
LCD_CmdWrite(0x60);//BGCR0
LCD_DataWrite(setR);
LCD_CmdWrite(0x61);//BGCR0
LCD_DataWrite(setG);
LCD_CmdWrite(0x62);//BGCR0
LCD_DataWrite(setB);
return 0;
}
/*
* Name : Text_Foreground_Color1
* Description : ---
* Author : momo.
*
* History
* ----------------------
* Rev : 0.00
* Date : 07/19/2013
*
* create.
* ----------------------
* 前景颜色设置
*/
static int Text_Foreground_Color1(unsigned int b_color)
{
LCD_CmdWrite(0x63);//BGCR0
LCD_DataWrite((int)((b_color & 0xf800 )>>11));
LCD_CmdWrite(0x64);//BGCR0
LCD_DataWrite((int)((b_color & 0x07e0 )>>5));
LCD_CmdWrite(0x65);//BGCR0
LCD_DataWrite((int)((b_color & 0x001f)));
return 0;
}
static int Text_Foreground_Color(unsigned int setR,unsigned int setG,unsigned int setB)
{
LCD_CmdWrite(0x63);//BGCR0
LCD_DataWrite(setR);
LCD_CmdWrite(0x64);//BGCR0
LCD_DataWrite(setG);
LCD_CmdWrite(0x65);//BGCR0
LCD_DataWrite(setB);
return 0;
}
/*
* Name : Draw_Circle
* Description : ---
* Author : momo.
*
* History
* ----------------------
* Rev : 0.00
* Date : 07/19/2013
*
* create.
* ----------------------
* 画圆
*/
static int Draw_Circle(int X,int Y,int R)
{
int temp;
temp=X;
LCD_CmdWrite(0x99);
LCD_DataWrite(temp);
temp=X>>8;
LCD_CmdWrite(0x9a);
LCD_DataWrite(temp);
temp=Y;
LCD_CmdWrite(0x9b);
LCD_DataWrite(temp);
temp=Y>>8;
LCD_CmdWrite(0x9c);
LCD_DataWrite(temp);
temp=R;
LCD_CmdWrite(0x9d);
LCD_DataWrite(temp);
return 0;
}
/*
* Name : BTE_Size
* Description : ---
* Author : momo.
*
* History
* ----------------------
* Rev : 0.00
* Date : 07/19/2013
*
* create.
* ----------------------
* 椭圆
*/
static int Draw_Ellipse(int X,int Y,int R1,int R2)
{
LCD_CmdWrite(0xA5);
LCD_DataWrite(X);
LCD_CmdWrite(0xA7);
LCD_DataWrite(Y);
LCD_CmdWrite(0xA1);
LCD_DataWrite(R1);
LCD_CmdWrite(0xA3);
LCD_DataWrite(R2);
return 0;
}
/*
* Name : BTE_Size
* Description : ---
* Author : momo.
*
* History
* ----------------------
* Rev : 0.00
* Date : 07/19/2013
*
* create.
* ----------------------
* 画线、矩形、三角形
*/
static int Draw_Line(int XS,int XE ,int YS,int YE)
{
int temp;
temp=XS;
LCD_CmdWrite(0x91);
LCD_DataWrite(temp);
temp=XS>>8;
LCD_CmdWrite(0x92);
LCD_DataWrite(temp);
temp=XE;
LCD_CmdWrite(0x95);
LCD_DataWrite(temp);
temp=XE>>8;
LCD_CmdWrite(0x96);
LCD_DataWrite(temp);
temp=YS;
LCD_CmdWrite(0x93);
LCD_DataWrite(temp);
temp=YS>>8;
LCD_CmdWrite(0x94);
LCD_DataWrite(temp);
temp=YE;
LCD_CmdWrite(0x97);
LCD_DataWrite(temp);
temp=YE>>8;
LCD_CmdWrite(0x98);
LCD_DataWrite(temp);
return 0;
}