chiark / gitweb /
undo broken deletion
[trains.git] / cebpic / tblrd_test.asm
1 ; This program copies the values of flash memory bytes 20 0000h and
2 ; 20 0001h into program memory an then transmits them over the
3 ; serial port
4
5
6 ; pin 21 (per-pic-led, RD2/PSP2/C1IN) states: 
7 ;  high H = green, low L = red, float Z = black
8
9
10
11         include         /usr/share/gputils/header/p18f458.inc
12         include         ../iwjpictest/clockvaries.inc
13
14         extern  led_green
15         extern  led_red
16         extern  led_black
17
18
19 ; reserved for NMRA:
20 NMRACTRL        equ     0x4     ; byte 4: state relevant to NMRA control
21 TRANSMITBIT     equ     0x7     ; bit 7: 0/1 bit currently being transmitted
22 NEXTACTION      equ     0x6     ; bit 6: change polarity on next interrupt y/n
23
24 FROMSERIAL      equ     0x1     ; byte 1: from-serial buffer location (in BSR5)
25 TOTRACK         equ     0x2     ; byte 2: to-track buffer location (in BSR5)
26 TOTRACKBIT      equ     0x3     ; byte 3: bit location of pointer within byte
27
28 NMRA_BUFF_PAGE  equ     5
29
30 ; flash locations for PIC-dependent info:
31
32 F_PIC_NO_U      equ     20h     ; flash location of PIC number
33 F_PIC_NO_H      equ     00h
34 F_PIC_NO_L      equ     00h
35
36 F_I2C_CTRL_U    equ     20h     ; flash location of i2c control byte
37 F_I2C_CTRL_H    equ     00h
38 F_I2C_CTRL_L    equ     01h
39
40 ; i2c specifit stuff
41
42 I2C_BUFF_PAGE   equ     4       ; put i2c relevant stuff into buffer page 4
43 PIC_NO          equ     00h     ; pic no goes to 400h
44 I2C_CTRL        equ     01h     ; i2c ctrl bit goes to 401h
45
46
47
48
49         ifdef   SLOW_VERSION
50         messg   "hello this is the slow version"
51         endif
52
53         ifndef  SLOW_VERSION
54         messg   "hello this is the fast version"
55         endif
56
57         ifdef   SLOW_VERSION
58         messg   "with an if"
59         else
60         messg   "and an else"
61         endif
62
63         org     0
64         goto    initialise
65
66 ;****************************************************************************
67
68 ; high priority interrupt 
69
70         org     000008h
71         goto    interrupt_high
72
73 ; low priority interrupt 
74
75         org     000018h
76         goto    interrupt_low
77
78 ;****************************************************************************
79
80         code
81
82 ;****************************************************************************
83
84 macros
85
86 ; macro to call subroutine to transmit over serial port for debugging
87 ; takes 8-bit value, puts in W, invokes debug_serial_transmit
88         
89         ifndef  SLOW_VERSION
90 debug macro debugvalue
91         endm
92         endif
93
94         ifdef   SLOW_VERSION
95 debug macro debugvalue
96         movlw   debugvalue
97         call    debug_serial_transmit
98         endm
99         endif
100
101 debug_serial_transmit
102         movwf   TXREG,0         ; move contents of W (i.e. debugvalue)
103                                 ;       to TXREG for transmission
104 waitfortsr
105         btfss   TXSTA,1,0
106         bra     waitfortsr
107         return
108
109 ;****************************************************************************
110
111 initialise
112
113 ; serial set-up
114
115 ; initial config - TXSTA register p181
116         bcf     TXSTA,6,0       ; p181, set 8-bit mode
117         bsf     TXSTA,5,0       ; transmit enable
118         bcf     TXSTA,4,0       ; asynchronous mode
119         bsc_txsta_brgh          ; set high or low baud rate
120         
121 ; initial config - RCSTA register p182
122         bsf     RCSTA,7,0       ; serial port enable (p182)
123         bcf     RCSTA,6,0       ; 8-bit reception
124         bsf     RCSTA,4,0       ; enable continuous receive
125
126 ; set SPBRG to get correct baud rate
127         movlw_movwf_spbrg
128
129
130 ; interrupt set-up for serial receive
131         bcf     IPR1,5,0        ; set to low-priority interrupt
132
133
134 ;----------------------------------------------------------------------------
135
136 ; copy PIC-dependent info out of flash memory
137
138         movlw   F_PIC_NO_U      ; set table pointer to point to
139         movwf   TBLPTRU         ; PIC number in flash
140         movlw   F_PIC_NO_H
141         movwf   TBLPTRH
142         movlw   F_PIC_NO_L
143         movwf   TBLPTRL
144
145         tblrd   *+              ; read then increment pointer 
146                                 ; (now points to i2c control byte)
147
148         movlb   I2C_BUFF_PAGE   ; ser BSR=i2c BSR (4)
149         movf    TABLAT,0,0      ; move pic number into normal memory
150         movwf   PIC_NO,1
151
152         tblrd   *               ; read i2c 
153
154         movf    TABLAT,0,0      ; move i2c_ctrl byte into normal memory
155         movwf   I2C_CTRL,1
156
157 ; now have: PIC number in 400h, i2c control byte in 401h
158
159
160 ;----------------------------------------------------------------------------
161
162 ; interrupt set-up
163
164 ; globally enable interrupts - p77
165         bsf     RCON,7,0        ; enable priority levels
166         bsf     INTCON,7,0      ; enable high-priority interrupts
167         bsf     INTCON,6,0      ; enable low-priority interrupts
168         bsf     PIE1,5,0        ; enable USART receive interrupt (p85)
169
170
171 ;****************************************************************************
172
173         call    led_green
174 main_loop_led
175         movlb   I2C_BUFF_PAGE   ; ser BSR=i2c BSR (4)
176
177         movlw   '|'
178         movwf   TXREG,0
179         call    waitfortsr
180
181         movf    PIC_NO,0,1
182         movwf   TXREG,0
183         call    waitfortsr
184
185         movf    I2C_CTRL,0,1
186         movwf   TXREG,0
187         call    waitfortsr
188
189         goto    main_loop_led
190
191
192 ;****************************************************************************
193
194 interrupt_high
195         debug   'H'     
196         goto    panic
197
198
199 ;****************************************************************************
200
201 interrupt_low
202
203 ; check which interrupt.
204         debug   'L'     
205         goto    panic
206 ; serial only?
207
208
209 ;****************************************************************************
210
211 serial_receive
212         debug   'S'     
213         goto    panic
214
215 ;****************************************************************************
216
217
218 panic
219         debug   'x'
220         clrf    INTCON,0        ; disable all interrupts EVER
221         debug   'y'
222         bcf     PORTC,1,0       ; switch off booster
223         debug   'z'
224         call    led_red
225 panic_loop
226         goto    panic_loop
227         
228 ;****************************************************************************
229
230         end