diff options
| author | jrmoulton <[email protected]> | 2025-06-10 15:47:54 -0600 |
|---|---|---|
| committer | jrmoulton <[email protected]> | 2025-06-10 15:48:36 -0600 |
| commit | cfad9798ff99d4de0571a512d156b5fe1ef1d427 (patch) | |
| tree | fc3bf670f82d139de19466cddad1e909db7f3d2e /examples/stm32g4 | |
| parent | fc342915e6155dec7bafa3e135da7f37a9a07f5c (diff) | |
| parent | 6186d111a5c150946ee5b7e9e68d987a38c1a463 (diff) | |
merge new embassy changes
Diffstat (limited to 'examples/stm32g4')
| -rw-r--r-- | examples/stm32g4/Cargo.toml | 16 | ||||
| -rw-r--r-- | examples/stm32g4/src/bin/adc_differential.rs | 47 | ||||
| -rw-r--r-- | examples/stm32g4/src/bin/adc_dma.rs | 60 | ||||
| -rw-r--r-- | examples/stm32g4/src/bin/adc_oversampling.rs | 57 | ||||
| -rw-r--r-- | examples/stm32g4/src/bin/pwm.rs | 19 | ||||
| -rw-r--r-- | examples/stm32g4/src/bin/usb_c_pd.rs | 2 | ||||
| -rw-r--r-- | examples/stm32g4/src/bin/usb_serial.rs | 5 |
7 files changed, 182 insertions, 24 deletions
diff --git a/examples/stm32g4/Cargo.toml b/examples/stm32g4/Cargo.toml index 2768147a1..3d2c2aa7d 100644 --- a/examples/stm32g4/Cargo.toml +++ b/examples/stm32g4/Cargo.toml | |||
| @@ -6,22 +6,22 @@ license = "MIT OR Apache-2.0" | |||
| 6 | 6 | ||
| 7 | [dependencies] | 7 | [dependencies] |
| 8 | # Change stm32g491re to your chip name, if necessary. | 8 | # Change stm32g491re to your chip name, if necessary. |
| 9 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = [ "defmt", "time-driver-any", "stm32g491re", "memory-x", "unstable-pac", "exti"] } | 9 | embassy-stm32 = { version = "0.2.0", path = "../../embassy-stm32", features = [ "defmt", "time-driver-any", "stm32g491re", "memory-x", "unstable-pac", "exti"] } |
| 10 | embassy-sync = { version = "0.6.0", path = "../../embassy-sync", features = ["defmt"] } | 10 | embassy-sync = { version = "0.7.0", path = "../../embassy-sync", features = ["defmt"] } |
| 11 | embassy-executor = { version = "0.6.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } | 11 | embassy-executor = { version = "0.7.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "defmt"] } |
| 12 | embassy-time = { version = "0.3.2", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } | 12 | embassy-time = { version = "0.4.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } |
| 13 | embassy-usb = { version = "0.3.0", path = "../../embassy-usb", features = ["defmt"] } | 13 | embassy-usb = { version = "0.4.0", path = "../../embassy-usb", features = ["defmt"] } |
| 14 | embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } | 14 | embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } |
| 15 | usbd-hid = "0.8.1" | 15 | usbd-hid = "0.8.1" |
| 16 | 16 | ||
| 17 | defmt = "0.3" | 17 | defmt = "1.0.1" |
| 18 | defmt-rtt = "0.4" | 18 | defmt-rtt = "1.0.0" |
| 19 | 19 | ||
| 20 | cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] } | 20 | cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] } |
| 21 | cortex-m-rt = "0.7.0" | 21 | cortex-m-rt = "0.7.0" |
| 22 | embedded-hal = "0.2.6" | 22 | embedded-hal = "0.2.6" |
| 23 | embedded-can = { version = "0.4" } | 23 | embedded-can = { version = "0.4" } |
| 24 | panic-probe = { version = "0.3", features = ["print-defmt"] } | 24 | panic-probe = { version = "1.0.0", features = ["print-defmt"] } |
| 25 | heapless = { version = "0.8", default-features = false } | 25 | heapless = { version = "0.8", default-features = false } |
| 26 | static_cell = "2.0.0" | 26 | static_cell = "2.0.0" |
| 27 | 27 | ||
diff --git a/examples/stm32g4/src/bin/adc_differential.rs b/examples/stm32g4/src/bin/adc_differential.rs new file mode 100644 index 000000000..78d071d45 --- /dev/null +++ b/examples/stm32g4/src/bin/adc_differential.rs | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | //! adc differential mode example | ||
| 2 | //! | ||
| 3 | //! This example uses adc1 in differential mode | ||
| 4 | //! p:pa0 n:pa1 | ||
| 5 | |||
| 6 | #![no_std] | ||
| 7 | #![no_main] | ||
| 8 | |||
| 9 | use defmt::*; | ||
| 10 | use embassy_executor::Spawner; | ||
| 11 | use embassy_stm32::adc::{Adc, SampleTime}; | ||
| 12 | use embassy_stm32::Config; | ||
| 13 | use embassy_time::Timer; | ||
| 14 | use {defmt_rtt as _, panic_probe as _}; | ||
| 15 | |||
| 16 | #[embassy_executor::main] | ||
| 17 | async fn main(_spawner: Spawner) { | ||
| 18 | let mut config = Config::default(); | ||
| 19 | { | ||
| 20 | use embassy_stm32::rcc::*; | ||
| 21 | config.rcc.pll = Some(Pll { | ||
| 22 | source: PllSource::HSI, | ||
| 23 | prediv: PllPreDiv::DIV4, | ||
| 24 | mul: PllMul::MUL85, | ||
| 25 | divp: None, | ||
| 26 | divq: None, | ||
| 27 | // Main system clock at 170 MHz | ||
| 28 | divr: Some(PllRDiv::DIV2), | ||
| 29 | }); | ||
| 30 | config.rcc.mux.adc12sel = mux::Adcsel::SYS; | ||
| 31 | config.rcc.sys = Sysclk::PLL1_R; | ||
| 32 | } | ||
| 33 | let mut p = embassy_stm32::init(config); | ||
| 34 | |||
| 35 | let mut adc = Adc::new(p.ADC1); | ||
| 36 | adc.set_sample_time(SampleTime::CYCLES247_5); | ||
| 37 | adc.set_differential(&mut p.PA0, true); //p:pa0,n:pa1 | ||
| 38 | |||
| 39 | // can also use | ||
| 40 | // adc.set_differential_channel(1, true); | ||
| 41 | info!("adc initialized"); | ||
| 42 | loop { | ||
| 43 | let measured = adc.blocking_read(&mut p.PA0); | ||
| 44 | info!("data: {}", measured); | ||
| 45 | Timer::after_millis(500).await; | ||
| 46 | } | ||
| 47 | } | ||
diff --git a/examples/stm32g4/src/bin/adc_dma.rs b/examples/stm32g4/src/bin/adc_dma.rs new file mode 100644 index 000000000..202704085 --- /dev/null +++ b/examples/stm32g4/src/bin/adc_dma.rs | |||
| @@ -0,0 +1,60 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_executor::Spawner; | ||
| 6 | use embassy_stm32::adc::{Adc, AdcChannel as _, SampleTime}; | ||
| 7 | use embassy_stm32::Config; | ||
| 8 | use embassy_time::Timer; | ||
| 9 | use {defmt_rtt as _, panic_probe as _}; | ||
| 10 | |||
| 11 | static mut DMA_BUF: [u16; 2] = [0; 2]; | ||
| 12 | |||
| 13 | #[embassy_executor::main] | ||
| 14 | async fn main(_spawner: Spawner) { | ||
| 15 | let mut read_buffer = unsafe { &mut DMA_BUF[..] }; | ||
| 16 | |||
| 17 | let mut config = Config::default(); | ||
| 18 | { | ||
| 19 | use embassy_stm32::rcc::*; | ||
| 20 | config.rcc.pll = Some(Pll { | ||
| 21 | source: PllSource::HSI, | ||
| 22 | prediv: PllPreDiv::DIV4, | ||
| 23 | mul: PllMul::MUL85, | ||
| 24 | divp: None, | ||
| 25 | divq: None, | ||
| 26 | // Main system clock at 170 MHz | ||
| 27 | divr: Some(PllRDiv::DIV2), | ||
| 28 | }); | ||
| 29 | config.rcc.mux.adc12sel = mux::Adcsel::SYS; | ||
| 30 | config.rcc.sys = Sysclk::PLL1_R; | ||
| 31 | } | ||
| 32 | let p = embassy_stm32::init(config); | ||
| 33 | |||
| 34 | info!("Hello World!"); | ||
| 35 | |||
| 36 | let mut adc = Adc::new(p.ADC1); | ||
| 37 | |||
| 38 | let mut dma = p.DMA1_CH1; | ||
| 39 | let mut vrefint_channel = adc.enable_vrefint().degrade_adc(); | ||
| 40 | let mut pa0 = p.PA0.degrade_adc(); | ||
| 41 | |||
| 42 | loop { | ||
| 43 | adc.read( | ||
| 44 | dma.reborrow(), | ||
| 45 | [ | ||
| 46 | (&mut vrefint_channel, SampleTime::CYCLES247_5), | ||
| 47 | (&mut pa0, SampleTime::CYCLES247_5), | ||
| 48 | ] | ||
| 49 | .into_iter(), | ||
| 50 | &mut read_buffer, | ||
| 51 | ) | ||
| 52 | .await; | ||
| 53 | |||
| 54 | let vrefint = read_buffer[0]; | ||
| 55 | let measured = read_buffer[1]; | ||
| 56 | info!("vrefint: {}", vrefint); | ||
| 57 | info!("measured: {}", measured); | ||
| 58 | Timer::after_millis(500).await; | ||
| 59 | } | ||
| 60 | } | ||
diff --git a/examples/stm32g4/src/bin/adc_oversampling.rs b/examples/stm32g4/src/bin/adc_oversampling.rs new file mode 100644 index 000000000..d31eb20f8 --- /dev/null +++ b/examples/stm32g4/src/bin/adc_oversampling.rs | |||
| @@ -0,0 +1,57 @@ | |||
| 1 | //! adc oversampling example | ||
| 2 | //! | ||
| 3 | //! This example uses adc oversampling to achieve 16bit data | ||
| 4 | |||
| 5 | #![no_std] | ||
| 6 | #![no_main] | ||
| 7 | |||
| 8 | use defmt::*; | ||
| 9 | use embassy_executor::Spawner; | ||
| 10 | use embassy_stm32::adc::vals::{Rovsm, Trovs}; | ||
| 11 | use embassy_stm32::adc::{Adc, SampleTime}; | ||
| 12 | use embassy_stm32::Config; | ||
| 13 | use embassy_time::Timer; | ||
| 14 | use {defmt_rtt as _, panic_probe as _}; | ||
| 15 | |||
| 16 | #[embassy_executor::main] | ||
| 17 | async fn main(_spawner: Spawner) { | ||
| 18 | let mut config = Config::default(); | ||
| 19 | { | ||
| 20 | use embassy_stm32::rcc::*; | ||
| 21 | config.rcc.pll = Some(Pll { | ||
| 22 | source: PllSource::HSI, | ||
| 23 | prediv: PllPreDiv::DIV4, | ||
| 24 | mul: PllMul::MUL85, | ||
| 25 | divp: None, | ||
| 26 | divq: None, | ||
| 27 | // Main system clock at 170 MHz | ||
| 28 | divr: Some(PllRDiv::DIV2), | ||
| 29 | }); | ||
| 30 | config.rcc.mux.adc12sel = mux::Adcsel::SYS; | ||
| 31 | config.rcc.sys = Sysclk::PLL1_R; | ||
| 32 | } | ||
| 33 | let mut p = embassy_stm32::init(config); | ||
| 34 | |||
| 35 | let mut adc = Adc::new(p.ADC1); | ||
| 36 | adc.set_sample_time(SampleTime::CYCLES6_5); | ||
| 37 | // From https://www.st.com/resource/en/reference_manual/rm0440-stm32g4-series-advanced-armbased-32bit-mcus-stmicroelectronics.pdf | ||
| 38 | // page652 Oversampler | ||
| 39 | // Table 172. Maximum output results vs N and M. Grayed values indicates truncation | ||
| 40 | // 0x00 oversampling ratio X2 | ||
| 41 | // 0x01 oversampling ratio X4 | ||
| 42 | // 0x02 oversampling ratio X8 | ||
| 43 | // 0x03 oversampling ratio X16 | ||
| 44 | // 0x04 oversampling ratio X32 | ||
| 45 | // 0x05 oversampling ratio X64 | ||
| 46 | // 0x06 oversampling ratio X128 | ||
| 47 | // 0x07 oversampling ratio X256 | ||
| 48 | adc.set_oversampling_ratio(0x03); // ratio X3 | ||
| 49 | adc.set_oversampling_shift(0b0000); // no shift | ||
| 50 | adc.enable_regular_oversampling_mode(Rovsm::RESUMED, Trovs::AUTOMATIC, true); | ||
| 51 | |||
| 52 | loop { | ||
| 53 | let measured = adc.blocking_read(&mut p.PA0); | ||
| 54 | info!("data: 0x{:X}", measured); //max 0xFFF0 -> 65520 | ||
| 55 | Timer::after_millis(500).await; | ||
| 56 | } | ||
| 57 | } | ||
diff --git a/examples/stm32g4/src/bin/pwm.rs b/examples/stm32g4/src/bin/pwm.rs index d4809a481..6c965012c 100644 --- a/examples/stm32g4/src/bin/pwm.rs +++ b/examples/stm32g4/src/bin/pwm.rs | |||
| @@ -6,7 +6,6 @@ use embassy_executor::Spawner; | |||
| 6 | use embassy_stm32::gpio::OutputType; | 6 | use embassy_stm32::gpio::OutputType; |
| 7 | use embassy_stm32::time::khz; | 7 | use embassy_stm32::time::khz; |
| 8 | use embassy_stm32::timer::simple_pwm::{PwmPin, SimplePwm}; | 8 | use embassy_stm32::timer::simple_pwm::{PwmPin, SimplePwm}; |
| 9 | use embassy_stm32::timer::Channel; | ||
| 10 | use embassy_time::Timer; | 9 | use embassy_time::Timer; |
| 11 | use {defmt_rtt as _, panic_probe as _}; | 10 | use {defmt_rtt as _, panic_probe as _}; |
| 12 | 11 | ||
| @@ -15,22 +14,22 @@ async fn main(_spawner: Spawner) { | |||
| 15 | let p = embassy_stm32::init(Default::default()); | 14 | let p = embassy_stm32::init(Default::default()); |
| 16 | info!("Hello World!"); | 15 | info!("Hello World!"); |
| 17 | 16 | ||
| 18 | let ch1 = PwmPin::new_ch1(p.PC0, OutputType::PushPull); | 17 | let ch1_pin = PwmPin::new_ch1(p.PC0, OutputType::PushPull); |
| 19 | let mut pwm = SimplePwm::new(p.TIM1, Some(ch1), None, None, None, khz(10), Default::default()); | 18 | let mut pwm = SimplePwm::new(p.TIM1, Some(ch1_pin), None, None, None, khz(10), Default::default()); |
| 20 | let max = pwm.get_max_duty(); | 19 | let mut ch1 = pwm.ch1(); |
| 21 | pwm.enable(Channel::Ch1); | 20 | ch1.enable(); |
| 22 | 21 | ||
| 23 | info!("PWM initialized"); | 22 | info!("PWM initialized"); |
| 24 | info!("PWM max duty {}", max); | 23 | info!("PWM max duty {}", ch1.max_duty_cycle()); |
| 25 | 24 | ||
| 26 | loop { | 25 | loop { |
| 27 | pwm.set_duty(Channel::Ch1, 0); | 26 | ch1.set_duty_cycle_fully_off(); |
| 28 | Timer::after_millis(300).await; | 27 | Timer::after_millis(300).await; |
| 29 | pwm.set_duty(Channel::Ch1, max / 4); | 28 | ch1.set_duty_cycle_fraction(1, 4); |
| 30 | Timer::after_millis(300).await; | 29 | Timer::after_millis(300).await; |
| 31 | pwm.set_duty(Channel::Ch1, max / 2); | 30 | ch1.set_duty_cycle_fraction(1, 2); |
| 32 | Timer::after_millis(300).await; | 31 | Timer::after_millis(300).await; |
| 33 | pwm.set_duty(Channel::Ch1, max - 1); | 32 | ch1.set_duty_cycle(ch1.max_duty_cycle() - 1); |
| 34 | Timer::after_millis(300).await; | 33 | Timer::after_millis(300).await; |
| 35 | } | 34 | } |
| 36 | } | 35 | } |
diff --git a/examples/stm32g4/src/bin/usb_c_pd.rs b/examples/stm32g4/src/bin/usb_c_pd.rs index 7caea634f..2e87d3931 100644 --- a/examples/stm32g4/src/bin/usb_c_pd.rs +++ b/examples/stm32g4/src/bin/usb_c_pd.rs | |||
| @@ -55,7 +55,7 @@ async fn main(_spawner: Spawner) { | |||
| 55 | 55 | ||
| 56 | info!("Hello World!"); | 56 | info!("Hello World!"); |
| 57 | 57 | ||
| 58 | let mut ucpd = Ucpd::new(p.UCPD1, Irqs {}, p.PB6, p.PB4); | 58 | let mut ucpd = Ucpd::new(p.UCPD1, Irqs {}, p.PB6, p.PB4, Default::default()); |
| 59 | ucpd.cc_phy().set_pull(CcPull::Sink); | 59 | ucpd.cc_phy().set_pull(CcPull::Sink); |
| 60 | 60 | ||
| 61 | info!("Waiting for USB connection..."); | 61 | info!("Waiting for USB connection..."); |
diff --git a/examples/stm32g4/src/bin/usb_serial.rs b/examples/stm32g4/src/bin/usb_serial.rs index ed2ac7fac..9f66f0c53 100644 --- a/examples/stm32g4/src/bin/usb_serial.rs +++ b/examples/stm32g4/src/bin/usb_serial.rs | |||
| @@ -51,11 +51,6 @@ async fn main(_spawner: Spawner) { | |||
| 51 | config.product = Some("USB-Serial Example"); | 51 | config.product = Some("USB-Serial Example"); |
| 52 | config.serial_number = Some("123456"); | 52 | config.serial_number = Some("123456"); |
| 53 | 53 | ||
| 54 | config.device_class = 0xEF; | ||
| 55 | config.device_sub_class = 0x02; | ||
| 56 | config.device_protocol = 0x01; | ||
| 57 | config.composite_with_iads = true; | ||
| 58 | |||
| 59 | let mut config_descriptor = [0; 256]; | 54 | let mut config_descriptor = [0; 256]; |
| 60 | let mut bos_descriptor = [0; 256]; | 55 | let mut bos_descriptor = [0; 256]; |
| 61 | let mut control_buf = [0; 64]; | 56 | let mut control_buf = [0; 64]; |
