diff options
| -rw-r--r-- | embassy-stm32/src/dac/tsel.rs | 17 | ||||
| -rw-r--r-- | embassy-stm32/src/usb/usb.rs | 14 | ||||
| -rw-r--r-- | examples/stm32u0/Cargo.toml | 3 | ||||
| -rw-r--r-- | examples/stm32u0/src/bin/adc.rs | 33 | ||||
| -rw-r--r-- | examples/stm32u0/src/bin/dac.rs | 35 | ||||
| -rw-r--r-- | examples/stm32u0/src/bin/rng.rs | 41 | ||||
| -rw-r--r-- | examples/stm32u0/src/bin/usart.rs | 31 | ||||
| -rw-r--r-- | examples/stm32u0/src/bin/usb_serial.rs | 109 |
8 files changed, 276 insertions, 7 deletions
diff --git a/embassy-stm32/src/dac/tsel.rs b/embassy-stm32/src/dac/tsel.rs index 22d8d3dfa..1877954b9 100644 --- a/embassy-stm32/src/dac/tsel.rs +++ b/embassy-stm32/src/dac/tsel.rs | |||
| @@ -235,6 +235,23 @@ pub enum TriggerSel { | |||
| 235 | Exti9 = 13, | 235 | Exti9 = 13, |
| 236 | } | 236 | } |
| 237 | 237 | ||
| 238 | /// Trigger selection for U0. | ||
| 239 | #[cfg(stm32u0)] | ||
| 240 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] | ||
| 241 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 242 | pub enum TriggerSel { | ||
| 243 | Software = 0, | ||
| 244 | Tim1 = 1, | ||
| 245 | Tim2 = 2, | ||
| 246 | Tim3 = 3, | ||
| 247 | Tim6 = 5, | ||
| 248 | Tim7 = 6, | ||
| 249 | Tim15 = 8, | ||
| 250 | Lptim1 = 11, | ||
| 251 | Lptim2 = 12, | ||
| 252 | Exti9 = 14, | ||
| 253 | } | ||
| 254 | |||
| 238 | /// Trigger selection for G4. | 255 | /// Trigger selection for G4. |
| 239 | #[cfg(stm32g4)] | 256 | #[cfg(stm32g4)] |
| 240 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] | 257 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
diff --git a/embassy-stm32/src/usb/usb.rs b/embassy-stm32/src/usb/usb.rs index f48808cb3..81a2d2623 100644 --- a/embassy-stm32/src/usb/usb.rs +++ b/embassy-stm32/src/usb/usb.rs | |||
| @@ -107,14 +107,14 @@ const EP_COUNT: usize = 8; | |||
| 107 | 107 | ||
| 108 | #[cfg(any(usbram_16x1_512, usbram_16x2_512))] | 108 | #[cfg(any(usbram_16x1_512, usbram_16x2_512))] |
| 109 | const USBRAM_SIZE: usize = 512; | 109 | const USBRAM_SIZE: usize = 512; |
| 110 | #[cfg(usbram_16x2_1024)] | 110 | #[cfg(any(usbram_16x2_1024, usbram_32_1024))] |
| 111 | const USBRAM_SIZE: usize = 1024; | 111 | const USBRAM_SIZE: usize = 1024; |
| 112 | #[cfg(usbram_32_2048)] | 112 | #[cfg(usbram_32_2048)] |
| 113 | const USBRAM_SIZE: usize = 2048; | 113 | const USBRAM_SIZE: usize = 2048; |
| 114 | 114 | ||
| 115 | #[cfg(not(usbram_32_2048))] | 115 | #[cfg(not(any(usbram_32_2048, usbram_32_1024)))] |
| 116 | const USBRAM_ALIGN: usize = 2; | 116 | const USBRAM_ALIGN: usize = 2; |
| 117 | #[cfg(usbram_32_2048)] | 117 | #[cfg(any(usbram_32_2048, usbram_32_1024))] |
| 118 | const USBRAM_ALIGN: usize = 4; | 118 | const USBRAM_ALIGN: usize = 4; |
| 119 | 119 | ||
| 120 | const NEW_AW: AtomicWaker = AtomicWaker::new(); | 120 | const NEW_AW: AtomicWaker = AtomicWaker::new(); |
| @@ -159,7 +159,7 @@ fn calc_out_len(len: u16) -> (u16, u16) { | |||
| 159 | } | 159 | } |
| 160 | } | 160 | } |
| 161 | 161 | ||
| 162 | #[cfg(not(usbram_32_2048))] | 162 | #[cfg(not(any(usbram_32_2048, usbram_32_1024)))] |
| 163 | mod btable { | 163 | mod btable { |
| 164 | use super::*; | 164 | use super::*; |
| 165 | 165 | ||
| @@ -180,7 +180,7 @@ mod btable { | |||
| 180 | USBRAM.mem(index * 4 + 3).read() | 180 | USBRAM.mem(index * 4 + 3).read() |
| 181 | } | 181 | } |
| 182 | } | 182 | } |
| 183 | #[cfg(usbram_32_2048)] | 183 | #[cfg(any(usbram_32_2048, usbram_32_1024))] |
| 184 | mod btable { | 184 | mod btable { |
| 185 | use super::*; | 185 | use super::*; |
| 186 | 186 | ||
| @@ -224,9 +224,9 @@ impl<T: Instance> EndpointBuffer<T> { | |||
| 224 | let n = USBRAM_ALIGN.min(buf.len() - i * USBRAM_ALIGN); | 224 | let n = USBRAM_ALIGN.min(buf.len() - i * USBRAM_ALIGN); |
| 225 | val[..n].copy_from_slice(&buf[i * USBRAM_ALIGN..][..n]); | 225 | val[..n].copy_from_slice(&buf[i * USBRAM_ALIGN..][..n]); |
| 226 | 226 | ||
| 227 | #[cfg(not(usbram_32_2048))] | 227 | #[cfg(not(any(usbram_32_2048, usbram_32_1024)))] |
| 228 | let val = u16::from_le_bytes(val); | 228 | let val = u16::from_le_bytes(val); |
| 229 | #[cfg(usbram_32_2048)] | 229 | #[cfg(any(usbram_32_2048, usbram_32_1024))] |
| 230 | let val = u32::from_le_bytes(val); | 230 | let val = u32::from_le_bytes(val); |
| 231 | USBRAM.mem(self.addr as usize / USBRAM_ALIGN + i).write_value(val); | 231 | USBRAM.mem(self.addr as usize / USBRAM_ALIGN + i).write_value(val); |
| 232 | } | 232 | } |
diff --git a/examples/stm32u0/Cargo.toml b/examples/stm32u0/Cargo.toml index af3d7ff61..8219cb038 100644 --- a/examples/stm32u0/Cargo.toml +++ b/examples/stm32u0/Cargo.toml | |||
| @@ -10,6 +10,7 @@ embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = [ | |||
| 10 | embassy-sync = { version = "0.5.0", path = "../../embassy-sync", features = ["defmt"] } | 10 | embassy-sync = { version = "0.5.0", path = "../../embassy-sync", features = ["defmt"] } |
| 11 | embassy-executor = { version = "0.5.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } | 11 | embassy-executor = { version = "0.5.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } |
| 12 | embassy-time = { version = "0.3.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } | 12 | embassy-time = { version = "0.3.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } |
| 13 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", default-features = false, features = ["defmt"] } | ||
| 13 | 14 | ||
| 14 | defmt = "0.3" | 15 | defmt = "0.3" |
| 15 | defmt-rtt = "0.4" | 16 | defmt-rtt = "0.4" |
| @@ -21,5 +22,7 @@ panic-probe = { version = "0.3", features = ["print-defmt"] } | |||
| 21 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } | 22 | futures = { version = "0.3.17", default-features = false, features = ["async-await"] } |
| 22 | heapless = { version = "0.8", default-features = false } | 23 | heapless = { version = "0.8", default-features = false } |
| 23 | 24 | ||
| 25 | micromath = "2.0.0" | ||
| 26 | |||
| 24 | [profile.release] | 27 | [profile.release] |
| 25 | debug = 2 | 28 | debug = 2 |
diff --git a/examples/stm32u0/src/bin/adc.rs b/examples/stm32u0/src/bin/adc.rs new file mode 100644 index 000000000..f7145b85d --- /dev/null +++ b/examples/stm32u0/src/bin/adc.rs | |||
| @@ -0,0 +1,33 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_stm32::adc::{Adc, Resolution}; | ||
| 6 | use embassy_stm32::Config; | ||
| 7 | use embassy_time::Duration; | ||
| 8 | use {defmt_rtt as _, panic_probe as _}; | ||
| 9 | |||
| 10 | #[cortex_m_rt::entry] | ||
| 11 | fn main() -> ! { | ||
| 12 | info!("Hello World!"); | ||
| 13 | |||
| 14 | let mut config = Config::default(); | ||
| 15 | { | ||
| 16 | use embassy_stm32::rcc::*; | ||
| 17 | config.rcc.mux.adcsel = mux::Adcsel::SYS; | ||
| 18 | } | ||
| 19 | let p = embassy_stm32::init(config); | ||
| 20 | |||
| 21 | let mut adc = Adc::new(p.ADC1); | ||
| 22 | let mut temp = adc.enable_temperature(); | ||
| 23 | adc.set_resolution(Resolution::BITS8); | ||
| 24 | let mut channel = p.PC0; | ||
| 25 | |||
| 26 | loop { | ||
| 27 | let v = adc.read(&mut channel); | ||
| 28 | info!("--> {}", v); | ||
| 29 | let v = adc.read(&mut temp); | ||
| 30 | info!("Temp: --> {}", v); | ||
| 31 | embassy_time::block_for(Duration::from_millis(1000)); | ||
| 32 | } | ||
| 33 | } | ||
diff --git a/examples/stm32u0/src/bin/dac.rs b/examples/stm32u0/src/bin/dac.rs new file mode 100644 index 000000000..fdbf1d374 --- /dev/null +++ b/examples/stm32u0/src/bin/dac.rs | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_stm32::dac::{DacCh1, Value}; | ||
| 6 | use embassy_stm32::dma::NoDma; | ||
| 7 | use {defmt_rtt as _, panic_probe as _}; | ||
| 8 | |||
| 9 | #[cortex_m_rt::entry] | ||
| 10 | fn main() -> ! { | ||
| 11 | let p = embassy_stm32::init(Default::default()); | ||
| 12 | info!("Hello World!"); | ||
| 13 | |||
| 14 | let mut dac = DacCh1::new(p.DAC1, NoDma, p.PA4); | ||
| 15 | |||
| 16 | loop { | ||
| 17 | for v in 0..=255 { | ||
| 18 | dac.set(Value::Bit8(to_sine_wave(v))); | ||
| 19 | } | ||
| 20 | } | ||
| 21 | } | ||
| 22 | |||
| 23 | use micromath::F32Ext; | ||
| 24 | |||
| 25 | fn to_sine_wave(v: u8) -> u8 { | ||
| 26 | if v >= 128 { | ||
| 27 | // top half | ||
| 28 | let r = 3.14 * ((v - 128) as f32 / 128.0); | ||
| 29 | (r.sin() * 128.0 + 127.0) as u8 | ||
| 30 | } else { | ||
| 31 | // bottom half | ||
| 32 | let r = 3.14 + 3.14 * (v as f32 / 128.0); | ||
| 33 | (r.sin() * 128.0 + 127.0) as u8 | ||
| 34 | } | ||
| 35 | } | ||
diff --git a/examples/stm32u0/src/bin/rng.rs b/examples/stm32u0/src/bin/rng.rs new file mode 100644 index 000000000..9b4c2537e --- /dev/null +++ b/examples/stm32u0/src/bin/rng.rs | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_executor::Spawner; | ||
| 6 | use embassy_stm32::rng::Rng; | ||
| 7 | use embassy_stm32::{bind_interrupts, peripherals, rng, Config}; | ||
| 8 | use {defmt_rtt as _, panic_probe as _}; | ||
| 9 | |||
| 10 | bind_interrupts!(struct Irqs { | ||
| 11 | RNG_CRYP => rng::InterruptHandler<peripherals::RNG>; | ||
| 12 | }); | ||
| 13 | |||
| 14 | #[embassy_executor::main] | ||
| 15 | async fn main(_spawner: Spawner) { | ||
| 16 | let mut config = Config::default(); | ||
| 17 | { | ||
| 18 | use embassy_stm32::rcc::*; | ||
| 19 | config.rcc.hsi = true; | ||
| 20 | config.rcc.pll = Some(Pll { | ||
| 21 | source: PllSource::HSI, // 16 MHz | ||
| 22 | prediv: PllPreDiv::DIV1, | ||
| 23 | mul: PllMul::MUL7, // 16 * 7 = 112 MHz | ||
| 24 | divp: None, | ||
| 25 | divq: None, | ||
| 26 | divr: Some(PllRDiv::DIV2), // 112 / 2 = 56 MHz | ||
| 27 | }); | ||
| 28 | config.rcc.sys = Sysclk::PLL1_R; | ||
| 29 | } | ||
| 30 | |||
| 31 | let p = embassy_stm32::init(config); | ||
| 32 | |||
| 33 | info!("Hello World!"); | ||
| 34 | |||
| 35 | let mut rng = Rng::new(p.RNG, Irqs); | ||
| 36 | info!("Hello World 2!"); | ||
| 37 | |||
| 38 | let mut buf = [0u8; 16]; | ||
| 39 | unwrap!(rng.async_fill_bytes(&mut buf).await); | ||
| 40 | info!("random bytes: {:02x}", buf); | ||
| 41 | } | ||
diff --git a/examples/stm32u0/src/bin/usart.rs b/examples/stm32u0/src/bin/usart.rs new file mode 100644 index 000000000..1fcf0029a --- /dev/null +++ b/examples/stm32u0/src/bin/usart.rs | |||
| @@ -0,0 +1,31 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::*; | ||
| 5 | use embassy_stm32::dma::NoDma; | ||
| 6 | use embassy_stm32::usart::{Config, Uart}; | ||
| 7 | use embassy_stm32::{bind_interrupts, peripherals, usart}; | ||
| 8 | use {defmt_rtt as _, panic_probe as _}; | ||
| 9 | |||
| 10 | bind_interrupts!(struct Irqs { | ||
| 11 | USART2_LPUART2 => usart::InterruptHandler<peripherals::USART2>; | ||
| 12 | }); | ||
| 13 | |||
| 14 | #[cortex_m_rt::entry] | ||
| 15 | fn main() -> ! { | ||
| 16 | info!("Hello World!"); | ||
| 17 | |||
| 18 | let p = embassy_stm32::init(Default::default()); | ||
| 19 | |||
| 20 | let config = Config::default(); | ||
| 21 | let mut usart = Uart::new(p.USART2, p.PA3, p.PA2, Irqs, NoDma, NoDma, config).unwrap(); | ||
| 22 | |||
| 23 | unwrap!(usart.blocking_write(b"Hello Embassy World!\r\n")); | ||
| 24 | info!("wrote Hello, starting echo"); | ||
| 25 | |||
| 26 | let mut buf = [0u8; 1]; | ||
| 27 | loop { | ||
| 28 | unwrap!(usart.blocking_read(&mut buf)); | ||
| 29 | unwrap!(usart.blocking_write(&buf)); | ||
| 30 | } | ||
| 31 | } | ||
diff --git a/examples/stm32u0/src/bin/usb_serial.rs b/examples/stm32u0/src/bin/usb_serial.rs new file mode 100644 index 000000000..9b38fd5dc --- /dev/null +++ b/examples/stm32u0/src/bin/usb_serial.rs | |||
| @@ -0,0 +1,109 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::{panic, *}; | ||
| 5 | use defmt_rtt as _; // global logger | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_stm32::usb::{Driver, Instance}; | ||
| 8 | use embassy_stm32::{bind_interrupts, peripherals, usb, Config}; | ||
| 9 | use embassy_usb::class::cdc_acm::{CdcAcmClass, State}; | ||
| 10 | use embassy_usb::driver::EndpointError; | ||
| 11 | use embassy_usb::Builder; | ||
| 12 | use futures::future::join; | ||
| 13 | use panic_probe as _; | ||
| 14 | |||
| 15 | bind_interrupts!(struct Irqs { | ||
| 16 | USB_DRD_FS => usb::InterruptHandler<peripherals::USB>; | ||
| 17 | }); | ||
| 18 | |||
| 19 | #[embassy_executor::main] | ||
| 20 | async fn main(_spawner: Spawner) { | ||
| 21 | let mut config = Config::default(); | ||
| 22 | { | ||
| 23 | use embassy_stm32::rcc::*; | ||
| 24 | config.rcc.hsi = true; | ||
| 25 | config.rcc.pll = Some(Pll { | ||
| 26 | source: PllSource::HSI, // 16 MHz | ||
| 27 | prediv: PllPreDiv::DIV1, | ||
| 28 | mul: PllMul::MUL7, | ||
| 29 | divp: None, | ||
| 30 | divq: None, | ||
| 31 | divr: Some(PllRDiv::DIV2), // 56 MHz | ||
| 32 | }); | ||
| 33 | config.rcc.sys = Sysclk::PLL1_R; | ||
| 34 | config.rcc.hsi48 = Some(Hsi48Config { sync_from_usb: true }); // needed for USB | ||
| 35 | config.rcc.mux.clk48sel = mux::Clk48sel::HSI48; // USB uses ICLK | ||
| 36 | } | ||
| 37 | |||
| 38 | let p = embassy_stm32::init(config); | ||
| 39 | |||
| 40 | info!("Hello World!"); | ||
| 41 | |||
| 42 | // Create the driver, from the HAL. | ||
| 43 | let driver = Driver::new(p.USB, Irqs, p.PA12, p.PA11); | ||
| 44 | |||
| 45 | // Create embassy-usb Config | ||
| 46 | let config = embassy_usb::Config::new(0xc0de, 0xcafe); | ||
| 47 | //config.max_packet_size_0 = 64; | ||
| 48 | |||
| 49 | // Create embassy-usb DeviceBuilder using the driver and config. | ||
| 50 | // It needs some buffers for building the descriptors. | ||
| 51 | let mut config_descriptor = [0; 256]; | ||
| 52 | let mut bos_descriptor = [0; 256]; | ||
| 53 | let mut control_buf = [0; 7]; | ||
| 54 | |||
| 55 | let mut state = State::new(); | ||
| 56 | |||
| 57 | let mut builder = Builder::new( | ||
| 58 | driver, | ||
| 59 | config, | ||
| 60 | &mut config_descriptor, | ||
| 61 | &mut bos_descriptor, | ||
| 62 | &mut [], // no msos descriptors | ||
| 63 | &mut control_buf, | ||
| 64 | ); | ||
| 65 | |||
| 66 | // Create classes on the builder. | ||
| 67 | let mut class = CdcAcmClass::new(&mut builder, &mut state, 64); | ||
| 68 | |||
| 69 | // Build the builder. | ||
| 70 | let mut usb = builder.build(); | ||
| 71 | |||
| 72 | // Run the USB device. | ||
| 73 | let usb_fut = usb.run(); | ||
| 74 | |||
| 75 | // Do stuff with the class! | ||
| 76 | let echo_fut = async { | ||
| 77 | loop { | ||
| 78 | class.wait_connection().await; | ||
| 79 | info!("Connected"); | ||
| 80 | let _ = echo(&mut class).await; | ||
| 81 | info!("Disconnected"); | ||
| 82 | } | ||
| 83 | }; | ||
| 84 | |||
| 85 | // Run everything concurrently. | ||
| 86 | // If we had made everything `'static` above instead, we could do this using separate tasks instead. | ||
| 87 | join(usb_fut, echo_fut).await; | ||
| 88 | } | ||
| 89 | |||
| 90 | struct Disconnected {} | ||
| 91 | |||
| 92 | impl From<EndpointError> for Disconnected { | ||
| 93 | fn from(val: EndpointError) -> Self { | ||
| 94 | match val { | ||
| 95 | EndpointError::BufferOverflow => panic!("Buffer overflow"), | ||
| 96 | EndpointError::Disabled => Disconnected {}, | ||
| 97 | } | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | async fn echo<'d, T: Instance + 'd>(class: &mut CdcAcmClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> { | ||
| 102 | let mut buf = [0; 64]; | ||
| 103 | loop { | ||
| 104 | let n = class.read_packet(&mut buf).await?; | ||
| 105 | let data = &buf[..n]; | ||
| 106 | info!("data: {:x}", data); | ||
| 107 | class.write_packet(data).await?; | ||
| 108 | } | ||
| 109 | } | ||
