Slimmed down template significantly

This commit is contained in:
2022-04-21 17:19:10 +02:00
parent 9c00026300
commit ccf36b6b73
4 changed files with 15 additions and 193 deletions

View File

@@ -0,0 +1 @@
This is based on João Nuno Carvalho's template at https://github.com/joaocarvalhoopen/Raspberry_Pi_Pico_in_Rust__Proj_Template_with_RTIC_USB-Serial_UF2

View File

@@ -9,16 +9,15 @@ edition = "2021"
[dependencies]
cortex-m = "0.7.3"
cortex-m-rt = "0.7.0"
cortex-m = "0.7.4"
cortex-m-rt = "0.7.1"
cortex-m-rtic = "1.0.0"
embedded-hal = { version = "0.2.5", features=["unproven"] }
embedded-hal = { version = "0.2.7", features=["unproven"] }
embedded-time = "0.12.0"
# jnc
usb-device = "0.2.8"
usbd-serial = "0.1.1"
usbd-hid = "0.5.1"
futures = { version = "0.3", default-features = false, optional = true }
defmt = "0.3.0"
@@ -26,7 +25,7 @@ defmt-rtt = "0.3.1"
panic-probe = { version = "0.3.0", features = ["print-defmt"] }
# We're using a Pico by default on this template
rp-pico = "0.2.0"
rp-pico = "0.3.0"
# but you can use any BSP. Uncomment this to use the pro_micro_rp2040 BSP instead
# sparkfun-pro-micro-rp2040 = "0.1.0"
@@ -36,54 +35,3 @@ rp2040-hal = { version="0.4.0", features=["rt"] }
rp2040-boot2 = {version="0.2.0", optional = true }
# cargo build/run
[profile.dev]
codegen-units = 1
debug = 2
debug-assertions = true
incremental = false
opt-level = 3
overflow-checks = true
# cargo build/run --release
[profile.release]
codegen-units = 1
debug = 2
debug-assertions = false
incremental = false
lto = 'fat'
opt-level = 3
overflow-checks = false
# do not optimize proc-macro crates = faster builds from scratch
[profile.dev.build-override]
codegen-units = 8
debug = false
debug-assertions = false
opt-level = 0
overflow-checks = false
[profile.release.build-override]
codegen-units = 8
debug = false
debug-assertions = false
opt-level = 0
overflow-checks = false
# cargo test
[profile.test]
codegen-units = 1
debug = 2
debug-assertions = true
incremental = false
opt-level = 3
overflow-checks = true
# cargo test --release
[profile.bench]
codegen-units = 1
debug = 2
debug-assertions = false
incremental = false
lto = 'fat'
opt-level = 3

View File

View File

@@ -17,6 +17,7 @@ mod app {
use rp_pico::hal;
use rp_pico::pac;
use rp_pico::XOSC_CRYSTAL_FREQ;
use rtic::mutex_prelude::{TupleExt04, TupleExt05};
// USB Device support
use usb_device::{class_prelude::*, prelude::*};
@@ -52,38 +53,12 @@ mod app {
// Global Static variable, has to be written inside unsafe blocks.
// A reference can be obtained with as_ref() method.
pub struct Counter {
counter: u32,
enable: bool,
}
impl Counter {
fn new() -> Self {
Counter {
counter: 0_u32,
enable: true,
}
}
fn get(&self) -> u32 {
self.counter
}
fn reset(&mut self) {
self.counter = 0_u32;
}
fn increment(&mut self) {
self.counter += 1_u32;
}
fn enable(&mut self, state: bool) {
self.enable = state;
}
}
#[shared]
struct Shared {
timer: hal::Timer,
alarm: hal::timer::Alarm0,
led: hal::gpio::Pin<hal::gpio::pin::bank0::Gpio25, hal::gpio::PushPullOutput>,
@@ -92,7 +67,6 @@ mod app {
serial: SerialPort<'static, hal::usb::UsbBus>,
usb_dev: usb_device::device::UsbDevice<'static, hal::usb::UsbBus>,
counter: Counter,
}
#[local]
@@ -164,14 +138,11 @@ mod app {
let mut timer = hal::Timer::new(c.device.TIMER, &mut resets);
let mut alarm = timer.alarm_0().unwrap();
let _ = alarm.schedule(SCAN_TIME_US.microseconds());
alarm.enable_interrupt(&mut timer);
alarm.enable_interrupt();
// Enable led_blink.
let led_blink_enable = true;
// Reset the counter
let counter = Counter::new();
//********
// Return the Shared variables struct, the Local variables struct and the XPTO Monitonics
// (Note: Read again the RTIC book in the section of Monotonics timers)
@@ -184,8 +155,6 @@ mod app {
serial,
usb_dev,
counter,
},
Local {},
init::Monotonics(),
@@ -196,82 +165,23 @@ mod app {
#[task(
binds = TIMER_IRQ_0,
priority = 1,
shared = [timer, alarm, led, led_blink_enable, serial, counter],
shared = [timer, alarm, serial],
local = [tog: bool = true],
)]
fn timer_irq(mut cx: timer_irq::Context) {
let mut buf = [0u8; 64];
let led = cx.shared.led;
let led_blink_enable = cx.shared.led_blink_enable;
let counter = cx.shared.counter;
let tog = cx.local.tog;
// Blinks the LED ON / OFF.
(led, led_blink_enable, counter).lock(|led_a, led_blink_enable_a, counter_a| {
let led_state_str: &str;
if *led_blink_enable_a {
if *tog {
led_a.set_high().unwrap();
led_state_str = "ON ";
} else {
led_a.set_low().unwrap();
led_state_str = "OFF";
}
let _ = writeln!(
Wrapper::new(&mut buf),
"LED {}! counter = {}",
led_state_str,
counter_a.get()
);
}
if counter_a.enable {
counter_a.increment();
}
if *led_blink_enable_a {
*tog = !*tog;
}
});
// Clears the timer interrupt and Set's the new delta_time in the future.
let mut timer = cx.shared.timer;
let mut alarm = cx.shared.alarm;
(alarm).lock(|a| {
(timer).lock(|timer_a| {
a.clear_interrupt(timer_a);
let _ = a.schedule(SCAN_TIME_US.microseconds());
});
});
// Write the message "blabla! 2" do USB-Serial.
cx.shared.serial.lock(|s| {
write_serial(s, unsafe { core::str::from_utf8_unchecked(&buf) }, false);
});
/*
// Write the message "blabla! 2" do USB-Serial.
c.shared.serial.lock(|s| {
let mut buf = [0u8; 64];
let _ = writeln!(Wrapper::new(&mut buf), "blabla! {}", 2); /*"{:?}"*/
write_serial(s, unsafe { core::str::from_utf8_unchecked(&buf) }, false);
});
*/
fn timer_irq(mut _cx: timer_irq::Context) {
}
/// Usb interrupt handler. Runs every time the host requests new data.
#[task(binds = USBCTRL_IRQ, priority = 3, shared = [led, led_blink_enable, serial, usb_dev, counter])]
#[task(binds = USBCTRL_IRQ, priority = 3, shared = [led, led_blink_enable, serial, usb_dev])]
fn usb_rx(cx: usb_rx::Context) {
let led = cx.shared.led;
let led_blink_enable = cx.shared.led_blink_enable;
let usb_dev = cx.shared.usb_dev;
let serial = cx.shared.serial;
let counter = cx.shared.counter;
(led, led_blink_enable, usb_dev, serial, counter).lock(
|led_a, led_blink_enable_a, usb_dev_a, serial_a, counter_a| {
(led, led_blink_enable, usb_dev, serial).lock(
|led_a, led_blink_enable_a, usb_dev_a, serial_a| {
// Check for new data
if usb_dev_a.poll(&mut [serial_a]) {
let mut buf = [0u8; 64];
@@ -292,40 +202,8 @@ mod app {
led_a,
led_blink_enable_a,
serial_a,
counter_a,
);
/*
// Code to echo the characters in Upper Case.
// Convert to upper case
buf.iter_mut().take(count).for_each(|b| {
b.make_ascii_uppercase();
});
if count > 0 {
let _ = serial_a.write(b"Received data! ");
let _ = serial_a.flush();
}
// Send back to the host
let mut wr_ptr = &buf[..count];
while !wr_ptr.is_empty() {
match serial_a.write(wr_ptr) {
Ok(len) => {
wr_ptr = &wr_ptr[len..];
// if wr_ptr.len() == 0 {
// let _ = serial_a.write(b"");
let _ = serial_a.flush();
// }
}
// On error, just drop unwritten data.
// One possible error is Err(WouldBlock), meaning the USB
// write buffer is full.
Err(_) => break,
};
}
*/
}
}
}
@@ -385,7 +263,6 @@ mod app {
led: &mut hal::gpio::Pin<hal::gpio::pin::bank0::Gpio25, hal::gpio::PushPullOutput>,
led_blink_enable: &mut bool,
serial: &mut SerialPort<'static, hal::usb::UsbBus>,
counter: &mut Counter,
) {
let _buf_len = buf.len();
match buf[0] {
@@ -397,22 +274,18 @@ mod app {
// 0 - Reset counter
b'0' => {
write_serial(serial, "M - Print Menu\n", false);
counter.reset();
}
// 1 - Increment counter
b'1' => {
write_serial(serial, "1 - Increment counter\n", false);
counter.increment();
}
// 2 - Start continues counter
b'2' => {
write_serial(serial, "2 - Start continues counter\n", false);
counter.enable(true);
}
// 3 - Stop continues counter
b'3' => {
write_serial(serial, "3 - Stop continues counter\n", false);
counter.enable(false);
}
// 4 - Get switch and LED state
b'4' => {