From 13dc49ac603c56f6666ea43ac91e9d4ce94963d2 Mon Sep 17 00:00:00 2001
From: Sebastian Krzyszkowiak <dos@dosowisko.net>
Date: Fri, 2 Feb 2024 14:31:13 +0100
Subject: [PATCH] phhw

---
 components/flow3r_bsp/flow3r_bsp.h            |  2 +-
 components/flow3r_bsp/flow3r_bsp_audio.c      | 38 ++++++------
 components/flow3r_bsp/flow3r_bsp_captouch.c   | 50 ++++++++++++++-
 components/flow3r_bsp/flow3r_bsp_display.c    | 12 ++--
 components/flow3r_bsp/flow3r_bsp_gc9a01.c     |  2 +-
 components/flow3r_bsp/flow3r_bsp_i2c.c        |  9 ++-
 components/flow3r_bsp/flow3r_bsp_imu.c        | 15 +++++
 components/flow3r_bsp/flow3r_bsp_rmtled.c     | 12 ++--
 components/flow3r_bsp/flow3r_bsp_spio.c       | 62 ++++++++++++++-----
 .../micropython/include/mpconfigboard.h       |  4 +-
 .../vendor/ports/esp32/machine_pin.c          | 22 +++----
 components/st3m/st3m_audio.c                  | 12 ++--
 components/st3m/st3m_captouch.c               |  6 +-
 components/st3m/st3m_fs_sd.c                  |  6 +-
 components/st3m/st3m_io.c                     |  2 +-
 15 files changed, 178 insertions(+), 76 deletions(-)

diff --git a/components/flow3r_bsp/flow3r_bsp.h b/components/flow3r_bsp/flow3r_bsp.h
index 100dc7b0ef..a0c730f1f0 100644
--- a/components/flow3r_bsp/flow3r_bsp.h
+++ b/components/flow3r_bsp/flow3r_bsp.h
@@ -131,7 +131,7 @@ esp_err_t flow3r_bsp_audio_write(const void *src, size_t size,
 // Write audio codec register. Obviously very unsafe. Have fun.
 void flow3r_bsp_audio_register_poke(uint8_t reg, uint8_t data);
 
-#define FLOW3R_BSP_LED_COUNT 40
+#define FLOW3R_BSP_LED_COUNT 6
 
 // Initialize LEDs.
 esp_err_t flow3r_bsp_leds_init(void);
diff --git a/components/flow3r_bsp/flow3r_bsp_audio.c b/components/flow3r_bsp/flow3r_bsp_audio.c
index 2157ddc804..03e316bab7 100644
--- a/components/flow3r_bsp/flow3r_bsp_audio.c
+++ b/components/flow3r_bsp/flow3r_bsp_audio.c
@@ -14,10 +14,10 @@ esp_err_t flow3r_bsp_audio_write(const void *src, size_t size,
 #include "flow3r_bsp_max98091.h"
 
 void flow3r_bsp_audio_init(void) {
-    flow3r_bsp_max98091_init();
-    vTaskDelay(100 / portTICK_PERIOD_MS);  // dunno if necessary
+    //flow3r_bsp_max98091_init();
+    //vTaskDelay(100 / portTICK_PERIOD_MS);  // dunno if necessary
     static const i2s_config_t i2s_config = {
-        .mode = I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_RX,
+        .mode = I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_PDM,
         .sample_rate = FLOW3R_BSP_AUDIO_SAMPLE_RATE,
         .bits_per_sample = 16,
         .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
@@ -28,54 +28,56 @@ void flow3r_bsp_audio_init(void) {
         .use_apll = false
     };
     static const i2s_pin_config_t pin_config = {
-        .bck_io_num = 10,
-        .mck_io_num = 18,
-        .ws_io_num = 11,
-        .data_out_num = 12,
-        .data_in_num = 13,
+        .bck_io_num = I2S_PIN_NO_CHANGE,
+        .mck_io_num = I2S_PIN_NO_CHANGE,
+        .ws_io_num = 45,
+        .data_out_num = 46,
+        .data_in_num = I2S_PIN_NO_CHANGE,
     };
     ESP_ERROR_CHECK(i2s_driver_install(0, &i2s_config, 0, NULL));
     i2s_set_pin(0, &pin_config);
 }
 float flow3r_bsp_audio_headphones_set_volume(bool mute, float dB) {
-    return flow3r_bsp_max98091_headphones_set_volume(mute, dB);
+    return dB; //flow3r_bsp_max98091_headphones_set_volume(mute, dB);
 }
 
 float flow3r_bsp_audio_speaker_set_volume(bool mute, float dB) {
-    return flow3r_bsp_max98091_speaker_set_volume(mute, dB);
+    return dB; //flow3r_bsp_max98091_speaker_set_volume(mute, dB);
 }
 
 int8_t flow3r_bsp_audio_headset_set_gain_dB(int8_t gain_dB) {
-    return flow3r_bsp_max98091_headset_set_gain_dB(gain_dB);
+    return gain_dB; //flow3r_bsp_max98091_headset_set_gain_dB(gain_dB);
 }
 
 void flow3r_bsp_audio_read_jacksense(flow3r_bsp_audio_jacksense_state_t *st) {
-    flow3r_bsp_max98091_read_jacksense(st);
+    //flow3r_bsp_max98091_read_jacksense(st);
 }
 
 void flow3r_bsp_audio_input_set_source(flow3r_bsp_audio_input_source_t source) {
-    flow3r_bsp_max98091_input_set_source(source);
+    //flow3r_bsp_max98091_input_set_source(source);
 }
 
 void flow3r_bsp_audio_headphones_line_in_set_hardware_thru(bool enable) {
-    flow3r_bsp_max98091_headphones_line_in_set_hardware_thru(enable);
+    //flow3r_bsp_max98091_headphones_line_in_set_hardware_thru(enable);
 }
 
 void flow3r_bsp_audio_speaker_line_in_set_hardware_thru(bool enable) {
-    flow3r_bsp_max98091_speaker_line_in_set_hardware_thru(enable);
+    //flow3r_bsp_max98091_speaker_line_in_set_hardware_thru(enable);
 }
 
 void flow3r_bsp_audio_line_in_set_hardware_thru(bool enable) {
-    flow3r_bsp_max98091_line_in_set_hardware_thru(enable);
+    //flow3r_bsp_max98091_line_in_set_hardware_thru(enable);
 }
 
 bool flow3r_bsp_audio_has_hardware_mute(void) { return true; }
 
 void flow3r_bsp_audio_register_poke(uint8_t reg, uint8_t data) {
-    flow3r_bsp_max98091_register_poke(reg, data);
+    //flow3r_bsp_max98091_register_poke(reg, data);
 }
 
 esp_err_t flow3r_bsp_audio_read(void *dest, size_t size, size_t *bytes_read,
                                 TickType_t ticks_to_wait) {
-    return i2s_read(0, dest, size, bytes_read, ticks_to_wait);
+    *bytes_read = size;
+    return ESP_OK;
+    //return i2s_read(0, dest, size, bytes_read, ticks_to_wait);
 }
diff --git a/components/flow3r_bsp/flow3r_bsp_captouch.c b/components/flow3r_bsp/flow3r_bsp_captouch.c
index e74379b232..99f59d04e5 100644
--- a/components/flow3r_bsp/flow3r_bsp_captouch.c
+++ b/components/flow3r_bsp/flow3r_bsp_captouch.c
@@ -7,6 +7,7 @@
 #include "freertos/task.h"
 
 #include "driver/gpio.h"
+#include "driver/touch_sensor.h"
 
 #include "esp_log.h"
 
@@ -213,6 +214,7 @@ static void _task(void *data) {
     bool bot_ok = true;
     esp_err_t ret;
     for (;;) {
+#if 0
 #if defined(CONFIG_FLOW3R_HW_GEN_P4)
         bool top = true, bot = true;
         vTaskDelay(10 / portTICK_PERIOD_MS);
@@ -258,6 +260,41 @@ static void _task(void *data) {
             }
         }
 
+#endif
+    vTaskDelay(10 / portTICK_PERIOD_MS);
+
+    for (size_t i = 0; i < sizeof(_map_top) / sizeof(_map_top[0]); i++) {
+        flow3r_bsp_captouch_petal_state_t *petal =
+            &_state.petals[_map_top[i].petal_number];
+        flow3r_bsp_captouch_petal_pad_state_t *pad =
+            flow3r_bsp_captouch_pad_for_petal(petal, _map_top[i].pad_kind);
+        pad->raw = 0;
+        int num = _map_top[i].petal_number;
+        if (num % 2 == 0) {
+            uint32_t val;
+            touch_pad_read_raw_data(num / 2 + 10, &val);
+            pad->raw = val / 4;
+            if (val >= 0xffff * 4) pad->raw = 0xffff;
+        }
+    }
+
+    for (size_t i = 0; i < sizeof(_map_bot) / sizeof(_map_bot[0]); i++) {
+        flow3r_bsp_captouch_petal_state_t *petal =
+            &_state.petals[_map_bot[i].petal_number];
+        flow3r_bsp_captouch_petal_pad_state_t *pad =
+            flow3r_bsp_captouch_pad_for_petal(petal, _map_bot[i].pad_kind);
+        pad->raw = 0;
+        int num = _map_bot[i].petal_number;
+        if (num % 2 == 0) {
+            uint32_t val;
+            touch_pad_read_raw_data(num / 2 + 10, &val);
+            pad->raw = val / 4;
+            if (val >= 0xffff * 4) pad->raw = 0xffff;
+        }
+    }
+
+
+
         _callback(&_state);
     }
 }
@@ -281,6 +318,7 @@ esp_err_t _gpio_interrupt_setup(gpio_num_t num, gpio_isr_t isr) {
 }
 
 esp_err_t flow3r_bsp_captouch_init(flow3r_bsp_captouch_callback_t callback) {
+
     assert(_callback == NULL);
     assert(callback != NULL);
     _callback = callback;
@@ -290,6 +328,10 @@ esp_err_t flow3r_bsp_captouch_init(flow3r_bsp_captouch_callback_t callback) {
 
     esp_err_t ret;
 
+    for (int i=10; i<=14; i++) {
+        touch_pad_config(i);
+    }
+
     for (size_t i = 0; i < 10; i++) {
         bool top = (i % 2) == 0;
         _state.petals[i].kind = top ? petal_top : petal_bottom;
@@ -308,6 +350,7 @@ esp_err_t flow3r_bsp_captouch_init(flow3r_bsp_captouch_callback_t callback) {
     _bot.callback = _on_chip_data;
     _bot.user = &_bot;
 
+/*
     if ((ret = flow3r_bsp_ad7147_chip_init(
              &_top, flow3r_i2c_addresses.touch_top)) != ESP_OK) {
         return ret;
@@ -316,6 +359,7 @@ esp_err_t flow3r_bsp_captouch_init(flow3r_bsp_captouch_callback_t callback) {
              &_bot, flow3r_i2c_addresses.touch_bottom)) != ESP_OK) {
         return ret;
     }
+*/
     ESP_LOGI(TAG, "Captouch initialized");
 
     if ((ret = gpio_install_isr_service(ESP_INTR_FLAG_SHARED |
@@ -323,6 +367,7 @@ esp_err_t flow3r_bsp_captouch_init(flow3r_bsp_captouch_callback_t callback) {
         ESP_LOGE(TAG, "Failed to install GPIO ISR service");
         return ret;
     }
+/*
     if ((ret = _gpio_interrupt_setup(_interrupt_gpio_bot, _bot_isr)) !=
         ESP_OK) {
         ESP_LOGE(TAG, "Failed to add bottom captouch ISR");
@@ -337,9 +382,9 @@ esp_err_t flow3r_bsp_captouch_init(flow3r_bsp_captouch_callback_t callback) {
             return ret;
         }
     }
-
+*/
     xTaskCreate(&_task, "captouch", 4096, NULL, configMAX_PRIORITIES - 1, NULL);
-    _kickstart();
+    //_kickstart();
     return ESP_OK;
 }
 
@@ -375,6 +420,7 @@ flow3r_bsp_captouch_petal_pad_state_t *flow3r_bsp_captouch_pad_for_petal(
 }
 
 void flow3r_bsp_captouch_calibrate() {
+    return;
     _bot.calibration_pending = true;
     _top.calibration_pending = true;
 }
diff --git a/components/flow3r_bsp/flow3r_bsp_display.c b/components/flow3r_bsp/flow3r_bsp_display.c
index 9c78e1663c..0cc91ccff7 100644
--- a/components/flow3r_bsp/flow3r_bsp_display.c
+++ b/components/flow3r_bsp/flow3r_bsp_display.c
@@ -13,13 +13,13 @@ flow3r_bsp_gc9a01_config_t gc9a01_config = {
     .reset_used = 0,
     .backlight_used = 1,
 
-    .pin_sck = 41,
-    .pin_mosi = 42,
-    .pin_cs = 40,
-    .pin_dc = 38,
-    .pin_backlight = 46,
+    .pin_sck = 39,
+    .pin_mosi = 40,
+    .pin_cs = 42,
+    .pin_dc = 41,
+    .pin_backlight = 47,
 
-    .host = 2,
+    .host = SPI2_HOST,
 };
 
 static flow3r_bsp_gc9a01_t gc9a01;
diff --git a/components/flow3r_bsp/flow3r_bsp_gc9a01.c b/components/flow3r_bsp/flow3r_bsp_gc9a01.c
index 3616391283..0f2d1231a9 100644
--- a/components/flow3r_bsp/flow3r_bsp_gc9a01.c
+++ b/components/flow3r_bsp/flow3r_bsp_gc9a01.c
@@ -526,7 +526,7 @@ esp_err_t flow3r_bsp_gc9a01_init(flow3r_bsp_gc9a01_t *gc9a01,
         ix++;
     }
 
-    ret = flow3r_bsp_gc9a01_mem_access_mode_set(gc9a01, 3, 0, 0, 1);
+    ret = flow3r_bsp_gc9a01_mem_access_mode_set(gc9a01, 0, 0, 0, 1);
     if (ret != ESP_OK) {
         goto cleanup_spi_device;
     }
diff --git a/components/flow3r_bsp/flow3r_bsp_i2c.c b/components/flow3r_bsp/flow3r_bsp_i2c.c
index 350d725d70..6af9a9cf30 100644
--- a/components/flow3r_bsp/flow3r_bsp_i2c.c
+++ b/components/flow3r_bsp/flow3r_bsp_i2c.c
@@ -46,6 +46,8 @@ static i2c_config_t i2c_conf = {
 };
 
 void flow3r_bsp_i2c_init(void) {
+    return;
+
     if (mutex != NULL) {
         return;
     }
@@ -62,14 +64,15 @@ void flow3r_bsp_i2c_init(void) {
 }
 
 // Take I2C bus lock.
-static void flow3r_bsp_i2c_get(void) { xSemaphoreTake(mutex, portMAX_DELAY); }
+static void flow3r_bsp_i2c_get(void) { return; xSemaphoreTake(mutex, portMAX_DELAY); }
 
 // Release I2C bus lock.
-static void flow3r_bsp_i2c_put(void) { xSemaphoreGive(mutex); }
+static void flow3r_bsp_i2c_put(void) { return; xSemaphoreGive(mutex); }
 
 esp_err_t flow3r_bsp_i2c_write_to_device(uint8_t address, const uint8_t *buffer,
                                          size_t write_size,
                                          TickType_t ticks_to_wait) {
+    return ESP_OK;
     flow3r_bsp_i2c_get();
     esp_err_t res = i2c_master_write_to_device(I2C_NUM_0, address, buffer,
                                                write_size, ticks_to_wait);
@@ -80,6 +83,7 @@ esp_err_t flow3r_bsp_i2c_write_to_device(uint8_t address, const uint8_t *buffer,
 esp_err_t flow3r_bsp_i2c_write_read_device(
     uint8_t address, const uint8_t *write_buffer, size_t write_size,
     uint8_t *read_buffer, size_t read_size, TickType_t ticks_to_wait) {
+    return ESP_OK;
     flow3r_bsp_i2c_get();
     esp_err_t res = i2c_master_write_read_device(
         I2C_NUM_0, address, write_buffer, write_size, read_buffer, read_size,
@@ -89,6 +93,7 @@ esp_err_t flow3r_bsp_i2c_write_read_device(
 }
 
 void flow3r_bsp_i2c_scan(flow3r_bsp_i2c_scan_result_t *res) {
+    return;
     if (res != NULL) {
         memset(res, 0, sizeof(flow3r_bsp_i2c_scan_result_t));
     }
diff --git a/components/flow3r_bsp/flow3r_bsp_imu.c b/components/flow3r_bsp/flow3r_bsp_imu.c
index 684fc1688b..1c8311b12f 100644
--- a/components/flow3r_bsp/flow3r_bsp_imu.c
+++ b/components/flow3r_bsp/flow3r_bsp_imu.c
@@ -34,6 +34,7 @@ static struct bmi2_sens_data _bmi_sens_data;
 
 static BMI2_INTF_RETURN_TYPE bmi2_i2c_read(uint8_t reg_addr, uint8_t *reg_data,
                                            uint32_t len, void *intf_ptr) {
+    return BMI2_OK;
     flow3r_bsp_imu_t *imu = (flow3r_bsp_imu_t *)intf_ptr;
 
     uint8_t tx[] = { reg_addr };
@@ -55,6 +56,7 @@ static BMI2_INTF_RETURN_TYPE bmi2_i2c_read(uint8_t reg_addr, uint8_t *reg_data,
 static BMI2_INTF_RETURN_TYPE bmi2_i2c_write(uint8_t reg_addr,
                                             const uint8_t *reg_data,
                                             uint32_t len, void *intf_ptr) {
+    return BMI2_OK;
     flow3r_bsp_imu_t *imu = (flow3r_bsp_imu_t *)intf_ptr;
 
     uint8_t tx[len + 1];
@@ -77,6 +79,7 @@ static BMI2_INTF_RETURN_TYPE bmi2_i2c_write(uint8_t reg_addr,
 
 static BMP5_INTF_RET_TYPE bmp5_i2c_read(uint8_t reg_addr, uint8_t *reg_data,
                                         uint32_t length, void *intf_ptr) {
+    return BMP5_OK;
     flow3r_bsp_imu_t *imu = (flow3r_bsp_imu_t *)intf_ptr;
 
     ESP_LOGD(TAG, "bmp read register %02x (%" PRIu32 " bytes) to %p", reg_addr,
@@ -106,6 +109,7 @@ static BMP5_INTF_RET_TYPE bmp5_i2c_read(uint8_t reg_addr, uint8_t *reg_data,
 static BMP5_INTF_RET_TYPE bmp5_i2c_write(uint8_t reg_addr,
                                          const uint8_t *reg_data,
                                          uint32_t length, void *intf_ptr) {
+    return BMP5_OK;
     flow3r_bsp_imu_t *imu = (flow3r_bsp_imu_t *)intf_ptr;
 
     ESP_LOGD(TAG, "bmp write to %02X (%" PRIu32 " bytes) from %p", reg_addr,
@@ -120,6 +124,7 @@ static BMP5_INTF_RET_TYPE bmp5_i2c_write(uint8_t reg_addr,
 }
 
 static void bmi2_delay_us(uint32_t period, void *intf_ptr) {
+    return;
     int period_ms = (period + 999) / 1000;
     int port_ms = period_ms + portTICK_PERIOD_MS - 1;
     int port_ticks = port_ms / portTICK_PERIOD_MS;
@@ -130,6 +135,7 @@ static void bmi2_delay_us(uint32_t period, void *intf_ptr) {
 }
 
 esp_err_t flow3r_bsp_imu_init(flow3r_bsp_imu_t *imu) {
+    return ESP_OK;
     memset(imu, 0, sizeof(*imu));
 
     imu->bmi_dev_addr = BMI2_I2C_PRIM_ADDR;
@@ -224,6 +230,7 @@ esp_err_t flow3r_bsp_imu_init(flow3r_bsp_imu_t *imu) {
 }
 
 esp_err_t flow3r_bsp_imu_update(flow3r_bsp_imu_t *imu) {
+    return ESP_OK;
     struct bmi2_sens_data sens_data = {
         0,
     };
@@ -238,6 +245,7 @@ esp_err_t flow3r_bsp_imu_update(flow3r_bsp_imu_t *imu) {
 
 esp_err_t flow3r_bsp_imu_read_acc(flow3r_bsp_imu_t *imu, int *x, int *y,
                                   int *z) {
+    return ESP_OK;
     if (_bmi_sens_data.status & BMI2_DRDY_ACC) {
         *x = _bmi_sens_data.acc.x;
         *y = _bmi_sens_data.acc.y;
@@ -249,6 +257,7 @@ esp_err_t flow3r_bsp_imu_read_acc(flow3r_bsp_imu_t *imu, int *x, int *y,
 
 esp_err_t flow3r_bsp_imu_read_acc_mps(flow3r_bsp_imu_t *imu, float *x, float *y,
                                       float *z) {
+    return ESP_OK;
     int ix, iy, iz;
 
     esp_err_t res = flow3r_bsp_imu_read_acc(imu, &ix, &iy, &iz);
@@ -264,6 +273,7 @@ esp_err_t flow3r_bsp_imu_read_acc_mps(flow3r_bsp_imu_t *imu, float *x, float *y,
 
 esp_err_t flow3r_bsp_imu_read_gyro(flow3r_bsp_imu_t *imu, int *x, int *y,
                                    int *z) {
+    return ESP_OK;
     if (_bmi_sens_data.status & BMI2_DRDY_GYR) {
         *x = _bmi_sens_data.gyr.x;
         *y = _bmi_sens_data.gyr.y;
@@ -275,6 +285,7 @@ esp_err_t flow3r_bsp_imu_read_gyro(flow3r_bsp_imu_t *imu, int *x, int *y,
 
 esp_err_t flow3r_bsp_imu_read_gyro_dps(flow3r_bsp_imu_t *imu, float *x,
                                        float *y, float *z) {
+    return ESP_OK;
     int ix, iy, iz;
 
     esp_err_t res = flow3r_bsp_imu_read_gyro(imu, &ix, &iy, &iz);
@@ -290,6 +301,7 @@ esp_err_t flow3r_bsp_imu_read_gyro_dps(flow3r_bsp_imu_t *imu, float *x,
 
 esp_err_t flow3r_bsp_imu_read_pressure(flow3r_bsp_imu_t *imu, float *pressure,
                                        float *temperature) {
+    return ESP_OK;
     int8_t rslt = 0;
     struct bmp5_sensor_data sensor_data;
 
@@ -614,6 +626,7 @@ static void bmp5_error_codes_print_result(const char api_name[], int8_t rslt) {
 }
 
 static int8_t set_accel_config(flow3r_bsp_imu_t *imu) {
+    return BMI2_OK;
     int8_t rslt;
     struct bmi2_sens_config config;
 
@@ -670,6 +683,7 @@ static int8_t set_accel_config(flow3r_bsp_imu_t *imu) {
 }
 
 static int8_t set_gyro_config(flow3r_bsp_imu_t *imu) {
+    return BMI2_OK;
     int8_t rslt;
     struct bmi2_sens_config config;
 
@@ -722,6 +736,7 @@ static int8_t set_gyro_config(flow3r_bsp_imu_t *imu) {
 
 static int8_t set_bmp_config(
     struct bmp5_osr_odr_press_config *osr_odr_press_cfg, struct bmp5_dev *dev) {
+    return BMP5_OK;
     int8_t rslt;
     struct bmp5_iir_config set_iir_cfg;
     struct bmp5_int_source_select int_source_select;
diff --git a/components/flow3r_bsp/flow3r_bsp_rmtled.c b/components/flow3r_bsp/flow3r_bsp_rmtled.c
index ffb96dcbd0..f4eaebbcba 100644
--- a/components/flow3r_bsp/flow3r_bsp_rmtled.c
+++ b/components/flow3r_bsp/flow3r_bsp_rmtled.c
@@ -23,10 +23,10 @@ static const char *TAG = "flow3r-rmtled";
 #include "driver/rmt.h"
 #include "esp_log.h"
 
-#define WS2812_T0H_NS (350)
-#define WS2812_T0L_NS (1000)
-#define WS2812_T1H_NS (1000)
-#define WS2812_T1L_NS (350)
+#define WS2812_T0H_NS (400)
+#define WS2812_T0L_NS (850)
+#define WS2812_T1H_NS (800)
+#define WS2812_T1L_NS (450)
 #define WS2812_RESET_US (280)
 
 typedef struct {
@@ -85,7 +85,7 @@ esp_err_t flow3r_bsp_rmtled_init(uint16_t num_leds) {
         return ESP_ERR_INVALID_STATE;
     }
 
-    rmt_config_t config = RMT_DEFAULT_CONFIG_TX(14, 0);
+    rmt_config_t config = RMT_DEFAULT_CONFIG_TX(48, 0);
     // set counter clock to 40MHz
     config.clk_div = 2;
 
@@ -164,4 +164,4 @@ esp_err_t flow3r_bsp_rmtled_refresh(int32_t timeout_ms) {
         rmt_write_sample(rmtled.chan, rmtled.buffer, rmtled.num_leds * 3, true);
     rmtled.transmitting = true;
     return ret;
-}
\ No newline at end of file
+}
diff --git a/components/flow3r_bsp/flow3r_bsp_spio.c b/components/flow3r_bsp/flow3r_bsp_spio.c
index 553a81eee9..8456112c30 100644
--- a/components/flow3r_bsp/flow3r_bsp_spio.c
+++ b/components/flow3r_bsp/flow3r_bsp_spio.c
@@ -3,6 +3,7 @@
 #include <stdbool.h>
 
 #include "driver/gpio.h"
+#include "driver/touch_sensor.h"
 #include "esp_log.h"
 
 static const char *TAG = "flow3r-spio";
@@ -11,6 +12,7 @@ typedef enum {
     flow3r_bsp_iochip_esp32 = 0,
     flow3r_bsp_iochip_portexp = 1,
     flow3r_bsp_iochip_dummy = 2,
+    flow3r_bsp_iochip_touch = 3,
 } flow3r_bsp_iochip_t;
 
 typedef struct {
@@ -36,6 +38,10 @@ typedef struct {
         .chip = flow3r_bsp_iochip_portexp, .pin = pinno + pexno * 8, \
         .output = true, __VA_ARGS__                                  \
     }
+#define ITOUCH(pinno, ...)                                           \
+    {                                                                \
+        .chip = flow3r_bsp_iochip_touch, .pin = pinno, __VA_ARGS__   \
+    }
 #define IODUMMY \
     { .chip = flow3r_bsp_iochip_dummy }
 
@@ -100,34 +106,34 @@ const flow3r_bsp_spio_programmable_pins_t flow3r_bsp_spio_programmable_pins = {
 static const flow3r_bsp_iodef_t iodef = {
     .tripos_left =
         {
-            .left = IPEX(7, 1, .invert = true),
-            .mid = IESP(0, false, .invert = true),
-            .right = IPEX(0, 1, .invert = true),
+            .left = ITOUCH(3), //IESP(3, true, .invert = true),
+            .mid = ITOUCH(4), //IESP(0, false, .invert = true),
+            .right = ITOUCH(5), //IESP(5, true, .invert = true),
         },
     .tripos_right =
         {
-            .left = IPEX(4, 1, .invert = true),
-            .mid = IESP(3, true, .invert = true),
-            .right = IPEX(5, 1, .invert = true),
+            .left = ITOUCH(6), //IESP(6, true, .invert = true),
+            .mid = ITOUCH(7),
+            .right = ITOUCH(8), //IESP(8, true, .invert = true),
         },
     .trrs_left =
         {
-            .tip_badgelink_enable = OPEX(6, 0),
-            .ring_badgelink_enable = OPEX(5, 0),
+            .tip_badgelink_enable = IODUMMY,
+            .ring_badgelink_enable = IODUMMY,
         },
     .trrs_right =
         {
-            .tip_badgelink_enable = OPEX(4, 0),
-            .ring_badgelink_enable = OPEX(3, 0),
+            .tip_badgelink_enable = IODUMMY,
+            .ring_badgelink_enable = IODUMMY,
         },
-    .charger_state = IPEX(2, 1),
-    .jacksense_right = IPEX(6, 1, .invert = true),
+    .charger_state = IODUMMY,
+    .jacksense_right = IODUMMY,
 };
 const flow3r_bsp_spio_programmable_pins_t flow3r_bsp_spio_programmable_pins = {
-    .badgelink_left_tip = 7,
-    .badgelink_left_ring = 6,
+    .badgelink_left_tip = 4,
+    .badgelink_left_ring = 4,
     .badgelink_right_tip = 4,
-    .badgelink_right_ring = 5,
+    .badgelink_right_ring = 4,
 };
 #define PORTEXP_MAX7321S
 #else
@@ -207,6 +213,13 @@ static esp_err_t _iopin_esp32_init(const flow3r_bsp_iopin_t *iopin) {
     return gpio_config(&cfg);
 }
 
+static esp_err_t _iopin_touch_init(const flow3r_bsp_iopin_t *iopin) {
+    if (iopin->output) {
+        return ESP_ERR_NOT_SUPPORTED;
+    }
+    return touch_pad_config(iopin->pin);
+}
+
 static esp_err_t _iopin_init(const flow3r_bsp_iopin_t *iopin) {
     switch (iopin->chip) {
         case flow3r_bsp_iochip_esp32:
@@ -216,6 +229,8 @@ static esp_err_t _iopin_init(const flow3r_bsp_iopin_t *iopin) {
             return ESP_OK;
         case flow3r_bsp_iochip_dummy:
             return ESP_OK;
+        case flow3r_bsp_iochip_touch:
+            return _iopin_touch_init(iopin);
     }
     return ESP_ERR_NOT_SUPPORTED;
 }
@@ -224,6 +239,13 @@ static bool _iopin_esp32_get_pin(const flow3r_bsp_iopin_t *iopin) {
     return gpio_get_level(iopin->pin);
 }
 
+static bool _iopin_touch_get_pin(const flow3r_bsp_iopin_t *iopin) {
+    uint32_t val;
+    touch_pad_read_raw_data(iopin->pin, &val);
+    //ESP_LOGE(TAG, "status %lu pin %lu raw %lu", touch_pad_get_status(), iopin->pin, val);
+    return val >= 50000; //touch_pad_get_status() & (1 << iopin->pin);
+}
+
 static bool _iopin_get_pin(const flow3r_bsp_iopin_t *iopin) {
     bool res = false;
     switch (iopin->chip) {
@@ -233,6 +255,9 @@ static bool _iopin_get_pin(const flow3r_bsp_iopin_t *iopin) {
         case flow3r_bsp_iochip_portexp:
             res = _iopin_portexp_get_pin(iopin);
             break;
+        case flow3r_bsp_iochip_touch:
+            res = _iopin_touch_get_pin(iopin);
+            break;
         case flow3r_bsp_iochip_dummy:
             res = false;
     }
@@ -256,6 +281,8 @@ static void _iopin_set_pin(const flow3r_bsp_iopin_t *iopin, bool on) {
             break;
         case flow3r_bsp_iochip_dummy:
             break;
+        default:
+            break;
     }
 }
 
@@ -265,6 +292,8 @@ static void _iopin_set_pin(const flow3r_bsp_iopin_t *iopin, bool on) {
         return ret;                 \
     }
 esp_err_t flow3r_bsp_spio_init(void) {
+    touch_pad_init();
+
     esp_err_t ret = _portexp_init();
     if (ret != ESP_OK) {
         ESP_LOGE(TAG, "Port Expander initialzation failed: %s",
@@ -282,6 +311,9 @@ esp_err_t flow3r_bsp_spio_init(void) {
     INITIO(trrs_right.tip_badgelink_enable);
     INITIO(trrs_right.ring_badgelink_enable);
 
+    touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
+    touch_pad_fsm_start();
+
     return _portexp_update();
 }
 
diff --git a/components/micropython/include/mpconfigboard.h b/components/micropython/include/mpconfigboard.h
index 933be9981a..220cd89f09 100644
--- a/components/micropython/include/mpconfigboard.h
+++ b/components/micropython/include/mpconfigboard.h
@@ -4,8 +4,8 @@
 #define MICROPY_ENABLE_FINALISER (1)
 #define MICROPY_PY_MACHINE_DAC (0)
 #define MICROPY_PY_MACHINE_PWM (1)
-#define MICROPY_HW_I2C1_SCL (45)
-#define MICROPY_HW_I2C1_SDA (17)
+#define MICROPY_HW_I2C1_SCL (1)
+#define MICROPY_HW_I2C1_SDA (2)
 #define MICROPY_HW_ESP32S3_EXTENDED_IO (0)
 
 #define MICROPY_ESP_IDF_4 1
diff --git a/components/micropython/vendor/ports/esp32/machine_pin.c b/components/micropython/vendor/ports/esp32/machine_pin.c
index b0ce5f927c..d4715d18b0 100644
--- a/components/micropython/vendor/ports/esp32/machine_pin.c
+++ b/components/micropython/vendor/ports/esp32/machine_pin.c
@@ -147,13 +147,13 @@ STATIC const machine_pin_obj_t machine_pin_obj[GPIO_NUM_MAX] = {
 	#elif CONFIG_FLOW3R_ESP32S3
 
     {{NULL}, -1}, // 0
-    {{NULL}, -1}, // 1
-    {{NULL}, -1}, // 2
+    {{&machine_pin_type}, GPIO_NUM_1}, // QWIIC
+    {{&machine_pin_type}, GPIO_NUM_2}, // QWIIC
     {{NULL}, -1}, // 3
-    {{&machine_pin_type}, GPIO_NUM_4}, // Badgelink
-    {{&machine_pin_type}, GPIO_NUM_5}, // Badgelink
-    {{&machine_pin_type}, GPIO_NUM_6}, // Badgelink
-    {{&machine_pin_type}, GPIO_NUM_7}, // Badgelink
+    {{NULL}, -1}, // 4
+    {{NULL}, -1}, // 5
+    {{NULL}, -1}, // 6
+    {{NULL}, -1}, // 7
     {{NULL}, -1}, // 8
     {{&machine_pin_type}, GPIO_NUM_9}, // Battery ADC
     {{NULL}, -1}, // 10
@@ -161,9 +161,9 @@ STATIC const machine_pin_obj_t machine_pin_obj[GPIO_NUM_MAX] = {
     {{NULL}, -1}, // 12
     {{NULL}, -1}, // 13
     {{NULL}, -1}, // 14
-    {{NULL}, -1}, // 15
-    {{NULL}, -1}, // 16
-    {{&machine_pin_type}, GPIO_NUM_17}, // QWIIC
+    {{&machine_pin_type}, GPIO_NUM_15}, // GPIO1
+    {{&machine_pin_type}, GPIO_NUM_16}, // GPIO2
+    {{NULL}, -1}, // 17
     {{NULL}, -1}, // 18
     {{NULL}, -1}, // 19
     {{NULL}, -1}, // 20
@@ -184,14 +184,14 @@ STATIC const machine_pin_obj_t machine_pin_obj[GPIO_NUM_MAX] = {
     {{NULL}, -1}, // 35
     {{NULL}, -1}, // 36
     {{NULL}, -1}, // 37
-    {{NULL}, -1}, // 38
+    {{&machine_pin_type}, GPIO_NUM_38}, // GPIO0
     {{NULL}, -1}, // 39
     {{NULL}, -1}, // 40
     {{NULL}, -1}, // 41
     {{NULL}, -1}, // 42
     {{NULL}, -1}, // 43
     {{NULL}, -1}, // 44
-    {{&machine_pin_type}, GPIO_NUM_45}, // QWIIC
+    {{NULL}, -1}, // 45
     {{NULL}, -1}, // 46
 
     #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
diff --git a/components/st3m/st3m_audio.c b/components/st3m/st3m_audio.c
index 19eb042243..63ce6d4471 100644
--- a/components/st3m/st3m_audio.c
+++ b/components/st3m/st3m_audio.c
@@ -543,15 +543,15 @@ void st3m_audio_init(void) {
         }
     }
 
-    _update_routing();
-    _output_apply(&state.speaker);
-    _output_apply(&state.headphones);
-    bool _speaker_eq = (!_headphones_connected()) && state.speaker_eq_on;
-    flow3r_bsp_max98091_set_speaker_eq(_speaker_eq);
+    //_update_routing();
+    //_output_apply(&state.speaker);
+    //_output_apply(&state.headphones);
+    //bool _speaker_eq = (!_headphones_connected()) && state.speaker_eq_on;
+    //flow3r_bsp_max98091_set_speaker_eq(_speaker_eq);
 
     xTaskCreate(&_audio_player_task, "audio", 10000, NULL,
                 configMAX_PRIORITIES - 1, NULL);
-    xTaskCreate(&_jacksense_update_task, "jacksense", 2048, NULL, 8, NULL);
+    //xTaskCreate(&_jacksense_update_task, "jacksense", 2048, NULL, 8, NULL);
     ESP_LOGI(TAG, "Audio task started");
 }
 
diff --git a/components/st3m/st3m_captouch.c b/components/st3m/st3m_captouch.c
index 47f63295f3..de63631784 100644
--- a/components/st3m/st3m_captouch.c
+++ b/components/st3m/st3m_captouch.c
@@ -48,6 +48,8 @@ static inline void _pad_feed(st3m_petal_pad_state_t *pad, uint16_t data,
     } else {
         pad->pressure = 0;
     }
+
+//ESP_LOGE(TAG, "petal %d pressure %d thres %ld", index, data, thres);
 }
 
 // roughly matches the behavior of the legacy api. someday we should have more
@@ -66,7 +68,7 @@ static inline void _petal_process(st3m_petal_state_t *petal, uint8_t index) {
         petal->pressure =
             (petal->base.pressure + petal->ccw.pressure + petal->cw.pressure) /
             3;
-        int32_t raw = petal->base.raw + petal->ccw.raw + petal->cw.raw;
+        int32_t raw = (petal->base.raw + petal->ccw.raw + petal->cw.raw) / 3;
         petal->pressed = raw > thres;
         int32_t left = petal->ccw.raw;
         int32_t right = petal->cw.raw;
@@ -83,7 +85,7 @@ static inline void _petal_process(st3m_petal_state_t *petal, uint8_t index) {
 #endif
     } else {
         petal->pressure = (petal->base.pressure + petal->tip.pressure) / 2;
-        int32_t raw = petal->base.raw + petal->tip.raw;
+        int32_t raw = (petal->base.raw + petal->tip.raw) / 2;
         if (index == 5) raw *= 2;
         petal->pressed = raw > thres;
         int32_t base = petal->base.raw;
diff --git a/components/st3m/st3m_fs_sd.c b/components/st3m/st3m_fs_sd.c
index 99b8ce3ebf..b2d9d55fc8 100644
--- a/components/st3m/st3m_fs_sd.c
+++ b/components/st3m/st3m_fs_sd.c
@@ -39,9 +39,9 @@ esp_err_t st3m_fs_sd_init(void) {
     }
 
     sdmmc_slot_config_t slot = SDMMC_SLOT_CONFIG_DEFAULT();
-    slot.clk = GPIO_NUM_47;
-    slot.cmd = GPIO_NUM_48;
-    slot.d0 = GPIO_NUM_21;
+    slot.clk = GPIO_NUM_21;
+    slot.cmd = GPIO_NUM_18;
+    slot.d0 = GPIO_NUM_17;
     slot.width = 1;
     slot.flags = SDMMC_SLOT_NO_CD | SDMMC_SLOT_NO_WP;
     if ((ret = sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot)) != ESP_OK) {
diff --git a/components/st3m/st3m_io.c b/components/st3m/st3m_io.c
index 9d7a6f9e45..e8f16dd873 100644
--- a/components/st3m/st3m_io.c
+++ b/components/st3m/st3m_io.c
@@ -148,7 +148,7 @@ void st3m_io_init(void) {
         }
     }
 
-    st3m_io_badge_link_disable(BADGE_LINK_PIN_MASK_ALL);
+    //st3m_io_badge_link_disable(BADGE_LINK_PIN_MASK_ALL);
 
     xTaskCreate(&_task, "io", 4096, NULL, configMAX_PRIORITIES - 1, NULL);
     ESP_LOGI(TAG, "IO task started");
-- 
GitLab