touch with abtraction working, SD is not working
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
static ft6336u_config_t g_config_storage;
|
||||
static const ft6336u_config_t *g_config = NULL;
|
||||
|
||||
// Helper function to write a register
|
||||
@@ -18,29 +19,48 @@ static bool ft6336u_write_reg(uint8_t reg, uint8_t value) {
|
||||
|
||||
// Helper function to read a register
|
||||
static bool ft6336u_read_reg(uint8_t reg, uint8_t *value) {
|
||||
int result = i2c_write_blocking(g_config->i2c, FT6336U_ADDR, ®, 1, true);
|
||||
if (result != 1) {
|
||||
printf("[FT6336U] I2C write failed: result=%d (expected 1)\n", result);
|
||||
if (result == PICO_ERROR_GENERIC) printf(" Error: PICO_ERROR_GENERIC\n");
|
||||
if (result == PICO_ERROR_TIMEOUT) printf(" Error: PICO_ERROR_TIMEOUT\n");
|
||||
return false;
|
||||
// Retry up to 3 times like Arduino library
|
||||
for (int retry = 0; retry < 3; retry++) {
|
||||
int result = i2c_write_blocking(g_config->i2c, FT6336U_ADDR, ®, 1, true);
|
||||
if (result != 1) {
|
||||
sleep_us(1000); // 1ms delay before retry
|
||||
continue;
|
||||
}
|
||||
|
||||
// Add delay after write, before read (like Arduino library does)
|
||||
sleep_us(10000); // 10ms delay
|
||||
|
||||
result = i2c_read_blocking(g_config->i2c, FT6336U_ADDR, value, 1, false);
|
||||
if (result == 1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
sleep_us(1000); // 1ms delay before retry
|
||||
}
|
||||
|
||||
result = i2c_read_blocking(g_config->i2c, FT6336U_ADDR, value, 1, false);
|
||||
if (result != 1) {
|
||||
printf("[FT6336U] I2C read failed: result=%d (expected 1)\n", result);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Helper function to read multiple registers
|
||||
static bool ft6336u_read_regs(uint8_t reg, uint8_t *buf, size_t len) {
|
||||
int result = i2c_write_blocking(g_config->i2c, FT6336U_ADDR, ®, 1, true);
|
||||
if (result != 1) return false;
|
||||
|
||||
result = i2c_read_blocking(g_config->i2c, FT6336U_ADDR, buf, len, false);
|
||||
return result == (int)len;
|
||||
// Retry up to 3 times
|
||||
for (int retry = 0; retry < 3; retry++) {
|
||||
int result = i2c_write_blocking(g_config->i2c, FT6336U_ADDR, ®, 1, true);
|
||||
if (result != 1) {
|
||||
sleep_us(1000); // 1ms delay before retry
|
||||
continue;
|
||||
}
|
||||
|
||||
// Add delay after write, before read (like Arduino library does)
|
||||
sleep_us(10000); // 10ms delay
|
||||
|
||||
result = i2c_read_blocking(g_config->i2c, FT6336U_ADDR, buf, len, false);
|
||||
if (result == (int)len) {
|
||||
return true;
|
||||
}
|
||||
|
||||
sleep_us(1000); // 1ms delay before retry
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ft6336u_init(const ft6336u_config_t *config) {
|
||||
@@ -53,7 +73,9 @@ bool ft6336u_init(const ft6336u_config_t *config) {
|
||||
printf("[FT6336U] Pins: SDA=%d, SCL=%d, RST=%d, INT=%d\n",
|
||||
config->gpio_sda, config->gpio_scl, config->gpio_rst, config->gpio_int);
|
||||
|
||||
g_config = config;
|
||||
// Copy config to static storage to avoid dangling pointer
|
||||
memcpy(&g_config_storage, config, sizeof(ft6336u_config_t));
|
||||
g_config = &g_config_storage;
|
||||
|
||||
// Initialize I2C
|
||||
printf("[FT6336U] Initializing I2C at 400 kHz...\n");
|
||||
@@ -115,6 +137,17 @@ bool ft6336u_init(const ft6336u_config_t *config) {
|
||||
printf("[FT6336U] WARNING: Failed to set device mode\n");
|
||||
}
|
||||
|
||||
// Enable trigger mode for better gesture and interrupt support
|
||||
printf("[FT6336U] Enabling trigger mode...\n");
|
||||
if (!ft6336u_write_reg(FT6336U_REG_G_MODE, FT6336U_G_MODE_TRIGGER)) {
|
||||
printf("[FT6336U] WARNING: Failed to set G_MODE\n");
|
||||
}
|
||||
|
||||
// Verify gesture mode was set
|
||||
uint8_t g_mode = ft6336u_get_g_mode();
|
||||
printf("[FT6336U] G_MODE: 0x%02X (%s mode)\n",
|
||||
g_mode, g_mode == FT6336U_G_MODE_TRIGGER ? "trigger" : "polling");
|
||||
|
||||
printf("[FT6336U] Initialization complete!\n");
|
||||
return true;
|
||||
}
|
||||
@@ -124,27 +157,13 @@ bool ft6336u_read_touch(ft6336u_touch_data_t *data) {
|
||||
|
||||
memset(data, 0, sizeof(ft6336u_touch_data_t));
|
||||
|
||||
// Small delay to ensure touch controller is ready
|
||||
sleep_us(100);
|
||||
|
||||
// Read gesture ID (optional, skip if causing issues)
|
||||
// Read gesture ID
|
||||
ft6336u_read_reg(FT6336U_REG_GESTURE_ID, &data->gesture);
|
||||
|
||||
// Read number of touch points - retry on failure
|
||||
// Read number of touch points (with retries)
|
||||
uint8_t td_status;
|
||||
int retry_count = 3;
|
||||
bool success = false;
|
||||
|
||||
for (int retry = 0; retry < retry_count; retry++) {
|
||||
if (ft6336u_read_reg(FT6336U_REG_TD_STATUS, &td_status)) {
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
sleep_us(500); // Brief delay before retry
|
||||
}
|
||||
|
||||
if (!success) {
|
||||
return false; // Failed after retries
|
||||
if (!ft6336u_read_reg(FT6336U_REG_TD_STATUS, &td_status)) {
|
||||
return false; // I2C error
|
||||
}
|
||||
|
||||
data->touch_count = td_status & 0x0F;
|
||||
@@ -157,22 +176,12 @@ bool ft6336u_read_touch(ft6336u_touch_data_t *data) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Read touch point data (6 bytes per point)
|
||||
// Read touch point data (6 bytes per point: XH, XL, YH, YL, WEIGHT, MISC)
|
||||
for (int i = 0; i < data->touch_count; i++) {
|
||||
uint8_t reg_base = (i == 0) ? FT6336U_REG_P1_XH : FT6336U_REG_P2_XH;
|
||||
uint8_t buf[6];
|
||||
|
||||
// Retry read if it fails
|
||||
bool read_success = false;
|
||||
for (int retry = 0; retry < 3; retry++) {
|
||||
if (ft6336u_read_regs(reg_base, buf, 6)) {
|
||||
read_success = true;
|
||||
break;
|
||||
}
|
||||
sleep_us(200);
|
||||
}
|
||||
|
||||
if (!read_success) {
|
||||
if (!ft6336u_read_regs(reg_base, buf, 6)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -181,7 +190,8 @@ bool ft6336u_read_touch(ft6336u_touch_data_t *data) {
|
||||
uint16_t raw_x = ((buf[0] & 0x0F) << 8) | buf[1];
|
||||
uint16_t raw_y = ((buf[2] & 0x0F) << 8) | buf[3];
|
||||
data->points[i].id = (buf[2] >> 4) & 0x0F;
|
||||
data->points[i].weight = buf[4];
|
||||
data->points[i].weight = buf[4]; // Touch pressure/area
|
||||
data->points[i].misc = (buf[5] >> 4) & 0x0F; // Touch area (upper nibble)
|
||||
|
||||
// Apply coordinate transformations
|
||||
if (g_config->swap_xy) {
|
||||
@@ -294,3 +304,28 @@ bool ft6336u_test_i2c(void) {
|
||||
|
||||
return overall;
|
||||
}
|
||||
|
||||
bool ft6336u_set_g_mode(uint8_t mode) {
|
||||
if (g_config == NULL) return false;
|
||||
return ft6336u_write_reg(FT6336U_REG_G_MODE, mode);
|
||||
}
|
||||
|
||||
uint8_t ft6336u_get_g_mode(void) {
|
||||
if (g_config == NULL) return 0xFF;
|
||||
|
||||
uint8_t g_mode;
|
||||
if (!ft6336u_read_reg(FT6336U_REG_G_MODE, &g_mode)) {
|
||||
return 0xFF;
|
||||
}
|
||||
return g_mode;
|
||||
}
|
||||
|
||||
uint8_t ft6336u_get_power_mode(void) {
|
||||
if (g_config == NULL) return 0xFF;
|
||||
|
||||
uint8_t pwr_mode;
|
||||
if (!ft6336u_read_reg(FT6336U_REG_POWER_MODE, &pwr_mode)) {
|
||||
return 0xFF;
|
||||
}
|
||||
return pwr_mode;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user