forked from maccasoft/propeller-graphics-card
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2c.inc
129 lines (110 loc) · 4.39 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
eeprom_read
// Select the device & send address
call #i2c_start
mov i2c_data, i2c_addr
shr i2c_data, #15
and i2c_data, #$02
or i2c_data, #EEPROM_ADDR | I2C_WRITE
call #i2c_write
mov i2c_data, i2c_addr
shr i2c_data, #8
call #i2c_write
mov i2c_data, i2c_addr
call #i2c_write
// Reselect the device for reading
call #i2c_start
mov i2c_data, i2c_addr
shr i2c_data, #15
and i2c_data, #$02
or i2c_data, #EEPROM_ADDR | I2C_READ
call #i2c_write
// Read i2c_data
_l4r add i2c_addr, #1
cmp ccnt, #1 wz
if_z jmp #_l5r
test i2c_addr, block_boundary wz
if_z jmp #_l5r
mov ack, #I2C_ACK
call #i2c_read
wrbyte i2c_data, i2c_hub_addr
add i2c_hub_addr, #1
sub ccnt, #1
jmp #_l4r
_l5r mov ack, #I2C_NAK
call #i2c_read
wrbyte i2c_data, i2c_hub_addr
add i2c_hub_addr, #1
call #i2c_stop
djnz ccnt, #eeprom_read
eeprom_read_ret ret
i2c_start
or OUTA, i2c_scl
or DIRA, i2c_scl
nop
or OUTA, i2c_sda
or DIRA, i2c_sda
nop
nop
andn OUTA, i2c_sda
nop
andn OUTA, i2c_scl
nop
i2c_start_ret ret
// SDA goes LOW to HIGH with SCL High
i2c_stop
andn OUTA, i2c_scl
nop
andn OUTA, i2c_sda
nop
nop
andn DIRA, i2c_scl
nop
andn DIRA, i2c_sda
nop
i2c_stop_ret ret
i2c_write
mov mask, #$80
_l2 test i2c_data, mask wz
if_nz or OUTA, i2c_sda
if_z andn OUTA, i2c_sda
or OUTA, i2c_scl // Toggle SCL from LOW to HIGH to LOW
nop
nop
andn OUTA, i2c_scl
shr mask, #1 wz
if_nz jmp #_l2
andn DIRA, i2c_sda
nop
nop
or OUTA, i2c_scl
test i2c_sda, INA wc
rcl ack, #1
andn OUTA, i2c_scl
or OUTA, i2c_sda
or DIRA, i2c_sda
i2c_write_ret ret
i2c_read
mov i2c_data, #0
andn DIRA, i2c_sda
mov mask, #8
_l3
nop
nop
or OUTA, i2c_scl
test i2c_sda, INA wc
rcl i2c_data, #1
andn OUTA, i2c_scl
djnz mask, #_l3
cmp ack, #I2C_NAK wz
if_z or OUTA, i2c_sda
if_nz andn OUTA, i2c_sda
or DIRA, i2c_sda
or OUTA, i2c_scl
nop
nop
andn OUTA, i2c_scl
andn OUTA, i2c_sda
i2c_read_ret ret
i2c_addr long 0
i2c_hub_addr long 0
i2c_data long 0