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
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:
161 pin_vh p0_booster_shutdown
162 tst_f_ifnz retry_wait
163 bra fault_persists ; that deals with Retry
169 mov_lw stop_timeout / tickdiv_us
171 pin_vl p0_booster_userfault
172 rcall power_stop_doflash
173 intrl_handled_nostack
176 fault_persists ; Retry -> Persists (booster was shut down just a moment ago)
177 mov_lw persist_timeout / tick_us
179 pin_vl p0_booster_userfault
181 intrl_handled_nostack
183 ;--------------------
185 tst_f_ifnz retry_wait
186 dec_f_ifnz retry_wait
188 ; retry_wait was running but has just reached zero;
189 ; we were in Stopping, Retry or Persists:
192 bra power_stop_doflash ; that deals with Stopping (just flash)
193 ; must be Retry or Persists:
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.
199 pin_vh p0_booster_shutdown
200 pin_vh p0_booster_userfault
206 pin_vhl p0_booster_userfault
207 mov_lw stopflash_timeout / tick_us
211 ;--------------------
212 power_fault_tickdiv @
216 ; stop_wait was running but has just reached zero;
217 ; we were in Stopping, now we can Retry:
220 mov_lw retry_timeout / tickdiv_us
222 pin_vl p0_booster_shutdown
223 pin_vh p0_booster_userfault
226 ;--------------------
228 pin_vh p0_booster_shutdown
229 pin_vl p0_booster_pwm
230 pin_vh p0_booster_userfault
233 ;======================================================================