1 ;======================================================================
2 ; TRACK POWER AND SHORT CIRCUIT
6 settling_timeout equ 10000 ; us
8 stop_timeout equ 500000 ; us
9 stopflash_timeout equ 50000 ; us
10 retry_timeout equ 50000 ; us
11 persist_timeout equ 90000 ; us
14 settling res 1 ; all counters are 0 if stopped
15 stop_wait res 1 ; or otherwise count down
18 fault_spurious_count res 1
22 ;======================================================================
24 ; POLARISING/SETTLING:
26 ; NMRA flag_p. settling Booster PWM
28 ; Normal running 0 stopped mark
29 ; Polarising paused 1 stopped space
30 ; Settling paused 1 running space
32 ; NMRA is paused by disabling timer 0 intr; when we restart,
33 ; we restart the current message.
35 ;----------------------------------------------------------------------
38 power_polarising_init @
44 power_polarising_begin @
46 bc_f INTCON, TMR0IE ; disables NMRA interrupt
47 bs_f flags, flags_polarising
52 power_polarising_nonetodo @
54 bt_f_if0 flags, flags_polarising
58 mov_lw settling_timeout / tick_us
63 power_polarising_tick @
65 dec_f_ifnz settling ; decrement if not zero
66 return ; return if not dec'd, or if decrement didn't make zero
67 ; settle timeout complete:
69 bc_f flags, flags_polarising
71 call nmra_restartmessage
72 bs_f INTCON, TMR0IE ; reenables NMRA interrupt
73 goto i2c_consider_restartread
75 ;======================================================================
79 ; Shutdown CDU Time User Fault Next states and action when
80 ; fault timeout ON OFF
82 ; Off shutdown off none off On
83 ; On power-up on none off Stopping,FAULT Off
84 ; Stopping shutdown on 500ms 50% 1/100ms Retry
85 ; Retry power-up off 50ms off Persists Off,FIXED Off
86 ; Persists shutdown off 90ms on Retry Off
88 ; (no notation: event ignored)
90 ; shutdown LAT stop_wait retry_wait
92 ; Off H (shutdown) 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
98 ; (uses tickdiv) (uses tick)
100 ;--------------------
102 bs_f INTCON2, INTEDG1 ; rising edge
108 clr_f fault_spurious_count
109 pin_h p0_booster_shutdown
110 pin_h p0_booster_userfault
114 command_power_section code 0x2080
115 ;--------------------
117 ; Off -> On; noop in other states
118 pinlat_ifl p0_booster_shutdown
120 ; might be Off, Stopping or Persists:
122 tst_f_ifnz retry_wait
123 return ; that deals with Stopping or Persists
126 pin_vl p0_booster_shutdown
132 ; On, Retry, Persists -> Off; noop in Off or Stopping
134 return ; that deals with Stopping
135 ; must be On, Off, Retry or Persists:
136 ; (it's harmless to do this if we were already Off)
139 pin_vh p0_booster_userfault
140 pin_vh p0_booster_shutdown
144 ;--------------------
146 bt_f_if0 INTCON3, INT1IF
148 ; we may have a fault:
153 pin_inw_ifh p0_booster_overload
157 inc_f_ifz fault_spurious_count ; ++f.s._count == 0x00 ?
158 bs_f fault_spurious_count, 7 ; f.s._count = 0x80
160 intrl_handled_nostack
164 pinlat_ifh p0_booster_shutdown
166 ; Off, Stopping or Persists - must have lost the race
167 ; or flapped while we faffed. Well, never mind, it's
168 ; off now and we're taking care of it. (If the host
169 ; caused Off by OFF they can at worst send an OFF and
170 ; immediate ON in about 2ms, so worst-case duty cycle
171 ; for a short (if the host is perversely timed) is
172 ; (our interrupt latency) / 2ms
173 ; and if they ever don't manage to get the OFF through
174 ; in time then we go into Stopping.)
175 ; must be On or Retry:
177 pin_vh p0_booster_shutdown
178 tst_f_ifnz retry_wait
179 bra fault_persists ; that deals with Retry
185 mov_lw stop_timeout / tickdiv_us
187 pin_vl p0_booster_userfault
188 rcall power_stop_doflash
189 intrl_handled_nostack
192 fault_persists ; Retry -> Persists (booster was shut down just a moment ago)
193 mov_lw persist_timeout / tick_us
195 pin_vl p0_booster_userfault
197 intrl_handled_nostack
199 ;--------------------
201 tst_f_ifnz retry_wait
202 dec_f_ifnz retry_wait
204 ; retry_wait was running but has just reached zero;
205 ; we were in Stopping, Retry or Persists:
208 bra power_stop_doflash ; that deals with Stopping (just flash)
209 ; must be Retry or Persists:
211 pinlat_ifh p0_booster_shutdown
212 bra power_retry ; we were in Persists, now we can try again
213 ; we were in Retry and it worked.
215 pin_vh p0_booster_shutdown
216 pin_vh p0_booster_userfault
222 pin_vhl p0_booster_userfault
223 mov_lw stopflash_timeout / tick_us
227 ;--------------------
228 power_fault_tickdiv @
229 tst_f_ifnz fault_spurious_count
230 bra fault_spurious_tickdiv
236 ; stop_wait was running but has just reached zero;
237 ; we were in Stopping, now we can Retry:
240 mov_lw retry_timeout / tickdiv_us
242 pin_vl p0_booster_shutdown
243 pin_vh p0_booster_userfault
247 fault_spurious_tickdiv
248 ; does not return, instead continues with fault_tickdiv_rest
249 mov_lw 0xc1 ; SPURIOUS
252 mov_fw fault_spurious_count
253 bra_n fault_spurious_tickdiv_isoverflow
254 fault_spurious_tickdiv_writeamt
255 call serial_addbyte_another
256 clr_f fault_spurious_count
257 bra fault_tickdiv_rest
260 fault_spurious_tickdiv_isoverflow
262 bra fault_spurious_tickdiv_writeamt
264 ;--------------------
266 pin_vh p0_booster_shutdown
267 pin_vl p0_booster_pwm
268 pin_vh p0_booster_userfault
271 ;======================================================================