-
Notifications
You must be signed in to change notification settings - Fork 14
/
usitwislave.c
450 lines (355 loc) · 9.87 KB
/
usitwislave.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/* See LICENSE for Copyright etc. */
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include "usitwislave_devices.h"
#include "usitwislave.h"
enum
{
of_state_check_address,
of_state_send_data,
of_state_request_ack,
of_state_check_ack,
of_state_receive_data,
of_state_store_data_and_send_ack
} overflow_state_t;
enum
{
ss_state_before_start,
ss_state_after_start,
ss_state_address_selected,
ss_state_address_not_selected,
ss_state_data_processed
} startstop_state_t;
static void (*idle_callback)(void);
static void (*data_callback)(uint8_t input_buffer_length, const uint8_t *input_buffer,
uint8_t *output_buffer_length, uint8_t *output_buffer);
static uint8_t of_state;
static uint8_t ss_state;
static uint8_t slave_address;
static uint8_t input_buffer[USI_TWI_BUFFER_SIZE];
static uint8_t input_buffer_length;
static uint8_t output_buffer[USI_TWI_BUFFER_SIZE];
static uint8_t output_buffer_length;
static uint8_t output_buffer_current;
static uint8_t stats_enabled;
static uint16_t start_conditions_count;
static uint16_t stop_conditions_count;
static uint16_t error_conditions_count;
static uint16_t overflow_conditions_count;
static uint16_t local_frames_count;
static uint16_t idle_call_count;
static void set_sda_to_input(void)
{
DDR_USI &= ~_BV(PORT_USI_SDA);
}
static void set_sda_to_output(void)
{
DDR_USI |= _BV(PORT_USI_SDA);
}
static inline void set_scl_to_input(void)
{
DDR_USI &= ~_BV(PORT_USI_SCL);
}
static inline void set_scl_to_output(void)
{
DDR_USI |= _BV(PORT_USI_SCL);
}
static inline void set_sda_low(void)
{
PORT_USI &= ~_BV(PORT_USI_SDA);
}
static inline void set_sda_high(void)
{
PORT_USI |= _BV(PORT_USI_SDA);
}
static inline void set_scl_low(void)
{
PORT_USI &= ~_BV(PORT_USI_SCL);
}
static inline void set_scl_high(void)
{
PORT_USI |= _BV(PORT_USI_SCL);
}
static inline void twi_reset_state(void)
{
USISR =
(1 << USISIF) | // clear start condition flag
(1 << USIOIF) | // clear overflow condition flag
(0 << USIPF) | // !clear stop condition flag
(1 << USIDC) | // clear arbitration error flag
(0x00 << USICNT0); // set counter to "8" bits
USICR =
(1 << USISIE) | // enable start condition interrupt
(0 << USIOIE) | // !enable overflow interrupt
(1 << USIWM1) | (0 << USIWM0) | // set usi in two-wire mode, disable bit counter overflow hold
(1 << USICS1) | (0 << USICS0) | (0 << USICLK) | // shift register clock source = external, positive edge, 4-bit counter source = external, both edges
(0 << USITC); // don't toggle clock-port pin
}
static void twi_reset(void)
{
// make sure no sda/scl remains pulled up or down
set_sda_to_input(); // deactivate internal pullup on sda/scl
set_sda_low();
set_scl_to_input();
set_scl_low();
set_sda_to_output(); // release (set high) on sda/scl
set_sda_high();
set_sda_to_input();
set_sda_high();
set_scl_to_output();
set_scl_high();
twi_reset_state();
}
static inline void twi_init(void)
{
#if defined(USIPP)
#if defined(USI_ON_PORT_A)
USIPP |= _BV(USIPOS);
#else
USIPP &= ~_BV(USIPOS);
# endif
#endif
twi_reset();
}
ISR(USI_START_vect)
{
set_sda_to_input();
// wait for SCL to go low to ensure the start condition has completed (the
// start detector will hold SCL low) - if a stop condition arises then leave
// the interrupt to prevent waiting forever - don't use USISR to test for stop
// condition as in Application Note AVR312 because the stop condition Flag is
// going to be set from the last TWI sequence
while(!(PIN_USI & _BV(PIN_USI_SDA)) &&
(PIN_USI & _BV(PIN_USI_SCL)))
// possible combinations
// sda = low scl = low break start condition
// sda = low scl = high loop
// sda = high scl = low break stop condition
// sda = high scl = high break stop condition
if((PIN_USI & _BV(PIN_USI_SDA))) // stop condition
{
twi_reset();
if(stats_enabled)
error_conditions_count++;
return;
}
if(stats_enabled)
start_conditions_count++;
of_state = of_state_check_address;
ss_state = ss_state_after_start;
USIDR = 0xff;
USICR =
(1 << USISIE) | // enable start condition interrupt
(1 << USIOIE) | // enable overflow interrupt
(1 << USIWM1) | (1 << USIWM0) | // set usi in two-wire mode, enable bit counter overflow hold
(1 << USICS1) | (0 << USICS0) | (0 << USICLK) | // shift register clock source = external, positive edge, 4-bit counter source = external, both edges
(0 << USITC); // don't toggle clock-port pin
USISR =
(1 << USISIF) | // clear start condition flag
(1 << USIOIF) | // clear overflow condition flag
(0 << USIPF) | // !clear stop condition flag
(1 << USIDC) | // clear arbitration error flag
(0x00 << USICNT0); // set counter to "8" bits
}
ISR(USI_OVERFLOW_VECTOR)
{
// bit shift register overflow condition occured
// scl forced low until overflow condition is cleared!
uint8_t data = USIDR;
uint8_t set_counter = 0x00; // send 8 bits (16 edges)
if(stats_enabled)
overflow_conditions_count++;
again:
switch(of_state)
{
// start condition occured and succeed
// check address, if not OK, reset usi
// note: not using general call address
case(of_state_check_address):
{
uint8_t address;
uint8_t direction;
direction = data & 0x01;
address = (data & 0xfe) >> 1;
if(address == slave_address)
{
ss_state = ss_state_address_selected;
if(direction) // read request from master
of_state = of_state_send_data;
else // write request from master
of_state = of_state_receive_data;
USIDR = 0x00;
set_counter = 0x0e; // send 1 bit (2 edges)
set_sda_to_output(); // initiate send ack
}
else
{
USIDR = 0x00;
set_counter = 0x00;
twi_reset_state();
ss_state = ss_state_address_not_selected;
}
break;
}
// process read request from master
case(of_state_send_data):
{
ss_state = ss_state_data_processed;
of_state = of_state_request_ack;
if(output_buffer_current < output_buffer_length)
USIDR = output_buffer[output_buffer_current++];
else
USIDR = 0x00; // no more data, but cannot send "nothing" or "nak"
set_counter = 0x00;
set_sda_to_output(); // initiate send data
break;
}
// data sent to master, request ack (or nack) from master
case(of_state_request_ack):
{
of_state = of_state_check_ack;
USIDR = 0x00;
set_counter = 0x0e; // receive 1 bit (2 edges)
set_sda_to_input(); // initiate receive ack
break;
}
// ack/nack from master received
case(of_state_check_ack):
{
if(data) // if NACK, the master does not want more data
{
of_state = of_state_check_address;
set_counter = 0x00;
twi_reset();
}
else
{
of_state = of_state_send_data;
goto again; // from here we just drop straight into state_send_data
} // don't wait for another overflow interrupt
break;
}
// process write request from master
case(of_state_receive_data):
{
ss_state = ss_state_data_processed;
of_state = of_state_store_data_and_send_ack;
set_counter = 0x00; // receive 1 bit (2 edges)
set_sda_to_input(); // initiate receive data
break;
}
// data received from master, store it and wait for more data
case(of_state_store_data_and_send_ack):
{
of_state = of_state_receive_data;
if(input_buffer_length < (USI_TWI_BUFFER_SIZE - 1))
input_buffer[input_buffer_length++] = data;
USIDR = 0x00;
set_counter = 0x0e; // send 1 bit (2 edges)
set_sda_to_output(); // initiate send ack
break;
}
}
USISR =
(0 << USISIF) | // don't clear start condition flag
(1 << USIOIF) | // clear overflow condition flag
(0 << USIPF) | // don't clear stop condition flag
(1 << USIDC) | // clear arbitration error flag
(set_counter << USICNT0); // set counter to 8 or 1 bits
}
void usi_twi_slave(uint8_t slave_address_in, uint8_t use_sleep,
void (*data_callback_in)(uint8_t input_buffer_length, const uint8_t *input_buffer,
uint8_t *output_buffer_length, uint8_t *output_buffer),
void (*idle_callback_in)(void))
{
uint8_t call_datacallback = 0;
slave_address = slave_address_in;
data_callback = data_callback_in;
idle_callback = idle_callback_in;
input_buffer_length = 0;
output_buffer_length = 0;
output_buffer_current = 0;
ss_state = ss_state_before_start;
if(use_sleep)
set_sleep_mode(SLEEP_MODE_IDLE);
twi_init();
sei();
for(;;)
{
if(idle_callback)
{
idle_callback();
if(stats_enabled)
idle_call_count++;
}
if(use_sleep && (ss_state == ss_state_before_start))
sleep_mode();
if(USISR & _BV(USIPF))
{
cli();
if(stats_enabled)
stop_conditions_count++;
USISR |= _BV(USIPF); // clear stop condition flag
switch(ss_state)
{
case(ss_state_after_start):
{
twi_reset();
break;
}
case(ss_state_data_processed):
{
if(stats_enabled)
local_frames_count++;
call_datacallback = 1;
break;
}
}
ss_state = ss_state_before_start;
sei();
}
if(call_datacallback)
{
output_buffer_length = 0;
output_buffer_current = 0;
data_callback(input_buffer_length, input_buffer, &output_buffer_length, output_buffer);
input_buffer_length = 0;
call_datacallback = 0;
}
}
}
void usi_twi_enable_stats(uint8_t onoff)
{
stats_enabled = onoff;
start_conditions_count = 0;
stop_conditions_count = 0;
error_conditions_count = 0;
overflow_conditions_count = 0;
local_frames_count = 0;
idle_call_count = 0;
}
uint16_t usi_twi_stats_start_conditions(void)
{
return(start_conditions_count);
}
uint16_t usi_twi_stats_stop_conditions(void)
{
return(stop_conditions_count);
}
uint16_t usi_twi_stats_error_conditions(void)
{
return(error_conditions_count);
}
uint16_t usi_twi_stats_overflow_conditions(void)
{
return(overflow_conditions_count);
}
uint16_t usi_twi_stats_local_frames(void)
{
return(local_frames_count);
}
uint16_t usi_twi_stats_idle_calls(void)
{
return(idle_call_count);
}