Skip to content

Commit

Permalink
bump st7789, heapless, embedded-sdmmc, usb-device, usbd-serial & usbd…
Browse files Browse the repository at this point in the history
…-hid versions
  • Loading branch information
ithinuel committed Nov 14, 2023
1 parent 975da35 commit 37a124c
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 106 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ members = [
"boards/waveshare-rp2040-zero",
"boards/waveshare-rp2040-lcd-0-96",
]
[patch.crates-io]
rp2040-hal = { git = "https://github.com/ithinuel/rp-hal", branch = "prepare-for-next-usb-device-release" }
4 changes: 2 additions & 2 deletions boards/boardsource-blok/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ embedded-hal = "0.2.7"
nb = "1.0"
smart-leds = "0.3.0"
ws2812-pio = "0.7.0"
usb-device = "0.2.9"
usbd-hid = "0.5.2"
critical-section = "1.1.1"
usb-device = "0.3.0"
usbd-hid = "0.7.0"

[features]
# This is the set of features we enable by default
Expand Down
4 changes: 3 additions & 1 deletion boards/boardsource-blok/examples/blok_usb_keyboard_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use boardsource_blok::{
Pins, XOSC_CRYSTAL_FREQ,
};
use panic_halt as _;
use usb_device::device::StringDescriptors;
use usb_device::{
bus::UsbBusAllocator, device::UsbDevice, device::UsbDeviceBuilder, device::UsbVidPid,
};
Expand Down Expand Up @@ -84,7 +85,8 @@ fn main() -> ! {
}

let usb_device = UsbDeviceBuilder::new(bus_ref, UsbVidPid(0x1209, 0x0001))
.product("keyboard input")
.strings(&[StringDescriptors::default().product("keyboard input")])
.expect("Failed to configure UsbDevice")
.build();
unsafe {
USB_DEVICE = Some(usb_device);
Expand Down
2 changes: 1 addition & 1 deletion boards/pimoroni-pico-explorer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ embedded-hal = { version = "0.2.7", features = ["unproven"] }
fugit = "0.3.7"
rp2040-boot2 = { version = "0.3.0", optional = true }
rp2040-hal = { version = "0.9.0" }
st7789 = "0.6.1"
st7789 = "0.7.0"

[dev-dependencies]
arrayvec = { version = "0.7.4", default-features = false }
Expand Down
3 changes: 2 additions & 1 deletion boards/pimoroni-pico-explorer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ pub type Screen = ST7789<
Pin<Gpio17, FunctionSioOutput, PullNone>,
>,
DummyPin,
DummyPin,
>;

pub struct PicoExplorer {
Expand Down Expand Up @@ -217,7 +218,7 @@ impl PicoExplorer {

let spii_screen = SPIInterface::new(spi_screen, dc, cs);

let mut screen = ST7789::new(spii_screen, DummyPin, 240, 240);
let mut screen = ST7789::new(spii_screen, Some(DummyPin), Some(DummyPin), 240, 240);

screen.init(delay).unwrap();
screen
Expand Down
10 changes: 5 additions & 5 deletions boards/rp-pico/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ cortex-m-rt = { version = "0.7", optional = true }
fugit = "0.3.7"
rp2040-boot2 = { version = "0.3.0", optional = true }
rp2040-hal = { version = "0.9.0" }
usb-device= "0.2.9"
usb-device= "0.3.0"

[dev-dependencies]
cortex-m = "0.7.7"
cortex-m-rtic = "1.1.4"
critical-section = "1.1.2"
embedded-graphics = "0.7.1"
embedded-hal ="0.2.7"
embedded-sdmmc = "0.5.0"
embedded-sdmmc = "0.6.0"
hd44780-driver = "0.4.0"
heapless = "0.7.16"
heapless = "0.8.0"
i2c-pio = "0.7.0"
nb = "1.1"
panic-halt= "0.2.0"
Expand All @@ -34,8 +34,8 @@ pio-proc = "0.2.2"
rp2040-hal = { version = "0.9.0", features = [ "defmt" ] }
smart-leds = "0.3.0"
ssd1306 = "0.7.1"
usbd-hid = "0.5.2"
usbd-serial = "0.1.1"
usbd-hid = "0.7.0"
usbd-serial = "0.2.0"
ws2812-pio = "0.7.0"

defmt = "0.3.5"
Expand Down
125 changes: 41 additions & 84 deletions boards/rp-pico/examples/pico_spi_sd_card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@
#![no_std]
#![no_main]

use core::cell::RefCell;

// The macro for our start-up function
use rp_pico::entry;

Expand All @@ -73,6 +71,8 @@ use defmt_rtt as _;
use panic_halt as _;

// Pull in any important traits
use embedded_hal::blocking::delay::DelayMs;
use embedded_hal::digital::v2::OutputPin;
use rp_pico::hal::prelude::*;

// Embed the `Hz` function/trait:
Expand All @@ -95,17 +95,14 @@ use rp_pico::hal;
// Link in the embedded_sdmmc crate.
// The `SdMmcSpi` is used for block level access to the card.
// And the `VolumeManager` gives access to the FAT filesystem functions.
use embedded_sdmmc::{SdCard, TimeSource, Timestamp, VolumeIdx, VolumeManager};
use embedded_sdmmc::{SdCard, TimeSource, Timestamp, VolumeIdx};

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

use embedded_hal::blocking::delay::DelayMs;
use embedded_hal::blocking::delay::DelayUs;

/// 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
Expand All @@ -126,14 +123,14 @@ impl TimeSource for DummyTimesource {
const BLINK_OK_LONG: [u8; 1] = [8u8];
const BLINK_OK_SHORT_LONG: [u8; 4] = [1u8, 0u8, 6u8, 0u8];
const BLINK_OK_SHORT_SHORT_LONG: [u8; 6] = [1u8, 0u8, 1u8, 0u8, 6u8, 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];
const BLINK_ERR_2_SHORT: [u8; 6] = [1u8, 0u8, 1u8, 0u8, 1u8, 0u8];
const BLINK_ERR_3_SHORT: [u8; 8] = [1u8, 0u8, 1u8, 0u8, 1u8, 0u8, 1u8, 0u8];
const BLINK_ERR_4_SHORT: [u8; 10] = [1u8, 0u8, 1u8, 0u8, 1u8, 0u8, 1u8, 0u8, 1u8, 0u8];
const BLINK_ERR_5_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 dyn DelayMs<u32>,
pin: &mut impl OutputPin<Error = core::convert::Infallible>,
mut delay: impl DelayMs<u32> + Copy,
sig: &[u8],
) {
for bit in sig {
Expand All @@ -156,8 +153,8 @@ fn blink_signals(
}

fn blink_signals_loop(
pin: &mut dyn embedded_hal::digital::v2::OutputPin<Error = core::convert::Infallible>,
delay: &mut dyn DelayMs<u32>,
pin: &mut impl OutputPin<Error = core::convert::Infallible>,
mut delay: impl DelayMs<u32> + Copy,
sig: &[u8],
) -> ! {
loop {
Expand All @@ -172,7 +169,7 @@ fn main() -> ! {

// 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 @@ -225,61 +222,49 @@ fn main() -> ! {

// We need a delay implementation that can be passed to SdCard and still be used
// for the blink signals.
let mut delay = &SharedDelay::new(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);

info!("Initialize SPI SD/MMC data structures...");
let sdcard = SdCard::new(spi, spi_cs, delay);
let mut volume_mgr = VolumeManager::new(sdcard, DummyTimesource::default());

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

info!("Init SD card controller and retrieve card size...");
match volume_mgr.device().num_bytes() {
info!("OK!\nCard size...");
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);

// Now that the card is initialized, clock can go faster
volume_mgr
.device()
.spi(|spi| spi.set_baudrate(clocks.peripheral_clock.freq(), 16.MHz()));
blink_signals(&mut led_pin, timer, &BLINK_OK_LONG);

info!("Getting Volume 0...");
let mut volume = match volume_mgr.get_volume(VolumeIdx(0)) {
let mut volume_mgr = embedded_sdmmc::VolumeManager::new(sdcard, DummyTimesource);
let volume = match volume_mgr.open_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 volume_mgr.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):
volume_mgr
.iterate_dir(&volume, &dir, |ent| {
.iterate_dir(dir, |ent| {
info!(
"/{}.{}",
core::str::from_utf8(ent.name.base_name()).unwrap(),
Expand All @@ -288,15 +273,15 @@ fn main() -> ! {
})
.unwrap();

blink_signals(&mut led_pin, &mut delay, &BLINK_OK_LONG);
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) = volume_mgr.open_file_in_dir(&mut volume, &dir, "O.TST", Mode::ReadOnly) {
if let Ok(file) = volume_mgr.open_file_in_dir(dir, "O.TST", Mode::ReadOnly) {
let mut buf = [0u8; 32];
let read_count = volume_mgr.read(&volume, &mut file, &mut buf).unwrap();
volume_mgr.close_file(&volume, file).unwrap();
let read_count = volume_mgr.read(file, &mut buf).unwrap();
volume_mgr.close_file(file).unwrap();

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

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

match volume_mgr.open_file_in_dir(&mut volume, &dir, "O.TST", Mode::ReadWriteCreateOrTruncate) {
Ok(mut file) => {
volume_mgr
.write(&mut volume, &mut file, b"\x42\x1E")
.unwrap();
volume_mgr.close_file(&volume, file).unwrap();
match volume_mgr.open_file_in_dir(dir, "O.TST", Mode::ReadWriteCreateOrTruncate) {
Ok(file) => {
volume_mgr.write(file, b"\x42\x1E").unwrap();
volume_mgr.close_file(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);
}
}

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 @@ -338,37 +321,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);
}

delay.delay_ms(1000);
}
}

// Can be removed once we have https://github.com/rp-rs/rp-hal/pull/614,
// ie. when we move to rp2040-hal 0.9
struct SharedDelay {
inner: RefCell<cortex_m::delay::Delay>,
}

impl SharedDelay {
fn new(delay: cortex_m::delay::Delay) -> Self {
Self {
inner: delay.into(),
blink_signals(&mut led_pin, timer, &BLINK_OK_SHORT_LONG);
}
}
}

impl DelayMs<u32> for &SharedDelay {
fn delay_ms(&mut self, ms: u32) {
self.inner.borrow_mut().delay_ms(ms);
}
}

impl DelayUs<u8> for &SharedDelay {
fn delay_us(&mut self, us: u8) {
self.inner.borrow_mut().delay_us(us as u32);
timer.delay_ms(1000);
}
}
10 changes: 6 additions & 4 deletions boards/rp-pico/examples/pico_usb_serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use rp_pico::hal::pac;
use rp_pico::hal;

// USB Device support
use usb_device::{class_prelude::*, prelude::*};
use usb_device::{class_prelude::*, device::StringDescriptors, prelude::*};

// USB Communications Class Device support
use usbd_serial::SerialPort;
Expand Down Expand Up @@ -94,9 +94,11 @@ fn main() -> ! {

// Create a USB device with a fake VID and PID
let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd))
.manufacturer("Fake company")
.product("Serial port")
.serial_number("TEST")
.strings(&[StringDescriptors::default()
.manufacturer("Fake company")
.product("Serial port")
.serial_number("TEST")])
.expect("Failed to configure UsbDevice")
.device_class(2) // from: https://www.usb.org/defined-class-codes
.build();

Expand Down
10 changes: 6 additions & 4 deletions boards/rp-pico/examples/pico_usb_serial_interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use rp_pico::hal::pac;
use rp_pico::hal;

// USB Device support
use usb_device::{class_prelude::*, prelude::*};
use usb_device::{class_prelude::*, device::StringDescriptors, prelude::*};

// USB Communications Class Device support
use usbd_serial::SerialPort;
Expand Down Expand Up @@ -130,9 +130,11 @@ fn main() -> ! {
let usb_serial = SerialPort::new(bus_ref);
// Create a USB device with a fake VID and PID
let usb_device = UsbDeviceBuilder::new(bus_ref, UsbVidPid(0x16c0, 0x27dd))
.manufacturer("Fake company")
.product("Serial port")
.serial_number("TEST")
.strings(&[StringDescriptors::default()
.manufacturer("Fake company")
.product("Serial port")
.serial_number("TEST")])
.expect("Failed to configure UsbDevice")
.device_class(2) // from: https://www.usb.org/defined-class-codes
.build();

Expand Down
Loading

0 comments on commit 37a124c

Please sign in to comment.