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 device support and usb_serial example #50

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

kevswims
Copy link
Contributor

Draft PR to go in after the separate #49 to ease review.

This PR adds the following features:

  • Updates dependencies
  • Updates vscode settings for rust analyzer
  • Adds better defmt support along with using probe-run instead of openocd
  • Adds USB device support along with a working usb_serial example

This has been tested on a custom STM32F473 board. If desired I can split this out into separate PRs for the defmt/probe-run and the USB support.

@kevswims kevswims force-pushed the feature/usb-support branch from 6a8fabe to 1b44464 Compare February 12, 2023 05:13
This was tested on an STM32G473 on a custom board with an external 8MHz oscillator
@kevswims kevswims force-pushed the feature/usb-support branch from 1b44464 to 91f65b8 Compare February 15, 2023 19:30
//! This example currently requires an 8MHz external oscillator
//! and assumed an LED is connected to port A6.
//!
//! Further work could be done to setup the HSI48 and the clock
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is done now

// - APB1 = PCLK1 = 72 MHz
// - APB2 = PCLK2 = 72 MHz
// - USB = 48 MHz
let mut rcc = rcc.freeze(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have the CRS support now this could be reworked to not depend on the HSE at all. Would make it easier to run on other hardware.

configure_usb_clock_source(ClockSource::Hsi48, &rcc);

// Configure an LED
let gpioa = dp.GPIOA.split(&mut rcc);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The LED is probably a distraction. Take it out to simplify stuff.

.build();

loop {
if !usb_dev.poll(&mut [&mut serial]) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A separate example or rework this one to poll in an interrupt would be good. Calling poll from the main loop is not super useful for anything but the most trivial of projects.

@@ -85,3 +85,6 @@ pub mod syscfg;
pub mod time;
pub mod timer;
// pub mod watchdog;

#[cfg(all(feature = "stm32-usbd", any(feature = "stm32g473",)))]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The machine feature needs changed here. Do all G4's have USB?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per ST's website, yes they all have USB. I've been bringing a G431 up and this is the only line that needed adjusting. G431 is the minimum spec G4 available.

/// Sets up the clock recovery system for the HSI48 oscillator.
/// TODO: make this configurable for more than just USB applications.
pub fn configure(self, crs_config: CrsConfig, rcc: &Rcc) -> Self {
// TODO: This needs to ensure that the HSI48 is enabled
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of this is done

}

#[inline(always)]
pub fn configure_usb_clock_source(cs: ClockSource, rcc: &Rcc) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the HSI48 is selected, can this do the configuration of it?

q: Some(PllQDiv::DIV_6),
p: None,
})
.ahb_psc(Prescaler::Div2)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought it was worth mentioning because I noticed in test, this div2 is setting the HCLK to 72Mhz -- differs from the comment above.

@mattthebaker
Copy link

I'm bringing up a g431 with usbd-serial and everything is working great so far. Works with CRS and with HSE+PLL configured for the 12Mhz crystal on a custom board.

@usbalbin usbalbin mentioned this pull request Aug 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants