Skip to content

Commit

Permalink
fix clippy errors
Browse files Browse the repository at this point in the history
Signed-off-by: Chuanhong Guo <[email protected]>
  • Loading branch information
981213 committed Feb 27, 2024
1 parent 2fdc071 commit 103d900
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 35 deletions.
28 changes: 11 additions & 17 deletions src/bl2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::slice;
use std::time::Duration;
use serialport::{ClearBuffer, SerialPort};

static BL2_HANDSHAKE_REQ: &'static [u8] = "mudl".as_bytes();
static BL2_HANDSHAKE_RESP: &'static [u8] = "TF-A".as_bytes();
static BL2_HANDSHAKE_REQ: &[u8] = "mudl".as_bytes();
static BL2_HANDSHAKE_RESP: &[u8] = "TF-A".as_bytes();

pub struct BL2 {
port: Box<dyn SerialPort>,
Expand All @@ -26,10 +26,10 @@ impl BL2 {
let mut rx_char = 0;
self.port.set_timeout(Duration::from_millis(500)).unwrap();
while i < 4 {
self.port.write(&BL2_HANDSHAKE_REQ[i..i + 1])
self.port.write_all(&BL2_HANDSHAKE_REQ[i..i + 1])
.expect("failed to write to port.");
if let Ok(len) = self.port.read(slice::from_mut(&mut rx_char)) {
if len == 1 && BL2_HANDSHAKE_RESP[i] == rx_char {
if let Ok(()) = self.port.read_exact(slice::from_mut(&mut rx_char)) {
if BL2_HANDSHAKE_RESP[i] == rx_char {
i += 1;
}
}
Expand All @@ -41,35 +41,29 @@ impl BL2 {
fn echo(&mut self, buf: &[u8]) {
let mut rx_buf: Vec<u8> = vec![0; buf.len()];
self.port.set_timeout(Duration::from_millis(100)).unwrap();
self.port.write(buf).expect("failed to write to port.");
self.port.read(rx_buf.as_mut_slice()).unwrap();
self.port.write_all(buf).expect("failed to write to port.");
self.port.read_exact(rx_buf.as_mut_slice()).unwrap();
if buf != rx_buf {
panic!("returned data isn't the same. Tx: {:?} Rx: {:?}", buf, rx_buf);
}
}

fn read_be16(&mut self) -> u16 {
let mut rx_buf: Vec<u8> = vec![0; 2];
let len = self.port.read(rx_buf.as_mut_slice()).unwrap();
if len != 2 {
panic!("not enough data returned.")
}
self.port.read_exact(rx_buf.as_mut_slice()).unwrap();
u16::from_be_bytes(rx_buf.try_into().unwrap())
}

fn read_be32(&mut self) -> u32 {
let mut rx_buf: Vec<u8> = vec![0; 4];
let len = self.port.read(rx_buf.as_mut_slice()).unwrap();
if len != 4 {
panic!("not enough data returned.")
}
self.port.read_exact(rx_buf.as_mut_slice()).unwrap();
u32::from_be_bytes(rx_buf.try_into().unwrap())
}

pub fn version(&mut self) -> u8 {
self.echo(slice::from_ref(&1));
let mut rx_char = 0;
self.port.read(slice::from_mut(&mut rx_char)).unwrap();
self.port.read_exact(slice::from_mut(&mut rx_char)).unwrap();
rx_char
}

Expand Down Expand Up @@ -104,7 +98,7 @@ impl BL2 {
self.echo(&u16::to_be_bytes(fip.len() as u16));
let checksum = BL2::fip_packet_checksum(fip);
self.echo(&u16::to_be_bytes(checksum));
self.port.write(fip).expect("failed to send fip packet.");
self.port.write_all(fip).expect("failed to send fip packet.");

while self.port.bytes_to_write().unwrap() > 0 {
std::thread::sleep(Duration::from_millis(50));
Expand Down
24 changes: 9 additions & 15 deletions src/bootrom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::slice;
use std::time::Duration;
use serialport::{ClearBuffer, SerialPort};

static BROM_HANDSHAKE: &'static [u8] = &[0xa0, 0x0a, 0x50, 0x05];
static BROM_HANDSHAKE: &[u8] = &[0xa0, 0x0a, 0x50, 0x05];
pub struct BootROM {
port: Box<dyn SerialPort>,
}
Expand All @@ -24,10 +24,10 @@ impl BootROM {
let mut rx_char = 0;
self.port.set_timeout(Duration::from_millis(5)).unwrap();
while i < BROM_HANDSHAKE.len() {
self.port.write(&BROM_HANDSHAKE[i .. i+1])
self.port.write_all(&BROM_HANDSHAKE[i .. i+1])
.expect("failed to write to port.");
if let Ok(len) = self.port.read(slice::from_mut(&mut rx_char)) {
if len == 1 && BROM_HANDSHAKE[i] == !rx_char {
if let Ok(()) = self.port.read_exact(slice::from_mut(&mut rx_char)) {
if BROM_HANDSHAKE[i] == !rx_char {
i += 1;
}
}
Expand All @@ -39,28 +39,22 @@ impl BootROM {
fn echo(&mut self, buf: &[u8]) {
let mut rx_buf: Vec<u8> =vec![0; buf.len()];
self.port.set_timeout(Duration::from_millis(100)).unwrap();
self.port.write(buf).expect("failed to write to port.");
self.port.read(rx_buf.as_mut_slice()).unwrap();
self.port.write_all(buf).expect("failed to write to port.");
self.port.read_exact(rx_buf.as_mut_slice()).unwrap();
if buf != rx_buf {
panic!("returned data isn't the same. Tx: {:?} Rx: {:?}", buf, rx_buf);
}
}

fn read_be16(&mut self) -> u16 {
let mut rx_buf: Vec<u8> = vec![0; 2];
let len = self.port.read(rx_buf.as_mut_slice()).unwrap();
if len != 2 {
panic!("not enough data returned.")
}
self.port.read_exact(rx_buf.as_mut_slice()).unwrap();
u16::from_be_bytes(rx_buf.try_into().unwrap())
}

fn read_be32(&mut self) -> u32 {
let mut rx_buf: Vec<u8> = vec![0; 4];
let len = self.port.read(rx_buf.as_mut_slice()).unwrap();
if len != 4 {
panic!("not enough data returned.")
}
self.port.read_exact(rx_buf.as_mut_slice()).unwrap();
u32::from_be_bytes(rx_buf.try_into().unwrap())
}

Expand Down Expand Up @@ -110,7 +104,7 @@ impl BootROM {
panic!("send_da cmd status: {}", ret);
}

self.port.write(da_buf).unwrap();
self.port.write_all(da_buf).unwrap();
while self.port.bytes_to_write().unwrap() > 0 {
std::thread::sleep(Duration::from_millis(200));
}
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,14 @@ fn open_serial(port: Option<&str>) -> Box<dyn SerialPort> {
Some(p) => p.to_owned(),
None => {
let ports = serialport::available_ports().expect("No serial ports found.");
ports.into_iter().nth(0).expect("No serial ports found.").port_name
ports.into_iter().next().expect("No serial ports found.").port_name
},
};

println!("Using serial port: {}", port);
return serialport::new(&port, 115200)
serialport::new(&port, 115200)
.timeout(Duration::from_secs(2))
.open().expect("Failed to open port");
.open().expect("Failed to open port")
}

fn main() {
Expand Down

0 comments on commit 103d900

Please sign in to comment.