diff --git a/src/console.cpp b/src/console.cpp index 419eb8082..fa3992ae6 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -65,7 +65,11 @@ void EMSESPShell::display_banner() { println(); printfln(F("┌──────────────────────────────────────────┐")); +#if defined(ESP32) + printfln(F("│ %sEMS-ESP version %-10s ESP32%s │"), COLOR_BOLD_ON, settings.app_version().c_str(), COLOR_BOLD_OFF); +#else printfln(F("│ %sEMS-ESP version %-10s%s │"), COLOR_BOLD_ON, settings.app_version().c_str(), COLOR_BOLD_OFF); +#endif printfln(F("│ %s%shttps://github.com/proddy/EMS-ESP%s │"), COLOR_BRIGHT_GREEN, COLOR_UNDERLINE, COLOR_RESET); printfln(F("│ │")); diff --git a/src/devices/boiler.cpp b/src/devices/boiler.cpp index 0e6309ca7..26f804142 100644 --- a/src/devices/boiler.cpp +++ b/src/devices/boiler.cpp @@ -51,7 +51,6 @@ Boiler::Boiler(uint8_t device_type, int8_t device_id, uint8_t product_id, const // the telegram handlers... register_telegram_type(0x10, F("UBAErrorMessage1"), false, std::bind(&Boiler::process_UBAErrorMessage, this, _1)); register_telegram_type(0x11, F("UBAErrorMessage2"), false, std::bind(&Boiler::process_UBAErrorMessage, this, _1)); - register_telegram_type(0x12, F("UBAErrorMessage3"), false, std::bind(&Boiler::process_UBAErrorMessage, this, _1)); register_telegram_type(0x18, F("UBAMonitorFast"), false, std::bind(&Boiler::process_UBAMonitorFast, this, _1)); register_telegram_type(0x19, F("UBAMonitorSlow"), true, std::bind(&Boiler::process_UBAMonitorSlow, this, _1)); register_telegram_type(0x34, F("UBAMonitorWW"), false, std::bind(&Boiler::process_UBAMonitorWW, this, _1)); diff --git a/src/devices/solar.cpp b/src/devices/solar.cpp index 61ed8e5eb..68cd990e0 100644 --- a/src/devices/solar.cpp +++ b/src/devices/solar.cpp @@ -36,8 +36,7 @@ Solar::Solar(uint8_t device_type, uint8_t device_id, uint8_t product_id, const s register_telegram_type(0x0097, F("SM10Monitor"), true, std::bind(&Solar::process_SM10Monitor, this, _1)); register_telegram_type(0x0362, F("SM100Monitor"), true, std::bind(&Solar::process_SM100Monitor, this, _1)); register_telegram_type(0x0363, F("SM100Monitor2"), true, std::bind(&Solar::process_SM100Monitor2, this, _1)); - - register_telegram_type(0x0363, F("SM100Config"), true, std::bind(&Solar::process_SM100Config, this, _1)); + register_telegram_type(0x0366, F("SM100Config"), true, std::bind(&Solar::process_SM100Config, this, _1)); register_telegram_type(0x0364, F("SM100Status"), false, std::bind(&Solar::process_SM100Status, this, _1)); register_telegram_type(0x036A, F("SM100Status2"), false, std::bind(&Solar::process_SM100Status2, this, _1)); diff --git a/src/emsesp.cpp b/src/emsesp.cpp index dee34067a..30a67f0a0 100644 --- a/src/emsesp.cpp +++ b/src/emsesp.cpp @@ -568,14 +568,15 @@ void EMSESP::send_write_request(const uint16_t type_id, // this is main entry point when data is received on the Rx line, via emsuart library // we check if its a complete telegram or just a single byte (which could be a poll or a return status) void EMSESP::incoming_telegram(uint8_t * data, const uint8_t length) { - // LOG_DEBUG(F("Rx: %s"), Helpers::data_to_hex(data, length).c_str()); + static uint32_t tx_time_ = 0; // check first for echo uint8_t first_value = data[0]; if (((first_value & 0x7F) == txservice_.ems_bus_id()) && (length > 1)) { // if we ask ourself at roomcontrol for version e.g. 0B 98 02 00 20 Roomctrl::check((data[1] ^ 0x80 ^ rxservice_.ems_mask()), data); #ifdef EMSESP_DEBUG - LOG_DEBUG(F("[DEBUG] Echo: %s"), Helpers::data_to_hex(data, length).c_str()); + // get_uptime is only updated once per loop, does not give the right time + LOG_DEBUG(F("[DEBUG] Echo after %d ms: %s"), ::millis() - tx_time_, Helpers::data_to_hex(data, length).c_str()); #endif return; // it's an echo } @@ -638,6 +639,7 @@ void EMSESP::incoming_telegram(uint8_t * data, const uint8_t length) { // if ht3 poll must be ems_bus_id else if Buderus poll must be (ems_bus_id | 0x80) if ((first_value ^ 0x80 ^ rxservice_.ems_mask()) == txservice_.ems_bus_id()) { EMSbus::last_bus_activity(uuid::get_uptime()); // set the flag indication the EMS bus is active + tx_time_ = ::millis(); // get_uptime is only updated once per loop, does not give the right time txservice_.send(); } // send remote room temperature if active @@ -731,7 +733,7 @@ void EMSESP::console_commands(Shell & shell, unsigned int context) { 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)) { + if ((tx_mode > 0) && (tx_mode <= 50)) { Settings settings; settings.ems_tx_mode(tx_mode); settings.commit(); diff --git a/src/helpers.cpp b/src/helpers.cpp index ddb128ebe..bd9c3a9ea 100644 --- a/src/helpers.cpp +++ b/src/helpers.cpp @@ -104,7 +104,7 @@ char * Helpers::render_value(char * result, uint8_t value, uint8_t format) { return (result); } - static char s2[5] = {0}; + char s2[5] = {0}; switch (format) { case 2: @@ -226,7 +226,7 @@ char * Helpers::render_value(char * result, const uint32_t value, const uint8_t return (result); } - static char s[20] = {0}; + char s[20] = {0}; #ifndef EMSESP_STANDALONE if (format <= 1) { diff --git a/src/system.cpp b/src/system.cpp index 27d0bc1f3..4b1b38eb1 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -206,7 +206,11 @@ void System::start() { settings.app_version(EMSESP_APP_VERSION); settings.commit(); +#if defined(ESP32) + LOG_INFO(F("System booted (EMS-ESP version %s ESP32)"), settings.app_version().c_str()); +#else LOG_INFO(F("System booted (EMS-ESP version %s)"), settings.app_version().c_str()); +#endif if (LED_GPIO) { pinMode(LED_GPIO, OUTPUT); // LED pin, 0 is disabled diff --git a/src/telegram.cpp b/src/telegram.cpp index 7ee6bd0b1..cc4fb0335 100644 --- a/src/telegram.cpp +++ b/src/telegram.cpp @@ -461,6 +461,11 @@ void TxService::send_telegram(const QueuedTxTelegram & tx_telegram) { length++; // add one since we want to now include the CRC +#if defined(ESP8266) + Settings settings; + if (settings.ems_tx_mode() <= 4) { +#endif + // This logging causes errors with timer based tx-modes on esp8266! LOG_DEBUG(F("Sending %s Tx [#%d], telegram: %s"), (telegram->operation == Telegram::Operation::TX_WRITE) ? F("write") : F("read"), tx_telegram.id_, @@ -472,7 +477,9 @@ void TxService::send_telegram(const QueuedTxTelegram & tx_telegram) { LOG_NOTICE(F("[DEBUG] Tx: %s"), Helpers::data_to_hex(telegram_raw, length).c_str()); } #endif - +#if defined(ESP8266) + } +#endif // send the telegram to the UART Tx uint16_t status = EMSuart::transmit(telegram_raw, length); @@ -497,13 +504,10 @@ void TxService::send_telegram(const uint8_t * data, const uint8_t length) { } telegram_raw[length] = calculate_crc(telegram_raw, length); // apppend CRC - LOG_DEBUG(F("Sending Raw telegram: %s (length=%d)"), Helpers::data_to_hex(telegram_raw, length).c_str(), length); - tx_waiting(false); // no post validation // send the telegram to the UART Tx uint16_t status = EMSuart::transmit(telegram_raw, length); - //LOG_DEBUG(F("Tx: %s"), Helpers::data_to_hex(telegram_raw, length).c_str()); if (status == EMS_TX_STATUS_ERR) { LOG_ERROR(F("Failed to transmit Tx via UART.")); diff --git a/src/uart/emsuart_esp32.cpp b/src/uart/emsuart_esp32.cpp index 111457217..c93e4b2ad 100644 --- a/src/uart/emsuart_esp32.cpp +++ b/src/uart/emsuart_esp32.cpp @@ -33,11 +33,10 @@ static RingbufHandle_t buf_handle = NULL; 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 @@ -63,6 +62,10 @@ void IRAM_ATTR EMSuart::emsuart_rx_intr_handler(void * para) { if (EMS_UART.int_st.brk_det) { EMS_UART.int_clr.brk_det = 1; // clear flag + if (emsTxBufIdx < emsTxBufLen) { // timer tx_mode is interrupted by + emsTxBufIdx = emsTxBufLen; // stop timer mode + drop_next_rx = true; // we have trash in buffer + } length = 0; while (EMS_UART.status.rxfifo_cnt) { uint8_t rx = EMS_UART.fifo.rw_byte; // read all bytes from fifo @@ -82,7 +85,7 @@ void IRAM_ATTR EMSuart::emsuart_rx_intr_handler(void * para) { void IRAM_ATTR EMSuart::emsuart_tx_timer_intr_handler() { - if (emsTxBufIdx > 32) { + if (emsTxBufIdx > EMS_MAXBUFFERSIZE) { return; } @@ -92,7 +95,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 } } @@ -101,24 +104,12 @@ void IRAM_ATTR EMSuart::emsuart_tx_timer_intr_handler() { * init UART driver */ void EMSuart::start(uint8_t tx_mode) { - if (tx_mode == EMS_TXMODE_DEFAULT) { - emsTxWait = EMSUART_BIT_TIME * 11; - } else if (tx_mode == EMS_TXMODE_EMSPLUS) { - emsTxWait = EMSUART_BIT_TIME * 20; - } else if (tx_mode == EMS_TXMODE_HT3) { - emsTxWait = EMSUART_BIT_TIME * 17; - } else if (tx_mode > 10) { - emsTxWait = EMSUART_BIT_TIME * tx_mode; - } else if (tx_mode > 5) { - emsTxWait = EMSUART_BIT_TIME * tx_mode * 2; - } - + emsTxWait = EMSUART_TX_BIT_TIME * (tx_mode + 10); if (tx_mode_ != 0xFF) { // uart already initialized tx_mode_ = tx_mode; restart(); return; } - tx_mode_ = tx_mode; uart_config_t uart_config = { @@ -128,23 +119,22 @@ void EMSuart::start(uint8_t tx_mode) { .stop_bits = UART_STOP_BITS_1, .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, }; - - ESP_ERROR_CHECK(uart_param_config(EMSUART_UART, &uart_config)); - ESP_ERROR_CHECK(uart_set_pin(EMSUART_UART, EMSUART_TXPIN, EMSUART_RXPIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE)); + uart_param_config(EMSUART_UART, &uart_config); + uart_set_pin(EMSUART_UART, EMSUART_TXPIN, EMSUART_RXPIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); EMS_UART.int_ena.val = 0; // disable all intr. EMS_UART.int_clr.val = 0xFFFFFFFF; // clear all intr. flags EMS_UART.idle_conf.tx_brk_num = 11; // breaklength 11 bit EMS_UART.idle_conf.rx_idle_thrhd = 256; drop_next_rx = true; buf_handle = xRingbufferCreate(128, RINGBUF_TYPE_NOSPLIT); - ESP_ERROR_CHECK(uart_isr_register(EMSUART_UART, emsuart_rx_intr_handler, NULL, ESP_INTR_FLAG_IRAM, &uart_handle)); + uart_isr_register(EMSUART_UART, emsuart_rx_intr_handler, NULL, ESP_INTR_FLAG_IRAM, &uart_handle); xTaskCreate(emsuart_recvTask, "emsuart_recvTask", 2048, NULL, configMAX_PRIORITIES - 1, NULL); EMS_UART.int_ena.brk_det = 1; // activate only break 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 } /* @@ -152,7 +142,7 @@ void EMSuart::start(uint8_t tx_mode) { */ void EMSuart::stop() { EMS_UART.int_ena.val = 0; // disable all intr. - // timerAlarmDisable(timer); + // timerAlarmDisable(timer); }; /* @@ -163,25 +153,45 @@ void EMSuart::restart() { EMS_UART.int_clr.brk_det = 1; // clear flag drop_next_rx = true; // and drop first frame } - EMS_UART.int_ena.brk_det = 1; // activate only break emsTxBufIdx = 0; emsTxBufLen = 0; -}; +} /* * Sends a 1-byte poll, ending with a */ void EMSuart::send_poll(uint8_t data) { - if (tx_mode_ == EMS_TXMODE_NEW || tx_mode_ == 5) { - EMS_UART.fifo.rw_byte = data; - EMS_UART.conf0.txd_brk = 1; // after send - } else { + // if (tx_mode_ >= 6 || tx_mode_ < 4) { // modes 1, 2, 3 also here + if (tx_mode_ >= 5) { EMS_UART.fifo.rw_byte = data; emsTxBufIdx = 0; emsTxBufLen = 1; timerAlarmWrite(timer, emsTxWait, false); timerAlarmEnable(timer); + } else if (tx_mode_ == EMS_TXMODE_NEW) { + EMS_UART.fifo.rw_byte = data; + EMS_UART.conf0.txd_brk = 1; // after send + } else if (tx_mode_ == EMS_TXMODE_HT3) { + EMS_UART.fifo.rw_byte = data; + delayMicroseconds(EMSUART_TX_WAIT_HT3); + EMS_UART.conf0.txd_brk = 1; // + // delayMicroseconds(EMSUART_TX_WAIT_BRK); + // EMS_UART.conf0.txd_brk = 0; + } else if (tx_mode_ == EMS_TXMODE_EMSPLUS) { + EMS_UART.fifo.rw_byte = data; + delayMicroseconds(EMSUART_TX_WAIT_PLUS); + EMS_UART.conf0.txd_brk = 1; // + // delayMicroseconds(EMSUART_TX_WAIT_BRK); + // EMS_UART.conf0.txd_brk = 0; + } else { + volatile uint8_t _usrxc = EMS_UART.status.rxfifo_cnt; + EMS_UART.fifo.rw_byte = data; + uint8_t timeoutcnt = EMSUART_TX_TIMEOUT; + while ((EMS_UART.status.rxfifo_cnt == _usrxc) && (--timeoutcnt > 0)) { + delayMicroseconds(EMSUART_TX_BUSY_WAIT); // burn CPU cycles... + } + EMS_UART.conf0.txd_brk = 1; // } } @@ -191,16 +201,11 @@ void EMSuart::send_poll(uint8_t data) { * returns code, 1=success */ uint16_t EMSuart::transmit(uint8_t * buf, uint8_t len) { - if (len == 0 || len > 32) { + if (len == 0 || len >= EMS_MAXBUFFERSIZE) { return EMS_TX_STATUS_ERR; } - - if (tx_mode_ == EMS_TXMODE_NEW || tx_mode_ == 5) { - for (uint8_t i = 0; i < len; i++) { - EMS_UART.fifo.rw_byte = buf[i]; - } - EMS_UART.conf0.txd_brk = 1; // after send - } else { + // if (tx_mode_ >= 6 || tx_mode_ < 4) { // timer controlled modes, also modes 1, 2, 3 because delays not working + if (tx_mode_ >= 5) { // timer controlled modes for (uint8_t i = 0; i < len; i++) { emsTxBuf[i] = buf[i]; } @@ -209,8 +214,50 @@ uint16_t EMSuart::transmit(uint8_t * buf, uint8_t len) { emsTxBufLen = len; timerAlarmWrite(timer, emsTxWait, false); timerAlarmEnable(timer); + return EMS_TX_STATUS_OK; } - + if (tx_mode_ == EMS_TXMODE_NEW) { // hardware controlled modes + for (uint8_t i = 0; i < len; i++) { + EMS_UART.fifo.rw_byte = buf[i]; + } + EMS_UART.conf0.txd_brk = 1; // after send + return EMS_TX_STATUS_OK; + } + if (tx_mode_ == EMS_TXMODE_EMSPLUS) { // EMS+ with long delay + for (uint8_t i = 0; i < len; i++) { + EMS_UART.fifo.rw_byte = buf[i]; + delayMicroseconds(EMSUART_TX_WAIT_PLUS); + } + EMS_UART.conf0.txd_brk = 1; // after send, cleard by hardware after send + // delayMicroseconds(EMSUART_TX_WAIT_BRK); + // EMS_UART.conf0.txd_brk = 0; + return EMS_TX_STATUS_OK; + } + if (tx_mode_ == EMS_TXMODE_HT3) { // HT3 with 7 bittimes delay + for (uint8_t i = 0; i < len; i++) { + EMS_UART.fifo.rw_byte = buf[i]; + delayMicroseconds(EMSUART_TX_WAIT_HT3); + } + EMS_UART.conf0.txd_brk = 1; // after send, cleard by hardware after send + // delayMicroseconds(EMSUART_TX_WAIT_BRK); + // EMS_UART.conf0.txd_brk = 0; + return EMS_TX_STATUS_OK; + } + // mode 1 + // flush fifos -- not supported in ESP32 uart #2! + // EMS_UART.conf0.rxfifo_rst = 1; + // EMS_UART.conf0.txfifo_rst = 1; + for (uint8_t i = 0; i < len; i++) { + volatile uint8_t _usrxc = EMS_UART.status.rxfifo_cnt; + EMS_UART.fifo.rw_byte = buf[i]; // send each Tx byte + uint8_t timeoutcnt = EMSUART_TX_TIMEOUT; + while ((EMS_UART.status.rxfifo_cnt == _usrxc) && (--timeoutcnt > 0)) { + delayMicroseconds(EMSUART_TX_BUSY_WAIT); // burn CPU cycles... + } + } + EMS_UART.conf0.txd_brk = 1; // after send, cleard by hardware after send + // delayMicroseconds(EMSUART_TX_WAIT_BRK); + // EMS_UART.conf0.txd_brk = 0; return EMS_TX_STATUS_OK; } diff --git a/src/uart/emsuart_esp32.h b/src/uart/emsuart_esp32.h index e2fe0149b..5e332ea07 100644 --- a/src/uart/emsuart_esp32.h +++ b/src/uart/emsuart_esp32.h @@ -38,13 +38,27 @@ #define EMSUART_UART UART_NUM_2 // on the ESP32 we're using UART2 #define EMS_UART UART2 // for intr setting #define EMSUART_BAUD 9600 // uart baud rate for the EMS circuit -#define EMSUART_BIT_TIME 104 // bit time @9600 baud #define EMS_TXMODE_DEFAULT 1 #define EMS_TXMODE_EMSPLUS 2 #define EMS_TXMODE_HT3 3 #define EMS_TXMODE_NEW 4 // for michael's testing +// LEGACY +#define EMSUART_TX_BIT_TIME 104 // bit time @9600 baud +#define EMSUART_TX_WAIT_BRK (EMSUART_TX_BIT_TIME * 11) // 1144 + +// EMS 1.0 +#define EMSUART_TX_BUSY_WAIT (EMSUART_TX_BIT_TIME / 8) // 13 +#define EMSUART_TX_TIMEOUT (22 * EMSUART_TX_BIT_TIME / EMSUART_TX_BUSY_WAIT) // 176 + +// HT3/Junkers - Time to send one Byte (8 Bits, 1 Start Bit, 1 Stop Bit) plus 7 bit delay. The -8 is for lag compensation. +// since we use a faster processor the lag is negligible +#define EMSUART_TX_WAIT_HT3 (EMSUART_TX_BIT_TIME * 17) // 1768 + +// EMS+ - Time to send one Byte (8 Bits, 1 Start Bit, 1 Stop Bit) and delay of another Bytetime. +#define EMSUART_TX_WAIT_PLUS (EMSUART_TX_BIT_TIME * 20) // 2080 + // customize the GPIO pins for RX and TX here #ifdef WEMOS_D1_32 diff --git a/src/uart/emsuart_esp8266.cpp b/src/uart/emsuart_esp8266.cpp index e0d21c577..faedc23d8 100644 --- a/src/uart/emsuart_esp8266.cpp +++ b/src/uart/emsuart_esp8266.cpp @@ -30,11 +30,11 @@ 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; +uint8_t emsTxBuf[EMS_MAXBUFFERSIZE]; +uint8_t emsTxBufIdx; +uint8_t emsTxBufLen; +uint32_t emsTxWait; + // // Main interrupt handler @@ -46,8 +46,10 @@ void ICACHE_RAM_ATTR EMSuart::emsuart_rx_intr_handler(void * para) { if (USIS(EMSUART_UART) & ((1 << UIBD))) { // BREAK detection = End of EMS data block USC0(EMSUART_UART) &= ~(1 << UCBRK); // reset tx-brk - // just for testing if break isn't finished yet - // while((USS(EMSUART_UART) >> USRXD) == 0); // wait for idle state of pin + if (emsTxBufIdx < emsTxBufLen) { // timer tx_mode is interrupted by + emsTxBufIdx = emsTxBufLen; // stop timer mode + drop_next_rx = true; // we have trash in buffer + } USIC(EMSUART_UART) = (1 << UIBD); // INT clear the BREAK detect interrupt length = 0; while ((USS(EMSUART_UART) >> USRXC) & 0x0FF) { // read fifo into buffer @@ -61,7 +63,6 @@ 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(); } drop_next_rx = false; system_os_post(EMSUART_recvTaskPrio, 0, 0); // call emsuart_recvTask() at next opportunity @@ -79,16 +80,8 @@ 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; - // 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); - return; - } - - // ignore double BRK at the end, possibly from the Tx loopback - // also telegrams with no data value - // then transmit EMS buffer, excluding the BRK - if (length > 4) { + // Ignore telegrams with no data value, then transmit EMS buffer, excluding the BRK + if (length > 4 || length == 2) { EMSESP::incoming_telegram((uint8_t *)pCurrent->buffer, length - 1); } } @@ -97,9 +90,8 @@ void ICACHE_FLASH_ATTR EMSuart::emsuart_recvTask(os_event_t * events) { * flush everything left over in buffer, this clears both rx and tx FIFOs */ void ICACHE_FLASH_ATTR EMSuart::emsuart_flush_fifos() { - uint32_t tmp = ((1 << UCRXRST) | (1 << UCTXRST)); // bit mask - USC0(EMSUART_UART) |= (tmp); // set bits - USC0(EMSUART_UART) &= ~(tmp); // clear bits + USC0(EMSUART_UART) |= ((1 << UCRXRST) | (1 << UCTXRST)); // set bits + USC0(EMSUART_UART) &= ~((1 << UCRXRST) | (1 << UCTXRST)); // clear bits } // ISR to Fire when Timer is triggered @@ -107,54 +99,33 @@ void ICACHE_RAM_ATTR EMSuart::emsuart_tx_timer_intr_handler() { if (emsTxBufIdx > EMS_MAXBUFFERSIZE) { return; } - emsTxBufIdx++; if (emsTxBufIdx < emsTxBufLen) { USF(EMSUART_UART) = emsTxBuf[emsTxBufIdx]; timer1_write(emsTxWait); } 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); - 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 } } - /* * init UART0 driver */ 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 - } else if (tx_mode > 5) { - emsTxWait = 10 * EMSUART_TX_BIT_TIME * tx_mode; // bittimes for tx_mode + if (tx_mode >= 5) { + emsTxWait = 5 * EMSUART_TX_BIT_TIME * (tx_mode + 10); // bittimes for tx_mode } - - if (tx_mode == 5) { - USC0(EMSUART_UART) = 0x2C; // 8N1,5 - } else { - USC0(EMSUART_UART) = EMSUART_CONFIG; // 8N1 - } - if (tx_mode_ != 0xFF) { // it's a restart no need to configure uart tx_mode_ = 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(); @@ -166,7 +137,9 @@ 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); - USD(EMSUART_UART) = (UART_CLK_FREQ / EMSUART_BAUD); + // set 9600, 8 bits, no parity check, 1 stop bit + USD(EMSUART_UART) = (UART_CLK_FREQ / EMSUART_BAUD); + USC0(EMSUART_UART) = EMSUART_CONFIG; // 8N1 emsuart_flush_fifos(); @@ -180,7 +153,7 @@ void ICACHE_FLASH_ATTR EMSuart::start(uint8_t tx_mode) { // Otherwise, we're only noticed by UCTOT or RxBRK! // change: don't care, we do not use these interrupts USC1(EMSUART_UART) = 0; // reset config first - // USC1(EMSUART_UART) = (0x7F << UCFFT) | (0x04 << UCTOT) | (1 << UCTOE); // enable interupts + // USC1(EMSUART_UART) = (0x7F << UCFFT) | (0x01 << UCTOT) | (1 << UCTOE); // enable interupts // set interrupts for triggers USIC(EMSUART_UART) = 0xFFFF; // clear all interupts @@ -204,6 +177,7 @@ 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 @@ -238,25 +212,18 @@ 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)) - ; - // do not clear buffers to get a echo back - // tmp = ((1 << UCRXRST) | (1 << UCTXRST)); // bit mask - // USC0(EMSUART_UART) |= (tmp); // set bits - // USC0(EMSUART_UART) &= ~(tmp); // clear bits + // make sure Tx FIFO is empty + while (((USS(EMSUART_UART) >> USTXC) & 0xFF)) { + } // To create a 11-bit we set TXD_BRK bit so the break signal will // automatically be sent when the tx fifo is empty ETS_UART_INTR_DISABLE(); USC0(EMSUART_UART) |= (1 << UCBRK); // set bit - if (tx_mode_ == EMS_TXMODE_EMSPLUS) { // EMS+ mode - delayMicroseconds(EMSUART_TX_BRK_WAIT); // 2070 - } else if (tx_mode_ == EMS_TXMODE_HT3) { // junkers mode - delayMicroseconds(EMSUART_TX_BRK_WAIT_HT3); // 1144 - } + // also for EMS+ there is no need to wait longer, we are finished and can free the bus. + delayMicroseconds(EMSUART_TX_WAIT_BRK); // 1144 USC0(EMSUART_UART) &= ~(1 << UCBRK); // clear BRK bit ETS_UART_INTR_ENABLE(); @@ -269,19 +236,38 @@ void ICACHE_FLASH_ATTR EMSuart::tx_brk() { void EMSuart::send_poll(uint8_t data) { // reset tx-brk, just in case it is accidentally set USC0(EMSUART_UART) &= ~(1 << UCBRK); - if (tx_mode_ > 5) { // timer controlled modes + + 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) || (tx_mode_ == 5)) { // hardware controlled modes + } 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 - // EMS1.0, EMS+ and HT3 + USC0(EMSUART_UART) |= (1 << UCBRK); // brk after sendout + } else if (tx_mode_ == EMS_TXMODE_HT3) { USF(EMSUART_UART) = data; - delayMicroseconds(EMSUART_TX_BRK_WAIT); + delayMicroseconds(EMSUART_TX_WAIT_HT3); tx_brk(); // send + } else if (tx_mode_ == EMS_TXMODE_EMSPLUS) { + USF(EMSUART_UART) = data; + delayMicroseconds(EMSUART_TX_WAIT_PLUS); + tx_brk(); // send + } else { // EMS1.0, same logic as in transmit + ETS_UART_INTR_DISABLE(); + volatile uint8_t _usrxc = (USS(EMSUART_UART) >> USRXC) & 0xFF; + USF(EMSUART_UART) = data; + uint8_t timeoutcnt = EMSUART_TX_TIMEOUT; + while ((((USS(EMSUART_UART) >> USRXC) & 0xFF) == _usrxc) && (--timeoutcnt > 0)) { + delayMicroseconds(EMSUART_TX_BUSY_WAIT); // burn CPU cycles... + } + USC0(EMSUART_UART) |= (1 << UCBRK); // set + timeoutcnt = EMSUART_TX_TIMEOUT; + while (!(USIR(EMSUART_UART) & (1 << UIBD)) && (--timeoutcnt > 0)) { + delayMicroseconds(EMSUART_TX_BUSY_WAIT); + } + USC0(EMSUART_UART) &= ~(1 << UCBRK); // clear + ETS_UART_INTR_ENABLE(); } } @@ -291,24 +277,15 @@ 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 > EMS_MAXBUFFERSIZE) { + 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); // timer controlled modes with extra delay - if (tx_mode_ > 5) { + if (tx_mode_ >= 5) { for (uint8_t i = 0; i < len; i++) { emsTxBuf[i] = buf[i]; } @@ -320,7 +297,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]; } @@ -329,26 +306,24 @@ 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) { // tx_mode 2, with extra tx delay for EMS+ + if (tx_mode_ == EMS_TXMODE_EMSPLUS) { // 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 + delayMicroseconds(EMSUART_TX_WAIT_PLUS); // 2070 } tx_brk(); // send return EMS_TX_STATUS_OK; } // Junkers logic by @philrich - if (tx_mode_ == EMS_TXMODE_HT3) { // tx_mode 3 + if (tx_mode_ == EMS_TXMODE_HT3) { for (uint8_t i = 0; i < len; i++) { USF(EMSUART_UART) = buf[i]; - // just to be safe wait for tx fifo empty (still needed?) - while (((USS(EMSUART_UART) >> USTXC) & 0xff)) - ; - + while (((USS(EMSUART_UART) >> USTXC) & 0xff)) { + } // wait until bits are sent on wire - delayMicroseconds(EMSUART_TX_BRK_WAIT_HT3); + delayMicroseconds(EMSUART_TX_WAIT_HT3); } tx_brk(); // send return EMS_TX_STATUS_OK; @@ -382,15 +357,15 @@ 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 ETS_UART_INTR_DISABLE(); - // USC0(EMSUART_UART) |= (1 << UCRXRST); // reset uart rx fifo emsuart_flush_fifos(); // send the bytes along the serial line for (uint8_t i = 0; i < len; i++) { volatile uint8_t _usrxc = (USS(EMSUART_UART) >> USRXC) & 0xFF; + uint8_t timeoutcnt = EMSUART_TX_TIMEOUT; USF(EMSUART_UART) = buf[i]; // send each Tx byte - // wait for echo from the busmaster - while (((USS(EMSUART_UART) >> USRXC) & 0xFF) == _usrxc) { + // wait for echo + while ((((USS(EMSUART_UART) >> USRXC) & 0xFF) == _usrxc) && (--timeoutcnt > 0)) { delayMicroseconds(EMSUART_TX_BUSY_WAIT); // burn CPU cycles... } } @@ -402,12 +377,11 @@ uint16_t ICACHE_FLASH_ATTR EMSuart::transmit(uint8_t * buf, uint8_t len) { // neither bus collision nor timeout - send terminating BRK signal if (!(USIS(EMSUART_UART) & (1 << UIBD))) { // no bus collision - send terminating BRK signal - // USC0(EMSUART_UART) |= (1 << UCLBE) | (1 << UCBRK); // enable loopback & set USC0(EMSUART_UART) |= (1 << UCBRK); // set - + uint8_t timeoutcnt = EMSUART_TX_TIMEOUT; // wait until BRK detected... - while (!(USIR(EMSUART_UART) & (1 << UIBD))) { - delayMicroseconds(EMSUART_TX_BIT_TIME); + while (!(USIR(EMSUART_UART) & (1 << UIBD)) && (--timeoutcnt > 0)) { + delayMicroseconds(EMSUART_TX_BUSY_WAIT); } USC0(EMSUART_UART) &= ~(1 << UCBRK); // clear diff --git a/src/uart/emsuart_esp8266.h b/src/uart/emsuart_esp8266.h index 26ca2478c..0f6d7d1e7 100644 --- a/src/uart/emsuart_esp8266.h +++ b/src/uart/emsuart_esp8266.h @@ -42,13 +42,18 @@ // LEGACY #define EMSUART_TX_BIT_TIME 104 // bit time @9600 baud -#define EMSUART_TX_BRK_WAIT 2070 // the BRK from Boiler master is roughly 1.039ms, so accounting for hardware lag using around 2078 (for half-duplex) - 8 (lag) +#define EMSUART_TX_WAIT_BRK (EMSUART_TX_BIT_TIME * 11) // 1144 // EMS 1.0 #define EMSUART_TX_BUSY_WAIT (EMSUART_TX_BIT_TIME / 8) // 13 +#define EMSUART_TX_TIMEOUT (22 * EMSUART_TX_BIT_TIME / EMSUART_TX_BUSY_WAIT) // 176 -// HT3/Junkers - Time to send one Byte (8 Bits, 1 Start Bit, 1 Stop Bit). The -8 is for lag compensation. -#define EMSUART_TX_BRK_WAIT_HT3 (EMSUART_TX_BIT_TIME * 11) - 8 // 1136 +// HT3/Junkers - Time to send one Byte (8 Bits, 1 Start Bit, 1 Stop Bit) plus 7 bit delay. The -8 is for lag compensation. +// since we use a faster processor the lag is negligible +#define EMSUART_TX_WAIT_HT3 (EMSUART_TX_BIT_TIME * 17) // 1768 + +// EMS+ - Time to send one Byte (8 Bits, 1 Start Bit, 1 Stop Bit) and delay of another Bytetime. +#define EMSUART_TX_WAIT_PLUS (EMSUART_TX_BIT_TIME * 20) // 2080 namespace emsesp { @@ -72,8 +77,6 @@ class EMSuart { } EMSRxBuf_t; private: - // static constexpr uint32_t EMS_RX_TO_TX_TIMEOUT = 20; - static uuid::log::Logger logger_; static void ICACHE_RAM_ATTR emsuart_rx_intr_handler(void * para); static void ICACHE_FLASH_ATTR emsuart_recvTask(os_event_t * events); static void ICACHE_FLASH_ATTR emsuart_flush_fifos();