Ersetzt unsichere Casts von esp_err_t nach int8_t durch ein stabiles Mapping gemäß Sensirion-HAL, nutzt die i2cdev-Mutex-API direkt statt Makros mit esp_err_t-Rückgaben, korrigiert sleep_usec() auf aufrundende µs→ms-Umrechnung zur Einhaltung der Mindestwartezeit und setzt addr_bit_len explizit auf 7 Bit mit zusätzlicher Grundvalidierung der I2C-Frequenz.
206 lines
5.6 KiB
C
206 lines
5.6 KiB
C
// File: sensirion_i2c_hal.c
|
|
|
|
#include "sensirion_i2c_hal.h"
|
|
#include "sensirion_common.h"
|
|
#include "sensirion_config.h"
|
|
|
|
#include <esp_log.h>
|
|
#include <freertos/FreeRTOS.h>
|
|
#include <freertos/task.h>
|
|
#include <string.h>
|
|
#include <inttypes.h>
|
|
|
|
#include "sensirion_i2c_esp32_config.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;
|
|
|
|
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) {
|
|
/*
|
|
* 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) {
|
|
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) {
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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);
|
|
}
|