embedded-nrf24l01
embedded-nrf24l01 copied to clipboard
A pure Rust driver for NRF24L01(+) transceivers on embedded-hal platforms
embedded-nrf24l01
Features
- Designed for use with the embedded-hal crate
- Safe and declarative register definitions
- Chip operation modes lifted to the type-level
- Lets you go straight into RX/TX with the default config
Reference datasheets
Usage
Parameters
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::SpiDevicefor the SPI peripheralWe provide a
mod setupwith a few constants for SPI. -
embedded_hal::digital::OutputPinfor the CE pin
Constructor
let mut nrf24 = NRF24L01::new(ce, spi).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.
Configuration
Before you start transmission, the device must be configured. Example with an nrf24l01+:
nrf24.set_frequency(8).unwrap();
nrf24.set_auto_retransmit(15, 15).unwrap();
nrf24.set_rf(&DataRate::R2Mbps, 0).unwrap();
nrf24
.set_pipes_rx_enable(&[true, false, false, false, false, false])
.unwrap();
nrf24
.set_auto_ack(&[true, false, false, false, false, false])
.unwrap();
nrf24.set_pipes_rx_lengths(&[None; 6]).unwrap();
nrf24.set_crc(CrcMode::TwoBytes).unwrap();
nrf24.set_rx_addr(0, &b"fnord"[..]).unwrap();
nrf24.set_tx_addr(&b"fnord"[..]).unwrap();
nrf24.flush_rx().unwrap();
nrf24.flush_tx().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
- Use
tx.can_send()to prevent sending on a full queue. Note: not needed ifpoll_sendorwait_emptywas used aftersend. - Use
tx.send()to enqueue a packet. - Use
tx.wait_empty()to synchronously flush. Ortx.poll_send()to asynchronously flush and get whether package transmission was successful.
Note
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.