-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi2c_jk.c
More file actions
251 lines (213 loc) · 7.31 KB
/
i2c_jk.c
File metadata and controls
251 lines (213 loc) · 7.31 KB
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
#include <xc.h>
#include <stdint.h>
#include <stdbool.h>
#include <peripheral/i2c.h>
#include "i2c_jk.h"
/*******************************************************************************
Function:
bool StartTransfer( bool restart )
Summary:
Starts (or restarts) a transfer to/from the CENTIPEDE SHIELD.
Description:
This routine starts (or restarts) a transfer to/from the CENTIPEDE, waiting (in
a blocking loop) until the start (or re-start) condition has completed.
Precondition:
The I2C module must have been initialized.
Parameters:
restart - If false, send a "Start" condition
- If true, send a "Restart" condition
Returns:
true - If successful
false - If a collision occured during Start signaling
Example:
<code>
StartTransfer(false);
</code>
Remarks:
This is a blocking routine that waits for the bus to be idle and the Start
(or Restart) signal to complete.
*****************************************************************************/
bool StartTransfer(I2C_MODULE i2cModule, bool restart) {
I2C_STATUS status;
// Send the Start (or Restart) signal
if (restart) {
I2CRepeatStart(i2cModule);
} else {
// Wait for the bus to be idle, then start the transfer
while (!I2CBusIsIdle(i2cModule));
if (I2CStart(i2cModule) != I2C_SUCCESS) {
return false;
}
}
// Wait for the signal to complete
do {
status = I2CGetStatus(i2cModule);
} while (!(status & I2C_START));
return true;
}
/*******************************************************************************
Function:
bool TransmitOneByte( uint8_t data )
Summary:
This transmits one byte to the CENTIPEDE SHIELD.
Description:
This transmits one byte to the CENTIPEDE, and reports errors for any bus
collisions.
Precondition:
The transfer must have been previously started.
Parameters:
data - Data byte to transmit
Returns:
true - Data was sent successfully
false - A bus collision occured
Example:
<code>
TransmitOneByte(0xAA);
</code>
Remarks:
This is a blocking routine that waits for the transmission to complete.
*****************************************************************************/
bool TransmitOneByte(I2C_MODULE i2cModule, uint8_t data) {
// Wait for the transmitter to be ready
while (!I2CTransmitterIsReady(i2cModule));
// Transmit the byte
if (I2CSendByte(i2cModule, data) == I2C_MASTER_BUS_COLLISION) {
return false;
}
// Wait for the transmission to finish
while (!I2CTransmissionHasCompleted(i2cModule));
return true;
}
/*******************************************************************************
Function:
void StopTransfer( void )
Summary:
Stops a transfer to/from the CENTIPEDE SHIELD.
Description:
This routine Stops a transfer to/from the CENTIPEDE, waiting (in a
blocking loop) until the Stop condition has completed.
Precondition:
The I2C module must have been initialized & a transfer started.
Parameters:
None.
Returns:
None.
Example:
<code>
StopTransfer();
</code>
Remarks:
This is a blocking routine that waits for the Stop signal to complete.
*****************************************************************************/
void StopTransfer(I2C_MODULE i2cModule) {
I2C_STATUS status;
// Send the Stop signal
I2CStop(i2cModule);
// Wait for the signal to complete
do {
status = I2CGetStatus(i2cModule);
} while (!(status & I2C_STOP));
}
bool initCentipedeChip(I2C_MODULE i2cModule, uint8_t chipAddress, bool isOutput) {
uint8_t i2cData[10];
I2C_7_BIT_ADDRESS SlaveAddress;
bool Success = true;
int Index;
int DataSz;
// Initialize the data buffer
I2C_FORMAT_7_BIT_ADDRESS(SlaveAddress, chipAddress, I2C_WRITE);
if (isOutput) {
i2cData[0] = SlaveAddress.byte;
i2cData[1] = 0x00; // CENTIPEDE IODIRA register (defaults to inputs)
i2cData[2] = 0x00; // PORT A all outputs
i2cData[3] = 0x00; // PORT B all outputs
DataSz = 4;
}
else {
i2cData[0] = SlaveAddress.byte;
i2cData[1] = 0x0C; // CENTIPEDE GPPUA register (pullups)
i2cData[2] = 0xFF; // Enable PORT A pullups
i2cData[3] = 0xFF; // Enable PORT B pullups
DataSz = 4;
}
// Start the transfer to write data to the CENTIPEDE
StartTransfer(i2cModule, false);
// Transmit all data
Index = 0;
while (Index < DataSz) {
// Transmit a byte
if (TransmitOneByte(i2cModule, i2cData[Index])) {
// Advance to the next byte
Index++;
// Verify that the byte was acknowledged
if (!I2CByteWasAcknowledged(i2cModule))
Success = false;
}
}
StopTransfer(i2cModule);
return Success;
}
bool putCentipedeBytes(I2C_MODULE i2cModule, uint8_t chipAddress, UINT16_VAL wordA) {
uint8_t i2cData[10];
I2C_7_BIT_ADDRESS SlaveAddress;
bool Success = true;
int Index;
int DataSz;
// Initialize the data buffer
I2C_FORMAT_7_BIT_ADDRESS(SlaveAddress, chipAddress, I2C_WRITE);
i2cData[0] = SlaveAddress.byte;
i2cData[1] = 0x12; // CENTIPEDE GPIOA register
i2cData[2] = wordA.byte.LB; // PORT A all outputs
i2cData[3] = wordA.byte.HB; // PORT B all outputs
DataSz = 4;
// Start the transfer to write data to the CENTIPEDE
StartTransfer(i2cModule, false);
// Transmit all data
Index = 0;
while (Index < DataSz) {
// Transmit a byte
if (TransmitOneByte(i2cModule, i2cData[Index])) {
// Advance to the next byte
Index++;
// Verify that the byte was acknowledged
if (!I2CByteWasAcknowledged(i2cModule))
Success = false;
}
}
StopTransfer(i2cModule);
return Success;
}
uint16_t getCentipedeBytes(I2C_MODULE i2cModule, uint8_t chipAddress, uint8_t chipRegister) {
I2C_7_BIT_ADDRESS SlaveAddress;
uint16_t i2cResult = 0;
uint16_t tempData = 0;
I2C_FORMAT_7_BIT_ADDRESS(SlaveAddress, chipAddress, I2C_WRITE);
// Start the transfer to read the CENTIPEDE.
StartTransfer(i2cModule, false);
// Address the CENTIPEDE.
TransmitOneByte(i2cModule, SlaveAddress.byte);
while (!I2CByteWasAcknowledged(i2cModule));
TransmitOneByte(i2cModule, chipRegister);
while (!I2CByteWasAcknowledged(i2cModule));
// Restart and send the CENTIPEDE's internal address to switch to a read transfer
StartTransfer(i2cModule, true);
// Transmit the address with the READ bit set
SlaveAddress.rw = I2C_READ;
TransmitOneByte(i2cModule, SlaveAddress.byte);
while (!I2CByteWasAcknowledged(i2cModule));
// Read the data from PORTA
I2CReceiverEnable(i2cModule, true);
while (!I2CReceivedDataIsAvailable(i2cModule));
i2cResult = I2CGetByte(i2cModule);
I2CAcknowledgeByte(i2cModule, true);
while (!I2CAcknowledgeHasCompleted(i2cModule));
// Read the data from PORTB
I2CReceiverEnable(i2cModule, true);
while (!I2CReceivedDataIsAvailable(i2cModule));
tempData = I2CGetByte(i2cModule);
I2CAcknowledgeByte(i2cModule, false);
while (!I2CAcknowledgeHasCompleted(i2cModule));
i2cResult |= tempData << 8;
StopTransfer(i2cModule);
return i2cResult;
}