/
softi2c.c
84 lines (69 loc) · 1.4 KB
/
softi2c.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
#include "softi2c.h"
void inline __start(const SoftI2C *i2c) {
i2c->sck(1);
i2c->sda(1);
i2c->sda(0);
i2c->sck(0);
}
void inline __stop(const SoftI2C *i2c) {
i2c->sda(0);
i2c->sck(1);
i2c->sda(1);
}
bool inline __onebit(const SoftI2C *i2c, bool bit) {
uint32_t timeout = 0;
i2c->sda(bit);
while (i2c->sck(1) == 0) {
if (timeout++ > 1000) {
break;
}
}
bit = i2c->sda(bit);
i2c->sck(0);
return bit;
}
uint8_t inline __onebyte(const SoftI2C *i2c, uint8_t dat) {
uint8_t ret;
uint8_t bit;
for (ret = 0, bit = 0x80; bit != 0x00; bit = bit >> 1) {
if (__onebit(i2c, dat & bit)) {
ret |= bit;
}
}
return ret;
}
void SoftI2C_Init(const SoftI2C *i2c) {
i2c->init();
i2c->sda(1);
i2c->sck(1);
}
int SoftI2C_Write(const SoftI2C *i2c, uint8_t addr, const uint8_t *dat, int len) {
int i;
__start(i2c); // start
__onebyte(i2c, addr << 1); // addr + Write
if (0 != __onebit(i2c, 1)) { // check ACK
return 0;
}
for (i = 0; i < len; ++i) {
__onebyte(i2c, *dat++);
if (0 != __onebit(i2c, 1)) { // check ACK
break;
}
}
__stop(i2c);
return i;
}
int SoftI2C_Read(const SoftI2C *i2c, uint8_t addr, uint8_t *dat, int len) {
int i;
__start(i2c); // start
__onebyte(i2c, (addr << 1) + 1); // addr + Write
if (0 != __onebit(i2c, 1)) { // check ACK
return 0;
}
for (i = 0; i < len; ++i) {
*dat++ = __onebyte(i2c, 0xFF);
__onebit(i2c, 0); // ACK
}
__stop(i2c);
return i;
}