mirror of
https://github.com/ImplFerris/esp32-book.git
synced 2025-09-24 14:31:13 +00:00
rfid migrated to esp-hal 1.0.0-beta
This commit is contained in:
parent
e3adc50013
commit
43e9837e03
@ -14,7 +14,8 @@ esp-generate --chip esp32 joystick-movement
|
||||
|
||||
This will open a screen asking you to select options.
|
||||
|
||||
- Select the option "Adds embassy framework support".
|
||||
- Select the option "Enable unstable HAL features"
|
||||
- Then, select the option "Adds embassy framework support".
|
||||
|
||||
Just save it by pressing "s" in the keyboard.
|
||||
|
||||
|
@ -126,3 +126,142 @@ As you can see in the output, when you run the program, it will display the cont
|
||||
You can also modify the read data program we used earlier with the new key to verify it.
|
||||
|
||||
**Note:** If you're wondering why Key A is not being shown, as we explained earlier, you cannot read the Key A value if you use it for authentication. Refer to the [Access Bits](./access-bits.md) chapter for more details.
|
||||
|
||||
|
||||
## The Full code
|
||||
|
||||
```rust
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use defmt::{info, println};
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_time::{Duration, Timer};
|
||||
use embedded_hal_bus::spi::ExclusiveDevice;
|
||||
use esp_hal::clock::CpuClock;
|
||||
use esp_hal::delay::Delay;
|
||||
use esp_hal::gpio::{Level, Output, OutputConfig};
|
||||
use esp_hal::spi;
|
||||
use esp_hal::spi::master::Spi;
|
||||
use esp_hal::time::Rate;
|
||||
use esp_hal::timer::timg::TimerGroup;
|
||||
use esp_println::{self as _, print};
|
||||
use mfrc522::comm::blocking::spi::SpiInterface;
|
||||
use mfrc522::Mfrc522;
|
||||
|
||||
#[panic_handler]
|
||||
fn panic(_: &core::panic::PanicInfo) -> ! {
|
||||
loop {}
|
||||
}
|
||||
|
||||
#[esp_hal_embassy::main]
|
||||
async fn main(_spawner: Spawner) {
|
||||
// generator version: 0.3.1
|
||||
|
||||
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
|
||||
let peripherals = esp_hal::init(config);
|
||||
|
||||
let timer0 = TimerGroup::new(peripherals.TIMG1);
|
||||
esp_hal_embassy::init(timer0.timer0);
|
||||
|
||||
info!("Embassy initialized!");
|
||||
|
||||
let spi_bus = Spi::new(
|
||||
peripherals.SPI2,
|
||||
spi::master::Config::default()
|
||||
.with_frequency(Rate::from_mhz(5))
|
||||
.with_mode(spi::Mode::_0),
|
||||
)
|
||||
.unwrap()
|
||||
.with_sck(peripherals.GPIO18)
|
||||
.with_mosi(peripherals.GPIO23)
|
||||
.with_miso(peripherals.GPIO19);
|
||||
|
||||
let sd_cs = Output::new(peripherals.GPIO5, Level::High, OutputConfig::default());
|
||||
|
||||
let delay = Delay::new();
|
||||
let spi_dev = ExclusiveDevice::new(spi_bus, sd_cs, delay).unwrap();
|
||||
|
||||
let spi_interface = SpiInterface::new(spi_dev);
|
||||
let mut rfid = Mfrc522::new(spi_interface).init().unwrap();
|
||||
|
||||
let target_sector = 1;
|
||||
let rel_block = 3; //relative block within the sector (4th block within the sector 1)
|
||||
const DATA: [u8; 16] = [
|
||||
0x52, 0x75, 0x73, 0x74, 0x65, 0x64, // Key A: "Rusted"
|
||||
0xFF, 0x07, 0x80, 0x69, // Access bits and trailer byte
|
||||
0x46, 0x65, 0x72, 0x72, 0x69, 0x73, // Key B: "Ferris"
|
||||
];
|
||||
let current_key = &[0xFF; 6];
|
||||
// reset to 0xFF, if you want
|
||||
// const DATA: [u8; 16] = [
|
||||
// 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // Key A: "Rusted"
|
||||
// 0xFF, 0x07, 0x80, 0x69, // Access bits and trailer byte
|
||||
// 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // Key B: "Ferris"
|
||||
// ];
|
||||
// let current_key = &[0x52, 0x75, 0x73, 0x74, 0x65, 0x64];
|
||||
let new_key: &[u8; 6] = &DATA[..6].try_into().unwrap(); // First 6 bytes of the block
|
||||
|
||||
loop {
|
||||
if let Ok(atqa) = rfid.reqa() {
|
||||
println!("Got atqa");
|
||||
Timer::after(Duration::from_millis(50)).await;
|
||||
if let Ok(uid) = rfid.select(&atqa) {
|
||||
println!("\r\n----Before Write----");
|
||||
read_sector(&uid, target_sector, &mut rfid, current_key);
|
||||
|
||||
write_block(&uid, target_sector, rel_block, DATA, &mut rfid, current_key);
|
||||
|
||||
println!("\r\n----After Write----");
|
||||
read_sector(&uid, target_sector, &mut rfid, new_key);
|
||||
rfid.hlta().unwrap();
|
||||
rfid.stop_crypto1().unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn write_block<E, COMM: mfrc522::comm::Interface<Error = E>>(
|
||||
uid: &mfrc522::Uid,
|
||||
sector: u8,
|
||||
rel_block: u8,
|
||||
data: [u8; 16],
|
||||
rfid: &mut Mfrc522<COMM, mfrc522::Initialized>,
|
||||
auth_key: &[u8; 6], //additional argument for the auth key
|
||||
) {
|
||||
let block_offset = sector * 4;
|
||||
let abs_block = block_offset + rel_block;
|
||||
|
||||
rfid.mf_authenticate(uid, block_offset, auth_key)
|
||||
.map_err(|_| "Auth failed")
|
||||
.unwrap();
|
||||
|
||||
rfid.mf_write(abs_block, data)
|
||||
.map_err(|_| "Write failed")
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn read_sector<E, COMM: mfrc522::comm::Interface<Error = E>>(
|
||||
uid: &mfrc522::Uid,
|
||||
sector: u8,
|
||||
rfid: &mut Mfrc522<COMM, mfrc522::Initialized>,
|
||||
auth_key: &[u8; 6], //additional argument for the auth key
|
||||
) {
|
||||
let block_offset = sector * 4;
|
||||
rfid.mf_authenticate(uid, block_offset, auth_key)
|
||||
.map_err(|_| "Auth failed")
|
||||
.unwrap();
|
||||
|
||||
for abs_block in block_offset..block_offset + 4 {
|
||||
let data = rfid.mf_read(abs_block).map_err(|_| "Read failed").unwrap();
|
||||
print_hex_bytes(&data);
|
||||
}
|
||||
}
|
||||
|
||||
fn print_hex_bytes(data: &[u8]) {
|
||||
for &b in data.iter() {
|
||||
print!("{:02x} ", b);
|
||||
}
|
||||
println!("");
|
||||
}
|
||||
```
|
||||
|
@ -104,54 +104,55 @@ The image shows only the first 5 sectors. When you run the program, you should s
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use defmt::{info, println};
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_time::{Duration, Timer};
|
||||
use embedded_hal_bus::spi::ExclusiveDevice;
|
||||
use esp_backtrace as _;
|
||||
use esp_hal::{
|
||||
delay::Delay,
|
||||
gpio::{Level, Output},
|
||||
prelude::*,
|
||||
spi::{
|
||||
master::{Config, Spi},
|
||||
SpiMode,
|
||||
},
|
||||
};
|
||||
use esp_println::{print, println};
|
||||
use log::info;
|
||||
use mfrc522::{comm::blocking::spi::SpiInterface, Mfrc522};
|
||||
use esp_hal::clock::CpuClock;
|
||||
use esp_hal::delay::Delay;
|
||||
use esp_hal::gpio::{Level, Output, OutputConfig};
|
||||
use esp_hal::spi;
|
||||
use esp_hal::spi::master::Spi;
|
||||
use esp_hal::time::Rate;
|
||||
use esp_hal::timer::timg::TimerGroup;
|
||||
use esp_println::{self as _, print};
|
||||
use mfrc522::comm::blocking::spi::SpiInterface;
|
||||
use mfrc522::Mfrc522;
|
||||
|
||||
#[main]
|
||||
#[panic_handler]
|
||||
fn panic(_: &core::panic::PanicInfo) -> ! {
|
||||
loop {}
|
||||
}
|
||||
|
||||
#[esp_hal_embassy::main]
|
||||
async fn main(_spawner: Spawner) {
|
||||
let peripherals = esp_hal::init({
|
||||
let mut config = esp_hal::Config::default();
|
||||
config.cpu_clock = CpuClock::max();
|
||||
config
|
||||
});
|
||||
// generator version: 0.3.1
|
||||
|
||||
esp_println::logger::init_logger_from_env();
|
||||
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
|
||||
let peripherals = esp_hal::init(config);
|
||||
|
||||
let timer0 = esp_hal::timer::timg::TimerGroup::new(peripherals.TIMG1);
|
||||
let timer0 = TimerGroup::new(peripherals.TIMG1);
|
||||
esp_hal_embassy::init(timer0.timer0);
|
||||
|
||||
info!("Embassy initialized!");
|
||||
let delay = Delay::new();
|
||||
|
||||
let spi = Spi::new_with_config(
|
||||
let spi_bus = Spi::new(
|
||||
peripherals.SPI2,
|
||||
Config {
|
||||
frequency: 5.MHz(),
|
||||
mode: SpiMode::Mode0,
|
||||
..Config::default()
|
||||
},
|
||||
spi::master::Config::default()
|
||||
.with_frequency(Rate::from_mhz(5))
|
||||
.with_mode(spi::Mode::_0),
|
||||
)
|
||||
.unwrap()
|
||||
.with_sck(peripherals.GPIO18)
|
||||
.with_mosi(peripherals.GPIO23)
|
||||
.with_miso(peripherals.GPIO19);
|
||||
let sd_cs = Output::new(peripherals.GPIO5, Level::High);
|
||||
let spi = ExclusiveDevice::new(spi, sd_cs, delay).unwrap();
|
||||
|
||||
let spi_interface = SpiInterface::new(spi);
|
||||
let sd_cs = Output::new(peripherals.GPIO5, Level::High, OutputConfig::default());
|
||||
|
||||
let delay = Delay::new();
|
||||
let spi_dev = ExclusiveDevice::new(spi_bus, sd_cs, delay).unwrap();
|
||||
|
||||
let spi_interface = SpiInterface::new(spi_dev);
|
||||
let mut rfid = Mfrc522::new(spi_interface).init().unwrap();
|
||||
|
||||
loop {
|
||||
@ -218,4 +219,5 @@ fn dump_memory<E, COMM: mfrc522::comm::Interface<Error = E>>(
|
||||
read_sector(uid, sector, rfid);
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
@ -80,54 +80,55 @@ Where it shows "13 73 73 31", it will display the UID of your RFID tag.
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use defmt::{info, println};
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_time::{Duration, Timer};
|
||||
use embedded_hal_bus::spi::ExclusiveDevice;
|
||||
use esp_backtrace as _;
|
||||
use esp_hal::{
|
||||
delay::Delay,
|
||||
gpio::{Level, Output},
|
||||
prelude::*,
|
||||
spi::{
|
||||
master::{Config, Spi},
|
||||
SpiMode,
|
||||
},
|
||||
};
|
||||
use esp_println::{print, println};
|
||||
use log::info;
|
||||
use mfrc522::{comm::blocking::spi::SpiInterface, Mfrc522};
|
||||
use esp_hal::clock::CpuClock;
|
||||
use esp_hal::delay::Delay;
|
||||
use esp_hal::gpio::{Level, Output, OutputConfig};
|
||||
use esp_hal::spi;
|
||||
use esp_hal::spi::master::Spi;
|
||||
use esp_hal::time::Rate;
|
||||
use esp_hal::timer::timg::TimerGroup;
|
||||
use esp_println::{self as _, print};
|
||||
use mfrc522::comm::blocking::spi::SpiInterface;
|
||||
use mfrc522::Mfrc522;
|
||||
|
||||
#[main]
|
||||
#[panic_handler]
|
||||
fn panic(_: &core::panic::PanicInfo) -> ! {
|
||||
loop {}
|
||||
}
|
||||
|
||||
#[esp_hal_embassy::main]
|
||||
async fn main(_spawner: Spawner) {
|
||||
let peripherals = esp_hal::init({
|
||||
let mut config = esp_hal::Config::default();
|
||||
config.cpu_clock = CpuClock::max();
|
||||
config
|
||||
});
|
||||
// generator version: 0.3.1
|
||||
|
||||
esp_println::logger::init_logger_from_env();
|
||||
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
|
||||
let peripherals = esp_hal::init(config);
|
||||
|
||||
let timer0 = esp_hal::timer::timg::TimerGroup::new(peripherals.TIMG1);
|
||||
let timer0 = TimerGroup::new(peripherals.TIMG1);
|
||||
esp_hal_embassy::init(timer0.timer0);
|
||||
|
||||
info!("Embassy initialized!");
|
||||
let delay = Delay::new();
|
||||
|
||||
let spi = Spi::new_with_config(
|
||||
let spi_bus = Spi::new(
|
||||
peripherals.SPI2,
|
||||
Config {
|
||||
frequency: 5.MHz(),
|
||||
mode: SpiMode::Mode0,
|
||||
..Config::default()
|
||||
},
|
||||
spi::master::Config::default()
|
||||
.with_frequency(Rate::from_mhz(5))
|
||||
.with_mode(spi::Mode::_0),
|
||||
)
|
||||
.unwrap()
|
||||
.with_sck(peripherals.GPIO18)
|
||||
.with_mosi(peripherals.GPIO23)
|
||||
.with_miso(peripherals.GPIO19);
|
||||
let sd_cs = Output::new(peripherals.GPIO5, Level::High);
|
||||
let spi = ExclusiveDevice::new(spi, sd_cs, delay).unwrap();
|
||||
|
||||
let spi_interface = SpiInterface::new(spi);
|
||||
let sd_cs = Output::new(peripherals.GPIO5, Level::High, OutputConfig::default());
|
||||
|
||||
let delay = Delay::new();
|
||||
let spi_dev = ExclusiveDevice::new(spi_bus, sd_cs, delay).unwrap();
|
||||
|
||||
let spi_interface = SpiInterface::new(spi_dev);
|
||||
let mut rfid = Mfrc522::new(spi_interface).init().unwrap();
|
||||
|
||||
let sector_num = 0;
|
||||
@ -167,7 +168,7 @@ fn print_hex_bytes(data: &[u8]) {
|
||||
for &b in data.iter() {
|
||||
print!("{:02x} ", b);
|
||||
}
|
||||
println!();
|
||||
println!("");
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -11,7 +11,8 @@ esp-generate --chip esp32 rfid-uid
|
||||
|
||||
This will open a screen asking you to select options.
|
||||
|
||||
- Select the option "Adds embassy framework support".
|
||||
- Select the option "Enable unstable HAL features"
|
||||
- Then, select the option "Adds embassy framework support".
|
||||
|
||||
Just save it by pressing "s" in the keyboard.
|
||||
|
||||
@ -39,18 +40,19 @@ So, we need to get the SpiDevice from the SpiBus to use it with the SD card. Thi
|
||||
To communicate with the RFID module, we will initialize the SPI instance using the SPI2 peripheral. In this setup, we will configure the SPI clock to 5MHz and map the necessary pins to GPIOs for proper communication.
|
||||
|
||||
```rust
|
||||
let spi = Spi::new_with_config(
|
||||
let spi_bus = Spi::new(
|
||||
peripherals.SPI2,
|
||||
Config {
|
||||
frequency: 5.MHz(),
|
||||
mode: SpiMode::Mode0,
|
||||
..Config::default()
|
||||
},
|
||||
spi::master::Config::default()
|
||||
.with_frequency(Rate::from_mhz(5))
|
||||
.with_mode(spi::Mode::_0),
|
||||
)
|
||||
.unwrap()
|
||||
.with_sck(peripherals.GPIO18)
|
||||
.with_mosi(peripherals.GPIO23)
|
||||
.with_miso(peripherals.GPIO19);
|
||||
let sd_cs = Output::new(peripherals.GPIO5, Level::High);
|
||||
.with_miso(peripherals.GPIO19)
|
||||
.into_async();
|
||||
|
||||
let sd_cs = Output::new(peripherals.GPIO5, Level::High, OutputConfig::default());
|
||||
```
|
||||
|
||||
### Getting the `SpiDevice` from SPI Bus
|
||||
@ -58,14 +60,14 @@ To work with the mfrc522 crate, we need an `SpiDevice`. Since we only have the S
|
||||
|
||||
```rust
|
||||
let delay = Delay::new();
|
||||
let spi = ExclusiveDevice::new(spi, sd_cs, delay).unwrap();
|
||||
let spi_dev = ExclusiveDevice::new(spi_bus, sd_cs, delay).unwrap();
|
||||
```
|
||||
|
||||
### Initialize the mfrc522
|
||||
Next, we initialize the MFRC522 driver. To do this, we wrap the SpiDevice instance with the SpiInterface wrapper provided by the mfrc522 crate and pass it to the Mfrc522 initialization:
|
||||
|
||||
```rust
|
||||
let spi_interface = SpiInterface::new(spi);
|
||||
let spi_interface = SpiInterface::new(spi_dev);
|
||||
let mut rfid = Mfrc522::new(spi_interface).init().unwrap();
|
||||
```
|
||||
|
||||
@ -79,7 +81,7 @@ fn print_hex_bytes(data: &[u8]) {
|
||||
for &b in data.iter() {
|
||||
print!("{:02x} ", b);
|
||||
}
|
||||
println!();
|
||||
println!("");
|
||||
}
|
||||
```
|
||||
|
||||
@ -119,54 +121,62 @@ After flashing the code onto the ESP32, bring the RFID tag close to the reader.
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use defmt::{info, println};
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_time::{Duration, Timer};
|
||||
use embedded_hal_bus::spi::ExclusiveDevice;
|
||||
use esp_backtrace as _;
|
||||
use esp_hal::{
|
||||
delay::Delay,
|
||||
gpio::{Level, Output},
|
||||
prelude::*,
|
||||
spi::{
|
||||
master::{Config, Spi},
|
||||
SpiMode,
|
||||
},
|
||||
};
|
||||
use esp_println::{print, println};
|
||||
use log::info;
|
||||
use mfrc522::{comm::blocking::spi::SpiInterface, Mfrc522};
|
||||
use esp_hal::clock::CpuClock;
|
||||
use esp_hal::delay::Delay;
|
||||
use esp_hal::gpio::{Level, Output, OutputConfig};
|
||||
use esp_hal::spi;
|
||||
use esp_hal::spi::master::Spi;
|
||||
use esp_hal::time::Rate;
|
||||
use esp_hal::timer::timg::TimerGroup;
|
||||
use esp_println::{self as _, print};
|
||||
use mfrc522::comm::blocking::spi::SpiInterface;
|
||||
use mfrc522::Mfrc522;
|
||||
|
||||
#[main]
|
||||
#[panic_handler]
|
||||
fn panic(_: &core::panic::PanicInfo) -> ! {
|
||||
loop {}
|
||||
}
|
||||
|
||||
fn print_hex_bytes(data: &[u8]) {
|
||||
for &b in data.iter() {
|
||||
print!("{:02x} ", b);
|
||||
}
|
||||
println!("");
|
||||
}
|
||||
|
||||
#[esp_hal_embassy::main]
|
||||
async fn main(_spawner: Spawner) {
|
||||
let peripherals = esp_hal::init({
|
||||
let mut config = esp_hal::Config::default();
|
||||
config.cpu_clock = CpuClock::max();
|
||||
config
|
||||
});
|
||||
// generator version: 0.3.1
|
||||
|
||||
esp_println::logger::init_logger_from_env();
|
||||
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
|
||||
let peripherals = esp_hal::init(config);
|
||||
|
||||
let timer0 = esp_hal::timer::timg::TimerGroup::new(peripherals.TIMG1);
|
||||
let timer0 = TimerGroup::new(peripherals.TIMG1);
|
||||
esp_hal_embassy::init(timer0.timer0);
|
||||
|
||||
info!("Embassy initialized!");
|
||||
let delay = Delay::new();
|
||||
|
||||
let spi = Spi::new_with_config(
|
||||
let spi_bus = Spi::new(
|
||||
peripherals.SPI2,
|
||||
Config {
|
||||
frequency: 5.MHz(),
|
||||
mode: SpiMode::Mode0,
|
||||
..Config::default()
|
||||
},
|
||||
spi::master::Config::default()
|
||||
.with_frequency(Rate::from_mhz(5))
|
||||
.with_mode(spi::Mode::_0),
|
||||
)
|
||||
.unwrap()
|
||||
.with_sck(peripherals.GPIO18)
|
||||
.with_mosi(peripherals.GPIO23)
|
||||
.with_miso(peripherals.GPIO19);
|
||||
let sd_cs = Output::new(peripherals.GPIO5, Level::High);
|
||||
let spi = ExclusiveDevice::new(spi, sd_cs, delay).unwrap();
|
||||
|
||||
let spi_interface = SpiInterface::new(spi);
|
||||
let sd_cs = Output::new(peripherals.GPIO5, Level::High, OutputConfig::default());
|
||||
|
||||
let delay = Delay::new();
|
||||
let spi_dev = ExclusiveDevice::new(spi_bus, sd_cs, delay).unwrap();
|
||||
|
||||
let spi_interface = SpiInterface::new(spi_dev);
|
||||
let mut rfid = Mfrc522::new(spi_interface).init().unwrap();
|
||||
|
||||
loop {
|
||||
@ -180,12 +190,4 @@ async fn main(_spawner: Spawner) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn print_hex_bytes(data: &[u8]) {
|
||||
for &b in data.iter() {
|
||||
print!("{:02x} ", b);
|
||||
}
|
||||
println!();
|
||||
}
|
||||
|
||||
```
|
||||
|
@ -87,54 +87,55 @@ When you run the program, the output will display the hex representation of "imp
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use defmt::{info, println};
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_time::{Duration, Timer};
|
||||
use embedded_hal_bus::spi::ExclusiveDevice;
|
||||
use esp_backtrace as _;
|
||||
use esp_hal::{
|
||||
delay::Delay,
|
||||
gpio::{Level, Output},
|
||||
prelude::*,
|
||||
spi::{
|
||||
master::{Config, Spi},
|
||||
SpiMode,
|
||||
},
|
||||
};
|
||||
use esp_println::{print, println};
|
||||
use log::info;
|
||||
use mfrc522::{comm::blocking::spi::SpiInterface, Mfrc522};
|
||||
use esp_hal::clock::CpuClock;
|
||||
use esp_hal::delay::Delay;
|
||||
use esp_hal::gpio::{Level, Output, OutputConfig};
|
||||
use esp_hal::spi;
|
||||
use esp_hal::spi::master::Spi;
|
||||
use esp_hal::time::Rate;
|
||||
use esp_hal::timer::timg::TimerGroup;
|
||||
use esp_println::{self as _, print};
|
||||
use mfrc522::comm::blocking::spi::SpiInterface;
|
||||
use mfrc522::Mfrc522;
|
||||
|
||||
#[main]
|
||||
#[panic_handler]
|
||||
fn panic(_: &core::panic::PanicInfo) -> ! {
|
||||
loop {}
|
||||
}
|
||||
|
||||
#[esp_hal_embassy::main]
|
||||
async fn main(_spawner: Spawner) {
|
||||
let peripherals = esp_hal::init({
|
||||
let mut config = esp_hal::Config::default();
|
||||
config.cpu_clock = CpuClock::max();
|
||||
config
|
||||
});
|
||||
// generator version: 0.3.1
|
||||
|
||||
esp_println::logger::init_logger_from_env();
|
||||
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
|
||||
let peripherals = esp_hal::init(config);
|
||||
|
||||
let timer0 = esp_hal::timer::timg::TimerGroup::new(peripherals.TIMG1);
|
||||
let timer0 = TimerGroup::new(peripherals.TIMG1);
|
||||
esp_hal_embassy::init(timer0.timer0);
|
||||
|
||||
info!("Embassy initialized!");
|
||||
let delay = Delay::new();
|
||||
|
||||
let spi = Spi::new_with_config(
|
||||
let spi_bus = Spi::new(
|
||||
peripherals.SPI2,
|
||||
Config {
|
||||
frequency: 5.MHz(),
|
||||
mode: SpiMode::Mode0,
|
||||
..Config::default()
|
||||
},
|
||||
spi::master::Config::default()
|
||||
.with_frequency(Rate::from_mhz(5))
|
||||
.with_mode(spi::Mode::_0),
|
||||
)
|
||||
.unwrap()
|
||||
.with_sck(peripherals.GPIO18)
|
||||
.with_mosi(peripherals.GPIO23)
|
||||
.with_miso(peripherals.GPIO19);
|
||||
let sd_cs = Output::new(peripherals.GPIO5, Level::High);
|
||||
let spi = ExclusiveDevice::new(spi, sd_cs, delay).unwrap();
|
||||
|
||||
let spi_interface = SpiInterface::new(spi);
|
||||
let sd_cs = Output::new(peripherals.GPIO5, Level::High, OutputConfig::default());
|
||||
|
||||
let delay = Delay::new();
|
||||
let spi_dev = ExclusiveDevice::new(spi_bus, sd_cs, delay).unwrap();
|
||||
|
||||
let spi_interface = SpiInterface::new(spi_dev);
|
||||
let mut rfid = Mfrc522::new(spi_interface).init().unwrap();
|
||||
|
||||
let target_sector = 4;
|
||||
@ -206,6 +207,6 @@ fn print_hex_bytes(data: &[u8]) {
|
||||
for &b in data.iter() {
|
||||
print!("{:02x} ", b);
|
||||
}
|
||||
println!();
|
||||
println!("");
|
||||
}
|
||||
```
|
||||
|
@ -76,7 +76,7 @@ To communicate with the SD card reader, we will initialize the SPI instance usin
|
||||
The SCK (Serial Clock) will be assigned to GPIO14, MOSI (Master Out, Slave In) to GPIO15, and MISO (Master In, Slave Out) to GPIO2. Additionally, we will configure the CS (Chip Select) pin on GPIO13 and set its initial state to High.
|
||||
|
||||
```rust
|
||||
let spi = Spi::new(
|
||||
let spi_bus = Spi::new(
|
||||
peripherals.SPI2,
|
||||
spi::master::Config::default()
|
||||
.with_frequency(Rate::from_khz(400))
|
||||
@ -94,8 +94,8 @@ let sd_cs = Output::new(peripherals.GPIO5, Level::High, OutputConfig::default())
|
||||
Once the SPI is configured, we will create an SpiDevice for the SD card reader. To achieve this, we will use the ExclusiveDevice provided by the embedded-hal-bus crate. Finally, we initialize the SD card.
|
||||
|
||||
```rust
|
||||
let spi = ExclusiveDevice::new(spi, sd_cs, Delay).unwrap();
|
||||
let sdcard = SdCard::new(spi, Delay);
|
||||
let spi_dev = ExclusiveDevice::new(spi_bus, sd_cs, Delay).unwrap();
|
||||
let sdcard = SdCard::new(spi_dev, Delay);
|
||||
```
|
||||
|
||||
## Volume manager
|
||||
@ -215,7 +215,7 @@ async fn main(_spawner: Spawner) {
|
||||
|
||||
info!("Embassy initialized!");
|
||||
|
||||
let spi = Spi::new(
|
||||
let spi_bus = Spi::new(
|
||||
peripherals.SPI2,
|
||||
spi::master::Config::default()
|
||||
.with_frequency(Rate::from_khz(400))
|
||||
@ -227,9 +227,9 @@ async fn main(_spawner: Spawner) {
|
||||
.with_miso(peripherals.GPIO19)
|
||||
.into_async();
|
||||
let sd_cs = Output::new(peripherals.GPIO5, Level::High, OutputConfig::default());
|
||||
let spi = ExclusiveDevice::new(spi, sd_cs, Delay).unwrap();
|
||||
let spi_dev = ExclusiveDevice::new(spi_bus, sd_cs, Delay).unwrap();
|
||||
|
||||
let sdcard = SdCard::new(spi, Delay);
|
||||
let sdcard = SdCard::new(spi_dev, Delay);
|
||||
let mut volume_mgr = VolumeManager::new(sdcard, DummyTimesource::default());
|
||||
|
||||
println!("Init SD card controller and retrieve card size...");
|
||||
|
@ -191,7 +191,7 @@ async fn main(_spawner: Spawner) {
|
||||
info!("Embassy initialized!");
|
||||
|
||||
// Configure SPI
|
||||
let spi = Spi::new(
|
||||
let spi_bus = Spi::new(
|
||||
peripherals.SPI2,
|
||||
spi::master::Config::default()
|
||||
.with_frequency(Rate::from_khz(400))
|
||||
@ -203,7 +203,7 @@ async fn main(_spawner: Spawner) {
|
||||
.with_miso(peripherals.GPIO19)
|
||||
.into_async();
|
||||
let sd_cs = Output::new(peripherals.GPIO5, Level::High, OutputConfig::default());
|
||||
let spi = ExclusiveDevice::new(spi, sd_cs, Delay).unwrap();
|
||||
let spi_dev = ExclusiveDevice::new(spi_bus, sd_cs, Delay).unwrap();
|
||||
|
||||
// Timer for sdcard
|
||||
let rtc = Rtc::new(peripherals.LPWR);
|
||||
@ -213,7 +213,7 @@ async fn main(_spawner: Spawner) {
|
||||
|
||||
let sd_timer = SdTimeSource::new(rtc);
|
||||
|
||||
let sdcard = SdCard::new(spi, Delay);
|
||||
let sdcard = SdCard::new(spi_dev, Delay);
|
||||
let mut volume_mgr = VolumeManager::new(sdcard, sd_timer);
|
||||
|
||||
println!("Init SD card controller and retrieve card size...");
|
||||
|
Loading…
x
Reference in New Issue
Block a user