-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbbi2c.cpp
More file actions
198 lines (171 loc) · 5 KB
/
bbi2c.cpp
File metadata and controls
198 lines (171 loc) · 5 KB
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
#include <Arduino.h>
#include "bbi2c.h"
////////////////////////////////////////////////////////////////////////////////
typedef struct {
int digital_output;
volatile uint8_t * ddr;
volatile uint8_t * pin;
volatile uint8_t * port;
uint8_t mask;
} pin_map_t;
static pin_map_t pin_map[] {
{ 0, &DDRD, &PIND, &PORTD, 0x01 },
{ 1, &DDRD, &PIND, &PORTD, 0x02 },
{ 2, &DDRD, &PIND, &PORTD, 0x04 },
{ 3, &DDRD, &PIND, &PORTD, 0x08 },
{ 4, &DDRD, &PIND, &PORTD, 0x10 },
{ 5, &DDRD, &PIND, &PORTD, 0x20 },
{ 6, &DDRD, &PIND, &PORTD, 0x40 },
{ 7, &DDRD, &PIND, &PORTD, 0x80 },
{ 8, &DDRB, &PINB, &PORTB, 0x01 },
{ 9, &DDRB, &PINB, &PORTB, 0x02 },
{ 10, &DDRB, &PINB, &PORTB, 0x04 },
{ 11, &DDRB, &PINB, &PORTB, 0x08 },
{ 12, &DDRB, &PINB, &PORTB, 0x10 },
{ 13, &DDRB, &PINB, &PORTB, 0x20 },
{ 14, &DDRC, &PINC, &PORTC, 0x01 }, // A0
{ 15, &DDRC, &PINC, &PORTC, 0x02 }, // A1
{ 16, &DDRC, &PINC, &PORTC, 0x04 }, // A2
{ 17, &DDRC, &PINC, &PORTC, 0x08 }, // A3
{ 18, &DDRC, &PINC, &PORTC, 0x10 }, // A4
{ 19, &DDRC, &PINC, &PORTC, 0x20 }, // A5
// { 20, &DDRC, &PINC, &PORTC, 0x40 }, // A6
// { 21, &DDRC, &PINC, &PORTC, 0x80 }, // A7
};
////////////////////////////////////////////////////////////////////////////////
void bbi2c::init(int clock_pin, int data_pin) {
sck_ddr = pin_map[clock_pin].ddr;
sck_pin = pin_map[clock_pin].pin;
sck_port = pin_map[clock_pin].port;
sck_mask = pin_map[clock_pin].mask;
sda_ddr = pin_map[data_pin].ddr;
sda_pin = pin_map[data_pin].pin;
sda_port = pin_map[data_pin].port;
sda_mask = pin_map[data_pin].mask;
// SCK is always an output
sck_is_output();
sda_write(1);
// SDA is an output for now
sda_is_output();
sda_write(1);
}
////////////////////////////////////////////////////////////////////////////////
void bbi2c::start(void) {
sda_is_output();
sda_write(0);
spacer();
sck_write(0);
spacer();
}
////////////////////////////////////////////////////////////////////////////////
void bbi2c::reset(void) {
sda_is_output();
sda_write(1);
sck_write(0);
int i;
for(i=0;i<9;i++) {
sck_write(1);
spacer();
sck_write(0);
spacer();
}
}
////////////////////////////////////////////////////////////////////////////////
uint8_t bbi2c::send_byte(uint8_t b) {
uint8_t errors=0;
uint8_t mask;
sda_is_output();
for(mask=0x80;mask;mask>>=1) {
// present the data bit while the clock is low
sda_write((b&mask)?1:0);
spacer();
// pulse clock line
sck_write(1);
spacer();
sck_write(0);
spacer();
}
// send ACK pulse, read his ACK
sda_is_input();
sck_write(1);
// receiver should pull SDA low here
spacer();
errors=sda_read();
sck_write(0);
sda_is_output();
spacer();
return errors;
}
////////////////////////////////////////////////////////////////////////////////
uint8_t bbi2c::recv_byte(uint8_t ack) {
uint8_t b=0;
// release SDA line
sda_is_input();
// cycle clock and read bits in
uint8_t mask;
for(mask=0x80;mask;mask>>=1) {
sck_write(1);
spacer();
uint8_t inbit = sda_read();
if (inbit) b=b|mask;
sck_write(0);
spacer();
}
// send ACK pulse
sda_is_output();
sda_write(ack?0:1);
sck_write(1);
spacer();
sck_write(0);
spacer();
// release data line
sda_is_input();
return b;
}
////////////////////////////////////////////////////////////////////////////////
void bbi2c::stop(void) {
sda_is_output();
sda_write(0);
spacer();
sck_write(1);
spacer();
sda_write(1);
spacer();
}
////////////////////////////////////////////////////////////////////////////////
// PRIMITIVE PIN OPERATIONS
////////////////////////////////////////////////////////////////////////////////
// BITWISE OPERATIONS
#define MASKSET(name,mask) ((name)|=(mask))
#define MASKCLR(name,mask) ((name)&=~(mask))
#define MASKTST(name,mask) ((name) & (mask))
#define MASKNOT(name,mask) ((name)^=(mask))
//#define BITSET(name,bitn) ((name)|=(0x01<<(bitn)))
//#define BITCLR(name,bitn) ((name)&=~(0x01<<(bitn)))
//#define BITTST(name,bitn) ((name) & (0x01<<(bitn)))
//#define BITNOT(name,bitn) ((name)^=(0x01<<(bitn)))
inline void bbi2c::sck_is_output(void) {
MASKSET(*sck_ddr,sck_mask);
}
inline void bbi2c::sck_write(uint8_t hl) {
if (hl) MASKSET(*sck_port,sck_mask);
else MASKCLR(*sck_port,sck_mask);
}
inline void bbi2c::sda_is_input(void) {
MASKCLR(*sda_ddr,sda_mask);
MASKSET(*sda_port,sda_mask);
}
inline uint8_t bbi2c::sda_read(void) {
return MASKTST(*sda_pin,sda_mask)?1:0;
}
inline void bbi2c::sda_is_output(void) {
MASKSET(*sda_ddr,sda_mask);
}
inline void bbi2c::sda_write(uint8_t hl) {
if (hl) MASKSET(*sda_port,sda_mask);
else MASKCLR(*sda_port,sda_mask);
}
void bbi2c::spacer(void) {
delayMicroseconds(1);
}
////////////////////////////////////////////////////////////////////////////////