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

Peripheral ref/usb #304

Merged
merged 3 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions esp-hal-common/src/peripherals/esp32c3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@ mod peripherals {
SYSTIMER,
UART0,
UART1,
USB_DEVICE,
}
}
1 change: 1 addition & 0 deletions esp-hal-common/src/peripherals/esp32s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,6 @@ mod peripherals {
UART0,
UART1,
UART2,
USB_DEVICE,
}
}
27 changes: 13 additions & 14 deletions esp-hal-common/src/usb_serial_jtag.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
use core::convert::Infallible;

use crate::pac::{usb_device::RegisterBlock, USB_DEVICE};
use crate::{
pac::{usb_device::RegisterBlock, USB_DEVICE},
peripheral::{Peripheral, PeripheralRef},
};

pub struct UsbSerialJtag<T> {
usb_serial: T,
pub struct UsbSerialJtag<'d, T> {
usb_serial: PeripheralRef<'d, T>,
}

/// Custom USB serial error type
type Error = Infallible;

impl<T> UsbSerialJtag<T>
impl<'d, T> UsbSerialJtag<'d, T>
where
T: Instance,
{
/// Create a new USB serial/JTAG instance with defaults
pub fn new(usb_serial: T) -> Self {
pub fn new(usb_serial: impl Peripheral<P = T> + 'd) -> Self {
crate::into_ref!(usb_serial);
let mut dev = Self { usb_serial };
dev.usb_serial.disable_rx_interrupts();
dev.usb_serial.disable_tx_interrupts();

dev
}

/// Return the raw interface to the underlying USB serial/JTAG instance
pub fn free(self) -> T {
self.usb_serial
}

/// Write data to the serial output in chunks of up to 64 bytes
pub fn write_bytes(&mut self, data: &[u8]) -> Result<(), Error> {
let reg_block = self.usb_serial.register_block();
Expand Down Expand Up @@ -186,14 +185,14 @@ pub trait Instance {
}
}

impl Instance for USB_DEVICE {
impl Instance for crate::peripherals::USB_DEVICE {
#[inline(always)]
fn register_block(&self) -> &RegisterBlock {
self
}
}

impl<T> core::fmt::Write for UsbSerialJtag<T>
impl<T> core::fmt::Write for UsbSerialJtag<'_, T>
where
T: Instance,
{
Expand All @@ -202,7 +201,7 @@ where
}
}

impl<T> embedded_hal::serial::Read<u8> for UsbSerialJtag<T>
impl<T> embedded_hal::serial::Read<u8> for UsbSerialJtag<'_, T>
where
T: Instance,
{
Expand All @@ -213,7 +212,7 @@ where
}
}

impl<T> embedded_hal::serial::Write<u8> for UsbSerialJtag<T>
impl<T> embedded_hal::serial::Write<u8> for UsbSerialJtag<'_, T>
where
T: Instance,
{
Expand Down