chiark / gitweb /
reserve /^ [^ ;]/ for statements inserted for debugging purposes
[trains.git] / detpic / common.inc
1 ;======================================================================
2 ; common.inc
3 ; common macros & equs etc.
4 ; generally include this at the top of each file.
5
6 ;----------------------------------------------------------------------
7 ; COMMON INCLUDES and BOILERPLATE
8         include         /usr/share/gputils/header/p18f458.inc
9         radix           dec 
10         include         panic.inc
11         include         morse+auto.inc
12         include         ../iwjpictest/insn-aliases.inc
13         include         ../iwjpictest/clockvaries.inc
14         include         variables+vars.inc
15         include         pindata.inc
16         include         program+clocks.inc
17         include         i2clib.incm
18
19 tickdiv equ 16
20 tickdiv_us equ tick_us * tickdiv
21
22 ;----------------------------------------------------------------------
23 ; Common conventions for function register notation:
24
25 ;                       Master                  Slave
26 ; Registers etc.
27 ;   W                   Trashed                 Trashed
28 ;   STATUS              Trashed                 Trashed
29 ;   BSR                 Not used                Not used
30 ;   t,u,v               Low ISR                 Low ISR
31 ;   TBLPTR*,TABLAT      Low ISR                 Low ISR
32 ;   PROD*               Low ISR                 Low ISR
33 ;   FSR0                Low ISR                 Low ISR
34 ;   PCLATU              Always set to 0         Not used
35 ;   PCLATH              Low ISR                 Not used
36 ;   t_dolocal           Low ISR                 High ISR
37 ;   FSR1                Low ISR                 High ISR (detect[1])
38 ;   FSR2                High ISR (nmra[1])      High ISR (detect[1])
39 ;
40 ;   Main loop           detection scan          detection scan
41 ;   High ISR            NMRA output             I2C service
42 ;   Low ISRs            everything else         everything else
43 ;
44 ; Trashed       May be trashed by any routine anywhere.  Saved
45 ;               during every ISR entry/exit.
46 ;
47 ; Low ISR       May be used/trashed by any routine run in low-priority
48 ;               interrupt, or any routine run during initialisation.
49 ;               May therefore not be used in background loop with
50 ;               interrupts enabled.  May not be used by high-priority
51 ;               ISR (unless explicitly saved, eg isrh_fsr0_{save,restore}).
52 ;
53 ; High ISR      May be used/trashed by any routine run in high-priority
54 ;               interrupt, or any routine run during initialisation.
55 ;               May therefore not be used elsewhere with interrupts
56 ;               enabled.
57 ;
58 ;               Only the routines specially noted as intended to
59 ;               be called from the High ISR are safe.
60 ;
61 ; ... (subsystem)
62 ;               Register is reserved for use by this subsystem, which
63 ;               is allowed to expect the value to be preserved.
64 ;               Anything else which uses it must save and restore (and
65 ;               may also need to disable interrupts, depending on its
66 ;               relative status).
67 ;
68 ; Not High      May be used by any routine not running in high-priority
69 ;               interrupt.  Not saved by high-priority interrupt
70 ;               entry/exit, so any high-priority interrupt routine which
71 ;               uses this register must save and restore it.
72 ;
73 ; A routine which is allowed to trash a register may document that it
74 ; saves that register for the benefit of its callers.
75 ;
76 ;  [1]  FSR1 and FSR2 on slave pics are reserved exclusively for the
77 ;       I2C response and detection code (detect.asm), after
78 ;       detect_slave_init.  Likewise FSR2 is reserved exclusively
79 ;       for the NMRA output ISR after nmra_init.
80 ;
81 ; General-purpose hardware allocation:
82 ;
83 ;                       Master                  Slave
84 ;  Timer 0              nmra                    Disabled
85 ;  Timer 2              tick, int. low          -
86 ;  Timer 1              -                       -
87 ;  CCP1                 -                       -
88 ;  Timer 3              point fire timer        point fire timer
89 ;  ECCP                 -                       -
90 ;
91 ;   (...) indicates that this is a projected use, NYI
92
93 ;----------------------------------------------------------------------
94 ; Conventional routine names:
95 ;
96 ; <periph>_local_do     Process a master-to-slave command to activate
97 ;                       a local peripheral, in High ISR.  Also called
98 ;                       on master in Low ISR to activate its own
99 ;                       local peripherals.  NB strange calling convention!
100 ;
101 ; <periph>_local_init   Initialises RAM tables for local peripheral
102 ;                       and arranges for pins to be set in appropriate
103 ;                       quiescent state.  Configures pic built-in
104 ;                       peripherals.
105 ;
106 ; <periph>_local_intrl  Low ISR service routine for peripheral (see below).
107 ;                               
108 ; command_<periph>      Called when an appropriate message has been
109 ;                       received from the host.
110 ;
111 ; <something>_intrl     Low ISR service routine.
112 ;                       Checks for any relevant interrupt.
113 ;                       If not, just returns.
114 ;                       If found, services it and then does either
115 ;                        intrl_handled or intrl_handled_nostack
116 ;                        neither of which return; the latter is
117 ;                        faster but implies a promise 
118 ;
119 ;----------------------------------------------------------------------
120 ; MACROS
121
122 ;----------------------------------------
123 ; For entering and leaving Low ISR, saving and restoring STATUS and W
124 ; See above under <something>_intrl, and {master,slave}_interrupt_low
125
126 enter_interrupt_low macro
127         mov_ff  STATUS, isr_low_save_status
128         mov_wf  isr_low_save_w
129         mov_ff  STKPTR, isr_low_save_stkptr
130         endm
131
132 intrh_fsr0_save macro
133         mov_ff  FSR0L, isr_high_save_fsr0
134         mov_ff  FSR0H, isr_high_save_fsr0+1
135         endm
136
137 intrh_fsr0_restore macro
138         mov_ff  isr_high_save_fsr0,   FSR0L
139         mov_ff  isr_high_save_fsr0+1, FSR0H
140         endm
141
142 intrl_handled_core macro ; for internal use only
143         mov_fw  isr_low_save_w
144         mov_ff  isr_low_save_status, STATUS
145         retfie
146         endm
147
148 intrl_handled_nostack macro
149         pop     ; undo the `call' from the original ISR
150         intrl_handled_core
151         endm
152
153 intrl_handled macro
154         goto    intrl_handled_routine
155         endm
156
157 ;----------------------------------------
158 ; For disabling all interrupts, to make a critical section:
159 ; (for use from main program and Low ISR only)
160 ;
161 ;  GIEH                 modified appropriately
162 ;  everything else      preserved
163
164 intrh_mask macro
165         bc_f    INTCON,GIEH
166         endm
167
168 intrh_unmask macro
169         bs_f    INTCON,GIEH
170         endm
171
172 ;----------------------------------------
173 ; For the fix specified in the silicon errata:
174 ; silicon revision B4 issue 4
175 ;
176 ;                       Before          After
177 ;  TABLAT               any             data from flash
178 ;  TBLPTR*              correct         incremented/decremented
179 ;  everything else      any             preserved
180
181 tblrd_postinc_fixup macro
182         tblrd   *+
183         dw      0xffff
184         endm
185
186 tblrd_postdec_fixup macro
187         tblrd   *-
188         dw      0xffff
189         endm
190
191 ;----------------------------------------
192 ; For setting up TBLPTR
193
194 load_tblptr macro value
195 ;
196 ;                       Before          After
197 ;  TBLPTR*              any             set
198 ;  W, STATUS            any             undefined
199 ;
200         mov_lw  value & 0xff
201         mov_wf  TBLPTRL
202
203         mov_lw  value >> 8
204         mov_wf  TBLPTRH
205
206   if value > 0xffff
207         mov_lw  value >> 16
208         mov_wf  TBLPTRU
209   else
210         clr_f   TBLPTRU
211   endif
212         endm
213
214 load_perpic_tblptr macro flash_map_base, perpic_entry_size
215 ;
216 ;                       Before          After
217 ;  TBLPTR*              any             set
218 ;  W, STATUS, PROD*     any             undefined
219 ;  everything else      any             preserved
220 ;
221         mov_lw  perpic_entry_size
222         mul_wf  picno
223
224         mov_lw  flash_map_base & 0xff
225         add_wfw PRODL
226         mov_wf  TBLPTRL
227
228         mov_lw  flash_map_base >> 8
229         addc_wfw PRODH
230         mov_wf  TBLPTRH
231
232         clr_f   TBLPTRU         ; TBLPTR* -> our point data
233         endm
234
235 ;----------------------------------------------------------------------
236 ; PINSPECS stuff
237 ;
238 ; A PINSPEC is a constant 0x<bit><port> where <port> is a b c d e
239 ; and <port> is 0 1 2 3 4 5 6 7.  Generally p<picno>_<subsystem>_<pin>
240 ; are equ'd for this.
241
242   radix hex
243 p0_cdu_enable           equ     5b
244 p0_rs232_fcin           equ     4b
245 p0_booster_shutdown     equ     2b
246 p0_booster_overload     equ     1b
247 p0_booster_userfault    equ     0b
248 p0_spare2               equ     6d
249 p0_spare1               equ     5d
250 p0_rs232_fcout          equ     5c
251 pall_perpicled          equ     2d
252 pall_pt0reverse         equ     7b
253 p0_spare0               equ     0a
254 p0_booster_dirn         equ     0c
255 p0_booster_pwm          equ     1c
256   radix dec
257
258 ;                       
259 ;  LAT*                 may be subject to read-modify-write, see below
260 ;  TRIS*                may be subject to read-modify-write, see below
261 ;  PORT*                may be read, see below
262 ;  everything else      untouched
263 ;
264 ;                       LAT*<bit>       TRIS*<bit>      PORT*
265 ;  pin_z                untouched       set             untouched
266 ;  pin_h                set             cleared         untouched
267 ;  pin_l                cleared         cleared         untouched
268 ;  pin_nz               untouched       cleared         untouched
269 ;  pin_vh               set             untouched       untouched
270 ;  pin_vl               cleared         untouched       untouched
271 ;  pin_ifh              untouched       untouched       read
272 ;  pin_ifl              untouched       untouched       read
273
274 pin_z   macro   pinspec
275         bs_f    TRISA + (TRISB-TRISA)*((pinspec-0xa) & 15), pinspec >> 4
276         endm
277
278 pin_nz  macro   pinspec
279         bc_f    TRISA + (TRISB-TRISA)*((pinspec-0xa) & 15), pinspec >> 4
280         endm
281
282 pin_znz macro   pinspec
283         btg_f   TRISA + (TRISB-TRISA)*((pinspec-0xa) & 15), pinspec >> 4
284         endm
285
286 pin_vh  macro   pinspec
287         bs_f    LATA + (LATB-LATA)*((pinspec-0xa) & 15), pinspec >> 4
288         endm
289
290 pin_vl  macro   pinspec
291         bc_f    LATA + (LATB-LATA)*((pinspec-0xa) & 15), pinspec >> 4
292         endm
293
294 pin_h   macro   pinspec
295         pin_vh  pinspec
296         pin_nz  pinspec
297         endm
298
299 pin_l   macro   pinspec
300         pin_vl  pinspec
301         pin_nz  pinspec
302         endm
303
304 pin_ifh macro   pinspec
305         bt_f_if1 PORTA + (PORTB-PORTA)*((pinspec-0xa) & 15), pinspec >> 4
306         endm
307
308 pin_ifl macro   pinspec
309         bt_f_if0 PORTA + (PORTB-PORTA)*((pinspec-0xa) & 15), pinspec >> 4
310         endm
311
312 pinlat_ifh macro        pinspec
313         bt_f_if1 LATA + (LATB-LATA)*((pinspec-0xa) & 15), pinspec >> 4
314         endm
315
316 pinlat_ifl      macro   pinspec
317         bt_f_if0 LATA + (LATB-LATA)*((pinspec-0xa) & 15), pinspec >> 4
318         endm
319
320 ;----------------------------------------------------------------------