aboutsummaryrefslogtreecommitdiff
path: root/examples/nrf52840
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2024-11-20 23:19:37 +0100
committerDario Nieuwenhuis <[email protected]>2024-11-20 23:29:22 +0100
commit0740b235ac546081d54d103b0cdd018e7ef2581c (patch)
tree9b35bcf531a98ead9f01f51b19e39050e94aa9d0 /examples/nrf52840
parent227e073fca97bcbcec42d9705e0a8ef19fc433b5 (diff)
nrf: Add NFCT driver.
Co-Authored-By: turbocool3r <[email protected]> Co-Authored-By: ferris <[email protected]>
Diffstat (limited to 'examples/nrf52840')
-rw-r--r--examples/nrf52840/src/bin/nfct.rs79
1 files changed, 79 insertions, 0 deletions
diff --git a/examples/nrf52840/src/bin/nfct.rs b/examples/nrf52840/src/bin/nfct.rs
new file mode 100644
index 000000000..d559d006a
--- /dev/null
+++ b/examples/nrf52840/src/bin/nfct.rs
@@ -0,0 +1,79 @@
1#![no_std]
2#![no_main]
3
4use defmt::*;
5use embassy_executor::Spawner;
6use embassy_nrf::config::HfclkSource;
7use embassy_nrf::nfct::{Config as NfcConfig, NfcId, NfcT};
8use embassy_nrf::{bind_interrupts, nfct};
9use {defmt_rtt as _, embassy_nrf as _, panic_probe as _};
10
11bind_interrupts!(struct Irqs {
12 NFCT => nfct::InterruptHandler;
13});
14
15#[embassy_executor::main]
16async fn main(_spawner: Spawner) {
17 let mut config = embassy_nrf::config::Config::default();
18 config.hfclk_source = HfclkSource::ExternalXtal;
19 let p = embassy_nrf::init(config);
20
21 dbg!("Setting up...");
22 let config = NfcConfig {
23 nfcid1: NfcId::DoubleSize([0x04, 0x68, 0x95, 0x71, 0xFA, 0x5C, 0x64]),
24 sdd_pat: nfct::SddPat::SDD00100,
25 plat_conf: 0b0000,
26 protocol: nfct::SelResProtocol::Type4A,
27 };
28
29 let mut nfc = NfcT::new(p.NFCT, Irqs, &config);
30
31 let mut buf = [0u8; 256];
32
33 loop {
34 info!("activating");
35 nfc.activate().await;
36
37 loop {
38 info!("rxing");
39 let n = match nfc.receive(&mut buf).await {
40 Ok(n) => n,
41 Err(e) => {
42 error!("rx error {}", e);
43 break;
44 }
45 };
46 let req = &buf[..n];
47 info!("received frame {:02x}", req);
48
49 let mut deselect = false;
50 let resp = match req {
51 [0xe0, ..] => {
52 info!("Got RATS, tx'ing ATS");
53 &[0x06, 0x77, 0x77, 0x81, 0x02, 0x80][..]
54 }
55 [0xc2] => {
56 info!("Got deselect!");
57 deselect = true;
58 &[0xc2]
59 }
60 _ => {
61 info!("Got unknown command!");
62 &[0xFF]
63 }
64 };
65
66 match nfc.transmit(resp).await {
67 Ok(()) => {}
68 Err(e) => {
69 error!("tx error {}", e);
70 break;
71 }
72 }
73
74 if deselect {
75 break;
76 }
77 }
78 }
79}