chiark / gitweb /
undo broken deletion
[trains.git] / cebpic / 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 clock equ mclock
14         include ../iwjpictest/clockvaries.inc
15
16         extern  led_green
17         extern  led_red
18         extern  led_black
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 ; VECTORS: special locations, where the PIC starts executing
47 ; after interrupts
48
49
50         org     0
51         goto    vector_reset
52
53
54 ; high priority interrupt
55 ;
56 ;        org     000008h
57 ;        goto    interrupt_high
58
59 ; low priority interrupt
60
61         org     000018h
62         goto    interrupt_low
63 ;
64 ;****************************************************************************
65 ; MACROS
66
67 ;----------------------------------------
68 ; errmsg(ERRCODE,COLOUR)
69 ;       reads the chosen error msg out of flash and transmits by
70 ;       flashing LED in chosen colour [1 = blue (=green), 0 = orange (=red)]
71 ;
72 ;
73 ;errmsg macro ERRCODE, COLOUR
74 ;
75 ;
76 ;
77 ;
78 ;
79 ;
80 ;       endm
81
82 ;----------------------------------------
83 readout
84 ; Flashes the per-pic led red and black in a specified pattern.
85 ; The pattern is specified as the state for 8 identically-long time
86 ; periods each as long as a morse `dot', encoded into a byte with
87 ; most significant bit first.
88 ;               On entry                On exit
89 ; W             any                     undefined
90 ; WREG2         flash pattern           preserved
91 ; WREG4         any                     undefined
92 ;
93         clrf    WREG4,0         ; clear loop counter (WREG4)
94         rrncf   WREG2,1
95
96 readout_loop
97         movlw   8
98         cpfslt  WREG4,0         ; if loop counter >=8, return
99         return
100
101         rlncf   WREG2,1         ; top bit goes into N flag, ie Negative if 1
102         bn      readout_if_led_on
103 readout_if_led_off
104         call    led_black
105         bra     readout_endif_led
106
107 readout_if_led_on
108         call    led_red
109 readout_endif_led
110         incf    WREG4,1,0       ; increment loop counter
111         call    waiting
112         bra     readout_loop
113
114 ;****************************************************************************
115
116         code
117
118 ;****************************************************************************
119
120 vector_reset
121
122 ; serial set-up
123 ; enable interrupts so that this can be used as a trigger for the
124 ; panic routine
125
126 ; initial config - TXSTA register p181
127         bcf     TXSTA,6,0       ; p181, set 8-bit mode
128         bsf     TXSTA,5,0       ; transmit enable
129         bcf     TXSTA,4,0       ; asynchronous mode
130         bsc_txsta_brgh          ; set high or low baud rate
131
132 ; initial config - RCSTA register p182
133         bsf     RCSTA,7,0       ; serial port enable (p182)
134         bcf     RCSTA,6,0       ; 8-bit reception
135         bsf     RCSTA,4,0       ; enable continuous receive
136
137 ; set SPBRG to get correct baud rate
138         movlw_movwf_spbrg
139
140 ; interrupt set-up for serial receive
141         bcf     IPR1,5,0        ; set to low-priority interrupt
142
143 ;---------------------------------------------------------------------------
144 ; interrupt set-up
145
146 ; globally enable interrupts - p77
147         bsf     RCON,7,0        ; enable priority levels
148         bsf     INTCON,7,0      ; enable high-priority interrupts
149         bsf     INTCON,6,0      ; enable low-priority interrupts
150         bsf     PIE1,5,0        ; enable USART receive interrupt (p85)
151
152 ;---------------------------------------------------------------------------
153
154 ; write error code for SOS into flash memory (starting at 30 0000h)
155 ;        movlw   F_ERROR_H      ; set table pointer to point to
156 ;        movwf   TBLPTRU                ; start of flash p3
157 ;        movlw   F_SOS_H
158 ;        movwf   TBLPTRH
159 ;        movlw   F_SOS_L
160 ;        movwf   TBLPTRL
161 ;
162 ; write message into memory, incrementing tbl pointer each time
163 ;
164 ;       movlw   10101000b
165 ;       movwf   TABLAT
166 ;       tblwt*+
167 ;
168 ;       movlw   11101110b
169 ;       movwf   TABLAT
170 ;       tblwt*+
171 ;
172 ;       movlw   11100010b
173 ;       movwf   TABLAT
174 ;       tblwt*+
175 ;
176 ;       movlw   10100000b
177 ;       movwf   TABLAT
178 ;       tblwt*+
179
180 ;---------------------------------------------------------------------------
181 ; turn LED green if we have made it this far....
182
183         call    led_green
184 main_loop_led
185         goto    main_loop_led
186
187
188 ;****************************************************************************
189 ; INTERRUPT SUBROUTINES
190
191 interrupt_low
192         goto    informative_panic
193
194 informative_panic
195 ; switch off interrupts and power
196 ; reconfigure timer0 for writing diagnostic msg to the LED
197
198         clrf    INTCON,0        ; disable all interrupts EVER
199         bcf     PORTC,1,0       ; switch off booster
200
201         movlw   10101100b
202         movwf   TESTFLASH
203
204
205 ; re-initialise timer0 config
206         morse_t0setup mclock, (1<<TMR0ON), -1, -1
207
208         clrf    BLANK,0
209 panic_loop
210 ;       errmsg  err_SOS,0       ; transmit SOS in red
211         movff   TESTFLASH,WREG2
212         rcall   readout
213 ;       readout BLANK,0         ; transmit blank buffer
214         call    led_green
215         call    waiting
216         goto    panic_loop
217
218 ;****************************************************************************
219 ; GENERAL SUBROUTINES
220
221 ;----------------------------------------
222 waiting
223 ; waits for a fixed interval, depending on the configuration of TMR0
224
225         bcf     INTCON,2,0      ; clear timer0 interrupt bit (p109)
226         clrf    TMR0H,0         ; p107 set high byte of timer0 to 0 (buffered,
227                                 ; only actually set when write to tmr0l occurs)
228         clrf    TMR0L,0         ; set timer0 low byte - timer now set to 0000h
229 loop
230         btfss   INTCON,2,0      ; check whether timer0 interrupt has been set -
231                                 ; skip next instruction if so
232         bra     loop
233         return
234
235
236 ;****************************************************************************
237
238         end