-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathi2c.c
695 lines (575 loc) · 12.8 KB
/
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
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/**
@file hardware/i2c.c
@brief I2C Interrupt driven driver
Total rewrite
Reference AVR315 for technical details
@par Edit History
- [1.0] [Mike Gore] Initial revision of file.
@copyright Copyright © 2014-2020 Mike Gore, Inc. All rights reserved.
@copyright GNU Public License.
@author Mike Gore
*/
#include "user_config.h"
///@brief I2C interrupt state registers
i2c_op_t i2c;
///@brief I2C task list
///@ brief I2C callback function
/// Used when automattically sending several transactions
typedef int8_t (*i2c_callback_t)(void);
i2c_callback_t i2c_callback = (i2c_callback_t) NULL;
///@brief I2C task state
i2c_task_t i2c_task = { 0,0,0,0 };
i2c_op_t *i2c_task_op[I2C_OPS] = { NULL };
/// @brief Check if I2C structure pointer is valid
///
/// @param[in] index: index of i2c_task_op[] array
/// @return 1 if OK, 0 if NOT
uint8_t i2c_check_op(int8_t index)
{
if(index < 0 || index >= I2C_OPS)
{
printf("I2C op[INDEX %d >= %d]\n",(int) index, I2C_OPS);
return(0);
}
if(i2c_task_op[index] == NULL)
{
return(0);
}
if(i2c_task_op[index]->buf == NULL)
{
printf("I2C op[%d]->buf == NULL\n",(int) index);
return(0);
}
if(i2c_task_op[index]->len == 0)
{
printf("I2C op[%d]->len == 0\n",(int) index);
return(0);
}
return(1);
}
/// @brief Initialize I2C task op list
/// NOTE: We ASSUME all i2c_task_op[].buf are statically allocated
///
/// @return void
void i2c_task_init()
{
int8_t i;
uint8_t sreg = SREG;
cli();
for(i=0;i<I2C_OPS;++i)
i2c_task_op[i] = NULL;
i2c_task.enable = 0;
i2c_task.done = 1;
i2c_task.ind = 0;
i2c_task.error = 0;
SREG = sreg;
}
/// @brief Free all allocated memory for i2c_[] pointers
/// NOTE: We ASSUME all i2c_task_op[].buf are statically allocated
///
/// @return void
void i2c_task_free_ops()
{
i2c_op_t *o;
int8_t i;
uint8_t sreg = SREG;
cli();
for(i=0;i2c_check_op(i);++i)
{
o = i2c_task_op[i];
// We ASSUME the buffer is static and not allocated
o->buf = NULL;
o->done = 0;
o->enable = 0;
o->flags = 0;
o->timeout = 0;
o->ind = 0;
safefree(o);
i2c_task_op[i] = NULL;
}
// disable interrupts
TWCR = 0;
// disable slave mode
TWAR = 0;
// Reset status
TWSR &= ~(_BV(TWPS0) | _BV(TWPS1));
SREG = sreg;
}
///@brief I2C setup new OP but do not run it yet
///
/// @param[in] address: I2C address
/// @param[in] mode: TW_READ or TW_WRITE
/// @param[in] *buf: pointer to buffer for send or receive
/// @param[in] len: size of buffer to read or write
/// @return i2c_op_t * pointer
i2c_op_t *i2c_task_op_add(uint8_t address, uint8_t mode, uint8_t *buf, uint8_t len)
{
uint8_t sreg = SREG;
i2c_op_t *o;
o = safecalloc(1,sizeof(i2c_op_t));
if(o == NULL)
return(o);
cli();
o->enable = 0; // NOT enabled
o->done = 0;
o->address = (address << 1) | (mode & 1);
o->timeout = I2C_TIMEOUT;
o->flags = 0;
o->len = len;
o->ind = 0;
o->buf = buf;
SREG = sreg;
return(o);
}
///@brief I2C task ISR callback function
///
/// @return void
int8_t i2c_task_next_op()
{
i2c_op_t *o;
// NOTE: we are in an ISR do not disable other interrupts
if(i2c_task.enable)
{
// Save state of LAST operation
if(i2c_check_op(i2c_task.ind) )
{
o = i2c_task_op[i2c_task.ind];
// Save state in last opperation
*o = i2c;
if(o->flags)
i2c_task.error = 1;
}
if(i2c_check_op(i2c_task.ind+1) )
{
i2c_task.ind++;
o = i2c_task_op[i2c_task.ind];
if( o->enable == 1 || o->done == 0)
{
o->timeout = I2C_TIMEOUT;
o->flags = 0;
o->ind = 0;
o->enable = 1;
o->done = 0;
i2c = *o;
i2c_send_start();
return(1);
}
}
}
// program error
// Disable TASK
i2c_task.enable = 0;
i2c_task.done = 1;
// I2C disable
i2c.done = 1;
i2c.enable = 0;
i2c.flags = 0;
i2c.ind = 0;
i2c_send_stop();
return(0);
}
/// @brief Run all valid i2c_task_op[] tasks
///
/// @return void
void i2c_task_run()
{
uint8_t sreg = SREG;
i2c_op_t *o;
uint8_t run = 0;
int8_t i;
cli();
i2c_task.done = 0;
i2c_task.error = 0;
i2c_task.ind = 0;
// re-enable tasks
for(i=0;i2c_check_op(i);++i)
{
o = i2c_task_op[i];
o->enable = 1;
o->done = 0;
o->flags = 0;
o->ind = 0;
o->timeout = I2C_TIMEOUT;
if(!run)
{
run = 1;
i2c = *o;
}
}
if(run)
{
// TASK callback
i2c_callback = i2c_task_next_op;
i2c_task.enable = 1;
i2c_task.done = 0;
// Reset Status
TWSR &= ~(_BV(TWPS0) | _BV(TWPS1));
// Start a transactions
// TWI Enable
// TWI Interrupt Enable
// TWI Interrupt Clear
// TWI SEND RESTART
// TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWSTA);
// Disable Slave Mode
i2c_send_start();
TWAR = 0;
}
else
{
// FIXME we should notify the user ?
// Nothing to DO
// User Error
// TASK callback
i2c_callback = NULL;
// TASK Nothing to DO
i2c_task.enable = 0;
i2c_task.done = 1;
// I2C Nothing to do
i2c.done = 1;
i2c.enable = 0;
// TWI Enable
// TWI Disable Enable
// TWI Interrupt Clear
TWCR = _BV(TWEN) | _BV(TWINT);
// Reset status
TWSR &= ~(_BV(TWPS0) | _BV(TWPS1));
// Disable Slave Mode
TWAR = 0;
}
// Disable Slave Mode
SREG = sreg;
}
static int8_t i2c_init_status = 0;
/// =========================================================================
/// @brief I2C initialize
/// Clear all i2c_task_op[] pointers and disables I2C tasks
///
/// @return void
void i2c_init(uint32_t speed)
{
uint8_t sreg = SREG;
uint16_t rate = ((F_CPU / speed) - 16) / 2;
if(rate & 0xff00)
{
printf("i2c_init prescale overflow\n");
return;
}
cli();
TWBR = rate;
i2c.enable = 0; // NOT enabled
i2c.done = 1;
i2c_task.enable = 0;
i2c_task.done = 1;
GPIO_PIN_LATCH_HI(SCL); // Pull Up on
GPIO_PIN_LATCH_HI(SDA); // Pull Up on
// TWI Enable
// TWI Interrupt Disable
// TWI Interrupt Clear
TWCR = _BV(TWEN) | _BV(TWINT);
// Disable SLAVE
TWAR = 0;
// Reset Status
TWSR &= ~(_BV(TWPS0) | _BV(TWPS1));
if(!i2c_init_status)
{
int8_t i;
for(i=0;i<I2C_OPS;++i)
i2c_task_op[i] = NULL;
// Enable timer task to monitor timeouts
if(set_timers(i2c_timer,1) == -1)
printf("i2c_timer init failed\n");
i2c_init_status = 1;
}
/* Restore the status register. */
SREG = sreg;
}
///@brief check if I2C trasnaction detected an error
///@brief Did we get an error durring any i2c_task_op[] seand/receive operations
///
/// @return 1 OK, 0 NOT OK
int8_t i2c_ok()
{
if (i2c.flags)
return(0);
return(1);
}
///@brief Is i2c structure done sending/receiving ?
///
/// @return 1 DONE, 0 NOT DONE
int8_t i2c_done()
{
delayus(1);
if(!i2c.enable || i2c.done )
return(1);
return(0);
}
///@brief Are all i2c_task_op[] pointers done sending/receiving ?
///
/// @return 1 DONE, 0 NOT DONE
int8_t i2c_task_done()
{
delayus(1);
if(!i2c_task.enable || i2c_task.done )
return(1);
return(0);
}
///@brief I2C setup new OP but do not run it yet
///
/// @param[in] address: I2C address
/// @param[in] mode: TW_READ or TW_WRITE
/// @param[in] *buf: pointer to buffer for send or receive
/// @param[in] len: size of buffer to read or write
/// @return 1 = OK, 0 = ERROR
uint8_t i2c_fn(uint8_t address, uint8_t mode, uint8_t *buf, uint8_t len)
{
uint8_t sreg = SREG;
cli();
// sign task only
i2c_callback = NULL;
i2c.enable = 1; // Enabled
i2c.done = 0;
i2c.address = (address << 1) | (mode & 1);
i2c.flags = 0;
i2c.len = len;
i2c.ind = 0;
i2c.buf = buf;
// Reset Status
TWSR &= ~(_BV(TWPS0) | _BV(TWPS1));
// Disable Slave Mode
TWAR = 0;
SREG = sreg;
i2c_send_start();
// Debugging
#if 0
while(i2c.enable && !i2c.done )
{
printf("timeout: %d\r",(int)i2c.timeout);
}
printf("\n");
#else
while(! i2c_done() )
;
#endif
return( i2c.flags ? 0 : 1);
}
///@brief Send I2C START and enable interrupts
///
/// @return void
void i2c_send_start()
{
i2c.done = 0;
i2c.enable = 1;
i2c.timeout = I2C_TIMEOUT; // Start timeout timer
// Start a transactions
// TWI Enable
// TWI Interrupt Enable
// TWI Interrupt Clear
// TWI SEND RESTART
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWSTA);
}
///@brief Send I2C STOP and disable interrupts
///
/// @return void
void i2c_send_stop()
{
// All transactions are done
i2c.done = 1;
i2c.enable = 0;
// We are DONE
// TWI Enable
// TWI Interrupt Disable
// TWI Interrupt Clear
// TWI SEND STOP
TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWSTO);
delayus(10);
}
///@brief Is there anything else to send ?
/// Called after I2C transaction finishes
///
/// @return void
static void i2c_next()
{
// IF we have an i2c_callback() function then
// it must save status and reset i2c structure for next operation
if(i2c_callback)
i2c_callback();
else
i2c_send_stop();
}
/// @brief I2C timer task - check for I2C operation timeouts
/// @return void
void i2c_timer()
{
uint8_t sreg = SREG;
if(i2c.enable || !i2c.done )
{
if(i2c.timeout)
i2c.timeout--;
}
SREG = sreg;
}
///@brief I2C ISR for send/receive
///
/// @return void
ISR(TWI_vect)
{
// FYI: reading TWSR clears the status
// twi.h defines TW_STATUS
// #define TW_STATUS (TWSR & TW_STATUS_MASK)
uint8_t status = TW_STATUS;
// Are we Enabled to Receive/Send ?
// Are we Done ?
// Program errors - these should not happen
if(!i2c.enable || i2c.done || !i2c.buf || !i2c.len )
{
i2c_send_stop();
return;
}
// TUMEOUT ? STOP everything
if(i2c.timeout == 0)
{
i2c.flags |= (I2C_OP_TIMEOUT);
i2c_task.enable = 0;
i2c_task.done = 1;
i2c_task.error = 1;
i2c_send_stop();
return;
}
// Master Receiver mode
switch (status)
{
case TW_START: // START has been transmitted
case TW_REP_START: // RE-START has been transmitted
i2c.ind = 0;
TWDR = i2c.address;
// TWI Enable
// TWI Interrupt Enable
// TWI Interrupt Clear
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT);
break;
case TW_MT_SLA_ACK: // SLA+W trasnmitted and ACK received
case TW_MT_DATA_ACK:// Data trasnmitted and ACK received
if (i2c.ind < i2c.len)
{
TWDR = i2c.buf[i2c.ind++];
// TWI Enable
// TWI Interrupt Enable
// TWI Interrupt Clear
// SEND ACK after receive
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT);
}
else
{
// Done
i2c.done = 1;
i2c.enable = 0;
i2c_next();
}
break;
case TW_MR_DATA_ACK: // Data received ACK transmitted
if(i2c.ind < i2c.len)
i2c.buf[i2c.ind++] = TWDR;
case TW_MR_SLA_ACK: // SLA+R transmitted ACK received
if ((i2c.ind+1) < i2c.len)
{
// TWI Enable
// TWI Interrupt Enable
// TWI Interrupt Clear
// SEND ACK after receive
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWEA);
}
else
{
// LAST BYTE
// TWI Enable
// TWI Interrupt Enable
// TWI Interrupt Clear
// SEND NACK after receive
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT);
}
break;
case TW_MR_DATA_NACK: // Data received NACK transmitted
if(i2c.ind < i2c.len)
i2c.buf[i2c.ind++] = TWDR;
i2c.done = 1;
i2c.enable = 0;
i2c_next();
break;
// Arbitration lost
// NOTE: TW_ARB_LOST == TW_MR_ARB_LOST == TW_MT_ARB_LOST
case TW_ARB_LOST:
// TWI Enable
// TWI Interrupt Enable
// TWI Interrupt Clear
// TWI SEND RESTART
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWSTA);
break;
// Error cases
case TW_MT_SLA_NACK: // SLA+W transmitted NACK received
i2c.done = 1;
i2c.enable = 0;
i2c.flags |= I2C_TW_MT_SLA_NACK;
// ERROR
i2c_next();
break;
case TW_MR_SLA_NACK: // SLA+R transmitted NACK received
i2c.done = 1;
i2c.enable = 0;
i2c.flags |= I2C_TW_MR_SLA_NACK;
// ERROR
i2c_next();
break;
case TW_MT_DATA_NACK: // Data Transmitted NACK received
i2c.done = 1;
i2c.enable = 0;
i2c.flags |= I2C_TW_MT_DATA_NACK;
// ERROR
i2c_next();
break;
default: // Error
i2c.done = 1;
i2c.enable = 0;
// ERROR
i2c.flags |= I2C_BUS_ERROR;
i2c_next();
break;
}
}
/// @brief Display Errors for i2c_task_op[index]
///
/// @param[in] index: index of i2c_task_op[] array
/// @return void
void i2c_print_error(i2c_op_t *o)
{
int flags = o->flags;
if(flags)
{
printf(" %s\n", (i2c.done ? "DONE" : "ACTIVE") );
if(flags & I2C_OP_TIMEOUT)
printf(" OP_TIMEOUT\n");
if(flags & I2C_OP_LEN)
printf(" OP_LEN\n");
if(flags & I2C_OP_ERROR)
printf(" OP_ERROR\n");
if(flags & I2C_TW_MR_SLA_NACK)
printf(" TW_MR_SLA_NACK\n");
if(flags & I2C_TW_MT_SLA_NACK)
printf(" TW_MT_SLA_NACK\n");
if(flags & I2C_TW_MT_DATA_NACK)
printf(" TW_MT_DATA_NACK\n");
printf("\n");
}
}
///@brief Display any task errors
void i2c_display_task_errors()
{
int8_t i;
printf("i2c_task.done: %d\n", (int) i2c_task.done);
printf("i2c_task.error: %d\n", (int) i2c_task.error);
for(i=0;i2c_task_op[i] && i < I2C_OPS;++i)
{
printf("task: %d\n", (int) i);
i2c_print_error(i2c_task_op[i]);
printf("\n");
}
}