chiark / gitweb /
detect.asm just calls panis
[trains.git] / detpic / panic.asm
1 ; program writes SOS msg into flash then turns LED green
2 ; when serial interrupt received, turns off interrupts, turns off
3 ; power, transmits contents of SSPCON1 by flashing LED red, repeats
4 ; (next version will transmit SOS code first, then contents of 
5 ; SSPCON1)
6
7
8 ; to start, need to write into flash, starting at 30 0000h:
9 ; 10 10 10 00 | 1110 1110 | 1110 00 10 | 10 10
10
11         include common.inc
12
13         extern  led_green
14         extern  led_red
15         extern  led_black
16
17         extern  informative_panic
18
19
20 ;---------------------------------------------------------------------------
21 ; reserved access bank locations
22
23 WREG2           equ     00h     ; a 2nd working reg :-)
24 WREG3           equ     01h     ; a 3rd working reg :-)
25 WREG4           equ     02h     ; a 4th working reg :-)
26 BLANK           equ     03h     ; register full of zeros
27 TESTFLASH       equ     04h     ; test LED flash pattern
28
29
30
31 ;---------------------------------------------------------------------------
32 ; memory location definitions
33
34 ERROR_BUF_PAGE  equ     3       ; error codes on flash p3
35 F_ERROR_U       equ     30h     ; upper part of error memory locations
36 F_SOS_H         equ     00h     ; high (middle) part of SOS error memory loc.
37 F_SOS_L         equ     00h     ; lower part of SOS error memory loc.
38
39
40 ;---------------------------------------------------------------------------
41 ; error messages
42
43 err_SOS equ     0       ; msg 0 = SOS
44
45 ;****************************************************************************
46
47 ;----------------------------------------
48 readout
49 ; Flashes the per-pic led red and black in a specified pattern.
50 ; The pattern is specified as the state for 8 identically-long time
51 ; periods each as long as a morse `dot', encoded into a byte with
52 ; most significant bit first.
53 ;               On entry                On exit
54 ; W             any                     undefined
55 ; WREG2         flash pattern           preserved
56 ; WREG4         any                     undefined
57 ;
58         clrf    WREG4,0         ; clear loop counter (WREG4)
59         rrncf   WREG2,1
60
61 readout_loop
62         movlw   8
63         cpfslt  WREG4,0         ; if loop counter >=8, return
64         return
65
66         rlncf   WREG2,1         ; top bit goes into N flag, ie Negative if 1
67         bn      readout_if_led_on
68 readout_if_led_off
69         call    led_black
70         bra     readout_endif_led
71
72 readout_if_led_on
73         call    led_red
74 readout_endif_led
75         incf    WREG4,1,0       ; increment loop counter
76         call    waiting
77         bra     readout_loop
78
79 ;****************************************************************************
80
81         code
82
83 ;****************************************************************************
84 ; this is what happens when we panic
85
86 informative_panic
87 ; switch off interrupts and power
88 ; reconfigure timer0 for writing diagnostic msg to the LED
89
90         clrf    INTCON,0        ; disable all interrupts EVER
91         bcf     PORTC,1,0       ; switch off booster
92
93         movlw   10101100b
94         movwf   TESTFLASH
95
96
97 ; re-initialise timer0 config
98         bcf     T0CON,6,0       ; p107 Timer0 -> 16bit mode
99         bcf     T0CON,5,0       ; timer0 use internal clock
100         bcf     T0CON,3,0       ; use prescaler
101         bsf     T0CON,2,0       ; }
102         bcf     T0CON,1,0       ; } prescale value 1:32 (13ms x 32)
103         bcf     T0CON,0,0       ; }
104
105         clrf    BLANK,0
106 panic_loop
107 ;       errmsg  err_SOS,0       ; transmit SOS in red
108         movff   TESTFLASH,WREG2
109         rcall   readout
110 ;       readout BLANK,0         ; transmit blank buffer
111         call    led_green
112         call    waiting
113         goto    panic_loop
114
115 ;****************************************************************************
116 ; GENERAL SUBROUTINES
117
118 ;----------------------------------------
119 waiting
120 ; waits for a fixed interval, depending on the configuration of TMR0
121
122         bcf     INTCON,2,0      ; clear timer0 interrupt bit (p109)
123         clrf    TMR0H,0         ; p107 set high byte of timer0 to 0 (buffered,
124                                 ; only actually set when write to tmr0l occurs)
125         clrf    TMR0L,0         ; set timer0 low byte - timer now set to 0000h
126 loop
127         btfss   INTCON,2,0      ; check whether timer0 interrupt has been set -
128                                 ; skip next instruction if so
129         bra     loop
130         return
131
132
133 ;****************************************************************************
134
135         end