1 ;======================================================================
2 ; TRACK POWER AND SHORT CIRCUIT
6 settling_timeout equ 10000 ; us
8 stopping_timeout equ 500000 ; us
9 stopflash_timeout equ 100000 ; us
10 retriableflash_timeout equ 500000 ; us
13 settling res 1 ; all counters are 0 if stopped
14 stopping_wait res 1 ; or otherwise count down
17 fault_spurious_count res 1
21 ;======================================================================
23 ; POLARISING/SETTLING:
25 ; NMRA flag_p. settling Booster PWM
27 ; Normal running 0 stopped mark
28 ; Polarising paused 1 stopped space
29 ; Settling paused 1 running space
31 ; NMRA is paused by disabling timer 0 intr; when we restart,
32 ; we restart the current message.
34 ;----------------------------------------------------------------------
37 power_polarising_init @
43 power_polarising_begin @
45 bc_f INTCON, TMR0IE ; disables NMRA interrupt
46 bs_f flags, flags_polarising
51 power_polarising_nonetodo @
53 bt_f_if0 flags, flags_polarising
57 mov_lw settling_timeout / tick_us
62 power_polarising_tick @
64 dec_f_ifnz settling ; decrement if not zero
65 return ; return if not dec'd, or if decrement didn't make zero
66 ; settle timeout complete:
68 bc_f flags, flags_polarising
70 tst_f_ifnz stopping_wait
71 bra power_polarising_done_ifstopping
73 power_polarising_done_ifstopping
75 call nmra_restartmessage
76 bs_f INTCON, TMR0IE ; reenables NMRA interrupt
77 goto i2c_consider_restartread
79 ;======================================================================
84 ; Off On Stopping Retriable Watchdogged
86 ; stopping_wait 0 0 >0 0 0
87 ; ledflash_wait 0 0 >0 >0 0
88 ; b._shutdowwn H L H H H
89 ; b._userfault_ H H * * L
93 ; Fault LED - - 50% 1/100ms 50% 1/500ms on
94 ; Timeout? - - 500ms - -
95 ; next state Retriable
99 ; booster_pwm !polar !polar OFF !polar !polar[1]
100 ; so power is - ON off! - -
103 ; Fault - Stopping - Stopping[2] Stopping
104 ; Watchdog - W'dogged - - -
106 ; OFF (Off) Off - Off Off
107 ; (- means event ignored)
109 ; [1] booster_pwm `!polar' means on if we're not polarising.
110 ; [2] this is not supposed to happen but we don't check for it
112 ;--------------------
114 bs_f INTCON2, INTEDG1 ; rising edge
118 pin_h p0_booster_shutdown
119 pin_h p0_booster_userfault_
122 clr_f fault_spurious_count
126 command_power_section code 0x2080
127 ;--------------------
129 tst_f_ifnz stopping_wait
130 return ; that deals with Stopping
131 ; safe to just turn On, even if already On
133 pin_vl p0_booster_shutdown
134 pin_vh p0_booster_userfault_
140 tst_f_ifnz stopping_wait
141 return ; that deals with Stopping
142 ; safe to just turn Off, even if already Off
144 pin_vh p0_booster_shutdown
145 pin_vh p0_booster_userfault_
150 ;--------------------
152 bt_f_if0 INTCON3, INT1IF
154 ; we may have a fault:
159 pin_inw_ifh p0_booster_overload
163 inc_f_ifz fault_spurious_count ; ++f.s._count == 0x00 ?
164 bs_f fault_spurious_count, 7 ; f.s._count = 0x80
166 intrl_handled_nostack
170 pin_vl p0_booster_pwm
171 pin_vh p0_booster_shutdown
172 tst_f_ifnz stopping_wait
173 bra fault_isactual_ifalreadystopping
178 mov_lw stopping_timeout / tickdiv_us
180 pin_vh p0_booster_userfault_
181 rcall power_led_start_stoppingflash
183 fault_isactual_ifalreadystopping
184 intrl_handled_nostack
186 ;--------------------
188 tst_f_ifnz ledflash_wait
189 dec_f_ifnz ledflash_wait
191 ;... ledflash_wait was running but has just reached zero; flash the LED
193 pin_vhl p0_booster_userfault_
195 power_led_start_retriableflash
196 mov_lw retriableflash_timeout / 2 / tick_us
197 tst_f_ifnz stopping_wait
198 power_led_start_stoppingflash
199 mov_lw stopflash_timeout / 2 / tick_us
203 ;--------------------
204 power_watchdog_timeout @
205 pinlat_ifh p0_booster_shutdown
207 ;... must be On, so go to Watchdogged
208 pin_vh p0_booster_shutdown
209 pin_vl p0_booster_userfault_
212 ;--------------------
213 power_fault_tickdiv @
214 tst_f_ifnz fault_spurious_count
215 bra fault_spurious_tickdiv
218 tst_f_ifnz stopping_wait
219 dec_f_ifnz stopping_wait
221 ; stopping_wait was running but has just reached zero;
222 ; we were in Stopping, go to Retriable:
224 rcall power_led_start_retriableflash
226 bt_f_if0 flags, flags_polarising
227 pin_vh p0_booster_pwm
233 fault_spurious_tickdiv
234 ; does not return, instead continues with fault_tickdiv_rest
235 mov_lw 0xc1 ; SPURIOUS
238 mov_fw fault_spurious_count
239 bra_n fault_spurious_tickdiv_isoverflow
240 fault_spurious_tickdiv_writeamt
241 call serial_addbyte_another
242 clr_f fault_spurious_count
243 bra fault_tickdiv_rest
246 fault_spurious_tickdiv_isoverflow
248 bra fault_spurious_tickdiv_writeamt
250 ;--------------------
252 pin_vh p0_booster_shutdown
253 pin_vl p0_booster_pwm
254 pin_vh p0_booster_userfault_
257 ;======================================================================