From 87a3d11c45a3563ac2b3a98f795b7a9c31224aa1 Mon Sep 17 00:00:00 2001 From: proddy Date: Sun, 14 Jun 2020 14:41:23 +0200 Subject: [PATCH 1/7] added stepts to upload firmware --- README.md | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ec6d85222..759be3396 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,17 @@ Note: Version 2.0 is not backward compatible with v1.0. The File system structur - LED behaves like in 1.9. A solid LED means good connection and EMS data is coming in. A slow pulse means either the WiFi or the EMS bus is not connected. A very fast pulse is when the system is booting up and configuring itself. +### Uploading the firmware + +- If you don't have Python 3.8, install it (https://www.python.org/downloads/) +#### Using direct to USB: +- install `esptool` using the command `pip install esptool` +- connect an ESP8266 to the USB. Figure out which COM port it's on (e.g. windows device manager) +- `esptool.py -p COM6 -b 921600 write_flash 0x00000 ` +#### Using OTA (Over The Air): +- download `espota` from https://github.com/esp8266/Arduino/blob/master/tools/espota.py +- `espota.py --debug --progress --port 8266 --auth neo -i -f ` + ### Setting up for the first time: - connect the ESP8266/ESP32 via USB and enter via the serial/com port with baud 115200 @@ -46,16 +57,14 @@ Note: Version 2.0 is not backward compatible with v1.0. The File system structur - reboot and next time use the Telnet via WiFi to connect as the serial mode will be disabled. ### Debugging - - Turn on logging with either `log all` or `log trace` or `log debug` + - Turn on logging with either `log all` or `log debug` - Error messages are shown in the color red - type `show` from the main root to see if any data has come in - if not, go to the `ems` context and type `show` which will display some EMS bus stats - use the `refresh` command to fetch new data from the EMS bus - use `scan devices` or `scan devices deep` to locate devices on the EMS bus. If any are unknown please report back to the project so we can update our EMS device library. - - -# Full Console Commands +# List of console commands ``` common commands available in all contexts: From ac8efc410837dd4ca09417eb95ddbd98eb5b47ea Mon Sep 17 00:00:00 2001 From: proddy Date: Sun, 14 Jun 2020 14:41:39 +0200 Subject: [PATCH 2/7] bump to version a19 --- src/version.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/version.h b/src/version.h index 5393f3853..576e7d220 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define EMSESP_APP_VERSION "2.0.0a18" +#define EMSESP_APP_VERSION "2.0.0a19" From 1283657fc036167aaddb88d50951024a0558c9e2 Mon Sep 17 00:00:00 2001 From: proddy Date: Sun, 14 Jun 2020 14:42:43 +0200 Subject: [PATCH 3/7] clang formatting --- src/devices/boiler.cpp | 2 +- src/devices/mixing.cpp | 5 +++++ src/devices/thermostat.cpp | 6 +++--- src/devices/thermostat.h | 30 +++++++++++++------------- src/helpers.h | 2 +- src/roomcontrol.cpp | 6 +++--- src/roomcontrol.h | 1 - src/uart/emsuart_esp32.cpp | 32 +++++++++++++-------------- src/uart/emsuart_esp32.h | 11 +++++----- src/uart/emsuart_esp8266.cpp | 42 ++++++++++++------------------------ 10 files changed, 63 insertions(+), 74 deletions(-) diff --git a/src/devices/boiler.cpp b/src/devices/boiler.cpp index 031b74333..a69fc383e 100644 --- a/src/devices/boiler.cpp +++ b/src/devices/boiler.cpp @@ -680,7 +680,7 @@ void Boiler::process_UBAMaintenanceStatus(std::shared_ptr telegr void Boiler::process_UBAMaintenanceSettings(std::shared_ptr telegram) { } -// 0x10, 0x11, 0x12 +// 0x10, 0x11, 0x12 // not yet implemented void Boiler::process_UBAErrorMessage(std::shared_ptr telegram) { // data: displaycode(2), errornumner(2), year, month, hour, day, minute, duration(2), src-addr diff --git a/src/devices/mixing.cpp b/src/devices/mixing.cpp index 1ef7dd385..7fc0dadf6 100644 --- a/src/devices/mixing.cpp +++ b/src/devices/mixing.cpp @@ -162,6 +162,9 @@ void Mixing::process_MMStatusMessage(std::shared_ptr telegram) { telegram->read_value(flowSetTemp_, 0); } +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" + // Mixing on a MM10 - 0xAA // e.g. Thermostat -> Mixing Module, type 0xAA, telegram: 10 21 AA 00 FF 0C 0A 11 0A 32 xx void Mixing::process_MMConfigMessage(std::shared_ptr telegram) { @@ -177,4 +180,6 @@ void Mixing::process_MMSetMessage(std::shared_ptr telegram) { // pos 1: position in % } +#pragma GCC diagnostic pop + } // namespace emsesp \ No newline at end of file diff --git a/src/devices/thermostat.cpp b/src/devices/thermostat.cpp index f50e29927..c25bc0997 100644 --- a/src/devices/thermostat.cpp +++ b/src/devices/thermostat.cpp @@ -816,8 +816,8 @@ void Thermostat::show_values(uuid::console::Shell & shell) { if (flags == EMS_DEVICE_FLAG_RC35) { print_value(shell, 2, F("Damped Outdoor temperature"), F_(degrees), Helpers::render_value(buffer, dampedoutdoortemp, 1)); - print_value(shell, 2, F("Tempsensor 1"), F_(degrees), Helpers::render_value(buffer, tempsensor1, 10)); - print_value(shell, 2, F("Tempsensor 2"), F_(degrees), Helpers::render_value(buffer, tempsensor2, 10)); + print_value(shell, 2, F("Temp sensor 1"), F_(degrees), Helpers::render_value(buffer, tempsensor1, 10)); + print_value(shell, 2, F("Temp sensor 2"), F_(degrees), Helpers::render_value(buffer, tempsensor2, 10)); } if (flags == EMS_DEVICE_FLAG_RC30_1) { // settings parameters @@ -1042,7 +1042,7 @@ void Thermostat::process_RC300Set(std::shared_ptr telegram) { // manual is position 10 // comfort is position 2 // I think auto is position 8? - // actual setpoint taken from RC300Monitor (Michael 12.06.2020) + // actual setpoint taken from RC300Monitor (Michael 12.06.2020) // telegram->read_value8(hc->setpoint_roomTemp, 8); // single byte conversion, value is * 2 - auto? // telegram->read_value8(hc->setpoint_roomTemp, 10); // single byte conversion, value is * 2 - manual diff --git a/src/devices/thermostat.h b/src/devices/thermostat.h index 455418339..e27afa2a7 100644 --- a/src/devices/thermostat.h +++ b/src/devices/thermostat.h @@ -46,21 +46,21 @@ class Thermostat : public EMSdevice { } ~HeatingCircuit() = default; - int16_t setpoint_roomTemp = EMS_VALUE_SHORT_NOTSET; - int16_t curr_roomTemp = EMS_VALUE_SHORT_NOTSET; - uint8_t mode = EMS_VALUE_UINT_NOTSET; - uint8_t mode_type = EMS_VALUE_UINT_NOTSET; - uint8_t summer_mode = EMS_VALUE_UINT_NOTSET; - uint8_t holiday_mode = EMS_VALUE_UINT_NOTSET; - uint8_t daytemp = EMS_VALUE_UINT_NOTSET; - uint8_t nighttemp = EMS_VALUE_UINT_NOTSET; - uint8_t holidaytemp = EMS_VALUE_UINT_NOTSET; - uint8_t heatingtype = EMS_VALUE_UINT_NOTSET; // type of heating: 1 radiator, 2 convectors, 3 floors, 4 room supply - uint8_t circuitcalctemp = EMS_VALUE_UINT_NOTSET; - uint8_t summertemp = EMS_VALUE_UINT_NOTSET; - uint8_t nofrosttemp = EMS_VALUE_UINT_NOTSET; - uint8_t designtemp = EMS_VALUE_UINT_NOTSET; // heatingcurve design temp at MinExtTemp - int8_t offsettemp = EMS_VALUE_INT_NOTSET; // heatingcurve offest temp at roomtemp signed! + int16_t setpoint_roomTemp = EMS_VALUE_SHORT_NOTSET; + int16_t curr_roomTemp = EMS_VALUE_SHORT_NOTSET; + uint8_t mode = EMS_VALUE_UINT_NOTSET; + uint8_t mode_type = EMS_VALUE_UINT_NOTSET; + uint8_t summer_mode = EMS_VALUE_UINT_NOTSET; + uint8_t holiday_mode = EMS_VALUE_UINT_NOTSET; + uint8_t daytemp = EMS_VALUE_UINT_NOTSET; + uint8_t nighttemp = EMS_VALUE_UINT_NOTSET; + uint8_t holidaytemp = EMS_VALUE_UINT_NOTSET; + uint8_t heatingtype = EMS_VALUE_UINT_NOTSET; // type of heating: 1 radiator, 2 convectors, 3 floors, 4 room supply + uint8_t circuitcalctemp = EMS_VALUE_UINT_NOTSET; + uint8_t summertemp = EMS_VALUE_UINT_NOTSET; + uint8_t nofrosttemp = EMS_VALUE_UINT_NOTSET; + uint8_t designtemp = EMS_VALUE_UINT_NOTSET; // heatingcurve design temp at MinExtTemp + int8_t offsettemp = EMS_VALUE_INT_NOTSET; // heatingcurve offest temp at roomtemp signed! uint8_t hc_num() const { diff --git a/src/helpers.h b/src/helpers.h index 980685b18..10275b408 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -29,7 +29,7 @@ class Helpers { static char * hextoa(char * result, const uint8_t value); static std::string data_to_hex(const uint8_t * data, const uint8_t length); - static char * render_value(char * result, const float value, const uint8_t format); // format is precision + static char * render_value(char * result, const float value, const uint8_t format); // format is the precision static char * render_value(char * result, const uint8_t value, const uint8_t format); static char * render_value(char * result, const int8_t value, const uint8_t format); static char * render_value(char * result, const uint16_t value, const uint8_t format); diff --git a/src/roomcontrol.cpp b/src/roomcontrol.cpp index e5af386dc..e1ab12430 100644 --- a/src/roomcontrol.cpp +++ b/src/roomcontrol.cpp @@ -69,9 +69,9 @@ void Roomctrl::check(const uint8_t addr, const uint8_t * data) { if (remotetemp[hc_] == EMS_VALUE_SHORT_NOTSET) { return; } - // reply to writes with write nack byte - if(addr & 0x80) { // it's a write to us - nack_write(); // we don't accept writes. + // reply to writes with write nack byte + if (addr & 0x80) { // it's a write to us + nack_write(); // we don't accept writes. return; } // for now we only reply to version and remote temperature diff --git a/src/roomcontrol.h b/src/roomcontrol.h index 47f66654c..d828ca0b0 100644 --- a/src/roomcontrol.h +++ b/src/roomcontrol.h @@ -37,7 +37,6 @@ class Roomctrl { static void unknown(uint8_t addr, uint8_t dst, uint8_t type, uint8_t offset); static void temperature(uint8_t addr, uint8_t dst); static void nack_write(); - }; } // namespace emsesp diff --git a/src/uart/emsuart_esp32.cpp b/src/uart/emsuart_esp32.cpp index 091363cec..1d27c6155 100644 --- a/src/uart/emsuart_esp32.cpp +++ b/src/uart/emsuart_esp32.cpp @@ -34,10 +34,10 @@ static hw_timer_t * timer = NULL; bool drop_next_rx = true; uint8_t tx_mode_ = 0xFF; //portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED; -uint8_t emsTxBuf[EMS_MAXBUFFERSIZE]; -uint8_t emsTxBufIdx; -uint8_t emsTxBufLen; -uint32_t emsTxWait; +uint8_t emsTxBuf[EMS_MAXBUFFERSIZE]; +uint8_t emsTxBufIdx; +uint8_t emsTxBufLen; +uint32_t emsTxWait; /* * Task to handle the incoming data @@ -90,7 +90,7 @@ void IRAM_ATTR EMSuart::emsuart_tx_timer_intr_handler() { EMS_UART.fifo.rw_byte = emsTxBuf[emsTxBufIdx]; timerAlarmWrite(timer, emsTxWait, false); timerAlarmEnable(timer); - } else if (emsTxBufIdx == emsTxBufLen) { + } else if (emsTxBufIdx == emsTxBufLen) { EMS_UART.conf0.txd_brk = 1; // after send } } @@ -105,9 +105,9 @@ void EMSuart::start(uint8_t tx_mode) { emsTxWait = EMSUART_BIT_TIME * 20; } else if (tx_mode == EMS_TXMODE_HT3) { emsTxWait = EMSUART_BIT_TIME * 17; - } else if(tx_mode > 10 ) { + } else if (tx_mode > 10) { emsTxWait = EMSUART_BIT_TIME * tx_mode; - } else if(tx_mode > 5 ) { + } else if (tx_mode > 5) { emsTxWait = EMSUART_BIT_TIME * tx_mode * 2; } if (tx_mode_ != 0xFF) { // uart already initialized @@ -138,8 +138,8 @@ void EMSuart::start(uint8_t tx_mode) { emsTxBufIdx = 0; emsTxBufLen = 0; - timer = timerBegin(1, 80, true); // timer prescale to 1 µs, countup - timerAttachInterrupt(timer, &emsuart_tx_timer_intr_handler, true); // Timer with edge interrupt + timer = timerBegin(1, 80, true); // timer prescale to 1 µs, countup + timerAttachInterrupt(timer, &emsuart_tx_timer_intr_handler, true); // Timer with edge interrupt } /* @@ -147,7 +147,7 @@ void EMSuart::start(uint8_t tx_mode) { */ void EMSuart::stop() { EMS_UART.int_ena.val = 0; // disable all intr. - // timerAlarmDisable(timer); + // timerAlarmDisable(timer); }; /* @@ -159,8 +159,8 @@ void EMSuart::restart() { drop_next_rx = true; // and drop first frame } EMS_UART.int_ena.brk_det = 1; // activate only break - emsTxBufIdx = 0; - emsTxBufLen = 0; + emsTxBufIdx = 0; + emsTxBufLen = 0; }; /* @@ -172,8 +172,8 @@ void EMSuart::send_poll(uint8_t data) { EMS_UART.conf0.txd_brk = 1; // after send } else { EMS_UART.fifo.rw_byte = data; - emsTxBufIdx = 0; - emsTxBufLen = 1; + emsTxBufIdx = 0; + emsTxBufLen = 1; timerAlarmWrite(timer, emsTxWait, false); timerAlarmEnable(timer); } @@ -198,8 +198,8 @@ uint16_t EMSuart::transmit(uint8_t * buf, uint8_t len) { emsTxBuf[i] = buf[i]; } EMS_UART.fifo.rw_byte = buf[0]; - emsTxBufIdx = 0; - emsTxBufLen = len; + emsTxBufIdx = 0; + emsTxBufLen = len; timerAlarmWrite(timer, emsTxWait, false); timerAlarmEnable(timer); } diff --git a/src/uart/emsuart_esp32.h b/src/uart/emsuart_esp32.h index 4e958469e..8ea98b2aa 100644 --- a/src/uart/emsuart_esp32.h +++ b/src/uart/emsuart_esp32.h @@ -65,17 +65,16 @@ class EMSuart { EMSuart() = default; ~EMSuart() = default; - static void start(uint8_t tx_mode); - static void send_poll(uint8_t data); - static void stop(); - static void restart(); - static uint16_t transmit(uint8_t * buf, uint8_t len); + static void start(uint8_t tx_mode); + static void send_poll(uint8_t data); + static void stop(); + static void restart(); + static uint16_t transmit(uint8_t * buf, uint8_t len); private: static void emsuart_recvTask(void * para); static void IRAM_ATTR emsuart_rx_intr_handler(void * para); static void IRAM_ATTR emsuart_tx_timer_intr_handler(); - }; } // namespace emsesp diff --git a/src/uart/emsuart_esp8266.cpp b/src/uart/emsuart_esp8266.cpp index d77a77de3..597adf9fb 100644 --- a/src/uart/emsuart_esp8266.cpp +++ b/src/uart/emsuart_esp8266.cpp @@ -31,7 +31,6 @@ os_event_t recvTaskQueue[EMSUART_recvTaskQueueLen]; // our Rx queue EMSuart::EMSRxBuf_t * pEMSRxBuf; EMSuart::EMSRxBuf_t * paEMSRxBuf[EMS_MAXBUFFERS]; uint8_t emsRxBufIdx = 0; -uint8_t phantomBreak = 0; uint8_t tx_mode_ = 0xFF; bool drop_next_rx = true; uint32_t emsRxTime; @@ -40,7 +39,6 @@ uint8_t emsTxBufIdx; uint8_t emsTxBufLen; uint32_t emsTxWait; - // // Main interrupt handler // Important: must not use ICACHE_FLASH_ATTR @@ -84,11 +82,6 @@ void ICACHE_FLASH_ATTR EMSuart::emsuart_recvTask(os_event_t * events) { uint8_t length = pCurrent->length; // number of bytes including the BRK at the end pCurrent->length = 0; - if (phantomBreak) { - phantomBreak = 0; - length--; // remove phantom break from Rx buffer - } - // it's a poll or status code, single byte and ok to send on, then quit if (length == 2) { EMSESP::incoming_telegram((uint8_t *)pCurrent->buffer, 1); @@ -124,12 +117,12 @@ void ICACHE_RAM_ATTR EMSuart::emsuart_tx_timer_intr_handler() { } else if (emsTxBufIdx == emsTxBufLen) { USC0(EMSUART_UART) |= (1 << UCBRK); // set if (tx_mode_ > 5 || tx_mode_ < 11) { - timer1_write(5 * EMSUART_TX_BIT_TIME * 11); + timer1_write(5 * EMSUART_TX_BIT_TIME * 11); USIE(EMSUART_UART) &= ~(1 << UIBD); // disable break interrupt } } else if (USC0(EMSUART_UART) & (1 << UCBRK)) { USC0(EMSUART_UART) &= ~(1 << UCBRK); // clear - USIE(EMSUART_UART) |= (1 << UIBD); // enable break interrupt + USIE(EMSUART_UART) |= (1 << UIBD); // enable break interrupt } } /* @@ -137,9 +130,9 @@ void ICACHE_RAM_ATTR EMSuart::emsuart_tx_timer_intr_handler() { */ void ICACHE_FLASH_ATTR EMSuart::start(uint8_t tx_mode) { if (tx_mode > 10) { - emsTxWait = 5 * EMSUART_TX_BIT_TIME * tx_mode; // bittimes for tx_mode + emsTxWait = 5 * EMSUART_TX_BIT_TIME * tx_mode; // bittimes for tx_mode } else if (tx_mode > 5) { - emsTxWait = 10 * EMSUART_TX_BIT_TIME * tx_mode; // bittimes for tx_mode + emsTxWait = 10 * EMSUART_TX_BIT_TIME * tx_mode; // bittimes for tx_mode } if (tx_mode == 5) { USC0(EMSUART_UART) = 0x2C; // 8N1,5 @@ -171,7 +164,7 @@ void ICACHE_FLASH_ATTR EMSuart::start(uint8_t tx_mode) { PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD); // set 9600, 8 bits, no parity check, 1 stop bit - USD(EMSUART_UART) = (UART_CLK_FREQ / EMSUART_BAUD); + USD(EMSUART_UART) = (UART_CLK_FREQ / EMSUART_BAUD); emsuart_flush_fifos(); @@ -209,7 +202,6 @@ void ICACHE_FLASH_ATTR EMSuart::start(uint8_t tx_mode) { ETS_UART_INTR_ATTACH(emsuart_rx_intr_handler, nullptr); ETS_UART_INTR_ENABLE(); drop_next_rx = true; - // LOG_INFO(F("UART service for Rx/Tx started")); // for sending with large delay in EMS+ mode we use a timer interrupt timer1_attachInterrupt(emsuart_tx_timer_intr_handler); // Add ISR Function @@ -244,7 +236,6 @@ void ICACHE_FLASH_ATTR EMSuart::restart() { * Which is a 11-bit set of zero's (11 cycles) */ void ICACHE_FLASH_ATTR EMSuart::tx_brk() { - // must make sure Tx FIFO is empty while (((USS(EMSUART_UART) >> USTXC) & 0xFF)) ; @@ -277,13 +268,13 @@ void EMSuart::send_poll(uint8_t data) { USC0(EMSUART_UART) &= ~(1 << UCBRK); if (tx_mode_ > 5) { // timer controlled modes USF(EMSUART_UART) = data; - emsTxBufIdx = 0; - emsTxBufLen = 1; + emsTxBufIdx = 0; + emsTxBufLen = 1; timer1_write(emsTxWait); } else if (tx_mode_ >= EMS_TXMODE_NEW) { // hardware controlled modes USF(EMSUART_UART) = data; USC0(EMSUART_UART) |= (1 << UCBRK); // send at the end - } else { // software controlled modes + } else { // software controlled modes // EMS1.0, EMS+ and HT3 USF(EMSUART_UART) = data; delayMicroseconds(EMSUART_TX_BRK_WAIT); @@ -306,7 +297,7 @@ uint16_t ICACHE_FLASH_ATTR EMSuart::transmit(uint8_t * buf, uint8_t len) { // if ((uuid::get_uptime() - emsRxTime) > EMS_RX_TO_TX_TIMEOUT)) { // send allowed within 20 ms // return EMS_TX_STATUS_ERR; // } - // reset tx-brk, just in case it is accidently set + // reset tx-brk, just in case it is accidentally set USC0(EMSUART_UART) &= ~(1 << UCBRK); // timer controlled modes with extra delay @@ -314,15 +305,15 @@ uint16_t ICACHE_FLASH_ATTR EMSuart::transmit(uint8_t * buf, uint8_t len) { for (uint8_t i = 0; i < len; i++) { emsTxBuf[i] = buf[i]; } - emsTxBufIdx = 0; - emsTxBufLen = len; + emsTxBufIdx = 0; + emsTxBufLen = len; USF(EMSUART_UART) = buf[0]; timer1_write(emsTxWait); return EMS_TX_STATUS_OK; } // new code from Michael. See https://github.com/proddy/EMS-ESP/issues/380 - if (tx_mode_ >= EMS_TXMODE_NEW) { + if (tx_mode_ >= EMS_TXMODE_NEW) { // tx_mode 4 for (uint8_t i = 0; i < len; i++) { USF(EMSUART_UART) = buf[i]; } @@ -331,7 +322,7 @@ uint16_t ICACHE_FLASH_ATTR EMSuart::transmit(uint8_t * buf, uint8_t len) { } // EMS+ https://github.com/proddy/EMS-ESP/issues/23# - if (tx_mode_ == EMS_TXMODE_EMSPLUS) { // With extra tx delay for EMS+ + if (tx_mode_ == EMS_TXMODE_EMSPLUS) { // tx_mode 2, with extra tx delay for EMS+ for (uint8_t i = 0; i < len; i++) { USF(EMSUART_UART) = buf[i]; delayMicroseconds(EMSUART_TX_BRK_WAIT); // 2070 @@ -341,7 +332,7 @@ uint16_t ICACHE_FLASH_ATTR EMSuart::transmit(uint8_t * buf, uint8_t len) { } // Junkers logic by @philrich - if (tx_mode_ == EMS_TXMODE_HT3) { + if (tx_mode_ == EMS_TXMODE_HT3) { // tx_mode 3 for (uint8_t i = 0; i < len; i++) { USF(EMSUART_UART) = buf[i]; @@ -383,7 +374,6 @@ uint16_t ICACHE_FLASH_ATTR EMSuart::transmit(uint8_t * buf, uint8_t len) { // disable rx interrupt // clear Rx status register, resetting the Rx FIFO and flush it - // noInterrupts(); ETS_UART_INTR_DISABLE(); // USC0(EMSUART_UART) |= (1 << UCRXRST); // reset uart rx fifo emsuart_flush_fifos(); @@ -414,12 +404,8 @@ uint16_t ICACHE_FLASH_ATTR EMSuart::transmit(uint8_t * buf, uint8_t len) { } USC0(EMSUART_UART) &= ~(1 << UCBRK); // clear - // USC0(EMSUART_UART) &= ~((1 << UCBRK) | (1 << UCLBE)); // disable loopback & clear - // USIC(EMSUART_UART) = (1 << UIBD); // clear BRK detect IRQ - // phantomBreak = 1; } - // interrupts(); ETS_UART_INTR_ENABLE(); // open up the FIFO again to start receiving return EMS_TX_STATUS_OK; // send the Tx ok status back From a710080c465f22cb03515c5b3d3a723ff4fbd529 Mon Sep 17 00:00:00 2001 From: proddy Date: Sun, 14 Jun 2020 14:43:23 +0200 Subject: [PATCH 4/7] use log NOTICE for printing output from 'watch' command --- src/emsesp.cpp | 65 +++++++++++++++++++++++++++--------------------- src/system.cpp | 58 ++++++++++++++++++++++++++++-------------- src/telegram.cpp | 6 ++--- 3 files changed, 78 insertions(+), 51 deletions(-) diff --git a/src/emsesp.cpp b/src/emsesp.cpp index 4152eaf71..dee34067a 100644 --- a/src/emsesp.cpp +++ b/src/emsesp.cpp @@ -120,19 +120,20 @@ void EMSESP::show_emsbus(uuid::console::Shell & shell) { } shell.printfln(F("EMS Bus info:")); + shell.printfln(F(" Tx mode: %d"), Settings().ems_tx_mode()); shell.printfln(F(" Bus protocol: %s"), EMSbus::is_ht3() ? F("HT3") : F("Buderus")); shell.printfln(F(" #telegrams received: %d"), rxservice_.telegram_count()); shell.printfln(F(" #read requests sent: %d"), txservice_.telegram_read_count()); shell.printfln(F(" #write requests sent: %d"), txservice_.telegram_write_count()); - shell.printfln(F(" #incomplete telegrams: %d (%d%%)"), rxservice_.telegram_error_count(), success_rate); - shell.printfln(F(" #tx fails (after 3 retries): %d"), txservice_.telegram_fail_count()); + shell.printfln(F(" #corrupted telegrams: %d (%d%%)"), rxservice_.telegram_error_count(), success_rate); + shell.printfln(F(" #tx fails (after %d retries): %d"), TxService::MAXIMUM_TX_RETRIES, txservice_.telegram_fail_count()); } else { - shell.printfln(F("EMS Bus is disconnected")); + shell.printfln(F("EMS Bus is disconnected.")); } shell.println(); - // Rx + // Rx queue auto rx_telegrams = rxservice_.queue(); if (rx_telegrams.empty()) { shell.printfln(F("Rx Queue is empty")); @@ -145,7 +146,7 @@ void EMSESP::show_emsbus(uuid::console::Shell & shell) { shell.println(); - // Tx + // Tx queue auto tx_telegrams = txservice_.queue(); if (tx_telegrams.empty()) { shell.printfln(F("Tx Queue is empty")); @@ -393,9 +394,11 @@ void EMSESP::process_version(std::shared_ptr telegram) { // We also check for common telgram types, like the Version(0x02) // returns false if there are none found bool EMSESP::process_telegram(std::shared_ptr telegram) { + + // if watching... if (watch() == 1) { if ((watch_id_ == WATCH_NONE) || (telegram->src == watch_id_) || (telegram->dest == watch_id_) || (telegram->type_id == watch_id_)) { - LOG_INFO(pretty_telegram(telegram).c_str()); + LOG_NOTICE(pretty_telegram(telegram).c_str()); } } @@ -722,28 +725,24 @@ void EMSESP::console_commands(Shell & shell, unsigned int context) { }; }); - EMSESPShell::commands->add_command( - ShellContext::EMS, - CommandFlags::ADMIN, - flash_string_vector{F_(set), F_(tx_mode)}, - flash_string_vector{F_(n_mandatory)}, - [](Shell & shell, const std::vector & arguments) { - uint8_t tx_mode = std::strtol(arguments[0].c_str(), nullptr, 10); - if ((tx_mode > 0) && (tx_mode <= 30)) { - Settings settings; - settings.ems_tx_mode(tx_mode); - settings.commit(); - shell.printfln(F_(tx_mode_fmt), settings.ems_tx_mode()); - // reset the UART - EMSuart::stop(); - EMSuart::start(tx_mode); - } else { - shell.println(F("Must be 1 for EMS generic, 2 for EMS+, 3 for HT3, 4 for experimental")); - } - }, - [](Shell & shell __attribute__((unused)), const std::vector & arguments __attribute__((unused))) -> const std::vector { - return std::vector{read_flash_string(F("1")), read_flash_string(F("2")), read_flash_string(F("3")), read_flash_string(F("4"))}; - }); + EMSESPShell::commands->add_command(ShellContext::EMS, + CommandFlags::ADMIN, + flash_string_vector{F_(set), F_(tx_mode)}, + flash_string_vector{F_(n_mandatory)}, + [](Shell & shell, const std::vector & arguments) { + uint8_t tx_mode = std::strtol(arguments[0].c_str(), nullptr, 10); + if ((tx_mode > 0) && (tx_mode <= 30)) { + Settings settings; + settings.ems_tx_mode(tx_mode); + settings.commit(); + shell.printfln(F_(tx_mode_fmt), settings.ems_tx_mode()); + // reset the UART + EMSuart::stop(); + EMSuart::start(tx_mode); + } else { + shell.println(F("Must be 1 for EMS generic, 2 for EMS+, 3 for HT3, 4 for experimental")); + } + }); EMSESPShell::commands->add_command( ShellContext::EMS, @@ -858,7 +857,15 @@ void EMSESP::console_commands(Shell & shell, unsigned int context) { uint8_t watch = emsesp::EMSESP::watch(); if (watch == 0) { shell.printfln(F("Watch is off")); - } else if (watch == 1) { + return; + } + + // if logging is off, the watch won't show anything, show force it back to INFO + if (!logger_.enabled(Level::NOTICE)) { + shell.log_level(Level::NOTICE); + } + + if (watch == 1) { shell.printfln(F("Watching incoming telegrams, displayed in decoded format")); } else { shell.printfln(F("Watching incoming telegrams, displayed as raw bytes")); diff --git a/src/system.cpp b/src/system.cpp index f21d7a668..27d0bc1f3 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -72,8 +72,7 @@ void System::mqtt_commands(const char * message) { if (doc["send"] != nullptr) { const char * data = doc["send"]; EMSESP::send_raw_telegram(data); - LOG_INFO(F("Sending raw: %s"),data); - + LOG_INFO(F("Sending raw: %s"), data); } #if defined(ESP8266) @@ -87,39 +86,60 @@ void System::mqtt_commands(const char * message) { const uint8_t d2_ = 21; const uint8_t d3_ = 17; #endif - if(doc["D0"] != nullptr) { + +#ifndef EMSESP_STANDALONE + + if (doc["D0"] != nullptr) { const int8_t set = doc["D0"]; pinMode(d0_, OUTPUT); - if (set == 1) digitalWrite(d0_, HIGH); - else if (set == 0) digitalWrite(d0_, LOW); - LOG_INFO(F("Port D0 set to %d"),set); + if (set == 1) { + digitalWrite(d0_, HIGH); + } else if (set == 0) { + digitalWrite(d0_, LOW); + } + LOG_INFO(F("Port D0 set to %d"), set); } - if(doc["D1"] != nullptr) { + + if (doc["D1"] != nullptr) { const int8_t set = doc["D1"]; pinMode(d1_, OUTPUT); - if (set == 1) digitalWrite(d1_, HIGH); - else if (set == 0) digitalWrite(d1_, LOW); - LOG_INFO(F("Port D1 set to %d"),set); + if (set == 1) { + digitalWrite(d1_, HIGH); + } else if (set == 0) { + digitalWrite(d1_, LOW); + } + LOG_INFO(F("Port D1 set to %d"), set); } - if(doc["D2"] != nullptr) { + + if (doc["D2"] != nullptr) { const int8_t set = doc["D2"]; pinMode(d2_, OUTPUT); - if (set == 1) digitalWrite(d2_, HIGH); - else if (set == 0) digitalWrite(d2_, LOW); - LOG_INFO(F("Port D2 set to %d"),set); + if (set == 1) { + digitalWrite(d2_, HIGH); + } else if (set == 0) { + digitalWrite(d2_, LOW); + } + LOG_INFO(F("Port D2 set to %d"), set); } - if(doc["D3"] != nullptr) { + + if (doc["D3"] != nullptr) { const int8_t set = doc["D3"]; pinMode(d3_, OUTPUT); - if (set == 1) digitalWrite(d3_, HIGH); - else if (set == 0) digitalWrite(d3_, LOW); - LOG_INFO(F("Port D3 set to %d"),set); + if (set == 1) { + digitalWrite(d3_, HIGH); + } else if (set == 0) { + digitalWrite(d3_, LOW); + } + LOG_INFO(F("Port D3 set to %d"), set); } +#endif + const char * command = doc["cmd"]; if (command == nullptr) { return; } + // send raw command if (strcmp(command, "send") == 0) { const char * data = doc["data"]; @@ -127,7 +147,7 @@ void System::mqtt_commands(const char * message) { return; } EMSESP::send_raw_telegram(data); - LOG_INFO(F("Sending raw: %s"),data); + LOG_INFO(F("Sending raw: %s"), data); return; } } diff --git a/src/telegram.cpp b/src/telegram.cpp index a393093bc..16e40b98d 100644 --- a/src/telegram.cpp +++ b/src/telegram.cpp @@ -304,11 +304,11 @@ void RxService::add(uint8_t * data, uint8_t length) { return; } - // if we're in "trace" and "raw" print out actual telegram as bytes to the console + // if we're watching and "raw" print out actual telegram as bytes to the console if (EMSESP::watch() == 2) { uint16_t trace_watch_id = EMSESP::watch_id(); if ((trace_watch_id == WATCH_NONE) || (src == trace_watch_id) || (dest == trace_watch_id) || (type_id == trace_watch_id)) { - LOG_INFO(F("Rx: %s"), Helpers::data_to_hex(data, length).c_str()); + LOG_NOTICE(F("Rx: %s"), Helpers::data_to_hex(data, length).c_str()); } } @@ -471,7 +471,7 @@ void TxService::send_telegram(const QueuedTxTelegram & tx_telegram) { #ifdef EMSESP_DEBUG // if watching in 'raw' mode if (EMSESP::watch() == 2) { - LOG_INFO(F("[DEBUG] Tx: %s"), Helpers::data_to_hex(telegram_raw, length).c_str()); + LOG_NOTICE(F("[DEBUG] Tx: %s"), Helpers::data_to_hex(telegram_raw, length).c_str()); } #endif From d08cd09f4e6abd9ff015ce176498c943a3f0c9db Mon Sep 17 00:00:00 2001 From: proddy Date: Sun, 14 Jun 2020 14:43:43 +0200 Subject: [PATCH 5/7] fixed mock uart driver for standalone unit tests --- lib_standalone/emsuart_standalone.cpp | 2 +- lib_standalone/emsuart_standalone.h | 19 ++++++++----------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/lib_standalone/emsuart_standalone.cpp b/lib_standalone/emsuart_standalone.cpp index 4e1b24af1..4e506d835 100644 --- a/lib_standalone/emsuart_standalone.cpp +++ b/lib_standalone/emsuart_standalone.cpp @@ -56,7 +56,7 @@ void EMSuart::send_poll(uint8_t data) { * buf contains the CRC and len is #bytes including the CRC * returns code, 0=success, 1=brk error, 2=watchdog timeout */ -EMSUART_STATUS EMSuart::transmit(uint8_t * buf, uint8_t len) { +uint16_t EMSuart::transmit(uint8_t * buf, uint8_t len) { if (len == 0) { return EMS_TX_STATUS_OK; // nothing to send } diff --git a/lib_standalone/emsuart_standalone.h b/lib_standalone/emsuart_standalone.h index 7d6a8fe04..8fa13e8f2 100644 --- a/lib_standalone/emsuart_standalone.h +++ b/lib_standalone/emsuart_standalone.h @@ -23,25 +23,22 @@ namespace emsesp { -typedef enum { - EMS_TX_STATUS_OK = 1, - EMS_TX_WTD_TIMEOUT, // watchdog timeout during send - EMS_TX_BRK_DETECT, // incoming BRK during Tx -} EMSUART_STATUS; +#define EMS_TX_STATUS_ERR 0 +#define EMS_TX_STATUS_OK 1 class EMSuart { public: EMSuart() = default; ~EMSuart() = default; - static void start(uint8_t tx_mode); - static void stop(); - static void restart(); - static void send_poll(uint8_t data); - static EMSUART_STATUS transmit(uint8_t * buf, uint8_t len); + static void start(uint8_t tx_mode); + static void stop(); + static void restart(); + static void send_poll(uint8_t data); + static uint16_t transmit(uint8_t * buf, uint8_t len); private: - static char * hextoa(char * result, const uint8_t value); + static char * hextoa(char * result, const uint8_t value); }; } // namespace emsesp From 598c61e57b0ad9fdea5f087d627bcf34a38eb42c Mon Sep 17 00:00:00 2001 From: proddy Date: Mon, 15 Jun 2020 14:59:59 +0200 Subject: [PATCH 6/7] minor change. removed logger. no changes to logic. --- src/uart/emsuart_esp8266.cpp | 37 +++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/src/uart/emsuart_esp8266.cpp b/src/uart/emsuart_esp8266.cpp index 597adf9fb..f9eefa728 100644 --- a/src/uart/emsuart_esp8266.cpp +++ b/src/uart/emsuart_esp8266.cpp @@ -24,20 +24,17 @@ namespace emsesp { -MAKE_PSTR(logger_name, "emsuart") - -uuid::log::Logger EMSuart::logger_{F_(logger_name), uuid::log::Facility::CONSOLE}; os_event_t recvTaskQueue[EMSUART_recvTaskQueueLen]; // our Rx queue EMSuart::EMSRxBuf_t * pEMSRxBuf; EMSuart::EMSRxBuf_t * paEMSRxBuf[EMS_MAXBUFFERS]; uint8_t emsRxBufIdx = 0; uint8_t tx_mode_ = 0xFF; bool drop_next_rx = true; -uint32_t emsRxTime; -uint8_t emsTxBuf[EMS_MAXBUFFERSIZE]; -uint8_t emsTxBufIdx; -uint8_t emsTxBufLen; -uint32_t emsTxWait; +// uint32_t emsRxTime; +uint8_t emsTxBuf[EMS_MAXBUFFERSIZE]; +uint8_t emsTxBufIdx; +uint8_t emsTxBufLen; +uint32_t emsTxWait; // // Main interrupt handler @@ -64,7 +61,7 @@ void ICACHE_RAM_ATTR EMSuart::emsuart_rx_intr_handler(void * para) { if (!drop_next_rx) { pEMSRxBuf->length = length; os_memcpy((void *)pEMSRxBuf->buffer, (void *)&uart_buffer, pEMSRxBuf->length); // copy data into transfer buffer, including the BRK 0x00 at the end - emsRxTime = uuid::get_uptime(); + // emsRxTime = uuid::get_uptime(); } drop_next_rx = false; system_os_post(EMSUART_recvTaskPrio, 0, 0); // call emsuart_recvTask() at next opportunity @@ -107,9 +104,10 @@ void ICACHE_FLASH_ATTR EMSuart::emsuart_flush_fifos() { // ISR to Fire when Timer is triggered void ICACHE_RAM_ATTR EMSuart::emsuart_tx_timer_intr_handler() { - if (emsTxBufIdx > 32) { + if (emsTxBufIdx > EMS_MAXBUFFERSIZE) { return; } + emsTxBufIdx++; if (emsTxBufIdx < emsTxBufLen) { USF(EMSUART_UART) = emsTxBuf[emsTxBufIdx]; @@ -125,6 +123,7 @@ void ICACHE_RAM_ATTR EMSuart::emsuart_tx_timer_intr_handler() { USIE(EMSUART_UART) |= (1 << UIBD); // enable break interrupt } } + /* * init UART0 driver */ @@ -134,6 +133,7 @@ void ICACHE_FLASH_ATTR EMSuart::start(uint8_t tx_mode) { } else if (tx_mode > 5) { emsTxWait = 10 * EMSUART_TX_BIT_TIME * tx_mode; // bittimes for tx_mode } + if (tx_mode == 5) { USC0(EMSUART_UART) = 0x2C; // 8N1,5 } else { @@ -145,13 +145,16 @@ void ICACHE_FLASH_ATTR EMSuart::start(uint8_t tx_mode) { restart(); return; } + tx_mode_ = tx_mode; + // allocate and preset EMS Receive buffers for (int i = 0; i < EMS_MAXBUFFERS; i++) { EMSRxBuf_t * p = (EMSRxBuf_t *)malloc(sizeof(EMSRxBuf_t)); p->length = 0; paEMSRxBuf[i] = p; } + pEMSRxBuf = paEMSRxBuf[0]; // reset EMS Rx Buffer ETS_UART_INTR_DISABLE(); @@ -163,7 +166,6 @@ void ICACHE_FLASH_ATTR EMSuart::start(uint8_t tx_mode) { PIN_PULLUP_DIS(PERIPHS_IO_MUX_U0RXD_U); PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD); - // set 9600, 8 bits, no parity check, 1 stop bit USD(EMSUART_UART) = (UART_CLK_FREQ / EMSUART_BAUD); emsuart_flush_fifos(); @@ -239,6 +241,7 @@ void ICACHE_FLASH_ATTR EMSuart::tx_brk() { // must make sure Tx FIFO is empty while (((USS(EMSUART_UART) >> USTXC) & 0xFF)) ; + // do not clear buffers to get a echo back // tmp = ((1 << UCRXRST) | (1 << UCTXRST)); // bit mask // USC0(EMSUART_UART) |= (tmp); // set bits @@ -264,14 +267,14 @@ void ICACHE_FLASH_ATTR EMSuart::tx_brk() { * It's a bit dirty. there is no special wait logic per tx_mode type, fifo flushes or error checking */ void EMSuart::send_poll(uint8_t data) { - // reset tx-brk, just in case it is accidently set + // reset tx-brk, just in case it is accidentally set USC0(EMSUART_UART) &= ~(1 << UCBRK); if (tx_mode_ > 5) { // timer controlled modes USF(EMSUART_UART) = data; emsTxBufIdx = 0; emsTxBufLen = 1; timer1_write(emsTxWait); - } else if (tx_mode_ >= EMS_TXMODE_NEW) { // hardware controlled modes + } else if ((tx_mode_ == EMS_TXMODE_NEW) || (tx_mode_ == 5)) { // hardware controlled modes USF(EMSUART_UART) = data; USC0(EMSUART_UART) |= (1 << UCBRK); // send at the end } else { // software controlled modes @@ -288,15 +291,19 @@ void EMSuart::send_poll(uint8_t data) { * returns code, 0=success, 1=brk error, 2=watchdog timeout */ uint16_t ICACHE_FLASH_ATTR EMSuart::transmit(uint8_t * buf, uint8_t len) { - if (len == 0 || len > 32) { + if (len == 0 || len > EMS_MAXBUFFERSIZE) { return EMS_TX_STATUS_ERR; // nothing or to much to send } + + /* #ifdef EMSESP_DEBUG // LOG_INFO(F("[DEBUG] UART Response time: %d ms"), uuid::get_uptime() - emsRxTime); #endif // if ((uuid::get_uptime() - emsRxTime) > EMS_RX_TO_TX_TIMEOUT)) { // send allowed within 20 ms // return EMS_TX_STATUS_ERR; // } + */ + // reset tx-brk, just in case it is accidentally set USC0(EMSUART_UART) &= ~(1 << UCBRK); @@ -313,7 +320,7 @@ uint16_t ICACHE_FLASH_ATTR EMSuart::transmit(uint8_t * buf, uint8_t len) { } // new code from Michael. See https://github.com/proddy/EMS-ESP/issues/380 - if (tx_mode_ >= EMS_TXMODE_NEW) { // tx_mode 4 + if (tx_mode_ == EMS_TXMODE_NEW) { // tx_mode 4 for (uint8_t i = 0; i < len; i++) { USF(EMSUART_UART) = buf[i]; } From 9e84fefed6d429d64312d57308d0503f5f75f11c Mon Sep 17 00:00:00 2001 From: proddy Date: Mon, 15 Jun 2020 15:00:06 +0200 Subject: [PATCH 7/7] code cleanup --- src/console.h | 3 +-- src/telegram.cpp | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/console.h b/src/console.h index 69ad657f4..02d57938e 100644 --- a/src/console.h +++ b/src/console.h @@ -41,7 +41,7 @@ using uuid::log::Level; // clang-format off -#define LOG_DEBUG(...) if (logger_.enabled(Level::DEBUG)) {logger_.debug(__VA_ARGS__);} +#define LOG_DEBUG(...) logger_.debug(__VA_ARGS__) #define LOG_INFO(...) logger_.info(__VA_ARGS__) #define LOG_TRACE(...) logger_.trace(__VA_ARGS__) #define LOG_NOTICE(...) logger_.notice(__VA_ARGS__) @@ -51,7 +51,6 @@ using uuid::log::Level; #define MAKE_PSTR(string_name, string_literal) static const char __pstr__##string_name[] __attribute__((__aligned__(sizeof(int)))) PROGMEM = string_literal; #define MAKE_PSTR_WORD(string_name) MAKE_PSTR(string_name, #string_name) #define F_(string_name) FPSTR(__pstr__##string_name) -#define NO_ARGUMENTS std::vector{} // clang-format on diff --git a/src/telegram.cpp b/src/telegram.cpp index 16e40b98d..7ee6bd0b1 100644 --- a/src/telegram.cpp +++ b/src/telegram.cpp @@ -466,8 +466,6 @@ void TxService::send_telegram(const QueuedTxTelegram & tx_telegram) { tx_telegram.id_, telegram->to_string(telegram_raw, length).c_str()); - // send the telegram to the UART Tx - uint16_t status = EMSuart::transmit(telegram_raw, length); #ifdef EMSESP_DEBUG // if watching in 'raw' mode if (EMSESP::watch() == 2) { @@ -475,6 +473,9 @@ void TxService::send_telegram(const QueuedTxTelegram & tx_telegram) { } #endif + // send the telegram to the UART Tx + uint16_t status = EMSuart::transmit(telegram_raw, length); + if (status == EMS_TX_STATUS_ERR) { LOG_ERROR(F("Failed to transmit Tx via UART.")); increment_telegram_fail_count(); // another Tx fail @@ -616,7 +617,6 @@ void TxService::remember_tx(const uint8_t * data, const uint8_t length) { if (ems_mask() != EMS_MASK_UNSET) { telegram_last_[0] ^= ems_mask(); } - } // add last Tx to tx queue and increment count