-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathI2c.cpp
221 lines (194 loc) · 6.12 KB
/
I2c.cpp
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
#include "I2c.hpp"
#include "Osc.hpp"
enum :uint32_t {
I2CX_SPACING = 0x40, //spacing in words
I2CXCON_BASE = 0xBF805000, // address of I2C3, but others follow in memory
ON = 15,
SIDL = 13,
SCLREL = 12,
STRICT = 11,
A10M = 10,
DISSLW = 9,
SMEN = 8,
GCEN = 7,
STREN = 6,
ACKDT = 5,
ACKEN = 4,
RCEN = 3,
PEN = 2,
RSEN = 1,
SEN = 0,
I2CXSTAT = 4, //offset from I2xCON in words
I2CXADDR = 8,
I2CXMSK = 12,
I2CXBRG = 16,
I2CXTRN = 20,
I2CXRCV = 24,
TPGD_SHIFT = 23, // T Pulse Globber Delay 0.000000104 approximated by shift 23
};
//=============================================================================
I2c::
I2c (I2CX e)
: m_i2cx_con((volatile uint32_t*)I2CXCON_BASE + (e * I2CX_SPACING)),
m_speed(KHZ100)
{
}
//I2C1CON
//=============================================================================
auto I2c::
on (bool tf) -> void
{
//always set brg in case clock changed
//or speed not changed since init
speed(m_speed);
setbit(m_i2cx_con, 1<<ON, tf);
}
//=============================================================================
auto I2c::
stop_idle (bool tf) -> void
{
setbit(m_i2cx_con, 1<<SIDL, tf);
}
//=============================================================================
auto I2c::
clk_release (bool tf) -> void
{
setbit(m_i2cx_con, 1<<SCLREL, tf);
}
//=============================================================================
auto I2c::
strict (bool tf) -> void
{
setbit(m_i2cx_con, 1<<STRICT, tf);
}
//=============================================================================
auto I2c::
addr_10bit (bool tf) -> void
{
setbit(m_i2cx_con, 1<<A10M, tf);
}
//=============================================================================
auto I2c::
slew_rate (bool tf) -> void
{
setbit(m_i2cx_con, 1<<DISSLW, not tf);
}
//=============================================================================
auto I2c::
smb_levels (bool tf) -> void
{
setbit(m_i2cx_con, 1<<SMEN, tf);
}
//=============================================================================
auto I2c::
irq_gencall (bool tf) -> void
{
setbit(m_i2cx_con, 1<<GCEN, tf);
}
//=============================================================================
auto I2c::
clk_stretch (bool tf) -> void
{
setbit(m_i2cx_con, 1<<STREN, tf);
}
//=============================================================================
auto I2c::
ack (bool tf) -> void
{
setbit(m_i2cx_con, 1<<ACKDT, not tf); //0=ACK,1=NACK
setbit(m_i2cx_con, 1<<ACKEN);
}
//=============================================================================
auto I2c::
rx (bool tf) -> void
{
setbit(m_i2cx_con, 1<<RCEN, tf);
}
//=============================================================================
auto I2c::
stop (bool tf) -> void
{
setbit(m_i2cx_con, 1<<PEN, tf);
}
//=============================================================================
auto I2c::
repstart (bool tf) -> void
{
setbit(m_i2cx_con, 1<<RSEN, tf);
}
//=============================================================================
auto I2c::
start (bool tf) -> void
{
setbit(m_i2cx_con, 1<<SEN, tf);
}
//I2CXSTAT
//=============================================================================
auto I2c::
stat (STAT e) -> bool
{
return anybit(m_i2cx_con + I2CXSTAT, e);
}
//=============================================================================
auto I2c::
buscol_clr () -> void
{
clrbit(m_i2cx_con + I2CXSTAT, BUSCOL);
}
//=============================================================================
auto I2c::
txcol_clr () -> void
{
clrbit(m_i2cx_con + I2CXSTAT, TXCOL);
}
//=============================================================================
auto I2c::
rxoflow_clr () -> void
{
clrbit(m_i2cx_con + I2CXSTAT, RXOFLOW);
}
//I2CXADDR
//=============================================================================
auto I2c::
addr (uint16_t v) -> void
{
val(m_i2cx_con + I2CXADDR, v);
}
//I2CXMSK
//=============================================================================
auto I2c::
addr_mask (uint16_t v) -> void
{
val(m_i2cx_con + I2CXMSK, v);
}
//I2CXBRG
//=============================================================================
auto I2c::
speed (I2CSPEED e) -> void
{
uint32_t sck = e * 2;
uint32_t clk = Osc::pbclk();
brg((clk / sck) - (clk >> TPGD_SHIFT) - 2);
m_speed = e;
}
//=============================================================================
auto I2c::
brg (uint16_t v) -> void
{
if(v < 2) v = 2; //0,1 not allowed
val(m_i2cx_con + I2CXBRG, v);
}
//I2CXTRN
//=============================================================================
auto I2c::
write (uint8_t v) -> void
{
val(m_i2cx_con + I2CXTRN, v);
}
//I2CXRCV
//=============================================================================
auto I2c::
read () -> uint8_t
{
return val8(m_i2cx_con + I2CXRCV);
}