-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2c.c
147 lines (120 loc) · 2.49 KB
/
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
/*! \file i2c.c
\brief I2C Functions
I2C Functions
*/
#include "pic.h"
#include "i2c.h"
unsigned long I2C_TimeOut;
void I2C_Init(int Khz,unsigned long TimeOutCycles)
{
unsigned long BSR;
I2C_TimeOut = TimeOutCycles;
BSR = CLOCK_HZ;
BSR = BSR / 4;
BSR = BSR / Khz;
BSR = BSR / 1000;
BSR--;
TRISCbits.RC3 = 1; // Configure pins for input
TRISCbits.RC4 = 1;
SSPCON1 = 0b00101000;
SSPCON2 = 0;
SSPSTAT = 0;
if(Khz < 400)
SSPSTATbits.SMP = 1;
SSPADD = BSR;
PIR1bits.SSPIF = 0;
PIR2bits.BCLIF = 0;
}
char I2C_WaitInterrupt(void)
{
unsigned long counter = 0;
do {
counter++;
if(counter > I2C_TimeOut) {
PIR1bits.SSPIF = 0;
//Log_Error((FARROM*)"I2C Error: Interrupt Timeout\r\n");
return 1;
}
} while(PIR1bits.SSPIF==0);
PIR1bits.SSPIF=0;
counter = 0;
do {
counter++;
if(counter > I2C_TimeOut) {
Log_Error((FARROM*)"I2C Error: Idle Timeout\r\n");
return 1;
}
} while ( ( SSPCON2 & 0x1F ) || ( SSPSTATbits.R_W ) );
PIR1bits.SSPIF=0;
SSPCON1bits.WCOL = 0;
PIR2bits.BCLIF = 0;
return 0;
}
void I2C_Start(void)
{
SSPCON2bits.SEN=1;
I2C_WaitInterrupt();
}
void I2C_Stop(void)
{
SSPCON2bits.PEN=1;
I2C_WaitInterrupt();
}
//Send restart bit
void I2C_Restart(void)
{
SSPCON2bits.RSEN=1;
I2C_WaitInterrupt();
}
char I2C_TX(unsigned char Byte)
{
char t;
char ack;
//while(SSPSTATbits.BF); // Wait if the buffer is full
PIR1bits.SSPIF = 0; // Clear interrupt again just in case.
// Loop until the byte has been sent without collision
do {
SSPCON1bits.WCOL = 0;
PIR2bits.BCLIF = 0;
SSPBUF=Byte;
} while(!SSPCON1bits.WCOL);
t = I2C_WaitInterrupt();
ack = SSPCON2bits.ACKSTAT;
if(t == 0 && ack == 0) {
return 0;
}
if(t == 1) {
// Log_Error((FARROM*)"I2C Error: I2C_TX Timeout on interrupt\r\n");
return 2;
}
if(ack == 1) {
// Log_Error((FARROM*)"I2C Error: I2C_TX Received NACK\r\n");
return 1;
}
return 3;
}
int I2C_RX(void)
{
unsigned char t;
SSPCON2bits.RCEN=1;
t = I2C_WaitInterrupt();
if(t != 0 ) {
Log_Error((FARROM*)"I2C Error: I2C_RX Timeout on interrupt\r\n");
return -1;
}
while(SSPCON2bits.RSEN);
t = SSPBUF;
return t;
}
void I2C_NACK(void)
{
SSPCON2bits.ACKDT=1;
SSPCON2bits.ACKEN=1;
I2C_WaitInterrupt();
}
void I2C_ACK(void)
{
SSPCON2bits.ACKDT=0;
SSPCON2bits.ACKEN=1;
I2C_WaitInterrupt();
}