summaryrefslogtreecommitdiff
path: root/i2c-bus.c
blob: c75179e7e2818fd55fc98104156a3d7714fb02ba (plain)
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
// Copyright 2009  Segher Boessenkool  <segher@kernel.crashing.org>
// Licensed under the terms of the GNU GPL, version 2
// http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt

#include "types.h"
#include <stdlib.h>

#include "i2c.h"


static void i2c_bus_find_device(struct i2c_bus *bus, u8 address)
{
	struct i2c_device *dev;

	for (dev = bus->devices; dev; dev = dev->next)
		if (dev->address(dev, address) == 0)
			break;

	bus->device = dev;
}

static int i2c_bus_write(struct i2c_bus *bus, u8 data)
{
	struct i2c_device *dev = bus->device;
	return dev->write(dev, data);
}

static u8 i2c_bus_read(struct i2c_bus *bus)
{
	struct i2c_device *dev = bus->device;
	return dev->read(dev);
}

static void i2c_bus_read_ack(struct i2c_bus *bus, int ack)
{
	struct i2c_device *dev = bus->device;
	if (dev->read_ack)
		dev->read_ack(dev, ack);
}


struct i2c_bitbang_bus {
	struct i2c_bus bus;

	void (*clock)(struct i2c_bitbang_bus *);

	int sda, scl, sda_dev;
	int dir;
	u8 bits;
	int nbits;
};

struct i2c_bus *i2c_bitbang_bus_create(void)
{
	return calloc(sizeof(struct i2c_bitbang_bus), 1);
}

static void clock_write(struct i2c_bitbang_bus *bb);

static void clock_write_ack(struct i2c_bitbang_bus *bb)
{
	if (bb->sda_dev == 0) {
		bb->clock = clock_write;
		bb->bits = 0;
		bb->nbits = 0;
		bb->sda_dev = 1;
	} else {
		bb->clock = 0;
	}
}

static void clock_write(struct i2c_bitbang_bus *bb)
{
	bb->bits = 2*bb->bits + bb->sda;
	bb->nbits++;
	if (bb->nbits == 8) {
		bb->sda_dev = i2c_bus_write(&bb->bus, bb->bits);
		bb->clock = clock_write_ack;
	}
}

static void clock_read(struct i2c_bitbang_bus *bb);

static void clock_read_ack(struct i2c_bitbang_bus *bb)
{
	i2c_bus_read_ack(&bb->bus, bb->sda);
	bb->clock = clock_read;
	if (bb->sda == 0) {
		bb->bits = i2c_bus_read(&bb->bus);
		bb->nbits = 0;
		bb->sda_dev = bb->bits >> 7;
		bb->clock = clock_read;
	} else
		bb->clock = 0;
}

static void clock_read(struct i2c_bitbang_bus *bb)
{
	bb->bits = 2*bb->bits;
	bb->sda_dev = bb->bits >> 7;
	bb->nbits++;
	if (bb->nbits == 8) {
		bb->sda_dev = 1;
		bb->clock = clock_read_ack;
	}
}

static void clock_address_ack(struct i2c_bitbang_bus *bb)
{
	if (bb->dir == 0) {
		bb->clock = clock_write;
		bb->bits = 0;
		bb->nbits = 0;
		bb->sda_dev = 1;
	} else {
		bb->clock = clock_read;
		bb->bits = i2c_bus_read(&bb->bus);
		bb->nbits = 0;
		bb->sda_dev = bb->bits >> 7;
	}
}

static void clock_address(struct i2c_bitbang_bus *bb)
{
	bb->bits = 2*bb->bits + bb->sda;
	bb->nbits++;
	if (bb->nbits == 8) {
		i2c_bus_find_device(&bb->bus, bb->bits);
		if (bb->bus.device == 0) {
			bb->clock = 0;
			return;
		}
		bb->sda_dev = 0;
		bb->dir = bb->bits & 1;
		bb->clock = clock_address_ack;
	}
}

int i2c_bitbang(struct i2c_bus *bus, int sda, int scl)
{
	struct i2c_bitbang_bus *bb = subtype(struct i2c_bitbang_bus, bus, bus);
	int old_sda = bb->sda;
	int old_scl = bb->scl;
	bb->sda = sda;
	bb->scl = scl;

	if (old_scl == 1 && scl == 1) {
		if (old_sda == 1 && sda == 0) {		// START
			bb->clock = clock_address;
			bb->bits = 0;
			bb->nbits = -1;
			bb->sda_dev = 1;
			bus->device = 0;
		}
		if (old_sda == 0 && sda == 1) {		// STOP
			bb->clock = 0;
			bb->sda_dev = 1;
		}
	}

	if (old_scl == 1 && scl == 0 && bb->clock)
		bb->clock(bb);

	return sda & bb->sda_dev;
}