From ebe0482d953d66aa3f89734daa471afff7c77add Mon Sep 17 00:00:00 2001
From: Ash-4 <81280775+Ash-4@users.noreply.github.com>
Date: Wed, 2 Aug 2023 14:58:31 -0500
Subject: [PATCH 01/51] Add files via upload
---
IO_CMRI.cpp | 316 +++++++++++++++++++++++++++++++++++++++++++++
IO_CMRI.h | 291 +++++++++++++++++++++++++++++++++++++++++
mySetup_h_cmri.txt | 128 ++++++++++++++++++
3 files changed, 735 insertions(+)
create mode 100644 IO_CMRI.cpp
create mode 100644 IO_CMRI.h
create mode 100644 mySetup_h_cmri.txt
diff --git a/IO_CMRI.cpp b/IO_CMRI.cpp
new file mode 100644
index 0000000..1dfa2fc
--- /dev/null
+++ b/IO_CMRI.cpp
@@ -0,0 +1,316 @@
+/*
+ * © 2023, Neil McKechnie. All rights reserved.
+ *
+ * This file is part of DCC++EX API
+ *
+ * This is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * It is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with CommandStation. If not, see .
+ */
+
+#include "IO_CMRI.h"
+#include "defines.h"
+
+/************************************************************
+ * CMRIbus implementation
+ ************************************************************/
+
+// Constructor for CMRIbus
+CMRIbus::CMRIbus(uint8_t busNo, HardwareSerial &serial, unsigned long baud, uint16_t cycleTimeMS, VPIN transmitEnablePin) {
+ _busNo = busNo;
+ _serial = &serial;
+ _baud = baud;
+ _cycleTime = cycleTimeMS * 1000UL; // convert from milliseconds to microseconds.
+ _transmitEnablePin = transmitEnablePin;
+ if (_transmitEnablePin != VPIN_NONE) {
+ pinMode(_transmitEnablePin, OUTPUT);
+ ArduinoPins::fastWriteDigital(_transmitEnablePin, 0); // transmitter initially off
+ }
+
+ // Max message length is 256+6=262 bytes.
+ // Each byte is one start bit, 8 data bits and 1 or 2 stop bits, assume 11 bits per byte.
+ // Calculate timeout based on treble this time.
+ _timeoutPeriod = 3 * 11 * 262 * 1000UL / (_baud / 1000UL);
+#if defined(ARDUINOCMRI_COMPATIBLE)
+ // NOTE: The ArduinoCMRI library, unless modified, contains a 'delay(50)' between
+ // receiving the end of the prompt message and starting to send the response. This
+ // is allowed for below.
+ _timeoutPeriod += 50000UL;
+#endif
+
+ // Calculate the time in microseconds to transmit one byte (11 bits max).
+ _byteTransmitTime = 1000000UL * 11 / _baud;
+ // Postdelay is only required if we need to allow for data still being sent when
+ // we want to switch off the transmitter. The flush() method of HardwareSerial
+ // ensures that the data has completed being sent over the line.
+ _postDelay = 0;
+
+ // Add device to HAL device chain
+ IODevice::addDevice(this);
+
+ // Add bus to CMRIbus chain.
+ _nextBus = _busList;
+ _busList = this;
+}
+
+
+// Main loop function for CMRIbus.
+// Work through list of nodes. For each node, in separate loop entries
+// send initialisation message (once only); then send
+// output message; then send prompt for input data, and
+// process any response data received.
+// When the slot time has finished, move on to the next device.
+void CMRIbus::_loop(unsigned long currentMicros) {
+
+ _currentMicros = currentMicros;
+
+ while (_serial->available())
+ processIncoming();
+
+ // Send any data that needs sending.
+ processOutgoing();
+
+}
+
+// Send output data to the bus for nominated CMRInode
+uint16_t CMRIbus::sendData(CMRInode *node) {
+ uint16_t numDataBytes = (node->getNumOutputs()+7)/8;
+ _serial->write(SYN);
+ _serial->write(SYN);
+ _serial->write(STX);
+ _serial->write(node->getAddress() + 65);
+ _serial->write('T'); // T for Transmit data message
+ uint16_t charsSent = 6; // include header and trailer
+ for (uint8_t index=0; indexgetOutputStates(index);
+ if (value == DLE || value == STX || value == ETX) {
+ _serial->write(DLE);
+ charsSent++;
+ }
+ _serial->write(value);
+ charsSent++;
+ }
+ _serial->write(ETX);
+ return charsSent; // number of characters sent
+}
+
+// Send request for input data to nominated CMRInode.
+uint16_t CMRIbus::requestData(CMRInode *node) {
+ _serial->write(SYN);
+ _serial->write(SYN);
+ _serial->write(STX);
+ _serial->write(node->getAddress() + 65);
+ _serial->write('P'); // P for Poll message
+ _serial->write(ETX);
+ return 6; // number of characters sent
+}
+
+// Send initialisation message
+uint16_t CMRIbus::sendInitialisation(CMRInode *node) {
+ _serial->write(SYN);
+ _serial->write(SYN);
+ _serial->write(STX);
+ _serial->write(node->getAddress() + 65);
+ _serial->write('I'); // I for initialise message
+ _serial->write(node->getType()); // NDP
+ _serial->write((uint8_t)0); // dH
+ _serial->write((uint8_t)0); // dL
+ _serial->write((uint8_t)0); // NS
+ _serial->write(ETX);
+ return 10; // number of characters sent
+}
+
+void CMRIbus::processOutgoing() {
+ uint16_t charsSent = 0;
+ if (_currentNode == NULL) {
+ // If we're between read/write cycles then don't do anything else.
+ if (_currentMicros - _cycleStartTime < _cycleTime) return;
+ // ... otherwise start processing the first node in the list
+ _currentNode = _nodeListStart;
+ _transmitState = TD_INIT;
+ _cycleStartTime = _currentMicros;
+ }
+ if (_currentNode == NULL) return;
+ switch (_transmitState) {
+ case TD_IDLE:
+ case TD_INIT:
+ enableTransmitter();
+ if (!_currentNode->isInitialised()) {
+ charsSent = sendInitialisation(_currentNode);
+ _currentNode->setInitialised();
+ _transmitState = TD_TRANSMIT;
+ delayUntil(_currentMicros+_byteTransmitTime*charsSent);
+ break;
+ }
+ /* fallthrough */
+ case TD_TRANSMIT:
+ charsSent = sendData(_currentNode);
+ _transmitState = TD_PROMPT;
+ // Defer next entry for as long as it takes to transmit the characters,
+ // to allow output queue to empty. Allow 2 bytes extra.
+ delayUntil(_currentMicros+_byteTransmitTime*(charsSent+2));
+ break;
+ case TD_PROMPT:
+ charsSent = requestData(_currentNode);
+ disableTransmitter();
+ _transmitState = TD_RECEIVE;
+ _timeoutStart = _currentMicros; // Start timeout on response
+ break;
+ case TD_RECEIVE: // Waiting for response / timeout
+ if (_currentMicros - _timeoutStart > _timeoutPeriod) {
+ // End of time slot allocated for responses.
+ _transmitState = TD_IDLE;
+ // Reset state of receiver
+ _receiveState = RD_SYN1;
+ // Move to next node
+ _currentNode = _currentNode->getNext();
+ }
+ break;
+ }
+}
+
+// Process any data bytes received from a CMRInode.
+void CMRIbus::processIncoming() {
+ int data = _serial->read();
+ if (data < 0) return; // No characters to read
+
+ if (_transmitState != TD_RECEIVE || !_currentNode) return; // Not waiting for input, so ignore.
+
+ uint8_t nextState = RD_SYN1; // default to resetting state machine
+ switch(_receiveState) {
+ case RD_SYN1:
+ if (data == SYN) nextState = RD_SYN2;
+ break;
+ case RD_SYN2:
+ if (data == SYN) nextState = RD_STX; else nextState = RD_SYN2;
+ break;
+ case RD_STX:
+ if (data == STX) nextState = RD_ADDR;
+ break;
+ case RD_ADDR:
+ // If address doesn't match, then ignore everything until next SYN-SYN-STX.
+ if (data == _currentNode->getAddress() + 65) nextState = RD_TYPE;
+ break;
+ case RD_TYPE:
+ _receiveDataIndex = 0; // Initialise data pointer
+ if (data == 'R') nextState = RD_DATA;
+ break;
+ case RD_DATA: // data body
+ if (data == DLE) // escape next character
+ nextState = RD_ESCDATA;
+ else if (data == ETX) { // end of data
+ // End of data message. Protocol has all data in one
+ // message, so we don't need to wait any more. Allow
+ // transmitter to proceed with next node in list.
+ _currentNode = _currentNode->getNext();
+ _transmitState = TD_IDLE;
+ } else {
+ // Not end yet, so save data byte
+ _currentNode->saveIncomingData(_receiveDataIndex++, data);
+ nextState = RD_DATA; // wait for more data
+ }
+ break;
+ case RD_ESCDATA: // escaped data byte
+ _currentNode->saveIncomingData(_receiveDataIndex++, data);
+ nextState = RD_DATA;
+ break;
+ }
+ _receiveState = nextState;
+}
+
+// If configured for half duplex RS485, switch RS485 interface
+// into transmit mode.
+void CMRIbus::enableTransmitter() {
+ if (_transmitEnablePin != VPIN_NONE)
+ ArduinoPins::fastWriteDigital(_transmitEnablePin, 1);
+ // If we need a delay before we start the packet header,
+ // we can send a character or two to synchronise the
+ // transmitter and receiver.
+ // SYN characters should be used, but a bug in the
+ // ArduinoCMRI library causes it to ignore the packet if
+ // it's preceded by an odd number of SYN characters.
+ // So send a SYN followed by a NUL in that case.
+ _serial->write(SYN);
+#if defined(ARDUINOCMRI_COMPATIBLE)
+ _serial->write(NUL); // Reset the ArduinoCMRI library's parser
+#endif
+}
+
+// If configured for half duplex RS485, switch RS485 interface
+// into receive mode.
+void CMRIbus::disableTransmitter() {
+ // Wait until all data has been transmitted. On the standard
+ // AVR driver, this waits until the FIFO is empty and all
+ // data has been sent over the link.
+ _serial->flush();
+ // If we don't trust the 'flush' function and think the
+ // data's still in transit, then wait a bit longer.
+ if (_postDelay > 0)
+ delayMicroseconds(_postDelay);
+ // Hopefully, we can now safely switch off the transmitter.
+ if (_transmitEnablePin != VPIN_NONE)
+ ArduinoPins::fastWriteDigital(_transmitEnablePin, 0);
+}
+
+// Link to chain of CMRI bus instances
+CMRIbus *CMRIbus::_busList = NULL;
+
+
+/************************************************************
+ * CMRInode implementation
+ ************************************************************/
+
+// Constructor for CMRInode object
+CMRInode::CMRInode(VPIN firstVpin, int nPins, uint8_t busNo, uint8_t address, char type, uint16_t inputs, uint16_t outputs) {
+ _firstVpin = firstVpin;
+ _nPins = nPins;
+ _busNo = busNo;
+ _address = address;
+ _type = type;
+
+ switch (_type) {
+ case 'M': // SMINI, fixed 24 inputs and 48 outputs
+ _numInputs = 24;
+ _numOutputs = 48;
+ break;
+ case 'C': // CPNODE with 16 to 144 inputs/outputs using 8-bit cards
+ _numInputs = inputs;
+ _numOutputs = outputs;
+ break;
+ case 'N': // Classic USIC and SUSIC using 24 bit i/o cards
+ case 'X': // SUSIC using 32 bit i/o cards
+ default:
+ DIAG(F("CMRInode: bus:%d address:%d ERROR unsupported type %c"), _busNo, _address, _type);
+ return; // Don't register device.
+ }
+ if ((unsigned int)_nPins < _numInputs + _numOutputs)
+ DIAG(F("CMRInode: bus:%d address:%d WARNING number of Vpins does not cover all inputs and outputs"), _busNo, _address);
+
+ // Allocate memory for states
+ _inputStates = (uint8_t *)calloc((_numInputs+7)/8, 1);
+ _outputStates = (uint8_t *)calloc((_numOutputs+7)/8, 1);
+ if (!_inputStates || !_outputStates) {
+ DIAG(F("CMRInode: ERROR insufficient memory"));
+ return;
+ }
+
+ // Add this device to HAL device list
+ IODevice::addDevice(this);
+
+ // Add CMRInode to CMRIbus object.
+ CMRIbus *bus = CMRIbus::findBus(_busNo);
+ if (bus != NULL) {
+ bus->addNode(this);
+ return;
+ }
+}
+
diff --git a/IO_CMRI.h b/IO_CMRI.h
new file mode 100644
index 0000000..bb1abfb
--- /dev/null
+++ b/IO_CMRI.h
@@ -0,0 +1,291 @@
+/*
+ * © 2023, Neil McKechnie. All rights reserved.
+ *
+ * This file is part of DCC++EX API
+ *
+ * This is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * It is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with CommandStation. If not, see .
+ */
+
+/*
+ * CMRIbus
+ * =======
+ * To define a CMRI bus, example syntax:
+ * CMRIbus::create(bus, serial, baud[, cycletime[, pin]]);
+ *
+ * bus = 0-255
+ * serial = serial port to be used (e.g. Serial3)
+ * baud = baud rate (9600, 19200, 28800, 57600 or 115200)
+ * cycletime = minimum time between successive updates/reads of a node in millisecs (default 500ms)
+ * pin = pin number connected to RS485 module's DE and !RE terminals for half-duplex operation (default VPIN_NONE)
+ *
+ * Each bus must use a different serial port.
+ *
+ * IMPORTANT: If you are using ArduinoCMRI library code by Michael Adams, at the time of writing this library
+ * is not compliant with the LCS-9.10.1 specification for CMRInet protocol.
+ * Various work-arounds may be enabled within the driver by adding the following line to your config.h file,
+ * to allow nodes running the ArduinoCMRI library to communicate:
+ *
+ * #define ARDUINOCMRI_COMPATIBLE
+ *
+ * CMRINode
+ * ========
+ * To define a CMRI node and associate it with a CMRI bus,
+ * CMRInode::create(firstVPIN, numVPINs, bus, address, type [, inputs, outputs]);
+ *
+ * firstVPIN = first vpin in block allocated to this device
+ * numVPINs = number of vpins (e.g. 72 for an SMINI node)
+ * bus = 0-255
+ * address = 0-127
+ * type = 'M' for SMINI (fixed 24 inputs and 48 outputs)
+ * 'C' for CPNODE (16 to 144 inputs/outputs in groups of 8)
+ * (other types are not supported at this time).
+ * inputs = number of inputs (CPNODE only)
+ * outputs = number of outputs (CPNODE only)
+ *
+ * Reference: "LCS-9.10.1
+ * Layout Control Specification: CMRInet Protocol
+ * Version 1.1 December 2014."
+ */
+
+#ifndef IO_CMRI_H
+#define IO_CMRI_H
+
+#include "IODevice.h"
+
+/**********************************************************************
+ * CMRInode class
+ *
+ * This encapsulates the state associated with a single CMRI node,
+ * which includes the address type, number of inputs and outputs, and
+ * the states of the inputs and outputs.
+ **********************************************************************/
+class CMRInode : public IODevice {
+private:
+ uint8_t _busNo;
+ uint8_t _address;
+ char _type;
+ CMRInode *_next = NULL;
+ uint8_t *_inputStates = NULL;
+ uint8_t *_outputStates = NULL;
+ uint16_t _numInputs = 0;
+ uint16_t _numOutputs = 0;
+ bool _initialised = false;
+
+public:
+ static void create(VPIN firstVpin, int nPins, uint8_t busNo, uint8_t address, char type, uint16_t inputs=0, uint16_t outputs=0) {
+ if (checkNoOverlap(firstVpin, nPins)) new CMRInode(firstVpin, nPins, busNo, address, type, inputs, outputs);
+ }
+ CMRInode(VPIN firstVpin, int nPins, uint8_t busNo, uint8_t address, char type, uint16_t inputs=0, uint16_t outputs=0);
+
+ uint8_t getAddress() {
+ return _address;
+ }
+ CMRInode *getNext() {
+ return _next;
+ }
+ void setNext(CMRInode *node) {
+ _next = node;
+ }
+ bool isInitialised() {
+ return _initialised;
+ }
+ void setInitialised() {
+ _initialised = true;
+ }
+
+ void _begin() {
+ _initialised = false;
+ }
+
+ int _read(VPIN vpin) {
+ // Return current state from this device
+ uint16_t pin = vpin - _firstVpin;
+ if (pin < _numInputs) {
+ uint8_t mask = 1 << (pin & 0x7);
+ int index = pin / 8;
+ return (_inputStates[index] & mask) != 0;
+ } else
+ return 0;
+ }
+
+ void _write(VPIN vpin, int value) {
+ // Update current state for this device, in preparation the bus transmission
+ uint16_t pin = vpin - _firstVpin - _numInputs;
+ if (pin < _numOutputs) {
+ uint8_t mask = 1 << (pin & 0x7);
+ int index = pin / 8;
+ if (value)
+ _outputStates[index] |= mask;
+ else
+ _outputStates[index] &= ~mask;
+ }
+ }
+
+ void saveIncomingData(uint8_t index, uint8_t data) {
+ if (index < (_numInputs+7)/8)
+ _inputStates[index] = data;
+ }
+
+ uint8_t getOutputStates(uint8_t index) {
+ if (index < (_numOutputs+7)/8)
+ return _outputStates[index];
+ else
+ return 0;
+ }
+
+ uint16_t getNumInputs() {
+ return _numInputs;
+ }
+
+ uint16_t getNumOutputs() {
+ return _numOutputs;
+ }
+
+ char getType() {
+ return _type;
+ }
+
+ uint8_t getBusNumber() {
+ return _busNo;
+ }
+
+ void _display() override {
+ DIAG(F("CMRInode type:'%c' configured on bus:%d address:%d VPINs:%u-%u (in) %u-%u (out)"),
+ _type, _busNo, _address, _firstVpin, _firstVpin+_numInputs-1,
+ _firstVpin+_numInputs, _firstVpin+_numInputs+_numOutputs-1);
+ }
+
+};
+
+/**********************************************************************
+ * CMRIbus class
+ *
+ * This encapsulates the properties state of the bus and the
+ * transmission and reception of data across that bus. Each CMRIbus
+ * object owns a set of CMRInode objects which represent the nodes
+ * attached to that bus.
+ **********************************************************************/
+class CMRIbus : public IODevice {
+private:
+ // Here we define the device-specific variables.
+ uint8_t _busNo;
+ HardwareSerial *_serial;
+ unsigned long _baud;
+ VPIN _transmitEnablePin = VPIN_NONE;
+ CMRInode *_nodeListStart = NULL, *_nodeListEnd = NULL;
+ CMRInode *_currentNode = NULL;
+
+ // Transmitter state machine states
+ enum {TD_IDLE, TD_PRETRANSMIT, TD_INIT, TD_TRANSMIT, TD_PROMPT, TD_RECEIVE};
+ uint8_t _transmitState = TD_IDLE;
+ // Receiver state machine states.
+ enum {RD_SYN1, RD_SYN2, RD_STX, RD_ADDR, RD_TYPE,
+ RD_DATA, RD_ESCDATA, RD_SKIPDATA, RD_SKIPESCDATA, RD_ETX};
+ uint8_t _receiveState = RD_SYN1;
+ uint16_t _receiveDataIndex = 0; // Index of next data byte to be received.
+ CMRIbus *_nextBus = NULL; // Pointer to next bus instance in list.
+ unsigned long _cycleStartTime = 0;
+ unsigned long _timeoutStart = 0;
+ unsigned long _cycleTime; // target time between successive read/write cycles, microseconds
+ unsigned long _timeoutPeriod; // timeout on read responses, in microseconds.
+ unsigned long _currentMicros; // last value of micros() from _loop function.
+ unsigned long _postDelay; // delay time after transmission before switching off transmitter (in us)
+ unsigned long _byteTransmitTime; // time in us for transmission of one byte
+
+ static CMRIbus *_busList; // linked list of defined bus instances
+
+ // Definition of special characters in CMRInet protocol
+ enum : uint8_t {
+ NUL = 0x00,
+ STX = 0x02,
+ ETX = 0x03,
+ DLE = 0x10,
+ SYN = 0xff,
+ };
+
+public:
+ static void create(uint8_t busNo, HardwareSerial &serial, unsigned long baud, uint16_t cycleTimeMS=500, VPIN transmitEnablePin=VPIN_NONE) {
+ new CMRIbus(busNo, serial, baud, cycleTimeMS, transmitEnablePin);
+ }
+
+ // Device-specific initialisation
+ void _begin() override {
+ // CMRInet spec states one stop bit, JMRI and ArduinoCMRI use two stop bits
+#if defined(ARDUINOCMRI_COMPATIBLE)
+ _serial->begin(_baud, SERIAL_8N2);
+#else
+ _serial->begin(_baud, SERIAL_8N1);
+#endif
+ #if defined(DIAG_IO)
+ _display();
+ #endif
+ }
+
+ // Loop function (overriding IODevice::_loop(unsigned long))
+ void _loop(unsigned long currentMicros) override;
+
+ // Display information about the device
+ void _display() override {
+ DIAG(F("CMRIbus %d configured, speed=%d baud, cycle=%d ms"), _busNo, _baud, _cycleTime/1000);
+ }
+
+ // Locate CMRInode object with specified address.
+ CMRInode *findNode(uint8_t address) {
+ for (CMRInode *node = _nodeListStart; node != NULL; node = node->getNext()) {
+ if (node->getAddress() == address)
+ return node;
+ }
+ return NULL;
+ }
+
+ // Add new CMRInode to the list of nodes for this bus.
+ void addNode(CMRInode *newNode) {
+ if (!_nodeListStart)
+ _nodeListStart = newNode;
+ if (!_nodeListEnd)
+ _nodeListEnd = newNode;
+ else
+ _nodeListEnd->setNext(newNode);
+ }
+
+protected:
+ CMRIbus(uint8_t busNo, HardwareSerial &serial, unsigned long baud, uint16_t cycleTimeMS, VPIN transmitEnablePin);
+ uint16_t sendData(CMRInode *node);
+ uint16_t requestData(CMRInode *node);
+ uint16_t sendInitialisation(CMRInode *node);
+
+ // Process any data bytes received from a CMRInode.
+ void processIncoming();
+ // Process any outgoing traffic that is due.
+ void processOutgoing();
+ // Enable transmitter
+ void enableTransmitter();
+ // Disable transmitter and enable receiver
+ void disableTransmitter();
+
+
+public:
+ uint8_t getBusNumber() {
+ return _busNo;
+ }
+
+ static CMRIbus *findBus(uint8_t busNo) {
+ for (CMRIbus *bus=_busList; bus!=NULL; bus=bus->_nextBus) {
+ if (bus->_busNo == busNo) return bus;
+ }
+ return NULL;
+ }
+};
+
+#endif // IO_CMRI_H
\ No newline at end of file
diff --git a/mySetup_h_cmri.txt b/mySetup_h_cmri.txt
new file mode 100644
index 0000000..405c3b1
--- /dev/null
+++ b/mySetup_h_cmri.txt
@@ -0,0 +1,128 @@
+// mySetup.h
+// defining CMRI accessories
+// CMRI connections defined in myHal.cpp
+//
+// this is for testing.
+SETUP("D CMD 1");
+// Turnouts defined in myAutomation.h can include descriptions which will appear in Engine Driver
+// Sensors and digital outputs do not require pre-definition for use in EXRAIL automation
+//
+// SMINI emulation node 24-input/48-outputs
+// the sketch I use
+// 16 or 24 input pins
+// 32 or 48 output pins
+//
+// Define 16 input pins 1000-1015
+SETUP("S 1000 1000 1");
+SETUP("S 1001 1001 1");
+SETUP("S 1002 1002 1");
+SETUP("S 1003 1003 1");
+SETUP("S 1004 1004 1");
+SETUP("S 1005 1005 1");
+SETUP("S 1006 1006 1");
+SETUP("S 1007 1007 1");
+SETUP("S 1008 1008 1");
+SETUP("S 1009 1009 1");
+SETUP("S 1010 1010 1");
+SETUP("S 1011 1011 1");
+SETUP("S 1012 1012 1");
+SETUP("S 1013 1013 1");
+SETUP("S 1014 1014 1");
+SETUP("S 1015 1015 1");
+//
+// define 16 turnouts using VPIN (for Throw/Close commands via CMRI)
+SETUP("T 1024 VPIN 1024");
+SETUP("T 1025 VPIN 1025");
+SETUP("T 1026 VPIN 1026");
+SETUP("T 1027 VPIN 1027");
+SETUP("T 1028 VPIN 1028");
+SETUP("T 1029 VPIN 1029");
+SETUP("T 1030 VPIN 1030");
+SETUP("T 1031 VPIN 1031");
+SETUP("T 1032 VPIN 1032");
+SETUP("T 1033 VPIN 1033");
+SETUP("T 1034 VPIN 1034");
+SETUP("T 1035 VPIN 1035");
+SETUP("T 1036 VPIN 1036");
+SETUP("T 1037 VPIN 1037");
+SETUP("T 1038 VPIN 1038");
+SETUP("T 1039 VPIN 1039");
+//
+// define 16 pins for digital outputs
+SETUP("Z 1040 1040 0");
+SETUP("Z 1041 1041 0");
+SETUP("Z 1042 1042 0");
+SETUP("Z 1043 1043 0");
+SETUP("Z 1044 1044 0");
+SETUP("Z 1045 1045 0");
+SETUP("Z 1046 1046 0");
+SETUP("Z 1047 1047 0");
+SETUP("Z 1048 1048 0");
+SETUP("Z 1049 1049 0");
+SETUP("Z 1050 1050 0");
+SETUP("Z 1051 1051 0");
+SETUP("Z 1052 1052 0");
+SETUP("Z 1053 1053 0");
+SETUP("Z 1054 1054 0");
+SETUP("Z 1055 1055 0");
+//
+// additional 16 outputs available 1056-1071
+//SETUP("Z 1056 1056 0");
+//
+// CMRI sketch used for testing available here
+// https://www.trainboard.com/highball/index.php?threads/24-in-48-out-card-for-jmri.116454/page-2#post-1141569
+//
+
+// Define 16 input pins 900-915
+SETUP("S 900 900 1");
+SETUP("S 901 901 1");
+SETUP("S 902 902 1");
+SETUP("S 903 903 1");
+SETUP("S 904 904 1");
+SETUP("S 905 905 1");
+SETUP("S 906 906 1");
+SETUP("S 907 907 1");
+SETUP("S 908 908 1");
+SETUP("S 909 909 1");
+SETUP("S 910 910 1");
+SETUP("S 911 911 1");
+SETUP("S 912 912 1");
+SETUP("S 913 913 1");
+SETUP("S 914 914 1");
+SETUP("S 915 915 1");
+//
+// define 16 turnouts using VPIN (for Throw/Close commands via CMRI)
+SETUP("T 924 VPIN 924");
+SETUP("T 925 VPIN 925");
+SETUP("T 926 VPIN 926");
+SETUP("T 927 VPIN 927");
+SETUP("T 928 VPIN 928");
+SETUP("T 929 VPIN 929");
+SETUP("T 930 VPIN 930");
+SETUP("T 931 VPIN 931");
+SETUP("T 932 VPIN 932");
+SETUP("T 933 VPIN 933");
+SETUP("T 934 VPIN 934");
+SETUP("T 935 VPIN 935");
+SETUP("T 936 VPIN 936");
+SETUP("T 937 VPIN 937");
+SETUP("T 938 VPIN 938");
+SETUP("T 939 VPIN 939");
+//
+// define 16 pins for digital outputs
+SETUP("Z 940 940 0");
+SETUP("Z 941 941 0");
+SETUP("Z 942 942 0");
+SETUP("Z 943 943 0");
+SETUP("Z 944 944 0");
+SETUP("Z 945 945 0");
+SETUP("Z 946 946 0");
+SETUP("Z 947 947 0");
+SETUP("Z 948 948 0");
+SETUP("Z 949 949 0");
+SETUP("Z 950 950 0");
+SETUP("Z 951 951 0");
+SETUP("Z 952 952 0");
+SETUP("Z 953 953 0");
+SETUP("Z 954 954 0");
+SETUP("Z 955 955 0");
From 27fcdfbbbaf873aba1285f0fde18caf20763eff0 Mon Sep 17 00:00:00 2001
From: Ash-4 <81280775+Ash-4@users.noreply.github.com>
Date: Wed, 2 Aug 2023 15:31:49 -0500
Subject: [PATCH 02/51] CMRI setup lines
---
myHal.cpp_example.txt | 40 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 40 insertions(+)
diff --git a/myHal.cpp_example.txt b/myHal.cpp_example.txt
index d93ea5c..f3fa1b3 100644
--- a/myHal.cpp_example.txt
+++ b/myHal.cpp_example.txt
@@ -24,6 +24,16 @@
//#include "IO_TouchKeypad.h // Touch keypad with 16 keys
//#include "IO_EXTurntable.h" // Turntable-EX turntable controller
//#include "IO_EXFastClock.h" // FastClock driver
+//#include "IO_CMRI.h" // CMRI nodes
+
+//==========================================================================
+// also for CMRI connection using RS485 TTL module
+//==========================================================================
+// define UARt2 pins for ESP32 Rx=16, Tx=17 -- can conflict if sabertooth defined
+//HardwareSerial mySerial2(2); // use UART2
+//
+// for SERIAL_8N2 include this in config.h
+// #define ARDUINOCMRI_COMPATIBLE
//==========================================================================
// The function halSetup() is invoked from CS if it exists within the build.
@@ -33,6 +43,36 @@
void halSetup() {
+//==========================================================================
+// CMRI bus and nodes defined
+//==========================================================================
+// further explanation in IO_CMRI.h
+// this example is being used to test connection of existing CMRI device
+// add lines to myHal.cpp within halSetup()
+
+// for ESP32
+//mySerial2.begin(9600, SERIAL_8N2, 16, 17); // ESP32 to define pins also check DCCTimerESP.cpp
+//CMRIbus::create(0, mySerial2, 9600, 500, 4); // for ESP32
+
+// for Mega
+//CMRIbus::create(0, Serial3, 9600, 500, 38); // for Mega - Serial3 already defined
+ // bus=0 always, unless multiple serial ports are used
+ // baud=9600 to match setting in existing CMRI nodes
+ // cycletime.. 500ms is default -- more frequent might be needed on master
+ // pin.. DE/!RE pins tied together on TTL RS485 module.
+ // pin 38 should work on Mega and F411RE (pin D38 aka PB12 on CN10_16)
+
+//CMRInode::create(900, 72, 0, 4, 'M');
+//CMRInode::create(1000, 72, 0, 5, 'M');
+ // bus=0 must agree with bus in CMRIbus
+ // node=4 number to agree with node numbering
+ // 'M' is for SMINI.
+ // Starting VPin, Number of VPins=72 for SMINI
+//==========================================================================
+// end of CMRI
+//==========================================================================
+
+
//=======================================================================
// The following directives define auxiliary display devices.
// These can be defined in addition to the system display (display
From ad6a079c0b7677b34109bc9c0891c55904df0ca6 Mon Sep 17 00:00:00 2001
From: Ash-4
Date: Fri, 18 Aug 2023 14:21:26 -0500
Subject: [PATCH 03/51] Enable multiple CMRI nodes to function
---
DCCTimerSTM32.cpp | 1 +
IO_CMRI.h | 4 +++-
2 files changed, 4 insertions(+), 1 deletion(-)
diff --git a/DCCTimerSTM32.cpp b/DCCTimerSTM32.cpp
index cffae40..7e121be 100644
--- a/DCCTimerSTM32.cpp
+++ b/DCCTimerSTM32.cpp
@@ -55,6 +55,7 @@ HardwareSerial Serial5(PD2, PC12); // Rx=PC7, Tx=PC6 -- UART5 - F446RE
#elif defined(ARDUINO_NUCLEO_F413ZH) || defined(ARDUINO_NUCLEO_F429ZI) || defined(ARDUINO_NUCLEO_F446ZE)|| defined(ARDUINO_NUCLEO_F412ZG)
// Nucleo-144 boards don't have Serial1 defined by default
HardwareSerial Serial6(PG9, PG14); // Rx=PG9, Tx=PG14 -- USART6
+HardwareSerial Serial5(PE7, PE8); // Rx=PE7, Tx=PE8 -- USART5
// Serial3 is defined to use USART3 by default, but is in fact used as the diag console
// via the debugger on the Nucleo-144. It is therefore unavailable for other DCC-EX uses like WiFi, DFPlayer, etc.
#else
diff --git a/IO_CMRI.h b/IO_CMRI.h
index bb1abfb..ef647b8 100644
--- a/IO_CMRI.h
+++ b/IO_CMRI.h
@@ -255,8 +255,10 @@ public:
_nodeListStart = newNode;
if (!_nodeListEnd)
_nodeListEnd = newNode;
- else
+ else {
_nodeListEnd->setNext(newNode);
+ _nodeListEnd = newNode;
+ }
}
protected:
From 0d5495aa25c5107b369724543e8b3410a170f7aa Mon Sep 17 00:00:00 2001
From: pmantoine
Date: Mon, 23 Oct 2023 21:28:25 +0800
Subject: [PATCH 04/51] F429ZI edits for ethernet
---
EthernetInterface.cpp | 63 +++++++++++++++++++++++++++++++++----------
EthernetInterface.h | 10 +++++++
platformio.ini | 18 ++++++++++++-
version.h | 3 ++-
4 files changed, 78 insertions(+), 16 deletions(-)
diff --git a/EthernetInterface.cpp b/EthernetInterface.cpp
index 5cf531c..993d5d0 100644
--- a/EthernetInterface.cpp
+++ b/EthernetInterface.cpp
@@ -41,8 +41,11 @@ void EthernetInterface::setup()
DIAG(F("Prog Error!"));
return;
}
- if ((singleton=new EthernetInterface()))
+ DIAG(F("Ethernet Class setup, attempting to instantiate"));
+ if ((singleton=new EthernetInterface())) {
+ DIAG(F("Ethernet Class initialized"));
return;
+ }
DIAG(F("Ethernet not initialized"));
};
@@ -55,24 +58,48 @@ void EthernetInterface::setup()
*/
EthernetInterface::EthernetInterface()
{
- byte mac[6];
- DCCTimer::getSimulatedMacAddress(mac);
connected=false;
-
- #ifdef IP_ADDRESS
- Ethernet.begin(mac, IP_ADDRESS);
- #else
- if (Ethernet.begin(mac) == 0)
+#if defined(STM32_ETHERNET)
+ // Set a HOSTNAME for the DHCP request - a nice to have, but hard it seems on LWIP for STM32
+ // The default is "lwip", which is **always** set in STM32Ethernet/src/utility/ethernetif.cpp
+ // for some reason. One can edit it to instead read:
+ // #if LWIP_NETIF_HOSTNAME
+ // /* Initialize interface hostname */
+ // if (netif->hostname == NULL)
+ // netif->hostname = "lwip";
+ // #endif /* LWIP_NETIF_HOSTNAME */
+ // Which seems more useful! We should propose the patch... so the following line actually works!
+ netif_set_hostname(&gnetif, WIFI_HOSTNAME); // Should probably be passed in the contructor...
+ #ifdef IP_ADDRESS
+ if (Ethernet.begin(IP_ADDRESS) == 0)
+ #else
+ if (Ethernet.begin() == 0)
+ #endif // IP_ADDRESS
{
DIAG(F("Ethernet.begin FAILED"));
return;
}
- #endif
+#else // All other architectures
+ byte mac[6]= { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
+ DIAG(F("Ethernet attempting to get MAC address"));
+ DCCTimer::getSimulatedMacAddress(mac);
+ DIAG(F("Ethernet got MAC address"));
+ #ifdef IP_ADDRESS
+ if (Ethernet.begin(IP_ADDRESS) == 0)
+ #else
+ if (Ethernet.begin(mac, IP_ADDRESS) == 0)
+ #endif
+ {
+ DIAG(F("Ethernet.begin FAILED"));
+ return;
+ }
+
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
DIAG(F("Ethernet shield not found or W5100"));
}
-
- unsigned long startmilli = millis();
+#endif
+
+ uint32_t startmilli = millis();
while ((millis() - startmilli) < 5500) { // Loop to give time to check for cable connection
if (Ethernet.linkStatus() == LinkON)
break;
@@ -171,17 +198,25 @@ void EthernetInterface::loop2() {
return;
}
// get client from the server
+ #if defined (STM32_ETHERNET)
+ // STM32Ethernet doesn't use accept(), just available()
+ EthernetClient client = server->available();
+ #else
EthernetClient client = server->accept();
-
+ #endif
// check for new client
if (client)
{
- if (Diag::ETHERNET) DIAG(F("Ethernet: New client "));
byte socket;
for (socket = 0; socket < MAX_SOCK_NUM; socket++)
{
- if (!clients[socket])
+ if (clients[socket]) {
+ if (clients[socket] == client)
+ break;
+ }
+ else //if (!clients[socket])
{
+ if (Diag::ETHERNET) DIAG(F("Ethernet: New client "));
// On accept() the EthernetServer doesn't track the client anymore
// so we store it in our client array
if (Diag::ETHERNET) DIAG(F("Socket %d"),socket);
diff --git a/EthernetInterface.h b/EthernetInterface.h
index 8078c3f..f4f18dd 100644
--- a/EthernetInterface.h
+++ b/EthernetInterface.h
@@ -35,8 +35,18 @@
#if defined (ARDUINO_TEENSY41)
#include //TEENSY Ethernet Treiber
#include
+ #define MAX_SOCK_NUM 4
+#elif defined (ARDUINO_NUCLEO_F429ZI) || defined (ARDUINO_NUCLEO_F439ZI)
+ #include
+// #include "STM32lwipopts.h"
+ #include
+ #include
+ extern "C" struct netif gnetif;
+ #define STM32_ETHERNET
+ #define MAX_SOCK_NUM 10
#else
#include "Ethernet.h"
+ #define MAX_SOCK_NUM 4
#endif
#include "RingStream.h"
diff --git a/platformio.ini b/platformio.ini
index 8767ef1..e1ddfb5 100644
--- a/platformio.ini
+++ b/platformio.ini
@@ -245,7 +245,23 @@ monitor_echo = yes
; Experimental - Ethernet work still in progress
;
-; [env:Nucleo-F429ZI]
+[env:Nucleo-F429ZI]
+platform = ststm32
+board = nucleo_f429zi
+framework = arduino
+lib_deps = ${env.lib_deps}
+ arduino-libraries/Ethernet @ ^2.0.1
+ stm32duino/STM32Ethernet @ ^1.3.0
+ stm32duino/STM32duino LwIP @ ^2.1.2
+build_flags = -std=c++17 -Os -g2 -Wunused-variable
+monitor_speed = 115200
+monitor_echo = yes
+upload_protocol = stlink
+
+; Experimental - Ethernet work still in progress
+; Commented out as the F439ZI also needs variant files
+;
+; [env:Nucleo-F439ZI]
; platform = ststm32
; board = nucleo_f429zi
; framework = arduino
diff --git a/version.h b/version.h
index 4daafba..3c98c21 100644
--- a/version.h
+++ b/version.h
@@ -3,7 +3,8 @@
#include "StringFormatter.h"
-#define VERSION "5.1.17"
+#define VERSION "5.1.17eth"
+// 5.1.17e - Initial ethernet code for STM32F429ZI and F439ZI boards
// 5.1.17 - Divide out C for config and D for diag commands
// 5.1.16 - Remove I2C address from EXTT_TURNTABLE macro to work with MUX, requires separate HAL macro to create
// 5.1.15 - LCC/Adapter support and Exrail feature-compile-out.
From 9437945745bcf5c861eb6df2c67d9cf8a45e1e5b Mon Sep 17 00:00:00 2001
From: pmantoine
Date: Sun, 29 Oct 2023 17:34:51 +0800
Subject: [PATCH 05/51] STM32F439ZI updates
---
DCCTimerSTM32.cpp | 2 +-
I2CManager_STM32.h | 3 ++-
platformio.ini | 24 ++++++++++++------------
3 files changed, 15 insertions(+), 14 deletions(-)
diff --git a/DCCTimerSTM32.cpp b/DCCTimerSTM32.cpp
index f2d51ff..13694aa 100644
--- a/DCCTimerSTM32.cpp
+++ b/DCCTimerSTM32.cpp
@@ -52,7 +52,7 @@ HardwareSerial Serial6(PA12, PA11); // Rx=PA12, Tx=PA11 -- CN10 pins 12 and 14
HardwareSerial Serial3(PC11, PC10); // Rx=PC11, Tx=PC10 -- USART3 - F446RE
HardwareSerial Serial5(PD2, PC12); // Rx=PC7, Tx=PC6 -- UART5 - F446RE
// On the F446RE, Serial4 and Serial6 also use pins we can't readily map while using the Arduino pins
-#elif defined(ARDUINO_NUCLEO_F412ZG) || defined(ARDUINO_NUCLEO_F413ZH) || defined(ARDUINO_NUCLEO_F429ZI) || defined(ARDUINO_NUCLEO_F446ZE)
+#elif defined(ARDUINO_NUCLEO_F412ZG) || defined(ARDUINO_NUCLEO_F413ZH) || defined(ARDUINO_NUCLEO_F429ZI) || defined(ARDUINO_NUCLEO_F439ZI) || defined(ARDUINO_NUCLEO_F446ZE)
// Nucleo-144 boards don't have Serial1 defined by default
HardwareSerial Serial6(PG9, PG14); // Rx=PG9, Tx=PG14 -- USART6
// Serial3 is defined to use USART3 by default, but is in fact used as the diag console
diff --git a/I2CManager_STM32.h b/I2CManager_STM32.h
index 7e0f547..f386959 100644
--- a/I2CManager_STM32.h
+++ b/I2CManager_STM32.h
@@ -39,7 +39,8 @@
#if defined(I2C_USE_INTERRUPTS) && defined(ARDUINO_ARCH_STM32)
#if defined(ARDUINO_NUCLEO_F401RE) || defined(ARDUINO_NUCLEO_F411RE) || defined(ARDUINO_NUCLEO_F446RE) \
|| defined(ARDUINO_NUCLEO_F412ZG) || defined(ARDUINO_NUCLEO_F413ZH) \
- || defined(ARDUINO_NUCLEO_F429ZI) || defined(ARDUINO_NUCLEO_F446ZE)
+ || defined(ARDUINO_NUCLEO_F429ZI) || defined(ARDUINO_NUCLEO_F439ZI) \
+ || defined(ARDUINO_NUCLEO_F446ZE)
// Assume I2C1 for now - default I2C bus on Nucleo-F411RE and likely all Nucleo-64
// and Nucleo-144 variants
I2C_TypeDef *s = I2C1;
diff --git a/platformio.ini b/platformio.ini
index e1ddfb5..f13d4f5 100644
--- a/platformio.ini
+++ b/platformio.ini
@@ -261,18 +261,18 @@ upload_protocol = stlink
; Experimental - Ethernet work still in progress
; Commented out as the F439ZI also needs variant files
;
-; [env:Nucleo-F439ZI]
-; platform = ststm32
-; board = nucleo_f429zi
-; framework = arduino
-; lib_deps = ${env.lib_deps}
-; arduino-libraries/Ethernet @ ^2.0.1
-; stm32duino/STM32Ethernet @ ^1.3.0
-; stm32duino/STM32duino LwIP @ ^2.1.2
-; build_flags = -std=c++17 -Os -g2 -Wunused-variable
-; monitor_speed = 115200
-; monitor_echo = yes
-; upload_protocol = stlink
+[env:Nucleo-F439ZI]
+platform = ststm32
+board = nucleo_f429zi
+framework = arduino
+lib_deps = ${env.lib_deps}
+ arduino-libraries/Ethernet @ ^2.0.1
+ stm32duino/STM32Ethernet @ ^1.3.0
+ stm32duino/STM32duino LwIP @ ^2.1.2
+build_flags = -std=c++17 -Os -g2 -Wunused-variable
+monitor_speed = 115200
+monitor_echo = yes
+upload_protocol = stlink
[env:Teensy3_2]
platform = teensy
From 645c9f9f893b71b06e6caf263769ba94d08d0087 Mon Sep 17 00:00:00 2001
From: pmantoine
Date: Sun, 29 Oct 2023 17:39:12 +0800
Subject: [PATCH 06/51] STM32 Use UID_BASE to get MAC address
---
DCCTimerSTM32.cpp | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/DCCTimerSTM32.cpp b/DCCTimerSTM32.cpp
index 13694aa..17d4b44 100644
--- a/DCCTimerSTM32.cpp
+++ b/DCCTimerSTM32.cpp
@@ -215,9 +215,9 @@ void DCCTimer::clearPWM() {
}
void DCCTimer::getSimulatedMacAddress(byte mac[6]) {
- volatile uint32_t *serno1 = (volatile uint32_t *)0x1FFF7A10;
- volatile uint32_t *serno2 = (volatile uint32_t *)0x1FFF7A14;
- // volatile uint32_t *serno3 = (volatile uint32_t *)0x1FFF7A18;
+ volatile uint32_t *serno1 = (volatile uint32_t *)UID_BASE;
+ volatile uint32_t *serno2 = (volatile uint32_t *)UID_BASE+4;
+ // volatile uint32_t *serno3 = (volatile uint32_t *)UID_BASE+8;
volatile uint32_t m1 = *serno1;
volatile uint32_t m2 = *serno2;
From 22b066c40044e6ad13c237f6044ff69725744227 Mon Sep 17 00:00:00 2001
From: kempe63 <78020007+kempe63@users.noreply.github.com>
Date: Sun, 5 Nov 2023 15:57:58 +0000
Subject: [PATCH 07/51] Initial submit I2CDFPlayer
---
I2CManager.cpp | 5 +-
IODevice.h | 3 +-
IO_I2CDFPlayer.h | 589 +++++++++++++++++++++++++++++++++++++++++++++++
IO_Template.h | 69 ++++++
4 files changed, 664 insertions(+), 2 deletions(-)
create mode 100644 IO_I2CDFPlayer.h
create mode 100644 IO_Template.h
diff --git a/I2CManager.cpp b/I2CManager.cpp
index 1d1387e..fadf8bb 100644
--- a/I2CManager.cpp
+++ b/I2CManager.cpp
@@ -54,6 +54,8 @@ static const FSH * guessI2CDeviceType(uint8_t address) {
return F("Time-of-flight sensor");
else if (address >= 0x3c && address <= 0x3d)
return F("OLED Display");
+ else if (address >= 0x48 && address <= 0x57) // Henkk: Added SC16IS752 UART detection
+ return F("SC16IS752 UART");
else if (address >= 0x48 && address <= 0x4f)
return F("Analogue Inputs or PWM");
else if (address >= 0x40 && address <= 0x4f)
@@ -64,6 +66,7 @@ static const FSH * guessI2CDeviceType(uint8_t address) {
return F("Real-time clock");
else if (address >= 0x70 && address <= 0x77)
return F("I2C Mux");
+ else if (address >= 0x90 && address <= 0xAE);
else
return F("?");
}
@@ -363,4 +366,4 @@ void I2CAddress::toHex(const uint8_t value, char *buffer) {
/* static */ bool I2CAddress::_addressWarningDone = false;
-#endif
\ No newline at end of file
+#endif
diff --git a/IODevice.h b/IODevice.h
index 74fe49b..4a2fc48 100644
--- a/IODevice.h
+++ b/IODevice.h
@@ -22,7 +22,8 @@
#define iodevice_h
// Define symbol DIAG_IO to enable diagnostic output
-//#define DIAG_IO Y
+//#define DIAG_IO
+
// Define symbol DIAG_LOOPTIMES to enable CS loop execution time to be reported
//#define DIAG_LOOPTIMES
diff --git a/IO_I2CDFPlayer.h b/IO_I2CDFPlayer.h
new file mode 100644
index 0000000..d4905a5
--- /dev/null
+++ b/IO_I2CDFPlayer.h
@@ -0,0 +1,589 @@
+/*
+ * © 2023, Neil McKechnie. All rights reserved.
+ *
+ * This file is part of DCC++EX API
+ *
+ * This is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * It is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with CommandStation. If not, see .
+ */
+
+/*
+ * DFPlayer is an MP3 player module with an SD card holder. It also has an integrated
+ * amplifier, so it only needs a power supply and a speaker.
+ *
+ * This driver allows the device to be controlled through IODevice::write() and
+ * IODevice::writeAnalogue() calls.
+ *
+ * The driver is configured as follows:
+ *
+ * DFPlayer::create(firstVpin, nPins, Serialn);
+ *
+ * Where firstVpin is the first vpin reserved for reading the device,
+ * nPins is the number of pins to be allocated (max 5)
+ * and Serialn is the name of the Serial port connected to the DFPlayer (e.g. Serial1).
+ *
+ * Example:
+ * In halSetup function within myHal.cpp:
+ * DFPlayer::create(3500, 5, Serial1);
+ * or in myAutomation.h:
+ * HAL(DFPlayer, 3500, 5, Serial1)
+ *
+ * Writing an analogue value 1-2999 to the first pin (3500) will play the numbered file from the
+ * SD card; e.g. a value of 1 will play the first file, 2 for the second file etc.
+ * Writing an analogue value 0 to the first pin (3500) will stop the file playing;
+ * Writing an analogue value 0-30 to the second pin (3501) will set the volume;
+ * Writing a digital value of 1 to a pin will play the file corresponding to that pin, e.g.
+ the first file will be played by setting pin 3500, the second by setting pin 3501 etc.;
+ * Writing a digital value of 0 to any pin will stop the player;
+ * Reading a digital value from any pin will return true(1) if the player is playing, false(0) otherwise.
+ *
+ * From EX-RAIL, the following commands may be used:
+ * SET(3500) -- starts playing the first file (file 1) on the SD card
+ * SET(3501) -- starts playing the second file (file 2) on the SD card
+ * etc.
+ * RESET(3500) -- stops all playing on the player
+ * WAITFOR(3500) -- wait for the file currently being played by the player to complete
+ * SERVO(3500,2,Instant) -- plays file 2 at current volume
+ * SERVO(3501,20,Instant) -- Sets the volume to 20
+ *
+ * NB The DFPlayer's serial lines are not 5V safe, so connecting the Arduino TX directly
+ * to the DFPlayer's RX terminal will cause lots of noise over the speaker, or worse.
+ * A 1k resistor in series with the module's RX terminal will alleviate this.
+ *
+ * Files on the SD card are numbered according to their order in the directory on the
+ * card (as listed by the DIR command in Windows). This may not match the order of the files
+ * as displayed by Windows File Manager, which sorts the file names. It is suggested that
+ * files be copied into an empty SDcard in the desired order, one at a time.
+ *
+ * The driver now polls the device for its current status every second. Should the device
+ * fail to respond it will be marked off-line and its busy indicator cleared, to avoid
+ * lock-ups in automation scripts that are executing for a WAITFOR().
+ *
+ * *********************************************************************************************
+ * 2023, Added NXP SC16IS752 I2C Dual UART to enable the DFPlayer connection over the I2C bus
+ * The SC16IS752 has 64 bytes TX & RX FIFO buffer
+ * First version without interrupts from I2C UART and only RX/TX are used, interrupts may not be needed as the RX Fifo holds the reply
+ *
+ *
+ */
+
+#ifndef IO_I2CDFPlayer_h
+#define IO_I2CDFPlayer_h
+
+#include "IODevice.h"
+#include "I2CManager.h"
+#include "DIAG.h"
+
+//#define DIAG_I2CDFplayer
+//#define DIAG_I2CDFplayer_data
+//#define DIAG_I2CDFplayer_reg
+
+class I2CDFPlayer : public IODevice {
+private:
+ const uint8_t MAXVOLUME=30;
+ bool _playing = false;
+ uint8_t _inputIndex = 0;
+ unsigned long _commandSendTime; // Time (us) that last transmit took place.
+ unsigned long _timeoutTime;
+ uint8_t _recvCMD; // Last received command code byte
+ bool _awaitingResponse = false;
+ uint8_t _requestedVolumeLevel = MAXVOLUME;
+ uint8_t _currentVolume = MAXVOLUME;
+ int _requestedSong = -1; // -1=none, 0=stop, >0=file number
+ // SC16IS752 defines
+ I2CAddress _I2CAddress;
+ I2CRB _rb;
+ uint8_t _UART_CH;
+ // Communication parameters for the DFPlayer are fixed at 8 bit, No parity, 1 stopbit
+ uint8_t WORD_LEN = 0x03; // Value LCR bit 0,1
+ uint8_t STOP_BIT = 0x00; // Value LCR bit 2
+ uint8_t PARITY_ENA = 0x00; // Value LCR bit 3
+ uint8_t PARITY_TYPE = 0x00; // Value LCR bit 4
+ uint32_t BAUD_RATE = 9600;
+ uint8_t PRESCALER = 0x01; // Value MCR bit 7
+ uint8_t TEMP_REG_VAL = 0x00;
+ uint8_t FIFO_RX_LEVEL = 0x00;
+ uint8_t FIFO_TX_LEVEL = 0x00;
+ uint8_t _outbuffer [11]; // DFPlayer command is 10 bytes + 1 byte register address & UART channel
+ uint8_t _inbuffer[10]; // expected DFPlayer return 10 bytes
+
+ unsigned long SC16IS752_XTAL_FREQ = 1843200; // May need to change xtal frequency to 14.7456Mhz (14745600) to allow for higher baud rates
+ //unsigned long SC16IS752_XTAL_FREQ = 14745600; // Support for higher baud rates
+
+public:
+ // Constructor
+ I2CDFPlayer(VPIN firstVpin, int nPins, I2CAddress i2cAddress, uint8_t UART_CH){
+ _firstVpin = firstVpin;
+ _nPins = nPins;
+ _I2CAddress = i2cAddress;
+ _UART_CH = UART_CH;
+ addDevice(this);
+
+ }
+
+
+public:
+ static void create(VPIN firstVpin, int nPins, I2CAddress i2cAddress, uint8_t UART_CH) {
+ if (checkNoOverlap(firstVpin, nPins, i2cAddress)) new I2CDFPlayer(firstVpin, nPins, i2cAddress, UART_CH);
+ }
+
+ void _begin() override {
+ // check if SC16IS752 exist first, initialize and then resume DFPlayer init via SC16IS752
+ I2CManager.begin();
+ //I2CManager.setClock(1000000);
+ if (I2CManager.exists(_I2CAddress)){
+ DIAG(F("SC16IS752 I2C:%s UART detected"), _I2CAddress.toString());
+ Init_SC16IS752(); // Initialize UART
+ if (_deviceState == DEVSTATE_FAILED){
+ DIAG(F("SC16IS752 I2C:%s UART initialization failed"), _I2CAddress.toString());
+ }
+ } else {
+ DIAG(F("SC16IS752 I2C:%s UART not detected"), _I2CAddress.toString());
+ }
+ #if defined(DIAG_IO)
+ _display();
+ #endif
+ // Now init DFPlayer
+ // Send a query to the device to see if it responds
+ _deviceState = DEVSTATE_INITIALISING;
+ sendPacket(0x42);
+ _timeoutTime = micros() + 5000000UL; // 5 second timeout
+ //_timeoutTime = micros() + 10000000UL; // 5 second timeout
+ _awaitingResponse = true;
+ }
+
+
+ void _loop(unsigned long currentMicros) override {
+ // Read responses from device
+
+ processIncoming();
+ // Check if a command sent to device has timed out. Allow 0.5 second for response
+ if (_awaitingResponse && (int32_t)(currentMicros - _timeoutTime) > 0) {
+ DIAG(F("I2CDFPlayer:%s, DFPlayer not responding on UART channel: 0x%x"), _I2CAddress.toString(), _UART_CH);
+ _deviceState = DEVSTATE_FAILED;
+ _awaitingResponse = false;
+ _playing = false;
+ }
+
+ // Send any commands that need to go.
+ processOutgoing(currentMicros);
+ delayUntil(currentMicros + 10000); // Only enter every 10ms
+ }
+
+
+ // Check for incoming data on _serial, and update busy flag and other state accordingly
+
+ void processIncoming() {
+ // Expected message is in the form "7E FF 06 3D xx xx xx xx xx EF"
+ RX_fifo_lvl();
+ if (FIFO_RX_LEVEL >= 10) {
+ #ifdef DIAG_I2CDFplayer
+ DIAG(F("I2CDFPlayer: %s Retrieving data from RX Fifo on UART_CH: 0x%x"),_I2CAddress.toString(), _UART_CH);
+ #endif
+ ReceiveI2CData();
+ } else {
+ return; // No data or not enough data in rx fifo, check again next time around
+ }
+
+ bool ok = false;
+ while (FIFO_RX_LEVEL != 0) {
+ int c = _inbuffer[_inputIndex]; // Start at 0, increment to FIFO_RX_LEVEL
+ switch (_inputIndex) {
+ case 0:
+ if (c == 0x7E) ok = true;
+ break;
+ case 1:
+ if (c == 0xFF) ok = true;
+ break;
+ case 2:
+ if (c== 0x06) ok = true;
+ break;
+ case 3:
+ _recvCMD = c; // CMD byte
+ ok = true;
+ break;
+ case 6:
+ switch (_recvCMD) {
+ case 0x42:
+ // Response to status query
+ _playing = (c != 0);
+ // Mark the device online and cancel timeout
+ if (_deviceState==DEVSTATE_INITIALISING) {
+ _deviceState = DEVSTATE_NORMAL;
+ #ifdef DIAG_I2CDFplayer
+ DIAG(F("I2CDFPlayer: %s, UART_CH: 0x0%x, _deviceState: 0x0%x"),_I2CAddress.toString(), _UART_CH, _deviceState);
+ #endif
+ #ifdef DIAG_IO
+ _display();
+ #endif
+ }
+ _awaitingResponse = false;
+ break;
+ case 0x3d:
+ // End of play
+ if (_playing) {
+ #ifdef DIAG_IO
+ DIAG(F("I2CDFPlayer: Finished"));
+ #endif
+ _playing = false;
+ }
+ break;
+ case 0x40:
+ // Error codes; 1: Module Busy
+ DIAG(F("I2CDFPlayer: Error %d returned from device"), c);
+ _playing = false;
+ break;
+ }
+ ok = true;
+ break;
+ case 4: case 5: case 7: case 8:
+ ok = true; // Skip over these bytes in message.
+ break;
+ case 9:
+ if (c==0xef) {
+ // Message finished
+ }
+ break;
+ default:
+ break;
+ }
+ if (ok){
+ _inputIndex++; // character as expected, so increment index
+ FIFO_RX_LEVEL --; // Decrease FIFO_RX_LEVEL with each character read from _inbuffer[_inputIndex]
+ } else {
+ _inputIndex = 0; // otherwise reset.
+ FIFO_RX_LEVEL = 0;
+ }
+ }
+ }
+
+ // Send any commands that need to be sent
+ void processOutgoing(unsigned long currentMicros) {
+ // When two commands are sent in quick succession, the device will often fail to
+ // execute one. Testing has indicated that a delay of 100ms or more is required
+ // between successive commands to get reliable operation.
+ // If 100ms has elapsed since the last thing sent, then check if there's some output to do.
+ if (((int32_t)currentMicros - _commandSendTime) > 100000) {
+ if (_currentVolume > _requestedVolumeLevel) {
+ // Change volume before changing song if volume is reducing.
+ _currentVolume = _requestedVolumeLevel;
+ sendPacket(0x06, _currentVolume);
+ } else if (_requestedSong > 0) {
+ // Change song
+ sendPacket(0x03, _requestedSong);
+ _requestedSong = -1;
+ } else if (_requestedSong == 0) {
+ sendPacket(0x16); // Stop playing
+ _requestedSong = -1;
+ } else if (_currentVolume < _requestedVolumeLevel) {
+ // Change volume after changing song if volume is increasing.
+ _currentVolume = _requestedVolumeLevel;
+ sendPacket(0x06, _currentVolume);
+ } else if ((int32_t)currentMicros - _commandSendTime > 1000000) {
+ // Poll device every second that other commands aren't being sent,
+ // to check if it's still connected and responding.
+ sendPacket(0x42);
+ if (!_awaitingResponse) {
+ _timeoutTime = currentMicros + 5000000UL; // Timeout if no response within 5 seconds
+ _awaitingResponse = true;
+ }
+ }
+ }
+ }
+
+ // Write with value 1 starts playing a song. The relative pin number is the file number.
+ // Write with value 0 stops playing.
+ void _write(VPIN vpin, int value) override {
+ if (_deviceState == DEVSTATE_FAILED) return;
+ int pin = vpin - _firstVpin;
+ if (value) {
+ // Value 1, start playing
+ #ifdef DIAG_IO
+ DIAG(F("I2CDFPlayer: Play %d"), pin+1);
+ #endif
+ _requestedSong = pin+1;
+ _playing = true;
+ } else {
+ // Value 0, stop playing
+ #ifdef DIAG_IO
+ DIAG(F("I2CDFPlayer: Stop"));
+ #endif
+ _requestedSong = 0; // No song
+ _playing = false;
+ }
+ }
+
+ // WriteAnalogue on first pin uses the nominated value as a file number to start playing, if file number > 0.
+ // Volume may be specified as second parameter to writeAnalogue.
+ // If value is zero, the player stops playing.
+ // WriteAnalogue on second pin sets the output volume.
+ //
+ void _writeAnalogue(VPIN vpin, int value, uint8_t volume=0, uint16_t=0) override {
+ if (_deviceState == DEVSTATE_FAILED) return;
+ uint8_t pin = vpin - _firstVpin;
+
+ #ifdef DIAG_IO
+ DIAG(F("I2CDFPlayer: VPIN:%u FileNo:%d Volume:%d"), vpin, value, volume);
+ #endif
+
+ // Validate parameter.
+ if (volume > MAXVOLUME) volume = MAXVOLUME;
+
+ if (pin == 0) {
+ // Play track
+ if (value > 0) {
+ if (volume > 0)
+ _requestedVolumeLevel = volume;
+ _requestedSong = value;
+ _playing = true;
+ } else {
+ _requestedSong = 0; // stop playing
+ _playing = false;
+ }
+ } else if (pin == 1) {
+ // Set volume (0-30)
+ _requestedVolumeLevel = value;
+ }
+ }
+
+ // A read on any pin indicates whether the player is still playing.
+ int _read(VPIN) override {
+ if (_deviceState == DEVSTATE_FAILED) return false;
+ return _playing;
+ }
+
+ void _display() override {
+ DIAG(F("I2CDFPlayer Configured on Vpins:%u-%u %S"), _firstVpin, _firstVpin+_nPins-1,
+ (_deviceState==DEVSTATE_FAILED) ? F("OFFLINE") : F(""));
+ }
+
+private:
+ // 7E FF 06 0F 00 01 01 xx xx EF
+ // 0 -> 7E is start code
+ // 1 -> FF is version
+ // 2 -> 06 is length
+ // 3 -> 0F is command
+ // 4 -> 00 is no receive
+ // 5~6 -> 01 01 is argument
+ // 7~8 -> checksum = 0 - ( FF+06+0F+00+01+01 )
+ // 9 -> EF is end code
+
+ void sendPacket(uint8_t command, uint16_t arg = 0)
+ {
+ FIFO_TX_LEVEL = 0; // Reset FIFO_TX_LEVEL
+ uint8_t out[] = {
+ 0x7E,
+ 0xFF,
+ 06,
+ command,
+ 00,
+ static_cast(arg >> 8),
+ static_cast(arg & 0x00ff),
+ 00,
+ 00,
+ 0xEF };
+
+ setChecksum(out);
+
+ // Prepend the DFPlayer command with REG address and UART Channel in _outbuffer
+ _outbuffer[0] = REG_THR << 3 | _UART_CH << 1; //TX FIFO and UART Channel
+ for ( int i = 1; i < sizeof(out)+1 ; i++){
+ _outbuffer[i] = out[i-1];
+ }
+
+ #ifdef DIAG_I2CDFplayer_data
+ DIAG(F("SC16IS752: I2C: %s Sent packet function"), _I2CAddress.toString());
+ for (int i = 0; i < sizeof _outbuffer; i++){
+ DIAG(F("SC16IS752: Data _outbuffer[0x%x]: 0x%x"), i, _outbuffer[i]);
+ }
+ #endif
+
+ TX_fifo_lvl();
+ if(FIFO_TX_LEVEL > 0){ //FIFO is empty
+ //I2CManager.write(_I2CAddress, _outbuffer, sizeof(_outbuffer), &_rb);
+ I2CManager.write(_I2CAddress, _outbuffer, sizeof(_outbuffer));
+ #ifdef DIAG_I2CDFplayer
+ DIAG(F("SC16IS752: I2C: %s data transmit complete on UART: 0x%x"), _I2CAddress.toString(), _UART_CH);
+ #endif
+ } else {
+ DIAG(F("I2CDFPlayer at: %s, TX FIFO not empty on UART: 0x%x"), _I2CAddress.toString(), _UART_CH);
+ _deviceState = DEVSTATE_FAILED; // This should not happen
+ }
+ _commandSendTime = micros();
+ }
+
+ uint16_t calcChecksum(uint8_t* packet)
+ {
+ uint16_t sum = 0;
+ for (int i = 1; i < 7; i++)
+ {
+ sum += packet[i];
+ }
+ return -sum;
+ }
+
+ void setChecksum(uint8_t* out)
+ {
+ uint16_t sum = calcChecksum(out);
+
+ out[7] = (sum >> 8);
+ out[8] = (sum & 0xff);
+ }
+
+ // SC16IS752 functions
+ // Initialise SC16IS752 only for this channel
+ // First a software reset
+ // Enable FIFO and clear TX & RX FIFO
+ // Need to set the following registers
+ // LCR bit 7=0 divisor latch (clock division registers DLH & DLL, they store 16 bit divisor),
+ // WORD_LEN, STOP_BIT, PARITY_ENA and PARITY_TYPE
+ // MCR bit 7=0 clock divisor devide-by-1 clock input
+ // DLH most significant part of divisor
+ // DLL least significant part of divisor
+ //
+ // BAUD_RATE, WORD_LEN, STOP_BIT, PARITY_ENA and PARITY_TYPE have been defined and initialized
+ //
+ void Init_SC16IS752(){ // Return value is in _deviceState
+ #ifdef DIAG_I2CDFplayer
+ DIAG(F("SC16IS752: Initialize I2C: %s , UART Ch: 0x%x"), _I2CAddress.toString(), _UART_CH);
+ #endif
+ uint16_t _divisor = (SC16IS752_XTAL_FREQ / PRESCALER) / (BAUD_RATE * 16);
+ TEMP_REG_VAL = 0x08; // UART Software reset
+ UART_WriteRegister(REG_IOCONTROL, TEMP_REG_VAL);
+ TEMP_REG_VAL = 0x07; // Reset FIFO, clear RX & TX FIFO
+ UART_WriteRegister(REG_FCR, TEMP_REG_VAL);
+ TEMP_REG_VAL = 0x00; // Set MCR to all 0, includes Clock divisor
+ UART_WriteRegister(REG_MCR, TEMP_REG_VAL);
+ TEMP_REG_VAL = 0x80 | WORD_LEN | STOP_BIT | PARITY_ENA | PARITY_TYPE;
+ UART_WriteRegister(REG_LCR, TEMP_REG_VAL); // Divisor latch enabled
+ UART_WriteRegister(REG_DLL, (uint8_t)_divisor); // Write DLL
+ UART_WriteRegister(REG_DLH, (uint8_t)(_divisor >> 8)); // Write DLH
+ UART_ReadRegister(REG_LCR);
+ TEMP_REG_VAL = _inbuffer[0] & 0x7F; // Disable Divisor latch enabled bit
+ UART_WriteRegister(REG_LCR, TEMP_REG_VAL); // Divisor latch disabled
+
+ uint8_t status = _rb.wait();
+ if (status != I2C_STATUS_OK) {
+ DIAG(F("SC16IS752: I2C: %s failed %S"), _I2CAddress.toString(), I2CManager.getErrorMessage(status));
+ _deviceState = DEVSTATE_FAILED;
+ } else {
+ #ifdef DIAG_IO
+ DIAG(F("SC16IS752: I2C: %s, _deviceState == I2C_STATUS_OK"), _I2CAddress.toString());
+ #endif
+ _deviceState = DEVSTATE_NORMAL; // If I2C state is OK, then proceed to initialize DFPlayer
+ }
+ }
+
+
+ // Read the Receive FIFO Level register (RXLVL), return a single unsigned integer
+ // of nr of characters in the RX FIFO, bit 6:0, 7 not used, set to zero
+ // value from 0 (0x00) to 64 (0x40) Only display if RX FIFO has data
+ void RX_fifo_lvl(){
+ UART_ReadRegister(REG_RXLV);
+ FIFO_RX_LEVEL = _inbuffer[0];
+ #ifdef DIAG_I2CDFplayer
+ if (FIFO_RX_LEVEL > 0){
+ DIAG(F("SC16IS752: At I2C: %s, UART channel: 0x%x, RX FIFO Level: 0d%d"), _I2CAddress.toString(), _UART_CH, _inbuffer[0]);
+ }
+ #endif
+ }
+
+ // Read the Tranmit FIFO Level register (TXLVL), return a single unsigned integer
+ // of nr characters free in the TX FIFO, bit 6:0, 7 not used, set to zero
+ // value from 0 (0x00) to 64 (0x40)
+ //
+ void TX_fifo_lvl(){
+ UART_ReadRegister(REG_TXLV);
+ FIFO_TX_LEVEL = _inbuffer[0];
+ #ifdef DIAG_I2CDFplayer
+ DIAG(F("SC16IS752: At I2C: %s, UART channel: 0x%x, TX FIFO Level: 0d%d"), _I2CAddress.toString(), _UART_CH, FIFO_TX_LEVEL);
+ #endif
+ }
+
+ // Read from RX FIFO, we know the register REG_RHR
+ void ReceiveI2CData(){
+ //_inbuffer[0] = 0x00;
+ _outbuffer[0] = REG_RHR << 3 | _UART_CH << 1;
+ //I2CManager.read(_I2CAddress, _inbuffer, FIFO_RX_LEVEL, _outbuffer, 1, &_rb); // inbuffer[] has the data now
+ I2CManager.read(_I2CAddress, _inbuffer, FIFO_RX_LEVEL, _outbuffer, 1); // _inbuffer[] has the data now
+ #ifdef DIAG_I2CDFplayer_data
+ DIAG(F("SC16IS752: At I2C: %s, UART channel: 0x%x, RX FIFO Data"), _I2CAddress.toString(), _UART_CH);
+ for (int i = 0; i < sizeof _inbuffer; i++){
+ DIAG(F("SC16IS752: Data _inbuffer[0x%x]: 0x%x"), i, _inbuffer[i]);
+ }
+ #endif
+ }
+
+
+
+ //void UART_WriteRegister(I2CAddress _I2CAddress, uint8_t _UART_CH, uint8_t UART_REG, uint8_t Val, I2CRB &_rb){
+ void UART_WriteRegister(uint8_t UART_REG, uint8_t Val){
+ _outbuffer[0] = UART_REG << 3 | _UART_CH << 1;
+ _outbuffer[1] = Val;
+ #ifdef DIAG_I2CDFplayer_reg
+ DIAG(F("SC16IS752: Write register at I2C: %s, UART channel: 0x%x, Register: 0x%x, Data: 0b%b"), _I2CAddress.toString(), _UART_CH, UART_REG, _outbuffer[1]);
+ #endif
+ I2CManager.write(_I2CAddress, _outbuffer, 2);
+ }
+
+
+ void UART_ReadRegister(uint8_t UART_REG){
+ _outbuffer[0] = UART_REG << 3 | _UART_CH << 1; // _outbuffer[0] has now UART_REG and UART_CH
+ I2CManager.read(_I2CAddress, _inbuffer, 1, _outbuffer, 1);
+ // _inbuffer has the REG data
+ #ifdef DIAG_I2CDFplayer_reg
+ DIAG(F("SC16IS752: Read register at I2C: %s, UART channel: 0x%x, Register: 0x%x, Data: 0b%b"), _I2CAddress.toString(), _UART_CH, UART_REG, _inbuffer[0]);
+ #endif
+ }
+
+// SC16IS752 General register set (from the datasheet)
+enum : uint8_t{
+ REG_RHR = 0x00, // FIFO Read
+ REG_THR = 0x00, // FIFO Write
+ REG_IER = 0x01, // Interrupt Enable Register R/W
+ REG_FCR = 0x02, // FIFO Control Register Write
+ REG_IIR = 0x02, // Interrupt Identification Register Read
+ REG_LCR = 0x03, // Line Control Register R/W
+ REG_MCR = 0x04, // Modem Control Register R/W
+ REG_LSR = 0x05, // Line Status Register Read
+ REG_MSR = 0x06, // Modem Status Register Read
+ REG_SPR = 0x07, // Scratchpad Register R/W
+ REG_TCR = 0x06, // Transmission Control Register R/W
+ REG_TLR = 0x07, // Trigger Level Register R/W
+ REG_TXLV = 0x08, // Transmitter FIFO Level register Read
+ REG_RXLV = 0x09, // Receiver FIFO Level register Read
+ REG_IODIR = 0x0A, // Programmable I/O pins Direction register R/W
+ REG_IOSTATE = 0x0B, // Programmable I/O pins State register R/W
+ REG_IOINTENA = 0x0C, // I/O Interrupt Enable register R/W
+ REG_IOCONTROL = 0x0E, // I/O Control register R/W
+ REG_EFCR = 0x0F, // Extra Features Control Register R/W
+ };
+
+// SC16IS752 Special register set
+enum : uint8_t{
+ REG_DLL = 0x00, // Division registers R/W
+ REG_DLH = 0x01, // Division registers R/W
+ };
+
+// SC16IS752 Enhanced regiter set
+enum : uint8_t{
+ REG_EFR = 0X02, // Enhanced Features Register R/W
+ REG_XON1 = 0x04, // R/W
+ REG_XON2 = 0x05, // R/W
+ REG_XOFF1 = 0x06, // R/W
+ REG_XOFF2 = 0x07, // R/W
+ };
+
+};
+
+#endif // IO_I2CDFPlayer_h
diff --git a/IO_Template.h b/IO_Template.h
new file mode 100644
index 0000000..adc545a
--- /dev/null
+++ b/IO_Template.h
@@ -0,0 +1,69 @@
+
+/*
+* Creation - a create() function and constructor are required;
+* Initialisation - a _begin() function is written (optional);
+* Background operations - a _loop() function is written (optional);
+* Operations - you can optionally supply any of _write() (digital) function, _writeAnalogue() function, _read() (digital) function and _readAnalogue() function.
+*
+*
+*
+*
+*
+*
+*/
+
+
+#ifndef IO_MYDEVICE_H
+#define IO_MYDEVICE_H
+
+#include "IODevice.h"
+#include "DIAG.h" // for DIAG calls
+
+class MyDevice: public IODevice {
+public:
+ // Constructor
+ MyDevice(VPIN firstVpin, int nPins) {
+ _firstVpin = firstVpin;
+ _nPins = min(nPins,16);
+ // Other object initialisation here
+ // ...
+ addDevice(this);
+ }
+ static void create(VPIN firstVpin, int nPins, uint8_t i2cAddress) {
+ new MyDevice(firstVpin, nPins);
+ }
+private:
+ void _begin() override {
+ // Initialise device
+ // ...
+ }
+ void _loop(unsigned long currentMicros) override {
+ // Regular operations, e.g. acquire data
+ // ...
+ delayUntil(currentMicros + 10*1000UL); // 10ms till next entry
+ }
+ int _readAnalogue(VPIN vpin) override {
+ // Return acquired data value, e.g.
+ int pin = vpin - _firstVpin;
+ return _value[pin];
+ }
+ int _read(VPIN vpin) override {
+ // Return acquired data value, e.g.
+ int pin = vpin - _firstVpin;
+ return _value[pin];
+ }
+ void write(VPIN vpin, int value) override {
+ // Do something with value , e.g. write to device.
+ // ...
+ }
+ void writeAnalogue(VPIN vpin, int value) override {
+ // Do something with value, e.g. write to device.
+ // ...
+ }
+ void _display() override {
+ DIAG(F("MyDevice Configured on Vpins:%d-%d %S"), _firstVpin, _firstVpin+_nPins-1,
+ _deviceState == DEVSTATE_FAILED ? F("OFFLINE") : F(""));
+ }
+ uint16_t _value[16];
+};
+#endif // IO_MYDEVICE_H
\ No newline at end of file
From 44ce1c0cfa17f6c5b2e3c2b7d6d6f589513b9cc5 Mon Sep 17 00:00:00 2001
From: kempe63 <78020007+kempe63@users.noreply.github.com>
Date: Sun, 12 Nov 2023 12:14:02 +0000
Subject: [PATCH 08/51] Preliminary working version of I2CDFPlayer
Working, need some endurance testing and testing at scale
---
IO_I2CDFPlayer.h | 124 +++++++++++++++++++++++++++--------------------
1 file changed, 71 insertions(+), 53 deletions(-)
diff --git a/IO_I2CDFPlayer.h b/IO_I2CDFPlayer.h
index d4905a5..f0e4c09 100644
--- a/IO_I2CDFPlayer.h
+++ b/IO_I2CDFPlayer.h
@@ -100,6 +100,8 @@ private:
uint8_t _requestedVolumeLevel = MAXVOLUME;
uint8_t _currentVolume = MAXVOLUME;
int _requestedSong = -1; // -1=none, 0=stop, >0=file number
+ uint8_t _repeat;
+ uint8_t _previousCmd = true;
// SC16IS752 defines
I2CAddress _I2CAddress;
I2CRB _rb;
@@ -113,6 +115,7 @@ private:
uint8_t PRESCALER = 0x01; // Value MCR bit 7
uint8_t TEMP_REG_VAL = 0x00;
uint8_t FIFO_RX_LEVEL = 0x00;
+ uint8_t RX_BUFFER = 0x00; // nr of bytes copied into _inbuffer
uint8_t FIFO_TX_LEVEL = 0x00;
uint8_t _outbuffer [11]; // DFPlayer command is 10 bytes + 1 byte register address & UART channel
uint8_t _inbuffer[10]; // expected DFPlayer return 10 bytes
@@ -128,9 +131,7 @@ public:
_I2CAddress = i2cAddress;
_UART_CH = UART_CH;
addDevice(this);
-
}
-
public:
static void create(VPIN firstVpin, int nPins, I2CAddress i2cAddress, uint8_t UART_CH) {
@@ -140,7 +141,7 @@ public:
void _begin() override {
// check if SC16IS752 exist first, initialize and then resume DFPlayer init via SC16IS752
I2CManager.begin();
- //I2CManager.setClock(1000000);
+ I2CManager.setClock(1000000);
if (I2CManager.exists(_I2CAddress)){
DIAG(F("SC16IS752 I2C:%s UART detected"), _I2CAddress.toString());
Init_SC16IS752(); // Initialize UART
@@ -158,45 +159,71 @@ public:
_deviceState = DEVSTATE_INITIALISING;
sendPacket(0x42);
_timeoutTime = micros() + 5000000UL; // 5 second timeout
- //_timeoutTime = micros() + 10000000UL; // 5 second timeout
- _awaitingResponse = true;
+ _awaitingResponse = true;
}
void _loop(unsigned long currentMicros) override {
// Read responses from device
-
- processIncoming();
- // Check if a command sent to device has timed out. Allow 0.5 second for response
- if (_awaitingResponse && (int32_t)(currentMicros - _timeoutTime) > 0) {
- DIAG(F("I2CDFPlayer:%s, DFPlayer not responding on UART channel: 0x%x"), _I2CAddress.toString(), _UART_CH);
- _deviceState = DEVSTATE_FAILED;
- _awaitingResponse = false;
- _playing = false;
+ uint8_t status = _rb.status;
+ if (status == I2C_STATUS_PENDING) return; // Busy, so don't do anything
+ if (status == I2C_STATUS_OK) {
+ processIncoming(currentMicros);
+ // Check if a command sent to device has timed out. Allow 0.5 second for response
+ if (_awaitingResponse && (int32_t)(currentMicros - _timeoutTime) > 0) {
+ DIAG(F("I2CDFPlayer:%s, DFPlayer not responding on UART channel: 0x%x"), _I2CAddress.toString(), _UART_CH);
+ _deviceState = DEVSTATE_FAILED;
+ _awaitingResponse = false;
+ _playing = false;
+ }
}
- // Send any commands that need to go.
- processOutgoing(currentMicros);
- delayUntil(currentMicros + 10000); // Only enter every 10ms
+ status = _rb.status;
+ if (status == I2C_STATUS_PENDING) return; // Busy, try next time
+ if (status == I2C_STATUS_OK) {
+ // Send any commands that need to go.
+ processOutgoing(currentMicros);
+ }
+ delayUntil(currentMicros + 10000); // Only enter every 10ms
}
// Check for incoming data on _serial, and update busy flag and other state accordingly
- void processIncoming() {
+ void processIncoming(unsigned long currentMicros) {
// Expected message is in the form "7E FF 06 3D xx xx xx xx xx EF"
RX_fifo_lvl();
if (FIFO_RX_LEVEL >= 10) {
#ifdef DIAG_I2CDFplayer
- DIAG(F("I2CDFPlayer: %s Retrieving data from RX Fifo on UART_CH: 0x%x"),_I2CAddress.toString(), _UART_CH);
+ DIAG(F("I2CDFPlayer: %s Retrieving data from RX Fifo on UART_CH: 0x%x FIFO_RX_LEVEL: %d"),_I2CAddress.toString(), _UART_CH, FIFO_RX_LEVEL);
#endif
- ReceiveI2CData();
+ _outbuffer[0] = REG_RHR << 3 | _UART_CH << 1;
+ // Only copy 10 bytes from RX FIFO, there maybe additional partial return data after a track is finished playing in the RX FIFO
+ I2CManager.read(_I2CAddress, _inbuffer, 10, _outbuffer, 1); // inbuffer[] has the data now
+ //delayUntil(currentMicros + 10000); // Allow time to get the data
+ RX_BUFFER = 10; // We have copied 10 bytes from RX FIFO to _inbuffer
+ #ifdef DIAG_I2CDFplayer_data
+ DIAG(F("SC16IS752: At I2C: %s, UART channel: 0x%x, RX FIFO Data"), _I2CAddress.toString(), _UART_CH);
+ for (int i = 0; i < sizeof _inbuffer; i++){
+ DIAG(F("SC16IS752: Data _inbuffer[0x%x]: 0x%x"), i, _inbuffer[i]);
+ }
+ #endif
} else {
- return; // No data or not enough data in rx fifo, check again next time around
+ FIFO_RX_LEVEL = 0; //set to 0, we'll read a fresh FIFO_RX_LEVEL next time
+ return; // No data or not enough data in rx fifo, check again next time around
}
+/*
+ #ifdef DIAG_I2CDFplayer
+ if (FIFO_RX_LEVEL > 10) {
+ DIAG(F("I2CDFPlayer: %s FIFO_RX_LEVEL: %d"),_I2CAddress.toString(), FIFO_RX_LEVEL);
+ }
+ #endif
+*/
+
bool ok = false;
- while (FIFO_RX_LEVEL != 0) {
+ //DIAG(F("I2CDFPlayer: RX_BUFFER: %d"), RX_BUFFER);
+ while (RX_BUFFER != 0) {
int c = _inbuffer[_inputIndex]; // Start at 0, increment to FIFO_RX_LEVEL
switch (_inputIndex) {
case 0:
@@ -214,6 +241,7 @@ public:
break;
case 6:
switch (_recvCMD) {
+ DIAG(F("I2CDFPlayer: %s, _recvCMD: 0x%x _awaitingResponse: 0x0%x"),_I2CAddress.toString(), _recvCMD, _awaitingResponse);
case 0x42:
// Response to status query
_playing = (c != 0);
@@ -229,7 +257,7 @@ public:
}
_awaitingResponse = false;
break;
- case 0x3d:
+ case 0x3d:
// End of play
if (_playing) {
#ifdef DIAG_IO
@@ -259,12 +287,13 @@ public:
}
if (ok){
_inputIndex++; // character as expected, so increment index
- FIFO_RX_LEVEL --; // Decrease FIFO_RX_LEVEL with each character read from _inbuffer[_inputIndex]
+ RX_BUFFER --; // Decrease FIFO_RX_LEVEL with each character read from _inbuffer[_inputIndex]
} else {
_inputIndex = 0; // otherwise reset.
- FIFO_RX_LEVEL = 0;
+ RX_BUFFER = 0;
}
}
+ RX_BUFFER = 0; //Set to 0, we'll read a new RX FIFO level again
}
// Send any commands that need to be sent
@@ -273,7 +302,7 @@ public:
// execute one. Testing has indicated that a delay of 100ms or more is required
// between successive commands to get reliable operation.
// If 100ms has elapsed since the last thing sent, then check if there's some output to do.
- if (((int32_t)currentMicros - _commandSendTime) > 100000) {
+ if (((int32_t)currentMicros - _commandSendTime) > 100000) {
if (_currentVolume > _requestedVolumeLevel) {
// Change volume before changing song if volume is reducing.
_currentVolume = _requestedVolumeLevel;
@@ -281,7 +310,7 @@ public:
} else if (_requestedSong > 0) {
// Change song
sendPacket(0x03, _requestedSong);
- _requestedSong = -1;
+ _requestedSong = -1;
} else if (_requestedSong == 0) {
sendPacket(0x16); // Stop playing
_requestedSong = -1;
@@ -298,7 +327,7 @@ public:
_awaitingResponse = true;
}
}
- }
+ }
}
// Write with value 1 starts playing a song. The relative pin number is the file number.
@@ -328,23 +357,27 @@ public:
// If value is zero, the player stops playing.
// WriteAnalogue on second pin sets the output volume.
//
- void _writeAnalogue(VPIN vpin, int value, uint8_t volume=0, uint16_t=0) override {
+ //void _writeAnalogue(VPIN vpin, int value, uint8_t volume=0, uint16_t=0) override {
+ void _writeAnalogue(VPIN vpin, int value, uint8_t volume=0, uint16_t cmd=0) override {
if (_deviceState == DEVSTATE_FAILED) return;
uint8_t pin = vpin - _firstVpin;
-
#ifdef DIAG_IO
- DIAG(F("I2CDFPlayer: VPIN:%u FileNo:%d Volume:%d"), vpin, value, volume);
+ DIAG(F("I2CDFPlayer: VPIN:%u FileNo:%d Volume:%d Repeat:0x0%x"), vpin, value, volume, cmd);
#endif
-
// Validate parameter.
if (volume > MAXVOLUME) volume = MAXVOLUME;
if (pin == 0) {
- // Play track
+ // Play track
if (value > 0) {
if (volume > 0)
_requestedVolumeLevel = volume;
_requestedSong = value;
+ if (cmd = 1){ // check for Repeat playback of song
+ _repeat = true;
+ } else {
+ _repeat = false;
+ }
_playing = true;
} else {
_requestedSong = 0; // stop playing
@@ -410,8 +443,8 @@ private:
TX_fifo_lvl();
if(FIFO_TX_LEVEL > 0){ //FIFO is empty
- //I2CManager.write(_I2CAddress, _outbuffer, sizeof(_outbuffer), &_rb);
- I2CManager.write(_I2CAddress, _outbuffer, sizeof(_outbuffer));
+ I2CManager.write(_I2CAddress, _outbuffer, sizeof(_outbuffer), &_rb);
+ //I2CManager.write(_I2CAddress, _outbuffer, sizeof(_outbuffer));
#ifdef DIAG_I2CDFplayer
DIAG(F("SC16IS752: I2C: %s data transmit complete on UART: 0x%x"), _I2CAddress.toString(), _UART_CH);
#endif
@@ -472,13 +505,13 @@ private:
TEMP_REG_VAL = _inbuffer[0] & 0x7F; // Disable Divisor latch enabled bit
UART_WriteRegister(REG_LCR, TEMP_REG_VAL); // Divisor latch disabled
- uint8_t status = _rb.wait();
+ uint8_t status = _rb.status;
if (status != I2C_STATUS_OK) {
DIAG(F("SC16IS752: I2C: %s failed %S"), _I2CAddress.toString(), I2CManager.getErrorMessage(status));
_deviceState = DEVSTATE_FAILED;
} else {
#ifdef DIAG_IO
- DIAG(F("SC16IS752: I2C: %s, _deviceState == I2C_STATUS_OK"), _I2CAddress.toString());
+ DIAG(F("SC16IS752: I2C: %s, _deviceState: %S"), _I2CAddress.toString(), I2CManager.getErrorMessage(status));
#endif
_deviceState = DEVSTATE_NORMAL; // If I2C state is OK, then proceed to initialize DFPlayer
}
@@ -493,7 +526,7 @@ private:
FIFO_RX_LEVEL = _inbuffer[0];
#ifdef DIAG_I2CDFplayer
if (FIFO_RX_LEVEL > 0){
- DIAG(F("SC16IS752: At I2C: %s, UART channel: 0x%x, RX FIFO Level: 0d%d"), _I2CAddress.toString(), _UART_CH, _inbuffer[0]);
+ // DIAG(F("SC16IS752: At I2C: %s, UART channel: 0x%x, FIFO_RX_LEVEL: 0d%d"), _I2CAddress.toString(), _UART_CH, _inbuffer[0]);
}
#endif
}
@@ -506,25 +539,10 @@ private:
UART_ReadRegister(REG_TXLV);
FIFO_TX_LEVEL = _inbuffer[0];
#ifdef DIAG_I2CDFplayer
- DIAG(F("SC16IS752: At I2C: %s, UART channel: 0x%x, TX FIFO Level: 0d%d"), _I2CAddress.toString(), _UART_CH, FIFO_TX_LEVEL);
+ // DIAG(F("SC16IS752: At I2C: %s, UART channel: 0x%x, FIFO_TX_LEVEL: 0d%d"), _I2CAddress.toString(), _UART_CH, FIFO_TX_LEVEL);
#endif
}
- // Read from RX FIFO, we know the register REG_RHR
- void ReceiveI2CData(){
- //_inbuffer[0] = 0x00;
- _outbuffer[0] = REG_RHR << 3 | _UART_CH << 1;
- //I2CManager.read(_I2CAddress, _inbuffer, FIFO_RX_LEVEL, _outbuffer, 1, &_rb); // inbuffer[] has the data now
- I2CManager.read(_I2CAddress, _inbuffer, FIFO_RX_LEVEL, _outbuffer, 1); // _inbuffer[] has the data now
- #ifdef DIAG_I2CDFplayer_data
- DIAG(F("SC16IS752: At I2C: %s, UART channel: 0x%x, RX FIFO Data"), _I2CAddress.toString(), _UART_CH);
- for (int i = 0; i < sizeof _inbuffer; i++){
- DIAG(F("SC16IS752: Data _inbuffer[0x%x]: 0x%x"), i, _inbuffer[i]);
- }
- #endif
- }
-
-
//void UART_WriteRegister(I2CAddress _I2CAddress, uint8_t _UART_CH, uint8_t UART_REG, uint8_t Val, I2CRB &_rb){
void UART_WriteRegister(uint8_t UART_REG, uint8_t Val){
From 5e0cf8eb74f485f87403fee211ab442b1f097322 Mon Sep 17 00:00:00 2001
From: kempe63 <78020007+kempe63@users.noreply.github.com>
Date: Sun, 12 Nov 2023 20:02:01 +0000
Subject: [PATCH 09/51] Update myHal.cpp_example.txt
Added IO_I2CDFPlayer example
---
myHal.cpp_example.txt | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/myHal.cpp_example.txt b/myHal.cpp_example.txt
index 5533554..6363674 100644
--- a/myHal.cpp_example.txt
+++ b/myHal.cpp_example.txt
@@ -234,6 +234,23 @@ void halSetup() {
// DFPlayer::create(10000, 10, Serial1);
+ //=======================================================================
+ // Play mp3 files from a Micro-SD card, using a DFPlayer MP3 Module on a SC16IS752 I2C Dual UART
+ //=======================================================================
+ // DFPlayer via NXP SC16IS752 I2C Dual UART. Each device has 2 UARTs on a single I2C address
+ // Total nr of devices on an I2C bus is 16, with 2 UARTs on each address making a total of 32 UARTs per I2C bus
+ // I2C address range 0x48 - 0x57
+ // I2CDFPlayer::create(1st vPin,vPins, I2C address, UART ch);
+
+ // I2CDFPlayer::create(10000, 10, 0x48, 0);
+ // I2CDFPlayer::create(10010, 10, 0x48, 1);
+
+ // Multiplexer example
+ // I2CDFPlayer::create(10020, 10, {I2CMux_0, SubBus_0, 0x50}, 0);
+
+
+
+
//=======================================================================
// 16-pad capacitative touch key pad based on TP229 IC.
//=======================================================================
From b8e0185540fd49f4e08a420a9cf5c802ff446447 Mon Sep 17 00:00:00 2001
From: kempe63 <78020007+kempe63@users.noreply.github.com>
Date: Sun, 12 Nov 2023 20:03:41 +0000
Subject: [PATCH 10/51] Update IO_I2CDFPlayer.h
Further tweaks and cleanup
---
IO_I2CDFPlayer.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/IO_I2CDFPlayer.h b/IO_I2CDFPlayer.h
index f0e4c09..bc98e17 100644
--- a/IO_I2CDFPlayer.h
+++ b/IO_I2CDFPlayer.h
@@ -120,7 +120,7 @@ private:
uint8_t _outbuffer [11]; // DFPlayer command is 10 bytes + 1 byte register address & UART channel
uint8_t _inbuffer[10]; // expected DFPlayer return 10 bytes
- unsigned long SC16IS752_XTAL_FREQ = 1843200; // May need to change xtal frequency to 14.7456Mhz (14745600) to allow for higher baud rates
+ unsigned long SC16IS752_XTAL_FREQ = 1843200; // May need to change oscillator frequency to 14.7456Mhz (14745600) to allow for higher baud rates
//unsigned long SC16IS752_XTAL_FREQ = 14745600; // Support for higher baud rates
public:
From d703c42d79559d03b5eb967b7aa55eca19cdfd97 Mon Sep 17 00:00:00 2001
From: pmantoine
Date: Thu, 30 Nov 2023 16:16:44 +0800
Subject: [PATCH 11/51] STM32 Ethernet fix lib_deps
---
platformio.ini | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)
diff --git a/platformio.ini b/platformio.ini
index f13d4f5..93804de 100644
--- a/platformio.ini
+++ b/platformio.ini
@@ -30,7 +30,6 @@ include_dir = .
[env]
build_flags = -Wall -Wextra
-; monitor_filters = time
[env:samd21-dev-usb]
platform = atmelsam
@@ -250,7 +249,6 @@ platform = ststm32
board = nucleo_f429zi
framework = arduino
lib_deps = ${env.lib_deps}
- arduino-libraries/Ethernet @ ^2.0.1
stm32duino/STM32Ethernet @ ^1.3.0
stm32duino/STM32duino LwIP @ ^2.1.2
build_flags = -std=c++17 -Os -g2 -Wunused-variable
@@ -263,10 +261,9 @@ upload_protocol = stlink
;
[env:Nucleo-F439ZI]
platform = ststm32
-board = nucleo_f429zi
+board = nucleo_f439zi
framework = arduino
lib_deps = ${env.lib_deps}
- arduino-libraries/Ethernet @ ^2.0.1
stm32duino/STM32Ethernet @ ^1.3.0
stm32duino/STM32duino LwIP @ ^2.1.2
build_flags = -std=c++17 -Os -g2 -Wunused-variable
From 6fab3a5dd531efd458fee4206bf5ae87b21a079a Mon Sep 17 00:00:00 2001
From: Ash-4
Date: Fri, 8 Dec 2023 16:27:15 -0600
Subject: [PATCH 12/51] Nucleo-144b Serial LCD lines
---
I2CManager_STM32.h | 3 +--
WifiInterface.cpp | 5 +++--
2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/I2CManager_STM32.h b/I2CManager_STM32.h
index f386959..9350d14 100644
--- a/I2CManager_STM32.h
+++ b/I2CManager_STM32.h
@@ -39,8 +39,7 @@
#if defined(I2C_USE_INTERRUPTS) && defined(ARDUINO_ARCH_STM32)
#if defined(ARDUINO_NUCLEO_F401RE) || defined(ARDUINO_NUCLEO_F411RE) || defined(ARDUINO_NUCLEO_F446RE) \
|| defined(ARDUINO_NUCLEO_F412ZG) || defined(ARDUINO_NUCLEO_F413ZH) \
- || defined(ARDUINO_NUCLEO_F429ZI) || defined(ARDUINO_NUCLEO_F439ZI) \
- || defined(ARDUINO_NUCLEO_F446ZE)
+ || defined(ARDUINO_NUCLEO_F429ZI) || defined(ARDUINO_NUCLEO_F439ZI) || defined(ARDUINO_NUCLEO_F446ZE)
// Assume I2C1 for now - default I2C bus on Nucleo-F411RE and likely all Nucleo-64
// and Nucleo-144 variants
I2C_TypeDef *s = I2C1;
diff --git a/WifiInterface.cpp b/WifiInterface.cpp
index 8b2251a..d08ce29 100644
--- a/WifiInterface.cpp
+++ b/WifiInterface.cpp
@@ -68,9 +68,10 @@ Stream * WifiInterface::wifiStream;
#define NUM_SERIAL 3
#define SERIAL1 Serial3
#define SERIAL3 Serial5
-#elif defined(ARDUINO_NUCLEO_F413ZH) || defined(ARDUINO_NUCLEO_F429ZI) || defined(ARDUINO_NUCLEO_F446ZE) || defined(ARDUINO_NUCLEO_F412ZG)
-#define NUM_SERIAL 2
+#elif defined(ARDUINO_NUCLEO_F413ZH) || defined(ARDUINO_NUCLEO_F429ZI) || defined(ARDUINO_NUCLEO_F439ZI) || defined(ARDUINO_NUCLEO_F446ZE) || defined(ARDUINO_NUCLEO_F412ZG)
+#define NUM_SERIAL 3
#define SERIAL1 Serial6
+#define SERIAL3 Serial2
#else
#warning This variant of Nucleo not yet explicitly supported
#endif
From 330a85ec172b5c36db68e2014b51c49813d2eecc Mon Sep 17 00:00:00 2001
From: Ash-4 <81280775+Ash-4@users.noreply.github.com>
Date: Wed, 13 Dec 2023 16:12:03 -0600
Subject: [PATCH 13/51] version updated 5.2.15ethC
---
version.h | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/version.h b/version.h
index b74cbd2..8895dc2 100644
--- a/version.h
+++ b/version.h
@@ -3,9 +3,11 @@
#include "StringFormatter.h"
-#define VERSION "5.2.15"
+#define VERSION "5.2.15ethC"
// 5.2.15 - move call to CommandDistributor::broadcastPower() into the TrackManager::setTrackPower(*) functions
// - add repeats to function packets that are not reminded in accordance with accessory packets
+// 5.2.14eth - Initial ethernet code for STM32F429ZI and F439ZI boards
+// - CMRI RS485 connection
// 5.2.14 - Reminder window DCC packet optimization
// - Optional #define DISABLE_FUNCTION_REMINDERS
// 5.2.13 - EXRAIL STEALTH
From 32ecbbe147b5dc608ded89525ac8fd74d1db0458 Mon Sep 17 00:00:00 2001
From: kempe63 <78020007+kempe63@users.noreply.github.com>
Date: Wed, 27 Dec 2023 17:31:31 +0000
Subject: [PATCH 14/51] Update IO_I2CDFPlayer.h
Implented more DFPlayer commands
---
IO_I2CDFPlayer.h | 223 ++++++++++++++++++++++++++++++++++++++++-------
1 file changed, 192 insertions(+), 31 deletions(-)
diff --git a/IO_I2CDFPlayer.h b/IO_I2CDFPlayer.h
index bc98e17..536dff5 100644
--- a/IO_I2CDFPlayer.h
+++ b/IO_I2CDFPlayer.h
@@ -1,4 +1,4 @@
-/*
+ /*
* © 2023, Neil McKechnie. All rights reserved.
*
* This file is part of DCC++EX API
@@ -87,6 +87,7 @@
//#define DIAG_I2CDFplayer
//#define DIAG_I2CDFplayer_data
//#define DIAG_I2CDFplayer_reg
+#define DIAG_I2CDFplayer_playing
class I2CDFPlayer : public IODevice {
private:
@@ -100,12 +101,13 @@ private:
uint8_t _requestedVolumeLevel = MAXVOLUME;
uint8_t _currentVolume = MAXVOLUME;
int _requestedSong = -1; // -1=none, 0=stop, >0=file number
- uint8_t _repeat;
+ bool _repeat = false; // audio file is repeat playing
uint8_t _previousCmd = true;
// SC16IS752 defines
I2CAddress _I2CAddress;
I2CRB _rb;
uint8_t _UART_CH;
+ uint8_t _audioMixer = 0x00; // Default no audio mixer installed
// Communication parameters for the DFPlayer are fixed at 8 bit, No parity, 1 stopbit
uint8_t WORD_LEN = 0x03; // Value LCR bit 0,1
uint8_t STOP_BIT = 0x00; // Value LCR bit 2
@@ -116,26 +118,43 @@ private:
uint8_t TEMP_REG_VAL = 0x00;
uint8_t FIFO_RX_LEVEL = 0x00;
uint8_t RX_BUFFER = 0x00; // nr of bytes copied into _inbuffer
- uint8_t FIFO_TX_LEVEL = 0x00;
+ uint8_t FIFO_TX_LEVEL = 0x00;
+ //uint8_t DFPlayerValue = NONE; // Values for enhanced commands
+ //uint8_t DFPlayerCmd = NONE; // Enhanced commands
+ bool _playCmd = false;
+ bool _volCmd = false;
+ bool _folderCmd = false;
+ uint8_t _requestedFolder = 0x01; // default to folder 01
+ uint8_t _currentFolder = 0x01; // default to folder 01
+ bool _repeatCmd = false;
+ bool _stopplayCmd = false;
+ bool _resetCmd = false;
+ bool _eqCmd = false;
+ uint8_t _requestedEQValue = NORMAL;
+ uint8_t _currentEQvalue = NORMAL; // start equalizer value
+ bool _daconCmd = false;
+
uint8_t _outbuffer [11]; // DFPlayer command is 10 bytes + 1 byte register address & UART channel
uint8_t _inbuffer[10]; // expected DFPlayer return 10 bytes
+
- unsigned long SC16IS752_XTAL_FREQ = 1843200; // May need to change oscillator frequency to 14.7456Mhz (14745600) to allow for higher baud rates
- //unsigned long SC16IS752_XTAL_FREQ = 14745600; // Support for higher baud rates
+ //unsigned long SC16IS752_XTAL_FREQ = 1843200; // May need to change oscillator frequency to 14.7456Mhz (14745600) to allow for higher baud rates
+ unsigned long SC16IS752_XTAL_FREQ = 14745600; // Support for higher baud rates
public:
// Constructor
- I2CDFPlayer(VPIN firstVpin, int nPins, I2CAddress i2cAddress, uint8_t UART_CH){
+ I2CDFPlayer(VPIN firstVpin, int nPins, I2CAddress i2cAddress, uint8_t UART_CH, uint8_t AM){
_firstVpin = firstVpin;
_nPins = nPins;
_I2CAddress = i2cAddress;
- _UART_CH = UART_CH;
+ _UART_CH = UART_CH;
+ _audioMixer = AM;
addDevice(this);
}
public:
- static void create(VPIN firstVpin, int nPins, I2CAddress i2cAddress, uint8_t UART_CH) {
- if (checkNoOverlap(firstVpin, nPins, i2cAddress)) new I2CDFPlayer(firstVpin, nPins, i2cAddress, UART_CH);
+ static void create(VPIN firstVpin, int nPins, I2CAddress i2cAddress, uint8_t UART_CH, uint8_t AM) {
+ if (checkNoOverlap(firstVpin, nPins, i2cAddress)) new I2CDFPlayer(firstVpin, nPins, i2cAddress, UART_CH, AM);
}
void _begin() override {
@@ -159,7 +178,7 @@ public:
_deviceState = DEVSTATE_INITIALISING;
sendPacket(0x42);
_timeoutTime = micros() + 5000000UL; // 5 second timeout
- _awaitingResponse = true;
+ _awaitingResponse = true;
}
@@ -296,28 +315,75 @@ public:
RX_BUFFER = 0; //Set to 0, we'll read a new RX FIFO level again
}
+ //sendPacket(0x1A,0x00,0x01); //Enable DAC
+
// Send any commands that need to be sent
void processOutgoing(unsigned long currentMicros) {
// When two commands are sent in quick succession, the device will often fail to
// execute one. Testing has indicated that a delay of 100ms or more is required
// between successive commands to get reliable operation.
// If 100ms has elapsed since the last thing sent, then check if there's some output to do.
- if (((int32_t)currentMicros - _commandSendTime) > 100000) {
+ if (((int32_t)currentMicros - _commandSendTime) > 100000) {
if (_currentVolume > _requestedVolumeLevel) {
// Change volume before changing song if volume is reducing.
_currentVolume = _requestedVolumeLevel;
- sendPacket(0x06, _currentVolume);
- } else if (_requestedSong > 0) {
+ sendPacket(0x06, 0x00, _currentVolume);
+ } else if (_playCmd == true) {
// Change song
- sendPacket(0x03, _requestedSong);
- _requestedSong = -1;
- } else if (_requestedSong == 0) {
- sendPacket(0x16); // Stop playing
+ if (_requestedSong != -1) {
+ #ifdef DIAG_I2CDFplayer_playing
+ DIAG(F("I2CDFPlayer: _requestedVolumeLevel: %u, _requestedSong: %u, _playCmd: 0x%x"), _requestedVolumeLevel, _requestedSong, _playCmd);
+ #endif
+ sendPacket(0x0F, 0x01, _requestedSong); // audio file in folder 01
+ _requestedSong = -1;
+ _playCmd = false;
+ }
+ } //else if (_requestedSong == 0) {
+ else if (_stopplayCmd == true) {
+ #ifdef DIAG_I2CDFplayer_playing
+ DIAG(F("I2CDFPlayer: Stop playing: _stopplayCmd: 0x%x"), _stopplayCmd);
+ #endif
+ sendPacket(0x16, 0x00, 0x00); // Stop playing
_requestedSong = -1;
+ _repeat = false; // reset repeat
+ _stopplayCmd = false;
+ } else if (_folderCmd == true) {
+ if (_currentFolder != _requestedFolder){
+ _currentFolder = _requestedFolder;
+ }
+ _folderCmd = false;
+ } else if (_repeatCmd == true) {
+ if(_repeat == false) { // No repeat play currently
+ #ifdef DIAG_I2CDFplayer_playing
+ DIAG(F("I2CDFPlayer: Repeat: _repeatCmd: 0x%x, _requestedSong: %d, _repeat: 0x0%x"), _repeatCmd, _requestedSong, _repeat);
+ #endif
+ sendPacket(0x08, 0x00, _requestedSong); // repeat playing audio file in root folder
+ _requestedSong = -1;
+ _repeat = true;
+ }
+ _repeatCmd= false;
+ } else if (_daconCmd == true) { // Always turn DAC on
+ #ifdef DIAG_I2CDFplayer_playing
+ DIAG(F("I2CDFPlayer: DACON: _daconCmd: 0x%x"), _daconCmd);
+ #endif
+ sendPacket(0x1A,0,0x00);
+ _daconCmd = false;
+ } else if (_eqCmd == true){ // Set Equalizer, values 0x00 - 0x05
+ if (_currentEQvalue != _requestedEQValue){
+ #ifdef DIAG_I2CDFplayer_playing
+ DIAG(F("I2CDFPlayer: EQ: _eqCmd: 0x%x, _currentEQvalue: 0x0%x, _requestedEQValue: 0x0%x"), _eqCmd, _currentEQvalue, _requestedEQValue);
+ #endif
+ _currentEQvalue = _requestedEQValue;
+ sendPacket(0x07,0x00,_currentEQvalue);
+ }
+ _eqCmd = false;
+ } else if ( _resetCmd == true){
+ sendPacket(0x0C,0,0);
+ _resetCmd = false;
} else if (_currentVolume < _requestedVolumeLevel) {
// Change volume after changing song if volume is increasing.
_currentVolume = _requestedVolumeLevel;
- sendPacket(0x06, _currentVolume);
+ sendPacket(0x06, 0x00, _currentVolume);
} else if ((int32_t)currentMicros - _commandSendTime > 1000000) {
// Poll device every second that other commands aren't being sent,
// to check if it's still connected and responding.
@@ -357,13 +423,15 @@ public:
// If value is zero, the player stops playing.
// WriteAnalogue on second pin sets the output volume.
//
+ // Currently all WrtiteAnalogue to be done on vpin 2, will move to vpin 0 when fully implemented
+ //
//void _writeAnalogue(VPIN vpin, int value, uint8_t volume=0, uint16_t=0) override {
void _writeAnalogue(VPIN vpin, int value, uint8_t volume=0, uint16_t cmd=0) override {
- if (_deviceState == DEVSTATE_FAILED) return;
- uint8_t pin = vpin - _firstVpin;
+ if (_deviceState == DEVSTATE_FAILED) return;
#ifdef DIAG_IO
- DIAG(F("I2CDFPlayer: VPIN:%u FileNo:%d Volume:%d Repeat:0x0%x"), vpin, value, volume, cmd);
+ DIAG(F("I2CDFPlayer: VPIN:%u FileNo:%d Volume:%d Command:0x%x"), vpin, value, volume, cmd);
#endif
+ uint8_t pin = vpin - _firstVpin;
// Validate parameter.
if (volume > MAXVOLUME) volume = MAXVOLUME;
@@ -372,12 +440,7 @@ public:
if (value > 0) {
if (volume > 0)
_requestedVolumeLevel = volume;
- _requestedSong = value;
- if (cmd = 1){ // check for Repeat playback of song
- _repeat = true;
- } else {
- _repeat = false;
- }
+ _requestedSong = value;
_playing = true;
} else {
_requestedSong = 0; // stop playing
@@ -386,7 +449,75 @@ public:
} else if (pin == 1) {
// Set volume (0-30)
_requestedVolumeLevel = value;
- }
+
+ } else if (pin == 2) { // Enhanced DFPlayer commands
+ // Read command and value
+ switch (cmd){
+ //case NONE:
+ // DFPlayerCmd = cmd;
+ // break;
+ case PLAY:
+ _playCmd = true;
+ //DFPlayerCmd = cmd;
+ _requestedSong = value;
+ _requestedVolumeLevel = volume;
+ _playing = true;
+ break;
+ case VOL:
+ _volCmd = true;
+ //DFPlayerCmd = cmd;
+ _requestedVolumeLevel = volume;
+ break;
+ case FOLDER:
+ _folderCmd = true;
+ if (volume <= 0 && volume > 99){ // Range checking
+ _requestedFolder = 0x01; // if outside range, default to folder 01
+ } else {
+ _requestedFolder = volume;
+ }
+ break;
+ case REPEATPLAY: // Need to check if _repeat == true, if so do nothing
+ if (_repeat == false) {
+ #ifdef DIAG_I2CDFplayer_playing
+ DIAG(F("I2CDFPlayer: WriteAnalog Repeat: _repeat: 0x0%x, value: %d _repeatCmd: 0x%x"), _repeat, value, _repeatCmd);
+ #endif
+ _repeatCmd = true;
+ _requestedSong = value;
+ _requestedVolumeLevel = volume;
+ _playing = true;
+ }
+ break;
+ case STOPPLAY:
+ _stopplayCmd = true;
+ //DFPlayerCmd = cmd;
+ break;
+ case EQ:
+ //DIAG(F("I2CDFPlayer: WriteAnalog EQ: cmd: 0x%x, EQ value: 0x%x"), cmd, volume);
+ _eqCmd = true;
+ //DFPlayerCmd = cmd;
+ if (volume <= NORMAL) { // to keep backward compatibility the volume parameter is used for values of the EQ cmd
+ _requestedEQValue = NORMAL;
+ //DFPlayerValue = NONE;
+ } else if (volume <= 0x05) { // Validate EQ parameters
+ _requestedEQValue = volume;
+ //DFPlayerValue = volume;
+ }
+ break;
+
+ case RESET:
+ _resetCmd = true;
+ //DFPlayerCmd = cmd;
+ break;
+ case DACON: // Works, but without the DACOFF command limited value, except when not relying on DFPlayer default to turn the DAC on
+ //DIAG(F("I2CDFPlayer: WrtieAnalog DACON: cmd: 0x%x"), cmd);
+ _daconCmd = true;
+ //DFPlayerCmd = 0x1A;
+ //DFPlayerValue = 0x00;
+ break;
+ default:
+ break;
+ }
+ }
}
// A read on any pin indicates whether the player is still playing.
@@ -411,7 +542,8 @@ private:
// 7~8 -> checksum = 0 - ( FF+06+0F+00+01+01 )
// 9 -> EF is end code
- void sendPacket(uint8_t command, uint16_t arg = 0)
+ //void sendPacket(uint8_t command, uint16_t arg = 0)
+ void sendPacket(uint8_t command, uint8_t arg1 = 0, uint8_t arg2 = 0)
{
FIFO_TX_LEVEL = 0; // Reset FIFO_TX_LEVEL
uint8_t out[] = {
@@ -420,8 +552,10 @@ private:
06,
command,
00,
- static_cast(arg >> 8),
- static_cast(arg & 0x00ff),
+ //static_cast(arg >> 8),
+ //static_cast(arg & 0x00ff),
+ arg1,
+ arg2,
00,
00,
0xEF };
@@ -493,6 +627,13 @@ private:
uint16_t _divisor = (SC16IS752_XTAL_FREQ / PRESCALER) / (BAUD_RATE * 16);
TEMP_REG_VAL = 0x08; // UART Software reset
UART_WriteRegister(REG_IOCONTROL, TEMP_REG_VAL);
+ TEMP_REG_VAL = 0x00; // Set pins to GPIO mode
+ UART_WriteRegister(REG_IOCONTROL, TEMP_REG_VAL);
+ TEMP_REG_VAL = 0xFF; //Set all pins as output
+ UART_WriteRegister(REG_IODIR, TEMP_REG_VAL);
+ TEMP_REG_VAL = 0x01; //Set initial value as high
+ //TEMP_REG_VAL = 0x00; //Set initial value as low
+ UART_WriteRegister(REG_IOSTATE, TEMP_REG_VAL);
TEMP_REG_VAL = 0x07; // Reset FIFO, clear RX & TX FIFO
UART_WriteRegister(REG_FCR, TEMP_REG_VAL);
TEMP_REG_VAL = 0x00; // Set MCR to all 0, includes Clock divisor
@@ -602,6 +743,26 @@ enum : uint8_t{
REG_XOFF2 = 0x07, // R/W
};
+// DFPlayer commands and values
+enum : uint8_t{
+ //NONE = 0x00, // redundant
+ PLAY = 0x0F,
+ VOL = 0x06,
+ FOLDER = 0x2B, // Not a DFPlayer command, used to set folder nr where audio file is
+ REPEATPLAY = 0x08,
+ STOPPLAY = 0x16,
+ EQ = 0x07, // Set equaliser, require parameter NORMAL, POP, ROCK, JAZZ, CLASSIC or BASS
+ RESET = 0x0C,
+ //DACOFF = 0x1A, // Require 3rd byte to 0x00 in processOutgoing()
+ DACON = 0x1A, // Not a DFLayer command,need to sent 0x1A and 3rd byte to 0x01 in processOutgoing()
+ NORMAL = 0x00, // Equalizer parameters
+ POP = 0x01,
+ ROCK = 0x02,
+ JAZZ = 0x03,
+ CLASSIC = 0x04,
+ BASS = 0x05,
+ };
+
};
#endif // IO_I2CDFPlayer_h
From 0feb2c74e7c217b4c4f5eb2011862c8727c7a123 Mon Sep 17 00:00:00 2001
From: kempe63 <78020007+kempe63@users.noreply.github.com>
Date: Fri, 29 Dec 2023 22:11:40 +0000
Subject: [PATCH 15/51] Update IO_I2CDFPlayer.h
Completed implementation of exposing most relevant DFPlayer commands
Fixed a problem with RX corrupted data from DFPlayer.
Added retry (currently max retry 3) after timeout and recovery
Note: Need to think of a more elegant way to recover from a reset command
---
IO_I2CDFPlayer.h | 135 +++++++++++++++++++++++++++++------------------
1 file changed, 83 insertions(+), 52 deletions(-)
diff --git a/IO_I2CDFPlayer.h b/IO_I2CDFPlayer.h
index 536dff5..1ea6c25 100644
--- a/IO_I2CDFPlayer.h
+++ b/IO_I2CDFPlayer.h
@@ -87,7 +87,7 @@
//#define DIAG_I2CDFplayer
//#define DIAG_I2CDFplayer_data
//#define DIAG_I2CDFplayer_reg
-#define DIAG_I2CDFplayer_playing
+//#define DIAG_I2CDFplayer_playing
class I2CDFPlayer : public IODevice {
private:
@@ -98,6 +98,8 @@ private:
unsigned long _timeoutTime;
uint8_t _recvCMD; // Last received command code byte
bool _awaitingResponse = false;
+ uint8_t RETRYCOUNT = 0x03;
+ uint8_t _retryCounter = RETRYCOUNT; // Max retries before timing out
uint8_t _requestedVolumeLevel = MAXVOLUME;
uint8_t _currentVolume = MAXVOLUME;
int _requestedSong = -1; // -1=none, 0=stop, >0=file number
@@ -107,7 +109,7 @@ private:
I2CAddress _I2CAddress;
I2CRB _rb;
uint8_t _UART_CH;
- uint8_t _audioMixer = 0x00; // Default no audio mixer installed
+ uint8_t _audioMixer = 0x01; // Default to output amplifier 1
// Communication parameters for the DFPlayer are fixed at 8 bit, No parity, 1 stopbit
uint8_t WORD_LEN = 0x03; // Value LCR bit 0,1
uint8_t STOP_BIT = 0x00; // Value LCR bit 2
@@ -136,11 +138,12 @@ private:
uint8_t _outbuffer [11]; // DFPlayer command is 10 bytes + 1 byte register address & UART channel
uint8_t _inbuffer[10]; // expected DFPlayer return 10 bytes
-
-
+
//unsigned long SC16IS752_XTAL_FREQ = 1843200; // May need to change oscillator frequency to 14.7456Mhz (14745600) to allow for higher baud rates
unsigned long SC16IS752_XTAL_FREQ = 14745600; // Support for higher baud rates
+ unsigned long test = 0;
+
public:
// Constructor
I2CDFPlayer(VPIN firstVpin, int nPins, I2CAddress i2cAddress, uint8_t UART_CH, uint8_t AM){
@@ -176,7 +179,7 @@ public:
// Now init DFPlayer
// Send a query to the device to see if it responds
_deviceState = DEVSTATE_INITIALISING;
- sendPacket(0x42);
+ sendPacket(0x42,0,0);
_timeoutTime = micros() + 5000000UL; // 5 second timeout
_awaitingResponse = true;
}
@@ -188,13 +191,26 @@ public:
if (status == I2C_STATUS_PENDING) return; // Busy, so don't do anything
if (status == I2C_STATUS_OK) {
processIncoming(currentMicros);
- // Check if a command sent to device has timed out. Allow 0.5 second for response
- if (_awaitingResponse && (int32_t)(currentMicros - _timeoutTime) > 0) {
- DIAG(F("I2CDFPlayer:%s, DFPlayer not responding on UART channel: 0x%x"), _I2CAddress.toString(), _UART_CH);
- _deviceState = DEVSTATE_FAILED;
- _awaitingResponse = false;
- _playing = false;
- }
+ // Check if a command sent to device has timed out. Allow 0.5 second for response
+ // added retry counter, sometimes we do not sent keep alive due to other commands sent to DFPlayer
+ if (_awaitingResponse && (int32_t)(currentMicros - _timeoutTime) > 0) { // timeout triggered
+ if(_retryCounter == 0){ // retry counter out of luck, must take the device to failed state
+ DIAG(F("I2CDFPlayer:%s, DFPlayer not responding on UART channel: 0x%x"), _I2CAddress.toString(), _UART_CH);
+ _deviceState = DEVSTATE_FAILED;
+ _awaitingResponse = false;
+ _playing = false;
+ _retryCounter = RETRYCOUNT;
+ } else { // timeout and retry protection and recovery of corrupt data frames from DFPlayer
+ DIAG(F("I2CDFPlayer: %s, DFPlayer timout, retry counter: %d on UART channel: 0x%x"), _I2CAddress.toString(), _retryCounter, _UART_CH);
+ _timeoutTime = currentMicros + 5000000UL; // Timeout if no response within 5 seconds// reset timeout
+ _awaitingResponse = false; // trigger sending a keep alive 0x42 in processOutgoing()
+ _retryCounter --; // decrement retry counter
+ _resetCmd = true; // queue a DFPlayer reset
+ _currentVolume = MAXVOLUME; // Resetting the DFPlayer makes the volume go to default i.e. MAXVOLUME
+ //sendPacket(0x0C,0,0); // Reset DFPlayer
+ resetRX_fifo(); // reset the RX fifo as it maybe poisoned
+ }
+ }
}
status = _rb.status;
@@ -232,13 +248,6 @@ public:
return; // No data or not enough data in rx fifo, check again next time around
}
-/*
- #ifdef DIAG_I2CDFplayer
- if (FIFO_RX_LEVEL > 10) {
- DIAG(F("I2CDFPlayer: %s FIFO_RX_LEVEL: %d"),_I2CAddress.toString(), FIFO_RX_LEVEL);
- }
- #endif
-*/
bool ok = false;
//DIAG(F("I2CDFPlayer: RX_BUFFER: %d"), RX_BUFFER);
@@ -260,7 +269,7 @@ public:
break;
case 6:
switch (_recvCMD) {
- DIAG(F("I2CDFPlayer: %s, _recvCMD: 0x%x _awaitingResponse: 0x0%x"),_I2CAddress.toString(), _recvCMD, _awaitingResponse);
+ //DIAG(F("I2CDFPlayer: %s, _recvCMD: 0x%x _awaitingResponse: 0x0%x"),_I2CAddress.toString(), _recvCMD, _awaitingResponse);
case 0x42:
// Response to status query
_playing = (c != 0);
@@ -299,6 +308,7 @@ public:
case 9:
if (c==0xef) {
// Message finished
+ _retryCounter = RETRYCOUNT; // reset the retry counter as we have received a valid packet
}
break;
default:
@@ -315,7 +325,6 @@ public:
RX_BUFFER = 0; //Set to 0, we'll read a new RX FIFO level again
}
- //sendPacket(0x1A,0x00,0x01); //Enable DAC
// Send any commands that need to be sent
void processOutgoing(unsigned long currentMicros) {
@@ -323,8 +332,15 @@ public:
// execute one. Testing has indicated that a delay of 100ms or more is required
// between successive commands to get reliable operation.
// If 100ms has elapsed since the last thing sent, then check if there's some output to do.
- if (((int32_t)currentMicros - _commandSendTime) > 100000) {
- if (_currentVolume > _requestedVolumeLevel) {
+ if (((int32_t)currentMicros - _commandSendTime) > 100000) {
+ if ( _resetCmd == true){
+ sendPacket(0x0C,0,0);
+ _resetCmd = false;
+ return; // after reset do not execute more commands, wait for the next time giving the DFPlayer time to reset
+ // A more saver/elegant way is to wait for the 'SD card online' packet (7E FF 06 3F 00 00 02 xx xx EF)
+ // this indicate that the DFPlayer is ready.This may take between 500ms and 1500ms depending on the
+ // number of tracks on the SD card
+ } else if (_currentVolume > _requestedVolumeLevel) {
// Change volume before changing song if volume is reducing.
_currentVolume = _requestedVolumeLevel;
sendPacket(0x06, 0x00, _currentVolume);
@@ -332,9 +348,9 @@ public:
// Change song
if (_requestedSong != -1) {
#ifdef DIAG_I2CDFplayer_playing
- DIAG(F("I2CDFPlayer: _requestedVolumeLevel: %u, _requestedSong: %u, _playCmd: 0x%x"), _requestedVolumeLevel, _requestedSong, _playCmd);
+ DIAG(F("I2CDFPlayer: _requestedVolumeLevel: %u, _requestedSong: %u, _currentFolder: %u _playCmd: 0x%x"), _requestedVolumeLevel, _requestedSong, _currentFolder, _playCmd);
#endif
- sendPacket(0x0F, 0x01, _requestedSong); // audio file in folder 01
+ sendPacket(0x0F, _currentFolder, _requestedSong); // audio file in folder
_requestedSong = -1;
_playCmd = false;
}
@@ -348,6 +364,9 @@ public:
_repeat = false; // reset repeat
_stopplayCmd = false;
} else if (_folderCmd == true) {
+ #ifdef DIAG_I2CDFplayer_playing
+ DIAG(F("I2CDFPlayer: Folder: _folderCmd: 0x%x, _requestedFolder: %d"), _stopplayCmd, _requestedFolder);
+ #endif
if (_currentFolder != _requestedFolder){
_currentFolder = _requestedFolder;
}
@@ -377,9 +396,6 @@ public:
sendPacket(0x07,0x00,_currentEQvalue);
}
_eqCmd = false;
- } else if ( _resetCmd == true){
- sendPacket(0x0C,0,0);
- _resetCmd = false;
} else if (_currentVolume < _requestedVolumeLevel) {
// Change volume after changing song if volume is increasing.
_currentVolume = _requestedVolumeLevel;
@@ -387,8 +403,14 @@ public:
} else if ((int32_t)currentMicros - _commandSendTime > 1000000) {
// Poll device every second that other commands aren't being sent,
// to check if it's still connected and responding.
- sendPacket(0x42);
+ #ifdef DIAG_I2CDFplayer_playing
+ DIAG(F("I2CDFPlayer: Send keepalive") );
+ #endif
+ sendPacket(0x42,0,0);
if (!_awaitingResponse) {
+ #ifdef DIAG_I2CDFplayer_playing
+ DIAG(F("I2CDFPlayer: Send keepalive, _awaitingResponse: 0x0%x"), _awaitingResponse );
+ #endif
_timeoutTime = currentMicros + 5000000UL; // Timeout if no response within 5 seconds
_awaitingResponse = true;
}
@@ -457,15 +479,13 @@ public:
// DFPlayerCmd = cmd;
// break;
case PLAY:
- _playCmd = true;
- //DFPlayerCmd = cmd;
+ _playCmd = true;
_requestedSong = value;
_requestedVolumeLevel = volume;
_playing = true;
break;
case VOL:
- _volCmd = true;
- //DFPlayerCmd = cmd;
+ _volCmd = true;
_requestedVolumeLevel = volume;
break;
case FOLDER:
@@ -479,7 +499,7 @@ public:
case REPEATPLAY: // Need to check if _repeat == true, if so do nothing
if (_repeat == false) {
#ifdef DIAG_I2CDFplayer_playing
- DIAG(F("I2CDFPlayer: WriteAnalog Repeat: _repeat: 0x0%x, value: %d _repeatCmd: 0x%x"), _repeat, value, _repeatCmd);
+ DIAG(F("I2CDFPlayer: WriteAnalog Repeat: _repeat: 0x0%x, value: %d _repeatCmd: 0x%x"), _repeat, value, _repeatCmd);
#endif
_repeatCmd = true;
_requestedSong = value;
@@ -488,31 +508,27 @@ public:
}
break;
case STOPPLAY:
- _stopplayCmd = true;
- //DFPlayerCmd = cmd;
+ _stopplayCmd = true;
break;
case EQ:
- //DIAG(F("I2CDFPlayer: WriteAnalog EQ: cmd: 0x%x, EQ value: 0x%x"), cmd, volume);
- _eqCmd = true;
- //DFPlayerCmd = cmd;
+ #ifdef DIAG_I2CDFplayer_playing
+ DIAG(F("I2CDFPlayer: WriteAnalog EQ: cmd: 0x%x, EQ value: 0x%x"), cmd, volume);
+ #endif
+ _eqCmd = true;
if (volume <= NORMAL) { // to keep backward compatibility the volume parameter is used for values of the EQ cmd
- _requestedEQValue = NORMAL;
- //DFPlayerValue = NONE;
+ _requestedEQValue = NORMAL;
} else if (volume <= 0x05) { // Validate EQ parameters
- _requestedEQValue = volume;
- //DFPlayerValue = volume;
+ _requestedEQValue = volume;
}
- break;
-
+ break;
case RESET:
- _resetCmd = true;
- //DFPlayerCmd = cmd;
+ _resetCmd = true;
break;
case DACON: // Works, but without the DACOFF command limited value, except when not relying on DFPlayer default to turn the DAC on
- //DIAG(F("I2CDFPlayer: WrtieAnalog DACON: cmd: 0x%x"), cmd);
+ #ifdef DIAG_I2CDFplayer_playing
+ DIAG(F("I2CDFPlayer: WrtieAnalog DACON: cmd: 0x%x"), cmd);
+ #endif
_daconCmd = true;
- //DFPlayerCmd = 0x1A;
- //DFPlayerValue = 0x00;
break;
default:
break;
@@ -666,12 +682,27 @@ private:
UART_ReadRegister(REG_RXLV);
FIFO_RX_LEVEL = _inbuffer[0];
#ifdef DIAG_I2CDFplayer
- if (FIFO_RX_LEVEL > 0){
- // DIAG(F("SC16IS752: At I2C: %s, UART channel: 0x%x, FIFO_RX_LEVEL: 0d%d"), _I2CAddress.toString(), _UART_CH, _inbuffer[0]);
+ //if (FIFO_RX_LEVEL > 0){
+ if (FIFO_RX_LEVEL > 0 && FIFO_RX_LEVEL < 10){
+ DIAG(F("SC16IS752: At I2C: %s, UART channel: 0x%x, FIFO_RX_LEVEL: 0d%d"), _I2CAddress.toString(), _UART_CH, _inbuffer[0]);
}
#endif
}
+ // When a frame is transmitted from the DFPlayer to the serial port, and at the same time the CS is sending a 42 query
+ // the following two frames from the DFPlayer are corrupt. This result in the receive buffer being out of sync and the
+ // CS will complain and generate a timeout.
+ // The RX fifo has corrupt data and need to be flushed, this function does that
+ //
+ void resetRX_fifo(){
+ #ifdef DIAG_I2CDFplayer
+ DIAG(F("SC16IS752: At I2C: %s, UART channel: 0x%x, RX fifo reset"), _I2CAddress.toString(), _UART_CH);
+ #endif
+ TEMP_REG_VAL = 0x03; // Reset RX fifo
+ UART_WriteRegister(REG_FCR, TEMP_REG_VAL);
+ }
+
+
// Read the Tranmit FIFO Level register (TXLVL), return a single unsigned integer
// of nr characters free in the TX FIFO, bit 6:0, 7 not used, set to zero
// value from 0 (0x00) to 64 (0x40)
From c4689795010406fc623b45bccfef79b0d8de3e85 Mon Sep 17 00:00:00 2001
From: kempe63 <78020007+kempe63@users.noreply.github.com>
Date: Sat, 30 Dec 2023 20:51:35 +0000
Subject: [PATCH 16/51] Update IO_I2CDFPlayer.h
Cleaned up the code and tested again.
This is ready to be merged into the devel branch when documentation is finished
---
IO_I2CDFPlayer.h | 211 ++++++++++++++++-------------------------------
1 file changed, 73 insertions(+), 138 deletions(-)
diff --git a/IO_I2CDFPlayer.h b/IO_I2CDFPlayer.h
index 1ea6c25..3c0ae3c 100644
--- a/IO_I2CDFPlayer.h
+++ b/IO_I2CDFPlayer.h
@@ -20,60 +20,26 @@
/*
* DFPlayer is an MP3 player module with an SD card holder. It also has an integrated
* amplifier, so it only needs a power supply and a speaker.
- *
- * This driver allows the device to be controlled through IODevice::write() and
- * IODevice::writeAnalogue() calls.
- *
- * The driver is configured as follows:
- *
- * DFPlayer::create(firstVpin, nPins, Serialn);
- *
- * Where firstVpin is the first vpin reserved for reading the device,
- * nPins is the number of pins to be allocated (max 5)
- * and Serialn is the name of the Serial port connected to the DFPlayer (e.g. Serial1).
- *
- * Example:
- * In halSetup function within myHal.cpp:
- * DFPlayer::create(3500, 5, Serial1);
- * or in myAutomation.h:
- * HAL(DFPlayer, 3500, 5, Serial1)
- *
- * Writing an analogue value 1-2999 to the first pin (3500) will play the numbered file from the
- * SD card; e.g. a value of 1 will play the first file, 2 for the second file etc.
- * Writing an analogue value 0 to the first pin (3500) will stop the file playing;
- * Writing an analogue value 0-30 to the second pin (3501) will set the volume;
- * Writing a digital value of 1 to a pin will play the file corresponding to that pin, e.g.
- the first file will be played by setting pin 3500, the second by setting pin 3501 etc.;
- * Writing a digital value of 0 to any pin will stop the player;
- * Reading a digital value from any pin will return true(1) if the player is playing, false(0) otherwise.
- *
- * From EX-RAIL, the following commands may be used:
- * SET(3500) -- starts playing the first file (file 1) on the SD card
- * SET(3501) -- starts playing the second file (file 2) on the SD card
- * etc.
- * RESET(3500) -- stops all playing on the player
- * WAITFOR(3500) -- wait for the file currently being played by the player to complete
- * SERVO(3500,2,Instant) -- plays file 2 at current volume
- * SERVO(3501,20,Instant) -- Sets the volume to 20
- *
- * NB The DFPlayer's serial lines are not 5V safe, so connecting the Arduino TX directly
- * to the DFPlayer's RX terminal will cause lots of noise over the speaker, or worse.
- * A 1k resistor in series with the module's RX terminal will alleviate this.
- *
- * Files on the SD card are numbered according to their order in the directory on the
- * card (as listed by the DIR command in Windows). This may not match the order of the files
- * as displayed by Windows File Manager, which sorts the file names. It is suggested that
- * files be copied into an empty SDcard in the desired order, one at a time.
- *
- * The driver now polls the device for its current status every second. Should the device
- * fail to respond it will be marked off-line and its busy indicator cleared, to avoid
- * lock-ups in automation scripts that are executing for a WAITFOR().
- *
+ * This driver is a modified version of the IO_DFPlayer.h file
* *********************************************************************************************
+ *
* 2023, Added NXP SC16IS752 I2C Dual UART to enable the DFPlayer connection over the I2C bus
* The SC16IS752 has 64 bytes TX & RX FIFO buffer
- * First version without interrupts from I2C UART and only RX/TX are used, interrupts may not be needed as the RX Fifo holds the reply
- *
+ * First version without interrupts from I2C UART and only RX/TX are used, interrupts may not be
+ * needed as the RX Fifo holds the reply
+ *
+ * myHall.cpp configuration syntax:
+ *
+ * I2CDFPlayer::create(1st vPin, vPins, I2C address, UART ch, AM);
+ *
+ * Parameters:
+ * 1st vPin : First virtual pin that EX-Rail can control to play a sound, use PLAYSOUND command (alias of ANOUT)
+ * vPins : Total number of virtual pins allocated (only 1 vPin is supported)
+ * I2C Address : I2C address of the serial controller, in 0x format,
+ * UART ch : Indicating UART 0 or UART 1, values 0 or 1
+ * AM : audio mixer, values: 1 or 2 to select an audio amplifier, no effect if AM is not installed
+ *
+ * The vPin is also an pin that can be read, it indicated if the DFPlayer has finished playing a track
*
*/
@@ -84,6 +50,7 @@
#include "I2CManager.h"
#include "DIAG.h"
+// Debug and diagnostic defines, enable too many will result in slowing the driver
//#define DIAG_I2CDFplayer
//#define DIAG_I2CDFplayer_data
//#define DIAG_I2CDFplayer_reg
@@ -92,13 +59,13 @@
class I2CDFPlayer : public IODevice {
private:
const uint8_t MAXVOLUME=30;
+ uint8_t RETRYCOUNT = 0x03;
bool _playing = false;
uint8_t _inputIndex = 0;
unsigned long _commandSendTime; // Time (us) that last transmit took place.
unsigned long _timeoutTime;
uint8_t _recvCMD; // Last received command code byte
- bool _awaitingResponse = false;
- uint8_t RETRYCOUNT = 0x03;
+ bool _awaitingResponse = false;
uint8_t _retryCounter = RETRYCOUNT; // Max retries before timing out
uint8_t _requestedVolumeLevel = MAXVOLUME;
uint8_t _currentVolume = MAXVOLUME;
@@ -121,8 +88,6 @@ private:
uint8_t FIFO_RX_LEVEL = 0x00;
uint8_t RX_BUFFER = 0x00; // nr of bytes copied into _inbuffer
uint8_t FIFO_TX_LEVEL = 0x00;
- //uint8_t DFPlayerValue = NONE; // Values for enhanced commands
- //uint8_t DFPlayerCmd = NONE; // Enhanced commands
bool _playCmd = false;
bool _volCmd = false;
bool _folderCmd = false;
@@ -135,15 +100,13 @@ private:
uint8_t _requestedEQValue = NORMAL;
uint8_t _currentEQvalue = NORMAL; // start equalizer value
bool _daconCmd = false;
-
uint8_t _outbuffer [11]; // DFPlayer command is 10 bytes + 1 byte register address & UART channel
uint8_t _inbuffer[10]; // expected DFPlayer return 10 bytes
- //unsigned long SC16IS752_XTAL_FREQ = 1843200; // May need to change oscillator frequency to 14.7456Mhz (14745600) to allow for higher baud rates
- unsigned long SC16IS752_XTAL_FREQ = 14745600; // Support for higher baud rates
+ //unsigned long SC16IS752_XTAL_FREQ = 1843200; // To support cheap eBay/AliExpress SC16IS752 boards
+ unsigned long SC16IS752_XTAL_FREQ = 14745600; // Support for higher baud rates, standard for modular EX-IO system
- unsigned long test = 0;
-
+
public:
// Constructor
I2CDFPlayer(VPIN firstVpin, int nPins, I2CAddress i2cAddress, uint8_t UART_CH, uint8_t AM){
@@ -201,14 +164,13 @@ public:
_playing = false;
_retryCounter = RETRYCOUNT;
} else { // timeout and retry protection and recovery of corrupt data frames from DFPlayer
- DIAG(F("I2CDFPlayer: %s, DFPlayer timout, retry counter: %d on UART channel: 0x%x"), _I2CAddress.toString(), _retryCounter, _UART_CH);
+ #ifdef DIAG_I2CDFplayer_playing
+ DIAG(F("I2CDFPlayer: %s, DFPlayer timout, retry counter: %d on UART channel: 0x%x"), _I2CAddress.toString(), _retryCounter, _UART_CH);
+ #endif
_timeoutTime = currentMicros + 5000000UL; // Timeout if no response within 5 seconds// reset timeout
_awaitingResponse = false; // trigger sending a keep alive 0x42 in processOutgoing()
- _retryCounter --; // decrement retry counter
- _resetCmd = true; // queue a DFPlayer reset
- _currentVolume = MAXVOLUME; // Resetting the DFPlayer makes the volume go to default i.e. MAXVOLUME
- //sendPacket(0x0C,0,0); // Reset DFPlayer
- resetRX_fifo(); // reset the RX fifo as it maybe poisoned
+ _retryCounter --; // decrement retry counter
+ resetRX_fifo(); // reset the RX fifo as it has corrupt data
}
}
}
@@ -223,7 +185,7 @@ public:
}
- // Check for incoming data on _serial, and update busy flag and other state accordingly
+ // Check for incoming data, and update busy flag and other state accordingly
void processIncoming(unsigned long currentMicros) {
// Expected message is in the form "7E FF 06 3D xx xx xx xx xx EF"
@@ -335,15 +297,13 @@ public:
if (((int32_t)currentMicros - _commandSendTime) > 100000) {
if ( _resetCmd == true){
sendPacket(0x0C,0,0);
- _resetCmd = false;
- return; // after reset do not execute more commands, wait for the next time giving the DFPlayer time to reset
- // A more saver/elegant way is to wait for the 'SD card online' packet (7E FF 06 3F 00 00 02 xx xx EF)
- // this indicate that the DFPlayer is ready.This may take between 500ms and 1500ms depending on the
- // number of tracks on the SD card
- } else if (_currentVolume > _requestedVolumeLevel) {
- // Change volume before changing song if volume is reducing.
- _currentVolume = _requestedVolumeLevel;
- sendPacket(0x06, 0x00, _currentVolume);
+ _resetCmd = false;
+ } else if(_volCmd == true) { // do the volme before palying a track
+ if(_requestedVolumeLevel >= 0 && _requestedVolumeLevel <= 30){
+ _currentVolume = _requestedVolumeLevel; // If _requestedVolumeLevel is out of range, sent _currentV1olume
+ }
+ sendPacket(0x06, 0x00, _currentVolume);
+ _volCmd = false;
} else if (_playCmd == true) {
// Change song
if (_requestedSong != -1) {
@@ -396,10 +356,6 @@ public:
sendPacket(0x07,0x00,_currentEQvalue);
}
_eqCmd = false;
- } else if (_currentVolume < _requestedVolumeLevel) {
- // Change volume after changing song if volume is increasing.
- _currentVolume = _requestedVolumeLevel;
- sendPacket(0x06, 0x00, _currentVolume);
} else if ((int32_t)currentMicros - _commandSendTime > 1000000) {
// Poll device every second that other commands aren't being sent,
// to check if it's still connected and responding.
@@ -418,34 +374,22 @@ public:
}
}
- // Write with value 1 starts playing a song. The relative pin number is the file number.
- // Write with value 0 stops playing.
+
+ // Write to a vPin will do nothing
void _write(VPIN vpin, int value) override {
if (_deviceState == DEVSTATE_FAILED) return;
- int pin = vpin - _firstVpin;
- if (value) {
- // Value 1, start playing
#ifdef DIAG_IO
- DIAG(F("I2CDFPlayer: Play %d"), pin+1);
+ DIAG(F("I2CDFPlayer: Writing to any vPin not supported"));
#endif
- _requestedSong = pin+1;
- _playing = true;
- } else {
- // Value 0, stop playing
- #ifdef DIAG_IO
- DIAG(F("I2CDFPlayer: Stop"));
- #endif
- _requestedSong = 0; // No song
- _playing = false;
- }
}
+
// WriteAnalogue on first pin uses the nominated value as a file number to start playing, if file number > 0.
// Volume may be specified as second parameter to writeAnalogue.
// If value is zero, the player stops playing.
// WriteAnalogue on second pin sets the output volume.
//
- // Currently all WrtiteAnalogue to be done on vpin 2, will move to vpin 0 when fully implemented
+ // WriteAnalogue to be done on first vpin
//
//void _writeAnalogue(VPIN vpin, int value, uint8_t volume=0, uint16_t=0) override {
void _writeAnalogue(VPIN vpin, int value, uint8_t volume=0, uint16_t cmd=0) override {
@@ -454,32 +398,15 @@ public:
DIAG(F("I2CDFPlayer: VPIN:%u FileNo:%d Volume:%d Command:0x%x"), vpin, value, volume, cmd);
#endif
uint8_t pin = vpin - _firstVpin;
- // Validate parameter.
- if (volume > MAXVOLUME) volume = MAXVOLUME;
-
- if (pin == 0) {
- // Play track
- if (value > 0) {
- if (volume > 0)
- _requestedVolumeLevel = volume;
- _requestedSong = value;
- _playing = true;
- } else {
- _requestedSong = 0; // stop playing
- _playing = false;
- }
- } else if (pin == 1) {
- // Set volume (0-30)
- _requestedVolumeLevel = value;
-
- } else if (pin == 2) { // Enhanced DFPlayer commands
+ if (pin == 0) { // Enhanced DFPlayer commands, do nothing if not vPin 0
// Read command and value
switch (cmd){
//case NONE:
// DFPlayerCmd = cmd;
// break;
case PLAY:
- _playCmd = true;
+ _playCmd = true;
+ _volCmd = true;
_requestedSong = value;
_requestedVolumeLevel = volume;
_playing = true;
@@ -536,31 +463,33 @@ public:
}
}
- // A read on any pin indicates whether the player is still playing.
- int _read(VPIN) override {
+ // A read on any pin indicates if the player is still playing.
+ int _read(VPIN vpin) override {
if (_deviceState == DEVSTATE_FAILED) return false;
- return _playing;
- }
+ uint8_t pin = vpin - _firstVpin;
+ if (pin == 0) { // Do nothing if not vPin 0
+ return _playing;
+ }
+ }
void _display() override {
DIAG(F("I2CDFPlayer Configured on Vpins:%u-%u %S"), _firstVpin, _firstVpin+_nPins-1,
(_deviceState==DEVSTATE_FAILED) ? F("OFFLINE") : F(""));
}
-private:
+private:
+ // DFPlayer command frame
// 7E FF 06 0F 00 01 01 xx xx EF
- // 0 -> 7E is start code
- // 1 -> FF is version
- // 2 -> 06 is length
- // 3 -> 0F is command
- // 4 -> 00 is no receive
+ // 0 -> 7E is start code
+ // 1 -> FF is version
+ // 2 -> 06 is length
+ // 3 -> 0F is command
+ // 4 -> 00 is no receive
// 5~6 -> 01 01 is argument
// 7~8 -> checksum = 0 - ( FF+06+0F+00+01+01 )
- // 9 -> EF is end code
+ // 9 -> EF is end code
- //void sendPacket(uint8_t command, uint16_t arg = 0)
- void sendPacket(uint8_t command, uint8_t arg1 = 0, uint8_t arg2 = 0)
- {
+ void sendPacket(uint8_t command, uint8_t arg1 = 0, uint8_t arg2 = 0) {
FIFO_TX_LEVEL = 0; // Reset FIFO_TX_LEVEL
uint8_t out[] = {
0x7E,
@@ -618,7 +547,6 @@ private:
void setChecksum(uint8_t* out)
{
uint16_t sum = calcChecksum(out);
-
out[7] = (sum >> 8);
out[8] = (sum & 0xff);
}
@@ -628,6 +556,9 @@ private:
// First a software reset
// Enable FIFO and clear TX & RX FIFO
// Need to set the following registers
+ // IOCONTROL set bit 1 and 2 to 0 indicating that they are GPIO
+ // IODIR set all bit to 1 indicating al are output
+ // IOSTATE set only bit 0 to 1 for UART 0, or only bit 1 for UART 1 //
// LCR bit 7=0 divisor latch (clock division registers DLH & DLL, they store 16 bit divisor),
// WORD_LEN, STOP_BIT, PARITY_ENA and PARITY_TYPE
// MCR bit 7=0 clock divisor devide-by-1 clock input
@@ -647,8 +578,13 @@ private:
UART_WriteRegister(REG_IOCONTROL, TEMP_REG_VAL);
TEMP_REG_VAL = 0xFF; //Set all pins as output
UART_WriteRegister(REG_IODIR, TEMP_REG_VAL);
- TEMP_REG_VAL = 0x01; //Set initial value as high
- //TEMP_REG_VAL = 0x00; //Set initial value as low
+ UART_ReadRegister(REG_IOSTATE); // Read current state as not to overwrite the other GPIO pins
+ TEMP_REG_VAL = _inbuffer[0];
+ if (_UART_CH == 0){
+ TEMP_REG_VAL |= (0x01 << _UART_CH); //Set GPIO pin 0 to high
+ } else { // must be UART 1
+ TEMP_REG_VAL |= (0x01 << _UART_CH); //Set GPIO pin 1 to high
+ }
UART_WriteRegister(REG_IOSTATE, TEMP_REG_VAL);
TEMP_REG_VAL = 0x07; // Reset FIFO, clear RX & TX FIFO
UART_WriteRegister(REG_FCR, TEMP_REG_VAL);
@@ -678,12 +614,13 @@ private:
// Read the Receive FIFO Level register (RXLVL), return a single unsigned integer
// of nr of characters in the RX FIFO, bit 6:0, 7 not used, set to zero
// value from 0 (0x00) to 64 (0x40) Only display if RX FIFO has data
+ // The RX fifo level is used to check if there are enough bytes to process a frame
void RX_fifo_lvl(){
UART_ReadRegister(REG_RXLV);
FIFO_RX_LEVEL = _inbuffer[0];
#ifdef DIAG_I2CDFplayer
- //if (FIFO_RX_LEVEL > 0){
- if (FIFO_RX_LEVEL > 0 && FIFO_RX_LEVEL < 10){
+ if (FIFO_RX_LEVEL > 0){
+ //if (FIFO_RX_LEVEL > 0 && FIFO_RX_LEVEL < 10){
DIAG(F("SC16IS752: At I2C: %s, UART channel: 0x%x, FIFO_RX_LEVEL: 0d%d"), _I2CAddress.toString(), _UART_CH, _inbuffer[0]);
}
#endif
@@ -776,7 +713,6 @@ enum : uint8_t{
// DFPlayer commands and values
enum : uint8_t{
- //NONE = 0x00, // redundant
PLAY = 0x0F,
VOL = 0x06,
FOLDER = 0x2B, // Not a DFPlayer command, used to set folder nr where audio file is
@@ -784,7 +720,6 @@ enum : uint8_t{
STOPPLAY = 0x16,
EQ = 0x07, // Set equaliser, require parameter NORMAL, POP, ROCK, JAZZ, CLASSIC or BASS
RESET = 0x0C,
- //DACOFF = 0x1A, // Require 3rd byte to 0x00 in processOutgoing()
DACON = 0x1A, // Not a DFLayer command,need to sent 0x1A and 3rd byte to 0x01 in processOutgoing()
NORMAL = 0x00, // Equalizer parameters
POP = 0x01,
From 46673007cc18f12bf3102f03cc1cb87121c2a23d Mon Sep 17 00:00:00 2001
From: kempe63 <78020007+kempe63@users.noreply.github.com>
Date: Mon, 1 Jan 2024 20:35:39 +0000
Subject: [PATCH 17/51] Update IO_I2CDFPlayer.h
Added SETAM, set the audio Mixer command to the supported EX-Rail commands.
the audio mixer can now be set at startup by using the configuration in myHall.cpp and it can be modified in an EX-Rail scripts.
Syntax: PLAYSOUND(, 0, , SETAM)
Valid values for : 1 or 2
---
IO_I2CDFPlayer.h | 81 ++++++++++++++++++++++++++++++++++++++++++------
1 file changed, 71 insertions(+), 10 deletions(-)
diff --git a/IO_I2CDFPlayer.h b/IO_I2CDFPlayer.h
index 3c0ae3c..2bf28cc 100644
--- a/IO_I2CDFPlayer.h
+++ b/IO_I2CDFPlayer.h
@@ -75,8 +75,7 @@ private:
// SC16IS752 defines
I2CAddress _I2CAddress;
I2CRB _rb;
- uint8_t _UART_CH;
- uint8_t _audioMixer = 0x01; // Default to output amplifier 1
+ uint8_t _UART_CH;
// Communication parameters for the DFPlayer are fixed at 8 bit, No parity, 1 stopbit
uint8_t WORD_LEN = 0x03; // Value LCR bit 0,1
uint8_t STOP_BIT = 0x00; // Value LCR bit 2
@@ -100,6 +99,8 @@ private:
uint8_t _requestedEQValue = NORMAL;
uint8_t _currentEQvalue = NORMAL; // start equalizer value
bool _daconCmd = false;
+ uint8_t _audioMixer = 0x01; // Default to output amplifier 1
+ bool _setamCmd = false; // Set the Audio mixer channel
uint8_t _outbuffer [11]; // DFPlayer command is 10 bytes + 1 byte register address & UART channel
uint8_t _inbuffer[10]; // expected DFPlayer return 10 bytes
@@ -356,6 +357,28 @@ public:
sendPacket(0x07,0x00,_currentEQvalue);
}
_eqCmd = false;
+ } else if (_setamCmd == true){ // Set Audio mixer channel
+ setGPIO(); // Set the audio mixer channel
+ /*
+ if (_audioMixer == 1){ // set to audio mixer 1
+ if (_UART_CH == 0){
+ TEMP_REG_VAL |= (0x01 << _UART_CH); //Set GPIO pin 0 to high
+ } else { // must be UART 1
+ TEMP_REG_VAL |= (0x01 << _UART_CH); //Set GPIO pin 1 to high
+ }
+ //_setamCmd = false;
+ //UART_WriteRegister(REG_IOSTATE, TEMP_REG_VAL);
+ } else { // set to audio mixer 2
+ if (_UART_CH == 0){
+ TEMP_REG_VAL &= (0x00 << _UART_CH); //Set GPIO pin 0 to Low
+ } else { // must be UART 1
+ TEMP_REG_VAL &= (0x00 << _UART_CH); //Set GPIO pin 1 to Low
+ }
+ //_setamCmd = false;
+ //UART_WriteRegister(REG_IOSTATE, TEMP_REG_VAL);
+ }*/
+ _setamCmd = false;
+ UART_WriteRegister(REG_IOSTATE, TEMP_REG_VAL);
} else if ((int32_t)currentMicros - _commandSendTime > 1000000) {
// Poll device every second that other commands aren't being sent,
// to check if it's still connected and responding.
@@ -417,7 +440,7 @@ public:
break;
case FOLDER:
_folderCmd = true;
- if (volume <= 0 && volume > 99){ // Range checking
+ if (volume <= 0 || volume > 99){ // Range checking, valid values 1-99, else default to 1
_requestedFolder = 0x01; // if outside range, default to folder 01
} else {
_requestedFolder = volume;
@@ -442,11 +465,11 @@ public:
DIAG(F("I2CDFPlayer: WriteAnalog EQ: cmd: 0x%x, EQ value: 0x%x"), cmd, volume);
#endif
_eqCmd = true;
- if (volume <= NORMAL) { // to keep backward compatibility the volume parameter is used for values of the EQ cmd
+ if (volume <= 0 || volume > 5) { // If out of range, default to NORMAL
_requestedEQValue = NORMAL;
- } else if (volume <= 0x05) { // Validate EQ parameters
+ } else { // Valid EQ parameter range
_requestedEQValue = volume;
- }
+ }
break;
case RESET:
_resetCmd = true;
@@ -457,6 +480,17 @@ public:
#endif
_daconCmd = true;
break;
+ case SETAM: // Set the audio mixer channel to 1 or 2
+ _setamCmd = true;
+ #ifdef DIAG_I2CDFplayer_playing
+ DIAG(F("I2CDFPlayer: WrtieAnalog SETAM: cmd: 0x%x"), cmd);
+ #endif
+ if (volume <= 0 || volume > 2) { // If out of range, default to 1
+ _audioMixer = 1;
+ } else { // Valid SETAM parameter in range
+ _audioMixer = volume; // _audioMixer valid values 1 or 2
+ }
+ break;
default:
break;
}
@@ -580,12 +614,15 @@ private:
UART_WriteRegister(REG_IODIR, TEMP_REG_VAL);
UART_ReadRegister(REG_IOSTATE); // Read current state as not to overwrite the other GPIO pins
TEMP_REG_VAL = _inbuffer[0];
- if (_UART_CH == 0){
+ setGPIO(); // Set the audio mixer channel
+ /*
+ if (_UART_CH == 0){ // Set Audio mixer channel
TEMP_REG_VAL |= (0x01 << _UART_CH); //Set GPIO pin 0 to high
} else { // must be UART 1
TEMP_REG_VAL |= (0x01 << _UART_CH); //Set GPIO pin 1 to high
}
- UART_WriteRegister(REG_IOSTATE, TEMP_REG_VAL);
+ UART_WriteRegister(REG_IOSTATE, TEMP_REG_VAL);
+ */
TEMP_REG_VAL = 0x07; // Reset FIFO, clear RX & TX FIFO
UART_WriteRegister(REG_FCR, TEMP_REG_VAL);
TEMP_REG_VAL = 0x00; // Set MCR to all 0, includes Clock divisor
@@ -638,6 +675,29 @@ private:
TEMP_REG_VAL = 0x03; // Reset RX fifo
UART_WriteRegister(REG_FCR, TEMP_REG_VAL);
}
+
+ // Set or reset GPIO pin 0 and 1 depending on the UART ch
+ // This function may be modified in a future release to enable all 8 pins to be set or reset with EX-Rail
+ // for various auxilary functions
+ void setGPIO(){
+ UART_ReadRegister(REG_IOSTATE); // Get the current GPIO pins state from the IOSTATE register
+ TEMP_REG_VAL = _inbuffer[0];
+ if (_audioMixer == 1){ // set to audio mixer 1
+ if (_UART_CH == 0){
+ TEMP_REG_VAL |= (0x01 << _UART_CH); //Set GPIO pin 0 to high
+ } else { // must be UART 1
+ TEMP_REG_VAL |= (0x01 << _UART_CH); //Set GPIO pin 1 to high
+ }
+ } else { // set to audio mixer 2
+ if (_UART_CH == 0){
+ TEMP_REG_VAL &= ~(0x01 << _UART_CH); //Set GPIO pin 0 to Low
+ } else { // must be UART 1
+ TEMP_REG_VAL &= ~(0x01 << _UART_CH); //Set GPIO pin 1 to Low
+ }
+ }
+ UART_WriteRegister(REG_IOSTATE, TEMP_REG_VAL);
+ _setamCmd = false;
+ }
// Read the Tranmit FIFO Level register (TXLVL), return a single unsigned integer
@@ -720,13 +780,14 @@ enum : uint8_t{
STOPPLAY = 0x16,
EQ = 0x07, // Set equaliser, require parameter NORMAL, POP, ROCK, JAZZ, CLASSIC or BASS
RESET = 0x0C,
- DACON = 0x1A, // Not a DFLayer command,need to sent 0x1A and 3rd byte to 0x01 in processOutgoing()
+ DACON = 0x1A,
+ SETAM = 0x2A, // Set audio mixer 1 or 2 for this DFPLayer
NORMAL = 0x00, // Equalizer parameters
POP = 0x01,
ROCK = 0x02,
JAZZ = 0x03,
CLASSIC = 0x04,
- BASS = 0x05,
+ BASS = 0x05,
};
};
From 0e9caa11c8b423ce55c1a6ca4144e92fb3569a15 Mon Sep 17 00:00:00 2001
From: kempe63 <78020007+kempe63@users.noreply.github.com>
Date: Mon, 1 Jan 2024 20:39:37 +0000
Subject: [PATCH 18/51] Update IO_I2CDFPlayer.h
Oops, fixed a typo
---
IO_I2CDFPlayer.h | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/IO_I2CDFPlayer.h b/IO_I2CDFPlayer.h
index 2bf28cc..39c0e02 100644
--- a/IO_I2CDFPlayer.h
+++ b/IO_I2CDFPlayer.h
@@ -377,8 +377,7 @@ public:
//_setamCmd = false;
//UART_WriteRegister(REG_IOSTATE, TEMP_REG_VAL);
}*/
- _setamCmd = false;
- UART_WriteRegister(REG_IOSTATE, TEMP_REG_VAL);
+ _setamCmd = false;
} else if ((int32_t)currentMicros - _commandSendTime > 1000000) {
// Poll device every second that other commands aren't being sent,
// to check if it's still connected and responding.
From 74f7af16751095deabe2fbfa9ebf59707c4a2cea Mon Sep 17 00:00:00 2001
From: Harald Barth
Date: Wed, 3 Jan 2024 02:36:07 +0100
Subject: [PATCH 19/51] Display network IP fix
---
GITHUB_SHA.h | 2 +-
WifiESP32.cpp | 2 +-
version.h | 3 ++-
3 files changed, 4 insertions(+), 3 deletions(-)
diff --git a/GITHUB_SHA.h b/GITHUB_SHA.h
index 42646fe..4ece558 100644
--- a/GITHUB_SHA.h
+++ b/GITHUB_SHA.h
@@ -1 +1 @@
-#define GITHUB_SHA "devel-202312251647Z"
+#define GITHUB_SHA "devel-202401030135Z"
diff --git a/WifiESP32.cpp b/WifiESP32.cpp
index c990495..2aef5d1 100644
--- a/WifiESP32.cpp
+++ b/WifiESP32.cpp
@@ -179,7 +179,7 @@ bool WifiESP::setup(const char *SSid,
if (WiFi.status() == WL_CONNECTED) {
// DIAG(F("Wifi STA IP %s"),WiFi.localIP().toString().c_str());
DIAG(F("Wifi in STA mode"));
- LCD(7, F("IP: %s"), WiFi.softAPIP().toString().c_str());
+ LCD(7, F("IP: %s"), WiFi.localIP().toString().c_str());
wifiUp = true;
} else {
DIAG(F("Could not connect to Wifi SSID %s"),SSid);
diff --git a/version.h b/version.h
index bc79b3e..4ac82a5 100644
--- a/version.h
+++ b/version.h
@@ -3,7 +3,8 @@
#include "StringFormatter.h"
-#define VERSION "5.2.17"
+#define VERSION "5.2.18"
+// 5.2.18 - Display network IP fix
// 5.2.17 - ESP32 simplify network logic
// 5.2.16 - Bugfix to allow for devices using the EX-IOExpander protocol to have no analogue or no digital pins
// 5.2.15 - move call to CommandDistributor::broadcastPower() into the TrackManager::setTrackPower(*) functions
From 4a3d3228a93622d8a7ab48cf1b0000079a51b208 Mon Sep 17 00:00:00 2001
From: Harald Barth
Date: Sun, 7 Jan 2024 22:22:38 +0100
Subject: [PATCH 20/51] ESP32: Use SOC_RMT_MEM_WORDS_PER_CHANNEL to determine
if the RMT hardware can handle DCC
---
DCCRMT.cpp | 50 +++++++++++++++++++++++++++++++++++---------------
DCCWaveform.h | 8 ++++----
2 files changed, 39 insertions(+), 19 deletions(-)
diff --git a/DCCRMT.cpp b/DCCRMT.cpp
index cbd9af6..afada7b 100644
--- a/DCCRMT.cpp
+++ b/DCCRMT.cpp
@@ -1,5 +1,5 @@
/*
- * © 2021-2022, Harald Barth.
+ * © 2021-2024, Harald Barth.
*
* This file is part of DCC-EX
*
@@ -25,6 +25,18 @@
#include "DCCWaveform.h" // for MAX_PACKET_SIZE
#include "soc/gpio_sig_map.h"
+// check for right type of ESP32
+#include "soc/soc_caps.h"
+#ifndef SOC_RMT_MEM_WORDS_PER_CHANNEL
+#error This symobol should be defined
+#endif
+#if SOC_RMT_MEM_WORDS_PER_CHANNEL < 64
+#warning This is not an ESP32-WROOM but some other unsupported variant
+#warning You are outside of the DCC-EX supported hardware
+#endif
+
+static const byte RMT_CHAN_PER_DCC_CHAN = 2;
+
// Number of bits resulting out of X bytes of DCC payload data
// Each byte has one bit extra and at the end we have one EOF marker
#define DATA_LEN(X) ((X)*9+1)
@@ -75,12 +87,30 @@ void IRAM_ATTR interrupt(rmt_channel_t channel, void *t) {
RMTChannel::RMTChannel(pinpair pins, bool isMain) {
byte ch;
byte plen;
+
+ // Below we check if the DCC packet actually fits into the RMT hardware
+ // Currently MAX_PACKET_SIZE = 5 so with checksum there are
+ // MAX_PACKET_SIZE+1 data packets. Each need DATA_LEN (9) bits.
+ // To that we add the preamble length, the fencepost DCC end bit
+ // and the RMT EOF marker.
+ // SOC_RMT_MEM_WORDS_PER_CHANNEL is either 64 (original WROOM) or
+ // 48 (all other ESP32 like the -C3 or -S2
+ // The formula to get the possible MAX_PACKET_SIZE is
+ //
+ // ALLOCATED = RMT_CHAN_PER_DCC_CHAN * SOC_RMT_MEM_WORDS_PER_CHANNEL
+ // MAX_PACKET_SIZE = floor((ALLOCATED - PREAMBLE_LEN - 2)/9 - 1)
+ //
+
if (isMain) {
ch = 0;
plen = PREAMBLE_BITS_MAIN;
+ static_assert (DATA_LEN(MAX_PACKET_SIZE+1) + PREAMBLE_BITS_MAIN + 2 <= RMT_CHAN_PER_DCC_CHAN * SOC_RMT_MEM_WORDS_PER_CHANNEL,
+ "Number of DCC packet bits greater than ESP32 RMT memory available");
} else {
- ch = 2;
+ ch = RMT_CHAN_PER_DCC_CHAN; // number == offset
plen = PREAMBLE_BITS_PROG;
+ static_assert (DATA_LEN(MAX_PACKET_SIZE+1) + PREAMBLE_BITS_PROG + 2 <= RMT_CHAN_PER_DCC_CHAN * SOC_RMT_MEM_WORDS_PER_CHANNEL,
+ "Number of DCC packet bits greater than ESP32 RMT memory available");
}
// preamble
@@ -115,7 +145,7 @@ RMTChannel::RMTChannel(pinpair pins, bool isMain) {
// data: max packet size today is 5 + checksum
maxDataLen = DATA_LEN(MAX_PACKET_SIZE+1); // plus checksum
data = (rmt_item32_t*)malloc(maxDataLen*sizeof(rmt_item32_t));
-
+
rmt_config_t config;
// Configure the RMT channel for TX
bzero(&config, sizeof(rmt_config_t));
@@ -123,20 +153,10 @@ RMTChannel::RMTChannel(pinpair pins, bool isMain) {
config.channel = channel = (rmt_channel_t)ch;
config.clk_div = RMT_CLOCK_DIVIDER;
config.gpio_num = (gpio_num_t)pins.pin;
- config.mem_block_num = 2; // With longest DCC packet 11 inc checksum (future expansion)
- // number of bits needed is 22preamble + start +
- // 11*9 + extrazero + EOT = 124
- // 2 mem block of 64 RMT items should be enough
-
+ config.mem_block_num = RMT_CHAN_PER_DCC_CHAN;
+ // use config
ESP_ERROR_CHECK(rmt_config(&config));
addPin(pins.invpin, true);
- /*
- // test: config another gpio pin
- gpio_num_t gpioNum = (gpio_num_t)(pin-1);
- PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpioNum], PIN_FUNC_GPIO);
- gpio_set_direction(gpioNum, GPIO_MODE_OUTPUT);
- gpio_matrix_out(gpioNum, RMT_SIG_OUT0_IDX, 0, 0);
- */
// NOTE: ESP_INTR_FLAG_IRAM is *NOT* included in this bitmask
ESP_ERROR_CHECK(rmt_driver_install(config.channel, 0, ESP_INTR_FLAG_LOWMED|ESP_INTR_FLAG_SHARED));
diff --git a/DCCWaveform.h b/DCCWaveform.h
index 2202b53..1392288 100644
--- a/DCCWaveform.h
+++ b/DCCWaveform.h
@@ -2,7 +2,7 @@
* © 2021 M Steve Todd
* © 2021 Mike S
* © 2021 Fred Decker
- * © 2020-2021 Harald Barth
+ * © 2020-2024 Harald Barth
* © 2020-2021 Chris Harlow
* All rights reserved.
*
@@ -33,9 +33,9 @@
// Number of preamble bits.
-const int PREAMBLE_BITS_MAIN = 16;
-const int PREAMBLE_BITS_PROG = 22;
-const byte MAX_PACKET_SIZE = 5; // NMRA standard extended packets, payload size WITHOUT checksum.
+const byte PREAMBLE_BITS_MAIN = 16;
+const byte PREAMBLE_BITS_PROG = 22;
+const byte MAX_PACKET_SIZE = 5; // NMRA standard extended packets, payload size WITHOUT checksum.
// The WAVE_STATE enum is deliberately numbered because a change of order would be catastrophic
From 39d0cbb791020e69be0a1da37a4ba7dcdc3a33e5 Mon Sep 17 00:00:00 2001
From: Harald Barth
Date: Sun, 7 Jan 2024 22:24:15 +0100
Subject: [PATCH 21/51] version 5.2.19
---
GITHUB_SHA.h | 2 +-
version.h | 3 ++-
2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/GITHUB_SHA.h b/GITHUB_SHA.h
index 4ece558..79fd3fe 100644
--- a/GITHUB_SHA.h
+++ b/GITHUB_SHA.h
@@ -1 +1 @@
-#define GITHUB_SHA "devel-202401030135Z"
+#define GITHUB_SHA "devel-202401072123Z"
diff --git a/version.h b/version.h
index 4ac82a5..ee6bd24 100644
--- a/version.h
+++ b/version.h
@@ -3,7 +3,8 @@
#include "StringFormatter.h"
-#define VERSION "5.2.18"
+#define VERSION "5.2.19"
+// 5.2.19 - ESP32: Determine if the RMT hardware can handle DCC
// 5.2.18 - Display network IP fix
// 5.2.17 - ESP32 simplify network logic
// 5.2.16 - Bugfix to allow for devices using the EX-IOExpander protocol to have no analogue or no digital pins
From 70a1b9538c303613328cadc554af13d5bb23c9bf Mon Sep 17 00:00:00 2001
From: Harald Barth
Date: Mon, 8 Jan 2024 13:19:22 +0100
Subject: [PATCH 22/51] Check return of Ethernet.begin() in all code variants
---
EthernetInterface.cpp | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/EthernetInterface.cpp b/EthernetInterface.cpp
index 5cf531c..7d8d7b0 100644
--- a/EthernetInterface.cpp
+++ b/EthernetInterface.cpp
@@ -59,15 +59,15 @@ EthernetInterface::EthernetInterface()
DCCTimer::getSimulatedMacAddress(mac);
connected=false;
- #ifdef IP_ADDRESS
- Ethernet.begin(mac, IP_ADDRESS);
- #else
+#ifdef IP_ADDRESS
+ if (Ethernet.begin(mac, IP_ADDRESS) == 0)
+#else
if (Ethernet.begin(mac) == 0)
+#endif
{
DIAG(F("Ethernet.begin FAILED"));
return;
}
- #endif
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
DIAG(F("Ethernet shield not found or W5100"));
}
From 718e78fca686c4dee836e0e84ab3c9a1556ab054 Mon Sep 17 00:00:00 2001
From: Harald Barth
Date: Mon, 8 Jan 2024 13:20:29 +0100
Subject: [PATCH 23/51] version 5.2.20
---
GITHUB_SHA.h | 2 +-
version.h | 3 ++-
2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/GITHUB_SHA.h b/GITHUB_SHA.h
index 79fd3fe..1cc285a 100644
--- a/GITHUB_SHA.h
+++ b/GITHUB_SHA.h
@@ -1 +1 @@
-#define GITHUB_SHA "devel-202401072123Z"
+#define GITHUB_SHA "devel-202401081219Z"
diff --git a/version.h b/version.h
index ee6bd24..75c2899 100644
--- a/version.h
+++ b/version.h
@@ -3,7 +3,8 @@
#include "StringFormatter.h"
-#define VERSION "5.2.19"
+#define VERSION "5.2.20"
+// 5.2.20 - Check return of Ethernet.begin()
// 5.2.19 - ESP32: Determine if the RMT hardware can handle DCC
// 5.2.18 - Display network IP fix
// 5.2.17 - ESP32 simplify network logic
From b51a8fe126d75bddc96ab5e6f1742a0c6be2da61 Mon Sep 17 00:00:00 2001
From: peteGSX <97784652+peteGSX@users.noreply.github.com>
Date: Tue, 9 Jan 2024 10:41:29 +1000
Subject: [PATCH 24/51] Add STARTUP_DELAY
---
CommandStation-EX.ino | 5 +++++
config.example.h | 8 ++++++++
version.h | 3 ++-
3 files changed, 15 insertions(+), 1 deletion(-)
diff --git a/CommandStation-EX.ino b/CommandStation-EX.ino
index 205babf..9c94606 100644
--- a/CommandStation-EX.ino
+++ b/CommandStation-EX.ino
@@ -76,6 +76,11 @@ void setup()
DIAG(F("License GPLv3 fsf.org (c) dcc-ex.com"));
+// If user has defined a startup delay, delay here before starting IO
+#if defined(STARTUP_DELAY)
+ delay(STARTUP_DELAY)
+#endif
+
// Initialise HAL layer before reading EEprom or setting up MotorDrivers
IODevice::begin();
diff --git a/config.example.h b/config.example.h
index 89d6c1f..a2e08b2 100644
--- a/config.example.h
+++ b/config.example.h
@@ -222,6 +222,14 @@ The configuration file for DCC-EX Command Station
// We do not support to use the same address, for example 100(long) and 100(short)
// at the same time, there must be a border.
+/////////////////////////////////////////////////////////////////////////////////////
+// Some newer 32bit microcontrollers boot very quickly, so powering on I2C and other
+// peripheral devices at the same time may result in the CommandStation booting too
+// quickly to detect them.
+// To work around this, uncomment the STARTUP_DELAY line below and set a value in
+// milliseconds that works for your environment, default is 3000 (3 seconds).
+// #define STARTUP_DELAY 3000
+
/////////////////////////////////////////////////////////////////////////////////////
//
// DEFINE TURNOUTS/ACCESSORIES FOLLOW NORM RCN-213
diff --git a/version.h b/version.h
index 75c2899..9f19afe 100644
--- a/version.h
+++ b/version.h
@@ -3,7 +3,8 @@
#include "StringFormatter.h"
-#define VERSION "5.2.20"
+#define VERSION "5.2.21"
+// 5.2.21 - Add STARTUP_DELAY config option to delay CS bootup
// 5.2.20 - Check return of Ethernet.begin()
// 5.2.19 - ESP32: Determine if the RMT hardware can handle DCC
// 5.2.18 - Display network IP fix
From 5ac26ce505b28c7f0a0a76b2d4e39b6b51b6cc5c Mon Sep 17 00:00:00 2001
From: peteGSX <97784652+peteGSX@users.noreply.github.com>
Date: Tue, 9 Jan 2024 10:49:22 +1000
Subject: [PATCH 25/51] Add missing ; and DIAG message
---
CommandStation-EX.ino | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/CommandStation-EX.ino b/CommandStation-EX.ino
index 9c94606..2cd9d33 100644
--- a/CommandStation-EX.ino
+++ b/CommandStation-EX.ino
@@ -78,7 +78,8 @@ void setup()
// If user has defined a startup delay, delay here before starting IO
#if defined(STARTUP_DELAY)
- delay(STARTUP_DELAY)
+ DIAG(F("Delaying startup for %dms"), STARTUP_DELAY);
+ delay(STARTUP_DELAY);
#endif
// Initialise HAL layer before reading EEprom or setting up MotorDrivers
From 6b8b3e2664c2ba3fd5936824d1c531f892e672da Mon Sep 17 00:00:00 2001
From: Ash-4 <81280775+Ash-4@users.noreply.github.com>
Date: Mon, 8 Jan 2024 19:02:06 -0600
Subject: [PATCH 26/51] Update version.h
---
version.h | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/version.h b/version.h
index 8239649..42fd03e 100644
--- a/version.h
+++ b/version.h
@@ -3,7 +3,8 @@
#include "StringFormatter.h"
-#define VERSION "5.2.20ethCdf"
+#define VERSION "5.2.21"
+// 5.2.21 - Add STARTUP_DELAY config option to delay CS bootup
// 5.2.20 - Check return of Ethernet.begin()
// 5.2.19 - ESP32: Determine if the RMT hardware can handle DCC
// 5.2.18 - Display network IP fix
From b3667b4d8585d05f107e93e988fe8d40da3ff469 Mon Sep 17 00:00:00 2001
From: Ash-4 <81280775+Ash-4@users.noreply.github.com>
Date: Mon, 8 Jan 2024 19:07:16 -0600
Subject: [PATCH 27/51] Update version.h 5.2.21 startup-delay
---
version.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/version.h b/version.h
index 42fd03e..ee50594 100644
--- a/version.h
+++ b/version.h
@@ -3,7 +3,7 @@
#include "StringFormatter.h"
-#define VERSION "5.2.21"
+#define VERSION "5.2.21ethCdf"
// 5.2.21 - Add STARTUP_DELAY config option to delay CS bootup
// 5.2.20 - Check return of Ethernet.begin()
// 5.2.19 - ESP32: Determine if the RMT hardware can handle DCC
From ca380d11dcaf01459614d2527577a08eb00cb43b Mon Sep 17 00:00:00 2001
From: Harald Barth
Date: Wed, 10 Jan 2024 08:15:30 +0100
Subject: [PATCH 28/51] Do not crash on turnouts or turntables without
description
---
EXRAIL2.cpp | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/EXRAIL2.cpp b/EXRAIL2.cpp
index cc3269b..8a2eadf 100644
--- a/EXRAIL2.cpp
+++ b/EXRAIL2.cpp
@@ -333,13 +333,15 @@ if (compileFeatures & FEATURE_SIGNAL) {
}
void RMFT2::setTurnoutHiddenState(Turnout * t) {
- // turnout descriptions are in low flash F strings
- t->setHidden(GETFLASH(getTurnoutDescription(t->getId()))==0x01);
+ // turnout descriptions are in low flash F strings
+ const FSH *desc = getTurnoutDescription(t->getId());
+ if (desc) t->setHidden(GETFLASH(desc)==0x01);
}
#ifndef IO_NO_HAL
void RMFT2::setTurntableHiddenState(Turntable * tto) {
- tto->setHidden(GETFLASH(getTurntableDescription(tto->getId()))==0x01);
+ const FSH *desc = getTurntableDescription(tto->getId());
+ if (desc) tto->setHidden(GETFLASH(desc)==0x01);
}
#endif
From 27bd4448845e82b8d5d614dcc2d412bbde975c1c Mon Sep 17 00:00:00 2001
From: Harald Barth
Date: Wed, 10 Jan 2024 08:18:14 +0100
Subject: [PATCH 29/51] Numbers for automations/routes can be negative
---
EXRAIL2Parser.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/EXRAIL2Parser.cpp b/EXRAIL2Parser.cpp
index b049699..d231342 100644
--- a/EXRAIL2Parser.cpp
+++ b/EXRAIL2Parser.cpp
@@ -134,7 +134,7 @@ void RMFT2::ComandFilter(Print * stream, byte & opcode, byte & paramCount, int16
return;
}
if (paramCount==2) { //
- uint16_t id=p[1];
+ int16_t id=p[1];
StringFormatter::send(stream,F("\n"),
id, getRouteType(id), getRouteDescription(id));
From 796d5c47748a4c189f2b04c1e3215f6f54a2c9b2 Mon Sep 17 00:00:00 2001
From: Harald Barth
Date: Wed, 10 Jan 2024 08:20:14 +0100
Subject: [PATCH 30/51] version 5.2.22
---
GITHUB_SHA.h | 2 +-
version.h | 3 ++-
2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/GITHUB_SHA.h b/GITHUB_SHA.h
index 1cc285a..a53b57a 100644
--- a/GITHUB_SHA.h
+++ b/GITHUB_SHA.h
@@ -1 +1 @@
-#define GITHUB_SHA "devel-202401081219Z"
+#define GITHUB_SHA "devel-202401100719Z"
diff --git a/version.h b/version.h
index 9f19afe..3c33933 100644
--- a/version.h
+++ b/version.h
@@ -3,7 +3,8 @@
#include "StringFormatter.h"
-#define VERSION "5.2.21"
+#define VERSION "5.2.22"
+// 5.2.22 - Bugfixes: Empty turnout descriptions ok; negative route numbers valid.
// 5.2.21 - Add STARTUP_DELAY config option to delay CS bootup
// 5.2.20 - Check return of Ethernet.begin()
// 5.2.19 - ESP32: Determine if the RMT hardware can handle DCC
From 8e56a1e7f9c8940b77bde293290dd3327776c718 Mon Sep 17 00:00:00 2001
From: Ash-4 <81280775+Ash-4@users.noreply.github.com>
Date: Wed, 10 Jan 2024 01:34:29 -0600
Subject: [PATCH 31/51] Update version.h
---
version.h | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/version.h b/version.h
index ee50594..bb100bb 100644
--- a/version.h
+++ b/version.h
@@ -3,7 +3,8 @@
#include "StringFormatter.h"
-#define VERSION "5.2.21ethCdf"
+#define VERSION "5.2.22"
+// 5.2.22 - Bugfixes: Empty turnout descriptions ok; negative route numbers valid.
// 5.2.21 - Add STARTUP_DELAY config option to delay CS bootup
// 5.2.20 - Check return of Ethernet.begin()
// 5.2.19 - ESP32: Determine if the RMT hardware can handle DCC
From 1bd113a35f87701b7e5a097661ec9b3c1b123591 Mon Sep 17 00:00:00 2001
From: Ash-4 <81280775+Ash-4@users.noreply.github.com>
Date: Wed, 10 Jan 2024 01:40:31 -0600
Subject: [PATCH 32/51] update for negative routes 5.2.22
---
version.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/version.h b/version.h
index bb100bb..909d56b 100644
--- a/version.h
+++ b/version.h
@@ -3,7 +3,7 @@
#include "StringFormatter.h"
-#define VERSION "5.2.22"
+#define VERSION "5.2.22ethCdf"
// 5.2.22 - Bugfixes: Empty turnout descriptions ok; negative route numbers valid.
// 5.2.21 - Add STARTUP_DELAY config option to delay CS bootup
// 5.2.20 - Check return of Ethernet.begin()
From fa44d547749a36b2b89c7254c2d608d3d09e2604 Mon Sep 17 00:00:00 2001
From: Harald Barth
Date: Wed, 10 Jan 2024 09:16:27 +0100
Subject: [PATCH 33/51] guessI2CDeviceType should always return FSH "string"
---
I2CManager.cpp | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/I2CManager.cpp b/I2CManager.cpp
index fadf8bb..2591e13 100644
--- a/I2CManager.cpp
+++ b/I2CManager.cpp
@@ -66,9 +66,9 @@ static const FSH * guessI2CDeviceType(uint8_t address) {
return F("Real-time clock");
else if (address >= 0x70 && address <= 0x77)
return F("I2C Mux");
- else if (address >= 0x90 && address <= 0xAE);
- else
- return F("?");
+ else if (address >= 0x90 && address <= 0xAE)
+ return F("UART");
+ return F("?");
}
// If not already initialised, initialise I2C
From 2e4995cab364893b37796cffd79ce992c1b1e47f Mon Sep 17 00:00:00 2001
From: Asbelos
Date: Wed, 10 Jan 2024 11:58:30 +0000
Subject: [PATCH 34/51] Keyword Hasher _hk
---
DCCEXParser.cpp | 154 ++++++++++++++++------------------------------
EXRAIL2Parser.cpp | 52 +++++-----------
KeywordHasher.h | 57 +++++++++++++++++
TrackManager.cpp | 36 ++++-------
4 files changed, 137 insertions(+), 162 deletions(-)
create mode 100644 KeywordHasher.h
diff --git a/DCCEXParser.cpp b/DCCEXParser.cpp
index aefed4c..47665c5 100644
--- a/DCCEXParser.cpp
+++ b/DCCEXParser.cpp
@@ -116,6 +116,7 @@ Once a new OPCODE is decided upon, update this list.
#include "EXRAIL2.h"
#include "Turntables.h"
#include "version.h"
+#include "KeywordHasher.h"
// This macro can't be created easily as a portable function because the
// flashlist requires a far pointer for high flash access.
@@ -126,57 +127,6 @@ Once a new OPCODE is decided upon, update this list.
StringFormatter::send(stream,F(" %d"),value); \
}
-
-// 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
-const int16_t HASH_KEYWORD_MAIN = 11339;
-const int16_t HASH_KEYWORD_CABS = -11981;
-const int16_t HASH_KEYWORD_RAM = 25982;
-const int16_t HASH_KEYWORD_CMD = 9962;
-const int16_t HASH_KEYWORD_ACK = 3113;
-const int16_t HASH_KEYWORD_ON = 2657;
-const int16_t HASH_KEYWORD_DCC = 6436;
-const int16_t HASH_KEYWORD_SLOW = -17209;
-#ifndef DISABLE_PROG
-const int16_t HASH_KEYWORD_JOIN = -30750;
-const int16_t HASH_KEYWORD_PROG = -29718;
-const int16_t HASH_KEYWORD_PROGBOOST = -6353;
-#endif
-#ifndef DISABLE_EEPROM
-const int16_t HASH_KEYWORD_EEPROM = -7168;
-#endif
-const int16_t HASH_KEYWORD_LIMIT = 27413;
-const int16_t HASH_KEYWORD_MAX = 16244;
-const int16_t HASH_KEYWORD_MIN = 15978;
-const int16_t HASH_KEYWORD_RESET = 26133;
-const int16_t HASH_KEYWORD_RETRY = 25704;
-const int16_t HASH_KEYWORD_SPEED28 = -17064;
-const int16_t HASH_KEYWORD_SPEED128 = 25816;
-const int16_t HASH_KEYWORD_SERVO=27709;
-const int16_t HASH_KEYWORD_TT=2688;
-const int16_t HASH_KEYWORD_VPIN=-415;
-const int16_t HASH_KEYWORD_A='A';
-const int16_t HASH_KEYWORD_C='C';
-const int16_t HASH_KEYWORD_G='G';
-const int16_t HASH_KEYWORD_H='H';
-const int16_t HASH_KEYWORD_I='I';
-const int16_t HASH_KEYWORD_M='M';
-const int16_t HASH_KEYWORD_O='O';
-const int16_t HASH_KEYWORD_P='P';
-const int16_t HASH_KEYWORD_R='R';
-const int16_t HASH_KEYWORD_T='T';
-const int16_t HASH_KEYWORD_X='X';
-const int16_t HASH_KEYWORD_LCN = 15137;
-const int16_t HASH_KEYWORD_HAL = 10853;
-const int16_t HASH_KEYWORD_SHOW = -21309;
-const int16_t HASH_KEYWORD_ANIN = -10424;
-const int16_t HASH_KEYWORD_ANOUT = -26399;
-const int16_t HASH_KEYWORD_WIFI = -5583;
-const int16_t HASH_KEYWORD_ETHERNET = -30767;
-const int16_t HASH_KEYWORD_WIT = 31594;
-const int16_t HASH_KEYWORD_EXTT = 8573;
-const int16_t HASH_KEYWORD_ADD = 3201;
-
int16_t DCCEXParser::stashP[MAX_COMMAND_PARAMS];
bool DCCEXParser::stashBusy;
Print *DCCEXParser::stashStream = NULL;
@@ -567,20 +517,20 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream)
TrackManager::setTrackPower(TRACK_MODE_ALL, POWERMODE::ON);
}
if (params==1) {
- if (p[0]==HASH_KEYWORD_MAIN) { // <1 MAIN>
+ if (p[0]=="MAIN"_hk) { // <1 MAIN>
TrackManager::setTrackPower(TRACK_MODE_MAIN, POWERMODE::ON);
}
#ifndef DISABLE_PROG
- else if (p[0] == HASH_KEYWORD_JOIN) { // <1 JOIN>
+ else if (p[0] == "JOIN"_hk) { // <1 JOIN>
TrackManager::setJoin(true);
TrackManager::setTrackPower(TRACK_MODE_MAIN|TRACK_MODE_PROG, POWERMODE::ON);
}
- else if (p[0]==HASH_KEYWORD_PROG) { // <1 PROG>
+ else if (p[0]=="PROG"_hk) { // <1 PROG>
TrackManager::setJoin(false);
TrackManager::setTrackPower(TRACK_MODE_PROG, POWERMODE::ON);
}
#endif
- else if (p[0] >= HASH_KEYWORD_A && p[0] <= HASH_KEYWORD_H) { // <1 A-H>
+ else if (p[0] >= "A"_hk && p[0] <= "H"_hk) { // <1 A-H>
byte t = (p[0] - 'A');
TrackManager::setTrackPower(POWERMODE::ON, t);
//StringFormatter::send(stream, F("\n"), t+'A');
@@ -600,17 +550,17 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream)
TrackManager::setTrackPower(TRACK_MODE_ALL, POWERMODE::OFF);
}
if (params==1) {
- if (p[0]==HASH_KEYWORD_MAIN) { // <0 MAIN>
+ if (p[0]=="MAIN"_hk) { // <0 MAIN>
TrackManager::setJoin(false);
TrackManager::setTrackPower(TRACK_MODE_MAIN, POWERMODE::OFF);
}
#ifndef DISABLE_PROG
- else if (p[0]==HASH_KEYWORD_PROG) { // <0 PROG>
+ else if (p[0]=="PROG"_hk) { // <0 PROG>
TrackManager::progTrackBoosted=false; // Prog track boost mode will not outlive prog track off
TrackManager::setTrackPower(TRACK_MODE_PROG, POWERMODE::OFF);
}
#endif
- else if (p[0] >= HASH_KEYWORD_A && p[0] <= HASH_KEYWORD_H) { // <1 A-H>
+ else if (p[0] >= "A"_hk && p[0] <= "H"_hk) { // <1 A-H>
byte t = (p[0] - 'A');
TrackManager::setJoin(false);
TrackManager::setTrackPower(POWERMODE::OFF, t);
@@ -704,7 +654,7 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream)
//if ((params<1) | (params>2)) break; //
int16_t id=(params==2)?p[1]:0;
switch(p[0]) {
- case HASH_KEYWORD_C: // sets time and speed
+ case "C"_hk: // sets time and speed
if (params==1) { // returns latest time
int16_t x = CommandDistributor::retClockTime();
StringFormatter::send(stream, F("\n"), x);
@@ -713,28 +663,28 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream)
CommandDistributor::setClockTime(p[1], p[2], 1);
return;
- case HASH_KEYWORD_G: // current gauge limits
+ case "G"_hk: // current gauge limits
if (params>1) break;
TrackManager::reportGauges(stream); //
return;
- case HASH_KEYWORD_I: // current values
+ case "I"_hk: // current values
if (params>1) break;
TrackManager::reportCurrent(stream); //
return;
- case HASH_KEYWORD_A: // intercepted by EXRAIL// returns automations/routes
+ case "A"_hk: // intercepted by EXRAIL// returns automations/routes
if (params!=1) break; //
StringFormatter::send(stream, F("\n"));
return;
- case HASH_KEYWORD_M: // intercepted by EXRAIL
+ case "M"_hk: // intercepted by EXRAIL
if (params>1) break; // invalid cant do
// requests stash size so say none.
StringFormatter::send(stream,F("\n"));
return;
- case HASH_KEYWORD_R: // returns rosters
+ case "R"_hk: // returns rosters
StringFormatter::send(stream, F("\n"));
return;
- case HASH_KEYWORD_T: // returns turnout list
+ case "T"_hk: // returns turnout list
StringFormatter::send(stream, F("
for ( Turnout * t=Turnout::first(); t; t=t->next()) {
@@ -780,7 +730,7 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream)
return;
// No turntables without HAL support
#ifndef IO_NO_HAL
- case HASH_KEYWORD_O: //
for (Turntable * tto=Turntable::first(); tto; tto=tto->next()) {
@@ -805,7 +755,7 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream)
}
}
return;
- case HASH_KEYWORD_P: // returns turntable position list for the turntable id
+ case "P"_hk: // returns turntable position list for the turntable id
if (params==2) { //
Turntable *tto=Turntable::get(id);
if (!tto || tto->isHidden()) {
@@ -972,14 +922,14 @@ bool DCCEXParser::parseT(Print *stream, int16_t params, int16_t p[])
switch (p[1]) {
// Turnout messages use 1=throw, 0=close.
case 0:
- case HASH_KEYWORD_C:
+ case "C"_hk:
state = true;
break;
case 1:
- case HASH_KEYWORD_T:
+ case "T"_hk:
state= false;
break;
- case HASH_KEYWORD_X:
+ case "X"_hk:
{
Turnout *tt = Turnout::get(p[0]);
if (tt) {
@@ -996,14 +946,14 @@ bool DCCEXParser::parseT(Print *stream, int16_t params, int16_t p[])
}
default: // Anything else is some kind of turnout create function.
- if (params == 6 && p[1] == HASH_KEYWORD_SERVO) { //
+ if (params == 6 && p[1] == "SERVO"_hk) { //
if (!ServoTurnout::create(p[0], (VPIN)p[2], (uint16_t)p[3], (uint16_t)p[4], (uint8_t)p[5]))
return false;
} else
- if (params == 3 && p[1] == HASH_KEYWORD_VPIN) { //
+ if (params == 3 && p[1] == "VPIN"_hk) { //
if (!VpinTurnout::create(p[0], p[2])) return false;
} else
- if (params >= 3 && p[1] == HASH_KEYWORD_DCC) {
+ if (params >= 3 && p[1] == "DCC"_hk) {
// 0<=addr<=511, 0<=subadd<=3 (like command).
if (params==4 && p[2]>=0 && p[2]<512 && p[3]>=0 && p[3]<4) { //
if (!DCCTurnout::create(p[0], p[2], p[3])) return false;
@@ -1069,41 +1019,41 @@ bool DCCEXParser::parseC(Print *stream, int16_t params, int16_t p[]) {
switch (p[0])
{
#ifndef DISABLE_PROG
- case HASH_KEYWORD_PROGBOOST:
+ case "PROGBOOST"_hk:
TrackManager::progTrackBoosted=true;
return true;
#endif
- case HASH_KEYWORD_RESET:
+ case "RESET"_hk:
DCCTimer::reset();
break; // and if we didnt restart
- case HASH_KEYWORD_SPEED28:
+ case "SPEED28"_hk:
DCC::setGlobalSpeedsteps(28);
DIAG(F("28 Speedsteps"));
return true;
- case HASH_KEYWORD_SPEED128:
+ case "SPEED128"_hk:
DCC::setGlobalSpeedsteps(128);
DIAG(F("128 Speedsteps"));
return true;
#ifndef DISABLE_PROG
- case HASH_KEYWORD_ACK: //
+ case "ACK"_hk: //
if (params >= 3) {
- if (p[1] == HASH_KEYWORD_LIMIT) {
+ if (p[1] == "LIMIT"_hk) {
DCCACK::setAckLimit(p[2]);
LCD(1, F("Ack Limit=%dmA"), p[2]); //
- } else if (p[1] == HASH_KEYWORD_MIN) {
+ } else if (p[1] == "MIN"_hk) {
DCCACK::setMinAckPulseDuration(p[2]);
LCD(0, F("Ack Min=%uus"), p[2]); //
- } else if (p[1] == HASH_KEYWORD_MAX) {
+ } else if (p[1] == "MAX"_hk) {
DCCACK::setMaxAckPulseDuration(p[2]);
LCD(0, F("Ack Max=%uus"), p[2]); //
- } else if (p[1] == HASH_KEYWORD_RETRY) {
+ } else if (p[1] == "RETRY"_hk) {
if (p[2] >255) p[2]=3;
LCD(0, F("Ack Retry=%d Sum=%d"), p[2], DCCACK::setAckRetry(p[2])); //
}
} else {
- bool onOff = (params > 0) && (p[1] == 1 || p[1] == HASH_KEYWORD_ON); // dont care if other stuff or missing... just means off
+ bool onOff = (params > 0) && (p[1] == 1 || p[1] == "ON"_hk); // dont care if other stuff or missing... just means off
DIAG(F("Ack diag %S"), onOff ? F("on") : F("off"));
Diag::ACK = onOff;
@@ -1121,64 +1071,64 @@ bool DCCEXParser::parseD(Print *stream, int16_t params, int16_t p[])
{
if (params == 0)
return false;
- bool onOff = (params > 0) && (p[1] == 1 || p[1] == HASH_KEYWORD_ON); // dont care if other stuff or missing... just means off
+ bool onOff = (params > 0) && (p[1] == 1 || p[1] == "ON"_hk); // dont care if other stuff or missing... just means off
switch (p[0])
{
- case HASH_KEYWORD_CABS: //
+ case "CABS"_hk: //
DCC::displayCabList(stream);
return true;
- case HASH_KEYWORD_RAM: //
+ case "RAM"_hk: //
DIAG(F("Free memory=%d"), DCCTimer::getMinimumFreeMemory());
return true;
- case HASH_KEYWORD_CMD: //
+ case "CMD"_hk: //
Diag::CMD = onOff;
return true;
#ifdef HAS_ENOUGH_MEMORY
- case HASH_KEYWORD_WIFI: //
+ case "WIFI"_hk: //
Diag::WIFI = onOff;
return true;
- case HASH_KEYWORD_ETHERNET: //
+ case "ETHERNET"_hk: //
Diag::ETHERNET = onOff;
return true;
- case HASH_KEYWORD_WIT: //
+ case "WIT"_hk: //
Diag::WITHROTTLE = onOff;
return true;
- case HASH_KEYWORD_LCN: //
+ case "LCN"_hk: //
Diag::LCN = onOff;
return true;
#endif
#ifndef DISABLE_EEPROM
- case HASH_KEYWORD_EEPROM: //
+ case "EEPROM"_hk: //
if (params >= 2)
EEStore::dump(p[1]);
return true;
#endif
- case HASH_KEYWORD_SERVO: //
+ case "SERVO"_hk: //
- case HASH_KEYWORD_ANOUT: //
+ case "ANOUT"_hk: //
IODevice::writeAnalogue(p[1], p[2], params>3 ? p[3] : 0);
break;
- case HASH_KEYWORD_ANIN: // Display analogue input value
+ case "ANIN"_hk: // Display analogue input value
DIAG(F("VPIN=%u value=%d"), p[1], IODevice::readAnalogue(p[1]));
break;
#if !defined(IO_NO_HAL)
- case HASH_KEYWORD_HAL:
- if (p[1] == HASH_KEYWORD_SHOW)
+ case "HAL"_hk:
+ if (p[1] == "SHOW"_hk)
IODevice::DumpAll();
- else if (p[1] == HASH_KEYWORD_RESET)
+ else if (p[1] == "RESET"_hk)
IODevice::reset();
break;
#endif
- case HASH_KEYWORD_TT: //
+ case "TT"_hk: //
IODevice::writeAnalogue(p[1], p[2], params>3 ? p[3] : 0);
break;
@@ -1232,7 +1182,7 @@ bool DCCEXParser::parseI(Print *stream, int16_t params, int16_t p[])
case 3: // | - rotate to position for EX-Turntable or create DCC turntable
{
Turntable *tto = Turntable::get(p[0]);
- if (p[1] == HASH_KEYWORD_DCC) {
+ if (p[1] == "DCC"_hk) {
if (tto || p[2] < 0 || p[2] > 3600) return false;
if (!DCCTurntable::create(p[0])) return false;
Turntable *tto = Turntable::get(p[0]);
@@ -1249,7 +1199,7 @@ bool DCCEXParser::parseI(Print *stream, int16_t params, int16_t p[])
case 4: // create an EXTT turntable
{
Turntable *tto = Turntable::get(p[0]);
- if (p[1] == HASH_KEYWORD_EXTT) {
+ if (p[1] == "EXTT"_hk) {
if (tto || p[3] < 0 || p[3] > 3600) return false;
if (!EXTTTurntable::create(p[0], (VPIN)p[2])) return false;
Turntable *tto = Turntable::get(p[0]);
@@ -1264,7 +1214,7 @@ bool DCCEXParser::parseI(Print *stream, int16_t params, int16_t p[])
case 5: // add a position
{
Turntable *tto = Turntable::get(p[0]);
- if (p[1] == HASH_KEYWORD_ADD) {
+ if (p[1] == "ADD"_hk) {
// tto must exist, no more than 48 positions, angle 0 - 3600
if (!tto || p[2] > 48 || p[4] < 0 || p[4] > 3600) return false;
tto->addPosition(p[2], p[3], p[4]);
diff --git a/EXRAIL2Parser.cpp b/EXRAIL2Parser.cpp
index b049699..872d27a 100644
--- a/EXRAIL2Parser.cpp
+++ b/EXRAIL2Parser.cpp
@@ -28,25 +28,7 @@
#include "defines.h"
#include "EXRAIL2.h"
#include "DCC.h"
-// Command parsing keywords
-const int16_t HASH_KEYWORD_EXRAIL=15435;
-const int16_t HASH_KEYWORD_ON = 2657;
-const int16_t HASH_KEYWORD_START=23232;
-const int16_t HASH_KEYWORD_RESERVE=11392;
-const int16_t HASH_KEYWORD_FREE=-23052;
-const int16_t HASH_KEYWORD_LATCH=1618;
-const int16_t HASH_KEYWORD_UNLATCH=1353;
-const int16_t HASH_KEYWORD_PAUSE=-4142;
-const int16_t HASH_KEYWORD_RESUME=27609;
-const int16_t HASH_KEYWORD_KILL=5218;
-const int16_t HASH_KEYWORD_ALL=3457;
-const int16_t HASH_KEYWORD_ROUTES=-3702;
-const int16_t HASH_KEYWORD_RED=26099;
-const int16_t HASH_KEYWORD_AMBER=18713;
-const int16_t HASH_KEYWORD_GREEN=-31493;
-const int16_t HASH_KEYWORD_A='A';
-const int16_t HASH_KEYWORD_M='M';
-
+#include "KeywordHasher.h"
// This filter intercepts <> commands to do the following:
// - Implement RMFT specific commands/diagnostics
@@ -58,8 +40,8 @@ void RMFT2::ComandFilter(Print * stream, byte & opcode, byte & paramCount, int16
switch(opcode) {
case 'D':
- if (p[0]==HASH_KEYWORD_EXRAIL) { //
- diag = paramCount==2 && (p[1]==HASH_KEYWORD_ON || p[1]==1);
+ if (p[0]=="EXRAIL"_hk) { //
+ diag = paramCount==2 && (p[1]=="ON"_hk || p[1]==1);
opcode=0;
}
break;
@@ -125,7 +107,7 @@ void RMFT2::ComandFilter(Print * stream, byte & opcode, byte & paramCount, int16
case 'J': // throttle info commands
if (paramCount<1) return;
switch(p[0]) {
- case HASH_KEYWORD_A: // returns automations/routes
+ case "A"_hk: // returns automations/routes
if (paramCount==1) {//
StringFormatter::send(stream, F("stream(stream);
@@ -152,7 +134,7 @@ void RMFT2::ComandFilter(Print * stream, byte & opcode, byte & paramCount, int16
return;
}
break;
- case HASH_KEYWORD_M:
+ case "M"_hk:
// NOTE: we only need to handle valid calls here because
// DCCEXParser has to have code to handle the cases where
// exrail isnt involved anyway.
@@ -236,13 +218,13 @@ bool RMFT2::parseSlash(Print * stream, byte & paramCount, int16_t p[]) {
return true;
}
switch (p[0]) {
- case HASH_KEYWORD_PAUSE: // PAUSE>
+ case "PAUSE"_hk: // PAUSE>
if (paramCount!=1) return false;
DCC::setThrottle(0,1,true); // pause all locos on the track
pausingTask=(RMFT2 *)1; // Impossible task address
return true;
- case HASH_KEYWORD_RESUME: // RESUME>
+ case "RESUME"_hk: // RESUME>
if (paramCount!=1) return false;
pausingTask=NULL;
{
@@ -256,7 +238,7 @@ bool RMFT2::parseSlash(Print * stream, byte & paramCount, int16_t p[]) {
return true;
- case HASH_KEYWORD_START: // START [cab] route >
+ case "START"_hk: // START [cab] route >
if (paramCount<2 || paramCount>3) return false;
{
int route=(paramCount==2) ? p[1] : p[2];
@@ -273,7 +255,7 @@ bool RMFT2::parseSlash(Print * stream, byte & paramCount, int16_t p[]) {
}
// check KILL ALL here, otherwise the next validation confuses ALL with a flag
- if (p[0]==HASH_KEYWORD_KILL && p[1]==HASH_KEYWORD_ALL) {
+ if (p[0]=="KILL"_hk && p[1]=="ALL"_hk) {
while (loopTask) loopTask->kill(F("KILL ALL")); // destructor changes loopTask
return true;
}
@@ -282,7 +264,7 @@ bool RMFT2::parseSlash(Print * stream, byte & paramCount, int16_t p[]) {
if (paramCount!=2 ) return false;
switch (p[0]) {
- case HASH_KEYWORD_KILL: // Kill taskid|ALL
+ case "KILL"_hk: // Kill taskid|ALL
{
if ( p[1]<0 || p[1]>=MAX_FLAGS) return false;
RMFT2 * task=loopTask;
@@ -297,27 +279,27 @@ bool RMFT2::parseSlash(Print * stream, byte & paramCount, int16_t p[]) {
}
return false;
- case HASH_KEYWORD_RESERVE: // force reserve a section
+ case "RESERVE"_hk: // force reserve a section
return setFlag(p[1],SECTION_FLAG);
- case HASH_KEYWORD_FREE: // force free a section
+ case "FREE"_hk: // force free a section
return setFlag(p[1],0,SECTION_FLAG);
- case HASH_KEYWORD_LATCH:
+ case "LATCH"_hk:
return setFlag(p[1], LATCH_FLAG);
- case HASH_KEYWORD_UNLATCH:
+ case "UNLATCH"_hk:
return setFlag(p[1], 0, LATCH_FLAG);
- case HASH_KEYWORD_RED:
+ case "RED"_hk:
doSignal(p[1],SIGNAL_RED);
return true;
- case HASH_KEYWORD_AMBER:
+ case "AMBER"_hk:
doSignal(p[1],SIGNAL_AMBER);
return true;
- case HASH_KEYWORD_GREEN:
+ case "GREEN"_hk:
doSignal(p[1],SIGNAL_GREEN);
return true;
diff --git a/KeywordHasher.h b/KeywordHasher.h
new file mode 100644
index 0000000..d30f566
--- /dev/null
+++ b/KeywordHasher.h
@@ -0,0 +1,57 @@
+/*
+ * © 2024 Vincent Hamp and Chris Harlow
+ * All rights reserved.
+ *
+ * This file is part of CommandStation-EX
+ *
+ * This is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * It is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with CommandStation. If not, see .
+ */
+
+
+/* Reader be aware:
+ This function implements the _hk data type so that a string keyword
+ is hashed to the same value as the DCCEXParser uses to hash incoming
+ keywords.
+ Thus "MAIN"_hk generates exactly the same run time vakue
+ as const int16_t HASH_KEYWORD_MAIN=11339
+*/
+#ifndef KeywordHAsher_h
+#define KeywordHasher_h
+
+#include
+constexpr uint16_t CompiletimeKeywordHasher(const char * sv, uint16_t running=0) {
+ return (*sv==0) ? running : CompiletimeKeywordHasher(sv+1,
+ (*sv >= '0' && *sv <= '9')
+ ? (10*running+*sv-'0') // Numeric hash
+ : ((running << 5) + running) ^ *sv
+ ); //
+}
+
+constexpr int16_t operator""_hk(const char * keyword, size_t len)
+{
+ return (int16_t) CompiletimeKeywordHasher(keyword,len*0);
+}
+
+/* Some historical values for testing:
+const int16_t HASH_KEYWORD_MAIN = 11339;
+const int16_t HASH_KEYWORD_SLOW = -17209;
+const int16_t HASH_KEYWORD_SPEED28 = -17064;
+const int16_t HASH_KEYWORD_SPEED128 = 25816;
+*/
+
+static_assert("MAIN"_hk == 11339);
+static_assert("SLOW"_hk == -17209);
+static_assert("SPEED28"_hk == -17064);
+static_assert("SPEED128"_hk == 25816);
+#endif
\ No newline at end of file
diff --git a/TrackManager.cpp b/TrackManager.cpp
index 4a501a1..da96832 100644
--- a/TrackManager.cpp
+++ b/TrackManager.cpp
@@ -28,6 +28,7 @@
#include "DIAG.h"
#include "CommandDistributor.h"
#include "DCCEXParser.h"
+#include "KeywordHasher.h"
// Virtualised Motor shield multi-track hardware Interface
#define FOR_EACH_TRACK(t) for (byte t=0;t<=lastTrack;t++)
@@ -35,21 +36,6 @@
FOR_EACH_TRACK(t) \
if (track[t]->getMode()==findmode) \
track[t]->function;
-#ifndef DISABLE_PROG
-const int16_t HASH_KEYWORD_PROG = -29718;
-#endif
-const int16_t HASH_KEYWORD_MAIN = 11339;
-const int16_t HASH_KEYWORD_OFF = 22479;
-const int16_t HASH_KEYWORD_NONE = -26550;
-const int16_t HASH_KEYWORD_DC = 2183;
-const int16_t HASH_KEYWORD_DCX = 6463; // DC reversed polarity
-const int16_t HASH_KEYWORD_EXT = 8201; // External DCC signal
-const int16_t HASH_KEYWORD_A = 65; // parser makes single chars the ascii.
-const int16_t HASH_KEYWORD_AUTO = -5457;
-#ifdef BOOSTER_INPUT
-const int16_t HASH_KEYWORD_BOOST = 11269;
-#endif
-const int16_t HASH_KEYWORD_INV = 11857;
MotorDriver * TrackManager::track[MAX_TRACKS];
int16_t TrackManager::trackDCAddr[MAX_TRACKS];
@@ -362,38 +348,38 @@ bool TrackManager::parseEqualSign(Print *stream, int16_t params, int16_t p[])
}
- p[0]-=HASH_KEYWORD_A; // convert A... to 0....
+ p[0]-="A"_hk; // convert A... to 0....
if (params>1 && (p[0]<0 || p[0]>=MAX_TRACKS))
return false;
- if (params==2 && p[1]==HASH_KEYWORD_MAIN) // <= id MAIN>
+ if (params==2 && p[1]=="MAIN"_hk) // <= id MAIN>
return setTrackMode(p[0],TRACK_MODE_MAIN);
#ifndef DISABLE_PROG
- if (params==2 && p[1]==HASH_KEYWORD_PROG) // <= id PROG>
+ if (params==2 && p[1]=="PROG"_hk) // <= id PROG>
return setTrackMode(p[0],TRACK_MODE_PROG);
#endif
- if (params==2 && (p[1]==HASH_KEYWORD_OFF || p[1]==HASH_KEYWORD_NONE)) // <= id OFF> <= id NONE>
+ if (params==2 && (p[1]=="OFF"_hk || p[1]=="NONE"_hk)) // <= id OFF> <= id NONE>
return setTrackMode(p[0],TRACK_MODE_NONE);
- if (params==2 && p[1]==HASH_KEYWORD_EXT) // <= id EXT>
+ if (params==2 && p[1]=="EXT"_hk) // <= id EXT>
return setTrackMode(p[0],TRACK_MODE_EXT);
#ifdef BOOSTER_INPUT
- if (params==2 && p[1]==HASH_KEYWORD_BOOST) // <= id BOOST>
+ if (params==2 && p[1]=="BOOST"_hk) // <= id BOOST>
return setTrackMode(p[0],TRACK_MODE_BOOST);
#endif
- if (params==2 && p[1]==HASH_KEYWORD_AUTO) // <= id AUTO>
+ if (params==2 && p[1]=="AUTO"_hk) // <= id AUTO>
return setTrackMode(p[0], track[p[0]]->getMode() | TRACK_MODE_AUTOINV);
- if (params==2 && p[1]==HASH_KEYWORD_INV) // <= id AUTO>
+ if (params==2 && p[1]=="INV"_hk) // <= id AUTO>
return setTrackMode(p[0], track[p[0]]->getMode() | TRACK_MODE_INV);
- if (params==3 && p[1]==HASH_KEYWORD_DC && p[2]>0) // <= id DC cab>
+ if (params==3 && p[1]=="DC"_hk && p[2]>0) // <= id DC cab>
return setTrackMode(p[0],TRACK_MODE_DC,p[2]);
- if (params==3 && p[1]==HASH_KEYWORD_DCX && p[2]>0) // <= id DCX cab>
+ if (params==3 && p[1]=="DCX"_hk && p[2]>0) // <= id DCX cab>
return setTrackMode(p[0],TRACK_MODE_DC|TRACK_MODE_INV,p[2]);
return false;
From 43648fd9f4df5a1fdbc9053a95de1b803866792f Mon Sep 17 00:00:00 2001
From: Asbelos
Date: Wed, 10 Jan 2024 12:01:40 +0000
Subject: [PATCH 35/51] 5.2.23
---
version.h | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/version.h b/version.h
index 3c33933..d849eae 100644
--- a/version.h
+++ b/version.h
@@ -3,7 +3,8 @@
#include "StringFormatter.h"
-#define VERSION "5.2.22"
+#define VERSION "5.2.23"
+// 5.2.23 - KeywordHasher _hk (no functional change)
// 5.2.22 - Bugfixes: Empty turnout descriptions ok; negative route numbers valid.
// 5.2.21 - Add STARTUP_DELAY config option to delay CS bootup
// 5.2.20 - Check return of Ethernet.begin()
From d8c282434cea0f939b8768066948b7e55f570952 Mon Sep 17 00:00:00 2001
From: Asbelos
Date: Wed, 10 Jan 2024 12:11:14 +0000
Subject: [PATCH 36/51] _hk in myAutomation
---
DCCEX.h | 1 +
1 file changed, 1 insertion(+)
diff --git a/DCCEX.h b/DCCEX.h
index 2dc8eb7..3aa7b7a 100644
--- a/DCCEX.h
+++ b/DCCEX.h
@@ -49,6 +49,7 @@
#include "CommandDistributor.h"
#include "TrackManager.h"
#include "DCCTimer.h"
+#include "KeywordHasher.h"
#include "EXRAIL.h"
#endif
From 9ab6b3d4eae983f07ed4192c511960581fd03a84 Mon Sep 17 00:00:00 2001
From: Harald Barth
Date: Wed, 10 Jan 2024 15:09:22 +0100
Subject: [PATCH 37/51] Bugfix: Ethernet fixed IP start
---
EthernetInterface.cpp | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/EthernetInterface.cpp b/EthernetInterface.cpp
index 7d8d7b0..34e209a 100644
--- a/EthernetInterface.cpp
+++ b/EthernetInterface.cpp
@@ -47,6 +47,10 @@ void EthernetInterface::setup()
};
+#ifdef IP_ADDRESS
+static IPAddress myIP(IP_ADDRESS);
+#endif
+
/**
* @brief Aquire IP Address from DHCP and start server
*
@@ -60,14 +64,14 @@ EthernetInterface::EthernetInterface()
connected=false;
#ifdef IP_ADDRESS
- if (Ethernet.begin(mac, IP_ADDRESS) == 0)
+ Ethernet.begin(mac, myIP);
#else
if (Ethernet.begin(mac) == 0)
-#endif
{
DIAG(F("Ethernet.begin FAILED"));
return;
}
+#endif
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
DIAG(F("Ethernet shield not found or W5100"));
}
@@ -136,7 +140,7 @@ bool EthernetInterface::checkLink() {
DIAG(F("Ethernet cable connected"));
connected=true;
#ifdef IP_ADDRESS
- Ethernet.setLocalIP(IP_ADDRESS); // for static IP, set it again
+ Ethernet.setLocalIP(myIP); // for static IP, set it again
#endif
IPAddress ip = Ethernet.localIP(); // look what IP was obtained (dynamic or static)
server = new EthernetServer(IP_PORT); // Ethernet Server listening on default port IP_PORT
From d24d09c37a3e2a457bd3f630f64d28827b8221ec Mon Sep 17 00:00:00 2001
From: Harald Barth
Date: Wed, 10 Jan 2024 15:10:25 +0100
Subject: [PATCH 38/51] subversion
---
GITHUB_SHA.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/GITHUB_SHA.h b/GITHUB_SHA.h
index a53b57a..759a0cc 100644
--- a/GITHUB_SHA.h
+++ b/GITHUB_SHA.h
@@ -1 +1 @@
-#define GITHUB_SHA "devel-202401100719Z"
+#define GITHUB_SHA "devel-202401101409Z"
From 20ae915eaf82c85626bf52134c10310d7c3ecef7 Mon Sep 17 00:00:00 2001
From: Harald Barth
Date: Wed, 10 Jan 2024 15:23:52 +0100
Subject: [PATCH 39/51] remove unused ccr_reg variable
---
I2CManager_STM32.h | 1 -
1 file changed, 1 deletion(-)
diff --git a/I2CManager_STM32.h b/I2CManager_STM32.h
index 7e0f547..7e9e63e 100644
--- a/I2CManager_STM32.h
+++ b/I2CManager_STM32.h
@@ -110,7 +110,6 @@ void I2CManagerClass::I2C_setClock(uint32_t i2cClockSpeed) {
// Calculate a rise time appropriate to the requested bus speed
// Use 10x the rise time spec to enable integer divide of 50ns clock period
uint16_t t_rise;
- uint32_t ccr_freq;
while (s->CR1 & I2C_CR1_STOP); // Prevents lockup by guarding further
// writes to CR1 while STOP is being executed!
From 7ae6ff27f5eb24b684e329aba7461a3e9814b7c8 Mon Sep 17 00:00:00 2001
From: Harald Barth
Date: Wed, 10 Jan 2024 15:09:22 +0100
Subject: [PATCH 40/51] Bugfix: Ethernet fixed IP start
---
EthernetInterface.cpp | 19 +++++++++++--------
1 file changed, 11 insertions(+), 8 deletions(-)
diff --git a/EthernetInterface.cpp b/EthernetInterface.cpp
index 914dd8c..6c8e8fa 100644
--- a/EthernetInterface.cpp
+++ b/EthernetInterface.cpp
@@ -50,6 +50,10 @@ void EthernetInterface::setup()
};
+#ifdef IP_ADDRESS
+static IPAddress myIP(IP_ADDRESS);
+#endif
+
/**
* @brief Aquire IP Address from DHCP and start server
*
@@ -71,33 +75,32 @@ EthernetInterface::EthernetInterface()
// Which seems more useful! We should propose the patch... so the following line actually works!
netif_set_hostname(&gnetif, WIFI_HOSTNAME); // Should probably be passed in the contructor...
#ifdef IP_ADDRESS
- if (Ethernet.begin(IP_ADDRESS) == 0)
+ Ethernet.begin(myIP);
#else
if (Ethernet.begin() == 0)
- #endif // IP_ADDRESS
{
DIAG(F("Ethernet.begin FAILED"));
return;
}
+ #endif // IP_ADDRESS
#else // All other architectures
byte mac[6]= { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
DIAG(F("Ethernet attempting to get MAC address"));
DCCTimer::getSimulatedMacAddress(mac);
DIAG(F("Ethernet got MAC address"));
#ifdef IP_ADDRESS
- if (Ethernet.begin(mac, IP_ADDRESS) == 0)
+ Ethernet.begin(mac, myIP);
#else
if (Ethernet.begin(mac) == 0)
- #endif
{
DIAG(F("Ethernet.begin FAILED"));
return;
- }
-
+ }
+ #endif // IP_ADDRESS
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
DIAG(F("Ethernet shield not found or W5100"));
}
-#endif
+#endif STM32_ETHERNET
uint32_t startmilli = millis();
while ((millis() - startmilli) < 5500) { // Loop to give time to check for cable connection
@@ -163,7 +166,7 @@ bool EthernetInterface::checkLink() {
DIAG(F("Ethernet cable connected"));
connected=true;
#ifdef IP_ADDRESS
- Ethernet.setLocalIP(IP_ADDRESS); // for static IP, set it again
+ Ethernet.setLocalIP(myIP); // for static IP, set it again
#endif
IPAddress ip = Ethernet.localIP(); // look what IP was obtained (dynamic or static)
server = new EthernetServer(IP_PORT); // Ethernet Server listening on default port IP_PORT
From ba3d29e4b565c2e02f6d87bf5e0947a9129e299a Mon Sep 17 00:00:00 2001
From: Harald Barth
Date: Wed, 10 Jan 2024 16:29:21 +0100
Subject: [PATCH 41/51] STM32Ethernet lib for onboard ethernet is different
---
EthernetInterface.cpp | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/EthernetInterface.cpp b/EthernetInterface.cpp
index 6c8e8fa..ad26778 100644
--- a/EthernetInterface.cpp
+++ b/EthernetInterface.cpp
@@ -100,7 +100,7 @@ EthernetInterface::EthernetInterface()
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
DIAG(F("Ethernet shield not found or W5100"));
}
-#endif STM32_ETHERNET
+#endif // STM32_ETHERNET
uint32_t startmilli = millis();
while ((millis() - startmilli) < 5500) { // Loop to give time to check for cable connection
@@ -166,8 +166,10 @@ bool EthernetInterface::checkLink() {
DIAG(F("Ethernet cable connected"));
connected=true;
#ifdef IP_ADDRESS
+ #ifndef STM32_ETHERNET
Ethernet.setLocalIP(myIP); // for static IP, set it again
#endif
+ #endif
IPAddress ip = Ethernet.localIP(); // look what IP was obtained (dynamic or static)
server = new EthernetServer(IP_PORT); // Ethernet Server listening on default port IP_PORT
server->begin();
From a508ee7055edfbc72de4aa5559f31869f213006d Mon Sep 17 00:00:00 2001
From: Asbelos
Date: Wed, 10 Jan 2024 16:08:11 +0000
Subject: [PATCH 42/51] Fix asserts for Teensy
---
KeywordHasher.h | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/KeywordHasher.h b/KeywordHasher.h
index d30f566..d232bd2 100644
--- a/KeywordHasher.h
+++ b/KeywordHasher.h
@@ -50,8 +50,8 @@ const int16_t HASH_KEYWORD_SPEED28 = -17064;
const int16_t HASH_KEYWORD_SPEED128 = 25816;
*/
-static_assert("MAIN"_hk == 11339);
-static_assert("SLOW"_hk == -17209);
-static_assert("SPEED28"_hk == -17064);
-static_assert("SPEED128"_hk == 25816);
+static_assert("MAIN"_hk == 11339,"Keyword hasher error");
+static_assert("SLOW"_hk == -17209,"Keyword hasher error");
+static_assert("SPEED28"_hk == -17064,"Keyword hasher error");
+static_assert("SPEED128"_hk == 25816,"Keyword hasher error");
#endif
\ No newline at end of file
From 8503835e1c99f8dd5c99dbadd29e5058502e9bcf Mon Sep 17 00:00:00 2001
From: pmantoine
Date: Sat, 13 Jan 2024 10:51:27 +0800
Subject: [PATCH 43/51] STM32 I2C pullups config turned off for testing
---
I2CManager_STM32.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/I2CManager_STM32.h b/I2CManager_STM32.h
index 9350d14..ea56fec 100644
--- a/I2CManager_STM32.h
+++ b/I2CManager_STM32.h
@@ -185,7 +185,7 @@ void I2CManagerClass::I2C_init()
GPIOB->OTYPER |= (1<<8) | (1<<9); // PB8 and PB9 set to open drain output capability
GPIOB->OSPEEDR |= (3<<(8*2)) | (3<<(9*2)); // PB8 and PB9 set to High Speed mode
GPIOB->PUPDR &= ~((3<<(8*2)) | (3<<(9*2))); // Clear all PUPDR bits for PB8 and PB9
- GPIOB->PUPDR |= (1<<(8*2)) | (1<<(9*2)); // PB8 and PB9 set to pull-up capability
+ // GPIOB->PUPDR |= (1<<(8*2)) | (1<<(9*2)); // PB8 and PB9 set to pull-up capability
// Alt Function High register routing pins PB8 and PB9 for I2C1:
// Bits (3:2:1:0) = 0:1:0:0 --> AF4 for pin PB8
// Bits (7:6:5:4) = 0:1:0:0 --> AF4 for pin PB9
From a54a262f6841c7a1b150b171e1a69bebbf3ead96 Mon Sep 17 00:00:00 2001
From: Asbelos
Date: Sun, 14 Jan 2024 02:03:42 +0000
Subject: [PATCH 44/51] 5.2.24 EXRAIL asserts
---
EXRAILMacros.h | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++
version.h | 6 ++++-
2 files changed, 69 insertions(+), 1 deletion(-)
diff --git a/EXRAILMacros.h b/EXRAILMacros.h
index f79693d..2c18ae0 100644
--- a/EXRAILMacros.h
+++ b/EXRAILMacros.h
@@ -74,6 +74,70 @@
#define ALIAS(name,value...) const int name= 1##value##0 ==10 ? -__COUNTER__ : value##0/10;
#include "myAutomation.h"
+// Pass 1d Detect sequence duplicates.
+// This pass generates no runtime data or code
+#include "EXRAIL2MacroReset.h"
+#undef AUTOMATION
+#define AUTOMATION(id, description) id,
+#undef ROUTE
+#define ROUTE(id, description) id,
+#undef SEQUENCE
+#define SEQUENCE(id) id,
+constexpr int16_t compileTimeSequenceList[]={
+ #include "myAutomation.h"
+ 0
+ };
+constexpr int16_t stuffSize=sizeof(compileTimeSequenceList)/sizeof(int16_t) - 1;
+
+
+// Compile time function to check for sequence nos.
+constexpr bool hasseq(const int16_t value, const uint16_t pos=0 ) {
+ return pos>=stuffSize? false :
+ compileTimeSequenceList[pos]==value
+ || hasseq(value,pos+1);
+}
+
+// Compile time function to check for duplicate sequence nos.
+constexpr bool hasdup(const int16_t value, const uint16_t pos ) {
+ return pos>=stuffSize? false :
+ compileTimeSequenceList[pos]==value
+ || hasseq(value,pos+1)
+ || hasdup(compileTimeSequenceList[pos],pos+1);
+}
+
+
+static_assert(!hasdup(compileTimeSequenceList[0],1),"Duplicate SEQUENCE/ROUTE/AUTOMATION detected");
+
+//pass 1s static asserts to
+// - check call and follows etc for existing sequence numbers
+// - check range on LATCH/UNLATCH
+// This pass generates no runtime data or code
+#include "EXRAIL2MacroReset.h"
+#undef CALL
+#define CALL(id) static_assert(hasseq(id),"Sequence not found");
+#undef FOLLOW
+#define FOLLOW(id) static_assert(hasseq(id),"Sequence not found");
+#undef START
+#define START(id) static_assert(hasseq(id),"Sequence not found");
+#undef SENDLOCO
+#define SENDLOCO(cab,id) static_assert(hasseq(id),"Sequence not found");
+#undef LATCH
+#define LATCH(id) static_assert(id>=0 && id=0 && id=0 && id=0 && id=0 && speed<128,"Speed out of valid range 0-127");
+#undef FWD
+#define FWD(speed) static_assert(speed>=0 && speed<128,"Speed out of valid range 0-127");
+#undef REV
+#define REV(speed) static_assert(speed>=0 && speed<128,"Speed out of valid range 0-127");
+
+#include "myAutomation.h"
+
// Pass 1h Implements HAL macro by creating exrailHalSetup function
// Also allows creating EXTurntable object
#include "EXRAIL2MacroReset.h"
diff --git a/version.h b/version.h
index d849eae..15b7e5d 100644
--- a/version.h
+++ b/version.h
@@ -3,7 +3,11 @@
#include "StringFormatter.h"
-#define VERSION "5.2.23"
+#define VERSION "5.2.24"
+// 5.2.24 - Exrail macro asserts to catch
+// : duplicate/missing automation/route/sequence/call ids
+// : latches and reserves out of range
+// : speeds out of range
// 5.2.23 - KeywordHasher _hk (no functional change)
// 5.2.22 - Bugfixes: Empty turnout descriptions ok; negative route numbers valid.
// 5.2.21 - Add STARTUP_DELAY config option to delay CS bootup
From 8216579f62e166bcaf474fa182162233d1cbbec2 Mon Sep 17 00:00:00 2001
From: Asbelos
Date: Sun, 14 Jan 2024 02:09:22 +0000
Subject: [PATCH 45/51] 5.2.25 returns bugs
---
DCCEXParser.cpp | 8 ++++----
version.h | 3 ++-
2 files changed, 6 insertions(+), 5 deletions(-)
diff --git a/DCCEXParser.cpp b/DCCEXParser.cpp
index 47665c5..16f4494 100644
--- a/DCCEXParser.cpp
+++ b/DCCEXParser.cpp
@@ -1113,11 +1113,11 @@ bool DCCEXParser::parseD(Print *stream, int16_t params, int16_t p[])
case "ANOUT"_hk: //
IODevice::writeAnalogue(p[1], p[2], params>3 ? p[3] : 0);
- break;
+ return true;
case "ANIN"_hk: // Display analogue input value
DIAG(F("VPIN=%u value=%d"), p[1], IODevice::readAnalogue(p[1]));
- break;
+ return true;
#if !defined(IO_NO_HAL)
case "HAL"_hk:
@@ -1125,12 +1125,12 @@ bool DCCEXParser::parseD(Print *stream, int16_t params, int16_t p[])
IODevice::DumpAll();
else if (p[1] == "RESET"_hk)
IODevice::reset();
- break;
+ return true;
#endif
case "TT"_hk: //
IODevice::writeAnalogue(p[1], p[2], params>3 ? p[3] : 0);
- break;
+ return true;
default: // invalid/unknown
return parseC(stream, params, p);
diff --git a/version.h b/version.h
index 15b7e5d..33c6164 100644
--- a/version.h
+++ b/version.h
@@ -3,7 +3,8 @@
#include "StringFormatter.h"
-#define VERSION "5.2.24"
+#define VERSION "5.2.25"
+// 5.2.25 - Fix bug causing after working
Date: Sun, 14 Jan 2024 20:20:22 +0000
Subject: [PATCH 46/51] HAL defaults control
---
EXRAIL2MacroReset.h | 2 ++
EXRAILMacros.h | 7 ++++++-
IODevice.cpp | 44 ++++++++++++++++++++++----------------------
IODevice.h | 3 ++-
version.h | 4 +++-
5 files changed, 35 insertions(+), 25 deletions(-)
diff --git a/EXRAIL2MacroReset.h b/EXRAIL2MacroReset.h
index 7811a0d..3554f6c 100644
--- a/EXRAIL2MacroReset.h
+++ b/EXRAIL2MacroReset.h
@@ -67,6 +67,7 @@
#undef FWD
#undef GREEN
#undef HAL
+#undef HAL_IGNORE_DEFAULTS
#undef IF
#undef IFAMBER
#undef IFCLOSED
@@ -218,6 +219,7 @@
#define FWD(speed)
#define GREEN(signal_id)
#define HAL(haltype,params...)
+#define HAL_IGNORE_DEFAULTS
#define IF(sensor_id)
#define IFAMBER(signal_id)
#define IFCLOSED(turnout_id)
diff --git a/EXRAILMacros.h b/EXRAILMacros.h
index 2c18ae0..8ed2b82 100644
--- a/EXRAILMacros.h
+++ b/EXRAILMacros.h
@@ -143,8 +143,12 @@ static_assert(!hasdup(compileTimeSequenceList[0],1),"Duplicate SEQUENCE/ROUTE/AU
#include "EXRAIL2MacroReset.h"
#undef HAL
#define HAL(haltype,params...) haltype::create(params);
-void exrailHalSetup() {
+#undef HAL_IGNORE_DEFAULTS
+#define HAL_IGNORE_DEFAULTS ignore_defaults=true;
+bool exrailHalSetup() {
+ bool ignore_defaults=false;
#include "myAutomation.h"
+ return ignore_defaults;
}
// Pass 1c detect compile time featurtes
@@ -460,6 +464,7 @@ int RMFT2::onLCCLookup[RMFT2::countLCCLookup];
#define FWD(speed) OPCODE_FWD,V(speed),
#define GREEN(signal_id) OPCODE_GREEN,V(signal_id),
#define HAL(haltype,params...)
+#define HAL_IGNORE_DEFAULTS
#define IF(sensor_id) OPCODE_IF,V(sensor_id),
#define IFAMBER(signal_id) OPCODE_IFAMBER,V(signal_id),
#define IFCLOSED(turnout_id) OPCODE_IFCLOSED,V(turnout_id),
diff --git a/IODevice.cpp b/IODevice.cpp
index e811fff..99199aa 100644
--- a/IODevice.cpp
+++ b/IODevice.cpp
@@ -33,7 +33,7 @@
// Link to halSetup function. If not defined, the function reference will be NULL.
extern __attribute__((weak)) void halSetup();
-extern __attribute__((weak)) void exrailHalSetup();
+extern __attribute__((weak)) bool exrailHalSetup();
//==================================================================================================================
// Static methods
@@ -60,34 +60,31 @@ void IODevice::begin() {
halSetup();
// include any HAL devices defined in exrail.
+ bool ignoreDefaults=false;
if (exrailHalSetup)
- exrailHalSetup();
-
+ ignoreDefaults=exrailHalSetup();
+ if (ignoreDefaults) return;
+
// Predefine two PCA9685 modules 0x40-0x41 if no conflicts
// Allocates 32 pins 100-131
- if (checkNoOverlap(100, 16, 0x40)) {
+ const bool silent=true; // no message if these conflict
+ if (checkNoOverlap(100, 16, 0x40, silent)) {
PCA9685::create(100, 16, 0x40);
- } else {
- DIAG(F("Default PCA9685 at I2C 0x40 disabled due to configured user device"));
- }
- if (checkNoOverlap(116, 16, 0x41)) {
+ }
+
+ if (checkNoOverlap(116, 16, 0x41, silent)) {
PCA9685::create(116, 16, 0x41);
- } else {
- DIAG(F("Default PCA9685 at I2C 0x41 disabled due to configured user device"));
- }
+ }
// Predefine two MCP23017 module 0x20/0x21 if no conflicts
// Allocates 32 pins 164-195
- if (checkNoOverlap(164, 16, 0x20)) {
+ if (checkNoOverlap(164, 16, 0x20, silent)) {
MCP23017::create(164, 16, 0x20);
- } else {
- DIAG(F("Default MCP23017 at I2C 0x20 disabled due to configured user device"));
- }
- if (checkNoOverlap(180, 16, 0x21)) {
+ }
+
+ if (checkNoOverlap(180, 16, 0x21, silent)) {
MCP23017::create(180, 16, 0x21);
- } else {
- DIAG(F("Default MCP23017 at I2C 0x21 disabled due to configured user device"));
- }
+ }
}
// reset() function to reinitialise all devices
@@ -339,7 +336,10 @@ IODevice *IODevice::findDeviceFollowing(VPIN vpin) {
// returns true if pins DONT overlap with existing device
// TODO: Move the I2C address reservation and checks into the I2CManager code.
// That will enable non-HAL devices to reserve I2C addresses too.
-bool IODevice::checkNoOverlap(VPIN firstPin, uint8_t nPins, I2CAddress i2cAddress) {
+// Silent is used by the default setup so that there is no message if the default
+// device has already been handled by the user setup.
+bool IODevice::checkNoOverlap(VPIN firstPin, uint8_t nPins,
+ I2CAddress i2cAddress, bool silent) {
#ifdef DIAG_IO
DIAG(F("Check no overlap %u %u %s"), firstPin,nPins,i2cAddress.toString());
#endif
@@ -352,14 +352,14 @@ bool IODevice::checkNoOverlap(VPIN firstPin, uint8_t nPins, I2CAddress i2cAddres
VPIN lastDevPin=firstDevPin+dev->_nPins-1;
bool noOverlap= firstPin>lastDevPin || lastPin_I2CAddress==i2cAddress) {
- DIAG(F("WARNING HAL Overlap. i2c Addr %s ignored."),i2cAddress.toString());
+ if (!silent) DIAG(F("WARNING HAL Overlap. i2c Addr %s ignored."),i2cAddress.toString());
return false;
}
}
diff --git a/IODevice.h b/IODevice.h
index d12fafd..6c70f5f 100644
--- a/IODevice.h
+++ b/IODevice.h
@@ -166,7 +166,8 @@ public:
void setGPIOInterruptPin(int16_t pinNumber);
// Method to check if pins will overlap before creating new device.
- static bool checkNoOverlap(VPIN firstPin, uint8_t nPins=1, I2CAddress i2cAddress=0);
+ static bool checkNoOverlap(VPIN firstPin, uint8_t nPins=1,
+ I2CAddress i2cAddress=0, bool silent=false);
// Method used by IODevice filters to locate slave pins that may be overlayed by their own
// pin range.
diff --git a/version.h b/version.h
index 33c6164..8c8ef68 100644
--- a/version.h
+++ b/version.h
@@ -3,7 +3,9 @@
#include "StringFormatter.h"
-#define VERSION "5.2.25"
+#define VERSION "5.2.26"
+// 5.2.26 - Silently ignore overridden HAL defaults
+// - include HAL_IGNORE_DEFAULTS macro in EXRAIL
// 5.2.25 - Fix bug causing after working
Date: Thu, 18 Jan 2024 08:20:33 +0100
Subject: [PATCH 47/51] Bugfix: allocate enough bytes for digital pins. Add
more sanity checks when allocating memory
---
IO_EXIOExpander.h | 33 ++++++++++++++++++++++++---------
1 file changed, 24 insertions(+), 9 deletions(-)
diff --git a/IO_EXIOExpander.h b/IO_EXIOExpander.h
index c8bcba0..b5c40c9 100644
--- a/IO_EXIOExpander.h
+++ b/IO_EXIOExpander.h
@@ -100,8 +100,14 @@ private:
if (_digitalPinBytes < digitalBytesNeeded) {
// Not enough space, free any existing buffer and allocate a new one
if (_digitalPinBytes > 0) free(_digitalInputStates);
- _digitalInputStates = (byte*) calloc(_digitalPinBytes, 1);
- _digitalPinBytes = digitalBytesNeeded;
+ if ((_digitalInputStates = (byte*) calloc(digitalBytesNeeded, 1)) != NULL) {
+ _digitalPinBytes = digitalBytesNeeded;
+ } else {
+ DIAG(F("EX-IOExpander I2C:%s ERROR alloc %d bytes"), _I2CAddress.toString(), digitalBytesNeeded);
+ _deviceState = DEVSTATE_FAILED;
+ _digitalPinBytes = 0;
+ return;
+ }
}
}
@@ -117,7 +123,16 @@ private:
_analogueInputStates = (uint8_t*) calloc(analogueBytesNeeded, 1);
_analogueInputBuffer = (uint8_t*) calloc(analogueBytesNeeded, 1);
_analoguePinMap = (uint8_t*) calloc(_numAnaloguePins, 1);
- _analoguePinBytes = analogueBytesNeeded;
+ if (_analogueInputStates != NULL &&
+ _analogueInputBuffer != NULL &&
+ _analoguePinMap != NULL) {
+ _analoguePinBytes = analogueBytesNeeded;
+ } else {
+ DIAG(F("EX-IOExpander I2C:%s ERROR alloc analog pin bytes"), _I2CAddress.toString());
+ _deviceState = DEVSTATE_FAILED;
+ _analoguePinBytes = 0;
+ return;
+ }
}
}
} else {
@@ -364,14 +379,14 @@ private:
uint8_t _minorVer = 0;
uint8_t _patchVer = 0;
- uint8_t* _digitalInputStates;
- uint8_t* _analogueInputStates;
- uint8_t* _analogueInputBuffer; // buffer for I2C input transfers
+ uint8_t* _digitalInputStates = NULL;
+ uint8_t* _analogueInputStates = NULL;
+ uint8_t* _analogueInputBuffer = NULL; // buffer for I2C input transfers
uint8_t _readCommandBuffer[1];
- uint8_t _digitalPinBytes = 0; // Size of allocated memory buffer (may be longer than needed)
- uint8_t _analoguePinBytes = 0; // Size of allocated memory buffers (may be longer than needed)
- uint8_t* _analoguePinMap;
+ uint8_t _digitalPinBytes = 0; // Size of allocated memory buffer (may be longer than needed)
+ uint8_t _analoguePinBytes = 0; // Size of allocated memory buffer (may be longer than needed)
+ uint8_t* _analoguePinMap = NULL;
I2CRB _i2crb;
enum {RDS_IDLE, RDS_DIGITAL, RDS_ANALOGUE}; // Read operation states
From bc37a2d2cf8d6eea6dd28fc97de7286a444604ce Mon Sep 17 00:00:00 2001
From: Harald Barth
Date: Thu, 18 Jan 2024 08:22:28 +0100
Subject: [PATCH 48/51] version 5.2.27
---
GITHUB_SHA.h | 2 +-
version.h | 3 ++-
2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/GITHUB_SHA.h b/GITHUB_SHA.h
index 759a0cc..24f96c3 100644
--- a/GITHUB_SHA.h
+++ b/GITHUB_SHA.h
@@ -1 +1 @@
-#define GITHUB_SHA "devel-202401101409Z"
+#define GITHUB_SHA "devel-202401180721Z"
diff --git a/version.h b/version.h
index 8c8ef68..3512451 100644
--- a/version.h
+++ b/version.h
@@ -3,7 +3,8 @@
#include "StringFormatter.h"
-#define VERSION "5.2.26"
+#define VERSION "5.2.27"
+// 5.2.27 - Bugfix: IOExpander memory allocation
// 5.2.26 - Silently ignore overridden HAL defaults
// - include HAL_IGNORE_DEFAULTS macro in EXRAIL
// 5.2.25 - Fix bug causing after working
Date: Thu, 18 Jan 2024 18:56:15 +1000
Subject: [PATCH 49/51] Update EX-IOExpander copyright
---
IO_EXIOExpander.h | 15 ++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)
diff --git a/IO_EXIOExpander.h b/IO_EXIOExpander.h
index b5c40c9..191bf3c 100644
--- a/IO_EXIOExpander.h
+++ b/IO_EXIOExpander.h
@@ -1,5 +1,6 @@
/*
* © 2022, Peter Cole. All rights reserved.
+ * © 2022, Harald Barth. All rights reserved.
*
* This file is part of EX-CommandStation
*
@@ -101,13 +102,13 @@ private:
// Not enough space, free any existing buffer and allocate a new one
if (_digitalPinBytes > 0) free(_digitalInputStates);
if ((_digitalInputStates = (byte*) calloc(digitalBytesNeeded, 1)) != NULL) {
- _digitalPinBytes = digitalBytesNeeded;
- } else {
- DIAG(F("EX-IOExpander I2C:%s ERROR alloc %d bytes"), _I2CAddress.toString(), digitalBytesNeeded);
- _deviceState = DEVSTATE_FAILED;
- _digitalPinBytes = 0;
- return;
- }
+ _digitalPinBytes = digitalBytesNeeded;
+ } else {
+ DIAG(F("EX-IOExpander I2C:%s ERROR alloc %d bytes"), _I2CAddress.toString(), digitalBytesNeeded);
+ _deviceState = DEVSTATE_FAILED;
+ _digitalPinBytes = 0;
+ return;
+ }
}
}
From cf1e1c92b3b0f8d084def7f26f7aff5a3dcde3cd Mon Sep 17 00:00:00 2001
From: Harald Barth
Date: Sat, 20 Jan 2024 22:15:47 +0100
Subject: [PATCH 50/51] Check "easy" check first
---
IO_EXIOExpander.h | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/IO_EXIOExpander.h b/IO_EXIOExpander.h
index 191bf3c..2e83eb7 100644
--- a/IO_EXIOExpander.h
+++ b/IO_EXIOExpander.h
@@ -1,6 +1,6 @@
/*
* © 2022, Peter Cole. All rights reserved.
- * © 2022, Harald Barth. All rights reserved.
+ * © 2024, Harald Barth. All rights reserved.
*
* This file is part of EX-CommandStation
*
@@ -257,7 +257,7 @@ private:
// If we're not doing anything now, check to see if a new input transfer is due.
if (_readState == RDS_IDLE) {
- if (currentMicros - _lastDigitalRead > _digitalRefresh && _numDigitalPins>0) { // Delay for digital read refresh
+ if (_numDigitalPins>0 && currentMicros - _lastDigitalRead > _digitalRefresh) { // Delay for digital read refresh
// Issue new read request for digital states. As the request is non-blocking, the buffer has to
// be allocated from heap (object state).
_readCommandBuffer[0] = EXIORDD;
@@ -265,7 +265,7 @@ private:
// non-blocking read
_lastDigitalRead = currentMicros;
_readState = RDS_DIGITAL;
- } else if (currentMicros - _lastAnalogueRead > _analogueRefresh && _numAnaloguePins>0) { // Delay for analogue read refresh
+ } else if (_numAnaloguePins>0 && currentMicros - _lastAnalogueRead > _analogueRefresh) { // Delay for analogue read refresh
// Issue new read for analogue input states
_readCommandBuffer[0] = EXIORDAN;
I2CManager.read(_I2CAddress, _analogueInputBuffer,
From 811bce4b2a8fdd26315fc843183a5ad801981c6e Mon Sep 17 00:00:00 2001
From: Harald Barth
Date: Sat, 20 Jan 2024 22:16:26 +0100
Subject: [PATCH 51/51] tag
---
GITHUB_SHA.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/GITHUB_SHA.h b/GITHUB_SHA.h
index 24f96c3..d15db46 100644
--- a/GITHUB_SHA.h
+++ b/GITHUB_SHA.h
@@ -1 +1 @@
-#define GITHUB_SHA "devel-202401180721Z"
+#define GITHUB_SHA "devel-202401202116Z"