chiark / gitweb /
can reprogram santafe
[trains.git] / hostside / nmra-packets.h
1 /*
2  * strange macros - do not use directly, usually
3  */
4   
5 NMRA(speed28, Aint(addr,0) Aint(speed,1) Aint(reverse,2), {
6   /* 0<=speed<=28 or <=126; reverse: 0 forwards, non-0 backwards */
7   int adj;
8   Byte cmd;
9   ADDR;
10   nmra_errchk(cn, speed, speed>=0 && speed<=28);
11   cmd= 0x40 | (reverse ? 0 : 0x20);
12   if (speed) {
13     adj= speed + 3;
14     cmd |= adj & 1 ? 0x10 : 0;
15     cmd |= adj >> 1;
16   }
17   *c++= cmd;
18 })
19 NMRA(estop1, Aint(addr,0), {
20   /* Baseline Speed and direction Forwards E-Stop(I) S9.2 B table l.56 */
21   ADDR;
22   *c++= 0x71;
23 })
24 NMRA(speed126, Aint(addr,0) Aint(speed,1) Aint(reverse,2), {
25   /* Advanced Operations 128 Speed Step Control
26    * (actually speeds 0..126) RP9.2.1 C l.200- */
27   ADDR;
28   nmra_errchk(cn, speed, speed>=0 && speed<=126);
29   *c++= 0x3f;
30   *c++= (speed ? speed + 1 : 0) | (reverse ? 0 : 0x80);
31 })
32
33 NMRA(estop, Anone, {
34   /* Baseline Broadcast stop Forwards(I) Emergency S9.2 B l.98- */
35   CONST(0x00, 0x71);
36 })
37 NMRA(bstop, Anone, {
38   /* Baseline Broadcast stop Forwards(I) non-Emergency S9.2 B l.98- */
39   CONST(0x00, 0x70);
40 })
41 NMRA(reset, Anone, {
42   /* Baseline Decoder Reset S9.2 B l.77- */
43   CONST(0x00, 0x00);
44 })
45 NMRA(idle, Anone, {
46   /* Baseline Idle S9.2 B l.87- */
47   CONST(0xff, 0x00);
48 })
49
50 /* For functions:
51  * bit 0 is FL aka F0; bits 1-12 are F1-F12; do not call with bits
52  * outside 0-12 set; bits in 0-12 but not relevant for the relevant
53  * command are ignored. */
54 NMRA(funcs0to4, Aint(addr,0) Abitmap(bitmap,1), {
55   /* Function Group One RP9.2.1 C l.234- */
56   FUNCS(0x80 | ((bitmap >> 1) & 0x0f) | ((bitmap << 4 & 0x10)));
57 })
58 NMRA(funcs5to8, Aint(addr,0) Abitmap(bitmap,1), {
59   /* Function Group Two RP9.2.1 C l.246- */
60   FUNCS(0xb0 | ((bitmap >> 5) & 0x0f));
61 })
62 NMRA(funcs9to12, Aint(addr,0) Abitmap(bitmap,1), {
63   /* Function Group Two RP9.2.1 C l.246- */
64   FUNCS(0xa0 | ((bitmap >> 9) & 0x0f));
65 })
66 NMRA(cvwrite, Aint(addr,0) Aint(cv,1) Abyte(value,2), {
67   /* Configuration Variable Access Long Form RP9.2.1 C l.286- */
68   int adj;
69   ADDR;
70   nmra_errchk(cn, cv, cv>=1 && cv<=1024);
71   adj= cv - 1;
72   *c++= 0xec | (adj >> 8);
73   *c++= adj;
74   *c++= value;
75 })
76
77 /*
78  * Service mode.
79  */
80 NMRA(svc_cvwrite, Aint(cv,0) Abyte(value,1), {
81   /* Service Mode Instruction for Direct Mode, Write Byte
82    * RP 9.2.3 E l.107-
83    */
84   int adj;
85   nmra_errchk(cn, cv, cv>=1 && cv<=1024);
86   adj= cv - 1;
87   *c++= 0x7c | (adj >> 8);
88   *c++= adj;
89   *c++= value;
90 })
91 NMRA(svc_factoryreset, Anone, {
92   /* Service Mode Instruction for Decoder Factory Reset
93    * RP 9.2.3 E l.279-
94    * (See also Packet Sequence for Physical Register Addressing
95    * RP 9.2.3 E l.166-)
96    */
97   CONST(0x7f, 0x08);
98 })
99 NMRA(svc_pagepreset, Anone, {
100   /* Service Mode Page Preset Instruction
101    * RP 9.2.3 E l.90-91
102    * used in various packet sequences.
103    */
104   CONST(0x7d, 0x01);
105 })
106      
107 #undef Aint
108 #undef Abitmap
109 #undef Abyte
110 #undef Anone
111 #undef NMRA