chiark / gitweb /
new low-priority interrupt arrangements
[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 clock equ 0
11  include ../iwjpictest/clockvaries.inc
12  include panic.inc
13  include morse+auto.inc
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
61 sspcon1         res     1
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                          slave
71 st_starting     equ 7 ; Writing-Setup?,Reading-Busy?
72 st_addressing   equ 6 ; Writing-Setup?,Reading-Busy?
73 st_writing      equ 5 ; Writing-*                       [Idle-going-]Receiving
74 st_subsequent   equ 4 ; Writing?                        Receiving
75 st_reading      equ 3 ; Reading-*                       Transmit-*
76 st_awaiting     equ 2 ; Reading-Wait                    Transmit-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 i2cpanic macro morse_addr
85 ; Like panic but turns off the I2C controller
86         bc_f    SSPCON1, SSPEN
87         panic   morse_addr
88         endm
89
90 ;----------
91 slave2addr
92 ; computes slave address in form suitable for use in i2c controller
93 ; actual i2c slave address is (slave number) + 0b0001000
94 ;       W               slave number            i2c address * 2
95                 add_lw  b'0001000'
96                 rlc_w
97                 return
98
99 ;----------
100 improper_read_done_data
101                 i2cpanic morse_SD
102
103 ;======================================================================
104 ; MASTER
105
106 ;----------
107 i2cm_init
108                 mov_lw  i2c_sspadd
109                 mov_wf  SSPADD
110                 clr_f   st
111                 clr_f   slave_next
112                 mov_lw  0x08    ; !SSPEN, Master mode
113                 mov_wf  SSPCON1
114                 clr_f   SSPCON2 ; nothing going
115                 mov_lw  0x80    ; SMP(noslew), !CKE(!smbus)
116                 mov_wf  SSPSTAT
117                 bc_f    IPR1, SSPIP ; low priority
118                 bra     init_enable
119
120 ;----------
121 i2cm_interrupt
122                 bt_f_if0 PIR1, SSPIF
123                 return
124                 ; We have an interrupt:
125 ;...
126 i2cm_interrupt_definite
127                 mov_ff  SSPSTAT, sspstat
128                 mov_ff  SSPCON1, sspcon1
129                 mov_ff  SSPCON2, sspcon2
130
131                 bc_f    PIR1, SSPIF
132
133                 mov_lw  (1<<WCOL) | (1<<SSPOV)
134                 and_wfw sspcon1
135                 bra_nz  m_event_bad
136
137                 ; No ?  Well, then the I2C should be idle now:
138                 mov_fw  sspcon2
139                 and_lw  ~((1<<ACKSTAT) | (1<<ACKDT)) ; those two are ok if set
140                 bra_nz  m_event_bad
141                 ; OK...
142
143                 bt_f_if1 sspstat, R_W
144                 bra_nz  m_event_bad
145
146                 bt_f_if1 st, st_stopping
147                 bra     m_event_done_stopping
148
149                 bt_f_if1 st, st_starting
150                 bra     m_event_done_starting
151                 ; not just done SEN
152
153                 bt_f_if1 st, st_addressing
154                 bra     m_event_done_addressing
155
156                 bt_f_if1 st, st_writing
157                 bra     m_event_done_writing
158
159                 bt_f_if1 st, st_acking
160                 bra     m_event_done_acking
161
162                 bt_f_if1 st, st_reading
163                 bra     m_event_done_reading
164
165 m_event_bad
166                 i2cpanic morse_SM
167
168 ;========================================
169 ; MASTER - STARTING, ADDRESSING, STOPPING
170
171 ;----------
172 m_start
173 ;       st                      checked for busyness    correct
174 ;       st_reading/writing      set                     unchanged
175 ;       st_starting             clear                   set
176 ;       W                       slave number            any
177 ;       slave                   any                     slave_number
178 ; expects to return directly to main program (caller)
179                 mov_wf  slave
180                 bra_z   m_improper_slave
181                 bs_f    SSPCON2, SEN
182 m_start_or_restart
183                 and_lw  ~31
184                 bra_nz  m_improper_slave
185                 bs_f    st, st_starting
186                 return
187
188 ;----------
189 m_event_done_starting
190                 mov_fw  slave
191                 rcall   slave2addr
192
193                 bt_f_if1 st, st_reading
194                 bs_w    0       ; address bottom bit means read
195
196                 mov_wf  SSPBUF
197                 bc_f    st, st_starting
198                 bs_f    st, st_addressing
199                 return
200
201 ;----------
202 m_event_done_addressing
203                 bt_f_if1 sspcon2, ACKSTAT
204                 bra     m_bad_address_ack
205                 ; OK, we got ack.
206
207                 bc_f    st, st_addressing
208                 bt_f_if1 st, st_reading
209                 bra     m_event_done_addressing_read
210                 bra     m_event_done_addressing_write
211
212 ;----------
213 m_stop
214 ;       st_stopping                     clear           set
215 ;       st_reading/acking/writing       any             unchanged
216 ; expects to return directly to main program or to end interrupt handler
217                 bs_f    st, st_stopping
218                 bs_f    SSPCON2, PEN
219                 return
220
221 ;----------
222 m_event_done_stopping
223                 clr_f   st
224                 goto    i2cmu_done
225
226 ;----------
227 m_bad_address_ack
228                 i2cpanic morse_SK
229
230 ;----------
231 m_improper_slave
232 ;       slave                   slave number
233                 i2cpanic morse_SN
234
235 ;========================================
236 ; MASTER - WRITING
237
238 ;----------
239 i2cm_write_start
240 ;                               At call         On return
241 ;   State                       Idle            Writing-Setup
242 ;   W                           slave number    any
243                 tst_f_ifnz st
244                 bra     m_improper_write_start
245
246                 bs_f    st, st_writing
247                 bra     m_start
248
249 ;----------
250 m_event_done_writing
251                 ; Did slave ack our byte ?  It had better have done !
252                 bt_f_if1 sspcon2, ACKSTAT
253                 bra     m_event_bad
254
255                 bs_f    st, st_subsequent
256 ;...
257
258 m_event_done_addressing_write
259 ;       ACKSTAT         checked
260 ;       st_addressing   cleared
261                 call    i2cmu_write_next_byte
262                 bra_z   m_event_write_mustfinish
263                 ; OK, we have the next byte:
264
265                 mov_wf  SSPBUF
266                 return
267
268 ;----------
269 m_event_write_mustfinish
270                 bt_f_if0 st, st_subsequent
271                 bra     m_improper_write_finish
272
273                 bra     m_stop
274
275 ;----------
276 m_improper_write_start
277                 i2cpanic morse_SW
278
279 ;----------
280 m_improper_write_finish
281                 i2cpanic morse_SF
282
283 ;========================================
284 ; MASTER - READING
285
286 ;----------
287 i2cm_read_start
288 ;                               At call         On return
289 ;       State                   Idle            Reading-Busy
290 ;       W                       slave number    any
291                 tst_f_ifnz st
292                 bra     m_read_start_busy
293
294                 bs_f    st, st_reading
295                 bra     m_start
296
297 ;----------
298 m_read_start_busy
299                 bt_f_if1 st, st_awaiting
300                 bra     m_read_different
301                 i2cpanic morse_SR
302
303 ;----------
304 m_read_different
305 ; Main program would like to address another slave.
306                 mov_wf  slave_next
307                 bra_z   m_improper_slave
308                 bra     i2cm_read_done
309
310 ;----------
311 m_event_done_addressing_read
312 m_event_done_acking_readmore
313 ;       ACKSTAT                 checked
314 ;       st_addressing/acking    cleared
315                 bs_f    SSPCON2, RCEN
316                 return
317
318 ;----------
319 m_event_done_reading
320                 bt_f_if0 sspstat, BF
321                 bra     m_event_bad
322
323                 mov_fw  SSPBUF
324
325                 bs_f    st, st_awaiting
326                 goto    i2cmu_read_got_byte
327
328 ;----------
329 i2cm_read_another
330 ;   State                       Reading-Wait    Reading-Busy
331                 bt_f_if0 st, st_awaiting
332                 bra     m_improper_read_another
333                 ; OK, we're fine to read another:
334 ;...
335
336 m_read_ack
337 ;       st_reading              1 iff not done          unchanged
338 ;       st_awaiting             still set               cleared
339 ;       st_acking               clear                   set
340 ; expects to return directly to main program or to end interrupt handler
341                 bc_f    st, st_awaiting
342                 bs_f    st, st_acking
343                 bc_f    SSPCON2, ACKDT ; ACKDT=0 means to acknowledge
344                 bt_f_if0 st, st_reading
345                 bs_f    SSPCON2, ACKDT ; don't ack last byte
346                 bs_f    SSPCON2, ACKEN
347                 return
348
349 ;----------
350 i2cm_read_done
351 ;   State                       Reading-Wait    Stopping
352                 bc_f    st, st_reading
353                 
354                 bt_f_if0 st, st_awaiting
355                 bra     improper_read_done_data
356                 ; OK:
357
358                 bra     m_read_ack
359
360 ;----------
361 m_event_done_acking
362                 bc_f    st, st_acking
363
364                 bt_f_if1 st, st_reading
365                 bra     m_event_done_acking_readmore
366
367                 mov_fw  slave_next
368                 bra_z   m_stop
369 ; ok, we want to read another:
370                 mov_wf  slave
371                 clr_f   slave_next
372                 bs_f    st, st_reading
373                 bs_f    SSPCON2, RSEN
374                 bra     m_start_or_restart
375
376 ;----------
377 m_improper_read_another
378                 i2cpanic morse_SA
379
380 ;======================================================================
381 ; SLAVE
382
383 ;----------
384 i2cs_init
385 ;       W               slave number            undefined
386                 rcall   slave2addr
387                 mov_wf  SSPADD
388                 clr_f   st
389                 mov_lw  0x16 ; !SSPEN, CKP(release), I2C 7-bit slave no-SP-int
390                 mov_wf  SSPCON1
391                 mov_lw  0x01 ; !GCEN, SEN
392                 mov_wf  SSPCON2
393                 mov_lw  0x80 ; SMP(noslew), !CKE(!smbus)
394                 mov_wf  SSPSTAT
395                 bs_f    IPR1, SSPIP ; high priority
396 init_enable
397 ; Actually engages the I2C controller, which must already have
398 ; been set up (all but SSPEN):
399 ;  SSPADD,SSPCON1,SSPCON2       configured correctly    unchanged
400 ;       SSPSTAT                 configured correctly    unchanged, except:
401 ;       SSPSTAT<SSPEN>          0 (disabled)            1 (enabled)
402 ;       SSPIE                   0 (disabled)            1 (enabled)
403 ;       SSPIF                   configured correctly    unchanged
404 ;       TRISB<1,0>              any                     configured for I2C
405 ;       SSPIP                   any                     configured correctly
406 ;       GIEL                    0 (disabled)            0 (disabled)
407 ;       ssp* shadows            any                     all bits set
408                 set_f   sspstat
409                 set_f   sspcon1
410                 set_f   sspcon2
411                 set_f   st_orig
412                 bs_f    TRISB, 0
413                 bs_f    TRISB, 1
414                 bs_f    SSPCON1, SSPEN
415                 bs_f    PIE1, SSPIE
416                 return
417
418 ;========================================
419 ; SLAVE - INTERRUPT HANDLING
420
421 ; In general, we figure out our state and then see what kind of events
422 ; we were expecting.  Bits we want to check:
423 ;       80    60    20    10            08    04    02    01
424 ;       SMP   CKE   D_A   P             S     R_W   UA    BF
425 ;       set   clr   data? stop          start read? clr   full?
426 ; (we don't usually mention SMP, CKE and UA below)
427
428 ; Labels of the form s_event_* are branches of the interrupt
429 ; handler and are supposed to finish with return.
430
431 ; Some macros:
432
433 chkvals_start macro what
434                 mov_fw  what
435                 endm
436
437 chkval macro lastval, value, label
438                 xor_lw  value ^ lastval
439                 bra_z   label
440                 endm
441
442 chkvals_addrrecv macro lastval
443         chkval  lastval, 0x8c, s_event_idle_addrrecvread ; A,!P, S,R,!BF
444         chkval  0x8c,    0x89, s_event_idle_addrrecvwrite ; A,!P, S,W,BF
445         endm
446 chkvals_addrrecv_lastval equ 0x89
447
448 ;----------
449 i2cs_interrupt  ; 4cy interrupt latency + 3cy until branch to here
450                 bt_f_if0 PIR1, SSPIF
451                 bra     s_event_bad_intr
452                 ; We have an interrupt:
453
454 ; Firstly, clear the interrupt flag so that if something else happens
455 ; while we faff, the interrupt will be regenerated:
456                 bc_f    PIR1, SSPIF
457
458                 mov_ff  st, st_orig
459
460                 mov_lw  (1<<WCOL) | (1<<SSPOV)
461                 and_wfw SSPCON1
462                 bra_nz  s_event_bad
463
464                 ; 8cy from entry to here, so total of 15cy
465                 bt_f_if1 st, st_reading
466                 bra     s_event_reading ; 18cy to 1st insn of event_reading
467
468                 bt_f_if1 st, st_writing
469                 bra     s_event_writing
470
471 s_event_idle
472                 chkvals_start SSPSTAT
473                 chkvals_addrrecv 0 ; 23cy to 1st insn of addrrecvread
474 s_event_bad
475                 i2cpanic morse_SS ; slave, interrupt, controller in bad state
476
477 s_event_bad_intr
478                 i2cpanic morse_IH ; unknown high-priority interrupt
479
480 ;========================================
481 ; SLAVE - READING
482
483 ;----------
484 s_event_idle_addrrecvread
485                 bs_f    st, st_awaiting
486                 goto    i2csu_read_begin ; 26cy until 1st insn of read_begin
487
488 ;----------
489 s_event_reading
490                 bs_f    st, st_awaiting ; (probably)
491
492                 mov_fw  SSPSTAT
493                 xor_lw  0xac ; D,!P, S,R,!BF
494                 bra_nz  s_event_reading_not_another
495                 goto    i2csu_read_another
496                                 ; 24cy until 1st insn of i2csu_read_another
497
498 ;----------
499 s_event_reading_datanack
500                 return
501
502 ;----------
503 s_event_reading_not_another
504                 ; Whatever is happening, we're done reading now !
505                 clr_f   st
506                 call    i2csu_read_done
507
508                 chkvals_start SSPSTAT
509                 chkval  0, 0xa8, s_event_reading_datanack ; D,!P, S,!R,!BF
510                 ; Or, maybe it was nack and then we were reselected:
511                 chkvals_addrrecv 0xa8
512
513                 bra     s_event_bad
514
515 ;----------
516 i2cs_read_data
517 ;       W               byte for master         any
518 ;       State           Transmit-Wait           Transmit-Busy
519                 mov_wf  SSPBUF
520                 bs_f    SSPCON1, CKP
521                 
522                 bt_f_if0 st, st_awaiting
523                 bra     improper_read_done_data
524                 bc_f    st, st_awaiting
525                 bs_f    st, st_reading
526                 return
527
528 ;========================================
529 ; SLAVE - WRITING
530
531 ;----------
532 s_event_idle_addrrecvwrite
533                 bs_f    SSPCON1, 3 ; we'll need the Stop interrupt
534                 bs_f    st, st_writing
535                 ; well, this is all fine so far, so do carry on:
536
537 s_write_slurpbyte
538 ;       W               any                     byte from master
539 ;       i2c controller  waiting due to SEN etc  continuing with next byte
540                 mov_fw  SSPBUF
541                 bs_f    SSPCON1, CKP
542                 return
543
544 ;----------
545 s_event_writing
546                 chkvals_start SSPSTAT
547                 chkval  0, 0xa9, s_event_writing_datarecv ; D,!P, S,W,BF
548
549                 ; Well, we're done writing now in any case
550                 clr_f   st
551                 bc_f    SSPCON1, 3 ; no Start and Stop interrupts any more
552                 call    i2csu_write_done
553
554                 ; Who knows what might have happened.  We may have
555                 ; missed a number of S and P due to delay between
556                 ; clearing SSPIF and SSPM3(s&p-intrs) so we can't be
557                 ; too picky.
558
559                 ; First, the nice cases:
560                 chkvals_start SSPSTAT
561                 chkvals_addrrecv 0
562
563                 ; Then random junk:
564                 mov_fw  SSPSTAT
565                 and_lw  0xc7 ; ?D_A, ?P; ?S
566                 xor_lw  0x80 ; SMP, !CKE, !R_W, !UA, !BF
567                 bra_nz  s_event_bad
568                 return
569
570 ;----------
571 s_event_writing_datarecv
572                 rcall   s_write_slurpbyte
573
574                 bt_f_if1 st, st_subsequent
575                 goto    i2csu_write_another
576
577                 bs_f    st, st_subsequent
578                 goto    i2csu_write_begin
579
580
581 ;======================================================================
582
583  include panic.fin
584  include i2clib.inc
585
586  end