Skip to content

Commit

Permalink
Update sd_card
Browse files Browse the repository at this point in the history
  • Loading branch information
ianrrees committed Aug 27, 2024
1 parent 84759fb commit 2f712ab
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 35 deletions.
5 changes: 4 additions & 1 deletion boards/pygamer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ version = "0.5.1"
optional = true

[dependencies.embedded-sdmmc]
version = "0.3.0"
version = "0.8.0"
optional = true

[dependencies.usb-device]
Expand All @@ -45,6 +45,9 @@ optional = true
usbd-serial = "0.2"
panic-halt = "0.2"
embedded-graphics = "0.7.1"

# Currently just used by sd_card example
embedded-hal-bus = "0.2.0"
smart-leds = "0.3"
ws2812-spi = { version = "0.4.0", features = ["mosi_idle_high"] }
lis3dh = "0.1.0"
Expand Down
84 changes: 50 additions & 34 deletions boards/pygamer/examples/sd_card.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
//! Place a bitmap image on the screen. Ferris pngs from https://rustacean.net/
//! Convert png to .bmp bytes
//! Place a series of bitmap images on the screen
//!
//! Ferris pngs from <https://rustacean.net/> , convert png to .bmp bytes:
//! * Resize and export images directly from image editor by saving as .bmp and
//! choosing 16bit R5 G6 B5
//! * OR Convert with imagemagick: convert rustacean-flat-noshadow.png -type
//! truecolor -define bmp:subtype=RGB565 -depth 16 -strip -resize 86x64
//! ferris.bmp
//!
//! cp ferris*.bmp /Volumes/SDCARD/
//! * SD card should have one (or at least, the first) primary partition of type
//! W95 FAT32, formatted eg `sudo mkfs.fat /dev/path/to/sdcardp1`
//! * Put assets/ferris*.bmp in the root directory of the sd card `cp
//! assets/ferris*.bmp /Volumes/SDCARD/`
#![no_std]
#![no_main]
Expand All @@ -19,12 +22,14 @@ use pygamer as bsp;
use embedded_graphics::prelude::*;
use embedded_graphics::primitives::{PrimitiveStyleBuilder, Rectangle};
use embedded_graphics::{image::Image, pixelcolor::Rgb565};
use embedded_sdmmc::{TimeSource, Timestamp, VolumeIdx};
use embedded_hal_bus::spi::ExclusiveDevice;
use embedded_sdmmc::{Mode, TimeSource, Timestamp, VolumeIdx, VolumeManager};
use hal::clock::GenericClockController;
use hal::delay::Delay;
use hal::ehal::digital::v1_compat::OldOutputPin;
use hal::nb;
use hal::prelude::*;
use hal::time::MegaHertz;
use hal::time::Hertz;
use hal::timer::TimerCounter;
use pac::{CorePeripherals, Peripherals};
use tinybmp::Bmp;

Expand All @@ -45,16 +50,6 @@ fn main() -> ! {

let mut red_led: RedLed = pins.led_pin.into();

let sdmmc_cs: OldOutputPin<_> = pins.sd_cs_pin.into_push_pull_output().into();
let sdmmc_spi = pins.spi.init(
&mut clocks,
MegaHertz(3),
peripherals.SERCOM1,
&mut peripherals.MCLK,
);
let mut cont =
embedded_sdmmc::Controller::new(embedded_sdmmc::SdMmcSpi::new(sdmmc_spi, sdmmc_cs), Clock);

let (mut display, _backlight) = pins
.display
.init(
Expand All @@ -76,9 +71,30 @@ fn main() -> ! {
.draw(&mut display)
.unwrap();

cont.device().init().unwrap();
let mut volume = cont.get_volume(VolumeIdx(0)).unwrap();
let dir = cont.open_root_dir(&volume).unwrap();
let gclk0 = clocks.gclk0();
let timer_clock = clocks.tc4_tc5(&gclk0).unwrap();
let mut khz_timer = TimerCounter::tc4_(&timer_clock, peripherals.TC4, &mut peripherals.MCLK);
InterruptDrivenTimer::start(&mut khz_timer, Hertz::kHz(1).into_duration());

let sdmmc_cs = pins.sd_cs_pin.into_push_pull_output();
let sdmmc_spi_bus = pins.spi.init(
&mut clocks,
3.MHz(),
peripherals.SERCOM1,
&mut peripherals.MCLK,
);

let sdmmc_spi =
ExclusiveDevice::new_no_delay(sdmmc_spi_bus, sdmmc_cs).expect("Failed to create SpiDevice");

let card = embedded_sdmmc::SdCard::new(sdmmc_spi, delay);

let mut volume_mgr = VolumeManager::new(card, Clock {});
let mut volume = volume_mgr
.open_volume(VolumeIdx(0))
.expect("Failed to open volume");

let mut dir = volume.open_root_dir().expect("Failed to open root dir");

let mut scratch = [0u8; 11008];

Expand All @@ -87,22 +103,22 @@ fn main() -> ! {

loop {
for image in images.iter() {
if let Ok(mut f) =
cont.open_file_in_dir(&mut volume, &dir, image, embedded_sdmmc::Mode::ReadOnly)
{
let _ = cont.read(&volume, &mut f, &mut scratch);

let raw_image: Bmp<Rgb565> = Bmp::from_slice(&scratch).unwrap();

let ferris = Image::new(&raw_image, Point::new(32, 32));

let _ = ferris.draw(&mut display);
match dir.open_file_in_dir(*image, Mode::ReadOnly) {
Ok(mut f) => {
let _ = f.read(&mut scratch);
let raw_image: Bmp<Rgb565> = Bmp::from_slice(&scratch).unwrap();
let ferris = Image::new(&raw_image, Point::new(32, 32));
let _ = ferris.draw(&mut display);
f.close();
}
Err(err) => {
red_led.set_high().ok();
}
}

cont.close_file(&volume, f).ok();
} else {
red_led.set_high().ok();
for _ in 0..100 {
nb::block!(InterruptDrivenTimer::wait(&mut khz_timer)).unwrap();
}
delay.delay_ms(200u8);
}
}
}
Expand Down

0 comments on commit 2f712ab

Please sign in to comment.