chiark / gitweb /
ebbf62ddbe946edf1689328637bb1bcfc1c0c5b8
[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                 bs_f    SSPCON2, SEN
174 m_start_or_restart
175                 and_lw  ~31
176                 bra_nz  m_improper_slave
177                 bs_f    st, st_starting
178                 tst_f_ifnz slave
179                 return
180                 ; oops:
181 ;...
182
183 m_improper_slave
184 ;       slave                   slave number
185                 i2cpanic morse_SN
186
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 ; MASTER - WRITING
232
233 ;----------
234 i2cm_write_start
235 ;                               At call         On return
236 ;   State                   Idle/Reading-Wait   Writing-Setup
237 ;   W                           slave number    any
238                 tst_f_ifnz st
239                 bra     m_write_start_busy
240
241                 bs_f    st, st_writing
242                 bra     m_start
243
244 ;----------
245 m_event_done_writing
246                 ; Did slave ack our byte ?  It had better have done !
247                 bt_f_if1 sspcon2, ACKSTAT
248                 bra     m_event_bad
249
250                 bs_f    st, st_subsequent
251 ;...
252
253 m_event_done_addressing_write
254 ;       ACKSTAT         checked
255 ;       st_addressing   cleared
256                 call    i2cmu_write_next_byte
257                 bra_z   m_event_write_mustfinish
258                 ; OK, we have the next byte:
259
260                 mov_wf  SSPBUF
261                 return
262
263 ;----------
264 m_event_write_mustfinish
265                 bt_f_if0 st, st_subsequent
266                 bra     m_improper_write_finish
267
268                 bra     m_stop
269
270 ;----------
271 m_improper_write_finish
272                 i2cpanic morse_SF
273
274 ;========================================
275 ; MASTER - READING
276
277 ;----------
278 i2cm_read_start
279 ;                               At call         On return
280 ;       State                   Idle            Reading-Busy
281 ;       W                       slave number    any
282                 tst_f_ifnz st
283                 bra     m_read_start_busy
284
285                 bs_f    st, st_reading
286                 bra     m_start
287
288 ;----------
289 m_write_start_busy
290                 bs_f    st, st_writing
291 m_read_start_busy
292                 bt_f_if1 st, st_awaiting
293                 bra     m_address_different
294                 i2cpanic morse_SB
295
296 ;----------
297 m_address_different
298 ; Main program would like to address another slave for reading.
299                 mov_wf  slave_next
300                 tst_f_ifnz slave_next
301                 bra     i2cm_read_done
302                 panic   morse_SO                
303
304 ;----------
305 m_event_done_addressing_read
306 m_event_done_acking_readmore
307 ;       ACKSTAT                 checked
308 ;       st_addressing/acking    cleared
309                 bs_f    SSPCON2, RCEN
310                 return
311
312 ;----------
313 m_event_done_reading
314                 bt_f_if0 sspstat, BF
315                 bra     m_event_bad
316
317                 mov_fw  SSPBUF
318
319                 bs_f    st, st_awaiting
320                 goto    i2cmu_read_got_byte
321
322 ;----------
323 i2cm_read_another
324 ;   State                       Reading-Wait    Reading-Busy
325                 bt_f_if0 st, st_awaiting
326                 bra     m_improper_read_another
327                 ; OK, we're fine to read another:
328 ;...
329
330 m_read_ack
331 ;       st_reading              1 iff not done          unchanged
332 ;       st_awaiting             still set               cleared
333 ;       st_acking               clear                   set
334 ; expects to return directly to main program or to end interrupt handler
335                 bc_f    st, st_awaiting
336                 bs_f    st, st_acking
337                 bc_f    SSPCON2, ACKDT ; ACKDT=0 means to acknowledge
338                 bt_f_if0 st, st_reading
339                 bs_f    SSPCON2, ACKDT ; don't ack last byte
340                 bs_f    SSPCON2, ACKEN
341                 return
342
343 ;----------
344 i2cm_read_done
345 ;   State                       Reading-Wait    Stopping
346                 bc_f    st, st_reading
347                 
348                 bt_f_if0 st, st_awaiting
349                 bra     improper_read_done_data
350                 ; OK:
351
352                 bra     m_read_ack
353
354 ;----------
355 m_event_done_acking
356                 bc_f    st, st_acking
357
358                 bt_f_if1 st, st_reading
359                 bra     m_event_done_acking_readmore
360
361                 mov_fw  slave_next
362                 bra_z   m_stop
363 ; ok, we want to read another:
364                 mov_wf  slave
365                 clr_f   slave_next
366                 bt_f_if0 st, st_writing ; because of i2cm_write_start ?
367                 bs_f    st, st_reading ; no, then we will want to read
368                 bs_f    SSPCON2, RSEN
369                 bra     m_start_or_restart
370
371 ;----------
372 m_improper_read_another
373                 i2cpanic morse_SA
374
375 ;======================================================================
376 ; SLAVE
377
378 ;----------
379 i2cs_init
380 ;       W               slave number            undefined
381                 rcall   slave2addr
382                 mov_wf  SSPADD
383                 clr_f   st
384                 mov_lw  0x16 ; !SSPEN, CKP(release), I2C 7-bit slave no-SP-int
385                 mov_wf  SSPCON1
386                 mov_lw  0x01 ; !GCEN, SEN
387                 mov_wf  SSPCON2
388                 mov_lw  0x80 ; SMP(noslew), !CKE(!smbus)
389                 mov_wf  SSPSTAT
390                 bs_f    IPR1, SSPIP ; high priority
391 init_enable
392 ; Actually engages the I2C controller, which must already have
393 ; been set up (all but SSPEN):
394 ;  SSPADD,SSPCON1,SSPCON2       configured correctly    unchanged
395 ;       SSPSTAT                 configured correctly    unchanged, except:
396 ;       SSPSTAT<SSPEN>          0 (disabled)            1 (enabled)
397 ;       SSPIE                   0 (disabled)            1 (enabled)
398 ;       SSPIF                   configured correctly    unchanged
399 ;       TRISB<1,0>              any                     configured for I2C
400 ;       SSPIP                   any                     configured correctly
401 ;       GIEL                    0 (disabled)            0 (disabled)
402 ;       ssp* shadows            any                     all bits set
403                 set_f   sspstat
404                 set_f   sspcon1
405                 set_f   sspcon2
406                 set_f   st_orig
407                 bs_f    TRISC, 3
408                 bs_f    TRISC, 4
409                 bs_f    SSPCON1, SSPEN
410                 bs_f    PIE1, SSPIE
411                 return
412
413 ;========================================
414 ; SLAVE
415 ;
416 ; In general, we figure out our state and then see what kind of events
417 ; we were expecting.  Bits we want to check:
418 ;       80    40    20    10            08    04    02    01
419 ;       SMP   CKE   D_A   P             S     R_W   UA    BF
420 ;       set   clr   data? stop          start read? clr   full?
421 ; (we don't usually mention SMP, CKE and UA below)
422 ;
423 ; Labels of the form s_event_* are branches of the interrupt
424 ; handler and are supposed to finish with return.
425
426 ;----------
427 ; Macros: chkvals_start and chkval
428
429 chkvals_start macro chvals_what
430                 mov_fw  chvals_what
431                 endm
432
433 chkval macro chkval_lastval, chkval_value, chkval_label
434                 xor_lw  chkval_value ^ chkval_lastval
435                 bra_z   chkval_label
436                 endm
437
438 near_i2csu code
439
440 ;----------
441 s_write_slurpbyte macro
442 ;       W               any                     byte from master
443 ;       i2c controller  waiting due to SEN etc  continuing with next byte
444                 mov_fw  SSPBUF
445                 bs_f    SSPCON1, CKP
446                 endm
447
448 ;----------------------------------------
449 i2cs_read_data
450                 i2cs_read_data_macro
451                 return
452
453 ;----------------------------------------
454 ; branches from the ISR
455
456 ;----------
457 s_event_addrrecvwrite
458                 s_write_slurpbyte
459                 goto    i2csu_write_begin
460
461 ;----------
462 s_event_reading_datanack
463                 return
464
465 ;----------
466 s_event_writing_datarecv
467                 s_write_slurpbyte
468                 goto    i2csu_write_data
469
470 ;----------
471 s_event_bad_intr
472                 i2cpanic morse_IH ; unknown high-priority interrupt
473
474 ;----------------------------------------
475 i2cs_interrupt  ; 4cy interrupt latency + 3cy until branch to here
476                 bt_f_if0 PIR1, SSPIF
477                 bra     s_event_bad_intr
478                 ; We have an interrupt:
479
480                 mov_lw  (1<<WCOL) | (1<<SSPOV)
481                 and_wfw SSPCON1
482                 bra_nz  s_event_bad
483
484 ; Firstly, clear the interrupt flag so that if something else happens
485 ; while we faff, the interrupt will be regenerated:
486                 bc_f    PIR1, SSPIF
487
488                 chkvals_start SSPSTAT
489                 chkval  0,   0x8c, i2csu_read_begin             ;A,!P, S,R,!BF
490                 chkval  0x8c,0xac, i2csu_read_another           ;D,!P, S,R,!BF
491                 chkval  0xac,0x89, s_event_addrrecvwrite        ;A,!P, S,W,BF
492                 chkval  0x89,0xa9, s_event_writing_datarecv     ;D,!P, S,W,BF
493                 chkval  0xa9,0xa8, s_event_reading_datanack     ;D,!P, S,!R,!BF
494 s_event_bad
495                 i2cpanic morse_SS
496
497 ;======================================================================
498
499   include program+externs.fin
500   include i2clib.inc
501
502   end