1 ;======================================================================
2 ; TRACK POWER AND SHORT CIRCUIT
6 settling_timeout equ 10000 ; us
8 stop_timeout equ 500000 ; us
9 stopflash_timeout equ 25000 ; 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 ;----------------------------------------------------------------------
41 power_polarising_begin
43 bc_f INTCON, TMR0IE ; disables NMRA interrupt
44 bs_f flags, flags_polarising
49 power_polarising_settling
53 mov_lw settling_timeout / tick_us
60 dec_f_ifnz settling ; decrement if not zero
61 return ; return if not dec'd, or if decrement didn't make zero
62 ; settle timeout complete:
64 bc_f flags, flags_polarising
65 call nmra_restartmessage
66 bs_f INTCON, TMR0IE ; reenables NMRA interrupt
67 goto i2c_consider_restartread
69 ;======================================================================
73 ; Shutdown CDU Timeout User Fault Next states
75 ; Off shutdown off none off *On (or +Fault)
76 ; On power-up on none off Fault, or *Off
77 ; Stopping shutdown off 500ms 50% 1/50ms ->Retry
78 ; Retry power-up off 50ms off ->Off, or Persists
79 ; Persists shutdown off 90ms on ->Retry
80 ; * = when host instructs
84 ; shutdown LAT stop_wait retry_wait
86 ; Off H (shutdown) stopped stopped
87 ; On L (power-up) stopped stopped
88 ; Stopping H (shutdown) before Retry for LED flash
89 ; Retry L (power-up) stopped before Off
90 ; Persists H (shutdown) stopped before retry
92 ; (uses tickdiv) (uses tick)
96 bs_f INTCON2, INTEDG1 ; rising edge
102 pin_h p0_booster_shutdown
103 pin_vl p0_booster_userfault
104 pin_z p0_booster_userfault
108 ;--------------------
110 bra_nc command_power_off
113 ; Off -> On; noop in other states
114 pinlat_ifh 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_l p0_booster_shutdown
127 ; On -> Off; noop in other states
128 pinlat_ifl 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_h p0_booster_shutdown
139 ;--------------------
141 bt_f_if1 INTCON3, INT1IF
146 bra_z power_stop ; we were Off or On, goto Stopping
147 ; must be Stopping, Retry or Persists:
150 bra power_stop ; we were already Stopping; restart timer
151 ; might be Retry or Persists, goto Persists (restarting any timer):
154 mov_lw persist_timeout / tick_us
158 ;--------------------
160 tst_f_ifnz retry_wait
161 dec_f_ifnz retry_wait
163 ; retry_wait was running but has just reached zero;
164 ; we were in Stopping, Retry or Persists:
167 bra power_stop_doflash ; that was Stopping, just flash the LED
168 ; must be Retry or Persists:
170 pinlat_ifl p0_booster_shutdown
171 bra power_retry ; we were in Persists, now we can try again
172 ; we were in Retry but it didn't work:
174 pin_h p0_booster_shutdown
175 mov_lw persist_timeout / tickdiv_us
177 pin_nz p0_booster_userfault
183 mov_lw stop_timeout / tickdiv_us
185 pin_z p0_booster_userfault
189 pin_znz p0_booster_userfault
190 mov_lw stopflash_timeout / tickdiv_us
194 ;--------------------
199 ; stop_wait was running but has just reached zero;
200 ; we were in Stopping, now we can Retry:
202 mov_lw retry_timeout / tickdiv_us
204 pin_l p0_booster_shutdown
205 pin_z p0_booster_userfault
208 ;--------------------
210 pin_h p0_booster_shutdown
212 pin_z p0_booster_userfault
215 ;======================================================================