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