forked from lockfale/Defcon-22-badge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjm_i2c.spin
167 lines (107 loc) · 5.26 KB
/
jm_i2c.spin
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
'' =================================================================================================
''
'' File....... jm_i2c.spin
'' Purpose.... Low-level I2C routines (requires pull-ups on SCL and SDA)
'' Author..... Jon "JonnyMac" McPhalen
'' Copyright (c) 2009-2014 Jon McPhalen
'' -- elements inspired by code from Mike Green
'' E-mail.....
'' Started.... 28 JUL 2009
'' Updated.... 06 APR 2014
''
'' =================================================================================================
' IMPORTANT Note: This code requires pull-ups on the SDA _and_ SCL lines -- it does not drive
' the SCL line high.
con { fixed io pins }
RX1 = 31 ' programming / debug port
TX1 = 30
EE_SDA = 29 ' eeprom
EE_SCL = 28
con
#0, ACK, NAK
var
long scl ' buss pins
long sda
pub setup
'' Setup I2C using Propeller EEPROM pins
setupx(EE_SCL, EE_SDA)
pub setupx(sclpin, sdapin)
'' Define I2C SCL (clock) and SDA (data) pins
longmove(@scl, @sclpin, 2) ' copy pins
dira[scl] := 0 ' float to pull-up
outa[scl] := 0 ' write 0 to output reg
dira[sda] := 0
outa[sda] := 0
repeat 9 ' reset device
dira[scl] := 1
dira[scl] := 0
if (ina[sda])
quit
pub present(ctrl)
'' Pings device, returns true it ACK
start
return (write(ctrl) == ACK)
pub wait(ctrl) | ackbit
'' Waits for I2C device to be ready for new command
repeat
start
ackbit := write(ctrl)
until (ackbit == ACK)
pub start
'' Create I2C start sequence
'' -- will wait if I2C buss SDA pin is held low
dira[sda] := 0 ' float SDA (1)
dira[scl] := 0 ' float SCL (1)
repeat
while (ina[scl] == 0) ' allow "clock stretching"
dira[sda] := 1 ' SDA low (0)
dira[scl] := 1 ' SCL low (0)
pub write(i2cbyte) | ackbit
'' Write byte to I2C buss
'' -- leaves SCL low
i2cbyte := (i2cbyte ^ $FF) << 24 ' move msb (bit7) to bit31
repeat 8 ' output eight bits
dira[sda] := i2cbyte <-= 1 ' send msb first
dira[scl] := 0 ' SCL high (float to p/u)
dira[scl] := 1 ' SCL low
dira[sda] := 0 ' relase SDA to read ack bit
dira[scl] := 0 ' SCL high (float to p/u)
ackbit := ina[sda] ' read ack bit
dira[scl] := 1 ' SCL low
return ackbit
pub read(ackbit) | i2cbyte
'' Read byte from I2C buss
dira[sda] := 0 ' make sda input
repeat 8
dira[scl] := 0 ' SCL high (float to p/u)
i2cbyte := (i2cbyte << 1) | ina[sda] ' read the bit
dira[scl] := 1 ' SCL low
dira[sda] := !ackbit ' output ack bit
dira[scl] := 0 ' clock it
dira[scl] := 1
return (i2cbyte & $FF)
pub stop
'' Create I2C stop sequence
dira[sda] := 1 ' SDA low
dira[scl] := 0 ' float SCL
repeat
until (ina[scl] == 1) ' hold for clock stretch
dira[sda] := 0 ' float SDA
dat
{{
Terms of Use: MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}}