From 590d5bcc8600f984bd86f138b198d08762d400eb Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Sat, 17 Jul 2021 12:17:07 +0100 Subject: [PATCH 01/22] add pins for neo_trinkey --- boards/neo_trinkey/.cargo/config | 15 ++++++++ boards/neo_trinkey/Cargo.toml | 34 ++++++++++++++++++ boards/neo_trinkey/README.md | 29 +++++++++++++++ boards/neo_trinkey/build.rs | 16 +++++++++ boards/neo_trinkey/examples/blinky_basic.rs | 29 +++++++++++++++ boards/neo_trinkey/memory.x | 8 +++++ boards/neo_trinkey/src/lib.rs | 40 +++++++++++++++++++++ 7 files changed, 171 insertions(+) create mode 100644 boards/neo_trinkey/.cargo/config create mode 100644 boards/neo_trinkey/Cargo.toml create mode 100644 boards/neo_trinkey/README.md create mode 100644 boards/neo_trinkey/build.rs create mode 100644 boards/neo_trinkey/examples/blinky_basic.rs create mode 100644 boards/neo_trinkey/memory.x create mode 100644 boards/neo_trinkey/src/lib.rs diff --git a/boards/neo_trinkey/.cargo/config b/boards/neo_trinkey/.cargo/config new file mode 100644 index 000000000000..05d096540fd9 --- /dev/null +++ b/boards/neo_trinkey/.cargo/config @@ -0,0 +1,15 @@ +# samd21 is a Cortex-M0 and thus thumbv6m + +[build] +target = "thumbv6m-none-eabi" + +[target.thumbv6m-none-eabi] +runner = 'arm-none-eabi-gdb' +rustflags = [ + + # This is needed if your flash or ram addresses are not aligned to 0x10000 in memory.x + # See https://github.com/rust-embedded/cortex-m-quickstart/pull/95 + "-C", "link-arg=--nmagic", + + "-C", "link-arg=-Tlink.x", +] diff --git a/boards/neo_trinkey/Cargo.toml b/boards/neo_trinkey/Cargo.toml new file mode 100644 index 000000000000..e46869b963ad --- /dev/null +++ b/boards/neo_trinkey/Cargo.toml @@ -0,0 +1,34 @@ +[package] +name = "neo_trinkey" +version = "0.1.0" +authors = ["Daniel Mason "] +description = "Board Support crate for the Adafruit Neo Trinkey" +keywords = ["no-std", "arm", "cortex-m", "embedded-hal"] +license = "MIT OR Apache-2.0" +repository = "https://github.com/atsamd-rs/atsamd" +readme = "README.md" +edition = "2018" + +[dependencies] +cortex-m = "0.6" +embedded-hal = "0.2.3" +nb = "0.1" + +[dependencies.cortex-m-rt] +version = "0.6.12" +optional = true + +[dependencies.atsamd-hal] +path = "../../hal" +version = "0.12" +default-features = false + +[dev-dependencies] +panic-halt = "0.2" + +[features] +# ask the HAL to enable atsamd21e support +default = ["rt", "atsamd-hal/samd21e"] +rt = ["cortex-m-rt", "atsamd-hal/samd21e-rt"] +unproven = ["atsamd-hal/unproven"] +use_semihosting = [] diff --git a/boards/neo_trinkey/README.md b/boards/neo_trinkey/README.md new file mode 100644 index 000000000000..599d37f2d71f --- /dev/null +++ b/boards/neo_trinkey/README.md @@ -0,0 +1,29 @@ +# Adafruit Gemma M0 Board Support Crate + +This crate provides a type-safe API for working with the [Adafruit Neo Trinkey +board](https://www.adafruit.com/product/4870). + +## Prerequisites +* Install the cross compile toolchain `rustup target add thumbv6m-none-eabi` +* Install [cargo-hf2 the hf2 bootloader flasher tool](https://crates.io/crates/cargo-hf2) however your platform requires + +## Uploading an example +Check out the repository for examples: + +https://github.com/atsamd-rs/atsamd/tree/master/boards/neo_trinkey/examples + +* Be in this directory `cd boards/neo_trinkey` +* Put your device in bootloader mode usually by hitting the reset button twice. +* Build and upload in one step +```bash +$ cargo hf2 --release --example blinky_basic --pid 0x00ef --vid 0x239a +``` +You should see the following output +```text +Finished release [optimized] target(s) in 5.55s +Trying Ok(Some("Adafruit Industries")) Ok(Some("NeoPixel Trinkey M0")) +Flashing "/Users/danielmason/projects/rust/atsamd/boards/neo_trinkey/target/thumbv6m-none-eabi/release/examples/blinky_basic" +Finished in 0.051s +``` +Note: If hf2 can not find your Neo Trinkey, you should check the Product ID (pid) and Vendor ID (vid) in your system +settings. diff --git a/boards/neo_trinkey/build.rs b/boards/neo_trinkey/build.rs new file mode 100644 index 000000000000..4bed4688f2c0 --- /dev/null +++ b/boards/neo_trinkey/build.rs @@ -0,0 +1,16 @@ +use std::env; +use std::fs::File; +use std::io::Write; +use std::path::PathBuf; +fn main() { + if env::var_os("CARGO_FEATURE_RT").is_some() { + let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); + File::create(out.join("memory.x")) + .unwrap() + .write_all(include_bytes!("memory.x")) + .unwrap(); + println!("cargo:rustc-link-search={}", out.display()); + println!("cargo:rerun-if-changed=memory.x"); + } + println!("cargo:rerun-if-changed=build.rs"); +} diff --git a/boards/neo_trinkey/examples/blinky_basic.rs b/boards/neo_trinkey/examples/blinky_basic.rs new file mode 100644 index 000000000000..88d19afd7503 --- /dev/null +++ b/boards/neo_trinkey/examples/blinky_basic.rs @@ -0,0 +1,29 @@ +#![no_std] +#![no_main] + +use neo_trinkey as bsp; +use panic_halt as _; + +use bsp::hal; +use bsp::entry; +use hal::clock::GenericClockController; +use hal::delay::Delay; +use hal::pac::{CorePeripherals, Peripherals}; +use hal::prelude::*; + +#[entry] +fn main() -> ! { + let mut peripherals = Peripherals::take().unwrap(); + let core = CorePeripherals::take().unwrap(); + let mut clocks = GenericClockController::with_internal_32kosc( + peripherals.GCLK, + &mut peripherals.PM, + &mut peripherals.SYSCTRL, + &mut peripherals.NVMCTRL, + ); + let mut pins = bsp::Pins::new(peripherals.PORT); + + // TODO: There are no pin outs or on board leds except the neo pixels so we need to work out + // how to access them + loop {} +} diff --git a/boards/neo_trinkey/memory.x b/boards/neo_trinkey/memory.x new file mode 100644 index 000000000000..6f7f80898daa --- /dev/null +++ b/boards/neo_trinkey/memory.x @@ -0,0 +1,8 @@ +MEMORY +{ + /* Leave 8k for the default bootloader on the Neo Trinkey */ + FLASH (rx) : ORIGIN = 0x00000000 + 8K, LENGTH = 256K - 8K + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 32K +} +_stack_start = ORIGIN(RAM) + LENGTH(RAM); + diff --git a/boards/neo_trinkey/src/lib.rs b/boards/neo_trinkey/src/lib.rs new file mode 100644 index 000000000000..5ddf7fbcbd86 --- /dev/null +++ b/boards/neo_trinkey/src/lib.rs @@ -0,0 +1,40 @@ +#![no_std] + +#[cfg(feature = "rt")] +pub use cortex_m_rt::entry; + +pub use atsamd_hal as hal; +pub use embedded_hal as ehal; +pub use hal::pac; + +// use hal::clock::GenericClockController; +// use hal::sercom::v2::{spi, Sercom4}; +// use hal::sercom::{I2CMaster3, UART0}; +// use hal::time::Hertz; + +#[cfg(feature = "usb")] +use hal::usb::{usb_device::bus::UsbBusAllocator, UsbBus}; + +hal::bsp_pins!( + PA03 { + name: touch1, + aliases: { + AlternateB: Touch1 + } + }, + + PA05 { + name: neo_pixel, + aliases: { + AlternateB: NeoPixel + } + }, + + PA07 { + name: touch2, + aliases: { + AlternateB: Touch2 + } + }, +); + From 90d698c266f46ded8ee6190b354da7c578b8476d Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Sat, 17 Jul 2021 12:22:31 +0100 Subject: [PATCH 02/22] fix copy pasta --- boards/neo_trinkey/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boards/neo_trinkey/README.md b/boards/neo_trinkey/README.md index 599d37f2d71f..688e324f9d01 100644 --- a/boards/neo_trinkey/README.md +++ b/boards/neo_trinkey/README.md @@ -1,4 +1,4 @@ -# Adafruit Gemma M0 Board Support Crate +# Adafruit Neo Trinkey Board Support Crate This crate provides a type-safe API for working with the [Adafruit Neo Trinkey board](https://www.adafruit.com/product/4870). From fb5c615ed1177feeb0d2aa10c164c211c72b4918 Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Sat, 17 Jul 2021 17:12:20 +0100 Subject: [PATCH 03/22] attempting a few different ways to access the neo pixels --- boards/neo_trinkey/Cargo.toml | 2 + boards/neo_trinkey/examples/blinky_basic.rs | 45 ++++++++++++++++++--- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/boards/neo_trinkey/Cargo.toml b/boards/neo_trinkey/Cargo.toml index e46869b963ad..9d9c619c289d 100644 --- a/boards/neo_trinkey/Cargo.toml +++ b/boards/neo_trinkey/Cargo.toml @@ -25,6 +25,8 @@ default-features = false [dev-dependencies] panic-halt = "0.2" +ws2812-timer-delay = "0.3.0" +smart-leds = "0.3.0" [features] # ask the HAL to enable atsamd21e support diff --git a/boards/neo_trinkey/examples/blinky_basic.rs b/boards/neo_trinkey/examples/blinky_basic.rs index 88d19afd7503..870332c38a08 100644 --- a/boards/neo_trinkey/examples/blinky_basic.rs +++ b/boards/neo_trinkey/examples/blinky_basic.rs @@ -4,12 +4,22 @@ use neo_trinkey as bsp; use panic_halt as _; -use bsp::hal; use bsp::entry; +use bsp::hal; +use bsp::NeoPixel; use hal::clock::GenericClockController; use hal::delay::Delay; use hal::pac::{CorePeripherals, Peripherals}; use hal::prelude::*; +use hal::timer::SpinTimer; + +use embedded_hal::digital::v1_compat::OldOutputPin; +use smart_leds::hsv::{hsv2rgb, Hsv}; +use smart_leds::SmartLedsWrite; + +use atsamd_hal::timer::TimerCounter; +use neo_trinkey::hal::clock::Tcc2Tc3Clock; +use ws2812_timer_delay::Ws2812; #[entry] fn main() -> ! { @@ -21,9 +31,34 @@ fn main() -> ! { &mut peripherals.SYSCTRL, &mut peripherals.NVMCTRL, ); - let mut pins = bsp::Pins::new(peripherals.PORT); - // TODO: There are no pin outs or on board leds except the neo pixels so we need to work out - // how to access them - loop {} + // let mut delay = Delay::new(core.SYST, &mut clocks); + + let pins = bsp::Pins::new(peripherals.PORT); + // let mut neo_pixel: NeoPixel = pins.neo_pixel.into(); + // let mut neo_pixel: OldOutputPin<_> = pins.neo_pixel.into_push_pull_output().into(); + // let neo_pixel: OldOutputPin<_> = pins.neo_pixel.into_push_pull_output().into(); + let neo_pixel = pins.neo_pixel.into_push_pull_output(); + + // let gclk0 = clocks.gclk0(); + // let timer_clock = clocks.tc4_tc5(&gclk0).unwrap(); + // let mut timer = TimerCounter::tc4_(&timer_clock, peripherals.TC4, &mut peripherals.PM); + // timer.start(3.mhz()); + let timer = SpinTimer::new(16); + + let mut ws2812 = Ws2812::new(timer, neo_pixel); + + let mut delay = Delay::new(core.SYST, &mut clocks); + + loop { + for j in 0..255u8 { + let colors = [hsv2rgb(Hsv { + hue: j, + sat: 255, + val: 32, + })]; + ws2812.write(colors.iter().cloned()).unwrap(); + delay.delay_ms(100u8); + } + } } From 8e569dc8858d3de8c13f5ef0a9d46243ae68f34b Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Sun, 18 Jul 2021 20:08:14 +0100 Subject: [PATCH 04/22] blinky rainbow --- boards/neo_trinkey/Cargo.toml | 2 +- boards/neo_trinkey/examples/blinky_basic.rs | 64 ------------------ boards/neo_trinkey/examples/blinky_rainbow.rs | 67 +++++++++++++++++++ 3 files changed, 68 insertions(+), 65 deletions(-) delete mode 100644 boards/neo_trinkey/examples/blinky_basic.rs create mode 100644 boards/neo_trinkey/examples/blinky_rainbow.rs diff --git a/boards/neo_trinkey/Cargo.toml b/boards/neo_trinkey/Cargo.toml index 9d9c619c289d..ece0edd84fb4 100644 --- a/boards/neo_trinkey/Cargo.toml +++ b/boards/neo_trinkey/Cargo.toml @@ -25,7 +25,7 @@ default-features = false [dev-dependencies] panic-halt = "0.2" -ws2812-timer-delay = "0.3.0" +ws2812-timer-delay = { version = "0.3.0", features = ["slow"] } smart-leds = "0.3.0" [features] diff --git a/boards/neo_trinkey/examples/blinky_basic.rs b/boards/neo_trinkey/examples/blinky_basic.rs deleted file mode 100644 index 870332c38a08..000000000000 --- a/boards/neo_trinkey/examples/blinky_basic.rs +++ /dev/null @@ -1,64 +0,0 @@ -#![no_std] -#![no_main] - -use neo_trinkey as bsp; -use panic_halt as _; - -use bsp::entry; -use bsp::hal; -use bsp::NeoPixel; -use hal::clock::GenericClockController; -use hal::delay::Delay; -use hal::pac::{CorePeripherals, Peripherals}; -use hal::prelude::*; -use hal::timer::SpinTimer; - -use embedded_hal::digital::v1_compat::OldOutputPin; -use smart_leds::hsv::{hsv2rgb, Hsv}; -use smart_leds::SmartLedsWrite; - -use atsamd_hal::timer::TimerCounter; -use neo_trinkey::hal::clock::Tcc2Tc3Clock; -use ws2812_timer_delay::Ws2812; - -#[entry] -fn main() -> ! { - let mut peripherals = Peripherals::take().unwrap(); - let core = CorePeripherals::take().unwrap(); - let mut clocks = GenericClockController::with_internal_32kosc( - peripherals.GCLK, - &mut peripherals.PM, - &mut peripherals.SYSCTRL, - &mut peripherals.NVMCTRL, - ); - - // let mut delay = Delay::new(core.SYST, &mut clocks); - - let pins = bsp::Pins::new(peripherals.PORT); - // let mut neo_pixel: NeoPixel = pins.neo_pixel.into(); - // let mut neo_pixel: OldOutputPin<_> = pins.neo_pixel.into_push_pull_output().into(); - // let neo_pixel: OldOutputPin<_> = pins.neo_pixel.into_push_pull_output().into(); - let neo_pixel = pins.neo_pixel.into_push_pull_output(); - - // let gclk0 = clocks.gclk0(); - // let timer_clock = clocks.tc4_tc5(&gclk0).unwrap(); - // let mut timer = TimerCounter::tc4_(&timer_clock, peripherals.TC4, &mut peripherals.PM); - // timer.start(3.mhz()); - let timer = SpinTimer::new(16); - - let mut ws2812 = Ws2812::new(timer, neo_pixel); - - let mut delay = Delay::new(core.SYST, &mut clocks); - - loop { - for j in 0..255u8 { - let colors = [hsv2rgb(Hsv { - hue: j, - sat: 255, - val: 32, - })]; - ws2812.write(colors.iter().cloned()).unwrap(); - delay.delay_ms(100u8); - } - } -} diff --git a/boards/neo_trinkey/examples/blinky_rainbow.rs b/boards/neo_trinkey/examples/blinky_rainbow.rs new file mode 100644 index 000000000000..03e2b3db13e1 --- /dev/null +++ b/boards/neo_trinkey/examples/blinky_rainbow.rs @@ -0,0 +1,67 @@ +#![no_std] +#![no_main] + +use neo_trinkey as bsp; +use panic_halt as _; + +use bsp::entry; +use bsp::hal; +use hal::clock::GenericClockController; +use hal::delay::Delay; +use hal::pac::{CorePeripherals, Peripherals}; +use hal::prelude::*; +use hal::timer::TimerCounter; + +use smart_leds::{brightness, hsv::RGB8, SmartLedsWrite}; +use ws2812_timer_delay::Ws2812; + +#[entry] +fn main() -> ! { + let mut peripherals = Peripherals::take().unwrap(); + let core = CorePeripherals::take().unwrap(); + let mut clocks = GenericClockController::with_internal_32kosc( + peripherals.GCLK, + &mut peripherals.PM, + &mut peripherals.SYSCTRL, + &mut peripherals.NVMCTRL, + ); + + let pins = bsp::Pins::new(peripherals.PORT); + + let gclk0 = clocks.gclk0(); + let timer_clock = clocks.tcc2_tc3(&gclk0).unwrap(); + let mut timer = TimerCounter::tc3_(&timer_clock, peripherals.TC3, &mut peripherals.PM); + timer.start(3.mhz()); + let neo_pixel = pins.neo_pixel.into_push_pull_output(); + let mut ws2812 = Ws2812::new(timer, neo_pixel); + + let mut delay = Delay::new(core.SYST, &mut clocks); + + const NUM_LEDS: usize = 4; + let mut data = [RGB8::default(); NUM_LEDS]; + + loop { + for j in 0..(256 * 5) { + for i in 0..NUM_LEDS { + data[i] = wheel((((i * 256) as u16 / NUM_LEDS as u16 + j as u16) & 255) as u8); + } + ws2812.write(brightness(data.iter().cloned(), 32)).unwrap(); + delay.delay_ms(5u8); + } + } +} + +/// Input a value 0 to 255 to get a color value +/// The colours are a transition r - g - b - back to r. +fn wheel(mut wheel_pos: u8) -> RGB8 { + wheel_pos = 255 - wheel_pos; + if wheel_pos < 85 { + return (255 - wheel_pos * 3, 0, wheel_pos * 3).into(); + } + if wheel_pos < 170 { + wheel_pos -= 85; + return (0, wheel_pos * 3, 255 - wheel_pos * 3).into(); + } + wheel_pos -= 170; + (wheel_pos * 3, 255 - wheel_pos * 3, 0).into() +} From 6c00b0bc5099e5ba786bb791ff3e6f94315d8cad Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Sun, 18 Jul 2021 22:58:37 +0100 Subject: [PATCH 05/22] blinky basic --- boards/neo_trinkey/examples/blinky_basic.rs | 55 +++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 boards/neo_trinkey/examples/blinky_basic.rs diff --git a/boards/neo_trinkey/examples/blinky_basic.rs b/boards/neo_trinkey/examples/blinky_basic.rs new file mode 100644 index 000000000000..935c3e3c28fd --- /dev/null +++ b/boards/neo_trinkey/examples/blinky_basic.rs @@ -0,0 +1,55 @@ +#![no_std] +#![no_main] + +use neo_trinkey as bsp; +use panic_halt as _; + +use bsp::entry; +use bsp::hal; +use hal::clock::GenericClockController; +use hal::delay::Delay; +use hal::pac::{CorePeripherals, Peripherals}; +use hal::prelude::*; +use hal::timer::TimerCounter; + +use smart_leds::{hsv::RGB8, SmartLedsWrite}; +use ws2812_timer_delay::Ws2812; + +#[entry] +fn main() -> ! { + let mut peripherals = Peripherals::take().unwrap(); + let core = CorePeripherals::take().unwrap(); + let mut clocks = GenericClockController::with_internal_32kosc( + peripherals.GCLK, + &mut peripherals.PM, + &mut peripherals.SYSCTRL, + &mut peripherals.NVMCTRL, + ); + + let pins = bsp::Pins::new(peripherals.PORT); + + let gclk0 = clocks.gclk0(); + let timer_clock = clocks.tcc2_tc3(&gclk0).unwrap(); + let mut timer = TimerCounter::tc3_(&timer_clock, peripherals.TC3, &mut peripherals.PM); + timer.start(3.mhz()); + let neo_pixel = pins.neo_pixel.into_push_pull_output(); + let mut ws2812 = Ws2812::new(timer, neo_pixel); + + let mut delay = Delay::new(core.SYST, &mut clocks); + + const NUM_LEDS: usize = 4; + let off = [RGB8::default(); NUM_LEDS]; + let on = [ + RGB8::new(5, 5, 0), + RGB8::new(0, 5, 5), + RGB8::new(5, 0, 5), + RGB8::new(2, 2, 2), + ]; + + loop { + ws2812.write(off.iter().cloned()).unwrap(); + delay.delay_ms(500u16); + ws2812.write(on.iter().cloned()).unwrap(); + delay.delay_ms(500u16); + } +} From cf4c64a113c6acd07e091be507bff3b1d55bc3d5 Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Sat, 31 Jul 2021 18:34:05 +0100 Subject: [PATCH 06/22] Add usb_echo example and update readme --- boards/neo_trinkey/Cargo.toml | 14 ++++ boards/neo_trinkey/README.md | 66 ++++++++++++++++- boards/neo_trinkey/examples/usb_echo.rs | 95 +++++++++++++++++++++++++ boards/neo_trinkey/src/lib.rs | 33 ++++++++- 4 files changed, 204 insertions(+), 4 deletions(-) create mode 100644 boards/neo_trinkey/examples/usb_echo.rs diff --git a/boards/neo_trinkey/Cargo.toml b/boards/neo_trinkey/Cargo.toml index ece0edd84fb4..67f86dea1f29 100644 --- a/boards/neo_trinkey/Cargo.toml +++ b/boards/neo_trinkey/Cargo.toml @@ -23,6 +23,18 @@ path = "../../hal" version = "0.12" default-features = false +[dependencies.usb-device] +version = "0.2" +optional = true + +[dependencies.usbd-serial] +version = "0.1" +optional = true + +[dependencies.panic-halt] +version = "0.2" +optional = true + [dev-dependencies] panic-halt = "0.2" ws2812-timer-delay = { version = "0.3.0", features = ["slow"] } @@ -34,3 +46,5 @@ default = ["rt", "atsamd-hal/samd21e"] rt = ["cortex-m-rt", "atsamd-hal/samd21e-rt"] unproven = ["atsamd-hal/unproven"] use_semihosting = [] +usb = ["atsamd-hal/usb", "usb-device", "usbd-serial"] +panic_halt = ["panic-halt"] diff --git a/boards/neo_trinkey/README.md b/boards/neo_trinkey/README.md index 688e324f9d01..ea7e2ba42381 100644 --- a/boards/neo_trinkey/README.md +++ b/boards/neo_trinkey/README.md @@ -16,8 +16,11 @@ https://github.com/atsamd-rs/atsamd/tree/master/boards/neo_trinkey/examples * Put your device in bootloader mode usually by hitting the reset button twice. * Build and upload in one step ```bash -$ cargo hf2 --release --example blinky_basic --pid 0x00ef --vid 0x239a +$ cargo hf2 --release --example --pid 0x00ef --vid 0x239a ``` +> **Note:** If you have a newer version of cargo-hf2 you won't need the pid and vid, it will automatically recognise the +> device + You should see the following output ```text Finished release [optimized] target(s) in 5.55s @@ -27,3 +30,64 @@ Finished in 0.051s ``` Note: If hf2 can not find your Neo Trinkey, you should check the Product ID (pid) and Vendor ID (vid) in your system settings. + +## Examples + +### Blinky basic + +```bash +$ cargo hf2 --release --example blinky_basic --pid 0x00ef --vid 0x239a +``` + +Once the Neo Trinkey has restarted, you will see the 4 leds flash in unison. Each led will be a different color (pink, +cyan, yellow and white). + +**Warning** even though the lights are turned down very low, they are still very bright. + +### Blinky rainbow + +```bash +$ cargo hf2 --release --example blinky_rainbow --pid 0x00ef --vid 0x239a +``` + +A slightly more satisfying version of blinky where the lights will cycle through the color spectrum. + +**Warning** even though the lights are turned down very low, they are still very bright. + +### USB echo + +```bash +$ cargo hf2 --release --example usb_echo --pid 0x00ef --vid 0x239a --features usb,panic-halt +``` + +Once the device has reset, all the lights will be off. You will then need to find the USB device on your machine. + +Usually this is located in `/dev/cu.usbmodemTRINKEY_ECHO1`. though if you have multiple trinkeys plugged in and running +this example, the number at the end may change. + +You can then send the USB device bytes. Each time the device receives data, it will respond with "Received: X" where X +is the data that it received. To test this in a variety of ways but the easiest is probably with screen. + +Connect to the device like this (9600 is the baud rate) + +```bash +$ screen /dev/cu.usbmodemTRINKEY_ECHO1 9600 +``` + +You can then press keys and you should get a response Eg: + +```text +Received: h +Received: e +Received: l +Received: l +Received: o +Received: +Received: w +Received: o +Received: r +Received: l +Received: d +``` + +To quit screen, use `ctrl-a` followed by `crtl-\` diff --git a/boards/neo_trinkey/examples/usb_echo.rs b/boards/neo_trinkey/examples/usb_echo.rs new file mode 100644 index 000000000000..fb0098244d8d --- /dev/null +++ b/boards/neo_trinkey/examples/usb_echo.rs @@ -0,0 +1,95 @@ +#![no_std] +#![no_main] + +use panic_halt as _; + +use cortex_m::asm::delay as cycle_delay; +use cortex_m::peripheral::NVIC; +use usb_device::bus::UsbBusAllocator; +use usb_device::prelude::*; +use usbd_serial::{SerialPort, USB_CLASS_CDC}; + +use bsp::hal; +use bsp::pac; +use neo_trinkey as bsp; + +use bsp::entry; +use hal::clock::GenericClockController; +use hal::usb::UsbBus; +use pac::{interrupt, CorePeripherals, Peripherals}; + +#[entry] +fn main() -> ! { + let mut peripherals = Peripherals::take().unwrap(); + let mut core = CorePeripherals::take().unwrap(); + let mut clocks = GenericClockController::with_internal_32kosc( + peripherals.GCLK, + &mut peripherals.PM, + &mut peripherals.SYSCTRL, + &mut peripherals.NVMCTRL, + ); + let pins = bsp::Pins::new(peripherals.PORT); + + let bus_allocator = unsafe { + USB_ALLOCATOR = Some(bsp::usb_allocator( + peripherals.USB, + &mut clocks, + &mut peripherals.PM, + pins.usb_dm, + pins.usb_dp, + )); + USB_ALLOCATOR.as_ref().unwrap() + }; + + unsafe { + USB_SERIAL = Some(SerialPort::new(&bus_allocator)); + USB_BUS = Some( + UsbDeviceBuilder::new(&bus_allocator, UsbVidPid(0x16c0, 0x27dd)) + .manufacturer("Fake company") + .product("Serial port") + .serial_number("TRINKEY_ECHO") + .device_class(USB_CLASS_CDC) + .build(), + ); + } + + unsafe { + core.NVIC.set_priority(interrupt::USB, 1); + NVIC::unmask(interrupt::USB); + } + + loop { + cycle_delay(15 * 1024 * 1024); + } +} + +static mut USB_ALLOCATOR: Option> = None; +static mut USB_BUS: Option> = None; +static mut USB_SERIAL: Option> = None; + +fn poll_usb() { + unsafe { + USB_BUS.as_mut().map(|usb_dev| { + USB_SERIAL.as_mut().map(|serial| { + usb_dev.poll(&mut [serial]); + let mut buf = [0u8; 64]; + + if let Ok(count) = serial.read(&mut buf) { + for (i, c) in buf.iter().enumerate() { + if i >= count { + break; + } + serial.write("Received: ".as_bytes()).ok(); + serial.write(&[c.clone()]).ok(); + serial.write("\r\n".as_bytes()).ok(); + } + }; + }); + }); + }; +} + +#[interrupt] +fn USB() { + poll_usb(); +} diff --git a/boards/neo_trinkey/src/lib.rs b/boards/neo_trinkey/src/lib.rs index 5ddf7fbcbd86..66362876e96b 100644 --- a/boards/neo_trinkey/src/lib.rs +++ b/boards/neo_trinkey/src/lib.rs @@ -7,13 +7,13 @@ pub use atsamd_hal as hal; pub use embedded_hal as ehal; pub use hal::pac; -// use hal::clock::GenericClockController; +use hal::clock::GenericClockController; // use hal::sercom::v2::{spi, Sercom4}; // use hal::sercom::{I2CMaster3, UART0}; // use hal::time::Hertz; #[cfg(feature = "usb")] -use hal::usb::{usb_device::bus::UsbBusAllocator, UsbBus}; +pub use hal::usb::{usb_device::bus::UsbBusAllocator, UsbBus}; hal::bsp_pins!( PA03 { @@ -32,9 +32,36 @@ hal::bsp_pins!( PA07 { name: touch2, + aliases: { AlternateB: Touch2 } + }, + + PA24 { + /// The USB D- pad + name: usb_dm aliases: { - AlternateB: Touch2 + AlternateG: UsbDm } }, + + PA25 { + /// The USB D+ pad + name: usb_dp + aliases: { + AlternateG: UsbDp + } + } ); +#[cfg(feature = "usb")] +pub fn usb_allocator( + usb: pac::USB, + clocks: &mut GenericClockController, + pm: &mut pac::PM, + dm: impl Into, + dp: impl Into, +) -> UsbBusAllocator { + let gclk0 = clocks.gclk0(); + let clock = &clocks.usb(&gclk0).unwrap(); + let (dm, dp) = (dm.into(), dp.into()); + UsbBusAllocator::new(UsbBus::new(clock, pm, dm, dp, usb)) +} From fce8bcb55bceedf239992d386e7204d33bb4548d Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Sat, 31 Jul 2021 23:33:19 +0100 Subject: [PATCH 07/22] Make usb_echo do what it does in other examples, add new demo to make the received data clearer --- boards/neo_trinkey/README.md | 23 +++++- boards/neo_trinkey/examples/usb_ack.rs | 95 +++++++++++++++++++++++++ boards/neo_trinkey/examples/usb_echo.rs | 2 - 3 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 boards/neo_trinkey/examples/usb_ack.rs diff --git a/boards/neo_trinkey/README.md b/boards/neo_trinkey/README.md index ea7e2ba42381..7aa015d4d315 100644 --- a/boards/neo_trinkey/README.md +++ b/boards/neo_trinkey/README.md @@ -57,7 +57,7 @@ A slightly more satisfying version of blinky where the lights will cycle through ### USB echo ```bash -$ cargo hf2 --release --example usb_echo --pid 0x00ef --vid 0x239a --features usb,panic-halt +$ cargo hf2 --release --example usb_echo --features usb,panic-halt --pid 0x00ef --vid 0x239a ``` Once the device has reset, all the lights will be off. You will then need to find the USB device on your machine. @@ -74,6 +74,27 @@ Connect to the device like this (9600 is the baud rate) $ screen /dev/cu.usbmodemTRINKEY_ECHO1 9600 ``` +You can now type, and the characters you type will appear on screen, but the magic here is that what's actually +happening is your key presses are being sent to the device, and the device is responding with the same data which is +what you see appearing on the screen. + +To quit screen, use `ctrl-a` followed by `crtl-\` + +### USB ack + +```bash +$ cargo hf2 --release --example usb_ack --features usb,panic-halt --pid 0x00ef --vid 0x239a +``` + +This behaves similarly to the USB echo example above except that each time the device receives data, it will respond +with "Received: X" where X is the data that it received. This makes what's happening a little clearer. + +Connect to the device like this (9600 is the baud rate) + +```bash +$ screen /dev/cu.usbmodemTRINKEY_ECHO1 9600 +``` + You can then press keys and you should get a response Eg: ```text diff --git a/boards/neo_trinkey/examples/usb_ack.rs b/boards/neo_trinkey/examples/usb_ack.rs new file mode 100644 index 000000000000..fb0098244d8d --- /dev/null +++ b/boards/neo_trinkey/examples/usb_ack.rs @@ -0,0 +1,95 @@ +#![no_std] +#![no_main] + +use panic_halt as _; + +use cortex_m::asm::delay as cycle_delay; +use cortex_m::peripheral::NVIC; +use usb_device::bus::UsbBusAllocator; +use usb_device::prelude::*; +use usbd_serial::{SerialPort, USB_CLASS_CDC}; + +use bsp::hal; +use bsp::pac; +use neo_trinkey as bsp; + +use bsp::entry; +use hal::clock::GenericClockController; +use hal::usb::UsbBus; +use pac::{interrupt, CorePeripherals, Peripherals}; + +#[entry] +fn main() -> ! { + let mut peripherals = Peripherals::take().unwrap(); + let mut core = CorePeripherals::take().unwrap(); + let mut clocks = GenericClockController::with_internal_32kosc( + peripherals.GCLK, + &mut peripherals.PM, + &mut peripherals.SYSCTRL, + &mut peripherals.NVMCTRL, + ); + let pins = bsp::Pins::new(peripherals.PORT); + + let bus_allocator = unsafe { + USB_ALLOCATOR = Some(bsp::usb_allocator( + peripherals.USB, + &mut clocks, + &mut peripherals.PM, + pins.usb_dm, + pins.usb_dp, + )); + USB_ALLOCATOR.as_ref().unwrap() + }; + + unsafe { + USB_SERIAL = Some(SerialPort::new(&bus_allocator)); + USB_BUS = Some( + UsbDeviceBuilder::new(&bus_allocator, UsbVidPid(0x16c0, 0x27dd)) + .manufacturer("Fake company") + .product("Serial port") + .serial_number("TRINKEY_ECHO") + .device_class(USB_CLASS_CDC) + .build(), + ); + } + + unsafe { + core.NVIC.set_priority(interrupt::USB, 1); + NVIC::unmask(interrupt::USB); + } + + loop { + cycle_delay(15 * 1024 * 1024); + } +} + +static mut USB_ALLOCATOR: Option> = None; +static mut USB_BUS: Option> = None; +static mut USB_SERIAL: Option> = None; + +fn poll_usb() { + unsafe { + USB_BUS.as_mut().map(|usb_dev| { + USB_SERIAL.as_mut().map(|serial| { + usb_dev.poll(&mut [serial]); + let mut buf = [0u8; 64]; + + if let Ok(count) = serial.read(&mut buf) { + for (i, c) in buf.iter().enumerate() { + if i >= count { + break; + } + serial.write("Received: ".as_bytes()).ok(); + serial.write(&[c.clone()]).ok(); + serial.write("\r\n".as_bytes()).ok(); + } + }; + }); + }); + }; +} + +#[interrupt] +fn USB() { + poll_usb(); +} diff --git a/boards/neo_trinkey/examples/usb_echo.rs b/boards/neo_trinkey/examples/usb_echo.rs index fb0098244d8d..e4d6277c49b1 100644 --- a/boards/neo_trinkey/examples/usb_echo.rs +++ b/boards/neo_trinkey/examples/usb_echo.rs @@ -79,9 +79,7 @@ fn poll_usb() { if i >= count { break; } - serial.write("Received: ".as_bytes()).ok(); serial.write(&[c.clone()]).ok(); - serial.write("\r\n".as_bytes()).ok(); } }; }); From e390b10ea853d075dbeddceeb01e7ce52f1d6de3 Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Sun, 1 Aug 2021 08:44:46 +0100 Subject: [PATCH 08/22] added examples to Cargo.toml --- boards/neo_trinkey/Cargo.toml | 14 ++++++++++++++ boards/neo_trinkey/README.md | 8 ++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/boards/neo_trinkey/Cargo.toml b/boards/neo_trinkey/Cargo.toml index 67f86dea1f29..7450f250668f 100644 --- a/boards/neo_trinkey/Cargo.toml +++ b/boards/neo_trinkey/Cargo.toml @@ -48,3 +48,17 @@ unproven = ["atsamd-hal/unproven"] use_semihosting = [] usb = ["atsamd-hal/usb", "usb-device", "usbd-serial"] panic_halt = ["panic-halt"] + +[[example]] +name = "blinky_basic" + +[[example]] +name = "blinky_rainbow" + +[[example]] +name = "usb_ack" +required-features = ["usb"] + +[[example]] +name = "usb_echo" +required-features = ["usb"] diff --git a/boards/neo_trinkey/README.md b/boards/neo_trinkey/README.md index 7aa015d4d315..ca896a2f827f 100644 --- a/boards/neo_trinkey/README.md +++ b/boards/neo_trinkey/README.md @@ -57,7 +57,7 @@ A slightly more satisfying version of blinky where the lights will cycle through ### USB echo ```bash -$ cargo hf2 --release --example usb_echo --features usb,panic-halt --pid 0x00ef --vid 0x239a +$ cargo hf2 --release --example usb_echo --features usb --pid 0x00ef --vid 0x239a ``` Once the device has reset, all the lights will be off. You will then need to find the USB device on your machine. @@ -78,12 +78,12 @@ You can now type, and the characters you type will appear on screen, but the mag happening is your key presses are being sent to the device, and the device is responding with the same data which is what you see appearing on the screen. -To quit screen, use `ctrl-a` followed by `crtl-\` +To quit screen, use `ctrl-a` followed by `crtl-\` then `y` ### USB ack ```bash -$ cargo hf2 --release --example usb_ack --features usb,panic-halt --pid 0x00ef --vid 0x239a +$ cargo hf2 --release --example usb_ack --features usb --pid 0x00ef --vid 0x239a ``` This behaves similarly to the USB echo example above except that each time the device receives data, it will respond @@ -111,4 +111,4 @@ Received: l Received: d ``` -To quit screen, use `ctrl-a` followed by `crtl-\` +To quit screen, use `ctrl-a` followed by `crtl-\` then `y` From aae4ef9e05eef41a90a6ac03260e4ed0e398c314 Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Sun, 1 Aug 2021 14:53:58 +0100 Subject: [PATCH 09/22] Tidy up dependencies and features --- boards/neo_trinkey/Cargo.toml | 16 +++++++++++++--- boards/neo_trinkey/README.md | 6 +++--- boards/neo_trinkey/examples/usb_ack.rs | 17 +++++++---------- boards/neo_trinkey/src/lib.rs | 7 +------ 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/boards/neo_trinkey/Cargo.toml b/boards/neo_trinkey/Cargo.toml index 7450f250668f..68a2a4caa09b 100644 --- a/boards/neo_trinkey/Cargo.toml +++ b/boards/neo_trinkey/Cargo.toml @@ -35,25 +35,35 @@ optional = true version = "0.2" optional = true +[dependencies.ws2812-timer-delay] +version = "0.3.0" +features = ["slow"] +optional = true + +[dependencies.smart-leds] +version = "0.3.0" +optional = true + [dev-dependencies] panic-halt = "0.2" -ws2812-timer-delay = { version = "0.3.0", features = ["slow"] } -smart-leds = "0.3.0" [features] # ask the HAL to enable atsamd21e support default = ["rt", "atsamd-hal/samd21e"] +leds = ["ws2812-timer-delay", "smart-leds"] +panic_halt = ["panic-halt"] rt = ["cortex-m-rt", "atsamd-hal/samd21e-rt"] unproven = ["atsamd-hal/unproven"] use_semihosting = [] usb = ["atsamd-hal/usb", "usb-device", "usbd-serial"] -panic_halt = ["panic-halt"] [[example]] name = "blinky_basic" +required-features = ["leds"] [[example]] name = "blinky_rainbow" +required-features = ["leds"] [[example]] name = "usb_ack" diff --git a/boards/neo_trinkey/README.md b/boards/neo_trinkey/README.md index ca896a2f827f..06cd29c28236 100644 --- a/boards/neo_trinkey/README.md +++ b/boards/neo_trinkey/README.md @@ -16,7 +16,7 @@ https://github.com/atsamd-rs/atsamd/tree/master/boards/neo_trinkey/examples * Put your device in bootloader mode usually by hitting the reset button twice. * Build and upload in one step ```bash -$ cargo hf2 --release --example --pid 0x00ef --vid 0x239a +$ cargo hf2 --release --example --features --pid 0x00ef --vid 0x239a ``` > **Note:** If you have a newer version of cargo-hf2 you won't need the pid and vid, it will automatically recognise the > device @@ -36,7 +36,7 @@ settings. ### Blinky basic ```bash -$ cargo hf2 --release --example blinky_basic --pid 0x00ef --vid 0x239a +$ cargo hf2 --release --example blinky_basic --features leds --pid 0x00ef --vid 0x239a ``` Once the Neo Trinkey has restarted, you will see the 4 leds flash in unison. Each led will be a different color (pink, @@ -47,7 +47,7 @@ cyan, yellow and white). ### Blinky rainbow ```bash -$ cargo hf2 --release --example blinky_rainbow --pid 0x00ef --vid 0x239a +$ cargo hf2 --release --example blinky_rainbow --features leds --pid 0x00ef --vid 0x239a ``` A slightly more satisfying version of blinky where the lights will cycle through the color spectrum. diff --git a/boards/neo_trinkey/examples/usb_ack.rs b/boards/neo_trinkey/examples/usb_ack.rs index fb0098244d8d..bf9b3447db2d 100644 --- a/boards/neo_trinkey/examples/usb_ack.rs +++ b/boards/neo_trinkey/examples/usb_ack.rs @@ -30,16 +30,13 @@ fn main() -> ! { ); let pins = bsp::Pins::new(peripherals.PORT); - let bus_allocator = unsafe { - USB_ALLOCATOR = Some(bsp::usb_allocator( - peripherals.USB, - &mut clocks, - &mut peripherals.PM, - pins.usb_dm, - pins.usb_dp, - )); - USB_ALLOCATOR.as_ref().unwrap() - }; + let mut bus_allocator = bsp::usb_allocator( + peripherals.USB, + &mut clocks, + &mut peripherals.PM, + pins.usb_dm, + pins.usb_dp, + ); unsafe { USB_SERIAL = Some(SerialPort::new(&bus_allocator)); diff --git a/boards/neo_trinkey/src/lib.rs b/boards/neo_trinkey/src/lib.rs index 66362876e96b..069d826f5aae 100644 --- a/boards/neo_trinkey/src/lib.rs +++ b/boards/neo_trinkey/src/lib.rs @@ -7,11 +7,6 @@ pub use atsamd_hal as hal; pub use embedded_hal as ehal; pub use hal::pac; -use hal::clock::GenericClockController; -// use hal::sercom::v2::{spi, Sercom4}; -// use hal::sercom::{I2CMaster3, UART0}; -// use hal::time::Hertz; - #[cfg(feature = "usb")] pub use hal::usb::{usb_device::bus::UsbBusAllocator, UsbBus}; @@ -55,7 +50,7 @@ hal::bsp_pins!( #[cfg(feature = "usb")] pub fn usb_allocator( usb: pac::USB, - clocks: &mut GenericClockController, + clocks: &mut hal::clock::GenericClockController, pm: &mut pac::PM, dm: impl Into, dp: impl Into, From dac292ec702dc82b3fcc4f8a3b9ee6b642b1b4bb Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Sat, 7 Aug 2021 13:28:46 +0100 Subject: [PATCH 10/22] fix: usb_ack --- boards/neo_trinkey/examples/usb_ack.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/boards/neo_trinkey/examples/usb_ack.rs b/boards/neo_trinkey/examples/usb_ack.rs index bf9b3447db2d..fb0098244d8d 100644 --- a/boards/neo_trinkey/examples/usb_ack.rs +++ b/boards/neo_trinkey/examples/usb_ack.rs @@ -30,13 +30,16 @@ fn main() -> ! { ); let pins = bsp::Pins::new(peripherals.PORT); - let mut bus_allocator = bsp::usb_allocator( - peripherals.USB, - &mut clocks, - &mut peripherals.PM, - pins.usb_dm, - pins.usb_dp, - ); + let bus_allocator = unsafe { + USB_ALLOCATOR = Some(bsp::usb_allocator( + peripherals.USB, + &mut clocks, + &mut peripherals.PM, + pins.usb_dm, + pins.usb_dp, + )); + USB_ALLOCATOR.as_ref().unwrap() + }; unsafe { USB_SERIAL = Some(SerialPort::new(&bus_allocator)); From 159483342e1ed3a6d2bc6af2181cebcec04efb36 Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Sat, 4 Sep 2021 15:54:52 +0100 Subject: [PATCH 11/22] update crates.json --- crates.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates.json b/crates.json index 34566eeee0f7..2dfa8e243cc6 100644 --- a/crates.json +++ b/crates.json @@ -60,6 +60,9 @@ "tier": 1, "build": "cargo build --examples --features=unproven,usb" }, + "neo_trinkey": { + "build": "cargo build --examples --features=leds,unproven,usb" + }, "p1am_100": { "tier": 2, "build": "cargo build --examples --features=unproven,usb" From d46ee7f880bf6c4d5a6c5d989c1c96fd982d52a2 Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Thu, 7 Oct 2021 11:06:36 +0100 Subject: [PATCH 12/22] Add comments explaining the weirdness of USB interrupt --- boards/neo_trinkey/examples/usb_ack.rs | 3 +++ boards/neo_trinkey/examples/usb_echo.rs | 3 +++ 2 files changed, 6 insertions(+) diff --git a/boards/neo_trinkey/examples/usb_ack.rs b/boards/neo_trinkey/examples/usb_ack.rs index fb0098244d8d..4feb61783858 100644 --- a/boards/neo_trinkey/examples/usb_ack.rs +++ b/boards/neo_trinkey/examples/usb_ack.rs @@ -91,5 +91,8 @@ fn poll_usb() { #[interrupt] fn USB() { + // Note: USB is the name of the interrupt, you can not attach the #[interrupt] tag to poll_usb. + // Although you could add the contents of poll_usb into this function, separating them allows + // you to add more functions to run on the USB interrupt in the future. poll_usb(); } diff --git a/boards/neo_trinkey/examples/usb_echo.rs b/boards/neo_trinkey/examples/usb_echo.rs index e4d6277c49b1..cc4d330535c6 100644 --- a/boards/neo_trinkey/examples/usb_echo.rs +++ b/boards/neo_trinkey/examples/usb_echo.rs @@ -89,5 +89,8 @@ fn poll_usb() { #[interrupt] fn USB() { + // Note: USB is the name of the interrupt, you can not attach the #[interrupt] tag to poll_usb. + // Although you could add the contents of poll_usb into this function, separating them allows + // you to add more functions to run on the USB interrupt in the future. poll_usb(); } From 7a33bd664d0e9f5d8775a95020d48033a410a641 Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Thu, 7 Oct 2021 11:34:22 +0100 Subject: [PATCH 13/22] Remove unused dep nb --- boards/neo_trinkey/Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/boards/neo_trinkey/Cargo.toml b/boards/neo_trinkey/Cargo.toml index 68a2a4caa09b..634765c9a567 100644 --- a/boards/neo_trinkey/Cargo.toml +++ b/boards/neo_trinkey/Cargo.toml @@ -12,7 +12,6 @@ edition = "2018" [dependencies] cortex-m = "0.6" embedded-hal = "0.2.3" -nb = "0.1" [dependencies.cortex-m-rt] version = "0.6.12" From bb550e71f62b1a9520a83eec839a81ff0f7d1272 Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Thu, 7 Oct 2021 11:46:45 +0100 Subject: [PATCH 14/22] tidy imports --- boards/neo_trinkey/examples/blinky_basic.rs | 3 ++- boards/neo_trinkey/examples/blinky_rainbow.rs | 3 ++- boards/neo_trinkey/examples/usb_ack.rs | 6 +++--- boards/neo_trinkey/examples/usb_echo.rs | 6 +++--- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/boards/neo_trinkey/examples/blinky_basic.rs b/boards/neo_trinkey/examples/blinky_basic.rs index 935c3e3c28fd..1aa804de6ed3 100644 --- a/boards/neo_trinkey/examples/blinky_basic.rs +++ b/boards/neo_trinkey/examples/blinky_basic.rs @@ -1,11 +1,12 @@ #![no_std] #![no_main] +use panic_halt as _; use neo_trinkey as bsp; -use panic_halt as _; use bsp::entry; use bsp::hal; + use hal::clock::GenericClockController; use hal::delay::Delay; use hal::pac::{CorePeripherals, Peripherals}; diff --git a/boards/neo_trinkey/examples/blinky_rainbow.rs b/boards/neo_trinkey/examples/blinky_rainbow.rs index 03e2b3db13e1..66bc312ce202 100644 --- a/boards/neo_trinkey/examples/blinky_rainbow.rs +++ b/boards/neo_trinkey/examples/blinky_rainbow.rs @@ -1,11 +1,12 @@ #![no_std] #![no_main] +use panic_halt as _; use neo_trinkey as bsp; -use panic_halt as _; use bsp::entry; use bsp::hal; + use hal::clock::GenericClockController; use hal::delay::Delay; use hal::pac::{CorePeripherals, Peripherals}; diff --git a/boards/neo_trinkey/examples/usb_ack.rs b/boards/neo_trinkey/examples/usb_ack.rs index 4feb61783858..091a7e946020 100644 --- a/boards/neo_trinkey/examples/usb_ack.rs +++ b/boards/neo_trinkey/examples/usb_ack.rs @@ -1,6 +1,5 @@ #![no_std] #![no_main] - use panic_halt as _; use cortex_m::asm::delay as cycle_delay; @@ -9,11 +8,12 @@ use usb_device::bus::UsbBusAllocator; use usb_device::prelude::*; use usbd_serial::{SerialPort, USB_CLASS_CDC}; -use bsp::hal; -use bsp::pac; use neo_trinkey as bsp; use bsp::entry; +use bsp::hal; +use bsp::pac; + use hal::clock::GenericClockController; use hal::usb::UsbBus; use pac::{interrupt, CorePeripherals, Peripherals}; diff --git a/boards/neo_trinkey/examples/usb_echo.rs b/boards/neo_trinkey/examples/usb_echo.rs index cc4d330535c6..96f14bdfd6e7 100644 --- a/boards/neo_trinkey/examples/usb_echo.rs +++ b/boards/neo_trinkey/examples/usb_echo.rs @@ -1,6 +1,5 @@ #![no_std] #![no_main] - use panic_halt as _; use cortex_m::asm::delay as cycle_delay; @@ -9,11 +8,12 @@ use usb_device::bus::UsbBusAllocator; use usb_device::prelude::*; use usbd_serial::{SerialPort, USB_CLASS_CDC}; -use bsp::hal; -use bsp::pac; use neo_trinkey as bsp; use bsp::entry; +use bsp::hal; +use bsp::pac; + use hal::clock::GenericClockController; use hal::usb::UsbBus; use pac::{interrupt, CorePeripherals, Peripherals}; From a046e2ed81854d17e1d56e43fa6fbd3fdc86fc9c Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Thu, 7 Oct 2021 11:46:57 +0100 Subject: [PATCH 15/22] Add documentation for usb_allocator --- boards/neo_trinkey/src/lib.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/boards/neo_trinkey/src/lib.rs b/boards/neo_trinkey/src/lib.rs index 069d826f5aae..e886b2575c34 100644 --- a/boards/neo_trinkey/src/lib.rs +++ b/boards/neo_trinkey/src/lib.rs @@ -47,6 +47,32 @@ hal::bsp_pins!( } ); +/// Convenience method for getting the USB Bus Allocator. +/// +/// Basic usage would look like the below: +/// ```no_run +/// use neo_trinkey::hal::clock::GenericClockController; +/// use neo_trinkey::pac::Peripherals; +/// +/// let mut peripherals = Peripherals::take().unwrap(); +/// let mut clocks = GenericClockController::with_internal_32kosc( +/// peripherals.GCLK, +/// &mut peripherals.PM, +/// &mut peripherals.SYSCTRL, +/// &mut peripherals.NVMCTRL, +/// ); +/// let pins = bsp::Pins::new(peripherals.PORT); +/// +/// let bus_allocator = bsp::usb_allocator( +/// peripherals.USB, +/// &mut clocks, +/// &mut peripherals.PM, +/// pins.usb_dm, +/// pins.usb_dp, +/// ); +/// ``` +/// However to take advantage of USB interrupts you will need, to do some unsafe rust. See the USB +/// code examples in the `examples/` directory of the project. #[cfg(feature = "usb")] pub fn usb_allocator( usb: pac::USB, From 1356742b6717b1841f49de24594a9212e6769c89 Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Thu, 7 Oct 2021 11:51:50 +0100 Subject: [PATCH 16/22] Add CHANGELOG.md --- boards/neo_trinkey/CHANGELOG.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 boards/neo_trinkey/CHANGELOG.md diff --git a/boards/neo_trinkey/CHANGELOG.md b/boards/neo_trinkey/CHANGELOG.md new file mode 100644 index 000000000000..e198cd799730 --- /dev/null +++ b/boards/neo_trinkey/CHANGELOG.md @@ -0,0 +1,19 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres +to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +## [0.1.0] - 2021-10-07 + +### Added +- pin map +- usb_allocator helper function +- Examples: + - blinky_basic + - blinky_rainbow + - usb_ack + - usb_echo From e08603025f537243367bf28de06200438b9504b2 Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Thu, 7 Oct 2021 15:10:43 +0100 Subject: [PATCH 17/22] depend on published atsamd-hal package for better stability also simplified dependencies to make them more readable --- boards/neo_trinkey/Cargo.toml | 37 +++++++---------------------------- crates.json | 1 + 2 files changed, 8 insertions(+), 30 deletions(-) diff --git a/boards/neo_trinkey/Cargo.toml b/boards/neo_trinkey/Cargo.toml index 634765c9a567..453b42a19042 100644 --- a/boards/neo_trinkey/Cargo.toml +++ b/boards/neo_trinkey/Cargo.toml @@ -12,36 +12,13 @@ edition = "2018" [dependencies] cortex-m = "0.6" embedded-hal = "0.2.3" - -[dependencies.cortex-m-rt] -version = "0.6.12" -optional = true - -[dependencies.atsamd-hal] -path = "../../hal" -version = "0.12" -default-features = false - -[dependencies.usb-device] -version = "0.2" -optional = true - -[dependencies.usbd-serial] -version = "0.1" -optional = true - -[dependencies.panic-halt] -version = "0.2" -optional = true - -[dependencies.ws2812-timer-delay] -version = "0.3.0" -features = ["slow"] -optional = true - -[dependencies.smart-leds] -version = "0.3.0" -optional = true +cortex-m-rt = { version = "0.6.12", optional = true } +atsamd-hal = { version = "0.13", default-features = false } +usb-device = { version = "0.2", optional = true } +usbd-serial = { version = "0.1", optional = true } +panic-halt = { version = "0.2", optional = true } +smart-leds = { version = "0.3.0", optional = true } +ws2812-timer-delay = { version = "0.3.0", features = ["slow"], optional = true } [dev-dependencies] panic-halt = "0.2" diff --git a/crates.json b/crates.json index 52309e1433c9..9efef8326ce4 100644 --- a/crates.json +++ b/crates.json @@ -61,6 +61,7 @@ "build": "cargo build --examples --features=unproven,usb" }, "neo_trinkey": { + "tier": 2, "build": "cargo build --examples --features=leds,unproven,usb" }, "p1am_100": { From d4e3c55b00af095c80e932d5bdbadc19d0e14630 Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Thu, 7 Oct 2021 15:21:50 +0100 Subject: [PATCH 18/22] remove panic halt from dependencies and features --- boards/neo_trinkey/Cargo.toml | 2 -- 1 file changed, 2 deletions(-) diff --git a/boards/neo_trinkey/Cargo.toml b/boards/neo_trinkey/Cargo.toml index 453b42a19042..94256cb0cf5b 100644 --- a/boards/neo_trinkey/Cargo.toml +++ b/boards/neo_trinkey/Cargo.toml @@ -16,7 +16,6 @@ cortex-m-rt = { version = "0.6.12", optional = true } atsamd-hal = { version = "0.13", default-features = false } usb-device = { version = "0.2", optional = true } usbd-serial = { version = "0.1", optional = true } -panic-halt = { version = "0.2", optional = true } smart-leds = { version = "0.3.0", optional = true } ws2812-timer-delay = { version = "0.3.0", features = ["slow"], optional = true } @@ -27,7 +26,6 @@ panic-halt = "0.2" # ask the HAL to enable atsamd21e support default = ["rt", "atsamd-hal/samd21e"] leds = ["ws2812-timer-delay", "smart-leds"] -panic_halt = ["panic-halt"] rt = ["cortex-m-rt", "atsamd-hal/samd21e-rt"] unproven = ["atsamd-hal/unproven"] use_semihosting = [] From cd10f78b40f539a770453da1e3387439e62f4814 Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Thu, 7 Oct 2021 15:22:13 +0100 Subject: [PATCH 19/22] restore atsamd-hal block dependency for CI --- boards/neo_trinkey/Cargo.toml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/boards/neo_trinkey/Cargo.toml b/boards/neo_trinkey/Cargo.toml index 94256cb0cf5b..6ca7c0249c55 100644 --- a/boards/neo_trinkey/Cargo.toml +++ b/boards/neo_trinkey/Cargo.toml @@ -13,12 +13,16 @@ edition = "2018" cortex-m = "0.6" embedded-hal = "0.2.3" cortex-m-rt = { version = "0.6.12", optional = true } -atsamd-hal = { version = "0.13", default-features = false } usb-device = { version = "0.2", optional = true } usbd-serial = { version = "0.1", optional = true } smart-leds = { version = "0.3.0", optional = true } ws2812-timer-delay = { version = "0.3.0", features = ["slow"], optional = true } +[dependencies.atsamd-hal] +path = "../../hal" +version = "0.12" +default-features = false + [dev-dependencies] panic-halt = "0.2" From 283731272179685a11a5c583e7abfe5d412a305e Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Thu, 7 Oct 2021 15:29:30 +0100 Subject: [PATCH 20/22] fix git mess up, atsamd-hal package location and version --- boards/neo_trinkey/Cargo.toml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/boards/neo_trinkey/Cargo.toml b/boards/neo_trinkey/Cargo.toml index 6ca7c0249c55..0afad72929f8 100644 --- a/boards/neo_trinkey/Cargo.toml +++ b/boards/neo_trinkey/Cargo.toml @@ -19,8 +19,7 @@ smart-leds = { version = "0.3.0", optional = true } ws2812-timer-delay = { version = "0.3.0", features = ["slow"], optional = true } [dependencies.atsamd-hal] -path = "../../hal" -version = "0.12" +version = "0.13" default-features = false [dev-dependencies] From b8218f0b7d34949a16081908afd34d3be7126c5a Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Thu, 7 Oct 2021 16:31:25 +0100 Subject: [PATCH 21/22] remove echo example, simplify README.md --- boards/neo_trinkey/CHANGELOG.md | 1 - boards/neo_trinkey/Cargo.toml | 4 -- boards/neo_trinkey/README.md | 48 +++++-------- boards/neo_trinkey/examples/usb_ack.rs | 2 +- boards/neo_trinkey/examples/usb_echo.rs | 96 ------------------------- 5 files changed, 18 insertions(+), 133 deletions(-) delete mode 100644 boards/neo_trinkey/examples/usb_echo.rs diff --git a/boards/neo_trinkey/CHANGELOG.md b/boards/neo_trinkey/CHANGELOG.md index e198cd799730..b31e7dd450f7 100644 --- a/boards/neo_trinkey/CHANGELOG.md +++ b/boards/neo_trinkey/CHANGELOG.md @@ -16,4 +16,3 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - blinky_basic - blinky_rainbow - usb_ack - - usb_echo diff --git a/boards/neo_trinkey/Cargo.toml b/boards/neo_trinkey/Cargo.toml index 0afad72929f8..f3205c92470f 100644 --- a/boards/neo_trinkey/Cargo.toml +++ b/boards/neo_trinkey/Cargo.toml @@ -45,7 +45,3 @@ required-features = ["leds"] [[example]] name = "usb_ack" required-features = ["usb"] - -[[example]] -name = "usb_echo" -required-features = ["usb"] diff --git a/boards/neo_trinkey/README.md b/boards/neo_trinkey/README.md index 06cd29c28236..a91d7965b62e 100644 --- a/boards/neo_trinkey/README.md +++ b/boards/neo_trinkey/README.md @@ -16,10 +16,8 @@ https://github.com/atsamd-rs/atsamd/tree/master/boards/neo_trinkey/examples * Put your device in bootloader mode usually by hitting the reset button twice. * Build and upload in one step ```bash -$ cargo hf2 --release --example --features --pid 0x00ef --vid 0x239a +$ cargo hf2 --release --example --features ``` -> **Note:** If you have a newer version of cargo-hf2 you won't need the pid and vid, it will automatically recognise the -> device You should see the following output ```text @@ -28,15 +26,23 @@ Trying Ok(Some("Adafruit Industries")) Ok(Some("NeoPixel Trinkey M0")) Flashing "/Users/danielmason/projects/rust/atsamd/boards/neo_trinkey/target/thumbv6m-none-eabi/release/examples/blinky_basic" Finished in 0.051s ``` -Note: If hf2 can not find your Neo Trinkey, you should check the Product ID (pid) and Vendor ID (vid) in your system -settings. +Note: If hf2 can not find your Neo Trinkey, check that you have the latest version of cargo-hf2. + +If it still doesn't work you can add the Product ID (pid) and Vendor ID (vid) which are usually `0x00ef` and `0x239a` +respectively. + +```bash +$ cargo hf2 --release --example --features --pid 0x00ef --vid 0x239a +``` + +If this _still_ doesn't work, check the USB device in your system settings in case your pid and vid are different. ## Examples ### Blinky basic ```bash -$ cargo hf2 --release --example blinky_basic --features leds --pid 0x00ef --vid 0x239a +$ cargo hf2 --release --example blinky_basic --features leds ``` Once the Neo Trinkey has restarted, you will see the 4 leds flash in unison. Each led will be a different color (pink, @@ -47,22 +53,22 @@ cyan, yellow and white). ### Blinky rainbow ```bash -$ cargo hf2 --release --example blinky_rainbow --features leds --pid 0x00ef --vid 0x239a +$ cargo hf2 --release --example blinky_rainbow --features leds ``` A slightly more satisfying version of blinky where the lights will cycle through the color spectrum. **Warning** even though the lights are turned down very low, they are still very bright. -### USB echo +### USB ack ```bash -$ cargo hf2 --release --example usb_echo --features usb --pid 0x00ef --vid 0x239a +$ cargo hf2 --release --example usb_ack --features usb ``` Once the device has reset, all the lights will be off. You will then need to find the USB device on your machine. -Usually this is located in `/dev/cu.usbmodemTRINKEY_ECHO1`. though if you have multiple trinkeys plugged in and running +Usually this is located in `/dev/cu.usbmodemTRINKEY_ACK1`. though if you have multiple trinkeys plugged in and running this example, the number at the end may change. You can then send the USB device bytes. Each time the device receives data, it will respond with "Received: X" where X @@ -74,27 +80,6 @@ Connect to the device like this (9600 is the baud rate) $ screen /dev/cu.usbmodemTRINKEY_ECHO1 9600 ``` -You can now type, and the characters you type will appear on screen, but the magic here is that what's actually -happening is your key presses are being sent to the device, and the device is responding with the same data which is -what you see appearing on the screen. - -To quit screen, use `ctrl-a` followed by `crtl-\` then `y` - -### USB ack - -```bash -$ cargo hf2 --release --example usb_ack --features usb --pid 0x00ef --vid 0x239a -``` - -This behaves similarly to the USB echo example above except that each time the device receives data, it will respond -with "Received: X" where X is the data that it received. This makes what's happening a little clearer. - -Connect to the device like this (9600 is the baud rate) - -```bash -$ screen /dev/cu.usbmodemTRINKEY_ECHO1 9600 -``` - You can then press keys and you should get a response Eg: ```text @@ -112,3 +97,4 @@ Received: d ``` To quit screen, use `ctrl-a` followed by `crtl-\` then `y` + diff --git a/boards/neo_trinkey/examples/usb_ack.rs b/boards/neo_trinkey/examples/usb_ack.rs index 091a7e946020..80c84a26f76b 100644 --- a/boards/neo_trinkey/examples/usb_ack.rs +++ b/boards/neo_trinkey/examples/usb_ack.rs @@ -47,7 +47,7 @@ fn main() -> ! { UsbDeviceBuilder::new(&bus_allocator, UsbVidPid(0x16c0, 0x27dd)) .manufacturer("Fake company") .product("Serial port") - .serial_number("TRINKEY_ECHO") + .serial_number("TRINKEY_ACK") .device_class(USB_CLASS_CDC) .build(), ); diff --git a/boards/neo_trinkey/examples/usb_echo.rs b/boards/neo_trinkey/examples/usb_echo.rs deleted file mode 100644 index 96f14bdfd6e7..000000000000 --- a/boards/neo_trinkey/examples/usb_echo.rs +++ /dev/null @@ -1,96 +0,0 @@ -#![no_std] -#![no_main] -use panic_halt as _; - -use cortex_m::asm::delay as cycle_delay; -use cortex_m::peripheral::NVIC; -use usb_device::bus::UsbBusAllocator; -use usb_device::prelude::*; -use usbd_serial::{SerialPort, USB_CLASS_CDC}; - -use neo_trinkey as bsp; - -use bsp::entry; -use bsp::hal; -use bsp::pac; - -use hal::clock::GenericClockController; -use hal::usb::UsbBus; -use pac::{interrupt, CorePeripherals, Peripherals}; - -#[entry] -fn main() -> ! { - let mut peripherals = Peripherals::take().unwrap(); - let mut core = CorePeripherals::take().unwrap(); - let mut clocks = GenericClockController::with_internal_32kosc( - peripherals.GCLK, - &mut peripherals.PM, - &mut peripherals.SYSCTRL, - &mut peripherals.NVMCTRL, - ); - let pins = bsp::Pins::new(peripherals.PORT); - - let bus_allocator = unsafe { - USB_ALLOCATOR = Some(bsp::usb_allocator( - peripherals.USB, - &mut clocks, - &mut peripherals.PM, - pins.usb_dm, - pins.usb_dp, - )); - USB_ALLOCATOR.as_ref().unwrap() - }; - - unsafe { - USB_SERIAL = Some(SerialPort::new(&bus_allocator)); - USB_BUS = Some( - UsbDeviceBuilder::new(&bus_allocator, UsbVidPid(0x16c0, 0x27dd)) - .manufacturer("Fake company") - .product("Serial port") - .serial_number("TRINKEY_ECHO") - .device_class(USB_CLASS_CDC) - .build(), - ); - } - - unsafe { - core.NVIC.set_priority(interrupt::USB, 1); - NVIC::unmask(interrupt::USB); - } - - loop { - cycle_delay(15 * 1024 * 1024); - } -} - -static mut USB_ALLOCATOR: Option> = None; -static mut USB_BUS: Option> = None; -static mut USB_SERIAL: Option> = None; - -fn poll_usb() { - unsafe { - USB_BUS.as_mut().map(|usb_dev| { - USB_SERIAL.as_mut().map(|serial| { - usb_dev.poll(&mut [serial]); - let mut buf = [0u8; 64]; - - if let Ok(count) = serial.read(&mut buf) { - for (i, c) in buf.iter().enumerate() { - if i >= count { - break; - } - serial.write(&[c.clone()]).ok(); - } - }; - }); - }); - }; -} - -#[interrupt] -fn USB() { - // Note: USB is the name of the interrupt, you can not attach the #[interrupt] tag to poll_usb. - // Although you could add the contents of poll_usb into this function, separating them allows - // you to add more functions to run on the USB interrupt in the future. - poll_usb(); -} From 202d6c9858ed88cf416acede9b8d2bfbe5edafef Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Fri, 8 Oct 2021 07:53:30 +0100 Subject: [PATCH 22/22] fix comment fmt --- boards/neo_trinkey/examples/usb_ack.rs | 7 ++++--- boards/neo_trinkey/src/lib.rs | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/boards/neo_trinkey/examples/usb_ack.rs b/boards/neo_trinkey/examples/usb_ack.rs index 80c84a26f76b..2427ee577b6d 100644 --- a/boards/neo_trinkey/examples/usb_ack.rs +++ b/boards/neo_trinkey/examples/usb_ack.rs @@ -91,8 +91,9 @@ fn poll_usb() { #[interrupt] fn USB() { - // Note: USB is the name of the interrupt, you can not attach the #[interrupt] tag to poll_usb. - // Although you could add the contents of poll_usb into this function, separating them allows - // you to add more functions to run on the USB interrupt in the future. + // Note: USB is the name of the interrupt, you can not attach the #[interrupt] + // tag to poll_usb. Although you could add the contents of poll_usb into + // this function, separating them allows you to add more functions to run on + // the USB interrupt in the future. poll_usb(); } diff --git a/boards/neo_trinkey/src/lib.rs b/boards/neo_trinkey/src/lib.rs index e886b2575c34..025f51302b7a 100644 --- a/boards/neo_trinkey/src/lib.rs +++ b/boards/neo_trinkey/src/lib.rs @@ -71,8 +71,8 @@ hal::bsp_pins!( /// pins.usb_dp, /// ); /// ``` -/// However to take advantage of USB interrupts you will need, to do some unsafe rust. See the USB -/// code examples in the `examples/` directory of the project. +/// However to take advantage of USB interrupts you will need, to do some unsafe +/// rust. See the USB code examples in the `examples/` directory of the project. #[cfg(feature = "usb")] pub fn usb_allocator( usb: pac::USB,