t res 1
-st res 1
-st_subsequent equ 0
+st res 1 ; bitmask:
+st_writing equ 0
code
; Find out what's just happened:
mov_ff SSPSTAT, t
+ ; bits we want to check
+ ; 80 60 20 10 08 04 02 01
+ ; SMP CKE D_A P S R_W UA BF
+ ; set clr data? stop start read? clr full?
+
+ bt_f_if1 t, I2C_START
+ bra si_if_start
+
+si_if_notstart
+ ; So it should be stop
+ mov_fw t
+ and_lw 0xdf ; ?D_A
+ xor_lw 0x90 ; SMP, !CKE, P; !S, !R_W, !UA, !BF
+ bra_nz si_if_bad
+
+ ; Were we receiving ?
+ bt_f_if0 st, st_writing
+ return
+
+ ; Yes, we were receiving:
+ bc_f st, st_writing
+ goto i2csu_write_done
+ ; tail call; we couldn't do anything after that
+ ; anyway since it might well reenter us.
+
+si_if_start
+ bt_f_if1 t, BF
+ bra si_if_bufferfull
+si_if_bufferempty
+ bt_f_if1 t, R_W ;read?
+ bra si_if_bufferempty_reading
+si_if_bufferempty_notreading
+ ; So we think this is just a START (which we want to ignore)
+ mov_fw t
+ and_lw 0xdf ; ?D_A
+ xor_lw 0x88 ; SMP, !CKE, !P; S, !R_W, !UA, !BF
+ bra_nz si_if_bad
+
+ ; OK, ignore it
+ return
+
+
+
chkval macro mask, value, label
mov_fw t