请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
触摸屏的C235739_触控芯片的驱动程序.
/*
* "st1633i" touchscreen driver
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
* All the command sent to st1633i must be 2 byte format(1 byte command and 2 bytes data)
* -------------------------------------------------------------
* S | SlaveAddress R/W A | Command A | Data0 A |Data1 A | P |
* -------------------------------------------------------------
*/
#include "st1633i.h"
#include "string.h"
//#include "fram.h"
//#include "tim.h"
#include "bsp_dwt.h"
//#include "MainTask.h"
//#define EE_I2C_HARD
#ifdef EE_I2C_HARD
#include "i2c.h"
#else
#include "bsp_i2c.h"
#endif
//GUI_PID_STATE State;
struct st1633i_data st1633i;
static uint8_t st_tp_down = 0;
#ifdef EE_I2C_HARD
static int st_i2c_write_bytes(uint8_t *txbuf, uint8_t len)
{
if((len==0) || (txbuf == NULL)) return -1;
if( HAL_I2C_Master_Transmit_DMA(&hi2c2, ST1633I_WR_ADDR, txbuf, len) != HAL_OK)
{
if (HAL_I2C_GetError(&hi2c2) != HAL_I2C_ERROR_AF)
{
Error_Handler();
}
}
while (HAL_I2C_GetState(&hi2c2) != HAL_I2C_STATE_READY){}
return len;
}
#else
static int st_i2c_write_bytes(uint8_t *txbuf, uint8_t len)
{
uint8_t i;
if((len==0)||(txbuf == NULL)) return -1;
i2c_Start();
i2c_SendByte(ST1633I_WR_ADDR);
i2c_WaitAck();
for (i = 0; i < len; i )
{
i2c_SendByte(txbuf[i]);
i2c_WaitAck();
}
i2c_Stop();
bsp_DelayUS(10);
return len;
}
#endif
#ifdef EE_I2C_HARD
static int st_i2c_read_bytes(uint8_t *rxbuf, uint8_t len)
{
if(HAL_I2C_Master_Receive_DMA(&hi2c2, ST1633I_WR_ADDR, rxbuf, len) != HAL_OK)
{
if (HAL_I2C_GetError(&hi2c2) != HAL_I2C_ERROR_AF)
{
Error_Handler();
}
}
while (HAL_I2C_GetState(&hi2c2) != HAL_I2C_STATE_READY){}
return len;
}
#else
static int st_i2c_read_bytes(uint8_t *rxbuf, uint8_t len)
{
__IO int j;
uint8_t i;
i2c_Start();
i2c_SendByte(ST1633I_RD_ADDR);
i2c_WaitAck();
bsp_DelayUS(30);
for (i = 0; i < len; i )
{
rxbuf[i] = i2c_ReadByte();
if(i!= (len-1)) i2c_Ack();
else i2c_NAck();
bsp_DelayUS(10);
}
i2c_Stop();
return len;
}
#endif
static int sitronix_i2c_read_bytes(uint8_t addr, uint8_t *rxbuf, uint8_t len)
{
int ret = 0;
uint8_t txbuf = addr;
if(rxbuf == NULL) return -1;
ret = st_i2c_write_bytes(&txbuf, 1);
ret = st_i2c_read_bytes (rxbuf, len);
return ret;
}
static int sitronix_i2c_write_bytes(uint8_t *txbuf, uint8_t len)
{
int ret = 0;
if(txbuf == NULL) return -1;
ret = st_i2c_write_bytes(txbuf, len);
return ret;
}
// Read 1 byte FW Version from Reg. 0x0 set previously.
static int sitronix_ts_get_fw_revision(uint32_t *rev)
{
uint8_t buf[4];
sitronix_i2c_read_bytes(FIRMWARE_REVISION_3, buf, 4); //0x0C
*rev = (((uint32_t)buf[3]) << 0);
*rev |= (((uint32_t)buf[2]) << 8);
*rev |= (((uint32_t)buf[1]) << 16);
*rev |= (((uint32_t)buf[0]) << 24);
buf[0] = 0x10; //Set Reg. address back to 0x10 for coordinates.
st_i2c_write_bytes(buf, 1);
return 0;
}
static int sitronix_ts_get_CHIP_ID(void)
{
uint8_t buf[4];
sitronix_i2c_read_bytes(CHIP_ID, buf, 3); //0xF4 0x06 0x15 0x0C
st1633i.IdDevice = buf[0];
st1633i.Num_X = buf[1];
st1633i.Num_Y = buf[2];
buf[0] = 0x10; //Set Reg. address back to 0x10 for coordinates.
st_i2c_write_bytes(buf, 1);
return 0;
}
static int sitronix_ts_disable_int(uint8_t value)
{
uint8_t buf[4];
sitronix_i2c_read_bytes(0xF1, buf, 1); //0xF1 0x08
buf[1] = buf[0] & ~(0x01 << 2);
buf[1] |= value; //write disable coord. flag
buf[0] = 0xF1;
st_i2c_write_bytes(buf, 2);
return 0;
}
static int sitronix_get_max_touches(void)
{
uint8_t buffer[1];
sitronix_i2c_read_bytes(MAX_NUM_TOUCHES, buffer, 1); //0x3F
if(buffer[0] > SITRONIX_MAX_SUPPORTED_POINT)
{
buffer[0] = SITRONIX_MAX_SUPPORTED_POINT;
}
return buffer[0];
}
static int sitronix_ts_get_resolution(uint16_t *x_res, uint16_t *y_res)
{
uint8_t buf[3];
// int ret;
sitronix_i2c_read_bytes(XY_RESOLUTION_HIGH, buf, 3); //0x04
*x_res = ((buf[0] & 0xF0) << 4) | buf[1];
*y_res = ((buf[0] & 0x0F) << 8) | buf[2];
buf[0] = 0x10; //Set Reg. address back to 0x10 for coordinates.
st_i2c_write_bytes(buf, 1);
return 0;
}
static int sitronix_set_TimeOut(uint8_t timo)
{
int ret = 0;
uint8_t buffer[4];
buffer[0] = TIMEOUT_TO_IDLE_REG;
buffer[1] = timo;
ret = sitronix_i2c_write_bytes(buffer, 2);
return ret;
}
static int sitronix_ts_get_device_status(uint8_t *err_code, uint8_t *dev_status)
{
int ret = 0;
uint8_t buf[2];
// Set Reg. address to 0x01 for reading Device Status
sitronix_i2c_read_bytes(STATUS_REG, buf, 1);
buf[0] = 0x10; //Set Reg. address back to 0x10 for coordinates.
st_i2c_write_bytes(buf, 1);
return ret;
}
static int sitronix_ts_get_protocol_type(void)
{
int ret = 0;
uint8_t buf[2];
// Set Reg. address to 0x01 for reading Device Status
sitronix_i2c_read_bytes(I2C_PROTOCOL, buf, 1);
buf[0] = 0x10; //Set Reg. address back to 0x10 for coordinates.
st_i2c_write_bytes(buf, 1);
return ret;
}
static int sitronix_ts_get_coordinates(uint8_t *count, uint32_t *x0, uint32_t *y0)
{
static uint8_t buf[10];
stx_report_data_t *pdata;
int ret = 0;
*count = 0; // Set point detected count to 0.
buf[0] = FINGERS;
// ret = st_i2c_write_bytes(buf, 1);
ret = st_i2c_read_bytes(buf, 5);
pdata = (stx_report_data_t *) buf;
if (pdata->xy_data[0].valid)
{
*x0 = pdata->xy_data[0].x_h << 8 | pdata->xy_data[0].x_l;
*y0 = pdata->xy_data[0].y_h << 8 | pdata->xy_data[0].y_l;
(*count) ;
}
else
{
if(st_tp_down == 1)
{
st_tp_down = 0;
st1633i.FingerDetect = 0;
//State.Pressed = 0;
//GUI_PID_StoreState(&State); /* �ͷ� */
}
}
// err:
return ret;
}
/**
* @brief EXTI line detection callbacks
* @param GPIO_Pin: Specifies the pins connected EXTI line
* @retval None
*/
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == GPIO_PIN_13)
{
// if(g_TouchType == CT_ST1633I)
{
ST1633I_Scan();
}
}
}
__IO uint8_t lucas_array[256];
static uint8_t BeepStatus;
void ST1633I_Scan(void)
{
uint8_t bk;
// uint8_t rxbuf[32];
uint16_t x, y;
uint32_t FingerX[FINGERNO],FingerY[FINGERNO];
if(BeepStatus == 1)
{
BeepStatus=0;
//HAL_TIM_PWM_Stop(&htim12, TIM_CHANNEL_1);
//sItem->TimeOutCnt = 0;
}
if(HAL_GPIO_ReadPin(CTP_NINT_GPIO_Port,CTP_NINT_Pin)==GPIO_PIN_SET) return;//PG13
sitronix_ts_get_coordinates(&bk, FingerX, FingerY);
st1633i.FingerX[0]=FingerX[0];
st1633i.FingerY[0]=FingerY[0];
x = st1633i.FingerX[0];
y = st1633i.FingerY[0];
if(x > 799) { return; }
if(y > 499) { return; }
if (st_tp_down == 0)
{
st_tp_down = 1;
//HAL_TIM_PWM_Start(&htim12, TIM_CHANNEL_1);
BeepStatus = 1;
}
st1633i.x = x;
st1633i.y = y;
st1633i.FingerDetect = 1;
// State.x = x;
// State.y = y;
// State.Pressed = 1;
// GUI_PID_StoreState(&State); //�洢PID�ĵ�ǰ״̬
}
int ST1633I_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
// int i;
/* 1. ����CTP_NINTΪ����ģʽ */
HAL_GPIO_WritePin(CTP_NINT_GPIO_Port, CTP_NINT_Pin, GPIO_PIN_SET);
GPIO_InitStruct.Pin = CTP_NINT_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(CTP_NINT_GPIO_Port, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 3, 0);
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
#ifndef EE_I2C_HARD
BSP_I2C2_Init();
#endif
/* 2. ��λCTP_NRST */
HAL_GPIO_WritePin(CTP_NRST_GPIO_Port, CTP_NRST_Pin, GPIO_PIN_SET);
bsp_DelayMS(10);
HAL_GPIO_WritePin(CTP_NRST_GPIO_Port, CTP_NRST_Pin, GPIO_PIN_RESET);
bsp_DelayMS(1);
HAL_GPIO_WritePin(CTP_NRST_GPIO_Port, CTP_NRST_Pin, GPIO_PIN_SET);
bsp_DelayMS(50);
st1633i.FingerNo = FINGERNO;
bsp_DelayMS(1);
sitronix_get_max_touches(); //0x3F
bsp_DelayUS(50);
sitronix_ts_get_protocol_type(); //0x3E
bsp_DelayUS(50);
sitronix_ts_get_CHIP_ID(); //0xF4
bsp_DelayUS(50);
sitronix_ts_disable_int(0x01); //0xF1
bsp_DelayUS(50);
sitronix_set_TimeOut(0x08);
bsp_DelayUS(50);
sitronix_ts_get_device_status(&st1633i.err_code, &st1633i.dev_status);
bsp_DelayUS(50);
sitronix_ts_get_resolution((uint16_t *)&st1633i.x_res, (uint16_t *)&st1633i.y_res);
bsp_DelayUS(50);
sitronix_i2c_read_bytes(0x00, (uint8_t *)&lucas_array[0], 9);
bsp_DelayUS(50);
sitronix_ts_get_fw_revision((uint32_t *)&st1633i.IdVersion);
bsp_DelayUS(50);
return 0;
}
//===============================**End**===========================