diff --git a/.cargo/config.toml b/.cargo/config.toml index c39ded0..32388e2 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,5 +1,6 @@ [build] target = "avr-specs/avr-atmega2560.json" +rustc-wrapper = "./rustc-wrapper.sh" [target.'cfg(target_arch = "avr")'] runner = "ravedude mega2560 -cb 57600" diff --git a/Cargo.lock b/Cargo.lock index 7820f4d..a75be05 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -69,6 +69,7 @@ name = "avr-webee" version = "0.1.0" dependencies = [ "arduino-hal", + "avr-device", "embedded-hal", "nb 0.1.3", "panic-halt", @@ -153,9 +154,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.32" +version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50f3b39ccfb720540debaa0164757101c08ecb8d326b15358ce76a62c7e85965" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" dependencies = [ "proc-macro2", ] diff --git a/Cargo.toml b/Cargo.toml index 00edff4..8e89630 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" authors = ["Adrian Marquis "] edition = "2021" license = "MIT OR Apache-2.0" +publish = false [[bin]] name = "avr-webee" @@ -15,6 +16,7 @@ panic-halt = "0.2.0" ufmt = "0.1.0" nb = "0.1.2" embedded-hal = "0.2.3" +avr-device = "0.5.1" [dependencies.arduino-hal] git = "https://github.com/rahix/avr-hal" diff --git a/rustc-wrapper.sh b/rustc-wrapper.sh new file mode 100755 index 0000000..a7f2c54 --- /dev/null +++ b/rustc-wrapper.sh @@ -0,0 +1,37 @@ +#!/bin/sh +# rustc-wrapper +# +# Use this script in the `[build]`,`rustc-wrapper` section of your +# `.cargo/config.toml` configuration file to correctly set the +# `RUST_TARGET_PATH` environment variable before running rustc. +# +# This is to workaround a bug whereby `rustc --print cfg` fails when you +# use a custom `target.json` file, unless `RUST_TARGET_PATH` is set to the +# directory containing the target description. + +# `cargo` passes "rustc " as the first parameter to the wrapper; pop it off +shift + +# Extract the target config JSON from the `config.toml` +# +# NOTE: We assume that this wrapper script is in the root of your project, and +# that the `build.target` value is relative to the location of the same. +# + +TARGET_JSON_FILE=$(cargo config -Z unstable-options get --format json-value build.target 2>/dev/null || echo "") + +if [ ! -z "${TARGET_JSON_FILE}" ]; then + TARGET_JSON_DIR=$(dirname "${TARGET_JSON_FILE//\"/}") + + # Work out where we are and which rustup we're wrapping + BASEDIR=$(dirname "$0") + + + # Set the env variable + export RUST_TARGET_PATH="${BASEDIR}/${TARGET_JSON_DIR}" +fi + +RUSTC=$(rustup which rustc) + +# Now let rustc do its thing +"${RUSTC}" $@ diff --git a/src/main.rs b/src/main.rs index 7a6a7ba..16da565 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,27 +1,123 @@ #![no_std] #![no_main] +mod webee; +use webee::{Webee, Vector}; + +use arduino_hal::Peripherals; +use arduino_hal::prelude::*; use panic_halt as _; + #[arduino_hal::entry] fn main() -> ! { - let dp = arduino_hal::Peripherals::take().unwrap(); + let dp = Peripherals::take().unwrap(); let pins = arduino_hal::pins!(dp); + let status = pins.d13.into_output(); + let mut serial = arduino_hal::default_serial!(dp, pins, 57600); + let rx = pins.d2; - /* - * For examples (and inspiration), head to - * - * https://github.com/Rahix/avr-hal/tree/main/examples - * - * NOTE: Not all examples were ported to all boards! There is a good chance though, that code - * for a different board can be adapted for yours. The Arduino Uno currently has the most - * examples available. - */ + let mut test = Webee::new( + dp.USART1, + pins.d19, + pins.d18.into_output() + ); - let mut led = pins.d13.into_output(); + // let mut webee = Usart::new( + // dp.USART1, + // pins.d19, + // pins.d18.into_output(), + // 38400.into_baudrate(), + // ); + + // ufmt::uwriteln!(&mut serial, "Hello, World!").void_unwrap(); + // ufmt::uwriteln!(&mut serial, "RX ready: {}", rx.is_high()).void_unwrap(); + // delay_ms(1000); + // ufmt::uwriteln!(&mut serial, "RX ready: {}", rx.is_high()).void_unwrap(); + + // let webee = Webee::new(dp.USART1, pins.d19, pins.d18); + + + // let mut test = webee.write(0xFF); + // while let Err(_) = test { + // test = webee.write(0xFF); + // ufmt::uwriteln!(&mut serial, "initializing..").void_unwrap(); + // delay_ms(500); + // } + ufmt::uwriteln!(&mut serial, "Webee initialized!").void_unwrap(); + // delay_ms(1000); + + // let mut init = true; + // while init { + // match webee.write(0) { + // Ok(_) => { + // webee.flush(); + // ufmt::uwriteln!(&mut serial, "Webee initialized!").void_unwrap(); + // init = false; + // } + // Err(_) => { + // ufmt::uwriteln!(&mut serial, "initializing..").void_unwrap(); + // delay_ms(500); + // } + // } + // } + + // ufmt::uwriteln!(&mut serial, "RX ready: {}", rx.is_high()).void_unwrap(); + let role : Vector = test.send(vector!([0x5a, 0xaa, 0xb1])); + ufmt::uwriteln!(&mut serial, "Role: {}", role); + + // for byte in [0x5a, 0xaa, 0xb1].iter() { + // webee.write_byte(*byte); + // ufmt::uwriteln!(&mut serial, "Wrote byte: {:X}", *byte).void_unwrap(); + // } + // + // ufmt::uwrite!(&mut serial, "Role:").void_unwrap(); + // let mut result: u8 = webee.read_byte(); + // + // while result != 0xFF { + // ufmt::uwrite!(&mut serial, " {:02X}", result).void_unwrap(); + // result = webee.read_byte(); + // } loop { - led.toggle(); - arduino_hal::delay_ms(1000); + let b = nb::block!(serial.read()).void_unwrap(); + + ufmt::uwriteln!(&mut serial, "Got {:X}!", b).void_unwrap(); } } + +// fn readBytesUntil() + +// struct Webee { +// serial: Usart, +// } +// +// impl Webee { +// pub fn new(usart: USART, rx: RX, tx: TX) -> Self { +// Webee { +// serial: crate::Usart::new(usart, rx, tx, Baudrate::new(38400)), +// } +// } +// } + + +// struct Webee<'a> { +// serial: &'a usart +// } +// +// impl Webee { +// fn new(serial: usart) -> Self { +// Self { +// serial +// } +// } +// +// fn send_cmd(&mut self, cmd: u8) -> ! { +// let frame: [u8; 3] = [0x55, 0xAA, cmd]; +// self.serial.write(&frame) +// } +// +// fn recv(&mut self) -> ! { +// self.serial.read() +// } +// } diff --git a/src/webee.rs b/src/webee.rs new file mode 100644 index 0000000..4f697d3 --- /dev/null +++ b/src/webee.rs @@ -0,0 +1,44 @@ +use arduino_hal::Usart; +use arduino_hal::usart::UsartOps; +use arduino_hal::prelude::*; +use embedded_hal::serial::{Read}; + +mod vector; +pub use vector::Vector; + +pub struct Webee { + webee: Usart, +} + +enum SEND_CMD { + +} + +impl Webee { + const STOP : u8 = 0xFF; + + pub fn new(device: USART, rx: RX, tx: TX) -> Self { + Self { + webee: Usart::new(device, rx, tx, 38400.into_baudrate()) + } + } + + pub fn send(&mut self, data: Vector) -> Vector { + for byte in data { + self.webee.write_byte(byte); + } + + return self.recv(); + } + + fn recv(&mut self) -> Vector { + const buffer : Vector = Vector::new(0, &[]); + let byte : u8 = self.webee.read_byte(); + + while byte != self.STOP { + buffer.push(byte); + } + + return buffer; + } +} diff --git a/src/webee/vector.rs b/src/webee/vector.rs new file mode 100644 index 0000000..e5aca05 --- /dev/null +++ b/src/webee/vector.rs @@ -0,0 +1,19 @@ +pub struct Vector<'a, T> { + size: u16, + array: &'a [T] +} + +impl<'a, T> Vector<'a, T> { + pub fn new(size: u16, array: &[T]) -> Self { + Self { + size, array + } + } +} + +#[macro_export] +macro_rules! vector { + ($array:expr) => { + Vector::new($array.len(), $array); + }; +} \ No newline at end of file