1 Star 0 Fork 10

PaulJiang/Linux RC522 SPI S50 Read Card Device Drivers

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
rc522_test.c 18.94 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
/*
* /linux-3.0.8/drivers/spi/rc522_test.c
*
*/
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <time.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include "rc522_test.h"
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
static const char *device = "/dev/rc522-1.0";
static uint8_t mode = SPI_MODE_0;
static uint8_t bits = 8;
static uint32_t speed = 500000;
static uint16_t delay;
static void pabort(const char *s){perror(s);abort();}
static void print_usage(const char *prog)
{
printf("Usage: %s [-DsbdlHOLC3]\n", prog);
puts(" -D --device device to use (default /dev/spidev1.1)\n"
" -s --speed max speed (Hz)\n"
" -d --delay delay (usec)\n"
" -b --bpw bits per word \n"
" -l --loop loopback\n"
" -H --cpha clock phase\n"
" -O --cpol clock polarity\n"
" -L --lsb least significant bit first\n"
" -C --cs-high chip select active high\n"
" -3 --3wire SI/SO signals shared\n");
exit(1);
}
static void parse_opts(int argc, char *argv[])
{
while (1) {
static const struct option lopts[] = {
{ "device", 1, 0, 'D' },
{ "speed", 1, 0, 's' },
{ "delay", 1, 0, 'd' },
{ "bpw", 1, 0, 'b' },
{ "loop", 0, 0, 'l' },
{ "cpha", 0, 0, 'H' },
{ "cpol", 0, 0, 'O' },
{ "lsb", 0, 0, 'L' },
{ "cs-high", 0, 0, 'C' },
{ "3wire", 0, 0, '3' },
{ "no-cs", 0, 0, 'N' },
{ "ready", 0, 0, 'R' },
{ NULL, 0, 0, 0 },
};
int c;
c = getopt_long(argc, argv, "D:s:d:b:lHOLC3NR", lopts, NULL);
if (c == -1)
break;
switch (c) {
case 'D':
device = optarg;
break;
case 's':
speed = atoi(optarg);
break;
case 'd':
delay = atoi(optarg);
break;
case 'b':
bits = atoi(optarg);
break;
case 'l':
mode |= SPI_LOOP;
break;
case 'H':
mode |= SPI_CPHA;
break;
case 'O':
mode |= SPI_CPOL;
break;
case 'L':
mode |= SPI_LSB_FIRST;
break;
case 'C':
mode |= SPI_CS_HIGH;
break;
case '3':
mode |= SPI_3WIRE;
break;
case 'N':
mode |= SPI_NO_CS;
break;
case 'R':
mode |= SPI_READY;
break;
default:
print_usage(argv[0]);
break;
}
}
}
/***** rc522 card ops *****/
static int rc522_operate_card(int fd,
struct rc522_ioc_transfer *xfer)
{
int ret = 0;
ret = ioctl(fd, SPI_IOC_MESSAGE(1), xfer);
if (ret == -1)
printf(" Y@_@Y (ioctl error) Y@_@Y\n");
return ret;
}
/***** rc522 read, write blk ops *****/
static int rc522_read_card(int fd,
struct rc522_ioc_transfer *xfer)
{
enum rc522_sect_num sect = 0;
enum rc522_blk_num blk = 0;
int i =0;
xfer->ioc_type = RC522_IOC_READ_CARD;
printf("/***** rc522 read card test: start *****/\n");
//for(sect = RC522_SECT_15; sect < RC522_SECT_15 + 1; sect++){
printf("read 0x%02x sector: start\n", xfer->sect_num);
//xfer->sect_num = sect;
//for(blk = RC522_BLK_0; blk < RC522_BLK_1; blk++){
printf("read 0x%02x blk: start", xfer->blk_num);
// xfer->blk_num = blk;
memset(xfer->txrx_buf, (__u8)0x00, CARD_BLK_SIZE + CARD_PLUS_SIZE);
if(rc522_operate_card(fd, xfer))
return 0;
for(i=0; i<CARD_BLK_SIZE; i++){
if(i%4 == 0) printf("\n");
printf("0x%02x ", xfer->txrx_buf[i]);
}
printf("\nread 0x%02x blk: end\n", xfer->blk_num);
//}
printf("read 0x%02x sector: end\n", xfer->sect_num);
//}
printf("/***** rc522 read card test: end *****/\n");
}
static int rc522_write_card(int fd,
struct rc522_ioc_transfer *xfer)
{
enum rc522_sect_num sect = 0;
enum rc522_blk_num blk = 0;
int i =0;
xfer->ioc_type = RC522_IOC_WRITE_CARD;
printf("/***** rc522 write card test: start *****/\n");
//for(sect = RC522_SECT_15; sect < RC522_SECT_15 + 1; sect++){
printf("write 0x%02x sector: start\n", xfer->sect_num);
//xfer->sect_num = sect;
//for(blk = RC522_BLK_0; blk < RC522_BLK_1; blk++){
printf("write 0x%02x blk: start", xfer->blk_num);
// xfer->blk_num = blk;
//memset(xfer->txrx_buf, (__u8)0x00,
// CARD_BLK_SIZE + CARD_PLUS_SIZE);
memset(xfer->txrx_buf, (__u8)(rand()%10),
CARD_BLK_SIZE);
/* write value: 0x00 */
if(rc522_operate_card(fd, xfer))
return 0;
for(i=0; i<CARD_BLK_SIZE; i++){
if(i%4 == 0) printf("\n");
printf("0x%02x ", xfer->txrx_buf[i]);
}
printf("\nwrite 0x%02x blk: end\n", xfer->blk_num);
//}
printf("write 0x%02x sector: end\n", xfer->sect_num);
//}
printf("/***** rc522 write card test: end *****/\n");
}
/***** rc522 inc, dec blk ops *****/
static int rc522_inc_card(int fd,
struct rc522_ioc_transfer *xfer)
{
enum rc522_sect_num sect = 0;
enum rc522_blk_num blk = 0;
int i =0;
xfer->ioc_type = RC522_IOC_INC_CARD;
printf("/***** rc522 inc card test: start *****/\n");
//for(sect = RC522_SECT_15; sect < RC522_SECT_15 + 1; sect++){
printf("inc 0x%02x sector: start\n", xfer->sect_num);
//xfer->sect_num = sect;
//for(blk = RC522_BLK_0; blk < RC522_BLK_1; blk++){
printf("inc 0x%02x blk: start", xfer->blk_num);
//xfer->blk_num = blk;
xfer->txrx_buf[0] = 0x00;
xfer->txrx_buf[1] = 0x00;
xfer->txrx_buf[2] = 0x00;
xfer->txrx_buf[3] = 0x00; /* inc value: 0x01 */
if(rc522_operate_card(fd, xfer))
return 0;
for(i=0; i<CARD_INC_SIZE; i++){
if(i%4 == 0) printf("\n");
printf("0x%02x ", xfer->txrx_buf[i]);
}
printf("\ninc 0x%02x blk: end\n", xfer->blk_num);
//}
printf("inc 0x%02x sector: end\n", xfer->sect_num);
//}
printf("/***** rc522 inc card test: end *****/\n");
}
static int rc522_dec_card(int fd,
struct rc522_ioc_transfer *xfer)
{
enum rc522_sect_num sect = 0;
enum rc522_blk_num blk = 0;
int i =0;
xfer->ioc_type = RC522_IOC_DEC_CARD;
printf("/***** rc522 dec card test: start *****/\n");
//for(sect = RC522_SECT_15; sect < RC522_SECT_15 + 1; sect++){
printf("dec 0x%02x sector: start\n", xfer->sect_num);
//xfer->sect_num = sect;
//for(blk = RC522_BLK_0; blk < RC522_BLK_1; blk++){
printf("dec 0x%02x blk: start", xfer->blk_num);
// xfer->blk_num = blk;
xfer->txrx_buf[0] = 0x01;
xfer->txrx_buf[1] = 0x00;
xfer->txrx_buf[2] = 0x00;
xfer->txrx_buf[3] = 0x00; /* inc value: 0x01 */
if(rc522_operate_card(fd, xfer))
return 0;
for(i=0; i<CARD_INC_SIZE; i++){
if(i%4 == 0) printf("\n");
printf("0x%02x ", xfer->txrx_buf[i]);
}
printf("\ndec 0x%02x blk: end\n", xfer->blk_num);
//}
printf("dec 0x%02x sector: end\n", xfer->sect_num);
//}
printf("/***** rc522 dec card test: end *****/\n");
}
/***** rc522 key area ops *****/
static int rc522_read_keya(int fd,
struct rc522_ioc_transfer *xfer)
{
enum rc522_sect_num sect = 0;
enum rc522_blk_num blk = 0;
int i =0;
xfer->ioc_type = RC522_IOC_READ_KEYA;
printf("/***** rc522 read keya test: start *****/\n");
//for(; sect < RC522_SECT_15 + 1; sect++){
printf("read 0x%02x sector keya: start\n", xfer->sect_num);
//xfer->sect_num = sect;
memset(xfer->txrx_buf, (__u8)0x00,
CARD_BLK_SIZE + CARD_PLUS_SIZE);
if(rc522_operate_card(fd, xfer))
return 0;
for(i=0; i<CARD_KEYA_SIZE; i++){
if(i%4 == 0) printf("\n");
printf("0x%02x ", xfer->txrx_buf[i]);
}
printf("\nread 0x%02x sector keya: end\n\n", xfer->sect_num);
//}
printf("/***** rc522 read keya test: end *****/\n");
}
static int rc522_read_keyb(int fd,
struct rc522_ioc_transfer *xfer)
{
enum rc522_sect_num sect = 0;
enum rc522_blk_num blk = 0;
int i =0;
xfer->ioc_type = RC522_IOC_READ_KEYB;
printf("/***** rc522 read keyb test: start *****/\n");
//for(sect = RC522_SECT_15; sect < RC522_SECT_15 + 1; sect++){
printf("read 0x%02x sector keyb: start\n", xfer->sect_num);
//xfer->sect_num = sect;
memset(xfer->txrx_buf, (__u8)0x00,
CARD_BLK_SIZE + CARD_PLUS_SIZE);
if(rc522_operate_card(fd, xfer))
return 0;
for(i=0; i<CARD_KEYA_SIZE; i++){
if(i%4 == 0) printf("\n");
printf("0x%02x ", xfer->txrx_buf[i]);
}
printf("\nread 0x%02x sector keyb: end\n", xfer->sect_num);
//}
printf("/***** rc522 read keyb test: end *****/\n");
}
static int rc522_read_ctrl(int fd,
struct rc522_ioc_transfer *xfer)
{
enum rc522_sect_num sect = 0;
enum rc522_blk_num blk = 0;
int i =0;
xfer->ioc_type = RC522_IOC_READ_CTRL;
printf("/***** rc522 read ctrl test: start *****/\n");
//for(sect = RC522_SECT_15; sect < RC522_SECT_15 + 1; sect++){
printf("read 0x%02x sector ctrl: start\n", xfer->sect_num);
// xfer->sect_num = sect;
memset(xfer->txrx_buf, (__u8)0x00, CARD_BLK_SIZE+CARD_PLUS_SIZE);
if(rc522_operate_card(fd, xfer))
return 0;
for(i=0; i<CARD_CTRL_SIZE; i++){
printf("0x%02x ", xfer->txrx_buf[i]);
}
printf("\nread 0x%02x sector ctrl: end\n", xfer->sect_num);
//}
printf("/***** rc522 read ctrl test: end *****/\n");
}
static int rc522_write_keya(int fd,
struct rc522_ioc_transfer *xfer)
{
enum rc522_sect_num sect = 0;
enum rc522_blk_num blk = 0;
unsigned char rand_key = (__u8)(rand()/2);
int i =0;
xfer->ioc_type = RC522_IOC_WRITE_KEYA;
printf("/***** rc522 write keya test: start *****/\n");
//for(sect = RC522_SECT_15; sect < RC522_SECT_15 + 1; sect++){
printf("write 0x%02x sector keya: start\n", xfer->sect_num);
//xfer->sect_num = sect;
//memset(xfer->txrx_buf, rand_key, CARD_KEYA_SIZE);
//memset(xfer->keya_buf, rand_key, CARD_KEYA_SIZE);
xfer->txrx_buf[0] = (__u8)0xff;
xfer->txrx_buf[1] = (__u8)0xff;
xfer->txrx_buf[2] = (__u8)0xff;
xfer->txrx_buf[3] = (__u8)0xff;
xfer->txrx_buf[4] = (__u8)0xff;
xfer->txrx_buf[5] = (__u8)0xff;
if(rc522_operate_card(fd, xfer))
return 0;
for(i=0; i<CARD_KEYA_SIZE; i++){
printf("0x%02x ", xfer->txrx_buf[i]);
}
printf("\nwrite 0x%02x sector keya: end\n", xfer->sect_num);
//}
printf("/***** rc522 write keya test: end *****/\n");
}
static int rc522_write_keyb(int fd,
struct rc522_ioc_transfer *xfer)
{
enum rc522_sect_num sect = 0;
enum rc522_blk_num blk = 0;
unsigned char rand_key = (__u8)(rand()/2);
int i =0;
xfer->ioc_type = RC522_IOC_WRITE_KEYB;
printf("/***** rc522 write keyb test: start *****/\n");
//for(sect = RC522_SECT_15; sect < RC522_SECT_15 + 1; sect++){
printf("write 0x%02x sector keyb: start\n", xfer->sect_num);
//xfer->sect_num = sect;
xfer->txrx_buf[0] = (__u8)0xff;
xfer->txrx_buf[1] = (__u8)0xff;
xfer->txrx_buf[2] = (__u8)0xff;
xfer->txrx_buf[3] = (__u8)0xff;
xfer->txrx_buf[4] = (__u8)0xff;
xfer->txrx_buf[5] = (__u8)0xff;
if(rc522_operate_card(fd, xfer))
return 0;
for(i=0; i<CARD_KEYB_SIZE; i++){
printf("0x%02x ", xfer->txrx_buf[i]);
}
printf("\nwrite 0x%02x sector keyb: end\n", xfer->sect_num);
//}
printf("/***** rc522 write keyb test: end *****/\n");
return 0;
}
static int rc522_write_ctrl(int fd,
struct rc522_ioc_transfer *xfer)
{
enum rc522_sect_num sect = 0;
enum rc522_blk_num blk = 0;
int i =0;
xfer->ioc_type = RC522_IOC_WRITE_CTRL;
printf("rc522 write ctrl test: start *****/\n");
//for(sect = RC522_SECT_15; sect < RC522_SECT_15 + 1; sect++){
printf("write 0x%02x sector ctrl: start\n", xfer->sect_num);
// xfer->sect_num = sect;
xfer->txrx_buf[0] = (__u8)0xff; /* using default value*/
xfer->txrx_buf[1] = (__u8)0x07; /* using default value*/
xfer->txrx_buf[2] = (__u8)0x80; /* using default value*/
xfer->txrx_buf[3] = (__u8)0x69; /* using default value*/
if(rc522_operate_card(fd, xfer))
return 0;
for(i=0; i<CARD_CTRL_SIZE; i++){
printf("0x%02x ", xfer->txrx_buf[i]);
}
printf("\nwrite 0x%02x sector ctrl: end\n", xfer->sect_num);
//}
printf("/***** rc522 write ctrl test: end *****/\n");
}
/***** critical ops: the user can have all permission for protected sector(0-9) *****/
static int rc522_unlock_low_sect(int fd,
struct rc522_ioc_transfer *xfer)
{
enum rc522_sect_num sect = 0;
enum rc522_blk_num blk = 0;
int i =0;
//xfer->ioc_type = RC522_IOC_WRITE_KEYA; //xfer->sect_num = RC522_SECT_9;
xfer->sect_lock = RC522_SECT_UNLOCK;
printf("/***** rc522 unlock low sect test: start *****/\n");
printf("unlock 0x%02x sector: start\n", xfer->sect_num);
printf("%s: \n%s \nfor %s\n",
xfer->sect_lock == RC522_SECT_UNLOCK ?
"unlock low sector(0-9) mode" :
"lock low sector(0-9) mode",
xfer->sect_lock == RC522_SECT_UNLOCK ?
"the user have all permission to write ctrl blk(3)" :
"the user have non permission to write ctrl blk(3)",
xfer->sect_lock == RC522_SECT_UNLOCK ?
"all sector(0-15)" :
"low sector(0-9)");
}
static int rc522_lock_low_sect(int fd,
struct rc522_ioc_transfer *xfer)
{
enum rc522_sect_num sect = 0;
enum rc522_blk_num blk = 0;
unsigned char rand_key = (__u8)(rand()/2);
int i =0;
//xfer->ioc_type = RC522_IOC_WRITE_KEYA; //xfer->sect_num = RC522_SECT_15;
xfer->sect_lock = RC522_SECT_LOCK;
printf("unlock 0x%02x sector: end\n", xfer->sect_num);
printf("/***** rc522 unlock low sect test: end *****/\n");
printf("%s: \n%s \nfor %s\n",
xfer->sect_lock == RC522_SECT_UNLOCK ?
"unlock low sector(0-9) mode" :
"lock low sector(0-9) mode",
xfer->sect_lock == RC522_SECT_UNLOCK ?
"the user have all permission to write ctrl blk(3)" :
"the user have non permission to write ctrl blk(3)",
xfer->sect_lock == RC522_SECT_UNLOCK ?
"all sector(0-15)" :
"low sector(0-9)");
}
static int rc522_goto_menu(int fd,
struct rc522_ioc_transfer *xfer)
{
return 0;
//}
}
struct {
void (*function)(int fd, struct rc522_ioc_transfer *xfer);
char *msg;
}ioc_type_menu[] = {
{ rc522_goto_menu, "rc522 goto before menu: \n" } ,
{ rc522_read_card, "rc522 read card test: \n" } ,
{ rc522_write_card, "rc522 write card test: \n\n" } ,
//{ rc522_inc_card, "rc522 increase card test: \n" } ,
//{ rc522_dec_card, "rc522 decrease card test: \n\n" } ,
{ rc522_read_keya, "rc522 read keya test: \n" } ,
{ rc522_read_keyb, "rc522 read keyb test: \n" } ,
{ rc522_read_ctrl, "rc522 read ctrl area: \n\n" } ,
{ rc522_write_keya, "rc522 write keya test: \n" } ,
{ rc522_write_keyb, "rc522 write keyb test: \n" } ,
{ rc522_write_ctrl, "rc522 write ctrl area: \n\n" } ,
{ rc522_lock_low_sect, "rc522 lock low sect test: \n" } ,
{ rc522_unlock_low_sect,"rc522 unlock low sect test: \n" } ,
{ 0, 0}
};
static int rc522_menu_card(int fd,
struct rc522_ioc_transfer *xfer)
{
int i =0;
int select_index = 0;
__u32 select_sect = 0;
while(1){
xfer->sect_lock = RC522_SECT_LOCK;
printf("RC522 Test Card Menu:\n");
printf("%s: \n%s \nfor %s\n",
xfer->sect_lock == RC522_SECT_UNLOCK ?
"unlock low sector(0-9) mode" :
"lock low sector(0-9) mode",
xfer->sect_lock == RC522_SECT_UNLOCK ?
"the user have all permission to write ctrl blk(3)" :
"the user have non permission to write ctrl blk(3)",
xfer->sect_lock == RC522_SECT_UNLOCK ?
"all sector(0-15)" :
"low sector(0-9)");
printf("\nPlease input the sect number(0-15):");
scanf("%u", &select_sect);
if(select_sect < 0 || select_sect > 15){
printf("\nYou select unused sect: %d", select_sect);
continue;
}
xfer->sect_num = (__u8)select_sect;
printf("You select sect: %u\n", xfer->sect_num);
while(1)
{
printf("\nPlease select function: \n");
for(i=0; ioc_type_menu[i].function!=0; i++)
printf("%d : %s", i, ioc_type_menu[i].msg);
printf("\nPlease input the select number:");
scanf("%d", &select_index) ;
printf("You select %d function: %s\n",
select_index, ioc_type_menu[select_index].msg);
//getchar();
if(0 < select_index && select_index < i)
{
(*ioc_type_menu[select_index].function)(fd, xfer);
sleep(1);
}else{
break;
}
//printf("\nPlease enter key to contine:\n");
//getchar();
}
}
printf("Do not goto here\n");
return i;
}
int main(int argc, char *argv[])
{
int ret = 0;
int fd = -1;
int i = 0;
parse_opts(argc, argv);
fd = open(device, O_RDWR);
if (fd < 0)
pabort("can't open device");
/***** RC522 Test
****** the key a have all permission ,
****** the key b have non permission ,
****** when ctrl area using default value .
*****/
struct rc522_ioc_transfer transfer={
.ioc_type = RC522_IOC_REQ_CARD,
.blk_num = RC522_BLK_0,
};
transfer.key_type = RC522_KEY_A;
//transfer.key_type = RC522_KEY_B;
//transfer.key_type = RC522_KEY_A_B;
//transfer.keya_buf[0] = 0; transfer.keya_buf[1] = 0;
//transfer.keya_buf[2] = 0; transfer.keya_buf[3] = 0;
//transfer.keya_buf[4] = 0; transfer.keya_buf[5] = 0;
transfer.keya_buf[0] = 0xff; transfer.keya_buf[1] = 0xff;
transfer.keya_buf[2] = 0xff; transfer.keya_buf[3] = 0xff;
transfer.keya_buf[4] = 0xff; transfer.keya_buf[5] = 0xff;
//transfer.keyb_buf[0] = 1; transfer.keyb_buf[1] = 1;
//transfer.keyb_buf[2] = 1; transfer.keyb_buf[3] = 1;
//transfer.keyb_buf[4] = 1; transfer.keyb_buf[5] = 1;
transfer.keyb_buf[0] = 0xff; transfer.keyb_buf[1] = 0xff;
transfer.keyb_buf[2] = 0xff; transfer.keyb_buf[3] = 0xff;
transfer.keyb_buf[4] = 0xff; transfer.keyb_buf[5] = 0xff;
memset(transfer.txrx_buf, 0x00, CARD_BLK_SIZE + CARD_PLUS_SIZE);
i = 10;
while(i){
if(rc522_operate_card(fd, &transfer)){
printf("Please put your card on readed pad:");
i--;
sleep(1);
continue;
}
break;
}
if(i == 0){
printf(" Failed to find your card on readed pad:\n");
close(fd);
return 0;
}
printf("Request Card: \n");
printf("type: 0x%02x, 0x%02x, %s \n",
transfer.txrx_buf[0],transfer.txrx_buf[1],
transfer.txrx_buf[0] == 0x04?"S50 card":
(transfer.txrx_buf[0] == 0x02?"S70 card":"Other card"));
printf("id: 0x%02x, 0x%02x, 0x%02x, 0x%02x, idcsum: 0x%02x\n",
transfer.txrx_buf[2], transfer.txrx_buf[3],
transfer.txrx_buf[4], transfer.txrx_buf[5],
transfer.txrx_buf[6]);
printf("id_buf: 0x%02x, 0x%02x, 0x%02x, 0x%02x\n",
transfer.id_buf[0], transfer.id_buf[1],
transfer.id_buf[2], transfer.id_buf[3]);
printf("cap: 0x%02x Kbits, msg: 0x%02x, 0x%02x\n",
transfer.txrx_buf[7], transfer.txrx_buf[8],transfer.txrx_buf[9]);
printf("Request Card: end\n\n");
if(transfer.txrx_buf[0] == 0x00){
printf("Failed to request used card: %s\n","Unused card");
return 0;
}
rc522_menu_card(fd, &transfer);
/*
printf("SPI interface mode: \n");
ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
if (ret == -1)
pabort("can't set spi mode");
ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
if (ret == -1)
pabort("can't get spi mode");
ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
if (ret == -1)
pabort("can't set bits per word");
ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
if (ret == -1)
pabort("can't get bits per word");
ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
if (ret == -1)
pabort("can't set max speed hz");
ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
if (ret == -1)
pabort("can't get max speed hz");
printf("spi mode: %d\n", mode);
printf("bits per word: %d\n", bits);
printf("max speed: %d Hz (%d KHz)\n\n\n", speed, speed/1000);
*/
close(fd);
return ret;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/jzhiyu/Linux-RC522-SPI-S50-Read-Card-Device-Drivers.git
[email protected]:jzhiyu/Linux-RC522-SPI-S50-Read-Card-Device-Drivers.git
jzhiyu
Linux-RC522-SPI-S50-Read-Card-Device-Drivers
Linux RC522 SPI S50 Read Card Device Drivers
master

搜索帮助