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

Merge branch 'devel-stm32EC' into temp-Ash-0204

This commit is contained in:
Ash-4 2024-02-04 16:30:25 -06:00 committed by GitHub
commit e96ecbffc6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
33 changed files with 1275 additions and 192 deletions

View File

@ -209,7 +209,9 @@ int16_t CommandDistributor::retClockTime() {
void CommandDistributor::broadcastLoco(byte slot) {
DCC::LOCO * sp=&DCC::speedTable[slot];
broadcastReply(COMMAND_TYPE, F("<l %d %d %d %l>\n"), sp->loco,slot,sp->speedCode,sp->functions);
uint32_t func = sp->functions;
func = func & 0x1fffffff; // mask out bits 0-28
broadcastReply(COMMAND_TYPE, F("<l %d %d %d %l>\n"), sp->loco,slot,sp->speedCode,func);
#ifdef SABERTOOTH
if (Serial2 && sp->loco == SABERTOOTH) {
static uint8_t rampingmode = 0;

45
DCC.cpp
View File

@ -153,6 +153,22 @@ uint8_t DCC::getThrottleSpeedByte(int cab) {
return speedTable[reg].speedCode;
}
// returns 0 to 7 for frequency
uint8_t DCC::getThrottleFrequency(int cab) {
#if defined(ARDUINO_AVR_UNO)
(void)cab;
return 0;
#else
int reg=lookupSpeedTable(cab);
if (reg<0)
return 0; // use default frequency
// shift out first 29 bits so we have the 3 "frequency bits" left
uint8_t res = (uint8_t)(speedTable[reg].functions >>29);
//DIAG(F("Speed table %d functions %l shifted %d"), reg, speedTable[reg].functions, res);
return res;
#endif
}
// returns direction on loco
// or true/forward on "loco not found"
bool DCC::getThrottleDirection(int cab) {
@ -183,43 +199,54 @@ bool DCC::setFn( int cab, int16_t functionNumber, bool on) {
b[nB++] = functionNumber >>7 ; // high order bits
}
DCCWaveform::mainTrack.schedulePacket(b, nB, 4);
return true;
}
// We use the reminder table up to 28 for normal functions.
// We use 29 to 31 for DC frequency as well so up to 28
// are "real" functions and 29 to 31 are frequency bits
// controlled by function buttons
if (functionNumber > 31)
return true;
int reg = lookupSpeedTable(cab);
if (reg<0) return false;
// Take care of functions:
// Set state of function
unsigned long previous=speedTable[reg].functions;
unsigned long funcmask = (1UL<<functionNumber);
uint32_t previous=speedTable[reg].functions;
uint32_t funcmask = (1UL<<functionNumber);
if (on) {
speedTable[reg].functions |= funcmask;
} else {
speedTable[reg].functions &= ~funcmask;
}
if (speedTable[reg].functions != previous) {
if (speedTable[reg].functions != previous && functionNumber <= 28) {
updateGroupflags(speedTable[reg].groupFlags, functionNumber);
CommandDistributor::broadcastLoco(reg);
}
return true;
}
// Flip function state
// Flip function state (used from withrottle protocol)
void DCC::changeFn( int cab, int16_t functionNumber) {
if (cab<=0 || functionNumber>28) return;
if (cab<=0 || functionNumber>31) return;
int reg = lookupSpeedTable(cab);
if (reg<0) return;
unsigned long funcmask = (1UL<<functionNumber);
speedTable[reg].functions ^= funcmask;
if (functionNumber <= 28) {
updateGroupflags(speedTable[reg].groupFlags, functionNumber);
CommandDistributor::broadcastLoco(reg);
}
}
int DCC::getFn( int cab, int16_t functionNumber) {
if (cab<=0 || functionNumber>28) return -1; // unknown
// Report function state (used from withrottle protocol)
// returns 0 false, 1 true or -1 for do not know
int8_t DCC::getFn( int cab, int16_t functionNumber) {
if (cab<=0 || functionNumber>28)
return -1; // unknown
int reg = lookupSpeedTable(cab);
if (reg<0) return -1;
if (reg<0)
return -1;
unsigned long funcmask = (1UL<<functionNumber);
return (speedTable[reg].functions & funcmask)? 1 : 0;

5
DCC.h
View File

@ -61,13 +61,14 @@ public:
static void setThrottle(uint16_t cab, uint8_t tSpeed, bool tDirection);
static int8_t getThrottleSpeed(int cab);
static uint8_t getThrottleSpeedByte(int cab);
static uint8_t getThrottleFrequency(int cab);
static bool getThrottleDirection(int cab);
static void writeCVByteMain(int cab, int cv, byte bValue);
static void writeCVBitMain(int cab, int cv, byte bNum, bool bValue);
static void setFunction(int cab, byte fByte, byte eByte);
static bool setFn(int cab, int16_t functionNumber, bool on);
static void changeFn(int cab, int16_t functionNumber);
static int getFn(int cab, int16_t functionNumber);
static int8_t getFn(int cab, int16_t functionNumber);
static uint32_t getFunctionMap(int cab);
static void updateGroupflags(byte &flags, int16_t functionNumber);
static void setAccessory(int address, byte port, bool gate, byte onoff = 2);
@ -98,7 +99,7 @@ public:
int loco;
byte speedCode;
byte groupFlags;
unsigned long functions;
uint32_t functions;
};
static LOCO speedTable[MAX_LOCOS];
static int lookupSpeedTable(int locoId, bool autoCreate=true);

View File

@ -575,12 +575,13 @@ void DCCEXParser::parseOne(Print *stream, byte *com, RingStream * ringStream)
DCC::setThrottle(0,1,1); // this broadcasts speed 1(estop) and sets all reminders to speed 1.
return;
#ifdef HAS_ENOUGH_MEMORY
case 'c': // SEND METER RESPONSES <c>
// No longer useful because of multiple tracks See <JG> and <JI>
if (params>0) break;
TrackManager::reportObsoleteCurrent(stream);
return;
#endif
case 'Q': // SENSORS <Q>
Sensor::printAll(stream);
return;

View File

@ -85,6 +85,7 @@ class DCCTimer {
static void reset();
private:
static void DCCEXanalogWriteFrequencyInternal(uint8_t pin, uint32_t frequency);
static int freeMemory();
static volatile int minimum_free_memory;
static const int DCC_SIGNAL_TIME=58; // this is the 58uS DCC 1-bit waveform half-cycle

View File

@ -29,6 +29,7 @@
#include <avr/boot.h>
#include <avr/wdt.h>
#include "DCCTimer.h"
#include "DIAG.h"
#ifdef DEBUG_ADC
#include "TrackManager.h"
#endif
@ -125,6 +126,81 @@ void DCCTimer::reset() {
}
void DCCTimer::DCCEXanalogWriteFrequency(uint8_t pin, uint32_t f) {
DCCTimer::DCCEXanalogWriteFrequencyInternal(pin, f);
}
void DCCTimer::DCCEXanalogWriteFrequencyInternal(uint8_t pin, uint32_t fbits) {
#if defined(ARDUINO_AVR_UNO)
// Not worth doin something here as:
// If we are on pin 9 or 10 we are on Timer1 and we can not touch Timer1 as that is our DCC source.
// If we are on pin 5 or 6 we are on Timer 0 ad we can not touch Timer0 as that is millis() etc.
// We are most likely not on pin 3 or 11 as no known motor shield has that as brake.
#endif
#if defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560)
// Speed mapping is done like this:
// No functions buttons: 000 0 -> low 131Hz
// Only F29 pressed 001 1 -> mid 490Hz
// F30 with or w/o F29 01x 2-3 -> high 3400Hz
// F31 with or w/o F29/30 1xx 4-7 -> supersonic 62500Hz
uint8_t abits;
uint8_t bbits;
if (pin == 9 || pin == 10) { // timer 2 is different
if (fbits >= 4)
abits = B00000011;
else
abits = B00000001;
if (fbits >= 4)
bbits = B0001;
else if (fbits >= 2)
bbits = B0010;
else if (fbits == 1)
bbits = B0100;
else // fbits == 0
bbits = B0110;
TCCR2A = (TCCR2A & B11111100) | abits; // set WGM0 and WGM1
TCCR2B = (TCCR2B & B11110000) | bbits; // set WGM2 and 3 bits of prescaler
DIAG(F("Timer 2 A=%x B=%x"), TCCR2A, TCCR2B);
} else { // not timer 9 or 10
abits = B01;
if (fbits >= 4)
bbits = B1001;
else if (fbits >= 2)
bbits = B0010;
else if (fbits == 1)
bbits = B0011;
else
bbits = B0100;
switch (pin) {
// case 9 and 10 taken care of above by if()
case 6:
case 7:
case 8:
// Timer4
TCCR4A = (TCCR4A & B11111100) | abits; // set WGM0 and WGM1
TCCR4B = (TCCR4B & B11100000) | bbits; // set WGM2 and WGM3 and divisor
//DIAG(F("Timer 4 A=%x B=%x"), TCCR4A, TCCR4B);
break;
case 46:
case 45:
case 44:
// Timer5
TCCR5A = (TCCR5A & B11111100) | abits; // set WGM0 and WGM1
TCCR5B = (TCCR5B & B11100000) | bbits; // set WGM2 and WGM3 and divisor
//DIAG(F("Timer 5 A=%x B=%x"), TCCR5A, TCCR5B);
break;
default:
break;
}
}
#endif
}
#if defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560)
#define NUM_ADC_INPUTS 16
#else

View File

@ -151,10 +151,26 @@ void DCCTimer::reset() {
ESP.restart();
}
void DCCTimer::DCCEXanalogWriteFrequency(uint8_t pin, uint32_t f) {
if (f >= 16)
DCCTimer::DCCEXanalogWriteFrequencyInternal(pin, f);
else if (f == 7)
DCCTimer::DCCEXanalogWriteFrequencyInternal(pin, 62500);
else if (f >= 4)
DCCTimer::DCCEXanalogWriteFrequencyInternal(pin, 32000);
else if (f >= 3)
DCCTimer::DCCEXanalogWriteFrequencyInternal(pin, 16000);
else if (f >= 2)
DCCTimer::DCCEXanalogWriteFrequencyInternal(pin, 3400);
else if (f == 1)
DCCTimer::DCCEXanalogWriteFrequencyInternal(pin, 480);
else
DCCTimer::DCCEXanalogWriteFrequencyInternal(pin, 131);
}
#include "esp32-hal.h"
#include "soc/soc_caps.h"
#ifdef SOC_LEDC_SUPPORT_HS_MODE
#define LEDC_CHANNELS (SOC_LEDC_CHANNEL_NUM<<1)
#else
@ -164,7 +180,7 @@ void DCCTimer::reset() {
static int8_t pin_to_channel[SOC_GPIO_PIN_COUNT] = { 0 };
static int cnt_channel = LEDC_CHANNELS;
void DCCTimer::DCCEXanalogWriteFrequency(uint8_t pin, uint32_t frequency) {
void DCCTimer::DCCEXanalogWriteFrequencyInternal(uint8_t pin, uint32_t frequency) {
if (pin < SOC_GPIO_PIN_COUNT) {
if (pin_to_channel[pin] != 0) {
ledcSetup(pin_to_channel[pin], frequency, 8);

View File

@ -125,6 +125,11 @@ void DCCTimer::reset() {
while(true){}
}
void DCCTimer::DCCEXanalogWriteFrequency(uint8_t pin, uint32_t f) {
}
void DCCTimer::DCCEXanalogWriteFrequencyInternal(uint8_t pin, uint32_t fbits) {
}
int16_t ADCee::ADCmax() {
return 4095;
}

View File

@ -156,6 +156,11 @@ void DCCTimer::reset() {
while(true) {};
}
void DCCTimer::DCCEXanalogWriteFrequency(uint8_t pin, uint32_t f) {
}
void DCCTimer::DCCEXanalogWriteFrequencyInternal(uint8_t pin, uint32_t fbits) {
}
#define NUM_ADC_INPUTS NUM_ANALOG_INPUTS
uint16_t ADCee::usedpins = 0;

View File

@ -56,9 +56,9 @@ HardwareSerial Serial5(PD2, PC12); // Rx=PD2, Tx=PC12 -- UART5 - F446RE
defined(ARDUINO_NUCLEO_F429ZI) || defined(ARDUINO_NUCLEO_F439ZI)
// Nucleo-144 boards don't have Serial1 defined by default
HardwareSerial Serial6(PG9, PG14); // Rx=PG9, Tx=PG14 -- USART6
HardwareSerial Serial5(PD2, PC12); // Rx=PD2, Tx=PC12 -- UART5
#if !defined(ARDUINO_NUCLEO_F412ZG)
HardwareSerial Serial2(PD6, PD5); // Rx=PD6, Tx=PD5 -- UART5
HardwareSerial Serial2(PD6, PD5); // Rx=PD6, Tx=PD5 -- UART2
#if !defined(ARDUINO_NUCLEO_F412ZG) // F412ZG does not have UART5
HardwareSerial Serial5(PD2, PC12); // Rx=PD2, Tx=PC12 -- UART5
#endif
// 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.
@ -257,6 +257,23 @@ void DCCTimer::reset() {
while(true) {};
}
void DCCTimer::DCCEXanalogWriteFrequency(uint8_t pin, uint32_t f) {
if (f >= 16)
DCCTimer::DCCEXanalogWriteFrequencyInternal(pin, f);
else if (f == 7)
DCCTimer::DCCEXanalogWriteFrequencyInternal(pin, 62500);
else if (f >= 4)
DCCTimer::DCCEXanalogWriteFrequencyInternal(pin, 32000);
else if (f >= 3)
DCCTimer::DCCEXanalogWriteFrequencyInternal(pin, 16000);
else if (f >= 2)
DCCTimer::DCCEXanalogWriteFrequencyInternal(pin, 3400);
else if (f == 1)
DCCTimer::DCCEXanalogWriteFrequencyInternal(pin, 480);
else
DCCTimer::DCCEXanalogWriteFrequencyInternal(pin, 131);
}
// TODO: rationalise the size of these... could really use sparse arrays etc.
static HardwareTimer * pin_timer[100] = {0};
static uint32_t channel_frequency[100] = {0};
@ -267,7 +284,7 @@ static uint32_t pin_channel[100] = {0};
// sophisticated about detecting any clash between the timer we'd like to use for PWM and the ones
// currently used for HA so they don't interfere with one another. For now we'll just make PWM
// work well... then work backwards to integrate with HA mode if we can.
void DCCTimer::DCCEXanalogWriteFrequency(uint8_t pin, uint32_t frequency)
void DCCTimer::DCCEXanalogWriteFrequencyInternal(uint8_t pin, uint32_t frequency)
{
if (pin_timer[pin] == NULL) {
// Automatically retrieve TIM instance and channel associated to pin

View File

@ -141,6 +141,11 @@ void DCCTimer::reset() {
SCB_AIRCR = 0x05FA0004;
}
void DCCTimer::DCCEXanalogWriteFrequency(uint8_t pin, uint32_t f) {
}
void DCCTimer::DCCEXanalogWriteFrequencyInternal(uint8_t pin, uint32_t fbits) {
}
int16_t ADCee::ADCmax() {
return 4095;
}

View File

@ -669,6 +669,45 @@ void RMFT2::loop2() {
}
break;
case OPCODE_SETFREQ:
// Frequency is default 0, or 1, 2,3
//if (loco) DCC::setFn(loco,operand,true);
switch (operand) {
case 0: // default - all F-s off
if (loco) {
DCC::setFn(loco,29,false);
DCC::setFn(loco,30,false);
DCC::setFn(loco,31,false);
}
break;
case 1:
if (loco) {
DCC::setFn(loco,29,true);
DCC::setFn(loco,30,false);
DCC::setFn(loco,31,false);
}
break;
case 2:
if (loco) {
DCC::setFn(loco,29,false);
DCC::setFn(loco,30,true);
DCC::setFn(loco,31,false);
}
break;
case 3:
if (loco) {
DCC::setFn(loco,29,false);
DCC::setFn(loco,30,false);
DCC::setFn(loco,31,true);
}
break;
default:
; // do nothing
break;
}
break;
case OPCODE_RESUME:
pausingTask=NULL;
driveLoco(speedo);

View File

@ -51,7 +51,7 @@ enum OPCODE : byte {OPCODE_THROW,OPCODE_CLOSE,
OPCODE_JOIN,OPCODE_UNJOIN,OPCODE_READ_LOCO1,OPCODE_READ_LOCO2,
#endif
OPCODE_POM,
OPCODE_START,OPCODE_SETLOCO,OPCODE_SENDLOCO,OPCODE_FORGET,
OPCODE_START,OPCODE_SETLOCO,OPCODE_SETFREQ,OPCODE_SENDLOCO,OPCODE_FORGET,
OPCODE_PAUSE, OPCODE_RESUME,OPCODE_POWEROFF,OPCODE_POWERON,
OPCODE_ONCLOSE, OPCODE_ONTHROW, OPCODE_SERVOTURNOUT, OPCODE_PINTURNOUT,
OPCODE_PRINT,OPCODE_DCCACTIVATE,

View File

@ -152,6 +152,7 @@
#undef SET_TRACK
#undef SET_POWER
#undef SETLOCO
#undef SETFREQ
#undef SIGNAL
#undef SIGNALH
#undef SPEED
@ -304,6 +305,7 @@
#define SET_TRACK(track,mode)
#define SET_POWER(track,onoff)
#define SETLOCO(loco)
#define SETFREQ(loco,freq)
#define SIGNAL(redpin,amberpin,greenpin)
#define SIGNALH(redpin,amberpin,greenpin)
#define SPEED(speed)

View File

@ -566,6 +566,7 @@ int RMFT2::onLCCLookup[RMFT2::countLCCLookup];
#define SET_TRACK(track,mode) OPCODE_SET_TRACK,V(TRACK_MODE_##mode <<8 | TRACK_NUMBER_##track),
#define SET_POWER(track,onoff) OPCODE_SET_POWER,V(TRACK_POWER_##onoff),OPCODE_PAD, V(TRACK_NUMBER_##track),
#define SETLOCO(loco) OPCODE_SETLOCO,V(loco),
#define SETFREQ(loco,freq) OPCODE_SETLOCO,V(loco), OPCODE_SETFREQ,V(freq),
#define SIGNAL(redpin,amberpin,greenpin)
#define SIGNALH(redpin,amberpin,greenpin)
#define SPEED(speed) OPCODE_SPEED,V(speed),

View File

@ -1,4 +1,6 @@
/*
* © 2024 Morten "Doc" Nielsen
* © 2023-2024 Paul M. Antoine
* © 2022 Bruno Sanches
* © 2021 Fred Decker
* © 2020-2022 Harald Barth
@ -29,6 +31,10 @@
#include "CommandDistributor.h"
#include "WiThrottle.h"
#include "DCCTimer.h"
#include "MDNS_Generic.h"
EthernetUDP udp;
MDNS mdns(udp);
EthernetInterface * EthernetInterface::singleton=NULL;
/**
@ -41,8 +47,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"));
};
@ -59,24 +68,47 @@ static IPAddress myIP(IP_ADDRESS);
*/
EthernetInterface::EthernetInterface()
{
byte mac[6];
DCCTimer::getSimulatedMacAddress(mac);
connected=false;
#ifdef IP_ADDRESS
#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
Ethernet.begin(myIP);
#else
if (Ethernet.begin() == 0)
{
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
Ethernet.begin(mac, myIP);
#else
#else
if (Ethernet.begin(mac) == 0)
{
DIAG(F("Ethernet.begin FAILED"));
return;
}
#endif
#endif // IP_ADDRESS
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
DIAG(F("Ethernet shield not found or W5100"));
}
#endif // STM32_ETHERNET
unsigned long startmilli = millis();
uint32_t startmilli = millis();
while ((millis() - startmilli) < 5500) { // Loop to give time to check for cable connection
if (Ethernet.linkStatus() == LinkON)
break;
@ -140,20 +172,31 @@ 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
IPAddress ip = Ethernet.localIP(); // look what IP was obtained (dynamic or static)
#endif
server = new EthernetServer(IP_PORT); // Ethernet Server listening on default port IP_PORT
server->begin();
IPAddress ip = Ethernet.localIP(); // look what IP was obtained (dynamic or static)
if (ip[0] == 0)
LCD(4,F("Awaiting DHCP..."));
while (ip[0] == 0) { // wait until we are given an IP address from the DHCP server
// LCD(4,F("."));
ip = Ethernet.localIP(); // look what IP was obtained (dynamic or static)
}
LCD(4,F("IP: %d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);
LCD(5,F("Port:%d"), IP_PORT);
mdns.begin(Ethernet.localIP(), WIFI_HOSTNAME); // hostname
mdns.addServiceRecord(WIFI_HOSTNAME "._withrottle", IP_PORT, MDNSServiceTCP);
// only create a outboundRing it none exists, this may happen if the cable
// gets disconnected and connected again
if(!outboundRing)
outboundRing=new RingStream(OUTBOUND_RING_SIZE);
}
return true;
} else { // connected
} else { // LinkOFF
if (connected) { // Were connected, but no longer without a LINK!
DIAG(F("Ethernet cable disconnected"));
connected=false;
//clean up any client
@ -161,11 +204,13 @@ bool EthernetInterface::checkLink() {
if(clients[socket].connected())
clients[socket].stop();
}
mdns.removeServiceRecord(IP_PORT, MDNSServiceTCP);
// tear down server
delete server;
server = nullptr;
LCD(4,F("IP: None"));
}
}
return false;
}
@ -175,24 +220,35 @@ 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])
{
bool sockfound = false;
for (socket = 0; socket < MAX_SOCK_NUM; socket++) {
if (clients[socket] && (clients[socket] == client)) {
sockfound = true;
if (Diag::ETHERNET) DIAG(F("Ethernet: Old client socket %d"),socket);
break;
}
}
if (!sockfound) { // new client
for (socket = 0; socket < MAX_SOCK_NUM; socket++) {
if (!clients[socket]) {
// 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);
clients[socket] = client;
if (Diag::ETHERNET) DIAG(F("Ethernet: New client socket %d"),socket);
break;
}
}
}
if (socket==MAX_SOCK_NUM) DIAG(F("new Ethernet OVERFLOW"));
}
@ -200,6 +256,18 @@ void EthernetInterface::loop2() {
for (byte socket = 0; socket < MAX_SOCK_NUM; socket++)
{
if (clients[socket]) {
if (!clients[socket].connected()) { // stop any clients which disconnect
CommandDistributor::forget(socket);
clients[socket].stop();
#if defined(ARDUINO_ARCH_AVR)
clients[socket]=NULL;
#else
clients[socket]=(EthernetClient)nullptr;
#endif
//if (Diag::ETHERNET)
DIAG(F("Ethernet: disconnect %d "), socket);
return; // Trick: So that we do not continue in this loop with client that is NULL
}
int available=clients[socket].available();
if (available > 0) {
@ -215,14 +283,7 @@ void EthernetInterface::loop2() {
}
}
// stop any clients which disconnect
for (int socket = 0; socket<MAX_SOCK_NUM; socket++) {
if (clients[socket] && !clients[socket].connected()) {
clients[socket].stop();
CommandDistributor::forget(socket);
if (Diag::ETHERNET) DIAG(F("Ethernet: disconnect %d "), socket);
}
}
mdns.run();
WiThrottle::loop(outboundRing);

View File

@ -35,8 +35,18 @@
#if defined (ARDUINO_TEENSY41)
#include <NativeEthernet.h> //TEENSY Ethernet Treiber
#include <NativeEthernetUdp.h>
#define MAX_SOCK_NUM 4
#elif defined (ARDUINO_NUCLEO_F429ZI) || defined (ARDUINO_NUCLEO_F439ZI)
#include <LwIP.h>
// #include "STM32lwipopts.h"
#include <STM32Ethernet.h>
#include <lwip/netif.h>
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"

View File

@ -1 +1 @@
#define GITHUB_SHA "devel-202402041151Z"
#define GITHUB_SHA "devel-stm32EC 202402041604Z"

View File

@ -66,7 +66,8 @@ static const FSH * guessI2CDeviceType(uint8_t address) {
return F("Real-time clock");
else if (address >= 0x70 && address <= 0x77)
return F("I2C Mux");
else
else if (address >= 0x90 && address <= 0xAE)
return F("UART");
return F("?");
}

View File

@ -39,7 +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_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;
@ -184,7 +184,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

View File

@ -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

316
IO_CMRI.cpp Normal file
View File

@ -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 <https://www.gnu.org/licenses/>.
*/
#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; index<numDataBytes; index++) {
uint8_t value = node->getOutputStates(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;
}
}

293
IO_CMRI.h Normal file
View File

@ -0,0 +1,293 @@
/*
* © 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 <https://www.gnu.org/licenses/>.
*/
/*
* 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);
_nodeListEnd = 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

69
IO_Template.h Normal file
View File

@ -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

View File

@ -325,49 +325,23 @@ uint16_t taurustones[28] = { 165, 175, 196, 220,
220, 196, 175, 165 };
#endif
#endif
void MotorDriver::setDCSignal(byte speedcode) {
void MotorDriver::setDCSignal(byte speedcode, uint8_t frequency /*default =0*/) {
if (brakePin == UNUSED_PIN)
return;
switch(brakePin) {
#if defined(ARDUINO_AVR_UNO)
// Not worth doin something here as:
// If we are on pin 9 or 10 we are on Timer1 and we can not touch Timer1 as that is our DCC source.
// If we are on pin 5 or 6 we are on Timer 0 ad we can not touch Timer0 as that is millis() etc.
// We are most likely not on pin 3 or 11 as no known motor shield has that as brake.
#endif
#if defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560)
case 9:
case 10:
// Timer2 (is differnet)
TCCR2A = (TCCR2A & B11111100) | B00000001; // set WGM1=0 and WGM0=1 phase correct PWM
TCCR2B = (TCCR2B & B11110000) | B00000110; // set WGM2=0 ; set divisor on timer 2 to 1/256 for 122.55Hz
//DIAG(F("2 A=%x B=%x"), TCCR2A, TCCR2B);
break;
case 6:
case 7:
case 8:
// Timer4
TCCR4A = (TCCR4A & B11111100) | B00000001; // set WGM0=1 and WGM1=0 for normal PWM 8-bit
TCCR4B = (TCCR4B & B11100000) | B00000100; // set WGM2=0 and WGM3=0 for normal PWM 8 bit and div 1/256 for 122.55Hz
break;
case 46:
case 45:
case 44:
// Timer5
TCCR5A = (TCCR5A & B11111100) | B00000001; // set WGM0=1 and WGM1=0 for normal PWM 8-bit
TCCR5B = (TCCR5B & B11100000) | B00000100; // set WGM2=0 and WGM3=0 for normal PWM 8 bit and div 1/256 for 122.55Hz
break;
#endif
default:
break;
}
// spedcoode is a dcc speed & direction
byte tSpeed=speedcode & 0x7F; // DCC Speed with 0,1 stop and speed steps 2 to 127
byte tDir=speedcode & 0x80;
byte brake;
if (tSpeed <= 1) brake = 255;
else if (tSpeed >= 127) brake = 0;
else brake = 2 * (128-tSpeed);
if (invertBrake)
brake=255-brake;
{ // new block because of variable f
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_STM32)
{
int f = 131;
int f = frequency;
#ifdef VARIABLE_TONES
if (tSpeed > 2) {
if (tSpeed <= 58) {
@ -375,19 +349,15 @@ void MotorDriver::setDCSignal(byte speedcode) {
}
}
#endif
DCCTimer::DCCEXanalogWriteFrequency(brakePin, f); // set DC PWM frequency to 100Hz XXX May move to setup
}
#endif
if (tSpeed <= 1) brake = 255;
else if (tSpeed >= 127) brake = 0;
else brake = 2 * (128-tSpeed);
if (invertBrake)
brake=255-brake;
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_STM32)
//DIAG(F("Brake pin %d freqency %d"), brakePin, f);
DCCTimer::DCCEXanalogWriteFrequency(brakePin, f); // set DC PWM frequency
DCCTimer::DCCEXanalogWrite(brakePin,brake);
#else
#else // all AVR here
DCCTimer::DCCEXanalogWriteFrequency(brakePin, frequency); // frequency steps
analogWrite(brakePin,brake);
#endif
}
//DIAG(F("DCSignal %d"), speedcode);
if (HAVE_PORTA(fastSignalPin.shadowinout == &PORTA)) {
noInterrupts();
@ -436,58 +406,26 @@ void MotorDriver::throttleInrush(bool on) {
return;
if ( !(trackMode & (TRACK_MODE_MAIN | TRACK_MODE_PROG | TRACK_MODE_EXT)))
return;
byte duty = on ? 208 : 0;
byte duty = on ? 207 : 0; // duty of 81% at 62500Hz this gives pauses of 3usec
if (invertBrake)
duty = 255-duty;
#if defined(ARDUINO_ARCH_ESP32)
if(on) {
DCCTimer::DCCEXanalogWrite(brakePin,duty);
DCCTimer::DCCEXanalogWriteFrequency(brakePin, 62500);
DCCTimer::DCCEXanalogWriteFrequency(brakePin, 7); // 7 means max
} else {
ledcDetachPin(brakePin);
}
#elif defined(ARDUINO_ARCH_STM32)
if(on) {
DCCTimer::DCCEXanalogWriteFrequency(brakePin, 62500);
DCCTimer::DCCEXanalogWriteFrequency(brakePin, 7); // 7 means max
DCCTimer::DCCEXanalogWrite(brakePin,duty);
} else {
pinMode(brakePin, OUTPUT);
}
#else
#else // all AVR here
if(on){
switch(brakePin) {
#if defined(ARDUINO_AVR_UNO)
// Not worth doin something here as:
// If we are on pin 9 or 10 we are on Timer1 and we can not touch Timer1 as that is our DCC source.
// If we are on pin 5 or 6 we are on Timer 0 ad we can not touch Timer0 as that is millis() etc.
// We are most likely not on pin 3 or 11 as no known motor shield has that as brake.
#endif
#if defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560)
case 9:
case 10:
// Timer2 (is different)
TCCR2A = (TCCR2A & B11111100) | B00000011; // set WGM0=1 and WGM1=1 for fast PWM
TCCR2B = (TCCR2B & B11110000) | B00000001; // set WGM2=0 and prescaler div=1 (max)
DIAG(F("2 A=%x B=%x"), TCCR2A, TCCR2B);
break;
case 6:
case 7:
case 8:
// Timer4
TCCR4A = (TCCR4A & B11111100) | B00000001; // set WGM0=1 and WGM1=0 for fast PWM 8-bit
TCCR4B = (TCCR4B & B11100000) | B00001001; // set WGM2=1 and WGM3=0 for fast PWM 8 bit and div=1 (max)
break;
case 46:
case 45:
case 44:
// Timer5
TCCR5A = (TCCR5A & B11111100) | B00000001; // set WGM0=1 and WGM1=0 for fast PWM 8-bit
TCCR5B = (TCCR5B & B11100000) | B00001001; // set WGM2=1 and WGM3=0 for fast PWM 8 bit and div=1 (max)
break;
#endif
default:
break;
}
DCCTimer::DCCEXanalogWriteFrequency(brakePin, 7); // 7 means max
}
analogWrite(brakePin,duty);
#endif

View File

@ -187,7 +187,7 @@ class MotorDriver {
}
};
inline pinpair getSignalPin() { return pinpair(signalPin,signalPin2); };
void setDCSignal(byte speedByte);
void setDCSignal(byte speedByte, uint8_t frequency=0);
void throttleInrush(bool on);
inline void detachDCSignal() {
#if defined(__arm__)

View File

@ -19,6 +19,7 @@
* You should have received a copy of the GNU General Public License
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
*/
#include "defines.h"
#include "TrackManager.h"
#include "FSH.h"
#include "DCCWaveform.h"
@ -188,7 +189,7 @@ void TrackManager::setDCSignal(int16_t cab, byte speedbyte) {
FOR_EACH_TRACK(t) {
if (trackDCAddr[t]!=cab && cab != 0) continue;
if (track[t]->getMode() & TRACK_MODE_DC)
track[t]->setDCSignal(speedbyte);
track[t]->setDCSignal(speedbyte, DCC::getThrottleFrequency(trackDCAddr[t]));
}
}
@ -334,8 +335,8 @@ bool TrackManager::setTrackMode(byte trackToSet, TRACK_MODE mode, int16_t dcAddr
}
void TrackManager::applyDCSpeed(byte t) {
uint8_t speedByte=DCC::getThrottleSpeedByte(trackDCAddr[t]);
track[t]->setDCSignal(speedByte);
track[t]->setDCSignal(DCC::getThrottleSpeedByte(trackDCAddr[t]),
DCC::getThrottleFrequency(trackDCAddr[t]));
}
bool TrackManager::parseEqualSign(Print *stream, int16_t params, int16_t p[])
@ -560,14 +561,17 @@ bool TrackManager::getPower(byte t, char s[]) {
return false;
}
void TrackManager::reportObsoleteCurrent(Print* stream) {
// This function is for backward JMRI compatibility only
// It reports the first track only, as main, regardless of track settings.
// <c MeterName value C/V unit min max res warn>
#ifdef HAS_ENOUGH_MEMORY
int maxCurrent=track[0]->raw2mA(track[0]->getRawCurrentTripValue());
StringFormatter::send(stream, F("<c CurrentMAIN %d C Milli 0 %d 1 %d>\n"),
track[0]->raw2mA(track[0]->getCurrentRaw(false)), maxCurrent, maxCurrent);
#else
(void)stream;
#endif
}
void TrackManager::reportCurrent(Print* stream) {

View File

@ -621,7 +621,7 @@ void WiThrottle::sendFunctions(Print* stream, byte loco) {
#endif
for(int fKey=0; fKey<fkeys; fKey++) {
int fstate=DCC::getFn(locoid,fKey);
int8_t fstate=DCC::getFn(locoid,fKey);
if (fstate>=0) StringFormatter::send(stream,F("M%cA%c%d<;>F%d%d\n"),myLocos[loco].throttle,LorS(locoid),locoid,fstate,fKey);
}
}

View File

@ -71,8 +71,9 @@ Stream * WifiInterface::wifiStream;
#elif defined(ARDUINO_NUCLEO_F413ZH) || defined(ARDUINO_NUCLEO_F429ZI) \
|| defined(ARDUINO_NUCLEO_F446ZE) || defined(ARDUINO_NUCLEO_F412ZG) \
|| defined(ARDUINO_NUCLEO_F439ZI)
#define NUM_SERIAL 2
#define NUM_SERIAL 3
#define SERIAL1 Serial6
#define SERIAL3 Serial2
#else
#warning This variant of Nucleo not yet explicitly supported
#endif

View File

@ -26,6 +26,16 @@
//#include "IO_EXFastClock.h" // FastClock driver
//#include "IO_PCA9555.h" // 16-bit I/O expander (NXP & Texas Instruments).
//#include "IO_I2CDFPlayer.h" // DFPlayer over I2C
//#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.
@ -35,6 +45,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

128
mySetup_h_cmri.txt Normal file
View File

@ -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");

View File

@ -29,7 +29,6 @@ include_dir = .
[env]
build_flags = -Wall -Wextra
; monitor_filters = time
[env:samd21-dev-usb]
platform = atmelsam
@ -103,6 +102,7 @@ framework = arduino
lib_deps =
${env.lib_deps}
arduino-libraries/Ethernet
MDNS_Generic
SPI
monitor_speed = 115200
monitor_echo = yes
@ -244,18 +244,34 @@ monitor_echo = yes
; Experimental - Ethernet work still in progress
;
; [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
[env:Nucleo-F429ZI]
platform = ststm32
board = nucleo_f429zi
framework = arduino
lib_deps = ${env.lib_deps}
stm32duino/STM32Ethernet @ ^1.3.0
stm32duino/STM32duino LwIP @ ^2.1.2
MDNS_Generic
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_f439zi
framework = arduino
lib_deps = ${env.lib_deps}
stm32duino/STM32Ethernet @ ^1.3.0
stm32duino/STM32duino LwIP @ ^2.1.2
MDNS_Generic
build_flags = -std=c++17 -Os -g2 -Wunused-variable
monitor_speed = 115200
monitor_echo = yes
upload_protocol = stlink
[env:Teensy3_2]
platform = teensy

View File

@ -3,7 +3,11 @@
#include "StringFormatter.h"
#define VERSION "5.2.30"
#define VERSION "5.3.4"
// 5.3.4 - Bugfix: WiThrottle sendIntro after initial N message as well
// 5.3.3 - Fix Ethernet cable disconnected message, wait for DHCP
// 5.3.2 - MDNS Generic library integration for Ethernet
// 5.3.1 - Variable frequency for DC mode
// 5.2.40 - Bugfix: WiThrottle sendIntro after initial N message as well
// 5.2.29 - Added IO_I2CDFPlayer.h to support DFPLayer over I2C connected to NXP SC16IS750/SC16IS752 (currently only single UART for SC16IS752)
// - Added enhanced IO_I2CDFPLayer enum commands to EXRAIL2.h
@ -27,8 +31,11 @@
// 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
// df - I2C DFPlayper capability in stm32 branch
// 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
// C - CMRI RS485 connection
// 5.2.14 - Reminder window DCC packet optimization
// - Optional #define DISABLE_FUNCTION_REMINDERS
// 5.2.13 - EXRAIL STEALTH