fix(sen5x): Sensirion I2C HAL an int8_t-Fehlercodes angepasst
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.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
// File: sensirion_i2c_hal.c
|
||||
|
||||
#include "sensirion_i2c_hal.h"
|
||||
#include "sensirion_common.h"
|
||||
#include "sensirion_config.h"
|
||||
@@ -7,35 +8,57 @@
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <string.h>
|
||||
#include <inttypes.h> // für PRIu32
|
||||
#include <inttypes.h>
|
||||
|
||||
#include "sensirion_i2c_esp32_config.h"
|
||||
|
||||
static const char* TAG = "sensirion_i2c_hal";
|
||||
|
||||
#define SLEEP_MS(x) \
|
||||
vTaskDelay(((x) + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS)
|
||||
#define SLEEP_MS(ms) vTaskDelay(((ms) + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS)
|
||||
|
||||
// Lokaler I2C-Gerätekontext aus i2cdev
|
||||
/*
|
||||
* 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) {
|
||||
// Multi-Bus-Support: Port umschalten (optional, für Single-Bus reicht das so)
|
||||
dev.port = bus_idx;
|
||||
i2c_cfg.port = 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; // 0
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
esp_err_t sensirion_i2c_config_esp32(struct esp32_i2c_config* cfg) {
|
||||
if (cfg != NULL) {
|
||||
memcpy(&i2c_cfg, cfg, sizeof(*cfg));
|
||||
return ESP_OK;
|
||||
} else {
|
||||
return ESP_FAIL;
|
||||
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) {
|
||||
@@ -47,30 +70,38 @@ esp_err_t sensirion_i2c_esp32_ok(void) {
|
||||
* communication.
|
||||
*/
|
||||
void sensirion_i2c_hal_init(void) {
|
||||
memset(&dev, 0, sizeof(i2c_dev_t));
|
||||
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;
|
||||
|
||||
// Port & Pins/Frequenz aus der App-Konfiguration übernehmen
|
||||
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;
|
||||
#if HELPER_TARGET_IS_ESP32
|
||||
dev.cfg.master.clk_speed = i2c_cfg.freq; // Gerätespezifische Busgeschwindigkeit
|
||||
#endif
|
||||
|
||||
// Mutex anlegen & Gerät registrieren (i2cdev kümmert sich um Bus-Setup on-demand)
|
||||
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. Addr: 0x%02x Port: %d SDA: %d SCL: %d Freq: %" PRIu32 "Hz",
|
||||
i2c_cfg.addr, i2c_cfg.port, i2c_cfg.sda, i2c_cfg.scl,
|
||||
(uint32_t)i2c_cfg.freq);
|
||||
"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! Addr: 0x%02x Port: %d SDA: %d SCL: %d (err=%s)",
|
||||
i2c_cfg.addr, i2c_cfg.port, i2c_cfg.sda, i2c_cfg.scl,
|
||||
esp_err_to_name(err));
|
||||
"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;
|
||||
@@ -80,7 +111,6 @@ void sensirion_i2c_hal_init(void) {
|
||||
* Release all resources initialized by sensirion_i2c_hal_init().
|
||||
*/
|
||||
void sensirion_i2c_hal_free(void) {
|
||||
// Gibt Mutex frei, deregistriert Device & entfernt ggf. Bus-Handle wenn letztes Gerät (siehe i2cdev.c)
|
||||
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));
|
||||
@@ -88,71 +118,87 @@ void sensirion_i2c_hal_free(void) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute one read transaction on the I2C bus, reading a given number of bytes.
|
||||
* If the device does not acknowledge the read command, an error shall be
|
||||
* returned.
|
||||
* Execute one read transaction on the I2C bus.
|
||||
*
|
||||
* @param address 7-bit I2C address to read from
|
||||
* @param data pointer to the buffer where the data is to be stored
|
||||
* @param count number of bytes to read from I2C and store in the buffer
|
||||
* @returns 0 on success, error code otherwise
|
||||
*/
|
||||
int8_t sensirion_i2c_hal_read(uint8_t address, uint8_t* data, uint16_t count) {
|
||||
ESP_LOGD(TAG, "sensirion_i2c_hal_read: addr=0x%02x len=%u", address, (unsigned)count);
|
||||
if (count > 0 && data == NULL) {
|
||||
return SENSIRION_I2C_HAL_ERROR;
|
||||
}
|
||||
|
||||
dev.addr = address;
|
||||
|
||||
I2C_DEV_TAKE_MUTEX(&dev);
|
||||
esp_err_t err = i2c_dev_read(&dev, NULL, 0, data, count);
|
||||
I2C_DEV_GIVE_MUTEX(&dev);
|
||||
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, "READ failed (addr=0x%02x, len=%u): %s", address, (unsigned)count, esp_err_to_name(err));
|
||||
return (int8_t)err; // non-zero -> Fehler
|
||||
ESP_LOGW(TAG, "I2C READ failed addr=0x%02x len=%u: %s",
|
||||
address, (unsigned)count, esp_err_to_name(err));
|
||||
i2c_ok = err;
|
||||
}
|
||||
// ESP_LOGV(TAG, "READ OK");
|
||||
return (int8_t)NO_ERROR; // 0
|
||||
|
||||
return sensirion_map_esp_err(err);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute one write transaction on the I2C bus, sending a given number of
|
||||
* bytes.
|
||||
* Execute one write transaction on the I2C bus.
|
||||
*
|
||||
* @param address 7-bit I2C address to write to
|
||||
* @param data pointer to the buffer containing the data to write
|
||||
* @param count number of bytes to send over I2C
|
||||
* @returns 0 on success, error code otherwise
|
||||
*/
|
||||
int8_t sensirion_i2c_hal_write(uint8_t address, const uint8_t* data,
|
||||
uint16_t count) {
|
||||
ESP_LOGD(TAG, "sensirion_i2c_hal_write: addr=0x%02x len=%u", address, (unsigned)count);
|
||||
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;
|
||||
|
||||
I2C_DEV_TAKE_MUTEX(&dev);
|
||||
esp_err_t err = i2c_dev_write(&dev, NULL, 0, data, count);
|
||||
I2C_DEV_GIVE_MUTEX(&dev);
|
||||
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, "WRITE failed (addr=0x%02x, len=%u): %s", address, (unsigned)count, esp_err_to_name(err));
|
||||
return (int8_t)err; // non-zero -> Fehler
|
||||
ESP_LOGW(TAG, "I2C WRITE failed addr=0x%02x len=%u: %s",
|
||||
address, (unsigned)count, esp_err_to_name(err));
|
||||
i2c_ok = err;
|
||||
}
|
||||
// ESP_LOGV(TAG, "WRITE OK");
|
||||
return (int8_t)NO_ERROR; // 0
|
||||
|
||||
return sensirion_map_esp_err(err);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sleep for a given number of microseconds. The function should delay the
|
||||
* execution for at least the given time, but may also sleep longer.
|
||||
*
|
||||
* Sleep for a given number of microseconds.
|
||||
* Despite the unit, a <10 millisecond precision is sufficient.
|
||||
*
|
||||
* @param useconds the sleep time in microseconds
|
||||
*/
|
||||
void sensirion_i2c_hal_sleep_usec(uint32_t useconds) {
|
||||
ESP_LOGV(TAG, "sensirion_i2c_hal_sleep: %" PRIu32 " usec", useconds);
|
||||
// Tick-basiert schlafen (µs->ms), Genauigkeit im Bereich <10 ms ist ausreichend
|
||||
uint32_t ms = useconds / 1000U;
|
||||
// at-least semantics: ceil to milliseconds
|
||||
uint32_t ms = (useconds + 999U) / 1000U;
|
||||
if (ms == 0 && useconds > 0) {
|
||||
// Mindestens 1 Tick warten, wenn >0µs angefordert, um aktives Busy-Wait zu vermeiden
|
||||
ms = 1;
|
||||
}
|
||||
SLEEP_MS(ms);
|
||||
|
||||
Reference in New Issue
Block a user