Skip to content

Commit 9b9fdb1

Browse files
Initial commit
0 parents  commit 9b9fdb1

File tree

8 files changed

+149
-0
lines changed

8 files changed

+149
-0
lines changed

.cargo-ok

Whitespace-only changes.

.cargo/config

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[target.thumbv6m-none-eabi]
2+
runner = 'arm-none-eabi-gdb'
3+
rustflags = [
4+
"-C", "link-arg=-Tlink.x",
5+
]
6+
7+
[build]
8+
target = "thumbv6m-none-eabi"

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/target
2+
**/*.rs.bk
3+
Cargo.lock

.travis.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: rust
2+
rust:
3+
- stable
4+
- nightly
5+
cache: cargo
6+
matrix:
7+
allow_failures:
8+
- rust: nightly
9+
fast_finish: true
10+
script:
11+
- rustup target add thumbv6m-none-eabi
12+
- cargo build --examples --release

Cargo.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[package]
2+
name = "ws2812-timer-delay"
3+
version = "0.1.0"
4+
authors = ["David Sawatzke <[email protected]>"]
5+
edition = "2018"
6+
7+
[dependencies]
8+
nb = "0.1.1"
9+
smart-leds-trait = {git = "https://github.com/smart-leds-rs/smart-leds-trait"}
10+
11+
[dependencies.embedded-hal]
12+
features = ["unproven"]
13+
version = "0.2.1"
14+
15+
[dev-dependencies]
16+
bare-metal = "0.2.4"
17+
cortex-m = "0.5.8"
18+
cortex-m-rt = "0.6"
19+
panic-halt = "0.2"
20+
stm32f0xx-hal = {version = "0.11", features = ["stm32f030x4"]}
21+
22+
[profile.dev]
23+
debug = true
24+
25+
[profile.release]
26+
debug = true
27+
lto = true
28+
opt-level = "s"

build.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use std::env;
2+
use std::fs::File;
3+
use std::io::Write;
4+
use std::path::PathBuf;
5+
6+
fn main() {
7+
// Put the linker script somewhere the linker can find it
8+
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
9+
File::create(out.join("memory.x"))
10+
.unwrap()
11+
.write_all(include_bytes!("memory.x"))
12+
.unwrap();
13+
println!("cargo:rustc-link-search={}", out.display());
14+
15+
// Only re-run the build script when memory.x is changed,
16+
// instead of when any part of the source code changes.
17+
println!("cargo:rerun-if-changed=memory.x");
18+
}

memory.x

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
MEMORY
2+
{
3+
/* NOTE K = KiBi = 1024 bytes */
4+
FLASH : ORIGIN = 0x08000000, LENGTH = 16K
5+
RAM : ORIGIN = 0x20000000, LENGTH = 4K
6+
}
7+
8+
/* This is where the call stack will be allocated. */
9+
/* The stack is of the full descending type. */
10+
/* NOTE Do NOT modify `_stack_start` unless you know what you are doing */
11+
_stack_start = ORIGIN(RAM) + LENGTH(RAM);

src/lib.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#![no_std]
2+
3+
use embedded_hal as hal;
4+
5+
use crate::hal::digital::OutputPin;
6+
use crate::hal::timer::{CountDown, Periodic};
7+
use smart_leds_trait::{Color, SmartLedsWrite};
8+
9+
use nb;
10+
use nb::block;
11+
12+
pub struct Ws2812<'a, TIMER, PIN> {
13+
timer: TIMER,
14+
pin: &'a mut PIN,
15+
}
16+
17+
impl<'a, TIMER, PIN> Ws2812<'a, TIMER, PIN>
18+
where
19+
TIMER: CountDown + Periodic,
20+
PIN: OutputPin,
21+
{
22+
/// The timer has to already run at with a frequency of 3 MHz
23+
pub fn new(timer: TIMER, pin: &'a mut PIN) -> Ws2812<'a, TIMER, PIN> {
24+
pin.set_low();
25+
Self { timer, pin }
26+
}
27+
/// Write a single color for ws2812 devices
28+
fn write_color(&mut self, data: Color) {
29+
let mut serial_bits = (data.g as u32) << 16 | (data.r as u32) << 8 | (data.b as u32) << 0;
30+
// Wait until a timer period has gone by, so we have clean timing
31+
block!(self.timer.wait()).ok();
32+
for _ in 0..24 {
33+
self.pin.set_high();
34+
block!(self.timer.wait()).ok();
35+
if (serial_bits & 0x00800000) != 0 {
36+
self.pin.set_high();
37+
} else {
38+
self.pin.set_low();
39+
}
40+
block!(self.timer.wait()).ok();
41+
self.pin.set_low();
42+
serial_bits <<= 1;
43+
block!(self.timer.wait()).ok();
44+
}
45+
}
46+
}
47+
48+
impl<TIMER, PIN> SmartLedsWrite for Ws2812<'_, TIMER, PIN>
49+
where
50+
TIMER: CountDown + Periodic,
51+
PIN: OutputPin,
52+
{
53+
type Error = ();
54+
55+
/// Write all the items of an iterator to a ws2812 strip
56+
fn write<T>(&mut self, iterator: T) -> Result<(), ()>
57+
where
58+
T: Iterator<Item = Color>,
59+
{
60+
for item in iterator {
61+
self.write_color(item);
62+
}
63+
// Get a timeout period of 300 ns
64+
for _ in 0..900 {
65+
block!(self.timer.wait()).ok();
66+
}
67+
Ok(())
68+
}
69+
}

0 commit comments

Comments
 (0)