Skip to content

Commit

Permalink
Bump dependencies versions & update accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
ithinuel committed Oct 14, 2023
1 parent e0588ce commit 55faaba
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 257 deletions.
37 changes: 17 additions & 20 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,27 @@ repository = "https://github.com/rp-rs/spi-pio-rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cortex-m = "0.7.3"
eh1_0_alpha = { version = "=1.0.0-alpha.9", package = "embedded-hal", optional = true }
embedded-hal = "0.2.6"
nb = "1.0.0"
pio = "0.2.0"
pio-proc = "0.2.0"
rp2040-hal = "0.8.0"
fugit = "0.3.5"
defmt = { version = "0.3.0", optional = true }
cortex-m = "0.7.7"
eh1_0_alpha = { version = "1.0.0-rc.1", package = "embedded-hal", optional = true }
embedded-hal = "0.2.7"
nb = "1.1.0"
pio = "0.2.1"
pio-proc = "0.2.2"
rp2040-hal = "0.9.0"
fugit = "0.3.7"
defmt = { version = "0.3.5", optional = true }

[dev-dependencies]
rp2040-hal = { version = "0.8.0", features = ["defmt"] }
rp2040-hal = { version = "0.9.0", features = ["defmt"] }
rp-pico = "*"
panic-halt = "0.2.0"
embedded-hal = "0.2.5"
embedded-hal = "0.2.7"
cortex-m-rt = "0.7"
nb = "1.0"
embedded-sdmmc = "0.4.0"
pio = "0.2.0"
pio-proc = "0.2.1"
critical-section = "1.0.0"
nb = "1.1"
embedded-sdmmc = "0.5.0"
pio = "0.2.1"
pio-proc = "0.2.2"
critical-section = "1.1.2"

defmt = "0.3.0"
defmt = "0.3.5"
defmt-rtt = "0.4.0"

[patch.crates-io]
rp2040-hal = { git = "https://github.com/rp-rs/rp-hal" }
154 changes: 55 additions & 99 deletions examples/pico_spi_pio_sd_card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,37 +39,6 @@
//!
//! $ mkfs.fat /dev/sdj1
//!
//! In the following ASCII art the SD card is also connected to 5 strong pull up
//! resistors. I've found varying values for these, from 50kOhm, 10kOhm
//! down to 5kOhm.
//! Stronger pull up resistors will eat more amperes, but also allow faster
//! data rates.
//!
//! ```text
//! +3.3V
//! Pull Ups ->||||
//! 4x[5kOhm]
//! ||| \
//! _______________ ||| \
//! | DAT2/NC 9\---o|| \ _|USB|_
//! | S DAT3/CS 1|---o+----+------SS--\ |1 R 40|
//! | D CMD/DI 2|----o----+-----MOSI-+-\ |2 P 39|
//! | VSS1 3|-- GND | | | GND-|3 38|- GND
//! | C VDD 4|-- +3.3V | /--SCK--+-+----SPI0 SCK-|4 P 37|
//! | A CLK/SCK 5|---------+-/ | \----SPI0 TX--|5 I 36|- +3.3V
//! | R VSS2 6|-- GND | /--MISO-+------SPI0 RX--|6 C |
//! | D DAT0/DO 7|---------o-/ \------SPI0 CSn-|7 O |
//! | DAT1/IRQ 8|-[5k]- +3.3V | |
//! """""""""""""""" | |
//! | |
//! .........
//! |20 21|
//! """""""
//! Symbols:
//! - (+) crossing lines, not connected
//! - (o) connected lines
//! ```
//!
//! The example can either be used with a probe to receive debug output
//! and also the LED is used as status output. There are different blinking
//! patterns.
Expand Down Expand Up @@ -104,7 +73,7 @@ use defmt_rtt as _;
use panic_halt as _;

// Pull in any important traits
use rp_pico::hal::prelude::*;
use rp_pico::hal::{gpio::PullUp, prelude::*};

// Embed the `Hz` function/trait:
use fugit::RateExtU32;
Expand All @@ -117,18 +86,22 @@ use rp_pico::hal::pac;
// higher-level drivers.
use rp_pico::hal;

// Provides `.delay_ms(…)`.
use embedded_hal::blocking::delay::DelayMs;
// Provides gpio generic operations.
use embedded_hal::digital::v2::OutputPin;

// Link in the embedded_sdmmc crate.
// The `SdMmcSpi` is used for block level access to the card.
// And the `Controller` gives access to the FAT filesystem functions.
use embedded_sdmmc::{Controller, SdMmcSpi, TimeSource, Timestamp, VolumeIdx};
use embedded_sdmmc::{SdCard, TimeSource, Timestamp, VolumeIdx};

// Get the file open mode enum:
use embedded_sdmmc::filesystem::Mode;

/// A dummy timesource, which is mostly important for creating files.
#[derive(Default)]
pub struct DummyTimesource();

pub struct DummyTimesource;
impl TimeSource for DummyTimesource {
// In theory you could use the RTC of the rp2040 here, if you had
// any external time synchronizing device.
Expand All @@ -152,11 +125,10 @@ const BLINK_ERR_2_SHORT: [u8; 4] = [1u8, 0u8, 1u8, 0u8];
const BLINK_ERR_3_SHORT: [u8; 6] = [1u8, 0u8, 1u8, 0u8, 1u8, 0u8];
const BLINK_ERR_4_SHORT: [u8; 8] = [1u8, 0u8, 1u8, 0u8, 1u8, 0u8, 1u8, 0u8];
const BLINK_ERR_5_SHORT: [u8; 10] = [1u8, 0u8, 1u8, 0u8, 1u8, 0u8, 1u8, 0u8, 1u8, 0u8];
const BLINK_ERR_6_SHORT: [u8; 12] = [1u8, 0u8, 1u8, 0u8, 1u8, 0u8, 1u8, 0u8, 1u8, 0u8, 1u8, 0u8];

fn blink_signals(
pin: &mut dyn embedded_hal::digital::v2::OutputPin<Error = core::convert::Infallible>,
delay: &mut cortex_m::delay::Delay,
pin: &mut impl OutputPin<Error = core::convert::Infallible>,
mut delay: impl DelayMs<u32>,
sig: &[u8],
) {
for bit in sig {
Expand All @@ -179,8 +151,8 @@ fn blink_signals(
}

fn blink_signals_loop(
pin: &mut dyn embedded_hal::digital::v2::OutputPin<Error = core::convert::Infallible>,
delay: &mut cortex_m::delay::Delay,
pin: &mut impl OutputPin<Error = core::convert::Infallible>,
mut delay: impl DelayMs<u32>,
sig: &[u8],
) -> ! {
loop {
Expand All @@ -191,11 +163,9 @@ fn blink_signals_loop(

#[entry]
fn main() -> ! {
info!("Program start");

// Grab our singleton objects
let mut pac = pac::Peripherals::take().unwrap();
let core = pac::CorePeripherals::take().unwrap();
let _core = pac::CorePeripherals::take().unwrap();

// Set up the watchdog driver - needed by the clock setup code
let mut watchdog = hal::Watchdog::new(pac.WATCHDOG);
Expand Down Expand Up @@ -230,13 +200,16 @@ fn main() -> ! {
let mut led_pin = pins.led.into_push_pull_output();

// Setup a delay for the LED blink signals:
let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().to_Hz());
let mut timer = rp_pico::hal::timer::Timer::new(pac.TIMER, &mut pac.RESETS, &clocks);

// Enable internal pull up on MISO
let gpio4 = pins.gpio4.into_pull_type::<PullUp>();

// These are implicitly used by the spi driver if they are in the correct mode
let (mut pio, sm0, _, _, _) = pac.PIO0.split(&mut pac.RESETS);
let spi: spi_pio::Spi<'_, _, _, _, _, _, 8> = spi_pio::Spi::new(
(&mut pio, sm0),
(pins.gpio4, pins.gpio3, pins.gpio2),
(gpio4, pins.gpio3, pins.gpio2),
embedded_hal::spi::MODE_0,
16u32.MHz(),
clocks.peripheral_clock.freq(),
Expand All @@ -247,83 +220,64 @@ fn main() -> ! {

// Exchange the uninitialised SPI driver for an initialised one

info!("Aquire SPI SD/MMC BlockDevice...");
let mut sdspi = SdMmcSpi::new(spi, spi_cs);

blink_signals(&mut led_pin, &mut delay, &BLINK_OK_LONG);

// Next we need to aquire the block device and initialize the
// communication with the SD card.
let block = match sdspi.acquire() {
Ok(block) => block,
Err(e) => {
error!("Error retrieving card size: {}", defmt::Debug2Format(&e));
blink_signals_loop(&mut led_pin, &mut delay, &BLINK_ERR_2_SHORT);
}
};

blink_signals(&mut led_pin, &mut delay, &BLINK_OK_LONG);

info!("Init SD card controller...");
let mut cont = Controller::new(block, DummyTimesource::default());

blink_signals(&mut led_pin, &mut delay, &BLINK_OK_LONG);
let sdcard = SdCard::new(spi, spi_cs, timer);

info!("OK!\nCard size...");
match cont.device().card_size_bytes() {
match sdcard.num_bytes() {
Ok(size) => info!("card size is {} bytes", size),
Err(e) => {
error!("Error retrieving card size: {}", defmt::Debug2Format(&e));
blink_signals_loop(&mut led_pin, &mut delay, &BLINK_ERR_3_SHORT);
blink_signals_loop(&mut led_pin, timer, &BLINK_ERR_2_SHORT);
}
}

blink_signals(&mut led_pin, &mut delay, &BLINK_OK_LONG);
blink_signals(&mut led_pin, timer, &BLINK_OK_LONG);

info!("Getting Volume 0...");
let mut volume = match cont.get_volume(VolumeIdx(0)) {
let mut volume_mgr = embedded_sdmmc::VolumeManager::new(sdcard, DummyTimesource);
let mut volume = match volume_mgr.get_volume(VolumeIdx(0)) {
Ok(v) => v,
Err(e) => {
error!("Error getting volume 0: {}", defmt::Debug2Format(&e));
blink_signals_loop(&mut led_pin, &mut delay, &BLINK_ERR_4_SHORT);
blink_signals_loop(&mut led_pin, timer, &BLINK_ERR_3_SHORT);
}
};

blink_signals(&mut led_pin, &mut delay, &BLINK_OK_LONG);
blink_signals(&mut led_pin, timer, &BLINK_OK_LONG);

// After we have the volume (partition) of the drive we got to open the
// root directory:
let dir = match cont.open_root_dir(&volume) {
let dir = match volume_mgr.open_root_dir(&volume) {
Ok(dir) => dir,
Err(e) => {
error!("Error opening root dir: {}", defmt::Debug2Format(&e));
blink_signals_loop(&mut led_pin, &mut delay, &BLINK_ERR_5_SHORT);
blink_signals_loop(&mut led_pin, timer, &BLINK_ERR_4_SHORT);
}
};

info!("Root directory opened!");
blink_signals(&mut led_pin, &mut delay, &BLINK_OK_LONG);
blink_signals(&mut led_pin, timer, &BLINK_OK_LONG);

// This shows how to iterate through the directory and how
// to get the file names (and print them in hope they are UTF-8 compatible):
cont.iterate_dir(&volume, &dir, |ent| {
info!(
"/{}.{}",
core::str::from_utf8(ent.name.base_name()).unwrap(),
core::str::from_utf8(ent.name.extension()).unwrap()
);
})
.unwrap();

blink_signals(&mut led_pin, &mut delay, &BLINK_OK_LONG);
volume_mgr
.iterate_dir(&volume, &dir, |ent| {
info!(
"/{}.{}",
core::str::from_utf8(ent.name.base_name()).unwrap(),
core::str::from_utf8(ent.name.extension()).unwrap()
);
})
.unwrap();

blink_signals(&mut led_pin, timer, &BLINK_OK_LONG);

let mut successful_read = false;

// Next we going to read a file from the SD card:
if let Ok(mut file) = cont.open_file_in_dir(&mut volume, &dir, "O.TST", Mode::ReadOnly) {
if let Ok(mut file) = volume_mgr.open_file_in_dir(&mut volume, &dir, "O.TST", Mode::ReadOnly) {
let mut buf = [0u8; 32];
let read_count = cont.read(&volume, &mut file, &mut buf).unwrap();
cont.close_file(&volume, file).unwrap();
let read_count = volume_mgr.read(&volume, &mut file, &mut buf).unwrap();
volume_mgr.close_file(&volume, file).unwrap();

if read_count >= 2 {
info!("READ {} bytes: {}", read_count, buf);
Expand All @@ -337,22 +291,24 @@ fn main() -> ! {
}
}

blink_signals(&mut led_pin, &mut delay, &BLINK_OK_LONG);
blink_signals(&mut led_pin, timer, &BLINK_OK_LONG);

match cont.open_file_in_dir(&mut volume, &dir, "O.TST", Mode::ReadWriteCreateOrTruncate) {
match volume_mgr.open_file_in_dir(&mut volume, &dir, "O.TST", Mode::ReadWriteCreateOrTruncate) {
Ok(mut file) => {
cont.write(&mut volume, &mut file, b"\x42\x1E").unwrap();
cont.close_file(&volume, file).unwrap();
volume_mgr
.write(&mut volume, &mut file, b"\x42\x1E")
.unwrap();
volume_mgr.close_file(&volume, file).unwrap();
}
Err(e) => {
error!("Error opening file 'O.TST': {}", defmt::Debug2Format(&e));
blink_signals_loop(&mut led_pin, &mut delay, &BLINK_ERR_6_SHORT);
blink_signals_loop(&mut led_pin, timer, &BLINK_ERR_5_SHORT);
}
}

cont.free();
volume_mgr.free();

blink_signals(&mut led_pin, &mut delay, &BLINK_OK_LONG);
blink_signals(&mut led_pin, timer, &BLINK_OK_LONG);

if successful_read {
info!("Successfully read previously written file 'O.TST'");
Expand All @@ -363,11 +319,11 @@ fn main() -> ! {

loop {
if successful_read {
blink_signals(&mut led_pin, &mut delay, &BLINK_OK_SHORT_SHORT_LONG);
blink_signals(&mut led_pin, timer, &BLINK_OK_SHORT_SHORT_LONG);
} else {
blink_signals(&mut led_pin, &mut delay, &BLINK_OK_SHORT_LONG);
blink_signals(&mut led_pin, timer, &BLINK_OK_SHORT_LONG);
}

delay.delay_ms(1000);
timer.delay_ms(1000);
}
}
Loading

0 comments on commit 55faaba

Please sign in to comment.