Skip to content

Commit

Permalink
Merge pull request #304 from SergioGasquez/main
Browse files Browse the repository at this point in the history
Peripheral ref/usb
  • Loading branch information
SergioGasquez authored Dec 13, 2022
2 parents d7babe6 + 0774018 commit 66c1944
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
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

0 comments on commit 66c1944

Please sign in to comment.