Skip to content

Commit

Permalink
Refactor pn532 process function with timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
zargony committed Aug 31, 2024
1 parent 92c96f4 commit ed69a7a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
8 changes: 1 addition & 7 deletions firmware/src/nfc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ use embedded_hal_async::i2c::I2c;
use log::{debug, info, warn};
use pn532::{Error as Pn532Error, I2CInterfaceWithIrq, Pn532, Request, SAMMode};

/// NFC reader default command timeout
const COMMAND_TIMEOUT: Duration = Duration::from_millis(50);

/// NFC reader read loop timeout
const READ_TIMEOUT: Duration = Duration::from_millis(100);

Expand Down Expand Up @@ -66,7 +63,6 @@ impl<I2C: I2c, IRQ: Wait<Error = Infallible>> Nfc<I2C, IRQ> {
// SAMConfiguration request (PN532 §7.2.10)
&Request::sam_configuration(SAMMode::Normal, true),
0,
COMMAND_TIMEOUT,
)
.await?;

Expand All @@ -76,7 +72,6 @@ impl<I2C: I2c, IRQ: Wait<Error = Infallible>> Nfc<I2C, IRQ> {
// GetFirmwareVersion request (PN532 §7.2.2)
&Request::GET_FIRMWARE_VERSION,
4,
COMMAND_TIMEOUT,
)
.await?;
// GetFirmwareVersion response (PN532 §7.2.2)
Expand Down Expand Up @@ -108,7 +103,7 @@ impl<I2C: I2c, IRQ: Wait<Error = Infallible>> Nfc<I2C, IRQ> {
// Detect any ISO/IEC14443 Type A target in passive mode
let list_response = match self
.driver
.process(
.process_timeout(
// InListPassiveTarget request (PN532 §7.3.5)
&Request::INLIST_ONE_ISO_A_TARGET,
pn532::BUFFER_SIZE - 9, // max response length
Expand Down Expand Up @@ -167,7 +162,6 @@ impl<I2C: I2c, IRQ: Wait<Error = Infallible>> Nfc<I2C, IRQ> {
// InRelease request (PN532 §7.3.11)
&Request::RELEASE_TAG_1,
1,
COMMAND_TIMEOUT,
)
.await
{
Expand Down
16 changes: 15 additions & 1 deletion firmware/src/pn532.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub const BUFFER_SIZE: usize = 32;
/// Command ACK timeout
const ACK_TIMEOUT: Duration = Duration::from_millis(50);

/// Command response timeout
const RESPONSE_TIMEOUT: Duration = Duration::from_millis(50);

const PREAMBLE: [u8; 3] = [0x00, 0x00, 0xFF];
const POSTAMBLE: u8 = 0x00;
const ACK: [u8; 6] = [0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00];
Expand Down Expand Up @@ -163,11 +166,22 @@ impl<I: Interface, const N: usize> Pn532<I, N> {
}

/// Send PN532 request and wait for ack and response.
/// Like `pn532::Pn532::process`, but fully asynchronous and with timeout
/// Like `pn532::Pn532::process`, but fully asynchronous
pub async fn process<'a>(
&mut self,
request: impl Into<BorrowedRequest<'a>>,
response_len: usize,
) -> Result<&[u8], Error<I::Error>> {
self.process_timeout(request, response_len, RESPONSE_TIMEOUT)
.await
}

/// Send PN532 request and wait for ack and response.
/// Like `pn532::Pn532::process`, but fully asynchronous and with timeout
pub async fn process_timeout<'a>(
&mut self,
request: impl Into<BorrowedRequest<'a>>,
response_len: usize,
timeout: Duration,
) -> Result<&[u8], Error<I::Error>> {
let request = request.into();
Expand Down

0 comments on commit ed69a7a

Please sign in to comment.