chiark / gitweb /
Fix double-Fault bug. Change state machine comment to have new Off behaviour, but...
[trains.git] / detpic / energy.asm
1 ;======================================================================
2 ; TRACK POWER AND SHORT CIRCUIT
3
4   include common.inc
5
6 settling_timeout        equ      10000 ; us
7
8 stop_timeout            equ     500000 ; us
9 stopflash_timeout       equ      50000 ; us
10 retry_timeout           equ      50000 ; us
11 persist_timeout         equ      90000 ; us
12
13   udata_acs
14 settling        res     1 ; all counters are 0 if stopped
15 stop_wait       res     1 ;  or otherwise count down
16 retry_wait      res     1
17
18   code
19
20 ;======================================================================
21 ;
22 ; POLARISING/SETTLING:
23 ;
24 ;               NMRA            flag_p. settling        Booster PWM
25 ;
26 ; Normal        running         0       stopped         mark
27 ; Polarising    paused          1       stopped         space
28 ; Settling      paused          1       running         space
29 ;
30 ; NMRA is paused by disabling timer 0 intr; when we restart,
31 ; we restart the current message.
32 ;
33 ;----------------------------------------------------------------------
34
35 ;--------------------
36 power_polarising_init @
37         clr_f   settling
38         pin_h   p0_booster_pwm
39         return
40
41 ;--------------------
42 power_polarising_begin @
43         pin_vl  p0_booster_pwm
44         bc_f    INTCON, TMR0IE ; disables NMRA interrupt
45         bs_f    flags, flags_polarising
46         clr_f   settling
47         return
48
49 ;--------------------
50 power_polarising_nonetodo @
51 ; idempotent
52         bt_f_if0 flags, flags_polarising
53         return
54         tst_f_ifnz settling
55         return
56         mov_lw  settling_timeout / tick_us
57         mov_wf  settling
58         return
59
60 ;--------------------
61 power_polarising_tick @
62         tst_f_ifnz settling
63         dec_f_ifnz settling ; decrement if not zero
64         return ; return if not dec'd, or if decrement didn't make zero
65         ; settle timeout complete:
66
67         bc_f    flags, flags_polarising
68         pin_vh  p0_booster_pwm
69         call    nmra_restartmessage
70         bs_f    INTCON, TMR0IE ; reenables NMRA interrupt
71         goto    i2c_consider_restartread
72
73 ;======================================================================
74 ;
75 ; FAULTS AND POWER:
76 ;
77 ;           Shutdown  CDU  Time   User Fault  Next states and action when
78 ;                                             fault          timeout  ON  OFF
79 ;
80 ; Off       shutdown  off  none   off                                 On
81 ; On        power-up  on   none   off         Stopping,FAULT              Off
82 ; Stopping  shutdown  on   500ms  50% 1/100ms                Retry      
83 ; Retry     power-up  off  50ms   off         Persists       Off,FIXED    Off
84 ; Persists  shutdown  off  90ms   on                         Retry        Off
85 ;
86 ;                                               (no notation: event ignored)
87  make code correspond to new
88  get rid of Clearing state.
89 ;
90 ;               shutdown LAT    stop_wait       retry_wait
91 ;
92 ; Off           H (shutdown)    0               0
93 ; On            L (power-up)    0               0
94 ; Stopping      H (shutdown)    before Retry    for LED flash
95 ; Retry         L (power-up)    0               before Off
96 ; Persists      H (shutdown)    0               before retry
97 ;
98 ;                               (uses tickdiv)  (uses tick)
99
100 ;--------------------
101 power_fault_init @
102         bs_f    INTCON2, INTEDG1 ; rising edge
103         bc_f    INTCON3, INT1IP
104         bc_f    INTCON3, INT1IF
105         bs_f    INTCON3, INT1IE
106         clr_f   stop_wait
107         clr_f   retry_wait
108         pin_h   p0_booster_shutdown
109         pin_h   p0_booster_userfault
110         ; now we are Off
111         return
112
113 command_power_section code 0x2080
114 ;--------------------
115 command_power_on @
116 ; Off -> On; noop in other states
117         pinlat_ifl p0_booster_shutdown
118         return  ; On or Retry
119         ; might be Off, Stopping or Persists:
120
121         tst_f_ifnz retry_wait
122         return  ; that deals with Stopping or Persists
123         ; must be Off:
124
125         pin_vl  p0_booster_shutdown
126         Dl 0x81
127         goto    cdu_on
128
129 ;----------
130 command_power_off @
131 ; On -> Off; noop in other states
132         pinlat_ifh p0_booster_shutdown
133         return  ; Off, Stopping or Persists
134         ; might be On or Retry:
135
136         tst_f_ifnz retry_wait
137         return  ; that deals with Retry
138         ; must be On:
139 power_off_now
140         pin_vh  p0_booster_shutdown
141         goto    cdu_off
142
143 code2 code
144 ;--------------------
145 power_fault_intrl @
146         bt_f_if0 INTCON3, INT1IF
147         return
148         ; we have a fault:
149
150         bc_f    INTCON3, INT1IF
151         call    power_off_now
152
153         mov_fw  retry_wait
154         bra_z   power_fault_newshort ; we were Off or On
155         ; must be Stopping, Retry or Persists:
156
157         tst_f_ifnz stop_wait
158         bra     power_fault_stopmore ; we were already Stopping; restart timer
159         ; might be Retry or Persists, goto Persists (restarting any timer):
160
161         mov_lw  persist_timeout / tick_us
162         mov_wf  retry_wait
163         pin_vl  p0_booster_userfault
164 faultintrl_noop
165         intrl_handled_nostack
166
167 ;-----
168 power_fault_newshort
169         mov_lw  b'00000111'
170         call    serial_addbyte
171 power_fault_stopmore
172         mov_lw  stop_timeout / tickdiv_us
173         mov_wf  stop_wait
174         pin_vl  p0_booster_userfault
175         rcall   power_stop_doflash
176         intrl_handled_nostack
177
178 ;--------------------
179 power_fault_tick @
180         tst_f_ifnz retry_wait
181         dec_f_ifnz retry_wait
182         return
183         ; retry_wait was running but has just reached zero;
184         ; we were in Stopping, Retry or Persists:
185
186         tst_f_ifnz stop_wait
187         bra     power_stop_doflash ; that was Stopping, just flash the LED
188         ; must be Retry or Persists:
189
190         pinlat_ifh p0_booster_shutdown
191         bra     power_retry ; we were in Persists, now we can try again
192         ; we were in Retry and it worked.
193 ; go to Off:
194         pin_vh  p0_booster_shutdown
195         pin_vh  p0_booster_userfault
196         mov_lw  b'00000110'
197         goto    serial_addbyte
198
199 ;----------
200 power_stop_doflash
201         pin_vhl p0_booster_userfault
202         mov_lw  stopflash_timeout / tick_us
203         mov_wf  retry_wait
204         return
205
206 ;--------------------
207 power_fault_tickdiv @
208         tst_f_ifnz stop_wait
209         dec_f_ifnz stop_wait
210         return
211         ; stop_wait was running but has just reached zero;
212         ; we were in Stopping, now we can Retry:
213 power_retry
214         mov_lw  retry_timeout / tickdiv_us
215         mov_wf  retry_wait
216         pin_vl  p0_booster_shutdown
217         pin_vh  p0_booster_userfault
218         return
219
220 ;--------------------
221 power_panichook @
222         pin_vh  p0_booster_shutdown
223         pin_vl  p0_booster_pwm
224         pin_vh  p0_booster_userfault
225         return
226
227 ;======================================================================
228   include final.inc