esp8266 uart with irq disabled

This commit is contained in:
MichaelDvP
2020-06-02 14:01:35 +02:00
parent ba08fb65ef
commit 3300f1e757
6 changed files with 42 additions and 37 deletions

View File

@@ -100,4 +100,4 @@ build_type = release
platform = espressif32 platform = espressif32
board = wemos_d1_mini32 board = wemos_d1_mini32
lib_deps = ${common.libs_core} ${common.libs_esp32} lib_deps = ${common.libs_core} ${common.libs_esp32}
build_flags = ${common.build_flags} ${common.debug_flags} -D PIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH build_flags = ${common.build_flags} ${common.debug_flags} -D PIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH -D WEMOS_D1_32

View File

@@ -66,7 +66,11 @@ class Sensors {
#if defined(ESP8266) #if defined(ESP8266)
static constexpr uint8_t SENSOR_GPIO = 14; // D5 static constexpr uint8_t SENSOR_GPIO = 14; // D5
#elif defined(ESP32) #elif defined(ESP32)
#ifdef WEMOS_D1_32
static constexpr uint8_t SENSOR_GPIO = 18; // Wemos D1-32 for compatibility D5 static constexpr uint8_t SENSOR_GPIO = 18; // Wemos D1-32 for compatibility D5
#else
static constexpr uint8_t SENSOR_GPIO = 14;
#endif
#endif #endif
enum class State { IDLE, READING, SCANNING }; enum class State { IDLE, READING, SCANNING };

View File

@@ -83,10 +83,13 @@ class System {
static constexpr uint8_t LED_GPIO = 2; static constexpr uint8_t LED_GPIO = 2;
static constexpr uint8_t LED_ON = LOW; static constexpr uint8_t LED_ON = LOW;
#elif defined(ESP32) #elif defined(ESP32)
// static constexpr uint8_t LED_GPIO = 5; // on Wemos D32 #ifdef WEMOS_D1_32
// static constexpr uint8_t LED_ON = LOW;
static constexpr uint8_t LED_GPIO = 2; // on Wemos D1-32 static constexpr uint8_t LED_GPIO = 2; // on Wemos D1-32
static constexpr uint8_t LED_ON = HIGH; static constexpr uint8_t LED_ON = HIGH;
#else
static constexpr uint8_t LED_GPIO = 5;
static constexpr uint8_t LED_ON = LOW;
#endif
#else #else
static constexpr uint8_t LED_GPIO = 0; static constexpr uint8_t LED_GPIO = 0;
static constexpr uint8_t LED_ON = 0; static constexpr uint8_t LED_ON = 0;

View File

@@ -126,7 +126,7 @@ void EMSuart::restart() {
*/ */
void EMSuart::send_poll(uint8_t data) { void EMSuart::send_poll(uint8_t data) {
EMS_UART.fifo.rw_byte = data; EMS_UART.fifo.rw_byte = data;
EMS_UART.conf0.txd_brk = 1; // <brk> after send EMS_UART.conf0.txd_brk = 1; // <brk> after send
} }
/* /*
@@ -139,7 +139,7 @@ EMSUART_STATUS EMSuart::transmit(uint8_t * buf, uint8_t len) {
for (uint8_t i = 0; i < len; i++) { for (uint8_t i = 0; i < len; i++) {
EMS_UART.fifo.rw_byte = buf[i]; EMS_UART.fifo.rw_byte = buf[i];
} }
EMS_UART.conf0.txd_brk = 1; // <brk> after send EMS_UART.conf0.txd_brk = 1; // <brk> after send
} }
return EMS_TX_STATUS_OK; return EMS_TX_STATUS_OK;
} }

View File

@@ -39,8 +39,13 @@
#define EMSUART_BAUD 9600 // uart baud rate for the EMS circuit #define EMSUART_BAUD 9600 // uart baud rate for the EMS circuit
// customize the GPIO pins for RX and TX here // customize the GPIO pins for RX and TX here
#ifdef WEMOS_D1_32
#define EMSUART_RXPIN 23 // 17 is UART2 RX. Use 23 for D7 on a Wemos D1-32 mini for backwards compatabilty #define EMSUART_RXPIN 23 // 17 is UART2 RX. Use 23 for D7 on a Wemos D1-32 mini for backwards compatabilty
#define EMSUART_TXPIN 5 // 16 is UART2 TX. Use 5 for D8 on a Wemos D1-32 mini for backwards compatabilty #define EMSUART_TXPIN 5 // 16 is UART2 TX. Use 5 for D8 on a Wemos D1-32 mini for backwards compatabilty
#else
#define EMSUART_RXPIN 17 // 17 is UART2 RX. Use 23 for D7 on a Wemos D1-32 mini for backwards compatabilty
#define EMSUART_TXPIN 16 // 16 is UART2 TX. Use 5 for D8 on a Wemos D1-32 mini for backwards compatabilty
#endif
namespace emsesp { namespace emsesp {

View File

@@ -32,38 +32,21 @@ uint8_t emsRxBufIdx = 0;
uint8_t phantomBreak = 0; uint8_t phantomBreak = 0;
uint8_t tx_mode_ = 0xFF; uint8_t tx_mode_ = 0xFF;
bool drop_next_rx = true; bool drop_next_rx = true;
uint32_t emsRxTime;
#define EMS_RX_TO_TX_TIMEOUT 20
// //
// Main interrupt handler // Main interrupt handler
// Important: must not use ICACHE_FLASH_ATTR // Important: must not use ICACHE_FLASH_ATTR
// //
void ICACHE_RAM_ATTR EMSuart::emsuart_rx_intr_handler(void * para) { void ICACHE_RAM_ATTR EMSuart::emsuart_rx_intr_handler(void * para) {
// static bool rx_idle_ = true;
static uint8_t length = 0; static uint8_t length = 0;
static uint8_t uart_buffer[EMS_MAXBUFFERSIZE + 2]; static uint8_t uart_buffer[EMS_MAXBUFFERSIZE + 2];
/* if (USIS(EMSUART_UART) & ((1 << UIBD))) { // BREAK detection = End of EMS data block
// is a new buffer? if so init the thing for a new telegram
if (rx_idle_) {
rx_idle_ = false; // status set to busy
length = 0;
}
// fill IRQ buffer, by emptying Rx FIFO
if (USIS(EMSUART_UART) & ((1 << UIFF) | (1 << UITO) | (1 << UIBD))) {
while ((USS(EMSUART_UART) >> USRXC) & 0xFF) {
uint8_t rx = USF(EMSUART_UART);
if (length < EMS_MAXBUFFERSIZE)
uart_buffer[length++] = rx;
}
// clear Rx FIFO full and Rx FIFO timeout interrupts
USIC(EMSUART_UART) = (1 << UIFF) | (1 << UITO);
}
*/
// BREAK detection = End of EMS data block
if (USIS(EMSUART_UART) & ((1 << UIBD))) {
length = 0; length = 0;
while ((USS(EMSUART_UART) >> USRXC) & 0xFF) { while ((USS(EMSUART_UART) >> USRXC) & 0xFF) { // read fifo into buffer
uint8_t rx = USF(EMSUART_UART); uint8_t rx = USF(EMSUART_UART);
if (length < EMS_MAXBUFFERSIZE) { if (length < EMS_MAXBUFFERSIZE) {
uart_buffer[length++] = rx; uart_buffer[length++] = rx;
@@ -71,17 +54,14 @@ void ICACHE_RAM_ATTR EMSuart::emsuart_rx_intr_handler(void * para) {
drop_next_rx = true; drop_next_rx = true;
} }
} }
ETS_UART_INTR_DISABLE(); // disable all interrupts and clear them
USIC(EMSUART_UART) = (1 << UIBD); // INT clear the BREAK detect interrupt USIC(EMSUART_UART) = (1 << UIBD); // INT clear the BREAK detect interrupt
USC0(EMSUART_UART) &= ~(1 << UCBRK); // reset tx-brk USC0(EMSUART_UART) &= ~(1 << UCBRK); // reset tx-brk
if (!drop_next_rx) { if (!drop_next_rx) {
pEMSRxBuf->length = length; 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 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 emsRxTime = millis();
} }
drop_next_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 system_os_post(EMSUART_recvTaskPrio, 0, 0); // call emsuart_recvTask() at next opportunity
} }
} }
@@ -211,7 +191,6 @@ void ICACHE_FLASH_ATTR EMSuart::restart() {
drop_next_rx = true; drop_next_rx = true;
} }
ETS_UART_INTR_ENABLE(); ETS_UART_INTR_ENABLE();
// emsuart_flush_fifos();
} }
/* /*
@@ -248,6 +227,7 @@ 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 * 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) { void EMSuart::send_poll(uint8_t data) {
noInterrupts();
if (tx_mode_ == EMS_TXMODE_NEW) { if (tx_mode_ == EMS_TXMODE_NEW) {
USC0(EMSUART_UART) &= ~(1 << UCBRK); // reset tx-brk USC0(EMSUART_UART) &= ~(1 << UCBRK); // reset tx-brk
USF(EMSUART_UART) = data; USF(EMSUART_UART) = data;
@@ -257,6 +237,7 @@ void EMSuart::send_poll(uint8_t data) {
delayMicroseconds(EMSUART_TX_BRK_WAIT); delayMicroseconds(EMSUART_TX_BRK_WAIT);
tx_brk(); // send <BRK> tx_brk(); // send <BRK>
} }
interrupts();
} }
/* /*
@@ -268,29 +249,37 @@ EMSUART_STATUS ICACHE_FLASH_ATTR EMSuart::transmit(uint8_t * buf, uint8_t len) {
if (len == 0) { if (len == 0) {
return EMS_TX_STATUS_OK; // nothing to send return EMS_TX_STATUS_OK; // nothing to send
} }
if(millis() > (emsRxTime + EMS_RX_TO_TX_TIMEOUT)) { // send allowed within 20 ms
return EMS_TX_WTD_TIMEOUT;
}
// new code from Michael. See https://github.com/proddy/EMS-ESP/issues/380 // 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) {
USC0(EMSUART_UART) &= ~(1 << UCBRK); // reset tx-brk USC0(EMSUART_UART) &= ~(1 << UCBRK); // reset tx-brk
noInterrupts();
for (uint8_t i = 0; i < len; i++) { for (uint8_t i = 0; i < len; i++) {
USF(EMSUART_UART) = buf[i]; USF(EMSUART_UART) = buf[i];
} }
USC0(EMSUART_UART) |= (1 << UCBRK); // send <BRK> at the end USC0(EMSUART_UART) |= (1 << UCBRK); // send <BRK> at the end
interrupts();
return EMS_TX_STATUS_OK; return EMS_TX_STATUS_OK;
} }
// EMS+ https://github.com/proddy/EMS-ESP/issues/23# // 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) { // With extra tx delay for EMS+
noInterrupts();
for (uint8_t i = 0; i < len; i++) { for (uint8_t i = 0; i < len; i++) {
USF(EMSUART_UART) = buf[i]; USF(EMSUART_UART) = buf[i];
delayMicroseconds(EMSUART_TX_BRK_WAIT); // 2070 delayMicroseconds(EMSUART_TX_BRK_WAIT); // 2070
} }
tx_brk(); // send <BRK> tx_brk(); // send <BRK>
interrupts();
return EMS_TX_STATUS_OK; return EMS_TX_STATUS_OK;
} }
// Junkers logic by @philrich // Junkers logic by @philrich
if (tx_mode_ == EMS_TXMODE_HT3) { if (tx_mode_ == EMS_TXMODE_HT3) {
noInterrupts();
for (uint8_t i = 0; i < len; i++) { for (uint8_t i = 0; i < len; i++) {
USF(EMSUART_UART) = buf[i]; USF(EMSUART_UART) = buf[i];
@@ -302,6 +291,7 @@ 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 delayMicroseconds(EMSUART_TX_WAIT_BYTE - EMSUART_TX_LAG + EMSUART_TX_WAIT_GAP); // 1760
} }
tx_brk(); // send <BRK> tx_brk(); // send <BRK>
interrupts();
return EMS_TX_STATUS_OK; return EMS_TX_STATUS_OK;
} }
@@ -334,8 +324,8 @@ EMSUART_STATUS ICACHE_FLASH_ATTR EMSuart::transmit(uint8_t * buf, uint8_t len) {
// disable rx interrupt // disable rx interrupt
// clear Rx status register, resetting the Rx FIFO and flush it // clear Rx status register, resetting the Rx FIFO and flush it
ETS_UART_INTR_DISABLE(); noInterrupts();
USC0(EMSUART_UART) |= (1 << UCRXRST); //ETS_UART_INTR_DISABLE();
emsuart_flush_fifos(); emsuart_flush_fifos();
// send the bytes along the serial line // send the bytes along the serial line
@@ -347,12 +337,14 @@ EMSUART_STATUS ICACHE_FLASH_ATTR EMSuart::transmit(uint8_t * buf, uint8_t len) {
while (((USS(EMSUART_UART) >> USRXC) & 0xFF) == _usrxc) { while (((USS(EMSUART_UART) >> USRXC) & 0xFF) == _usrxc) {
delayMicroseconds(EMSUART_BUSY_WAIT); // burn CPU cycles... delayMicroseconds(EMSUART_BUSY_WAIT); // burn CPU cycles...
if (--wdc == 0) { if (--wdc == 0) {
ETS_UART_INTR_ENABLE(); interrupts();
//ETS_UART_INTR_ENABLE();
return EMS_TX_WTD_TIMEOUT; return EMS_TX_WTD_TIMEOUT;
} }
if (USIR(EMSUART_UART) & (1 << UIBD)) { if (USIR(EMSUART_UART) & (1 << UIBD)) {
USIC(EMSUART_UART) = (1 << UIBD); // clear BRK detect IRQ USIC(EMSUART_UART) = (1 << UIBD); // clear BRK detect IRQ
ETS_UART_INTR_ENABLE(); interrupts();
//ETS_UART_INTR_ENABLE();
return EMS_TX_BRK_DETECT; return EMS_TX_BRK_DETECT;
} }
} }
@@ -379,7 +371,8 @@ EMSUART_STATUS ICACHE_FLASH_ATTR EMSuart::transmit(uint8_t * buf, uint8_t len) {
} }
} }
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 result; // send the Tx status back return result; // send the Tx status back
} }