IT7259這個(gè)驅(qū)動(dòng)IC在智能穿戴設(shè)備商用的比較多,它的驅(qū)動(dòng)拿出來(lái)分享一下- #include "it7259.h"
- #include "WM.h"
- #include "mainview.h"
- #include "string.h"
- I2C_HandleTypeDef hi2c3;
- TIM_HandleTypeDef htim6;
- uint16_t XResolution;
- uint16_t YResolution;
- uint8_t Step;
- extern WM_HWIN MainView;
- #define IT7259_ADDR 0x8C
- #define IT7259_ADDR_READ IT7259_ADDR | 1
- #define COMMAND_BUFFER_INDEX 0x20
- #define QUERY_BUFFER_INDEX 0x80
- #define COMMAND_RESPONSE_BUFFER_INDEX 0xA0
- #define POINT_BUFFER_INDEX 0xE0
- #define QUERY_SUCCESS 0x00
- #define QUERY_BUSY 0x01
- #define QUERY_ERROR 0x02
- #define QUERY_POINT 0x80
- #define IIC_SCL GPIO_PIN_6 //PB6
- #define IIC_SDA GPIO_PIN_7 //PB7
- #define RST GPIO_PIN_9 //PB9
- #define EINT GPIO_PIN_8 //PB8
- #define IIC_SCL_PORT GPIOB //
- #define IIC_SDA_PORT GPIOB //
- #define RST_PORT GPIOB //
- #define EINT_PORT GPIOB //
- #define EINT_PORT_IRQ EXTI9_5_IRQn
- #define LOW_PIN(x, y) (x->BSRR = y << 16)
- #define HIGH_PIN(x, y) (x->BSRR = y)
- #define READ_PIN(x, y) ((x->IDR & y) > 0 ? 1 : 0)
- #define SDA_OUTPUT() GPIOB->MODER = (GPIOB->MODER & 0xFFFF3FFF) | 0x00004000; //
- #define SDA_INPUT() GPIOB->MODER = (GPIOB->MODER & 0xFFFF3FFF);
- #define ACK 0
- #define NACK 1
- #define POINT 0x08
- #define GESTURES 0x80
- #define TAP 0x20
- #define FLICK 0x22
- #define DOUBLE_TAP 0x23
- #define UP 0x08
- #define UPPER_RIGHT 0x09
- #define RIGHT 0x0A
- #define LOWER_RIGHT 0x0B
- #define DOWN 0x0C
- #define LOWER_LEFT 0x0D
- #define LEFT 0x0E
- #define UPPER_LEFT 0x0F
- uint8_t pointdata[14]; //報(bào)點(diǎn)XY數(shù)據(jù)
- static void I2CDelay(uint16_t n)
- {
- TIM6->ARR = 0x10000 - n;
- TIM6->CNT = 0x10000 - n;
- __HAL_TIM_CLEAR_FLAG(&htim6, TIM_FLAG_UPDATE);
- TIM6->CR1 |= 1;
- while(__HAL_TIM_GET_FLAG(&htim6, TIM_FLAG_UPDATE) == RESET);
- }
- /*************************************************************
- *函數(shù)名稱:I2CStart
- *函數(shù)功能:I2C開(kāi)始信號(hào)
- *輸入?yún)?shù):
- *輸出參數(shù):
- *備 注:時(shí)鐘線高時(shí),數(shù)據(jù)線由高到低的跳變,表示I2C開(kāi)始信號(hào)
- **************************************************************/
- static void I2CStart( void )
- {
- SDA_OUTPUT();
-
- HIGH_PIN(IIC_SDA_PORT, IIC_SDA);
- //I2CDelay(6);
- HIGH_PIN(IIC_SCL_PORT, IIC_SCL);
- //I2CDelay(6);
- LOW_PIN(IIC_SDA_PORT, IIC_SDA);
- I2CDelay(6);
- LOW_PIN(IIC_SCL_PORT, IIC_SCL);
- I2CDelay(6);
- }
- /*************************************************************
- *函數(shù)名稱:I2CStop
- *函數(shù)功能:I2C停止信號(hào)
- *輸入?yún)?shù):
- *輸出參數(shù):
- *備 注:時(shí)鐘線高時(shí),數(shù)據(jù)線由低到高的跳變,表示I2C停止信號(hào)
- **************************************************************/
- static void I2CStop( void )
- {
- SDA_OUTPUT();
- LOW_PIN(IIC_SDA_PORT, IIC_SDA);
- I2CDelay(6);
- HIGH_PIN(IIC_SCL_PORT, IIC_SCL);
- I2CDelay(6);
- HIGH_PIN(IIC_SDA_PORT, IIC_SDA);
- I2CDelay(6);
- }
- /*************************************************************
- *函數(shù)名稱:I2CWriteByte
- *函數(shù)功能:I2C寫(xiě)一字節(jié)數(shù)據(jù)
- *輸入?yún)?shù):
- *輸出參數(shù):
- *備 注:
- **************************************************************/
- static uint8_t I2CWriteByte(uint8_t byte)
- {
- uint8_t i;
- uint8_t ucErrTime=0;
- SDA_OUTPUT();
- //LOW_PIN(IIC_SCL_PORT, IIC_SCL);
-
- for (i=0; i<8; i++)
- {
- LOW_PIN(IIC_SCL_PORT, IIC_SCL);
- I2CDelay(6);
- if(0x80 & byte)
- HIGH_PIN(IIC_SDA_PORT, IIC_SDA);
- else
- LOW_PIN(IIC_SDA_PORT, IIC_SDA);
- byte <<= 1;
- I2CDelay(6);
- HIGH_PIN(IIC_SCL_PORT, IIC_SCL);
- I2CDelay(6);
-
- }
-
- SDA_INPUT();
- LOW_PIN(IIC_SCL_PORT, IIC_SCL);
- I2CDelay(6);
- while(READ_PIN(IIC_SDA_PORT, IIC_SDA))
- {
- if(ucErrTime++>250)
- {
- I2CStop();
- return NACK;
- }
- }
-
- HIGH_PIN(IIC_SCL_PORT, IIC_SCL);
- I2CDelay(6);
-
-
- return ACK;
- }
- //產(chǎn)生ACK應(yīng)答
- void IIC_Ack(void)
- {
- LOW_PIN(IIC_SCL_PORT, IIC_SCL);
- SDA_OUTPUT();
- LOW_PIN(IIC_SDA_PORT, IIC_SDA);;
- I2CDelay(6);
- HIGH_PIN(IIC_SDA_PORT, IIC_SCL);
- I2CDelay(6);
- LOW_PIN(IIC_SCL_PORT, IIC_SCL);
- }
- //不產(chǎn)生ACK應(yīng)答
- void IIC_NAck(void)
- {
- LOW_PIN(IIC_SCL_PORT, IIC_SCL);
- SDA_OUTPUT();
- HIGH_PIN(IIC_SDA_PORT, IIC_SDA);
- I2CDelay(6);
- HIGH_PIN(IIC_SCL_PORT, IIC_SCL);
- I2CDelay(6);
- LOW_PIN(IIC_SCL_PORT, IIC_SCL);
- }
- /*************************************************************
- *函數(shù)名稱:I2CReadByte
- *函數(shù)功能:I2C讀一字節(jié)數(shù)據(jù)
- *輸入?yún)?shù):
- *輸出參數(shù):
- *備 注:
- **************************************************************/
- static uint8_t I2CReadByte(uint8_t ack)
- {
- int8_t i;
- uint8_t ReadValue = 0;
- SDA_INPUT();
-
- for( i=7; i>=0; i-- )
- {
- LOW_PIN(IIC_SCL_PORT, IIC_SCL);
- I2CDelay(6);
-
- HIGH_PIN(IIC_SCL_PORT, IIC_SCL);
- I2CDelay(6);
-
- ReadValue |= READ_PIN(IIC_SDA_PORT, IIC_SDA) << i;
-
- }
- SDA_OUTPUT();
- if (!ack)
- IIC_NAck();//發(fā)送nACK
- else
- IIC_Ack(); //發(fā)送ACK
-
-
-
- return ReadValue;
- }
- /*************************************************************
- *函數(shù)名稱:MemoryWriteByte
- *函數(shù)功能:E2PROM指定地址寫(xiě)一字節(jié)數(shù)據(jù)
- *輸入?yún)?shù):addr E2PROM地址
- data 寫(xiě)入的數(shù)據(jù)
- *輸出參數(shù):SET: 寫(xiě)入正常;RESET:寫(xiě)入錯(cuò)誤
- *備 注:
- **************************************************************/
- static uint8_t MemoryWriteBuffer(uint8_t addr, uint8_t* pdata, int len)
- {
- I2CStart();
- if(I2CWriteByte(IT7259_ADDR) == NACK)
- {
- I2CStop();
- return 0;
- }
- if(I2CWriteByte(addr) == NACK)
- {
- I2CStop();
- return 0;
- }
-
- while(len--)
- {
- if(I2CWriteByte(*pdata++) == NACK)
- {
- I2CStop();
- return 0;
- }
- }
-
- I2CStop();
- return 1;
- }
- /*************************************************************
- *函數(shù)名稱:E2promReadByte
- *函數(shù)功能:E2PROM指定地址讀一字節(jié)數(shù)據(jù)
- *輸入?yún)?shù):addr E2PROM地址
- *輸出參數(shù):返回讀出的數(shù)據(jù)
- *備 注:
- **************************************************************/
- static uint8_t MemoryReadBuffer(uint8_t addr, uint8_t* pdata, int len)
- {
- I2CStart();
- if(I2CWriteByte(IT7259_ADDR) == NACK)
- {
- I2CStop();
- return 0;
- }
-
- if(I2CWriteByte(addr) == NACK)
- {
- I2CStop();
- return 0;
- }
-
- I2CStart();
-
- if(I2CWriteByte(IT7259_ADDR_READ) == NACK) ///////////???????????
- {
- I2CStop();
- return 0;
- }
-
- while(--len)
- {
- *pdata++ = I2CReadByte(ACK);
- }
-
- *pdata = I2CReadByte(NACK);
-
- I2CStop();
- return 1;
- }
- static void CTP_CheckBusy(void)
- {
- uint8_t query, result;
- uint16_t i=0;
-
- do
- {
- result = MemoryReadBuffer(QUERY_BUFFER_INDEX, &query, 1);
- i++;
- if(i>500)
- return;
-
- }while((query & QUERY_BUSY) || result == 0);
- }
- static uint8_t CTP_IdentifyCapSensor(void)
- {
- uint8_t cmd = 0;
- uint8_t data[10];
-
- CTP_CheckBusy();
- if(!MemoryWriteBuffer(COMMAND_BUFFER_INDEX, &cmd, 1)) return 0;
- CTP_CheckBusy();
- if(!MemoryReadBuffer(COMMAND_RESPONSE_BUFFER_INDEX, data, 10)) return 0;
-
- if(data[1] == 'I' && data[2] == 'T' && data[3] == 'E' && data[4] == '7' && data[5] == '2' && data[6] == '6' && data[7] == '0')
- return 1;
-
- return 0;
-
- }
- static uint8_t CTP_SetInterruptNotification(uint8_t ucStatus, uint8_t ucType)
- {
- uint8_t cmd[4] = {0x02, 0x04, ucStatus, ucType};
- uint8_t data[2];
-
- CTP_CheckBusy();
- if(!MemoryWriteBuffer(COMMAND_BUFFER_INDEX, cmd, 4)) return 0;
- CTP_CheckBusy();
- if(!MemoryReadBuffer(COMMAND_RESPONSE_BUFFER_INDEX, data, 2)) return 0;
-
- if(*(uint16_t *)data == 0)
- return 1;
-
- return 0;
- }
- static uint8_t CTP_GetFirmwareInformation(uint8_t* info)
- {
- uint8_t cmd[2] = {0x01, 0x00};
-
- CTP_CheckBusy();
- if(!MemoryWriteBuffer(COMMAND_BUFFER_INDEX, cmd, 2)) return 0;
- CTP_CheckBusy();
- if(!MemoryReadBuffer(COMMAND_RESPONSE_BUFFER_INDEX, info, 9)) return 0;
-
- if(info[5]== 0 && info[6] == 0 && info[7] == 0 && info[8] == 0)
- return 0;
-
- return 1;
- }
- static uint8_t CTP_Get2DResolutions(uint16_t *pwXResolution, uint16_t *pwYResolution, uint8_t *pucStep)
- {
- uint8_t cmd[2] = {0x01, 0x02};
- uint8_t data[14];
-
- CTP_CheckBusy();
- if(!MemoryWriteBuffer(COMMAND_BUFFER_INDEX, cmd, 2)) return 0;
- CTP_CheckBusy();
- if(!MemoryReadBuffer(COMMAND_RESPONSE_BUFFER_INDEX, data, 14)) return 0;
-
- if(pwXResolution != NULL)
- *pwXResolution = data[2] + (data[3] << 8);
- if(pwYResolution != NULL)
- *pwYResolution = data[4] + (data[5] << 8);
- if(pucStep != NULL)
- *pucStep = data[6];
-
- return 1;
- }
- static void TOUCH_IO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
-
- //__HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- //__HAL_RCC_GPIOC_CLK_ENABLE();
-
- /**I2C1 GPIO Configuration
- PA8 ------> I2C1_SCL
- PC9 ------> I2C1_SDA
- */
- GPIO_InitStruct.Pin = IIC_SCL;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
- HAL_GPIO_Init(IIC_SCL_PORT, &GPIO_InitStruct);
-
- GPIO_InitStruct.Pin = IIC_SDA;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
- HAL_GPIO_Init(IIC_SDA_PORT, &GPIO_InitStruct);
-
- GPIO_InitStruct.Pin = RST;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
- HAL_GPIO_Init(RST_PORT, &GPIO_InitStruct);
-
- GPIO_InitStruct.Pin = EINT;
- GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- HAL_GPIO_Init(EINT_PORT, &GPIO_InitStruct);
-
- HAL_NVIC_SetPriority(EINT_PORT_IRQ, 1, 0);
- }
- /* TIM6 init function */
- static void MX_TIM6_Init(void)
- {
- TIM_MasterConfigTypeDef sMasterConfig;
- __HAL_RCC_TIM6_CLK_ENABLE();
-
- htim6.Instance = TIM6;
- htim6.Init.Prescaler = 84 - 1;
- htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
- HAL_TIM_Base_Init(&htim6);
-
- HAL_TIM_OnePulse_Init(&htim6, TIM_OPMODE_SINGLE);
- sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
- sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig);
- }
- uint8_t firmwareinfo[9];
- void IT7259_Init(void)
- {
- TOUCH_IO_Init();
-
- MX_TIM6_Init();
- HAL_GPIO_WritePin(RST_PORT, RST, GPIO_PIN_RESET);
- HAL_Delay(10);
- HAL_GPIO_WritePin(RST_PORT, RST, GPIO_PIN_SET);
- HAL_Delay(200);
-
- if(!CTP_IdentifyCapSensor()) return; //識(shí)別觸摸屏
-
- if(!CTP_GetFirmwareInformation(firmwareinfo)) return; //檢測(cè)固件狀態(tài)
-
- if(!CTP_Get2DResolutions(&XResolution, &YResolution, &Step)) return; //獲取觸摸屏分辨率
-
- //if(!CTP_SetInterruptNotification(1, 0)) return; //設(shè)置觸摸產(chǎn)生下降沿中斷
-
- HAL_NVIC_EnableIRQ(EINT_PORT_IRQ); //使能PC7中斷
- }
- static uint8_t CTP_CheckNewPoint(void)
- {
- uint8_t query;
-
- if(!MemoryReadBuffer(QUERY_BUFFER_INDEX, &query, 1)) return 0;
- if (query & QUERY_POINT) return 1;
- }
-
- uint8_t Read_X(void);
- uint8_t Read_Y(void);
- void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) //觸摸中斷
- {
-
- uint16_t xraw,yraw;
- if(CTP_CheckNewPoint())
- {
- if(!MemoryReadBuffer(POINT_BUFFER_INDEX, pointdata, 14)) return;
-
- if(pointdata[0] & POINT) //報(bào)點(diǎn)
- {
- // Read_X();
- // Read_Y();
- xraw = ((pointdata[3] & 0x0F) << 8) + pointdata[2];
- yraw = ((pointdata[3] & 0xF0) << 4) + pointdata[4];
- GUI_TOUCH_Exec();
- // memset(pointdata,0,14);
- // GUI_TOUCH_GetState();
- }
-
- if(pointdata[0] & GESTURES) //手勢(shì)
- {
- // if(pointdata[1] == TAP || pointdata[1] == DOUBLE_TAP) //單擊
- // {
- // WM_SendMessageNoPara(MainView, WM_TAP);
- // }
-
- if(pointdata[1] == FLICK) //滑動(dòng)
- {
- switch(pointdata[10])
- {
- case UP: //上
- case UPPER_RIGHT:
- WM_SendMessageNoPara(MainView, WM_SLIDE_UP);
- break;
-
- case RIGHT: //右
- case LOWER_RIGHT:
- WM_SendMessageNoPara(MainView, WM_SLIDE_RIGHT);
- break;
-
- case DOWN: //下
- case LOWER_LEFT:
- WM_SendMessageNoPara(MainView, WM_SLIDE_DOWN);
- break;
-
- case LEFT: //左
- case UPPER_LEFT:
- WM_SendMessageNoPara(MainView, WM_SLIDE_LEFT);
- break;
- }
- }
- }
- }
- }
- uint16_t x,y,temp;
- uint16_t Read_X(void)
- {
-
- x = pointdata[2];
- temp = pointdata[3];
- x |= ((temp & 0x0F)<<8);
- return x;
- }
- uint16_t Read_Y(void)
- {
-
- y = pointdata[4];
- temp = pointdata[3];
- y |= ((temp & 0xF0)<<4);
- return y;
- //memset(pointdata,0,14);
- }
復(fù)制代碼
|