sen5x Module in eigenes Component verschoben
This commit is contained in:
@@ -2,12 +2,6 @@ idf_component_register(
|
||||
SRCS
|
||||
"main.c"
|
||||
"lv_fs_port.c"
|
||||
"i2cdev.c"
|
||||
"sensirion_common.c"
|
||||
"sensirion_i2c.c"
|
||||
"sensirion_i2c_hal.c"
|
||||
"sen5x_i2c.c"
|
||||
"sen5x_app.c"
|
||||
"wifi_mgr.c"
|
||||
"http_portal.c"
|
||||
"storage.c"
|
||||
@@ -15,6 +9,7 @@ idf_component_register(
|
||||
"net_mdns.c"
|
||||
"net_influx.c"
|
||||
"sys_status.c"
|
||||
"sen5x_app.c"
|
||||
INCLUDE_DIRS
|
||||
"."
|
||||
REQUIRES
|
||||
|
||||
@@ -1,101 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2019 Tomoyuki Sakurai <y@trombik.org>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#if !defined(__ESP_IDF_LIB_HELPERS__H__)
|
||||
#define __ESP_IDF_LIB_HELPERS__H__
|
||||
|
||||
/* XXX this header file does not need to include freertos/FreeRTOS.h.
|
||||
* but without it, ESP8266 RTOS SDK does not include `sdkconfig.h` in correct
|
||||
* order. as this header depends on sdkconfig.h, sdkconfig.h must be included
|
||||
* first. however, the SDK includes this header first, then includes
|
||||
* `sdkconfig.h` when freertos/FreeRTOS.h is not explicitly included. an
|
||||
* evidence can be found in `build/${COMPONENT}/${COMPONENT}.d` in a failed
|
||||
* build.
|
||||
*/
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <esp_idf_version.h>
|
||||
|
||||
#if !defined(ESP_IDF_VERSION) || !defined(ESP_IDF_VERSION_VAL)
|
||||
#error Unknown ESP-IDF/ESP8266 RTOS SDK version
|
||||
#endif
|
||||
|
||||
/* Minimal supported version for ESP32, ESP32S2 */
|
||||
#define HELPER_ESP32_MIN_VER ESP_IDF_VERSION_VAL(3, 3, 5)
|
||||
/* Minimal supported version for ESP8266 */
|
||||
#define HELPER_ESP8266_MIN_VER ESP_IDF_VERSION_VAL(3, 3, 0)
|
||||
|
||||
/* HELPER_TARGET_IS_ESP32
|
||||
* 1 when the target is esp32
|
||||
*/
|
||||
#if defined(CONFIG_IDF_TARGET_ESP32) \
|
||||
|| defined(CONFIG_IDF_TARGET_ESP32S2) \
|
||||
|| defined(CONFIG_IDF_TARGET_ESP32S3) \
|
||||
|| defined(CONFIG_IDF_TARGET_ESP32C2) \
|
||||
|| defined(CONFIG_IDF_TARGET_ESP32C3) \
|
||||
|| defined(CONFIG_IDF_TARGET_ESP32C5) \
|
||||
|| defined(CONFIG_IDF_TARGET_ESP32C6) \
|
||||
|| defined(CONFIG_IDF_TARGET_ESP32P4) \
|
||||
|| defined(CONFIG_IDF_TARGET_ESP32C61) \
|
||||
|| defined(CONFIG_IDF_TARGET_ESP32H2)
|
||||
#define HELPER_TARGET_IS_ESP32 (1)
|
||||
|
||||
/* HELPER_TARGET_IS_ESP8266
|
||||
* 1 when the target is esp8266
|
||||
*/
|
||||
#elif defined(CONFIG_IDF_TARGET_ESP8266)
|
||||
#define HELPER_TARGET_IS_ESP8266 (1)
|
||||
#else
|
||||
#error BUG: cannot determine the target
|
||||
#endif
|
||||
|
||||
#if HELPER_TARGET_IS_ESP32 && ESP_IDF_VERSION < HELPER_ESP32_MIN_VER
|
||||
#error Unsupported ESP-IDF version. Please update!
|
||||
#endif
|
||||
|
||||
#if HELPER_TARGET_IS_ESP8266 && ESP_IDF_VERSION < HELPER_ESP8266_MIN_VER
|
||||
#error Unsupported ESP8266 RTOS SDK version. Please update!
|
||||
#endif
|
||||
|
||||
/* HELPER_SPI_HOST_DEFAULT
|
||||
*
|
||||
* The default SPI_HOST for spi_host_device_t
|
||||
*/
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
#define HELPER_SPI_HOST_DEFAULT HSPI_HOST
|
||||
#elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
|
||||
#define HELPER_SPI_HOST_DEFAULT SPI2_HOST
|
||||
#elif CONFIG_IDF_TARGET_ESP32C2 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C5 || CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32C61
|
||||
#define HELPER_SPI_HOST_DEFAULT SPI1_HOST
|
||||
#elif CONFIG_IDF_TARGET_ESP32H2
|
||||
#define HELPER_SPI_HOST_DEFAULT SPI1_HOST
|
||||
#elif CONFIG_IDF_TARGET_ESP32P4
|
||||
#define HELPER_SPI_HOST_DEFAULT SPI1_HOST
|
||||
#endif
|
||||
|
||||
/* show the actual values for debugging */
|
||||
#if DEBUG
|
||||
#define VALUE_TO_STRING(x) #x
|
||||
#define VALUE(x) VALUE_TO_STRING(x)
|
||||
#define VAR_NAME_VALUE(var) #var "=" VALUE(var)
|
||||
#pragma message(VAR_NAME_VALUE(CONFIG_IDF_TARGET_ESP32C3))
|
||||
#pragma message(VAR_NAME_VALUE(CONFIG_IDF_TARGET_ESP32H2))
|
||||
#pragma message(VAR_NAME_VALUE(CONFIG_IDF_TARGET_ESP32S2))
|
||||
#pragma message(VAR_NAME_VALUE(CONFIG_IDF_TARGET_ESP32))
|
||||
#pragma message(VAR_NAME_VALUE(CONFIG_IDF_TARGET_ESP8266))
|
||||
#pragma message(VAR_NAME_VALUE(ESP_IDF_VERSION_MAJOR))
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,920 +0,0 @@
|
||||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2018 Ruslan V. Uss <unclerus@gmail.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file i2cdev.c
|
||||
*
|
||||
* ESP-IDF I2C master thread-safe functions for communication with I2C slave
|
||||
*
|
||||
* Copyright (C) 2018 Ruslan V. Uss <unclerus@gmail.com>
|
||||
* Updated 2025 by quinkq to use newer ESP-IDF I2C master driver API
|
||||
*
|
||||
* MIT Licensed as described in the file LICENSE
|
||||
*/
|
||||
// File: src/i2cdev.c
|
||||
#include "i2cdev.h"
|
||||
#include <driver/i2c_master.h>
|
||||
#include <esp_log.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
|
||||
static const char *TAG = "i2cdev";
|
||||
|
||||
// Fallback definition for platforms without 10-bit address support
|
||||
#ifndef I2C_ADDR_BIT_LEN_10
|
||||
#define I2C_ADDR_BIT_LEN_10 1
|
||||
#endif
|
||||
|
||||
#define I2C_DEFAULT_FREQ_HZ 400000
|
||||
#define I2C_MAX_RETRIES 3
|
||||
#define I2C_RETRY_BASE_DELAY_MS 20
|
||||
#define I2CDEV_MAX_STACK_ALLOC_SIZE 32 // Stack allocation threshold to avoid heap fragmentation for small buffers
|
||||
|
||||
typedef struct
|
||||
{
|
||||
SemaphoreHandle_t lock; // Mutex for exclusive access to this port's state
|
||||
i2c_master_bus_handle_t bus_handle; // Handle to the initialized I2C master bus
|
||||
bool installed; // Flag indicating if the bus for this port has been installed
|
||||
bool adopted;
|
||||
uint32_t ref_count; // Number of devices currently active on this bus port
|
||||
int sda_pin_current; // Actual SDA pin the bus was initialized with
|
||||
int scl_pin_current; // Actual SCL pin the bus was initialized with
|
||||
} i2c_port_state_t;
|
||||
|
||||
static i2c_port_state_t i2c_ports[I2C_NUM_MAX] = { 0 };
|
||||
static i2c_dev_t *active_devices[I2C_NUM_MAX][CONFIG_I2CDEV_MAX_DEVICES_PER_PORT] = { { NULL } };
|
||||
|
||||
esp_err_t i2cdev_adopt_bus(int port, i2c_master_bus_handle_t bus, int sda, int scl)
|
||||
{
|
||||
if (port < 0 || port >= I2C_NUM_MAX || !bus) return ESP_ERR_INVALID_ARG;
|
||||
i2c_port_state_t *st = &i2c_ports[port];
|
||||
if (!st->lock) st->lock = xSemaphoreCreateMutex();
|
||||
|
||||
xSemaphoreTake(st->lock, portMAX_DELAY);
|
||||
st->bus_handle = bus;
|
||||
st->installed = true; // <- wichtig
|
||||
st->adopted = true;
|
||||
st->sda_pin_current = sda; // <- du hattest bisher nur SCL gesetzt
|
||||
st->scl_pin_current = scl;
|
||||
// ref_count NICHT anfassen; das passiert beim Add/Remove von Devices
|
||||
xSemaphoreGive(st->lock);
|
||||
|
||||
ESP_LOGI(TAG, "[Port %d] Adopted external I2C bus (SDA=%d, SCL=%d, handle=%p)",
|
||||
port, sda, scl, bus);
|
||||
return ESP_OK;
|
||||
}
|
||||
// Helper to register a device
|
||||
static esp_err_t register_device(i2c_dev_t *dev)
|
||||
{
|
||||
if (!dev)
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
int port = dev->port;
|
||||
if (port >= I2C_NUM_MAX)
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
|
||||
// Note: Port mutex should be held by caller
|
||||
for (int i = 0; i < CONFIG_I2CDEV_MAX_DEVICES_PER_PORT; i++)
|
||||
{
|
||||
if (active_devices[port][i] == NULL)
|
||||
{
|
||||
active_devices[port][i] = dev;
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Registered device in slot %d", dev->addr, port, i);
|
||||
return ESP_OK;
|
||||
}
|
||||
}
|
||||
ESP_LOGE(TAG, "[0x%02x at %d] No free slots to register device - limit reached", dev->addr, port);
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
// Helper to deregister a device
|
||||
static void deregister_device(i2c_dev_t *dev)
|
||||
{
|
||||
if (!dev)
|
||||
return;
|
||||
int port = dev->port;
|
||||
if (port >= I2C_NUM_MAX)
|
||||
return;
|
||||
for (int i = 0; i < CONFIG_I2CDEV_MAX_DEVICES_PER_PORT; i++)
|
||||
{
|
||||
if (active_devices[port][i] == dev)
|
||||
{
|
||||
active_devices[port][i] = NULL;
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Deregistered device from slot %d", dev->addr, port, i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t i2cdev_init(void)
|
||||
{
|
||||
ESP_LOGV(TAG, "Initializing I2C subsystem...");
|
||||
memset(active_devices, 0, sizeof(active_devices));
|
||||
|
||||
for (int i = 0; i < I2C_NUM_MAX; i++) {
|
||||
if (!i2c_ports[i].lock) {
|
||||
i2c_ports[i].lock = xSemaphoreCreateMutex();
|
||||
if (!i2c_ports[i].lock) return ESP_ERR_NO_MEM;
|
||||
}
|
||||
// i2c_ports[i].installed = false;
|
||||
// i2c_ports[i].adopted = false;
|
||||
// i2c_ports[i].ref_count = 0;
|
||||
// i2c_ports[i].bus_handle = NULL;
|
||||
// i2c_ports[i].sda_pin_current = -1;
|
||||
// i2c_ports[i].scl_pin_current = -1;
|
||||
}
|
||||
ESP_LOGV(TAG, "I2C subsystem initialized.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t i2c_dev_create_mutex(i2c_dev_t *dev)
|
||||
{
|
||||
#if !CONFIG_I2CDEV_NOLOCK
|
||||
if (!dev)
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Creating device mutex...", dev->addr, dev->port);
|
||||
if (dev->mutex)
|
||||
{
|
||||
ESP_LOGW(TAG, "[0x%02x at %d] device mutex already exists (Handle: %p)", dev->addr, dev->port, dev->mutex);
|
||||
return ESP_OK; // Already created
|
||||
}
|
||||
|
||||
dev->mutex = xSemaphoreCreateMutex();
|
||||
if (!dev->mutex)
|
||||
{
|
||||
ESP_LOGE(TAG, "[0x%02x at %d] Could not create device mutex", dev->addr, dev->port);
|
||||
return ESP_ERR_NO_MEM; // Use ESP_ERR_NO_MEM for memory allocation failures
|
||||
}
|
||||
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Device mutex created (Handle: %p)", dev->addr, dev->port, dev->mutex);
|
||||
|
||||
// Register the device for cleanup tracking (under port mutex for consistency)
|
||||
if (dev->port < I2C_NUM_MAX && i2c_ports[dev->port].lock)
|
||||
{
|
||||
if (xSemaphoreTake(i2c_ports[dev->port].lock, pdMS_TO_TICKS(CONFIG_I2CDEV_TIMEOUT)) == pdTRUE)
|
||||
{
|
||||
esp_err_t reg_res = register_device(dev);
|
||||
if (reg_res != ESP_OK)
|
||||
{
|
||||
ESP_LOGW(TAG, "[0x%02x at %d] Failed to register device: %s - device will work but cleanup tracking disabled", dev->addr, dev->port, esp_err_to_name(reg_res));
|
||||
// Continue - device can still function without registration tracking
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Device registered successfully for cleanup tracking", dev->addr, dev->port);
|
||||
}
|
||||
xSemaphoreGive(i2c_ports[dev->port].lock);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGW(TAG, "[0x%02x at %d] Could not take port mutex for device registration", dev->addr, dev->port);
|
||||
// Continue - device can still function without registration tracking
|
||||
}
|
||||
}
|
||||
|
||||
// Set default address bit length if not explicitly set
|
||||
if (dev->addr_bit_len != I2C_ADDR_BIT_LEN_7 && dev->addr_bit_len != I2C_ADDR_BIT_LEN_10)
|
||||
{
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Setting default 7-bit address format", dev->addr, dev->port);
|
||||
dev->addr_bit_len = I2C_ADDR_BIT_LEN_7;
|
||||
}
|
||||
#else
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Mutex creation skipped (CONFIG_I2CDEV_NOLOCK=1)", dev->addr, dev->port);
|
||||
#endif
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t i2c_dev_delete_mutex(i2c_dev_t *dev)
|
||||
{
|
||||
#if !CONFIG_I2CDEV_NOLOCK
|
||||
if (!dev)
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Deleting device mutex and cleaning up resources", dev->addr, dev->port);
|
||||
|
||||
// Remove device from bus if handle exists
|
||||
if (dev->dev_handle)
|
||||
{
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Removing device handle %p from bus", dev->addr, dev->port, dev->dev_handle);
|
||||
esp_err_t rm_res = i2c_master_bus_rm_device((i2c_master_dev_handle_t)dev->dev_handle);
|
||||
if (rm_res != ESP_OK)
|
||||
{
|
||||
ESP_LOGW(TAG, "[0x%02x at %d] Failed to remove device handle: %s", dev->addr, dev->port, esp_err_to_name(rm_res));
|
||||
// Continue with cleanup despite error
|
||||
}
|
||||
dev->dev_handle = NULL;
|
||||
}
|
||||
|
||||
// Deregister the device
|
||||
deregister_device(dev);
|
||||
|
||||
// Update port reference count if port is valid
|
||||
if (dev->port < I2C_NUM_MAX)
|
||||
{
|
||||
if (xSemaphoreTake(i2c_ports[dev->port].lock, pdMS_TO_TICKS(CONFIG_I2CDEV_TIMEOUT)) == pdTRUE)
|
||||
{
|
||||
if (i2c_ports[dev->port].installed && i2c_ports[dev->port].ref_count > 0) {
|
||||
i2c_ports[dev->port].ref_count--;
|
||||
ESP_LOGV(TAG, "[Port %d] Decremented ref_count to %" PRIu32,
|
||||
dev->port, i2c_ports[dev->port].ref_count);
|
||||
|
||||
if (i2c_ports[dev->port].ref_count == 0) {
|
||||
if (i2c_ports[dev->port].adopted) {
|
||||
// ---> Bus gehört dem BSP; NICHT löschen, NICHT anfassen
|
||||
ESP_LOGI(TAG, "[Port %d] Last device removed, but bus is adopted; leaving bus intact",
|
||||
dev->port);
|
||||
// Hinweis: installed/bus_handle/adopted bleiben gesetzt,
|
||||
// damit später weitere Geräte ohne Re-Adopt hinzugefügt werden können.
|
||||
} else {
|
||||
// ---> Bus gehört i2cdev selbst; jetzt sauber löschen
|
||||
ESP_LOGI(TAG, "[Port %d] Last device removed, deleting this port's bus", dev->port);
|
||||
if (i2c_ports[dev->port].bus_handle) {
|
||||
ESP_LOGI(TAG, "[Port %d] Deleting bus handle %p",
|
||||
dev->port, i2c_ports[dev->port].bus_handle);
|
||||
esp_err_t del_bus_res = i2c_del_master_bus(i2c_ports[dev->port].bus_handle);
|
||||
if (del_bus_res != ESP_OK) {
|
||||
ESP_LOGE(TAG, "[Port %d] Failed to delete master bus: %s",
|
||||
dev->port, esp_err_to_name(del_bus_res));
|
||||
}
|
||||
}
|
||||
i2c_ports[dev->port].bus_handle = NULL;
|
||||
i2c_ports[dev->port].installed = false;
|
||||
i2c_ports[dev->port].sda_pin_current = -1;
|
||||
i2c_ports[dev->port].scl_pin_current = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
xSemaphoreGive(i2c_ports[dev->port].lock);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGW(TAG, "[0x%02x at %d] Could not take port mutex for ref_count update", dev->addr, dev->port);
|
||||
}
|
||||
}
|
||||
|
||||
// Delete the mutex itself last
|
||||
if (dev->mutex)
|
||||
{
|
||||
vSemaphoreDelete(dev->mutex);
|
||||
dev->mutex = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Device mutex was NULL, nothing to delete", dev->addr, dev->port);
|
||||
}
|
||||
#endif
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t i2c_dev_take_mutex(i2c_dev_t *dev)
|
||||
{
|
||||
#if !CONFIG_I2CDEV_NOLOCK
|
||||
if (!dev)
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Attempting to take device mutex (Handle: %p)...", dev->addr, dev->port, dev->mutex);
|
||||
if (!dev->mutex)
|
||||
{
|
||||
ESP_LOGE(TAG, "[0x%02x at %d] Attempt to take NULL device mutex!", dev->addr, dev->port);
|
||||
return ESP_ERR_INVALID_STATE; // Mutex doesn't exist
|
||||
}
|
||||
|
||||
TickType_t timeout_ticks = pdMS_TO_TICKS(CONFIG_I2CDEV_TIMEOUT);
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Taking device mutex with timeout %d ms (%lu ticks)", dev->addr, dev->port, CONFIG_I2CDEV_TIMEOUT, (unsigned long)timeout_ticks);
|
||||
if (!xSemaphoreTake(dev->mutex, timeout_ticks))
|
||||
{
|
||||
ESP_LOGE(TAG, "[0x%02x at %d] Could not take device mutex (Timeout after %d ms)", dev->addr, dev->port, CONFIG_I2CDEV_TIMEOUT);
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Device mutex taken successfully.", dev->addr, dev->port);
|
||||
#else
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Mutex take skipped (CONFIG_I2CDEV_NOLOCK=1)", dev->addr, dev->port);
|
||||
#endif
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t i2c_dev_give_mutex(i2c_dev_t *dev)
|
||||
{
|
||||
#if !CONFIG_I2CDEV_NOLOCK
|
||||
if (!dev)
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Giving device mutex (Handle: %p)...", dev->addr, dev->port, dev->mutex);
|
||||
if (!dev->mutex)
|
||||
{
|
||||
ESP_LOGE(TAG, "[0x%02x at %d] Attempt to give NULL device mutex!", dev->addr, dev->port);
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
if (!xSemaphoreGive(dev->mutex))
|
||||
{
|
||||
// This case should ideally not happen if the mutex was taken correctly
|
||||
ESP_LOGE(TAG, "[0x%02x at %d] Could not give device mutex (Was it taken?) (Handle: %p)", dev->addr, dev->port, dev->mutex);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Device mutex given successfully.", dev->addr, dev->port);
|
||||
#else
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Mutex give skipped (CONFIG_I2CDEV_NOLOCK=1)", dev->addr, dev->port);
|
||||
#endif
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
// i2c_setup_port: Initializes the I2C master bus for a given port if not already done.
|
||||
// It uses pin configurations from dev->cfg.sda_io_num and dev->cfg.scl_io_num.
|
||||
// The pins for a port are fixed after the first device initializes it.
|
||||
static esp_err_t i2c_setup_port(i2c_dev_t *dev) // dev is non-const to update dev->sda_pin, dev->scl_pin
|
||||
{
|
||||
if (!dev)
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
if (dev->port >= I2C_NUM_MAX)
|
||||
{
|
||||
ESP_LOGE(TAG, "Invalid I2C port number: %d", dev->port);
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
esp_err_t res = ESP_OK;
|
||||
i2c_port_state_t *port_state = &i2c_ports[dev->port];
|
||||
|
||||
ESP_LOGV(TAG, "[Port %d] Setup request for device 0x%02x", dev->port, dev->addr);
|
||||
if (xSemaphoreTake(port_state->lock, pdMS_TO_TICKS(CONFIG_I2CDEV_TIMEOUT)) != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(TAG, "[Port %d] Could not take port mutex for setup", dev->port);
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
|
||||
if (!port_state->installed)
|
||||
{
|
||||
if (port_state->adopted && port_state->bus_handle) {
|
||||
ESP_LOGI(TAG, "[Port %d] Using adopted I2C bus (SDA=%d, SCL=%d, handle=%p)",
|
||||
dev->port, port_state->sda_pin_current, port_state->scl_pin_current,
|
||||
port_state->bus_handle);
|
||||
port_state->installed = true; // wieder auf 'installed'
|
||||
dev->sda_pin = port_state->sda_pin_current;
|
||||
dev->scl_pin = port_state->scl_pin_current;
|
||||
xSemaphoreGive(port_state->lock);
|
||||
return ESP_OK; // KEIN i2c_new_master_bus()!
|
||||
}
|
||||
// Pin Selection Logic: Use device-specified pins, fallback to Kconfig defaults if -1
|
||||
gpio_num_t sda_pin = (dev->cfg.sda_io_num == (gpio_num_t)-1) ? (gpio_num_t)CONFIG_I2CDEV_DEFAULT_SDA_PIN : dev->cfg.sda_io_num;
|
||||
gpio_num_t scl_pin = (dev->cfg.scl_io_num == (gpio_num_t)-1) ? (gpio_num_t)CONFIG_I2CDEV_DEFAULT_SCL_PIN : dev->cfg.scl_io_num;
|
||||
|
||||
// Validate pins (basic check, gpio_is_valid_gpio could be used for more robust check)
|
||||
if (sda_pin < 0 || scl_pin < 0)
|
||||
{
|
||||
ESP_LOGE(TAG, "[Port %d] Invalid SCL/SDA pins: SDA=%d, SCL=%d. Check driver or Kconfig defaults.", dev->port, sda_pin, scl_pin);
|
||||
xSemaphoreGive(port_state->lock);
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
/*
|
||||
* OPTIONAL I2C PULLUP AUTO-CONFIGURATION
|
||||
*
|
||||
* By default: Uses whatever sda_pullup_en/scl_pullup_en you set (usually false)
|
||||
*
|
||||
* When CONFIG_I2CDEV_AUTO_ENABLE_PULLUPS=y: If both pullup flags are false,
|
||||
* automatically change them to true to enable internal pullups (~45kΩ).
|
||||
*
|
||||
* Manual pullup configuration:
|
||||
* - Set sda_pullup_en=true, scl_pullup_en=true for internal pullups
|
||||
* - Set sda_pullup_en=false, scl_pullup_en=false for external pullups
|
||||
*/
|
||||
|
||||
// Read user's pullup configuration (default false if not set)
|
||||
bool sda_pullup = dev->cfg.sda_pullup_en;
|
||||
bool scl_pullup = dev->cfg.scl_pullup_en;
|
||||
|
||||
#if CONFIG_I2CDEV_AUTO_ENABLE_PULLUPS
|
||||
// CONFIG_I2CDEV_AUTO_ENABLE_PULLUPS=y: If user didn't configure pullups, enable them automatically
|
||||
if (!sda_pullup && !scl_pullup)
|
||||
{
|
||||
sda_pullup = true;
|
||||
scl_pullup = true;
|
||||
ESP_LOGI(TAG, "[Port %d] Auto-enabling internal pullups (CONFIG_I2CDEV_AUTO_ENABLE_PULLUPS=y)", dev->port);
|
||||
}
|
||||
#endif
|
||||
|
||||
ESP_LOGI(TAG,
|
||||
"[Port %d] First initialization. Configuring bus with SDA=%d, SCL=%d (Pullups "
|
||||
"SCL:%d SDA:%d)",
|
||||
dev->port, sda_pin, scl_pin, scl_pullup, sda_pullup);
|
||||
|
||||
i2c_master_bus_config_t bus_config = {
|
||||
.i2c_port = dev->port,
|
||||
.sda_io_num = sda_pin,
|
||||
.scl_io_num = scl_pin,
|
||||
.clk_source = I2C_CLK_SRC_DEFAULT,
|
||||
.glitch_ignore_cnt = 7,
|
||||
.flags.enable_internal_pullup = (sda_pullup || scl_pullup),
|
||||
// Bus speed is not set here. It's per-device or a global target for the bus can be set
|
||||
// if desired, but i2c_master supports per-device speeds.
|
||||
};
|
||||
|
||||
res = i2c_new_master_bus(&bus_config, &port_state->bus_handle);
|
||||
if (res == ESP_OK)
|
||||
{
|
||||
port_state->installed = true;
|
||||
port_state->ref_count = 0; // Will be incremented when a device is successfully added
|
||||
port_state->sda_pin_current = sda_pin;
|
||||
port_state->scl_pin_current = scl_pin;
|
||||
dev->sda_pin = sda_pin; // Update dev struct with actual pins used
|
||||
dev->scl_pin = scl_pin;
|
||||
ESP_LOGI(TAG, "[Port %d] Successfully installed I2C master bus (Handle: %p).", dev->port, port_state->bus_handle);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "[Port %d] Failed to create master bus: %d (%s)", dev->port, res, esp_err_to_name(res));
|
||||
port_state->installed = false;
|
||||
port_state->bus_handle = NULL;
|
||||
port_state->sda_pin_current = -1;
|
||||
port_state->scl_pin_current = -1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGV(TAG, "[Port %d] Port already installed (SDA=%d, SCL=%d, Handle: %p).", dev->port, port_state->sda_pin_current, port_state->scl_pin_current, port_state->bus_handle);
|
||||
// Pin Consistency Check: For subsequent devices, ensure pins match already-configured bus
|
||||
gpio_num_t sda_desired = (dev->cfg.sda_io_num == (gpio_num_t)-1) ? (gpio_num_t)port_state->sda_pin_current : dev->cfg.sda_io_num;
|
||||
gpio_num_t scl_desired = (dev->cfg.scl_io_num == (gpio_num_t)-1) ? (gpio_num_t)port_state->scl_pin_current : dev->cfg.scl_io_num;
|
||||
|
||||
if (sda_desired != port_state->sda_pin_current || scl_desired != port_state->scl_pin_current)
|
||||
{
|
||||
ESP_LOGE(TAG,
|
||||
"[Port %d] Pin mismatch for device 0x%02x! Bus on SDA=%d,SCL=%d. Device wants "
|
||||
"SDA=%d,SCL=%d",
|
||||
dev->port, dev->addr, port_state->sda_pin_current, port_state->scl_pin_current, sda_desired, scl_desired);
|
||||
res = ESP_ERR_INVALID_STATE; // Cannot change pins for an installed bus
|
||||
}
|
||||
else
|
||||
{
|
||||
dev->sda_pin = port_state->sda_pin_current; // Update dev struct with actual pins used
|
||||
dev->scl_pin = port_state->scl_pin_current;
|
||||
}
|
||||
// ref_count is managed by i2c_setup_device when adding/removing device handles
|
||||
}
|
||||
|
||||
xSemaphoreGive(port_state->lock);
|
||||
ESP_LOGV(TAG, "[Port %d] Port setup finished with res %d.", dev->port, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
// i2c_setup_device: Ensures port is set up and adds the device to the bus if not already added.
|
||||
// It also registers the device in active_devices for cleanup purposes.
|
||||
static esp_err_t i2c_setup_device(i2c_dev_t *dev) // dev is non-const - modifies dev->dev_handle, dev->addr_bit_len
|
||||
{
|
||||
if (!dev)
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Setting up device context...", dev->addr, dev->port);
|
||||
|
||||
esp_err_t res = i2c_setup_port(dev);
|
||||
if (res != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "[0x%02x at %d] Port setup failed during device setup: %d (%s)", dev->addr, dev->port, res, esp_err_to_name(res));
|
||||
return res;
|
||||
}
|
||||
|
||||
// If addr_bit_len is not set (e.g. 0, which is invalid for i2c_addr_bit_len_t enum), default to
|
||||
// 7-bit. Modified to conditionally check for I2C_ADDR_BIT_LEN_10 based on hardware support
|
||||
if (dev->addr_bit_len != I2C_ADDR_BIT_LEN_7
|
||||
#if SOC_I2C_SUPPORT_10BIT_ADDR
|
||||
&& dev->addr_bit_len != I2C_ADDR_BIT_LEN_10
|
||||
#endif
|
||||
)
|
||||
{
|
||||
ESP_LOGD(TAG, "[0x%02x at %d] addr_bit_len not explicitly set, defaulting to 7-bit.", dev->addr, dev->port);
|
||||
dev->addr_bit_len = I2C_ADDR_BIT_LEN_7;
|
||||
}
|
||||
|
||||
// Only warn about address size if the device is actually using 10-bit addressing
|
||||
if (dev->addr_bit_len == I2C_ADDR_BIT_LEN_7 && dev->addr > 0x7F)
|
||||
{
|
||||
ESP_LOGW(TAG,
|
||||
"[0x%02x at %d] Device address > 0x7F but addr_bit_len is 7-bit. Ensure address "
|
||||
"is correct.",
|
||||
dev->addr, dev->port);
|
||||
}
|
||||
|
||||
#if !defined(SOC_I2C_SUPPORT_10BIT_ADDR) || !SOC_I2C_SUPPORT_10BIT_ADDR
|
||||
// On platforms without 10-bit support, force 7-bit addressing regardless of user setting
|
||||
if (dev->addr_bit_len == I2C_ADDR_BIT_LEN_10)
|
||||
{
|
||||
ESP_LOGW(TAG, "[0x%02x at %d] 10-bit addressing not supported on this platform, forcing 7-bit mode", dev->addr, dev->port);
|
||||
dev->addr_bit_len = I2C_ADDR_BIT_LEN_7;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (dev->dev_handle == NULL)
|
||||
{
|
||||
i2c_port_state_t *port_state = &i2c_ports[dev->port];
|
||||
if (xSemaphoreTake(port_state->lock, pdMS_TO_TICKS(CONFIG_I2CDEV_TIMEOUT)) != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(TAG, "[0x%02x at %d] Could not take port mutex for device add", dev->addr, dev->port);
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
|
||||
if (!port_state->installed || !port_state->bus_handle)
|
||||
{
|
||||
ESP_LOGE(TAG, "[0x%02x at %d] Cannot add device, bus for port %d not ready!", dev->addr, dev->port, dev->port);
|
||||
xSemaphoreGive(port_state->lock);
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Adding device to bus (Bus Handle: %p)...", dev->addr, dev->port, port_state->bus_handle);
|
||||
|
||||
uint32_t effective_dev_speed = dev->cfg.master.clk_speed;
|
||||
if (effective_dev_speed == 0)
|
||||
{
|
||||
ESP_LOGW(TAG,
|
||||
"[0x%02x at %d] Device speed (dev->cfg.master.clk_speed) is 0, using default: "
|
||||
"%" PRIu32 " Hz",
|
||||
dev->addr, dev->port, (uint32_t)I2C_DEFAULT_FREQ_HZ);
|
||||
effective_dev_speed = I2C_DEFAULT_FREQ_HZ;
|
||||
}
|
||||
|
||||
i2c_device_config_t dev_config = {
|
||||
// Use the possibly modified addr_bit_len that respects hardware capabilities
|
||||
.dev_addr_length = dev->addr_bit_len,
|
||||
.device_address = dev->addr,
|
||||
.scl_speed_hz = effective_dev_speed,
|
||||
.flags.disable_ack_check = false,
|
||||
};
|
||||
|
||||
res = i2c_master_bus_add_device(port_state->bus_handle, &dev_config, (i2c_master_dev_handle_t *)&dev->dev_handle);
|
||||
if (res == ESP_OK)
|
||||
{
|
||||
ESP_LOGI(TAG, "[0x%02x at %d] Device added successfully (Device Handle: %p, Speed: %" PRIu32 " Hz).", dev->addr, dev->port, dev->dev_handle, effective_dev_speed);
|
||||
|
||||
// Increment the port reference count for each device successfully added
|
||||
port_state->ref_count++;
|
||||
ESP_LOGV(TAG, "[Port %d] Incremented ref_count to %" PRIu32, dev->port, port_state->ref_count);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "[0x%02x at %d] Failed to add device to bus: %d (%s)", dev->addr, dev->port, res, esp_err_to_name(res));
|
||||
dev->dev_handle = NULL;
|
||||
}
|
||||
xSemaphoreGive(port_state->lock);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Device handle %p already exists. Skipping add.", dev->addr, dev->port, dev->dev_handle);
|
||||
res = ESP_OK;
|
||||
}
|
||||
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Device context setup finished with res %d.", dev->addr, dev->port, res);
|
||||
return res;
|
||||
}
|
||||
|
||||
// Helper function with retry mechanism for I2C operations
|
||||
static esp_err_t i2c_do_operation_with_retry(i2c_dev_t *dev, esp_err_t (*i2c_func)(i2c_master_dev_handle_t, const void *, size_t, void *, size_t, int), const void *write_buffer, size_t write_size,
|
||||
void *read_buffer, size_t read_size)
|
||||
{
|
||||
if (!dev)
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
esp_err_t res = ESP_FAIL;
|
||||
int retry = 0;
|
||||
int timeout_ms = CONFIG_I2CDEV_TIMEOUT;
|
||||
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Performing I2C operation (timeout %d ms)...", dev->addr, dev->port, timeout_ms);
|
||||
|
||||
while (retry <= I2C_MAX_RETRIES)
|
||||
{
|
||||
// Ensure device is set up before each attempt, in case handle became stale or bus was reset
|
||||
// This is more robust if issues like bus errors or device resets occur.
|
||||
res = i2c_setup_device(dev);
|
||||
if (res != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "[0x%02x at %d] Device setup failed (Try %d): %d (%s). Retrying setup...", dev->addr, dev->port, retry, res, esp_err_to_name(res));
|
||||
// No point continuing this attempt if setup fails, but the loop will retry setup.
|
||||
vTaskDelay(pdMS_TO_TICKS(I2C_RETRY_BASE_DELAY_MS * (1 << (retry))));
|
||||
retry++;
|
||||
continue;
|
||||
}
|
||||
if (!dev->dev_handle)
|
||||
{
|
||||
ESP_LOGE(TAG,
|
||||
"[0x%02x at %d] Device handle is NULL after setup (Try %d)! Cannot perform "
|
||||
"operation.",
|
||||
dev->addr, dev->port, retry);
|
||||
// This indicates a persistent problem with adding the device to the bus.
|
||||
// No point retrying the i2c_func if handle is null.
|
||||
res = ESP_ERR_INVALID_STATE;
|
||||
vTaskDelay(pdMS_TO_TICKS(I2C_RETRY_BASE_DELAY_MS * (1 << (retry))));
|
||||
retry++;
|
||||
continue;
|
||||
}
|
||||
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Attempting I2C op (Try %d, Handle %p)", dev->addr, dev->port, retry, dev->dev_handle);
|
||||
res = i2c_func(dev->dev_handle, write_buffer, write_size, read_buffer, read_size, timeout_ms);
|
||||
|
||||
if (res == ESP_OK)
|
||||
{
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] I2C operation successful (Try %d).", dev->addr, dev->port, retry);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
ESP_LOGW(TAG, "[0x%02x at %d] I2C op failed (Try %d, Handle %p): %d (%s).", dev->addr, dev->port, retry, dev->dev_handle, res, esp_err_to_name(res));
|
||||
|
||||
// Only remove handle on errors that indicate handle corruption or permanent invalidity
|
||||
// Don't remove on temporary errors like ESP_ERR_TIMEOUT, ESP_FAIL (NACK), etc.
|
||||
bool should_remove_handle = false;
|
||||
switch (res)
|
||||
{
|
||||
case ESP_ERR_INVALID_ARG:
|
||||
// Handle was likely removed by another task or is corrupted
|
||||
should_remove_handle = true;
|
||||
ESP_LOGW(TAG, "[0x%02x at %d] Invalid argument error - handle may be corrupted", dev->addr, dev->port);
|
||||
break;
|
||||
case ESP_ERR_INVALID_STATE:
|
||||
// I2C driver is in invalid state, handle likely needs recreation
|
||||
should_remove_handle = true;
|
||||
ESP_LOGW(TAG, "[0x%02x at %d] Invalid state error - handle may need recreation", dev->addr, dev->port);
|
||||
break;
|
||||
default:
|
||||
// For other errors (timeout, NACK, bus busy, etc.), keep the handle
|
||||
// These are usually temporary and don't require handle recreation
|
||||
should_remove_handle = false;
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Temporary error - keeping handle for retry", dev->addr, dev->port);
|
||||
break;
|
||||
}
|
||||
|
||||
if (should_remove_handle && dev->dev_handle)
|
||||
{
|
||||
ESP_LOGW(TAG, "[0x%02x at %d] Removing potentially corrupted device handle %p after permanent error", dev->addr, dev->port, dev->dev_handle);
|
||||
// Try to remove the handle from the bus before nullifying
|
||||
esp_err_t rm_res = i2c_master_bus_rm_device(dev->dev_handle);
|
||||
if (rm_res != ESP_OK)
|
||||
{
|
||||
ESP_LOGW(TAG, "[0x%02x at %d] Failed to remove corrupted handle (expected): %s", dev->addr, dev->port, esp_err_to_name(rm_res));
|
||||
// This is expected if the handle was already invalid - continue cleanup
|
||||
}
|
||||
dev->dev_handle = NULL;
|
||||
}
|
||||
|
||||
retry++;
|
||||
if (retry <= I2C_MAX_RETRIES)
|
||||
{
|
||||
vTaskDelay(pdMS_TO_TICKS(I2C_RETRY_BASE_DELAY_MS * (1 << retry))); // Exponential backoff
|
||||
ESP_LOGW(TAG, "[0x%02x at %d] Retrying operation...", dev->addr, dev->port);
|
||||
}
|
||||
}
|
||||
|
||||
ESP_LOGE(TAG, "[0x%02x at %d] I2C operation failed after %d retries. Last error: %d (%s)", dev->addr, dev->port, I2C_MAX_RETRIES + 1, res, esp_err_to_name(res));
|
||||
return res;
|
||||
}
|
||||
|
||||
// Wrapper functions for the I2C master API to use with the retry mechanism
|
||||
// i2c_do_operation_with_retry() needs a unified function signature for all I2C operations
|
||||
static esp_err_t i2c_master_transmit_wrapper(i2c_master_dev_handle_t handle, const void *write_buffer, size_t write_size, void *read_buffer, size_t read_size, int timeout_ms)
|
||||
{
|
||||
return i2c_master_transmit(handle, write_buffer, write_size, timeout_ms);
|
||||
}
|
||||
|
||||
static esp_err_t i2c_master_receive_wrapper(i2c_master_dev_handle_t handle, const void *write_buffer, size_t write_size, void *read_buffer, size_t read_size, int timeout_ms)
|
||||
{
|
||||
return i2c_master_receive(handle, read_buffer, read_size, timeout_ms);
|
||||
}
|
||||
|
||||
static esp_err_t i2c_master_transmit_receive_wrapper(i2c_master_dev_handle_t handle, const void *write_buffer, size_t write_size, void *read_buffer, size_t read_size, int timeout_ms)
|
||||
{
|
||||
return i2c_master_transmit_receive(handle, write_buffer, write_size, read_buffer, read_size, timeout_ms);
|
||||
}
|
||||
|
||||
esp_err_t i2c_dev_read(const i2c_dev_t *dev, const void *out_data, size_t out_size, void *in_data, size_t in_size)
|
||||
{
|
||||
if (!dev || !in_data || !in_size)
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] i2c_dev_read called (out_size: %u, in_size: %u)", dev->addr, dev->port, out_size, in_size);
|
||||
|
||||
esp_err_t result = i2c_do_operation_with_retry((i2c_dev_t *)dev, // Cast to non-const for i2c_setup_device internal modifications
|
||||
out_data && out_size ? i2c_master_transmit_receive_wrapper : i2c_master_receive_wrapper, out_data, out_size, in_data, in_size);
|
||||
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] i2c_dev_read result: %s (%d)", dev->addr, dev->port, esp_err_to_name(result), result);
|
||||
return result;
|
||||
}
|
||||
|
||||
esp_err_t i2c_dev_write(const i2c_dev_t *dev, const void *out_reg, size_t out_reg_size, const void *out_data, size_t out_size)
|
||||
{
|
||||
if (!dev)
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
if ((!out_reg || !out_reg_size) && (!out_data || !out_size))
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] i2c_dev_write called (reg_size: %u, data_size: %u)", dev->addr, dev->port, out_reg_size, out_size);
|
||||
|
||||
esp_err_t res;
|
||||
if (out_reg && out_reg_size && out_data && out_size)
|
||||
{
|
||||
size_t total_write_size = out_reg_size + out_size;
|
||||
|
||||
// Check for overflow before proceeding
|
||||
if (total_write_size < out_reg_size || total_write_size < out_size)
|
||||
{
|
||||
ESP_LOGE(TAG, "[0x%02x at %d] Write size overflow: reg_size=%u + data_size=%u", dev->addr, dev->port, out_reg_size, out_size);
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
// Use stack for small buffers to avoid heap fragmentation
|
||||
if (total_write_size <= I2CDEV_MAX_STACK_ALLOC_SIZE)
|
||||
{ // Use stack allocation for small buffers
|
||||
uint8_t stack_buf[I2CDEV_MAX_STACK_ALLOC_SIZE];
|
||||
memcpy(stack_buf, out_reg, out_reg_size);
|
||||
memcpy(stack_buf + out_reg_size, out_data, out_size);
|
||||
res = i2c_do_operation_with_retry((i2c_dev_t *)dev, i2c_master_transmit_wrapper, stack_buf, total_write_size, NULL, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint8_t *heap_buf = malloc(total_write_size);
|
||||
if (!heap_buf)
|
||||
{
|
||||
ESP_LOGE(TAG, "[0x%02x at %d] Failed to allocate %u bytes for write", dev->addr, dev->port, total_write_size);
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
memcpy(heap_buf, out_reg, out_reg_size);
|
||||
memcpy(heap_buf + out_reg_size, out_data, out_size);
|
||||
res = i2c_do_operation_with_retry((i2c_dev_t *)dev, i2c_master_transmit_wrapper, heap_buf, total_write_size, NULL, 0);
|
||||
free(heap_buf); // Free buffer regardless of operation result
|
||||
}
|
||||
}
|
||||
else if (out_reg && out_reg_size)
|
||||
{
|
||||
res = i2c_do_operation_with_retry((i2c_dev_t *)dev, i2c_master_transmit_wrapper, out_reg, out_reg_size, NULL, 0);
|
||||
}
|
||||
else if (out_data && out_size)
|
||||
{
|
||||
res = i2c_do_operation_with_retry((i2c_dev_t *)dev, i2c_master_transmit_wrapper, out_data, out_size, NULL, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
return ESP_ERR_INVALID_ARG; // Shouldn't reach here given the earlier check
|
||||
}
|
||||
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] i2c_dev_write result: %s (%d)", dev->addr, dev->port, esp_err_to_name(res), res);
|
||||
return res;
|
||||
}
|
||||
|
||||
esp_err_t i2c_dev_read_reg(const i2c_dev_t *dev, uint8_t reg, void *data, size_t size)
|
||||
{
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] i2c_dev_read_reg called (reg: 0x%02x, size: %u)", dev->addr, dev->port, reg, size);
|
||||
return i2c_dev_read(dev, ®, 1, data, size);
|
||||
}
|
||||
|
||||
esp_err_t i2c_dev_write_reg(const i2c_dev_t *dev, uint8_t reg, const void *data, size_t size)
|
||||
{
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] i2c_dev_write_reg called (reg: 0x%02x, size: %u)", dev->addr, dev->port, reg, size);
|
||||
return i2c_dev_write(dev, ®, 1, data, size);
|
||||
}
|
||||
|
||||
esp_err_t i2c_dev_check_present(const i2c_dev_t *dev_const)
|
||||
{
|
||||
if (!dev_const)
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Probing device presence...", dev_const->addr, dev_const->port);
|
||||
|
||||
// Cast to non-const for i2c_setup_port (which may modify internal state)
|
||||
i2c_dev_t *dev = (i2c_dev_t *)dev_const;
|
||||
|
||||
// Ensure the I2C port is set up before probing
|
||||
esp_err_t setup_res = i2c_setup_port(dev);
|
||||
if (setup_res != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "[0x%02x at %d] Failed to setup port for probe: %s", dev_const->addr, dev_const->port, esp_err_to_name(setup_res));
|
||||
return setup_res;
|
||||
}
|
||||
|
||||
// Now probe using the initialized bus
|
||||
if (dev_const->port < I2C_NUM_MAX && i2c_ports[dev_const->port].lock)
|
||||
{
|
||||
if (xSemaphoreTake(i2c_ports[dev_const->port].lock, pdMS_TO_TICKS(CONFIG_I2CDEV_TIMEOUT)) == pdTRUE)
|
||||
{
|
||||
if (i2c_ports[dev_const->port].installed && i2c_ports[dev_const->port].bus_handle)
|
||||
{
|
||||
// Use ESP-IDF's built-in probe function - completely non-intrusive
|
||||
esp_err_t probe_res = i2c_master_probe(i2c_ports[dev_const->port].bus_handle, dev_const->addr, CONFIG_I2CDEV_TIMEOUT);
|
||||
xSemaphoreGive(i2c_ports[dev_const->port].lock);
|
||||
|
||||
if (probe_res == ESP_OK)
|
||||
{
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Device probe successful - device present", dev_const->addr, dev_const->port);
|
||||
return ESP_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Device probe failed: %s", dev_const->addr, dev_const->port, esp_err_to_name(probe_res));
|
||||
return probe_res;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
xSemaphoreGive(i2c_ports[dev_const->port].lock);
|
||||
ESP_LOGW(TAG, "[0x%02x at %d] Cannot probe - bus not ready on port %d", dev_const->addr, dev_const->port, dev_const->port);
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "[0x%02x at %d] Could not take port mutex for probe", dev_const->addr, dev_const->port);
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "[0x%02x at %d] Invalid port or port not initialized", dev_const->addr, dev_const->port);
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
}
|
||||
|
||||
// Compatibility wrapper for legacy code that still calls i2c_dev_probe
|
||||
// The new driver implementation uses i2c_master_probe which doesn't need operation_type
|
||||
esp_err_t i2c_dev_probe(const i2c_dev_t *dev, i2c_dev_type_t operation_type)
|
||||
{
|
||||
ESP_LOGV(TAG, "[0x%02x at %d] Legacy probe called (operation_type %d), redirecting to new implementation", dev->addr, dev->port, operation_type);
|
||||
|
||||
return i2c_dev_check_present(dev);
|
||||
}
|
||||
|
||||
// Clean up function to be called at application exit
|
||||
esp_err_t i2cdev_done(void)
|
||||
{
|
||||
esp_err_t result = ESP_OK;
|
||||
ESP_LOGV(TAG, "Cleaning up I2C subsystem (i2c_master)...");
|
||||
for (int i = 0; i < I2C_NUM_MAX; i++) {
|
||||
if (i2c_ports[i].lock) {
|
||||
if (xSemaphoreTake(i2c_ports[i].lock, pdMS_TO_TICKS(CONFIG_I2CDEV_TIMEOUT)) != pdTRUE) {
|
||||
ESP_LOGE(TAG, "[Port %d] Could not take port mutex for cleanup", i);
|
||||
result = ESP_FAIL;
|
||||
} else {
|
||||
if (i2c_ports[i].installed) {
|
||||
// 1) Alle bekannten Geräte-Handles vom Bus nehmen
|
||||
for (int j = 0; j < CONFIG_I2CDEV_MAX_DEVICES_PER_PORT; j++) {
|
||||
i2c_dev_t *dev = active_devices[i][j];
|
||||
if (dev && dev->dev_handle) {
|
||||
esp_err_t rm = i2c_master_bus_rm_device((i2c_master_dev_handle_t)dev->dev_handle);
|
||||
if (rm != ESP_OK) {
|
||||
ESP_LOGW(TAG, "[Port %d] rm_device failed for slot %d: %s",
|
||||
i, j, esp_err_to_name(rm));
|
||||
result = (result == ESP_OK) ? rm : result;
|
||||
}
|
||||
dev->dev_handle = NULL;
|
||||
}
|
||||
active_devices[i][j] = NULL;
|
||||
}
|
||||
|
||||
// 2) Bus nur löschen, wenn NICHT adoptiert
|
||||
if (!i2c_ports[i].adopted) {
|
||||
if (i2c_ports[i].bus_handle) {
|
||||
esp_err_t del_res = i2c_del_master_bus(i2c_ports[i].bus_handle);
|
||||
if (del_res != ESP_OK) {
|
||||
ESP_LOGW(TAG, "[Port %d] i2c_del_master_bus failed: %s",
|
||||
i, esp_err_to_name(del_res));
|
||||
result = (result == ESP_OK) ? del_res : result;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ESP_LOGI(TAG, "[Port %d] Bus is adopted; skipping bus deletion", i);
|
||||
}
|
||||
|
||||
// 3) Interne Zustände zurücksetzen
|
||||
i2c_ports[i].bus_handle = NULL;
|
||||
i2c_ports[i].installed = false;
|
||||
i2c_ports[i].ref_count = 0;
|
||||
i2c_ports[i].sda_pin_current = -1;
|
||||
i2c_ports[i].scl_pin_current = -1;
|
||||
i2c_ports[i].adopted = false;
|
||||
}
|
||||
xSemaphoreGive(i2c_ports[i].lock);
|
||||
}
|
||||
vSemaphoreDelete(i2c_ports[i].lock);
|
||||
i2c_ports[i].lock = NULL;
|
||||
memset(active_devices[i], 0, sizeof(active_devices[i]));
|
||||
}
|
||||
}
|
||||
|
||||
ESP_LOGV(TAG, "I2C subsystem cleanup finished with result: %d", result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1,388 +0,0 @@
|
||||
/**
|
||||
* @file i2cdev.h
|
||||
* @defgroup i2cdev i2cdev
|
||||
* @{
|
||||
*
|
||||
* ESP-IDF I2C master thread-safe functions for communication with I2C slave
|
||||
*
|
||||
* This implementation uses the newer ESP-IDF I2C master driver (v5.0+).
|
||||
* For ESP-IDF versions using the legacy I2C driver, use i2cdev_legacy.c instead.
|
||||
*
|
||||
* Copyright (C) 2018 Ruslan V. Uss <unclerus@gmail.com>
|
||||
* Updated 2025 by quinkq to use newer ESP-IDF I2C master driver API
|
||||
*
|
||||
* MIT Licensed as described in the file LICENSE
|
||||
*
|
||||
* ============================================================================
|
||||
* OPTIONAL I2C PULLUP AUTO-CONFIGURATION
|
||||
* ============================================================================
|
||||
*
|
||||
* This library can optionally enable internal I2C pullups when no explicit
|
||||
* pullup configuration is provided. Feature is DISABLED by default for
|
||||
* backward compatibility (CONFIG_I2CDEV_AUTO_ENABLE_PULLUPS=n).
|
||||
*
|
||||
* Optional auto-pullup (CONFIG_I2CDEV_AUTO_ENABLE_PULLUPS=y):
|
||||
* - If both pullup flags are false (not set/default state), automatically enables internal pullups
|
||||
* - Only available on ESP32 family (modern driver)
|
||||
* - Legacy driver always uses explicit configuration
|
||||
*
|
||||
*
|
||||
* Example - Enable internal pullups:
|
||||
* i2c_dev_t sensor = {
|
||||
* .port = I2C_NUM_0,
|
||||
* .addr = 0x48,
|
||||
* .cfg = {
|
||||
* .sda_io_num = GPIO_NUM_21,
|
||||
* .scl_io_num = GPIO_NUM_22,
|
||||
* .sda_pullup_en = true, // Enable internal pullups
|
||||
* .scl_pullup_en = true, // Enable internal pullups
|
||||
* .master.clk_speed = 400000
|
||||
* }
|
||||
* };
|
||||
*
|
||||
* ============================================================================
|
||||
*/
|
||||
|
||||
#ifndef __I2CDEV_H__
|
||||
#define __I2CDEV_H__
|
||||
|
||||
#include <driver/gpio.h>
|
||||
#include <driver/i2c.h>
|
||||
#include <esp_err.h>
|
||||
#include <esp_idf_lib_helpers.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
|
||||
// Define missing types for older ESP-IDF versions
|
||||
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 2, 0)
|
||||
typedef enum {
|
||||
I2C_ADDR_BIT_LEN_7 = 0, /*!< I2C 7bit address for slave mode */
|
||||
I2C_ADDR_BIT_LEN_10, /*!< I2C 10bit address for slave mode */
|
||||
} i2c_addr_bit_len_t;
|
||||
#endif
|
||||
|
||||
/* ==== ESP-IDF I2C Header je nach Version ==== */
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
#include <driver/i2c_types.h> // i2c_port_t, i2c_addr_bit_len_t
|
||||
#include <driver/i2c_master.h> // i2c_master_bus_handle_t etc.
|
||||
#else
|
||||
#include <driver/i2c.h> // ältere IDF: alles hier drin
|
||||
#endif
|
||||
|
||||
// Definition for I2CDEV_MAX_STRETCH_TIME
|
||||
#if HELPER_TARGET_IS_ESP8266
|
||||
#define I2CDEV_MAX_STRETCH_TIME 0xffffffff
|
||||
#else
|
||||
#include <soc/i2c_reg.h> // For I2C_TIME_OUT_VALUE_V, etc.
|
||||
#if defined(I2C_TIME_OUT_VALUE_V)
|
||||
#define I2CDEV_MAX_STRETCH_TIME I2C_TIME_OUT_VALUE_V
|
||||
#elif defined(I2C_TIME_OUT_REG_V)
|
||||
#define I2CDEV_MAX_STRETCH_TIME I2C_TIME_OUT_REG_V
|
||||
#else
|
||||
#define I2CDEV_MAX_STRETCH_TIME 0x00ffffff
|
||||
#endif
|
||||
#endif /* HELPER_TARGET_IS_ESP8266 */
|
||||
|
||||
#ifndef CONFIG_I2CDEV_TIMEOUT
|
||||
#define CONFIG_I2CDEV_TIMEOUT 1000 // Default 1 second timeout
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_I2CDEV_NOLOCK
|
||||
#define CONFIG_I2CDEV_NOLOCK 0 // Enable locking by default
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_I2CDEV_MAX_DEVICES_PER_PORT
|
||||
#define CONFIG_I2CDEV_MAX_DEVICES_PER_PORT 8 // Maximum devices per I2C port
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_I2CDEV_DEFAULT_SDA_PIN
|
||||
#define CONFIG_I2CDEV_DEFAULT_SDA_PIN 21 // Default SDA pin
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_I2CDEV_DEFAULT_SCL_PIN
|
||||
#define CONFIG_I2CDEV_DEFAULT_SCL_PIN 22 // Default SCL pin
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_FREERTOS_HZ
|
||||
#define CONFIG_FREERTOS_HZ 100 // Default value in most ESP-IDF configs
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_LOG_MAXIMUM_LEVEL
|
||||
#define CONFIG_LOG_MAXIMUM_LEVEL 3 // INFO level as default
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief I2C transaction type for legacy probe
|
||||
*/
|
||||
typedef enum {
|
||||
I2C_DEV_WRITE = 0, /**< Write operation for probe */
|
||||
I2C_DEV_READ /**< Read operation for probe */
|
||||
} i2c_dev_type_t;
|
||||
|
||||
/**
|
||||
* I2C device descriptor
|
||||
*
|
||||
* This structure supports both legacy ESP-IDF I2C driver and modern i2c_master driver.
|
||||
*
|
||||
* @note INITIALIZATION CHECKLIST - Set these fields before calling i2c_dev_create_mutex():
|
||||
*
|
||||
* ┌─── REQUIRED (Set by user) ───────────────────────────────────────────┐
|
||||
* │ - dev->port - I2C port number (e.g., I2C_NUM_0) │
|
||||
* │ - dev->addr - Device I2C address (e.g., 0x48) │
|
||||
* │ - dev->cfg.sda_io_num - SDA pin (-1 = use Kconfig default) │
|
||||
* │ - dev->cfg.scl_io_num - SCL pin (-1 = use Kconfig default) │
|
||||
* │ - dev->cfg.master.clk_speed - Clock speed in Hz (e.g., 400000) │
|
||||
* └──────────────────────────────────────────────────────────────────────┘
|
||||
*
|
||||
* ┌─── OPTIONAL (Set by user if needed) ─────────────────────────────────┐
|
||||
* │ - dev->addr_bit_len - Address format (defaults to 7-bit) - NEW │
|
||||
* │ - dev->cfg.sda_pullup_en - Enable internal SDA pullup │
|
||||
* │ - dev->cfg.scl_pullup_en - Enable internal SCL pullup │
|
||||
* │ - dev->timeout_ticks - Legacy driver timeout (legacy only) │
|
||||
* └──────────────────────────────────────────────────────────────────────┘
|
||||
*
|
||||
* ┌─── AUTO-POPULATED (library fills these) ─────────────────────────────┐
|
||||
* │ - dev->mutex - Device mutex handle │
|
||||
* │ - dev->dev_handle - I2C device handle (modern driver) - NEW │
|
||||
* │ - dev->sda_pin - Actual SDA pin used by bus │
|
||||
* │ - dev->scl_pin - Actual SCL pin used by bus │
|
||||
* └──────────────────────────────────────────────────────────────────────┘
|
||||
*
|
||||
* @note BACKWARD COMPATIBILITY DESIGN:
|
||||
* The custom 'cfg' structure mimics ESP-IDF's deprecated i2c_config_t layout
|
||||
* to maintain zero-change compatibility with existing device drivers.
|
||||
* ESP-IDF ≥5.2 deprecated i2c_config_t and split it into separate bus/device
|
||||
* configs, but this library preserves the familiar field paths like:
|
||||
* dev->cfg.sda_io_num, dev->cfg.scl_io_num, dev->cfg.master.clk_speed
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
// ═══ Core Device Identity (REQUIRED) ═══
|
||||
i2c_port_t port; //!< I2C port number (e.g., I2C_NUM_0)
|
||||
uint16_t addr; //!< Device I2C address (e.g., 0x48 for 7-bit)
|
||||
i2c_addr_bit_len_t addr_bit_len; //!< Address format: I2C_ADDR_BIT_LEN_7 (default) or I2C_ADDR_BIT_LEN_10
|
||||
|
||||
// ═══ Library Internal State (AUTO-POPULATED) ═══
|
||||
SemaphoreHandle_t mutex; //!< Device mutex - Created by i2c_dev_create_mutex()
|
||||
void *dev_handle; //!< Device handle - Modern driver only, created lazily (when actual I2C operation is performed)
|
||||
int sda_pin; //!< Actual SDA pin used - Populated after port setup
|
||||
int scl_pin; //!< Actual SCL pin used - Populated after port setup
|
||||
|
||||
// ═══ Legacy Driver Compatibility ═══
|
||||
uint32_t timeout_ticks; //!< Clock stretching timeout - Legacy driver only
|
||||
|
||||
// ═══ User Configuration (REQUIRED) ═══
|
||||
// Configuration structure with i2c_config_t compatible field layout.
|
||||
struct
|
||||
{
|
||||
gpio_num_t sda_io_num; //!< Desired SDA pin (-1 = use Kconfig default)
|
||||
gpio_num_t scl_io_num; //!< Desired SCL pin (-1 = use Kconfig default)
|
||||
uint8_t sda_pullup_en; //!< Enable internal SDA pullup (optional)
|
||||
uint8_t scl_pullup_en; //!< Enable internal SCL pullup (optional)
|
||||
struct
|
||||
{
|
||||
uint32_t clk_speed; //!< Clock speed in Hz
|
||||
} master; //!< Master-specific config (mimics old i2c_config_t.master)
|
||||
} cfg; //!< Configuration set by device drivers (i2c_config_t compatible layout)
|
||||
} i2c_dev_t;
|
||||
|
||||
/**
|
||||
* @brief Initialize I2C subsystem (port mutexes and internal states)
|
||||
*
|
||||
* @note This should be called once at the beginning of your application
|
||||
* before any I2C devices are initialized.
|
||||
*
|
||||
* @return ESP_OK on success
|
||||
*/
|
||||
esp_err_t i2cdev_init(void);
|
||||
|
||||
/**
|
||||
* @brief Release I2C subsystem (deletes all devices, buses, and mutexes)
|
||||
*
|
||||
* @note Call this when no more I2C operations will be performed
|
||||
* to clean up resources.
|
||||
*
|
||||
* @return ESP_OK on success
|
||||
*/
|
||||
esp_err_t i2cdev_done(void);
|
||||
|
||||
/**
|
||||
* @brief Create mutex for device descriptor and register device
|
||||
*
|
||||
* @note IMPORTANT: Before calling this function, you must properly initialize the i2c_dev_t
|
||||
* structure with device address, port, and pin settings. For ESP-IDF legacy driver,
|
||||
* set pins in dev->cfg.sda_io_num and dev->cfg.scl_io_num. For newer ESP-IDF version,
|
||||
* either method is compatible. See the structure documentation for details.
|
||||
*
|
||||
* @param dev Pointer to device descriptor
|
||||
* @return `ESP_OK` on success
|
||||
*/
|
||||
esp_err_t i2c_dev_create_mutex(i2c_dev_t *dev);
|
||||
|
||||
/**
|
||||
* @brief Delete mutex for device descriptor and perform device cleanup
|
||||
*
|
||||
* @note This function performs cleanup tasks including removing the device from the
|
||||
* I2C bus, deregistering it, and deleting its mutex.
|
||||
*
|
||||
* @param dev Pointer to device descriptor
|
||||
* @return `ESP_OK` on success
|
||||
*/
|
||||
esp_err_t i2c_dev_delete_mutex(i2c_dev_t *dev);
|
||||
|
||||
/**
|
||||
* @brief Take device mutex
|
||||
*
|
||||
* @param dev Pointer to device descriptor
|
||||
* @return `ESP_OK` on success
|
||||
*/
|
||||
esp_err_t i2c_dev_take_mutex(i2c_dev_t *dev);
|
||||
|
||||
/**
|
||||
* @brief Give device mutex
|
||||
*
|
||||
* @param dev Pointer to device descriptor
|
||||
* @return `ESP_OK` on success
|
||||
*/
|
||||
esp_err_t i2c_dev_give_mutex(i2c_dev_t *dev);
|
||||
|
||||
/**
|
||||
* @brief Check the availability of a device on the I2C bus (New Driver) - legacy's i2c_dev_probe function equivalent.
|
||||
*
|
||||
* This function attempts to communicate with the I2C device to see if it ACKs.
|
||||
* It is non-intrusive; if the device is found, any temporary setup for
|
||||
* the check is torn down. Uses the new I2C driver logic.
|
||||
*
|
||||
* @param dev Pointer to the device descriptor. Pins and address must be configured.
|
||||
* @return `ESP_OK` if the device ACKs (is present), an error code otherwise.
|
||||
*/
|
||||
esp_err_t i2c_dev_check_present(const i2c_dev_t *dev);
|
||||
|
||||
/**
|
||||
* @brief Check the availability of a device on the I2C bus (Legacy Driver).
|
||||
*
|
||||
* Issue an operation of `operation_type` to the I2C device then stops.
|
||||
* Primarily for use with the legacy i2cdev_legacy.c implementation.
|
||||
*
|
||||
* @param dev Device descriptor.
|
||||
* @param operation_type Operation type (I2C_DEV_WRITE or I2C_DEV_READ).
|
||||
* @return `ESP_OK` if device is available for the specified operation type.
|
||||
*/
|
||||
esp_err_t i2c_dev_probe(const i2c_dev_t *dev, i2c_dev_type_t operation_type);
|
||||
|
||||
/**
|
||||
* @brief Read from device
|
||||
*
|
||||
* @param dev Pointer to device descriptor
|
||||
* @param[in] out_data Data to write before reading (can be NULL if out_size is 0)
|
||||
* @param out_size Size of data to write
|
||||
* @param[out] in_data Buffer to store data read
|
||||
* @param in_size Number of bytes to read
|
||||
* @return `ESP_OK` on success
|
||||
*/
|
||||
esp_err_t i2c_dev_read(const i2c_dev_t *dev, const void *out_data, size_t out_size, void *in_data, size_t in_size);
|
||||
|
||||
/**
|
||||
* @brief Write to device
|
||||
*
|
||||
* @param dev Pointer to device descriptor
|
||||
* @param[in] out_reg Register address to write to (can be NULL if out_reg_size is 0)
|
||||
* @param out_reg_size Size of register address
|
||||
* @param[in] out_data Data to write (can be NULL if out_size is 0)
|
||||
* @param out_size Size of data to write
|
||||
* @return `ESP_OK` on success
|
||||
*/
|
||||
esp_err_t i2c_dev_write(const i2c_dev_t *dev, const void *out_reg, size_t out_reg_size, const void *out_data, size_t out_size);
|
||||
|
||||
/**
|
||||
* @brief Read from device register (8-bit register address)
|
||||
*
|
||||
* @param dev Pointer to device descriptor
|
||||
* @param reg Command to write before reading
|
||||
* @param[out] data Buffer to store data
|
||||
* @param size Number of bytes to read
|
||||
* @return `ESP_OK` on success
|
||||
*/
|
||||
esp_err_t i2c_dev_read_reg(const i2c_dev_t *dev, uint8_t reg, void *data, size_t size);
|
||||
|
||||
/**
|
||||
* @brief Write to device register (8-bit register address)
|
||||
*
|
||||
* @param dev Pointer to device descriptor
|
||||
* @param reg Command to write before writing data
|
||||
* @param data Buffer with data to write
|
||||
* @param size Number of bytes to write
|
||||
* @return `ESP_OK` on success
|
||||
*/
|
||||
esp_err_t i2c_dev_write_reg(const i2c_dev_t *dev, uint8_t reg, const void *data, size_t size);
|
||||
|
||||
/**
|
||||
* @brief Take device mutex with error checking
|
||||
*/
|
||||
#define I2C_DEV_TAKE_MUTEX(dev) \
|
||||
do \
|
||||
{ \
|
||||
esp_err_t __ = i2c_dev_take_mutex(dev); \
|
||||
if (__ != ESP_OK) \
|
||||
return __; \
|
||||
} \
|
||||
while (0)
|
||||
|
||||
/**
|
||||
* @brief Give device mutex with error checking
|
||||
*/
|
||||
#define I2C_DEV_GIVE_MUTEX(dev) \
|
||||
do \
|
||||
{ \
|
||||
esp_err_t __ = i2c_dev_give_mutex(dev); \
|
||||
if (__ != ESP_OK) \
|
||||
return __; \
|
||||
} \
|
||||
while (0)
|
||||
|
||||
/**
|
||||
* @brief Execute operation, assuming mutex is held. Gives mutex ONLY on error.
|
||||
*/
|
||||
#define I2C_DEV_CHECK(dev, X) \
|
||||
do \
|
||||
{ \
|
||||
esp_err_t ___ = X; /* Execute operation */ \
|
||||
if (___ != ESP_OK) \
|
||||
{ \
|
||||
/* Give mutex ONLY if error occurred */ \
|
||||
i2c_dev_give_mutex(dev); \
|
||||
return ___; \
|
||||
} \
|
||||
} \
|
||||
while (0)
|
||||
|
||||
/**
|
||||
* @brief Execute operation, assuming mutex is held. Gives mutex ONLY on error, logs error.
|
||||
*/
|
||||
#define I2C_DEV_CHECK_LOGE(dev, X, msg, ...) \
|
||||
do \
|
||||
{ \
|
||||
esp_err_t ___ = X; /* Execute operation */ \
|
||||
if (___ != ESP_OK) \
|
||||
{ \
|
||||
/* Give mutex ONLY if error occurred */ \
|
||||
i2c_dev_give_mutex(dev); \
|
||||
ESP_LOGE(TAG, msg, ##__VA_ARGS__); \
|
||||
return ___; \
|
||||
} \
|
||||
} \
|
||||
while (0)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
/**@}*/
|
||||
|
||||
esp_err_t i2cdev_adopt_bus(int port, i2c_master_bus_handle_t bus, int sda, int scl);
|
||||
|
||||
#endif /* __I2CDEV_H__ */
|
||||
@@ -160,7 +160,7 @@ bool net_mdns_find_service_url(const char* service, const char* proto,
|
||||
}
|
||||
if (!txt_ok) continue;
|
||||
|
||||
if (r->instance_name && strncmp(r->instance_name, "influxdb2-", 9) != 0) {
|
||||
if (r->instance_name && strncmp(r->instance_name, "myapp-", 5) != 0) {
|
||||
continue; // falsche Instanz, weitersuchen
|
||||
}
|
||||
|
||||
|
||||
@@ -1,689 +0,0 @@
|
||||
/*
|
||||
* THIS FILE IS AUTOMATICALLY GENERATED
|
||||
*
|
||||
* I2C-Generator: 0.3.0
|
||||
* Yaml Version: 2.1.3
|
||||
* Template Version: 0.7.0-109-gb259776
|
||||
*/
|
||||
/*
|
||||
* Copyright (c) 2021, Sensirion AG
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of Sensirion AG nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
// File: src/sen5x_i2c.c
|
||||
#include "sen5x_i2c.h"
|
||||
#include "sensirion_common.h"
|
||||
#include "sensirion_i2c.h"
|
||||
#include "sensirion_i2c_hal.h"
|
||||
|
||||
#define SEN5X_I2C_ADDRESS 0x69
|
||||
|
||||
int16_t sen5x_start_measurement(void) {
|
||||
int16_t error;
|
||||
uint8_t buffer[2];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x21);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
sensirion_i2c_hal_sleep_usec(50000);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sen5x_start_measurement_without_pm(void) {
|
||||
int16_t error;
|
||||
uint8_t buffer[2];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x37);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
sensirion_i2c_hal_sleep_usec(50000);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sen5x_stop_measurement(void) {
|
||||
int16_t error;
|
||||
uint8_t buffer[2];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x104);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
sensirion_i2c_hal_sleep_usec(200000);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sen5x_read_data_ready(bool* data_ready) {
|
||||
int16_t error;
|
||||
uint8_t buffer[3];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x202);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
|
||||
sensirion_i2c_hal_sleep_usec(20000);
|
||||
|
||||
error = sensirion_i2c_read_data_inplace(SEN5X_I2C_ADDRESS, &buffer[0], 2);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
*data_ready = buffer[1];
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sen5x_read_measured_values(uint16_t* mass_concentration_pm1p0,
|
||||
uint16_t* mass_concentration_pm2p5,
|
||||
uint16_t* mass_concentration_pm4p0,
|
||||
uint16_t* mass_concentration_pm10p0,
|
||||
int16_t* ambient_humidity,
|
||||
int16_t* ambient_temperature,
|
||||
int16_t* voc_index, int16_t* nox_index) {
|
||||
int16_t error;
|
||||
uint8_t buffer[24];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x3C4);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
|
||||
sensirion_i2c_hal_sleep_usec(20000);
|
||||
|
||||
error = sensirion_i2c_read_data_inplace(SEN5X_I2C_ADDRESS, &buffer[0], 16);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
*mass_concentration_pm1p0 = sensirion_common_bytes_to_uint16_t(&buffer[0]);
|
||||
*mass_concentration_pm2p5 = sensirion_common_bytes_to_uint16_t(&buffer[2]);
|
||||
*mass_concentration_pm4p0 = sensirion_common_bytes_to_uint16_t(&buffer[4]);
|
||||
*mass_concentration_pm10p0 = sensirion_common_bytes_to_uint16_t(&buffer[6]);
|
||||
*ambient_humidity = sensirion_common_bytes_to_int16_t(&buffer[8]);
|
||||
*ambient_temperature = sensirion_common_bytes_to_int16_t(&buffer[10]);
|
||||
*voc_index = sensirion_common_bytes_to_int16_t(&buffer[12]);
|
||||
*nox_index = sensirion_common_bytes_to_int16_t(&buffer[14]);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sen5x_read_measured_raw_values(int16_t* raw_humidity,
|
||||
int16_t* raw_temperature,
|
||||
uint16_t* raw_voc, uint16_t* raw_nox) {
|
||||
int16_t error;
|
||||
uint8_t buffer[12];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x3D2);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
|
||||
sensirion_i2c_hal_sleep_usec(20000);
|
||||
|
||||
error = sensirion_i2c_read_data_inplace(SEN5X_I2C_ADDRESS, &buffer[0], 8);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
*raw_humidity = sensirion_common_bytes_to_int16_t(&buffer[0]);
|
||||
*raw_temperature = sensirion_common_bytes_to_int16_t(&buffer[2]);
|
||||
*raw_voc = sensirion_common_bytes_to_uint16_t(&buffer[4]);
|
||||
*raw_nox = sensirion_common_bytes_to_uint16_t(&buffer[6]);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sen5x_read_measured_values_sen50(uint16_t* mass_concentration_pm1p0,
|
||||
uint16_t* mass_concentration_pm2p5,
|
||||
uint16_t* mass_concentration_pm4p0,
|
||||
uint16_t* mass_concentration_pm10p0) {
|
||||
int16_t error;
|
||||
|
||||
int16_t ambient_humidity_dummy;
|
||||
int16_t ambient_temperature_dummy;
|
||||
int16_t voc_index_dummy;
|
||||
int16_t nox_index_dummy;
|
||||
|
||||
error = sen5x_read_measured_values(
|
||||
mass_concentration_pm1p0, mass_concentration_pm2p5,
|
||||
mass_concentration_pm4p0, mass_concentration_pm10p0,
|
||||
&ambient_humidity_dummy, &ambient_temperature_dummy, &voc_index_dummy,
|
||||
&nox_index_dummy);
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
int16_t sen5x_read_measured_pm_values(
|
||||
uint16_t* mass_concentration_pm1p0, uint16_t* mass_concentration_pm2p5,
|
||||
uint16_t* mass_concentration_pm4p0, uint16_t* mass_concentration_pm10p0,
|
||||
uint16_t* number_concentration_pm0p5, uint16_t* number_concentration_pm1p0,
|
||||
uint16_t* number_concentration_pm2p5, uint16_t* number_concentration_pm4p0,
|
||||
uint16_t* number_concentration_pm10p0, uint16_t* typical_particle_size) {
|
||||
int16_t error;
|
||||
uint8_t buffer[30];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x413);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
|
||||
sensirion_i2c_hal_sleep_usec(20000);
|
||||
|
||||
error = sensirion_i2c_read_data_inplace(SEN5X_I2C_ADDRESS, &buffer[0], 20);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
*mass_concentration_pm1p0 = sensirion_common_bytes_to_uint16_t(&buffer[0]);
|
||||
*mass_concentration_pm2p5 = sensirion_common_bytes_to_uint16_t(&buffer[2]);
|
||||
*mass_concentration_pm4p0 = sensirion_common_bytes_to_uint16_t(&buffer[4]);
|
||||
*mass_concentration_pm10p0 = sensirion_common_bytes_to_uint16_t(&buffer[6]);
|
||||
*number_concentration_pm0p5 =
|
||||
sensirion_common_bytes_to_uint16_t(&buffer[8]);
|
||||
*number_concentration_pm1p0 =
|
||||
sensirion_common_bytes_to_uint16_t(&buffer[10]);
|
||||
*number_concentration_pm2p5 =
|
||||
sensirion_common_bytes_to_uint16_t(&buffer[12]);
|
||||
*number_concentration_pm4p0 =
|
||||
sensirion_common_bytes_to_uint16_t(&buffer[14]);
|
||||
*number_concentration_pm10p0 =
|
||||
sensirion_common_bytes_to_uint16_t(&buffer[16]);
|
||||
*typical_particle_size = sensirion_common_bytes_to_uint16_t(&buffer[18]);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sen5x_start_fan_cleaning(void) {
|
||||
int16_t error;
|
||||
uint8_t buffer[2];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x5607);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
sensirion_i2c_hal_sleep_usec(20000);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sen5x_set_temperature_offset_parameters(int16_t temp_offset,
|
||||
int16_t slope,
|
||||
uint16_t time_constant) {
|
||||
int16_t error;
|
||||
uint8_t buffer[11];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x60B2);
|
||||
|
||||
offset =
|
||||
sensirion_i2c_add_int16_t_to_buffer(&buffer[0], offset, temp_offset);
|
||||
offset = sensirion_i2c_add_int16_t_to_buffer(&buffer[0], offset, slope);
|
||||
offset =
|
||||
sensirion_i2c_add_uint16_t_to_buffer(&buffer[0], offset, time_constant);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
sensirion_i2c_hal_sleep_usec(20000);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sen5x_get_temperature_offset_parameters(int16_t* temp_offset,
|
||||
int16_t* slope,
|
||||
uint16_t* time_constant) {
|
||||
int16_t error;
|
||||
uint8_t buffer[9];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x60B2);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
|
||||
sensirion_i2c_hal_sleep_usec(20000);
|
||||
|
||||
error = sensirion_i2c_read_data_inplace(SEN5X_I2C_ADDRESS, &buffer[0], 6);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
*temp_offset = sensirion_common_bytes_to_int16_t(&buffer[0]);
|
||||
*slope = sensirion_common_bytes_to_int16_t(&buffer[2]);
|
||||
*time_constant = sensirion_common_bytes_to_uint16_t(&buffer[4]);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sen5x_set_warm_start_parameter(uint16_t warm_start) {
|
||||
int16_t error;
|
||||
uint8_t buffer[5];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x60C6);
|
||||
|
||||
offset =
|
||||
sensirion_i2c_add_uint16_t_to_buffer(&buffer[0], offset, warm_start);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
sensirion_i2c_hal_sleep_usec(20000);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sen5x_get_warm_start_parameter(uint16_t* warm_start) {
|
||||
int16_t error;
|
||||
uint8_t buffer[3];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x60C6);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
|
||||
sensirion_i2c_hal_sleep_usec(20000);
|
||||
|
||||
error = sensirion_i2c_read_data_inplace(SEN5X_I2C_ADDRESS, &buffer[0], 2);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
*warm_start = sensirion_common_bytes_to_uint16_t(&buffer[0]);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sen5x_set_voc_algorithm_tuning_parameters(
|
||||
int16_t index_offset, int16_t learning_time_offset_hours,
|
||||
int16_t learning_time_gain_hours, int16_t gating_max_duration_minutes,
|
||||
int16_t std_initial, int16_t gain_factor) {
|
||||
int16_t error;
|
||||
uint8_t buffer[20];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x60D0);
|
||||
|
||||
offset =
|
||||
sensirion_i2c_add_int16_t_to_buffer(&buffer[0], offset, index_offset);
|
||||
offset = sensirion_i2c_add_int16_t_to_buffer(&buffer[0], offset,
|
||||
learning_time_offset_hours);
|
||||
offset = sensirion_i2c_add_int16_t_to_buffer(&buffer[0], offset,
|
||||
learning_time_gain_hours);
|
||||
offset = sensirion_i2c_add_int16_t_to_buffer(&buffer[0], offset,
|
||||
gating_max_duration_minutes);
|
||||
offset =
|
||||
sensirion_i2c_add_int16_t_to_buffer(&buffer[0], offset, std_initial);
|
||||
offset =
|
||||
sensirion_i2c_add_int16_t_to_buffer(&buffer[0], offset, gain_factor);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
sensirion_i2c_hal_sleep_usec(20000);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sen5x_get_voc_algorithm_tuning_parameters(
|
||||
int16_t* index_offset, int16_t* learning_time_offset_hours,
|
||||
int16_t* learning_time_gain_hours, int16_t* gating_max_duration_minutes,
|
||||
int16_t* std_initial, int16_t* gain_factor) {
|
||||
int16_t error;
|
||||
uint8_t buffer[18];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x60D0);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
|
||||
sensirion_i2c_hal_sleep_usec(20000);
|
||||
|
||||
error = sensirion_i2c_read_data_inplace(SEN5X_I2C_ADDRESS, &buffer[0], 12);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
*index_offset = sensirion_common_bytes_to_int16_t(&buffer[0]);
|
||||
*learning_time_offset_hours = sensirion_common_bytes_to_int16_t(&buffer[2]);
|
||||
*learning_time_gain_hours = sensirion_common_bytes_to_int16_t(&buffer[4]);
|
||||
*gating_max_duration_minutes =
|
||||
sensirion_common_bytes_to_int16_t(&buffer[6]);
|
||||
*std_initial = sensirion_common_bytes_to_int16_t(&buffer[8]);
|
||||
*gain_factor = sensirion_common_bytes_to_int16_t(&buffer[10]);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sen5x_set_nox_algorithm_tuning_parameters(
|
||||
int16_t index_offset, int16_t learning_time_offset_hours,
|
||||
int16_t learning_time_gain_hours, int16_t gating_max_duration_minutes,
|
||||
int16_t std_initial, int16_t gain_factor) {
|
||||
int16_t error;
|
||||
uint8_t buffer[20];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x60E1);
|
||||
|
||||
offset =
|
||||
sensirion_i2c_add_int16_t_to_buffer(&buffer[0], offset, index_offset);
|
||||
offset = sensirion_i2c_add_int16_t_to_buffer(&buffer[0], offset,
|
||||
learning_time_offset_hours);
|
||||
offset = sensirion_i2c_add_int16_t_to_buffer(&buffer[0], offset,
|
||||
learning_time_gain_hours);
|
||||
offset = sensirion_i2c_add_int16_t_to_buffer(&buffer[0], offset,
|
||||
gating_max_duration_minutes);
|
||||
offset =
|
||||
sensirion_i2c_add_int16_t_to_buffer(&buffer[0], offset, std_initial);
|
||||
offset =
|
||||
sensirion_i2c_add_int16_t_to_buffer(&buffer[0], offset, gain_factor);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
sensirion_i2c_hal_sleep_usec(20000);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sen5x_get_nox_algorithm_tuning_parameters(
|
||||
int16_t* index_offset, int16_t* learning_time_offset_hours,
|
||||
int16_t* learning_time_gain_hours, int16_t* gating_max_duration_minutes,
|
||||
int16_t* std_initial, int16_t* gain_factor) {
|
||||
int16_t error;
|
||||
uint8_t buffer[18];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x60E1);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
|
||||
sensirion_i2c_hal_sleep_usec(20000);
|
||||
|
||||
error = sensirion_i2c_read_data_inplace(SEN5X_I2C_ADDRESS, &buffer[0], 12);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
*index_offset = sensirion_common_bytes_to_int16_t(&buffer[0]);
|
||||
*learning_time_offset_hours = sensirion_common_bytes_to_int16_t(&buffer[2]);
|
||||
*learning_time_gain_hours = sensirion_common_bytes_to_int16_t(&buffer[4]);
|
||||
*gating_max_duration_minutes =
|
||||
sensirion_common_bytes_to_int16_t(&buffer[6]);
|
||||
*std_initial = sensirion_common_bytes_to_int16_t(&buffer[8]);
|
||||
*gain_factor = sensirion_common_bytes_to_int16_t(&buffer[10]);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sen5x_set_rht_acceleration_mode(uint16_t mode) {
|
||||
int16_t error;
|
||||
uint8_t buffer[5];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x60F7);
|
||||
|
||||
offset = sensirion_i2c_add_uint16_t_to_buffer(&buffer[0], offset, mode);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
sensirion_i2c_hal_sleep_usec(20000);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sen5x_get_rht_acceleration_mode(uint16_t* mode) {
|
||||
int16_t error;
|
||||
uint8_t buffer[3];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x60F7);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
|
||||
sensirion_i2c_hal_sleep_usec(20000);
|
||||
|
||||
error = sensirion_i2c_read_data_inplace(SEN5X_I2C_ADDRESS, &buffer[0], 2);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
*mode = sensirion_common_bytes_to_uint16_t(&buffer[0]);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sen5x_set_voc_algorithm_state(const uint8_t* state,
|
||||
uint8_t state_size) {
|
||||
int16_t error;
|
||||
uint8_t buffer[14];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x6181);
|
||||
|
||||
offset = sensirion_i2c_add_bytes_to_buffer(&buffer[0], offset, state,
|
||||
state_size);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
sensirion_i2c_hal_sleep_usec(20000);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sen5x_get_voc_algorithm_state(uint8_t* state, uint8_t state_size) {
|
||||
int16_t error;
|
||||
uint8_t buffer[12];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x6181);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
|
||||
sensirion_i2c_hal_sleep_usec(20000);
|
||||
|
||||
error = sensirion_i2c_read_data_inplace(SEN5X_I2C_ADDRESS, &buffer[0], 8);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
sensirion_common_copy_bytes(&buffer[0], state, state_size);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sen5x_set_fan_auto_cleaning_interval(uint32_t interval) {
|
||||
int16_t error;
|
||||
uint8_t buffer[8];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x8004);
|
||||
|
||||
offset = sensirion_i2c_add_uint32_t_to_buffer(&buffer[0], offset, interval);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
sensirion_i2c_hal_sleep_usec(20000);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sen5x_get_fan_auto_cleaning_interval(uint32_t* interval) {
|
||||
int16_t error;
|
||||
uint8_t buffer[6];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x8004);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
|
||||
sensirion_i2c_hal_sleep_usec(20000);
|
||||
|
||||
error = sensirion_i2c_read_data_inplace(SEN5X_I2C_ADDRESS, &buffer[0], 4);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
*interval = sensirion_common_bytes_to_uint32_t(&buffer[0]);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sen5x_get_product_name(unsigned char* product_name,
|
||||
uint8_t product_name_size) {
|
||||
int16_t error;
|
||||
uint8_t buffer[48];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0xD014);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
|
||||
sensirion_i2c_hal_sleep_usec(50000);
|
||||
|
||||
error = sensirion_i2c_read_data_inplace(SEN5X_I2C_ADDRESS, &buffer[0], 32);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
sensirion_common_copy_bytes(&buffer[0], product_name, product_name_size);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sen5x_get_serial_number(unsigned char* serial_number,
|
||||
uint8_t serial_number_size) {
|
||||
int16_t error;
|
||||
uint8_t buffer[48];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0xD033);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
|
||||
sensirion_i2c_hal_sleep_usec(50000);
|
||||
|
||||
error = sensirion_i2c_read_data_inplace(SEN5X_I2C_ADDRESS, &buffer[0], 32);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
sensirion_common_copy_bytes(&buffer[0], serial_number, serial_number_size);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sen5x_get_version(uint8_t* firmware_major, uint8_t* firmware_minor,
|
||||
bool* firmware_debug, uint8_t* hardware_major,
|
||||
uint8_t* hardware_minor, uint8_t* protocol_major,
|
||||
uint8_t* protocol_minor) {
|
||||
int16_t error;
|
||||
uint8_t buffer[12];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0xD100);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
|
||||
sensirion_i2c_hal_sleep_usec(20000);
|
||||
|
||||
error = sensirion_i2c_read_data_inplace(SEN5X_I2C_ADDRESS, &buffer[0], 8);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
*firmware_major = buffer[0];
|
||||
*firmware_minor = buffer[1];
|
||||
*firmware_debug = buffer[2];
|
||||
*hardware_major = buffer[3];
|
||||
*hardware_minor = buffer[4];
|
||||
*protocol_major = buffer[5];
|
||||
*protocol_minor = buffer[6];
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sen5x_read_device_status(uint32_t* device_status) {
|
||||
int16_t error;
|
||||
uint8_t buffer[6];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0xD206);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
|
||||
sensirion_i2c_hal_sleep_usec(20000);
|
||||
|
||||
error = sensirion_i2c_read_data_inplace(SEN5X_I2C_ADDRESS, &buffer[0], 4);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
*device_status = sensirion_common_bytes_to_uint32_t(&buffer[0]);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sen5x_read_and_clear_device_status(uint32_t* device_status) {
|
||||
int16_t error;
|
||||
uint8_t buffer[6];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0xD210);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
|
||||
sensirion_i2c_hal_sleep_usec(20000);
|
||||
|
||||
error = sensirion_i2c_read_data_inplace(SEN5X_I2C_ADDRESS, &buffer[0], 4);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
*device_status = sensirion_common_bytes_to_uint32_t(&buffer[0]);
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sen5x_device_reset(void) {
|
||||
int16_t error;
|
||||
uint8_t buffer[2];
|
||||
uint16_t offset = 0;
|
||||
offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0xD304);
|
||||
|
||||
error = sensirion_i2c_write_data(SEN5X_I2C_ADDRESS, &buffer[0], offset);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
sensirion_i2c_hal_sleep_usec(200000);
|
||||
return NO_ERROR;
|
||||
}
|
||||
@@ -1,721 +0,0 @@
|
||||
/*
|
||||
* THIS FILE IS AUTOMATICALLY GENERATED
|
||||
*
|
||||
* I2C-Generator: 0.3.0
|
||||
* Yaml Version: 2.1.3
|
||||
* Template Version: 0.7.0-109-gb259776
|
||||
*/
|
||||
/*
|
||||
* Copyright (c) 2021, Sensirion AG
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of Sensirion AG nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef SEN5X_I2C_H
|
||||
#define SEN5X_I2C_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "sensirion_config.h"
|
||||
|
||||
/**
|
||||
* sen5x_start_measurement() - Starts a continuous measurement.
|
||||
*
|
||||
* After starting the measurement, it takes some time (~1s) until the first
|
||||
* measurement results are available. You could poll with the command
|
||||
* 0x0202 \"Read Data Ready\" to check when the results are ready to read.
|
||||
*
|
||||
* If the device is in measure mode without particulate matter (low-power)
|
||||
* and the firmware version is at least 2.0, this command enables PM
|
||||
* measurement without affecting the already running RH/T/VOC/NOx measurements
|
||||
* (except that the \"data ready\"-flag will be cleared). In previous firmware
|
||||
* versions, this command is supported only in idle mode.
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_start_measurement(void);
|
||||
|
||||
/**
|
||||
* sen5x_start_measurement_without_pm() - Starts a continuous measurement
|
||||
* without PM. Only humidity, temperature, VOC and NOx are available in this
|
||||
* mode. Laser and fan are switched off to keep power consumption low.
|
||||
*
|
||||
* After starting the measurement, it takes some time (~1s) until the first
|
||||
* measurement results are available. You could poll with the command
|
||||
* 0x0202 \"Read Data Ready\" to check when the results are ready to read.
|
||||
*
|
||||
* If the device is in measure mode with particulate matter (normal measure
|
||||
* mode) and the firmware version is at least 2.0, this command disables PM
|
||||
* measurement without affecting the already running RH/T/VOC/NOx measurements
|
||||
* (except that the \"data ready\"-flag will be cleared). In previous firmware
|
||||
* versions, this command is supported only in idle mode.
|
||||
*
|
||||
* Supported sensors: SEN54, SEN55
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_start_measurement_without_pm(void);
|
||||
|
||||
/**
|
||||
* sen5x_stop_measurement() - Stops the measurement and returns to idle mode.
|
||||
*
|
||||
* If the device is already in idle mode, this command has no effect.
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_stop_measurement(void);
|
||||
|
||||
/**
|
||||
* sen5x_read_data_ready() - This command can be used to check if new
|
||||
* measurement results are ready to read. The data ready flag is automatically
|
||||
* reset after reading the measurement values with the 0x03.. \"Read Measured
|
||||
* Values\" commands.
|
||||
*
|
||||
* @note During fan (auto-)cleaning, no measurement data is available for
|
||||
* several seconds and thus this flag will not be set until cleaning has
|
||||
* finished. So please expect gaps of several seconds at any time if fan
|
||||
* auto-cleaning is enabled.
|
||||
*
|
||||
* @param padding Padding byte, always 0x00.
|
||||
*
|
||||
* @param data_ready True (0x01) if data is ready, False (0x00) if not. When no
|
||||
* measurement is running, False will be returned.
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_read_data_ready(bool* data_ready);
|
||||
|
||||
/**
|
||||
* sen5x_read_measured_values() - Returns the measured values.
|
||||
*
|
||||
* The command 0x0202 \"Read Data Ready\" can be used to check if new
|
||||
* data is available since the last read operation. If no new data is
|
||||
* available, the previous values will be returned again. If no data is
|
||||
* available at all (e.g. measurement not running for at least one
|
||||
* second), all values will be at their upper limit (0xFFFF for `uint16`,
|
||||
* 0x7FFF for `int16`).
|
||||
*
|
||||
* @param mass_concentration_pm1p0 Value is scaled with factor 10:
|
||||
* PM1.0 [µg/m³] = value / 10
|
||||
* @note If this value is unknown, 0xFFFF is returned.
|
||||
*
|
||||
* @param mass_concentration_pm2p5 Value is scaled with factor 10:
|
||||
* PM2.5 [µg/m³] = value / 10
|
||||
* @note If this value is unknown, 0xFFFF is returned.
|
||||
*
|
||||
* @param mass_concentration_pm4p0 Value is scaled with factor 10:
|
||||
* PM4.0 [µg/m³] = value / 10
|
||||
* @note If this value is unknown, 0xFFFF is returned.
|
||||
*
|
||||
* @param mass_concentration_pm10p0 Value is scaled with factor 10:
|
||||
* PM10.0 [µg/m³] = value / 10
|
||||
* @note If this value is unknown, 0xFFFF is returned.
|
||||
*
|
||||
* @param ambient_humidity Value is scaled with factor 100: RH [%] = value / 100
|
||||
* @note If this value is unknown, 0x7FFF is returned.*
|
||||
*
|
||||
* @param ambient_temperature Value is scaled with factor 200:
|
||||
* T [°C] = value / 200
|
||||
* @note If this value is unknown, 0x7FFF is returned.*
|
||||
*
|
||||
* @param voc_index Value is scaled with factor 10: VOC Index = value / 10
|
||||
* @note If this value is unknown, 0x7FFF is returned.*
|
||||
*
|
||||
* @param nox_index Value is scaled with factor 10: NOx Index = value / 10
|
||||
* @note If this value is unknown, 0x7FFF is returned. During
|
||||
* the first 10..11 seconds after power-on or device reset, this
|
||||
* value will be 0x7FFF as well.*
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_read_measured_values(uint16_t* mass_concentration_pm1p0,
|
||||
uint16_t* mass_concentration_pm2p5,
|
||||
uint16_t* mass_concentration_pm4p0,
|
||||
uint16_t* mass_concentration_pm10p0,
|
||||
int16_t* ambient_humidity,
|
||||
int16_t* ambient_temperature,
|
||||
int16_t* voc_index, int16_t* nox_index);
|
||||
|
||||
/**
|
||||
* sen5x_read_measured_raw_values() - Returns the measured raw values.
|
||||
|
||||
The command 0x0202 \"Read Data Ready\" can be used to check if new
|
||||
data is available since the last read operation. If no new data is
|
||||
available, the previous values will be returned again. If no data
|
||||
is available at all (e.g. measurement not running for at least one
|
||||
second), all values will be at their upper limit (0xFFFF for `uint16`,
|
||||
0x7FFF for `int16`).
|
||||
*
|
||||
* @param raw_humidity Value is scaled with factor 100: RH [%] = value / 100
|
||||
* @note If this value is unknown, 0x7FFF is returned.
|
||||
*
|
||||
* @param raw_temperature Value is scaled with factor 200: T [°C] = value / 200
|
||||
* @note If this value is unknown, 0x7FFF is returned.
|
||||
*
|
||||
* @param raw_voc Raw measured VOC ticks without scale factor.
|
||||
* @note If this value is unknown, 0xFFFF is returned.
|
||||
*
|
||||
* @param raw_nox Raw measured NOx ticks without scale factor.
|
||||
* @note If this value is unknown, 0xFFFF is returned. During
|
||||
* the first 10..11 seconds after power-on or device reset, this
|
||||
* value will be 0xFFFF as well.*
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_read_measured_raw_values(int16_t* raw_humidity,
|
||||
int16_t* raw_temperature,
|
||||
uint16_t* raw_voc, uint16_t* raw_nox);
|
||||
|
||||
/**
|
||||
* sen5x_read_measured_values_sen50() - Returns the measured values for SEN50.
|
||||
*
|
||||
* The command 0x0202 \"Read Data Ready\" can be used to check if new
|
||||
* data is available since the last read operation. If no new data is
|
||||
* available, the previous values will be returned again. If no data is
|
||||
* available at all (e.g. measurement not running for at least one
|
||||
* second), all values will be at their upper limit (0xFFFF).
|
||||
*
|
||||
* @param mass_concentration_pm1p0 Value is scaled with factor 10:
|
||||
* PM1.0 [µg/m³] = value / 10
|
||||
* @note If this value is unknown, 0xFFFF is returned.
|
||||
*
|
||||
* @param mass_concentration_pm2p5 Value is scaled with factor 10:
|
||||
* PM2.5 [µg/m³] = value / 10
|
||||
* @note If this value is unknown, 0xFFFF is returned.
|
||||
*
|
||||
* @param mass_concentration_pm4p0 Value is scaled with factor 10:
|
||||
* PM4.0 [µg/m³] = value / 10
|
||||
* @note If this value is unknown, 0xFFFF is returned.
|
||||
*
|
||||
* @param mass_concentration_pm10p0 Value is scaled with factor 10:
|
||||
* PM10.0 [µg/m³] = value / 10
|
||||
* @note If this value is unknown, 0xFFFF is returned.
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_read_measured_values_sen50(uint16_t* mass_concentration_pm1p0,
|
||||
uint16_t* mass_concentration_pm2p5,
|
||||
uint16_t* mass_concentration_pm4p0,
|
||||
uint16_t* mass_concentration_pm10p0);
|
||||
|
||||
/**
|
||||
* sen5x_read_measured_pm_values() - Returns the measured particulate matter
|
||||
* values.
|
||||
*
|
||||
* The command 0x0202 \"Read Data Ready\" can be used to check if new
|
||||
* data is available since the last read operation. If no new data is
|
||||
* available, the previous values will be returned again. If no data
|
||||
* is available at all (e.g. measurement not running for at least one
|
||||
* second), all values will be 0xFFFF.
|
||||
*
|
||||
* @param mass_concentration_pm1p0 Value is scaled with factor 10:
|
||||
* PM1.0 [µg/m³] = value / 10
|
||||
* @note If this value is unknown, 0xFFFF is returned.
|
||||
*
|
||||
* @param mass_concentration_pm2p5 Value is scaled with factor 10:
|
||||
* PM2.5 [µg/m³] = value / 10
|
||||
* @note If this value is unknown, 0xFFFF is returned.
|
||||
*
|
||||
* @param mass_concentration_pm4p0 Value is scaled with factor 10:
|
||||
* PM4.0 [µg/m³] = value / 10
|
||||
* @note If this value is unknown, 0xFFFF is returned.
|
||||
*
|
||||
* @param mass_concentration_pm10p0 Value is scaled with factor 10:
|
||||
* PM10.0 [µg/m³] = value / 10
|
||||
* @note If this value is unknown, 0xFFFF is returned.
|
||||
*
|
||||
* @param number_concentration_pm0p5 Value is scaled with factor 10:
|
||||
* PM0.5 [#/cm³] = value / 10
|
||||
* @note If this value is unknown, 0xFFFF is returned.
|
||||
*
|
||||
* @param number_concentration_pm1p0 Value is scaled with factor 10:
|
||||
* PM1.0 [#/cm³] = value / 10
|
||||
* @note If this value is unknown, 0xFFFF is returned.
|
||||
*
|
||||
* @param number_concentration_pm2p5 Value is scaled with factor 10:
|
||||
* PM2.5 [#/cm³] = value / 10
|
||||
* @note If this value is unknown, 0xFFFF is returned.
|
||||
*
|
||||
* @param number_concentration_pm4p0 Value is scaled with factor 10:
|
||||
* PM4.0 [#/cm³] = value / 10
|
||||
* @note If this value is unknown, 0xFFFF is returned.
|
||||
*
|
||||
* @param number_concentration_pm10p0 Value is scaled with factor 10:
|
||||
* PM10.0 [#/cm³] = value / 10
|
||||
* @note If this value is unknown, 0xFFFF is returned.
|
||||
*
|
||||
* @param typical_particle_size Value is scaled with factor 1000:
|
||||
* Size [µm] = value / 1000
|
||||
* @note If this value is unknown, 0xFFFF is returned.
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_read_measured_pm_values(
|
||||
uint16_t* mass_concentration_pm1p0, uint16_t* mass_concentration_pm2p5,
|
||||
uint16_t* mass_concentration_pm4p0, uint16_t* mass_concentration_pm10p0,
|
||||
uint16_t* number_concentration_pm0p5, uint16_t* number_concentration_pm1p0,
|
||||
uint16_t* number_concentration_pm2p5, uint16_t* number_concentration_pm4p0,
|
||||
uint16_t* number_concentration_pm10p0, uint16_t* typical_particle_size);
|
||||
|
||||
/**
|
||||
* sen5x_start_fan_cleaning() - Starts the fan cleaning manually. The \"data
|
||||
* ready\"-flag will be cleared immediately and during the next few seconds, no
|
||||
* new measurement results will be available (old values will be returned). Once
|
||||
* the cleaning is finished, the \"data ready\"-flag will be set and new
|
||||
* measurement results will be available.
|
||||
*
|
||||
* When executing this command while cleaning is already active, the
|
||||
* command does nothing.
|
||||
*
|
||||
* If you stop the measurement while fan cleaning is active, the cleaning
|
||||
* will be aborted immediately.
|
||||
*
|
||||
* @note This command is only available in measure mode with PM measurement
|
||||
* enabled, i.e. only if the fan is already running. In any other state, this
|
||||
* command does nothing.
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_start_fan_cleaning(void);
|
||||
|
||||
/**
|
||||
* sen5x_set_temperature_offset_parameters() - Sets the temperature offset
|
||||
* parameters for the device.
|
||||
*
|
||||
* Supported sensors: SEN54, SEN55
|
||||
*
|
||||
* @param temp_offset Constant temperature offset scaled with factor 200 (T [°C]
|
||||
* = value / 200). The default value is 0.
|
||||
*
|
||||
* @param slope Normalized temperature offset slope scaled with factor 10000
|
||||
* (applied factor = value / 10000). The default value is 0.
|
||||
*
|
||||
* @param time_constant Time constant [s] how fast the new slope and offset will
|
||||
* be applied. After the specified value in seconds, 63% of the new slope and
|
||||
* offset are applied. A time constant of zero means the new values will be
|
||||
* applied immediately (within the next measure interval of 1 second).
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_set_temperature_offset_parameters(int16_t temp_offset,
|
||||
int16_t slope,
|
||||
uint16_t time_constant);
|
||||
|
||||
/**
|
||||
* sen5x_get_temperature_offset_parameters() - Gets the temperature offset
|
||||
* parameters from the device.
|
||||
*
|
||||
* Supported sensors: SEN54, SEN55
|
||||
*
|
||||
* @param temp_offset Constant temperature offset scaled with factor 200 (T [°C]
|
||||
* = value / 200).
|
||||
*
|
||||
* @param slope Normalized temperature offset slope scaled with factor 10000
|
||||
* (applied factor = value / 10000).
|
||||
*
|
||||
* @param time_constant Time constant [s] how fast the slope and offset are
|
||||
* applied. After the specified value in seconds, 63% of the new slope and
|
||||
* offset are applied.
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_get_temperature_offset_parameters(int16_t* temp_offset,
|
||||
int16_t* slope,
|
||||
uint16_t* time_constant);
|
||||
|
||||
/**
|
||||
* sen5x_set_warm_start_parameter() - Sets the warm start parameter for the
|
||||
* device.
|
||||
*
|
||||
* Supported sensors: SEN54, SEN55
|
||||
*
|
||||
* @note This parameter can be changed in any state of the device (and the
|
||||
* getter immediately returns the new value), but it is applied only the next
|
||||
* time starting a measurement, i.e. when sending a \"Start Measurement\"
|
||||
* command! So the parameter needs to be set *before* a warm-start measurement
|
||||
* is started.
|
||||
*
|
||||
* @param warm_start Warm start behavior as a value in the range from 0 (cold
|
||||
* start) to 65535 (warm start). The default value is 0.
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_set_warm_start_parameter(uint16_t warm_start);
|
||||
|
||||
/**
|
||||
* sen5x_get_warm_start_parameter() - Gets the warm start parameter from the
|
||||
* device.
|
||||
*
|
||||
* Supported sensors: SEN54, SEN55
|
||||
*
|
||||
* @param warm_start Warm start behavior as a value in the range from 0 (cold
|
||||
* start) to 65535 (warm start).
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_get_warm_start_parameter(uint16_t* warm_start);
|
||||
|
||||
/**
|
||||
* sen5x_set_voc_algorithm_tuning_parameters() - Sets the tuning parameters of
|
||||
* the VOC algorithm.
|
||||
*
|
||||
* Supported sensors: SEN54, SEN55
|
||||
*
|
||||
* @note This command is available only in idle mode. In measure mode, this
|
||||
* command has no effect. In addition, it has no effect if at least one
|
||||
* parameter is outside the specified range.
|
||||
*
|
||||
* @param index_offset VOC index representing typical (average) conditions.
|
||||
* Allowed values are in range 1..250. The default value is 100.
|
||||
*
|
||||
* @param learning_time_offset_hours Time constant to estimate the VOC algorithm
|
||||
* offset from the history in hours. Past events will be forgotten after about
|
||||
* twice the learning time. Allowed values are in range 1..1000. The default
|
||||
* value is 12 hours.
|
||||
*
|
||||
* @param learning_time_gain_hours Time constant to estimate the VOC algorithm
|
||||
* gain from the history in hours. Past events will be forgotten after about
|
||||
* twice the learning time. Allowed values are in range 1..1000. The default
|
||||
* value is 12 hours.
|
||||
*
|
||||
* @param gating_max_duration_minutes Maximum duration of gating in minutes
|
||||
* (freeze of estimator during high VOC index signal). Set to zero to disable
|
||||
* the gating. Allowed values are in range 0..3000. The default value is 180
|
||||
* minutes.
|
||||
*
|
||||
* @param std_initial Initial estimate for standard deviation. Lower value
|
||||
* boosts events during initial learning period, but may result in larger
|
||||
* device-to-device variations. Allowed values are in range 10..5000. The
|
||||
* default value is 50.
|
||||
*
|
||||
* @param gain_factor Gain factor to amplify or to attenuate the VOC index
|
||||
* output. Allowed values are in range 1..1000. The default value is 230.
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_set_voc_algorithm_tuning_parameters(
|
||||
int16_t index_offset, int16_t learning_time_offset_hours,
|
||||
int16_t learning_time_gain_hours, int16_t gating_max_duration_minutes,
|
||||
int16_t std_initial, int16_t gain_factor);
|
||||
|
||||
/**
|
||||
* sen5x_get_voc_algorithm_tuning_parameters() - Gets the currently set tuning
|
||||
* parameters of the VOC algorithm.
|
||||
*
|
||||
* Supported sensors: SEN54, SEN55
|
||||
*
|
||||
* @param index_offset VOC index representing typical (average) conditions.
|
||||
*
|
||||
* @param learning_time_offset_hours Time constant to estimate the VOC algorithm
|
||||
* offset from the history in hours. Past events will be forgotten after about
|
||||
* twice the learning time.
|
||||
*
|
||||
* @param learning_time_gain_hours Time constant to estimate the VOC algorithm
|
||||
* gain from the history in hours. Past events will be forgotten after about
|
||||
* twice the learning time.
|
||||
*
|
||||
* @param gating_max_duration_minutes Maximum duration of gating in minutes
|
||||
* (freeze of estimator during high VOC index signal). Zero disables the gating.
|
||||
*
|
||||
* @param std_initial Initial estimate for standard deviation. Lower value
|
||||
* boosts events during initial learning period, but may result in larger
|
||||
* device-to-device variations.
|
||||
*
|
||||
* @param gain_factor Gain factor to amplify or to attenuate the VOC index
|
||||
* output.
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_get_voc_algorithm_tuning_parameters(
|
||||
int16_t* index_offset, int16_t* learning_time_offset_hours,
|
||||
int16_t* learning_time_gain_hours, int16_t* gating_max_duration_minutes,
|
||||
int16_t* std_initial, int16_t* gain_factor);
|
||||
|
||||
/**
|
||||
* sen5x_set_nox_algorithm_tuning_parameters() - Sets the tuning parameters of
|
||||
* the NOx algorithm.
|
||||
*
|
||||
* Supported sensors: SEN55
|
||||
*
|
||||
* @note This command is available only in idle mode. In measure mode, this
|
||||
* command has no effect. In addition, it has no effect if at least one
|
||||
* parameter is outside the specified range.
|
||||
*
|
||||
* @param index_offset NOx index representing typical (average) conditions.
|
||||
* Allowed values are in range 1..250. The default value is 1.
|
||||
*
|
||||
* @param learning_time_offset_hours Time constant to estimate the NOx algorithm
|
||||
* offset from the history in hours. Past events will be forgotten after about
|
||||
* twice the learning time. Allowed values are in range 1..1000. The default
|
||||
* value is 12 hours.
|
||||
*
|
||||
* @param learning_time_gain_hours The time constant to estimate the NOx
|
||||
* algorithm gain from the history has no impact for NOx. This parameter is
|
||||
* still in place for consistency reasons with the VOC tuning parameters
|
||||
* command. This parameter must always be set to 12 hours.
|
||||
*
|
||||
* @param gating_max_duration_minutes Maximum duration of gating in minutes
|
||||
* (freeze of estimator during high NOx index signal). Set to zero to disable
|
||||
* the gating. Allowed values are in range 0..3000. The default value is 720
|
||||
* minutes.
|
||||
*
|
||||
* @param std_initial The initial estimate for standard deviation parameter has
|
||||
* no impact for NOx. This parameter is still in place for consistency reasons
|
||||
* with the VOC tuning parameters command. This parameter must always be set
|
||||
* to 50.
|
||||
*
|
||||
* @param gain_factor Gain factor to amplify or to attenuate the NOx index
|
||||
* output. Allowed values are in range 1..1000. The default value is 230.
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_set_nox_algorithm_tuning_parameters(
|
||||
int16_t index_offset, int16_t learning_time_offset_hours,
|
||||
int16_t learning_time_gain_hours, int16_t gating_max_duration_minutes,
|
||||
int16_t std_initial, int16_t gain_factor);
|
||||
|
||||
/**
|
||||
* sen5x_get_nox_algorithm_tuning_parameters() - Gets the currently set tuning
|
||||
* parameters of the NOx algorithm.
|
||||
*
|
||||
* Supported sensors: SEN55
|
||||
*
|
||||
* @param index_offset NOx index representing typical (average) conditions.
|
||||
*
|
||||
* @param learning_time_offset_hours Time constant to estimate the NOx algorithm
|
||||
* offset from the history in hours. Past events will be forgotten after about
|
||||
* twice the learning time.
|
||||
*
|
||||
* @param learning_time_gain_hours The time constant to estimate the NOx
|
||||
* algorithm gain from the history has no impact for NOx. This parameter is
|
||||
* still in place for consistency reasons with the VOC tuning parameters
|
||||
* command.
|
||||
*
|
||||
* @param gating_max_duration_minutes Maximum duration of gating in minutes
|
||||
* (freeze of estimator during high NOx index signal). Zero disables the gating.
|
||||
*
|
||||
* @param std_initial The initial estimate for standard deviation has no impact
|
||||
* for NOx. This parameter is still in place for consistency reasons with the
|
||||
* VOC tuning parameters command.
|
||||
*
|
||||
* @param gain_factor Gain factor to amplify or to attenuate the NOx index
|
||||
* output.
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_get_nox_algorithm_tuning_parameters(
|
||||
int16_t* index_offset, int16_t* learning_time_offset_hours,
|
||||
int16_t* learning_time_gain_hours, int16_t* gating_max_duration_minutes,
|
||||
int16_t* std_initial, int16_t* gain_factor);
|
||||
|
||||
/**
|
||||
* sen5x_set_rht_acceleration_mode() - Sets the RH/T acceleration mode.
|
||||
*
|
||||
* Supported sensors: SEN54, SEN55
|
||||
*
|
||||
* @note This parameter can be changed in any state of the device (and the
|
||||
* getter immediately returns the new value), but it is applied only the next
|
||||
* time starting a measurement, i.e. when sending a \"Start Measurement\"
|
||||
* command. So the parameter needs to be set *before* a new measurement is
|
||||
* started.
|
||||
*
|
||||
* @param mode The new RH/T acceleration mode.
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_set_rht_acceleration_mode(uint16_t mode);
|
||||
|
||||
/**
|
||||
* sen5x_get_rht_acceleration_mode() - Gets the RH/T acceleration mode.
|
||||
*
|
||||
* Supported sensors: SEN54, SEN55
|
||||
*
|
||||
* @param mode The current RH/T acceleration mode.
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_get_rht_acceleration_mode(uint16_t* mode);
|
||||
|
||||
/**
|
||||
* sen5x_set_voc_algorithm_state() - Sets the VOC algorithm state previously
|
||||
* received with the \"Get VOC Algorithm State\" command.
|
||||
*
|
||||
* Supported sensors: SEN54, SEN55
|
||||
*
|
||||
* @note This command is only available in idle mode and the state will be
|
||||
* applied only once when starting the next measurement. Any further
|
||||
* measurements (i.e. when stopping and restarting the measure mode) will reset
|
||||
* the state to initial values. In measure mode, this command has no effect.
|
||||
*
|
||||
* @param state VOC algorithm state to restore.
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_set_voc_algorithm_state(const uint8_t* state, uint8_t state_size);
|
||||
|
||||
/**
|
||||
* sen5x_get_voc_algorithm_state() - Gets the current VOC algorithm state. This
|
||||
* data can be used to restore the state with the \"Set VOC Algorithm State\"
|
||||
* command after a short power cycle or device reset.
|
||||
*
|
||||
* This command can be used either in measure mode or in idle mode
|
||||
* (which will then return the state at the time when the measurement
|
||||
* was stopped). In measure mode, the state can be read each measure
|
||||
* interval to always have the latest state available, even in case of
|
||||
* a sudden power loss.
|
||||
*
|
||||
* Supported sensors: SEN54, SEN55
|
||||
*
|
||||
* @param state Current VOC algorithm state.
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_get_voc_algorithm_state(uint8_t* state, uint8_t state_size);
|
||||
|
||||
/**
|
||||
* sen5x_set_fan_auto_cleaning_interval() - Sets the fan auto cleaning interval
|
||||
* for the device.
|
||||
*
|
||||
* @param interval Fan auto cleaning interval [s]. Set to zero to disable auto
|
||||
* cleaning.
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_set_fan_auto_cleaning_interval(uint32_t interval);
|
||||
|
||||
/**
|
||||
* sen5x_get_fan_auto_cleaning_interval() - Gets the fan auto cleaning interval
|
||||
* from the device.
|
||||
*
|
||||
* @param interval Fan auto cleaning interval [s]. Zero means auto cleaning is
|
||||
* disabled.
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_get_fan_auto_cleaning_interval(uint32_t* interval);
|
||||
|
||||
/**
|
||||
* sen5x_get_product_name() - Gets the product name from the device.
|
||||
*
|
||||
* @param product_name Null-terminated ASCII string containing the product name.
|
||||
* Up to 32 characters can be read from the device.
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_get_product_name(unsigned char* product_name,
|
||||
uint8_t product_name_size);
|
||||
|
||||
/**
|
||||
* sen5x_get_serial_number() - Gets the serial number from the device.
|
||||
*
|
||||
* @param serial_number Null-terminated ASCII string containing the serial
|
||||
* number. Up to 32 characters can be read from the device.
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_get_serial_number(unsigned char* serial_number,
|
||||
uint8_t serial_number_size);
|
||||
|
||||
/**
|
||||
* sen5x_get_version() - Gets the version information for the hardware, firmware
|
||||
* and communication protocol.
|
||||
*
|
||||
* @param firmware_major Firmware major version number.
|
||||
*
|
||||
* @param firmware_minor Firmware minor version number.
|
||||
*
|
||||
* @param firmware_debug Firmware debug state. If the debug state is set, the
|
||||
* firmware is in development.
|
||||
*
|
||||
* @param hardware_major Hardware major version number.
|
||||
*
|
||||
* @param hardware_minor Hardware minor version number.
|
||||
*
|
||||
* @param protocol_major Protocol major version number.
|
||||
*
|
||||
* @param protocol_minor Protocol minor version number.
|
||||
*
|
||||
* @param padding Padding byte, ignore this.
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_get_version(uint8_t* firmware_major, uint8_t* firmware_minor,
|
||||
bool* firmware_debug, uint8_t* hardware_major,
|
||||
uint8_t* hardware_minor, uint8_t* protocol_major,
|
||||
uint8_t* protocol_minor);
|
||||
|
||||
/**
|
||||
* sen5x_read_device_status() - Reads the current device status.
|
||||
|
||||
* Use this command to get detailed information about the device status.
|
||||
* The device status is encoded in flags. Each device status flag
|
||||
* represents a single bit in a 32-bit integer value. If more than one
|
||||
* error is present, the device status register value is the sum of the
|
||||
* corresponding flag values. For details about the available flags,
|
||||
* refer to the device status flags documentation.
|
||||
*
|
||||
* @note The status flags of type \"Error\" are sticky, i.e. they are not
|
||||
* cleared automatically even if the error condition no longer exists. So they
|
||||
* can only be cleared manually with the command 0xD210 \"Read And Clear Device
|
||||
* Status\" or with a device reset. All other flags are not sticky, i.e. they
|
||||
* are cleared automatically if the trigger condition disappears.
|
||||
*
|
||||
* @param device_status Device status (32 flags as an integer value). For
|
||||
* details, please refer to the device status flags documentation.
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_read_device_status(uint32_t* device_status);
|
||||
|
||||
/**
|
||||
* sen5x_read_and_clear_device_status() - Reads the current device status (like
|
||||
* command 0xD206 \"Read Device Status\") and afterwards clears all flags.
|
||||
*
|
||||
* @param device_status Device status (32 flags as an integer value) **before**
|
||||
* clearing it. For details, please refer to the device status flags
|
||||
* documentation.
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_read_and_clear_device_status(uint32_t* device_status);
|
||||
|
||||
/**
|
||||
* sen5x_device_reset() - Executes a reset on the device. This has the same
|
||||
* effect as a power cycle.
|
||||
*
|
||||
* @return 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sen5x_device_reset(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* SEN5X_I2C_H */
|
||||
@@ -1,101 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018, Sensirion AG
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of Sensirion AG nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
// File: src/sensirion_common.c
|
||||
#include "sensirion_common.h"
|
||||
#include "sensirion_config.h"
|
||||
|
||||
uint16_t sensirion_common_bytes_to_uint16_t(const uint8_t* bytes) {
|
||||
return (uint16_t)bytes[0] << 8 | (uint16_t)bytes[1];
|
||||
}
|
||||
|
||||
uint32_t sensirion_common_bytes_to_uint32_t(const uint8_t* bytes) {
|
||||
return (uint32_t)bytes[0] << 24 | (uint32_t)bytes[1] << 16 |
|
||||
(uint32_t)bytes[2] << 8 | (uint32_t)bytes[3];
|
||||
}
|
||||
|
||||
int16_t sensirion_common_bytes_to_int16_t(const uint8_t* bytes) {
|
||||
return (int16_t)sensirion_common_bytes_to_uint16_t(bytes);
|
||||
}
|
||||
|
||||
int32_t sensirion_common_bytes_to_int32_t(const uint8_t* bytes) {
|
||||
return (int32_t)sensirion_common_bytes_to_uint32_t(bytes);
|
||||
}
|
||||
|
||||
float sensirion_common_bytes_to_float(const uint8_t* bytes) {
|
||||
union {
|
||||
uint32_t u32_value;
|
||||
float float32;
|
||||
} tmp;
|
||||
|
||||
tmp.u32_value = sensirion_common_bytes_to_uint32_t(bytes);
|
||||
return tmp.float32;
|
||||
}
|
||||
|
||||
void sensirion_common_uint32_t_to_bytes(const uint32_t value, uint8_t* bytes) {
|
||||
bytes[0] = value >> 24;
|
||||
bytes[1] = value >> 16;
|
||||
bytes[2] = value >> 8;
|
||||
bytes[3] = value;
|
||||
}
|
||||
|
||||
void sensirion_common_uint16_t_to_bytes(const uint16_t value, uint8_t* bytes) {
|
||||
bytes[0] = value >> 8;
|
||||
bytes[1] = value;
|
||||
}
|
||||
|
||||
void sensirion_common_int32_t_to_bytes(const int32_t value, uint8_t* bytes) {
|
||||
bytes[0] = value >> 24;
|
||||
bytes[1] = value >> 16;
|
||||
bytes[2] = value >> 8;
|
||||
bytes[3] = value;
|
||||
}
|
||||
|
||||
void sensirion_common_int16_t_to_bytes(const int16_t value, uint8_t* bytes) {
|
||||
bytes[0] = value >> 8;
|
||||
bytes[1] = value;
|
||||
}
|
||||
|
||||
void sensirion_common_float_to_bytes(const float value, uint8_t* bytes) {
|
||||
union {
|
||||
uint32_t u32_value;
|
||||
float float32;
|
||||
} tmp;
|
||||
tmp.float32 = value;
|
||||
sensirion_common_uint32_t_to_bytes(tmp.u32_value, bytes);
|
||||
}
|
||||
|
||||
void sensirion_common_copy_bytes(const uint8_t* source, uint8_t* destination,
|
||||
uint16_t data_length) {
|
||||
uint16_t i;
|
||||
for (i = 0; i < data_length; i++) {
|
||||
destination[i] = source[i];
|
||||
}
|
||||
}
|
||||
@@ -1,181 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018, Sensirion AG
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of Sensirion AG nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef SENSIRION_COMMON_H
|
||||
#define SENSIRION_COMMON_H
|
||||
|
||||
#include "sensirion_config.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define NO_ERROR 0
|
||||
#define NOT_IMPLEMENTED_ERROR 31
|
||||
|
||||
#ifndef ARRAY_SIZE
|
||||
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
|
||||
#endif
|
||||
|
||||
#define SENSIRION_COMMAND_SIZE 2
|
||||
#define SENSIRION_WORD_SIZE 2
|
||||
#define SENSIRION_NUM_WORDS(x) (sizeof(x) / SENSIRION_WORD_SIZE)
|
||||
#define SENSIRION_MAX_BUFFER_WORDS 32
|
||||
|
||||
/**
|
||||
* sensirion_common_bytes_to_int16_t() - Convert an array of bytes to an int16_t
|
||||
*
|
||||
* Convert an array of bytes received from the sensor in big-endian/MSB-first
|
||||
* format to an int16_t value in the correct system-endianness.
|
||||
*
|
||||
* @param bytes An array of at least two bytes (MSB first)
|
||||
* @return The byte array represented as int16_t
|
||||
*/
|
||||
int16_t sensirion_common_bytes_to_int16_t(const uint8_t* bytes);
|
||||
|
||||
/**
|
||||
* sensirion_common_bytes_to_int32_t() - Convert an array of bytes to an int32_t
|
||||
*
|
||||
* Convert an array of bytes received from the sensor in big-endian/MSB-first
|
||||
* format to an int32_t value in the correct system-endianness.
|
||||
*
|
||||
* @param bytes An array of at least four bytes (MSB first)
|
||||
* @return The byte array represented as int32_t
|
||||
*/
|
||||
int32_t sensirion_common_bytes_to_int32_t(const uint8_t* bytes);
|
||||
|
||||
/**
|
||||
* sensirion_common_bytes_to_uint16_t() - Convert an array of bytes to an
|
||||
* uint16_t
|
||||
*
|
||||
* Convert an array of bytes received from the sensor in big-endian/MSB-first
|
||||
* format to an uint16_t value in the correct system-endianness.
|
||||
*
|
||||
* @param bytes An array of at least two bytes (MSB first)
|
||||
* @return The byte array represented as uint16_t
|
||||
*/
|
||||
uint16_t sensirion_common_bytes_to_uint16_t(const uint8_t* bytes);
|
||||
|
||||
/**
|
||||
* sensirion_common_bytes_to_uint32_t() - Convert an array of bytes to an
|
||||
* uint32_t
|
||||
*
|
||||
* Convert an array of bytes received from the sensor in big-endian/MSB-first
|
||||
* format to an uint32_t value in the correct system-endianness.
|
||||
*
|
||||
* @param bytes An array of at least four bytes (MSB first)
|
||||
* @return The byte array represented as uint32_t
|
||||
*/
|
||||
uint32_t sensirion_common_bytes_to_uint32_t(const uint8_t* bytes);
|
||||
|
||||
/**
|
||||
* sensirion_common_bytes_to_float() - Convert an array of bytes to a float
|
||||
*
|
||||
* Convert an array of bytes received from the sensor in big-endian/MSB-first
|
||||
* format to an float value in the correct system-endianness.
|
||||
*
|
||||
* @param bytes An array of at least four bytes (MSB first)
|
||||
* @return The byte array represented as float
|
||||
*/
|
||||
float sensirion_common_bytes_to_float(const uint8_t* bytes);
|
||||
|
||||
/**
|
||||
* sensirion_common_uint32_t_to_bytes() - Convert an uint32_t to an array of
|
||||
* bytes
|
||||
*
|
||||
* Convert an uint32_t value in system-endianness to big-endian/MBS-first
|
||||
* format to send to the sensor.
|
||||
*
|
||||
* @param value Value to convert
|
||||
* @param bytes An array of at least four bytes
|
||||
*/
|
||||
void sensirion_common_uint32_t_to_bytes(const uint32_t value, uint8_t* bytes);
|
||||
|
||||
/**
|
||||
* sensirion_common_uint16_t_to_bytes() - Convert an uint16_t to an array of
|
||||
* bytes
|
||||
*
|
||||
* Convert an uint16_t value in system-endianness to big-endian/MBS-first
|
||||
* format to send to the sensor.
|
||||
*
|
||||
* @param value Value to convert
|
||||
* @param bytes An array of at least two bytes
|
||||
*/
|
||||
void sensirion_common_uint16_t_to_bytes(const uint16_t value, uint8_t* bytes);
|
||||
|
||||
/**
|
||||
* sensirion_common_int32_t_to_bytes() - Convert an int32_t to an array of bytes
|
||||
*
|
||||
* Convert an int32_t value in system-endianness to big-endian/MBS-first
|
||||
* format to send to the sensor.
|
||||
*
|
||||
* @param value Value to convert
|
||||
* @param bytes An array of at least four bytes
|
||||
*/
|
||||
void sensirion_common_int32_t_to_bytes(const int32_t value, uint8_t* bytes);
|
||||
|
||||
/**
|
||||
* sensirion_common_int16_t_to_bytes() - Convert an int16_t to an array of bytes
|
||||
*
|
||||
* Convert an int16_t value in system-endianness to big-endian/MBS-first
|
||||
* format to send to the sensor.
|
||||
*
|
||||
* @param value Value to convert
|
||||
* @param bytes An array of at least two bytes
|
||||
*/
|
||||
void sensirion_common_int16_t_to_bytes(const int16_t value, uint8_t* bytes);
|
||||
|
||||
/**
|
||||
* sensirion_common_float_to_bytes() - Convert an float to an array of bytes
|
||||
*
|
||||
* Convert an float value in system-endianness to big-endian/MBS-first
|
||||
* format to send to the sensor.
|
||||
*
|
||||
* @param value Value to convert
|
||||
* @param bytes An array of at least four bytes
|
||||
*/
|
||||
void sensirion_common_float_to_bytes(const float value, uint8_t* bytes);
|
||||
|
||||
/**
|
||||
* sensirion_common_copy_bytes() - Copy bytes from one array to the other.
|
||||
*
|
||||
* @param source Array of bytes to be copied.
|
||||
* @param destination Array of bytes to be copied to.
|
||||
* @param data_length Number of bytes to copy.
|
||||
*/
|
||||
void sensirion_common_copy_bytes(const uint8_t* source, uint8_t* destination,
|
||||
uint16_t data_length);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* SENSIRION_COMMON_H */
|
||||
@@ -1,88 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2019, Sensirion AG
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of Sensirion AG nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef SENSIRION_CONFIG_H
|
||||
#define SENSIRION_CONFIG_H
|
||||
|
||||
/**
|
||||
* If your platform does not provide the library stdlib.h you have to remove the
|
||||
* include and define NULL yourself (see below).
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
|
||||
/**
|
||||
* #ifndef NULL
|
||||
* #define NULL ((void *)0)
|
||||
* #endif
|
||||
*/
|
||||
|
||||
/**
|
||||
* If your platform does not provide the library stdint.h you have to
|
||||
* define the integral types yourself (see below).
|
||||
*/
|
||||
#include <stdint.h>
|
||||
|
||||
/**
|
||||
* Typedef section for types commonly defined in <stdint.h>
|
||||
* If your system does not provide stdint headers, please define them
|
||||
* accordingly. Please make sure to define int64_t and uint64_t.
|
||||
*/
|
||||
/* typedef unsigned long long int uint64_t;
|
||||
* typedef long long int int64_t;
|
||||
* typedef long int32_t;
|
||||
* typedef unsigned long uint32_t;
|
||||
* typedef short int16_t;
|
||||
* typedef unsigned short uint16_t;
|
||||
* typedef char int8_t;
|
||||
* typedef unsigned char uint8_t;
|
||||
*/
|
||||
|
||||
#ifndef __cplusplus
|
||||
|
||||
/**
|
||||
* If your platform doesn't define the bool type we define it as int. Depending
|
||||
* on your system update the definition below.
|
||||
*/
|
||||
#if __STDC_VERSION__ >= 199901L
|
||||
#include <stdbool.h>
|
||||
#else
|
||||
|
||||
#ifndef bool
|
||||
#define bool int
|
||||
#define true 1
|
||||
#define false 0
|
||||
#endif /* bool */
|
||||
|
||||
#endif /* __STDC_VERSION__ */
|
||||
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#endif /* SENSIRION_CONFIG_H */
|
||||
@@ -1,294 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018, Sensirion AG
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of Sensirion AG nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
// File: src/sensirion_i2c.c
|
||||
#include "sensirion_i2c.h"
|
||||
#include "sensirion_common.h"
|
||||
#include "sensirion_config.h"
|
||||
#include "sensirion_i2c_hal.h"
|
||||
|
||||
uint8_t sensirion_i2c_generate_crc(const uint8_t* data, uint16_t count) {
|
||||
uint16_t current_byte;
|
||||
uint8_t crc = CRC8_INIT;
|
||||
uint8_t crc_bit;
|
||||
|
||||
/* calculates 8-Bit checksum with given polynomial */
|
||||
for (current_byte = 0; current_byte < count; ++current_byte) {
|
||||
crc ^= (data[current_byte]);
|
||||
for (crc_bit = 8; crc_bit > 0; --crc_bit) {
|
||||
if (crc & 0x80)
|
||||
crc = (crc << 1) ^ CRC8_POLYNOMIAL;
|
||||
else
|
||||
crc = (crc << 1);
|
||||
}
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
int8_t sensirion_i2c_check_crc(const uint8_t* data, uint16_t count,
|
||||
uint8_t checksum) {
|
||||
if (sensirion_i2c_generate_crc(data, count) != checksum)
|
||||
return CRC_ERROR;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sensirion_i2c_general_call_reset(void) {
|
||||
const uint8_t data = 0x06;
|
||||
return sensirion_i2c_hal_write(0, &data, (uint16_t)sizeof(data));
|
||||
}
|
||||
|
||||
uint16_t sensirion_i2c_fill_cmd_send_buf(uint8_t* buf, uint16_t cmd,
|
||||
const uint16_t* args,
|
||||
uint8_t num_args) {
|
||||
uint8_t i;
|
||||
uint16_t idx = 0;
|
||||
|
||||
buf[idx++] = (uint8_t)((cmd & 0xFF00) >> 8);
|
||||
buf[idx++] = (uint8_t)((cmd & 0x00FF) >> 0);
|
||||
|
||||
for (i = 0; i < num_args; ++i) {
|
||||
buf[idx++] = (uint8_t)((args[i] & 0xFF00) >> 8);
|
||||
buf[idx++] = (uint8_t)((args[i] & 0x00FF) >> 0);
|
||||
|
||||
uint8_t crc = sensirion_i2c_generate_crc((uint8_t*)&buf[idx - 2],
|
||||
SENSIRION_WORD_SIZE);
|
||||
buf[idx++] = crc;
|
||||
}
|
||||
return idx;
|
||||
}
|
||||
|
||||
int16_t sensirion_i2c_read_words_as_bytes(uint8_t address, uint8_t* data,
|
||||
uint16_t num_words) {
|
||||
int16_t ret;
|
||||
uint16_t i, j;
|
||||
uint16_t size = num_words * (SENSIRION_WORD_SIZE + CRC8_LEN);
|
||||
uint16_t word_buf[SENSIRION_MAX_BUFFER_WORDS];
|
||||
uint8_t* const buf8 = (uint8_t*)word_buf;
|
||||
|
||||
ret = sensirion_i2c_hal_read(address, buf8, size);
|
||||
if (ret != NO_ERROR)
|
||||
return ret;
|
||||
|
||||
/* check the CRC for each word */
|
||||
for (i = 0, j = 0; i < size; i += SENSIRION_WORD_SIZE + CRC8_LEN) {
|
||||
|
||||
ret = sensirion_i2c_check_crc(&buf8[i], SENSIRION_WORD_SIZE,
|
||||
buf8[i + SENSIRION_WORD_SIZE]);
|
||||
if (ret != NO_ERROR)
|
||||
return ret;
|
||||
|
||||
data[j++] = buf8[i];
|
||||
data[j++] = buf8[i + 1];
|
||||
}
|
||||
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sensirion_i2c_read_words(uint8_t address, uint16_t* data_words,
|
||||
uint16_t num_words) {
|
||||
int16_t ret;
|
||||
uint8_t i;
|
||||
|
||||
ret = sensirion_i2c_read_words_as_bytes(address, (uint8_t*)data_words,
|
||||
num_words);
|
||||
if (ret != NO_ERROR)
|
||||
return ret;
|
||||
|
||||
for (i = 0; i < num_words; ++i) {
|
||||
const uint8_t* word_bytes = (uint8_t*)&data_words[i];
|
||||
data_words[i] = ((uint16_t)word_bytes[0] << 8) | word_bytes[1];
|
||||
}
|
||||
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int16_t sensirion_i2c_write_cmd(uint8_t address, uint16_t command) {
|
||||
uint8_t buf[SENSIRION_COMMAND_SIZE];
|
||||
|
||||
sensirion_i2c_fill_cmd_send_buf(buf, command, NULL, 0);
|
||||
return sensirion_i2c_hal_write(address, buf, SENSIRION_COMMAND_SIZE);
|
||||
}
|
||||
|
||||
int16_t sensirion_i2c_write_cmd_with_args(uint8_t address, uint16_t command,
|
||||
const uint16_t* data_words,
|
||||
uint16_t num_words) {
|
||||
uint8_t buf[SENSIRION_MAX_BUFFER_WORDS];
|
||||
uint16_t buf_size;
|
||||
|
||||
buf_size =
|
||||
sensirion_i2c_fill_cmd_send_buf(buf, command, data_words, num_words);
|
||||
return sensirion_i2c_hal_write(address, buf, buf_size);
|
||||
}
|
||||
|
||||
int16_t sensirion_i2c_delayed_read_cmd(uint8_t address, uint16_t cmd,
|
||||
uint32_t delay_us, uint16_t* data_words,
|
||||
uint16_t num_words) {
|
||||
int16_t ret;
|
||||
uint8_t buf[SENSIRION_COMMAND_SIZE];
|
||||
|
||||
sensirion_i2c_fill_cmd_send_buf(buf, cmd, NULL, 0);
|
||||
ret = sensirion_i2c_hal_write(address, buf, SENSIRION_COMMAND_SIZE);
|
||||
if (ret != NO_ERROR)
|
||||
return ret;
|
||||
|
||||
if (delay_us)
|
||||
sensirion_i2c_hal_sleep_usec(delay_us);
|
||||
|
||||
return sensirion_i2c_read_words(address, data_words, num_words);
|
||||
}
|
||||
|
||||
int16_t sensirion_i2c_read_cmd(uint8_t address, uint16_t cmd,
|
||||
uint16_t* data_words, uint16_t num_words) {
|
||||
return sensirion_i2c_delayed_read_cmd(address, cmd, 0, data_words,
|
||||
num_words);
|
||||
}
|
||||
|
||||
uint16_t sensirion_i2c_add_command_to_buffer(uint8_t* buffer, uint16_t offset,
|
||||
uint16_t command) {
|
||||
buffer[offset++] = (uint8_t)((command & 0xFF00) >> 8);
|
||||
buffer[offset++] = (uint8_t)((command & 0x00FF) >> 0);
|
||||
return offset;
|
||||
}
|
||||
|
||||
uint16_t sensirion_i2c_add_uint32_t_to_buffer(uint8_t* buffer, uint16_t offset,
|
||||
uint32_t data) {
|
||||
buffer[offset++] = (uint8_t)((data & 0xFF000000) >> 24);
|
||||
buffer[offset++] = (uint8_t)((data & 0x00FF0000) >> 16);
|
||||
buffer[offset] = sensirion_i2c_generate_crc(
|
||||
&buffer[offset - SENSIRION_WORD_SIZE], SENSIRION_WORD_SIZE);
|
||||
offset++;
|
||||
buffer[offset++] = (uint8_t)((data & 0x0000FF00) >> 8);
|
||||
buffer[offset++] = (uint8_t)((data & 0x000000FF) >> 0);
|
||||
buffer[offset] = sensirion_i2c_generate_crc(
|
||||
&buffer[offset - SENSIRION_WORD_SIZE], SENSIRION_WORD_SIZE);
|
||||
offset++;
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
uint16_t sensirion_i2c_add_int32_t_to_buffer(uint8_t* buffer, uint16_t offset,
|
||||
int32_t data) {
|
||||
return sensirion_i2c_add_uint32_t_to_buffer(buffer, offset, (uint32_t)data);
|
||||
}
|
||||
|
||||
uint16_t sensirion_i2c_add_uint16_t_to_buffer(uint8_t* buffer, uint16_t offset,
|
||||
uint16_t data) {
|
||||
buffer[offset++] = (uint8_t)((data & 0xFF00) >> 8);
|
||||
buffer[offset++] = (uint8_t)((data & 0x00FF) >> 0);
|
||||
buffer[offset] = sensirion_i2c_generate_crc(
|
||||
&buffer[offset - SENSIRION_WORD_SIZE], SENSIRION_WORD_SIZE);
|
||||
offset++;
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
uint16_t sensirion_i2c_add_int16_t_to_buffer(uint8_t* buffer, uint16_t offset,
|
||||
int16_t data) {
|
||||
return sensirion_i2c_add_uint16_t_to_buffer(buffer, offset, (uint16_t)data);
|
||||
}
|
||||
|
||||
uint16_t sensirion_i2c_add_float_to_buffer(uint8_t* buffer, uint16_t offset,
|
||||
float data) {
|
||||
union {
|
||||
uint32_t uint32_data;
|
||||
float float_data;
|
||||
} convert;
|
||||
|
||||
convert.float_data = data;
|
||||
|
||||
buffer[offset++] = (uint8_t)((convert.uint32_data & 0xFF000000) >> 24);
|
||||
buffer[offset++] = (uint8_t)((convert.uint32_data & 0x00FF0000) >> 16);
|
||||
buffer[offset] = sensirion_i2c_generate_crc(
|
||||
&buffer[offset - SENSIRION_WORD_SIZE], SENSIRION_WORD_SIZE);
|
||||
offset++;
|
||||
buffer[offset++] = (uint8_t)((convert.uint32_data & 0x0000FF00) >> 8);
|
||||
buffer[offset++] = (uint8_t)((convert.uint32_data & 0x000000FF) >> 0);
|
||||
buffer[offset] = sensirion_i2c_generate_crc(
|
||||
&buffer[offset - SENSIRION_WORD_SIZE], SENSIRION_WORD_SIZE);
|
||||
offset++;
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
uint16_t sensirion_i2c_add_bytes_to_buffer(uint8_t* buffer, uint16_t offset,
|
||||
const uint8_t* data,
|
||||
uint16_t data_length) {
|
||||
uint16_t i;
|
||||
|
||||
if (data_length % SENSIRION_WORD_SIZE != 0) {
|
||||
return BYTE_NUM_ERROR;
|
||||
}
|
||||
|
||||
for (i = 0; i < data_length; i += 2) {
|
||||
buffer[offset++] = data[i];
|
||||
buffer[offset++] = data[i + 1];
|
||||
|
||||
buffer[offset] = sensirion_i2c_generate_crc(
|
||||
&buffer[offset - SENSIRION_WORD_SIZE], SENSIRION_WORD_SIZE);
|
||||
offset++;
|
||||
}
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
int16_t sensirion_i2c_write_data(uint8_t address, const uint8_t* data,
|
||||
uint16_t data_length) {
|
||||
return sensirion_i2c_hal_write(address, data, data_length);
|
||||
}
|
||||
|
||||
int16_t sensirion_i2c_read_data_inplace(uint8_t address, uint8_t* buffer,
|
||||
uint16_t expected_data_length) {
|
||||
int16_t error;
|
||||
uint16_t i, j;
|
||||
uint16_t size = (expected_data_length / SENSIRION_WORD_SIZE) *
|
||||
(SENSIRION_WORD_SIZE + CRC8_LEN);
|
||||
|
||||
if (expected_data_length % SENSIRION_WORD_SIZE != 0) {
|
||||
return BYTE_NUM_ERROR;
|
||||
}
|
||||
|
||||
error = sensirion_i2c_hal_read(address, buffer, size);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
|
||||
for (i = 0, j = 0; i < size; i += SENSIRION_WORD_SIZE + CRC8_LEN) {
|
||||
|
||||
error = sensirion_i2c_check_crc(&buffer[i], SENSIRION_WORD_SIZE,
|
||||
buffer[i + SENSIRION_WORD_SIZE]);
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
buffer[j++] = buffer[i];
|
||||
buffer[j++] = buffer[i + 1];
|
||||
}
|
||||
|
||||
return NO_ERROR;
|
||||
}
|
||||
@@ -1,315 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018, Sensirion AG
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of Sensirion AG nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef SENSIRION_I2C_H
|
||||
#define SENSIRION_I2C_H
|
||||
|
||||
#include "sensirion_config.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define CRC_ERROR 1
|
||||
#define I2C_BUS_ERROR 2
|
||||
#define I2C_NACK_ERROR 3
|
||||
#define BYTE_NUM_ERROR 4
|
||||
|
||||
#define CRC8_POLYNOMIAL 0x31
|
||||
#define CRC8_INIT 0xFF
|
||||
#define CRC8_LEN 1
|
||||
|
||||
#define SENSIRION_COMMAND_SIZE 2
|
||||
#define SENSIRION_WORD_SIZE 2
|
||||
#define SENSIRION_NUM_WORDS(x) (sizeof(x) / SENSIRION_WORD_SIZE)
|
||||
#define SENSIRION_MAX_BUFFER_WORDS 32
|
||||
|
||||
uint8_t sensirion_i2c_generate_crc(const uint8_t* data, uint16_t count);
|
||||
|
||||
int8_t sensirion_i2c_check_crc(const uint8_t* data, uint16_t count,
|
||||
uint8_t checksum);
|
||||
|
||||
/**
|
||||
* sensirion_i2c_general_call_reset() - Send a general call reset.
|
||||
*
|
||||
* @warning This will reset all attached I2C devices on the bus which support
|
||||
* general call reset.
|
||||
*
|
||||
* @return NO_ERROR on success, an error code otherwise
|
||||
*/
|
||||
int16_t sensirion_i2c_general_call_reset(void);
|
||||
|
||||
/**
|
||||
* sensirion_i2c_fill_cmd_send_buf() - create the i2c send buffer for a command
|
||||
* and a set of argument words. The output buffer interleaves argument words
|
||||
* with their checksums.
|
||||
* @buf: The generated buffer to send over i2c. Then buffer length must
|
||||
* be at least SENSIRION_COMMAND_LEN + num_args *
|
||||
* (SENSIRION_WORD_SIZE + CRC8_LEN).
|
||||
* @cmd: The i2c command to send. It already includes a checksum.
|
||||
* @args: The arguments to the command. Can be NULL if none.
|
||||
* @num_args: The number of word arguments in args.
|
||||
*
|
||||
* @return The number of bytes written to buf
|
||||
*/
|
||||
uint16_t sensirion_i2c_fill_cmd_send_buf(uint8_t* buf, uint16_t cmd,
|
||||
const uint16_t* args,
|
||||
uint8_t num_args);
|
||||
|
||||
/**
|
||||
* sensirion_i2c_read_words() - read data words from sensor
|
||||
*
|
||||
* @address: Sensor i2c address
|
||||
* @data_words: Allocated buffer to store the read words.
|
||||
* The buffer may also have been modified in case of an error.
|
||||
* @num_words: Number of data words to read (without CRC bytes)
|
||||
*
|
||||
* @return NO_ERROR on success, an error code otherwise
|
||||
*/
|
||||
int16_t sensirion_i2c_read_words(uint8_t address, uint16_t* data_words,
|
||||
uint16_t num_words);
|
||||
|
||||
/**
|
||||
* sensirion_i2c_read_words_as_bytes() - read data words as byte-stream from
|
||||
* sensor
|
||||
*
|
||||
* Read bytes without adjusting values to the uP's word-order.
|
||||
*
|
||||
* @address: Sensor i2c address
|
||||
* @data: Allocated buffer to store the read bytes.
|
||||
* The buffer may also have been modified in case of an error.
|
||||
* @num_words: Number of data words(!) to read (without CRC bytes)
|
||||
* Since only word-chunks can be read from the sensor the size
|
||||
* is still specified in sensor-words (num_words = num_bytes *
|
||||
* SENSIRION_WORD_SIZE)
|
||||
*
|
||||
* @return NO_ERROR on success, an error code otherwise
|
||||
*/
|
||||
int16_t sensirion_i2c_read_words_as_bytes(uint8_t address, uint8_t* data,
|
||||
uint16_t num_words);
|
||||
|
||||
/**
|
||||
* sensirion_i2c_write_cmd() - writes a command to the sensor
|
||||
* @address: Sensor i2c address
|
||||
* @command: Sensor command
|
||||
*
|
||||
* @return NO_ERROR on success, an error code otherwise
|
||||
*/
|
||||
int16_t sensirion_i2c_write_cmd(uint8_t address, uint16_t command);
|
||||
|
||||
/**
|
||||
* sensirion_i2c_write_cmd_with_args() - writes a command with arguments to the
|
||||
* sensor
|
||||
* @address: Sensor i2c address
|
||||
* @command: Sensor command
|
||||
* @data: Argument buffer with words to send
|
||||
* @num_words: Number of data words to send (without CRC bytes)
|
||||
*
|
||||
* @return NO_ERROR on success, an error code otherwise
|
||||
*/
|
||||
int16_t sensirion_i2c_write_cmd_with_args(uint8_t address, uint16_t command,
|
||||
const uint16_t* data_words,
|
||||
uint16_t num_words);
|
||||
|
||||
/**
|
||||
* sensirion_i2c_delayed_read_cmd() - send a command, wait for the sensor to
|
||||
* process and read data back
|
||||
* @address: Sensor i2c address
|
||||
* @cmd: Command
|
||||
* @delay: Time in microseconds to delay sending the read request
|
||||
* @data_words: Allocated buffer to store the read data
|
||||
* @num_words: Data words to read (without CRC bytes)
|
||||
*
|
||||
* @return NO_ERROR on success, an error code otherwise
|
||||
*/
|
||||
int16_t sensirion_i2c_delayed_read_cmd(uint8_t address, uint16_t cmd,
|
||||
uint32_t delay_us, uint16_t* data_words,
|
||||
uint16_t num_words);
|
||||
/**
|
||||
* sensirion_i2c_read_cmd() - reads data words from the sensor after a command
|
||||
* is issued
|
||||
* @address: Sensor i2c address
|
||||
* @cmd: Command
|
||||
* @data_words: Allocated buffer to store the read data
|
||||
* @num_words: Data words to read (without CRC bytes)
|
||||
*
|
||||
* @return NO_ERROR on success, an error code otherwise
|
||||
*/
|
||||
int16_t sensirion_i2c_read_cmd(uint8_t address, uint16_t cmd,
|
||||
uint16_t* data_words, uint16_t num_words);
|
||||
|
||||
/**
|
||||
* sensirion_i2c_add_command_to_buffer() - Add a command to the buffer at
|
||||
* offset. Adds 2 bytes to the buffer.
|
||||
*
|
||||
* @param buffer Pointer to buffer in which the write frame will be prepared.
|
||||
* Caller needs to make sure that there is enough space after
|
||||
* offset left to write the data into the buffer.
|
||||
* @param offset Offset of the next free byte in the buffer.
|
||||
* @param command Command to be written into the buffer.
|
||||
*
|
||||
* @return Offset of next free byte in the buffer after writing the data.
|
||||
*/
|
||||
uint16_t sensirion_i2c_add_command_to_buffer(uint8_t* buffer, uint16_t offset,
|
||||
uint16_t command);
|
||||
|
||||
/**
|
||||
* sensirion_i2c_add_uint32_t_to_buffer() - Add a uint32_t to the buffer at
|
||||
* offset. Adds 6 bytes to the buffer.
|
||||
*
|
||||
* @param buffer Pointer to buffer in which the write frame will be prepared.
|
||||
* Caller needs to make sure that there is enough space after
|
||||
* offset left to write the data into the buffer.
|
||||
* @param offset Offset of the next free byte in the buffer.
|
||||
* @param data uint32_t to be written into the buffer.
|
||||
*
|
||||
* @return Offset of next free byte in the buffer after writing the data.
|
||||
*/
|
||||
uint16_t sensirion_i2c_add_uint32_t_to_buffer(uint8_t* buffer, uint16_t offset,
|
||||
uint32_t data);
|
||||
|
||||
/**
|
||||
* sensirion_i2c_add_int32_t_to_buffer() - Add a int32_t to the buffer at
|
||||
* offset. Adds 6 bytes to the buffer.
|
||||
*
|
||||
* @param buffer Pointer to buffer in which the write frame will be prepared.
|
||||
* Caller needs to make sure that there is enough space after
|
||||
* offset left to write the data into the buffer.
|
||||
* @param offset Offset of the next free byte in the buffer.
|
||||
* @param data int32_t to be written into the buffer.
|
||||
*
|
||||
* @return Offset of next free byte in the buffer after writing the data.
|
||||
*/
|
||||
uint16_t sensirion_i2c_add_int32_t_to_buffer(uint8_t* buffer, uint16_t offset,
|
||||
int32_t data);
|
||||
|
||||
/**
|
||||
* sensirion_i2c_add_uint16_t_to_buffer() - Add a uint16_t to the buffer at
|
||||
* offset. Adds 3 bytes to the buffer.
|
||||
*
|
||||
* @param buffer Pointer to buffer in which the write frame will be prepared.
|
||||
* Caller needs to make sure that there is enough space after
|
||||
* offset left to write the data into the buffer.
|
||||
* @param offset Offset of the next free byte in the buffer.
|
||||
* @param data uint16_t to be written into the buffer.
|
||||
*
|
||||
* @return Offset of next free byte in the buffer after writing the data.
|
||||
*/
|
||||
uint16_t sensirion_i2c_add_uint16_t_to_buffer(uint8_t* buffer, uint16_t offset,
|
||||
uint16_t data);
|
||||
|
||||
/**
|
||||
* sensirion_i2c_add_int16_t_to_buffer() - Add a int16_t to the buffer at
|
||||
* offset. Adds 3 bytes to the buffer.
|
||||
*
|
||||
* @param buffer Pointer to buffer in which the write frame will be prepared.
|
||||
* Caller needs to make sure that there is enough space after
|
||||
* offset left to write the data into the buffer.
|
||||
* @param offset Offset of the next free byte in the buffer.
|
||||
* @param data int16_t to be written into the buffer.
|
||||
*
|
||||
* @return Offset of next free byte in the buffer after writing the data.
|
||||
*/
|
||||
uint16_t sensirion_i2c_add_int16_t_to_buffer(uint8_t* buffer, uint16_t offset,
|
||||
int16_t data);
|
||||
|
||||
/**
|
||||
* sensirion_i2c_add_float_to_buffer() - Add a float to the buffer at offset.
|
||||
* Adds 6 bytes to the buffer.
|
||||
*
|
||||
* @param buffer Pointer to buffer in which the write frame will be prepared.
|
||||
* Caller needs to make sure that there is enough space after
|
||||
* offset left to write the data into the buffer.
|
||||
* @param offset Offset of the next free byte in the buffer.
|
||||
* @param data float to be written into the buffer.
|
||||
*
|
||||
* @return Offset of next free byte in the buffer after writing the data.
|
||||
*/
|
||||
uint16_t sensirion_i2c_add_float_to_buffer(uint8_t* buffer, uint16_t offset,
|
||||
float data);
|
||||
|
||||
/**
|
||||
* sensirion_i2c_add_bytes_to_buffer() - Add a byte array to the buffer at
|
||||
* offset.
|
||||
*
|
||||
* @param buffer Pointer to buffer in which the write frame will be
|
||||
* prepared. Caller needs to make sure that there is
|
||||
* enough space after offset left to write the data
|
||||
* into the buffer.
|
||||
* @param offset Offset of the next free byte in the buffer.
|
||||
* @param data Pointer to data to be written into the buffer.
|
||||
* @param data_length Number of bytes to be written into the buffer. Needs to
|
||||
* be a multiple of SENSIRION_WORD_SIZE otherwise the
|
||||
* function returns BYTE_NUM_ERROR.
|
||||
*
|
||||
* @return Offset of next free byte in the buffer after writing the
|
||||
* data.
|
||||
*/
|
||||
uint16_t sensirion_i2c_add_bytes_to_buffer(uint8_t* buffer, uint16_t offset,
|
||||
const uint8_t* data,
|
||||
uint16_t data_length);
|
||||
|
||||
/**
|
||||
* sensirion_i2c_write_data() - Writes data to the Sensor.
|
||||
*
|
||||
* @note This is just a wrapper for sensirion_i2c_hal_write() to
|
||||
* not need to include the HAL in the drivers.
|
||||
*
|
||||
* @param address I2C address to write to.
|
||||
* @param data Pointer to the buffer containing the data to write.
|
||||
* @param data_length Number of bytes to send to the Sensor.
|
||||
*
|
||||
* @return NO_ERROR on success, error code otherwise
|
||||
*/
|
||||
int16_t sensirion_i2c_write_data(uint8_t address, const uint8_t* data,
|
||||
uint16_t data_length);
|
||||
|
||||
/**
|
||||
* sensirion_i2c_read_data_inplace() - Reads data from the Sensor.
|
||||
*
|
||||
* @param address Sensor I2C address
|
||||
* @param buffer Allocated buffer to store data as bytes. Needs
|
||||
* to be big enough to store the data including
|
||||
* CRC. Twice the size of data should always
|
||||
* suffice.
|
||||
* @param expected_data_length Number of bytes to read (without CRC). Needs
|
||||
* to be a multiple of SENSIRION_WORD_SIZE,
|
||||
* otherwise the function returns BYTE_NUM_ERROR.
|
||||
*
|
||||
* @return NO_ERROR on success, an error code otherwise
|
||||
*/
|
||||
int16_t sensirion_i2c_read_data_inplace(uint8_t address, uint8_t* buffer,
|
||||
uint16_t expected_data_length);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* SENSIRION_I2C_H */
|
||||
@@ -1,60 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2023, Sensirion AG
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of Sensirion AG nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef SENSIRION_I2C_ESP32_CONFIG_H
|
||||
#define SENSIRION_I2C_ESP32_CONFIG_H
|
||||
|
||||
#include <i2cdev.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef int esp_err_t;
|
||||
|
||||
struct esp32_i2c_config {
|
||||
uint32_t freq;
|
||||
uint8_t addr;
|
||||
i2c_port_t port;
|
||||
gpio_num_t sda;
|
||||
gpio_num_t scl;
|
||||
bool sda_pullup;
|
||||
bool scl_pullup;
|
||||
};
|
||||
|
||||
extern esp_err_t sensirion_i2c_config_esp32(struct esp32_i2c_config* cfg);
|
||||
extern esp_err_t sensirion_i2c_esp32_ok(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* SENSIRION_I2C_ESP32_CONFIG_H */
|
||||
@@ -1,159 +0,0 @@
|
||||
// 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);
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018, Sensirion AG
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of Sensirion AG nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef SENSIRION_I2C_HAL_H
|
||||
#define SENSIRION_I2C_HAL_H
|
||||
|
||||
#include "sensirion_config.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
/**
|
||||
* Select the current i2c bus by index.
|
||||
* All following i2c operations will be directed at that bus.
|
||||
*
|
||||
* THE IMPLEMENTATION IS OPTIONAL ON SINGLE-BUS SETUPS (all sensors on the same
|
||||
* bus)
|
||||
*
|
||||
* @param bus_idx Bus index to select
|
||||
* @returns 0 on success, an error code otherwise
|
||||
*/
|
||||
int16_t sensirion_i2c_hal_select_bus(uint8_t bus_idx);
|
||||
|
||||
/**
|
||||
* Initialize all hard- and software components that are needed for the I2C
|
||||
* communication.
|
||||
*/
|
||||
void sensirion_i2c_hal_init(void);
|
||||
|
||||
/**
|
||||
* Release all resources initialized by sensirion_i2c_hal_init().
|
||||
*/
|
||||
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.
|
||||
*
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* Execute one write transaction on the I2C bus, sending a given number of
|
||||
* bytes. The bytes in the supplied buffer must be sent to the given address. If
|
||||
* the slave device does not acknowledge any of the bytes, an error shall be
|
||||
* returned.
|
||||
*
|
||||
* @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 read from the buffer and 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);
|
||||
|
||||
/**
|
||||
* Sleep for a given number of microseconds. The function should delay the
|
||||
* execution approximately, but no less than, the given time.
|
||||
*
|
||||
* When using hardware i2c:
|
||||
* Despite the unit, a <10 millisecond precision is sufficient.
|
||||
*
|
||||
* When using software i2c:
|
||||
* The precision needed depends on the desired i2c frequency, i.e. should be
|
||||
* exact to about half a clock cycle (defined in
|
||||
* `SENSIRION_I2C_CLOCK_PERIOD_USEC` in `sensirion_sw_i2c_gpio.h`).
|
||||
*
|
||||
* Example with 400kHz requires a precision of 1 / (2 * 400kHz) == 1.25usec.
|
||||
*
|
||||
* @param useconds the sleep time in microseconds
|
||||
*/
|
||||
void sensirion_i2c_hal_sleep_usec(uint32_t useconds);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#endif /* SENSIRION_I2C_HAL_H */
|
||||
@@ -360,7 +360,7 @@ static void mdns_discover_task(void *arg) {
|
||||
const TickType_t kDelay = pdMS_TO_TICKS(60000); // 60 s
|
||||
for (;;) {
|
||||
// Erst offizieller Service, dann Fallback auf HTTP:8086
|
||||
esp_err_t err = net_mdns_autodiscover_and_store("_influxdb2", "_tcp", -1,
|
||||
esp_err_t err = net_mdns_autodiscover_and_store("_myapp", "_tcp", -1,
|
||||
NULL, NULL, 2000);
|
||||
if (err != ESP_OK) {
|
||||
err = net_mdns_autodiscover_and_store("_http", "_tcp", 8086,
|
||||
|
||||
Reference in New Issue
Block a user