Subversion Repositories group.electronics

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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