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