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