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 Timeout User Fault Next states
79 ; Off shutdown off none off *On (or +Fault)
80 ; On power-up on none off Fault, or *Off
81 ; Stopping shutdown off 500ms 50% 1/100ms ->Retry
82 ; Retry power-up off 50ms off ->Off, or Persists
83 ; Persists shutdown off 90ms on ->Retry
84 ; * = when host instructs
88 ; shutdown LAT stop_wait retry_wait
90 ; Off H (shutdown) stopped stopped
91 ; On L (power-up) stopped stopped
92 ; Stopping H (shutdown) before Retry for LED flash
93 ; Retry L (power-up) stopped before Off
94 ; Persists H (shutdown) stopped 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
128 ; On -> Off; noop in other states
129 pinlat_ifh p0_booster_shutdown
130 return ; Off, Stopping or Persists
131 ; might be On or Retry:
133 tst_f_ifnz retry_wait
134 return ; that deals with Retry
137 pin_vh p0_booster_shutdown
141 ;--------------------
143 bt_f_if0 INTCON3, INT1IF
151 bra_z power_fault_stop ; we were Off or On, goto Stopping
152 ; must be Stopping, Retry or Persists:
155 bra power_fault_stop ; we were already Stopping; restart timer
156 ; might be Retry or Persists, goto Persists (restarting any timer):
158 mov_lw persist_timeout / tick_us
160 pin_vl p0_booster_userfault
165 mov_lw stop_timeout / tickdiv_us
167 pin_vl p0_booster_userfault
170 rcall power_stop_doflash
171 intrl_handled_nostack
173 ;--------------------
175 tst_f_ifnz retry_wait
176 dec_f_ifnz retry_wait
178 ; retry_wait was running but has just reached zero;
179 ; we were in Stopping, Retry or Persists:
182 bra power_stop_doflash ; that was Stopping, just flash the LED
183 ; must be Retry or Persists:
185 pinlat_ifh p0_booster_shutdown
186 bra power_retry ; we were in Persists, now we can try again
187 ; we were in Retry and it worked.
189 pin_vh p0_booster_shutdown
190 pin_vh p0_booster_userfault
196 pin_vhl p0_booster_userfault
197 mov_lw stopflash_timeout / tick_us
201 ;--------------------
202 power_fault_tickdiv @
206 ; stop_wait was running but has just reached zero;
207 ; we were in Stopping, now we can Retry:
209 mov_lw retry_timeout / tickdiv_us
211 pin_vl p0_booster_shutdown
212 pin_vh p0_booster_userfault
215 ;--------------------
217 pin_vh p0_booster_shutdown
218 pin_vl p0_booster_pwm
219 pin_vh p0_booster_userfault
222 ;======================================================================