diff --git a/hello_gradient/Cargo.lock b/hello_gradient/Cargo.lock index 5c7366e..97a1527 100644 --- a/hello_gradient/Cargo.lock +++ b/hello_gradient/Cargo.lock @@ -10,7 +10,7 @@ name = "atsamd-hal" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "atsamd21e18a 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "atsamd21g18a 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "bitfield 0.13.1 (registry+https://github.com/rust-lang/crates.io-index)", "cortex-m 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", "embedded-hal 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -21,7 +21,7 @@ dependencies = [ ] [[package]] -name = "atsamd21e18a" +name = "atsamd21g18a" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ @@ -84,7 +84,15 @@ dependencies = [ ] [[package]] -name = "gemma_m0" +name = "hello_gradient" +version = "0.1.0" +dependencies = [ + "itsybitsy_m0_lights 0.1.0", + "lights 0.1.0", +] + +[[package]] +name = "itsybitsy_m0" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ @@ -96,24 +104,16 @@ dependencies = [ ] [[package]] -name = "gemma_m0_lights" +name = "itsybitsy_m0_lights" version = "0.1.0" dependencies = [ "cortex-m 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", "embedded-hal 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "gemma_m0 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "itsybitsy_m0 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "lights 0.1.0", "panic-halt 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "hello_gradient" -version = "0.1.0" -dependencies = [ - "gemma_m0_lights 0.1.0", - "lights 0.1.0", -] - [[package]] name = "lights" version = "0.1.0" @@ -257,14 +257,14 @@ dependencies = [ [metadata] "checksum aligned 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d39da9b88ae1a81c03c9c082b8db83f1d0e93914126041962af61034ab44c4a5" "checksum atsamd-hal 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e0f224a1a946c4e817e87ed76d2dd48089bf545d35d4f30e83d579a87216681c" -"checksum atsamd21e18a 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "139787a30d118dd09a2e9570f5404cf582b70afe9b53dd1194239f9be382b897" +"checksum atsamd21g18a 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9f88795edced90ee7cf401714e60c3b7878d9fe9025059302e57a3b757f45e51" "checksum bare-metal 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a3caf393d93b2d453e80638d0674597020cef3382ada454faacd43d1a55a735a" "checksum bitfield 0.13.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a260ed6b9f3ca16a4389390b1b1cd15a3bc0a9d3e63b1ef39f4978cec58a4e83" "checksum cortex-m 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)" = "dab2164a0fc216781a47fc343347365112ae6917421d3fa4bac6faf0fbaaaec7" "checksum cortex-m-rt 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f69d2beca37acc3776c17201c9d1f8904fb9139fa3a4d2cf28c8436a07b21a88" "checksum cortex-m-rt-macros 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d7ae692573e0acccb1579fef1abf5a5bf1d2f3f0149a22b16870ec9309aee25f" "checksum embedded-hal 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9880e55238830314d41d88f1ac7a819d495799c3cc3bc392cc172bab26428c33" -"checksum gemma_m0 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5e0b6ce273c7aee0101ad2ca75b5459b7498d6a09e241de34aaf51b7f1abe222" +"checksum itsybitsy_m0 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c457960b339e016d68b64f928b7b2ea064574543573963642ee02b2039382d15" "checksum nb 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "69f380b5fe9fab8c0d7a6a99cda23e2cc0463bedb2cbc3aada0813b98496ecdc" "checksum panic-halt 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "de96540e0ebde571dc55c73d60ef407c653844e6f9a1e2fdbd40c07b9252d812" "checksum paste 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f50392d1265092fbee9273414cc40eb6d47d307bd66222c477bb8450c8504f9d" diff --git a/hello_gradient/Cargo.toml b/hello_gradient/Cargo.toml index 3210e8f..bbd0d1f 100644 --- a/hello_gradient/Cargo.toml +++ b/hello_gradient/Cargo.toml @@ -11,4 +11,4 @@ debug = true [dependencies] lights = { path = "../lights" } -lights_hal = { path = "../gemma_m0_lights", package = "gemma_m0_lights" } +lights_hal = { path = "../itsybitsy_m0_lights", package = "itsybitsy_m0_lights" } diff --git a/itsybitsy_m0_lights/Cargo.toml b/itsybitsy_m0_lights/Cargo.toml new file mode 100644 index 0000000..8a2e340 --- /dev/null +++ b/itsybitsy_m0_lights/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "itsybitsy_m0_lights" +version = "0.1.0" +authors = ["Tangent 128 "] +edition = "2018" + +[dependencies] +cortex-m = { version = "0.5.8", features = ["inline-asm"] } +embedded-hal = "0.2.2" +itsybitsy_m0 = "0.2.1" +lights = { path = "../lights" } +panic-halt = "0.2" diff --git a/itsybitsy_m0_lights/src/lib.rs b/itsybitsy_m0_lights/src/lib.rs new file mode 100644 index 0000000..f9292ca --- /dev/null +++ b/itsybitsy_m0_lights/src/lib.rs @@ -0,0 +1,83 @@ +#![no_std] + +extern crate cortex_m; +extern crate embedded_hal; +extern crate itsybitsy_m0 as hal; +extern crate panic_halt; + +pub use cortex_m::asm::delay; +pub use hal::entry; + +use core::u8; +use embedded_hal::digital::OutputPin; +use hal::{ + clock::GenericClockController, + Peripherals +}; +use lights::{ + HardwareRgb, + Lights +}; + +pub fn boot() -> impl Lights { + let mut peripherals = Peripherals::take().unwrap(); + let _clock = GenericClockController::with_internal_32kosc( + peripherals.GCLK, + &mut peripherals.PM, + &mut peripherals.SYSCTRL, + &mut peripherals.NVMCTRL + ); + + let mut pins = hal::Pins::new(peripherals.PORT); + + NeopixelLights { + out: pins.d7.into_push_pull_output(&mut pins.port) + } +} + +// approx. 20ns per clock cycle; +const ZERO_HIGH_CYCLES: u32 = 11; // smidge above 200ns +const ONE_HIGH_CYCLES: u32 = 35; // about 700ns +const DATA_LOW_CYCLES: u32 = 25; // about 500ns +const LATCH_CYCLES: u32 = 300; // about 6000ns + +pub struct NeopixelLights { + out: T +} + +impl NeopixelLights { + #[inline] + fn write(&mut self, bit: bool) { + self.out.set_high(); + delay(if bit { ONE_HIGH_CYCLES } else { ZERO_HIGH_CYCLES }); + self.out.set_low(); + delay(DATA_LOW_CYCLES); + } + + #[inline] + fn byte(&mut self, byte: u8) { + self.write(byte & 0x80 != 0); + self.write(byte & 0x40 != 0); + self.write(byte & 0x20 != 0); + self.write(byte & 0x10 != 0); + self.write(byte & 0x08 != 0); + self.write(byte & 0x04 != 0); + self.write(byte & 0x02 != 0); + self.write(byte & 0x01 != 0); + } +} + +impl Lights for NeopixelLights { + type Pixel = HardwareRgb; + + #[inline] + fn render(&mut self, rgb: &HardwareRgb) { + // GRB pixel order + self.byte(rgb.1); + self.byte(rgb.0); + self.byte(rgb.2); + } + fn latch(&mut self) { + delay(LATCH_CYCLES); + } +}