Skip to content

Commit

Permalink
22
Browse files Browse the repository at this point in the history
  • Loading branch information
xyzroe committed Oct 23, 2024
1 parent f9c2108 commit bcf1b27
Show file tree
Hide file tree
Showing 88 changed files with 9,672 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"configurations": [
{
"name": "Windows",
"cStandard": "c11",
"cppStandard": "c++17",
"includePath": [
"${config:idf.espIdfPath}/components/**",
"${workspaceFolder}/**"
],
"browse": {
"path": [
"${config:idf.espIdfPath}/components"
],
"limitSymbolsToIncludedHeaders": false
},
"compileCommands": "${workspaceFolder}/build/compile_commands.json",
"compilerPath": "${config:idf.toolsPath}/tools/riscv32-esp-elf/esp-13.2.0_20240530/riscv32-esp-elf/bin/riscv32-esp-elf-gcc"
}
],
"version": 4
}
19 changes: 19 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"idf.adapterTargetName": "esp32c6",
"idf.flashType": "UART",
"files.associations": {
"*.c": "c",
"*.h": "c"
},
"editor.formatOnSave": true,
"idf.port": "/dev/tty.usbmodem11101",
"idf.gitPath": "git",
"idf.espIdfPath": "/Users/lost/esp/v5.3.1/esp-idf",
"idf.pythonBinPath": "/Users/lost/.espressif/python_env/idf5.3_py3.9_env/bin/python",
"idf.toolsPath": "/Users/lost/.espressif",
"idf.customExtraPaths": "/Users/lost/.espressif/tools/xtensa-esp-elf-gdb/14.2_20240403/xtensa-esp-elf-gdb/bin:/Users/lost/.espressif/tools/riscv32-esp-elf-gdb/14.2_20240403/riscv32-esp-elf-gdb/bin:/Users/lost/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20240530/xtensa-esp-elf/bin:/Users/lost/.espressif/tools/riscv32-esp-elf/esp-13.2.0_20240530/riscv32-esp-elf/bin:/Users/lost/.espressif/tools/esp32ulp-elf/2.38_20240113/esp32ulp-elf/bin:/Users/lost/.espressif/tools/cmake/3.24.0/CMake.app/Contents/bin:/Users/lost/.espressif/tools/openocd-esp32/v0.12.0-esp32-20240318/openocd-esp32/bin:/Users/lost/.espressif/tools/ninja/1.11.1:/Users/lost/.espressif/tools/esp-rom-elfs/20240305",
"idf.customExtraVars": {
"OPENOCD_SCRIPTS": "/Users/lost/.espressif/tools/openocd-esp32/v0.12.0-esp32-20240318/openocd-esp32/share/openocd/scripts",
"ESP_ROM_ELF_DIR": "/Users/lost/.espressif/tools/esp-rom-elfs/20240305/"
}
}
29 changes: 29 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# The following lines of boilerplate must be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)


include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(Q_sensor)

# Add a custom command that will be executed before building the main target
add_custom_target(PreBuildCommand
COMMAND ${CMAKE_COMMAND} -E echo "Executing update version script..."
COMMAND bash ${CMAKE_SOURCE_DIR}/tools/update_version.sh
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMENT "Update version script finish!"
)

# Add a dependency of the main project on PreBuildCommand build target
add_dependencies(${CMAKE_PROJECT_NAME}.elf PreBuildCommand)

# Define a custom target for the OTA script
add_custom_target(make_ota ALL
COMMAND ${CMAKE_COMMAND} -E echo "Executing make OTA script..."
COMMAND bash ${CMAKE_SOURCE_DIR}/tools/make_ota.sh
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
COMMENT "Make OTA script finish!"
)

# Add a dependency of make_ota target on the main project build target
add_dependencies(make_ota ${CMAKE_PROJECT_NAME}.elf)
22 changes: 22 additions & 0 deletions components/ags10/.eil.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: ags10
description: Driver for AGS10 TVOC sensor
version: 0.5.0
groups:
- tvoc
code_owners: xyzroe
depends:
- i2cdev
- log
- esp_idf_lib_helpers
thread_safe: yes
targets:
- esp32
- esp32c3
- esp8266
- esp32s2
- esp32c3
- esp32c6
license: BSD-3
copyrights:
- name: xyzroe
year: 2024
5 changes: 5 additions & 0 deletions components/ags10/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
idf_component_register(
SRCS ags10.c
INCLUDE_DIRS .
REQUIRES i2cdev log esp_idf_lib_helpers
)
28 changes: 28 additions & 0 deletions components/ags10/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Copyright (c) 2024 xyzroe

Based on https://github.com/adafruit/Adafruit_AGS02MA

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

1. 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.

1. Neither the name of the copyright holder nor the names of itscontributors
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.
202 changes: 202 additions & 0 deletions components/ags10/ags10.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
#include "ags10.h"
#include <esp_log.h>
#include <driver/i2c.h>

#define CHECK_ARG(VAL) \
do \
{ \
if (!(VAL)) \
return ESP_ERR_INVALID_ARG; \
} while (0)

static const char *TAG = "ags10";

static uint8_t crc8(const uint8_t *data, int len);
static bool _read_reg(i2c_dev_t *dev, uint8_t cmd, uint16_t delayms, uint8_t *value, size_t value_size);

esp_err_t ags10_init_desc(i2c_dev_t *dev, i2c_port_t port, uint8_t addr, gpio_num_t sda_gpio, gpio_num_t scl_gpio)
{
CHECK_ARG(dev);

dev->port = port;
dev->addr = addr;
dev->cfg.sda_io_num = sda_gpio;
dev->cfg.scl_io_num = scl_gpio;
#if HELPER_TARGET_IS_ESP32
dev->cfg.master.clk_speed = I2C_FREQ_HZ;
#endif
// esp_err_t err = i2c_param_config(port, &dev->cfg);
// if (err != ESP_OK)
//{
// ESP_LOGE(TAG, "I2C param config failed: %s", esp_err_to_name(err));
// return err;
// }

// err = i2c_driver_install(port, I2C_MODE_MASTER, 0, 0, 0);
// if (err != ESP_OK)
//{
// ESP_LOGE(TAG, "I2C driver install failed: %s", esp_err_to_name(err));
// return err;
//}

return i2c_dev_create_mutex(dev);
}

esp_err_t ags10_free_desc(i2c_dev_t *dev)
{
// i2c_driver_delete(dev->port);
CHECK_ARG(dev);
return i2c_dev_delete_mutex(dev);
}

esp_err_t ags10_read_tvoc(i2c_dev_t *dev, uint32_t *tvoc)
{
CHECK_ARG(dev && tvoc);

uint8_t buf[5];
if (!_read_reg(dev, AGS10_TVOCSTAT_REG, 1500, buf, sizeof(buf)))
{
return ESP_FAIL;
}

*tvoc = (buf[1] << 16) | (buf[2] << 8) | buf[3];
return ESP_OK;
}

esp_err_t ags10_read_version(i2c_dev_t *dev, uint8_t *version)
{
CHECK_ARG(dev && version);

uint8_t buf[5];
if (!_read_reg(dev, AGS10_VERSION_REG, 30, buf, sizeof(buf)))
{
return ESP_FAIL;
}

*version = buf[3];
return ESP_OK;
}

esp_err_t ags10_read_resistance(i2c_dev_t *dev, uint32_t *resistance)
{
CHECK_ARG(dev && resistance);

uint8_t buf[5];
if (!_read_reg(dev, AGS10_GASRES_REG, 1500, buf, sizeof(buf)))
{
return ESP_FAIL;
}

*resistance = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
return ESP_OK;
}

esp_err_t ags10_set_i2c_address(i2c_dev_t *dev, uint8_t new_address)
{
CHECK_ARG(dev);

uint8_t buf[6];

new_address &= 0x7F;

buf[0] = AGS10_SETADDR_REG;
buf[3] = buf[1] = new_address;
buf[4] = buf[2] = ~new_address;
buf[5] = crc8(buf + 1, 4);

i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (dev->addr << 1) | I2C_MASTER_WRITE, true);
i2c_master_write(cmd, buf, 6, true);
i2c_master_stop(cmd);
esp_err_t err = i2c_master_cmd_begin(dev->port, cmd, 1000 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);

if (err != ESP_OK)
{
ESP_LOGE(TAG, "Failed to set new address: %s", esp_err_to_name(err));
return err;
}

dev->addr = new_address;
return ESP_OK;
}

esp_err_t ags10_set_zero_point_with_factory_defaults(i2c_dev_t *dev)
{
return ags10_set_zero_point_with(dev, 0xFFFF);
}

esp_err_t ags10_set_zero_point_with_current_resistance(i2c_dev_t *dev)
{
return ags10_set_zero_point_with(dev, 0x0000);
}

esp_err_t ags10_set_zero_point_with(i2c_dev_t *dev, uint16_t value)
{
CHECK_ARG(dev);

uint8_t buf[5] = {0x00, 0x0C, (uint8_t)((value >> 8) & 0xFF), (uint8_t)(value & 0xFF), 0};
buf[4] = crc8(buf, 4);

i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (dev->addr << 1) | I2C_MASTER_WRITE, true);
i2c_master_write(cmd, buf, 5, true);
i2c_master_stop(cmd);
esp_err_t err = i2c_master_cmd_begin(dev->port, cmd, 1000 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);

if (err != ESP_OK)
{
ESP_LOGE(TAG, "Failed to set zero point: %s", esp_err_to_name(err));
return err;
}

return ESP_OK;
}

static bool _read_reg(i2c_dev_t *dev, uint8_t cmd, uint16_t delayms, uint8_t *value, size_t value_size)

{
I2C_DEV_TAKE_MUTEX(dev);

esp_err_t err = i2c_dev_write(dev, NULL, 0, &cmd, 1);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Failed to send read command: %s", esp_err_to_name(err));
I2C_DEV_GIVE_MUTEX(dev);
return false;
}

vTaskDelay(pdMS_TO_TICKS(delayms));

err = i2c_dev_read(dev, NULL, 0, value, value_size);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Failed to read data: %s", esp_err_to_name(err));
I2C_DEV_GIVE_MUTEX(dev);
return false;
}

I2C_DEV_GIVE_MUTEX(dev);

return true;
}

static uint8_t crc8(const uint8_t *data, int len)
{
const uint8_t POLYNOMIAL = 0x31;
uint8_t crc = 0xFF;

for (int j = len; j; --j)
{
crc ^= *data++;

for (int i = 8; i; --i)
{
crc = (crc & 0x80) ? (crc << 1) ^ POLYNOMIAL : (crc << 1);
}
}
return crc;
}
Loading

0 comments on commit bcf1b27

Please sign in to comment.