切换配置时,radio还在等待读卡的状态,官方读取程序如下:
/**@brief Function for reading packet.
*/
uint32_t read_packet(uint8_t *received, uint32_t *getRssisample)
{
uint32_t result = 0;
NRF_RADIO->EVENTS_READY = 0U;
// Enable radio and wait for ready
NRF_RADIO->TASKS_RXEN = 1U;
while (NRF_RADIO->EVENTS_READY == 0U)
{
// wait
}
NRF_RADIO->EVENTS_END = 0U;
// Start listening and wait for address received event
NRF_RADIO->TASKS_START = 1U;
// Wait for end of packet or buttons state changed
while (NRF_RADIO->EVENTS_END == 0U) //会一直在这里等待接收标签数据
{
if (rfidConfiged == 1) {
rfidConfiged = 0;
break;
}
// wait
}
if (NRF_RADIO->CRCSTATUS == 1U)
{
//getRssisample = NRF_RADIO->RSSISAMPLE;
if (rfidMode == 1) {
memcpy(received, (((char *)packet) + 2), 10);
} else {
memcpy(received, packet, 10);
}
*getRssisample = NRF_RADIO->RSSISAMPLE;
result = 1;
}
NRF_RADIO->EVENTS_DISABLED = 0U;
// Disable radio
NRF_RADIO->TASKS_DISABLE = 1U;
while (NRF_RADIO->EVENTS_DISABLED == 0U)
{
// wait
}
return result;
}
如果radio仍处于读卡状态,这时切换配置,会卡死在while (NRF_RADIO->EVENTS_END == 0U) 这个循环里,就算有相应配置的标签也读不到。
解决方法:停止读卡,切换配置,重新读卡。代码如下:
void radio_change()
{
NRF_RADIO->EVENTS_DISABLED = 0; //停止读卡
NRF_RADIO->TASKS_DISABLE = 1u;
while(NRF_RADIO->EVENTS_DISABLED == 0u){
}
radio_configure(); //配置radio
NRF_RADIO->PACKETPTR = (uint32_t)packet;
NRF_RADIO->EVENTS_READY = 0U; //重新读卡
// Enable radio and wait for ready
NRF_RADIO->TASKS_RXEN = 1U;
while (NRF_RADIO->EVENTS_READY == 0U)
{
}
NRF_RADIO->EVENTS_END = 0U;
// Start listening and wait for address received event
NRF_RADIO->TASKS_START = 1U;
}