aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/rp/src/bin/usb_serial_with_handler.rs64
1 files changed, 64 insertions, 0 deletions
diff --git a/examples/rp/src/bin/usb_serial_with_handler.rs b/examples/rp/src/bin/usb_serial_with_handler.rs
new file mode 100644
index 000000000..a9e65be70
--- /dev/null
+++ b/examples/rp/src/bin/usb_serial_with_handler.rs
@@ -0,0 +1,64 @@
1//! This example shows how to use USB (Universal Serial Bus) in the RP2040 chip.
2//!
3//! This creates the possibility to send log::info/warn/error/debug! to USB serial port.
4
5#![no_std]
6#![no_main]
7
8use core::str;
9
10use embassy_executor::Spawner;
11use embassy_rp::bind_interrupts;
12use embassy_rp::peripherals::USB;
13use embassy_rp::rom_data::reset_to_usb_boot;
14use embassy_rp::usb::{Driver, InterruptHandler};
15use embassy_time::Timer;
16use embassy_usb_logger::ReceiverHandler;
17use {defmt_rtt as _, panic_probe as _};
18
19bind_interrupts!(struct Irqs {
20 USBCTRL_IRQ => InterruptHandler<USB>;
21});
22
23struct Handler;
24
25impl ReceiverHandler for Handler {
26 async fn handle_data(&self, data: &[u8]) {
27 if let Ok(data) = str::from_utf8(data) {
28 let data = data.trim();
29
30 // If you are using elf2uf2-term with the '-t' flag, then when closing the serial monitor,
31 // this will automatically put the pico into boot mode
32 if data == "q" || data == "elf2uf2-term" {
33 reset_to_usb_boot(0, 0); // Restart the chip
34 } else if data.eq_ignore_ascii_case("hello") {
35 log::info!("World!");
36 } else {
37 log::info!("Recieved: {:?}", data);
38 }
39 }
40 }
41
42 fn new() -> Self {
43 Self
44 }
45}
46
47#[embassy_executor::task]
48async fn logger_task(driver: Driver<'static, USB>) {
49 embassy_usb_logger::run!(1024, log::LevelFilter::Info, driver, Handler);
50}
51
52#[embassy_executor::main]
53async fn main(spawner: Spawner) {
54 let p = embassy_rp::init(Default::default());
55 let driver = Driver::new(p.USB, Irqs);
56 spawner.spawn(logger_task(driver)).unwrap();
57
58 let mut counter = 0;
59 loop {
60 counter += 1;
61 log::info!("Tick {}", counter);
62 Timer::after_secs(1).await;
63 }
64}