// File: sensirion_i2c_hal.c #include "sensirion_i2c_hal.h" #include "sensirion_common.h" #include "sensirion_config.h" #include #include #include #include #include #include "sensirion_i2c_esp32_config.h" #include "hardware_config.h" #include "sensor_soft_i2c.h" #include "esp_rom_sys.h" static const char* TAG = "sensirion_i2c_hal"; #define SLEEP_MS(ms) vTaskDelay(((ms) + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS) /* * Sensirion HAL uses int8_t return codes: * - 0 (NO_ERROR) on success * - !=0 on failure * * sensirion_common.h defines only NO_ERROR and NOT_IMPLEMENTED_ERROR, * so we provide one stable generic error that fits in int8_t. */ #define SENSIRION_I2C_HAL_ERROR ((int8_t)-1) // Global (single) i2cdev device context static i2c_dev_t dev = {0}; static struct esp32_i2c_config i2c_cfg = {0}; static esp_err_t i2c_ok = ESP_OK; #if NANOPSX_SENSOR_SOFT_I2C static SemaphoreHandle_t soft_mutex; static void soft_sda(void *ctx, int release) { struct esp32_i2c_config *p = ctx; gpio_set_level(p->sda, release); } static void soft_scl(void *ctx, int release) { struct esp32_i2c_config *p = ctx; gpio_set_level(p->scl, release); } static int soft_read_sda(void *ctx) { return gpio_get_level(((struct esp32_i2c_config *)ctx)->sda); } static int soft_read_scl(void *ctx) { return gpio_get_level(((struct esp32_i2c_config *)ctx)->scl); } static void soft_delay(void *ctx, unsigned us) { (void)ctx; esp_rom_delay_us(us); } static int8_t soft_transfer(uint8_t address, uint8_t *data, uint16_t count, bool read) { if (!soft_mutex || !xSemaphoreTake(soft_mutex, pdMS_TO_TICKS(1000))) return SENSIRION_I2C_HAL_ERROR; const sensor_soft_i2c_t port = { .context = &i2c_cfg, .sda = soft_sda, .scl = soft_scl, .read_sda = soft_read_sda, .read_scl = soft_read_scl, .delay_us = soft_delay, .half_period_us = (1000000U + 2 * i2c_cfg.freq - 1) / (2 * i2c_cfg.freq), .stretch_timeout_us = 5000, }; int rc = sensor_soft_i2c_transfer(&port, address, data, count, read); xSemaphoreGive(soft_mutex); i2c_ok = rc == 0 ? ESP_OK : ESP_FAIL; if (rc) ESP_LOGW(TAG, "GPIO I2C %s failed addr=0x%02x rc=%d", read ? "read" : "write", address, rc); return rc == 0 ? 0 : SENSIRION_I2C_HAL_ERROR; } #endif static int8_t sensirion_map_esp_err(esp_err_t err) { return (err == ESP_OK) ? (int8_t)NO_ERROR : SENSIRION_I2C_HAL_ERROR; } /** * Select the current i2c bus by index. * Optional for single-bus setups. */ int16_t sensirion_i2c_hal_select_bus(uint8_t bus_idx) { #if NANOPSX_SENSOR_SOFT_I2C return bus_idx == i2c_cfg.port ? NO_ERROR : SENSIRION_I2C_HAL_ERROR; #endif /* * NOTE: * - This only switches the I2C port number. * - If "bus_idx" implies different pins/frequency, update i2c_cfg accordingly * and call sensirion_i2c_hal_init() again. */ dev.port = (i2c_port_t)bus_idx; i2c_cfg.port = (i2c_port_t)bus_idx; ESP_LOGI(TAG, "Selected I2C bus index/port: %u", (unsigned)bus_idx); return NO_ERROR; } esp_err_t sensirion_i2c_config_esp32(struct esp32_i2c_config* cfg) { if (cfg == NULL) { return ESP_ERR_INVALID_ARG; } memcpy(&i2c_cfg, cfg, sizeof(*cfg)); return ESP_OK; } esp_err_t sensirion_i2c_esp32_ok(void) { return i2c_ok; } /** * Initialize all hard- and software components that are needed for the I2C * communication. */ void sensirion_i2c_hal_init(void) { #if NANOPSX_SENSOR_SOFT_I2C if (!i2c_cfg.freq || i2c_cfg.freq > 100000 || !GPIO_IS_VALID_OUTPUT_GPIO(i2c_cfg.sda) || !GPIO_IS_VALID_OUTPUT_GPIO(i2c_cfg.scl) || i2c_cfg.sda == i2c_cfg.scl) { i2c_ok = ESP_ERR_INVALID_ARG; return; } gpio_set_level(i2c_cfg.sda, 1); gpio_set_level(i2c_cfg.scl, 1); gpio_config_t pins = { .pin_bit_mask = (1ULL << i2c_cfg.sda) | (1ULL << i2c_cfg.scl), .mode = GPIO_MODE_INPUT_OUTPUT_OD, .pull_up_en = GPIO_PULLUP_ENABLE, .pull_down_en = GPIO_PULLDOWN_DISABLE, .intr_type = GPIO_INTR_DISABLE, }; i2c_ok = gpio_config(&pins); if (i2c_ok != ESP_OK) return; soft_mutex = xSemaphoreCreateMutex(); if (!soft_mutex) { i2c_ok = ESP_ERR_NO_MEM; return; } ESP_LOGI(TAG, "GPIO I2C ready SDA=%d SCL=%d target=%lu Hz; touch uses hardware I2C", i2c_cfg.sda, i2c_cfg.scl, (unsigned long)i2c_cfg.freq); return; #endif memset(&dev, 0, sizeof(dev)); // Minimal sanity check if (i2c_cfg.freq == 0) { ESP_LOGE(TAG, "Invalid I2C frequency: 0 Hz"); i2c_ok = ESP_ERR_INVALID_ARG; return; } // i2cdev supports 7-bit and 10-bit; Sensirion drivers use 7-bit addresses. dev.addr_bit_len = I2C_ADDR_BIT_LEN_7; dev.port = i2c_cfg.port; // Pins: i2cdev supports -1 meaning "use Kconfig default" dev.cfg.sda_io_num = i2c_cfg.sda; dev.cfg.scl_io_num = i2c_cfg.scl; dev.cfg.sda_pullup_en = i2c_cfg.sda_pullup; dev.cfg.scl_pullup_en = i2c_cfg.scl_pullup; dev.cfg.master.clk_speed = (uint32_t)i2c_cfg.freq; esp_err_t err = i2c_dev_create_mutex(&dev); if (err == ESP_OK) { ESP_LOGI(TAG, "Sensirion I2C HAL ready. Port=%d SDA=%d SCL=%d Freq=%" PRIu32 " Hz", (int)i2c_cfg.port, (int)i2c_cfg.sda, (int)i2c_cfg.scl, (uint32_t)i2c_cfg.freq); } else { ESP_LOGE(TAG, "Sensirion I2C HAL init error. Port=%d SDA=%d SCL=%d (err=%s)", (int)i2c_cfg.port, (int)i2c_cfg.sda, (int)i2c_cfg.scl, esp_err_to_name(err)); } i2c_ok = err; } /** * Release all resources initialized by sensirion_i2c_hal_init(). */ void sensirion_i2c_hal_free(void) { #if NANOPSX_SENSOR_SOFT_I2C gpio_set_level(i2c_cfg.sda, 1); gpio_set_level(i2c_cfg.scl, 1); if (soft_mutex) { vSemaphoreDelete(soft_mutex); soft_mutex = NULL; } return; #endif esp_err_t err = i2c_dev_delete_mutex(&dev); if (err != ESP_OK) { ESP_LOGW(TAG, "i2c_dev_delete_mutex failed: %s", esp_err_to_name(err)); } } /** * Execute one read transaction on the I2C bus. * * @returns 0 on success, error code otherwise */ int8_t sensirion_i2c_hal_read(uint8_t address, uint8_t* data, uint16_t count) { if (count > 0 && data == NULL) { return SENSIRION_I2C_HAL_ERROR; } #if NANOPSX_SENSOR_SOFT_I2C return soft_transfer(address, data, count, true); #endif dev.addr = address; esp_err_t err = i2c_dev_take_mutex(&dev); if (err != ESP_OK) { ESP_LOGW(TAG, "take_mutex failed: %s", esp_err_to_name(err)); i2c_ok = err; return sensirion_map_esp_err(err); } err = i2c_dev_read(&dev, NULL, 0, data, (size_t)count); esp_err_t give_err = i2c_dev_give_mutex(&dev); if (give_err != ESP_OK) { ESP_LOGW(TAG, "give_mutex failed: %s", esp_err_to_name(give_err)); i2c_ok = give_err; return sensirion_map_esp_err(give_err); } if (err != ESP_OK) { ESP_LOGW(TAG, "I2C READ failed addr=0x%02x len=%u: %s", address, (unsigned)count, esp_err_to_name(err)); i2c_ok = err; } return sensirion_map_esp_err(err); } /** * Execute one write transaction on the I2C bus. * * @returns 0 on success, error code otherwise */ int8_t sensirion_i2c_hal_write(uint8_t address, const uint8_t* data, uint16_t count) { if (count > 0 && data == NULL) { return SENSIRION_I2C_HAL_ERROR; } #if NANOPSX_SENSOR_SOFT_I2C return soft_transfer(address, (uint8_t *)data, count, false); #endif dev.addr = address; esp_err_t err = i2c_dev_take_mutex(&dev); if (err != ESP_OK) { ESP_LOGW(TAG, "take_mutex failed: %s", esp_err_to_name(err)); i2c_ok = err; return sensirion_map_esp_err(err); } err = i2c_dev_write(&dev, NULL, 0, data, (size_t)count); esp_err_t give_err = i2c_dev_give_mutex(&dev); if (give_err != ESP_OK) { ESP_LOGW(TAG, "give_mutex failed: %s", esp_err_to_name(give_err)); i2c_ok = give_err; return sensirion_map_esp_err(give_err); } if (err != ESP_OK) { ESP_LOGW(TAG, "I2C WRITE failed addr=0x%02x len=%u: %s", address, (unsigned)count, esp_err_to_name(err)); i2c_ok = err; } return sensirion_map_esp_err(err); } /** * Sleep for a given number of microseconds. * Despite the unit, a <10 millisecond precision is sufficient. */ void sensirion_i2c_hal_sleep_usec(uint32_t useconds) { // at-least semantics: ceil to milliseconds uint32_t ms = (useconds + 999U) / 1000U; if (ms == 0 && useconds > 0) { ms = 1; } SLEEP_MS(ms); }