From cbec612b0f9c01e65fcd80407133e026540f2c86 Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Sat, 28 Sep 2024 21:14:41 +0200 Subject: [PATCH 1/2] Make Raicom code compile on ESP32 by moving out class Pinpair --- DCCEXParser.cpp | 2 +- DCCRMT.cpp | 4 ++-- DCCRMT.h | 6 +++--- DCCWaveform.cpp | 28 ++++++++-------------------- DCCWaveform.h | 19 ++++++++++++++----- MotorDriver.h | 18 +++--------------- Pinpair.h | 14 ++++++++++++++ TrackManager.cpp | 2 +- 8 files changed, 46 insertions(+), 47 deletions(-) create mode 100644 Pinpair.h diff --git a/DCCEXParser.cpp b/DCCEXParser.cpp index a133a2e..4875eec 100644 --- a/DCCEXParser.cpp +++ b/DCCEXParser.cpp @@ -1121,7 +1121,7 @@ bool DCCEXParser::parseC(Print *stream, int16_t params, int16_t p[]) { DCC::setGlobalSpeedsteps(128); DIAG(F("128 Speedsteps")); return true; -#if defined(HAS_ENOUGH_MEMORY) && !defined(ARDUINO_ARCH_UNO) +#if defined(HAS_ENOUGH_MEMORY) && !defined(ARDUINO_ARCH_UNO) && !defined(ARDUINO_ARCH_ESP32) case "RAILCOM"_hk: { // if (params<2) return false; diff --git a/DCCRMT.cpp b/DCCRMT.cpp index afada7b..48d5369 100644 --- a/DCCRMT.cpp +++ b/DCCRMT.cpp @@ -84,7 +84,7 @@ void IRAM_ATTR interrupt(rmt_channel_t channel, void *t) { DCCTimer::updateMinimumFreeMemoryISR(0); } -RMTChannel::RMTChannel(pinpair pins, bool isMain) { +RMTChannel::RMTChannel(Pinpair pins, bool isMain) { byte ch; byte plen; @@ -253,7 +253,7 @@ bool RMTChannel::addPin(byte pin, bool inverted) { if (err != ESP_OK) return false; return true; } -bool RMTChannel::addPin(pinpair pins) { +bool RMTChannel::addPin(Pinpair pins) { return addPin(pins.pin) && addPin(pins.invpin, true); } #endif //ESP32 diff --git a/DCCRMT.h b/DCCRMT.h index 33257a0..4a2a068 100644 --- a/DCCRMT.h +++ b/DCCRMT.h @@ -23,7 +23,7 @@ #include "driver/rmt.h" #include "soc/rmt_reg.h" #include "soc/rmt_struct.h" -#include "MotorDriver.h" // for class pinpair +#include "Pinpair.h" // for class Pinpair // make calculations easy and set up for microseconds #define RMT_CLOCK_DIVIDER 80 @@ -32,9 +32,9 @@ class RMTChannel { public: - RMTChannel(pinpair pins, bool isMain); + RMTChannel(Pinpair pins, bool isMain); bool addPin(byte pin, bool inverted=0); - bool addPin(pinpair pins); + bool addPin(Pinpair pins); void IRAM_ATTR RMTinterrupt(); void RMTprefill(); //int RMTfillData(dccPacket packet); diff --git a/DCCWaveform.cpp b/DCCWaveform.cpp index 9c43730..1217769 100644 --- a/DCCWaveform.cpp +++ b/DCCWaveform.cpp @@ -126,22 +126,6 @@ volatile bool DCCWaveform::railcomActive=false; // switched on by user volatile bool DCCWaveform::railcomDebug=false; // switched on by user volatile bool DCCWaveform::railcomSampleWindow=false; // true during packet transmit -bool DCCWaveform::isRailcom() { - return railcomActive; -} - -bool DCCWaveform::isRailcomSampleWindow() { - return railcomSampleWindow; -} -bool DCCWaveform::isRailcomPossible() { - return railcomPossible; -} - -void DCCWaveform::setRailcomPossible(bool yes) { - railcomPossible=yes; - if (!yes) setRailcom(false,false); -} - bool DCCWaveform::setRailcom(bool on, bool debug) { if (on && railcomPossible) { // TODO check possible @@ -266,11 +250,13 @@ void DCCWaveform::promotePendingPacket() { transmitRepeats = 0; if (getResets() < 250) sentResetsSincePacket++; // only place to increment (private!) } -#endif +#endif //not ARDUINO_ARCH_ESP32 #ifdef ARDUINO_ARCH_ESP32 #include "DCCWaveform.h" +#include "TrackManager.h" #include "DCCACK.h" +#include "Pinpair.h" DCCWaveform DCCWaveform::mainTrack(PREAMBLE_BITS_MAIN, true); DCCWaveform DCCWaveform::progTrack(PREAMBLE_BITS_PROG, false); @@ -283,7 +269,7 @@ DCCWaveform::DCCWaveform(byte preambleBits, bool isMain) { } void DCCWaveform::begin() { for(const auto& md: TrackManager::getMainDrivers()) { - pinpair p = md->getSignalPin(); + Pinpair p = md->getSignalPin(); if(rmtMainChannel) { //DIAG(F("added pins %d %d to MAIN channel"), p.pin, p.invpin); rmtMainChannel->addPin(p); // add pin to existing main channel @@ -294,7 +280,7 @@ void DCCWaveform::begin() { } MotorDriver *md = TrackManager::getProgDriver(); if (md) { - pinpair p = md->getSignalPin(); + Pinpair p = md->getSignalPin(); if (rmtProgChannel) { //DIAG(F("added pins %d %d to PROG channel"), p.pin, p.invpin); rmtProgChannel->addPin(p); // add pin to existing prog channel @@ -352,7 +338,9 @@ void IRAM_ATTR DCCWaveform::loop() { } bool DCCWaveform::setRailcom(bool on, bool debug) { - // TODO... ESP32 railcom waveform + // todo + (void)on; + (void)debug; return false; } diff --git a/DCCWaveform.h b/DCCWaveform.h index 95de554..6bdbe15 100644 --- a/DCCWaveform.h +++ b/DCCWaveform.h @@ -25,7 +25,7 @@ #define DCCWaveform_h #ifdef ARDUINO_ARCH_ESP32 #include "DCCRMT.h" -#include "TrackManager.h" +//#include "TrackManager.h" #endif @@ -84,10 +84,19 @@ class DCCWaveform { bool isReminderWindowOpen(); void promotePendingPacket(); static bool setRailcom(bool on, bool debug); - static bool isRailcom(); - static bool isRailcomSampleWindow(); - static bool isRailcomPossible(); - static void setRailcomPossible(bool yes); + inline static bool isRailcom() { + return railcomActive; + }; + inline static bool isRailcomSampleWindow() { + return railcomSampleWindow; + }; + inline static bool isRailcomPossible() { + return railcomPossible; + }; + inline static void setRailcomPossible(bool yes) { + railcomPossible=yes; + if (!yes) setRailcom(false,false); + }; private: #ifndef ARDUINO_ARCH_ESP32 volatile bool packetPending; diff --git a/MotorDriver.h b/MotorDriver.h index 3438c05..6ccc4f6 100644 --- a/MotorDriver.h +++ b/MotorDriver.h @@ -124,21 +124,9 @@ enum TRACK_MODE : byte {TRACK_MODE_NONE = 1, TRACK_MODE_MAIN = 2, TRACK_MODE_PRO // Virtualised Motor shield 1-track hardware Interface -#ifndef UNUSED_PIN // sync define with the one in MotorDrivers.h -#define UNUSED_PIN 255 // inside uint8_t -#endif +#include "Pinpair.h" // for class Pinpair #define MAX_PIN 254 -class pinpair { -public: - pinpair(byte p1, byte p2) { - pin = p1; - invpin = p2; - }; - byte pin = UNUSED_PIN; - byte invpin = UNUSED_PIN; -}; - #if defined(__IMXRT1062__) || defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_STM32) typedef uint32_t portreg_t; #else @@ -209,7 +197,7 @@ class MotorDriver { pinMode(signalPin2, INPUT); } }; - inline pinpair getSignalPin() { return pinpair(signalPin,signalPin2); }; + inline Pinpair getSignalPin() { return Pinpair(signalPin,signalPin2); }; inline int8_t getBrakePinSigned() { return invertBrake ? -brakePin : brakePin; }; void setDCSignal(byte speedByte, uint8_t frequency=0); void throttleInrush(bool on); @@ -285,7 +273,7 @@ class MotorDriver { else invertPhase = 0; #if defined(ARDUINO_ARCH_ESP32) - pinpair p = getSignalPin(); + Pinpair p = getSignalPin(); uint32_t *outreg = (uint32_t *)(GPIO_FUNC0_OUT_SEL_CFG_REG + 4*p.pin); if (invertPhase) // set or clear the invert bit in the gpio out register *outreg |= ((uint32_t)0x1 << GPIO_FUNC0_OUT_INV_SEL_S); diff --git a/Pinpair.h b/Pinpair.h new file mode 100644 index 0000000..4452c51 --- /dev/null +++ b/Pinpair.h @@ -0,0 +1,14 @@ +#pragma once +#ifndef UNUSED_PIN // sync define with the one in MotorDrivers.h +#define UNUSED_PIN 255 // inside uint8_t +#endif +class Pinpair { +public: + Pinpair(byte p1, byte p2) { + pin = p1; + invpin = p2; + }; + byte pin = UNUSED_PIN; + byte invpin = UNUSED_PIN; +}; + diff --git a/TrackManager.cpp b/TrackManager.cpp index e1bd6d4..375f596 100644 --- a/TrackManager.cpp +++ b/TrackManager.cpp @@ -218,7 +218,7 @@ bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr #ifdef ARDUINO_ARCH_ESP32 // remove pin from MUX matrix and turn it off - pinpair p = track[trackToSet]->getSignalPin(); + Pinpair p = track[trackToSet]->getSignalPin(); //DIAG(F("Track=%c remove pin %d"),trackToSet+'A', p.pin); gpio_reset_pin((gpio_num_t)p.pin); if (p.invpin != UNUSED_PIN) { From 212708d88f15ed027c08510392d9768f0d6e45cf Mon Sep 17 00:00:00 2001 From: Harald Barth Date: Sat, 28 Sep 2024 21:35:28 +0200 Subject: [PATCH 2/2] Caculare from preamble start instead --- DCCWaveform.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/DCCWaveform.cpp b/DCCWaveform.cpp index 1217769..d61de9d 100644 --- a/DCCWaveform.cpp +++ b/DCCWaveform.cpp @@ -163,9 +163,12 @@ void DCCWaveform::interrupt2() { if (remainingPreambles==1) promotePendingPacket(); else if (isMainTrack && railcomActive) { // cutout has ended so its now possible to poll the railcom detectors - if (remainingPreambles==5) railcomSampleWindow=true; + // requiredPreambles is one higher that preamble length so + // if preamble length is 16 then this evaluates to 5 + if (remainingPreambles==(requiredPreambles-12)) railcomSampleWindow=true; // cutout can be ended when read - else if (remainingPreambles==14) DCCTimer::ackRailcomTimer(); + // see above for requiredPreambles + else if (remainingPreambles==(requiredPreambles-3)) DCCTimer::ackRailcomTimer(); } // 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.