硬件说明:
关于RFID的基础知识
程序设计流程图
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>
#include <sys/select.h>
#include <errno.h>
#include <termios.h>
#define DEBUG 0
#define DB_MSG(m) do{\
#if DEBUG \
printf(m) \
#endif \
}while(0)
#define TTY0_PATH "/dev/s3c2410_serial0"
#define TTY1_PATH "/dev/s3c2410_serial1"
#define TTY2_PATH "/dev/s3c2410_serial2"
#define TTY3_PATH "/dev/s3c2410_serial3"
#define SERIAL_SELECTED TTY1_PATH
#define REQUEST_MODE_ALL 0x52
#define REQUEST_MODE_IDLE 0x26
#define CMDTYPE_PROTOCOL_CTRL 0x00
#define CMDTYPE_DEVICE_CTRL 0x01
#define CMDTYPE_ISO14443A 0x02
#define CMD_GETEVCINFO 'A'
#define CMD_PCDCONFIG 'B'
#define CMD_PCDCLOSE 'C'
#define ATQ_Mifarel_S50 0x0004
#define ATQ_SHC1101 0x0004
#define ATQ_11RF32 0x0004
#define ATQ_Mifarel_S70 0x0002
#define ATQ_Mifarel_Light 0x0010
#define ATQ_Mifarel_UltraLight 0x0044
#define ATQ_SHC1102 0x3300
#define ATQ_Mifare3_DESFire 0x0344
#define AVOID_COLLISION_LEVEL_1 0x93
#define AVOID_COLLISION_LEVEL_2 0x95
#define AVOID_COLLISION_LEVEL_3 0x97
#define SAK_Mifarel_S50 0x08
#define SAK_Mifarel_S70 0x18
#define SAK_Mifarel_Light 0x01
#define SAK_Mifarel0_UltraLight 0x04
#define SAK_Mifare3_DESFire 0x24
#define SAK_SHC1101 0x22
#define SAK_SHC1102 0x21
#define SAK_11RF32 0x08
/* Frame (Byte) */
#define FRAME_MIN_LENGTH 6
#define FRAME_MAX_LENGTH 54
#define DATA_BUFF_LENGTH 16
#define DEVICE_INFO_SIZE 12
/* Communication Tab */
#define STX 0x20
#define ETX 0x03
#define ACK 0x06
#define NAK 0x15
/* Error Table */
#define ERR_ACCESS_FAILED 0x11
#define ERR_TIMEOUT 0x12
#define ERR_CHECKSUM_FAILED 0x13
#define ERR_DATA_ERROR 0x14
#define ERR_ANTICOLL_FAILED 0x15
#define ERR_INVALID_PARAM 0x16
#define ERR_PACKET_MISSED 0x17
#define ERR_REQUEST_FAILED 0x18
#define ERR_LENGTH_ERROR 0x19
#define ERR_READ_FAILED 0x1a
#define ERR_OPERATION_DENIED 0x1b
#define ERR_AUTHKEY_FAILED 0x0a
#define ERR_WRITE_FAILED 0x0b
#define EXCEPTION_INCOMPLETE_UID 0x20
#define ISO14443A_Request 'A'
#define ISO14443A_CascAnticoll 'B'
#define ISO14443A_CascSelect 'C'
#define ISO14443A_Halt 'D'
#define ISO14443A_Authentication 'E'
#define ISO14443A_AuthKey 'F'
#define ISO14443A_Read 'G'
#define ISO14443A_Write 'H'
#define KEY_A 0x60
#define KEY_B 0x61
#define BLOCK_SELECTED 0x1
unsigned char key_buff[6] = {0xff,0xff,0xff,0xff,0xff,0xff};
int tty_fd[4];
#define DELAY_SHORT_TIME (5 * 1000)
#define DELAY_MEDIUM_TIME (5 * DELAY_SHORT_TIME)
#define DELAY_LONG_TIME (5 * DELAY_MEDIUM_TIME)
volatile unsigned int cardid;
struct Uid{
unsigned char uid_buff[FRAME_MAX_LENGTH];
unsigned char uid_length;
};
unsigned char seq = 0;
unsigned char current_section_id;
unsigned char not_readed_flag = 0;
unsigned char readdata_buff[DATA_BUFF_LENGTH];
unsigned char writedata_buff[DATA_BUFF_LENGTH];
struct termios *new_config = NULL;
struct termios *old_config = NULL;
int tty_init(int fd, struct termios *old_config,struct termios *new_config);
int GetDvcInfo(int fd,unsigned char *dev_info);
int PCDConfig(int fd);
int PCDClose(int fd);
int PiccRequest(int fd, unsigned int *atq_num);
int PiccAnticoll(int fd,unsigned char level,struct Uid * uid_p);
int PiccSelect(int fd,struct Uid * uid_p);
int PiccHalt(int fd);
int PiccAuthKey(int fd,unsigned char key,struct Uid * uid_p,unsigned char block_id);
int PiccRead(int fd,unsigned char block_id,unsigned char *data_buff);
int PiccWrite(int fd,unsigned char block_id,unsigned char *data_buff);
static inline int Read_Buff(int fd,unsigned char *rbuff,unsigned char size);
static inline int Write_Buff(int fd,unsigned char *wbuff,unsigned char size);
static inline void Display_Atq_Num(unsigned int atq_num);
static inline void Display_Uid(struct Uid * uid_p);
static inline void Display_The_Frame(unsigned char *buff,unsigned char size);
static inline void Display_Card_Data(unsigned char *data_buff);
static inline void Display_Device_Info(unsigned char *dev_info);
static inline unsigned char Caculate_BCC(unsigned char* buff,unsigned char length);
static inline int Check_The_Frame(unsigned char *buff,
unsigned char frame_length,unsigned char packet_info,unsigned char checksum_bit);
/************** Use this interface ***************/
int Card_Operation(int fd,unsigned char operation,unsigned char block_id);
#define OPERATION_READ 11
#define OPERATION_WRITE 12
/************************