1 |
633c646a
|
henryk
|
/***************************************************************
|
2 |
|
|
*
|
3 |
|
|
* OpenBeacon.org - main entry for 2.4GHz RFID USB reader
|
4 |
|
|
*
|
5 |
|
|
* Copyright 2007 Milosch Meriac <meriac@openbeacon.de>
|
6 |
|
|
*
|
7 |
|
|
* basically starts the USB task, initializes all IO ports
|
8 |
|
|
* and introduces idle application hook to handle the HF traffic
|
9 |
|
|
* from the nRF24L01 chip
|
10 |
|
|
*
|
11 |
|
|
***************************************************************
|
12 |
|
|
|
13 |
|
|
This program is free software; you can redistribute it and/or modify
|
14 |
|
|
it under the terms of the GNU General Public License as published by
|
15 |
|
|
the Free Software Foundation; version 2.
|
16 |
|
|
|
17 |
|
|
This program is distributed in the hope that it will be useful,
|
18 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20 |
|
|
GNU General Public License for more details.
|
21 |
|
|
|
22 |
|
|
You should have received a copy of the GNU General Public License along
|
23 |
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
24 |
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
25 |
|
|
|
26 |
|
|
*/
|
27 |
|
|
/* Library includes. */
|
28 |
|
|
#include <string.h>
|
29 |
|
|
#include <stdio.h>
|
30 |
|
|
|
31 |
|
|
#include <FreeRTOS.h>
|
32 |
|
|
#include <AT91SAM7.h>
|
33 |
ad5b96d5
|
henryk
|
#include <lib_AT91SAM7.h>
|
34 |
633c646a
|
henryk
|
#include <USB-CDC.h>
|
35 |
|
|
#include <task.h>
|
36 |
|
|
|
37 |
|
|
#include "openpicc.h"
|
38 |
|
|
#include "board.h"
|
39 |
|
|
#include "led.h"
|
40 |
|
|
#include "env.h"
|
41 |
|
|
#include "cmd.h"
|
42 |
ee25d9a0
|
henryk
|
#include "da.h"
|
43 |
ee1f0214
|
henryk
|
#include "adc.h"
|
44 |
9d397dfb
|
henryk
|
#include "pll.h"
|
45 |
|
|
#include "pio_irq.h"
|
46 |
ee1f0214
|
henryk
|
#include "tc_cdiv.h"
|
47 |
3d0a9bd4
|
henryk
|
#include "tc_cdiv_sync.h"
|
48 |
3fb02f71
|
henryk
|
#include "tc_fdt.h"
|
49 |
c63871ac
|
henryk
|
#include "usb_print.h"
|
50 |
616746c2
|
henryk
|
#include "iso14443_layer3a.h"
|
51 |
6be69f0d
|
henryk
|
#include "iso14443_sniffer.h"
|
52 |
d3bab6e9
|
henryk
|
#include "iso14443a_pretender.h"
|
53 |
16d42d5f
|
henryk
|
#include "decoder.h"
|
54 |
3ee3c4a6
|
henryk
|
#include "tc_sniffer.h"
|
55 |
9cde1dd0
|
henryk
|
#include "performance.h"
|
56 |
633c646a
|
henryk
|
|
57 |
ad5b96d5
|
henryk
|
static inline int detect_board(void)
|
58 |
|
|
{
|
59 |
|
|
/* OpenPICC board detection logic.
|
60 |
|
|
* Interesting board differences: PA31 is open on OPENPICC_v0_4 and connected
|
61 |
|
|
* to PA18 on OPENPICC_v0_4_p1. PA18 is connected to U7 on both and might read
|
62 |
|
|
* differently depending on the state of U7 (primarily depending on U5 and the
|
63 |
|
|
* receive circuitry).
|
64 |
|
|
* Strategy: Enable Pullups, read PA31 and PA18, if both read low then U7 is
|
65 |
|
|
* switched through and this is an v0.4p1. If PA18 reads low and PA31 reads high
|
66 |
|
|
* then U7 is switched through and this is an v0.4. If both read high, then U7 is
|
67 |
|
|
* not switched through and it might be either board. In this case drive PA31 down
|
68 |
|
|
* and see whether PA18 follows down, then it's a v0.4p1 otherwise a v0.4.
|
69 |
|
|
*/
|
70 |
|
|
int result = -1;
|
71 |
|
|
|
72 |
|
|
AT91PS_PIO pio = AT91C_BASE_PIOA;
|
73 |
|
|
u_int32_t old_OSR = pio->PIO_OSR,
|
74 |
|
|
old_ODSR = pio->PIO_ODSR,
|
75 |
|
|
old_PUSR = pio->PIO_PPUSR,
|
76 |
|
|
old_PSR = pio->PIO_PSR;
|
77 |
|
|
|
78 |
|
|
pio->PIO_ODR = AT91C_PIO_PA18 | AT91C_PIO_PA31;
|
79 |
|
|
pio->PIO_PER = AT91C_PIO_PA18 | AT91C_PIO_PA31;
|
80 |
|
|
pio->PIO_PPUER = AT91C_PIO_PA18 | AT91C_PIO_PA31;
|
81 |
|
|
|
82 |
|
|
unsigned int pa18 = AT91F_PIO_IsInputSet(pio, AT91C_PIO_PA18),
|
83 |
|
|
pa31 = AT91F_PIO_IsInputSet(pio, AT91C_PIO_PA31);
|
84 |
|
|
if(!pa18 && !pa31) {
|
85 |
|
|
//vLedInit();
|
86 |
|
|
//vLedHaltBlinking(1);
|
87 |
8134c3aa
|
henryk
|
result = OPENPICC_v0_4_karsten;
|
88 |
ad5b96d5
|
henryk
|
} else if(!pa18 && pa31) {
|
89 |
|
|
vLedInit();
|
90 |
|
|
vLedHaltBlinking(2);
|
91 |
|
|
// Needs to be tested, should be v0.4
|
92 |
|
|
} else if(pa18 && pa31) {
|
93 |
|
|
// Can be either board
|
94 |
|
|
pio->PIO_OER = AT91C_PIO_PA31;
|
95 |
|
|
pio->PIO_CODR = AT91C_PIO_PA31;
|
96 |
|
|
pa18 = AT91F_PIO_IsInputSet(pio, AT91C_PIO_PA18);
|
97 |
|
|
if(!pa18) {
|
98 |
8134c3aa
|
henryk
|
result = OPENPICC_v0_4_karsten;
|
99 |
ad5b96d5
|
henryk
|
} else {
|
100 |
|
|
vLedInit();
|
101 |
|
|
vLedHaltBlinking(3);
|
102 |
|
|
// Needs to be tested, should be v0.4
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
// Restore state
|
106 |
|
|
if( old_OSR & AT91C_PIO_PA31 ) {
|
107 |
|
|
pio->PIO_OER = AT91C_PIO_PA31;
|
108 |
|
|
if(old_ODSR & AT91C_PIO_PA31) {
|
109 |
|
|
pio->PIO_SODR = AT91C_PIO_PA31;
|
110 |
|
|
} else {
|
111 |
|
|
pio->PIO_CODR = AT91C_PIO_PA31;
|
112 |
|
|
}
|
113 |
|
|
} else {
|
114 |
|
|
pio->PIO_ODR = AT91C_PIO_PA31;
|
115 |
|
|
}
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
// Restore state
|
119 |
|
|
if(old_PSR & AT91C_PIO_PA18) pio->PIO_PER = AT91C_PIO_PA18; else pio->PIO_PDR = AT91C_PIO_PA18;
|
120 |
|
|
if(old_PSR & AT91C_PIO_PA31) pio->PIO_PER = AT91C_PIO_PA31; else pio->PIO_PDR = AT91C_PIO_PA31;
|
121 |
|
|
|
122 |
|
|
if(old_PUSR & AT91C_PIO_PA18) pio->PIO_PPUDR = AT91C_PIO_PA18; else pio->PIO_PPUER = AT91C_PIO_PA18;
|
123 |
|
|
if(old_PUSR & AT91C_PIO_PA31) pio->PIO_PPUDR = AT91C_PIO_PA31; else pio->PIO_PPUER = AT91C_PIO_PA31;
|
124 |
|
|
|
125 |
|
|
return result;
|
126 |
|
|
}
|
127 |
|
|
|
128 |
633c646a
|
henryk
|
/**********************************************************************/
|
129 |
|
|
static inline void prvSetupHardware (void)
|
130 |
|
|
{
|
131 |
a72e0c20
|
henryk
|
/* The very, very first thing we do is setup the global OPENPICC variable to point to
|
132 |
|
|
* the correct hardware information.
|
133 |
|
|
*/
|
134 |
ad5b96d5
|
henryk
|
int release = detect_board();
|
135 |
|
|
if(release < 0) {
|
136 |
|
|
vLedInit();
|
137 |
|
|
vLedHaltBlinking(0);
|
138 |
|
|
}
|
139 |
|
|
OPENPICC = &OPENPICC_HARDWARE[release];
|
140 |
a72e0c20
|
henryk
|
|
141 |
|
|
|
142 |
633c646a
|
henryk
|
/* When using the JTAG debugger the hardware is not always initialised to
|
143 |
|
|
the correct default state. This line just ensures that this does not
|
144 |
|
|
cause all interrupts to be masked at the start. */
|
145 |
|
|
AT91C_BASE_AIC->AIC_EOICR = 0;
|
146 |
|
|
|
147 |
|
|
/* Enable the peripheral clock. */
|
148 |
|
|
AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOA;
|
149 |
|
|
AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOB;
|
150 |
|
|
|
151 |
|
|
/* initialize environment variables */
|
152 |
|
|
env_init();
|
153 |
|
|
if(!env_load())
|
154 |
|
|
{
|
155 |
|
|
env.e.mode=0;
|
156 |
|
|
env.e.reader_id=255;
|
157 |
|
|
env_store();
|
158 |
|
|
}
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
/**********************************************************************/
|
162 |
|
|
void vApplicationIdleHook(void)
|
163 |
|
|
{
|
164 |
|
|
static char disabled_green = 0;
|
165 |
5cc0ed49
|
henryk
|
//static int i=0;
|
166 |
|
|
//vLedSetGreen(i^=1);
|
167 |
633c646a
|
henryk
|
if(!disabled_green) {
|
168 |
|
|
//vLedSetGreen(0);
|
169 |
|
|
disabled_green = 1;
|
170 |
|
|
}
|
171 |
|
|
}
|
172 |
|
|
|
173 |
16d42d5f
|
henryk
|
/* This task pings the watchdog even when the idle task is not running
|
174 |
|
|
* It should be started with a very high priority and will delay most of the time */
|
175 |
|
|
void vMainWatchdogPinger (void *pvParameters)
|
176 |
|
|
{
|
177 |
|
|
(void)pvParameters;
|
178 |
|
|
|
179 |
|
|
while(1) {
|
180 |
|
|
/* Restart watchdog, has been enabled in Cstartup_SAM7.c */
|
181 |
|
|
AT91F_WDTRestart(AT91C_BASE_WDTC);
|
182 |
|
|
vTaskDelay(500*portTICK_RATE_MS);
|
183 |
|
|
}
|
184 |
|
|
}
|
185 |
|
|
|
186 |
ad5b96d5
|
henryk
|
void usb_print_flusher (void *pvParameters)
|
187 |
|
|
{
|
188 |
|
|
(void)pvParameters;
|
189 |
|
|
while(1) {
|
190 |
|
|
usb_print_flush();
|
191 |
|
|
vTaskDelay(100*portTICK_RATE_MS);
|
192 |
|
|
}
|
193 |
|
|
}
|
194 |
|
|
|
195 |
633c646a
|
henryk
|
/**********************************************************************/
|
196 |
|
|
int main (void)
|
197 |
|
|
{
|
198 |
|
|
prvSetupHardware ();
|
199 |
c63871ac
|
henryk
|
usb_print_init();
|
200 |
16d42d5f
|
henryk
|
decoder_init();
|
201 |
9cde1dd0
|
henryk
|
performance_init();
|
202 |
9d397dfb
|
henryk
|
|
203 |
|
|
pio_irq_init();
|
204 |
1dfa375a
|
henryk
|
|
205 |
633c646a
|
henryk
|
vLedInit();
|
206 |
|
|
|
207 |
ee25d9a0
|
henryk
|
da_init();
|
208 |
ee1f0214
|
henryk
|
adc_init();
|
209 |
ee25d9a0
|
henryk
|
|
210 |
ad5b96d5
|
henryk
|
xTaskCreate (usb_print_flusher, (signed portCHAR *) "PRINT-FLUSH", TASK_USB_STACK,
|
211 |
|
|
NULL, TASK_USB_PRIORITY, NULL);
|
212 |
6be69f0d
|
henryk
|
/*xTaskCreate (iso14443_layer3a_state_machine, (signed portCHAR *) "ISO14443A-3", TASK_ISO_STACK,
|
213 |
|
|
NULL, TASK_ISO_PRIORITY, NULL);*/
|
214 |
f7a420f2
|
henryk
|
/*xTaskCreate (iso14443_sniffer, (signed portCHAR *) "ISO14443-SNIFF", TASK_ISO_STACK,
|
215 |
3ee3c4a6
|
henryk
|
NULL, TASK_ISO_PRIORITY, NULL);*/
|
216 |
f7a420f2
|
henryk
|
xTaskCreate (iso14443a_pretender, (signed portCHAR *) "ISO14443A-PRETEND", TASK_ISO_STACK,
|
217 |
|
|
NULL, TASK_ISO_PRIORITY, NULL);
|
218 |
e2e37bea
|
henryk
|
/*xTaskCreate (tc_sniffer, (signed portCHAR *) "RFID-SNIFFER", TASK_ISO_STACK,
|
219 |
|
|
NULL, TASK_ISO_PRIORITY, NULL);*/
|
220 |
3ee3c4a6
|
henryk
|
|
221 |
|
|
|
222 |
633c646a
|
henryk
|
xTaskCreate (vUSBCDCTask, (signed portCHAR *) "USB", TASK_USB_STACK,
|
223 |
|
|
NULL, TASK_USB_PRIORITY, NULL);
|
224 |
|
|
|
225 |
|
|
vCmdInit();
|
226 |
|
|
|
227 |
16d42d5f
|
henryk
|
xTaskCreate (vMainWatchdogPinger, (signed portCHAR *) "WDT PINGER", 64,
|
228 |
|
|
NULL, TASK_ISO_PRIORITY -1, NULL);
|
229 |
|
|
|
230 |
062f55ca
|
henryk
|
//vLedSetGreen(1);
|
231 |
|
|
|
232 |
|
|
/* Remap RAM to addr 0 */
|
233 |
|
|
AT91C_BASE_MC->MC_RCR = AT91C_MC_RCB;
|
234 |
633c646a
|
henryk
|
|
235 |
|
|
vTaskStartScheduler ();
|
236 |
|
|
|
237 |
|
|
return 0;
|
238 |
|
|
}
|