1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-12-23 21:01:25 +01:00

Cleanup ESP specific details

This commit is contained in:
Harald Barth 2021-10-01 09:09:30 +02:00
parent 426b27f0dd
commit cf0c818138
11 changed files with 82 additions and 54 deletions

View File

@ -59,7 +59,9 @@ void setup()
// Responsibility 1: Start the usb connection for diagnostics
// This is normally Serial but uses SerialUSB on a SAMD processor
Serial.begin(115200);
#ifdef ESP_DEBUG
Serial.setDebugOutput(true);
#endif
DIAG(F("License GPLv3 fsf.org (c) dcc-ex.com"));
@ -73,9 +75,12 @@ void setup()
// Start the WiFi interface on a MEGA, Uno cannot currently handle WiFi
// Start Ethernet if it exists
#if WIFI_ON
#ifndef ESP_FAMILY
WifiInterface::setup(WIFI_SERIAL_LINK_SPEED, F(WIFI_SSID), F(WIFI_PASSWORD), F(WIFI_HOSTNAME), IP_PORT, WIFI_CHANNEL);
#else
WifiESP::setup(WIFI_SSID, WIFI_PASSWORD, WIFI_HOSTNAME, IP_PORT, WIFI_CHANNEL);
#endif
#endif // WIFI_ON
WifiESP::setup(WIFI_SSID, WIFI_PASSWORD, WIFI_HOSTNAME, 2560, 1);
#if ETHERNET_ON
EthernetInterface::setup();
#endif // ETHERNET_ON
@ -117,9 +122,12 @@ void loop()
// Responsibility 3: Optionally handle any incoming WiFi traffic
#if WIFI_ON
#ifndef ESP_FAMILY
WifiInterface::loop();
#endif
#else
WifiESP::loop();
#endif
#endif //WIFI_ON
#if ETHERNET_ON
EthernetInterface::loop();
#endif
@ -136,12 +144,13 @@ void loop()
// Report any decrease in memory (will automatically trigger on first call)
static int ramLowWatermark = __INT_MAX__; // replaced on first loop
/*
#ifdef ESP_FAMILY
updateMinimumFreeMemory(128);
#endif
int freeNow = minimumFreeMemory();
if (freeNow < ramLowWatermark)
{
ramLowWatermark = freeNow;
LCD(2,F("Free RAM=%5db"), ramLowWatermark);
}
*/
}

View File

@ -31,7 +31,9 @@
#include "DCCEXParser.h"
#include "version.h"
#include "WifiInterface.h"
#ifdef ESP_FAMILY
#include "WifiESP.h"
#endif
#if ETHERNET_ON == true
#include "EthernetInterface.h"
#endif

View File

@ -17,6 +17,7 @@
* You should have received a copy of the GNU General Public License
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
*/
#include "defines.h"
#include "StringFormatter.h"
#include "DCCEXParser.h"
#include "DCC.h"
@ -30,7 +31,9 @@
#include "EEStore.h"
#include "DIAG.h"
//#include <avr/wdt.h>
#ifndef ESP_FAMILY
#include <avr/wdt.h>
#endif
// These keywords are used in the <1> command. The number is what you get if you use the keyword as a parameter.
// To discover new keyword numbers , use the <$ YOURKEYWORD> command

View File

@ -147,27 +147,26 @@ void DCCTimer::read(uint8_t word, uint8_t *mac, uint8_t offset) {
#endif
#elif defined(ARDUINO_ARCH_ESP8266)
// ESP8266 !!!!!!!!!!!!!!!!!!!!!
void DCCTimer::begin(INTERRUPT_CALLBACK callback) {
interruptHandler=callback;
timer1_disable();
// ETS_FRC_TIMER1_INTR_ATTACH(NULL, NULL);
// ETS_FRC_TIMER1_NMI_INTR_ATTACH(interruptHandler);
// There seem to be differnt ways to attach interrupt handler
// ETS_FRC_TIMER1_INTR_ATTACH(NULL, NULL);
// ETS_FRC_TIMER1_NMI_INTR_ATTACH(interruptHandler);
// Let us choose the one from the API
timer1_attachInterrupt(interruptHandler);
// not exactly sure of order:
timer1_enable(TIM_DIV1, TIM_EDGE, TIM_LOOP);
timer1_write(CLOCK_CYCLES);
/*
noInterrupts();
timer1_attachInterrupt(interruptHandler);
timer1_write(CLOCK_CYCLES);
timer1_enable(TIM_DIV1, TIM_EDGE, TIM_LOOP);
interrupts();
*/
}
IRAM_ATTR bool DCCTimer::isPWMPin(byte pin) {
// We do not support to use PWM to make the Waveform on ESP
bool IRAM_ATTR DCCTimer::isPWMPin(byte pin) {
return false;
}
void ICACHE_RAM_ATTR DCCTimer::setPWM(byte pin, bool high) {
void IRAM_ATTR DCCTimer::setPWM(byte pin, bool high) {
}

View File

@ -20,6 +20,7 @@
#pragma GCC optimize ("-O3")
#include <Arduino.h>
#include "defines.h"
#include "DCCWaveform.h"
#include "DCCTimer.h"
#include "DIAG.h"
@ -53,7 +54,6 @@ void DCCWaveform::begin(MotorDriver * mainDriver, MotorDriver * progDriver) {
DCCTimer::begin(DCCWaveform::interruptHandler);
}
#define SLOW_ANALOG_READ
#ifdef SLOW_ANALOG_READ
// Flag to hold if we need to run ack checking in loop
static bool ackflag = 0;
@ -213,7 +213,7 @@ const bool DCCWaveform::signalTransform[]={
/* WAVE_LOW_0 -> */ LOW,
/* WAVE_PENDING (should not happen) -> */ LOW};
void ICACHE_RAM_ATTR DCCWaveform::interrupt2() {
void IRAM_ATTR DCCWaveform::interrupt2() {
// calculate the next bit to be sent:
// set state WAVE_MID_1 for a 1=bit
// or WAVE_HIGH_0 for a 0 bit.
@ -223,7 +223,9 @@ void ICACHE_RAM_ATTR DCCWaveform::interrupt2() {
remainingPreambles--;
// Update free memory diagnostic as we don't have anything else to do this time.
// Allow for checkAck and its called functions using 22 bytes more.
// might break ESP8266 updateMinimumFreeMemory(22);
#ifndef ESP_FAMILY
updateMinimumFreeMemory(22);
#endif
return;
}
@ -322,7 +324,7 @@ byte DCCWaveform::getAck() {
return(0); // pending set off but not detected means no ACK.
}
void ICACHE_RAM_ATTR DCCWaveform::checkAck() {
void IRAM_ATTR DCCWaveform::checkAck() {
// This function operates in interrupt() time so must be fast and can't DIAG
if (sentResetsSincePacket > 6) { //ACK timeout
ackCheckDuration=millis()-ackCheckStart;

View File

@ -104,8 +104,8 @@ void MotorDriver::setBrake(bool on) {
if (on ^ invertBrake) setHIGH(fastBrakePin);
else setLOW(fastBrakePin);
}
/*
IRAM_ATTR void MotorDriver::setSignal( bool high) {
void IRAM_ATTR MotorDriver::setSignal( bool high) {
if (usePWM) {
DCCTimer::setPWM(signalPin,high);
}
@ -120,7 +120,7 @@ IRAM_ATTR void MotorDriver::setSignal( bool high) {
}
}
}
*/
#if defined(ARDUINO_TEENSY32) || defined(ARDUINO_TEENSY35)|| defined(ARDUINO_TEENSY36)
volatile unsigned int overflow_count=0;
#endif
@ -179,5 +179,5 @@ void MotorDriver::getFastPin(const FSH* type,int pin, bool input, FASTPIN & res
result.inout = portOutputRegister(port);
result.maskHIGH = digitalPinToBitMask(pin);
result.maskLOW = ~result.maskHIGH;
DIAG(F(" port=0x%x, inoutpin=0x%x, isinput=%d, mask=0x%x"),port, result.inout,input,result.maskHIGH);
// DIAG(F(" port=0x%x, inoutpin=0x%x, isinput=%d, mask=0x%x"),port, result.inout,input,result.maskHIGH);
}

View File

@ -52,17 +52,20 @@ class MotorDriver {
MotorDriver(byte power_pin, byte signal_pin, byte signal_pin2, int8_t brake_pin,
byte current_pin, float senseFactor, unsigned int tripMilliamps, byte faultPin);
virtual void setPower( bool on);
virtual void setSignal( bool high) {
if (high) {
void setSignal( bool high);/* {
if (usePWM) {
DCCTimer::setPWM(signalPin,high);
}
if (high) {
setHIGH(fastSignalPin);
if (dualSignal) setLOW(fastSignalPin2);
}
else {
}
else {
setLOW(fastSignalPin);
if (dualSignal) setHIGH(fastSignalPin2);
}
};
}
};*/
virtual void setBrake( bool on);
virtual int getCurrentRaw();
virtual unsigned int raw2mA( int raw);

View File

@ -17,6 +17,8 @@
along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
*/
#include "defines.h"
#ifdef ESP_FAMILY
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <vector>
@ -92,7 +94,7 @@ static void deleteClient(AsyncClient* client) {
}
}
static void handleDisconnect(void* arg, AsyncClient* client) {
DIAG(F("client %s disconnected"), client->remoteIP().toString().c_str());
DIAG(F("Client disconnected"));
deleteClient(client);
}
@ -103,11 +105,11 @@ static void handleTimeOut(void* arg, AsyncClient* client, uint32_t time) {
static void handleNewClient(void* arg, AsyncClient* client) {
DIAG(F("New client has been connected to server, ip: %s"), client->remoteIP().toString().c_str());
DIAG(F("New client %s"), client->remoteIP().toString().c_str());
// add to list
clients.push_back(client);
// register events
client->onData(&handleData, NULL);
client->onError(&handleError, NULL);
@ -118,7 +120,7 @@ static void handleNewClient(void* arg, AsyncClient* client) {
/* Things one _might_ want to do:
Disable soft watchdog: ESP.wdtDisable()
Enable soft watchdog: ESP.wdtEnable(X) ignores the value of X and enables it for fixed
Enable soft watchdog: ESP.wdtEnable(X) ignores the value of X and enables it for fixed
time at least in version 3.0.2 of the esp8266 package.
Internet says:
@ -140,30 +142,29 @@ bool WifiESP::setup(const char *wifiESSID,
const char *hostname,
int port,
const byte channel) {
DIAG(F("START"));
// connects to access point
// We are server and should not sleep
wifi_set_sleep_type(NONE_SLEEP_T);
// connects to access point
WiFi.mode(WIFI_STA);
WiFi.setAutoReconnect(true);
DIAG(F("BEGIN"));
WiFi.begin(wifiESSID, wifiPassword);
DIAG(F("STATUS"));
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(500);
}
DIAG(F("SERVER"));
if (WiFi.status() == WL_CONNECTED)
DIAG(F("Wifi IP %s"),WiFi.localIP().toString().c_str());
else {
DIAG(F("Wifi fail"));
// no idea to go on
return false;
}
server = new AsyncServer(port); // start listening on tcp port
DIAG(F("CLIENT"));
server->onClient(&handleNewClient, server);
DIAG(F("SBEGIN"));
server->begin();
DIAG(F("ENDSETUP"));
DIAG(F("Server up port %d"),port);
return true;
}
@ -213,11 +214,13 @@ void WifiESP::loop() {
}
}
}
#ifdef ESP_DEBUG
static unsigned long last = 0;
if (millis() - last > 60000) {
last = millis();
DIAG(F("+"));
}
#endif
ESP.wdtFeed();
}
#endif //ESP_FAMILY

View File

@ -1,6 +1,6 @@
/*
* © 2021, Harald Barth.
*
*
* This file is part of CommandStation-EX
*
* This is free software: you can redistribute it and/or modify

View File

@ -1,5 +1,5 @@
/*
© 2020, Harald Barth.
© 2020,2021 Harald Barth.
This file is part of CommandStation-EX
@ -18,12 +18,20 @@
*/
////////////////////////////////////////////////////////////////////////////////
//
#if defined (ARDUINO_ARCH_ESP8266)
#define ESP_FAMILY
//#define ESP_DEBUG
#define SLOW_ANALOG_READ
#endif
////////////////////////////////////////////////////////////////////////////////
//
// WIFI_ON: All prereqs for running with WIFI are met
// Note: WIFI_CHANNEL may not exist in early config.h files so is added here if needed.
#if ENABLE_WIFI && (defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) || defined(ARDUINO_SAMD_ZERO) || defined(TEENSYDUINO))
#if ENABLE_WIFI && (defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) || defined(ARDUINO_SAMD_ZERO) || defined(TEENSYDUINO) || defined (ESP_FAMILY))
#define WIFI_ON true
#ifndef WIFI_CHANNEL
#define WIFI_CHANNEL 1

View File

@ -28,7 +28,7 @@ extern "C" char* sbrk(int);
extern char *__brkval;
extern char *__malloc_heap_start;
#elif defined(ARDUINO_ARCH_ESP8266)
// fine as well
// supported but nothing needed here
#else
#error Unsupported board type
#endif
@ -116,9 +116,8 @@ int minimumFreeMemory() {
// So even if all of the heap is freed, the reported minimum free
// memory will not increase.
//
void ICACHE_RAM_ATTR updateMinimumFreeMemory(unsigned char extraBytes) {
void updateMinimumFreeMemory(unsigned char extraBytes) {
int spare = freeMemory()-extraBytes;
if (spare < 0) spare = 0;
if (spare < minimum_free_memory) minimum_free_memory = spare;
}