From 7c1c0203c07f68e6a9be41847d09604518545a89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sun, 26 Dec 2021 18:47:35 +0100 Subject: [PATCH] wip, doesnt work --- main/app_main.c | 2 +- main/co2_sensor.c | 13 +++++++------ main/periph_init.c | 43 ++++++++++++++++++++++++------------------- 3 files changed, 32 insertions(+), 26 deletions(-) diff --git a/main/app_main.c b/main/app_main.c index df36aef..e3eb848 100644 --- a/main/app_main.c +++ b/main/app_main.c @@ -59,7 +59,7 @@ void app_main(void) { vTaskDelay(pdMS_TO_TICKS(1000)); - xTaskCreatePinnedToCore(voc_read_task, "VOC", 4096, NULL, PRIO_NORMAL, NULL, 1); + //xTaskCreatePinnedToCore(voc_read_task, "VOC", 4096, NULL, PRIO_NORMAL, NULL, 1); xTaskCreatePinnedToCore(co2_read_task, "CO2", 4096, NULL, PRIO_NORMAL, NULL, 1); // xTaskCreate(co2_read_task, "CO2", 4096, NULL, PRIO_NORMAL, NULL); diff --git a/main/co2_sensor.c b/main/co2_sensor.c index 99a9d08..6586b73 100644 --- a/main/co2_sensor.c +++ b/main/co2_sensor.c @@ -31,10 +31,8 @@ void co2_read_task(void *param) { uint8_t buffer[128]; while (1) { - vTaskDelay(pdMS_TO_TICKS(2000)); - - + uint16_t ppm = 0; int i = 0; @@ -42,20 +40,23 @@ void co2_read_task(void *param) { buffer[i++] = 0x04; buffer[i++] = 0x00; buffer[i++] = 0x00; + buffer[i++] = 0x00; buffer[i++] = 0x04; buffer[i++] = 0xf8; buffer[i++] = 0xf0; + ESP_LOGI(TAG, "send %d bytes", i); uart_write_bytes(CO2_UART_NUM, buffer, i); i = uart_read_bytes(CO2_UART_NUM, buffer, 13, pdMS_TO_TICKS(1000)); + ESP_LOGI(TAG, "received %d bytes", i); + + ESP_LOG_BUFFER_HEXDUMP(TAG, buffer, i, ESP_LOG_DEBUG); if (i != 13) { ESP_LOGE(TAG, "Rx failed"); continue; - } - - ESP_LOG_BUFFER_HEXDUMP(TAG, buffer, i, ESP_LOG_DEBUG); + } continue; diff --git a/main/periph_init.c b/main/periph_init.c index 809d473..b1b9ea0 100644 --- a/main/periph_init.c +++ b/main/periph_init.c @@ -61,24 +61,29 @@ esp_err_t periph_init() { return rv; } #else - 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, - .use_ref_tick = true - }; - ESP_ERROR_CHECK( uart_param_config(UART_NUM_1, &uart_config) ); - - // Set UART pins(TX: IO17 (UART2 default), RX: IO16 (UART2 default), RTS: IO18, CTS: IO19) - ESP_ERROR_CHECK(uart_set_pin(UART_NUM_2, - CONFIG_PIN_I2C_SCL1, - CONFIG_PIN_I2C_SDA1, - UART_PIN_NO_CHANGE, - UART_PIN_NO_CHANGE)); - - ESP_ERROR_CHECK(uart_driver_install(UART_NUM_1, - /* rxbuf */ 256, /* txbuf */ 0, /* que */ 0, /* uart que */ NULL, /* alloc flags */ 0)); + 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) ); + + // Set UART pins(TX, RX, RTS, CTS) + ESP_ERROR_CHECK(uart_set_pin(i2c_co2_port, + CONFIG_PIN_I2C_SCL1, + CONFIG_PIN_I2C_SDA1, + UART_PIN_NO_CHANGE, + UART_PIN_NO_CHANGE)); #endif } @@ -95,7 +100,7 @@ esp_err_t periph_init() { return rv; } - gpio_set_level(CONFIG_PIN_CO2_COMSEL, 0); // low=I2C + gpio_set_level(CONFIG_PIN_CO2_COMSEL, 1); // low=I2C gpio_set_level(CONFIG_PIN_CO2_EN, 1); // active high }