-
Notifications
You must be signed in to change notification settings - Fork 1
/
i2cSwBus.c
190 lines (162 loc) · 4.27 KB
/
i2cSwBus.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
/*
* $Id: i2cSwBus.c,v 1.3 2010/10/01 19:55:03 clivewebster Exp $
* Revision History
* ================
* $Log: i2cSwBus.c,v $
* Revision 1.3 2010/10/01 19:55:03 clivewebster
* Setting the bit rate only applies to a hardware master
*
* Revision 1.2 2010/09/30 16:43:33 clivewebster
* refactored
*
* Revision 1.1 2010/09/30 12:09:06 clivewebster
* Added
*
* ================
*
* Copyright (C) 2010 Clive Webster ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
* File: i2cSwBus.c
* Created on: 28 Sep 2010
* Author: Clive Webster
*
* Simulate an I2C bus in software. Only 'single master' is supported
*
* You MUST add external 4k7 resistors. I cannot use the internal pullups.
* Equally when setting either pin high then I can't use pin_high as if the
* slave extends the clock then something may blow up
*
*/
#include "i2cBus.h"
#include "iopin.h"
#include "timer.h"
// Make the clock high
static void scl_high(const I2C_SOFTWARE_BUS* i2c){
do{
pin_make_input(i2c->scl,FALSE); // Assumes external resistor (works)
}while(pin_is_low(i2c->scl));
}
// Make the data line high
static void sda_high(const I2C_SOFTWARE_BUS* i2c){
pin_make_input(i2c->sda,FALSE); // Assumes external resistor (works)
}
// Make the clock low
static void scl_low(const I2C_SOFTWARE_BUS* i2c){
pin_make_output(i2c->scl,FALSE);
}
// Make the data line low
static void sda_low(const I2C_SOFTWARE_BUS* i2c){
pin_make_output(i2c->sda,FALSE);
}
// Wait for at least 5uS
static void halfDelay(){
delay_us(5);
}
// Return true if the slave acknowledges
static boolean getAck(const I2C_SOFTWARE_BUS* i2c){
sda_high(i2c); // allow slave to drive sda
scl_high(i2c);
halfDelay();
boolean rtn = pin_is_low(i2c->sda);
scl_low(i2c);
return rtn;
}
// Put a byte across the wire
// Return true if the slave acknowledges
static boolean putByte(const I2C_ABSTRACT_BUS* bus, uint8_t b){
const I2C_SOFTWARE_BUS* i2c = (const I2C_SOFTWARE_BUS*)bus;
int8_t i;
for(i=7; i>=0; i--){
if( b & 0x80 ){
sda_high(i2c);
}else{
sda_low(i2c);
}
scl_high(i2c);
b <<= 1;
scl_low(i2c);
}
return getAck(i2c); // return ACK value
}
// Send the start and address
// return TRUE if acknowledged
static boolean start(const I2C_DEVICE* device, boolean writeMode){
boolean rtn = FALSE;
const I2C_SOFTWARE_BUS* i2c = (const I2C_SOFTWARE_BUS*)(device->bus);
if(i2c){
sda_high(i2c);
halfDelay();
scl_high(i2c);
halfDelay();
sda_low(i2c);
halfDelay();
scl_low(i2c);
halfDelay();
// Send the device addr and direction
uint8_t addr = device->addr & 0xfe;
if(writeMode==FALSE){
addr |= 1;
}
rtn = putByte(device->bus, addr);
}
return rtn;
}
static uint8_t getByte(const I2C_ABSTRACT_BUS* bus, boolean isLastByte){
const I2C_SOFTWARE_BUS* i2c = (const I2C_SOFTWARE_BUS*)bus;
int8_t i;
uint8_t b = 0;
sda_high(i2c);
for(i=7; i>=0; i--){
b <<= 1;
scl_high(i2c);
halfDelay();
if(pin_is_high(i2c->sda)){
b |= 1;
}
scl_low(i2c);
halfDelay();
}
// Put the ACK
if(isLastByte){
sda_high(i2c);
}else{
sda_low(i2c);
}
scl_high(i2c);
halfDelay(i2c);
scl_low(i2c);
sda_high(i2c);
return b; // return received byte
}
// Send the stop
static void stop(const I2C_ABSTRACT_BUS* bus){
const I2C_SOFTWARE_BUS* i2c = (const I2C_SOFTWARE_BUS*)bus;
sda_low(i2c);
halfDelay();
scl_high(i2c);
halfDelay();
sda_high(i2c);
halfDelay();
}
static void init(I2C_ABSTRACT_BUS* bus){
const I2C_SOFTWARE_BUS* i2c = (I2C_SOFTWARE_BUS*)bus;
// Make both pins high
sda_high(i2c);
scl_high(i2c);
}
// Expose this implementation to the linker
I2C_CLASS const c_sw_i2c = MAKE_I2C_CLASS(&init, &start,&stop, &getByte, &putByte, null);