chiark / gitweb /
Merge and end branch-hostside-wip-2008-01-25 PROPERLY; cvs up -j branch-hostside...
[trains.git] / detpic / energy.asm
index bb4c3f47f3fead1365b98423d487d49f777b546b..c94d451494f0445934b998c357c0d59d91089afe 100644 (file)
@@ -15,6 +15,8 @@ settling      res     1 ; all counters are 0 if stopped
 stop_wait      res     1 ;  or otherwise count down
 retry_wait     res     1
 
+fault_spurious_count   res     1
+
   code
 
 ;======================================================================
@@ -49,26 +51,21 @@ power_polarising_begin @
 ;--------------------
 power_polarising_nonetodo @
 ; idempotent
- bs_f xdebug+6, 2
        bt_f_if0 flags, flags_polarising
        return
- bs_f xdebug+6, 3
        tst_f_ifnz settling
        return
        mov_lw  settling_timeout / tick_us
        mov_wf  settling
- bs_f xdebug+6, 4
        return
 
 ;--------------------
 power_polarising_tick @
- bs_f xdebug+6, 6
        tst_f_ifnz settling
        dec_f_ifnz settling ; decrement if not zero
        return ; return if not dec'd, or if decrement didn't make zero
        ; settle timeout complete:
 
- bs_f xdebug+6, 7
        bc_f    flags, flags_polarising
        pin_vh  p0_booster_pwm
        call    nmra_restartmessage
@@ -79,24 +76,24 @@ power_polarising_tick @
 ;
 ; FAULTS AND POWER:
 ;
-;              Shutdown  CDU   Timeout User Fault      Next states
+;          Shutdown  CDU  Time   User Fault  Next states and action when
+;                                            fault          timeout  ON  OFF
+;
+; Off      shutdown  off  none   off                                 On
+; On       power-up  on   none   off         Stopping,FAULT              Off
+; Stopping  shutdown  on   500ms  50% 1/100ms               Retry
+; Retry            power-up  off  50ms   off         Persists       Off,FIXED    Off
+; Persists  shutdown  off  90ms          on                         Retry        Off
 ;
-; Off          shutdown  off   none    off             *On (or +Fault)
-; On           power-up  on    none    off             Fault, or *Off
-; Stopping     shutdown  off   500ms   50% 1/100ms     ->Retry
-; Retry                power-up  off   50ms    off             ->Off, or Persists
-; Persists     shutdown  off   90ms    on              ->Retry
-;                                                      * = when host instructs
-;                                                      + = lost race, only
-;                                                      -> = after timeout
+;                                              (no notation: event ignored)
 ;
 ;              shutdown LAT    stop_wait       retry_wait
 ;
-; Off          H (shutdown)    stopped         stopped
-; On           L (power-up)    stopped         stopped
+; Off          H (shutdown)    0               0
+; On           L (power-up)    0               0
 ; Stopping     H (shutdown)    before Retry    for LED flash
-; Retry                L (power-up)    stopped         before Off
-; Persists     H (shutdown)    stopped         before retry
+; Retry                L (power-up)    0               before Off
+; Persists     H (shutdown)    0               before retry
 ;
 ;                              (uses tickdiv)  (uses tick)
 
@@ -108,6 +105,7 @@ power_fault_init @
        bs_f    INTCON3, INT1IE
        clr_f   stop_wait
        clr_f   retry_wait
+       clr_f   fault_spurious_count
        pin_h   p0_booster_shutdown
        pin_h   p0_booster_userfault
        ; now we are Off
@@ -126,19 +124,19 @@ command_power_on @
        ; must be Off:
 
        pin_vl  p0_booster_shutdown
+       Dl 0x81
        goto    cdu_on
 
 ;----------
 command_power_off @
-; On -> Off; noop in other states
-       pinlat_ifh p0_booster_shutdown
-       return  ; Off, Stopping or Persists
-       ; might be On or Retry:
-
-       tst_f_ifnz retry_wait
-       return  ; that deals with Retry
-       ; must be On:
+; On, Retry, Persists -> Off;  noop in Off or Stopping
+       tst_f_ifnz stop_wait
+       return  ; that deals with Stopping
+       ; must be On, Off, Retry or Persists:
+       ; (it's harmless to do this if we were already Off)
 power_off_now
+       clr_f   retry_wait
+       pin_vh  p0_booster_userfault
        pin_vh  p0_booster_shutdown
        goto    cdu_off
 
@@ -147,34 +145,57 @@ code2 code
 power_fault_intrl @
        bt_f_if0 INTCON3, INT1IF
        return
-       ; we have a fault:
+       ; we may have a fault:
 
        bc_f    INTCON3, INT1IF
-       call    power_off_now
 
-       mov_fw  retry_wait
-       bra_z   power_fault_stop ; we were Off or On, goto Stopping
-       ; must be Stopping, Retry or Persists:
+       call    portb_read
+       pin_inw_ifh p0_booster_overload
+       bra     fault_isactual
+       ; spurious:
 
-       tst_f_ifnz stop_wait
-       bra     power_fault_stop ; we were already Stopping; restart timer
-       ; might be Retry or Persists, goto Persists (restarting any timer):
+       inc_f_ifz fault_spurious_count  ; ++f.s._count == 0x00 ?
+       bs_f    fault_spurious_count, 7 ; f.s._count = 0x80
 
-       mov_lw  persist_timeout / tick_us
-       mov_wf  retry_wait
-       pin_vl  p0_booster_userfault
-       return
+       intrl_handled_nostack
 
 ;-----
-power_fault_stop
+fault_isactual
+       pinlat_ifh p0_booster_shutdown
+       bra faultintrl_noop
+               ; Off, Stopping or Persists - must have lost the race
+               ;  or flapped while we faffed.  Well, never mind, it's
+               ;  off now and we're taking care of it.  (If the host
+               ;  caused Off by OFF they can at worst send an OFF and
+               ;  immediate ON in about 2ms, so worst-case duty cycle
+               ;  for a short (if the host is perversely timed) is
+               ;      (our interrupt latency) / 2ms
+               ;  and if they ever don't manage to get the OFF through
+               ;  in time then we go into Stopping.)
+       ; must be On or Retry:
+
+       pin_vh  p0_booster_shutdown
+       tst_f_ifnz retry_wait
+       bra     fault_persists ; that deals with Retry
+       ; must be On:
+
+       mov_lw  b'00000111'
+       call    serial_addbyte
+
        mov_lw  stop_timeout / tickdiv_us
        mov_wf  stop_wait
        pin_vl  p0_booster_userfault
-       mov_lw  b'00000111'
-       call    serial_addbyte
        rcall   power_stop_doflash
        intrl_handled_nostack
 
+;-----
+fault_persists ; Retry -> Persists (booster was shut down just a moment ago)
+       mov_lw  persist_timeout / tick_us
+       mov_wf  retry_wait
+       pin_vl  p0_booster_userfault
+faultintrl_noop
+       intrl_handled_nostack
+
 ;--------------------
 power_fault_tick @
        tst_f_ifnz retry_wait
@@ -184,7 +205,7 @@ power_fault_tick @
        ; we were in Stopping, Retry or Persists:
 
        tst_f_ifnz stop_wait
-       bra     power_stop_doflash ; that was Stopping, just flash the LED
+       bra     power_stop_doflash ; that deals with Stopping (just flash)
        ; must be Retry or Persists:
 
        pinlat_ifh p0_booster_shutdown
@@ -205,11 +226,16 @@ power_stop_doflash
 
 ;--------------------
 power_fault_tickdiv @
+       tst_f_ifnz fault_spurious_count
+       bra     fault_spurious_tickdiv
+       ; in any case:
+fault_tickdiv_rest
        tst_f_ifnz stop_wait
        dec_f_ifnz stop_wait
        return
        ; stop_wait was running but has just reached zero;
        ; we were in Stopping, now we can Retry:
+       call    cdu_off
 power_retry
        mov_lw  retry_timeout / tickdiv_us
        mov_wf  retry_wait
@@ -217,6 +243,24 @@ power_retry
        pin_vh  p0_booster_userfault
        return
 
+;----------
+fault_spurious_tickdiv
+; does not return, instead continues with fault_tickdiv_rest
+       mov_lw  0xc1 ; SPURIOUS
+       call    serial_addbyte
+
+       mov_fw  fault_spurious_count
+       bra_n   fault_spurious_tickdiv_isoverflow
+fault_spurious_tickdiv_writeamt
+       call    serial_addbyte_another
+       clr_f   fault_spurious_count
+       bra     fault_tickdiv_rest
+
+;-----
+fault_spurious_tickdiv_isoverflow
+       mov_lw  0x7f
+       bra     fault_spurious_tickdiv_writeamt
+
 ;--------------------
 power_panichook @
        pin_vh  p0_booster_shutdown