mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 16:29:51 +03:00
remove tx-modes 5-255
This commit is contained in:
@@ -28,15 +28,10 @@
|
||||
|
||||
namespace emsesp {
|
||||
|
||||
static RingbufHandle_t buf_handle = NULL;
|
||||
portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
|
||||
static hw_timer_t * timer = NULL;
|
||||
bool drop_next_rx = true;
|
||||
uint8_t tx_mode_ = 0xFF;
|
||||
uint8_t emsTxBuf[EMS_MAXBUFFERSIZE];
|
||||
uint8_t emsTxBufIdx = 0;
|
||||
uint8_t emsTxBufLen = 0;
|
||||
uint32_t emsTxWait;
|
||||
RingbufHandle_t buf_handle_ = NULL;
|
||||
portMUX_TYPE mux_ = portMUX_INITIALIZER_UNLOCKED;
|
||||
bool drop_next_rx_ = true;
|
||||
uint8_t tx_mode_ = 0xFF;
|
||||
|
||||
/*
|
||||
* Task to handle the incoming data
|
||||
@@ -44,11 +39,11 @@ uint32_t emsTxWait;
|
||||
void EMSuart::emsuart_recvTask(void * para) {
|
||||
while (1) {
|
||||
size_t item_size;
|
||||
uint8_t * telegram = (uint8_t *)xRingbufferReceive(buf_handle, &item_size, portMAX_DELAY);
|
||||
uint8_t * telegram = (uint8_t *)xRingbufferReceive(buf_handle_, &item_size, portMAX_DELAY);
|
||||
uint8_t telegramSize = item_size;
|
||||
if (telegram) {
|
||||
EMSESP::incoming_telegram(telegram, telegramSize);
|
||||
vRingbufferReturnItem(buf_handle, (void *)telegram);
|
||||
vRingbufferReturnItem(buf_handle_, (void *)telegram);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -57,7 +52,7 @@ void EMSuart::emsuart_recvTask(void * para) {
|
||||
* UART interrupt, on break read the fifo and put the whole telegram to ringbuffer
|
||||
*/
|
||||
void IRAM_ATTR EMSuart::emsuart_rx_intr_handler(void * para) {
|
||||
portENTER_CRITICAL(&mux);
|
||||
portENTER_CRITICAL(&mux_);
|
||||
if (EMS_UART.int_st.brk_det) {
|
||||
EMS_UART.int_clr.brk_det = 1; // clear flag
|
||||
uint8_t rxbuf[EMS_MAXBUFFERSIZE];
|
||||
@@ -65,40 +60,23 @@ void IRAM_ATTR EMSuart::emsuart_rx_intr_handler(void * para) {
|
||||
while (EMS_UART.status.rxfifo_cnt) {
|
||||
uint8_t rx = EMS_UART.fifo.rw_byte; // read all bytes from fifo
|
||||
if (length < EMS_MAXBUFFERSIZE) {
|
||||
rxbuf[length++] = rx;
|
||||
if (length || rx) { // skip leading zero
|
||||
rxbuf[length++] = rx;
|
||||
}
|
||||
} else {
|
||||
drop_next_rx = true; // we have a overflow
|
||||
drop_next_rx_ = true; // we have a overflow
|
||||
}
|
||||
}
|
||||
if ((!drop_next_rx) && ((length == 2) || (length > 4))) {
|
||||
if (rxbuf[length - 1]) { // check if last byte is break
|
||||
length++;
|
||||
}
|
||||
if ((!drop_next_rx_) && ((length == 2) || (length > 4))) {
|
||||
int baseType = 0;
|
||||
xRingbufferSendFromISR(buf_handle, rxbuf, length - 1, &baseType);
|
||||
xRingbufferSendFromISR(buf_handle_, rxbuf, length - 1, &baseType);
|
||||
}
|
||||
drop_next_rx = false;
|
||||
drop_next_rx_ = false;
|
||||
}
|
||||
portEXIT_CRITICAL(&mux);
|
||||
}
|
||||
|
||||
void IRAM_ATTR EMSuart::emsuart_tx_timer_intr_handler() {
|
||||
if (emsTxBufLen == 0) {
|
||||
return;
|
||||
}
|
||||
portENTER_CRITICAL(&mux);
|
||||
if (emsTxBufIdx < emsTxBufLen) {
|
||||
EMS_UART.fifo.rw_byte = emsTxBuf[emsTxBufIdx];
|
||||
if (emsTxBufIdx == 1) {
|
||||
timerAlarmWrite(timer, emsTxWait, true);
|
||||
}
|
||||
} else if (emsTxBufIdx == emsTxBufLen) {
|
||||
EMS_UART.conf0.txd_inv = 1;
|
||||
timerAlarmWrite(timer, EMSUART_TX_BRK_TIMER, true);
|
||||
} else if (emsTxBufIdx == emsTxBufLen + 1) {
|
||||
EMS_UART.conf0.txd_inv = 0;
|
||||
emsTxBufLen = 0;
|
||||
timerAlarmDisable(timer);
|
||||
}
|
||||
emsTxBufIdx++;
|
||||
portEXIT_CRITICAL(&mux);
|
||||
portEXIT_CRITICAL(&mux_);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -111,7 +89,7 @@ void EMSuart::start(const uint8_t tx_mode, const uint8_t rx_gpio, const uint8_t
|
||||
return;
|
||||
}
|
||||
tx_mode_ = tx_mode;
|
||||
|
||||
portENTER_CRITICAL(&mux_);
|
||||
uart_config_t uart_config = {
|
||||
.baud_rate = EMSUART_BAUD,
|
||||
.data_bits = UART_DATA_8_BITS,
|
||||
@@ -125,14 +103,17 @@ void EMSuart::start(const uint8_t tx_mode, const uint8_t rx_gpio, const uint8_t
|
||||
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 = 10; // breaklength 10 bit
|
||||
EMS_UART.idle_conf.rx_idle_thrhd = 256;
|
||||
drop_next_rx = true;
|
||||
buf_handle = xRingbufferCreate(128, RINGBUF_TYPE_NOSPLIT);
|
||||
drop_next_rx_ = true;
|
||||
// EMS_UART.idle_conf.rx_idle_thrhd = 256;
|
||||
// EMS_UART.auto_baud.glitch_filt = 192;
|
||||
#if (EMSUART_UART != UART_NUM_2)
|
||||
EMS_UART.conf0.rxfifo_rst = 1; // flush fifos, remove for UART2
|
||||
EMS_UART.conf0.txfifo_rst = 1;
|
||||
#endif
|
||||
buf_handle_ = xRingbufferCreate(128, RINGBUF_TYPE_NOSPLIT);
|
||||
uart_isr_register(EMSUART_UART, emsuart_rx_intr_handler, NULL, ESP_INTR_FLAG_IRAM, NULL);
|
||||
xTaskCreate(emsuart_recvTask, "emsuart_recvTask", 2048, NULL, configMAX_PRIORITIES - 1, NULL);
|
||||
|
||||
timer = timerBegin(0, 80, true); // timer prescale to 1 us, countup
|
||||
timerAttachInterrupt(timer, &emsuart_tx_timer_intr_handler, true); // Timer with edge interrupt
|
||||
xTaskCreate(emsuart_recvTask, "emsuart_recvTask", 2048, NULL, configMAX_PRIORITIES - 3, NULL);
|
||||
portEXIT_CRITICAL(&mux_);
|
||||
restart();
|
||||
}
|
||||
|
||||
@@ -140,34 +121,24 @@ void EMSuart::start(const uint8_t tx_mode, const uint8_t rx_gpio, const uint8_t
|
||||
* Stop, disable interrupt
|
||||
*/
|
||||
void EMSuart::stop() {
|
||||
EMS_UART.int_ena.val = 0; // disable all intr.
|
||||
EMS_UART.conf0.txd_inv = 0; // stop break
|
||||
if (emsTxBufLen > 0) {
|
||||
timerAlarmDisable(timer);
|
||||
}
|
||||
portENTER_CRITICAL(&mux_);
|
||||
EMS_UART.int_ena.val = 0; // disable all intr.
|
||||
portEXIT_CRITICAL(&mux_);
|
||||
};
|
||||
|
||||
/*
|
||||
* Restart uart and make mode dependent configs.
|
||||
*/
|
||||
void EMSuart::restart() {
|
||||
portENTER_CRITICAL(&mux_);
|
||||
if (EMS_UART.int_raw.brk_det) { // we received a break in the meantime
|
||||
EMS_UART.int_clr.brk_det = 1; // clear flag
|
||||
drop_next_rx = true; // and drop first frame
|
||||
drop_next_rx_ = true; // and drop first frame
|
||||
}
|
||||
EMS_UART.int_ena.brk_det = 1; // activate only break
|
||||
emsTxBufIdx = 0;
|
||||
emsTxBufLen = 0;
|
||||
if (tx_mode_ > 100) {
|
||||
emsTxWait = EMSUART_TX_BIT_TIME * (tx_mode_ - 90);
|
||||
} else {
|
||||
emsTxWait = EMSUART_TX_BIT_TIME * (tx_mode_ + 10);
|
||||
}
|
||||
if (tx_mode_ == EMS_TXMODE_NEW) {
|
||||
EMS_UART.conf0.txd_brk = 1;
|
||||
} else {
|
||||
EMS_UART.conf0.txd_brk = 0;
|
||||
}
|
||||
EMS_UART.conf0.txd_brk = (tx_mode_ == EMS_TXMODE_HW) ? 1 : 0;
|
||||
portEXIT_CRITICAL(&mux_);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -186,26 +157,12 @@ uint16_t EMSuart::transmit(const uint8_t * buf, const uint8_t len) {
|
||||
if (len == 0 || len >= EMS_MAXBUFFERSIZE) {
|
||||
return EMS_TX_STATUS_ERR;
|
||||
}
|
||||
|
||||
if (tx_mode_ == 0) {
|
||||
return EMS_TX_STATUS_OK;
|
||||
}
|
||||
|
||||
if (tx_mode_ > 5) { // timer controlled modes
|
||||
for (uint8_t i = 0; i < len; i++) {
|
||||
emsTxBuf[i] = buf[i];
|
||||
}
|
||||
emsTxBufIdx = 0;
|
||||
emsTxBufLen = len;
|
||||
if (tx_mode_ > 100 && len > 1) {
|
||||
timerAlarmWrite(timer, EMSUART_TX_WAIT_REPLY, true);
|
||||
} else {
|
||||
timerAlarmWrite(timer, emsTxWait, true); // start with autoreload
|
||||
}
|
||||
timerAlarmEnable(timer);
|
||||
return EMS_TX_STATUS_OK;
|
||||
}
|
||||
|
||||
if (tx_mode_ == EMS_TXMODE_NEW) { // hardware controlled modes
|
||||
if (tx_mode_ == EMS_TXMODE_HW) { // hardware controlled mode
|
||||
for (uint8_t i = 0; i < len; i++) {
|
||||
EMS_UART.fifo.rw_byte = buf[i];
|
||||
}
|
||||
|
||||
@@ -43,15 +43,11 @@
|
||||
#define EMS_TXMODE_DEFAULT 1
|
||||
#define EMS_TXMODE_EMSPLUS 2
|
||||
#define EMS_TXMODE_HT3 3
|
||||
#define EMS_TXMODE_NEW 4 // for michael's testing
|
||||
#define EMS_TXMODE_HW 4
|
||||
|
||||
// LEGACY
|
||||
#define EMSUART_TX_BIT_TIME 104 // bit time @9600 baud
|
||||
|
||||
// Timer controlled modes
|
||||
#define EMSUART_TX_BRK_TIMER (EMSUART_TX_BIT_TIME * 10 + 28) // 10.25 bit times
|
||||
#define EMSUART_TX_WAIT_REPLY 100000 // delay 100ms after first byte
|
||||
|
||||
// EMS 1.0
|
||||
#define EMSUART_TX_BUSY_WAIT (EMSUART_TX_BIT_TIME / 8) // 13
|
||||
#define EMSUART_TX_TIMEOUT (20 * EMSUART_TX_BIT_TIME / EMSUART_TX_BUSY_WAIT)
|
||||
@@ -79,13 +75,12 @@ class EMSuart {
|
||||
static void start(const uint8_t tx_mode, const uint8_t rx_gpio, const uint8_t tx_gpio);
|
||||
static void send_poll(const uint8_t data);
|
||||
static void stop();
|
||||
static void restart();
|
||||
static uint16_t transmit(const uint8_t * buf, const 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();
|
||||
static void restart();
|
||||
};
|
||||
|
||||
} // namespace emsesp
|
||||
|
||||
Reference in New Issue
Block a user