402 lines
9.5 KiB
C
Executable File
402 lines
9.5 KiB
C
Executable File
#include "lcd_init.h"
|
||
#include <sys/time.h>
|
||
#include <unistd.h>
|
||
|
||
|
||
|
||
#define Device_lcd "/dev/spidev0.0"
|
||
|
||
|
||
static int gpio_dc_io_fd;
|
||
static int gpio_cs_io_fd;
|
||
static int gpio_res_io_fd;
|
||
static int spi_lcd_fd;
|
||
|
||
static u_int32_t mode = SPI_MODE_0; //用于保存 SPI 工作模式
|
||
static u_int8_t bits = 8; // 接收、发送数据位数
|
||
static u_int32_t speed = 40000000; // 发送速度
|
||
static u_int16_t delay = 0; //保存延时时间
|
||
|
||
|
||
//-----------------LCD端口定义----------------
|
||
|
||
#define LCD_RES_Clr() write(gpio_res_io_fd, SYSFS_GPIO_LOW, sizeof(SYSFS_GPIO_HIGHT));
|
||
#define LCD_RES_Set() write(gpio_res_io_fd, SYSFS_GPIO_HIGHT, sizeof(SYSFS_GPIO_LOW));
|
||
|
||
#define LCD_DC_Clr() write(gpio_dc_io_fd, SYSFS_GPIO_LOW, sizeof(SYSFS_GPIO_HIGHT));
|
||
#define LCD_DC_Set() write(gpio_dc_io_fd, SYSFS_GPIO_HIGHT, sizeof(SYSFS_GPIO_LOW));
|
||
|
||
#define LCD_CS_Clr() write(gpio_cs_io_fd, SYSFS_GPIO_LOW, sizeof(SYSFS_GPIO_LOW));
|
||
#define LCD_CS_Set() write(gpio_cs_io_fd, SYSFS_GPIO_HIGHT, sizeof(SYSFS_GPIO_HIGHT));
|
||
|
||
#define LCD_BLK_Clr() {}
|
||
#define LCD_BLK_Set() {}
|
||
|
||
static int transfer(int fd, u_int8_t const *tx, u_int8_t const *rx, size_t len)
|
||
{
|
||
int ret;
|
||
|
||
struct spi_ioc_transfer tr = {
|
||
.tx_buf = (unsigned long )tx,
|
||
.rx_buf = (unsigned long )rx,
|
||
.len = len,
|
||
.delay_usecs = delay,
|
||
.speed_hz = speed,
|
||
.bits_per_word =bits,
|
||
};
|
||
|
||
ret = ioctl(fd,SPI_IOC_MESSAGE(1),&tr);
|
||
if( ret == -1 )
|
||
{
|
||
return -1;
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
|
||
|
||
|
||
static int Gpio_init()
|
||
{
|
||
if(access("/sys/class/gpio/gpio41/value",F_OK) == -1)
|
||
{
|
||
system("echo 41 > /sys/class/gpio/export");
|
||
system("echo \"out\" > /sys/class/gpio/gpio41/direction");
|
||
}else{
|
||
|
||
}
|
||
|
||
if(access("/sys/class/gpio/gpio72/value",F_OK) == -1)
|
||
{
|
||
system("echo 72 > /sys/class/gpio/export");
|
||
system("echo \"out\" > /sys/class/gpio/gpio72/direction");
|
||
}else{
|
||
|
||
}
|
||
|
||
// if(access("/sys/class/gpio/gpio73/value",F_OK) == -1)
|
||
// {
|
||
// system("echo 73 > /sys/class/gpio/export");
|
||
// system("echo \"out\" > /sys/class/gpio/gpio73/direction");
|
||
// }else{
|
||
|
||
// }
|
||
|
||
if(access("/sys/class/gpio/gpio48/value",F_OK) == -1)
|
||
{
|
||
system("echo 48 > /sys/class/gpio/export");
|
||
system("echo \"out\" > /sys/class/gpio/gpio48/direction");
|
||
}else{
|
||
|
||
}
|
||
|
||
//system("echo \"1\" > /sys/class/gpio/gpio30/value");
|
||
// gpio_dc_io_fd = open("/sys/class/gpio/gpio73/value", O_RDWR);
|
||
// if(gpio_dc_io_fd == -1)
|
||
// {
|
||
// printf("ERR: Radio hard reset pin open error.\n");
|
||
// return EXIT_FAILURE;
|
||
// }
|
||
|
||
gpio_res_io_fd = open("/dev/gpioled", O_RDWR);
|
||
if(gpio_dc_io_fd == -1)
|
||
{
|
||
printf("ERR: Radio hard reset pin open error.\n");
|
||
return EXIT_FAILURE;
|
||
}
|
||
|
||
gpio_dc_io_fd = open("/sys/class/gpio/gpio72/value", O_RDWR);
|
||
if(gpio_res_io_fd == -1)
|
||
{
|
||
printf("ERR: Radio hard reset pin open error.\n");
|
||
return EXIT_FAILURE;
|
||
}
|
||
|
||
gpio_cs_io_fd = open("/sys/class/gpio/gpio48/value", O_RDWR);
|
||
if(gpio_cs_io_fd == -1)
|
||
{
|
||
printf("ERR: Radio hard reset pin open error.\n");
|
||
return EXIT_FAILURE;
|
||
}
|
||
// write(fd, LCD_DC_PIN ,sizeof(LCD_DC_PIN));
|
||
}
|
||
|
||
|
||
int Hardware_interface_lcd()
|
||
{
|
||
int ret;
|
||
Gpio_init();
|
||
spi_lcd_fd = open(Device_lcd, O_RDWR); //打开i2c-0的节点
|
||
if(spi_lcd_fd < 0)
|
||
{
|
||
perror("open error\n");
|
||
return spi_lcd_fd;
|
||
}
|
||
|
||
//设置spi工作模式
|
||
ret = ioctl(spi_lcd_fd,SPI_IOC_WR_MODE32,&mode);
|
||
if( ret == -1)
|
||
{
|
||
printf("SPI_IOC_WR_MODE error......\n ");
|
||
goto fd_close;
|
||
}
|
||
ret = ioctl(spi_lcd_fd,SPI_IOC_RD_MODE32,&mode);
|
||
if( ret == -1)
|
||
{
|
||
printf("SPI_IOC_RD_MODE error......\n ");
|
||
goto fd_close;
|
||
}
|
||
|
||
//设置SPI通信的字长
|
||
ret = ioctl(spi_lcd_fd,SPI_IOC_WR_BITS_PER_WORD,&bits);//SPI_IOC_WR_BITS_PER_WORD
|
||
if( ret == -1)
|
||
{
|
||
printf("SPI_IOC_WR_BITS_PER_WORD error......\n ");
|
||
goto fd_close;
|
||
}
|
||
ret = ioctl(spi_lcd_fd,SPI_IOC_RD_BITS_PER_WORD,&bits);
|
||
if( ret == -1)
|
||
{
|
||
printf("SPI_IOC_RD_BITS_PER_WORD error......\n ");
|
||
goto fd_close;
|
||
}
|
||
|
||
|
||
//设置SPI最高工作频率
|
||
ret = ioctl(spi_lcd_fd,SPI_IOC_WR_MAX_SPEED_HZ,&speed);
|
||
if( ret == -1)
|
||
{
|
||
printf("SPI_IOC_WR_MAX_SPEED_HZ error......\n ");
|
||
goto fd_close;
|
||
}
|
||
ret = ioctl(spi_lcd_fd,SPI_IOC_RD_MAX_SPEED_HZ,&speed);
|
||
if( ret == -1)
|
||
{
|
||
printf("SPI_IOC_RD_MAX_SPEED_HZ error......\n ");
|
||
goto fd_close;
|
||
}
|
||
|
||
|
||
printf("spi mode: 0x%x\n", mode);
|
||
printf("bits per word: %d\n", bits);
|
||
printf("max speed: %d Hz (%d KHz)\n", speed, speed / 1000);
|
||
return 1;
|
||
|
||
fd_close:
|
||
|
||
close(spi_lcd_fd);
|
||
return -1;
|
||
}
|
||
|
||
/******************************************************************************
|
||
函数说明:LCD串行数据写入函数
|
||
入口数据:dat 要写入的串行数据
|
||
返回值: 无
|
||
******************************************************************************/
|
||
void LCD_Writ_Bus(u_int8_t dat)
|
||
{
|
||
u_int8_t data[100];
|
||
LCD_CS_Clr();
|
||
transfer(spi_lcd_fd,&dat,data,1);
|
||
//LCD_CS_Set();
|
||
}
|
||
|
||
|
||
|
||
/******************************************************************************
|
||
函数说明:LCD写入数据
|
||
入口数据:dat 写入的数据
|
||
返回值: 无
|
||
******************************************************************************/
|
||
void LCD_WR_DATA8(u_int8_t dat)
|
||
{
|
||
LCD_DC_Set();//写数据
|
||
LCD_Writ_Bus(dat&0xff);
|
||
LCD_DC_Clr();
|
||
}
|
||
|
||
/******************************************************************************
|
||
函数说明:LCD写入数据
|
||
入口数据:dat 写入的数据
|
||
返回值: 无
|
||
******************************************************************************/
|
||
void LCD_WR_DATA(u_int16_t dat)
|
||
{
|
||
LCD_DC_Set();//写数据
|
||
LCD_Writ_Bus(dat&0xff);
|
||
LCD_DC_Clr();
|
||
}
|
||
|
||
void LCD_WR_DATAS(u_int16_t dat)
|
||
{
|
||
LCD_DC_Set();//写数据
|
||
LCD_Writ_Bus(dat>>8);
|
||
LCD_Writ_Bus(dat&0xff);
|
||
LCD_DC_Clr();
|
||
}
|
||
|
||
void LCD_Write_Buffs(u_int8_t* buff,u_int16_t len)
|
||
{
|
||
LCD_DC_Set();//写数据
|
||
u_int8_t data[480];
|
||
transfer(spi_lcd_fd,&buff[0],&data,len);
|
||
|
||
LCD_DC_Clr();
|
||
}
|
||
|
||
/******************************************************************************
|
||
函数说明:LCD写入命令
|
||
入口数据:dat 写入的命令
|
||
返回值: 无
|
||
******************************************************************************/
|
||
void LCD_WR_REG(u_int8_t dat)
|
||
{
|
||
LCD_DC_Clr();//写命令
|
||
LCD_Writ_Bus(dat);
|
||
LCD_DC_Set();//写数据
|
||
}
|
||
|
||
|
||
/******************************************************************************
|
||
函数说明:设置起始和结束地址
|
||
入口数据:x1,x2 设置列的起始和结束地址
|
||
y1,y2 设置行的起始和结束地址
|
||
返回值: 无
|
||
******************************************************************************/
|
||
void LCD_Address_Set(u_int16_t x1,u_int16_t y1,u_int16_t x2,u_int16_t y2)
|
||
{
|
||
|
||
// if(USE_HORIZONTAL==0)
|
||
// {
|
||
// LCD_WR_REG(0x2a);//列地址设置
|
||
// LCD_WR_DATA(x1+2);
|
||
// LCD_WR_DATA(x2+2);
|
||
// LCD_WR_REG(0x2b);//行地址设置
|
||
// LCD_WR_DATA(y1+1);
|
||
// LCD_WR_DATA(y2+1);
|
||
// LCD_WR_REG(0x2c);//储存器写
|
||
// }
|
||
// else if(USE_HORIZONTAL==1)
|
||
// {
|
||
// LCD_WR_REG(0x2a);//列地址设置
|
||
// LCD_WR_DATA(x1+0);
|
||
// LCD_WR_DATA(x2+0);
|
||
// LCD_WR_REG(0x2b);//行地址设置
|
||
// LCD_WR_DATA(y1+0);
|
||
// LCD_WR_DATA(y2+0);
|
||
// LCD_WR_REG(0x2c);//储存器写
|
||
// }
|
||
// else if(USE_HORIZONTAL==2)
|
||
// {
|
||
// LCD_WR_REG(0x2a);//列地址设置
|
||
// LCD_WR_DATA(x1+1);
|
||
// LCD_WR_DATA(x2+1);
|
||
// LCD_WR_REG(0x2b);//行地址设置
|
||
// LCD_WR_DATA(y1+2);
|
||
// LCD_WR_DATA(y2+2);
|
||
// LCD_WR_REG(0x2c);//储存器写
|
||
|
||
// }
|
||
// else
|
||
{
|
||
LCD_WR_REG(0x2a);//列地址设置
|
||
LCD_WR_DATAS(x1);
|
||
LCD_WR_DATAS(x2);
|
||
LCD_WR_REG(0x2b);//行地址设置
|
||
LCD_WR_DATAS(y1);
|
||
LCD_WR_DATAS(y2);
|
||
LCD_WR_REG(0x2c);//储存器写
|
||
}
|
||
|
||
}
|
||
void LCD_RESET(void)
|
||
{
|
||
LCD_RES_Clr();
|
||
usleep(1000*100);
|
||
LCD_RES_Set();
|
||
usleep(1000*100);
|
||
}
|
||
|
||
|
||
|
||
void LCD_Init(void)
|
||
{
|
||
Hardware_interface_lcd();
|
||
usleep(1000*100);
|
||
LCD_RESET();
|
||
usleep(1000*100);
|
||
|
||
|
||
//************* Start Initial Sequence **********//
|
||
usleep(1000*120); // Delay 120ms
|
||
LCD_WR_REG(0x11); // Sleep Out
|
||
usleep(1000*120); // Delay 120ms
|
||
LCD_WR_REG(0xf0) ;
|
||
LCD_WR_DATA(0xc3) ;
|
||
LCD_WR_REG(0xf0) ;
|
||
LCD_WR_DATA(0x96) ;
|
||
LCD_WR_REG(0x36);
|
||
LCD_WR_DATA(0x48);
|
||
LCD_WR_REG(0x3A);
|
||
LCD_WR_DATA(0x55);
|
||
LCD_WR_REG(0xB4);
|
||
LCD_WR_DATA(0x01);
|
||
LCD_WR_REG(0xB7) ;
|
||
LCD_WR_DATA(0xC6) ;
|
||
LCD_WR_REG(0xe8);
|
||
LCD_WR_DATA(0x40);
|
||
LCD_WR_DATA(0x8a);
|
||
LCD_WR_DATA(0x00);
|
||
LCD_WR_DATA(0x00);
|
||
LCD_WR_DATA(0x29);
|
||
LCD_WR_DATA(0x19);
|
||
LCD_WR_DATA(0xa5);
|
||
LCD_WR_DATA(0x33);
|
||
LCD_WR_REG(0xc1);
|
||
LCD_WR_DATA(0x06);
|
||
LCD_WR_REG(0xc2);
|
||
LCD_WR_DATA(0xa7);
|
||
LCD_WR_REG(0xc5);
|
||
LCD_WR_DATA(0x18);
|
||
LCD_WR_REG(0xe0); //Positive Voltage Gamma Control
|
||
LCD_WR_DATA(0xf0);
|
||
LCD_WR_DATA(0x09);
|
||
LCD_WR_DATA(0x0b);
|
||
LCD_WR_DATA(0x06);
|
||
LCD_WR_DATA(0x04);
|
||
LCD_WR_DATA(0x15);
|
||
LCD_WR_DATA(0x2f);
|
||
LCD_WR_DATA(0x54);
|
||
LCD_WR_DATA(0x42);
|
||
LCD_WR_DATA(0x3c);
|
||
LCD_WR_DATA(0x17);
|
||
LCD_WR_DATA(0x14);
|
||
LCD_WR_DATA(0x18);
|
||
LCD_WR_DATA(0x1b);
|
||
LCD_WR_REG(0xe1); //Negative Voltage Gamma Control
|
||
LCD_WR_DATA(0xf0);
|
||
LCD_WR_DATA(0x09);
|
||
LCD_WR_DATA(0x0b);
|
||
LCD_WR_DATA(0x06);
|
||
LCD_WR_DATA(0x04);
|
||
LCD_WR_DATA(0x03);
|
||
LCD_WR_DATA(0x2d);
|
||
LCD_WR_DATA(0x43);
|
||
LCD_WR_DATA(0x42);
|
||
LCD_WR_DATA(0x3b);
|
||
LCD_WR_DATA(0x16);
|
||
LCD_WR_DATA(0x14);
|
||
LCD_WR_DATA(0x17);
|
||
LCD_WR_DATA(0x1b);
|
||
LCD_WR_REG(0xf0);
|
||
LCD_WR_DATA(0x3c);
|
||
LCD_WR_REG(0xf0);
|
||
LCD_WR_DATA(0x69);
|
||
usleep(1000*120); //Delay 120ms
|
||
LCD_WR_REG(0x29);// Display on
|
||
|
||
LCD_WR_REG(0x36);
|
||
LCD_WR_DATA((1<<3)|(0<<7)|(0<<6)|(1<<5));
|
||
}
|
||
|