forked from MacGyver2/SX30
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSX30.cpp
More file actions
462 lines (406 loc) · 14.5 KB
/
SX30.cpp
File metadata and controls
462 lines (406 loc) · 14.5 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
* SX30.cpp
*
* Created on: 02.01.2012
* Changed on: 10.11.2013
* Version: 2.1
* Copyright: Michael Blank
*
* Changed on: 11.01.2015 by Reinhard Thamm, referring to comments
* Version: 2.2
*
* This faster version uses more direct bitwise port and pin operations
* and needs to be used if an original TRIX ZENTRALEINHEIT 1 is in use.
* Only important for SX-Writing issues.
*
* Changed on: 09.05.2015 by Reinhard Thamm
* Version: 2.2b
*
* Due to a timing problem when writing the very first Bit0 of a
* SX-address on the SX-bus, the handling of the SCOPE-trigger-
* signal has been postponed. The SCOPE-trigger is now activated
* after writing Bit0.
*
* Changed on: 24.11.2015, 16.12.201 by Reinhard Thamm
* Version: 3.0
*
* Hardware: this new version now supports a dual transistor output stage
* for SX_OUT, which is now driven by TWO processor outputs instead of
* a single R100 resistor connected to processor pin 6 (SX_OUT).
* Software: writing data on the SX bus is now initiated BEFORE the
* T0 (clock) signal rises, while reading data from the
* SX bus remains unchanged in the trailing part of the ISR.
* To accomplish both operations, the ISR has to be attached via CHANGE
* condition in the Setup of the calling module.
*
* interface hardware needed ! see www.opensx.net
Read SX Signal - SX_CLK (T0) must be connected to Pin2=INT0.
SX_DATA (T1) should be connected according to SX30.h.
SX_OUT (D) needs two lines to drive the output stage: a high-side signal
and a low-side signal according to SX30.h.
For triggering a scope, a signal can be generated at Pin SCOPE (see SX30.h)
at a defined base address.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <Arduino.h>
#include "SX30.h"
SX30::SX30() {
_scopeFlag = 0;
}
void SX30::init() {
// initialize function
// initialize pins and variables
// and start looking for a SYNC signal
pinMode(SX_CLK, INPUT); // SX-clock is an input
pinMode(SX_DATA,INPUT); // SX-data is also an input
//digitalWrite(SX_CLK, HIGH); // = enable pullup resistor
//digitalWrite(SX_DATA, HIGH); // = enable pullup resistor
#ifndef _SXSINGLEOUTPUTLINE_ //Tha: new:
pinMode(SX_OUTH, OUTPUT);
digitalWrite(SX_OUTH, HIGH);
pinMode(SX_OUTL, OUTPUT);
digitalWrite(SX_OUTL, HIGH);
#endif
#ifdef _SXISRTIMECHK_ //Tha: new, for purposes only
pinMode(TIMECHK, OUTPUT);
digitalWrite(TIMECHK, LOW);
#endif
for (int i=0; i<112;i++) {
// reset sx variable to zero
_sx[i]=0;
}
_toggle=0;
_adrCount =0;
// start always with search for SYNC
_state = SYNC;
_zeroCount = 0;
_sx_write_busy = 0;
_sx_writing = 0;
_newTrackBit = 2;
}
void SX30::init(uint8_t tba) {
// special init which enable a scope trigger
// at a Selectrix address tba (must be 0..112)
// scope will go high at bit 12 of this address!!
// i.e. interesting data are shortly BEFORE
// this trigger
_scopeFlag=1;
pinMode(SCOPE,OUTPUT);
_triggerAdr = tba;
init();
}
void SX30::setSX_OUT(uint8_t val) { //Tha: new for output stage
#ifdef _SXSINGLEOUTPUTLINE_
switch(val) {
case 0: // writing a 0 to SX_OUT
bitWrite(SX_OUT_DDR, SX_OUT_PORTPIN, HIGH);
bitWrite(SX_OUT_PORT, SX_OUT_PORTPIN, LOW);
break;
case 1: // writing a 1 to SX_OUT
bitWrite(SX_OUT_DDR, SX_OUT_PORTPIN, HIGH);
bitWrite(SX_OUT_PORT, SX_OUT_PORTPIN, HIGH);
break;
case TRISTATE: // setting SX_OUT to high impedance
bitWrite(SX_OUT_DDR, SX_OUT_PORTPIN, LOW);
break;
}
#else
switch(val) {
case 0: // writing a 0 to SX_OUT (active LOW!)
bitWrite(SX_OUTH_PORT, SX_OUTH_PORTPIN, HIGH);
bitWrite(SX_OUTL_PORT, SX_OUTL_PORTPIN, LOW);
break;
case 1: // writing a 1 to SX_OUT
bitWrite(SX_OUTH_PORT, SX_OUTH_PORTPIN, LOW);
bitWrite(SX_OUTL_PORT, SX_OUTL_PORTPIN, HIGH);
break;
case TRISTATE: // setting SX_OUT to high impedance (both lines to 1)
bitWrite(SX_OUTH_PORT, SX_OUTH_PORTPIN, HIGH);
bitWrite(SX_OUTL_PORT, SX_OUTL_PORTPIN, HIGH);
break;
}
#endif
}
uint8_t SX30::get(uint8_t channel) {
// returns the value of a SX channel
if (channel < MAX_CHANNEL_NUMBER)
return _sx[channel];
else
return 0;
}
uint8_t SX30::set(uint8_t ch, uint8_t d) {
if (_sx_write_busy) return 1; // dont accept new data
_sx_write_channel = ch;
_sx_write_data = d;
_sx_write_busy=1;
return 0;
}
uint8_t SX30::writing() {
return _sx_write_busy;
}
uint8_t SX30::getTrackBit() { //Tha: getting the Track-Bit (track
return _trackBit; // voltage on/off), due to an
} // idea from Uli Beyenbach
void SX30::setTrackBit(uint8_t val) { //Tha: setting the Track-Bit (track
if (val==0 || val==1) { // voltage on/off), due to an
_newTrackBit = val; // idea from Uli Beyenbach
}
}
void SX30::checkAndWrite(void) { //Tha: NEW: for SX data writing
switch (_state) { // purposes @FALLING slope of ISR
case ADDR: // triggering
switch (_adrCount) {
case 0: // this is the GLEISSPANNUNG bit
if (_newTrackBit < 2)
setSX_OUT(_newTrackBit);
else
setSX_OUT(TRISTATE);
break;
case 1:
if (_newTrackBit != 2) {
_newTrackBit = 2;
setSX_OUT(TRISTATE);
}
break;
} // switch (adrCount)
break; // case ADDR
case DATA:
// prepare for data writing:
// calc sx channel from baseAdr and dataFrameCount
_channel = (15-_baseAdr) + ((6-_dataFrameCount)<<4);
if ((_sx_write_busy) && (_channel == _sx_write_channel)) {
switch (_dataBitCount) {
case 2: // Trennbits
case 5:
case 8:
setSX_OUT(TRISTATE);
break;
case 0: // D0
if (bitRead(_sx_write_data, 0) != bitRead(_sx[_channel], 0))
setSX_OUT(bitRead(_sx_write_data, 0));
else
setSX_OUT(TRISTATE);
_sx_writing = 1;
break;
case 1: // D1
if (bitRead(_sx_write_data, 1) != bitRead(_sx[_channel], 1))
setSX_OUT(bitRead(_sx_write_data, 1));
else
setSX_OUT(TRISTATE);
break;
case 3: // D2
if (bitRead(_sx_write_data, 2) != bitRead(_sx[_channel], 2))
setSX_OUT(bitRead(_sx_write_data, 2));
else
setSX_OUT(TRISTATE);
break;
case 4: // D3
if (bitRead(_sx_write_data, 3) != bitRead(_sx[_channel], 3))
setSX_OUT(bitRead(_sx_write_data, 3));
else
setSX_OUT(TRISTATE);
break;
case 6: // D4
if (bitRead(_sx_write_data, 4) != bitRead(_sx[_channel], 4))
setSX_OUT(bitRead(_sx_write_data, 4));
else
setSX_OUT(TRISTATE);
break;
case 7: // D5
if (bitRead(_sx_write_data, 5) != bitRead(_sx[_channel], 5))
setSX_OUT(bitRead(_sx_write_data, 5));
else
setSX_OUT(TRISTATE);
break;
case 9: // D6
if (bitRead(_sx_write_data, 6) != bitRead(_sx[_channel], 6))
setSX_OUT(bitRead(_sx_write_data, 6));
else
setSX_OUT(TRISTATE);
break;
case 10: // D7
if (bitRead(_sx_write_data, 7) != bitRead(_sx[_channel], 7))
setSX_OUT(bitRead(_sx_write_data, 7));
else
setSX_OUT(TRISTATE);
break;
case 11: // Trennbit
setSX_OUT(TRISTATE);
_sx_writing = 0; // was the last bit
_sx_write_busy = 0; // write has ended, can accept new data
break;
} // switch (_dataBitCount)
} // if _sx_write_busy && ...
if (_scopeFlag) {
if (_channel == _triggerAdr) {
bitWrite(SCOPE_PORT, SCOPE_PORTPIN, HIGH); // activate scope signal
}
}
break; // case DATA
} // switch (_state)
} // checkAndWrite()
void SX30::switchAdr() {
// a SYNC signal was received, now look for a valid
// base address
switch(_adrCount) {
case 0: // this is the GLEISSPANNUNG bit
_trackBit = _bit;
break;
case 1:
break;
case 4:
break; // ignore
case 2: // B3
bitWrite(_baseAdr,3,_bit);
break;
case 3: // B2
bitWrite(_baseAdr,2,_bit);
break;
case 5: // B1
bitWrite(_baseAdr,1,_bit);
break;
case 6: // B0
bitWrite(_baseAdr,0,_bit);
break;
case 7: // last "1"
// _baseAdr is complete !
// advance to next state - next we are looking
// for the 7 data bytes (i.e. 7 SX Channels)
_state = DATA;
_dataFrameCount = 0;
_dataBitCount = 0;
_data=0;
break;
}
}
void SX30::switchData() {
// continue reading _data
// a total of 7 DATA blocks will be received
// for a certain base-address
// calc sx channel from baseAdr and dataFrameCount
switch(_dataBitCount) {
case 2: // "Trenn_bits"
case 5:
case 8:
_dataBitCount++;
break; // ignore
case 0: // D0
_data=0;
bitWrite(_data,0,_bit);
_dataBitCount++;
if (_scopeFlag) { //Tha: also watching _scopeFlag..
if (_channel == _triggerAdr) { // (bugfix 30.01.2015)
bitWrite(SCOPE_PORT, SCOPE_PORTPIN, HIGH); //Tha: digitalWrite(SCOPE,HIGH);
}
else {
bitWrite(SCOPE_PORT, SCOPE_PORTPIN, LOW); //Tha: digitalWrite(SCOPE,LOW);
}
}
break;
case 1: // D1
bitWrite(_data,1,_bit);
_dataBitCount++;
break;
case 3: // D2
bitWrite(_data,2,_bit);
_dataBitCount++;
break;
case 4: // D3
bitWrite(_data,3,_bit);
_dataBitCount++;
break;
case 6: // D4
bitWrite(_data,4,_bit);
_dataBitCount++;
break;
case 7: // D5
bitWrite(_data,5,_bit);
_dataBitCount++;
break;
case 9: // D6
bitWrite(_data,6,_bit);
_dataBitCount++;
break;
case 10: // D7
bitWrite(_data,7,_bit);
_dataBitCount++;
break;
case 11: // == MAX_DATABITCOUNT
// _bit value should always equal HIGH, not tested here.
if (_scopeFlag) {
if (_channel == _triggerAdr) {
bitWrite(SCOPE_PORT, SCOPE_PORTPIN, LOW); // deactivate scope signal
}
}
// copy _data byte to SX _channel
_sx[_channel] = _data;
// increment dataFrameCount to move on the next DATA byte
// check, if we already reached the last DATA block - in this
// case move on to the next SX-Datenpaket, i.e. look for SYNC
_dataFrameCount ++;
if (_dataFrameCount == MAX_DATACOUNT) {
// and move on to SYNC _state
_dataFrameCount=0;
_state =SYNC;
_zeroCount = 0;
_dataBitCount=0;
}
else {
_dataBitCount = 0; // reset _bit counter
_data = 0;
// continue with reading next _data uint8_t
}
} //end switch/case _dataBitCount
}
void SX30::isr() {
// interrupt service routine (AVR INT0)
// driven by CHANGING clock signal T0 (SX-plug pin1)
// 3 different states are distinguished
// 1. SNYC = looking for a SYNC signal
// 2. ADDR = (after SYNC received) look for base address (0..15)
// 3. DATA = (after ADDR decoded) decode the 7 data-bytes
#ifdef _SXISRTIMECHK_
bitWrite(TIMECHK_PORT, TIMECHK_PORTPIN, HIGH); // for TEST purposes only
#endif
// check if the calling slope was the falling or the rising one:
if (!bitRead(SX_CLK_PINREG, SX_CLK_PORTPIN)) { // falling slope
checkAndWrite();
}
else { // rising slope
_bit = bitRead(SX_DATA_PINREG, SX_DATA_PORTPIN); // reading the T1 data pin
switch(_state) {
case SYNC:
if (_bit == LOW) {
_zeroCount++;
} else {
if (_zeroCount == 3) { // sync bits 0 0 0 1 found
_state = ADDR; // advance to next state
_baseAdr = 0; //init
_adrCount = 0; //init
} else { // no valid sync, try again ...
_zeroCount = 0; // reset _zeroCounter
} // endif _zeroCount
} // endif _bit==LOW
break;
case ADDR:
switchAdr();
_adrCount++;
break;
case DATA:
switchData();
}
}
#ifdef _SXISRTIMECHK_
bitWrite(TIMECHK_PORT, TIMECHK_PORTPIN, LOW); // for TEST purposes only
#endif
}