gpt4 book ai didi

c - 在正确字节到来之前收到通知 SPI RFID

转载 作者:行者123 更新时间:2023-11-30 19:33:38 28 4
gpt4 key购买 nike

我尝试从 RFID 读取代表设备版本号的字节。使用SPI协议(protocol)我得到0x92字节(RC522版本2.0),请求字节是0x37。除了字节的位置之外,一切都运行良好。在我按照计划得到正确的(0x92)之前,我不知道为什么会打印它。输出:

     Hello, world
received: 1
received: 92
= v2.0
received: 0
received: 92
= v2.0
received: 0
received: 92
= v2.0
received: 0
received: 92
= v2.0
received: 0
received: 92
= v2.0

我说,好吧,让我删除 printf("received: %x\n", data);\
行来自 SPI.h 库。但后来我什么也没打印(除了 Hello World)。另一个应该在 92h 返回时仅打印 "version 2.0" 的函数不会这样做,为什么 - 因为返回的 v 变量是 9f 92 和之前的所有内容都不同。

#define SPI_READ(data) \
do { \
SPI_TXBUF = 0; \
SPI_WAITFOREORx(); \
data = SPI_RXBUF; \
printf("received: %x\n", data); \
} while(0)

也许我应该放置一些等待或延迟之类的函数,以便让 RC522 执行命令?然后就让它打印出来。但即使发生这种情况,为什么 printf 删除会破坏整个输出?以下是调用 SPI_READ 的代码:

static uint8_t
read_register_rfid(uint8_t dev_cmd)
{
uint8_t ret = 0;
GPIO_CLR_PIN(GPIO_C_BASE, GPIO_PIN_1);
SPI_WRITE(((dev_cmd << 1) & 0x7e) | 0x80);
SPI_READ(ret);
GPIO_SET_PIN(GPIO_C_BASE, GPIO_PIN_1);
return ret;
}

以及检查版本的功能:

void
test_ver()
{
uint8_t reg=1;
// Get the MFRC522 firmware version
uint8_t v = 0;
while(reg!=0xa){
v = read_register_rfid(0x37);
//printf("%x\n", v);
// Lookup which version
switch(v) {
case 0x88: printf(" = (clone)"); break;
case 0x90: printf(" = v0.0"); break;
case 0x91: printf(" = v1.0"); break;
case 0x92: printf(" = v2.0\n"); break;
// default: printf(" = (unknown)\n");
}
reg++;
//When 0x00 or 0xFF is returned, communication probably failed
}
if ((v == 0x00) || (v == 0xFF))
printf("WARNING: Communication failure?\n");
}

最佳答案

这里我分享了使用 SPI 库读写 RC522 的更新函数:

void
writeMFRC522(uint8_t adr, uint8_t val)
{
GPIO_CLR_PIN(GPIO_C_BASE, GPIO_PIN_1);
GPIO_CLR_PIN(GPIO_C_BASE, GPIO_PIN_1);
SPI_WRITE((adr << 1) & 0x7e);
SPI_WRITE(val);
SPI_FLUSH();
//printf("456\n");
GPIO_SET_PIN(GPIO_C_BASE, GPIO_PIN_1);
}


static uint8_t
readMFRC522(uint8_t dev_cmd)
{
uint8_t ret = 0x0;
GPIO_CLR_PIN(GPIO_C_BASE, GPIO_PIN_1);
GPIO_CLR_PIN(GPIO_C_BASE, GPIO_PIN_1);

SPI_WRITE(((dev_cmd << 1) & 0x7e) | 0x80);
SPI_READ(ret);

ret = 0;
SPI_WRITE(0x00);
SPI_READ(ret);

GPIO_SET_PIN(GPIO_C_BASE, GPIO_PIN_1);
return ret;
}

关于c - 在正确字节到来之前收到通知 SPI RFID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45190515/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com