2018-09-17 19:32:38 +01:00
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
xnrg_04_mcp39f501.ino - MCP39F501 energy sensor support for Tasmota
|
2018-09-17 19:32:38 +01:00
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 Theo Arends
|
2018-09-17 19:32:38 +01:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_ENERGY_SENSOR
|
|
|
|
#ifdef USE_MCP39F501
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* MCP39F501 - Energy (Shelly 2)
|
|
|
|
*
|
|
|
|
* Based on datasheet from https://www.microchip.com/wwwproducts/en/MCP39F501
|
|
|
|
* and https://github.com/OLIMEX/olimex-iot-firmware-esp8266/blob/7a7f9bb56d4b72770dba8d0f18eaa9d956dd0baf/olimex/user/modules/mod_emtr.c
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-09-21 14:22:17 +01:00
|
|
|
#define XNRG_04 4
|
|
|
|
|
2019-02-05 09:13:22 +00:00
|
|
|
#define MCP_BAUDRATE 4800
|
2018-09-21 14:22:17 +01:00
|
|
|
#define MCP_TIMEOUT 4
|
2018-09-25 10:35:37 +01:00
|
|
|
#define MCP_CALIBRATION_TIMEOUT 2
|
|
|
|
|
|
|
|
#define MCP_CALIBRATE_POWER 0x001
|
|
|
|
#define MCP_CALIBRATE_VOLTAGE 0x002
|
|
|
|
#define MCP_CALIBRATE_CURRENT 0x004
|
|
|
|
#define MCP_CALIBRATE_FREQUENCY 0x008
|
|
|
|
#define MCP_SINGLE_WIRE_FLAG 0x100
|
2018-09-21 14:22:17 +01:00
|
|
|
|
|
|
|
#define MCP_START_FRAME 0xA5
|
|
|
|
#define MCP_ACK_FRAME 0x06
|
|
|
|
#define MCP_ERROR_NAK 0x15
|
|
|
|
#define MCP_ERROR_CRC 0x51
|
|
|
|
|
|
|
|
#define MCP_SINGLE_WIRE 0xAB
|
|
|
|
|
|
|
|
#define MCP_SET_ADDRESS 0x41
|
|
|
|
|
|
|
|
#define MCP_READ 0x4E
|
|
|
|
#define MCP_READ_16 0x52
|
|
|
|
#define MCP_READ_32 0x44
|
|
|
|
|
|
|
|
#define MCP_WRITE 0x4D
|
|
|
|
#define MCP_WRITE_16 0x57
|
|
|
|
#define MCP_WRITE_32 0x45
|
|
|
|
|
|
|
|
#define MCP_SAVE_REGISTERS 0x53
|
|
|
|
|
|
|
|
#define MCP_CALIBRATION_BASE 0x0028
|
|
|
|
#define MCP_CALIBRATION_LEN 52
|
|
|
|
|
|
|
|
#define MCP_FREQUENCY_REF_BASE 0x0094
|
|
|
|
#define MCP_FREQUENCY_GAIN_BASE 0x00AE
|
|
|
|
#define MCP_FREQUENCY_LEN 4
|
|
|
|
|
2019-02-04 17:18:47 +00:00
|
|
|
#define MCP_BUFFER_SIZE 60
|
|
|
|
|
|
|
|
#include <TasmotaSerial.h>
|
2019-03-26 17:26:50 +00:00
|
|
|
TasmotaSerial *McpSerial = nullptr;
|
2019-02-04 17:18:47 +00:00
|
|
|
|
2018-09-24 17:16:35 +01:00
|
|
|
typedef struct mcp_cal_registers_type {
|
2018-09-21 14:22:17 +01:00
|
|
|
uint16_t gain_current_rms;
|
|
|
|
uint16_t gain_voltage_rms;
|
|
|
|
uint16_t gain_active_power;
|
|
|
|
uint16_t gain_reactive_power;
|
|
|
|
sint32_t offset_current_rms;
|
|
|
|
sint32_t offset_active_power;
|
|
|
|
sint32_t offset_reactive_power;
|
|
|
|
sint16_t dc_offset_current;
|
|
|
|
sint16_t phase_compensation;
|
|
|
|
uint16_t apparent_power_divisor;
|
|
|
|
|
|
|
|
uint32_t system_configuration;
|
|
|
|
uint16_t dio_configuration;
|
|
|
|
uint32_t range;
|
|
|
|
|
|
|
|
uint32_t calibration_current;
|
|
|
|
uint16_t calibration_voltage;
|
|
|
|
uint32_t calibration_active_power;
|
|
|
|
uint32_t calibration_reactive_power;
|
|
|
|
uint16_t accumulation_interval;
|
2018-09-24 17:16:35 +01:00
|
|
|
} mcp_cal_registers_type;
|
2018-09-21 14:22:17 +01:00
|
|
|
|
2019-03-26 17:26:50 +00:00
|
|
|
char *mcp_buffer = nullptr;
|
2019-02-05 11:21:31 +00:00
|
|
|
unsigned long mcp_window = 0;
|
2018-09-25 10:35:37 +01:00
|
|
|
unsigned long mcp_kWhcounter = 0;
|
2018-09-21 14:22:17 +01:00
|
|
|
uint32_t mcp_system_configuration = 0x03000000;
|
2018-09-25 10:35:37 +01:00
|
|
|
uint32_t mcp_active_power;
|
|
|
|
//uint32_t mcp_reactive_power;
|
|
|
|
//uint32_t mcp_apparent_power;
|
|
|
|
uint32_t mcp_current_rms;
|
|
|
|
uint16_t mcp_voltage_rms;
|
|
|
|
uint16_t mcp_line_frequency;
|
|
|
|
//sint16_t mcp_power_factor;
|
2018-09-24 17:16:35 +01:00
|
|
|
uint8_t mcp_address = 0;
|
2018-09-21 14:22:17 +01:00
|
|
|
uint8_t mcp_calibration_active = 0;
|
|
|
|
uint8_t mcp_init = 0;
|
|
|
|
uint8_t mcp_timeout = 0;
|
2018-09-25 10:35:37 +01:00
|
|
|
uint8_t mcp_calibrate = 0;
|
2019-02-05 11:21:31 +00:00
|
|
|
uint8_t mcp_byte_counter = 0;
|
2018-09-17 19:32:38 +01:00
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Olimex tools
|
|
|
|
* https://github.com/OLIMEX/olimex-iot-firmware-esp8266/blob/7a7f9bb56d4b72770dba8d0f18eaa9d956dd0baf/olimex/user/modules/mod_emtr.c
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-09-21 14:22:17 +01:00
|
|
|
uint8_t McpChecksum(uint8_t *data)
|
|
|
|
{
|
|
|
|
uint8_t checksum = 0;
|
|
|
|
uint8_t offset = 0;
|
|
|
|
uint8_t len = data[1] -1;
|
|
|
|
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = offset; i < len; i++) { checksum += data[i]; }
|
2018-09-24 17:16:35 +01:00
|
|
|
return checksum;
|
2018-09-21 14:22:17 +01:00
|
|
|
}
|
2018-09-17 19:32:38 +01:00
|
|
|
|
2018-09-21 14:22:17 +01:00
|
|
|
unsigned long McpExtractInt(char *data, uint8_t offset, uint8_t size)
|
2018-09-17 19:32:38 +01:00
|
|
|
{
|
|
|
|
unsigned long result = 0;
|
|
|
|
unsigned long pow = 1;
|
|
|
|
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < size; i++) {
|
2018-09-21 14:22:17 +01:00
|
|
|
result = result + (uint8_t)data[offset + i] * pow;
|
2018-09-17 19:32:38 +01:00
|
|
|
pow = pow * 256;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-09-21 14:22:17 +01:00
|
|
|
void McpSetInt(unsigned long value, uint8_t *data, uint8_t offset, size_t size)
|
|
|
|
{
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < size; i++) {
|
2018-09-21 14:22:17 +01:00
|
|
|
data[offset + i] = ((value >> (i * 8)) & 0xFF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void McpSend(uint8_t *data)
|
|
|
|
{
|
|
|
|
if (mcp_timeout) { return; }
|
|
|
|
mcp_timeout = MCP_TIMEOUT;
|
|
|
|
|
|
|
|
data[0] = MCP_START_FRAME;
|
|
|
|
data[data[1] -1] = McpChecksum(data);
|
|
|
|
|
2019-01-17 16:48:34 +00:00
|
|
|
// AddLogBuffer(LOG_LEVEL_DEBUG_MORE, data, data[1]);
|
2018-09-21 14:22:17 +01:00
|
|
|
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < data[1]; i++) {
|
2019-02-04 17:18:47 +00:00
|
|
|
McpSerial->write(data[i]);
|
2018-09-21 14:22:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 17:16:35 +01:00
|
|
|
/********************************************************************************************/
|
|
|
|
|
|
|
|
void McpGetAddress(void)
|
|
|
|
{
|
|
|
|
uint8_t data[] = { MCP_START_FRAME, 7, MCP_SET_ADDRESS, 0x00, 0x26, MCP_READ_16, 0x00 };
|
|
|
|
|
|
|
|
McpSend(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void McpAddressReceive(void)
|
2018-09-21 14:22:17 +01:00
|
|
|
{
|
2018-09-24 17:16:35 +01:00
|
|
|
// 06 05 004D 58
|
2019-02-04 17:18:47 +00:00
|
|
|
mcp_address = mcp_buffer[3];
|
2018-09-21 14:22:17 +01:00
|
|
|
}
|
|
|
|
|
2018-09-24 17:16:35 +01:00
|
|
|
/********************************************************************************************/
|
|
|
|
|
|
|
|
void McpGetCalibration(void)
|
2018-09-21 14:22:17 +01:00
|
|
|
{
|
2018-09-24 17:16:35 +01:00
|
|
|
if (mcp_calibration_active) { return; }
|
2018-09-25 10:35:37 +01:00
|
|
|
mcp_calibration_active = MCP_CALIBRATION_TIMEOUT;
|
2018-09-24 17:16:35 +01:00
|
|
|
|
|
|
|
uint8_t data[] = { MCP_START_FRAME, 8, MCP_SET_ADDRESS, (MCP_CALIBRATION_BASE >> 8) & 0xFF, MCP_CALIBRATION_BASE & 0xFF, MCP_READ, MCP_CALIBRATION_LEN, 0x00 };
|
|
|
|
|
|
|
|
McpSend(data);
|
2018-09-21 14:22:17 +01:00
|
|
|
}
|
|
|
|
|
2018-09-24 17:16:35 +01:00
|
|
|
void McpParseCalibration(void)
|
|
|
|
{
|
|
|
|
bool action = false;
|
|
|
|
mcp_cal_registers_type cal_registers;
|
|
|
|
|
|
|
|
// 06 37 C882 B6AD 0781 9273 06000000 00000000 00000000 0000 D3FF 0300 00000003 9204 120C1300 204E0000 9808 E0AB0000 D9940000 0200 24
|
2019-02-04 17:18:47 +00:00
|
|
|
cal_registers.gain_current_rms = McpExtractInt(mcp_buffer, 2, 2);
|
|
|
|
cal_registers.gain_voltage_rms = McpExtractInt(mcp_buffer, 4, 2);
|
|
|
|
cal_registers.gain_active_power = McpExtractInt(mcp_buffer, 6, 2);
|
|
|
|
cal_registers.gain_reactive_power = McpExtractInt(mcp_buffer, 8, 2);
|
|
|
|
cal_registers.offset_current_rms = McpExtractInt(mcp_buffer, 10, 4);
|
|
|
|
cal_registers.offset_active_power = McpExtractInt(mcp_buffer, 14, 4);
|
|
|
|
cal_registers.offset_reactive_power = McpExtractInt(mcp_buffer, 18, 4);
|
|
|
|
cal_registers.dc_offset_current = McpExtractInt(mcp_buffer, 22, 2);
|
|
|
|
cal_registers.phase_compensation = McpExtractInt(mcp_buffer, 24, 2);
|
|
|
|
cal_registers.apparent_power_divisor = McpExtractInt(mcp_buffer, 26, 2);
|
|
|
|
|
|
|
|
cal_registers.system_configuration = McpExtractInt(mcp_buffer, 28, 4);
|
|
|
|
cal_registers.dio_configuration = McpExtractInt(mcp_buffer, 32, 2);
|
|
|
|
cal_registers.range = McpExtractInt(mcp_buffer, 34, 4);
|
|
|
|
|
|
|
|
cal_registers.calibration_current = McpExtractInt(mcp_buffer, 38, 4);
|
|
|
|
cal_registers.calibration_voltage = McpExtractInt(mcp_buffer, 42, 2);
|
|
|
|
cal_registers.calibration_active_power = McpExtractInt(mcp_buffer, 44, 4);
|
|
|
|
cal_registers.calibration_reactive_power = McpExtractInt(mcp_buffer, 48, 4);
|
|
|
|
cal_registers.accumulation_interval = McpExtractInt(mcp_buffer, 52, 2);
|
2018-09-24 17:16:35 +01:00
|
|
|
|
2018-09-25 10:35:37 +01:00
|
|
|
if (mcp_calibrate & MCP_CALIBRATE_POWER) {
|
|
|
|
cal_registers.calibration_active_power = Settings.energy_power_calibration;
|
2018-09-24 17:16:35 +01:00
|
|
|
if (McpCalibrationCalc(&cal_registers, 16)) { action = true; }
|
|
|
|
}
|
2018-09-25 10:35:37 +01:00
|
|
|
if (mcp_calibrate & MCP_CALIBRATE_VOLTAGE) {
|
|
|
|
cal_registers.calibration_voltage = Settings.energy_voltage_calibration;
|
2018-09-24 17:16:35 +01:00
|
|
|
if (McpCalibrationCalc(&cal_registers, 0)) { action = true; }
|
|
|
|
}
|
2018-09-25 10:35:37 +01:00
|
|
|
if (mcp_calibrate & MCP_CALIBRATE_CURRENT) {
|
|
|
|
cal_registers.calibration_current = Settings.energy_current_calibration;
|
2018-09-24 17:16:35 +01:00
|
|
|
if (McpCalibrationCalc(&cal_registers, 8)) { action = true; }
|
|
|
|
}
|
|
|
|
mcp_timeout = 0;
|
|
|
|
if (action) { McpSetCalibration(&cal_registers); }
|
2018-09-25 10:35:37 +01:00
|
|
|
|
|
|
|
mcp_calibrate = 0;
|
|
|
|
|
|
|
|
Settings.energy_power_calibration = cal_registers.calibration_active_power;
|
|
|
|
Settings.energy_voltage_calibration = cal_registers.calibration_voltage;
|
|
|
|
Settings.energy_current_calibration = cal_registers.calibration_current;
|
|
|
|
|
|
|
|
mcp_system_configuration = cal_registers.system_configuration;
|
|
|
|
|
|
|
|
if (mcp_system_configuration & MCP_SINGLE_WIRE_FLAG) {
|
|
|
|
mcp_system_configuration &= ~MCP_SINGLE_WIRE_FLAG; // Reset SingleWire flag
|
|
|
|
McpSetSystemConfiguration(2);
|
|
|
|
}
|
2018-09-24 17:16:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool McpCalibrationCalc(struct mcp_cal_registers_type *cal_registers, uint8_t range_shift)
|
2018-09-21 14:22:17 +01:00
|
|
|
{
|
|
|
|
uint32_t measured;
|
|
|
|
uint32_t expected;
|
|
|
|
uint16_t *gain;
|
|
|
|
uint32_t new_gain;
|
|
|
|
|
|
|
|
if (range_shift == 0) {
|
2018-09-25 10:35:37 +01:00
|
|
|
measured = mcp_voltage_rms;
|
2018-09-24 17:16:35 +01:00
|
|
|
expected = cal_registers->calibration_voltage;
|
|
|
|
gain = &(cal_registers->gain_voltage_rms);
|
2018-09-21 14:22:17 +01:00
|
|
|
} else if (range_shift == 8) {
|
2018-09-25 10:35:37 +01:00
|
|
|
measured = mcp_current_rms;
|
2018-09-24 17:16:35 +01:00
|
|
|
expected = cal_registers->calibration_current;
|
|
|
|
gain = &(cal_registers->gain_current_rms);
|
2018-09-21 14:22:17 +01:00
|
|
|
} else if (range_shift == 16) {
|
2018-09-25 10:35:37 +01:00
|
|
|
measured = mcp_active_power;
|
2018-09-24 17:16:35 +01:00
|
|
|
expected = cal_registers->calibration_active_power;
|
|
|
|
gain = &(cal_registers->gain_active_power);
|
2018-09-21 14:22:17 +01:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (measured == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-24 17:16:35 +01:00
|
|
|
uint32_t range = (cal_registers->range >> range_shift) & 0xFF;
|
2018-09-21 14:22:17 +01:00
|
|
|
|
|
|
|
calc:
|
|
|
|
new_gain = (*gain) * expected / measured;
|
|
|
|
|
|
|
|
if (new_gain < 25000) {
|
|
|
|
range++;
|
|
|
|
if (measured > 6) {
|
|
|
|
measured = measured / 2;
|
|
|
|
goto calc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_gain > 55000) {
|
|
|
|
range--;
|
|
|
|
measured = measured * 2;
|
|
|
|
goto calc;
|
|
|
|
}
|
|
|
|
|
|
|
|
*gain = new_gain;
|
2018-09-24 17:16:35 +01:00
|
|
|
uint32_t old_range = (cal_registers->range >> range_shift) & 0xFF;
|
|
|
|
cal_registers->range = cal_registers->range ^ (old_range << range_shift);
|
|
|
|
cal_registers->range = cal_registers->range | (range << range_shift);
|
2018-09-21 14:22:17 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-24 17:16:35 +01:00
|
|
|
/*
|
|
|
|
void McpCalibrationReactivePower(void)
|
2018-09-21 14:22:17 +01:00
|
|
|
{
|
2018-09-25 10:35:37 +01:00
|
|
|
cal_registers.gain_reactive_power = cal_registers.gain_reactive_power * cal_registers.calibration_reactive_power / mcp_reactive_power;
|
2018-09-21 14:22:17 +01:00
|
|
|
}
|
2018-09-24 17:16:35 +01:00
|
|
|
*/
|
|
|
|
void McpSetCalibration(struct mcp_cal_registers_type *cal_registers)
|
2018-09-21 14:22:17 +01:00
|
|
|
{
|
|
|
|
uint8_t data[7 + MCP_CALIBRATION_LEN + 2 + 1];
|
|
|
|
|
|
|
|
data[1] = sizeof(data);
|
2018-09-24 17:16:35 +01:00
|
|
|
data[2] = MCP_SET_ADDRESS; // Set address pointer
|
|
|
|
data[3] = (MCP_CALIBRATION_BASE >> 8) & 0xFF; // address
|
|
|
|
data[4] = (MCP_CALIBRATION_BASE >> 0) & 0xFF; // address
|
2018-09-21 14:22:17 +01:00
|
|
|
|
2018-09-24 17:16:35 +01:00
|
|
|
data[5] = MCP_WRITE; // Write N bytes
|
2018-09-21 14:22:17 +01:00
|
|
|
data[6] = MCP_CALIBRATION_LEN;
|
|
|
|
|
2018-09-24 17:16:35 +01:00
|
|
|
McpSetInt(cal_registers->gain_current_rms, data, 0+7, 2);
|
|
|
|
McpSetInt(cal_registers->gain_voltage_rms, data, 2+7, 2);
|
|
|
|
McpSetInt(cal_registers->gain_active_power, data, 4+7, 2);
|
|
|
|
McpSetInt(cal_registers->gain_reactive_power, data, 6+7, 2);
|
|
|
|
McpSetInt(cal_registers->offset_current_rms, data, 8+7, 4);
|
|
|
|
McpSetInt(cal_registers->offset_active_power, data, 12+7, 4);
|
|
|
|
McpSetInt(cal_registers->offset_reactive_power, data, 16+7, 4);
|
|
|
|
McpSetInt(cal_registers->dc_offset_current, data, 20+7, 2);
|
|
|
|
McpSetInt(cal_registers->phase_compensation, data, 22+7, 2);
|
|
|
|
McpSetInt(cal_registers->apparent_power_divisor, data, 24+7, 2);
|
|
|
|
|
|
|
|
McpSetInt(cal_registers->system_configuration, data, 26+7, 4);
|
|
|
|
McpSetInt(cal_registers->dio_configuration, data, 30+7, 2);
|
|
|
|
McpSetInt(cal_registers->range, data, 32+7, 4);
|
|
|
|
|
|
|
|
McpSetInt(cal_registers->calibration_current, data, 36+7, 4);
|
|
|
|
McpSetInt(cal_registers->calibration_voltage, data, 40+7, 2);
|
|
|
|
McpSetInt(cal_registers->calibration_active_power, data, 42+7, 4);
|
|
|
|
McpSetInt(cal_registers->calibration_reactive_power, data, 46+7, 4);
|
|
|
|
McpSetInt(cal_registers->accumulation_interval, data, 50+7, 2);
|
2018-09-21 14:22:17 +01:00
|
|
|
|
|
|
|
data[MCP_CALIBRATION_LEN+7] = MCP_SAVE_REGISTERS; // Save registers to flash
|
|
|
|
data[MCP_CALIBRATION_LEN+8] = mcp_address; // Device address
|
|
|
|
|
|
|
|
McpSend(data);
|
|
|
|
}
|
|
|
|
|
2018-09-24 17:16:35 +01:00
|
|
|
/********************************************************************************************/
|
|
|
|
|
2018-09-25 10:35:37 +01:00
|
|
|
void McpSetSystemConfiguration(uint16 interval)
|
|
|
|
{
|
|
|
|
// A5 11 41 00 42 45 03 00 01 00 41 00 5A 57 00 06 7A
|
|
|
|
uint8_t data[17];
|
|
|
|
|
|
|
|
data[ 1] = sizeof(data);
|
|
|
|
data[ 2] = MCP_SET_ADDRESS; // Set address pointer
|
|
|
|
data[ 3] = 0x00; // address
|
|
|
|
data[ 4] = 0x42; // address
|
|
|
|
data[ 5] = MCP_WRITE_32; // Write 4 bytes
|
|
|
|
data[ 6] = (mcp_system_configuration >> 24) & 0xFF; // system_configuration
|
|
|
|
data[ 7] = (mcp_system_configuration >> 16) & 0xFF; // system_configuration
|
|
|
|
data[ 8] = (mcp_system_configuration >> 8) & 0xFF; // system_configuration
|
|
|
|
data[ 9] = (mcp_system_configuration >> 0) & 0xFF; // system_configuration
|
|
|
|
data[10] = MCP_SET_ADDRESS; // Set address pointer
|
|
|
|
data[11] = 0x00; // address
|
|
|
|
data[12] = 0x5A; // address
|
|
|
|
data[13] = MCP_WRITE_16; // Write 2 bytes
|
|
|
|
data[14] = (interval >> 8) & 0xFF; // interval
|
|
|
|
data[15] = (interval >> 0) & 0xFF; // interval
|
|
|
|
|
|
|
|
McpSend(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
2018-09-24 17:16:35 +01:00
|
|
|
void McpGetFrequency(void)
|
2018-09-21 14:22:17 +01:00
|
|
|
{
|
|
|
|
if (mcp_calibration_active) { return; }
|
2018-09-25 10:35:37 +01:00
|
|
|
mcp_calibration_active = MCP_CALIBRATION_TIMEOUT;
|
2018-09-21 14:22:17 +01:00
|
|
|
|
2018-09-24 17:16:35 +01:00
|
|
|
uint8_t data[] = { MCP_START_FRAME, 11, MCP_SET_ADDRESS, (MCP_FREQUENCY_REF_BASE >> 8) & 0xFF, MCP_FREQUENCY_REF_BASE & 0xFF, MCP_READ_16,
|
|
|
|
MCP_SET_ADDRESS, (MCP_FREQUENCY_GAIN_BASE >> 8) & 0xFF, MCP_FREQUENCY_GAIN_BASE & 0xFF, MCP_READ_16, 0x00 };
|
2018-09-21 14:22:17 +01:00
|
|
|
|
2018-09-24 17:16:35 +01:00
|
|
|
McpSend(data);
|
|
|
|
}
|
2018-09-21 14:22:17 +01:00
|
|
|
|
2018-09-24 17:16:35 +01:00
|
|
|
void McpParseFrequency(void)
|
|
|
|
{
|
|
|
|
// 06 07 C350 8000 A0
|
2019-02-04 17:18:47 +00:00
|
|
|
uint16_t line_frequency_ref = mcp_buffer[2] * 256 + mcp_buffer[3];
|
|
|
|
uint16_t gain_line_frequency = mcp_buffer[4] * 256 + mcp_buffer[5];
|
2018-09-21 14:22:17 +01:00
|
|
|
|
2018-09-25 10:35:37 +01:00
|
|
|
if (mcp_calibrate & MCP_CALIBRATE_FREQUENCY) {
|
|
|
|
line_frequency_ref = Settings.energy_frequency_calibration;
|
2018-09-21 14:22:17 +01:00
|
|
|
|
2018-09-25 10:35:37 +01:00
|
|
|
if ((0xFFFF == mcp_line_frequency) || (0 == gain_line_frequency)) { // Reset values to 50Hz
|
|
|
|
mcp_line_frequency = 50000;
|
2018-09-24 17:16:35 +01:00
|
|
|
gain_line_frequency = 0x8000;
|
|
|
|
}
|
2018-09-25 10:35:37 +01:00
|
|
|
gain_line_frequency = gain_line_frequency * line_frequency_ref / mcp_line_frequency;
|
2018-09-21 14:22:17 +01:00
|
|
|
|
2018-09-24 17:16:35 +01:00
|
|
|
mcp_timeout = 0;
|
|
|
|
McpSetFrequency(line_frequency_ref, gain_line_frequency);
|
|
|
|
}
|
2018-09-25 10:35:37 +01:00
|
|
|
|
|
|
|
Settings.energy_frequency_calibration = line_frequency_ref;
|
|
|
|
|
|
|
|
mcp_calibrate = 0;
|
2018-09-21 14:22:17 +01:00
|
|
|
}
|
|
|
|
|
2018-09-24 17:16:35 +01:00
|
|
|
void McpSetFrequency(uint16_t line_frequency_ref, uint16_t gain_line_frequency)
|
2018-09-21 14:22:17 +01:00
|
|
|
{
|
|
|
|
// A5 11 41 00 94 57 C3 B4 41 00 AE 57 7E 46 53 4D 03
|
|
|
|
uint8_t data[17];
|
|
|
|
|
|
|
|
data[ 1] = sizeof(data);
|
2018-09-24 17:16:35 +01:00
|
|
|
data[ 2] = MCP_SET_ADDRESS; // Set address pointer
|
2018-09-21 14:22:17 +01:00
|
|
|
data[ 3] = (MCP_FREQUENCY_REF_BASE >> 8) & 0xFF; // address
|
|
|
|
data[ 4] = (MCP_FREQUENCY_REF_BASE >> 0) & 0xFF; // address
|
|
|
|
|
2018-09-24 17:16:35 +01:00
|
|
|
data[ 5] = MCP_WRITE_16; // Write register
|
|
|
|
data[ 6] = (line_frequency_ref >> 8) & 0xFF; // line_frequency_ref high
|
|
|
|
data[ 7] = (line_frequency_ref >> 0) & 0xFF; // line_frequency_ref low
|
2018-09-21 14:22:17 +01:00
|
|
|
|
2018-09-24 17:16:35 +01:00
|
|
|
data[ 8] = MCP_SET_ADDRESS; // Set address pointer
|
|
|
|
data[ 9] = (MCP_FREQUENCY_GAIN_BASE >> 8) & 0xFF; // address
|
|
|
|
data[10] = (MCP_FREQUENCY_GAIN_BASE >> 0) & 0xFF; // address
|
2018-09-21 14:22:17 +01:00
|
|
|
|
2018-09-24 17:16:35 +01:00
|
|
|
data[11] = MCP_WRITE_16; // Write register
|
|
|
|
data[12] = (gain_line_frequency >> 8) & 0xFF; // gain_line_frequency high
|
|
|
|
data[13] = (gain_line_frequency >> 0) & 0xFF; // gain_line_frequency low
|
2018-09-21 14:22:17 +01:00
|
|
|
|
2018-09-24 17:16:35 +01:00
|
|
|
data[14] = MCP_SAVE_REGISTERS; // Save registers to flash
|
|
|
|
data[15] = mcp_address; // Device address
|
2018-09-21 14:22:17 +01:00
|
|
|
|
|
|
|
McpSend(data);
|
|
|
|
}
|
|
|
|
|
2018-09-24 17:16:35 +01:00
|
|
|
/********************************************************************************************/
|
|
|
|
|
|
|
|
void McpGetData(void)
|
2018-09-21 14:22:17 +01:00
|
|
|
{
|
2018-09-24 17:16:35 +01:00
|
|
|
uint8_t data[] = { MCP_START_FRAME, 8, MCP_SET_ADDRESS, 0x00, 0x04, MCP_READ, 22, 0x00 };
|
2018-09-21 14:22:17 +01:00
|
|
|
|
2018-09-24 17:16:35 +01:00
|
|
|
McpSend(data);
|
2018-09-21 14:22:17 +01:00
|
|
|
}
|
2018-09-17 19:32:38 +01:00
|
|
|
|
2018-09-24 17:16:35 +01:00
|
|
|
void McpParseData(void)
|
2018-09-17 19:32:38 +01:00
|
|
|
{
|
2018-09-24 17:16:35 +01:00
|
|
|
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
|
|
|
// 06 19 61 06 00 00 FE 08 9B 0E 00 00 0B 00 00 00 97 0E 00 00 FF 7F 0C C6 35
|
|
|
|
// 06 19 CE 18 00 00 F2 08 3A 38 00 00 66 00 00 00 93 38 00 00 36 7F 9A C6 B7
|
|
|
|
// Ak Ln Current---- Volt- ActivePower ReActivePow ApparentPow Factr Frequ Ck
|
|
|
|
|
2019-02-04 17:18:47 +00:00
|
|
|
mcp_current_rms = McpExtractInt(mcp_buffer, 2, 4);
|
|
|
|
mcp_voltage_rms = McpExtractInt(mcp_buffer, 6, 2);
|
|
|
|
mcp_active_power = McpExtractInt(mcp_buffer, 8, 4);
|
|
|
|
// mcp_reactive_power = McpExtractInt(mcp_buffer, 12, 4);
|
|
|
|
// mcp_power_factor = McpExtractInt(mcp_buffer, 20, 2);
|
|
|
|
mcp_line_frequency = McpExtractInt(mcp_buffer, 22, 2);
|
2018-09-17 19:32:38 +01:00
|
|
|
|
2019-08-16 13:41:02 +01:00
|
|
|
if (Energy.power_on) { // Powered on
|
2019-09-21 16:10:52 +01:00
|
|
|
Energy.data_valid[0] = 0;
|
2019-09-15 12:10:32 +01:00
|
|
|
Energy.frequency[0] = (float)mcp_line_frequency / 1000;
|
|
|
|
Energy.voltage[0] = (float)mcp_voltage_rms / 10;
|
|
|
|
Energy.active_power[0] = (float)mcp_active_power / 100;
|
|
|
|
if (0 == Energy.active_power[0]) {
|
|
|
|
Energy.current[0] = 0;
|
2018-09-17 19:32:38 +01:00
|
|
|
} else {
|
2019-09-15 12:10:32 +01:00
|
|
|
Energy.current[0] = (float)mcp_current_rms / 10000;
|
2018-09-17 19:32:38 +01:00
|
|
|
}
|
|
|
|
} else { // Powered off
|
2019-09-21 16:10:52 +01:00
|
|
|
Energy.data_valid[0] = ENERGY_WATCHDOG;
|
2018-09-17 19:32:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 17:16:35 +01:00
|
|
|
/********************************************************************************************/
|
|
|
|
|
2019-02-04 17:18:47 +00:00
|
|
|
void McpSerialInput(void)
|
2018-09-17 19:32:38 +01:00
|
|
|
{
|
2019-02-05 11:21:31 +00:00
|
|
|
while ((McpSerial->available()) && (mcp_byte_counter < MCP_BUFFER_SIZE)) {
|
|
|
|
yield();
|
|
|
|
mcp_buffer[mcp_byte_counter++] = McpSerial->read();
|
|
|
|
mcp_window = millis();
|
|
|
|
}
|
2018-09-17 19:32:38 +01:00
|
|
|
|
2019-02-05 11:21:31 +00:00
|
|
|
// Ignore until non received after 2 chars (= 12 bits/char) time
|
|
|
|
if ((mcp_byte_counter) && (millis() - mcp_window > (24000 / MCP_BAUDRATE) +1)) {
|
2019-02-04 17:18:47 +00:00
|
|
|
AddLogBuffer(LOG_LEVEL_DEBUG_MORE, (uint8_t*)mcp_buffer, mcp_byte_counter);
|
2019-02-04 17:17:17 +00:00
|
|
|
|
2019-02-05 11:21:31 +00:00
|
|
|
if (MCP_BUFFER_SIZE == mcp_byte_counter) {
|
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG, PSTR("MCP: Overflow"));
|
|
|
|
}
|
|
|
|
else if (1 == mcp_byte_counter) {
|
2019-02-04 17:18:47 +00:00
|
|
|
if (MCP_ERROR_CRC == mcp_buffer[0]) {
|
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG, PSTR("MCP: Send " D_CHECKSUM_FAILURE));
|
|
|
|
mcp_timeout = 0;
|
|
|
|
}
|
|
|
|
else if (MCP_ERROR_NAK == mcp_buffer[0]) {
|
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG, PSTR("MCP: NAck"));
|
|
|
|
mcp_timeout = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (MCP_ACK_FRAME == mcp_buffer[0]) {
|
|
|
|
if (mcp_byte_counter == mcp_buffer[1]) {
|
|
|
|
|
|
|
|
if (McpChecksum((uint8_t *)mcp_buffer) != mcp_buffer[mcp_byte_counter -1]) {
|
|
|
|
AddLog_P(LOG_LEVEL_DEBUG, PSTR("MCP: " D_CHECKSUM_FAILURE));
|
|
|
|
} else {
|
|
|
|
if (5 == mcp_buffer[1]) { McpAddressReceive(); }
|
|
|
|
if (25 == mcp_buffer[1]) { McpParseData(); }
|
|
|
|
if (MCP_CALIBRATION_LEN + 3 == mcp_buffer[1]) { McpParseCalibration(); }
|
|
|
|
if (MCP_FREQUENCY_LEN + 3 == mcp_buffer[1]) { McpParseFrequency(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-09-21 14:22:17 +01:00
|
|
|
mcp_timeout = 0;
|
|
|
|
}
|
2019-02-04 17:18:47 +00:00
|
|
|
else if (MCP_SINGLE_WIRE == mcp_buffer[0]) {
|
2018-09-21 14:22:17 +01:00
|
|
|
mcp_timeout = 0;
|
|
|
|
}
|
|
|
|
|
2019-02-05 11:21:31 +00:00
|
|
|
mcp_byte_counter = 0;
|
2019-02-04 17:18:47 +00:00
|
|
|
McpSerial->flush();
|
2018-09-17 19:32:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
2018-09-24 17:16:35 +01:00
|
|
|
void McpEverySecond(void)
|
2018-09-17 19:32:38 +01:00
|
|
|
{
|
2019-09-21 16:10:52 +01:00
|
|
|
if (Energy.data_valid[0] > ENERGY_WATCHDOG) {
|
2019-05-30 11:45:02 +01:00
|
|
|
mcp_voltage_rms = 0;
|
|
|
|
mcp_current_rms = 0;
|
|
|
|
mcp_active_power = 0;
|
|
|
|
mcp_line_frequency = 0;
|
|
|
|
}
|
|
|
|
|
2018-09-25 10:35:37 +01:00
|
|
|
if (mcp_active_power) {
|
2019-08-16 13:41:02 +01:00
|
|
|
Energy.kWhtoday_delta += ((mcp_active_power * 10) / 36);
|
2018-09-21 15:02:56 +01:00
|
|
|
EnergyUpdateToday();
|
|
|
|
}
|
|
|
|
|
2018-09-21 14:22:17 +01:00
|
|
|
if (mcp_timeout) {
|
|
|
|
mcp_timeout--;
|
|
|
|
}
|
|
|
|
else if (mcp_calibration_active) {
|
|
|
|
mcp_calibration_active--;
|
|
|
|
}
|
|
|
|
else if (mcp_init) {
|
2018-09-25 10:35:37 +01:00
|
|
|
if (2 == mcp_init) {
|
|
|
|
McpGetCalibration(); // Get calibration parameters and disable SingleWire mode if enabled
|
|
|
|
}
|
|
|
|
else if (1 == mcp_init) {
|
|
|
|
McpGetFrequency(); // Get calibration parameter
|
|
|
|
}
|
|
|
|
mcp_init--;
|
2018-09-21 14:22:17 +01:00
|
|
|
}
|
|
|
|
else if (!mcp_address) {
|
2018-09-25 10:35:37 +01:00
|
|
|
McpGetAddress(); // Get device address for future calibration changes
|
2018-09-21 14:22:17 +01:00
|
|
|
}
|
2018-09-24 17:16:35 +01:00
|
|
|
else {
|
2018-09-25 10:35:37 +01:00
|
|
|
McpGetData(); // Get energy data
|
2018-09-17 19:32:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 17:16:35 +01:00
|
|
|
void McpSnsInit(void)
|
2018-09-17 19:32:38 +01:00
|
|
|
{
|
2019-02-04 17:18:47 +00:00
|
|
|
// Software serial init needs to be done here as earlier (serial) interrupts may lead to Exceptions
|
2020-04-27 10:54:23 +01:00
|
|
|
McpSerial = new TasmotaSerial(Pin(GPIO_MCP39F5_RX), Pin(GPIO_MCP39F5_TX), 1);
|
2019-02-05 09:13:22 +00:00
|
|
|
if (McpSerial->begin(MCP_BAUDRATE)) {
|
2019-02-05 11:21:31 +00:00
|
|
|
if (McpSerial->hardwareSerial()) {
|
|
|
|
ClaimSerial();
|
2020-10-30 11:29:48 +00:00
|
|
|
mcp_buffer = TasmotaGlobal.serial_in_buffer; // Use idle serial buffer to save RAM
|
2019-02-05 11:21:31 +00:00
|
|
|
} else {
|
|
|
|
mcp_buffer = (char*)(malloc(MCP_BUFFER_SIZE));
|
|
|
|
}
|
2020-04-27 09:35:38 +01:00
|
|
|
DigitalWrite(GPIO_MCP39F5_RST, 0, 1); // MCP enable
|
2019-02-04 17:18:47 +00:00
|
|
|
} else {
|
2020-10-30 11:29:48 +00:00
|
|
|
TasmotaGlobal.energy_driver = ENERGY_NONE;
|
2019-02-04 13:57:20 +00:00
|
|
|
}
|
2018-09-17 19:32:38 +01:00
|
|
|
}
|
|
|
|
|
2018-09-24 17:16:35 +01:00
|
|
|
void McpDrvInit(void)
|
2018-09-17 19:32:38 +01:00
|
|
|
{
|
2020-04-27 11:54:07 +01:00
|
|
|
if (PinUsed(GPIO_MCP39F5_RX) && PinUsed(GPIO_MCP39F5_TX)) {
|
|
|
|
if (PinUsed(GPIO_MCP39F5_RST)) {
|
2020-04-27 10:54:23 +01:00
|
|
|
pinMode(Pin(GPIO_MCP39F5_RST), OUTPUT);
|
|
|
|
digitalWrite(Pin(GPIO_MCP39F5_RST), 0); // MCP disable - Reset Delta Sigma ADC's
|
2018-09-17 19:32:38 +01:00
|
|
|
}
|
2019-09-08 15:57:56 +01:00
|
|
|
mcp_calibrate = 0;
|
|
|
|
mcp_timeout = 2; // Initial wait
|
|
|
|
mcp_init = 2; // Initial setup steps
|
2020-10-30 11:29:48 +00:00
|
|
|
TasmotaGlobal.energy_driver = XNRG_04;
|
2018-09-17 19:32:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool McpCommand(void)
|
2018-09-17 19:32:38 +01:00
|
|
|
{
|
2019-01-28 13:08:33 +00:00
|
|
|
bool serviced = true;
|
2018-09-23 15:41:22 +01:00
|
|
|
unsigned long value = 0;
|
2018-09-17 19:32:38 +01:00
|
|
|
|
2019-08-16 13:41:02 +01:00
|
|
|
if (CMND_POWERSET == Energy.command_code) {
|
2018-09-25 10:35:37 +01:00
|
|
|
if (XdrvMailbox.data_len && mcp_active_power) {
|
2019-07-01 17:20:43 +01:00
|
|
|
value = (unsigned long)(CharToFloat(XdrvMailbox.data) * 100);
|
2018-09-23 15:41:22 +01:00
|
|
|
if ((value > 100) && (value < 200000)) { // Between 1W and 2000W
|
|
|
|
Settings.energy_power_calibration = value;
|
2018-09-25 10:35:37 +01:00
|
|
|
mcp_calibrate |= MCP_CALIBRATE_POWER;
|
2018-09-23 15:41:22 +01:00
|
|
|
McpGetCalibration();
|
|
|
|
}
|
2018-09-17 19:32:38 +01:00
|
|
|
}
|
|
|
|
}
|
2019-08-16 13:41:02 +01:00
|
|
|
else if (CMND_VOLTAGESET == Energy.command_code) {
|
2018-09-25 10:35:37 +01:00
|
|
|
if (XdrvMailbox.data_len && mcp_voltage_rms) {
|
2019-07-01 17:20:43 +01:00
|
|
|
value = (unsigned long)(CharToFloat(XdrvMailbox.data) * 10);
|
2018-09-23 15:41:22 +01:00
|
|
|
if ((value > 1000) && (value < 2600)) { // Between 100V and 260V
|
|
|
|
Settings.energy_voltage_calibration = value;
|
2018-09-25 10:35:37 +01:00
|
|
|
mcp_calibrate |= MCP_CALIBRATE_VOLTAGE;
|
2018-09-23 15:41:22 +01:00
|
|
|
McpGetCalibration();
|
|
|
|
}
|
2018-09-17 19:32:38 +01:00
|
|
|
}
|
|
|
|
}
|
2019-08-16 13:41:02 +01:00
|
|
|
else if (CMND_CURRENTSET == Energy.command_code) {
|
2018-09-25 10:35:37 +01:00
|
|
|
if (XdrvMailbox.data_len && mcp_current_rms) {
|
2019-07-01 17:20:43 +01:00
|
|
|
value = (unsigned long)(CharToFloat(XdrvMailbox.data) * 10);
|
2018-09-23 15:41:22 +01:00
|
|
|
if ((value > 100) && (value < 80000)) { // Between 10mA and 8A
|
|
|
|
Settings.energy_current_calibration = value;
|
2018-09-25 10:35:37 +01:00
|
|
|
mcp_calibrate |= MCP_CALIBRATE_CURRENT;
|
2018-09-23 15:41:22 +01:00
|
|
|
McpGetCalibration();
|
|
|
|
}
|
2018-09-21 14:22:17 +01:00
|
|
|
}
|
|
|
|
}
|
2019-08-16 13:41:02 +01:00
|
|
|
else if (CMND_FREQUENCYSET == Energy.command_code) {
|
2018-09-25 10:35:37 +01:00
|
|
|
if (XdrvMailbox.data_len && mcp_line_frequency) {
|
2019-07-01 17:20:43 +01:00
|
|
|
value = (unsigned long)(CharToFloat(XdrvMailbox.data) * 1000);
|
2018-09-23 15:41:22 +01:00
|
|
|
if ((value > 45000) && (value < 65000)) { // Between 45Hz and 65Hz
|
|
|
|
Settings.energy_frequency_calibration = value;
|
2018-09-25 10:35:37 +01:00
|
|
|
mcp_calibrate |= MCP_CALIBRATE_FREQUENCY;
|
2018-09-23 15:41:22 +01:00
|
|
|
McpGetFrequency();
|
|
|
|
}
|
2018-09-17 19:32:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else serviced = false; // Unknown command
|
|
|
|
|
|
|
|
return serviced;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-09-08 15:57:56 +01:00
|
|
|
bool Xnrg04(uint8_t function)
|
2018-09-17 19:32:38 +01:00
|
|
|
{
|
2019-09-08 15:57:56 +01:00
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_LOOP:
|
|
|
|
if (McpSerial) { McpSerialInput(); }
|
|
|
|
break;
|
|
|
|
case FUNC_ENERGY_EVERY_SECOND:
|
|
|
|
if (McpSerial) { McpEverySecond(); }
|
|
|
|
break;
|
|
|
|
case FUNC_COMMAND:
|
|
|
|
result = McpCommand();
|
|
|
|
break;
|
|
|
|
case FUNC_INIT:
|
|
|
|
McpSnsInit();
|
|
|
|
break;
|
|
|
|
case FUNC_PRE_INIT:
|
|
|
|
McpDrvInit();
|
|
|
|
break;
|
2018-09-17 19:32:38 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_MCP39F501
|
|
|
|
#endif // USE_ENERGY_SENSOR
|