diff options
| author | chemicstry <[email protected]> | 2023-01-11 17:58:15 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2023-01-11 17:58:15 +0100 |
| commit | 1af102a1aaa11d03bfa37831a3284546b605efd8 (patch) | |
| tree | d416bfda40860e61898e5599809c503c7149285a /examples | |
| parent | 041531c82911053671e71b7554d1020021f45921 (diff) | |
stm32 otg: add examples.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32f4/Cargo.toml | 5 | ||||
| -rw-r--r-- | examples/stm32f4/src/bin/usb_serial.rs | 106 | ||||
| -rw-r--r-- | examples/stm32f7/Cargo.toml | 1 | ||||
| -rw-r--r-- | examples/stm32f7/src/bin/usb_serial.rs | 107 | ||||
| -rw-r--r-- | examples/stm32h7/Cargo.toml | 1 | ||||
| -rw-r--r-- | examples/stm32h7/src/bin/usb_serial.rs | 106 | ||||
| -rw-r--r-- | examples/stm32l4/Cargo.toml | 4 | ||||
| -rw-r--r-- | examples/stm32l4/src/bin/usb_serial.rs | 108 | ||||
| -rw-r--r-- | examples/stm32u5/Cargo.toml | 1 | ||||
| -rw-r--r-- | examples/stm32u5/src/bin/usb_serial.rs | 108 |
10 files changed, 542 insertions, 5 deletions
diff --git a/examples/stm32f4/Cargo.toml b/examples/stm32f4/Cargo.toml index 62d3f08df..252d60855 100644 --- a/examples/stm32f4/Cargo.toml +++ b/examples/stm32f4/Cargo.toml | |||
| @@ -10,6 +10,7 @@ embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["de | |||
| 10 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] } | 10 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] } |
| 11 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits", "tick-hz-32_768"] } | 11 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits", "tick-hz-32_768"] } |
| 12 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "unstable-traits", "defmt", "stm32f429zi", "unstable-pac", "memory-x", "time-driver-any", "exti"] } | 12 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "unstable-traits", "defmt", "stm32f429zi", "unstable-pac", "memory-x", "time-driver-any", "exti"] } |
| 13 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } | ||
| 13 | 14 | ||
| 14 | defmt = "0.3" | 15 | defmt = "0.3" |
| 15 | defmt-rtt = "0.4" | 16 | defmt-rtt = "0.4" |
| @@ -26,5 +27,5 @@ embedded-storage = "0.3.0" | |||
| 26 | micromath = "2.0.0" | 27 | micromath = "2.0.0" |
| 27 | static_cell = "1.0" | 28 | static_cell = "1.0" |
| 28 | 29 | ||
| 29 | usb-device = "0.2" | 30 | [profile.release] |
| 30 | usbd-serial = "0.1.1" | 31 | debug = 2 |
diff --git a/examples/stm32f4/src/bin/usb_serial.rs b/examples/stm32f4/src/bin/usb_serial.rs new file mode 100644 index 000000000..014647762 --- /dev/null +++ b/examples/stm32f4/src/bin/usb_serial.rs | |||
| @@ -0,0 +1,106 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::{panic, *}; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_stm32::time::mhz; | ||
| 8 | use embassy_stm32::usb_otg::{Driver, Instance}; | ||
| 9 | use embassy_stm32::{interrupt, Config}; | ||
| 10 | use embassy_usb::class::cdc_acm::{CdcAcmClass, State}; | ||
| 11 | use embassy_usb::driver::EndpointError; | ||
| 12 | use embassy_usb::Builder; | ||
| 13 | use futures::future::join; | ||
| 14 | use {defmt_rtt as _, panic_probe as _}; | ||
| 15 | |||
| 16 | #[embassy_executor::main] | ||
| 17 | async fn main(_spawner: Spawner) { | ||
| 18 | info!("Hello World!"); | ||
| 19 | |||
| 20 | let mut config = Config::default(); | ||
| 21 | config.rcc.pll48 = true; | ||
| 22 | config.rcc.sys_ck = Some(mhz(48)); | ||
| 23 | |||
| 24 | let p = embassy_stm32::init(config); | ||
| 25 | |||
| 26 | // Create the driver, from the HAL. | ||
| 27 | let irq = interrupt::take!(OTG_FS); | ||
| 28 | let mut ep_out_buffer = [0u8; 256]; | ||
| 29 | let driver = Driver::new_fs(p.USB_OTG_FS, irq, p.PA12, p.PA11, &mut ep_out_buffer); | ||
| 30 | |||
| 31 | // Create embassy-usb Config | ||
| 32 | let mut config = embassy_usb::Config::new(0xc0de, 0xcafe); | ||
| 33 | config.manufacturer = Some("Embassy"); | ||
| 34 | config.product = Some("USB-serial example"); | ||
| 35 | config.serial_number = Some("12345678"); | ||
| 36 | |||
| 37 | // Required for windows compatiblity. | ||
| 38 | // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help | ||
| 39 | config.device_class = 0xEF; | ||
| 40 | config.device_sub_class = 0x02; | ||
| 41 | config.device_protocol = 0x01; | ||
| 42 | config.composite_with_iads = true; | ||
| 43 | |||
| 44 | // Create embassy-usb DeviceBuilder using the driver and config. | ||
| 45 | // It needs some buffers for building the descriptors. | ||
| 46 | let mut device_descriptor = [0; 256]; | ||
| 47 | let mut config_descriptor = [0; 256]; | ||
| 48 | let mut bos_descriptor = [0; 256]; | ||
| 49 | let mut control_buf = [0; 64]; | ||
| 50 | |||
| 51 | let mut state = State::new(); | ||
| 52 | |||
| 53 | let mut builder = Builder::new( | ||
| 54 | driver, | ||
| 55 | config, | ||
| 56 | &mut device_descriptor, | ||
| 57 | &mut config_descriptor, | ||
| 58 | &mut bos_descriptor, | ||
| 59 | &mut control_buf, | ||
| 60 | None, | ||
| 61 | ); | ||
| 62 | |||
| 63 | // Create classes on the builder. | ||
| 64 | let mut class = CdcAcmClass::new(&mut builder, &mut state, 64); | ||
| 65 | |||
| 66 | // Build the builder. | ||
| 67 | let mut usb = builder.build(); | ||
| 68 | |||
| 69 | // Run the USB device. | ||
| 70 | let usb_fut = usb.run(); | ||
| 71 | |||
| 72 | // Do stuff with the class! | ||
| 73 | let echo_fut = async { | ||
| 74 | loop { | ||
| 75 | class.wait_connection().await; | ||
| 76 | info!("Connected"); | ||
| 77 | let _ = echo(&mut class).await; | ||
| 78 | info!("Disconnected"); | ||
| 79 | } | ||
| 80 | }; | ||
| 81 | |||
| 82 | // Run everything concurrently. | ||
| 83 | // If we had made everything `'static` above instead, we could do this using separate tasks instead. | ||
| 84 | join(usb_fut, echo_fut).await; | ||
| 85 | } | ||
| 86 | |||
| 87 | struct Disconnected {} | ||
| 88 | |||
| 89 | impl From<EndpointError> for Disconnected { | ||
| 90 | fn from(val: EndpointError) -> Self { | ||
| 91 | match val { | ||
| 92 | EndpointError::BufferOverflow => panic!("Buffer overflow"), | ||
| 93 | EndpointError::Disabled => Disconnected {}, | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | async fn echo<'d, T: Instance + 'd>(class: &mut CdcAcmClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> { | ||
| 99 | let mut buf = [0; 64]; | ||
| 100 | loop { | ||
| 101 | let n = class.read_packet(&mut buf).await?; | ||
| 102 | let data = &buf[..n]; | ||
| 103 | info!("data: {:x}", data); | ||
| 104 | class.write_packet(data).await?; | ||
| 105 | } | ||
| 106 | } | ||
diff --git a/examples/stm32f7/Cargo.toml b/examples/stm32f7/Cargo.toml index b80dbbf9c..ea4cbd808 100644 --- a/examples/stm32f7/Cargo.toml +++ b/examples/stm32f7/Cargo.toml | |||
| @@ -11,6 +11,7 @@ embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["de | |||
| 11 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32f767zi", "unstable-pac", "time-driver-any", "exti"] } | 11 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32f767zi", "unstable-pac", "time-driver-any", "exti"] } |
| 12 | embassy-net = { path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet"] } | 12 | embassy-net = { path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet"] } |
| 13 | embedded-io = { version = "0.4.0", features = ["async"] } | 13 | embedded-io = { version = "0.4.0", features = ["async"] } |
| 14 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } | ||
| 14 | 15 | ||
| 15 | defmt = "0.3" | 16 | defmt = "0.3" |
| 16 | defmt-rtt = "0.4" | 17 | defmt-rtt = "0.4" |
diff --git a/examples/stm32f7/src/bin/usb_serial.rs b/examples/stm32f7/src/bin/usb_serial.rs new file mode 100644 index 000000000..688bd0dab --- /dev/null +++ b/examples/stm32f7/src/bin/usb_serial.rs | |||
| @@ -0,0 +1,107 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::{panic, *}; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_stm32::time::mhz; | ||
| 8 | use embassy_stm32::usb_otg::{Driver, Instance}; | ||
| 9 | use embassy_stm32::{interrupt, Config}; | ||
| 10 | use embassy_usb::class::cdc_acm::{CdcAcmClass, State}; | ||
| 11 | use embassy_usb::driver::EndpointError; | ||
| 12 | use embassy_usb::Builder; | ||
| 13 | use futures::future::join; | ||
| 14 | use {defmt_rtt as _, panic_probe as _}; | ||
| 15 | |||
| 16 | #[embassy_executor::main] | ||
| 17 | async fn main(_spawner: Spawner) { | ||
| 18 | info!("Hello World!"); | ||
| 19 | |||
| 20 | let mut config = Config::default(); | ||
| 21 | config.rcc.hse = Some(mhz(8)); | ||
| 22 | config.rcc.pll48 = true; | ||
| 23 | config.rcc.sys_ck = Some(mhz(200)); | ||
| 24 | |||
| 25 | let p = embassy_stm32::init(config); | ||
| 26 | |||
| 27 | // Create the driver, from the HAL. | ||
| 28 | let irq = interrupt::take!(OTG_FS); | ||
| 29 | let mut ep_out_buffer = [0u8; 256]; | ||
| 30 | let driver = Driver::new_fs(p.USB_OTG_FS, irq, p.PA12, p.PA11, &mut ep_out_buffer); | ||
| 31 | |||
| 32 | // Create embassy-usb Config | ||
| 33 | let mut config = embassy_usb::Config::new(0xc0de, 0xcafe); | ||
| 34 | config.manufacturer = Some("Embassy"); | ||
| 35 | config.product = Some("USB-serial example"); | ||
| 36 | config.serial_number = Some("12345678"); | ||
| 37 | |||
| 38 | // Required for windows compatiblity. | ||
| 39 | // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help | ||
| 40 | config.device_class = 0xEF; | ||
| 41 | config.device_sub_class = 0x02; | ||
| 42 | config.device_protocol = 0x01; | ||
| 43 | config.composite_with_iads = true; | ||
| 44 | |||
| 45 | // Create embassy-usb DeviceBuilder using the driver and config. | ||
| 46 | // It needs some buffers for building the descriptors. | ||
| 47 | let mut device_descriptor = [0; 256]; | ||
| 48 | let mut config_descriptor = [0; 256]; | ||
| 49 | let mut bos_descriptor = [0; 256]; | ||
| 50 | let mut control_buf = [0; 64]; | ||
| 51 | |||
| 52 | let mut state = State::new(); | ||
| 53 | |||
| 54 | let mut builder = Builder::new( | ||
| 55 | driver, | ||
| 56 | config, | ||
| 57 | &mut device_descriptor, | ||
| 58 | &mut config_descriptor, | ||
| 59 | &mut bos_descriptor, | ||
| 60 | &mut control_buf, | ||
| 61 | None, | ||
| 62 | ); | ||
| 63 | |||
| 64 | // Create classes on the builder. | ||
| 65 | let mut class = CdcAcmClass::new(&mut builder, &mut state, 64); | ||
| 66 | |||
| 67 | // Build the builder. | ||
| 68 | let mut usb = builder.build(); | ||
| 69 | |||
| 70 | // Run the USB device. | ||
| 71 | let usb_fut = usb.run(); | ||
| 72 | |||
| 73 | // Do stuff with the class! | ||
| 74 | let echo_fut = async { | ||
| 75 | loop { | ||
| 76 | class.wait_connection().await; | ||
| 77 | info!("Connected"); | ||
| 78 | let _ = echo(&mut class).await; | ||
| 79 | info!("Disconnected"); | ||
| 80 | } | ||
| 81 | }; | ||
| 82 | |||
| 83 | // Run everything concurrently. | ||
| 84 | // If we had made everything `'static` above instead, we could do this using separate tasks instead. | ||
| 85 | join(usb_fut, echo_fut).await; | ||
| 86 | } | ||
| 87 | |||
| 88 | struct Disconnected {} | ||
| 89 | |||
| 90 | impl From<EndpointError> for Disconnected { | ||
| 91 | fn from(val: EndpointError) -> Self { | ||
| 92 | match val { | ||
| 93 | EndpointError::BufferOverflow => panic!("Buffer overflow"), | ||
| 94 | EndpointError::Disabled => Disconnected {}, | ||
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | async fn echo<'d, T: Instance + 'd>(class: &mut CdcAcmClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> { | ||
| 100 | let mut buf = [0; 64]; | ||
| 101 | loop { | ||
| 102 | let n = class.read_packet(&mut buf).await?; | ||
| 103 | let data = &buf[..n]; | ||
| 104 | info!("data: {:x}", data); | ||
| 105 | class.write_packet(data).await?; | ||
| 106 | } | ||
| 107 | } | ||
diff --git a/examples/stm32h7/Cargo.toml b/examples/stm32h7/Cargo.toml index d30c42b1f..ff38440a7 100644 --- a/examples/stm32h7/Cargo.toml +++ b/examples/stm32h7/Cargo.toml | |||
| @@ -11,6 +11,7 @@ embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["de | |||
| 11 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32h743bi", "time-driver-any", "exti", "unstable-pac", "unstable-traits"] } | 11 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32h743bi", "time-driver-any", "exti", "unstable-pac", "unstable-traits"] } |
| 12 | embassy-net = { path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet", "unstable-traits"] } | 12 | embassy-net = { path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet", "unstable-traits"] } |
| 13 | embedded-io = { version = "0.4.0", features = ["async"] } | 13 | embedded-io = { version = "0.4.0", features = ["async"] } |
| 14 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } | ||
| 14 | 15 | ||
| 15 | defmt = "0.3" | 16 | defmt = "0.3" |
| 16 | defmt-rtt = "0.4" | 17 | defmt-rtt = "0.4" |
diff --git a/examples/stm32h7/src/bin/usb_serial.rs b/examples/stm32h7/src/bin/usb_serial.rs new file mode 100644 index 000000000..b319d12c3 --- /dev/null +++ b/examples/stm32h7/src/bin/usb_serial.rs | |||
| @@ -0,0 +1,106 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::{panic, *}; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_stm32::time::mhz; | ||
| 8 | use embassy_stm32::usb_otg::{Driver, Instance}; | ||
| 9 | use embassy_stm32::{interrupt, Config}; | ||
| 10 | use embassy_usb::class::cdc_acm::{CdcAcmClass, State}; | ||
| 11 | use embassy_usb::driver::EndpointError; | ||
| 12 | use embassy_usb::Builder; | ||
| 13 | use futures::future::join; | ||
| 14 | use {defmt_rtt as _, panic_probe as _}; | ||
| 15 | |||
| 16 | #[embassy_executor::main] | ||
| 17 | async fn main(_spawner: Spawner) { | ||
| 18 | info!("Hello World!"); | ||
| 19 | |||
| 20 | let mut config = Config::default(); | ||
| 21 | config.rcc.sys_ck = Some(mhz(400)); | ||
| 22 | config.rcc.hclk = Some(mhz(200)); | ||
| 23 | config.rcc.pll1.q_ck = Some(mhz(100)); | ||
| 24 | let p = embassy_stm32::init(config); | ||
| 25 | |||
| 26 | // Create the driver, from the HAL. | ||
| 27 | let irq = interrupt::take!(OTG_FS); | ||
| 28 | let mut ep_out_buffer = [0u8; 256]; | ||
| 29 | let driver = Driver::new_fs(p.USB_OTG_FS, irq, p.PA12, p.PA11, &mut ep_out_buffer); | ||
| 30 | |||
| 31 | // Create embassy-usb Config | ||
| 32 | let mut config = embassy_usb::Config::new(0xc0de, 0xcafe); | ||
| 33 | config.manufacturer = Some("Embassy"); | ||
| 34 | config.product = Some("USB-serial example"); | ||
| 35 | config.serial_number = Some("12345678"); | ||
| 36 | |||
| 37 | // Required for windows compatiblity. | ||
| 38 | // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help | ||
| 39 | config.device_class = 0xEF; | ||
| 40 | config.device_sub_class = 0x02; | ||
| 41 | config.device_protocol = 0x01; | ||
| 42 | config.composite_with_iads = true; | ||
| 43 | |||
| 44 | // Create embassy-usb DeviceBuilder using the driver and config. | ||
| 45 | // It needs some buffers for building the descriptors. | ||
| 46 | let mut device_descriptor = [0; 256]; | ||
| 47 | let mut config_descriptor = [0; 256]; | ||
| 48 | let mut bos_descriptor = [0; 256]; | ||
| 49 | let mut control_buf = [0; 64]; | ||
| 50 | |||
| 51 | let mut state = State::new(); | ||
| 52 | |||
| 53 | let mut builder = Builder::new( | ||
| 54 | driver, | ||
| 55 | config, | ||
| 56 | &mut device_descriptor, | ||
| 57 | &mut config_descriptor, | ||
| 58 | &mut bos_descriptor, | ||
| 59 | &mut control_buf, | ||
| 60 | None, | ||
| 61 | ); | ||
| 62 | |||
| 63 | // Create classes on the builder. | ||
| 64 | let mut class = CdcAcmClass::new(&mut builder, &mut state, 64); | ||
| 65 | |||
| 66 | // Build the builder. | ||
| 67 | let mut usb = builder.build(); | ||
| 68 | |||
| 69 | // Run the USB device. | ||
| 70 | let usb_fut = usb.run(); | ||
| 71 | |||
| 72 | // Do stuff with the class! | ||
| 73 | let echo_fut = async { | ||
| 74 | loop { | ||
| 75 | class.wait_connection().await; | ||
| 76 | info!("Connected"); | ||
| 77 | let _ = echo(&mut class).await; | ||
| 78 | info!("Disconnected"); | ||
| 79 | } | ||
| 80 | }; | ||
| 81 | |||
| 82 | // Run everything concurrently. | ||
| 83 | // If we had made everything `'static` above instead, we could do this using separate tasks instead. | ||
| 84 | join(usb_fut, echo_fut).await; | ||
| 85 | } | ||
| 86 | |||
| 87 | struct Disconnected {} | ||
| 88 | |||
| 89 | impl From<EndpointError> for Disconnected { | ||
| 90 | fn from(val: EndpointError) -> Self { | ||
| 91 | match val { | ||
| 92 | EndpointError::BufferOverflow => panic!("Buffer overflow"), | ||
| 93 | EndpointError::Disabled => Disconnected {}, | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | async fn echo<'d, T: Instance + 'd>(class: &mut CdcAcmClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> { | ||
| 99 | let mut buf = [0; 64]; | ||
| 100 | loop { | ||
| 101 | let n = class.read_packet(&mut buf).await?; | ||
| 102 | let data = &buf[..n]; | ||
| 103 | info!("data: {:x}", data); | ||
| 104 | class.write_packet(data).await?; | ||
| 105 | } | ||
| 106 | } | ||
diff --git a/examples/stm32l4/Cargo.toml b/examples/stm32l4/Cargo.toml index 45d3dd366..5627760ef 100644 --- a/examples/stm32l4/Cargo.toml +++ b/examples/stm32l4/Cargo.toml | |||
| @@ -12,6 +12,7 @@ embassy-executor = { version = "0.1.0", path = "../../embassy-executor", feature | |||
| 12 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } | 12 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } |
| 13 | embassy-embedded-hal = { version = "0.1.0", path = "../../embassy-embedded-hal" } | 13 | embassy-embedded-hal = { version = "0.1.0", path = "../../embassy-embedded-hal" } |
| 14 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "unstable-pac", "stm32l4s5vi", "time-driver-any", "exti", "unstable-traits"] } | 14 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "unstable-pac", "stm32l4s5vi", "time-driver-any", "exti", "unstable-traits"] } |
| 15 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } | ||
| 15 | 16 | ||
| 16 | defmt = "0.3" | 17 | defmt = "0.3" |
| 17 | defmt-rtt = "0.4" | 18 | defmt-rtt = "0.4" |
| @@ -26,6 +27,3 @@ futures = { version = "0.3.17", default-features = false, features = ["async-awa | |||
| 26 | heapless = { version = "0.7.5", default-features = false } | 27 | heapless = { version = "0.7.5", default-features = false } |
| 27 | 28 | ||
| 28 | micromath = "2.0.0" | 29 | micromath = "2.0.0" |
| 29 | usb-device = "0.2" | ||
| 30 | usbd-serial = "0.1.1" | ||
| 31 | |||
diff --git a/examples/stm32l4/src/bin/usb_serial.rs b/examples/stm32l4/src/bin/usb_serial.rs new file mode 100644 index 000000000..3e38b10a3 --- /dev/null +++ b/examples/stm32l4/src/bin/usb_serial.rs | |||
| @@ -0,0 +1,108 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::{panic, *}; | ||
| 6 | use defmt_rtt as _; // global logger | ||
| 7 | use embassy_executor::Spawner; | ||
| 8 | use embassy_stm32::rcc::*; | ||
| 9 | use embassy_stm32::usb_otg::{Driver, Instance}; | ||
| 10 | use embassy_stm32::{interrupt, Config}; | ||
| 11 | use embassy_usb::class::cdc_acm::{CdcAcmClass, State}; | ||
| 12 | use embassy_usb::driver::EndpointError; | ||
| 13 | use embassy_usb::Builder; | ||
| 14 | use futures::future::join; | ||
| 15 | use panic_probe as _; | ||
| 16 | |||
| 17 | #[embassy_executor::main] | ||
| 18 | async fn main(_spawner: Spawner) { | ||
| 19 | info!("Hello World!"); | ||
| 20 | |||
| 21 | let mut config = Config::default(); | ||
| 22 | config.rcc.mux = ClockSrc::PLL(PLLSource::HSI16, PLLClkDiv::Div2, PLLSrcDiv::Div1, PLLMul::Mul10, None); | ||
| 23 | config.rcc.hsi48 = true; | ||
| 24 | |||
| 25 | let p = embassy_stm32::init(config); | ||
| 26 | |||
| 27 | // Create the driver, from the HAL. | ||
| 28 | let irq = interrupt::take!(OTG_FS); | ||
| 29 | let mut ep_out_buffer = [0u8; 256]; | ||
| 30 | let driver = Driver::new_fs(p.USB_OTG_FS, irq, p.PA12, p.PA11, &mut ep_out_buffer); | ||
| 31 | |||
| 32 | // Create embassy-usb Config | ||
| 33 | let mut config = embassy_usb::Config::new(0xc0de, 0xcafe); | ||
| 34 | config.max_packet_size_0 = 64; | ||
| 35 | config.manufacturer = Some("Embassy"); | ||
| 36 | config.product = Some("USB-serial example"); | ||
| 37 | config.serial_number = Some("12345678"); | ||
| 38 | |||
| 39 | // Required for windows compatiblity. | ||
| 40 | // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help | ||
| 41 | config.device_class = 0xEF; | ||
| 42 | config.device_sub_class = 0x02; | ||
| 43 | config.device_protocol = 0x01; | ||
| 44 | config.composite_with_iads = true; | ||
| 45 | |||
| 46 | // Create embassy-usb DeviceBuilder using the driver and config. | ||
| 47 | // It needs some buffers for building the descriptors. | ||
| 48 | let mut device_descriptor = [0; 256]; | ||
| 49 | let mut config_descriptor = [0; 256]; | ||
| 50 | let mut bos_descriptor = [0; 256]; | ||
| 51 | let mut control_buf = [0; 64]; | ||
| 52 | |||
| 53 | let mut state = State::new(); | ||
| 54 | |||
| 55 | let mut builder = Builder::new( | ||
| 56 | driver, | ||
| 57 | config, | ||
| 58 | &mut device_descriptor, | ||
| 59 | &mut config_descriptor, | ||
| 60 | &mut bos_descriptor, | ||
| 61 | &mut control_buf, | ||
| 62 | None, | ||
| 63 | ); | ||
| 64 | |||
| 65 | // Create classes on the builder. | ||
| 66 | let mut class = CdcAcmClass::new(&mut builder, &mut state, 64); | ||
| 67 | |||
| 68 | // Build the builder. | ||
| 69 | let mut usb = builder.build(); | ||
| 70 | |||
| 71 | // Run the USB device. | ||
| 72 | let usb_fut = usb.run(); | ||
| 73 | |||
| 74 | // Do stuff with the class! | ||
| 75 | let echo_fut = async { | ||
| 76 | loop { | ||
| 77 | class.wait_connection().await; | ||
| 78 | info!("Connected"); | ||
| 79 | let _ = echo(&mut class).await; | ||
| 80 | info!("Disconnected"); | ||
| 81 | } | ||
| 82 | }; | ||
| 83 | |||
| 84 | // Run everything concurrently. | ||
| 85 | // If we had made everything `'static` above instead, we could do this using separate tasks instead. | ||
| 86 | join(usb_fut, echo_fut).await; | ||
| 87 | } | ||
| 88 | |||
| 89 | struct Disconnected {} | ||
| 90 | |||
| 91 | impl From<EndpointError> for Disconnected { | ||
| 92 | fn from(val: EndpointError) -> Self { | ||
| 93 | match val { | ||
| 94 | EndpointError::BufferOverflow => panic!("Buffer overflow"), | ||
| 95 | EndpointError::Disabled => Disconnected {}, | ||
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | async fn echo<'d, T: Instance + 'd>(class: &mut CdcAcmClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> { | ||
| 101 | let mut buf = [0; 64]; | ||
| 102 | loop { | ||
| 103 | let n = class.read_packet(&mut buf).await?; | ||
| 104 | let data = &buf[..n]; | ||
| 105 | info!("data: {:x}", data); | ||
| 106 | class.write_packet(data).await?; | ||
| 107 | } | ||
| 108 | } | ||
diff --git a/examples/stm32u5/Cargo.toml b/examples/stm32u5/Cargo.toml index d88fdda50..2b02eda92 100644 --- a/examples/stm32u5/Cargo.toml +++ b/examples/stm32u5/Cargo.toml | |||
| @@ -9,6 +9,7 @@ embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["de | |||
| 9 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] } | 9 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] } |
| 10 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } | 10 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } |
| 11 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "unstable-pac", "stm32u585ai", "time-driver-any", "memory-x" ] } | 11 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "unstable-pac", "stm32u585ai", "time-driver-any", "memory-x" ] } |
| 12 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } | ||
| 12 | 13 | ||
| 13 | defmt = "0.3" | 14 | defmt = "0.3" |
| 14 | defmt-rtt = "0.4" | 15 | defmt-rtt = "0.4" |
diff --git a/examples/stm32u5/src/bin/usb_serial.rs b/examples/stm32u5/src/bin/usb_serial.rs new file mode 100644 index 000000000..c846836b0 --- /dev/null +++ b/examples/stm32u5/src/bin/usb_serial.rs | |||
| @@ -0,0 +1,108 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::{panic, *}; | ||
| 6 | use defmt_rtt as _; // global logger | ||
| 7 | use embassy_executor::Spawner; | ||
| 8 | use embassy_stm32::rcc::*; | ||
| 9 | use embassy_stm32::usb_otg::{Driver, Instance}; | ||
| 10 | use embassy_stm32::{interrupt, Config}; | ||
| 11 | use embassy_usb::class::cdc_acm::{CdcAcmClass, State}; | ||
| 12 | use embassy_usb::driver::EndpointError; | ||
| 13 | use embassy_usb::Builder; | ||
| 14 | use futures::future::join; | ||
| 15 | use panic_probe as _; | ||
| 16 | |||
| 17 | #[embassy_executor::main] | ||
| 18 | async fn main(_spawner: Spawner) { | ||
| 19 | info!("Hello World!"); | ||
| 20 | |||
| 21 | let mut config = Config::default(); | ||
| 22 | config.rcc.mux = ClockSrc::PLL1R(PllSrc::HSI16, PllM::Div2, PllN::Mul10, PllClkDiv::NotDivided); | ||
| 23 | //config.rcc.mux = ClockSrc::MSI(MSIRange::Range48mhz); | ||
| 24 | config.rcc.hsi48 = true; | ||
| 25 | |||
| 26 | let p = embassy_stm32::init(config); | ||
| 27 | |||
| 28 | // Create the driver, from the HAL. | ||
| 29 | let irq = interrupt::take!(OTG_FS); | ||
| 30 | let mut ep_out_buffer = [0u8; 256]; | ||
| 31 | let driver = Driver::new_fs(p.USB_OTG_FS, irq, p.PA12, p.PA11, &mut ep_out_buffer); | ||
| 32 | |||
| 33 | // Create embassy-usb Config | ||
| 34 | let mut config = embassy_usb::Config::new(0xc0de, 0xcafe); | ||
| 35 | config.manufacturer = Some("Embassy"); | ||
| 36 | config.product = Some("USB-serial example"); | ||
| 37 | config.serial_number = Some("12345678"); | ||
| 38 | |||
| 39 | // Required for windows compatiblity. | ||
| 40 | // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help | ||
| 41 | config.device_class = 0xEF; | ||
| 42 | config.device_sub_class = 0x02; | ||
| 43 | config.device_protocol = 0x01; | ||
| 44 | config.composite_with_iads = true; | ||
| 45 | |||
| 46 | // Create embassy-usb DeviceBuilder using the driver and config. | ||
| 47 | // It needs some buffers for building the descriptors. | ||
| 48 | let mut device_descriptor = [0; 256]; | ||
| 49 | let mut config_descriptor = [0; 256]; | ||
| 50 | let mut bos_descriptor = [0; 256]; | ||
| 51 | let mut control_buf = [0; 64]; | ||
| 52 | |||
| 53 | let mut state = State::new(); | ||
| 54 | |||
| 55 | let mut builder = Builder::new( | ||
| 56 | driver, | ||
| 57 | config, | ||
| 58 | &mut device_descriptor, | ||
| 59 | &mut config_descriptor, | ||
| 60 | &mut bos_descriptor, | ||
| 61 | &mut control_buf, | ||
| 62 | None, | ||
| 63 | ); | ||
| 64 | |||
| 65 | // Create classes on the builder. | ||
| 66 | let mut class = CdcAcmClass::new(&mut builder, &mut state, 64); | ||
| 67 | |||
| 68 | // Build the builder. | ||
| 69 | let mut usb = builder.build(); | ||
| 70 | |||
| 71 | // Run the USB device. | ||
| 72 | let usb_fut = usb.run(); | ||
| 73 | |||
| 74 | // Do stuff with the class! | ||
| 75 | let echo_fut = async { | ||
| 76 | loop { | ||
| 77 | class.wait_connection().await; | ||
| 78 | info!("Connected"); | ||
| 79 | let _ = echo(&mut class).await; | ||
| 80 | info!("Disconnected"); | ||
| 81 | } | ||
| 82 | }; | ||
| 83 | |||
| 84 | // Run everything concurrently. | ||
| 85 | // If we had made everything `'static` above instead, we could do this using separate tasks instead. | ||
| 86 | join(usb_fut, echo_fut).await; | ||
| 87 | } | ||
| 88 | |||
| 89 | struct Disconnected {} | ||
| 90 | |||
| 91 | impl From<EndpointError> for Disconnected { | ||
| 92 | fn from(val: EndpointError) -> Self { | ||
| 93 | match val { | ||
| 94 | EndpointError::BufferOverflow => panic!("Buffer overflow"), | ||
| 95 | EndpointError::Disabled => Disconnected {}, | ||
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | async fn echo<'d, T: Instance + 'd>(class: &mut CdcAcmClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> { | ||
| 101 | let mut buf = [0; 64]; | ||
| 102 | loop { | ||
| 103 | let n = class.read_packet(&mut buf).await?; | ||
| 104 | let data = &buf[..n]; | ||
| 105 | info!("data: {:x}", data); | ||
| 106 | class.write_packet(data).await?; | ||
| 107 | } | ||
| 108 | } | ||
