-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocoNet.ino
216 lines (190 loc) · 5.99 KB
/
LocoNet.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//=== LocoNet for Nebenuhr ===
#include <LocoNet.h>
//=== declaration of var's =======================================
LocoNetFastClockClass FastClock;
static lnMsg *LnPacket;
static LnBuf LnTxBuffer;
unsigned long ul_LastFastClockTick = 0;
/*
* https://www.digitrax.com/static/apps/cms/media/documents/loconet/loconetpersonaledition.pdf
* https://wiki.rocrail.net/doku.php?id=loconet:ln-pe-de
* http://embeddedloconet.sourceforge.net/SV_Programming_Messages_v13_PE.pdf
* remark: this software didnot follow '2.2.6) Standard SV/EEPROM Locations'
*/
// default OPC's are defined in 'utility\ln_opc.h'
// A3 is already used and defined as OPC_LOCO_F912
// A8 is already used by FRED and defined as OPC_DEBUG
// A8 is already used by FREDI and defined as OPC_FRED_BUTTON
//#define OPC_FRED_BUTTON 0xA8
// AF is already used by FREDI and defined as OPC_SELFTEST resp. as OPC_FRED_ADC
// ED is already used and defined as OPC_IMM_PACKET
// EE is already used and defined as OPC_IMM_PACKET_2
#define SV2_Format_1 0x01
#define SV2_Format_2 0x02
#define PIN_TX 7
#define PIN_RX 8
uint8_t ui8_WaitForTelegram = 0;
unsigned long ul_WaitStartForTelegram = 0;
int16_t i16_FracMinStart(0);
//=== functions for receiving telegrams from SerialMonitor =======
#if defined TELEGRAM_FROM_SERIAL
static const int MAX_LEN_LNBUF = 64;
uint8_t ui8_PointToBuffer = 0;
uint8_t ui8a_receiveBuffer[MAX_LEN_LNBUF];
uint8_t ui8a_resultBuffer[MAX_LEN_LNBUF];
void ClearReceiveBuffer()
{
ui8_PointToBuffer = 0;
for(uint8_t i = 0; i < MAX_LEN_LNBUF; i++)
ui8a_receiveBuffer[i] = 0;
}
uint8_t Adjust(uint8_t ui8_In)
{
uint8_t i = ui8_In;
if((i >= 48) && (i <= 57))
i -= 48;
else
if((i >= 65) && (i <= 70))
i -= 55;
else
if((i >= 97) && (i <= 102))
i -= 87;
else
i = 0;
return i;
}
uint8_t AnalyzeBuffer()
{
uint8_t i = 0;
uint8_t ui8_resBufSize = 0;
while(uint8_t j = ui8a_receiveBuffer[i++])
{
if(j != ' ')
{
j = Adjust(j);
uint8_t k = ui8a_receiveBuffer[i++];
if(k != ' ')
ui8a_resultBuffer[ui8_resBufSize++] = j * 16 + Adjust(k);
else
ui8a_resultBuffer[ui8_resBufSize++] = j;
}
}
// add checksum:
uint8_t ui8_checkSum(0xFF);
for(i = 0; i < ui8_resBufSize; i++)
ui8_checkSum ^= ui8a_resultBuffer[i];
bitWrite(ui8_checkSum, 7, 0); // set MSB zero
ui8a_resultBuffer[ui8_resBufSize++] = ui8_checkSum;
return ui8_resBufSize;
}
#endif
uint8_t GetWaitForTelegram() { return ui8_WaitForTelegram; }
void SetWaitForTelegram(uint8_t iType)
{
ui8_WaitForTelegram = iType;
ul_WaitStartForTelegram = millis();
}
//=== functions ==================================================
void InitLocoNet()
{
// First initialize the LocoNet interface, specifying the TX Pin
LocoNet.init(PIN_TX);
if(ENABLE_LN_FC_MODUL)
{
// Initialize the Fast Clock
FastClock.init(0, 0, 1); // DCS100CompatibleSpeed - CorrectDCS100Clock - NotifyFracMin
// Poll the Current Time from the Command Station
PollFastClock();
}
#if defined TELEGRAM_FROM_SERIAL
ClearReceiveBuffer();
#endif
}
void HandleLocoNetMessages()
{
#if defined TELEGRAM_FROM_SERIAL
LnPacket = NULL;
if (Serial.available())
{
byte byte_Read = Serial.read();
if(byte_Read == '\n')
{
if(AnalyzeBuffer() > 0)
LnPacket = (lnMsg*) (&ui8a_resultBuffer);
ClearReceiveBuffer();
}
else
{
if(ui8_PointToBuffer < MAX_LEN_LNBUF)
ui8a_receiveBuffer[ui8_PointToBuffer++] = byte_Read;
}
}
#else
// Check for any received LocoNet packets
LnPacket = LocoNet.receive();
#endif
if(LnPacket)
{
if (ENABLE_LN_FC_MODUL)
{
// JMRI sends only EF 0E 7B ... (OPC_WR_SL_DATA) with clk_cntrl == 0 -> so we correct it:
if (ENABLE_LN_FC_JMRI && (LnPacket->fc.slot == FC_SLOT) && (LnPacket->fc.command == OPC_WR_SL_DATA) && !(LnPacket->fc.clk_cntrl & 0x40))
LnPacket->fc.clk_cntrl |= 0x40;
if ((LnPacket->fc.slot == FC_SLOT) && ((LnPacket->fc.command == OPC_WR_SL_DATA) || (LnPacket->fc.command == OPC_SL_RD_DATA)))
i16_FracMinStart = 0x3FFF/*FC_FRAC_MIN_BASE*/ - ((LnPacket->fc.frac_minsh << 7) + LnPacket->fc.frac_minsl);
FastClock.processMessage(LnPacket); // will call 'notifyFastClock' with sync=1 if neccessary
}
uint8_t ui8_msgLen = getLnMsgSize(LnPacket);
uint8_t ui8_msgOPCODE = LnPacket->data[0];
//====================================================
if (ui8_msgLen == 16)
{
if (ui8_msgOPCODE == OPC_PEER_XFER) // E5
HandleE5MessageFormat2();
} // if(ui8_msgLen == 16)
//====================================================
} // if(LnPacket)
if (ENABLE_LN_FC_MODUL/* we are also Slave */ && isTimeForProcessActions(&ul_LastFastClockTick, 67))
FastClock.process66msActions(); // will call 'notifyFastClock' with sync=0 if neccessary
}
void HandleFracMins(uint16_t FracMins)
{
if (!ENABLE_LN_FC_MODUL || !GetClockRate())
return;
if ((i16_FracMinStart - 910) > FracMins) // 910 = (approx.) 60000ms / 66ms
{
i16_FracMinStart = FracMins;
if(ENABLE_LN_FC_INTERN)
IncFastClock(1);
}
}
void PollFastClock()
{
// Poll the Current Time from the Command Station
FastClock.poll();
#if defined DEBUG
Serial.println("...poll...");
#endif
}
#if defined DEBUG || defined TELEGRAM_FROM_SERIAL
void Printout(char ch)
{
if(LnPacket)
{
// print out the packet in HEX
Serial.print(ch);
Serial.print(F("X: "));
uint8_t ui8_msgLen = getLnMsgSize(LnPacket);
for (uint8_t i = 0; i < ui8_msgLen; i++)
{
uint8_t ui8_val = LnPacket->data[i];
// Print a leading 0 if less than 16 to make 2 HEX digits
if(ui8_val < 16)
Serial.print('0');
Serial.print(ui8_val, HEX);
Serial.print(' ');
}
Serial.println();
} // if(LnPacket)
}
#endif