Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
119 Kevin 1
#include "maindefs.h"
2
#include "i2c.h"
3
 
120 Kevin 4
static I2C_DATA i2c_data;
119 Kevin 5
 
6
// Set up the data structures for the i2c code
7
// Should be called once before any i2c routines are called
120 Kevin 8
void I2C_Init() {
9
    i2c_data.buffer_in_len = 0;
10
    i2c_data.buffer_in_len_tmp = 0;
11
    i2c_data.buffer_in_read_ind = 0;
12
    i2c_data.buffer_in_write_ind = 0;
13
 
14
    i2c_data.buffer_out_ind = 0;
15
    i2c_data.buffer_out_len = 0;
16
 
17
    i2c_data.operating_mode = 0;
18
    i2c_data.operating_state = I2C_IDLE;
19
    i2c_data.return_status = 0;
20
 
21
    i2c_data.slave_in_last_byte = 0;
22
    i2c_data.slave_sending_data = 0;
23
 
24
    i2c_data.master_dest_addr = 0;
25
    i2c_data.master_status = I2C_MASTER_IDLE;
26
 
27
    // Enable I2C interrupt
28
    PIE1bits.SSPIE = 1;
119 Kevin 29
}
30
 
31
// Setup the PIC to operate as a master.
121 Kevin 32
void I2C_Configure_Master(unsigned char speed) {
120 Kevin 33
    i2c_data.operating_mode = I2C_MODE_MASTER;
119 Kevin 34
 
35
    TRISCbits.TRISC3 = 1;
36
    TRISCbits.TRISC4 = 1;
37
 
38
    SSPSTAT = 0x0;
39
    SSPCON1 = 0x0;
40
    SSPCON2 = 0x0;
41
    SSPCON1bits.SSPM = 0x8; // I2C Master Mode
121 Kevin 42
    if (speed) {
43
        SSPADD = 0x74;          // Operate at 100KHz (48MHz)
44
    } else {
45
        SSPADD = 0x1A;          // Operate at 400KHz (48MHz)
46
    }
119 Kevin 47
    SSPSTATbits.SMP = 1;    // Disable Slew Rate Control
48
    SSPCON1bits.SSPEN = 1;  // Enable MSSP Module
49
}
50
 
51
// Sends length number of bytes in msg to specified address (no R/W bit)
120 Kevin 52
void I2C_Master_Send(unsigned char address, unsigned char length, unsigned char *msg) {
53
    unsigned char i;
119 Kevin 54
    if (length == 0)
55
        return;
56
 
57
    // Copy message to send into buffer and save length/address
58
    for (i = 0; i < length; i++) {
120 Kevin 59
        i2c_data.buffer_in[i] = msg[i];
119 Kevin 60
    }
120 Kevin 61
    i2c_data.buffer_in_len = length;
62
    i2c_data.master_dest_addr = address;
63
    i2c_data.buffer_in_read_ind = 0;
121 Kevin 64
    i2c_data.buffer_in_write_ind = 0;
119 Kevin 65
 
66
    // Change status to 'next' operation
120 Kevin 67
    i2c_data.operating_state = I2C_SEND_ADDR;
68
    i2c_data.master_status = I2C_MASTER_SEND;
119 Kevin 69
 
70
    // Generate start condition
71
    SSPCON2bits.SEN = 1;
72
}
73
 
74
// Reads length number of bytes from address (no R/W bit)
120 Kevin 75
void I2C_Master_Recv(unsigned char address, unsigned char length) {
119 Kevin 76
    if (length == 0)
77
        return;
78
 
79
    // Save length and address to get data from
120 Kevin 80
    i2c_data.buffer_in_len = length;
81
    i2c_data.master_dest_addr = address;
82
    i2c_data.buffer_in_read_ind = 0;
121 Kevin 83
    i2c_data.buffer_in_write_ind = 0;
119 Kevin 84
 
85
    // Change status to 'next' operation
120 Kevin 86
    i2c_data.operating_state = I2C_SEND_ADDR;
87
    i2c_data.master_status = I2C_MASTER_RECV;
119 Kevin 88
 
89
    // Generate start condition
90
    SSPCON2bits.SEN = 1;
91
}
92
 
120 Kevin 93
// Writes msg to address then reads length number of bytes from address
94
void I2C_Master_Restart(unsigned char address, unsigned char msg, unsigned char length) {
95
    unsigned char c;
96
    if (length == 0) {
97
        c = msg;
98
        I2C_Master_Send(address, 1, &c);
99
        return;
100
    }
101
 
102
    // Save length and address to get data from
103
    i2c_data.buffer_in[0] = msg;
104
    i2c_data.buffer_in_len = length;
105
    i2c_data.master_dest_addr = address;
106
    i2c_data.buffer_in_read_ind = 0;
121 Kevin 107
    i2c_data.buffer_in_write_ind = 0;
120 Kevin 108
 
109
    // Change status to 'next' operation
110
    i2c_data.operating_state = I2C_SEND_ADDR;
111
    i2c_data.master_status = I2C_MASTER_RESTART;
112
 
113
    // Generate start condition
114
    SSPCON2bits.SEN = 1;
115
}
116
 
119 Kevin 117
// Setup the PIC to operate as a slave. The address must not include the R/W bit
120 Kevin 118
void I2C_Configure_Slave(unsigned char addr) {
119
    i2c_data.operating_mode = I2C_MODE_SLAVE;
120
 
119 Kevin 121
    // Ensure the two lines are set for input (we are a slave)
122
    TRISCbits.TRISC3 = 1;
123
    TRISCbits.TRISC4 = 1;
124
 
125
    SSPADD = addr << 1;     // Set the slave address
126
 
127
    SSPSTAT = 0x0;
128
    SSPCON1 = 0x0;
129
    SSPCON2 = 0x0;
130
    SSPCON1bits.SSPM = 0xE; // Enable Slave 7-bit w/ start/stop interrupts
131
    SSPSTATbits.SMP = 1;    // Slew Off
132
    SSPCON2bits.SEN = 1;    // Enable clock-stretching
133
    SSPCON1bits.SSPEN = 1;  // Enable MSSP Module
134
}
135
 
120 Kevin 136
void I2C_Interrupt_Handler() {
119 Kevin 137
    // Call interrupt depending on which mode we are operating in
120 Kevin 138
    if (i2c_data.operating_mode == I2C_MODE_MASTER) {
139
        I2C_Interrupt_Master();
140
    } else if (i2c_data.operating_mode == I2C_MODE_SLAVE) {
141
        I2C_Interrupt_Slave();
119 Kevin 142
    }
143
}
144
 
145
// An internal subroutine used in the master version of the i2c_interrupt_handler
120 Kevin 146
void I2C_Interrupt_Master() {
119 Kevin 147
    // If we are in the middle of sending data
120 Kevin 148
    if (i2c_data.master_status == I2C_MASTER_SEND) {
149
        switch (i2c_data.operating_state) {
119 Kevin 150
            case I2C_IDLE:
151
                break;
152
            case I2C_SEND_ADDR:
153
                // Send the address with read bit set
120 Kevin 154
                i2c_data.operating_state = I2C_CHECK_ACK_SEND;
155
                SSPBUF = (i2c_data.master_dest_addr << 1) | 0x0;
119 Kevin 156
                break;
120 Kevin 157
            case I2C_CHECK_ACK_SEND:
119 Kevin 158
                // Check if ACK is received or not
159
                if (!SSPCON2bits.ACKSTAT) {
160
                    // If an ACK is received, send next byte of data
120 Kevin 161
                    if (i2c_data.buffer_in_read_ind < i2c_data.buffer_in_len) {
162
                        SSPBUF = i2c_data.buffer_in[i2c_data.buffer_in_read_ind];
163
                        i2c_data.buffer_in_read_ind++;
119 Kevin 164
                    } else {
165
                        // If no more data is to be sent, send stop bit
120 Kevin 166
                        i2c_data.operating_state = I2C_IDLE;
119 Kevin 167
                        SSPCON2bits.PEN = 1;
120 Kevin 168
                        i2c_data.master_status = I2C_MASTER_IDLE;
169
                        i2c_data.return_status = I2C_SEND_OK;
119 Kevin 170
                    }
171
                } else {
172
                    // If a NACK is received, stop transmission and send error
120 Kevin 173
                    i2c_data.operating_state = I2C_IDLE;
119 Kevin 174
                    SSPCON2bits.PEN = 1;
120 Kevin 175
                    i2c_data.master_status = I2C_MASTER_IDLE;
176
                    i2c_data.return_status = I2C_SEND_FAIL;
119 Kevin 177
                }
178
                break;
179
        }
180
    // If we are in the middle of receiving data
120 Kevin 181
    } else if (i2c_data.master_status == I2C_MASTER_RECV) {
182
        switch (i2c_data.operating_state) {
119 Kevin 183
            case I2C_IDLE:
184
                break;
185
            case I2C_SEND_ADDR:
186
                // Send address with write bit set
120 Kevin 187
                i2c_data.operating_state = I2C_CHECK_ACK_RECV;
188
                SSPBUF = (i2c_data.master_dest_addr << 1) | 0x1;
119 Kevin 189
                break;
120 Kevin 190
            case I2C_CHECK_ACK_RECV:
119 Kevin 191
                // Check if ACK is received
192
                if (!SSPCON2bits.ACKSTAT) {
193
                    // If an ACK is received, set module to receive 1 byte of data
120 Kevin 194
                    i2c_data.operating_state = I2C_RCV_DATA;
119 Kevin 195
                    SSPCON2bits.RCEN = 1;
196
                } else {
197
                    // If a NACK is received, stop transmission and send error
120 Kevin 198
                    i2c_data.operating_state = I2C_IDLE;
119 Kevin 199
                    SSPCON2bits.PEN = 1;
120 Kevin 200
                    i2c_data.master_status = I2C_MASTER_IDLE;
201
                    i2c_data.return_status = I2C_RECV_FAIL;
119 Kevin 202
                }
203
                break;
204
            case I2C_RCV_DATA:
205
                // On receive, save byte into buffer
121 Kevin 206
                // TODO: handle i2c buffer overflow
207
                i2c_data.buffer_in[i2c_data.buffer_in_write_ind] = SSPBUF;
208
                i2c_data.buffer_in_write_ind++;
209
                if (i2c_data.buffer_in_write_ind < i2c_data.buffer_in_len) {
119 Kevin 210
                    // If we still need to read, send an ACK to the slave
120 Kevin 211
                    i2c_data.operating_state = I2C_REQ_DATA;
119 Kevin 212
                    SSPCON2bits.ACKDT = 0;  // ACK
213
                    SSPCON2bits.ACKEN = 1;
214
                } else {
215
                    // If we are done reading, send an NACK to the slave
120 Kevin 216
                    i2c_data.operating_state = I2C_SEND_STOP;
119 Kevin 217
                    SSPCON2bits.ACKDT = 1;  // NACK
218
                    SSPCON2bits.ACKEN = 1;
219
                }
220
                break;
221
            case I2C_REQ_DATA:
222
                // Set module to receive one byte of data
120 Kevin 223
                i2c_data.operating_state = I2C_RCV_DATA;
119 Kevin 224
                SSPCON2bits.RCEN = 1;
225
                break;
226
            case I2C_SEND_STOP:
227
                // Send the stop bit and copy message to send to Main()
120 Kevin 228
                i2c_data.operating_state = I2C_IDLE;
119 Kevin 229
                SSPCON2bits.PEN = 1;
120 Kevin 230
                i2c_data.master_status = I2C_MASTER_IDLE;
231
                i2c_data.return_status = I2C_RECV_OK;
119 Kevin 232
                break;
233
        }
120 Kevin 234
    } else if (i2c_data.master_status == I2C_MASTER_RESTART) {
235
        switch (i2c_data.operating_state) {
236
            case I2C_IDLE:
237
                break;
238
            case I2C_SEND_ADDR:
239
                // Send the address with read bit set
240
                i2c_data.operating_state = I2C_CHECK_ACK_SEND;
241
                SSPBUF = (i2c_data.master_dest_addr << 1) | 0x0;
242
                break;
243
            case I2C_CHECK_ACK_SEND:
244
                // Check if ACK is received or not
245
                if (!SSPCON2bits.ACKSTAT) {
246
                    // If an ACK is received, send first byte of data
247
                    SSPBUF = i2c_data.buffer_in[0];
248
                    i2c_data.operating_state = I2C_CHECK_ACK_RESTART;
249
                } else {
250
                    // If a NACK is received, stop transmission and send error
251
                    i2c_data.operating_state = I2C_IDLE;
252
                    SSPCON2bits.PEN = 1;
253
                    i2c_data.master_status = I2C_MASTER_IDLE;
254
                    i2c_data.return_status = I2C_SEND_FAIL;
255
                }
256
                break;
257
            case I2C_CHECK_ACK_RESTART:
258
                if (!SSPCON2bits.ACKSTAT) {
259
                    SSPCON2bits.RSEN = 1;
260
                    i2c_data.operating_state = I2C_SEND_ADDR_2;
261
                } else {
262
                    // If a NACK is received, stop transmission and send error
263
                    i2c_data.operating_state = I2C_IDLE;
264
                    SSPCON2bits.PEN = 1;
265
                    i2c_data.master_status = I2C_MASTER_IDLE;
266
                    i2c_data.return_status = I2C_SEND_FAIL;
267
                }
268
                break;
269
            case I2C_SEND_ADDR_2:
270
                // Send the address with read bit set
271
                i2c_data.operating_state = I2C_CHECK_ACK_RECV;
272
                SSPBUF = (i2c_data.master_dest_addr << 1) | 0x1;
273
                break;
274
            case I2C_CHECK_ACK_RECV:
275
                // Check if ACK is received
276
                if (!SSPCON2bits.ACKSTAT) {
277
                    // If an ACK is received, set module to receive 1 byte of data
278
                    i2c_data.operating_state = I2C_RCV_DATA;
279
                    SSPCON2bits.RCEN = 1;
280
                } else {
281
                    // If a NACK is received, stop transmission and send error
282
                    i2c_data.operating_state = I2C_IDLE;
283
                    SSPCON2bits.PEN = 1;
284
                    i2c_data.master_status = I2C_MASTER_IDLE;
285
                    i2c_data.return_status = I2C_RECV_FAIL;
286
                }
287
                break;
288
            case I2C_RCV_DATA:
289
                // On receive, save byte into buffer
121 Kevin 290
                // TODO: handle i2c buffer overflow
291
                i2c_data.buffer_in[i2c_data.buffer_in_write_ind] = SSPBUF;
292
                i2c_data.buffer_in_write_ind++;
293
                if (i2c_data.buffer_in_write_ind < i2c_data.buffer_in_len) {
120 Kevin 294
                    // If we still need to read, send an ACK to the slave
295
                    i2c_data.operating_state = I2C_REQ_DATA;
296
                    SSPCON2bits.ACKDT = 0;  // ACK
297
                    SSPCON2bits.ACKEN = 1;
298
                } else {
299
                    // If we are done reading, send an NACK to the slave
300
                    i2c_data.operating_state = I2C_SEND_STOP;
301
                    SSPCON2bits.ACKDT = 1;  // NACK
302
                    SSPCON2bits.ACKEN = 1;
303
                }
304
                break;
305
            case I2C_REQ_DATA:
306
                // Set module to receive one byte of data
307
                i2c_data.operating_state = I2C_RCV_DATA;
308
                SSPCON2bits.RCEN = 1;
309
                break;
310
            case I2C_SEND_STOP:
311
                // Send the stop bit and copy message to send to Main()
312
                i2c_data.operating_state = I2C_IDLE;
313
                SSPCON2bits.PEN = 1;
314
                i2c_data.master_status = I2C_MASTER_IDLE;
315
                i2c_data.return_status = I2C_RECV_OK;
316
                break;
119 Kevin 317
        }
318
    }
319
}
320
 
120 Kevin 321
void I2C_Interrupt_Slave() {
322
    unsigned char received_data;
119 Kevin 323
    unsigned char data_read_from_buffer = 0;
324
    unsigned char data_written_to_buffer = 0;
325
    unsigned char overrun_error = 0;
326
 
327
    // Clear SSPOV (overflow bit)
328
    if (SSPCON1bits.SSPOV == 1) {
122 Kevin 329
        DBG_PRINT_I2C("I2C: (ERROR) overflow detected\r\n");
119 Kevin 330
        SSPCON1bits.SSPOV = 0;
331
        // We failed to read the buffer in time, so we know we
332
        //  can't properly receive this message, just put us in the
333
        //  a state where we are looking for a new message
120 Kevin 334
        i2c_data.operating_state = I2C_IDLE;
119 Kevin 335
        overrun_error = 1;
120 Kevin 336
        i2c_data.return_status = I2C_ERR_OVERRUN;
119 Kevin 337
    }
338
 
339
    // Read SPPxBUF if it is full
340
    if (SSPSTATbits.BF == 1) {
120 Kevin 341
        received_data = SSPBUF;
122 Kevin 342
//        DBG_PRINT_I2C("I2C: data read from buffer: %x\r\n", SSPBUF);
119 Kevin 343
        data_read_from_buffer = 1;
344
    }
345
 
346
    if (!overrun_error) {
120 Kevin 347
        switch (i2c_data.operating_state) {
119 Kevin 348
            case I2C_IDLE:
349
            {
350
                // Ignore anything except a start
351
                if (SSPSTATbits.S == 1) {
120 Kevin 352
                    i2c_data.buffer_in_len_tmp = 0;
353
                    i2c_data.operating_state = I2C_STARTED;
354
//                    if (data_read_from_buffer) {
355
//                        if (SSPSTATbits.D_A == 1) {
121 Kevin 356
//                            DBG_PRINT_I2C("I2C Start: (ERROR) no address recieved\r\n");
120 Kevin 357
//                            // This is bad because we got data and we wanted an address
358
//                            i2c_data.operating_state = I2C_IDLE;
359
//                            i2c_data.return_status = I2C_ERR_NOADDR;
360
//                        } else {
361
//                            // Determine if we are sending or receiving data
362
//                            if (SSPSTATbits.R_W == 1) {
363
//                                i2c_data.operating_state = I2C_SEND_DATA;
364
//                            } else {
365
//                                i2c_data.operating_state = I2C_RCV_DATA;
366
//                            }
367
//                        }
368
//                    } else {
369
//                        i2c_data.operating_state = I2C_STARTED;
370
//                    }
119 Kevin 371
                }
372
                break;
373
            }
374
            case I2C_STARTED:
375
            {
376
                // In this case, we expect either an address or a stop bit
377
                if (SSPSTATbits.P == 1) {
378
                    // Return to idle mode
120 Kevin 379
                    i2c_data.operating_state = I2C_IDLE;
119 Kevin 380
                } else if (data_read_from_buffer) {
381
                    if (SSPSTATbits.D_A == 0) {
120 Kevin 382
                        // Address received
383
                        if (SSPSTATbits.R_W == 0) {
384
                            // Slave write mode
385
                            i2c_data.operating_state = I2C_RCV_DATA;
386
                        } else {
387
                            // Slave read mode
388
                            i2c_data.operating_state = I2C_SEND_DATA;
389
                            // Process the first byte immediatly if sending data
390
                            goto send;
119 Kevin 391
                        }
392
                    } else {
121 Kevin 393
                        DBG_PRINT_I2C("I2C: (ERROR) no data recieved\r\n");
120 Kevin 394
                        i2c_data.operating_state = I2C_IDLE;
395
                        i2c_data.return_status = I2C_ERR_NODATA;
119 Kevin 396
                    }
397
                }
398
                break;
399
            }
120 Kevin 400
            send:
119 Kevin 401
            case I2C_SEND_DATA:
402
            {
120 Kevin 403
                if (!i2c_data.slave_sending_data) {
404
                    // If we are not currently sending data, figure out what to reply with
405
                    if (I2C_Process_Send(i2c_data.slave_in_last_byte)) {
406
                        // Data exists to be returned, send first byte
407
                        SSPBUF = i2c_data.buffer_out[0];
408
                        i2c_data.buffer_out_ind = 1;
409
                        i2c_data.slave_sending_data = 1;
119 Kevin 410
                        data_written_to_buffer = 1;
411
                    } else {
120 Kevin 412
                        // Unknown request
413
                        i2c_data.slave_sending_data = 0;
414
                        i2c_data.operating_state = I2C_IDLE;
119 Kevin 415
                    }
416
                } else {
120 Kevin 417
                    // Sending remaining data back to master
418
                    if (i2c_data.buffer_out_ind < i2c_data.buffer_out_len) {
419
                        SSPBUF = i2c_data.buffer_out[i2c_data.buffer_out_ind];
420
                        i2c_data.buffer_out_ind++;
119 Kevin 421
                        data_written_to_buffer = 1;
422
                    } else {
120 Kevin 423
                        // Nothing left to send
424
                        i2c_data.slave_sending_data = 0;
425
                        i2c_data.operating_state = I2C_IDLE;
119 Kevin 426
                    }
427
                }
428
                break;
429
            }
430
            case I2C_RCV_DATA:
431
            {
432
                // We expect either data or a stop bit or a (if a restart, an addr)
433
                if (SSPSTATbits.P == 1) {
120 Kevin 434
                    // Stop bit detected, we need to check to see if we also read data
119 Kevin 435
                    if (data_read_from_buffer) {
436
                        if (SSPSTATbits.D_A == 1) {
120 Kevin 437
                            // Data received with stop bit
121 Kevin 438
                            // TODO: handle i2c buffer overflow
120 Kevin 439
                            i2c_data.buffer_in[i2c_data.buffer_in_write_ind] = received_data;
440
                            if (i2c_data.buffer_in_write_ind == MAXI2CBUF-1) {
441
                                i2c_data.buffer_in_write_ind = 0;
442
                            } else {
443
                                i2c_data.buffer_in_write_ind++;
444
                            }
445
                            i2c_data.buffer_in_len_tmp++;
446
                            // Save the last byte received
447
                            i2c_data.slave_in_last_byte = received_data;
448
                            i2c_data.return_status = I2C_DATA_AVAL;
119 Kevin 449
                        } else {
121 Kevin 450
                            DBG_PRINT_I2C("I2C: (ERROR) no data recieved\r\n");
120 Kevin 451
                            i2c_data.operating_state = I2C_IDLE;
452
                            i2c_data.return_status = I2C_ERR_NODATA;
119 Kevin 453
                        }
454
                    }
120 Kevin 455
                    i2c_data.buffer_in_len += i2c_data.buffer_in_len_tmp;
456
                    i2c_data.operating_state = I2C_IDLE;
119 Kevin 457
                } else if (data_read_from_buffer) {
458
                    if (SSPSTATbits.D_A == 1) {
120 Kevin 459
                        // Data received
460
                        i2c_data.buffer_in[i2c_data.buffer_in_write_ind] = received_data;
461
                        if (i2c_data.buffer_in_write_ind == MAXI2CBUF-1) {
462
                            i2c_data.buffer_in_write_ind = 0;
463
                        } else {
464
                            i2c_data.buffer_in_write_ind++;
465
                        }
466
                        i2c_data.buffer_in_len_tmp++;
467
                        // Save the last byte received
468
                        i2c_data.slave_in_last_byte = received_data;
469
                        i2c_data.return_status = I2C_DATA_AVAL;
470
                    } else {
471
                        // Restart bit detected
119 Kevin 472
                        if (SSPSTATbits.R_W == 1) {
120 Kevin 473
                            i2c_data.buffer_in_len += i2c_data.buffer_in_len_tmp;
474
                            i2c_data.operating_state = I2C_SEND_DATA;
475
                            // Process the first byte immediatly if sending data
476
                            goto send;
477
                        } else {
478
                            // Bad to recv an address again, we aren't ready
121 Kevin 479
                            DBG_PRINT_I2C("I2C: (ERROR) no data recieved\r\n");
120 Kevin 480
                            i2c_data.operating_state = I2C_IDLE;
481
                            i2c_data.return_status = I2C_ERR_NODATA;
119 Kevin 482
                        }
483
                    }
484
                }
485
                break;
486
            }
487
        }
488
    }
489
 
490
    // Release the clock stretching bit (if we should)
491
    if (data_read_from_buffer || data_written_to_buffer) {
492
        // Release the clock
493
        if (SSPCON1bits.CKP == 0) {
494
            SSPCON1bits.CKP = 1;
495
        }
496
    }
120 Kevin 497
}
119 Kevin 498
 
120 Kevin 499
/* Returns 0 if I2C module is currently busy, otherwise returns status code */
500
unsigned char I2C_Get_Status() {
501
    if (i2c_data.operating_mode == I2C_MODE_MASTER) {
502
        if (i2c_data.master_status != I2C_MASTER_IDLE || i2c_data.buffer_in_len == 0) {
503
            return 0;
504
        } else {
505
            return i2c_data.return_status;
506
        }
507
    } else if (i2c_data.operating_mode = I2C_MODE_SLAVE) {
508
        if (i2c_data.operating_state != I2C_IDLE || i2c_data.buffer_in_len == 0) {
509
            return 0;
510
        } else {
511
            return i2c_data.return_status;
512
        }
119 Kevin 513
    }
120 Kevin 514
}
119 Kevin 515
 
121 Kevin 516
unsigned char I2C_Buffer_Len() {
517
    return i2c_data.buffer_in_len;
518
}
519
 
120 Kevin 520
/* Returns 0 if I2C module is currently busy, otherwise returns buffer length */
521
unsigned char I2C_Read_Buffer(char *buffer) {
522
    unsigned char i = 0;
523
    while (i2c_data.buffer_in_len != 0) {
524
        buffer[i] = i2c_data.buffer_in[i2c_data.buffer_in_read_ind];
525
        i++;
526
        if (i2c_data.buffer_in_read_ind == MAXI2CBUF-1) {
527
            i2c_data.buffer_in_read_ind = 0;
528
        } else {
529
            i2c_data.buffer_in_read_ind++;
530
        }
531
        i2c_data.buffer_in_len--;
119 Kevin 532
    }
120 Kevin 533
    return i;
119 Kevin 534
}
535
 
120 Kevin 536
/* Put data to be returned here */
537
unsigned char I2C_Process_Send(unsigned char c) {
538
    unsigned char ret = 0;
539
    switch (c) {
540
        case 0xAA:
541
            i2c_data.buffer_out[0] = 'A';
542
            i2c_data.buffer_out_len = 1;
543
            ret = 1;
544
            break;
545
        case 0xBB:
546
            i2c_data.buffer_out[0] = '1';
547
            i2c_data.buffer_out[1] = '2';
548
            i2c_data.buffer_out_len = 2;
549
            ret = 1;
550
            break;
119 Kevin 551
    }
120 Kevin 552
    return ret;
553
}