chiark / gitweb /
wip; before redo i2cs_interrupt
[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 ;============================================================
11 ; COMMON ADMINISTRATIVE ROUTINES
12
13                 udata_acs
14
15 t               res     1
16
17 st              res     1
18 st_subsequent   equ     0
19
20                 code
21
22 ;--------------------
23 i2cs_init
24 ;       W               slave number            undefined
25                 rcall   slave2addr2
26                 mov_wf  SSPADD
27                 clr_f   st
28                 mov_lw  0x1e ; !SSPEN, CKP(release), I2C 7-bit slave S&P
29                 mov_wf  SSPCON1
30                 mov_lw  0x01 ; !GCEN, SEN
31                 mov_wf  SSPCON2
32                 mov_lw  0x8 ; SMP(noslew), !CKE, !BF(empty)
33                 mov_wf  SSPSTAT
34                 bs_f    TRISB, 0
35                 bs_f    TRISB, 1
36                 bc_f    IPR1, SSPIP
37                 bs_f    SSPCON1, SSPEN
38                 bs_f    PIE1, SSPIE
39                 return
40
41 ;--------------------
42 i2cs_interrupt
43                 bt_f_if0 PIR1, SSPIF
44                 return
45
46 ; We have an interrupt.  Firstly, clear the interrupt flag
47 ; so that if something else happens while we faff, the interrupt
48 ; will be regenerated:
49                 bc_f    PIR1, SSPIF
50
51 ; Check that nothing is wrong:
52                 mov_fw  SSPCON1
53                 mov_wf  t
54                 and_lw  0xc0
55                 bra_nz  i2cs_interrupt_wcolsspov_endif
56                 panic   morse_SV
57 i2cs_interrupt_wcolsspov_endif
58
59 ; Find out what's just happened:
60                 mov_ff  SSPSTAT, t
61
62 chkval macro mask, value, label
63                 mov_fw  t
64  if mask ^ 0xff
65                 and_lw  mask
66  endif
67                 xor_lw  value
68                 bra_z   label
69                 endm
70                         ; bits we want to check
71                         ;  80  60  20    10    08    04    02  01
72                         ;  SMP CKE D_A   P     S     R_W   UA  BF
73                         ;  set clr data? stop  start read? clr full?
74
75                 chkval  0xff, 0x90, s_case_writing_stop
76                 chkval  0xff, 0x89, s_case_addr_recv_write
77                 chkval  0xff, 0x8d, s_case_addr_recv_read
78                 chkval  0xff, 0xa9, s_case_write_data_recv
79                 chkval  0xff, 0xac, s_case_read_data_sent
80                 chkval  0xff, 0xa8, s_case_read_data_nack
81                 chkval  0xdb, 0x90, s_case_unknown_stop
82                 chkval  0xdb, 0x88, s_case_unknown_start
83
84                 mov_ff  t, WREG2 ; fixme
85                 panic   morse_SI
86
87 ;----------
88 s_case_unknown_stop
89 s_case_unknown_start
90                 return
91
92 ;----------
93 s_case_got_write_addr
94                 
95
96                 mov_fw  SSPBUF
97                 and_lw  0xfe
98                 bra_nz  nonzero
99
100                 mov_wf  t
101                 
102
103                 mov_fw  SSPSTAT
104                 and_lw  0xe7 ; all except P and S
105
106                 xor_lw  0x80
107                         ; bits which might sensibly be set
108
109 chkval_last equ 0
110 chkval macro value, label
111                 xor_lw  value ^ chkval_last
112                 bra_z   label
113 chkval_last equ value
114                 endm
115                 chkval  0x80 ;     addr  dunno 
116
117                 mov_lw  0x8
118
119
120                 bt_f_if0 DATA_ADDRESS, SSPSTAT
121                 bra     data
122
123 slave2addr
124 ; computes slave address in form suitable for use in i2c controller
125 ; actual i2c slave address is (slave number) + 0b0001000
126 ;       W               slave number            i2c address * 2
127                 add_lw  0b0001000
128                 rlc_w
129                 return
130
131
132
133
134  include i2clib.inc
135  end