-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2c.inc
221 lines (176 loc) · 3.62 KB
/
i2c.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
; --------------------------------------------
; I2C_Init
;
; IN\OUT: None
; --------------------------------------------
I2C_Init:
#ifdef I2C_DEBUG
mprintln "INIT"
#endif
setb PORT_USI, SCL_PIN, r16
setb PORT_USI, SDA_PIN, r16
; clrb PORT_USI, SCL_PIN, r16
; clrb PORT_USI, SDA_PIN, r16
setb DDR_USI, SCL_PIN, r16
setb DDR_USI, SDA_PIN, r16
; clrb DDR_USI, SCL_PIN, r16
; clrb DDR_USI, SDA_PIN, r16
outi USIDR, 0xff
outi USICR, (1<<USIWM1) | (1<<USICS1) | (0<<USICS0) | (1<<USICLK) | (0<<USITC)
outi USISR, (1<<USISIF) | (1<<USIOIF) | (1<<USIPF) | (1<<USIDC) | (0x0<<USICNT0)
ret
; --------------------------------------------
; I2C_Delay
;
; IN\OUT: None
; --------------------------------------------
I2C_Delay:
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
ret
; --------------------------------------------
; I2C_Wait4SCL
;
; IN\OUT: None
; --------------------------------------------
I2C_Wait4SCL:
sbis PORT_USI, SCL_PIN
rjmp I2C_Wait4SCL
ret
; --------------------------------------------
; I2C_Transeive
;
; IN: r16 - USISR
; OUT: r16 - data
; --------------------------------------------
I2C_Transeive:
outu USISR, r16
ldi r17, (1<<USIWM1) | (1<<USICS1) | (0<<USICS0) | (1<<USICLK) | (1<<USITC)
i2c_transeive_loop:
rcall I2C_Delay
outu USICR, r17
rcall I2C_Wait4SCL
rcall I2C_Delay
outu USICR, r17
sbis USISR, USIOIF
rjmp i2c_transeive_loop
rcall I2C_Delay
in r16, USIDR
ldi r17, 0xff
out USIDR, r17
setb DDR_USI, SDA_PIN, r17
ret
; --------------------------------------------
; I2C_Trasmit
;
; IN: r16 - data
; OUT: r16 - 0 OK, 1 Error
; --------------------------------------------
I2C_Trasmit:
#ifdef I2C_DEBUG
push r16
mprintln "Transmit"
pop r16
#endif
clrb PORT_USI, SCL_PIN, r17
outu USIDR, r16
ldi r16, USISR_8bit
rcall I2C_Transeive
clrb DDR_USI, SDA_PIN, r16
#ifdef I2C_DEBUG
mprintln "Reading (N)ACK"
#endif
ldi r16, USISR_1bit
rcall I2C_Transeive
sbrs r16, 0
rjmp i2c_trasmit_ack
i2c_trasmit_nack:
#ifdef I2C_DEBUG
push r16
mprintln "Get NACK"
pop r16
#endif
rjmp i2c_trasmit_exit
i2c_trasmit_ack:
#ifdef I2C_DEBUG
push r16
mprintln "Get ACK"
pop r16
#endif
i2c_trasmit_exit:
andi r16, 0x01
ret
; --------------------------------------------
; I2C_Receive
;
; IN: r16 - 0x0 (ACK) or 0x1 (NACK)
; OUT: r16 - data
; --------------------------------------------
I2C_Receive:
push r16 ; Save ACK/NACK input
#ifdef I2C_DEBUG
push r16
mprintln "Read"
pop r16
#endif
clrb DDR_USI, SDA_PIN, r17
ldi r16, USISR_8bit
rcall I2C_Transeive
pop r18 ; Get ACK/NACK input
push r16 ; Save read data
#ifdef I2C_DEBUG
push r16
mprintln "Sending (N)ACK"
pop r16
#endif
outu USIDR, r18
ldi r16, USISR_1bit
rcall I2C_Transeive
pop r16
ret
; --------------------------------------------
; I2C_SendStart
;
; IN\OUT: None
; --------------------------------------------
I2C_SendStart:
#ifdef I2C_DEBUG
mprintln "START"
#endif
; clrb DDR_USI, SDA_PIN, r16 ; SDA - high ####
setb PORT_USI, SDA_PIN, r16; SDA - high
setb PORT_USI, SCL_PIN, r16 ; SCL - high
; clrb DDR_USI, SCL_PIN, r16
rcall I2C_Wait4SCL
rcall I2C_Delay
clrb PORT_USI, SDA_PIN, r16 ; SDA - low
rcall I2C_Delay
clrb PORT_USI, SCL_PIN, r16 ; SCL - low
setb PORT_USI, SDA_PIN, r16 ; SDA - high
ret
; --------------------------------------------
; I2C_SendStop
;
; IN\OUT: None
; --------------------------------------------
I2C_SendStop:
#ifdef I2C_DEBUG
mprintln "STOP"
#endif
clrb PORT_USI, SDA_PIN, r16 ; SDA - low
setb PORT_USI, SCL_PIN, r16 ; SCL - high
rcall I2C_Wait4SCL
rcall I2C_Delay
setb PORT_USI, SDA_PIN, r16 ; SDA - high
; clrb PORT_USI, SCL_PIN, r16 ; SCL - low
ret