rfid.c


1
#define VERSION "* V 0.00 "
2
3
#define F_CPU 1000000
4
5
//usage 24c256
6
#define EEBLOCKL    8
7
#define KEYPOS     8
8
#define KEYCOUNT   64
9
#define LOGPOS      KEYPOS+KEYCOUNT*EEBLOCKL
10
#define LOGCOUNT    4000
11
12
//usage pcf8583
13
#define LOGPPOS    32  
14
#define RESTIMEPOS  16
15
16
#define ST_NONE    0
17
#define ST_RESET    1
18
#define ST_OFF    2
19
#define ST_ACCEPT    3
20
#define ST_DENY    4
21
#define ST_NEW    5
22
#define ST_DELETE    6
23
24
#define RF_4xx2   0
25
#define RF_4xx5      1
26
#define RF_SCAN      2
27
28
#define CSE_ROM    0xA0
29
#define CSA_ROM    0xA1
30
#define CSE_CLOCK  0xA2
31
#define CSA_CLOCK  0xA3
32
33
#define SDA1  PORTC|=_BV(4) 
34
#define SDA0  PORTC&=~_BV(4) 
35
#define SDAi  DDRC&=~_BV(4) 
36
#define SDAo  DDRC|=_BV(4) 
37
#define SDAq  (PINC&_BV(4))
38
   
39
#define SCL1  PORTC|=_BV(5) 
40
#define SCL0  PORTC&=~_BV(5) 
41
#define SCLi  DDRC&=~_BV(5) 
42
#define SCLo  DDRC|=_BV(5) 
43
44
#define HTRC_PIN    PIND
45
#define HRTC_MASK  _BV(2)  
46
47
#define HTRC_CLK1  PORTD|=_BV(3) 
48
#define HTRC_CLK0  PORTD&=~_BV(3) 
49
#define HTRC_CLKi  DDRD&=~_BV(3) 
50
#define HTRC_CLKo  DDRD|=_BV(3) 
51
52
#define HTRC_DI1  PORTD|=_BV(4) 
53
#define HTRC_DI0  PORTD&=~_BV(4) 
54
#define HTRC_DIi  DDRD&=~_BV(4) 
55
#define HTRC_DIo  DDRD|=_BV(4) 
56
57
#define HTRC_DO1  PORTD|=_BV(2) 
58
#define HTRC_DO0  PORTD&=~_BV(2) 
59
#define HTRC_DOi  DDRD&=~_BV(2) 
60
#define HTRC_DOo  DDRD|=_BV(2) 
61
#define HTRC_DOq  (PIND&_BV(2))
62
63
#define LED1  PORTC|=_BV(0) 
64
#define LED0  PORTC&=~_BV(0) 
65
#define LEDi  DDRC&=~_BV(0) 
66
#define LEDo  DDRC|=_BV(0) 
67
68
#define REL1  PORTB|=_BV(0) 
69
#define REL0  PORTB&=~_BV(0) 
70
#define RELi  DDRB&=~_BV(0) 
71
#define RELo  DDRB|=_BV(0) 
72
73
#define BEEP1  PORTB|=_BV(1) 
74
#define BEEP0  PORTB&=~_BV(1) 
75
#define BEEPi  DDRB&=~_BV(1) 
76
#define BEEPo  DDRB|=_BV(1) 
77
78
#include <avr/io.h> 
79
#include <avr/interrupt.h>
80
#include <avr/sleep.h>
81
#include <avr/pgmspace.h>
82
#include <avr/eeprom.h>
83
#include <stdlib.h>
84
#include <string.h>
85
#include <util/crc16.h>
86
#include <util/delay.h>
87
#include <avr/boot.h>
88
#include <stdint.h>   
89
#include <avr/wdt.h> 
90
91
/*global time/status*/
92
unsigned char tm[8];
93
94
volatile unsigned char failtimeout,tcheck,pulsw,d_bit,d_ready,ttime,rtime,btime;
95
96
unsigned PROGMEM char parity[]={1,0,0,1,0,1,1,0, 0,1,1,0,1,0,0,1, 0,1,1,0,1,0,0,1,             1,0,0,1,0,1,1,0};
97
98
void dolog();
99
void readtime();
100
void htrc_init();
101
unsigned char puttm();
102
103
SIGNAL(SIG_OVERFLOW0)
104
{
105
    TCNT0=150;
106
    d_bit=0;d_ready=1;
107
}          
108
109
unsigned char rfmode=0;
110
unsigned char distime;
111
112
SIGNAL(SIG_OVERFLOW2)
113
{
114
static unsigned char prescale=0;
115
116
  rtime++;     //htrc-recivetimer
117
  tcheck++;  //watchdog controltimer
118
  btime++;  //timer for relay and timer
119
120
  prescale++;
121
  if(prescale>29)
122
  {
123
    prescale=0;
124
    ttime++;             //timout for terminalinputs
125
    distime++;            //periodic show date and time
126
    if(failtimeout)failtimeout--;  //dont fill the logfile with wrong tags  
127
  }
128
}     
129
SIGNAL(SIG_INTERRUPT0)
130
{
131
  static unsigned char nd,isd;
132
  unsigned char port;
133
134
  pulsw=TCNT0;
135
  if(rfmode==RF_4xx5)
136
  {
137
    if(pulsw>25)
138
    {
139
      TCNT0=0;
140
      d_bit=nd;
141
      nd=1;
142
      d_ready=1;
143
    } else nd=0;
144
  } else if (rfmode==RF_4xx2)
145
  {
146
    port=HTRC_PIN;
147
    if(pulsw>45)
148
    {
149
      isd=0;
150
      TCNT0=0;
151
    }
152
    if(isd)isd=0;else
153
    {
154
      isd=1;
155
      if(port&HRTC_MASK)d_bit=1;else d_bit=0;
156
      d_ready=1;
157
    }
158
  
159
  } else
160
  {
161
      TCNT0=0;
162
      d_ready=1;
163
  }
164
}
165
166
void do_wd()
167
{
168
  static unsigned char ltcheck;
169
  unsigned char c; 
170
  {
171
    c=tcheck-ltcheck;
172
    if(ltcheck!=tcheck)
173
    {
174
      ltcheck=tcheck;
175
       wdt_reset();
176
    }
177
  }
178
}
179
180
unsigned char htrc_getbit()
181
{
182
  while(d_ready==0)do_wd();
183
  d_ready=0;
184
  return d_bit;
185
}
186
187
void iic_delay()
188
{
189
    asm("nop");
190
    asm("nop");
191
    asm("nop");
192
    asm("nop");
193
}
194
195
void inline sda_hi(void)
196
{
197
  SDAi;
198
  SDA1;
199
  iic_delay();
200
}
201
void inline sda_lo(void)
202
{
203
   SDA0;
204
   SDAo;
205
  iic_delay();
206
}
207
unsigned inline char sda_get(void)
208
{
209
    return SDAq;
210
}
211
void inline scl_hi(void)
212
{
213
    SCL1;
214
  iic_delay();
215
}
216
void inline scl_lo(void)
217
{
218
  SCLo;
219
  SCL0;
220
  iic_delay();
221
}
222
void iic_start(void)
223
{
224
    scl_hi();
225
    sda_lo();
226
}
227
void iic_stop(void)
228
{
229
    sda_lo();
230
    scl_hi();
231
    sda_hi();
232
}
233
unsigned char writeok;
234
235
void iic_putc(unsigned char c)
236
{
237
    unsigned char co;   
238
239
    for(co=0;co<8;co++)
240
    {
241
        scl_lo();
242
        if(c&128)sda_hi(); else sda_lo();
243
        c<<=1;
244
        scl_hi();
245
    }
246
    scl_lo();
247
    sda_hi();
248
    writeok=!sda_get();
249
    scl_hi();
250
    scl_lo();
251
}
252
253
unsigned char iic_getc(unsigned char last)
254
{
255
256
    unsigned char co,res=0;
257
258
    for(co=0;co<8;co++)
259
    {
260
        res=res<<1;
261
262
        if(sda_get())res++;
263
        scl_hi();
264
        scl_lo();
265
    }
266
    if(last)sda_hi(); else sda_lo();
267
    scl_hi();
268
    scl_lo();
269
    sda_hi();
270
    return res;
271
}
272
273
void iic_eeprom_put_mem(unsigned int d,unsigned char *s, unsigned char l)
274
{
275
  unsigned char co;
276
277
  iic_start();
278
  iic_putc(CSE_ROM);
279
  iic_putc(d>>8);
280
  iic_putc(d&255);
281
  for(co=0;co<l;co++) iic_putc(*s++);
282
  iic_stop();  
283
  _delay_ms(10);
284
}
285
void iic_eeprom_get_mem(unsigned char *d,unsigned int s, unsigned char l)
286
{
287
  unsigned char co;
288
289
  iic_start();
290
  iic_putc(CSE_ROM);
291
  iic_putc(s>>8);
292
  iic_putc(s&255);
293
  iic_start();
294
  iic_putc(CSA_ROM);
295
  for(co=0;co<l;co++)
296
  {
297
     *(d++)=iic_getc((co==(l-1))?1:0);
298
  }
299
  iic_stop();  
300
}
301
/*
302
iic_eeprom_erase()
303
{
304
  unsigned int co;
305
  unsigned char c[32];
306
307
  memset(c,255,32);
308
  for(co=0;co<32768;co+=32)iic_eeprom_put_mem(co,c,32);
309
}
310
*/
311
312
void putch(unsigned char c)
313
{
314
        UCSRA|=(1<<TXC);
315
        UDR=c;
316
        while((UCSRA&(1<<TXC))==0);
317
}
318
319
unsigned char getch()
320
{
321
    unsigned char c;
322
        c=UDR;
323
        UCSRA|=(1<<RXC);
324
        return c;
325
}
326
unsigned char havech()
327
{
328
  do_wd();
329
    if(UCSRA&(1<<RXC))ttime=0;
330
    return UCSRA&(1<<RXC);
331
}     
332
unsigned int gethch()
333
{
334
335
    unsigned char c,o;
336
337
  while(havech()==0)if(ttime>20)return -1;
338
  c=getch();c-=48;if(c>9)c-=7; if(c>15) return -1;
339
  o=c;
340
  while(havech()==0)if(ttime>20)return -1;
341
  c=getch();c-=48;if(c>9)c-=7; if(c>15) return -1;
342
  o=o*16+c;
343
    return o;
344
}
345
346
void putstr(char *c )
347
{
348
    while (*c)
349
    {
350
        putch(*c);
351
        c++;
352
    }
353
}  
354
void putdch(unsigned char c)
355
{
356
  unsigned char cc;
357
358
  cc=c/10;
359
  putch(cc+48);
360
  cc=c%10;
361
  putch(cc+48);
362
}
363
void puthch(unsigned char c)
364
{
365
  unsigned char cc;
366
367
  cc=c/16;if (cc>9)cc+=7;
368
  putch(cc+48);
369
  cc=c%16;if (cc>9)cc+=7;
370
  putch(cc+48);
371
}
372
373
void putms(const char *c )
374
{
375
  do_wd();
376
    while (pgm_read_byte(c))
377
    {
378
        putch(pgm_read_byte(c));
379
        c++;
380
    }
381
}  
382
unsigned char  wtast()
383
{
384
  putms(PSTR("[space]")); 
385
   while(havech()==0&&(ttime<20));
386
  if(havech())return getch();  
387
  return 255;     
388
}
389
unsigned char wgetch()
390
{
391
   while(havech()==0&&(ttime<2));
392
  return getch();       
393
}
394
unsigned char spu[25]; // max linelength +5 char for appends
395
unsigned char getstr()
396
{
397
  unsigned char co=0,c;
398
399
  while (1)
400
  {
401
    if(ttime>20)return 0;
402
    if(havech())
403
    {
404
      c=getch();
405
      
406
      if(c==27)return 0;
407
      if(c==13){spu[co]=0;return co;}
408
      if(c==10){spu[co]=0;return co;}
409
      if(c==8){if(co>0)co--;else c=7;} else if((c>127)||(c<32))c=7;
410
      if(co>18)c=7;
411
      if(c>31)spu[co++]=c;
412
      putch(c);
413
    }
414
  }
415
}
416
417
/* buffer to read the tag */
418
unsigned char res[12];
419
unsigned char tres[12];
420
421
/* buffer to calculate 4xx5 serialnumber */
422
unsigned char mpu[6];
423
424
/* calculate 4xx5 serialnumber */
425
unsigned char brev(unsigned char c)
426
{
427
  unsigned char o;
428
429
  o=0;
430
  if(c&1)o|=128;
431
  if(c&2)o|=64;
432
  if(c&4)o|=32;
433
  if(c&8)o|=16;
434
  if(c&16)o|=8;
435
  if(c&32)o|=4;
436
  if(c&64)o|=2;
437
  if(c&128)o|=1;
438
  return o;
439
}
440
void  slmpu()
441
{
442
  unsigned char co;
443
                                                                                    for(co=5;co>0;co--)
444
    {
445
        mpu[co]<<=1;if(mpu[co-1]&128)mpu[co]++;
446
    }
447
    mpu[0]<<=1;       
448
}
449
void ppu()
450
{
451
  unsigned char cc,co,e,re[16];
452
  unsigned int i;
453
454
  mpu[0]=brev(res[0]);
455
  mpu[1]=brev(res[1]);
456
  mpu[2]=brev(res[2]);
457
  mpu[3]=brev(res[3]);
458
  mpu[4]=brev(res[4]&(255-7));
459
460
  i=brev(res[5]);
461
  i=4*i+(brev(res[4])/64&3);
462
463
  for(cc=0;cc<15;cc++)
464
  {
465
    mpu[5]=0;
466
    for(co=0;co<40;co++)
467
    {
468
      slmpu();
469
      e=0;
470
      if(mpu[5]>9)
471
      {  
472
        mpu[5]-=10;
473
        e=1;
474
      }
475
      if(e)mpu[0]++;
476
    }
477
    re[14-cc]=mpu[5]+48;
478
  }
479
  putch(32);
480
  putch((i/100)+48);
481
  putch((i/10)%10+48);
482
  putch(i%10+48);
483
  re[15]=0;
484
  if(re[0]!=48)putch('!');
485
  if(re[1]!=48)putch('!');
486
  if(re[2]!=48)putch('!');
487
  putstr((char *)re+3);
488
}
489
490
unsigned char readcount=0;
491
492
unsigned char readrfid()
493
{
494
  unsigned char co,cc,rpa,ch;
495
  unsigned int crc,tmp;
496
497
/* Scan the datarate toi select RF_4xx2 or RF_4xx5 */
498
  rfmode=RF_SCAN;
499
  for(co=0;co<sizeof(tres);co++)tres[co]=0x00;
500
  for(tmp=0,co=0;co<20;co++)
501
  {  
502
    htrc_getbit();
503
    tmp+=pulsw;
504
  }
505
  if(tmp>0x200)rfmode=RF_4xx2; else rfmode=RF_4xx5;
506
/* its a RF_4xx5 */
507
  if(rfmode)
508
  {
509
    rtime=0;
510
    for(co=0;co<12;co++)
511
    {
512
      if(co!=11)
513
      {
514
        if(htrc_getbit())co=0;
515
      } else
516
      {
517
        if(htrc_getbit()==0)co=0;
518
      }
519
      if(rtime>10)
520
      {
521
        return 0;
522
      }
523
    }
524
    LED0;
525
    for(rpa=0,cc=0;cc<10;cc++)
526
    {
527
      for(co=0,ch=0;co<8;co++)
528
      {
529
        ch=ch+ch+htrc_getbit();
530
      }
531
      if(htrc_getbit()==0)
532
      {
533
        cc=100;
534
        continue;
535
      }
536
      else
537
      {
538
        tres[cc]=ch;
539
      }
540
    }
541
    LED1;
542
    if(cc>64)
543
    {
544
      return 0;
545
    }  
546
    for(crc=0,cc=0;cc<10;cc++)
547
    {
548
      crc=_crc_ccitt_update(crc,brev(tres[cc]));
549
    }
550
    if(crc)
551
    {
552
      return 0;
553
    }  
554
    readcount=99;
555
    memcpy(res,tres,sizeof(res));
556
    return 1;
557
  }else
558
/* its a RF_4xx2 */
559
  {
560
    rtime=0;
561
    for(co=0;co<10;co++)
562
    {
563
      if(htrc_getbit()==0)co=0;
564
      if(rtime>10)
565
      {
566
        return 0;
567
      }
568
    }
569
    LED0;
570
    for(rpa=0,cc=0;cc<10;cc++)
571
    {
572
      for(co=0,ch=0;co<5;co++)
573
      {
574
        ch=ch+ch+htrc_getbit();
575
      }
576
      rpa^=ch;
577
      if( pgm_read_byte(&parity[ch])==0)
578
      {
579
        cc=100;
580
        continue;
581
      }
582
      else
583
      {
584
        ch/=2;
585
        if(cc&1)tres[cc/2]+=ch;else tres[cc/2]=ch*16;
586
      }
587
    }
588
    LED1;
589
    if(cc>64)
590
    {
591
      return 0;
592
    }  
593
    for(co=0,ch=0;co<4;co++)
594
    {
595
      ch=ch+ch+htrc_getbit();
596
    }
597
    if(ch!=rpa/2)return 0;
598
    if(memcmp(res,tres,sizeof(res))==0)readcount++; else readcount=1;
599
    memcpy(res,tres,sizeof(res));
600
    return 1;
601
  }
602
}
603
/* store tag in eeprom */
604
void wtag(co)
605
{
606
  unsigned char tp[1];
607
608
  if(co==255)
609
    for(co=0;co<(KEYCOUNT-1);co++)
610
       {
611
      iic_eeprom_get_mem(tp,co*8+KEYPOS,1);
612
          if(tp[0]==255)break;
613
      }
614
  iic_eeprom_put_mem(co*8+KEYPOS,res,8);
615
    tm[0]=254;  
616
    memcpy(tm+1,res,5);
617
   dolog();
618
    readtime();
619
    tm[5]=ST_NEW;
620
  tm[6]=co;
621
    dolog();
622
}
623
624
/* find tag in eeprom */
625
unsigned char ftag()
626
{
627
  unsigned char co,tp[8];
628
629
  for(co=0;co<KEYCOUNT;co++)
630
  {
631
    do_wd();   
632
    iic_eeprom_get_mem(tp,co*8+KEYPOS,8); 
633
    if(memcmp(tp,res,5)==0)return co;   
634
  }
635
  return 255;
636
}
637
void import()
638
{
639
    unsigned char to,p,ci,co,tp[8];
640
  unsigned int c;
641
642
  to=20;
643
    putms(PSTR("Daten senden !"));
644
  while(ttime<to)
645
  {
646
    if(wgetch()!='!')continue;
647
    if(wgetch()!='*')continue;
648
    to=2;
649
      c=gethch(); if(c<0)continue;
650
    ci=p=c;
651
    for(co=0;co<8;co++)
652
      {
653
      c=gethch(); if(c<0)continue;
654
      tp[co]=c;
655
      p+=p+((p&128)?1:0);
656
      p^=c;
657
    }
658
            puthch(ci);
659
    if(p==gethch()&&co<KEYCOUNT)
660
    {
661
      putch(19);
662
      iic_eeprom_put_mem(ci*8+KEYPOS,tp,8); 
663
      putch(17);
664
         putms(PSTR("*"));
665
    } else putms(PSTR("fail\n\r"));
666
  }
667
  wtast();
668
}
669
670
void export()
671
{
672
    unsigned char p,ci,co,tp[8];
673
674
    putms(PSTR("***"));
675
    for(co=0;co<KEYCOUNT;co++)
676
    {
677
    do_wd();
678
    iic_eeprom_get_mem(tp,co*8+KEYPOS,8); 
679
    if(tp[0]!=255)
680
    {
681
        putms(PSTR("\n\r!*"));
682
            puthch(co);p=co;
683
      for(ci=0;ci<8;ci++)
684
      {
685
        puthch(tp[ci]);
686
        p+=p+((p&128)?1:0);
687
        p^=tp[ci];
688
      }
689
            puthch(p);
690
      for(ci=0;ci<20;ci++)putch(' ');
691
        }
692
    }
693
    putch(13);
694
    putch(10);
695
  wtast();
696
}
697
698
699
void listtags()
700
{
701
    unsigned char fo,c,co,tp[8];
702
  unsigned char tmp;
703
704
     putch(13);
705
    putch(10);
706
707
  for(fo=0,co=0;co<KEYCOUNT;co++)
708
    {
709
    do_wd();
710
    iic_eeprom_get_mem(tp,co*8+KEYPOS,8);
711
    if(tp[0]!=255)
712
    {
713
      fo=1;
714
            putch(co%8+48);
715
            putch(32);
716
            putch(co/10+48);
717
            putch(co%10+48);
718
            putch(32);
719
            puthch(tp[0]);
720
            puthch(tp[1]);
721
            puthch(tp[2]);
722
            puthch(tp[3]);
723
            puthch(tp[4]);
724
            putch(32);
725
            putch(tp[5]);
726
            putch(tp[6]);
727
            putch(tp[7]);
728
             putch(13);
729
            putch(10);
730
        }
731
    if((co%8==7)&&fo)
732
    {
733
      fo=0;
734
      do_wd();
735
        putms(PSTR("[ENTER] or d[0-7]"));    
736
      c=getstr();
737
      if(c==2)
738
      {
739
        if(spu[0]=='d'&&spu[1]>47&&spu[1]<56)
740
        {
741
          co-=8;
742
          putms(PSTR("delete"));  
743
          
744
          iic_eeprom_get_mem(tp,tmp*8+KEYPOS,8);
745
            tm[0]=254;
746
            memcpy(tm+1,tp,5);
747
            dolog();
748
            readtime();
749
            tm[5]=ST_DELETE;
750
            tm[6]=tmp;
751
            dolog();  
752
          tp[0]=255;
753
          tmp=co+spu[1]-47;
754
          iic_eeprom_put_mem(tmp*8+KEYPOS,tp,1);
755
        }
756
      }
757
      putms(PSTR("\n\r"));
758
    }
759
    }
760
}
761
void settime()
762
{
763
  putms(PSTR("Zeit hhmmTTMMY[enter]: "));
764
  
765
  if(getstr()==9)
766
  {
767
    
768
    iic_start();
769
    iic_putc(0xa2);
770
    iic_putc(0x00);
771
    iic_putc(0x88);
772
    iic_putc(0x00);
773
    iic_putc(0x00);
774
    iic_putc((spu[2]-48)*16+(spu[3]-48));
775
    iic_putc((spu[0]-48)*16+(spu[1]-48));
776
    iic_putc((spu[4]-48)*16+(spu[5]-48));
777
    iic_putc((spu[6]-48)*16+(spu[7]-48)+64*(spu[8]-48));
778
    iic_stop();
779
    iic_start();
780
    iic_putc(0xa2);
781
    iic_putc(0x00);
782
    iic_putc(0x08);
783
    iic_stop();
784
    putms(PSTR("OK\n\r"));
785
  }  
786
}
787
void newtransponder()
788
{
789
  unsigned char c,tok;
790
791
    putms(PSTR("\n\rTransponder einlesen "));
792
  tok=0;
793
  while(tok==0)
794
  {   
795
    while(readrfid())
796
    {
797
      tok=1;
798
      putch(13);
799
      putch(10);
800
      puthch(res[0]);
801
           puthch(res[1]);
802
              puthch(res[2]);
803
          puthch(res[3]);
804
           puthch(res[4]);
805
    
806
         putms(PSTR(" Name or [enter]: "));
807
    }
808
  }
809
  if((c=getstr()))
810
  {
811
    strcat((char *)spu,"  "); 
812
    res[5]=spu[0];
813
    res[6]=spu[1];
814
    res[7]=spu[2];
815
    if(ftag()==255)
816
    {
817
      wtag(255);
818
      putms(PSTR(" stored \n\r"));
819
    }      
820
  }  
821
}
822
823
824
void dolog()
825
{
826
  unsigned int logpos;
827
  iic_start();
828
  iic_putc(CSE_CLOCK);
829
  iic_putc(LOGPPOS);
830
  iic_start();
831
  iic_putc(CSA_CLOCK);
832
  logpos=iic_getc(0);
833
  logpos=(logpos<<8)+iic_getc(1);
834
  iic_stop();
835
836
  logpos--; if(logpos>LOGCOUNT)logpos=LOGCOUNT-1;
837
838
    iic_start();
839
    iic_putc(CSE_CLOCK);
840
    iic_putc(LOGPPOS);
841
    iic_putc(logpos>>8);
842
    iic_putc(logpos&255);
843
    iic_stop();
844
  
845
  iic_eeprom_put_mem(LOGPOS+logpos*EEBLOCKL,tm,EEBLOCKL);
846
847
}
848
849
850
void showlog()
851
{
852
  unsigned int co;
853
  unsigned char ci,lc;
854
  unsigned int logpos;
855
856
  iic_start();
857
  iic_putc(CSE_CLOCK);
858
  iic_putc(LOGPPOS);
859
  iic_start();
860
  iic_putc(CSA_CLOCK);
861
  logpos=iic_getc(0);
862
  logpos=(logpos<<8)+iic_getc(1);
863
  iic_stop();
864
  
865
  for(lc=1,co=0;co<LOGCOUNT;co++,lc++)
866
  {
867
    do_wd();
868
    iic_eeprom_get_mem(tm,LOGPOS+((co+logpos)%LOGCOUNT)*EEBLOCKL,EEBLOCKL);
869
    if(lc>14)
870
    {
871
      lc=1;
872
      if(wtast()!=32)break;
873
      putch(13);  
874
      putch(10);  
875
    }
876
    if(tm[0]==254)
877
    {
878
      putms(PSTR(" ")); 
879
      for(ci=0;ci<5;ci++) puthch(tm[ci+1]);    
880
      putch(13);  
881
      putch(10);  
882
    }
883
    else
884
    {
885
      lc-=puttm();
886
    }
887
  }
888
  readtime();
889
}
890
void mainmenu()
891
{
892
  unsigned char c,pr=1;
893
894
      while(ttime<20)
895
   {    
896
    if(pr)
897
    {
898
      pr=0;
899
        putms(PSTR("\n\rHauptmenu\n\r\n"));
900
         putms(PSTR("New Tag   : n\n\r"));
901
        putms(PSTR("List Tags : l\n\r"));
902
        putms(PSTR("set time  : t\n\r"));
903
        putms(PSTR("Export    : e\n\r"));
904
        putms(PSTR("Import    : I\n\r"));
905
        putms(PSTR("InitHTRC  : c\n\r"));
906
        putms(PSTR("Show Log  : s\n\r"));
907
        putms(PSTR("Quit      : q\n\r"));
908
    }
909
    if(havech())
910
        {
911
          c=getch();
912
      switch(c)
913
      {
914
        default : putch(7);break;
915
        case 'q': return ;  
916
        case 'l': pr=1;listtags();break;  
917
        case 'n': pr=1;newtransponder();break;  
918
        case 't': pr=1;settime();break;  
919
        case 'e': pr=1;export();break;  
920
        case 'I': pr=1;import();break;  
921
        case 'c': pr=1;htrc_init();wtast();break;  
922
        case 's': pr=1;showlog();break;  
923
      }
924
    }
925
  }
926
}
927
928
void putbch(unsigned char c)
929
{
930
  unsigned char co;
931
932
  for(co=0;co<8;co++)
933
  {
934
    putch(c&128?49:48);
935
    c<<=1;
936
  }
937
}
938
939
unsigned char htrc_com(unsigned char ci)
940
{
941
  unsigned char co,c=0;
942
943
  HTRC_CLK1;
944
  HTRC_DI0;
945
  HTRC_DI1;
946
  HTRC_CLK0;
947
948
  for(co=0;co<8;co++)
949
  {
950
     if(ci&128)HTRC_DI1;else HTRC_DI0;
951
    HTRC_CLK1;
952
    HTRC_CLK0;
953
    ci<<=1;
954
  }
955
  for(co=0;co<8;co++)
956
  {
957
    c<<=1;
958
     if(HTRC_DOq)c|=1;
959
    HTRC_CLK1;
960
    HTRC_CLK0;
961
  }
962
  return c;
963
}
964
965
void htrc_scan()
966
{
967
  HTRC_CLK1;
968
    HTRC_DI0;
969
    HTRC_DI1;
970
    HTRC_CLK0;
971
    HTRC_DI1;
972
  HTRC_CLK1;
973
  HTRC_CLK0;
974
  HTRC_CLK1;
975
  HTRC_CLK0;
976
  HTRC_CLK1;
977
  HTRC_CLK0;
978
}
979
980
void htrc_init()
981
{
982
  unsigned char c;
983
  
984
  HTRC_CLKo; 
985
  HTRC_DIo; 
986
  htrc_com(3*16+64+0); //set HTRC-clock
987
  _delay_ms(10);
988
  htrc_com(0*16+64+12+2);
989
  htrc_com(1*16+64+0);
990
  htrc_com(2*16+64+0);
991
  htrc_com(3*16+64+0); //set HTRC-clock
992
  _delay_ms(10);
993
994
  //settling
995
  htrc_com(2*16+64+11);
996
  _delay_ms(4);
997
  htrc_com(2*16+64+8);
998
  _delay_ms(1);
999
  htrc_com(2*16+64+0);
1000
  
1001
  c=htrc_com(8);
1002
  putms(PSTR("\n\rPhase      "));  
1003
  puthch(c);
1004
1005
#define toff 0x38
1006
1007
  htrc_com(128+(((c*2)+toff)&0x3f)); // sampletime berechnen
1008
1009
  putms(PSTR("\n\rSampletime "));  
1010
  puthch(htrc_com(2));
1011
  putms(PSTR("\n\rPage 0     "));  
1012
  putbch(htrc_com(4));
1013
  putms(PSTR("\n\rPage 1     "));  
1014
  putbch(htrc_com(5));
1015
  putms(PSTR("\n\rPage 2     "));  
1016
  putbch(htrc_com(6));
1017
  putms(PSTR("\n\rPage 3     "));  
1018
  putbch(htrc_com(7));
1019
1020
  putch(13);
1021
  putch(10);
1022
1023
  htrc_scan();
1024
}
1025
void readtime()
1026
{
1027
  unsigned char co;
1028
1029
  iic_start();
1030
  iic_putc(0xa2);
1031
  iic_putc(0x00);
1032
  iic_putc(0x48);
1033
  iic_start();
1034
  iic_putc(0xa2);
1035
  iic_putc(0x02);
1036
  iic_start();
1037
  iic_putc(0xa3);
1038
  for(co=0;co<4;co++)
1039
    tm[co]=iic_getc(0);
1040
  tm[co]=iic_getc(1);
1041
  iic_start();
1042
  iic_putc(0xa2);
1043
  iic_putc(0x00);
1044
  iic_putc(0x08);
1045
  iic_start();
1046
  iic_putc(0xa2);
1047
  iic_putc(RESTIMEPOS);
1048
  for(co=0;co<5;co++)
1049
    iic_putc(tm[co]);
1050
  iic_stop();
1051
  tm[5]=0;
1052
}
1053
void readrestime()
1054
{
1055
  unsigned char co;
1056
1057
  iic_start();
1058
  iic_putc(0xa2);
1059
  iic_putc(RESTIMEPOS);
1060
  iic_start();
1061
  iic_putc(0xa3);
1062
  for(co=0;co<4;co++)
1063
    tm[co]=iic_getc(0);
1064
  tm[co]=iic_getc(1);
1065
  iic_stop();
1066
}
1067
unsigned char puttm()
1068
{
1069
  unsigned char tmp[8];
1070
1071
  puthch(tm[3]);
1072
  putch('.');
1073
  puthch(tm[4]);
1074
  putch(' ');
1075
  puthch(tm[2]);
1076
  putch(':');
1077
  puthch(tm[1]);
1078
  putch(':');
1079
  puthch(tm[0]);
1080
1081
  switch(tm[5])
1082
  {
1083
    case ST_RESET:
1084
            putms(PSTR(" Reset"));
1085
            break;     
1086
    case ST_OFF:
1087
            putms(PSTR(" OFF"));
1088
            break;     
1089
    case ST_ACCEPT:
1090
            putms(PSTR(" ACCEPT "));
1091
            putdch(tm[6]);
1092
            iic_eeprom_get_mem(tmp,tm[6]*8+KEYPOS,8);
1093
            putch(32);
1094
            putch(tmp[5]);
1095
            putch(tmp[6]);
1096
            putch(tmp[7]);
1097
            break;     
1098
    case ST_DENY:
1099
            putms(PSTR(" DENY"));
1100
            return 1;     
1101
    case ST_NEW:
1102
            putms(PSTR(" NEW "));
1103
            putdch(tm[6]);
1104
            return 1;     
1105
    case ST_DELETE:
1106
            putms(PSTR(" DEL "));
1107
            putdch(tm[6]);
1108
            return 1;     
1109
  }   
1110
1111
  putch(13);
1112
  putch(10);
1113
  return 0;
1114
}
1115
1116
int main()
1117
{
1118
  unsigned char ph,co,c,failbuf[5];
1119
1120
    TCCR2=5;
1121
    TCNT2=0;
1122
    TIMSK|=_BV(TOIE2); 
1123
1124
    UCSRA |= ( 1 << U2X );
1125
    PORTD |= ( 1 << PD0 );
1126
    UCSRB |= ( 1 << TXEN )|  ( 1 << RXEN );                         // UART TX einschalten
1127
    UCSRC |= ( 1 << URSEL )|( 3<<UCSZ0 );           // Asynchron 8N1
1128
    UBRRH  = 0;                                     // Highbyte ist 0
1129
    UBRRL  = 12;  
1130
1131
  wdt_enable(WDTO_500MS);
1132
1133
  MCUCR|=_BV(ISC00);
1134
  GICR|=_BV(INT0);
1135
  TCCR0=2;  
1136
1137
  LEDo;
1138
  TIMSK|= _BV(TOIE0);   
1139
  sei();
1140
1141
  htrc_init();
1142
1143
  REL0;  //  relais off
1144
  RELo;  //  DDR
1145
  BEEP0;  //  beeper off
1146
  BEEPo;  //  DDR
1147
  LED1;  //  LED on
1148
  LEDo;   //  DDR
1149
1150
    //iicinit
1151
  iic_start();
1152
  iic_stop();
1153
1154
  readrestime();
1155
  tm[5]=ST_OFF;
1156
  dolog();
1157
  readtime();
1158
  tm[5]=ST_RESET;
1159
  dolog();
1160
  while(1)
1161
  {
1162
    if(havech())
1163
    {
1164
      c=getch();
1165
      if(c=='p')
1166
      {
1167
#ifdef DEBUGHTRC
1168
        ph++;
1169
        ph&=63;
1170
        htrc_com(128+ph);
1171
        htrc_scan();
1172
         putms(PSTR("Phase "));
1173
        puthch(ph);
1174
        putms(PSTR("\n\r"));
1175
#endif
1176
      } else
1177
      if(c==27)
1178
      {
1179
        mainmenu();
1180
        putms(PSTR("Ready"));
1181
        putms(PSTR("\n\r"));
1182
      } else putms(PSTR("[ESC]->menu\n\r"));
1183
    }
1184
    
1185
    if(distime>29)
1186
    {
1187
      distime=0;
1188
      readtime();
1189
      puttm();
1190
    }
1191
    if(readrfid())
1192
    {
1193
      for(co=0;co<10;co++)
1194
        puthch(res[co]);
1195
      putch(32);
1196
      if(rfmode)ppu();
1197
      if(readcount>2)
1198
      {
1199
        c=ftag();
1200
        if(c!=255)
1201
        {
1202
           tm[5]=ST_ACCEPT;
1203
          tm[6]=c;
1204
          dolog();   
1205
              putms(PSTR(" no. 0x"));
1206
    
1207
          puthch(c);
1208
          btime=0;
1209
          REL1; // relais on
1210
          BEEP1; // beep on
1211
          while(btime<3)do_wd();
1212
          BEEP0; // beep off
1213
          while(btime<90)do_wd();
1214
          REL0; // relais off
1215
        }else 
1216
        {
1217
          if(failtimeout==0)
1218
          {
1219
            failtimeout=60;
1220
            
1221
            tm[0]=254;
1222
            memcpy(tm+1,res,5);
1223
            memcpy(failbuf,res,5);
1224
            dolog();
1225
            readtime();
1226
            tm[5]=ST_DENY;
1227
            dolog();
1228
          } else
1229
          {
1230
             if(memcmp(failbuf,res,5)==0)failtimeout=30;   
1231
          }
1232
          btime=0;
1233
          BEEP1;
1234
          while(btime<30)do_wd();
1235
          BEEP0;
1236
        }  
1237
      }
1238
      putch(13);
1239
      putch(10);
1240
    }
1241
  }
1242
}