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

128 lines
3.8 KiB

3 years ago
#include <esp_log.h>
#include "periph_init.h"
#include "driver/i2c.h"
#include "data_report.h"
3 years ago
3 years ago
#include <driver/uart.h>
3 years ago
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);
3 years ago
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;
}
3 years ago
}
// senseair i2c
{
3 years ago
#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;
}
3 years ago
#else
3 years ago
int i2c_co2_port = I2C_NUM_1;
3 years ago
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
};
3 years ago
ESP_ERROR_CHECK(uart_driver_install(i2c_co2_port,
/* rxbuf */ 256, /* txbuf */ 256, /* que */ 0, /* uart que */ NULL, /* alloc flags */ 0));
3 years ago
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));
3 years ago
// Set UART pins(TX, RX, RTS, CTS)
3 years ago
#endif
}
3 years ago
// outputs
{
gpio_config_t gpioconf = {
.pin_bit_mask = (1 << CONFIG_PIN_CO2_COMSEL) | (1 << CONFIG_PIN_CO2_EN),
.mode = GPIO_MODE_OUTPUT,
};
3 years ago
rv = gpio_config(&gpioconf);
if (rv != ESP_OK) {
ESP_LOGE(TAG, "Err in gpio_config");
return rv;
}
3 years ago
gpio_set_level(CONFIG_PIN_CO2_COMSEL, 1); // low=I2C
3 years ago
gpio_set_level(CONFIG_PIN_CO2_EN, 1); // active high
}
3 years ago
// 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,
};
3 years ago
rv = gpio_config(&gpioconf);
if (rv != ESP_OK) {
ESP_LOGE(TAG, "Err in gpio_config");
return rv;
}
}
3 years ago
return ESP_OK;
}