Add Webee stuff

This commit is contained in:
2023-08-24 01:53:41 +02:00
parent 86994e0f8c
commit 3c5a7215d1
7 changed files with 215 additions and 15 deletions

View File

@@ -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"

5
Cargo.lock generated
View File

@@ -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",
]

View File

@@ -4,6 +4,7 @@ version = "0.1.0"
authors = ["Adrian Marquis <adrian@marquis.site>"]
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"

37
rustc-wrapper.sh Executable file
View File

@@ -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}" $@

View File

@@ -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<u8> = 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<USART, RX, TX> {
// serial: Usart<USART, RX, TX>,
// }
//
// impl<USART, RX, TX> Webee<USART, RX, TX> {
// 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()
// }
// }

44
src/webee.rs Normal file
View File

@@ -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<USART, RX, TX> {
webee: Usart<USART, RX, TX>,
}
enum SEND_CMD {
}
impl<USART, RX, TX> Webee<USART, RX, TX> {
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<u8>) -> Vector<u8> {
for byte in data {
self.webee.write_byte(byte);
}
return self.recv();
}
fn recv(&mut self) -> Vector<u8> {
const buffer : Vector<u8> = Vector::new(0, &[]);
let byte : u8 = self.webee.read_byte();
while byte != self.STOP {
buffer.push(byte);
}
return buffer;
}
}

19
src/webee/vector.rs Normal file
View File

@@ -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);
};
}