-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c_eeap.c
58 lines (52 loc) · 2.11 KB
/
i2c_eeap.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
#include <p18cxxx.h>
#include "i2c.h"
/********************************************************************
* Function Name: EEAckPolling *
* Return Value: error condition status *
* Parameters: EE memory control byte *
* Description: Acknowledge polling of I2C EE memory *
* device. This routine can be used for most *
* I2C EE memory device which uses acknowledge *
* polling. *
********************************************************************/
#if defined (I2C_V1)
signed char EEAckPolling( unsigned char control )
{
IdleI2C(); // ensure module is idle
StartI2C(); // initiate START condition
while ( SSPCON2bits.SEN ); // wait until start condition is over
if ( PIR2bits.BCLIF ) // test for bus collision
{
return ( -1 ); // return with Bus Collision error
}
else
{
if ( WriteI2C( control ) == -1) // write byte - R/W bit should be 0
{
StopI2C();
return ( -3 ); // set error for write collision
}
while ( SSPCON2bits.ACKSTAT ) // test for ACK condition received
{
RestartI2C(); // initiate Restart condition
while ( SSPCON2bits.RSEN ); // wait until re-start condition is over
if ( PIR2bits.BCLIF ) // test for bus collision
{
return ( -1 ); // return with Bus Collision error
}
if ( WriteI2C( control ) == -1) // write byte - R/W bit should be 0
{
StopI2C();
return ( -3 ); // set error for write collision
}
}
}
StopI2C(); // send STOP condition
while ( SSPCON2bits.PEN ); // wait until stop condition is over
if ( PIR2bits.BCLIF ) // test for bus collision
{
return ( -1 ); // return with Bus Collision error
}
return ( 0 ); // return with no error
}
#endif