初级会员
- 积分
- 195
- 金钱
- 195
- 注册时间
- 2017-10-8
- 在线时间
- 55 小时
|
10金钱
买了原子1080的10寸屏。参考左神linux例程ft5426以及阿波罗stm32f767的例程代码改写触摸屏驱动。目前能够读写gt9271的寄存器,但是配置好以后读取gt9271状态寄存器却始终为零(无触摸)。求解?
gt9721.h如下:
#ifndef __BSP_GT9271_H
#define __BSP_GT9271_H
#include "imx6u.h"
/* 寄存器宏 */
/* 宏定义 */
#define GT9271_ADDR 0X14 /* GT9271设备地址(7位)*/
#define GT9271_CTRL_REG 0x8040 /* 控制寄存器 */
#define GT9271_CFGS_REG 0x8047 /* 配置起始地址寄存器 */
#define GT9271_CHECK_REG 0x80FF /* 校验和寄存器 */
#define GT9271_PID_REG 0x8140 /* 产品ID寄存器 */
#define GT9271_GSTID_REG 0x814E /* 产品状态寄存器 */
#define GT9271_TP1_REG 0x8150 /* 第1个触摸点数据地址 */
#define GT9271_TP2_REG 0x8158 /* 第2个触摸点数据地址 */
#define GT9271_TP3_REG 0x8160 /* 第3个触摸点数据地址 */
#define GT9271_TP4_REG 0x8168 /* 第4个触摸点数据地址 */
#define GT9271_TP5_REG 0x8170 /* 第5个触摸点数据地址 */
#define GT9271_TP6_REG 0x8178 /* 第6个触摸点数据地址 */
#define GT9271_TP7_REG 0x8180 /* 第7个触摸点数据地址 */
#define GT9271_TP8_REG 0x8188 /* 第8个触摸点数据地址 */
#define GT9271_TP9_REG 0x8190 /* 第9个触摸点数据地址 */
#define GT9271_TP10_REG 0x8198 /* 第10个触摸点数据地址 */
#define GT9271_XYCOORDREG_NUM 30 /* 触摸点坐标寄存器数量 */
#define GT9271_INIT_FINISHED 1 /* 触摸屏初始化完成 */
#define GT9271_INIT_NOTFINISHED 0 /* 触摸屏初始化未完成 */
/* 触摸信息结构体 */
struct gt9271_dev_struct {
unsigned char initflag; /* 触摸屏初始化状态 */
unsigned char point_num; /* 触摸点数量 */
unsigned short x[10]; /* 10点X轴坐标 */
unsigned short y[10]; /* 10点Y轴坐标 */
};
extern struct gt9271_dev_struct gt9271_dev;
void gt9271_init(void);
void gpio1_io09_irqhandler(unsigned int gicciar, void *param);
unsigned char gt9271_write_byte(unsigned char addr, unsigned int reg, unsigned char data);
unsigned char gt9271_write_len(unsigned char addr, unsigned int reg, unsigned int len, unsigned char *buf);
unsigned char gt9271_read_byte(unsigned char addr, unsigned int reg);
void gt9271_read_len(unsigned char addr, unsigned int reg, unsigned char len, unsigned char *buf);
void gt9271_read_tpcoord(void);
#endif
gt9721.c如下:
#include "bsp_gt9271.h"
#include "bsp_gpio.h"
#include "bsp_int.h"
#include "bsp_delay.h"
#include "bsp_i2c.h"
#include "stdio.h"
//GT9271配置参数表
//第一个字节为版本号(0X41),必须保证新的版本号大于等于GT9271内部
//flash原有版本号,才会更新配置.
//分辨率1280*800
const u8 GT9271_CFG_TBL[]=
{
0x41,0x00,0x05,0x20,0x03,0x0A,0x3D,0x20,0x01,0x0A,
0x28,0x0F,0x6E,0x5A,0x03,0x05,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x18,0x1A,0x1E,0x14,0x8F,0x2F,0xAA,
0x26,0x24,0x0C,0x08,0x00,0x00,0x00,0x81,0x03,0x2D,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x1A,0x3C,0x94,0xC5,0x02,0x07,0x00,0x00,0x04,
0x9E,0x1C,0x00,0x89,0x21,0x00,0x77,0x27,0x00,0x68,
0x2E,0x00,0x5B,0x37,0x00,0x5B,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x19,0x18,0x17,0x16,0x15,0x14,0x11,0x10,
0x0F,0x0E,0x0D,0x0C,0x09,0x08,0x07,0x06,0x05,0x04,
0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x02,0x04,0x06,0x07,0x08,0x0A,0x0C,
0x0D,0x0F,0x10,0x11,0x12,0x13,0x14,0x19,0x1B,0x1C,
0x1E,0x1F,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
0x28,0x29,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x5D,0x01
};
struct gt9271_dev_struct gt9271_dev;
/* 初始化GT9271 */
void gt9271_init(void)
{
unsigned char reg_value[2];
//unsigned int id;
/* 1、IO初始化 */
IOMUXC_SetPinMux(IOMUXC_UART5_TX_DATA_I2C2_SCL, 1); /*复用为I2C2_SCL */
IOMUXC_SetPinMux(IOMUXC_UART5_RX_DATA_I2C2_SDA, 1); /*复用为I2C2_SDA */
IOMUXC_SetPinConfig(IOMUXC_UART5_TX_DATA_I2C2_SCL, 0X70b0);
IOMUXC_SetPinConfig(IOMUXC_UART5_RX_DATA_I2C2_SDA, 0X70b0);
/* 初始化INT引脚,使能中断 */
IOMUXC_SetPinMux(IOMUXC_GPIO1_IO09_GPIO1_IO09, 0); /*复用为GPIO1_IO09 */
IOMUXC_SetPinConfig(IOMUXC_GPIO1_IO09_GPIO1_IO09 , 0XF080);
gpio_pin_config_t ctint_config;
ctint_config.direction = kGPIO_DigitalInput;
ctint_config.interruptMode = kGPIO_IntFallingEdge;
gpio_init(GPIO1, 9, &ctint_config);
/* 初始化RST引脚,使能中断 */
IOMUXC_SetPinMux(IOMUXC_SNVS_SNVS_TAMPER9_GPIO5_IO09, 0); /*复用为GPIO5_IO09 */
IOMUXC_SetPinConfig(IOMUXC_SNVS_SNVS_TAMPER9_GPIO5_IO09, 0X10b0);
gpio_pin_config_t ctrst_config;
ctrst_config.direction = kGPIO_DigitalOutput;
ctrst_config.outputLogic = 1;
gpio_init(GPIO5, 9, &ctrst_config);
GIC_EnableIRQ(GPIO1_Combined_0_15_IRQn );
system_register_irqhandler(GPIO1_Combined_0_15_IRQn , (system_irq_handler_t)gpio1_io09_irqhandler, NULL);
gpio_enableint(GPIO1, 9);
/* 2、I2C2接口初始化 */
i2c_init(I2C2);
/* 3、GT9271初始化*/
gpio_pinwrite(GPIO5, 9, 0); /* 复位GT9271 */
delay_ms(10);
gpio_pinwrite(GPIO5, 9, 1); /* 停止复位 */
delay_ms(500);
gt9271_read_len(GT9271_ADDR, GT9271_PID_REG, 4, reg_value);
printf("TouchPad ID:%s\r\n",reg_value);
if(strcmp((char*)reg_value,"9271")==0) //ID==9147
{
reg_value[0]=0X02;
gt9271_write_byte(GT9271_ADDR, GT9271_CTRL_REG,reg_value[0]);//软复位GT9271
delay_ms(10);
reg_value[0]=gt9271_read_byte(GT9271_ADDR, GT9271_CFGS_REG);
printf("TouchPad ConfigVersion:%#X\r\n",reg_value[0]);
if(reg_value[0]<0X41) //默认版本比较低,需要更新flash配置
{
gt9271_send_cfg(1); //更新并保存配置
printf("ReConfigPara Complete!!\r\n");
}
delay_ms(10);
reg_value[0]=gt9271_read_byte(GT9271_ADDR, GT9271_CFGS_REG);
printf("TouchPad ConfigVersion:%#X\r\n",reg_value[0]);
reg_value[0]=0X00;
gt9271_write_byte(GT9271_ADDR, GT9271_CTRL_REG,reg_value[0]); //结束复位
}
gt9271_dev.initflag = GT9271_INIT_FINISHED;
}
void gt9271_send_cfg(unsigned char mode)
{
unsigned char buf[2];
unsigned char i=0;
buf[0]=0;
buf[1]=mode; //是否写入到GT9271 FLASH? 即是否掉电保存
for(i=0;i<(sizeof(GT9271_CFG_TBL)-2);i++)buf[0]+=GT9271_CFG_TBL; //计算校验和
buf[0]=(~buf[0])+1;
gt9271_write_len(GT9271_ADDR, GT9271_CFGS_REG, sizeof(GT9271_CFG_TBL), GT9271_CFG_TBL);//发送寄存器配置
gt9271_write_len(GT9271_ADDR, GT9271_CHECK_REG, sizeof(buf), buf); //写入校验和,和配置更新标记
return 0;
}
/* 中断处理函数 */
void gpio1_io09_irqhandler(unsigned int gicciar, void *param)
{
if(gt9271_dev.initflag == GT9271_INIT_FINISHED)
{
gt9271_read_tpcoord();
printf("INT\r\n");
}
/* 清除中断表示位 */
gpio_clearintflags(GPIO1, 9);
}
/* 向GT9271写一个字节的数据 */
unsigned char gt9271_write_byte(unsigned char addr, unsigned int reg,
unsigned char data)
{
unsigned char writedata = data;
unsigned char status = 0;
struct i2c_transfer masterXfer;
masterXfer.slaveAddress = addr;
masterXfer.direction = kI2C_Write;
masterXfer.subaddress = reg;
masterXfer.subaddressSize = 2;
masterXfer.data = &writedata;
masterXfer.dataSize = 1;
if(i2c_master_transfer(I2C2, &masterXfer))
status = 1;
return status;
}
/* 向GT9271写多个字节的数据 */
unsigned char gt9271_write_len(unsigned char addr, unsigned int reg,
unsigned int len, unsigned char *buf)
{
unsigned char status = 0;
struct i2c_transfer masterXfer;
masterXfer.slaveAddress = addr;
masterXfer.direction = kI2C_Write;
masterXfer.subaddress = reg;
masterXfer.subaddressSize = 2;
masterXfer.data = buf;
masterXfer.dataSize = len;
if(i2c_master_transfer(I2C2, &masterXfer))
status = 1;
return status;
}
/* 从GT9271读取一个字节数据 */
unsigned char gt9271_read_byte(unsigned char addr, unsigned int reg)
{
unsigned char val = 0;
struct i2c_transfer masterXfer;
masterXfer.slaveAddress = addr;
masterXfer.direction = kI2C_Read;
masterXfer.subaddress = reg;
masterXfer.subaddressSize = 2;
masterXfer.data = &val;
masterXfer.dataSize = 1;
i2c_master_transfer(I2C2, &masterXfer);
return val;
}
/* 从GT9271读取多个寄存器数据 */
void gt9271_read_len(unsigned char addr, unsigned int reg,
unsigned char len, unsigned char *buf)
{
struct i2c_transfer masterXfer;
masterXfer.slaveAddress = addr;
masterXfer.direction = kI2C_Read;
masterXfer.subaddress = reg;
masterXfer.subaddressSize = 2;
masterXfer.data = buf;
masterXfer.dataSize = len;
i2c_master_transfer(I2C2, &masterXfer);
}
main.c
主要在循环中读取状态寄存器的值
#include "main.h"
#include "bsp_clk.h"
#include "bsp_led.h"
#include "bsp_delay.h"
#include "bsp_beep.h"
#include "bsp_key.h"
#include "bsp_int.h"
#include "bsp_exti.h"
#include "bsp_epit.h"
#include "bsp_keyfilter.h"
#include "bsp_uart.h"
#include "bsp_lcd.h"
#include "bsp_rtc.h"
#include "bsp_lcdapi.h"
#include "bsp_ap3216c.h"
#include "bsp_icm20608.h"
#include "bsp_gt9271.h"
#include "stdio.h"
/*
* @description : 使能I.MX6U的硬件NEON和FPU
* @param : 无
* @return : 无
*/
void imx6ul_hardfpu_enable(void)
{
uint32_t cpacr;
uint32_t fpexc;
/* 使能NEON和FPU */
cpacr = __get_CPACR();
cpacr = (cpacr & ~(CPACR_ASEDIS_Msk | CPACR_D32DIS_Msk))
| (3UL << CPACR_cp10_Pos) | (3UL << CPACR_cp11_Pos);
__set_CPACR(cpacr);
fpexc = __get_FPEXC();
fpexc |= 0x40000000UL;
__set_FPEXC(fpexc);
}
void lcd_draw_bigpoint(unsigned short x, unsigned short y, unsigned int color)
{
lcd_drawpoint(x, y, color);
if(x < tftlcd_dev.width -1) {
lcd_drawpoint(x + 1, y, color);
}
if(y < tftlcd_dev.height -1) {
lcd_drawpoint(x , y + 1, color);
}
if((x < tftlcd_dev.width -1) && (y < tftlcd_dev.height -1)) {
lcd_drawpoint(x + 1, y + 1, color);
}
}
int main(void)
{
unsigned char i = 0;
unsigned char state = OFF;
unsigned int k;
unsigned char reg_value[4];
imx6ul_hardfpu_enable(); /* 开启硬件浮点运算及ENON */
int_init(); /* 初始化中断 */
imx6u_clkinit(); /* 初始化系统时钟 */
delay_init(); /* 延时初始化 */
uart_init(); /* 初始化串口 */
clk_enable(); /* 使能外设时钟 */
led_init(); /* 初始化LED */
beep_init(); /* 初始化奉命器 */
key_init(); /* 初始化按键 */
lcd_init(); /* 初始化LCD */
gt9271_init(); /* 初始化GT9271 */
tftlcd_dev.forecolor = LCD_RED;
lcd_show_string(50, 10, 400, 24, 24, (char*)"IMX6U-ZERO TOUCH SCREEN TEST");
lcd_show_string(50, 40, 200, 16, 16, (char*)"TOUCH SCREEN TEST");
lcd_show_string(50, 60, 200, 16, 16, (char*)"ATOM@ALIENTEK");
lcd_show_string(50, 80, 200, 16, 16, (char*)"2019/3/27");
tftlcd_dev.forecolor = LCD_BLUE;
while(1)
{
delay_ms(10);
i++;
if(i == 100)
{
i = 0;
state = !state;
reg_value[0]=gt9271_read_byte(GT9271_ADDR, GT9271_GSTID_REG);
printf("%#X\r\n",reg_value[0]);
if(reg_value[0]&0x80)
{
gt9271_write_byte(GT9271_ADDR, GT9271_GSTID_REG, reg_value[0]&0x7F);
}
k=~k;
}
}
return 0;
}
另,左神例程代码(bsp_5426.h)
unsigned char ft5426_write_byte(unsigned char addr, unsigned char reg, unsigned char data);
unsigned char ft5426_read_byte(unsigned char addr, unsigned char reg);
void ft5426_read_len(unsigned char addr, unsigned char reg, unsigned char len, unsigned char *buf);
标红部分应该改为unsigned int。否则无法适配子地址为多个字节的情况(类似gt9271寄存器地址为2byte)
|
|