Skip to content
This repository has been archived by the owner on Oct 4, 2021. It is now read-only.

add smart-tx function #112

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.8.0dev] 2019-

### Added

- MM50 Mixer

### Fixed

- runtime exceptions with latest 2.5.1 arduino core

### Changed

## [1.7.0] 2019-05-11

### Added
Expand Down
Binary file modified doc/schematics/Schematic_EMS-ESP-supercap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 7 additions & 6 deletions lib/MyESP/MyESP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ void MyESP::_ota_setup() {
return;
}

//ArduinoOTA.setPort(OTA_PORT);
ArduinoOTA.setPort(OTA_PORT);
ArduinoOTA.setHostname(_app_hostname);

ArduinoOTA.onStart([this]() { _OTACallback(); });
Expand Down Expand Up @@ -906,14 +906,14 @@ void MyESP::showSystemStats() {
char buffer[16] = {0};
/* Crash info */
myDebug_P(PSTR(" [EEPROM] EEPROM size: %u"), EEPROMr.reserved() * SPI_FLASH_SEC_SIZE);
strlcpy(output_str, PSTR(" [EEPROM] EEPROM Sector pool size is "), sizeof(output_str));
strlcpy(output_str, " [EEPROM] EEPROM Sector pool size is ", sizeof(output_str));
strlcat(output_str, itoa(EEPROMr.size(), buffer, 10), sizeof(output_str));
strlcat(output_str, PSTR(", and in use are: "), sizeof(output_str));
strlcat(output_str, ", and in use are: ", sizeof(output_str));
for (uint32_t i = 0; i < EEPROMr.size(); i++) {
strlcat(output_str, itoa(EEPROMr.base() - i, buffer, 10), sizeof(output_str));
strlcat(output_str, PSTR(" "), sizeof(output_str));
strlcat(output_str, " ", sizeof(output_str));
}
myDebug_P(output_str);
myDebug(output_str);
#endif

#ifdef ARDUINO_BOARD
Expand Down Expand Up @@ -946,7 +946,6 @@ void MyESP::showSystemStats() {
myDebug_P(PSTR(" [MEM] Max OTA size: %d"), (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000);
myDebug_P(PSTR(" [MEM] OTA Reserved: %d"), 4 * SPI_FLASH_SEC_SIZE);
myDebug_P(PSTR(" [MEM] Free Heap: %d"), ESP.getFreeHeap());

myDebug_P(PSTR(""));
}

Expand Down Expand Up @@ -1554,10 +1553,12 @@ void MyESP::loop() {

jw.loop(); // WiFi

/*
// do nothing else until we've got a wifi connection
if (WiFi.getMode() & WIFI_AP) {
return;
}
*/

ArduinoOTA.handle(); // OTA
_mqttConnect(); // MQTT
Expand Down
6 changes: 3 additions & 3 deletions lib/MyESP/MyESP.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#ifndef MyEMS_h
#define MyEMS_h

#define MYESP_VERSION "1.1.9"
#define MYESP_VERSION "1.1.11"

#include <ArduinoJson.h>
#include <ArduinoOTA.h>
Expand All @@ -30,10 +30,10 @@ void custom_crash_callback(struct rst_info *, uint32_t, uint32_t);
//#include <ESPmDNS.h>
#include <SPIFFS.h> // added for ESP32
#define ets_vsnprintf vsnprintf // added for ESP32
#define OTA_PORT 8266
#define OTA_PORT 3232
#else
#include <ESPAsyncTCP.h>
#define OTA_PORT 3232
#define OTA_PORT 8266
#endif

#define MYEMS_CONFIG_FILE "/config.json"
Expand Down
2 changes: 1 addition & 1 deletion src/ems-esp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ typedef struct {
bool doingColdShot; // true if we've just sent a jolt of cold water
} _EMSESP_Shower;

command_t PROGMEM project_cmds[] = {
command_t project_cmds[] = {

{true, "led <on | off>", "toggle status LED on/off"},
{true, "led_gpio <gpio>", "set the LED pin. Default is the onboard LED (D1=5)"},
Expand Down
8 changes: 4 additions & 4 deletions src/ems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,22 +697,22 @@ void _ems_readTelegram(uint8_t * telegram, uint8_t length) {
} else {
// nothing to send so just send a poll acknowledgement back
if (EMS_Sys_Status.emsPollEnabled) {
emsaurt_tx_poll();
emsuart_tx_poll();
}
}
} else if (EMS_Sys_Status.emsTxStatus == EMS_TX_STATUS_WAIT) {
// this may be a single byte 01 (success) or 04 (error) from a recent write command?
if (value == EMS_TX_SUCCESS) {
EMS_Sys_Status.emsTxPkgs++;
// got a success 01. Send a validate to check the value of the last write
emsaurt_tx_poll(); // send a poll to free the EMS bus
emsuart_tx_poll(); // send a poll to free the EMS bus
_createValidate(); // create a validate Tx request (if needed)
} else if (value == EMS_TX_ERROR) {
// last write failed (04), delete it from queue and dont bother to retry
if (EMS_Sys_Status.emsLogging == EMS_SYS_LOGGING_VERBOSE) {
myDebug_P(PSTR("** Write command failed from host"));
}
emsaurt_tx_poll(); // send a poll to free the EMS bus
emsuart_tx_poll(); // send a poll to free the EMS bus
_removeTxQueue(); // remove from queue
}
}
Expand Down Expand Up @@ -1052,7 +1052,7 @@ void _processType(_EMS_RxTelegram * EMS_RxTelegram) {
}
}

emsaurt_tx_poll(); // send Acknowledgement back to free the EMS bus since we have the telegram
emsuart_tx_poll(); // send Acknowledgement back to free the EMS bus since we have the telegram
}


Expand Down
1 change: 1 addition & 0 deletions src/ems_devices.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ const _Other_Type Other_Types[] = {
{EMS_MODEL_OTHER, 250, 0x11, "WM10 Switch Module"}, // warning, fake product id!
{EMS_MODEL_OTHER, 160, 0x20, "MM100 Mixing Module"},
{EMS_MODEL_OTHER, 160, 0x21, "MM100 Mixing Module"},
{EMS_MODEL_OTHER, 159, 0x21, "MM50 Mixing Module"},
{EMS_MODEL_OTHER, 68, 0x09, "BC10/RFM20 Receiver"},
{EMS_MODEL_OTHER, 190, 0x09, "BC10 Base Controller"},
{EMS_MODEL_OTHER, 114, 0x09, "BC10 Base Controller"},
Expand Down
101 changes: 82 additions & 19 deletions src/emsuart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ static void emsuart_rx_intr_handler(void * para) {
}

// clear Rx FIFO full and Rx FIFO timeout interrupts
U0IC = (1 << UIFF);
U0IC = (1 << UITO);
U0IC = (1 << UIFF) | (1 << UITO);
}

// BREAK detection = End of EMS data block
Expand Down Expand Up @@ -76,6 +75,13 @@ static void ICACHE_FLASH_ATTR emsuart_recvTask(os_event_t * events) {
pEMSRxBuf = paEMSRxBuf[++emsRxBufIdx % EMS_MAXBUFFERS]; // next free EMS Receive buffer
}

static inline void ICACHE_FLASH_ATTR emsuart_flush_fifos() {
// flush everything left over in buffer, this clears both rx and tx FIFOs
uint32_t tmp = ((1 << UCRXRST) | (1 << UCTXRST)); // bit mask
USC0(EMSUART_UART) |= (tmp); // set bits
USC0(EMSUART_UART) &= ~(tmp); // clear bits
}

/*
* init UART0 driver
*/
Expand All @@ -100,11 +106,8 @@ void ICACHE_FLASH_ATTR emsuart_init() {
USD(EMSUART_UART) = (UART_CLK_FREQ / EMSUART_BAUD);
USC0(EMSUART_UART) = EMSUART_CONFIG; // 8N1

// flush everything left over in buffer, this clears both rx and tx FIFOs
uint32_t tmp = ((1 << UCRXRST) | (1 << UCTXRST)); // bit mask
USC0(EMSUART_UART) |= (tmp); // set bits
USC0(EMSUART_UART) &= ~(tmp); // clear bits

emsuart_flush_fifos();

// conf1 params
// UCTOE = RX TimeOut enable (default is 1)
// UCTOT = RX TimeOut Threshold (7bit) = want this when no more data after 2 characters. (default is 2)
Expand All @@ -128,7 +131,9 @@ void ICACHE_FLASH_ATTR emsuart_init() {
system_set_os_print(0); // https://github.com/espruino/Espruino/issues/655

// swap Rx and Tx pins to use GPIO13 (D7) and GPIO15 (D8) respectively
#ifndef NO_UART_SWAP
system_uart_swap();
#endif

ETS_UART_INTR_ATTACH(emsuart_rx_intr_handler, NULL);
ETS_UART_INTR_ENABLE();
Expand Down Expand Up @@ -172,26 +177,84 @@ void ICACHE_FLASH_ATTR emsuart_tx_brk() {
USC0(EMSUART_UART) &= ~(1 << UCBRK); // clear bit
}

/*
* set loopback mode and clear Tx/Rx FIFO
*/
static inline void ICACHE_FLASH_ATTR emsuart_loopback(boolean enable) {
uint32_t tmp = (1 << UCLBE); // Loopback mask
if (enable)
USC0(EMSUART_UART) |= (tmp); // enable loopback
else
USC0(EMSUART_UART) &= ~(tmp); // disable loopback
}

/*
* Send to Tx, ending with a <BRK>
*/
void ICACHE_FLASH_ATTR emsuart_tx_buffer(uint8_t * buf, uint8_t len) {
for (uint8_t i = 0; i < len; i++) {
USF(EMSUART_UART) = buf[i];

// check if we need to force a delay to slow down Tx
// https://github.com/proddy/EMS-ESP/issues/23#
if (EMS_Sys_Status.emsTxDelay) {
delayMicroseconds(EMS_TX_BRK_WAIT);
uint32_t tmp;

// backward compatibility
if (EMS_Sys_Status.emsTxDelay<2) {
for (uint8_t i = 0; i < len; i++) {
USF(EMSUART_UART) = buf[i];

// check if we need to force a delay to slow down Tx
// https://github.com/proddy/EMS-ESP/issues/23#
if (EMS_Sys_Status.emsTxDelay==1) {
delayMicroseconds(EMS_TX_BRK_WAIT);
}
}
emsuart_tx_brk(); // send <BRK>
} else {
// smart Tx
#define UART_BIT_TIME 104 // bit time @9600 baud

ETS_UART_INTR_DISABLE(); // disable rx interrupt
emsuart_flush_fifos();
emsuart_loopback(true); // reset FIFOs & enable loopback

for (uint8_t i = 0; i < len; i++) {
USF(EMSUART_UART) = buf[i]; // send byte
delayMicroseconds(10*UART_BIT_TIME);

/* wait until
* ° loopback char is received
* ° Rx-FIFO full (unlikely)
* ° Rx-TimeOut (unlikely)
* ° Break detected (bus collision) - not handled now...
*/
for (uint8_t l = 0; l < 13; l++) {
if (((USS(EMSUART_UART) >> USRXC) & 0xFF)
|| (U0IS & ((1 << UIFF) | (1 << UITO) | (1 << UIBD))))
break;
delayMicroseconds(UART_BIT_TIME / 8); // ~13µs
}

uint32_t break_detect = (U0IS & (1 << UIBD)); // keep break detect interrupt
(void) (USF(EMSUART_UART)); // read out fifo, also clears FIFO counter
U0IC = (1 << UIFF) | (1 << UITO) | (1 << UIBD); // clear pending interrupts
if (break_detect)
break; // collision / abort from master
}

// send <BRK> - wait until <BRK> detect
USC0(EMSUART_UART) |= (1 << UCBRK); // send <BRK>
while (!(U0IS & (1 << UIBD)))
delayMicroseconds(UART_BIT_TIME / 8); // ~13µs
USC0(EMSUART_UART) &= ~(1 << UCBRK); // clear <BRK>

U0IC = (1 << UIFF) | (1 << UITO) | (1 << UIBD); // clear pending interrupts
emsuart_flush_fifos();
emsuart_loopback(false); // disable loopback mode
ETS_UART_INTR_ENABLE(); // enable rx interrupt
}
emsuart_tx_brk(); // send <BRK>
}

/*
* Send the Poll (our own ID) to Tx as a single byte and end with a <BRK>
*/
void ICACHE_FLASH_ATTR emsaurt_tx_poll() {
USF(EMSUART_UART) = EMS_ID_ME;
emsuart_tx_brk(); // send <BRK>
}
void ICACHE_FLASH_ATTR emsuart_tx_poll() {
static uint8_t buf[] = {EMS_ID_ME};
emsuart_tx_buffer(buf,1);
}
2 changes: 1 addition & 1 deletion src/emsuart.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ void ICACHE_FLASH_ATTR emsuart_init();
void ICACHE_FLASH_ATTR emsuart_stop();
void ICACHE_FLASH_ATTR emsuart_start();
void ICACHE_FLASH_ATTR emsuart_tx_buffer(uint8_t * buf, uint8_t len);
void ICACHE_FLASH_ATTR emsaurt_tx_poll();
void ICACHE_FLASH_ATTR emsuart_tx_poll();
void ICACHE_FLASH_ATTR emsuart_tx_brk();
2 changes: 1 addition & 1 deletion src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
#pragma once

#define APP_NAME "EMS-ESP"
#define APP_VERSION "1.7.0b16"
#define APP_VERSION "1.8.0b1"
#define APP_HOSTNAME "ems-esp"