-
Notifications
You must be signed in to change notification settings - Fork 0
/
avr_i2c.c
228 lines (203 loc) · 7.94 KB
/
avr_i2c.c
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
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/sleep.h>
#include <stdio.h>
#include <inttypes.h>
#include <stdbool.h>
#include "avr_i2c.h"
// Slave Receive mode (Master writes, slave reads)
#define I2C_SR_START 0x60 // Own SLA+W has been received; ACK has been returned
#define I2C_SR_DATA 0x80 // Previously addressed with own SLA+W; data has been received; ACK has been returned
#define I2C_SR_DATA_NACK 0x88 // Previously addressed with own SLA+W; data has been received; NOT ACK has been returned
#define I2C_SR_STOP 0xA0 // A STOP condition or repeated START condition has been received while still addressed as Slave
// Slave Transmit mode (Master reads, slave writes)
#define I2C_ST_START 0xA8 // Own SLA+R has been received; ACK has been returned
#define I2C_ST_WROTE 0xB8 // Data byte in TWDR has been transmitted; ACK has been recieved
#define I2C_ST_WROTE_NACK 0xC0 // Data byte in TWDR has been transmitted; NOT ACK has been recieved
volatile uint8_t i2c_in_buffer[32];
volatile int i2c_in_ptr = 0;
volatile uint8_t i2c_out_buffer[32];
volatile int i2c_out_ptr = 0;
volatile int i2c_out_len = 0;
volatile bool i2c_out_data_ready = false;
volatile bool i2c_N_bytes_sent = false;
#ifdef DEBUG_I2C
volatile uint8_t saved_status_codes[32];
volatile int saved_status_code_ptr = 0;
#endif
volatile uint8_t debug = 0;
void I2C_init(uint8_t slave_address) {
TWAR = slave_address << 1; // Set slave address
// Start Slave Listening: Clear TWINT Flag, Enable ACK, Enable TWI, TWI Interrupt Enable
TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN) | (1<<TWIE);
// Default initial values
TWDR = 0x00;
i2c_new_data = false;
}
void I2C_reset() {
i2c_out_data_ready = false;
i2c_N_bytes_sent = false;
TWDR = 0x00;
TWCR |= (1<<TWINT |1<<TWSTO);
}
inline void debug_save_status_code(uint8_t status_code) {
# ifdef DEBUG_I2C
saved_status_codes[saved_status_code_ptr++] = status_code;
# endif
}
void I2C_pack_one(uint16_t message_name, uint16_t message) {
cli();
uint16_t message_names[1] = {message_name};
uint16_t messages[1] = {message};
I2C_pack(message_names, messages, 1);
}
void I2C_pack(uint16_t *message_names, uint16_t *messages, int len) {
cli();
if (!i2c_out_data_ready) {
i2c_out_len = 0;
for (int i=0; i<len; ++i) {
uint8_t name0 = (uint8_t)((message_names[i] & 0xff00) >> 8);
uint8_t name1 = (uint8_t)(message_names[i] & 0x00ff);
uint8_t data0 = (uint8_t)((messages[i] & 0xff00) >> 8);
uint8_t data1 = (uint8_t)(messages[i] & 0x00ff);
i2c_out_buffer[4*i + 0] = name0;
i2c_out_buffer[4*i + 1] = name1;
i2c_out_buffer[4*i + 2] = data0;
i2c_out_buffer[4*i + 3] = data1;
i2c_out_len += 4;
}
i2c_out_data_ready = true;
} else {
// There is already some data in the buffer
// If the new data has same message_name, replace the data
// if the message_name doesn't exist add it
uint8_t temp_buffer[28];
int temp_buffer_ptr = 0;
for (int i=0; i<len; ++i) {
uint8_t new_name0 = (uint8_t)(message_names[i] >> 8);
uint8_t new_name1 = (uint8_t)(message_names[i] & 0x00ff);
uint8_t new_data0 = (uint8_t)(messages[i] >> 8);
uint8_t new_data1 = (uint8_t)(messages[i] & 0x00ff);
bool found = false;
for (int j=0; j<i2c_out_len; ++j) {
uint8_t buffer_name0 = i2c_out_buffer[4*j + 0];
uint8_t buffer_name1 = i2c_out_buffer[4*j + 1];
if ((new_name0 == buffer_name0) && (new_name1 == buffer_name1)) {
found = true;
// Overwrite data
i2c_out_buffer[4*j + 2] = new_data0;
i2c_out_buffer[4*j + 3] = new_data1;
break; // this name should only come once
}
}
if (!found) {
// Add to temporary buffer
temp_buffer[temp_buffer_ptr++] = new_name0;
temp_buffer[temp_buffer_ptr++] = new_name1;
temp_buffer[temp_buffer_ptr++] = new_data0;
temp_buffer[temp_buffer_ptr++] = new_data1;
}
}
// Write the new bytes to the buffer
for (int i=0; i<temp_buffer_ptr; ++i) {
i2c_out_buffer[i2c_out_len++] = temp_buffer[i];
}
}
sei();
}
int I2C_unpack(uint16_t *message_names, uint16_t *messages) {
// The arrays must be long enough for all possible message types
// Use length 16 to be safe
i2c_new_data = false;
int msg_idx = 0;
// Translate the 8 bit packages to 16 bit data, and put them in the place for
for (int i=0; i<i2c_in_ptr/2; i++) {
uint16_t packet = (uint16_t)(i2c_in_buffer[2*i] << 8) | i2c_in_buffer[2*i+1];
if ((packet & 0xfff0) == 0xfff0) {
// Message name
message_names[msg_idx] = packet;
} else {
// Message
messages[msg_idx++] = packet;
}
}
i2c_in_ptr = 0;
return msg_idx;
}
ISR(TWI_vect) {
uint8_t status_code;
status_code = TWSR & 0xf8;
debug_save_status_code(status_code);
switch (status_code) {
// Slave Receive Mode
case I2C_SR_START:
// SLA+W has been received (slave is now reading)
TWCR |= (1<<TWINT) | (1<<TWEA) | (1<<TWIE);
break;
case I2C_SR_DATA:
// Data package received. Add to buffer
i2c_in_buffer[i2c_in_ptr++] = TWDR;
TWCR |= (1<<TWINT) | (1<<TWEA) | (1<<TWIE);
break;
case I2C_SR_STOP:
// Last byte received
i2c_new_data = true;
TWCR |= (1<<TWINT) | (1<<TWIE);
break;
// Slave Transmit mode
case I2C_ST_START:
if (!i2c_N_bytes_sent) {
if (i2c_out_data_ready) {
// Transmit number of bytes
TWDR = i2c_out_len;
TWCR |= (1<<TWINT) | (1<<TWIE);
} else {
// Not ready, transmit 0 bytes
TWDR = 0;
TWCR |= (1<<TWINT) | (1<<TWIE);
}
} else {
// Transmit first byte
i2c_out_ptr = 0;
TWDR = i2c_out_buffer[i2c_out_ptr++];
TWCR |= (1<<TWINT) | (1<<TWEA) | (1<<TWIE);
}
break;
case I2C_ST_WROTE:
// Byte written, ACK received
// Write another byte
if (i2c_out_ptr < i2c_out_len - 1) {
TWDR = i2c_out_buffer[i2c_out_ptr++];
TWCR |= (1<<TWINT) | (1<<TWEA) | (1<<TWIE);
} else if (i2c_out_ptr == i2c_out_len - 1) {
// Last byte
TWDR = i2c_out_buffer[i2c_out_ptr++];
TWCR |= (1<<TWINT) | (1<<TWIE);
} else {
// This is unexpected, try to reset
debug |= 0x02;
I2C_reset();
}
break;
case I2C_ST_WROTE_NACK:
// Byte written, NACK received
if (!i2c_N_bytes_sent) {
i2c_N_bytes_sent = true;
TWCR |= (1<<TWINT) | (1<<TWEA) | (1<<TWIE);
} else if (i2c_out_ptr == i2c_out_len) {
i2c_N_bytes_sent = false;
i2c_out_data_ready = false;
TWCR |= (1<<TWINT) | (1<<TWEA) | (1<<TWIE);
} else {
// This is unexpected, try to reset
debug |= 0x04;
I2C_reset();
}
break;
default:
// This should not happen, try to reset
debug |= 0x1;
I2C_reset();
break;
}
}