001package jmri.jmrix.can.cbus;
002
003/**
004 * CbusConstants.java
005 *
006 * Constants to represent CBUS protocol
007 *
008 * @author Andrew Crosland Copyright (C) 2008, 2021
009 */
010public final class CbusConstants {
011
012    /**
013     * Handle used by service mode programmer
014     */
015    public static final int SERVICE_HANDLE = 255;
016
017    public static final int DEFAULT_STANDARD_ID = 0x7a;
018    public static final int DEFAULT_EXTENDED_ID = 0x7a;
019
020    /**
021     * CBUS Manufacturer definitions
022     * Where the manufacturer already has an NMRA code, this is used
023     */
024    public static final int SPROG_DCC = 44;         // http://www.merg.co.uk
025    public static final int MANU_MERG = 165;        // http://www.merg.co.uk
026    public static final int MANU_ROCRAIL = 70;      // http://www.rocrail.net
027    public static final int MANU_SPECTRUM = 80;     // http://animatedmodeler.com  (Spectrum Engineering)
028
029    /**
030     * SPROG DCC module types
031     * 
032     * Must agree with firmware cbusdefs.h include file from MERGDEV repo
033     */
034    public static final int MTYP_CANPiSPRG3 = 1;    // Pi-SPROG 3 (not v2) firmware on Pi-SPROG One hardware
035    public static final int MTYP_CANSPROG3P = 2;    // Sprog 3 Plus, Pi-SPROG 3v2 and Pi-SPROG 3 Plus common firmware
036    public static final int MTYP_CANSPROG = 3;
037    public static final int MTYP_SBOOST = 4;
038    //public static final int Unsupported = 5;
039    public static final int MTYP_CANSOLNOID = 8;   // Servo I/O module similar to MERG CANSOLIIO
040    public static final int MTYP_CANSERVOIO = 50;   // Servo I/O module similar to MERG CANMIO-SVO
041    public static final int MTYP_CANISB = 100;
042    public static final int MTYP_CANSOLIO = 101;    // Solenoid I/O module with inputs
043
044    /**
045     * CBUS Opcodes
046     */
047    // Opcodes with no data
048    public static final int CBUS_ACK = 0x00;
049    public static final int CBUS_NAK = 0x01;
050    public static final int CBUS_HLT = 0x02;
051    public static final int CBUS_BON = 0x03;
052    public static final int CBUS_TOF = 0x04;
053    public static final int CBUS_TON = 0x05;
054    public static final int CBUS_ESTOP = 0x06;
055    public static final int CBUS_ARST = 0x07;
056    public static final int CBUS_RTOF = 0x08;
057    public static final int CBUS_RTON = 0x09;
058    public static final int CBUS_RESTP = 0x0A;
059
060    public static final int CBUS_RSTAT = 0x0C;
061    public static final int CBUS_QNN = 0x0D;
062
063    public static final int CBUS_RQNP = 0x10;
064    public static final int CBUS_RQMN = 0x11;
065
066    // Opcodes with 1 data
067    public static final int CBUS_KLOC = 0x21;
068    public static final int CBUS_QLOC = 0x22;
069    public static final int CBUS_DKEEP = 0x23;
070
071    public static final int CBUS_DBG1 = 0x30;
072
073    public static final int CBUS_EXTC = 0x3F;
074
075    // Opcodes with 2 data
076    public static final int CBUS_RLOC = 0x40;
077    public static final int CBUS_QCON = 0x41;
078    public static final int CBUS_SNN = 0x42;
079    public static final int CBUS_ALOC = 0x43;
080    public static final int CBUS_STMOD = 0x44;
081    public static final int CBUS_PCON = 0x45;
082    public static final int CBUS_KCON = 0x46;
083    public static final int CBUS_DSPD = 0x47;
084    public static final int CBUS_DFLG = 0x48;
085    public static final int CBUS_DFNON = 0x49;
086    public static final int CBUS_DFNOF = 0x4A;
087    public static final int CBUS_SSTAT = 0x4C;
088    public static final int CBUS_NNRSM = 0x4F;
089    public static final int CBUS_RQNN = 0x50;
090    public static final int CBUS_NNREL = 0x51;
091    public static final int CBUS_NNACK = 0x52;
092    public static final int CBUS_NNLRN = 0x53;
093    public static final int CBUS_NNULN = 0x54;
094    public static final int CBUS_NNCLR = 0x55;
095    public static final int CBUS_NNEVN = 0x56;
096    public static final int CBUS_NERD = 0x57;
097    public static final int CBUS_RQEVN = 0x58;
098    public static final int CBUS_WRACK = 0x59;
099    public static final int CBUS_RQDAT = 0x5A;
100    public static final int CBUS_RQDDS = 0x5B;
101    public static final int CBUS_BOOTM = 0x5C;
102    public static final int CBUS_ENUM = 0x5D;
103    public static final int CBUS_NNRST = 0x5E;
104    public static final int CBUS_EXTC1 = 0x5F;
105
106    // Opcodes with 3 data
107    public static final int CBUS_DFUN = 0x60;
108    public static final int CBUS_GLOC = 0x61;
109    public static final int CBUS_ERR = 0x63;
110
111    public static final int CBUS_CMDERR = 0x6F;
112
113    public static final int CBUS_EVNLF = 0x70;
114    public static final int CBUS_NVRD = 0x71;
115    public static final int CBUS_NENRD = 0x72;
116    public static final int CBUS_RQNPN = 0x73;
117    public static final int CBUS_NUMEV = 0x74;
118    public static final int CBUS_CANID = 0x75;
119
120    public static final int CBUS_EXTC2 = 0x7F;
121
122    // Opcodes with 4 data
123    public static final int CBUS_RDCC3 = 0x80;
124    public static final int CBUS_WCVO = 0x82;
125    public static final int CBUS_WCVB = 0x83;
126    public static final int CBUS_QCVS = 0x84;
127    public static final int CBUS_PCVS = 0x85;
128
129    public static final int CBUS_ACON = 0x90;
130    public static final int CBUS_ACOF = 0x91;
131    public static final int CBUS_AREQ = 0x92;
132    public static final int CBUS_ARON = 0x93;
133    public static final int CBUS_AROF = 0x94;
134    public static final int CBUS_EVULN = 0x95;
135    public static final int CBUS_NVSET = 0x96;
136    public static final int CBUS_NVANS = 0x97;
137    public static final int CBUS_ASON = 0x98;
138    public static final int CBUS_ASOF = 0x99;
139    public static final int CBUS_ASRQ = 0x9A;
140    public static final int CBUS_PARAN = 0x9B;
141    public static final int CBUS_REVAL = 0x9C;
142    public static final int CBUS_ARSON = 0x9D;
143    public static final int CBUS_ARSOF = 0x9E;
144    public static final int CBUS_EXTC3 = 0x9F;
145
146    // OPcodes with 5 data
147    public static final int CBUS_RDCC4 = 0xA0;
148    public static final int CBUS_WCVS = 0xA2;
149    public static final int CBUS_VCVS = 0xA4;
150
151    public static final int CBUS_ACON1 = 0xB0;
152    public static final int CBUS_ACOF1 = 0xB1;
153    public static final int CBUS_REQEV = 0xB2;
154    public static final int CBUS_ARON1 = 0xB3;
155    public static final int CBUS_AROF1 = 0xB4;
156    public static final int CBUS_NEVAL = 0xB5;
157    public static final int CBUS_PNN = 0xB6;
158    public static final int CBUS_ASON1 = 0xB8;
159    public static final int CBUS_ASOF1 = 0xB9;
160    public static final int CBUS_ARSON1 = 0xBD;
161    public static final int CBUS_ARSOF1 = 0xBE;
162    public static final int CBUS_EXTC4 = 0xBF;
163
164    // Opcodes with 6 data
165    public static final int CBUS_RDCC5 = 0xC0;
166    public static final int CBUS_WCVOA = 0xC1;
167    public static final int CBUS_CABDAT = 0xC2;
168    public static final int CBUS_FCLK = 0xCF;
169
170    public static final int CBUS_ACON2 = 0xD0;
171    public static final int CBUS_ACOF2 = 0xD1;
172    public static final int CBUS_EVLRN = 0xD2;
173    public static final int CBUS_EVANS = 0xD3;
174    public static final int CBUS_ARON2 = 0xD4;
175    public static final int CBUS_AROF2 = 0xD5;
176
177    public static final int CBUS_ASON2 = 0xD8;
178    public static final int CBUS_ASOF2 = 0xD9;
179
180    public static final int CBUS_ARSON2 = 0xDD;
181    public static final int CBUS_ARSOF2 = 0xDE;
182    public static final int CBUS_EXTC5 = 0xDF;
183
184    // Opcodes with 7 data
185    public static final int CBUS_RDCC6 = 0xE0;
186    public static final int CBUS_PLOC = 0xE1;
187    public static final int CBUS_NAME = 0xE2;
188    public static final int CBUS_STAT = 0xE3;
189
190    public static final int CBUS_PARAMS = 0xEF;
191
192    public static final int CBUS_ACON3 = 0xF0;
193    public static final int CBUS_ACOF3 = 0xF1;
194    public static final int CBUS_ENRSP = 0xF2;
195    public static final int CBUS_ARON3 = 0xF3;
196    public static final int CBUS_AROF3 = 0xF4;
197    public static final int CBUS_EVLRNI = 0xF5;
198    public static final int CBUS_ACDAT = 0xF6;
199    public static final int CBUS_ARDAT = 0xF7;
200    public static final int CBUS_ASON3 = 0xF8;
201    public static final int CBUS_ASOF3 = 0xF9;
202    public static final int CBUS_DDES = 0xFA;
203    public static final int CBUS_DDRS = 0xFB;
204    public static final int CBUS_DDWS = 0xFC;
205
206    public static final int CBUS_ARSON3 = 0xFD;
207    public static final int CBUS_ARSOF3 = 0xFE;
208    public static final int CBUS_EXTC6 = 0xFF;
209
210    /**
211     * Extended opcodes, currently only used by the bootloader
212     * <p>
213     * These are all responses from the bootloader
214     */
215    public static final int CBUS_EXT_BOOT_ERROR = 0x00;
216    public static final int CBUS_EXT_BOOT_OK = 0x01;
217    public static final int CBUS_EXT_BOOTC = 0x02;
218    public static final int CBUS_EXT_BOOT_OUT_OF_RANGE = 0x03;
219    public static final int CBUS_EXT_DEVID = 0x05;
220    public static final int CBUS_EXT_BOOTID = 0x06;
221
222    /**
223     * Bootloader commands
224     *
225     * These are used in the data payload of bootloader control frames.
226     */
227    public static final int CBUS_BOOT_NOP = 0x00;
228    public static final int CBUS_BOOT_RESET = 0x01;
229    public static final int CBUS_BOOT_INIT = 0x02;
230    public static final int CBUS_BOOT_CHECK = 0x03;
231    public static final int CBUS_BOOT_TEST = 0x04;
232    public static final int CBUS_BOOT_DEVID = 0x05;
233    public static final int CBUS_BOOT_BOOTID = 0x06;
234    public static final int CBUS_BOOT_ENABLES = 0x07;
235
236    /**
237     * Programming modes
238     */
239    public static final int CBUS_PROG_DIRECT_BYTE = 0;
240    public static final int CBUS_PROG_DIRECT_BIT = 1;
241    public static final int CBUS_PROG_PAGED = 2;
242    public static final int CBUS_PROG_REGISTER = 3;
243    public static final int CBUS_PROG_ADDRESS = 4;
244    public static final int CBUS_OPS_BYTE = 5;
245
246    /**
247     * Error codes returned by CBUS_ERR
248     */
249    public static final int ERR_LOCO_STACK_FULL = 1;
250    public static final int ERR_LOCO_ADDRESS_TAKEN = 2;
251    public static final int ERR_SESSION_NOT_PRESENT = 3;
252    public static final int ERR_CONSIST_EMPTY = 4;
253    public static final int ERR_LOCO_NOT_FOUND = 5;
254    public static final int ERR_CAN_BUS_ERROR = 6;
255    public static final int ERR_INVALID_REQUEST = 7;
256    public static final int ERR_SESSION_CANCELLED = 8;
257
258    /**
259     * Status codes for OPC_SSTAT
260     */
261    public static final int SSTAT_NO_ACK = 1;
262    public static final int SSTAT_OVLD = 2;
263    public static final int SSTAT_WR_ACK = 3;
264    public static final int SSTAT_BUSY = 4;
265    public static final int SSTAT_CV_ERROR = 5;
266
267    /**
268     * Event types
269     */
270    public static final int EVENT_ON = 0;
271    public static final int EVENT_OFF = 1;
272    public static final int EVENT_EITHER = 2;
273    public static final int EVENT_NEITHER = 3;
274
275    /**
276     * Event directions
277     */
278    public static final int EVENT_DIR_UNSET = 0;
279    public static final int EVENT_DIR_IN = 1;
280    public static final int EVENT_DIR_OUT = 2;
281    public static final int EVENT_DIR_EITHER = 3;
282
283    /**
284     * CBUS Priorities
285     */
286    public static final int DEFAULT_DYNAMIC_PRIORITY = 2;
287    public static final int DEFAULT_MINOR_PRIORITY = 3;
288
289    /**
290     * Function bits for group1
291     */
292    public static final int CBUS_F0 = 16;
293    public static final int CBUS_F1 = 1;
294    public static final int CBUS_F2 = 2;
295    public static final int CBUS_F3 = 4;
296    public static final int CBUS_F4 = 8;
297
298    /**
299     * Function bits for group2
300     */
301    public static final int CBUS_F5 = 1;
302    public static final int CBUS_F6 = 2;
303    public static final int CBUS_F7 = 4;
304    public static final int CBUS_F8 = 8;
305
306    /**
307     * Function bits for group3
308     */
309    public static final int CBUS_F9 = 1;
310    public static final int CBUS_F10 = 2;
311    public static final int CBUS_F11 = 4;
312    public static final int CBUS_F12 = 8;
313
314    /**
315     * Function bits for group4
316     */
317    public static final int CBUS_F13 = 1;
318    public static final int CBUS_F14 = 2;
319    public static final int CBUS_F15 = 4;
320    public static final int CBUS_F16 = 8;
321    public static final int CBUS_F17 = 0x10;
322    public static final int CBUS_F18 = 0x20;
323    public static final int CBUS_F19 = 0x40;
324    public static final int CBUS_F20 = 0x80;
325
326    /**
327     * Function bits for group5
328     */
329    public static final int CBUS_F21 = 1;
330    public static final int CBUS_F22 = 2;
331    public static final int CBUS_F23 = 4;
332    public static final int CBUS_F24 = 8;
333    public static final int CBUS_F25 = 0x10;
334    public static final int CBUS_F26 = 0x20;
335    public static final int CBUS_F27 = 0x40;
336    public static final int CBUS_F28 = 0x80;
337
338    /**
339     * Function bits for group6
340     */
341    public static final int CBUS_F29 = 1;
342    public static final int CBUS_F30 = 2;
343    public static final int CBUS_F31 = 4;
344    public static final int CBUS_F32 = 8;
345    public static final int CBUS_F33 = 16;
346    public static final int CBUS_F34 = 32;
347    public static final int CBUS_F35 = 64;
348    public static final int CBUS_F36 = 128;
349
350    static final int[] CBUS_FUNCTION_BITS = new int[]{ CBUS_F0,
351        CBUS_F1, CBUS_F2, CBUS_F3, CBUS_F4, CBUS_F5, CBUS_F6, CBUS_F7,
352        CBUS_F8, CBUS_F9, CBUS_F10, CBUS_F11, CBUS_F12, CBUS_F13, CBUS_F14,
353        CBUS_F15, CBUS_F16, CBUS_F17, CBUS_F18, CBUS_F19, CBUS_F20, CBUS_F21,
354        CBUS_F22, CBUS_F23, CBUS_F24, CBUS_F25, CBUS_F26, CBUS_F27, CBUS_F28,
355        CBUS_F29, CBUS_F30, CBUS_F31, CBUS_F32, CBUS_F33, CBUS_F34, CBUS_F35, CBUS_F36 };
356
357    /**
358     * Throttle modes
359     */
360    public static final int CBUS_SS_128 = 0;
361    public static final int CBUS_SS_14 = 1;
362    public static final int CBUS_SS_28_INTERLEAVE = 2;
363    public static final int CBUS_SS_28 = 3;
364
365    /**
366     * Number of function buttons on a throttle
367     */
368    public static int MAX_FUNCTIONS = 32; 
369    
370    /**
371     * Number of slots supported by the command station
372     */
373    public static final int CBUS_MAX_SLOTS = 32;
374}