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
20 ;======================================================================
22 ; POLARISING/SETTLING:
24 ; NMRA flag_p. settling Booster PWM
26 ; Normal running 0 stopped mark
27 ; Polarising paused 1 stopped space
28 ; Settling paused 1 running space
30 ; NMRA is paused by disabling timer 0 intr; when we restart,
31 ; we restart the current message.
33 ;----------------------------------------------------------------------
36 power_polarising_init @
42 power_polarising_begin @
44 bc_f INTCON, TMR0IE ; disables NMRA interrupt
45 bs_f flags, flags_polarising
50 power_polarising_nonetodo @
52 bt_f_if0 flags, flags_polarising
56 mov_lw settling_timeout / tick_us
61 power_polarising_tick @
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:
67 bc_f flags, flags_polarising
69 call nmra_restartmessage
70 bs_f INTCON, TMR0IE ; reenables NMRA interrupt
71 goto i2c_consider_restartread
73 ;======================================================================
77 ; Shutdown CDU Time User Fault Next states and action when
78 ; fault timeout ON OFF
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
86 ; (no notation: event ignored)
88 ; shutdown LAT stop_wait retry_wait
90 ; Off H (shutdown) 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
96 ; (uses tickdiv) (uses tick)
100 bs_f INTCON2, INTEDG1 ; rising edge
106 pin_h p0_booster_shutdown
107 pin_h p0_booster_userfault
111 command_power_section code 0x2080
112 ;--------------------
114 ; Off -> On; noop in other states
115 pinlat_ifl p0_booster_shutdown
117 ; might be Off, Stopping or Persists:
119 tst_f_ifnz retry_wait
120 return ; that deals with Stopping or Persists
123 pin_vl p0_booster_shutdown
129 ; On, Retry, Persists -> Off; noop in Off or Stopping
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)
136 pin_vh p0_booster_userfault
137 pin_vh p0_booster_shutdown
141 ;--------------------
143 bt_f_if0 INTCON3, INT1IF
149 pinlat_ifh p0_booster_shutdown
151 ; Off, Stopping or Persists - must have lost the race
152 ; or flapped while we faffed. Well, never mind, it's
153 ; off now and we're taking care of it. (If the host
154 ; caused Off by OFF they can at worst send an OFF and
155 ; immediate ON in about 2ms, so worst-case duty cycle
156 ; for a short (if the host is perversely timed) is
157 ; (our interrupt latency) / 2ms
158 ; and if they ever don't manage to get the OFF through
159 ; in time then we go into Stopping.)
160 ; must be On or Retry:
162 pin_vh p0_booster_shutdown
163 tst_f_ifnz retry_wait
164 bra fault_persists ; that deals with Retry
170 mov_lw stop_timeout / tickdiv_us
172 pin_vl p0_booster_userfault
173 rcall power_stop_doflash
174 intrl_handled_nostack
177 fault_persists ; Retry -> Persists (booster was shut down just a moment ago)
178 mov_lw persist_timeout / tick_us
180 pin_vl p0_booster_userfault
182 intrl_handled_nostack
184 ;--------------------
186 tst_f_ifnz retry_wait
187 dec_f_ifnz retry_wait
189 ; retry_wait was running but has just reached zero;
190 ; we were in Stopping, Retry or Persists:
193 bra power_stop_doflash ; that deals with Stopping (just flash)
194 ; must be Retry or Persists:
196 pinlat_ifh p0_booster_shutdown
197 bra power_retry ; we were in Persists, now we can try again
198 ; we were in Retry and it worked.
200 pin_vh p0_booster_shutdown
201 pin_vh p0_booster_userfault
207 pin_vhl p0_booster_userfault
208 mov_lw stopflash_timeout / tick_us
212 ;--------------------
213 power_fault_tickdiv @
217 ; stop_wait was running but has just reached zero;
218 ; we were in Stopping, now we can Retry:
221 mov_lw retry_timeout / tickdiv_us
223 pin_vl p0_booster_shutdown
224 pin_vh p0_booster_userfault
227 ;--------------------
229 pin_vh p0_booster_shutdown
230 pin_vl p0_booster_pwm
231 pin_vh p0_booster_userfault
234 ;======================================================================