forked from emsec/ChameleonMini
-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathISO15693.c
686 lines (597 loc) · 27.5 KB
/
ISO15693.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
/*
* ISO15693.c
*
* Created on: 25.01.2017
* Author: Phillip Nash
* Modified by: ceres-c
*
* Interrupts information
* - Compare/Capture Channels are used as compare. They're used to sample the field in different moments of time
* - Reader field demodulation events are routed on Event Channel 0, card data modulation on Event Channel 6
* - Reader field demodulation uses Compare Channel C, card data modulation Compare Channel D (PORTB in both cases, of course)
*
* Loadmod code flow:
* Loadmodulation is performed via a state machine inside a ISR, triggered by a counter's (TCD0) overflow interrupt. The interrupt
* is configured in StartISO15693Demod to have a PER (period) equal to ISO15693 t1 nominal time: 4352 carrier pulses.
* (see ISO15693-3:2009, section 9.1).
* Once a EOF is reached when demodulating data from the reader, TCD0's PERBUF register is configured with the period of
* bit transmission (32 carrier pulses). This means that, once the first period overflow is reached (t1 expiration)
* the ISR will output data at the frequency dictated by the standard. This is due to the behaviour of buffered registers, see
* 8045A-AVR-02/08 section 3.7.
*
* Once t1 is reached (the counter overflowed), transmission of data should theoretically begin, but this is not guaranteed,
* due to possible slowdowns of data generation in the currently running Application. Until the application is done,
* the state machine will be stuck in LOADMOD_WAIT state, not outputting any data.
* The ISR will now be invoked every 32 carrier pulses (see ISO15693-2:2006, section 8.2), even when waiting for data.
*/
#include "ISO15693.h"
#include "../System.h"
#include "../Application/Application.h"
#include "LEDHook.h"
#include "AntennaLevel.h"
#include "Terminal/Terminal.h"
#include <avr/io.h>
#define SOC_1_OF_4_CODE 0x7B
#define SOC_1_OF_256_CODE 0x7E
#define EOC_CODE 0xDF
#define REQ_SUBCARRIER_SINGLE 0x00
#define REQ_SUBCARRIER_DUAL 0x01
#define REQ_DATARATE_LOW 0x00
#define REQ_DATARATE_HIGH 0x02
#define ISO15693_SAMPLE_CLK TC_CLKSEL_DIV2_gc // 13.56MHz
#define ISO15693_SAMPLE_PERIOD 128 // 9.4us
#define ISO15693_T1_TIME 4352 - 14 /* ISO t1 time - ISR prologue compensation */ // 4192 + 128 + 128 - 1
#define SUBCARRIER_1 32
#define SUBCARRIER_2 28
#define SUBCARRIER_OFF 0
#define SOF_PATTERN 0x1D // 0001 1101
#define EOF_PATTERN 0xB8 // 1011 1000
// These registers provide quick access but are limited
// so global vars will be necessary
#define DataRegister Codec8Reg0
#define StateRegister Codec8Reg1
#define ModulationPauseCount Codec8Reg2
#define SampleRegister Codec8Reg3
#define BitSent CodecCount16Register1
#define BitSampleCount CodecCount16Register2
#define CodecBufferPtr CodecPtrRegister1
static volatile struct {
volatile bool DemodFinished;
volatile bool LoadmodFinished;
} Flags = { 0 };
typedef enum {
DEMOD_SOC_STATE,
DEMOD_1_OUT_OF_4_STATE,
DEMOD_1_OUT_OF_256_STATE
} DemodStateType;
static volatile enum {
LOADMOD_WAIT,
/* Single subcarrier */
LOADMOD_START_SINGLE,
LOADMOD_SOF_SINGLE,
LOADMOD_BIT0_SINGLE,
LOADMOD_BIT1_SINGLE,
LOADMOD_EOF_SINGLE,
/* Dual subcarrier */
LOADMOD_START_DUAL,
LOADMOD_SOF_DUAL,
LOADMOD_BIT0_DUAL,
LOADMOD_BIT1_DUAL,
LOADMOD_EOF_DUAL,
LOADMOD_FINISHED
} LoadModState;
static volatile DemodStateType DemodState;
static volatile uint8_t ShiftRegister;
static volatile uint8_t ByteCount;
static volatile uint16_t BitRate1;
static volatile uint16_t BitRate2;
static volatile uint16_t SampleDataCount;
/* This function implements CODEC_DEMOD_IN_INT0_VECT interrupt vector.
* It is called when a pulse is detected in CODEC_DEMOD_IN_PORT (PORTB).
* The relevant interrupt vector was registered to CODEC_DEMOD_IN_MASK0 (PIN1) via:
* CODEC_DEMOD_IN_PORT.INT0MASK = CODEC_DEMOD_IN_MASK0;
* and unregistered writing the INT0MASK to 0
*/
ISR_SHARED isr_ISO15693_CODEC_DEMOD_IN_INT0_VECT(void) {
/* Clear Compare Channel C (CCC) interrupt Flags - From 14.12.10 [8331F–AVR–04/2013] */
CODEC_TIMER_SAMPLING.INTFLAGS = TC0_CCCIF_bm;
/* Enable compare/capture for high level interrupts on Capture Channel C for TCD0 - From 14.12.7 [8331F–AVR–04/2013] */
CODEC_TIMER_SAMPLING.INTCTRLB = TC_CCCINTLVL_HI_gc;
/* Disable this interrupt. From now on we will sample the field using our internal clock - From 13.13.11 [8331F–AVR–04/2013] */
CODEC_DEMOD_IN_PORT.INT0MASK = 0;
}
/* This function is called from isr_ISO15693_CODEC_TIMER_SAMPLING_CCC_VECT
* when we have 8 bits in SampleRegister and they represent an end of frame.
*/
INLINE void ISO15693_EOC(void) {
/* Set bitrate required by the reader on SOF for our following response */
if (CodecBuffer[0] & REQ_DATARATE_HIGH) {
BitRate1 = 256;
BitRate2 = 252; /* If single subcarrier mode is requested, BitRate2 is useless, but setting it nevertheless was still faster than checking */
} else {
BitRate1 = 256 * 4; // Possible value: 256 * 4 - 1
BitRate2 = 252 * 4; // Possible value: 252 * 4 - 3
}
Flags.DemodFinished = 1;
/* Disable demodulation interrupt */
/* Sets timer off for TCD0, disabling clock source. We're done receiving data from reader and don't need to probe the antenna anymore - From 14.12.1 [8331F–AVR–04/2013] */
CODEC_TIMER_SAMPLING.CTRLA = TC_CLKSEL_OFF_gc;
/* Disable event action for CODEC_TIMER_SAMPLING (TCD0) - From 14.12.4 [8331F–AVR–04/2013] */
CODEC_TIMER_SAMPLING.CTRLD = TC_EVACT_OFF_gc;
/* Disable compare/capture interrupts on Channel C - From 14.12.7 [8331F–AVR–04/2013] */
CODEC_TIMER_SAMPLING.INTCTRLB = TC_CCCINTLVL_OFF_gc;
/* Clear Compare Channel C (CCC) interrupt Flags - From 14.12.10 [8331F–AVR–04/2013] */
CODEC_TIMER_SAMPLING.INTFLAGS = TC0_CCCIF_bm;
/* Enable loadmodulation interrupt */
/* Disable the event action for TCE0 - From 14.12.4 [8331F–AVR–04/2013] */
CODEC_TIMER_LOADMOD.CTRLD = TC_EVACT_OFF_gc;
/* Enable compare/capture for high level interrupts on channel B for TCE0 - From 14.12.7 [8331F–AVR–04/2013] */
CODEC_TIMER_LOADMOD.INTCTRLB = TC_CCBINTLVL_HI_gc;
/* Clear TCE0 interrupt flags for Capture Channel B - From 14.12.10 [8331F–AVR–04/2013] */
CODEC_TIMER_LOADMOD.INTFLAGS = TC0_CCBIF_bm;
/* Set the BUFFERED PERIOD to Bitrate - 1 (because PERBUF is 0-based).
* The current PERIOD was set in StartISO15693Demod to ISO15693_T1_TIME, once ISO15693_T1_TIME is reached, this will be the new PERIOD.
* From 3.7 [8045A-AVR-02/08] */
CODEC_TIMER_LOADMOD.PERBUF = BitRate1 - 1;
}
/* Disable data demodulation interrupt and inform the codec to restart demodulation from scratch */
INLINE void ISO15693_GARBAGE(void) {
Flags.DemodFinished = 1;
/* Sets timer off for TCD0, disabling clock source. We have demodulated rubbish and don't need to probe the antenna anymore - From 14.12.1 [8331F–AVR–04/2013] */
CODEC_TIMER_SAMPLING.CTRLA = TC_CLKSEL_OFF_gc;
/* Disable event action for CODEC_TIMER_SAMPLING (TCD0) - From 14.12.4 [8331F–AVR–04/2013] */
CODEC_TIMER_SAMPLING.CTRLD = TC_EVACT_OFF_gc;
/* Disable compare/capture interrupts on Channel C - From 14.12.7 [8331F–AVR–04/2013] */
CODEC_TIMER_SAMPLING.INTCTRLB = TC_CCCINTLVL_OFF_gc;
/* Clear Compare Channel C (CCC) interrupt Flags - From 14.12.10 [8331F–AVR–04/2013] */
CODEC_TIMER_SAMPLING.INTFLAGS = TC0_CCCIF_bm;
}
/* This function is registered to CODEC_TIMER_SAMPLING (TCD0)'s Counter Channel C (CCC).
* When the timer is enabled, this is called on counter's overflow
*
* It demodulates bits received from the reader and saves them in CodecBuffer.
*
* It disables its own interrupt when an EOF is found (calling ISO15693_EOC) or when it receives garbage
*/
ISR_SHARED isr_ISO15693_CODEC_TIMER_SAMPLING_CCC_VECT(void) {
/* Shift demod data */
SampleRegister = (SampleRegister << 1) | (!(CODEC_DEMOD_IN_PORT.IN & CODEC_DEMOD_IN_MASK) ? 0x01 : 0x00);
if (++BitSampleCount == 8) {
BitSampleCount = 0;
switch (DemodState) {
case DEMOD_SOC_STATE:
if (SampleRegister == SOC_1_OF_4_CODE) {
DemodState = DEMOD_1_OUT_OF_4_STATE;
SampleDataCount = 0;
ModulationPauseCount = 0;
} else if (SampleRegister == SOC_1_OF_256_CODE) {
DemodState = DEMOD_1_OUT_OF_256_STATE;
SampleDataCount = 0;
} else { // No SOC. Restart and try again, we probably received garbage.
ISO15693_GARBAGE();
}
break;
case DEMOD_1_OUT_OF_4_STATE:
if (SampleRegister == EOC_CODE) {
ISO15693_EOC();
} else {
uint8_t SampleData = ~SampleRegister;
if (SampleData == (0x01 << 6)) {
/* ^_^^^^^^ -> 00 */
ModulationPauseCount++;
DataRegister >>= 2;
} else if (SampleData == (0x01 << 4)) {
/* ^^^_^^^^ -> 01 */
ModulationPauseCount++;
DataRegister >>= 2;
DataRegister |= 0b01 << 6;
} else if (SampleData == (0x01 << 2)) {
/* ^^^^^_^^ -> 10 */
ModulationPauseCount++;
DataRegister >>= 2;
DataRegister |= 0b10 << 6;
} else if (SampleData == (0x01 << 0)) {
/* ^^^^^^^_ -> 11 */
ModulationPauseCount++;
DataRegister >>= 2;
DataRegister |= 0b11 << 6;
}
if (ModulationPauseCount == 4) {
ModulationPauseCount = 0;
*CodecBufferPtr = DataRegister;
++CodecBufferPtr;
++ByteCount;
}
}
break;
case DEMOD_1_OUT_OF_256_STATE:
if (SampleRegister == EOC_CODE) {
ISO15693_EOC();
} else {
uint8_t Position = ((SampleDataCount / 2) % 256) - 1;
uint8_t SampleData = ~SampleRegister;
if (SampleData == (0x01 << 6)) {
/* ^_^^^^^^ -> N-3 */
DataRegister = Position - 3;
ModulationPauseCount++;
} else if (SampleData == (0x01 << 4)) {
/* ^^^_^^^^ -> N-2 */
DataRegister = Position - 2;
ModulationPauseCount++;
} else if (SampleData == (0x01 << 2)) {
/* ^^^^^_^^ -> N-1 */
DataRegister = Position - 1;
ModulationPauseCount++;
} else if (SampleData == (0x01 << 0)) {
/* ^^^^^^^_ -> N-0 */
DataRegister = Position - 0;
ModulationPauseCount++;
}
if (ModulationPauseCount == 1) {
ModulationPauseCount = 0;
*CodecBufferPtr = DataRegister;
++CodecBufferPtr;
++ByteCount;
}
}
break;
}
}
SampleDataCount++;
}
/* This function is registered to CODEC_TIMER_LOADMOD (TCE0)'s Counter Channel B (CCB).
* When the timer is enabled, this is called on counter's overflow
*
* It modulates the carrier with consuming bytes in CodecBuffer until ByteCount is 0.
*
* It disables its own interrupt when all data has been sent
*/
ISR_SHARED isr_ISO15693_CODEC_TIMER_LOADMOD_CCB_VECT(void) {
static void *JumpTable[] = {
[LOADMOD_WAIT] = && LOADMOD_WAIT_LABEL,
[LOADMOD_START_SINGLE] = && LOADMOD_START_SINGLE_LABEL,
[LOADMOD_SOF_SINGLE] = && LOADMOD_SOF_SINGLE_LABEL,
[LOADMOD_BIT0_SINGLE] = && LOADMOD_BIT0_SINGLE_LABEL,
[LOADMOD_BIT1_SINGLE] = && LOADMOD_BIT1_SINGLE_LABEL,
[LOADMOD_EOF_SINGLE] = && LOADMOD_EOF_SINGLE_LABEL,
[LOADMOD_START_DUAL] = && LOADMOD_START_DUAL_LABEL,
[LOADMOD_SOF_DUAL] = && LOADMOD_SOF_DUAL_LABEL,
[LOADMOD_BIT0_DUAL] = && LOADMOD_BIT0_DUAL_LABEL,
[LOADMOD_BIT1_DUAL] = && LOADMOD_BIT1_DUAL_LABEL,
[LOADMOD_EOF_DUAL] = && LOADMOD_EOF_DUAL_LABEL,
[LOADMOD_FINISHED] = && LOADMOD_FINISHED_LABEL
};
goto *JumpTable[StateRegister]; /* It takes 28 clock cycles to get here from function entry point */
LOADMOD_WAIT_LABEL:
/* ISO t1 is over, but application is not done generating data yet */
return;
LOADMOD_START_SINGLE_LABEL:
/* Application produced data. With this interrupt we are aligned to the bit-grid. */
ShiftRegister = SOF_PATTERN;
BitSent = 0;
/* Fallthrough */
LOADMOD_SOF_SINGLE_LABEL:
if (ShiftRegister & 0x80) {
CodecSetLoadmodState(true);
} else {
CodecSetLoadmodState(false);
}
CodecStartSubcarrier();
ShiftRegister <<= 1;
BitSent++;
if ((BitSent % 8) == 0) {
/* Last SOF bit has been put out. Start sending out data */
StateRegister = LOADMOD_BIT0_SINGLE;
ShiftRegister = (*CodecBufferPtr++);
} else {
StateRegister = LOADMOD_SOF_SINGLE;
}
return;
LOADMOD_BIT0_SINGLE_LABEL: //Manchester encoding
if (ShiftRegister & 0x01) {
/* Deactivate carrier */
CodecSetLoadmodState(false);
} else {
/* Activate carrier */
CodecSetLoadmodState(true);
}
StateRegister = LOADMOD_BIT1_SINGLE;
return;
LOADMOD_BIT1_SINGLE_LABEL: //Manchester encoding
if (ShiftRegister & 0x01) {
CodecSetLoadmodState(true);
} else {
CodecSetLoadmodState(false);
}
ShiftRegister >>= 1;
BitSent++;
StateRegister = LOADMOD_BIT0_SINGLE;
if ((BitSent % 8) == 0) {
/* Byte boundary */
if (--ByteCount == 0) {
/* No more data left */
ShiftRegister = EOF_PATTERN;
StateRegister = LOADMOD_EOF_SINGLE;
} else {
ShiftRegister = (*CodecBufferPtr++);
}
}
return;
LOADMOD_EOF_SINGLE_LABEL: //End of Manchester encoding
/* Output EOF */
if (ShiftRegister & 0x80) {
CodecSetLoadmodState(true);
} else {
CodecSetLoadmodState(false);
}
ShiftRegister <<= 1;
BitSent++;
if ((BitSent % 8) == 0) {
/* Last bit has been put out */
StateRegister = LOADMOD_FINISHED;
} /* else: stay in this state. No changes needed */
return;
// -------------------------------------------------------------
LOADMOD_START_DUAL_LABEL:
ShiftRegister = SOF_PATTERN;
BitSent = 0;
CodecSetLoadmodState(true);
CodecStartSubcarrier();
// fallthrough
LOADMOD_SOF_DUAL_LABEL:
if (ShiftRegister & 0x80) {
CodecChangeDivider(SUBCARRIER_1);
CODEC_TIMER_LOADMOD.PER = BitRate1 - 1;
} else {
CodecChangeDivider(SUBCARRIER_2);
CODEC_TIMER_LOADMOD.PER = BitRate2 - 1;
}
ShiftRegister <<= 1;
BitSent++;
if ((BitSent % 8) == 0) {
/* Last SOF bit has been put out. Start sending out data */
StateRegister = LOADMOD_BIT0_DUAL;
ShiftRegister = (*CodecBufferPtr++);
} else {
StateRegister = LOADMOD_SOF_DUAL;
}
return;
LOADMOD_BIT0_DUAL_LABEL: //Manchester encoding
if (ShiftRegister & 0x01) {
CodecChangeDivider(SUBCARRIER_2);
CODEC_TIMER_LOADMOD.PER = BitRate2 - 1;
} else {
CodecChangeDivider(SUBCARRIER_1);
CODEC_TIMER_LOADMOD.PER = BitRate1 - 1;
}
StateRegister = LOADMOD_BIT1_DUAL;
return;
LOADMOD_BIT1_DUAL_LABEL: //Manchester encoding
if (ShiftRegister & 0x01) {
/* fc / 32 */
CodecChangeDivider(SUBCARRIER_1);
CODEC_TIMER_LOADMOD.PER = BitRate1 - 1;
} else {
/* fc / 28 */
CodecChangeDivider(SUBCARRIER_2);
CODEC_TIMER_LOADMOD.PER = BitRate2 - 1;
}
ShiftRegister >>= 1;
BitSent++;
StateRegister = LOADMOD_BIT0_DUAL;
if ((BitSent % 8) == 0) {
/* Byte boundary */
if (--ByteCount == 0) {
/* No more data left */
ShiftRegister = EOF_PATTERN;
StateRegister = LOADMOD_EOF_DUAL;
} else {
ShiftRegister = (*CodecBufferPtr++);
}
}
return;
LOADMOD_EOF_DUAL_LABEL: //End of Manchester encoding
/* Output EOF */
if (ShiftRegister & 0x80) {
CodecChangeDivider(SUBCARRIER_1);
CODEC_TIMER_LOADMOD.PER = BitRate1 - 1;
} else {
CodecChangeDivider(SUBCARRIER_2);
CODEC_TIMER_LOADMOD.PER = BitRate2 - 1;
}
ShiftRegister <<= 1;
BitSent++;
if ((BitSent % 8) == 0) {
/* Last bit has been put out */
StateRegister = LOADMOD_FINISHED;
} /* else: stay in this state. No changes needed */
return;
LOADMOD_FINISHED_LABEL:
/* Sets timer off for CODEC_TIMER_LOADMOD (TCE0) disabling clock source as we're done modulating */
CODEC_TIMER_LOADMOD.CTRLA = TC_CLKSEL_OFF_gc;
/* Disable compare/capture for all level interrupts on channel B for TCE0 - From 14.12.7 [8331F–AVR–04/2013] */
CODEC_TIMER_LOADMOD.INTCTRLB = TC_CCBINTLVL_OFF_gc;
CodecSetSubcarrier(CODEC_SUBCARRIERMOD_OFF, 0);
Flags.LoadmodFinished = 1;
return;
}
/* Reset global variables/interrupts to demodulate incoming reader data via ISRs */
void StartISO15693Demod(void) {
/* Reset global variables to default values */
CodecBufferPtr = CodecBuffer;
Flags.DemodFinished = 0;
Flags.LoadmodFinished = 0;
DemodState = DEMOD_SOC_STATE;
StateRegister = LOADMOD_WAIT;
DataRegister = 0;
SampleRegister = 0;
BitSampleCount = 0;
SampleDataCount = 0;
ModulationPauseCount = 0;
ByteCount = 0;
ShiftRegister = 0;
/* Set clock source for TCD0 to ISO15693_SAMPLE_CLK = TC_CLKSEL_DIV2_gc = System Clock / 2.
* Since the Chameleon is clocked at 13.56*2 MHz (see Makefile), this counter will hit at the same frequency of reader field.
* From 14.12.1 [8331F–AVR–04/2013]
*/
CODEC_TIMER_SAMPLING.CTRLA = ISO15693_SAMPLE_CLK;
/* Set up TCD0 action/source:
* - Event Action: Restart waveform period (reset PER register)
* - Event Source Select: trigger on CODEC_TIMER_MODSTART_EVSEL = TC_EVSEL_CH0_gc = Event Channel 0
* From 14.12.4 [8331F–AVR–04/2013]
*/
CODEC_TIMER_SAMPLING.CTRLD = TC_EVACT_RESTART_gc | CODEC_TIMER_MODSTART_EVSEL;
/* Resets the sampling counter (TCD0) to 0 - From 14.12.12 [8331F–AVR–04/2013] */
CODEC_TIMER_SAMPLING.CNT = 0;
/* Set Clock Source for TCE0 to CODEC_TIMER_CARRIER_CLKSEL = TC_CLKSEL_EVCH6_gc = Event Channel 6 */
CODEC_TIMER_LOADMOD.CTRLA = CODEC_TIMER_CARRIER_CLKSEL;
/* Set up TCE0 action/source:
* - Event Action: Restart waveform period (reset PER register)
* - Event Source Select: trigger on CODEC_TIMER_MODSTART_EVSEL = TC_EVSEL_CH0_gc = Event Channel 0
* From 14.12.4 [8331F–AVR–04/2013]
*/
CODEC_TIMER_LOADMOD.CTRLD = TC_EVACT_RESTART_gc | CODEC_TIMER_MODSTART_EVSEL;
/* Temporarily disable compare/capture for all level interrupts on channel B for TCE0. They'll be enabled later when load modulation will be needed.
* From 14.12.7 [8331F–AVR–04/2013] */
CODEC_TIMER_LOADMOD.INTCTRLB = TC_CCBINTLVL_OFF_gc;
/* Set the period for CODEC_TIMER_LOADMOD (TCE0) ISO standard t1 time.
* Once this timer is enabled, it will wait for a time equal to t1 and then its ISR will be called.
* From 14.12.18 [8331F–AVR–04/2013]
*/
CODEC_TIMER_LOADMOD.PER = ISO15693_T1_TIME;
/* Start looking out for modulation pause via interrupt. */
/* Clear PORTB interrupt flags - From 13.13.13 [8331F–AVR–04/2013] */
CODEC_DEMOD_IN_PORT.INTFLAGS = PORT_INT0IF_bm;
/* Use PIN1 as a source for modulation pauses. Will trigger PORTB Interrput 0 - From 13.13.11 [8331F–AVR–04/2013] */
CODEC_DEMOD_IN_PORT.INT0MASK = CODEC_DEMOD_IN_MASK0;
}
/* Configure unvarying interrupts settings.
* All configured interrupts will still be disabled once this function is done, they'll be enabled one by one as the communication flow requires them.
*/
void ISO15693CodecInit(void) {
CodecInitCommon();
/**************************************************
* Register function handlers to shared ISR *
**************************************************/
/* Register isr_ISO15693_CODEC_TIMER_SAMPLING_CCC_VECT function to CODEC_TIMER_SAMPLING (TCD0)'s Counter Channel C (CCC) */
isr_func_TCD0_CCC_vect = &isr_ISO15693_CODEC_TIMER_SAMPLING_CCC_VECT;
/* Register isr_ISO15693_CODEC_DEMOD_IN_INT0_VECT function to CODEC_DEMOD_IN_PORT (PORTB) interrupt 0 */
isr_func_CODEC_DEMOD_IN_INT0_VECT = &isr_ISO15693_CODEC_DEMOD_IN_INT0_VECT;
/* Register isr_ISO15693_CODEC_TIMER_LOADMOD_CCB_VECT function to CODEC_TIMER_LOADMOD (TCE0)'s Counter Channel B (CCB) */
isr_func_CODEC_TIMER_LOADMOD_CCB_VECT = &isr_ISO15693_CODEC_TIMER_LOADMOD_CCB_VECT;
/**************************************************
* Configure demod interrupt *
**************************************************/
/* Configure sampling-timer free running and sync to first modulation-pause */
/* Set the period for TCD0 to ISO15693_SAMPLE_PERIOD - 1 because PER is 0-based - From 14.12.14 [8331F–AVR–04/2013] */
CODEC_TIMER_SAMPLING.PER = ISO15693_SAMPLE_PERIOD - 1;
/* Set Compare Channel C (CCC) period to half bit period - 1. (- 14 to compensate ISR timing overhead) - From 14.12.16 [8331F–AVR–04/2013] */
CODEC_TIMER_SAMPLING.CCC = ISO15693_SAMPLE_PERIOD / 2 - 14 - 1;
/* Temporarily disable Compare Channel C (CCC) interrupts.
* They'll be enabled later in isr_ISO15693_CODEC_DEMOD_IN_INT0_VECT once we sensed the reader is sending data and we're in sync with the pulses.
* From 14.12.7 [8331F–AVR–04/2013]
*/
CODEC_TIMER_SAMPLING.INTCTRLB = TC_CCCINTLVL_OFF_gc;
/* Clear Compare Channel C (CCC) interrupt Flags - From 14.12.10 [8331F–AVR–04/2013] */
CODEC_TIMER_SAMPLING.INTFLAGS = TC0_CCCIF_bm;
/**************************************************
* Configure loadmod interrupt *
**************************************************/
/* Disable timer error and overflow interrupts - From 14.12.6 [8331F–AVR–04/2013] */
CODEC_TIMER_LOADMOD.INTCTRLA = 0;
/* Activate Power for demodulator */
CodecSetDemodPower(true);
StartISO15693Demod();
}
void ISO15693CodecDeInit(void) {
/* Gracefully shutdown codec */
CODEC_DEMOD_IN_PORT.INT0MASK = 0;
/* Reset global variables to default values */
CodecBufferPtr = CodecBuffer;
Flags.DemodFinished = 0;
Flags.LoadmodFinished = 0;
DemodState = DEMOD_SOC_STATE;
StateRegister = LOADMOD_WAIT;
DataRegister = 0;
SampleRegister = 0;
BitSampleCount = 0;
SampleDataCount = 0;
ModulationPauseCount = 0;
ByteCount = 0;
ShiftRegister = 0;
/* Disable sample timer */
/* Sets timer off for TCD0, disabling clock source - From 14.12.1 [8331F–AVR–04/2013] */
CODEC_TIMER_SAMPLING.CTRLA = TC_CLKSEL_OFF_gc;
/* Disable event action for CODEC_TIMER_SAMPLING (TCD0) - From 14.12.4 [8331F–AVR–04/2013] */
CODEC_TIMER_SAMPLING.CTRLD = TC_EVACT_OFF_gc;
/* Disable Compare Channel C (CCC) interrupts - From 14.12.7 [8331F–AVR–04/2013] */
CODEC_TIMER_SAMPLING.INTCTRLB = TC_CCCINTLVL_OFF_gc;
/* Clear Compare Channel C (CCC) interrupt Flags - From 14.12.10 [8331F–AVR–04/2013] */
CODEC_TIMER_SAMPLING.INTFLAGS = TC0_CCCIF_bm;
/* Disable load modulation */
/* Disable the event action for TCE0 - From 14.12.4 [8331F–AVR–04/2013] */
CODEC_TIMER_LOADMOD.CTRLD = TC_EVACT_OFF_gc;
/* Disable compare/capture for all level interrupts on channel B for TCE0 - From 14.12.7 [8331F–AVR–04/2013] */
CODEC_TIMER_LOADMOD.INTCTRLB = TC_CCBINTLVL_OFF_gc;
/* Clear TCE0 interrupt flags for Capture Channel B - From 14.12.10 [8331F–AVR–04/2013] */
CODEC_TIMER_LOADMOD.INTFLAGS = TC0_CCBIF_bm;
CodecSetSubcarrier(CODEC_SUBCARRIERMOD_OFF, 0);
CodecSetDemodPower(false);
CodecSetLoadmodState(false);
}
void ISO15693CodecTask(void) {
if (Flags.DemodFinished) {
Flags.DemodFinished = 0;
uint16_t DemodByteCount = ByteCount;
uint16_t AppReceivedByteCount = 0;
bool bDualSubcarrier = false;
if (DemodByteCount > 0) {
LogEntry(LOG_INFO_CODEC_RX_DATA, CodecBuffer, DemodByteCount);
LEDHook(LED_CODEC_RX, LED_PULSE);
if (CodecBuffer[0] & REQ_SUBCARRIER_DUAL) {
bDualSubcarrier = true;
}
AppReceivedByteCount = ApplicationProcess(CodecBuffer, DemodByteCount);
}
/* This is only reached when we've received a valid frame */
if (AppReceivedByteCount != ISO15693_APP_NO_RESPONSE) {
if (AppReceivedByteCount > CODEC_BUFFER_SIZE - ISO15693_CRC16_SIZE) { /* CRC would be written outside codec buffer */
CodecBuffer[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_ERROR;
CodecBuffer[ISO15693_RES_ADDR_PARAM] = ISO15693_RES_ERR_NOT_SUPP;
AppReceivedByteCount = 2;
LogEntry(LOG_INFO_GENERIC, "Too much data requested - See PR #274", 38);
}
LEDHook(LED_CODEC_TX, LED_PULSE);
ByteCount = AppReceivedByteCount;
CodecBufferPtr = CodecBuffer;
/* Start loadmodulating */
if (bDualSubcarrier) {
CodecSetSubcarrier(CODEC_SUBCARRIERMOD_OOK, SUBCARRIER_2);
StateRegister = LOADMOD_START_DUAL;
} else {
CodecSetSubcarrier(CODEC_SUBCARRIERMOD_OOK, SUBCARRIER_1);
StateRegister = LOADMOD_START_SINGLE;
}
/* Calculate the CRC while modulation is already ongoing */
ISO15693AppendCRC(CodecBuffer, AppReceivedByteCount);
ByteCount += ISO15693_CRC16_SIZE; /* Increase this variable as it will be read by the codec during loadmodulation */
LogEntry(LOG_INFO_CODEC_TX_DATA, CodecBuffer, AppReceivedByteCount + ISO15693_CRC16_SIZE);
} else {
/* Overwrite the PERBUF register, which was configured in ISO15693_EOC, with the new appropriate value.
* This is expecially needed because, since load modulation has not been performed, we're jumping here straight after
* ISO15693_EOC. This implies that the data stored in the PERBUF register by ISO15693_EOC still has to be copied
* in the PER register and that will happen on the next UPDATE event.
* The next UPDATE event will be timer/counter enablement in ISO15693_EOC, which is executed after StartISO15693Demod,
* effectively overwriting PER with an incorrect value.
* See 8045A-AVR-02/08 section 3.7 for information about buffered registers.
*/
CODEC_TIMER_LOADMOD.PERBUF = ISO15693_T1_TIME;
StartISO15693Demod();
}
}
if (Flags.LoadmodFinished) {
Flags.LoadmodFinished = 0;
/* Load modulation has been finished. Stop it and start to listen for incoming data again. */
StartISO15693Demod();
}
}