mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-09 01:09:51 +03:00
esp32 uart timing, RC35 thermostat mqtt enhancement, fix type_id display
This commit is contained in:
@@ -29,9 +29,9 @@
|
||||
namespace emsesp {
|
||||
|
||||
static intr_handle_t uart_handle;
|
||||
static RingbufHandle_t buf_handle = NULL;
|
||||
static bool drop_first_rx = true;
|
||||
static uint8_t tx_mode_ = 0xFF;
|
||||
static RingbufHandle_t buf_handle = NULL;
|
||||
static bool drop_next_rx = true;
|
||||
static uint8_t tx_mode_ = 0xFF;
|
||||
|
||||
/*
|
||||
* Task to handle the incoming data
|
||||
@@ -57,19 +57,20 @@ 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
|
||||
EMS_UART.conf0.txd_brk = 0; // if it was break from sending, clear bit
|
||||
length = 0;
|
||||
while (EMS_UART.status.rxfifo_cnt) {
|
||||
uint8_t rx = EMS_UART.fifo.rw_byte; // read all bytes into buffer
|
||||
uint8_t rx = EMS_UART.fifo.rw_byte; // read all bytes from fifo
|
||||
if (length < EMS_MAXBUFFERSIZE) {
|
||||
rxbuf[length++] = rx;
|
||||
} else {
|
||||
drop_next_rx = true; // we have a overflow
|
||||
}
|
||||
}
|
||||
if ((!drop_first_rx) && ((length == 2) || ((length > 4)))) {
|
||||
if ((!drop_next_rx) && ((length == 2) || (length > 4))) {
|
||||
int baseType = 0;
|
||||
xRingbufferSendFromISR(buf_handle, rxbuf, length - 1, &baseType);
|
||||
}
|
||||
drop_first_rx = false;
|
||||
drop_next_rx = false;
|
||||
}
|
||||
}
|
||||
/*
|
||||
@@ -91,14 +92,15 @@ void EMSuart::start(uint8_t tx_mode) {
|
||||
|
||||
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));
|
||||
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
|
||||
drop_first_rx = true;
|
||||
buf_handle = xRingbufferCreate(128, RINGBUF_TYPE_NOSPLIT);
|
||||
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));
|
||||
xTaskCreate(emsuart_recvTask, "emsuart_recvTask", 2048, NULL, 12, NULL);
|
||||
EMS_UART.int_ena.brk_det = 1; // activate only break
|
||||
EMS_UART.int_ena.brk_det = 1; // activate only break
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -114,19 +116,17 @@ void EMSuart::stop() {
|
||||
void EMSuart::restart() {
|
||||
if (EMS_UART.int_raw.brk_det) {
|
||||
EMS_UART.int_clr.brk_det = 1; // clear flag
|
||||
drop_first_rx = true; // and drop first frame
|
||||
drop_next_rx = true; // and drop first frame
|
||||
}
|
||||
EMS_UART.int_ena.brk_det = 1; // activate only break
|
||||
EMS_UART.int_ena.brk_det = 1; // activate only break
|
||||
};
|
||||
|
||||
/*
|
||||
* Sends a 1-byte poll, ending with a <BRK>
|
||||
*/
|
||||
void EMSuart::send_poll(uint8_t data) {
|
||||
EMS_UART.conf0.txd_brk = 0; // just to make sure the bit is cleared
|
||||
EMS_UART.fifo.rw_byte = data;
|
||||
//EMS_UART.idle_conf.tx_brk_num = 11; // breaklength 11 bit
|
||||
EMS_UART.conf0.txd_brk = 1; // sending ends in a break
|
||||
EMS_UART.fifo.rw_byte = data;
|
||||
EMS_UART.conf0.txd_brk = 1; // <brk> after send
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -136,13 +136,10 @@ void EMSuart::send_poll(uint8_t data) {
|
||||
*/
|
||||
EMSUART_STATUS EMSuart::transmit(uint8_t * buf, uint8_t len) {
|
||||
if (len > 0) {
|
||||
EMS_UART.conf0.txd_brk = 0; // just to make sure the bit is cleared
|
||||
for (uint8_t i = 0; i < len; i++) {
|
||||
EMS_UART.fifo.rw_byte = buf[i];
|
||||
}
|
||||
//uart_tx_chars(EMSUART_UART, (const char *)buf, len);
|
||||
//EMS_UART.idle_conf.tx_brk_num = 11; // breaklength 11 bit
|
||||
EMS_UART.conf0.txd_brk = 1; // sending ends in a break
|
||||
EMS_UART.conf0.txd_brk = 1; // <brk> after send
|
||||
}
|
||||
return EMS_TX_STATUS_OK;
|
||||
}
|
||||
|
||||
@@ -28,10 +28,10 @@ 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_first_rx = true;
|
||||
uint8_t emsRxBufIdx = 0;
|
||||
uint8_t phantomBreak = 0;
|
||||
uint8_t tx_mode_ = 0xFF;
|
||||
bool drop_next_rx = true;
|
||||
|
||||
//
|
||||
// Main interrupt handler
|
||||
@@ -67,17 +67,19 @@ void ICACHE_RAM_ATTR EMSuart::emsuart_rx_intr_handler(void * para) {
|
||||
uint8_t rx = USF(EMSUART_UART);
|
||||
if (length < EMS_MAXBUFFERSIZE) {
|
||||
uart_buffer[length++] = rx;
|
||||
} else {
|
||||
drop_next_rx = true;
|
||||
}
|
||||
}
|
||||
USC0(EMSUART_UART) &= ~(1 << UCBRK); // clear <BRK> bit
|
||||
ETS_UART_INTR_DISABLE(); // disable all interrupts and clear them
|
||||
ETS_UART_INTR_DISABLE(); // disable all interrupts and clear them
|
||||
USIC(EMSUART_UART) = (1 << UIBD); // INT clear the BREAK detect interrupt
|
||||
if (!drop_first_rx) {
|
||||
USC0(EMSUART_UART) &= ~(1 << UCBRK); // reset tx-brk
|
||||
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
|
||||
// rx_idle_ = true; // check set the status flag stating BRK has been received and we can start a new package
|
||||
}
|
||||
drop_first_rx = false;
|
||||
drop_next_rx = false;
|
||||
ETS_UART_INTR_ENABLE(); // re-enable UART interrupts
|
||||
|
||||
system_os_post(EMSUART_recvTaskPrio, 0, 0); // call emsuart_recvTask() at next opportunity
|
||||
@@ -109,7 +111,7 @@ void ICACHE_FLASH_ATTR EMSuart::emsuart_recvTask(os_event_t * events) {
|
||||
// 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) && (length <= EMS_MAXBUFFERSIZE + 1)) {
|
||||
if (length > 4) {
|
||||
EMSESP::incoming_telegram((uint8_t *)pCurrent->buffer, length - 1);
|
||||
}
|
||||
}
|
||||
@@ -188,7 +190,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_first_rx = true;
|
||||
drop_next_rx = true;
|
||||
// LOG_INFO(F("UART service for Rx/Tx started"));
|
||||
}
|
||||
|
||||
@@ -204,9 +206,9 @@ void ICACHE_FLASH_ATTR EMSuart::stop() {
|
||||
* re-start UART0 driver
|
||||
*/
|
||||
void ICACHE_FLASH_ATTR EMSuart::restart() {
|
||||
if (USIS(EMSUART_UART) & ((1 << UIBD))) {
|
||||
if (USIR(EMSUART_UART) & ((1 << UIBD))) {
|
||||
USIC(EMSUART_UART) = (1 << UIBD); // INT clear the BREAK detect interrupt
|
||||
drop_first_rx = true;
|
||||
drop_next_rx = true;
|
||||
}
|
||||
ETS_UART_INTR_ENABLE();
|
||||
// emsuart_flush_fifos();
|
||||
@@ -247,7 +249,7 @@ void ICACHE_FLASH_ATTR EMSuart::tx_brk() {
|
||||
*/
|
||||
void EMSuart::send_poll(uint8_t data) {
|
||||
if (tx_mode_ == EMS_TXMODE_NEW) {
|
||||
USC0(EMSUART_UART) &= ~(1 << UCBRK); // make sure <BRK> bit is cleared
|
||||
USC0(EMSUART_UART) &= ~(1 << UCBRK); // reset tx-brk
|
||||
USF(EMSUART_UART) = data;
|
||||
USC0(EMSUART_UART) |= (1 << UCBRK); // send <BRK> at the end
|
||||
} else {
|
||||
@@ -269,7 +271,7 @@ EMSUART_STATUS 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) {
|
||||
USC0(EMSUART_UART) &= ~(1 << UCBRK); // make sure <BRK> bit is cleared
|
||||
USC0(EMSUART_UART) &= ~(1 << UCBRK); // reset tx-brk
|
||||
for (uint8_t i = 0; i < len; i++) {
|
||||
USF(EMSUART_UART) = buf[i];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user