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