sen5x Module in eigenes Component verschoben
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
// 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> // für PRIu32
|
||||
|
||||
#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)
|
||||
|
||||
// Lokaler I2C-Gerätekontext aus i2cdev
|
||||
static i2c_dev_t dev = {0};
|
||||
static struct esp32_i2c_config i2c_cfg = {0};
|
||||
static esp_err_t i2c_ok = ESP_OK;
|
||||
|
||||
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;
|
||||
ESP_LOGI(TAG, "Selected I2C bus index/port: %u", (unsigned)bus_idx);
|
||||
return NO_ERROR; // 0
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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(i2c_dev_t));
|
||||
|
||||
// Port & Pins/Frequenz aus der App-Konfiguration übernehmen
|
||||
dev.port = i2c_cfg.port;
|
||||
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)
|
||||
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);
|
||||
} 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));
|
||||
}
|
||||
|
||||
i2c_ok = err;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @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);
|
||||
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);
|
||||
|
||||
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_LOGV(TAG, "READ OK");
|
||||
return (int8_t)NO_ERROR; // 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute one write transaction on the I2C bus, sending a given number of
|
||||
* bytes.
|
||||
*
|
||||
* @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);
|
||||
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);
|
||||
|
||||
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_LOGV(TAG, "WRITE OK");
|
||||
return (int8_t)NO_ERROR; // 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Sleep for a given number of microseconds. The function should delay the
|
||||
* execution for at least the given time, but may also sleep longer.
|
||||
*
|
||||
* 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;
|
||||
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