chiark / gitweb /
Send HELLO as a result of all slaves being online. Do not crash if slave is slow...
[trains.git] / detpic / program.asm
1 ;======================================================================
2 ; MAIN PROGRAM AND GLUE
3
4   include common.inc
5   code
6
7 ;======================================================================
8 ; COMMON TO MASTER AND SLAVE
9
10 ;----------------------------------------
11 vector_reset @
12         clr_f   INTCON
13         bs_f    RCON, IPEN      ; interrupt priorities
14
15         call    idlocs_init
16
17         bt_f_if0 idloc1,idloc1_master
18         call    slave_init
19
20         bt_f_if1 idloc1,idloc1_master
21         call    master_init
22
23         bs_f    INTCON, GIEH
24         bs_f    INTCON, GIEL
25
26         bt_f_if1 idloc1,idloc1_master
27         goto    backgroundloop_master
28         bt_f_if1 idloc1,idloc1_boarddet
29         goto    backgroundloop_detectors
30         goto    backgroundloop_reversers
31
32 ;----------
33 common_local_init
34 ; called by {master,slave}_init immediately after they've init'd i2c.
35         clr_f   PCLATU
36         call    bitnum2bit_init
37
38         call    points_local_init
39         call    polarity_local_init
40         call    detect_local_init
41         return
42
43 ;----------------------------------------
44 panic_kill_hook @
45         call    power_panichook
46         call    cdu_panichook
47         return
48
49 ;----------
50 intrl_handled_routine @
51         mov_ff  isr_low_save_stkptr, STKPTR
52         intrl_handled_core
53
54 ;======================================================================
55 ; MASTER
56
57 ;----------------------------------------
58 master_init
59 ; Master-specific initialisation.
60         call    memory_erase
61         clr_f   flags
62         call    serial_init
63         call    i2cm_init
64         call    serialtxbuf_init
65         call    polarity_master_init
66         call    points_master_init
67         call    common_local_init
68         call    nmra_init
69         call    serialtxfc_init
70         call    mascan_init
71         call    tick_init
72         call    cdu_init
73         call    power_polarising_init
74         call    power_fault_init
75         call    watchdog_init
76         call    i2c_consider_restartread ; sends hello when all slaves ack'd
77         return
78
79 ;----------------------------------------
80 master_interrupt_low @
81         enter_interrupt_low
82         Dl 0x80
83         Df      INTCON
84         Df      INTCON2
85         Df      INTCON3
86         Df      PIR1
87         Df      PIR2
88         Df      PIR3
89         Dl 0x8d
90         call    tick_intrl
91         call    nmra_serialrx_intrl
92         call    power_fault_intrl
93         call    serialtxfc_intrl
94         call    serialtx_intrl
95         call    points_local_intrl
96         call    i2cm_intrl
97         Dl 0x8e
98         Df      INTCON
99         Df      INTCON2
100         Df      INTCON3
101         Df      PIR1
102         Df      PIR2
103         Df      PIR3
104         Dl 0x8f
105         panic   morse_IL
106
107 ;----------------------------------------------------------------------
108 serialrx_table_section  code    0x2000
109
110 ;--------------------
111 command_tellmode
112         mov_lw  0x09
113         goto    serial_addbyte
114
115 ;--------------------
116 serialrx_generalmsg @
117 ;command_<something>  has same calling convention:
118 ;
119 ;  FSR0 ->      start of message                undefined
120 ;  *FSR0        message                         undefined
121 ;
122         mov_fw  INDF0
123         bra_n   serialrx_if_multibyte
124         bra_z   command_crashed
125
126         xor_lw  0x11
127         bra_z   command_power_on
128         xor_lw  0x10 ^ 0x11
129         bra_z   command_power_off
130         xor_lw  0x0a ^ 0x10
131         bra_z   command_tellmode
132         bra     serialrx_bad
133
134 ;-----
135 serialrx_if_multibyte
136         rr_fw   INDF0           ; W =   ii MM ww ww ww ww ii ii
137         and_lw  0x3c            ; W =   zz zz ww ww ww ww zz zz
138         bt_f_if0 PCL, 0 ; always true, but reads PCL
139         add_wff PCL
140         ; <--- here is zero
141
142         goto    serialrx_bad            ; 1 0000 xxx
143         goto    command_ping            ; 1 0001 xxx
144         goto    command_polarity        ; 1 0010 xxx
145         goto    command_watchdog        ; 1 0011 xxx
146         goto    command_point           ; 1 0100 xxx
147         goto    serialrx_bad            ; 1 0101 xxx
148         goto    serialrx_bad            ; 1 0110 xxx
149         goto    serialrx_bad            ; 1 0111 xxx
150         goto    serialrx_bad            ; 1 1000 xxx
151         goto    serialrx_bad            ; 1 1001 xxx
152         goto    serialrx_bad            ; 1 1010 xxx
153         goto    serialrx_bad            ; 1 1011 xxx
154         goto    serialrx_bad            ; 1 1100 xxx
155         goto    serialrx_bad            ; 1 1101 xxx
156         goto    serialrx_bad            ; 1 1110 xxx
157         goto    serialrx_bad            ; 1 1111 xxx
158
159 ;-----
160 serialrx_bad
161         mov_ff  INDF0, t
162         panic   morse_HX
163
164 code2 code
165 ;--------------------
166 command_ping
167         mov_fw  POSTINC0
168         mov_ff  INDF0, t
169         call    serial_addbyte
170         mov_fw  t
171         bra_n   command_ping_bad
172         goto    serial_addbyte_another
173
174 command_ping_bad panic morse_HP
175
176 ;======================================================================
177 ; SLAVE
178
179 ;----------------------------------------
180 slave_init
181 ; Slave-specific initialisation.
182         clr_f   flags
183
184         mov_fw  picno
185         call    i2cs_init
186
187         bc_f    T0CON, TMR0ON
188
189         call    common_local_init
190         call    detect_slave_init
191         return
192
193 ;----------------------------------------
194 slave_interrupt_low @
195         enter_interrupt_low
196         call    points_local_intrl
197         panic   morse_IL
198
199 ;----------------------------------------
200 i2csu_write_begin
201         return
202
203 near_local_do code
204 ;----------------------------------------
205 i2csu_write_data
206         call    led_green
207
208         bt_w_if0 7
209         bra     i2csu_write_if_special
210         bt_w_if1 6
211         bra     polarity_local_do
212         bt_w_if0 5
213         bra     point_local_do
214 i2csu_write_if_bad
215         mov_wf  t
216         panic   morse_DX
217
218 i2csu_write_if_special
219         tst_w_ifnz
220         bra     i2csu_write_if_bad
221         goto    panic_crashread_commanded
222
223 code3 code
224 ;======================================================================
225 ; MASTER/SLAVE deviations
226
227 ;----------
228 message_for_master @
229 ; Either transmits the message to the master, or if we are the master,
230 ; handles it as an incoming message from the notional `slave 0'.
231 ;  W            message         unchanged
232 ;  GIEH         set             set
233         bt_f_if0 idloc1,idloc1_master
234         goto    slave_add_short_message
235         goto    loopback_read_byte
236
237 ;======================================================================
238   include final.inc