chiark / gitweb /
new make-externs automatically generates " extern whatever" from "whatever @"...
[trains.git] / detpic / i2clib.asm
1 ;######################################################################
2 ; i2clib.inc - I2C LIBRARY - IMPLEMENTATION
3 ;
4 ; See i2clib.asm for documentation of the interface to this file.
5
6   include /usr/share/gputils/header/p18f458.inc
7   radix dec
8   include ../iwjpictest/insn-aliases.inc
9
10   include ../iwjpictest/clockvaries.inc
11   include panic.inc
12   include morse+auto.inc
13   include i2clib.incm
14
15 ;======================================================================
16 ; NOTATION
17
18 ; Naming conventions
19 ;
20 ;  m_...                        routines used by master only
21 ;  s_...                        routines used by slave only
22 ;  <any other name>             routines used by both
23 ;
24 ;  [ms]_event_...               event handler, branched to from interrupt
25 ;                                handler; conditions are as in the name;
26 ;                                should `return' at end which will return
27 ;                                from i2c[ms]_interrupt
28 ;
29 ;  [sm]_event_bad[_...]         event handler which panics; called when i2c
30 ;                                controller did something unexpected
31 ;
32 ;  m_improper_...               panics; called when main program
33 ;                                does something wrong
34 ;
35 ;  [ms]_<anything else>         routines or labels of some other kind
36
37 ; Whenever flow does not pass past the end of some code, we
38 ; have a boundary `;----------', and when flow passes past
39 ; an important label we sometimes mark it specially with `;...',
40 ; like this:
41 ;
42 ;               ;----------
43 ;               m_event_spong
44 ;                               bt_f_if0 st, st_something
45 ;                               bra     m_event_bad
46 ;               ;...
47 ;
48 ;               m_event_several_including_spong
49 ;                               bs_f    st, st_sponging
50 ;                               bra     metasyntacticing
51 ;
52 ;               ;----------
53 ;               m_event_wombat
54
55 ;============================================================
56 ; COMMON ADMINISTRATIVE ROUTINES and VARIABLES
57
58                 udata_acs
59
60 sspstat         res     1       ; master only
61 sspcon1         res     1       ; master only
62 sspcon2         res     1       ; master only
63 slave           res     1       ; master only
64 slave_next      res     1       ; master only
65
66 st              res     1
67 st_orig         res     1
68
69 ; st is a bitmask, bit set in visible states:
70                   ;    master
71 st_starting     equ 7 ; Writing-Setup?, Reading-Busy?
72 st_addressing   equ 6 ; Writing-Setup?, Reading-Busy?
73 st_writing      equ 5 ; Writing-*, Stopping(after Reading-Wait:write_start)
74 st_subsequent   equ 4 ; Writing?
75 st_reading      equ 3 ; Reading-*
76 st_awaiting     equ 2 ; Reading-Wait
77 st_acking       equ 1 ; Reading-Busy?, Stopping(from read)
78 st_stopping     equ 0 ; Stopping
79                   ; ...? means not always set in that state
80
81                 code
82
83 ;----------
84 slave2addr
85 ; computes slave address in form suitable for use in i2c controller
86 ; actual i2c slave address is (slave number) + 0b0001000
87 ;       W               slave number            i2c address * 2
88                 add_lw  b'0001000'
89                 rlc_w
90                 return
91
92 ;----------
93 improper_read_done_data
94                 i2cpanic morse_SD
95
96 ;======================================================================
97 ; MASTER
98
99 ;----------
100 i2cm_init
101                 mov_lw  i2c_sspadd
102                 mov_wf  SSPADD
103                 clr_f   st
104                 clr_f   slave_next
105                 mov_lw  0x08    ; !SSPEN, Master mode
106                 mov_wf  SSPCON1
107                 clr_f   SSPCON2 ; nothing going
108                 mov_lw  0x80    ; SMP(noslew), !CKE(!smbus)
109                 mov_wf  SSPSTAT
110                 bc_f    IPR1, SSPIP ; low priority
111                 bra     init_enable
112
113 ;----------
114 i2cm_interrupt
115                 bt_f_if0 PIR1, SSPIF
116                 return
117                 ; We have an interrupt:
118 ;...
119 i2cm_interrupt_definite
120                 mov_ff  SSPSTAT, sspstat
121                 mov_ff  SSPCON1, sspcon1
122                 mov_ff  SSPCON2, sspcon2
123
124                 bc_f    PIR1, SSPIF
125
126                 mov_lw  (1<<WCOL) | (1<<SSPOV)
127                 and_wfw sspcon1
128                 bra_nz  m_event_bad
129
130                 ; No ?  Well, then the I2C should be idle now:
131                 mov_fw  sspcon2
132                 and_lw  ~((1<<ACKSTAT) | (1<<ACKDT)) ; those two are ok if set
133                 bra_nz  m_event_bad
134                 ; OK...
135
136                 bt_f_if1 sspstat, R_W
137                 bra_nz  m_event_bad
138
139                 bt_f_if1 st, st_stopping
140                 bra     m_event_done_stopping
141
142                 bt_f_if1 st, st_starting
143                 bra     m_event_done_starting
144                 ; not just done SEN
145
146                 bt_f_if1 st, st_addressing
147                 bra     m_event_done_addressing
148
149                 bt_f_if1 st, st_acking
150                 bra     m_event_done_acking
151
152                 bt_f_if1 st, st_writing
153                 bra     m_event_done_writing
154
155                 bt_f_if1 st, st_reading
156                 bra     m_event_done_reading
157
158 m_event_bad
159                 i2cpanic morse_SM
160
161 ;========================================
162 ; MASTER - STARTING, ADDRESSING, STOPPING
163
164 ;----------
165 m_start
166 ;       st                      checked for busyness    correct
167 ;       st_reading/writing      one set, one clear      unchanged
168 ;       st_starting             clear                   set
169 ;       W                       slave number            any
170 ;       slave                   any                     slave_number
171 ; expects to return directly to main program (caller)
172                 mov_wf  slave
173                 bra_z   m_improper_slave
174                 bs_f    SSPCON2, SEN
175 m_start_or_restart
176                 and_lw  ~31
177                 bra_nz  m_improper_slave
178                 bs_f    st, st_starting
179                 return
180
181 ;----------
182 m_event_done_starting
183                 mov_fw  slave
184                 rcall   slave2addr
185
186                 bt_f_if1 st, st_reading
187                 bs_w    0       ; address bottom bit means read
188
189                 mov_wf  SSPBUF
190                 bc_f    st, st_starting
191                 bs_f    st, st_addressing
192                 return
193
194 ;----------
195 m_event_done_addressing
196                 bt_f_if1 sspcon2, ACKSTAT
197                 bra     m_bad_address_ack
198                 ; OK, we got ack.
199
200                 bc_f    st, st_addressing
201                 bt_f_if1 st, st_reading
202                 bra     m_event_done_addressing_read
203                 bra     m_event_done_addressing_write
204
205 ;----------
206 m_stop
207 ;       st_stopping                     clear           set
208 ;       st_reading/acking/writing       any             unchanged
209 ; expects to return directly to main program or to end interrupt handler
210                 bs_f    st, st_stopping
211                 bs_f    SSPCON2, PEN
212                 return
213
214 ;----------
215 m_event_done_stopping
216                 clr_f   st
217                 goto    i2cmu_done
218
219 ;----------
220 m_bad_address_ack
221                 i2cpanic morse_SK
222
223 ;----------
224 m_improper_slave
225 ;       slave                   slave number
226                 i2cpanic morse_SN
227
228 ;========================================
229 ; MASTER - WRITING
230
231 ;----------
232 i2cm_write_start
233 ;                               At call         On return
234 ;   State                   Idle/Reading-Wait   Writing-Setup
235 ;   W                           slave number    any
236                 tst_f_ifnz st
237                 bra     m_write_start_busy
238
239                 bs_f    st, st_writing
240                 bra     m_start
241
242 ;----------
243 m_event_done_writing
244                 ; Did slave ack our byte ?  It had better have done !
245                 bt_f_if1 sspcon2, ACKSTAT
246                 bra     m_event_bad
247
248                 bs_f    st, st_subsequent
249 ;...
250
251 m_event_done_addressing_write
252 ;       ACKSTAT         checked
253 ;       st_addressing   cleared
254                 call    i2cmu_write_next_byte
255                 bra_z   m_event_write_mustfinish
256                 ; OK, we have the next byte:
257
258                 mov_wf  SSPBUF
259                 return
260
261 ;----------
262 m_event_write_mustfinish
263                 bt_f_if0 st, st_subsequent
264                 bra     m_improper_write_finish
265
266                 bra     m_stop
267
268 ;----------
269 m_improper_write_finish
270                 i2cpanic morse_SF
271
272 ;========================================
273 ; MASTER - READING
274
275 ;----------
276 i2cm_read_start
277 ;                               At call         On return
278 ;       State                   Idle            Reading-Busy
279 ;       W                       slave number    any
280                 tst_f_ifnz st
281                 bra     m_read_start_busy
282
283                 bs_f    st, st_reading
284                 bra     m_start
285
286 ;----------
287 m_write_start_busy
288                 bs_f    st, st_writing
289 m_read_start_busy
290                 bt_f_if1 st, st_awaiting
291                 bra     m_address_different
292                 i2cpanic morse_SB
293
294 ;----------
295 m_address_different
296 ; Main program would like to address another slave for reading.
297                 mov_wf  slave_next
298                 bra_z   m_improper_slave
299                 bra     i2cm_read_done
300
301 ;----------
302 m_event_done_addressing_read
303 m_event_done_acking_readmore
304 ;       ACKSTAT                 checked
305 ;       st_addressing/acking    cleared
306                 bs_f    SSPCON2, RCEN
307                 return
308
309 ;----------
310 m_event_done_reading
311                 bt_f_if0 sspstat, BF
312                 bra     m_event_bad
313
314                 mov_fw  SSPBUF
315
316                 bs_f    st, st_awaiting
317                 goto    i2cmu_read_got_byte
318
319 ;----------
320 i2cm_read_another
321 ;   State                       Reading-Wait    Reading-Busy
322                 bt_f_if0 st, st_awaiting
323                 bra     m_improper_read_another
324                 ; OK, we're fine to read another:
325 ;...
326
327 m_read_ack
328 ;       st_reading              1 iff not done          unchanged
329 ;       st_awaiting             still set               cleared
330 ;       st_acking               clear                   set
331 ; expects to return directly to main program or to end interrupt handler
332                 bc_f    st, st_awaiting
333                 bs_f    st, st_acking
334                 bc_f    SSPCON2, ACKDT ; ACKDT=0 means to acknowledge
335                 bt_f_if0 st, st_reading
336                 bs_f    SSPCON2, ACKDT ; don't ack last byte
337                 bs_f    SSPCON2, ACKEN
338                 return
339
340 ;----------
341 i2cm_read_done
342 ;   State                       Reading-Wait    Stopping
343                 bc_f    st, st_reading
344                 
345                 bt_f_if0 st, st_awaiting
346                 bra     improper_read_done_data
347                 ; OK:
348
349                 bra     m_read_ack
350
351 ;----------
352 m_event_done_acking
353                 bc_f    st, st_acking
354
355                 bt_f_if1 st, st_reading
356                 bra     m_event_done_acking_readmore
357
358                 mov_fw  slave_next
359                 bra_z   m_stop
360 ; ok, we want to read another:
361                 mov_wf  slave
362                 clr_f   slave_next
363                 bt_f_if0 st, st_writing ; because of i2cm_write_start ?
364                 bs_f    st, st_reading ; no, then we will want to read
365                 bs_f    SSPCON2, RSEN
366                 bra     m_start_or_restart
367
368 ;----------
369 m_improper_read_another
370                 i2cpanic morse_SA
371
372 ;======================================================================
373 ; SLAVE
374
375 ;----------
376 i2cs_init
377 ;       W               slave number            undefined
378                 rcall   slave2addr
379                 mov_wf  SSPADD
380                 clr_f   st
381                 mov_lw  0x16 ; !SSPEN, CKP(release), I2C 7-bit slave no-SP-int
382                 mov_wf  SSPCON1
383                 mov_lw  0x01 ; !GCEN, SEN
384                 mov_wf  SSPCON2
385                 mov_lw  0x80 ; SMP(noslew), !CKE(!smbus)
386                 mov_wf  SSPSTAT
387                 bs_f    IPR1, SSPIP ; high priority
388 init_enable
389 ; Actually engages the I2C controller, which must already have
390 ; been set up (all but SSPEN):
391 ;  SSPADD,SSPCON1,SSPCON2       configured correctly    unchanged
392 ;       SSPSTAT                 configured correctly    unchanged, except:
393 ;       SSPSTAT<SSPEN>          0 (disabled)            1 (enabled)
394 ;       SSPIE                   0 (disabled)            1 (enabled)
395 ;       SSPIF                   configured correctly    unchanged
396 ;       TRISB<1,0>              any                     configured for I2C
397 ;       SSPIP                   any                     configured correctly
398 ;       GIEL                    0 (disabled)            0 (disabled)
399 ;       ssp* shadows            any                     all bits set
400                 set_f   sspstat
401                 set_f   sspcon1
402                 set_f   sspcon2
403                 set_f   st_orig
404                 bs_f    TRISC, 3
405                 bs_f    TRISC, 4
406                 bs_f    SSPCON1, SSPEN
407                 bs_f    PIE1, SSPIE
408                 return
409
410 ;========================================
411 ; SLAVE
412 ;
413 ; In general, we figure out our state and then see what kind of events
414 ; we were expecting.  Bits we want to check:
415 ;       80    40    20    10            08    04    02    01
416 ;       SMP   CKE   D_A   P             S     R_W   UA    BF
417 ;       set   clr   data? stop          start read? clr   full?
418 ; (we don't usually mention SMP, CKE and UA below)
419 ;
420 ; Labels of the form s_event_* are branches of the interrupt
421 ; handler and are supposed to finish with return.
422
423 ;----------
424 ; Macros: chkvals_start and chkval
425
426 chkvals_start macro what
427                 mov_fw  what
428                 endm
429
430 chkval macro lastval, value, label
431                 xor_lw  value ^ lastval
432                 bra_z   label
433                 endm
434
435 near_i2csu code
436
437 ;----------
438 s_write_slurpbyte macro
439 ;       W               any                     byte from master
440 ;       i2c controller  waiting due to SEN etc  continuing with next byte
441                 mov_fw  SSPBUF
442                 bs_f    SSPCON1, CKP
443                 endm
444
445 ;----------------------------------------
446 i2cs_read_data
447                 i2cs_read_data_macro
448                 return
449
450 ;----------------------------------------
451 ; branches from the ISR
452
453 ;----------
454 s_event_addrrecvwrite
455                 s_write_slurpbyte
456                 goto    i2csu_write_begin
457
458 ;----------
459 s_event_reading_datanack
460                 return
461
462 ;----------
463 s_event_writing_datarecv
464                 s_write_slurpbyte
465                 goto    i2csu_write_data
466
467 ;----------
468 s_event_bad_intr
469                 i2cpanic morse_IH ; unknown high-priority interrupt
470
471 ;----------------------------------------
472 i2cs_interrupt  ; 4cy interrupt latency + 3cy until branch to here
473                 bt_f_if0 PIR1, SSPIF
474                 bra     s_event_bad_intr
475                 ; We have an interrupt:
476
477                 mov_lw  (1<<WCOL) | (1<<SSPOV)
478                 and_wfw SSPCON1
479                 bra_nz  s_event_bad
480
481 ; Firstly, clear the interrupt flag so that if something else happens
482 ; while we faff, the interrupt will be regenerated:
483                 bc_f    PIR1, SSPIF
484
485                 chkvals_start SSPSTAT
486                 chkval  0,   0x8c, i2csu_read_begin             ;A,!P, S,R,!BF
487                 chkval  0x8c,0xac, i2csu_read_another           ;D,!P, S,R,!BF
488                 chkval  0xac,0x89, s_event_addrrecvwrite        ;A,!P, S,W,BF
489                 chkval  0x89,0xa9, s_event_writing_datarecv     ;D,!P, S,W,BF
490                 chkval  0xa9,0xa8, s_event_reading_datanack     ;D,!P, S,!R,!BF
491 s_event_bad
492                 i2cpanic morse_SS
493
494 ;======================================================================
495
496   include program+externs.fin
497   include i2clib.inc
498
499   end