mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-08 08:49:52 +03:00
Test new tx_mode 2, small changes to MM100, SM100 and RC300, see #397
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
/*
|
||||
* ESP32 UART port by @ArwedL and improved by @MichaelDvP. See https://github.com/proddy/EMS-ESP/issues/380
|
||||
*/
|
||||
#if defined(ESP32)
|
||||
|
||||
#ifndef EMSESP_EMSUART_H
|
||||
#define EMSESP_EMSUART_H
|
||||
@@ -74,3 +75,4 @@ class EMSuart {
|
||||
} // namespace emsesp
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -35,6 +35,11 @@ uint8_t phantomBreak = 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;
|
||||
|
||||
|
||||
//
|
||||
// Main interrupt handler
|
||||
@@ -45,6 +50,12 @@ void ICACHE_RAM_ATTR EMSuart::emsuart_rx_intr_handler(void * para) {
|
||||
static uint8_t uart_buffer[EMS_MAXBUFFERSIZE + 2];
|
||||
|
||||
if (USIS(EMSUART_UART) & ((1 << UIBD))) { // BREAK detection = End of EMS data block
|
||||
USC0(EMSUART_UART) &= ~(1 << UCBRK); // reset tx-brk
|
||||
// while((USS(EMSUART_UART) >> USRXD) == 0); // wait for idle state of pin
|
||||
// if((USS(EMSUART_UART) >> USRXD) == 0) { // if rx is not idle wait one bittime
|
||||
// delayMicroseconds(EMSUART_BIT_TIME);
|
||||
// }
|
||||
USIC(EMSUART_UART) = (1 << UIBD); // INT clear the BREAK detect interrupt
|
||||
length = 0;
|
||||
while ((USS(EMSUART_UART) >> USRXC) & 0x0FF) { // read fifo into buffer
|
||||
uint8_t rx = USF(EMSUART_UART);
|
||||
@@ -54,8 +65,6 @@ void ICACHE_RAM_ATTR EMSuart::emsuart_rx_intr_handler(void * para) {
|
||||
drop_next_rx = true;
|
||||
}
|
||||
}
|
||||
USIC(EMSUART_UART) = (1 << UIBD); // INT clear the BREAK detect interrupt
|
||||
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
|
||||
@@ -105,6 +114,19 @@ void ICACHE_FLASH_ATTR EMSuart::emsuart_flush_fifos() {
|
||||
USC0(EMSUART_UART) &= ~(tmp); // clear bits
|
||||
}
|
||||
|
||||
// ISR to Fire when Timer is triggered
|
||||
void ICACHE_RAM_ATTR EMSuart::emsuart_tx_timer_intr_handler() {
|
||||
emsTxBufIdx++;
|
||||
if (emsTxBufIdx < emsTxBufLen) {
|
||||
USF(EMSUART_UART) = emsTxBuf[emsTxBufIdx];
|
||||
timer1_write(emsTxWait);
|
||||
} else if (emsTxBufIdx == emsTxBufLen) {
|
||||
USC0(EMSUART_UART) |= (1 << UCBRK); // set <BRK>
|
||||
// timer1_write(emsTxWait);
|
||||
// } else {
|
||||
// USC0(EMSUART_UART) &= ~(1 << UCBRK); // clear <BRK>
|
||||
}
|
||||
}
|
||||
/*
|
||||
* init UART0 driver
|
||||
*/
|
||||
@@ -146,9 +168,9 @@ void ICACHE_FLASH_ATTR EMSuart::start(uint8_t tx_mode) {
|
||||
// change: we set UCFFT to 1 to get an immediate indicator about incoming traffic.
|
||||
// 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) = (0x01 << UCFFT) | (0x01 << UCTOT) | (0 << UCTOE); // enable interupts
|
||||
|
||||
// USC1(EMSUART_UART) = 0; // reset config first
|
||||
// USC1(EMSUART_UART) = (0x7F << UCFFT) | (0x04 << UCTOT) | (1 << UCTOE); // enable interupts
|
||||
|
||||
// set interrupts for triggers
|
||||
USIC(EMSUART_UART) = 0xFFFF; // clear all interupts
|
||||
USIE(EMSUART_UART) = 0; // disable all interrupts
|
||||
@@ -172,6 +194,11 @@ void ICACHE_FLASH_ATTR EMSuart::start(uint8_t tx_mode) {
|
||||
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
|
||||
timer1_enable(TIM_DIV16, TIM_EDGE, TIM_SINGLE); // 5 MHz timer
|
||||
emsTxWait = 5 * EMSUART_BIT_TIME * 20; // 20 bittimes for tx_mode 2
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -180,6 +207,7 @@ void ICACHE_FLASH_ATTR EMSuart::start(uint8_t tx_mode) {
|
||||
*/
|
||||
void ICACHE_FLASH_ATTR EMSuart::stop() {
|
||||
ETS_UART_INTR_DISABLE();
|
||||
timer1_disable();
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -191,6 +219,9 @@ void ICACHE_FLASH_ATTR EMSuart::restart() {
|
||||
drop_next_rx = true;
|
||||
}
|
||||
ETS_UART_INTR_ENABLE();
|
||||
emsTxBufIdx = 0;
|
||||
emsTxBufLen = 0;
|
||||
timer1_enable(TIM_DIV16, TIM_EDGE, TIM_SINGLE);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -227,17 +258,20 @@ 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) {
|
||||
noInterrupts();
|
||||
if (tx_mode_ == EMS_TXMODE_NEW) {
|
||||
USC0(EMSUART_UART) &= ~(1 << UCBRK); // reset tx-brk
|
||||
USF(EMSUART_UART) = data;
|
||||
USC0(EMSUART_UART) |= (1 << UCBRK); // send <BRK> at the end
|
||||
} else if (tx_mode_ == EMS_TXMODE_EMSPLUS) {
|
||||
USF(EMSUART_UART) = data;
|
||||
emsTxBufIdx = 0;
|
||||
emsTxBufLen = 1;
|
||||
timer1_write(emsTxWait);
|
||||
} else {
|
||||
USF(EMSUART_UART) = data;
|
||||
delayMicroseconds(EMSUART_TX_BRK_WAIT);
|
||||
tx_brk(); // send <BRK>
|
||||
}
|
||||
interrupts();
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -259,30 +293,30 @@ 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); // reset tx-brk
|
||||
noInterrupts();
|
||||
for (uint8_t i = 0; i < len; i++) {
|
||||
USF(EMSUART_UART) = buf[i];
|
||||
}
|
||||
USC0(EMSUART_UART) |= (1 << UCBRK); // send <BRK> at the end
|
||||
interrupts();
|
||||
return EMS_TX_STATUS_OK;
|
||||
}
|
||||
|
||||
// EMS+ https://github.com/proddy/EMS-ESP/issues/23#
|
||||
if (tx_mode_ == EMS_TXMODE_EMSPLUS) { // With extra tx delay for EMS+
|
||||
noInterrupts();
|
||||
for (uint8_t i = 0; i < len; i++) {
|
||||
USF(EMSUART_UART) = buf[i];
|
||||
delayMicroseconds(EMSUART_TX_BRK_WAIT); // 2070
|
||||
emsTxBuf[i] = buf[i];
|
||||
// USF(EMSUART_UART) = buf[i];
|
||||
// delayMicroseconds(EMSUART_TX_BRK_WAIT); // 2070
|
||||
}
|
||||
tx_brk(); // send <BRK>
|
||||
interrupts();
|
||||
emsTxBufIdx = 0;
|
||||
emsTxBufLen = len;
|
||||
USF(EMSUART_UART) = buf[0];
|
||||
timer1_write(emsTxWait);
|
||||
// tx_brk(); // send <BRK>
|
||||
return EMS_TX_STATUS_OK;
|
||||
}
|
||||
|
||||
// Junkers logic by @philrich
|
||||
if (tx_mode_ == EMS_TXMODE_HT3) {
|
||||
noInterrupts();
|
||||
for (uint8_t i = 0; i < len; i++) {
|
||||
USF(EMSUART_UART) = buf[i];
|
||||
|
||||
@@ -294,7 +328,6 @@ EMSUART_STATUS ICACHE_FLASH_ATTR EMSuart::transmit(uint8_t * buf, uint8_t len) {
|
||||
delayMicroseconds(EMSUART_TX_WAIT_BYTE - EMSUART_TX_LAG + EMSUART_TX_WAIT_GAP); // 1760
|
||||
}
|
||||
tx_brk(); // send <BRK>
|
||||
interrupts();
|
||||
return EMS_TX_STATUS_OK;
|
||||
}
|
||||
|
||||
@@ -325,8 +358,8 @@ EMSUART_STATUS 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();
|
||||
// noInterrupts();
|
||||
ETS_UART_INTR_DISABLE();
|
||||
// USC0(EMSUART_UART) |= (1 << UCRXRST); // reset uart rx fifo
|
||||
emsuart_flush_fifos();
|
||||
|
||||
@@ -339,14 +372,14 @@ EMSUART_STATUS ICACHE_FLASH_ATTR EMSuart::transmit(uint8_t * buf, uint8_t len) {
|
||||
while (((USS(EMSUART_UART) >> USRXC) & 0xFF) == _usrxc) {
|
||||
delayMicroseconds(EMSUART_BUSY_WAIT); // burn CPU cycles...
|
||||
if (--wdc == 0) {
|
||||
interrupts();
|
||||
// ETS_UART_INTR_ENABLE();
|
||||
// interrupts();
|
||||
ETS_UART_INTR_ENABLE();
|
||||
return EMS_TX_WTD_TIMEOUT;
|
||||
}
|
||||
if (USIR(EMSUART_UART) & (1 << UIBD)) {
|
||||
USIC(EMSUART_UART) = (1 << UIBD); // clear BRK detect IRQ
|
||||
interrupts();
|
||||
// ETS_UART_INTR_ENABLE();
|
||||
// interrupts();
|
||||
ETS_UART_INTR_ENABLE();
|
||||
return EMS_TX_BRK_DETECT;
|
||||
}
|
||||
}
|
||||
@@ -371,8 +404,8 @@ EMSUART_STATUS ICACHE_FLASH_ATTR EMSuart::transmit(uint8_t * buf, uint8_t len) {
|
||||
phantomBreak = 1;
|
||||
}
|
||||
|
||||
interrupts();
|
||||
// ETS_UART_INTR_ENABLE(); // open up the FIFO again to start receiving
|
||||
// interrupts();
|
||||
ETS_UART_INTR_ENABLE(); // open up the FIFO again to start receiving
|
||||
|
||||
return EMS_TX_STATUS_OK; // send the Tx ok status back
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#if defined(ESP8266)
|
||||
|
||||
#ifndef EMSESP_EMSUART_H
|
||||
#define EMSESP_EMSUART_H
|
||||
@@ -81,8 +82,11 @@ class EMSuart {
|
||||
static void ICACHE_FLASH_ATTR emsuart_recvTask(os_event_t * events);
|
||||
static void ICACHE_FLASH_ATTR emsuart_flush_fifos();
|
||||
static void ICACHE_FLASH_ATTR tx_brk();
|
||||
static void ICACHE_RAM_ATTR emsuart_tx_timer_intr_handler();
|
||||
|
||||
};
|
||||
|
||||
} // namespace emsesp
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user