forked from deemess/STM32Camera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2csoft.c
232 lines (179 loc) · 3.5 KB
/
i2csoft.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
#include "i2csoft.h"
int i2c_host_address = 0;
void Start() {
SET_SDA; I2CDELAY;
SET_SCL; I2CDELAY;
RESET_SDA; I2CDELAY;
RESET_SCL; I2CDELAY;
}
void Stop() {
RESET_SDA; I2CDELAY;
SET_SCL; I2CDELAY;
SET_SDA; I2CDELAY;
}
void ReStart() {
SET_SDA; I2CDELAY;
SET_SCL; I2CDELAY;
RESET_SDA; I2CDELAY;
RESET_SCL; I2CDELAY;
}
int SendAddress(uint8_t address, uint8_t write) {
uint8_t res = 0;
uint8_t i=0;
//send byte
for(i=0; i<7; i++) {
if(address & 0b10000000) {
//1
SET_SDA; I2CDELAY;
SET_SCL; I2CDELAY;
RESET_SCL; I2CDELAY;
SET_SDA; I2CDELAY;
} else {
//0
RESET_SDA; I2CDELAY;
SET_SCL; I2CDELAY;
RESET_SCL; I2CDELAY;
SET_SDA; I2CDELAY;
}
address = address << 1;
}
if(write) {
//0
RESET_SDA; I2CDELAY;
SET_SCL; I2CDELAY;
RESET_SCL; I2CDELAY;
SET_SDA; I2CDELAY;
} else {
//1
SET_SDA; I2CDELAY;
SET_SCL; I2CDELAY;
RESET_SCL; I2CDELAY;
SET_SDA; I2CDELAY;
}
//read ack
I2CDELAY;
SET_SCL;
I2CDELAY;
//check ack
res = GPIO_ReadInputDataBit(I2CPORT, SDA);
RESET_SCL; I2CDELAY;
RESET_SDA; I2CDELAY;
return res;
}
uint8_t SendByte(uint8_t byte) {
uint8_t res = 0;
uint8_t i=0;
//send byte
for(i=0; i<8; i++) {
if(byte & 0b10000000) {
//1
SET_SDA; I2CDELAY;
SET_SCL; I2CDELAY;
RESET_SCL; I2CDELAY;
SET_SDA; I2CDELAY;
} else {
//0
RESET_SDA; I2CDELAY;
SET_SCL; I2CDELAY;
RESET_SCL; I2CDELAY;
SET_SDA; I2CDELAY;
}
byte = byte << 1;
}
//read ack
I2CDELAY;
SET_SCL; I2CDELAY;
//check ack
res = GPIO_ReadInputDataBit(I2CPORT, SDA);
RESET_SCL; I2CDELAY;
RESET_SDA; I2CDELAY;
return res;
}
uint8_t ReadByte(uint8_t ack) {
uint8_t res = 0;
uint8_t i=0;
SET_SDA;
//read byte
for(i=0; i<8; i++) {
res = res << 1;
I2CDELAY;
SET_SCL;
I2CDELAY;
if(GPIO_ReadInputDataBit(I2CPORT, SDA)) {
res = res | 0b00000001;
}
RESET_SCL;
I2CDELAY;
I2CDELAY;
}
//ack
if(ack) {
RESET_SDA; I2CDELAY;
} else {
SET_SDA; I2CDELAY;
}
SET_SCL; I2CDELAY;
RESET_SCL; I2CDELAY;
RESET_SDA; I2CDELAY;
return res;
}
Status I2C_ReadAddr(uint8_t* buf, uint32_t nbuf, uint8_t slaveAddress, uint8_t writeAddress)
{
uint8_t res=0;
uint8_t i=0;
//start
Start();
//send address
res = SendAddress(slaveAddress, 1);
res |= SendByte(writeAddress);
ReStart();
//send address
res |= SendAddress(slaveAddress, 0);
//read data
for(i=0; i<nbuf; i++) {
if(i == nbuf-1) {
//last byte without ack
buf[i] = ReadByte(0);
} else {
buf[i] = ReadByte(1);
}
}
//stop
Stop();
if(res)
return Error;
return Success;
}
Status I2C_WriteAddr(const uint8_t* buf, uint32_t nbuf, uint8_t slaveAddress, uint8_t writeAddress)
{
uint8_t res=0;
uint8_t i=0;
//start
Start();
//send address
res = SendAddress(slaveAddress, 1);
res |= SendByte(writeAddress);
//send data
for(i=0; i<nbuf; i++) {
res |= SendByte(buf[i]);
}
//stop
Stop();
if(res)
return Error;
return Success;
}
void I2C_LowLevel_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
SET_SDA;
SET_SCL;
I2CPORTCLOCKENABLE;
/* Configure I2C1 GPIOs *****************************************************/
GPIO_InitStructure.GPIO_Pin = SCL | SDA;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(I2CPORT, &GPIO_InitStructure);
SET_SDA;
SET_SCL;
}