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 ;----------------------------------------------------------------------
42 power_polarising_begin
44 bc_f INTCON, TMR0IE ; disables NMRA interrupt
45 bs_f flags, flags_polarising
50 power_polarising_settling
54 mov_lw settling_timeout / tick_us
61 dec_f_ifnz settling ; decrement if not zero
62 return ; return if not dec'd, or if decrement didn't make zero
63 ; settle timeout complete:
65 bc_f flags, flags_polarising
66 call nmra_restartmessage
67 bs_f INTCON, TMR0IE ; reenables NMRA interrupt
68 goto i2c_consider_restartread
70 ;======================================================================
74 ; Shutdown CDU Timeout User Fault Next states
76 ; Off shutdown off none off *On (or +Fault)
77 ; On power-up on none off Fault, or *Off
78 ; Stopping shutdown off 500ms 50% 1/100ms ->Retry
79 ; Retry power-up off 50ms off ->Off, or Persists
80 ; Persists shutdown off 90ms on ->Retry
81 ; * = when host instructs
85 ; shutdown LAT stop_wait retry_wait
87 ; Off H (shutdown) stopped stopped
88 ; On L (power-up) stopped stopped
89 ; Stopping H (shutdown) before Retry for LED flash
90 ; Retry L (power-up) stopped before Off
91 ; Persists H (shutdown) stopped before retry
93 ; (uses tickdiv) (uses tick)
97 bs_f INTCON2, INTEDG1 ; rising edge
103 pin_h p0_booster_shutdown
104 pin_h p0_booster_userfault
108 ;--------------------
110 bra_nc command_power_off
113 ; Off -> On; noop in other states
114 pinlat_ifl p0_booster_shutdown
116 ; might be Off, Stopping or Persists:
118 tst_f_ifnz retry_wait
119 return ; that deals with Stopping or Persists
122 pin_vl p0_booster_shutdown
127 ; On -> Off; noop in other states
128 pinlat_ifh p0_booster_shutdown
129 return ; Off, Stopping or Persists
130 ; might be On or Retry:
132 tst_f_ifnz retry_wait
133 return ; that deals with Retry
136 pin_vh p0_booster_shutdown
139 ;--------------------
141 bt_f_if0 INTCON3, INT1IF
149 bra_z power_fault_stop ; we were Off or On, goto Stopping
150 ; must be Stopping, Retry or Persists:
153 bra power_fault_stop ; we were already Stopping; restart timer
154 ; might be Retry or Persists, goto Persists (restarting any timer):
156 mov_lw persist_timeout / tick_us
158 pin_vl p0_booster_userfault
163 mov_lw stop_timeout / tickdiv_us
165 pin_vl p0_booster_userfault
168 rcall power_stop_doflash
169 intrl_handled_nostack
171 ;--------------------
173 tst_f_ifnz retry_wait
174 dec_f_ifnz retry_wait
176 ; retry_wait was running but has just reached zero;
177 ; we were in Stopping, Retry or Persists:
180 bra power_stop_doflash ; that was Stopping, just flash the LED
181 ; must be Retry or Persists:
183 pinlat_ifh p0_booster_shutdown
184 bra power_retry ; we were in Persists, now we can try again
185 ; we were in Retry and it worked.
187 pin_vh p0_booster_shutdown
188 pin_vh p0_booster_userfault
194 pin_vhl p0_booster_userfault
195 mov_lw stopflash_timeout / tick_us
199 ;--------------------
204 ; stop_wait was running but has just reached zero;
205 ; we were in Stopping, now we can Retry:
207 mov_lw retry_timeout / tickdiv_us
209 pin_vl p0_booster_shutdown
210 pin_vh p0_booster_userfault
213 ;--------------------
215 pin_vh p0_booster_shutdown
216 pin_vl p0_booster_pwm
217 pin_vh p0_booster_userfault
220 ;======================================================================