paths changed
[kk_librfid.git] / firmware / src / pcd / .svn / text-base / main_presence.c.svn-base
blobf61878fe1eb9c26b867b9aba9b3b2d7fa267a91b
1 /* AT91SAM7 "presence reader" firmware for OpenPCD
2  *
3  * (C) 2006 by Milosch Meriac <meriac@openpcd.de>
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by 
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
21 #include <errno.h>
22 #include <string.h>
23 #include <lib_AT91SAM7.h>
24 #include <librfid/rfid_layer2_iso14443a.h>
25 #include "rc632.h"
26 #include <os/dbgu.h>
27 #include <os/led.h>
28 #include <os/pcd_enumerate.h>
29 #include <os/usb_handler.h>
30 #include <pcd/rc632_highlevel.h>
32 #include <librfid/rfid_reader.h>
33 #include <librfid/rfid_layer2.h>
35 #include "../openpcd.h"
36 #include <os/main.h>
38 #define RAH NULL
40 u_int32_t delay_scan,delay_blink,last_uid,last_polled_uid;
41 static struct rfid_reader_handle *rh;
42 static struct rfid_layer2_handle *l2h;
44 static int usb_presence_rx(struct req_ctx *rctx)
46     struct openpcd_hdr *poh = (struct openpcd_hdr *) rctx->data;
48     switch (poh->cmd)
49     {
50         case OPENPCD_CMD_PRESENCE_UID_GET:
51                 DEBUGPCRF("get presence UID");  
52                 
53                 poh->flags |= OPENPCD_FLAG_RESPOND;
54                 if(last_polled_uid)
55                 {
56                     rctx->tot_len += 4;
57                     poh->data[0]=(u_int8_t)(last_polled_uid>>24);
58                     poh->data[1]=(u_int8_t)(last_polled_uid>>16);
59                     poh->data[2]=(u_int8_t)(last_polled_uid>> 8);
60                     poh->data[3]=(u_int8_t)(last_polled_uid    );
61                     last_polled_uid=0;
62                 }
63                 break;
64         default:
65                 DEBUGP("UNKNOWN ");
66                 return USB_ERR(USB_ERR_CMD_UNKNOWN);
67                 break;
68     }
69         
70     if (poh->flags & OPENPCD_FLAG_RESPOND)
71         return USB_RET_RESPOND;
73     return 0;
76 void _init_func(void)
78         DEBUGPCRF("enabling RC632");    
79         rc632_init();
81         DEBUGPCRF("turning on RF");
82         rc632_turn_on_rf(RAH);
84         DEBUGPCRF("initializing 14443A operation");
85         rh = rfid_reader_open(NULL, RFID_READER_OPENPCD);
86         l2h = rfid_layer2_init(rh, RFID_LAYER2_ISO14443A);
87                 
88         DEBUGPCRF("registering USB handler");
89         usb_hdlr_register(&usb_presence_rx, OPENPCD_CMD_CLS_PRESENCE);
90         
91         delay_scan=delay_blink=0;
92         last_uid=0;
95 int _main_dbgu(char key)
97         unsigned char value;
99         switch (key) {
100         case '4':
101                 AT91F_DBGU_Printk("Testing RC632 : ");
102                 if (rc632_test(RAH) == 0)
103                         AT91F_DBGU_Printk("SUCCESS!\n\r");
104                 else
105                         AT91F_DBGU_Printk("ERROR!\n\r");
106                         
107                 break;
108         case '5':
109                 opcd_rc632_reg_read(RAH, RC632_REG_RX_WAIT, &value);
110                 DEBUGPCR("Reading RC632 Reg RxWait: 0x%02xr", value);
112                 break;
113         case '6':
114                 DEBUGPCR("Writing RC632 Reg RxWait: 0x55");
115                 opcd_rc632_reg_write(RAH, RC632_REG_RX_WAIT, 0x55);
116                 break;
117         case '7':
118                 rc632_dump();
119                 break;
120         }
122         return -EINVAL;
125 void _main_func(void)
127         u_int32_t uid;
128         int status;
129         
130         status = rfid_layer2_open(l2h);
131         if (status >= 0  && l2h->uid_len==4)
132         {
133             uid=((u_int32_t)l2h->uid[0])    |
134                 ((u_int32_t)l2h->uid[1])<< 8|
135                 ((u_int32_t)l2h->uid[2])<<16|
136                 ((u_int32_t)l2h->uid[3])<<24;
137                         
138             delay_scan=100;
139                 
140             if(uid!=last_uid)
141             {
142                 last_uid=last_polled_uid=uid;
143                 delay_blink=10;
144                     
145                 DEBUGPCR("UID:0x%08X", uid);
146             }
147         }
148         else
149             if(delay_scan)
150                 delay_scan--;
151             else
152                 last_uid=0;
153             
154         led_switch(1,(delay_blink==0)?1:0);
155         if(delay_blink)
156             delay_blink--;
157         
158         /* first we try to get rid of pending to-be-sent stuff */
159         usb_out_process();
160         /* next we deal with incoming reqyests from USB EP1 (OUT) */
161         usb_in_process();
162         rc632_unthrottle();