Air quality sensor
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
esp-airsensor/main/periph_init.c

127 lines
3.8 KiB

#include <esp_log.h>
#include "periph_init.h"
#include "driver/i2c.h"
#include "data_report.h"
#include <driver/uart.h>
static const char *TAG = "periph_init";
esp_err_t periph_init() {
esp_err_t rv;
g_mux_data_report = xSemaphoreCreateMutex(); // XXX move elsewhere
assert(g_mux_data_report);
ESP_LOGI(TAG, "Init I2C");
// VOC i2c
{
int i2c_master_port = I2C_NUM_0;
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = CONFIG_PIN_I2C_SDA0,
.sda_pullup_en = GPIO_PULLUP_ENABLE, // is this enough?
.scl_io_num = CONFIG_PIN_I2C_SCL0,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = 100000,
};
rv = i2c_param_config(i2c_master_port, &conf);
if (rv != ESP_OK) {
ESP_LOGE(TAG, "Err in i2c_param_config");
return rv;
}
rv = i2c_driver_install(i2c_master_port, conf.mode, 0, 0, 0);
if (rv != ESP_OK) {
ESP_LOGE(TAG, "Err in i2c_driver_install");
return rv;
}
}
// senseair i2c
{
#if 0
int i2c_master_port = I2C_NUM_1;
i2c_config_t conf2 = {
.mode = I2C_MODE_MASTER,
.sda_io_num = CONFIG_PIN_I2C_SDA1,
.sda_pullup_en = GPIO_PULLUP_ENABLE, // is this enough?
.scl_io_num = CONFIG_PIN_I2C_SCL1,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = 100000,
};
rv = i2c_param_config(i2c_master_port, &conf2);
if (rv != ESP_OK) {
ESP_LOGE(TAG, "Err in i2c_param_config");
return rv;
}
rv = i2c_driver_install(i2c_master_port, conf2.mode, 0, 0, 0);
if (rv != ESP_OK) {
ESP_LOGE(TAG, "Err in i2c_driver_install");
return rv;
}
#else
int i2c_co2_port = I2C_NUM_1;
const uart_config_t uart_config = {
.baud_rate = 9600,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_APB,
//.use_ref_tick = true
};
ESP_ERROR_CHECK(uart_driver_install(i2c_co2_port,
/* rxbuf */ 256, /* txbuf */ 256, /* que */ 0, /* uart que */ NULL, /* alloc flags */ 0));
ESP_ERROR_CHECK( uart_param_config(i2c_co2_port, &uart_config) );
ESP_ERROR_CHECK(uart_set_pin(i2c_co2_port,
// CONFIG_PIN_I2C_SCL1,
// CONFIG_PIN_I2C_SDA1,
CONFIG_PIN_I2C_SDA1,
CONFIG_PIN_I2C_SCL1,
UART_PIN_NO_CHANGE,
UART_PIN_NO_CHANGE));
// Set UART pins(TX, RX, RTS, CTS)
#endif
}
// outputs
{
gpio_config_t gpioconf = {
.pin_bit_mask = (1 << CONFIG_PIN_CO2_COMSEL) | (1 << CONFIG_PIN_CO2_EN),
.mode = GPIO_MODE_OUTPUT,
};
rv = gpio_config(&gpioconf);
if (rv != ESP_OK) {
ESP_LOGE(TAG, "Err in gpio_config");
return rv;
}
gpio_set_level(CONFIG_PIN_CO2_COMSEL, 1); // low=I2C
gpio_set_level(CONFIG_PIN_CO2_EN, 1); // active high
}
// input
{
gpio_config_t gpioconf = {
.pin_bit_mask = (1 << CONFIG_PIN_CO2_NRDY),
.mode = GPIO_MODE_INPUT,
.pull_up_en = 1,
.pull_down_en = 0,
.intr_type = GPIO_INTR_DISABLE,
};
rv = gpio_config(&gpioconf);
if (rv != ESP_OK) {
ESP_LOGE(TAG, "Err in gpio_config");
return rv;
}
}
return ESP_OK;
}