/
i2c.c
284 lines (242 loc) · 5.97 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#if defined(__XC)
#include <xc.h> /* XC8 General Include File */
#elif defined(HI_TECH_C)
#include <htc.h> /* HiTech General Include File */
#elif defined(__18CXX)
#include <p18cxxx.h> /* C18 General Include File */
#endif
#if defined(__XC) || defined(HI_TECH_C)
#include <stdint.h> /* For uint8_t definition */
#include <stdbool.h> /* For true/false definition */
#endif
#include "i2c.h"
#include "system.h"
#include "stdio.h"
void i2c_fail(uint8_t bus)
{
}
void i2c_init(_t_i2c_mode mode, uint8_t bus){
_t_priority prio = HIGH_PRI;
i2cMode = mode;
if (prio == HIGH_PRI) {
IPR1bits.SSP1IP = 1;
} else IPR1bits.SSP1IP = 0;
#ifdef _MIXED
switch (bus){
case 0:
switch (mode){
case I2C_MASTER:
SSP1STAT = 0b11000000;
SSP1CON1 = 0b00111000;
SSP1CON2 = 0b00000000;
// SSP1MSK = 0b00000000;
// BRG su SSPxADD = 77h per 100kHz @48MHz
SSP1ADD = 0x77;
PIE1bits.SSP1IE = 1;
break;
case I2C_SLAVE:
SSP1CON1 = 0b00110110; // I2C I2C_SLAVE mode, 7-bit address. SSP enabled. CLK released
SSP1ADD = SLAVE_ADDR<<1; // address
//SSP1MSK = 0x01;
SSP1CON2 = 0b00000000; //NOT Enables interrupt when a general call address (0000h) is received, not mask, not stretch
PIE1bits.SSP1IE = 1;
break;
default:
SSP1STAT = 0xFF;
SSP1CON1 = 0x00;
SSP2CON2 = 0x00;
PIE1bits.SSP1IE = 0;
break;
}
PIR1bits.SSP1IF = 0;
break;
case 1:
break;
}
#endif
#ifdef _SLAVE
SSP1CON1 = 0b00110110; // I2C I2C_SLAVE mode, 7-bit address. SSP enabled. CLK released
SSP1ADD = SLAVE_ADDR<<1;
//SSP1MSK = 0x01;
PIR1bits.SSP1IF = 0; //interrupt after receive byte
SSP1CON2 = 0b00000000; //NOT Enables interrupt when a general call address (0000h) is received, not mask, not stretch
PIR1bits.SSP1IF = 0;
#endif
#ifdef _MASTER
SSP1STAT = 0b11000000;
SSP1CON1 = 0b00111000;
SSP1CON2 = 0b00000000;
// SSP1MSK = 0b00000000;
// BRG su SSPxADD = 77h per 100kHz @48MHz
SSP1ADD = 0x77;
#endif
}
#if defined (_MASTER) || defined(_MIXED)
void i2c_waitForIdle(uint8_t bus)
{
if (bus == 0){ //bus0 of MCU
while (( SSP1CON2 & 0x1F ) | SSP1STATbits.R_W ) {}; // wait until...
}
}
#endif
/******************************************************************************************/
#if defined (_MASTER) || defined(_MIXED)
void i2c_start(uint8_t bus)
{
if (bus == 0){ //bus0 of MCU
i2c_waitForIdle(bus);
SSP1CON2bits.SEN=1;
}
}
#endif
/******************************************************************************************/
#if defined (_MASTER) || defined(_MIXED)
void i2c_repStart(uint8_t bus)
{
if (bus == 0){ //bus0 of MCU
i2c_waitForIdle(bus);
SSP1CON2bits.RSEN=1;
}
}
#endif
/******************************************************************************************/
#if defined (_MASTER) || defined(_MIXED)
void i2c_stop(uint8_t bus)
{
if (bus == 0){ //bus0 of MCU
SSP1CON2bits.PEN=1;
}
}
#endif
/******************************************************************************************/
unsigned char i2c_read( unsigned char ack, uint8_t bus )
{
if (bus == 0){ //bus0 of MCU
if (i2cMode == I2C_MASTER){
unsigned char i2cReadData;
PIR1bits.SSP1IF = 0;
SSP1CON2bits.RCEN=1;
i2cReadData = SSP1BUF;
PIR1bits.SSP1IF = 0;
if ( ack )
{
SSP1CON2bits.ACKDT=0;
}
else
{
SSP1CON2bits.ACKDT=1;
}
SSP1CON2bits.ACKEN=1; // send acknowledge sequence
return( i2cReadData );
}
else if (i2cMode == I2C_SLAVE)
return( SSP1BUF );
}
return 0;
}
/******************************************************************************************/
unsigned char i2c_write( unsigned char i2cWriteData, uint8_t bus )
{
if (bus == 0){ //bus0 of MCU
if (i2cMode == I2C_MASTER){
PIR1bits.SSP1IF = 0;
SSP1BUF = i2cWriteData;
return (0);
}
else if (i2cMode == I2C_SLAVE){
SSP1BUF = i2cWriteData;
return 0;
}
}
return 0;
}
#if defined (_SLAVE) || defined(_MIXED)
uint8_t getI2Cstatus(uint8_t bus){
if (bus == 0){ //bus0 of MCU
return (SSP1STAT & SLAVE_STATUS);
} else if (bus == 1){
}
return 0;
}
#endif
uint8_t isAckI2C(uint8_t bus){
if (bus == 0){ //bus0 of MCU
return (SSP1CON2bits.ACKSTAT);
} else if (bus == 1){
return (0);
}
return 0;
}
#if defined (_SLAVE) || defined(_MIXED)
void i2cSlaveHandler(uint8_t bus)
{
temp = getI2Cstatus(bus);
if (temp == ADX_W){
temp = _writeAdx;
i2c_read(0, bus); //dummy read, empty the buff
}
else if (temp == DATA_W) { // write to DS
temp = _writeData; //write to slave, i.e. receive
}
else if ((temp & MASK_BF_BIT) == ADX_R) {
temp = _readAdx;
i2c_read(0, bus); // empty the buff
}
else if (temp == DATA_R) { //read from DS, not needed to update the RTC timetable
temp = _readData; // read from slave, i.e. transmit
i2c_read(0, bus); // empty the buff
}
else if (temp == ERR) {
}
else {
}
}
#endif
//
//#if defined(__XC)
// #include <xc.h> /* XC8 General Include File */
//#elif defined(HI_TECH_C)
// #include <htc.h> /* HiTech General Include File */
//#elif defined(__18CXX)
// #include <p18cxxx.h> /* C18 General Include File */
//#endif
//
//#if defined(__XC) || defined(HI_TECH_C)
//
//#include <stdint.h> /* For uint8_t definition */
//#include <stdbool.h> /* For true/false definition */
//
//#endif
//
//#include "i2c.h"
//
//
//
//void i2c_init(){
// SSP1CON1 = 0b00110110; // I2C I2C_SLAVE mode, 7-bit address. SSP enabled. CLK released
// SSP1ADD = SLAVE_ADDR<<1; // Indirizzo del DS1307
// //SSP1MSK = 0x01;
// SSP1CON2 = 0b00000000; //NOT Enables interrupt when a general call address (0000h) is received, not mask, not stretch
// PIR1bits.SSP1IF = 0; //interrupt after receive byte
// PIE1bits.SSP1IE = 1;
//}
//
//
//unsigned char i2c_read()
//{
// return( SSP1BUF );
//}
//
///******************************************************************************************/
//
//void i2c_write( unsigned char i2cWriteData )
//{
//
// //i2c_waitForIdle();
// SSP1BUF = i2cWriteData;
//}
//
//uint8_t getI2Cstatus(){
// return (SSP1STAT & SLAVE_STATUS);
//}
//