From 620ad6275bef8c3dd5659d913122ef579b347fef Mon Sep 17 00:00:00 2001 From: Asbelos Date: Fri, 28 Feb 2025 01:16:03 +0000 Subject: [PATCH] Railcom3 --- IO_I2CRailcom3.cpp | 134 +++++++++++++++++++++++++++++++++++++++++++++ IO_I2CRailcom3.h | 73 ++++++++++++++++++++++++ 2 files changed, 207 insertions(+) create mode 100644 IO_I2CRailcom3.cpp create mode 100644 IO_I2CRailcom3.h diff --git a/IO_I2CRailcom3.cpp b/IO_I2CRailcom3.cpp new file mode 100644 index 0000000..9938580 --- /dev/null +++ b/IO_I2CRailcom3.cpp @@ -0,0 +1,134 @@ + /* + * © 2024, Henk Kruisbrink & Chris Harlow. All rights reserved. + * © 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 . + */ + +/* + * + * Dec 2023, Added NXP SC16IS752 I2C Dual UART + * 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 + * + * Jan 2024, Issue with using both UARTs simultaniously, the secod uart seems to work but the first transmit + * corrupt data. This need more analysis and experimenatation. + * Will push this driver to the dev branch with the uart fixed to 0 + * Both SC16IS750 (single uart) and SC16IS752 (dual uart, but only uart 0 is enable) + * + * myHall.cpp configuration syntax: + * + * I2CRailcom::create(1st vPin, vPins, I2C address); + * + * myAutomation configuration + * HAL(I2CRailcom, 1st vPin, vPins, I2C address) + * 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 (2 vPins are supported, one for each UART) + * 1st vPin for UART 0, 2nd for UART 1 + * I2C Address : I2C address of the serial controller, in 0x format + */ + +#include "IODevice.h" +#include "IO_I2CRailcom3.h" +#include "I2CManager.h" +#include "DIAG.h" +#include "DCC.h" +#include "DCCWaveform.h" + + +// Debug and diagnostic defines, enable too many will result in slowing the driver +#define DIAG_I2CRailcom + +I2CRailcom3::I2CRailcom3(VPIN firstVpin, int nPins, I2CAddress i2cAddress){ + _firstVpin = firstVpin; + _nPins = nPins; + _I2CAddress = i2cAddress; + addDevice(this); + } + +void I2CRailcom3::create(VPIN firstVpin, int nPins, I2CAddress i2cAddress) { + if (checkNoOverlap(firstVpin, nPins, i2cAddress)) + new I2CRailcom3(firstVpin,nPins,i2cAddress); + } + + void I2CRailcom3::_begin() { + I2CManager.setClock(1000000); // TODO do we need this? + I2CManager.begin(); + auto exists=I2CManager.exists(_I2CAddress); + DIAG(F("I2CRailcom3: %s RailcomCollector %S detected"), + _I2CAddress.toString(), exists?F(""):F(" NOT")); + if (!exists) return; + + _deviceState=DEVSTATE_NORMAL; + _display(); + } + + + void I2CRailcom3::_loop(unsigned long currentMicros) { + // DIAG(F("lopper")); + // Read responses from device + if (_deviceState!=DEVSTATE_NORMAL) return; + + // have we read this cutout already? + // basically we only poll once per packet when railcom cutout is working + auto cut=DCCWaveform::getRailcomCutoutCounter(); + if (cutoutCounter==cut) return; + cutoutCounter=cut; + + // Read data buffer from UART + byte inbuf[1]; + byte queryLength[]={'?'}; + auto state=I2CManager.read(_I2CAddress, inbuf, 1,queryLength,sizeof(queryLength)); + if (state) { + DIAG(F("RC ? state=%d"),state); + return; + } + auto length=inbuf[0]; + if (length==0) return; // nothing to report + + byte inbuf2[length]; + byte queryData[]={'>'}; + state=I2CManager.read(_I2CAddress, inbuf2, length,queryData,sizeof(queryData)); + if (state) { + DIAG(F("RC > %d state=%d"),length,state); + return; + } + // read triplets blk, locohi, locolow from buffer + for (byte i=0;i. + */ + +/* + * + * Dec 2023, Added NXP SC16IS752 I2C Dual UART + * 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 + * + * Jan 2024, Issue with using both UARTs simultaniously, the secod uart seems to work but the first transmit + * corrupt data. This need more analysis and experimenatation. + * Will push this driver to the dev branch with the uart fixed to 0 + * Both SC16IS750 (single uart) and SC16IS752 (dual uart, but only uart 0 is enable) + * + * myHall.cpp configuration syntax: + * + * I2CRailcom::create(1st vPin, vPins, I2C address); + * + * myAutomation configuration + * HAL(I2CRailcom, 1st vPin, vPins, I2C address) + * 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 (2 vPins are supported, one for each UART) + * 1st vPin for UART 0, 2nd for UART 1 + * I2C Address : I2C address of the serial controller, in 0x format + */ + +#ifndef IO_I2CRailcom3_h +#define IO_I2CRailcom3_h +#include "Arduino.h" +#include "IODevice.h" + +// Debug and diagnostic defines, enable too many will result in slowing the driver +#define DIAG_I2CRailcom + +class I2CRailcom3 : public IODevice { +private: + byte cutoutCounter; + public: + // Constructor + I2CRailcom3(VPIN firstVpin, int nPins, I2CAddress i2cAddress); + + static void create(VPIN firstVpin, int nPins, I2CAddress i2cAddress) ; + + void _begin() ; + void _loop(unsigned long currentMicros) override ; + void _display() override ; + +private: + + + +}; + +#endif // IO_I2CRailcom_h