~foura/uxn900

ref: a8ae1f98345ad57037f19ac626426572d551a513 uxn900/src/i2c.c -rw-r--r-- 4.8 KiB
a8ae1f98james palmer i2c: set master and transmit bits in i2c_initiate_transfer. 6 months ago
                                                                                
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
233
234
235
236
237
238
#include "uxn.h"
#include "dat.h"
#include "fns.h"

#include "dev/console.h"

enum {
	Rrev     = 0x00,
	Rie      = 0x04,
		Riexrdy = 1 << 4, /* tx ready */
		Rierrdy = 1 << 3, /* rx ready */
		Rieardy = 1 << 2, /* register access ready */
		Rienack = 1 << 1, /* no acknowledgment */
		Rieal   = 1 << 0, /* arbitration lost */
	Rstat    = 0x08,
		Rstatsbd  = 1 << 15, /* single byte data */
		Rstatbb   = 1 << 12, /* bus busy */
		Rstatrovr = 1 << 11, /* receive overrun */
		Rstatxudf = 1 << 10, /* transmit underflow */
		Rstataas  = 1 << 9,  /* address as slave */
		Rstatxrdy = 1 << 4, /* tx ready */
		Rstatrrdy = 1 << 3, /* rx ready */
		Rstatardy = 1 << 2, /* register access ready */
		Rstatnack = 1 << 1, /* no acknowledgment */
		Rstatal   = 1 << 0, /* arbitration lost */
	Rwe      = 0x0c,
	Rsyss    = 0x10,
		Rsyssrdone = 1 << 0, /* reset monitoring */
	Rbuf     = 0x14,
		Rbufrdma = 1 << 15,  /* recieve dma */
		Rbufxdma = 1 << 7,   /* transmit dma */
	Rcnt     = 0x18,
	Rdata    = 0x1c,
	Rsysc    = 0x20,
	Rcon     = 0x24,
		Rconen  = 1 << 15, /* module enable */
		Rconbe  = 1 << 14, /* big endian mode */
		Rconstb = 1 << 11, /* start byte mode */
		Rconmst = 1 << 10, /* master/slave mode */
		Rcontrx = 1 << 9,  /* transmitter/receiver mode */
		Rconxa  = 1 << 8,  /* expand address */
		Rconstp = 1 << 1,  /* stop condition */
		Rconstt = 1 << 0,  /* start condition */
	Roa      = 0x28,
	Rsa      = 0x2c,
	Rpsc     = 0x30,
	Rscll    = 0x34,
	Rsclh    = 0x38,
	Rsystest = 0x3c,
	Rbufstat = 0x40,
};

static Uint32
i2c_reg_read(Uint32 base, Uint8 r)
{
	volatile Uint32* reg = (Uint32*) (base + r);
	return *reg;
}

static void
i2c_reg_write(Uint32 base, Uint8 r, Uint32 value)
{
	volatile Uint32* reg = (Uint32*) (base + r);
	*reg = value;
}

static void
i2c_wait_for_bus(Uint32 base)
{
	while(i2c_reg_read(base, Rstat) & Rstatbb)
		;
}

static void
i2c_initiate_transfer(Uint32 base)
{
	Uint32 tmp;

	tmp = i2c_reg_read(base, Rcon);

	tmp |= Rconmst;
	tmp |= Rconstt;
	tmp |= Rconstp;
	tmp |= Rcontrx;

	i2c_reg_write(base, Rcon, tmp);
}

static void
i2c_flush_fifo(Uint32 base)
{
	Uint16 stat;

	for(;;) {
		stat = i2c_reg_read(base, Rstat);
		if(stat == Rstatrrdy) {
			i2c_reg_read(base, Rdata);
			i2c_reg_write(base, Rstat, Rstatrrdy);
			timer_delay(PHYSTIMER2, 1000);
		} else {
			break;
		}
	}
}

static Uint32
i2c_wait_for_event(Uint32 base)
{
	int timeout;
	Uint32 status;

	timeout = 1000;
	do {
		status = i2c_reg_read(base, Rstat);
		timer_delay(PHYSTIMER2, 10);
	} while(status == 0 && timeout--);

	if(timeout <= 0) {
		console_puts("i2c: timeout waiting for event\n");
		i2c_reg_write(base, Rstat, 0xFFFF);
	}

	return status;
}

void
i2c_init(Uint32 base)
{
	Uint32 tmp;

	if(i2c_reg_read(base, Rcon) & Rconen) {
		i2c_reg_write(base, Rcon, 0);
		timer_delay(PHYSTIMER2, 50000);
	}

	i2c_reg_write(base, Rsysc, 0x2);
	timer_delay(PHYSTIMER2, 1000);

	i2c_reg_write(base, Rcon, Rconen);
	while(!(i2c_reg_read(base, Rsyss) & 1))
		timer_delay(PHYSTIMER2, 1000);

	i2c_reg_write(base, Roa, 0x11);
	i2c_reg_write(base, Rie, Riexrdy
		| Rierrdy	
		| Rieardy
		| Rienack
		| Rieal);

	timer_delay(PHYSTIMER2, 1000);
	i2c_flush_fifo(base);
	i2c_reg_write(base, Rstat, 0xFFFF);
	i2c_reg_write(base, Rbuf, 0);

	tmp = i2c_reg_read(base, Rcon);
	tmp |= Rconen;
	tmp |= Rconmst;
	tmp |= Rcontrx;

	i2c_reg_write(base, Rcon, tmp);
}

void
i2c_write(Uint32 base, Uint8 chip, Uint8 *data, Uint8 count)
{
	Uint32 status;

	i2c_reg_write(base, Rsa, chip);
	i2c_reg_write(base, Rcnt, count);

	i2c_wait_for_bus(base);
	i2c_initiate_transfer(base);
	while(count) {
		status = i2c_wait_for_event(base);
		if(status == 0) {
			console_puts("i2c: status register zero after event\n");
			goto error;
		}

		if(status & Rstatnack) {
			console_puts("i2c: got no acknowledgement\n");
			goto error;
		}

		if(status & Rstatxrdy) {
			i2c_reg_write(base, Rdata, *data++);
			i2c_reg_write(base, Rstat, Rstatxrdy);
			count--;
		} else {
			console_puts("i2c: was not ready to transmit\n");
			goto error;
		}
	}

	do {
		status = i2c_wait_for_event(base);
	} while(!(status & Rstatardy));

error:
	i2c_flush_fifo(base);
	i2c_reg_write(base, Rstat, 0xFFFF);
}

void
i2c_read(Uint32 base, Uint8 chip, Uint8 *data, Uint8 count)
{
	Uint32 status;

	i2c_reg_write(base, Rsa, chip);
	i2c_reg_write(base, Rcnt, count);

	i2c_wait_for_bus(base);
	i2c_initiate_transfer(base);
	while(count) {
		status = i2c_wait_for_event(base);
		if(status == 0) {
			console_puts("i2c: status register zero after event\n");
			goto error;
		}

		if(status & Rstatnack) {
			console_puts("i2c: got no acknowledgement\n");
			goto error;
		}

		if(status & Rstatrrdy) {
			*data++ = i2c_reg_read(base, Rdata);
			i2c_reg_write(base, Rstat, Rstatrrdy);
		} else {
			console_puts("i2c: was not ready to receive\n");
			goto error;
		}
	}

error:
	i2c_flush_fifo(base);
	i2c_reg_write(base, Rstat, 0xFFFF);
}