-
Notifications
You must be signed in to change notification settings - Fork 1
/
I2C0.cpp
executable file
·207 lines (170 loc) · 5.74 KB
/
I2C0.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
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA *
* *
***************************************************************************
* *
* (c) Copyright, 1997-2012, ANSR *
* *
***************************************************************************
* *
* Filename: I2C0.cpp *
* *
***************************************************************************/
#include "armlib.h"
/// Reserve memory for singleton object.
static I2C0 i2c0SingletonObject;
/**
* Get a pointer to the I2C port 0 object.
*/
I2C0 *I2C0::GetInstance()
{
return &i2c0SingletonObject;
}
/**
* Enable the required pins for the I2C port and configure it for operation.
*/
void I2C0::Enable()
{
uint32_t clock;
// Set the pins required for the I2C controller.
PINSEL0 |= ((0x01 << 4) | (0x01 << 6));
I2C0CONCLR = I2CxCONCLR_AA | I2CxCONCLR_SI | I2CxCONCLR_STAC | I2CxCONCLR_I2ENC;
// Set the I2C clock rate to 250KHz.
clock = SystemControl::GetInstance()->GetPClock() / 500000;
I2C0SCLH = clock;
I2C0SCLL = clock;
// Enable the I2C engine.
I2C0CONSET = I2CxCONSET_I2EN;
}
/**
* Send the start command sequence and slave address.
*
* @param data slave address
*
* @return true if slave ACKed request; otherwise false
*/
bool_t I2C0::Start(uint32_t data)
{
uint32_t value, retryCount;
retryCount = 0;
for (;;)
{
// Clear the interrupt status.
I2C0CONCLR = I2CxCONCLR_SI;
// Send the start command.
I2C0CONSET = I2CxCONSET_STA;
if (WaitInterrupt())
return false;
// Write the slave address.
I2C0DAT = data;
// Clear the start flag and interrupt.
I2C0CONCLR = I2CxCONSET_STA | I2CxCONCLR_SI;
// Wait for the interrupt.
if (WaitInterrupt())
return false;
value = I2C0STAT;
// Check the start status.
if (value != ((data & 0x01) ? 0x40 : 0x18))
{
Stop();
if (++retryCount == 5)
return false;
} else
return true;
} // END for
}
/**
* Read a one or more bytes from the slave device.
*
* @param data pointer to buffer to hold count bytes of data
* @param count number of bytes to read
*/
void I2C0::Read (uint8_t *data, uint32_t count)
{
I2C0CONCLR = I2CxCONCLR_SI;
I2C0CONSET = I2CxCONSET_AA;
if (count == 1)
{
I2C0CONCLR = I2CxCONCLR_AA | I2CxCONCLR_SI;
if (WaitInterrupt())
return;
*data = I2C0DAT;
return;
} // END if
for(;;)
{
// Wait for end data from slave.
if (WaitInterrupt())
return;
*data++ = I2C0DAT;
count--;
switch (count)
{
case 0x00:
return;
// After next will NO ASK
case 0x01:
I2C0CONCLR = I2CxCONCLR_AA | I2CxCONCLR_SI;
break;
default:
I2C0CONCLR = I2CxCONCLR_SI;
I2C0CONSET = I2CxCONSET_AA;
break;
} // END switch
} // END for
}
/**
* Write one or more bytes to the slave device.
*
* @param data pointer to buffer of data to write
* @param count number of bytes to send
*/
void I2C0::Write (const uint8_t *data, uint32_t count)
{
while (count != 0)
{
I2C0DAT = *data++;
I2C0CONCLR = I2CxCONCLR_SI;
if (WaitInterrupt())
return;
--count;
} // END while
}
/**
* Send the STOP signal to the slave device.
*/
void I2C0::Stop()
{
I2C0CONSET = I2CxCONSET_STO;
I2C0CONCLR = I2CxCONCLR_SI;
}
/**
* Wait for the interrupt flag to get set or the time out period to expire.
*
* @return true if interrupt not detected and wait timed out; otherwise false
*/
bool_t I2C0::WaitInterrupt()
{
uint32_t startTime;
startTime = SystemControl::GetTick() + 50;
while (!(I2C0CONSET & I2CxCONSET_SI))
if (SystemControl::GetTick() > startTime)
{
Stop();
return true;
} // END if
return false;
}