chiark / gitweb /
new workable OFF behaviour implemented and it compiles but is not tested
[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 ;
88 ;               shutdown LAT    stop_wait       retry_wait
89 ;
90 ; Off           H (shutdown)    0               0
91 ; On            L (power-up)    0               0
92 ; Stopping      H (shutdown)    before Retry    for LED flash
93 ; Retry         L (power-up)    0               before Off
94 ; Persists      H (shutdown)    0               before retry
95 ;
96 ;                               (uses tickdiv)  (uses tick)
97
98 ;--------------------
99 power_fault_init @
100         bs_f    INTCON2, INTEDG1 ; rising edge
101         bc_f    INTCON3, INT1IP
102         bc_f    INTCON3, INT1IF
103         bs_f    INTCON3, INT1IE
104         clr_f   stop_wait
105         clr_f   retry_wait
106         pin_h   p0_booster_shutdown
107         pin_h   p0_booster_userfault
108         ; now we are Off
109         return
110
111 command_power_section code 0x2080
112 ;--------------------
113 command_power_on @
114 ; Off -> On; noop in other states
115         pinlat_ifl p0_booster_shutdown
116         return  ; On or Retry
117         ; might be Off, Stopping or Persists:
118
119         tst_f_ifnz retry_wait
120         return  ; that deals with Stopping or Persists
121         ; must be Off:
122
123         pin_vl  p0_booster_shutdown
124         Dl 0x81
125         goto    cdu_on
126
127 ;----------
128 command_power_off @
129 ; On, Retry, Persists -> Off;  noop in Off or Stopping
130         tst_f_ifnz stop_wait
131         return  ; that deals with Stopping
132         ; must be On, Off, Retry or Persists:
133         ; (it's harmless to do this if we were already Off)
134 power_off_now
135         clr_f   retry_wait
136         pin_vh  p0_booster_userfault
137         pin_vh  p0_booster_shutdown
138         goto    cdu_off
139
140 code2 code
141 ;--------------------
142 power_fault_intrl @
143         bt_f_if0 INTCON3, INT1IF
144         return
145         ; we have a fault:
146
147         bc_f    INTCON3, INT1IF
148
149         pinlat_ifh p0_booster_shutdown
150         return  ; Off, Stopping or Persists - must have lost the race
151                 ;  or flapped while we faffed.  Well, never mind, it's
152                 ;  off now and we're taking care of it.  (If the host
153                 ;  caused Off by OFF they can at worst send an OFF and
154                 ;  immediate ON in about 2ms, so worst-case duty cycle
155                 ;  for a short (if the host is perversely timed) is
156                 ;      (our interrupt latency) / 2ms
157                 ;  and if they ever don't manage to get the OFF through
158                 ;  in time then we go into Stopping.)
159         ; must be On or Retry:
160
161         pin_vh  p0_booster_shutdown
162         tst_f_ifnz retry_wait
163         bra     fault_persists ; that deals with Retry
164         ; must be On:
165
166         mov_lw  b'00000111'
167         call    serial_addbyte
168
169         mov_lw  stop_timeout / tickdiv_us
170         mov_wf  stop_wait
171         pin_vl  p0_booster_userfault
172         rcall   power_stop_doflash
173         intrl_handled_nostack
174
175 ;-----
176 fault_persists ; Retry -> Persists (booster was shut down just a moment ago)
177         mov_lw  persist_timeout / tick_us
178         mov_wf  retry_wait
179         pin_vl  p0_booster_userfault
180 faultintrl_noop
181         intrl_handled_nostack
182
183 ;--------------------
184 power_fault_tick @
185         tst_f_ifnz retry_wait
186         dec_f_ifnz retry_wait
187         return
188         ; retry_wait was running but has just reached zero;
189         ; we were in Stopping, Retry or Persists:
190
191         tst_f_ifnz stop_wait
192         bra     power_stop_doflash ; that deals with Stopping (just flash)
193         ; must be Retry or Persists:
194
195         pinlat_ifh p0_booster_shutdown
196         bra     power_retry ; we were in Persists, now we can try again
197         ; we were in Retry and it worked.
198 ; go to Off:
199         pin_vh  p0_booster_shutdown
200         pin_vh  p0_booster_userfault
201         mov_lw  b'00000110'
202         goto    serial_addbyte
203
204 ;----------
205 power_stop_doflash
206         pin_vhl p0_booster_userfault
207         mov_lw  stopflash_timeout / tick_us
208         mov_wf  retry_wait
209         return
210
211 ;--------------------
212 power_fault_tickdiv @
213         tst_f_ifnz stop_wait
214         dec_f_ifnz stop_wait
215         return
216         ; stop_wait was running but has just reached zero;
217         ; we were in Stopping, now we can Retry:
218         call    cdu_off
219 power_retry
220         mov_lw  retry_timeout / tickdiv_us
221         mov_wf  retry_wait
222         pin_vl  p0_booster_shutdown
223         pin_vh  p0_booster_userfault
224         return
225
226 ;--------------------
227 power_panichook @
228         pin_vh  p0_booster_shutdown
229         pin_vl  p0_booster_pwm
230         pin_vh  p0_booster_userfault
231         return
232
233 ;======================================================================
234   include final.inc