Browse Source

finalizations and debugging

master
Ondřej Hruška 8 months ago
parent
commit
688bff3a6a
  1. 24
      main/co2_sensor.c
  2. 6
      main/modbus_fn.c

24
main/co2_sensor.c

@ -28,12 +28,19 @@ static int mb_write_and_read(uint8_t *buffer, int num, size_t resplen) {
return 0;
}
// sometimes there's trash in the buffer, get rid of it
int discarded = 0;
uint8_t junk;
do {
discarded = uart_read_bytes(CO2_UART_NUM, &junk, 1, pdMS_TO_TICKS(2));
} while(discarded);
ESP_LOGD(TAG, "Sending");
ESP_LOG_BUFFER_HEXDUMP(TAG, buffer, num, ESP_LOG_DEBUG);
uart_write_bytes(CO2_UART_NUM, buffer, num);
ESP_LOGD(TAG, "Expect resp of %d bytes", resplen);
num = uart_read_bytes(CO2_UART_NUM, buffer, resplen, pdMS_TO_TICKS(500));
num = uart_read_bytes(CO2_UART_NUM, buffer, resplen, pdMS_TO_TICKS(TIMEOUT_MS));
ESP_LOGD(TAG, "Received %d bytes", num);
ESP_LOG_BUFFER_HEXDUMP(TAG, buffer, num, ESP_LOG_DEBUG);
@ -54,7 +61,7 @@ static void write_saved_calib_to_sensor() {
/* Write calib */
resplen = 0;
num = mb_build_fc16(buffer, 128, &resplen, 104, 4, g_Settings.co2_calib, 5);
num = mb_build_fc16(buffer, 128, &resplen, CO2_ADDR, 4, g_Settings.co2_calib, 5);
num = mb_write_and_read(buffer, num, resplen);
resp = mb_parse_fc16(buffer, num);
if (resp < 0) {
@ -85,6 +92,8 @@ static bool ppm_looks_valid(uint16_t ppm) {
void co2_read_task(void *param) {
(void) param;
// 68 04 0a 00 00 00 00 00 00 03 b5 03 f2 dd c3
vTaskDelay(pdMS_TO_TICKS(500));
// TODO
@ -97,7 +106,7 @@ void co2_read_task(void *param) {
write_saved_calib_to_sensor();
const uint32_t read_cycle_time_ticks = 1000; //10 * 1000;
const uint32_t read_cycle_time_ticks = 10 * 1000;
const uint32_t calib_persist_time_ticks = 12 * 3600 * 1000;
_Static_assert(configTICK_RATE_HZ == 1000, "1kHz tick");
@ -118,7 +127,7 @@ void co2_read_task(void *param) {
}
resplen = 0;
num = mb_build_fc4(buffer, 128, &resplen, 104, 0, 5);
num = mb_build_fc4(buffer, 128, &resplen, CO2_ADDR, 0, 5);
num = mb_write_and_read(buffer, num, resplen);
qty = mb_parse_fc4(buffer, num, values, 32);
if (qty < 0) {
@ -179,7 +188,10 @@ void co2_read_task(void *param) {
if (!ppm_looks_valid(ppm)) {
ESP_LOGW(TAG, "CO2 measures nonsense!");
co2_restart(true);
if (ppm != 0) {
// ppm=0 is OK, it means the ppm wasn't measured yet. just ignore it.
co2_restart(true);
}
} else {
// CO2 measurement looks OK
@ -191,7 +203,7 @@ void co2_read_task(void *param) {
last_calib_persist = tickNow;
resplen = 0;
num = mb_build_fc3(buffer, 128, &resplen, 104, 4, 5);
num = mb_build_fc3(buffer, 128, &resplen, CO2_ADDR, 4, 5);
num = mb_write_and_read(buffer, num, resplen);
qty = mb_parse_fc3(buffer, num, values, 32);
if (qty < 0) {

6
main/modbus_fn.c

@ -1,3 +1,6 @@
//#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
#include <assert.h>
#include "modbus_fn.h"
#include "modbus_crc.h"
@ -9,6 +12,9 @@ static bool crc_matches(const uint8_t *buf, size_t len) {
assert(buf);
uint16_t real = modbus_crc(buf, len - 2);
uint16_t given = ((uint16_t) buf[len - 2] << 8) | (uint16_t) buf[len - 1];
if (real != given) {
ESP_LOGE(TAG, "CRC computed of %d bytes: %04x, given %04x", len-2, real, given);
}
return real == given;
}

Loading…
Cancel
Save