-
Notifications
You must be signed in to change notification settings - Fork 6
/
device_i2c.c
executable file
·265 lines (214 loc) · 4.9 KB
/
device_i2c.c
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include "device_i2c.h"
#include <peripheral/i2c.h>
#include "hardwareprofile.h"
/*
* Initialize i2c module:
* I2C clock is 100KHz
* If mode parameter is SLAVE, uses address to set slave address for the module
* Enable module
*/
void i2c_init(I2C_MODULE i2cnum, i2cmode mode, BYTE address)
{
//enabling i2c module doesnt need changing port
//direction/value etc, and is not a pin muxed peripheral
I2CConfigure ( i2cnum, I2C_ENABLE_SLAVE_CLOCK_STRETCHING);
I2CSetFrequency ( i2cnum, 48000000, 300000);
if(mode == SLAVE)
{
//address mask is set to 0
I2CSetSlaveAddress ( i2cnum, address&0x7f, 0, I2C_USE_7BIT_ADDRESS );
}
I2CEnable(i2cnum, TRUE);
}
/*
* Assert start condition
* if restart, then assert repeat transmission
* (Change direction of data without ending transmission)
* Used by:master
*/
BOOL i2c_begin_transmission(I2C_MODULE i2cnum, BOOL restart)
{
I2C_STATUS status;
// Send the Start (or Restart) signal
if(restart)
{
I2CRepeatStart(i2cnum);
}
else
{
// Wait for the bus to be idle, then start the transfer
while( !I2CBusIsIdle(i2cnum) );
if(I2CStart(i2cnum) != I2C_SUCCESS)
{
return FALSE;
}
}
// Wait for the signal to complete
do
{
status = I2CGetStatus(i2cnum);
} while ( !(status & I2C_START) );
return TRUE;
}
/*
* Assert stop condition
* Used by: master
*/
void i2c_end_transmission(I2C_MODULE i2cnum)
{
I2C_STATUS status;
// Send the Stop signal
I2CStop(i2cnum);
// Wait for the signal to complete
do
{
status = I2CGetStatus(i2cnum);
} while ( !(status & I2C_STOP) );
}
/*
*
* Used by:master
*/
I2C_RESULT i2c_restart_transmission(I2C_MODULE i2cnum)
{
BYTE status;
I2CRepeatStart(i2cnum);
// Wait for the signal to complete
do
{
status = I2CGetStatus(i2cnum);
} while ( !(status & I2C_START) );
return TRUE;
}
/*
* Send byte of data, wait for ack. Transfer must have been started.
* Used by: master,slave
*/
BOOL i2c_write_byte(I2C_MODULE i2cnum, BYTE data)
{
while(!I2CTransmitterIsReady(i2cnum));
if(I2CSendByte(i2cnum, data)!=I2C_SUCCESS)
return FALSE;
while(!I2CTransmissionHasCompleted(i2cnum));
if (I2CByteWasAcknowledged(i2cnum))
{
return TRUE;
}
else
{
return FALSE;
}
}
/*
* A transfer must have been initiated prior to this
* Used by: master, slave
*/
I2C_RESULT i2c_request_data(I2C_MODULE i2cnum)
{
while( I2CReceiverEnable ( i2cnum, TRUE) != I2C_SUCCESS )
{
I2CClearStatus ( i2cnum, I2C_RECEIVER_OVERFLOW);
}
return I2C_SUCCESS;
}
/*
* A transfer must have been initiated
* Used by:master,slave
*/
inline BOOL i2c_is_available(I2C_MODULE i2cnum)
{
return I2CReceivedDataIsAvailable(i2cnum);
}
/*
* Get byte of data from i2c bus
* Used by:master,slave
*/
BYTE i2c_read_byte(I2C_MODULE i2cnum)
{
if(I2CReceiverEnable(i2cnum, TRUE) == I2C_RECEIVE_OVERFLOW)
{
//todo
return 0;
}
else
{
while(!I2CReceivedDataIsAvailable(i2cnum));
return I2CGetByte(i2cnum);
}
}
void i2c_ack(I2C_MODULE i2cnum)
{
I2CAcknowledgeByte(i2cnum, TRUE);
while ( !I2CAcknowledgeHasCompleted(i2cnum) );
}
void i2c_nack(I2C_MODULE i2cnum)
{
I2CAcknowledgeByte(i2cnum, FALSE);
while ( !I2CAcknowledgeHasCompleted(i2cnum) );
}
/* slave clock stretching handled automatically */
/*
Sending Data to slave
---------------------
master sends start sequence
send address of slave with r/w low
send internal register number you master wants to write to
send data
...can send more data here
send stop sequence
*/
/*
Reading data from slave
---------------------
Send start sequence
send address of slave with r/w low
send internal register number you master wants to read from
Send start sequence again OR restart transmission
send address of slave with r/w high
read data byte
send stop sequence
*/
BOOL mpu_i2c_init()
{
//open i2cin master mode
i2c_init(MPU_I2C, MASTER, 0);
}
// TODO timeouts in waiting for bus free
int i2c_read(unsigned char slave_addr, unsigned char reg_addr, unsigned char length, unsigned char *data)
{
BYTE i;
i2c_begin_transmission(MPU_I2C, FALSE);
i2c_send_address(MPU_I2C, slave_addr, 0);
i2c_write_byte(MPU_I2C, reg_addr);
i2c_restart_transmission(MPU_I2C);
i2c_send_address(MPU_I2C, slave_addr, 1);
I2CReceiverEnable ( I2C1, TRUE);
for(i=0;i<length;i++)
{
data[i] = i2c_read_byte(MPU_I2C);
if(i<(length-1)) // nack on last byte
{
i2c_ack(MPU_I2C);
}
else
{
i2c_nack(MPU_I2C);
}
}
i2c_end_transmission(MPU_I2C);
return 0;
}
int i2c_write(unsigned char slave_addr, unsigned char reg_addr, unsigned char length, unsigned char const *data)
{
BYTE i;
i2c_begin_transmission(MPU_I2C, FALSE);
i2c_send_address(MPU_I2C, slave_addr, 0);
i2c_write_byte(MPU_I2C, reg_addr);
// DO NOT restart transmission, or start condition.
for(i=0;i<length;i++)
{
i2c_write_byte(MPU_I2C, data[i]);
}
i2c_end_transmission(MPU_I2C);
return 0;
}