Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

USB support for Leonardo #572

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion arduino-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ board-selected = []
mcu-atmega = []
mcu-attiny = []
arduino-diecimila = ["mcu-atmega", "atmega-hal/atmega168", "board-selected"]
arduino-leonardo = ["mcu-atmega", "atmega-hal/atmega32u4", "board-selected"]
arduino-leonardo = ["mcu-atmega", "atmega-hal/atmega32u4", "board-selected", "usb-device"]
arduino-mega2560 = ["mcu-atmega", "atmega-hal/atmega2560", "board-selected"]
arduino-mega1280 = ["mcu-atmega", "atmega-hal/atmega1280", "board-selected"]
arduino-nano = ["mcu-atmega", "atmega-hal/atmega328p", "atmega-hal/enable-extra-adc", "board-selected"]
Expand All @@ -39,6 +39,7 @@ docsrs = ["arduino-uno"]
cfg-if = "1"
embedded-hal = "1.0"
ufmt = "0.2.0"
usb-device = { version = "0.3.2", optional = true }

[dependencies.embedded-hal-v0]
version = "0.2.3"
Expand Down
46 changes: 45 additions & 1 deletion arduino-hal/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![no_std]
#![feature(doc_cfg)]
#![feature(decl_macro)]

//! `arduino-hal`
//! =============
Expand Down Expand Up @@ -175,11 +176,23 @@ pub mod usart {
pub type UsartReader<USART, RX, TX> =
crate::hal::usart::UsartReader<USART, RX, TX, crate::DefaultClock>;
}

#[doc(no_inline)]
#[cfg(feature = "mcu-atmega")]
pub use usart::Usart;

#[cfg(feature = "arduino-leonardo")]
use usb_device::{bus::UsbBusAllocator, device::{UsbDeviceBuilder, UsbVidPid}};
#[cfg(feature = "arduino-leonardo")]
pub mod usb {
pub use crate::hal::usb::*;

pub type AvrUsbBus = crate::hal::usb::AvrUsbBus;
}
#[doc(no_inline)]
#[cfg(feature = "arduino-leonardo")]
pub use usb::AvrUsbBus;


#[cfg(feature = "board-selected")]
pub mod eeprom {
pub use crate::hal::eeprom::{Eeprom, EepromOps, OutOfBoundsError};
Expand Down Expand Up @@ -340,3 +353,34 @@ macro_rules! default_serial {
)
};
}

/// Convenience macro to instantiate the [`UsbBus`] driver for this board.
///
/// # Example
/// ```no_run
/// TODO
/// ```
#[cfg(feature = "arduino-leonardo")]
pub macro default_usb_bus ($usb:expr, $pll:expr) {
unsafe {
static mut USB_BUS: Option<UsbBusAllocator<AvrUsbBus>> = None;
&*USB_BUS.insert($crate::AvrUsbBus::with_suspend_notifier($usb, $pll))
};
}

/// Convenience macro to instantiate the [`UsbDevice`] driver for this board.
///
/// # Example
/// ```no_run
/// TODO
/// ```
#[cfg(feature = "arduino-leonardo")]
pub macro default_usb_device ($usb_bus:expr, $vid:expr, $pid:expr, $strings:expr) {
UsbDeviceBuilder::new(
$usb_bus,
UsbVidPid($vid, $pid)
)
.strings(&[$strings])
.unwrap()
.build()
}
1 change: 1 addition & 0 deletions arduino-hal/src/port/leonardo.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub use atmega_hal::port::{mode, Pin, PinMode, PinOps};

#[allow(non_camel_case_types)] // Mute LedRx and LedTx complaints
avr_hal_generic::renamed_pins! {
/// Pins of the **Arduino Leonardo**.
///
Expand Down
1 change: 1 addition & 0 deletions avr-hal-generic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ embedded-storage = "0.2"
embedded-hal = "1.0"
embedded-hal-bus = "0.1"
unwrap-infallible = "0.1.5"
usb-device = "0.3.2"

[dependencies.embedded-hal-v0]
version = "0.2.3"
Expand Down
2 changes: 2 additions & 0 deletions avr-hal-generic/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![no_std]
#![cfg_attr(avr_hal_asm_macro, feature(asm_experimental_arch))]
#![cfg_attr(not(avr_hal_asm_macro), feature(llvm_asm))]
#![feature(decl_macro)]

pub use embedded_hal as hal;
pub use embedded_hal_v0 as hal_v0;
Expand All @@ -22,6 +23,7 @@ pub mod simple_pwm;
pub mod spi;
pub mod usart;
pub mod wdt;
pub mod usb;

/// Prelude containing all HAL traits
pub mod prelude {
Expand Down
Loading