Merge branch 'development' of github.com:arendst/Tasmota into pr_tm1638

This commit is contained in:
Ajith Vasudevan 2021-03-04 21:22:26 +05:30
commit f3e358f81b
5 changed files with 225 additions and 106 deletions

View File

@ -4,21 +4,23 @@
"ldscript": "esp32s2_out.ld" "ldscript": "esp32s2_out.ld"
}, },
"core": "esp32", "core": "esp32",
"mcu": "esp32s2",
"extra_flags": "-Desp32S2_dev_module",
"f_cpu": "240000000L", "f_cpu": "240000000L",
"f_flash": "80000000L", "f_flash": "80000000L",
"flash_mode": "qio", "flash_mode": "dio",
"mcu": "esp32s2", "mcu": "esp32s2",
"variant": "esp32s2" "variant": "esp32s2"
}, },
"connectivity": [ "connectivity": [
"wifi" "wifi"
], ],
"debug": {
"openocd_target": "esp32s2.cfg"
},
"frameworks": [ "frameworks": [
"espidf",
"arduino" "arduino"
], ],
"name": "ESP32S2 Dev Module", "name": "Espressif ESP32-S2-Saola-1",
"upload": { "upload": {
"flash_size": "4MB", "flash_size": "4MB",
"maximum_ram_size": 327680, "maximum_ram_size": 327680,
@ -26,6 +28,6 @@
"require_upload_port": true, "require_upload_port": true,
"speed": 460800 "speed": 460800
}, },
"url": "https://espressif.com", "url": "https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/hw-reference/esp32s2/user-guide-saola-1-v1.2.html",
"vendor": "espressif" "vendor": "Espressif"
} }

View File

@ -171,6 +171,7 @@ build_flags = ${common32.build_flags}
[env:tasmota32s2] [env:tasmota32s2]
extends = env:tasmota32 extends = env:tasmota32
board = esp32s2 board = esp32s2
board_build.flash_mode = qio
platform_packages = framework-arduinoespressif32 @ https://github.com/Jason2866/arduino-esp32/releases/download/s2-1.0.5-rc6/esp32-s2-1.0.5-rc6.zip platform_packages = framework-arduinoespressif32 @ https://github.com/Jason2866/arduino-esp32/releases/download/s2-1.0.5-rc6/esp32-s2-1.0.5-rc6.zip
platformio/tool-mklittlefs @ ~1.203.200522 platformio/tool-mklittlefs @ ~1.203.200522
platformio/tool-esptoolpy @ ~1.30000.0 platformio/tool-esptoolpy @ ~1.30000.0

View File

@ -1,7 +1,7 @@
/* /*
it-IT.h - localization for Italian - Italy for Tasmota it-IT.h - localization for Italian - Italy for Tasmota
Copyright (C) 2021 Gennaro Tortone - some mods by Antonio Fragola - Updated by bovirus - rev. 27.02.2021 Copyright (C) 2021 Gennaro Tortone - some mods by Antonio Fragola - Updated by bovirus - rev. 03.03.2021
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
@ -673,9 +673,9 @@
#define D_SENSOR_HJL_CF "BL0937 - CF" #define D_SENSOR_HJL_CF "BL0937 - CF"
#define D_SENSOR_MCP39F5_TX "MCP39F5 - TX" #define D_SENSOR_MCP39F5_TX "MCP39F5 - TX"
#define D_SENSOR_MCP39F5_RX "MCP39F5 - RX" #define D_SENSOR_MCP39F5_RX "MCP39F5 - RX"
#define D_SENSOR_MCP39F5_RST "MCP39F5 - Reset" #define D_SENSOR_MCP39F5_RST "MCP39F5 - RESET"
#define D_SENSOR_CSE7761_TX "CSE7761 - Tx" #define D_SENSOR_CSE7761_TX "CSE7761 - TX"
#define D_SENSOR_CSE7761_RX "CSE7761 - Rx" #define D_SENSOR_CSE7761_RX "CSE7761 - RX"
#define D_SENSOR_CSE7766_TX "CSE7766 - TX" #define D_SENSOR_CSE7766_TX "CSE7766 - TX"
#define D_SENSOR_CSE7766_RX "CSE7766 - RX" #define D_SENSOR_CSE7766_RX "CSE7766 - RX"
#define D_SENSOR_PN532_TX "PN532 - TX" #define D_SENSOR_PN532_TX "PN532 - TX"
@ -696,10 +696,10 @@
#define D_SENSOR_HRE_DATA "HRE - Dati" #define D_SENSOR_HRE_DATA "HRE - Dati"
#define D_SENSOR_ADE7953_IRQ "ADE7953 - IRQ" #define D_SENSOR_ADE7953_IRQ "ADE7953 - IRQ"
#define D_SENSOR_BUZZER "Cicalino" #define D_SENSOR_BUZZER "Cicalino"
#define D_SENSOR_OLED_RESET "OLED - Reset" #define D_SENSOR_OLED_RESET "OLED - RESET"
#define D_SENSOR_ZIGBEE_TXD "Zigbee - TX" #define D_SENSOR_ZIGBEE_TXD "Zigbee - TX"
#define D_SENSOR_ZIGBEE_RXD "Zigbee - RX" #define D_SENSOR_ZIGBEE_RXD "Zigbee - RX"
#define D_SENSOR_ZIGBEE_RST "Zigbee - Reset" #define D_SENSOR_ZIGBEE_RST "Zigbee - RESET"
#define D_SENSOR_SOLAXX1_TX "SolaxX1 - TX" #define D_SENSOR_SOLAXX1_TX "SolaxX1 - TX"
#define D_SENSOR_SOLAXX1_RX "SolaxX1 - RX" #define D_SENSOR_SOLAXX1_RX "SolaxX1 - RX"
#define D_SENSOR_IBEACON_TX "iBeacon - TX" #define D_SENSOR_IBEACON_TX "iBeacon - TX"

View File

@ -347,6 +347,10 @@ TaskHandle_t TasmotaMainTask;
static int BLEMasterEnable = 0; static int BLEMasterEnable = 0;
static uint8_t BLEEnableUnsaved = 0;
static uint8_t BLEEnableMask = 1;
static int BLEInitState = 0; static int BLEInitState = 0;
static int BLERunningScan = 0; static int BLERunningScan = 0;
static uint32_t BLEScanCount = 0; static uint32_t BLEScanCount = 0;
@ -391,8 +395,10 @@ BLE_ESP32::generic_sensor_t* prepOperation = nullptr;
std::deque<BLE_ESP32::generic_sensor_t*> queuedOperations; std::deque<BLE_ESP32::generic_sensor_t*> queuedOperations;
// operations in progress (at the moment, only one) // operations in progress (at the moment, only one)
std::deque<BLE_ESP32::generic_sensor_t*> currentOperations; std::deque<BLE_ESP32::generic_sensor_t*> currentOperations;
// operations which have completed or failed, ready to send to MQTT // operations which have completed or failed
std::deque<BLE_ESP32::generic_sensor_t*> completedOperations; std::deque<BLE_ESP32::generic_sensor_t*> completedOperations;
// operations which are ready to send to MQTT
std::deque<BLE_ESP32::generic_sensor_t*> mqttOperations;
// seen devices // seen devices
#define MAX_BLE_DEVICES_LOGGED 80 #define MAX_BLE_DEVICES_LOGGED 80
@ -422,7 +428,7 @@ std::deque<BLE_ESP32::ble_alias_t*> aliases;
#define D_CMND_BLE "BLE" #define D_CMND_BLE "BLE"
const char kBLE_Commands[] PROGMEM = D_CMND_BLE "|" const char kBLE_Commands[] PROGMEM = D_CMND_BLE "|"
"Period|Adv|Op|Mode|Details|Scan|Alias|Name|Debug|Devices|MaxAge|AddrFilter"; "Period|Adv|Op|Mode|Details|Scan|Alias|Name|Debug|Devices|MaxAge|AddrFilter|EnableUnsaved";
static void CmndBLEPeriod(void); static void CmndBLEPeriod(void);
static void CmndBLEAdv(void); static void CmndBLEAdv(void);
@ -436,6 +442,7 @@ static void CmndBLEDebug(void);
static void CmndBLEDevices(void); static void CmndBLEDevices(void);
static void CmndBLEMaxAge(void); static void CmndBLEMaxAge(void);
static void CmndBLEAddrFilter(void); static void CmndBLEAddrFilter(void);
static void CmndBLEEnableUnsaved(void);
void (*const BLE_Commands[])(void) PROGMEM = { void (*const BLE_Commands[])(void) PROGMEM = {
&BLE_ESP32::CmndBLEPeriod, &BLE_ESP32::CmndBLEPeriod,
@ -449,7 +456,8 @@ void (*const BLE_Commands[])(void) PROGMEM = {
&BLE_ESP32::CmndBLEDebug, &BLE_ESP32::CmndBLEDebug,
&BLE_ESP32::CmndBLEDevices, &BLE_ESP32::CmndBLEDevices,
&BLE_ESP32::CmndBLEMaxAge, &BLE_ESP32::CmndBLEMaxAge,
&BLE_ESP32::CmndBLEAddrFilter &BLE_ESP32::CmndBLEAddrFilter,
&BLE_ESP32::CmndBLEEnableUnsaved
}; };
const char *successStates[] PROGMEM = { const char *successStates[] PROGMEM = {
@ -1412,8 +1420,12 @@ static void BLEGenNotifyCB(NimBLERemoteCharacteristic* pRemoteCharacteristic, ui
#endif #endif
} else { } else {
if (devaddr == op->addr){ if (devaddr == op->addr){
thisop = op; if (op->notifytimer){
break; thisop = op;
break;
} else {
AddLog(LOG_LEVEL_ERROR,PSTR("BLE: notify: op addr match but op found which is not waiting."));
}
} }
} }
} }
@ -1511,13 +1523,6 @@ static void BLEInit(void) {
BLEInitState = 1; BLEInitState = 1;
// dont start of disabled
BLEMasterEnable = Settings.flag5.mi32_enable;
if (!BLEMasterEnable) return;
StartBLE();
return; return;
} }
@ -1807,12 +1812,12 @@ static void BLETaskRunCurrentOperation(BLE_ESP32::generic_sensor_t** pCurrentOpe
#endif #endif
op->notifylen = 0; op->notifylen = 0;
if(pNCharacteristic->canNotify()) { if(pNCharacteristic->canNotify()) {
uint64_t now = esp_timer_get_time();
op->notifytimer = now;
if(pNCharacteristic->subscribe(true, BLE_ESP32::BLEGenNotifyCB)) { if(pNCharacteristic->subscribe(true, BLE_ESP32::BLEGenNotifyCB)) {
#ifdef BLE_ESP32_DEBUG #ifdef BLE_ESP32_DEBUG
if (BLEDebugMode > 0) AddLog(LOG_LEVEL_DEBUG,PSTR("BLE: subscribe for notify")); if (BLEDebugMode > 0) AddLog(LOG_LEVEL_DEBUG,PSTR("BLE: subscribe for notify"));
#endif #endif
uint64_t now = esp_timer_get_time();
op->notifytimer = now;
// this will get changed to read or write, // this will get changed to read or write,
// but here in case it's notify only (can that happen?) // but here in case it's notify only (can that happen?)
notifystate = GEN_STATE_WAITNOTIFY; notifystate = GEN_STATE_WAITNOTIFY;
@ -1822,22 +1827,24 @@ static void BLETaskRunCurrentOperation(BLE_ESP32::generic_sensor_t** pCurrentOpe
AddLog(LOG_LEVEL_ERROR,PSTR("BLE: failed subscribe for notify")); AddLog(LOG_LEVEL_ERROR,PSTR("BLE: failed subscribe for notify"));
#endif #endif
newstate = GEN_STATE_FAILED_NOTIFY; newstate = GEN_STATE_FAILED_NOTIFY;
op->notifytimer = 0L;
} }
} else { } else {
if(pNCharacteristic->canIndicate()) { if(pNCharacteristic->canIndicate()) {
uint64_t now = esp_timer_get_time();
op->notifytimer = now;
if(pNCharacteristic->subscribe(false, BLE_ESP32::BLEGenNotifyCB)) { if(pNCharacteristic->subscribe(false, BLE_ESP32::BLEGenNotifyCB)) {
#ifdef BLE_ESP32_DEBUG #ifdef BLE_ESP32_DEBUG
AddLog(LOG_LEVEL_DEBUG,PSTR("BLE: subscribe for indicate")); AddLog(LOG_LEVEL_DEBUG,PSTR("BLE: subscribe for indicate"));
#endif #endif
notifystate = GEN_STATE_WAITINDICATE; notifystate = GEN_STATE_WAITINDICATE;
uint64_t now = esp_timer_get_time();
op->notifytimer = now;
waitNotify = true; waitNotify = true;
} else { } else {
#ifdef BLE_ESP32_DEBUG #ifdef BLE_ESP32_DEBUG
AddLog(LOG_LEVEL_ERROR,PSTR("BLE: failed subscribe for indicate")); AddLog(LOG_LEVEL_ERROR,PSTR("BLE: failed subscribe for indicate"));
#endif #endif
newstate = GEN_STATE_FAILED_INDICATE; newstate = GEN_STATE_FAILED_INDICATE;
op->notifytimer = 0L;
} }
} else { } else {
newstate = GEN_STATE_FAILED_CANTNOTIFYORINDICATE; newstate = GEN_STATE_FAILED_CANTNOTIFYORINDICATE;
@ -2147,6 +2154,23 @@ void BLEEvery50mSecond(){
} }
static void stopStartBLE(){
// dont start of disabled
uint8_t enable = (Settings.flag5.mi32_enable || BLEEnableUnsaved) && BLEEnableMask;
if (enable != BLEMasterEnable){
if (enable){
if (StartBLE()){
BLEMasterEnable = enable;
}
} else {
if (StopBLE()){
BLEMasterEnable = enable;
}
}
AddLog(LOG_LEVEL_INFO,PSTR("BLE: MasterEnable->%d"), BLEMasterEnable);
}
}
/** /**
* @brief Main loop of the driver, "high level"-loop * @brief Main loop of the driver, "high level"-loop
@ -2159,27 +2183,14 @@ static void BLEEverySecond(bool restart){
checkDeviceTimouts(); checkDeviceTimouts();
stopStartBLE();
if (Settings.flag5.mi32_enable != BLEMasterEnable){
if (Settings.flag5.mi32_enable){
if (StartBLE()){
BLEMasterEnable = Settings.flag5.mi32_enable;
}
} else {
if (StopBLE()){
BLEMasterEnable = Settings.flag5.mi32_enable;
}
}
AddLog(LOG_LEVEL_INFO,PSTR("BLE: MasterEnable->%d"), BLEMasterEnable);
}
// check for application callbacks here. // check for application callbacks here.
// this may remove complete items. // this may remove complete items.
BLE_ESP32::mainThreadOpCallbacks(); BLE_ESP32::mainThreadOpCallbacks();
// post any MQTT data if we completed anything in the last second // post any MQTT data if we completed anything in the last second
if (completedOperations.size()){ if (mqttOperations.size()){
BLE_ESP32::BLEPostMQTT(true); // send only completed BLE_ESP32::BLEPostMQTT(true); // send only completed
} }
@ -2314,12 +2325,18 @@ int extQueueOperation(BLE_ESP32::generic_sensor_t** op){
AddLog(LOG_LEVEL_ERROR,PSTR("BLE: op invalid")); AddLog(LOG_LEVEL_ERROR,PSTR("BLE: op invalid"));
return 0; return 0;
} }
if (!BLEMasterEnable){
AddLog(LOG_LEVEL_ERROR,PSTR("BLE: extQueueOperation: BLE is deiabled"));
return 0;
}
(*op)->state = GEN_STATE_START; // trigger request later (*op)->state = GEN_STATE_START; // trigger request later
(*op)->opid = lastopid++; (*op)->opid = lastopid++;
int res = addOperation(&queuedOperations, op); int res = addOperation(&queuedOperations, op);
if (!res){ if (!res){
AddLog(LOG_LEVEL_ERROR,PSTR("BLE: extQueueOperation: op added id %d failed"), (lastopid-1)); AddLog(LOG_LEVEL_ERROR,PSTR("BLE: extQueueOperation: op adding id %d failed"), (lastopid-1));
} }
return res; return res;
} }
@ -2451,7 +2468,7 @@ static int StopBLE(void){
AddLog(LOG_LEVEL_INFO,PSTR("BLE: StopBLE - BLEStop->1")); AddLog(LOG_LEVEL_INFO,PSTR("BLE: StopBLE - BLEStop->1"));
BLEStopAt = esp_timer_get_time(); BLEStopAt = esp_timer_get_time();
// give a little time for it to stop. // give a little time for it to stop.
vTaskDelay(1000/ portTICK_PERIOD_MS); vTaskDelay(100/ portTICK_PERIOD_MS);
return 1; return 1;
} }
AddLog(LOG_LEVEL_ERROR,PSTR("BLE: StopBLE - wait as BLEStop==1")); AddLog(LOG_LEVEL_ERROR,PSTR("BLE: StopBLE - wait as BLEStop==1"));
@ -2654,6 +2671,21 @@ void CmndBLEMode(void){
} }
} }
//////////////////////////////////////////////////////////////
// Enables BLE even if master enable is unset
// use to temporarily enable after boot - e.g. at the end of autoexec
void CmndBLEEnableUnsaved(void){
int val = -1;
if (XdrvMailbox.data_len > 0) {
val = XdrvMailbox.payload;
}
if (val >= 0){
BLEEnableUnsaved = val;
}
ResponseCmndNumber(BLEEnableUnsaved);
}
////////////////////////////////////////// //////////////////////////////////////////
// get more drtails for a single MAC address // get more drtails for a single MAC address
@ -3036,7 +3068,7 @@ static void BLEPostMQTT(bool onlycompleted) {
// if (TasmotaGlobal.ota_state_flag) return; // if (TasmotaGlobal.ota_state_flag) return;
if (prepOperation || completedOperations.size() || queuedOperations.size() || currentOperations.size()){ if (prepOperation || mqttOperations.size() || queuedOperations.size() || currentOperations.size()){
#ifdef BLE_ESP32_DEBUG #ifdef BLE_ESP32_DEBUG
if (BLEDebugMode > 0) AddLog(LOG_LEVEL_INFO,PSTR("BLE: some to show")); if (BLEDebugMode > 0) AddLog(LOG_LEVEL_INFO,PSTR("BLE: some to show"));
#endif #endif
@ -3094,17 +3126,17 @@ static void BLEPostMQTT(bool onlycompleted) {
} }
} }
if (completedOperations.size()){ if (mqttOperations.size()){
#ifdef BLE_ESP32_DEBUG #ifdef BLE_ESP32_DEBUG
if (BLEDebugMode > 0) AddLog(LOG_LEVEL_INFO,PSTR("BLE: completed %d"), completedOperations.size()); if (BLEDebugMode > 0) AddLog(LOG_LEVEL_INFO,PSTR("BLE: completed %d"), mqttOperations.size());
#endif #endif
do { do {
generic_sensor_t *toSend = nextOperation(&completedOperations); generic_sensor_t *toSend = nextOperation(&mqttOperations);
if (!toSend) { if (!toSend) {
break; // break from while loop break; // break from while loop
} else { } else {
#ifdef BLE_ESP32_DEBUG #ifdef BLE_ESP32_DEBUG
if (BLEDebugMode > 0) AddLog(LOG_LEVEL_DEBUG,PSTR("BLE: completedOperation removed")); if (BLEDebugMode > 0) AddLog(LOG_LEVEL_DEBUG,PSTR("BLE: mqttOperation removed opid %d"), toSend->opid);
#endif #endif
std::string out = BLETriggerResponse(toSend); std::string out = BLETriggerResponse(toSend);
snprintf_P(TasmotaGlobal.mqtt_data, sizeof(TasmotaGlobal.mqtt_data), PSTR("%s"), out.c_str()); snprintf_P(TasmotaGlobal.mqtt_data, sizeof(TasmotaGlobal.mqtt_data), PSTR("%s"), out.c_str());
@ -3176,12 +3208,13 @@ static void mainThreadOpCallbacks() {
bool callbackres = false; bool callbackres = false;
if (BLEDebugMode > 0) AddLog(LOG_LEVEL_DEBUG,PSTR("BLE: op->completecallback is %u opid %d"), op->completecallback, op->opid);
if (op->completecallback){ if (op->completecallback){
try { try {
OPCOMPLETE_CALLBACK *pFn = (OPCOMPLETE_CALLBACK *)(op->completecallback); OPCOMPLETE_CALLBACK *pFn = (OPCOMPLETE_CALLBACK *)(op->completecallback);
callbackres = pFn(op); callbackres = pFn(op);
#ifdef BLE_ESP32_DEBUG #ifdef BLE_ESP32_DEBUG
if (BLEDebugMode > 0) AddLog(LOG_LEVEL_DEBUG,PSTR("BLE: op->completecallback %d"), callbackres); if (BLEDebugMode > 0) AddLog(LOG_LEVEL_DEBUG,PSTR("BLE: op->completecallback %d opid %d"), callbackres, op->opid);
#endif #endif
} catch(const std::exception& e){ } catch(const std::exception& e){
#ifdef BLE_ESP32_DEBUG #ifdef BLE_ESP32_DEBUG
@ -3193,6 +3226,7 @@ static void mainThreadOpCallbacks() {
if (!callbackres){ if (!callbackres){
for (int i = 0; i < operationsCallbacks.size(); i++){ for (int i = 0; i < operationsCallbacks.size(); i++){
try { try {
if (BLEDebugMode > 0) AddLog(LOG_LEVEL_DEBUG,PSTR("BLE: operationsCallbacks %d is %u"), i, operationsCallbacks[i]);
OPCOMPLETE_CALLBACK *pFn = operationsCallbacks[i]; OPCOMPLETE_CALLBACK *pFn = operationsCallbacks[i];
callbackres = pFn(op); callbackres = pFn(op);
#ifdef BLE_ESP32_DEBUG #ifdef BLE_ESP32_DEBUG
@ -3209,12 +3243,16 @@ static void mainThreadOpCallbacks() {
} }
} }
// if some callback told us not to send on MQTT, then remove from completed and delete the data // always remove from here
if (callbackres){ completedOperations.erase(completedOperations.begin() + i);
// unless some callback told us not to send on MQTT, then remove from completed and
// add to mqtt list
if (!callbackres){
addOperation(&mqttOperations, &op);
} else {
#ifdef BLE_ESP32_DEBUG #ifdef BLE_ESP32_DEBUG
if (BLEDebugMode > 0) AddLog(LOG_LEVEL_DEBUG,PSTR("BLE: callbackres true -> delete op")); if (BLEDebugMode > 0) AddLog(LOG_LEVEL_DEBUG,PSTR("BLE: callbackres true -> delete op opid %d"), op->opid);
#endif #endif
completedOperations.erase(completedOperations.begin() + i);
delete op; delete op;
} }
} }
@ -3462,8 +3500,15 @@ void HandleBleConfiguration(void)
int ExtStopBLE(){ int ExtStopBLE(){
AddLog(LOG_LEVEL_INFO, PSTR("BLE: Stopping if active")); AddLog(LOG_LEVEL_INFO, PSTR("BLE: Stopping if active"));
BLE_ESP32::BLEMode = BLE_ESP32::BLEModeDisabled; BLE_ESP32::BLEEnableMask = 0;
BLE_ESP32::StopBLE(); BLE_ESP32::stopStartBLE();
return 0;
}
int ExtRestartBLEIfEnabled(){
AddLog(LOG_LEVEL_INFO, PSTR("BLE: Starting if active"));
BLE_ESP32::BLEEnableMask = 1;
BLE_ESP32::stopStartBLE();
return 0; return 0;
} }

View File

@ -25,25 +25,63 @@
* Based on datasheet from ChipSea * Based on datasheet from ChipSea
\*********************************************************************************************/ \*********************************************************************************************/
#define XNRG_19 19 #define XNRG_19 19
#define CSE7761_K1 2 // Current channel sampling resistance in milli Ohm #define CSE7761_REMOVE_CHECKS
#define CSE7761_K2 2 // Voltage divider resistance in 1k/1M
#define CSE7761_2POWER22 4194304 #define CSE7761_DUAL_K1 2 // Current channel sampling resistance in milli Ohm
#define CSE7761_2POWER23 8388608 #define CSE7761_DUAL_K2 2 // Voltage divider resistance in 1k/1M
#define CSE7761_2POWER31 2147483648 #define CSE7761_DUAL_CLK1 3579545.0f // System clock (3.579545MHz) as used in frequency calculation
enum CSE7761 { RmsIAC, RmsIBC, RmsUC, PowerPAC, PowerPBC, PowerSC, EnergyAc, EnergyBC }; #define CSE7761_2POWER22 4194304
#define CSE7761_2POWER23 8388608
#define CSE7761_2POWER31 2147483648
#define CSE7761_REG_SYSCON 0x00 // System Control Register
#define CSE7761_REG_EMUCON 0x01 // Metering control register
#define CSE7761_REG_EMUCON2 0x13 // Metering control register 2
#define CSE7761_REG_UFREQ 0x23 // Voltage Frequency Register
#define CSE7761_REG_RMSIA 0x24 // The effective value of channel A current
#define CSE7761_REG_RMSIB 0x25 // The effective value of channel B current
#define CSE7761_REG_RMSU 0x26 // Voltage RMS
#define CSE7761_REG_POWERPA 0x2C // Channel A active power, update rate 27.2Hz
#define CSE7761_REG_POWERPB 0x2D // Channel B active power, update rate 27.2Hz
#define CSE7761_REG_SYSSTATUS 0x43 // System status register
#define CSE7761_REG_COEFFOFFSET 0x6E // Coefficient checksum offset (0xFFFF)
#define CSE7761_REG_COEFFCHKSUM 0x6F // Coefficient checksum
#define CSE7761_REG_RMSIAC 0x70 // Channel A effective current conversion coefficient
#define CSE7761_REG_RMSIBC 0x71 // Channel B effective current conversion coefficient
#define CSE7761_REG_RMSUC 0x72 // Effective voltage conversion coefficient
#define CSE7761_REG_POWERPAC 0x73 // Channel A active power conversion coefficient
#define CSE7761_REG_POWERPBC 0x74 // Channel B active power conversion coefficient
#define CSE7761_REG_POWERSC 0x75 // Apparent power conversion coefficient
#define CSE7761_REG_ENERGYAC 0x76 // Channel A energy conversion coefficient
#define CSE7761_REG_ENERGYBC 0x77 // Channel B energy conversion coefficient
#define CSE7761_SPECIAL_COMMAND 0xEA // Start special command
#define CSE7761_CMD_RESET 0x96 // Reset command, after receiving the command, the chip resets
#define CSE7761_CMD_CHAN_A_SELECT 0x5A // Current channel A setting command, which specifies the current used to calculate apparent power,
// Power factor, phase angle, instantaneous active power, instantaneous apparent power and
// The channel indicated by the signal of power overload is channel A
#define CSE7761_CMD_CHAN_B_SELECT 0xA5 // Current channel B setting command, which specifies the current used to calculate apparent power,
// Power factor, phase angle, instantaneous active power, instantaneous apparent power and
// The channel indicated by the signal of power overload is channel B
#define CSE7761_CMD_CLOSE_WRITE 0xDC // Close write operation
#define CSE7761_CMD_ENABLE_WRITE 0xE5 // Enable write operation
enum CSE7761 { RmsIAC, RmsIBC, RmsUC, PowerPAC, PowerPBC, PowerSC, EnergyAC, EnergyBC };
#include <TasmotaSerial.h> #include <TasmotaSerial.h>
TasmotaSerial *Cse7761Serial = nullptr; TasmotaSerial *Cse7761Serial = nullptr;
struct { struct {
uint32_t frequency = 0;
uint32_t voltage_rms = 0; uint32_t voltage_rms = 0;
uint32_t current_rms[2] = { 0 }; uint32_t current_rms[2] = { 0 };
uint32_t active_power[2] = { 0 }; int active_power[2] = { 0 };
uint16_t coefficient[8] = { 0 }; uint16_t coefficient[8] = { 0 };
uint8_t init = 0; uint8_t init = 0;
bool found = false; bool found = false;
@ -74,10 +112,10 @@ void Cse7761Write(uint32_t reg, uint32_t data) {
Cse7761Serial->write(buffer, len); Cse7761Serial->write(buffer, len);
AddLog(LOG_LEVEL_DEBUG, PSTR("C61: Send %d, Data %*_H"), len, len, buffer); AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("C61: Send %d, Data %*_H"), len, len, buffer);
} }
uint32_t Cse7761Read(uint32_t reg, uint32_t request) { uint32_t Cse7761Read(uint32_t reg) {
Cse7761Serial->flush(); Cse7761Serial->flush();
Cse7761Write(reg, 0); Cse7761Write(reg, 0);
@ -92,21 +130,31 @@ uint32_t Cse7761Read(uint32_t reg, uint32_t request) {
} }
if (!rcvd) { if (!rcvd) {
AddLog(LOG_LEVEL_DEBUG, PSTR("C61: Rcvd %d"), rcvd); AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("C61: Rcvd %d"), rcvd);
return 0; return 0;
} }
AddLog(LOG_LEVEL_DEBUG, PSTR("C61: Rcvd %d, Data %*_H"), rcvd, rcvd, buffer); AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("C61: Rcvd %d, Data %*_H"), rcvd, rcvd, buffer);
int result = 0; #ifndef CSE7761_REMOVE_CHECKS
if (rcvd > 5) {
return 0;
}
#endif
rcvd--;
uint32_t result = 0;
uint8_t crc = 0xA5 + reg; uint8_t crc = 0xA5 + reg;
for (uint32_t i = 0; i < rcvd -1; i++) { for (uint32_t i = 0; i < rcvd; i++) {
result = (result << 8) | buffer[i]; result = (result << 8) | buffer[i];
crc += buffer[i]; crc += buffer[i];
} }
crc = ~crc; crc = ~crc;
if (crc != buffer[rcvd]) { if (crc != buffer[rcvd]) {
result = 0; AddLog(LOG_LEVEL_DEBUG, PSTR("C61: CRC error"));
#ifndef CSE7761_REMOVE_CHECKS
return 0;
#endif
} }
return result; return result;
@ -115,22 +163,23 @@ uint32_t Cse7761Read(uint32_t reg, uint32_t request) {
bool Cse7761ChipInit(void) { bool Cse7761ChipInit(void) {
uint16_t calc_chksum = 0xFFFF; uint16_t calc_chksum = 0xFFFF;
for (uint32_t i = 0; i < 8; i++) { for (uint32_t i = 0; i < 8; i++) {
CSE7761Data.coefficient[i] = Cse7761Read(0x70 + i, 2); CSE7761Data.coefficient[i] = Cse7761Read(CSE7761_REG_RMSIAC + i);
calc_chksum += CSE7761Data.coefficient[i]; calc_chksum += CSE7761Data.coefficient[i];
} }
uint16_t dummy = Cse7761Read(0x6E, 2); calc_chksum = ~calc_chksum;
uint16_t coeff_chksum = Cse7761Read(0x6F, 2); uint16_t dummy = Cse7761Read(CSE7761_REG_COEFFOFFSET);
uint16_t coeff_chksum = Cse7761Read(CSE7761_REG_COEFFCHKSUM);
if (calc_chksum != coeff_chksum) { if (calc_chksum != coeff_chksum) {
AddLog(LOG_LEVEL_DEBUG, PSTR("C61: Coefficients invalid")); AddLog(LOG_LEVEL_DEBUG, PSTR("C61: Coefficients CRC error"));
// return false; #ifndef CSE7761_REMOVE_CHECKS
return false;
#endif
} }
delay(3); Cse7761Write(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_ENABLE_WRITE);
Cse7761Write(0xEA, 0xE5); // Enable write operation delay(8);
delay(5); uint8_t sys_status = Cse7761Read(CSE7761_REG_SYSSTATUS);
uint8_t sys_status = Cse7761Read(0x43, 1);
if (sys_status & 0x10) { // Write enable to protected registers (WREN) if (sys_status & 0x10) { // Write enable to protected registers (WREN)
delay(3);
/* /*
System Control Register (SYSCON) Addr:0x00 Default value: 0x0A04 System Control Register (SYSCON) Addr:0x00 Default value: 0x0A04
Bit name Function description Bit name Function description
@ -140,25 +189,25 @@ bool Cse7761ChipInit(void) {
=0, means ADC current channel B is closed =0, means ADC current channel B is closed
9 NC -, the default is 1. 9 NC -, the default is 1.
8-6 PGAIB[2:0] Current channel B analog gain selection highest bit 8-6 PGAIB[2:0] Current channel B analog gain selection highest bit
=1XX, PGA of current channel B=16 =1XX, PGA of current channel B=16 (Sonoff Dual R3 Pow)
=011, PGA of current channel B=8 =011, PGA of current channel B=8
=010, PGA of current channel B=4 =010, PGA of current channel B=4
=001, PGA of current channel B=2 =001, PGA of current channel B=2
=000, PGA of current channel B=1 (Sonoff Dual R3 Pow) =000, PGA of current channel B=1
5-3 PGAU[2:0] Highest bit of voltage channel analog gain selection 5-3 PGAU[2:0] Highest bit of voltage channel analog gain selection
=1XX, PGA of current channel U=16 =1XX, PGA of current channel U=16
=011, PGA of current channel U=8 =011, PGA of current channel U=8
=010, PGA of current channel U=4 =010, PGA of current channel U=4
=001, PGA of current channel U=2 (Sonoff Dual R3 Pow) =001, PGA of current channel U=2
=000, PGA of current channel U=1 =000, PGA of current channel U=1 (Sonoff Dual R3 Pow)
2-0 PGAIA[2:0] Current channel A analog gain selection highest bit 2-0 PGAIA[2:0] Current channel A analog gain selection highest bit
=1XX, PGA of current channel A=16 =1XX, PGA of current channel A=16 (Sonoff Dual R3 Pow)
=011, PGA of current channel A=8 =011, PGA of current channel A=8
=010, PGA of current channel A=4 =010, PGA of current channel A=4
=001, PGA of current channel A=2 =001, PGA of current channel A=2
=000, PGA of current channel A=1 (Sonoff Dual R3 Pow) =000, PGA of current channel A=1
*/ */
Cse7761Write(0x80, 0xFF04); // Set SYSCON Cse7761Write(CSE7761_REG_SYSCON | 0x80, 0xFF04);
/* /*
Energy Measure Control Register (EMUCON) Addr:0x01 Default value: 0x0000 Energy Measure Control Register (EMUCON) Addr:0x01 Default value: 0x0000
Bit name Function description Bit name Function description
@ -205,14 +254,14 @@ bool Cse7761ChipInit(void) {
=1, enable PFA pulse output and active energy register accumulation; (Sonoff Dual R3 Pow) =1, enable PFA pulse output and active energy register accumulation; (Sonoff Dual R3 Pow)
=0 (default), turn off PFA pulse output and active energy register accumulation. =0 (default), turn off PFA pulse output and active energy register accumulation.
*/ */
Cse7761Write(0x81, 0x1003); // Set EMUCON Cse7761Write(CSE7761_REG_EMUCON | 0x80, 0x1003);
/* /*
Energy Measure Control Register (EMUCON2) Addr: 0x13 Default value: 0x0001 Energy Measure Control Register (EMUCON2) Addr: 0x13 Default value: 0x0001
Bit name Function description Bit name Function description
15-13 NC - 15-13 NC -
12 SDOCmos 12 SDOCmos
=1, SDO pin CMOS open-drain output (Sonoff Dual R3 Pow) =1, SDO pin CMOS open-drain output
=0, SDO pin CMOS output =0, SDO pin CMOS output (Sonoff Dual R3 Pow)
11 EPB_CB Energy_PB clear signal control, the default is 0, and it needs to be configured to 1 in UART mode. 11 EPB_CB Energy_PB clear signal control, the default is 0, and it needs to be configured to 1 in UART mode.
Clear after reading is not supported in UART mode Clear after reading is not supported in UART mode
=1, Energy_PB will not be cleared after reading; (Sonoff Dual R3 Pow) =1, Energy_PB will not be cleared after reading; (Sonoff Dual R3 Pow)
@ -249,33 +298,50 @@ bool Cse7761ChipInit(void) {
=0, turn off the peak detection function (Sonoff Dual R3 Pow) =0, turn off the peak detection function (Sonoff Dual R3 Pow)
0 NC Default is 1 0 NC Default is 1
*/ */
Cse7761Write(0x93, 0x0FC1); // Set EMUCON2 Cse7761Write(CSE7761_REG_EMUCON2 | 0x80, 0x0FC1);
} else { } else {
AddLog(LOG_LEVEL_DEBUG, PSTR("C61: Write enable failed")); AddLog(LOG_LEVEL_DEBUG, PSTR("C61: Write enable failed"));
// return false; #ifndef CSE7761_REMOVE_CHECKS
return false;
#endif
} }
delay(80); delay(80);
Cse7761Write(0xEA, 0xDC); // Close write operation Cse7761Write(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_CLOSE_WRITE);
return true; return true;
} }
void Cse7761GetData(void) { void Cse7761GetData(void) {
CSE7761Data.voltage_rms = Cse7761Read(0x26, 3); CSE7761Data.voltage_rms = Cse7761Read(CSE7761_REG_RMSU);
CSE7761Data.current_rms[0] = Cse7761Read(0x24, 3); CSE7761Data.frequency = Cse7761Read(CSE7761_REG_UFREQ);
CSE7761Data.active_power[0] = Cse7761Read(0x2C, 4); CSE7761Data.current_rms[0] = Cse7761Read(CSE7761_REG_RMSIA);
CSE7761Data.current_rms[1] = Cse7761Read(0x25, 3); CSE7761Data.active_power[0] = Cse7761Read(CSE7761_REG_POWERPA);
CSE7761Data.active_power[1] = Cse7761Read(0x2D, 4); CSE7761Data.current_rms[1] = Cse7761Read(CSE7761_REG_RMSIB);
CSE7761Data.active_power[1] = Cse7761Read(CSE7761_REG_POWERPB);
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("C61: U %d, F %d, I %d/%d, P %d/%d"),
CSE7761Data.voltage_rms, CSE7761Data.frequency,
CSE7761Data.current_rms[0], CSE7761Data.current_rms[1],
CSE7761Data.active_power[0], CSE7761Data.active_power[1]);
// The effective value of current and voltage Rms is a 24-bit signed number, the highest bit is 0 for valid data,
// and when the highest bit is 1, the reading will be processed as zero
if (CSE7761Data.voltage_rms & 0x800000) { CSE7761Data.voltage_rms = 0; }
if (CSE7761Data.current_rms[0] & 0x800000) { CSE7761Data.current_rms[0] = 0; }
if (CSE7761Data.current_rms[1] & 0x800000) { CSE7761Data.current_rms[1] = 0; }
// The active power parameter PowerA/B is in twos complement format, 32-bit data, the highest bit is Sign bit.
if (Energy.power_on) { // Powered on if (Energy.power_on) { // Powered on
Energy.voltage[0] = ((float)CSE7761Data.voltage_rms * ((double)CSE7761Data.coefficient[RmsUC] / (CSE7761_K2 * 2 * CSE7761_2POWER22))) / 1000; // V Energy.voltage[0] = ((float)CSE7761Data.voltage_rms * ((double)CSE7761Data.coefficient[RmsUC] / (CSE7761_DUAL_K2 * 2 * CSE7761_2POWER22))) / 1000; // V
Energy.frequency[0] = CSE7761_DUAL_CLK1 / 8 / ((float)CSE7761Data.frequency + 1);
for (uint32_t channel = 0; channel < 2; channel++) { for (uint32_t channel = 0; channel < 2; channel++) {
Energy.data_valid[channel] = 0; Energy.data_valid[channel] = 0;
Energy.active_power[channel] = (float)CSE7761Data.active_power[channel] * ((double)CSE7761Data.coefficient[PowerPAC + channel] / (CSE7761_K1 * CSE7761_K2 * 2 * CSE7761_2POWER31)); // W Energy.active_power[channel] = (float)CSE7761Data.active_power[channel] * ((double)CSE7761Data.coefficient[PowerPAC + channel] / (CSE7761_DUAL_K1 * CSE7761_DUAL_K2 * 2 * CSE7761_2POWER31)); // W
if (0 == Energy.active_power[channel]) { if (0 == Energy.active_power[channel]) {
Energy.current[channel] = 0; Energy.current[channel] = 0;
} else { } else {
Energy.current[channel] = (float)CSE7761Data.current_rms[channel] * ((double)CSE7761Data.coefficient[RmsIAC + channel] / (CSE7761_K1 * 2 * CSE7761_2POWER23)); // mA Energy.current[channel] = (float)CSE7761Data.current_rms[channel] * ((double)CSE7761Data.coefficient[RmsIAC + channel] / (CSE7761_DUAL_K1 * 2 * CSE7761_2POWER23)); // mA
} }
} }
@ -295,13 +361,17 @@ void Cse7761GetData(void) {
void Cse7761EverySecond(void) { void Cse7761EverySecond(void) {
if (CSE7761Data.init) { if (CSE7761Data.init) {
if (2 == CSE7761Data.init) { if (2 == CSE7761Data.init) {
Cse7761Write(0xEA, 0x96); // Reset chip Cse7761Write(CSE7761_SPECIAL_COMMAND, CSE7761_CMD_RESET);
} }
else if (1 == CSE7761Data.init) { else if (1 == CSE7761Data.init) {
uint16_t syscon = Cse7761Read(0x00, 2); // Default 0x0A04 uint16_t syscon = Cse7761Read(0x00); // Default 0x0A04
#ifndef CSE7761_REMOVE_CHECKS
if (0x0A04 == syscon) { if (0x0A04 == syscon) {
CSE7761Data.found = Cse7761ChipInit(); CSE7761Data.found = Cse7761ChipInit();
} }
#else
CSE7761Data.found = Cse7761ChipInit();
#endif
if (CSE7761Data.found) { if (CSE7761Data.found) {
AddLog(LOG_LEVEL_INFO, PSTR("C61: CSE7761 found")); AddLog(LOG_LEVEL_INFO, PSTR("C61: CSE7761 found"));
} }
@ -320,7 +390,7 @@ void Cse7761SnsInit(void) {
Cse7761Serial = new TasmotaSerial(Pin(GPIO_CSE7761_RX), Pin(GPIO_CSE7761_TX), 1); Cse7761Serial = new TasmotaSerial(Pin(GPIO_CSE7761_RX), Pin(GPIO_CSE7761_TX), 1);
if (Cse7761Serial->begin(38400, SERIAL_8E1)) { if (Cse7761Serial->begin(38400, SERIAL_8E1)) {
if (Cse7761Serial->hardwareSerial()) { if (Cse7761Serial->hardwareSerial()) {
// SetSerial(38400, TS_SERIAL_8E1); SetSerial(38400, TS_SERIAL_8E1);
ClaimSerial(); ClaimSerial();
} }
} else { } else {
@ -334,6 +404,7 @@ void Cse7761DrvInit(void) {
CSE7761Data.init = 3; // Init setup steps CSE7761Data.init = 3; // Init setup steps
Energy.phase_count = 2; // Handle two channels as two phases Energy.phase_count = 2; // Handle two channels as two phases
Energy.voltage_common = true; // Use common voltage Energy.voltage_common = true; // Use common voltage
Energy.frequency_common = true; // Use common frequency
TasmotaGlobal.energy_driver = XNRG_19; TasmotaGlobal.energy_driver = XNRG_19;
} }
} }