Skip to content

Commit

Permalink
Merge pull request jamesmunns#6 from jamesmunns/sys
Browse files Browse the repository at this point in the history
Many crates
  • Loading branch information
jamesmunns authored Sep 18, 2016
2 parents b1641db + 667f940 commit 22f4753
Show file tree
Hide file tree
Showing 141 changed files with 79 additions and 65 deletions.
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
[package]
name = "teensy3"
name = "teensy3-demo"
version = "0.1.0"
authors = ["Simon Sapin <[email protected]>", "James Munns <[email protected]"]
build = "build.rs"

[dependencies]
teensy3 = { path = "teensy3" }

[profile.dev]
panic = "abort"
Expand Down
12 changes: 5 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
SERIAL_DEVICE = /dev/ttyACM0

hex: rust
@make --no-print-directory -C teensy3 NO_ARDUINO=1

flash: hex
teensy-loader-cli -w -s --mcu=mk20dx256 teensy3/main.hex

rust:
build:
@xargo build --release

flash: build
arm-none-eabi-objcopy -O ihex -R .eeprom target/thumbv7em-none-eabi/release/teensy3-demo target/hex
teensy-loader-cli -w -s --mcu=mk20dx256 target/hex

bindgen:
PATH="/home/simon/projects/servo/ports/geckolib/binding_tools/rust-bindgen/target/debug:$$PATH" \
bindgen --no-type-renaming --match teensy3 bindings.h -o src/bindings.rs -- \
Expand Down
42 changes: 8 additions & 34 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,20 @@
#![no_std]
#![no_main]

#[allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals, improper_ctypes)]
#[path = "bindings.rs"]
mod teensy3;

#[macro_use]
mod serial;
use serial::Serial;
extern crate teensy3;

use teensy3::bindings;

#[no_mangle]
pub unsafe extern fn main() {
// Blink Loop
teensy3::pinMode(13, teensy3::OUTPUT as u8);
bindings::pinMode(13, bindings::OUTPUT as u8);
loop {
teensy3::digitalWrite(13, teensy3::LOW as u8);
teensy3::delay(500);
teensy3::digitalWrite(13, teensy3::HIGH as u8);
teensy3::delay(200);
}
}

fn read_int(delimiter: u8) -> u32 {
Serial.try_read_int_until(delimiter).unwrap()
}

mod std {
pub use core::*;
pub mod os {
#[allow(non_camel_case_types)]
pub mod raw {
pub enum c_void {}
pub type c_uchar = u8;
pub type c_short = i16;
pub type c_ushort = u16;
pub type c_int = i32;
pub type c_uint = u32;
pub type c_long = i32;
pub type c_ulong = u32;
pub type c_longlong = i64;
pub type c_ulonglong = u64;
}
bindings::digitalWrite(13, bindings::LOW as u8);
bindings::delay(500);
bindings::digitalWrite(13, bindings::HIGH as u8);
bindings::delay(200);
}
}

Expand Down
8 changes: 8 additions & 0 deletions teensy3-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "teensy3-sys"
version = "0.1.0"
authors = ["Paul Stoffregen ", "Simon Sapin <[email protected]>", "James Munns <[email protected]>"]
build = "build.rs"

[lib]
path = "lib.rs"
File renamed without changes.
File renamed without changes.
6 changes: 2 additions & 4 deletions build.rs → teensy3-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ fn main() {

// TODO(@jamesmunns): How do I pipe stdout here so we dont just silently wait?
assert!(Command::new("make")
.args(&["--no-print-directory", "-C", "teensy3", "NO_ARDUINO=1"])
.args(&["--no-print-directory", "-C", "teensy3-core", "NO_ARDUINO=1"])
.status()
.expect("failed to build Teensyduino libs")
.success());

println!("cargo:rustc-link-search={}/teensy3", env!("CARGO_MANIFEST_DIR"));
println!("cargo:rustc-link-search={}/teensy3-core", env!("CARGO_MANIFEST_DIR"));
println!("cargo:libdir=teensy");
println!("cargo:rustc-link-lib=static=teensyduino");

println!("It worked?");
}
23 changes: 23 additions & 0 deletions teensy3-sys/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#![no_std]
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals, improper_ctypes)]

include!("bindings.rs");

mod std {
pub use core::*;
pub mod os {
#[allow(non_camel_case_types)]
pub mod raw {
pub enum c_void {}
pub type c_uchar = u8;
pub type c_short = i16;
pub type c_ushort = u16;
pub type c_int = i32;
pub type c_uint = u32;
pub type c_long = i32;
pub type c_ulong = u32;
pub type c_longlong = i64;
pub type c_ulonglong = u64;
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions teensy3/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "teensy3"
version = "0.1.0"
authors = ["Simon Sapin <[email protected]>", "James Munns <[email protected]>"]

[dependencies]
teensy3-sys = { path = "../teensy3-sys" }
3 changes: 3 additions & 0 deletions teensy3/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#![no_std]
pub extern crate teensy3_sys as bindings;
pub mod serial;
21 changes: 4 additions & 17 deletions src/serial.rs → teensy3/src/serial.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use teensy3;
use bindings;
use core::fmt;


Expand All @@ -8,7 +8,7 @@ pub struct Serial;
impl Serial {
pub fn readable(self) -> bool {
unsafe {
teensy3::usb_serial_available() > 0
bindings::usb_serial_available() > 0
}
}

Expand All @@ -17,28 +17,15 @@ impl Serial {
}

pub fn try_read_byte(self) -> Result<u8, &'static str> {
match unsafe { teensy3::usb_serial_getchar() } {
match unsafe { bindings::usb_serial_getchar() } {
-1 => Err("usb_serial_getchar returned -1"),
byte => Ok(byte as u8)
}
}

pub fn try_read_int_until(self, delimiter: u8) -> Result<u32, &'static str> {
let mut result = 0;
loop {
let byte = try!(self.try_read_byte());
if byte == delimiter {
return Ok(result)
}
let digit = try!((byte as char).to_digit(10).ok_or("expected a decimal digit"));
result *= 10;
result += digit;
}
}

pub fn write_bytes(self, bytes: &[u8]) -> Result<(), ()> {
unsafe {
if teensy3::usb_serial_write(bytes.as_ptr() as *const _, bytes.len() as u32) >= 0 {
if bindings::usb_serial_write(bytes.as_ptr() as *const _, bytes.len() as u32) >= 0 {
Ok(())
} else {
Err(())
Expand Down
2 changes: 1 addition & 1 deletion thumbv7em-none-eabi.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"pre-link-args": [
"-mcpu=cortex-m4",
"-mthumb",
"-Tteensy3/mk20dx256.ld",
"-Tteensy3-sys/teensy3-core/mk20dx256.ld",
"-Os",
"-Wl,--gc-sections,--defsym=__rtc_localtime=0",
"--specs=nano.specs"
Expand Down

0 comments on commit 22f4753

Please sign in to comment.