commit
15f09e1d9e
@ -0,0 +1,164 @@ |
||||
#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG |
||||
|
||||
#include "co2_sensor.h" |
||||
|
||||
#include <esp_log.h> |
||||
#include <esp_err.h> |
||||
#include <freertos/FreeRTOS.h> |
||||
#include <freertos/task.h> |
||||
#include <driver/i2c.h> |
||||
#include <sys/time.h> |
||||
#include <hal/i2c_hal.h> |
||||
#include "voc_sensor.h" |
||||
#include "i2c_utils.h" |
||||
#include "periph_init.h" |
||||
#include "data_report.h" |
||||
|
||||
static const char *TAG = "co2"; |
||||
|
||||
#define CO2_ADDR 104 |
||||
#define CO2_I2C_NUM I2C_NUM_1 |
||||
#define TIMEOUT_MS 500 |
||||
|
||||
// this works, but the bus is fucked up
|
||||
bool wake_up() { |
||||
esp_err_t suc; |
||||
#define TRY(x) suc=x; if(suc!=ESP_OK) return suc; |
||||
for (int i = 0; i < 5; i++) { |
||||
ESP_LOGD(TAG, "Wake up try %d", i); |
||||
uint8_t dummy; |
||||
|
||||
i2c_cmd_handle_t chain; |
||||
|
||||
chain = i2c_cmd_link_create(); |
||||
TRY(i2c_master_start(chain)); |
||||
TRY(i2c_master_write_byte(chain, (CO2_ADDR << 1) | I2C_MASTER_WRITE, false)); |
||||
TRY(i2c_master_write_byte(chain, 0x30, false)); |
||||
TRY(i2c_master_start(chain)); |
||||
TRY(i2c_master_write_byte(chain, (CO2_ADDR << 1) | I2C_MASTER_READ, false)); // TODO expect ack?
|
||||
TRY(i2c_master_read(chain, &dummy, 1, I2C_MASTER_LAST_NACK)); |
||||
TRY(i2c_master_stop(chain)); |
||||
i2c_master_cmd_begin(CO2_I2C_NUM, chain, pdMS_TO_TICKS(70)); |
||||
i2c_cmd_link_delete(chain); |
||||
|
||||
chain = i2c_cmd_link_create(); |
||||
TRY(i2c_master_start(chain)); |
||||
TRY(i2c_master_write_byte(chain, (CO2_ADDR << 1) | I2C_MASTER_WRITE, false)); |
||||
TRY(i2c_master_write_byte(chain, 0x30, false)); |
||||
TRY(i2c_master_start(chain)); |
||||
TRY(i2c_master_write_byte(chain, (CO2_ADDR << 1) | I2C_MASTER_READ, true)); // TODO expect ack?
|
||||
TRY(i2c_master_read(chain, &dummy, 1, I2C_MASTER_LAST_NACK)); |
||||
TRY(i2c_master_stop(chain)); |
||||
suc = i2c_master_cmd_begin(CO2_I2C_NUM, chain, pdMS_TO_TICKS(80)); |
||||
i2c_cmd_link_delete(chain); |
||||
|
||||
if (suc == ESP_OK) { |
||||
ESP_LOGD(TAG, "Woken up at try %d", i); |
||||
return true; |
||||
} |
||||
} |
||||
ESP_LOGE(TAG, "Fail to wake up"); |
||||
return false; |
||||
#undef TRY |
||||
} |
||||
|
||||
|
||||
static esp_err_t do_reg_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t length, uint32_t timeout_ms) { |
||||
i2c_cmd_handle_t cmd = i2c_cmd_link_create(); |
||||
i2c_reg_read(cmd, CO2_ADDR, reg_addr, reg_data, length); |
||||
esp_err_t ret = i2c_master_cmd_begin(CO2_I2C_NUM, cmd, pdMS_TO_TICKS(TIMEOUT_MS)); |
||||
i2c_cmd_link_delete(cmd); |
||||
if (ret != ESP_OK) { |
||||
ESP_LOGE(TAG, "I2C read error: %s, devaddr %x, reg %d, len %d", esp_err_to_name(ret), CO2_ADDR, reg_addr, length); |
||||
} |
||||
ESP_LOG_BUFFER_HEXDUMP(TAG, reg_data, length, ESP_LOG_DEBUG); |
||||
return ret; |
||||
} |
||||
|
||||
static esp_err_t reg_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t length, uint32_t timeout_ms) { |
||||
// BaseType_t suc = xSemaphoreTake(g_mux_i2c, pdMS_TO_TICKS(SEMA_TIMEOUT_MS));
|
||||
// if (suc != pdPASS) {
|
||||
// ESP_LOGE(TAG, "Sema fail");
|
||||
// return ESP_ERR_TIMEOUT;
|
||||
// }
|
||||
if (!wake_up()) { |
||||
// xSemaphoreGive(g_mux_i2c);
|
||||
return ESP_ERR_TIMEOUT; |
||||
} |
||||
esp_err_t rv = do_reg_read(reg_addr, reg_data, length, timeout_ms); |
||||
// xSemaphoreGive(g_mux_i2c);
|
||||
// ets_delay_us(12000);
|
||||
return rv; |
||||
} |
||||
|
||||
static esp_err_t do_reg_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t length, uint32_t timeout_ms) { |
||||
i2c_cmd_handle_t cmd = i2c_cmd_link_create(); |
||||
i2c_reg_write(cmd, CO2_ADDR, reg_addr, reg_data, length); |
||||
esp_err_t ret = i2c_master_cmd_begin(CO2_I2C_NUM, cmd, pdMS_TO_TICKS(timeout_ms)); |
||||
i2c_cmd_link_delete(cmd); |
||||
if (ret != ESP_OK) { |
||||
ESP_LOGE(TAG, "I2C write error: %s, devaddr %x, reg %d, len %d", esp_err_to_name(ret), CO2_ADDR, reg_addr, length); |
||||
} |
||||
return ret; |
||||
} |
||||
|
||||
static esp_err_t reg_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t length, uint32_t timeout_ms) { |
||||
// BaseType_t suc = xSemaphoreTake(g_mux_i2c, pdMS_TO_TICKS(SEMA_TIMEOUT_MS));
|
||||
// if (suc != pdPASS) {
|
||||
// ESP_LOGE(TAG, "Sema fail");
|
||||
// return ESP_ERR_TIMEOUT;
|
||||
// }
|
||||
if (!wake_up()) { |
||||
// xSemaphoreGive(g_mux_i2c);
|
||||
return ESP_ERR_TIMEOUT; |
||||
} |
||||
esp_err_t rv = do_reg_write(reg_addr, reg_data, length, timeout_ms); |
||||
// xSemaphoreGive(g_mux_i2c);
|
||||
// ets_delay_us(12000);
|
||||
return rv; |
||||
} |
||||
|
||||
void co2_read_task(void *param) { |
||||
// i2c is protected by a semaphore inside the driver, no need to worry about it here
|
||||
|
||||
// continuous is the default
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(500)); |
||||
|
||||
uint8_t pld[] = {0x00, 0x05, 0x00, 0x05}; |
||||
if (ESP_OK == reg_write(0x96, pld, 4, TIMEOUT_MS)) { |
||||
ESP_LOGI(TAG, "CO2 init OK"); |
||||
} |
||||
|
||||
bool init_suc = false; |
||||
vTaskDelay(pdMS_TO_TICKS(250)); |
||||
while (1) { |
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(2000)); |
||||
|
||||
uint8_t data[4] = {}; |
||||
|
||||
for (int retry = 0; retry < 3; retry++) { |
||||
if (ESP_OK == reg_read(0x06, data, 4, TIMEOUT_MS)) { |
||||
uint16_t filtered = (data[0] << 8) | data[1]; |
||||
uint16_t unfiltered = (data[2] << 8) | data[3]; |
||||
|
||||
ESP_LOGI(TAG, "CO2 ppm %d, raw %d", filtered, unfiltered); |
||||
|
||||
if (pdPASS == xSemaphoreTake(g_mux_data_report, pdMS_TO_TICKS(750))) { |
||||
if (filtered > 400 && filtered < 5000) { |
||||
g_data_report.co2_ppm = (float) filtered; |
||||
g_data_report.co2_ready = true; |
||||
g_data_report.co2_timestamp = xTaskGetTickCount(); |
||||
} else { |
||||
g_data_report.co2_ready = false; |
||||
} |
||||
} |
||||
xSemaphoreGive(g_mux_data_report); |
||||
break; |
||||
} |
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(50)); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,98 @@ |
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. |
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at> |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions |
||||
* are met: |
||||
* 1. Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* 2. Redistributions in binary form must reproduce the above copyright |
||||
* notice, this list of conditions and the following disclaimer in the |
||||
* documentation and/or other materials provided with the distribution. |
||||
* 3. The name of the author may not be used to endorse or promote products |
||||
* derived from this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
* File: $Id: mbcrc.c,v 1.7 2007/02/18 23:50:27 wolti Exp $ |
||||
*/ |
||||
|
||||
/* ----------------------- Platform includes --------------------------------*/ |
||||
#include "modbus_crc.h" |
||||
#include <stdint.h> |
||||
|
||||
static const uint8_t aucCRCHi[] = { |
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, |
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, |
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, |
||||
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, |
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, |
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, |
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, |
||||
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, |
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, |
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, |
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, |
||||
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, |
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, |
||||
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, |
||||
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, |
||||
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, |
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, |
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, |
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, |
||||
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, |
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, |
||||
0x00, 0xC1, 0x81, 0x40 |
||||
}; |
||||
|
||||
static const uint8_t aucCRCLo[] = { |
||||
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, |
||||
0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, |
||||
0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, |
||||
0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, |
||||
0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, |
||||
0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, |
||||
0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, |
||||
0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, |
||||
0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, |
||||
0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, |
||||
0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, |
||||
0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, |
||||
0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, |
||||
0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, |
||||
0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, |
||||
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, |
||||
0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, |
||||
0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, |
||||
0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, |
||||
0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, |
||||
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, |
||||
0x41, 0x81, 0x80, 0x40 |
||||
}; |
||||
|
||||
uint16_t modbus_crc(const uint8_t *pucFrame, uint16_t usLen) |
||||
{ |
||||
uint8_t ucCRCHi = 0xFF; |
||||
uint8_t ucCRCLo = 0xFF; |
||||
int iIndex; |
||||
|
||||
while (usLen--) { |
||||
iIndex = ucCRCLo ^ *(pucFrame++); |
||||
ucCRCLo = (uint8_t) (ucCRCHi ^ aucCRCHi[iIndex]); |
||||
ucCRCHi = aucCRCLo[iIndex]; |
||||
} |
||||
// return (uint16_t) (ucCRCHi << 8 | ucCRCLo);
|
||||
return (uint16_t) (ucCRCLo << 8 | ucCRCHi); |
||||
} |
@ -0,0 +1,37 @@ |
||||
/*
|
||||
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. |
||||
* Copyright (c) 2006 Christian Walter <wolti@sil.at> |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions |
||||
* are met: |
||||
* 1. Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* 2. Redistributions in binary form must reproduce the above copyright |
||||
* notice, this list of conditions and the following disclaimer in the |
||||
* documentation and/or other materials provided with the distribution. |
||||
* 3. The name of the author may not be used to endorse or promote products |
||||
* derived from this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
* File: $Id: mbcrc.h,v 1.5 2006/12/07 22:10:34 wolti Exp $ |
||||
*/ |
||||
|
||||
#ifndef _MODBUS_CRC_H |
||||
#define _MODBUS_CRC_H |
||||
#include <stdint.h> |
||||
|
||||
uint16_t modbus_crc(const uint8_t *pucFrame, uint16_t usLen); |
||||
|
||||
#endif |
@ -0,0 +1,167 @@ |
||||
#include <assert.h> |
||||
#include "modbus_fn.h" |
||||
#include "modbus_crc.h" |
||||
#include <esp_log.h> |
||||
|
||||
static const char *TAG = "mb"; |
||||
|
||||
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]; |
||||
return real == given; |
||||
} |
||||
|
||||
// Read holding registers
|
||||
int mb_build_fc3(uint8_t *buf, size_t len, size_t *resplen, uint8_t mbAddr, uint16_t ref, uint16_t qty) { |
||||
assert(buf); |
||||
assert(resplen); |
||||
if (len < 8) { |
||||
ESP_LOGE(TAG, "Buf len too short for FC3"); |
||||
return -1; |
||||
} |
||||
if (qty > 127) { |
||||
ESP_LOGE(TAG, "FC3 qty too high"); |
||||
return -1; |
||||
} |
||||
|
||||
size_t wp = 0; |
||||
buf[wp++] = mbAddr; |
||||
buf[wp++] = 0x03; |
||||
buf[wp++] = (ref & 0xFF00) >> 8; |
||||
buf[wp++] = (ref & 0xFF); |
||||
buf[wp++] = (qty & 0xFF00) >> 8; |
||||
buf[wp++] = (qty & 0xFF); |
||||
uint16_t crc = modbus_crc(buf, wp); |
||||
buf[wp++] = (crc & 0xFF00) >> 8; |
||||
buf[wp++] = (crc & 0xFF); |
||||
|
||||
*resplen = 5 + qty * 2; |
||||
return (int) wp; |
||||
} |
||||
|
||||
static int mb_parse_fc3_4(const uint8_t *buf, size_t len, uint16_t *dst, size_t capacity, int fcX) { |
||||
assert(buf); |
||||
|
||||
if (!crc_matches(buf, len)) { |
||||
ESP_LOGE(TAG, "FC%d CRC mismatch!", fcX); |
||||
return -1; |
||||
} |
||||
|
||||
//11 03 06 AE41 5652 4340 49AD
|
||||
if (len == 5 && buf[1] == (0x80+fcX)) { |
||||
ESP_LOGE(TAG, "FC%d exception: %d", fcX, buf[2]); |
||||
return -1; |
||||
} |
||||
|
||||
if (buf[1] != fcX) { |
||||
ESP_LOGE(TAG, "FC%d decode fail: not FC%d", fcX, fcX); |
||||
return -1; |
||||
} |
||||
|
||||
int num = buf[2] / 2; // this is the number of bytes
|
||||
if (capacity < num) { |
||||
ESP_LOGE(TAG, "FC%d decode fail: dst too small for %d registers", fcX, num); |
||||
return -1; |
||||
} |
||||
|
||||
for (int i = 0; i < num; i++) { |
||||
*dst++ = (((uint16_t) buf[3 + i * 2]) << 8) | (((uint16_t) buf[3 + i * 2 + 1])); |
||||
} |
||||
return num; |
||||
} |
||||
|
||||
int mb_parse_fc3(const uint8_t *buf, size_t len, uint16_t *dst, size_t capacity) { |
||||
return mb_parse_fc3_4(buf, len, dst, capacity, 3); |
||||
} |
||||
|
||||
//// Read input registers
|
||||
int mb_build_fc4(uint8_t *buf, size_t len, size_t *resplen, uint8_t mbAddr, uint16_t ref, uint16_t qty) { |
||||
assert(buf); |
||||
assert(resplen); |
||||
if (len < 8) { |
||||
ESP_LOGE(TAG, "Buf len too short for FC4"); |
||||
return -1; |
||||
} |
||||
if (qty > 127) { |
||||
ESP_LOGE(TAG, "FC3 qty too high"); |
||||
return -1; |
||||
} |
||||
|
||||
size_t wp = 0; |
||||
buf[wp++] = mbAddr; |
||||
buf[wp++] = 0x04; |
||||
buf[wp++] = (ref & 0xFF00) >> 8; |
||||
buf[wp++] = (ref & 0xFF); |
||||
buf[wp++] = (qty & 0xFF00) >> 8; |
||||
buf[wp++] = (qty & 0xFF); |
||||
uint16_t crc = modbus_crc(buf, wp); |
||||
buf[wp++] = (crc & 0xFF00) >> 8; |
||||
buf[wp++] = (crc & 0xFF); |
||||
|
||||
*resplen = 5 + qty * 2; |
||||
return (int) wp; |
||||
} |
||||
|
||||
int mb_parse_fc4(const uint8_t *buf, size_t len, uint16_t *dst, size_t capacity) { |
||||
return mb_parse_fc3_4(buf, len, dst, capacity, 4); |
||||
} |
||||
|
||||
// Write holding registers
|
||||
int mb_build_fc16(uint8_t *buf, size_t len, size_t *resplen, uint8_t mbAddr, uint16_t ref, const uint16_t *data, uint16_t qty) { |
||||
assert(buf); |
||||
assert(data); |
||||
assert(resplen); |
||||
if (len < 9 + qty * 2) { |
||||
ESP_LOGE(TAG, "Buf len too short for FC16, or bad len"); |
||||
return -1; |
||||
} |
||||
if (qty > 127) { |
||||
ESP_LOGE(TAG, "FC16 qty too high"); |
||||
return -1; |
||||
} |
||||
|
||||
size_t wp = 0; |
||||
buf[wp++] = mbAddr; |
||||
buf[wp++] = 0x10; |
||||
buf[wp++] = (ref & 0xFF00) >> 8; |
||||
buf[wp++] = (ref & 0xFF); |
||||
buf[wp++] = (qty & 0xFF00) >> 8; |
||||
buf[wp++] = (qty & 0xFF); |
||||
buf[wp++] = qty * 2; |
||||
|
||||
for (int i = 0; i < qty; i++) { |
||||
buf[wp++] = (data[i] & 0xFF00) >> 8; |
||||
buf[wp++] = (data[i] & 0xFF); |
||||
} |
||||
|
||||
uint16_t crc = modbus_crc(buf, wp); |
||||
buf[wp++] = (crc & 0xFF00) >> 8; |
||||
buf[wp++] = (crc & 0xFF); |
||||
*resplen = 8; |
||||
return (int) wp; |
||||
} |
||||
|
||||
|
||||
bool mb_parse_fc16(const uint8_t *buf, size_t len) |
||||
{ |
||||
assert(buf); |
||||
|
||||
if (!crc_matches(buf, len)) { |
||||
ESP_LOGE(TAG, "FC16 CRC mismatch!"); |
||||
return -1; |
||||
} |
||||
|
||||
//11 10 0001 0002 1298
|
||||
if (len == 5 && buf[1] == 0x90) { |
||||
ESP_LOGE(TAG, "FC16 exception: %d", buf[2]); |
||||
return -1; |
||||
} |
||||
|
||||
if (buf[1] != 16) { |
||||
ESP_LOGE(TAG, "FC16 decode fail: not FC16"); |
||||
return -1; |
||||
} |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,85 @@ |
||||
/**
|
||||
* TODO file description |
||||
* |
||||
* Created on 2022/01/02. |
||||
*/ |
||||
|
||||
#ifndef ESPNODE_MODBUS_FN_H |
||||
#define ESPNODE_MODBUS_FN_H |
||||
|
||||
#include <stdint.h> |
||||
#include <stddef.h> |
||||
#include <stdbool.h> |
||||
|
||||
/**
|
||||
* Build "read holding registers" packet |
||||
* |
||||
* @param buf buf to write to |
||||
* @param len buf len |
||||
* @param resplen expected response length (if not exception) |
||||
* @param mbAddr mb address |
||||
* @param ref first register number |
||||
* @param qty register quantity |
||||
* @return number of buf bytes if success, negative if error |
||||
*/ |
||||
int mb_build_fc3(uint8_t *buf, size_t len, size_t *resplen, uint8_t mbAddr, uint16_t ref, uint16_t qty); |
||||
|
||||
/**
|
||||
* Parse "read holding registers" response |
||||
* |
||||
* @param buf buf with the response bytes |
||||
* @param len response buf len |
||||
* @param dst store results here |
||||
* @param capacity capacity of "dst" |
||||
* @return num of results, negative if error |
||||
*/ |
||||
int mb_parse_fc3(const uint8_t *buf, size_t len, uint16_t *dst, size_t capacity); |
||||
|
||||
/**
|
||||
* Build "read input registers" packet |
||||
* |
||||
* @param buf buf to write to |
||||
* @param len buf len |
||||
* @param resplen expected response length (if not exception) |
||||
* @param mbAddr mb address |
||||
* @param ref first register number |
||||
* @param qty register quantity |
||||
* @return number of buf bytes if success, negative if error |
||||
*/ |
||||
int mb_build_fc4(uint8_t *buf, size_t len, size_t *resplen, uint8_t mbAddr, uint16_t ref, uint16_t qty); |
||||
|
||||
/**
|
||||
* Parse "read input registers" response |
||||
* |
||||
* @param buf buf with the response bytes |
||||
* @param len response buf len |
||||
* @param dst store results here |
||||
* @param capacity capacity of "dst" |
||||
* @return num of results, negative if error |
||||
*/ |
||||
int mb_parse_fc4(const uint8_t *buf, size_t len, uint16_t *dst, size_t capacity); |
||||
|
||||
/**
|
||||
* Build "write holding registers" packet |
||||
* |
||||
* @param buf buf to write to |
||||
* @param len buf len |
||||
* @param resplen expected response length (if not exception) |
||||
* @param mbAddr mb address |
||||
* @param ref first register number |
||||
* @param data register data to write, as an array |
||||
* @param qty register quantity |
||||
* @return number of buf bytes if success, negative if error |
||||
*/ |
||||
int mb_build_fc16(uint8_t *buf, size_t len, size_t *resplen, uint8_t mbAddr, uint16_t ref, const uint16_t *data, uint16_t qty); |
||||
|
||||
/**
|
||||
* Parse "write holding registers" response |
||||
* |
||||
* @param buf buf with the response bytes |
||||
* @param len response buf len |
||||
* @return 0 if success, negative if error |
||||
*/ |
||||
bool mb_parse_fc16(const uint8_t *buf, size_t len); |
||||
|
||||
#endif //ESPNODE_MODBUS_FN_H
|