diff --git a/components/i2c-bme680/CMakeLists.txt b/components/i2c-bme680/CMakeLists.txt new file mode 100644 index 0000000..ab1632e --- /dev/null +++ b/components/i2c-bme680/CMakeLists.txt @@ -0,0 +1,5 @@ +idf_component_register( + SRCS "bme680.c" + INCLUDE_DIRS include + REQUIRES "bus-i2c" +) diff --git a/components/i2c-bme680/bme680.c b/components/i2c-bme680/bme680.c new file mode 100644 index 0000000..cf3e631 --- /dev/null +++ b/components/i2c-bme680/bme680.c @@ -0,0 +1,47 @@ +/** + * Copyright (c) 2022 Nicolai Electronics + * + * SPDX-License-Identifier: MIT + */ + +#include +#include +#include +#include +#include +#include +#include "bme680.h" +#include "managed_i2c.h" + +static const char *TAG = "BME680"; + +esp_err_t bme680_check_id(BME680* device) { + uint8_t chip_id; + esp_err_t res = i2c_read_reg(device->i2c_bus, device->i2c_address, BME680_REG_CHIP_ID, &chip_id, 1); + if (res != ESP_OK) return res; + if (chip_id != BME680_CHIP_ID) { + ESP_LOGE(TAG, "Unexpected chip id value 0x%02X, expected 0x%02X", chip_id, BME680_CHIP_ID); + return ESP_FAIL; + } + return ESP_OK; +} + +esp_err_t bme680_reset(BME680* device) { + uint8_t value = 0xFF; + esp_err_t res = i2c_write_reg_n(device->i2c_bus, device->i2c_address, BME680_REG_RESET, &value, 1); + if (res != ESP_OK) return res; + return ESP_OK; +} + +esp_err_t bme680_init(BME680* device) { + esp_err_t res = bme680_reset(device); + if (res != ESP_OK) return res; + vTaskDelay(100 / portTICK_PERIOD_MS); + res = bme680_check_id(device); + if (res != ESP_OK) return res; + return res; +} + +esp_err_t bme680_deinit(BME680* device) { + return bme680_reset(device); +} diff --git a/components/i2c-bme680/include/bme680.h b/components/i2c-bme680/include/bme680.h new file mode 100644 index 0000000..30b76aa --- /dev/null +++ b/components/i2c-bme680/include/bme680.h @@ -0,0 +1,19 @@ +#pragma once + +#include +#include + +#define BME680_REG_RESET 0xE0 +#define BME680_REG_CHIP_ID 0xD0 + +#define BME680_CHIP_ID 0x61 + +typedef struct BME680 { + int i2c_bus; + int i2c_address; +} BME680; + +esp_err_t bme680_init(BME680* device); +esp_err_t bme680_deinit(BME680* device); +esp_err_t bme680_check_id(BME680* device); +esp_err_t bme680_reset(BME680* device); diff --git a/components/mch2022-bsp b/components/mch2022-bsp index 4ba1bb5..aa0214c 160000 --- a/components/mch2022-bsp +++ b/components/mch2022-bsp @@ -1 +1 @@ -Subproject commit 4ba1bb512e096d6b9ed2e07142c69037cfcdfe3c +Subproject commit aa0214c069b1b05a0392331c89dfa202a0fa61b1 diff --git a/components/spi-ice40 b/components/spi-ice40 index a7a0704..108625c 160000 --- a/components/spi-ice40 +++ b/components/spi-ice40 @@ -1 +1 @@ -Subproject commit a7a07046cd8748cb29725559a3f6ee095301521b +Subproject commit 108625c65834d6cdfef60f4725be8c53c3ad6d61 diff --git a/components/spi-ili9341 b/components/spi-ili9341 index 8fe7ca9..62f7cef 160000 --- a/components/spi-ili9341 +++ b/components/spi-ili9341 @@ -1 +1 @@ -Subproject commit 8fe7ca9876e2afed472c5d01170d4e76acdf6399 +Subproject commit 62f7cef36fc12c626eca56764695673e15bcb539 diff --git a/main/main.c b/main/main.c index 475214c..baa2f42 100644 --- a/main/main.c +++ b/main/main.c @@ -686,6 +686,18 @@ void app_main(void) { esp_restart(); } + if (bsp_bme680_init() != ESP_OK) { + ESP_LOGE(TAG, "Failed to initialize the BME680 position sensor"); + display_fatal_error(pax_buffer, ili9341, "Failed to initialize", "BME680 sensor error", "Check I2C bus", "Remove SAO and try again"); + esp_restart(); + } + + BME680* bme680 = get_bme680(); + if (bme680 == NULL) { + ESP_LOGE(TAG, "bme680 is NULL"); + esp_restart(); + } + /* Start AppFS */ res = appfs_init(); if (res != ESP_OK) {