Crates.io | embedded-nrf24l01-async |
lib.rs | embedded-nrf24l01-async |
version | 0.3.1 |
source | src |
created_at | 2024-09-23 09:50:41.640874 |
updated_at | 2024-09-23 09:50:41.640874 |
description | A driver for NRF24L01(+) transceivers on embedded-hal-async platforms(base on embedded-nrf24l01). |
homepage | https://github.com/kischang/embedded-nrf24l01 |
repository | https://github.com/kischang/embedded-nrf24l01 |
max_upload_size | |
id | 1383807 |
size | 47,031 |
Get the *-hal
crate for your micro-controller unit. Figure out how
to get to the peripherals implementing these embedded-hal traits:
embedded_hal::spi::SpiDevice
for the SPI peripheral
We provide a mod setup
with a few constants for SPI.
embedded_hal::digital::OutputPin
for the CE pin
let mut nrf24 = NRF24L01::new(ce, spi).await.unwrap();
This will provide an instance of Standby
and activate acknoladgements and dynamic payload length. You can use
.rx()
or .tx()
to transfer into a RXMode
and TXMode
instances. They
implement .standby()
methods to get back to Standby
and then
switch to the other mode.
Before you start transmission, the device must be configured. Example with an nrf24l01+:
nrf24.set_frequency(8).await.unwrap();
nrf24.set_auto_retransmit(15, 15).await.unwrap();
nrf24.set_rf(&DataRate::R2Mbps, 0).await.unwrap();
nrf24
.set_pipes_rx_enable(&[true, false, false, false, false, false])
.await
.unwrap();
nrf24
.set_auto_ack(&[true, false, false, false, false, false])
.await
.unwrap();
nrf24.set_pipes_rx_lengths(&[None; 6]).await.unwrap();
nrf24.set_crc(CrcMode::TwoBytes).await.unwrap();
nrf24.set_rx_addr(0, &b"fnord"[..]).await.unwrap();
nrf24.set_tx_addr(&b"fnord"[..]).await.unwrap();
nrf24.flush_rx().await.unwrap();
nrf24.flush_tx().await.unwrap();
RXMode
Use rx.can_read()
to poll, then rx.read()
to receive payload.
If can_read()
always returns true
, it's usually a power supply issue.
You can attach a 1uF or 10uF capacitor as close to the module as possible or
upgrade to a better 3.3v regulator.
TXMode
tx.can_send()
to prevent sending on a full queue. Note: not needed if poll_send
or wait_empty
was used after send
.tx.send()
to enqueue a packet.tx.wait_empty()
to synchronously flush. Or tx.poll_send()
to asynchronously flush and get whether package transmission was successful.Automatic retransmission (for TX) and acknowledgement (for RX) features go hand in hand. Since setting retransmissions means TX device is expecting an ack, and auto acknowledgement means RX device will check if received packet isn't a duplicate + send an ack back. Turn them both on for tx.poll_send()
to be reliable.