Subversion Repositories group.electronics

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
117 pfowler 1
#include <avr/io.h>
2
#include "wire.h"
3
#include "twi.h"
4
 
5
void i2c_master(void) {
6
	wire.rxBufferIndex = 0;
7
	wire.rxBufferLength = 0;
8
 
9
	wire.txBufferIndex = 0;
10
	wire.txBufferLength = 0;
11
 
12
	twi_init();
13
}
14
 
15
void i2c_slave(uint8_t address) {
16
	twi_setAddress(address);
17
	twi_attachSlaveTxEvent(&i2c_onRequestService);
18
	twi_attachSlaveRxEvent(&i2c_onReceiveService);
19
	i2c_master();
20
}
21
 
22
uint8_t i2c_requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop) {
23
	if (quantity > MAX_BUFFER) {
24
		quantity = MAX_BUFFER;
25
	}
26
 
27
	uint8_t read = twi_readFrom(address, wire.rxBuffer, quantity, sendStop);
28
	wire.rxBufferIndex = 0;
29
	wire.rxBufferLength = read;
30
 
31
	return read;
32
}
33
 
34
void i2c_beginTransmission(uint8_t address) {
35
	wire.transmitting = 1;
36
	wire.txAddress = address;
37
	wire.txBufferIndex = 0;
38
	wire.txBufferLength = 0;
39
}
40
 
41
uint8_t i2c_endTransmission(uint8_t sendStop) {
42
	int8_t ret = twi_writeTo(wire.txAddress, 
43
					wire.txBuffer, 
44
					wire.txBufferLength, 
45
					1, sendStop);
46
	wire.txBufferIndex = 0;
47
	wire.txBufferLength = 0;
48
	wire.transmitting = 0;
49
	return ret;
50
}
51
 
52
uint8_t i2c_writeByte(uint8_t data) {
53
	if (wire.transmitting) {
54
		if (wire.txBufferLength >= MAX_BUFFER) {
55
			return 0;
56
		}
57
 
58
		wire.txBuffer[wire.txBufferIndex] = data;
59
		++wire.txBufferIndex;
60
		wire.txBufferLength = wire.txBufferIndex;
61
	} else {
62
		twi_transmit(&data, 1);
63
	}
64
	return 1;
65
}
66
 
67
uint8_t i2c_writeBytes(uint8_t *data, uint8_t bytes) {
68
	if (wire.transmitting) {
69
		uint8_t i = 0;
70
		for (i = 0; i < bytes; i++) {
71
			i2c_writeByte(data[i]);
72
		}
73
	} else {
74
		twi_transmit(data, bytes);
75
	}
76
	return bytes;
77
}
78
 
79
 
80
int i2c_available(void) {
81
	return wire.rxBufferLength - wire.rxBufferIndex;
82
}
83
 
84
uint8_t i2c_read(void) {
85
	int value = 0;
86
	if (wire.rxBufferIndex < wire.rxBufferLength) {
87
		value = wire.rxBuffer[wire.rxBufferIndex];
88
		++wire.rxBufferIndex;
89
	}
90
	return value;
91
}
92
 
93
void i2c_onReceiveService(uint8_t* inBytes, int numBytes) {
94
	if(!wire.user_onReceive) {
95
		return;
96
	}
97
 
98
	if(wire.rxBufferIndex < wire.rxBufferLength) {
99
		return;
100
	}
101
 
102
	uint8_t i = 0;
103
	for(i = 0; i < numBytes; ++i) {
104
		wire.rxBuffer[i] = inBytes[i];
105
	}
106
 
107
	wire.rxBufferIndex = 0;
108
	wire.rxBufferLength = numBytes;
109
	wire.user_onReceive(numBytes);
110
}
111
 
112
void i2c_onRequestService(void) {
113
	if(!wire.user_onRequest) {
114
		return;
115
	}
116
 
117
	wire.txBufferIndex = 0;
118
	wire.txBufferLength = 0;
119
	wire.user_onRequest();
120
}
121
 
122
void i2c_onReceive( void(*function)(uint8_t)) {
123
	wire.user_onReceive = function;
124
}
125
 
126
void i2c_onRequest( void(*function)(void)) {
127
	wire.user_onRequest = function;
128
}