summaryrefslogtreecommitdiffstats
path: root/firmware/twi_master.c
blob: dccc814ded5e5c594e0935bbc401275adb777c81 (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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * TWI-master Medium Access Control
 *
 * Copyright (c) 2013 Michael Buesch <m@bues.ch>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "twi_master.h"
#include "twi_master_sync.h"
#include "util.h"

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/twi.h>

#include <string.h>


#ifndef TWI_SCL_HZ
# warning "No TWI_SCL_HZ defined. Defaulting to 100 KHz."
# define TWI_SCL_HZ	100000ul
#endif

#define TWI_SYNC


enum twi_transfer_status_flags {
	TWI_XFER_READ		= 0x80,

	TWI_XFER_STAT_MASK	= 0x0F,
	TWI_XFER_STAT_SHIFT	= 0,
};

struct twi_context {
	struct twi_transfer *first_xfer;
	struct twi_transfer *last_xfer;
};

static struct twi_context twi;


static inline void TWCR_write(uint8_t additional_flags)
{
	mb();
	TWCR = (1 << TWEN) | (1 << TWINT) |  additional_flags;
}

static void send_start_condition(void)
{
	/* Send start condition. */
	TWCR_write((1 << TWIE) | (1 << TWSTA));
}

static void send_stop_condition(void)
{
	/* Send stop condition. */
	TWCR_write(1 << TWSTO);
}

static void transfer_set_status(struct twi_transfer *xfer,
				enum twi_status status)
{
	xfer->status = (xfer->status & ~TWI_XFER_STAT_MASK) |
		       (status << TWI_XFER_STAT_SHIFT);
}

static enum twi_status transfer_get_status(const struct twi_transfer *xfer)
{
	return (xfer->status & TWI_XFER_STAT_MASK) >> TWI_XFER_STAT_SHIFT;
}

static void stop_transfer(struct twi_transfer *xfer,
			  enum twi_status new_status)
{
	send_stop_condition();

	transfer_set_status(xfer, new_status);

	twi.first_xfer = xfer->next;
	if (twi.first_xfer)
		send_start_condition();
	else
		twi.last_xfer = NULL;

	if (xfer->callback)
		xfer->callback(xfer, new_status);
}

static void handle_tw_status(struct twi_transfer *xfer, uint8_t twstat)
{
	uint8_t *buffer = xfer->buffer;

	switch (twstat) {
	default:
		stop_transfer(xfer, TWI_STAT_BUSERROR);
		break;
	case TW_START:
	case TW_REP_START:
		if (xfer->status & TWI_XFER_READ)
			TWDR = (xfer->address << 1) | 1;
		else
			TWDR = (xfer->address << 1);
		TWCR_write(1 << TWIE);
		break;
	case TW_MT_DATA_ACK:
	case TW_MR_DATA_ACK:
	case TW_MR_DATA_NACK:
	case TW_MT_SLA_ACK:
		if (xfer->status & TWI_XFER_READ) {
			buffer[xfer->offset++] = TWDR;

			if (xfer->offset == xfer->read_size) {
				stop_transfer(xfer, TWI_STAT_FINISHED);
			} else {
				if (xfer->offset + 1 == xfer->read_size)
					TWCR_write(1 << TWIE);
				else
					TWCR_write((1 << TWIE) | (1 << TWEA));
			}
		} else {
			if (xfer->offset == xfer->write_size) {
				if (xfer->read_size) {
					xfer->status |= TWI_XFER_READ;
					xfer->offset = 0;
					send_start_condition();
				} else
					stop_transfer(xfer, TWI_STAT_FINISHED);
			} else {
				TWDR = buffer[xfer->offset++];
				TWCR_write(1 << TWIE);
			}
		}
		break;
	case TW_MR_SLA_ACK:
		if (xfer->offset + 1 == xfer->read_size)
			TWCR_write(1 << TWIE);
		else
			TWCR_write((1 << TWIE) | (1 << TWEA));
		break;
	}
}

static void twi_interrupt_handler(void)
{
	struct twi_transfer *xfer;
	uint8_t twstat;

	mb();
	twstat = TW_STATUS;
	xfer = twi.first_xfer;
	if (xfer)
		handle_tw_status(xfer, twstat);
	mb();
}

ISR(TWI_vect)
{
	twi_interrupt_handler();
}

void twi_init(void)
{
#ifdef TWI_SYNC
	i2c_init();
#else
	memset(&twi, 0, sizeof(twi));
	TWSR = 0;
	TWBR = ((F_CPU / TWI_SCL_HZ) - 16) / 2;
	TWAR = 0;
#endif
}

void twi_transfer(struct twi_transfer *xfer)
{
#ifdef TWI_SYNC
	twi_size_t i;
	uint8_t *buffer = xfer->buffer;

	if (xfer->write_size) {
		i2c_start(xfer->address << 1);
		for (i = 0; i < xfer->write_size; i++)
			i2c_write(buffer[i]);
		if (!xfer->read_size)
			i2c_stop();
	}
	if (xfer->read_size) {
		i2c_start((xfer->address << 1) | 1);
		for (i = 0; i < xfer->read_size; i++) {
			if (i + 1 == xfer->read_size)
				buffer[i] = i2c_readNak();
			else
				buffer[i] = i2c_readAck();
		}
		i2c_stop();
	}
	if (xfer->callback)
		xfer->callback(xfer, TWI_STAT_FINISHED);

#else /* TWI_SYNC */

	uint8_t sreg;

	xfer->offset = 0;
	xfer->next = NULL;
	xfer->status = 0;
	if (!xfer->write_size)
		xfer->status |= TWI_XFER_READ;
	transfer_set_status(xfer, TWI_STAT_INPROGRESS);

	sreg = irq_disable_save();

	if (twi.last_xfer)
		twi.last_xfer->next = xfer;
	twi.last_xfer = xfer;
	if (!twi.first_xfer) {
		twi.first_xfer = xfer;
		send_start_condition();
	}
	irq_restore(sreg);
#endif
}

enum twi_status twi_transfer_get_status(const struct twi_transfer *xfer)
{
	enum twi_status status;
	uint8_t sreg;

#ifdef TWI_SYNC
	return TWI_STAT_FINISHED;
#endif

	sreg = irq_disable_save();
	status = transfer_get_status(xfer);
	irq_restore(sreg);

	return status;
}

enum twi_status twi_transfer_wait(struct twi_transfer *xfer,
				  uint16_t timeout_ms)
{
	enum twi_status status;
	uint32_t timeout = (uint32_t)timeout_ms * 100;

	while (1) {
		status = twi_transfer_get_status(xfer);
		if (status != TWI_STAT_INPROGRESS)
			break;
		if (timeout-- == 0) {
			twi_transfer_cancel(xfer);
			status = TWI_STAT_TIMEOUT;
			break;
		}
		_delay_us(10);
		if (!irqs_enabled()) {
			if (TWCR & (1 << TWINT))
				twi_interrupt_handler();
		}
	}

	return status;
}

void twi_transfer_cancel(struct twi_transfer *xfer)
{
	uint8_t sreg;

#ifdef TWI_SYNC
	return;
#endif

	sreg = irq_disable_save();
	if (transfer_get_status(xfer) == TWI_STAT_INPROGRESS)
		stop_transfer(xfer, TWI_STAT_CANCELLED);
	irq_restore(sreg);
}
bues.ch cgit interface