Skip to content

Commit

Permalink
Fixed CSTXXX Chip dected function
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisxhe committed Sep 8, 2023
1 parent cc80f3a commit 5d6d48f
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 48 deletions.
5 changes: 3 additions & 2 deletions src/REG/CSTxxxConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@
#define CSTXXX_REG_CHIP_TYPE_L (0xAA)
#define CSTXXX_REG_CHIP_TYPE_H (0xAB)

#define CST816T_MODEL_ID (0x22)
#define CST226SE_MODEL_ID (0x08)
#define CST816T_MODEL_ID (0x5356)
#define CST226SE_MODEL_ID (0xA8)

#define CST226SE_BUFFER_NUM (28)



Expand Down
179 changes: 137 additions & 42 deletions src/TouchDrvCSTXXX.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,45 @@ class TouchDrvCSTXXX :

uint8_t getPoint(int16_t *x_array, int16_t *y_array, uint8_t get_point = 1)
{
uint8_t point = 0;
if (!x_array && !y_array || !get_point) {
return 0;
}
switch (__model) {
case CST816T_MODEL_ID:
return __getCST816T(x_array, y_array, get_point);
return updateCST816T(x_array, y_array, get_point);
case CST226SE_MODEL_ID:
return __getCST226SE(x_array, y_array, get_point);
point = updateCST226SE();
if (point) {
for (int i = 0; i < get_point; i++) {
x_array[i] = report.x[i];
y_array[i] = report.y[i];
}
}
break;
default:
return 0;
}
return point;
}

const TouchData getPoint()
{
uint8_t point = 0;
TouchData data;
switch (__model) {
case CST816T_MODEL_ID:
// return updateCST816T(x_array, y_array, get_point);
//TODO:
return TouchData();
case CST226SE_MODEL_ID:
point = updateCST226SE();
if (point) {
return report;
}
default:
break;
}
}

bool isPressed()
Expand Down Expand Up @@ -162,10 +193,12 @@ class TouchDrvCSTXXX :
//2uA
void sleep()
{
writeRegister(0xD1, 0x05);
}

void wakeup()
{

}

void idle()
Expand All @@ -180,25 +213,21 @@ class TouchDrvCSTXXX :

uint8_t getSupportTouchPoint()
{
return 1;
return __maxPoint;
}

bool getResolution(int16_t *x, int16_t *y)
{
return false;
*x = resX;
*y = resY;
return true;
}

uint8_t getGesture()
{
return 0;
}

uint8_t getMaxDetectedPoint()
{
return __maxPoint;
}


void setHomeButtonCallback(home_button_callback_t cb, void *user_data = NULL)
{
this->__homeButtonCb = cb;
Expand All @@ -207,23 +236,19 @@ class TouchDrvCSTXXX :

private:

uint8_t __getCST226SE(int16_t *x_array, int16_t *y_array, uint8_t get_point)
uint8_t updateCST226SE()
{
uint8_t buffer[26];
uint8_t buffer[CST226SE_BUFFER_NUM];
uint8_t index = 0;

if (!x_array && !y_array || !get_point) {
return 0;
}

if (readRegister(CSTXXX_REG_STATUS, buffer, 26) == DEV_WIRE_ERR) {
if (readRegister(CSTXXX_REG_STATUS, buffer, CST226SE_BUFFER_NUM) == DEV_WIRE_ERR) {
return 0;
}

#ifdef LOG_PORT
LOG_PORT.print("RAW:");
for (int i = 0; i < 26; ++i) {
LOG_PORT.print(buffer[i], HEX); LOG_PORT.print(",");
for (int i = 0; i < CST226SE_BUFFER_NUM; ++i) {
LOG_PORT.printf("%02X,", buffer[i]);
}
LOG_PORT.println();
#endif
Expand All @@ -232,36 +257,39 @@ class TouchDrvCSTXXX :
if (__homeButtonCb) {
__homeButtonCb(this->user_data);
}
return 1;
return 0;
}

if (buffer[6] != 0xAB)return 0;
if (buffer[0] == 0xAB)return 0;
if (buffer[5] == 0x80) {
return 0;
}
if (buffer[5] == 0x80)return 0;

uint8_t point = buffer[5] & 0x7F;
if (point > 5 || !point) {
writeRegister(0x00, 0xAB);
return 0;
}

for (int i = 0; i < point; i++) {
x_array[i] = (int16_t)((buffer[index + 1] << 4) | ((buffer[index + 3] >> 4) & 0x0F));
y_array[i] = (int16_t)((buffer[index + 2] << 4) | (buffer[index + 3] & 0x0F));
index = (i == 0) ? (index + 2) : (index + 5);
report.id[i] = buffer[index] >> 4;
report.status[i] = buffer[index] & 0x0F;
report.x[i] = (uint16_t)((buffer[index + 1] << 4) | ((buffer[index + 3] >> 4) & 0x0F));
report.y[i] = (uint16_t)((buffer[index + 2] << 4) | (buffer[index + 3] & 0x0F));
report.pressure[i] = buffer[index + 4];
index = (i == 0) ? (index + 7) : (index + 5);
}

#ifdef LOG_PORT
for (int i = 0; i < point; i++) {
LOG_PORT.printf("[%d] --> X:%d Y:%d \n", i, x_array[i], y_array[i]);
for (int i = 0; i < 5; i++) {
LOG_PORT.printf("[%d]-X:%u Y:%u P:%u sta:%u ", report.id[i], report.x[i], report.y[i], report.pressure[i], report.status[i]);
}
LOG_PORT.println();
#endif
return 0;
return point;
}


uint8_t __getCST816T(int16_t *x_array, int16_t *y_array, uint8_t get_point)
uint8_t updateCST816T(int16_t *x_array, int16_t *y_array, uint8_t get_point)
{
uint8_t buffer[13];
if (readRegister(CSTXXX_REG_STATUS, buffer, 13) == DEV_WIRE_ERR) {
Expand Down Expand Up @@ -301,8 +329,10 @@ class TouchDrvCSTXXX :

bool initImpl()
{
uint8_t buffer[8];

setReadRegisterSendStop(false);
setRegAddressLenght(1);
setRegAddressLenght(2);

if (__irq != SENSOR_PIN_NONE) {
pinMode(__irq, INPUT);
Expand All @@ -314,26 +344,89 @@ class TouchDrvCSTXXX :

reset();

//Model ID Register : 0xA8
//CST816T : 0X22 , One point
//CST226SE : 0X08 , five point
__model = readRegister(CSTXXX_REG_MODEL_ID);
// Enter Command mode
writeRegister(0xD1, 0x01);
delay(10);
readRegister(0xD1FC, buffer, 4);
uint32_t checkcode = 0;
checkcode = buffer[3];
checkcode <<= 8;
checkcode |= buffer[2];
checkcode <<= 8;
checkcode |= buffer[1];
checkcode <<= 8;
checkcode |= buffer[0];

log_i("Chip checkcode:0x%x.\r\n", checkcode);

readRegister(0xD1F8, buffer, 4);
resX = ( buffer[1] << 8) | buffer[0];
resY = ( buffer[3] << 8) | buffer[2];
log_i("Chip resolution X:%u Y:%u\r\n", resX, resY);

readRegister(0xD204, buffer, 4);
uint32_t chipType = buffer[3];
chipType <<= 8;
chipType |= buffer[2];


uint32_t ProjectID = buffer[1];
ProjectID <<= 8;
ProjectID |= buffer[0];

log_i("Chip type :0x%X, ProjectID:0X%x\r\n",
chipType, ProjectID);


readRegister(0xD208, buffer, 8);

uint32_t fwVersion = buffer[3];
fwVersion <<= 8;
fwVersion |= buffer[2];
fwVersion <<= 8;
fwVersion |= buffer[1];
fwVersion <<= 8;
fwVersion |= buffer[0];

uint32_t checksum = buffer[7];
checksum <<= 8;
checksum |= buffer[6];
checksum <<= 8;
checksum |= buffer[5];
checksum <<= 8;
checksum |= buffer[4];


log_i("Chip ic version:0x%X, checksum:0x%X\n",
fwVersion, checksum);

if (fwVersion == 0xA5A5A5A5) {
log_i(" Chip ic don't have firmware. \n");
return false;
}
if ((checkcode & 0xffff0000) != 0xCACA0000) {
log_i("firmware info read error .\n");
return false;
}

// Exit Command mode
writeRegister(0xD1, 0x09);

__model = chipType;
switch (__model) {
case CST816T_MODEL_ID:
Serial.println("Find CST816T");
log_i("Find CST816T");
__maxPoint = 1;
break;
case CST226SE_MODEL_ID:
Serial.println("Find CST226SE");
log_i("Find CST226SE");
__maxPoint = 5;
break;
default:
Serial.print("Find device ID :"); Serial.println(__model);
__maxPoint = 1;
break;
default: return false;
}

setRegAddressLenght(1);

return probe();
}

Expand All @@ -346,10 +439,12 @@ class TouchDrvCSTXXX :
protected:
int __rst = SENSOR_PIN_NONE;
int __irq = SENSOR_PIN_NONE;
uint8_t __model = 0xFF;
uint32_t __model = 0xFF;
uint8_t __maxPoint = 1;
home_button_callback_t __homeButtonCb = NULL;
void *user_data = NULL;
TouchData report;
uint16_t resX, resY;
};


Expand Down
20 changes: 16 additions & 4 deletions src/TouchDrvInterface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,31 @@ typedef void (*home_button_callback_t)(void *user_data);
class TouchData
{
public:
TouchData() {}
~TouchData() {}
uint8_t available;
uint8_t id[5];
uint16_t x[5];
uint16_t y[5];
uint8_t status[5];
uint8_t pressure[5];

uint16_t getX(uint8_t index = 0)
{
return 0;
return x[index];
}

uint16_t getY(uint8_t index = 0)
{
return 0;
return y[index];
}
uint16_t getPressure(uint8_t index = 0)
{
return pressure[index];
}
uint16_t getStatus(uint8_t index = 0)
{
return status[index];
}

};


Expand Down

0 comments on commit 5d6d48f

Please sign in to comment.