nrf24_rs/
lib.rs

1//! # `nrf24_rs`
2//!
3//! This crate provides a platform agnostic Rust driver using no_std for the nRF24L01+ single chip 2.4 GHz
4//! transceiver by Nordic Semiconduct for communicating data wirelessly using the [`embedded-hal`] traits.
5//!
6//! [`embedded-hal`]: https://github.com/rust-embedded/embedded-hal
7//!
8//! ## Usage
9//!
10//! This crate can be used by adding `nrf24-rs` to your dependencies in your project's `Cargo.toml`.
11//!
12//! ```toml
13//! [dependencies]
14//! nrf24-rs = "0.2"
15//! ```
16//!
17//! The main driver is created by using the [`Nrf24l01::new`] method, which takes a handle to an
18//! SpiDevice, a Chip Enable Output Pin and an [`NrfConfig`][config::NrfConfig] instance.
19//!
20//! ## Examples
21//!
22//! ### Sending data
23//! This simple example will send a simple "Hello world" message.
24//! ```rust
25//! use nrf24_rs::config::{NrfConfig, PALevel};
26//! use nrf24_rs::{Nrf24l01, SPI_MODE};
27//! use embedded_hal::spi::SpiBus;
28//! use embedded_hal_bus::spi::ExclusiveDevice;
29//!
30//! fn main() {
31//!     let p = get_peripherals(); // peripherals
32//!
33//!     let spi = setup_spi(); // configure your SPI to use SPI_MODE
34//!     
35//!     // If you get an SpiBus, convert it into an SpiDevice using the
36//!     // `embedded-hal-bus` crate
37//!     let delay = setup_delay();
38//!     let cs = setup_cs_pin(); // chip select pin
39//!     let spi_device = ExclusiveDevice::new(spi, cs, delay).unwrap();
40//!
41//!     let message = b"Hello world!"; // The message we will be sending
42//!
43//!     // Setup some configuration values using the builder pattern
44//!     let config = NrfConfig::default()
45//!         .channel(8)
46//!         .pa_level(PALevel::Min)
47//!         // We will use a payload size the size of our message
48//!         .payload_size(message.len());
49//!
50//!     // Initialize the chip
51//!     let mut delay = setup_delay(); // create new delay
52//!
53//!     let mut radio = Nrf24l01::new(spi, ce, &mut delay, config).unwrap();
54//!
55//!     if !radio.is_connected().unwrap() {
56//!         panic!("Chip is not connected.");
57//!     }
58//!
59//!     // Open a writing pipe on address "Node1".
60//!     // The listener will have to open a reading pipe with the same address
61//!     // in order to recieve this message.
62//!     radio.open_writing_pipe(b"Node1").unwrap();
63//!
64//!     // Keep trying to send the message
65//!     while let Err(e) = radio.write(&mut delay, &message) {
66//!         // Something went wrong while writing, try again in 50ms
67//!         delay.delay_ms(50);
68//!     }
69//!
70//!     // Message should now successfully have been sent!
71//!     loop {}
72//! }
73//! ```
74//!
75//!
76//! ### Reading data
77//! This simple example will read a "Hello world" message.
78//! ```rust
79//! use nrf24_rs::config::{NrfConfig, PALevel, DataPipe};
80//! use nrf24_rs::{Nrf24l01, SPI_MODE};
81//! use embedded_hal::spi::SpiBus;
82//! use embedded_hal_bus::spi::ExclusiveDevice;
83//!
84//! fn main() {
85//!     let p = get_peripherals(); // peripherals
86//!
87//!     let spi = setup_spi(); // configure your SPI to use SPI_MODE
88//!     
89//!     // If you get an SpiBus, convert it into an SpiDevice using the
90//!     // `embedded-hal-bus` crate
91//!     let delay = setup_delay();
92//!     let cs = setup_cs_pin(); // chip select pin
93//!     let spi_device = ExclusiveDevice::new(spi, cs, delay).unwrap();
94//!
95//!     let message = b"Hello world!"; // The message we will be sending
96//!
97//!     // Setup some configuration values using the builder pattern
98//!     let config = NrfConfig::default()
99//!         .channel(8)
100//!         .pa_level(PALevel::Min)
101//!         // We will use a payload size the size of our message
102//!         .payload_size(b"Hello world!".len());
103//!
104//!     // Initialize the chip
105//!     let mut delay = setup_delay; // create new delay
106//!
107//!     let mut radio = Nrf24l01::new(spi, ce, &mut delay, config).unwrap();
108//!
109//!     if !radio.is_connected().unwrap() {
110//!         panic!("Chip is not connected.");
111//!     }
112//!
113//!     // Open reading pipe 0 with address "Node1".
114//!     // The sender will have to open its writing pipe with the same address
115//!     // in order to transmit this message successfully.
116//!     radio.open_reading_pipe(DataPipe::DP0, b"Node1").unwrap();
117//!     // Set the chip in RX mode
118//!     radio.start_listening().unwrap();
119//!
120//!     // Keep checking if there is any data available to read
121//!     while !radio.data_available().unwrap() {
122//!         // No data availble, wait 50ms, then check again
123//!         delay.delay_ms(50);
124//!     }
125//!     // Now there is some data availble to read
126//!
127//!     // Initialize empty buffer
128//!     let mut buffer = [0; b"Hello world!".len()];
129//!     radio.read(&mut buffer).unwrap();
130//!
131//!     assert_eq!(buffer, b"Hello world!");
132//!
133//!     loop {}
134//! }
135//! ```
136//!
137//! ## Feature-flags
138//!
139//! - **defmt** provides a `defmt::Format` implementation from the [defmt crate](https://docs.rs/defmt) for all public structs and enums.
140#![warn(
141    missing_docs,
142    missing_copy_implementations,
143    trivial_casts,
144    trivial_numeric_casts
145)]
146#![no_std]
147
148pub mod config;
149pub mod error;
150mod nrf24;
151mod register_acces;
152pub mod status;
153
154pub use crate::nrf24::Nrf24l01;
155
156/// SPI mode. Use this when initializing the SPI instance.
157pub const SPI_MODE: embedded_hal::spi::Mode = embedded_hal::spi::MODE_0;
158/// Max size in bytes of a single payload to be sent or recieved.
159pub const MAX_PAYLOAD_SIZE: u8 = 32;