Blame view

main.c 4.12 KB
1a2e5ee4   henyxia   Big revision
1
2
  #include <util/delay.h>
  #include <avr/io.h>
55af57c0   henyxia   Customized versio...
3
  #include "PN532.h"
1a2e5ee4   henyxia   Big revision
4
  #include <PN532_SPI/PN532_SPI.h>
55af57c0   henyxia   Customized versio...
5
  
25fb98d3   henyxia   Rev 1
6
7
8
9
10
11
  #define	MODEL_QUERY			0x80
  #define	SERIAL_ERROR		0x55
  #define	NFC_TAGQUERY		0x82
  #define	NFC_TAGQUERY_UID	0x84
  #define	NFC_ARDUINO			0x02
  #define	NFC_NOTAG			0x81
1a2e5ee4   henyxia   Big revision
12
  #define	NFC_TYPE_PROFESSOR	0x04
25fb98d3   henyxia   Rev 1
13
  #define	NFC_TYPE_STUDENT	0x01
1a2e5ee4   henyxia   Big revision
14
  #define	CPU_FREQ			16000000L
25fb98d3   henyxia   Rev 1
15
16
17
18
  
  boolean	tagDetected;
  boolean	tagType;
  
55af57c0   henyxia   Customized versio...
19
20
21
  PN532_SPI pn532spi(SPI, 10);
  PN532 nfc(pn532spi);
  
1a2e5ee4   henyxia   Big revision
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  void init_serial(int speed)
  {
  	UBRR0 = CPU_FREQ/(((unsigned long int)speed)<<4)-1;
  	UCSR0B = (1<<TXEN0 | 1<<RXEN0);
  	UCSR0C = (1<<UCSZ01 | 1<<UCSZ00);
  	UCSR0A &= ~(1 << U2X0);
  }
  
  void send_serial(unsigned char c)
  {
  	loop_until_bit_is_set(UCSR0A, UDRE0);
  	UDR0 = c;
  }
  
  unsigned char get_serial(void)
  {
  	loop_until_bit_is_set(UCSR0A, RXC0);
  	return UDR0;
  }
  
  
25fb98d3   henyxia   Rev 1
43
44
45
46
  void setup(void)
  {
  	int ser;
  
1a2e5ee4   henyxia   Big revision
47
  	init_serial(9600);
55af57c0   henyxia   Customized versio...
48
  
1a2e5ee4   henyxia   Big revision
49
  	send_serial('1');
55af57c0   henyxia   Customized versio...
50
51
  	nfc.begin();
  
1a2e5ee4   henyxia   Big revision
52
  	send_serial('2');
55af57c0   henyxia   Customized versio...
53
  	uint32_t versiondata = nfc.getFirmwareVersion();
25fb98d3   henyxia   Rev 1
54
  	if(!versiondata)
55af57c0   henyxia   Customized versio...
55
  	{
1a2e5ee4   henyxia   Big revision
56
  		send_serial(SERIAL_ERROR);
55af57c0   henyxia   Customized versio...
57
  		while (1);
25fb98d3   henyxia   Rev 1
58
59
  	}  
  
1a2e5ee4   henyxia   Big revision
60
  	send_serial('3');
55af57c0   henyxia   Customized versio...
61
62
  	nfc.SAMConfig();
    
25fb98d3   henyxia   Rev 1
63
64
65
66
  	tagDetected = false;
  
  	do
  	{
1a2e5ee4   henyxia   Big revision
67
  		ser = get_serial();
25fb98d3   henyxia   Rev 1
68
  		if(ser == MODEL_QUERY)
1a2e5ee4   henyxia   Big revision
69
  			send_serial(NFC_ARDUINO);
25fb98d3   henyxia   Rev 1
70
  		else
1a2e5ee4   henyxia   Big revision
71
  			send_serial(SERIAL_ERROR);
25fb98d3   henyxia   Rev 1
72
  	}while(ser != MODEL_QUERY);
55af57c0   henyxia   Customized versio...
73
74
75
76
77
78
  }
  
  
  void loop(void)
  {
  	uint8_t success;
25fb98d3   henyxia   Rev 1
79
80
81
82
  	uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
  	uint8_t uidLength;
  	uint8_t data1[16];
  	uint8_t data2[16];
1a2e5ee4   henyxia   Big revision
83
84
          char  professorTag[7];
          char  studentTag[9];
25fb98d3   henyxia   Rev 1
85
  	int ser;
55af57c0   henyxia   Customized versio...
86
      
55af57c0   henyxia   Customized versio...
87
  	success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
25fb98d3   henyxia   Rev 1
88
89
  
  	if(success && !tagDetected)
55af57c0   henyxia   Customized versio...
90
  	{
55af57c0   henyxia   Customized versio...
91
92
  		if(uidLength == 4)
  		{
55af57c0   henyxia   Customized versio...
93
  			uint8_t keya[6] = { 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 };
25fb98d3   henyxia   Rev 1
94
  			success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 44, 0, keya);  
55af57c0   henyxia   Customized versio...
95
96
  			if(success)
  			{
25fb98d3   henyxia   Rev 1
97
  				success = nfc.mifareclassic_ReadDataBlock(44, data1);
55af57c0   henyxia   Customized versio...
98
99
100
  		
  				if(success)
  				{
25fb98d3   henyxia   Rev 1
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  					//nfc.PrintHexChar(data1, 16);
  					success = nfc.mifareclassic_ReadDataBlock(45, data2);
  
  					if(success)
  					{
  						tagDetected = true;
  						if(data1[4] == data1[15] &&
  						data1[5] == data2[0] &&
  						data1[6] == data2[1] &&
  						data1[7] == data2[2] &&
  						data1[8] == data2[3] &&
  						data1[9] == data2[4])
  						{
  							tagType = NFC_TYPE_PROFESSOR;
1a2e5ee4   henyxia   Big revision
115
116
117
118
119
120
  							professorTag[0] = (char) 48 + data1[4];
  							professorTag[1] = (char) 48 + data1[5];
  							professorTag[2] = (char) 48 + data1[6];
  							professorTag[3] = (char) 48 + data1[7];
  							professorTag[4] = (char) 48 + data1[8];
  							professorTag[5] = (char) 48 + data1[9];
25fb98d3   henyxia   Rev 1
121
122
123
124
  						}
  						else
  						{
  							tagType = NFC_TYPE_STUDENT;
1a2e5ee4   henyxia   Big revision
125
126
127
128
129
130
131
132
  							studentTag[0] = (char) 48 + data1[15];
  							studentTag[1] = (char) 48 + data2[0];
  							studentTag[2] = (char) 48 + data2[1];
  							studentTag[3] = (char) 48 + data2[2];
  							studentTag[4] = (char) 48 + data2[3];
  							studentTag[5] = (char) 48 + data2[4];
  							studentTag[6] = (char) 48 + data2[5];
  							studentTag[7] = (char) 48 + data2[6];
25fb98d3   henyxia   Rev 1
133
134
  						}
  					}
55af57c0   henyxia   Customized versio...
135
136
  				}
  			}
55af57c0   henyxia   Customized versio...
137
  		}
25fb98d3   henyxia   Rev 1
138
139
140
  	}
  	else if(tagDetected)
  	{
1a2e5ee4   henyxia   Big revision
141
  		ser = get_serial();
25fb98d3   henyxia   Rev 1
142
  		if(ser == NFC_TAGQUERY)
1a2e5ee4   henyxia   Big revision
143
  			send_serial(tagType);
25fb98d3   henyxia   Rev 1
144
  		else if(ser == NFC_TAGQUERY_UID)
55af57c0   henyxia   Customized versio...
145
  		{
1a2e5ee4   henyxia   Big revision
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
                      if(tagType == NFC_TYPE_STUDENT)
                      {/*
                          Serial.print((char) studentTag[0]);
  						Serial.print((char) studentTag[1]);
                          Serial.print((char) studentTag[2]);
  						Serial.print((char) studentTag[3]);
                          Serial.print((char) studentTag[4]);
  						Serial.print((char) studentTag[5]);
                          Serial.print((char) studentTag[6]);
  						Serial.print((char) studentTag[7]);*/
                          send_serial(0x10);
  						send_serial(0x20);
                          send_serial(0x30);
  						send_serial(0x40);
                          send_serial(0x50);
  						send_serial(0x60);
  	                    send_serial(0x70);
  						send_serial(0x80);
                      }
                      else
                      {
                          send_serial(professorTag[0]);
                      }
  		    tagDetected = false;
55af57c0   henyxia   Customized versio...
170
  		}
25fb98d3   henyxia   Rev 1
171
  		else
1a2e5ee4   henyxia   Big revision
172
  			send_serial((char) SERIAL_ERROR);
25fb98d3   henyxia   Rev 1
173
174
175
  	}
  	else
  	{
1a2e5ee4   henyxia   Big revision
176
177
  		if(get_serial() == NFC_TAGQUERY)
  			send_serial(NFC_NOTAG);
55af57c0   henyxia   Customized versio...
178
179
  	}
  }
1a2e5ee4   henyxia   Big revision
180
181
182
183
184
185
186
187
188
189
190
  
  int main(void)
  {
  	setup();
  
  	while(1)
  		send_serial('Y');
  		//loop();
  
  	return 0;
  }