Subversion Repositories Code-Repo

Rev

Rev 275 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 275 Rev 277
Line 39... Line 39...
39
    I2C_1_DAT_TRIS = 1;
39
    I2C_1_DAT_TRIS = 1;
40
 
40
 
41
    SSP1STAT = 0x0;
41
    SSP1STAT = 0x0;
42
    SSP1CON1 = 0x0;
42
    SSP1CON1 = 0x0;
43
    SSP1CON2 = 0x0;
43
    SSP1CON2 = 0x0;
-
 
44
    SSP1CON3 = 0x0;
44
    SSP1CON1bits.SSPM = 0x8; // I2C Master Mode
45
    SSP1CON1bits.SSPM = 0x8; // I2C Master Mode
45
    if (speed == 0x01) {
46
    if (speed == 0x01) {
46
        SSP1ADD = 0x13;         // Operate at 400KHz (32MHz)
47
        SSP1ADD = 0x13;         // Operate at 400KHz (32MHz)
47
        SSP1STATbits.SMP = 1;    // Disable Slew Rate Control
48
        SSP1STATbits.SMP = 1;    // Disable Slew Rate Control
48
    } else if (speed == 0x02) {
49
    } else if (speed == 0x02) {
Line 132... Line 133...
132
    SSP1ADD = addr << 1;     // Set the slave address
133
    SSP1ADD = addr << 1;     // Set the slave address
133
 
134
 
134
    SSP1STAT = 0x0;
135
    SSP1STAT = 0x0;
135
    SSP1CON1 = 0x0;
136
    SSP1CON1 = 0x0;
136
    SSP1CON2 = 0x0;
137
    SSP1CON2 = 0x0;
-
 
138
    SSP1CON3 = 0x0;
137
    SSP1CON1bits.SSPM = 0xE; // Enable Slave 7-bit w/ start/stop interrupts
139
    SSP1CON1bits.SSPM = 0x6; // Enable Slave 7-bit address
138
    SSP1STATbits.SMP = 1;    // Slew Off
140
    SSP1STATbits.SMP = 1;    // Slew Off
139
    SSP1CON2bits.SEN = 1;    // Enable clock-stretching
141
    SSP1CON2bits.SEN = 1;    // Enable clock-stretching
-
 
142
    SSP1CON3bits.PCIE = 1;   // Interrupt on stop condition
-
 
143
    SSP1CON3bits.SCIE = 0;   // Disable interrupt on start condition
140
    SSP1CON1bits.SSPEN = 1;  // Enable MSSP1 Module
144
    SSP1CON1bits.SSPEN = 1;  // Enable MSSP1 Module
141
}
145
}
142
 
146
 
143
void I2C1_Interrupt_Handler() {
147
void I2C1_Interrupt_Handler() {
144
    // Call interrupt depending on which mode we are operating in
148
    // Call interrupt depending on which mode we are operating in
Line 347... Line 351...
347
    }
351
    }
348
 
352
 
349
    // Read SPPxBUF if it is full
353
    // Read SPPxBUF if it is full
350
    if (SSP1STATbits.BF == 1) {
354
    if (SSP1STATbits.BF == 1) {
351
        received_data = SSP1BUF;
355
        received_data = SSP1BUF;
352
//        DBG_PRINT_I2C("I2C: data read from buffer: %x\r\n", SSP1BUF);
-
 
353
        data_read_from_buffer = 1;
356
        data_read_from_buffer = 1;
354
    }
357
    }
355
 
358
 
356
    if (!overrun_error) {
359
    if (!overrun_error) {
357
        switch (i2c_data_p->operating_state) {
360
        switch (i2c_data_p->operating_state) {
358
            case I2C_IDLE:
361
            case I2C_IDLE:
359
            {
362
            {
360
                // Ignore anything except a start
363
//                // Ignore anything except a start
361
                if (SSP1STATbits.S == 1) {
364
//                if (SSP1STATbits.S == 1) {
362
                    i2c_data_p->buffer_in_len_tmp = 0;
365
//                    i2c_data_p->buffer_in_len_tmp = 0;
363
                    i2c_data_p->operating_state = I2C_STARTED;
366
//                    i2c_data_p->operating_state = I2C_STARTED;
364
                }
367
//                }
365
                break;
368
//                break;
366
            }
369
//            }
367
            case I2C_STARTED:
370
//            case I2C_STARTED:
368
            {
371
//            {
369
                // In this case, we expect either an address or a stop bit
372
                // In this case, we expect either an address or a stop bit
370
                if (SSP1STATbits.P == 1) {
373
                if (SSP1STATbits.P == 1) {
371
                    // Return to idle mode
374
                    // Return to idle mode
372
                    i2c_data_p->operating_state = I2C_IDLE;
375
                    i2c_data_p->operating_state = I2C_IDLE;
373
                } else if (data_read_from_buffer) {
376
                } else if (data_read_from_buffer) {
-
 
377
                    i2c_data_p->buffer_in_len_tmp = 0;
374
                    if (SSP1STATbits.D_nA == 0) {
378
                    if (SSP1STATbits.D_nA == 0) {
375
                        // Address received
379
                        // Address received
376
                        if (SSP1STATbits.R_nW == 0) {
380
                        if (SSP1STATbits.R_nW == 0) {
377
                            // Slave write mode
381
                            // Slave write mode
378
                            i2c_data_p->operating_state = I2C_RCV_DATA;
382
                            i2c_data_p->operating_state = I2C_RCV_DATA;
Line 382... Line 386...
382
                            // Process the first byte immediatly if sending data
386
                            // Process the first byte immediatly if sending data
383
                            goto send;
387
                            goto send;
384
                        }
388
                        }
385
                    } else {
389
                    } else {
386
                        i2c_data_p->operating_state = I2C_IDLE;
390
                        i2c_data_p->operating_state = I2C_IDLE;
387
                        i2c_data_p->return_status = I2C_ERR_NODATA;
391
                        i2c_data_p->return_status = I2C_ERR_NOADDR;
388
                    }
392
                    }
389
                }
393
                }
390
                break;
394
                break;
391
            }
395
            }
392
            send:
396
            send: