start work on usart requests

This commit is contained in:
Robert 2022-12-13 01:38:50 +01:00
parent 7a41ba87b6
commit ca5125eaf9
4 changed files with 89 additions and 24 deletions

View file

@ -10,4 +10,4 @@ name = "b15f"
path = "src/lib.rs" path = "src/lib.rs"
[dependencies] [dependencies]
serialport = "4.2.0"

View file

@ -2,7 +2,10 @@
//! interacting with the B15 on a high level. If you are writing code //! interacting with the B15 on a high level. If you are writing code
//! for the B15, this is the module you want to use. //! for the B15, this is the module you want to use.
use std::process::Command; use std::{process::Command, time::Duration, fmt::{Debug, Display}, thread::sleep, error::Error};
use serialport::SerialPort;
use crate::request::Request;
/// Hardcoded commit hash of the most recent firmware /// Hardcoded commit hash of the most recent firmware
static COMMIT_HASH: &'static str = "bc459c80cec755d7df2c11a807d74e085cbed332"; static COMMIT_HASH: &'static str = "bc459c80cec755d7df2c11a807d74e085cbed332";
@ -23,9 +26,8 @@ macro_rules! log_end {
} }
/// Structure representing the driver for the board 15 /// Structure representing the driver for the board 15
#[derive(Debug)]
pub struct B15F { pub struct B15F {
usart: Box<dyn SerialPort>
} }
impl B15F { impl B15F {
@ -42,39 +44,50 @@ impl B15F {
/// ///
/// let drv = B15F::new().unwrap(); /// let drv = B15F::new().unwrap();
/// ``` /// ```
pub fn new() -> Result<B15F, &'static str> { pub fn new() -> Result<B15F, String> {
let drv = B15F {}; let port = B15F::init_connection()?;
drv.init()
}
fn init(self) -> Result<B15F, &'static str> { let mut drv =B15F {
let devices = B15F::get_devices(); usart: port
let device = match devices.first() {
Some(item) => item,
None => return Err("Failed to find adapter")
}; };
log!("Using adapter: {}", device);
log_start!("Establish connection with adapter");
todo!("Implement USART");
log_end!("Ok!");
log_start!("Testing connection"); log_start!("Testing connection");
todo!("Test connection"); for tries in 0..3 {
drv.discard();
}
log_end!("Ok!"); log_end!("Ok!");
let info = self.get_board_info(); let info = drv.get_board_info();
log!("AVR firmware version: {} built at {} ({})", info[0], info[1], info[2]); log!("AVR firmware version: {} built at {} ({})", info[0], info[1], info[2]);
let avr_commit_hash = info[3]; let avr_commit_hash = info[3];
if avr_commit_hash != COMMIT_HASH { if avr_commit_hash != COMMIT_HASH {
log!("Different commit hashes: {} vs {}", avr_commit_hash, COMMIT_HASH); log!("Different commit hashes: {} vs {}", avr_commit_hash, COMMIT_HASH);
return Err("Versions incompatible. Please update the software!"); return Err("Versions incompatible. Please update the software!".into());
} }
Ok(self) Ok(drv)
}
fn init_connection() -> Result<Box<dyn SerialPort>, String> {
let devices = B15F::get_devices();
let device = match devices.first() {
Some(item) => item,
None => return Err("Failed to find adapter".into())
};
log!("Using adapter: {}", device);
log_start!("Establish connection with adapter");
let port = serialport::new(device, 57_600)
.timeout(Duration::from_millis(1000))
.open()
.map_err(|err| err.to_string())?;
log_end!("Ok!");
Ok(port)
} }
/// Yields information about the installed firmware on the B15 /// Yields information about the installed firmware on the B15
@ -97,6 +110,18 @@ impl B15F {
todo!(); todo!();
} }
/// Clears data in the USART buffers on this device and on the B15
pub fn discard(&mut self) {
self.usart.clear(serialport::ClearBuffer::Output).unwrap();
for i in 0..16 {
self.usart.write(&Request::new().discard().done()[..]).unwrap();
sleep(Duration::from_millis(4));
}
self.usart.clear(serialport::ClearBuffer::Input).unwrap()
}
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))] #[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
fn get_devices() -> Vec<String> { fn get_devices() -> Vec<String> {
let output = Command::new("bash") let output = Command::new("bash")
@ -127,4 +152,12 @@ impl B15F {
.map(|item| item.into()) .map(|item| item.into())
.collect() .collect()
} }
}
impl Debug for B15F {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "Baudrate: {}", self.usart.baud_rate().unwrap())?;
writeln!(f, "Data bits: {:?}", self.usart.data_bits().unwrap())?;
writeln!(f, "Parity: {:?}", self.usart.parity().unwrap())
}
} }

View file

@ -14,4 +14,6 @@
//! [`original`]: https://github.com/devfix/b15f //! [`original`]: https://github.com/devfix/b15f
pub mod b15f; pub mod b15f;
mod request;
pub use crate::b15f::B15F; pub use crate::b15f::B15F;

30
src/request.rs Normal file
View file

@ -0,0 +1,30 @@
//! This module contains the request data used to communicate
//! with the B15 via USART.
//!
//! Using a direct USART connection to the B15 is discouraged,
//! if you are trying to interact with the B15 consider using
//! the `b15f::B15F` structure instead.
#[repr(u8)]
enum RequestType {
Discard = 0
}
pub struct Request {
req: Vec<u8>
}
impl Request {
pub fn new() -> Request {
Request { req: vec![] }
}
pub fn discard(mut self) -> Self {
self.req.push(RequestType::Discard as u8);
self
}
pub fn done(self) -> Vec<u8> {
self.req
}
}