diff options
| author | Mick Chanthaseth <[email protected]> | 2024-02-16 22:27:06 -0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-02-16 22:27:06 -0800 |
| commit | 7609313a75fee51613535eb8ea5dac4cdb193d3e (patch) | |
| tree | 696777d9e54a88a77cf5ac0066f90ad559f2a84e /examples/rp | |
| parent | 0097cbcfe3311ca89f52284a4a1187e19666a330 (diff) | |
| parent | 377e58e408f830f79171a470ba602b7d8bc525e4 (diff) | |
Merge branch 'embassy-rs:main' into main
Diffstat (limited to 'examples/rp')
| -rw-r--r-- | examples/rp/src/bin/i2c_slave.rs | 2 | ||||
| -rw-r--r-- | examples/rp/src/bin/pio_ws2812.rs | 7 | ||||
| -rw-r--r-- | examples/rp/src/bin/usb_serial_with_logger.rs | 117 |
3 files changed, 123 insertions, 3 deletions
diff --git a/examples/rp/src/bin/i2c_slave.rs b/examples/rp/src/bin/i2c_slave.rs index ac470d2be..9fffb4646 100644 --- a/examples/rp/src/bin/i2c_slave.rs +++ b/examples/rp/src/bin/i2c_slave.rs | |||
| @@ -110,7 +110,7 @@ async fn main(spawner: Spawner) { | |||
| 110 | let c_sda = p.PIN_1; | 110 | let c_sda = p.PIN_1; |
| 111 | let c_scl = p.PIN_0; | 111 | let c_scl = p.PIN_0; |
| 112 | let mut config = i2c::Config::default(); | 112 | let mut config = i2c::Config::default(); |
| 113 | config.frequency = 5_000; | 113 | config.frequency = 1_000_000; |
| 114 | let controller = i2c::I2c::new_async(p.I2C0, c_sda, c_scl, Irqs, config); | 114 | let controller = i2c::I2c::new_async(p.I2C0, c_sda, c_scl, Irqs, config); |
| 115 | 115 | ||
| 116 | unwrap!(spawner.spawn(controller_task(controller))); | 116 | unwrap!(spawner.spawn(controller_task(controller))); |
diff --git a/examples/rp/src/bin/pio_ws2812.rs b/examples/rp/src/bin/pio_ws2812.rs index 9a97cb8a7..ac145933c 100644 --- a/examples/rp/src/bin/pio_ws2812.rs +++ b/examples/rp/src/bin/pio_ws2812.rs | |||
| @@ -12,7 +12,7 @@ use embassy_rp::pio::{ | |||
| 12 | Common, Config, FifoJoin, Instance, InterruptHandler, Pio, PioPin, ShiftConfig, ShiftDirection, StateMachine, | 12 | Common, Config, FifoJoin, Instance, InterruptHandler, Pio, PioPin, ShiftConfig, ShiftDirection, StateMachine, |
| 13 | }; | 13 | }; |
| 14 | use embassy_rp::{bind_interrupts, clocks, into_ref, Peripheral, PeripheralRef}; | 14 | use embassy_rp::{bind_interrupts, clocks, into_ref, Peripheral, PeripheralRef}; |
| 15 | use embassy_time::Timer; | 15 | use embassy_time::{Duration, Ticker, Timer}; |
| 16 | use fixed::types::U24F8; | 16 | use fixed::types::U24F8; |
| 17 | use fixed_macro::fixed; | 17 | use fixed_macro::fixed; |
| 18 | use smart_leds::RGB8; | 18 | use smart_leds::RGB8; |
| @@ -107,6 +107,8 @@ impl<'d, P: Instance, const S: usize, const N: usize> Ws2812<'d, P, S, N> { | |||
| 107 | 107 | ||
| 108 | // DMA transfer | 108 | // DMA transfer |
| 109 | self.sm.tx().dma_push(self.dma.reborrow(), &words).await; | 109 | self.sm.tx().dma_push(self.dma.reborrow(), &words).await; |
| 110 | |||
| 111 | Timer::after_micros(55).await; | ||
| 110 | } | 112 | } |
| 111 | } | 113 | } |
| 112 | 114 | ||
| @@ -143,6 +145,7 @@ async fn main(_spawner: Spawner) { | |||
| 143 | let mut ws2812 = Ws2812::new(&mut common, sm0, p.DMA_CH0, p.PIN_16); | 145 | let mut ws2812 = Ws2812::new(&mut common, sm0, p.DMA_CH0, p.PIN_16); |
| 144 | 146 | ||
| 145 | // Loop forever making RGB values and pushing them out to the WS2812. | 147 | // Loop forever making RGB values and pushing them out to the WS2812. |
| 148 | let mut ticker = Ticker::every(Duration::from_millis(10)); | ||
| 146 | loop { | 149 | loop { |
| 147 | for j in 0..(256 * 5) { | 150 | for j in 0..(256 * 5) { |
| 148 | debug!("New Colors:"); | 151 | debug!("New Colors:"); |
| @@ -152,7 +155,7 @@ async fn main(_spawner: Spawner) { | |||
| 152 | } | 155 | } |
| 153 | ws2812.write(&data).await; | 156 | ws2812.write(&data).await; |
| 154 | 157 | ||
| 155 | Timer::after_millis(10).await; | 158 | ticker.next().await; |
| 156 | } | 159 | } |
| 157 | } | 160 | } |
| 158 | } | 161 | } |
diff --git a/examples/rp/src/bin/usb_serial_with_logger.rs b/examples/rp/src/bin/usb_serial_with_logger.rs new file mode 100644 index 000000000..4ba4fc25c --- /dev/null +++ b/examples/rp/src/bin/usb_serial_with_logger.rs | |||
| @@ -0,0 +1,117 @@ | |||
| 1 | //! This example shows how to use USB (Universal Serial Bus) in the RP2040 chip as well as how to create multiple usb classes for one device | ||
| 2 | //! | ||
| 3 | //! This creates a USB serial port that echos. It will also print out logging information on a separate serial device | ||
| 4 | |||
| 5 | #![no_std] | ||
| 6 | #![no_main] | ||
| 7 | |||
| 8 | use defmt::{info, panic}; | ||
| 9 | use embassy_executor::Spawner; | ||
| 10 | use embassy_futures::join::join; | ||
| 11 | use embassy_rp::bind_interrupts; | ||
| 12 | use embassy_rp::peripherals::USB; | ||
| 13 | use embassy_rp::usb::{Driver, Instance, InterruptHandler}; | ||
| 14 | use embassy_usb::class::cdc_acm::{CdcAcmClass, State}; | ||
| 15 | use embassy_usb::driver::EndpointError; | ||
| 16 | use embassy_usb::{Builder, Config}; | ||
| 17 | use {defmt_rtt as _, panic_probe as _}; | ||
| 18 | |||
| 19 | bind_interrupts!(struct Irqs { | ||
| 20 | USBCTRL_IRQ => InterruptHandler<USB>; | ||
| 21 | }); | ||
| 22 | |||
| 23 | #[embassy_executor::main] | ||
| 24 | async fn main(_spawner: Spawner) { | ||
| 25 | info!("Hello there!"); | ||
| 26 | |||
| 27 | let p = embassy_rp::init(Default::default()); | ||
| 28 | |||
| 29 | // Create the driver, from the HAL. | ||
| 30 | let driver = Driver::new(p.USB, Irqs); | ||
| 31 | |||
| 32 | // Create embassy-usb Config | ||
| 33 | let mut config = Config::new(0xc0de, 0xcafe); | ||
| 34 | config.manufacturer = Some("Embassy"); | ||
| 35 | config.product = Some("USB-serial example"); | ||
| 36 | config.serial_number = Some("12345678"); | ||
| 37 | config.max_power = 100; | ||
| 38 | config.max_packet_size_0 = 64; | ||
| 39 | |||
| 40 | // Required for windows compatibility. | ||
| 41 | // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help | ||
| 42 | config.device_class = 0xEF; | ||
| 43 | config.device_sub_class = 0x02; | ||
| 44 | config.device_protocol = 0x01; | ||
| 45 | config.composite_with_iads = true; | ||
| 46 | |||
| 47 | // Create embassy-usb DeviceBuilder using the driver and config. | ||
| 48 | // It needs some buffers for building the descriptors. | ||
| 49 | let mut device_descriptor = [0; 256]; | ||
| 50 | let mut config_descriptor = [0; 256]; | ||
| 51 | let mut bos_descriptor = [0; 256]; | ||
| 52 | let mut control_buf = [0; 64]; | ||
| 53 | |||
| 54 | let mut state = State::new(); | ||
| 55 | let mut logger_state = State::new(); | ||
| 56 | |||
| 57 | let mut builder = Builder::new( | ||
| 58 | driver, | ||
| 59 | config, | ||
| 60 | &mut device_descriptor, | ||
| 61 | &mut config_descriptor, | ||
| 62 | &mut bos_descriptor, | ||
| 63 | &mut [], // no msos descriptors | ||
| 64 | &mut control_buf, | ||
| 65 | ); | ||
| 66 | |||
| 67 | // Create classes on the builder. | ||
| 68 | let mut class = CdcAcmClass::new(&mut builder, &mut state, 64); | ||
| 69 | |||
| 70 | // Create a class for the logger | ||
| 71 | let logger_class = CdcAcmClass::new(&mut builder, &mut logger_state, 64); | ||
| 72 | |||
| 73 | // Creates the logger and returns the logger future | ||
| 74 | // Note: You'll need to use log::info! afterwards instead of info! for this to work (this also applies to all the other log::* macros) | ||
| 75 | let log_fut = embassy_usb_logger::with_class!(1024, log::LevelFilter::Info, logger_class); | ||
| 76 | |||
| 77 | // Build the builder. | ||
| 78 | let mut usb = builder.build(); | ||
| 79 | |||
| 80 | // Run the USB device. | ||
| 81 | let usb_fut = usb.run(); | ||
| 82 | |||
| 83 | // Do stuff with the class! | ||
| 84 | let echo_fut = async { | ||
| 85 | loop { | ||
| 86 | class.wait_connection().await; | ||
| 87 | log::info!("Connected"); | ||
| 88 | let _ = echo(&mut class).await; | ||
| 89 | log::info!("Disconnected"); | ||
| 90 | } | ||
| 91 | }; | ||
| 92 | |||
| 93 | // Run everything concurrently. | ||
| 94 | // If we had made everything `'static` above instead, we could do this using separate tasks instead. | ||
| 95 | join(usb_fut, join(echo_fut, log_fut)).await; | ||
| 96 | } | ||
| 97 | |||
| 98 | struct Disconnected {} | ||
| 99 | |||
| 100 | impl From<EndpointError> for Disconnected { | ||
| 101 | fn from(val: EndpointError) -> Self { | ||
| 102 | match val { | ||
| 103 | EndpointError::BufferOverflow => panic!("Buffer overflow"), | ||
| 104 | EndpointError::Disabled => Disconnected {}, | ||
| 105 | } | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | async fn echo<'d, T: Instance + 'd>(class: &mut CdcAcmClass<'d, Driver<'d, T>>) -> Result<(), Disconnected> { | ||
| 110 | let mut buf = [0; 64]; | ||
| 111 | loop { | ||
| 112 | let n = class.read_packet(&mut buf).await?; | ||
| 113 | let data = &buf[..n]; | ||
| 114 | info!("data: {:x}", data); | ||
| 115 | class.write_packet(data).await?; | ||
| 116 | } | ||
| 117 | } | ||
