找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4611|回复: 9
打印 上一主题 下一主题
收起左侧

STM32F407驱动IT7259的驱动代码

  [复制链接]
跳转到指定楼层
楼主
ID:84421 发表于 2019-12-24 16:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
IT7259这个驱动IC在智能穿戴设备商用的比较多,它的驱动拿出来分享一下
  1. #include "it7259.h"
  2. #include "WM.h"
  3. #include "mainview.h"
  4. #include "string.h"

  5. I2C_HandleTypeDef hi2c3;
  6. TIM_HandleTypeDef htim6;

  7. uint16_t         XResolution;
  8. uint16_t         YResolution;
  9. uint8_t         Step;
  10. extern WM_HWIN MainView;

  11. #define IT7259_ADDR        0x8C
  12. #define IT7259_ADDR_READ         IT7259_ADDR | 1

  13. #define COMMAND_BUFFER_INDEX                                   0x20
  14. #define QUERY_BUFFER_INDEX                                                0x80
  15. #define COMMAND_RESPONSE_BUFFER_INDEX 0xA0
  16. #define POINT_BUFFER_INDEX                                    0xE0
  17. #define QUERY_SUCCESS                                                     0x00
  18. #define QUERY_BUSY                                                                     0x01
  19. #define QUERY_ERROR                                                             0x02
  20. #define QUERY_POINT                                                             0x80

  21. #define IIC_SCL GPIO_PIN_6     //PB6
  22. #define IIC_SDA GPIO_PIN_7     //PB7
  23. #define RST         GPIO_PIN_9       //PB9
  24. #define EINT         GPIO_PIN_8       //PB8

  25. #define IIC_SCL_PORT GPIOB     //
  26. #define IIC_SDA_PORT GPIOB     //

  27. #define RST_PORT GPIOB         //
  28. #define EINT_PORT GPIOB        //

  29. #define EINT_PORT_IRQ EXTI9_5_IRQn

  30. #define LOW_PIN(x, y) (x->BSRR = y << 16)
  31. #define HIGH_PIN(x, y) (x->BSRR = y)
  32. #define READ_PIN(x, y) ((x->IDR & y) > 0 ? 1 : 0)

  33. #define SDA_OUTPUT()         GPIOB->MODER = (GPIOB->MODER & 0xFFFF3FFF) | 0x00004000; //
  34. #define SDA_INPUT()         GPIOB->MODER = (GPIOB->MODER & 0xFFFF3FFF);

  35. #define ACK                0
  36. #define NACK         1

  37. #define POINT                 0x08
  38. #define GESTURES        0x80

  39. #define TAP                                 0x20
  40. #define FLICK                         0x22
  41. #define DOUBLE_TAP         0x23

  42. #define UP                                        0x08
  43. #define UPPER_RIGHT 0x09
  44. #define RIGHT                         0x0A
  45. #define LOWER_RIGHT 0x0B
  46. #define DOWN                                 0x0C
  47. #define LOWER_LEFT         0x0D
  48. #define LEFT                                 0x0E
  49. #define UPPER_LEFT         0x0F

  50. uint8_t pointdata[14];  //报点XY数据


  51. static void I2CDelay(uint16_t n)
  52. {
  53.         TIM6->ARR = 0x10000 - n;
  54.         TIM6->CNT = 0x10000 - n;
  55.         __HAL_TIM_CLEAR_FLAG(&htim6, TIM_FLAG_UPDATE);
  56.         TIM6->CR1 |= 1;
  57.         while(__HAL_TIM_GET_FLAG(&htim6, TIM_FLAG_UPDATE) == RESET);
  58. }

  59. /*************************************************************
  60. *函数名称:I2CStart
  61. *函数功能:I2C开始信号
  62. *输入参数:
  63. *输出参数:
  64. *备           注:时钟线高时,数据线由高到低的跳变,表示I2C开始信号
  65. **************************************************************/
  66. static void I2CStart( void )
  67. {
  68.         SDA_OUTPUT();
  69.        
  70.   HIGH_PIN(IIC_SDA_PORT, IIC_SDA);
  71.         //I2CDelay(6);
  72.   HIGH_PIN(IIC_SCL_PORT, IIC_SCL);
  73.   //I2CDelay(6);
  74.   LOW_PIN(IIC_SDA_PORT, IIC_SDA);
  75.   I2CDelay(6);
  76.   LOW_PIN(IIC_SCL_PORT, IIC_SCL);
  77.         I2CDelay(6);
  78. }

  79. /*************************************************************
  80. *函数名称:I2CStop
  81. *函数功能:I2C停止信号
  82. *输入参数:
  83. *输出参数:
  84. *备           注:时钟线高时,数据线由低到高的跳变,表示I2C停止信号
  85. **************************************************************/
  86. static void I2CStop( void )
  87. {
  88.         SDA_OUTPUT();       

  89.   LOW_PIN(IIC_SDA_PORT, IIC_SDA);
  90.         I2CDelay(6);
  91.         HIGH_PIN(IIC_SCL_PORT, IIC_SCL);
  92.   I2CDelay(6);
  93.   HIGH_PIN(IIC_SDA_PORT, IIC_SDA);
  94.   I2CDelay(6);
  95. }



  96. /*************************************************************
  97. *函数名称:I2CWriteByte
  98. *函数功能:I2C写一字节数据
  99. *输入参数:
  100. *输出参数:
  101. *备           注:
  102. **************************************************************/
  103. static uint8_t I2CWriteByte(uint8_t byte)
  104. {
  105.   uint8_t i;
  106.         uint8_t ucErrTime=0;

  107.         SDA_OUTPUT();
  108.         //LOW_PIN(IIC_SCL_PORT, IIC_SCL);
  109.        
  110.   for (i=0; i<8; i++)
  111.   {
  112.           LOW_PIN(IIC_SCL_PORT, IIC_SCL);
  113.                 I2CDelay(6);
  114.     if(0x80 & byte)
  115.       HIGH_PIN(IIC_SDA_PORT, IIC_SDA);
  116.     else
  117.       LOW_PIN(IIC_SDA_PORT, IIC_SDA);
  118.     byte <<= 1;
  119.                 I2CDelay(6);
  120.     HIGH_PIN(IIC_SCL_PORT, IIC_SCL);
  121.                 I2CDelay(6);
  122.    
  123.   }
  124.        
  125.         SDA_INPUT();
  126.         LOW_PIN(IIC_SCL_PORT, IIC_SCL);
  127.         I2CDelay(6);       
  128.         while(READ_PIN(IIC_SDA_PORT, IIC_SDA))
  129.         {
  130.                 if(ucErrTime++>250)
  131.                 {
  132.                         I2CStop();
  133.                         return NACK;
  134.                 }
  135.         }
  136.        
  137.         HIGH_PIN(IIC_SCL_PORT, IIC_SCL);
  138.         I2CDelay(6);
  139.   
  140.        
  141.         return ACK;
  142. }
  143. //产生ACK应答
  144. void IIC_Ack(void)
  145. {
  146.         LOW_PIN(IIC_SCL_PORT, IIC_SCL);
  147.         SDA_OUTPUT();
  148.         LOW_PIN(IIC_SDA_PORT, IIC_SDA);;
  149.         I2CDelay(6);
  150.         HIGH_PIN(IIC_SDA_PORT, IIC_SCL);
  151.         I2CDelay(6);
  152.         LOW_PIN(IIC_SCL_PORT, IIC_SCL);
  153. }
  154. //不产生ACK应答                    
  155. void IIC_NAck(void)
  156. {
  157.         LOW_PIN(IIC_SCL_PORT, IIC_SCL);
  158.         SDA_OUTPUT();
  159.          HIGH_PIN(IIC_SDA_PORT, IIC_SDA);
  160.         I2CDelay(6);
  161.          HIGH_PIN(IIC_SCL_PORT, IIC_SCL);
  162.         I2CDelay(6);
  163.         LOW_PIN(IIC_SCL_PORT, IIC_SCL);
  164. }                                                        
  165. /*************************************************************
  166. *函数名称:I2CReadByte
  167. *函数功能:I2C读一字节数据
  168. *输入参数:
  169. *输出参数:
  170. *备           注:
  171. **************************************************************/
  172. static uint8_t I2CReadByte(uint8_t ack)
  173. {
  174.   int8_t i;
  175.   uint8_t ReadValue = 0;

  176.         SDA_INPUT();

  177.        
  178.   for( i=7; i>=0; i-- )
  179.   {
  180.           LOW_PIN(IIC_SCL_PORT, IIC_SCL);
  181.     I2CDelay(6);
  182.    
  183.                 HIGH_PIN(IIC_SCL_PORT, IIC_SCL);
  184.     I2CDelay(6);
  185.    
  186.     ReadValue |= READ_PIN(IIC_SDA_PORT, IIC_SDA) << i;
  187.                
  188.   }

  189.         SDA_OUTPUT();
  190.          if (!ack)
  191.         IIC_NAck();//发送nACK
  192.     else
  193.         IIC_Ack(); //发送ACK   
  194.                
  195.                
  196.        
  197.   return ReadValue;
  198. }

  199. /*************************************************************
  200. *函数名称:MemoryWriteByte
  201. *函数功能:E2PROM指定地址写一字节数据
  202. *输入参数:addr  E2PROM地址
  203.                              data  写入的数据
  204. *输出参数:SET: 写入正常;RESET:写入错误
  205. *备           注:
  206. **************************************************************/
  207. static uint8_t MemoryWriteBuffer(uint8_t addr, uint8_t* pdata, int len)
  208. {

  209.   I2CStart();

  210.   if(I2CWriteByte(IT7259_ADDR) == NACK)
  211.         {
  212.                         I2CStop();
  213.                         return 0;
  214.         }


  215.   if(I2CWriteByte(addr) == NACK)
  216.         {
  217.                         I2CStop();
  218.                         return 0;
  219.         }

  220.        
  221.         while(len--)
  222.         {
  223.                 if(I2CWriteByte(*pdata++) == NACK)
  224.                 {
  225.                         I2CStop();
  226.                         return 0;
  227.                 }
  228.         }       
  229.        
  230.   I2CStop();
  231.   return 1;
  232. }

  233. /*************************************************************
  234. *函数名称:E2promReadByte
  235. *函数功能:E2PROM指定地址读一字节数据
  236. *输入参数:addr  E2PROM地址
  237. *输出参数:返回读出的数据
  238. *备           注:
  239. **************************************************************/  
  240. static uint8_t MemoryReadBuffer(uint8_t addr, uint8_t* pdata, int len)
  241. {
  242.   I2CStart();

  243.   if(I2CWriteByte(IT7259_ADDR) == NACK)
  244.         {
  245.                 I2CStop();
  246.                 return 0;
  247.         }
  248.        
  249.         if(I2CWriteByte(addr) == NACK)
  250.         {
  251.                 I2CStop();
  252.                 return 0;
  253.         }
  254.                
  255.         I2CStart();
  256.                                        
  257.         if(I2CWriteByte(IT7259_ADDR_READ) == NACK)  ///////////???????????
  258.         {
  259.                 I2CStop();
  260.                 return 0;
  261.         }
  262.                                                        
  263.         while(--len)
  264.         {
  265.                 *pdata++ = I2CReadByte(ACK);
  266.         }
  267.                                                        
  268.         *pdata = I2CReadByte(NACK);
  269.        
  270.         I2CStop();
  271.         return 1;
  272. }

  273. static void CTP_CheckBusy(void)
  274. {
  275.         uint8_t query, result;
  276.         uint16_t i=0;
  277.        
  278.         do
  279.         {
  280.                 result = MemoryReadBuffer(QUERY_BUFFER_INDEX, &query, 1);
  281.                 i++;
  282.                 if(i>500)
  283.                         return;
  284.                        
  285.         }while((query & QUERY_BUSY) || result == 0);
  286. }


  287. static uint8_t CTP_IdentifyCapSensor(void)
  288. {
  289.         uint8_t cmd = 0;
  290.         uint8_t data[10];
  291.        
  292.         CTP_CheckBusy();
  293.         if(!MemoryWriteBuffer(COMMAND_BUFFER_INDEX, &cmd, 1)) return 0;

  294.         CTP_CheckBusy();
  295.         if(!MemoryReadBuffer(COMMAND_RESPONSE_BUFFER_INDEX, data, 10)) return 0;
  296.        
  297.         if(data[1] == 'I' && data[2] == 'T' && data[3] == 'E' && data[4] == '7' && data[5] == '2'  && data[6] == '6'  && data[7] == '0')
  298.                 return 1;
  299.        
  300.         return 0;
  301.        
  302. }

  303. static uint8_t CTP_SetInterruptNotification(uint8_t ucStatus, uint8_t ucType)
  304. {
  305.         uint8_t cmd[4] = {0x02, 0x04, ucStatus, ucType};
  306.         uint8_t data[2];
  307.        
  308.         CTP_CheckBusy();
  309.         if(!MemoryWriteBuffer(COMMAND_BUFFER_INDEX, cmd, 4)) return 0;

  310.         CTP_CheckBusy();
  311.         if(!MemoryReadBuffer(COMMAND_RESPONSE_BUFFER_INDEX, data, 2)) return 0;
  312.        
  313.         if(*(uint16_t *)data == 0)
  314.                 return 1;
  315.        
  316.         return 0;
  317. }

  318. static uint8_t CTP_GetFirmwareInformation(uint8_t* info)
  319. {
  320.         uint8_t cmd[2] = {0x01, 0x00};
  321.        
  322.         CTP_CheckBusy();
  323.         if(!MemoryWriteBuffer(COMMAND_BUFFER_INDEX, cmd, 2)) return 0;

  324.         CTP_CheckBusy();
  325.         if(!MemoryReadBuffer(COMMAND_RESPONSE_BUFFER_INDEX, info, 9)) return 0;
  326.        
  327.         if(info[5]== 0  && info[6] == 0  && info[7] == 0  && info[8] == 0)
  328.                         return 0;
  329.        
  330.         return 1;

  331. }


  332. static uint8_t CTP_Get2DResolutions(uint16_t *pwXResolution, uint16_t *pwYResolution, uint8_t *pucStep)
  333. {
  334.         uint8_t cmd[2] = {0x01, 0x02};
  335.         uint8_t data[14];
  336.        
  337.         CTP_CheckBusy();
  338.         if(!MemoryWriteBuffer(COMMAND_BUFFER_INDEX, cmd, 2)) return 0;

  339.         CTP_CheckBusy();
  340.         if(!MemoryReadBuffer(COMMAND_RESPONSE_BUFFER_INDEX, data, 14)) return 0;
  341.        
  342.         if(pwXResolution != NULL)
  343.                 *pwXResolution = data[2] + (data[3] << 8);

  344.         if(pwYResolution != NULL)
  345.                 *pwYResolution = data[4] + (data[5] << 8);

  346.         if(pucStep != NULL)
  347.                 *pucStep = data[6];
  348.        
  349.         return 1;
  350. }       

  351. static void TOUCH_IO_Init(void)
  352. {
  353.     GPIO_InitTypeDef GPIO_InitStruct;
  354.        
  355.           //__HAL_RCC_GPIOA_CLK_ENABLE();
  356.                 __HAL_RCC_GPIOB_CLK_ENABLE();
  357.           //__HAL_RCC_GPIOC_CLK_ENABLE();
  358.        
  359.     /**I2C1 GPIO Configuration   
  360.     PA8     ------> I2C1_SCL
  361.     PC9     ------> I2C1_SDA
  362.     */
  363.     GPIO_InitStruct.Pin = IIC_SCL;
  364.     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  365.     GPIO_InitStruct.Pull = GPIO_PULLUP;
  366.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  367.     HAL_GPIO_Init(IIC_SCL_PORT, &GPIO_InitStruct);
  368.        
  369.     GPIO_InitStruct.Pin = IIC_SDA;
  370.     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  371.     GPIO_InitStruct.Pull = GPIO_PULLUP;
  372.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  373.     HAL_GPIO_Init(IIC_SDA_PORT, &GPIO_InitStruct);
  374.                
  375.                 GPIO_InitStruct.Pin = RST;
  376.     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  377.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  378.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  379.     HAL_GPIO_Init(RST_PORT, &GPIO_InitStruct);
  380.                
  381.                 GPIO_InitStruct.Pin = EINT;
  382.     GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  383.     GPIO_InitStruct.Pull = GPIO_PULLUP;
  384.     HAL_GPIO_Init(EINT_PORT, &GPIO_InitStruct);
  385.                
  386.                 HAL_NVIC_SetPriority(EINT_PORT_IRQ, 1, 0);
  387. }

  388. /* TIM6 init function */
  389. static void MX_TIM6_Init(void)
  390. {

  391.   TIM_MasterConfigTypeDef sMasterConfig;

  392.         __HAL_RCC_TIM6_CLK_ENABLE();
  393.        
  394.   htim6.Instance = TIM6;
  395.   htim6.Init.Prescaler = 84 - 1;
  396.   htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
  397.   HAL_TIM_Base_Init(&htim6);
  398.        
  399.         HAL_TIM_OnePulse_Init(&htim6, TIM_OPMODE_SINGLE);

  400.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  401.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  402.   HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig);

  403. }

  404. uint8_t firmwareinfo[9];
  405. void IT7259_Init(void)
  406. {
  407.         TOUCH_IO_Init();
  408.        
  409.         MX_TIM6_Init();
  410.         HAL_GPIO_WritePin(RST_PORT, RST, GPIO_PIN_RESET);
  411.         HAL_Delay(10);
  412.         HAL_GPIO_WritePin(RST_PORT, RST, GPIO_PIN_SET);
  413.         HAL_Delay(200);       
  414.        

  415.         if(!CTP_IdentifyCapSensor()) return;        //识别触摸屏
  416.        
  417.         if(!CTP_GetFirmwareInformation(firmwareinfo)) return; //检测固件状态
  418.        
  419.         if(!CTP_Get2DResolutions(&XResolution, &YResolution, &Step)) return;        //获取触摸屏分辨率
  420.        
  421.         //if(!CTP_SetInterruptNotification(1, 0)) return; //设置触摸产生下降沿中断
  422.        
  423.         HAL_NVIC_EnableIRQ(EINT_PORT_IRQ);        //使能PC7中断
  424. }

  425. static uint8_t CTP_CheckNewPoint(void)
  426. {
  427.         uint8_t query;
  428.        
  429.         if(!MemoryReadBuffer(QUERY_BUFFER_INDEX, &query, 1)) return 0;

  430.                 if (query & QUERY_POINT)  return 1;
  431. }
  432.        
  433. uint8_t Read_X(void);
  434. uint8_t Read_Y(void);
  435. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) //触摸中断
  436. {
  437.        
  438. uint16_t xraw,yraw;
  439.         if(CTP_CheckNewPoint())
  440.         {
  441.                 if(!MemoryReadBuffer(POINT_BUFFER_INDEX, pointdata, 14)) return;
  442.                
  443.                 if(pointdata[0] & POINT)                                                                                                                                        //报点
  444.                 {
  445.                 //                Read_X();
  446.                 //          Read_Y();
  447.                   xraw = ((pointdata[3] & 0x0F) << 8) + pointdata[2];
  448.                         yraw = ((pointdata[3] & 0xF0) << 4) + pointdata[4];
  449.                                 GUI_TOUCH_Exec();
  450.                 //                memset(pointdata,0,14);
  451.                 //        GUI_TOUCH_GetState();
  452.                 }
  453.                
  454.                 if(pointdata[0] & GESTURES)                                                                                                                                        //手势
  455.                 {
  456. //                        if(pointdata[1] == TAP ||  pointdata[1] == DOUBLE_TAP)                //单击
  457. //                        {
  458. //                                WM_SendMessageNoPara(MainView, WM_TAP);
  459. //                        }
  460.                        
  461.                         if(pointdata[1] == FLICK)                                                                                                                                        //滑动
  462.                         {
  463.                                 switch(pointdata[10])
  464.                                 {
  465.                                         case UP:                                                                                                                                                                                        //上
  466.                                         case UPPER_RIGHT:
  467.                                                 WM_SendMessageNoPara(MainView, WM_SLIDE_UP);
  468.                                                 break;
  469.                                        
  470.                                         case RIGHT:                                                                                                                                                                                //右
  471.                                         case LOWER_RIGHT:
  472.                                                 WM_SendMessageNoPara(MainView, WM_SLIDE_RIGHT);
  473.                                                 break;
  474.                                        
  475.                                         case DOWN:                                                                                                                                                                                //下
  476.                                         case LOWER_LEFT:
  477.                                                 WM_SendMessageNoPara(MainView, WM_SLIDE_DOWN);
  478.                                                 break;
  479.                                        
  480.                                         case LEFT:                                                                                                                                                                                //左
  481.                                         case UPPER_LEFT:
  482.                                                 WM_SendMessageNoPara(MainView, WM_SLIDE_LEFT);
  483.                                                 break;
  484.                                 }
  485.                         }
  486.                 }
  487.         }
  488. }

  489. uint16_t x,y,temp;
  490. uint16_t Read_X(void)
  491. {
  492.        
  493.         x = pointdata[2];
  494.         temp = pointdata[3];
  495.         x |= ((temp & 0x0F)<<8);
  496.         return x;
  497. }
  498. uint16_t Read_Y(void)
  499. {
  500.        
  501.         y = pointdata[4];
  502.         temp = pointdata[3];
  503.         y |= ((temp & 0xF0)<<4);
  504.         return y;
  505.         //memset(pointdata,0,14);
  506. }
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:703123 发表于 2020-4-19 14:38 | 只看该作者
整体的源码 能发我吗?
回复

使用道具 举报

板凳
ID:84421 发表于 2020-4-27 14:29 | 只看该作者
zxh51 发表于 2020-4-19 14:38
整体的源码 能发我吗?

留下你的邮箱地址
回复

使用道具 举报

地板
ID:384489 发表于 2020-7-6 11:23 | 只看该作者
我也正在调这个,可以分享一下吗?
回复

使用道具 举报

5#
ID:813945 发表于 2020-8-13 18:19 | 只看该作者
lfgxp 发表于 2020-4-27 14:29
留下你的邮箱地址

Hello! Can I get the overall source code too?
Here's my email :
Thank you in advance!
回复

使用道具 举报

6#
ID:150905 发表于 2020-12-13 23:03 | 只看该作者
我也正在调这个,可以分享一下吗?

回复

使用道具 举报

7#
ID:923134 发表于 2021-5-18 10:23 | 只看该作者
能把这个tp的整套驱动发大家一下吗,我也在调试,谢谢!
回复

使用道具 举报

8#
ID:79064 发表于 2021-5-19 13:01 | 只看该作者
还能发吗  我也想要一份  给我发一份可以吗
回复

使用道具 举报

9#
ID:1076578 发表于 2023-5-10 15:10 | 只看该作者
Could you share the full source code?
回复

使用道具 举报

10#
ID:287366 发表于 2023-7-19 08:29 | 只看该作者
大师  资料给我发一份吧,最近在调这个触摸芯片。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表