Skip to content

Commit

Permalink
Add Neo Trinkey BSP (#471)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gisleburt authored Oct 9, 2021
1 parent a8e1f72 commit 55b2f0a
Show file tree
Hide file tree
Showing 11 changed files with 519 additions and 0 deletions.
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

0 comments on commit 55b2f0a

Please sign in to comment.