在好例子网,分享、交流、成长!
您当前所在位置:首页C/C++ 开发实例常规C/C++编程 → CTP触摸屏驱动_C235739_触控芯片

CTP触摸屏驱动_C235739_触控芯片

常规C/C++编程

下载此实例
  • 开发语言:C/C++
  • 实例大小:44.11M
  • 下载次数:31
  • 浏览次数:455
  • 发布时间:2020-12-05
  • 实例类别:常规C/C++编程
  • 发 布 人:小米奇爸比
  • 文件格式:.zip
  • 所需积分:0
 相关标签: CTP 触摸屏 触摸 TP 驱动

实例介绍

【实例简介】

触摸屏的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**===========================



实例下载地址

CTP触摸屏驱动_C235739_触控芯片

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警