Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Neo Trinkey #471

Merged
merged 26 commits into from
Oct 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
590d5bc
add pins for neo_trinkey
Gisleburt Jul 17, 2021
90d698c
fix copy pasta
Gisleburt Jul 17, 2021
fb5c615
attempting a few different ways to access the neo pixels
Gisleburt Jul 17, 2021
8e569dc
blinky rainbow
Gisleburt Jul 18, 2021
6c00b0b
blinky basic
Gisleburt Jul 18, 2021
cf4c64a
Add usb_echo example and update readme
Gisleburt Jul 31, 2021
fce8bcb
Make usb_echo do what it does in other examples, add new demo to make…
Gisleburt Jul 31, 2021
e390b10
added examples to Cargo.toml
Gisleburt Aug 1, 2021
aae4ef9
Tidy up dependencies and features
Gisleburt Aug 1, 2021
dac292e
fix: usb_ack
Gisleburt Aug 7, 2021
299079f
Merge branch 'master' into neo-trinkey
Gisleburt Sep 4, 2021
1594833
update crates.json
Gisleburt Sep 4, 2021
66d40ec
Merge branch 'master' into neo-trinkey
Gisleburt Oct 4, 2021
d46ee7f
Add comments explaining the weirdness of USB interrupt
Gisleburt Oct 7, 2021
7a33bd6
Remove unused dep nb
Gisleburt Oct 7, 2021
bb550e7
tidy imports
Gisleburt Oct 7, 2021
a046e2e
Add documentation for usb_allocator
Gisleburt Oct 7, 2021
1356742
Add CHANGELOG.md
Gisleburt Oct 7, 2021
9126bf8
Merge branch 'neo-trinkey' of github.com:Gisleburt/atsamd into neo-tr…
Gisleburt Oct 7, 2021
e086030
depend on published atsamd-hal package for better stability
Gisleburt Oct 7, 2021
d4e3c55
remove panic halt from dependencies and features
Gisleburt Oct 7, 2021
cd10f78
restore atsamd-hal block dependency for CI
Gisleburt Oct 7, 2021
2837312
fix git mess up, atsamd-hal package location and version
Gisleburt Oct 7, 2021
b8218f0
remove echo example, simplify README.md
Gisleburt Oct 7, 2021
202d6c9
fix comment fmt
Gisleburt Oct 8, 2021
5984247
Merge branch 'master' into neo-trinkey
bradleyharden Oct 9, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions boards/neo_trinkey/.cargo/config
Original file line number Diff line number Diff line change
@@ -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",
]
18 changes: 18 additions & 0 deletions boards/neo_trinkey/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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
47 changes: 47 additions & 0 deletions boards/neo_trinkey/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[package]
name = "neo_trinkey"
version = "0.1.0"
authors = ["Daniel Mason <[email protected]>"]
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"
cortex-m-rt = { version = "0.6.12", optional = true }
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]
version = "0.13"
default-features = false

[dev-dependencies]
panic-halt = "0.2"

[features]
# ask the HAL to enable atsamd21e support
default = ["rt", "atsamd-hal/samd21e"]
leds = ["ws2812-timer-delay", "smart-leds"]
rt = ["cortex-m-rt", "atsamd-hal/samd21e-rt"]
unproven = ["atsamd-hal/unproven"]
use_semihosting = []
usb = ["atsamd-hal/usb", "usb-device", "usbd-serial"]

[[example]]
name = "blinky_basic"
required-features = ["leds"]

[[example]]
name = "blinky_rainbow"
required-features = ["leds"]

[[example]]
name = "usb_ack"
required-features = ["usb"]
100 changes: 100 additions & 0 deletions boards/neo_trinkey/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# 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).

## 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 <example-name> --features <any-required-features>
```

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, 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 <example-name> --features <any-required-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
```

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 --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 ack

```bash
$ 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_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
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-\` then `y`

16 changes: 16 additions & 0 deletions boards/neo_trinkey/build.rs
Original file line number Diff line number Diff line change
@@ -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");
}
56 changes: 56 additions & 0 deletions boards/neo_trinkey/examples/blinky_basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#![no_std]
#![no_main]
use panic_halt as _;

use neo_trinkey as bsp;

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);
}
}
68 changes: 68 additions & 0 deletions boards/neo_trinkey/examples/blinky_rainbow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#![no_std]
#![no_main]
use panic_halt as _;

use neo_trinkey as bsp;

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()
}
Loading