Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print chip name when opening HID device #13

Merged
merged 2 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[workspace]
members = ["hpm_isp"]
resolver = "2"
78 changes: 49 additions & 29 deletions hpm_isp/src/hid.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,12 @@
#![allow(unused)]
use std::fmt::Display;

use hidapi::{HidApi, HidDevice, HidError};
use num_enum::FromPrimitive;
use strum::{EnumIter, IntoEnumIterator};
use zerocopy::{AsBytes, FromBytes, FromZeroes};

use crate::hid::PacketType::Ack;
use crate::isp_command::{Error, Interface, IspCommand, Packet};

#[derive(EnumIter, Clone, Copy)]
#[repr(u16)]
enum Family {
HPM6700_6400 = 0x0001,
HPM6300 = 0x0002,
HPM6200 = 0x0003,
HPM6800 = 0x0004,
HPM5300 = 0x0005,
}

impl Family {
pub fn pid() -> u16 {
0x34b7
}

pub fn vid(&self) -> u16 {
*self as u16
}
}

pub struct HpmDevice {
device: HidDevice,
}

#[derive(AsBytes, FromZeroes, FromBytes)]
#[repr(C, packed)]
struct HidPayloadPacket {
Expand Down Expand Up @@ -87,14 +62,59 @@ impl HidAcknowledgement {
}
}

#[derive(EnumIter, Clone, Copy)]
#[repr(u16)]
pub enum Family {
HPM6700_6400 = 0x0001,
HPM6300 = 0x0002,
HPM6200 = 0x0003,
HPM6800 = 0x0004,
HPM5300 = 0x0005,
}

impl Family {
pub fn pid() -> u16 {
0x34b7
}

pub fn vid(&self) -> u16 {
*self as u16
}
}

impl Display for Family {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let name = match self {
Family::HPM6700_6400 => "HPM6700/6400",
Family::HPM6300 => "HPM6300",
Family::HPM6200 => "HPM6200",
Family::HPM6800 => "HPM6800",
Family::HPM5300 => "HPM5300",
};
write!(f, "{name}")
}
}

pub struct HpmDevice {
device: HidDevice,
family: Family,
}

impl HpmDevice {
pub fn open() -> Result<Self, Box<dyn std::error::Error>> {
let api = HidApi::new().unwrap();
// Connect to device using its VID and PID
let device = Family::iter()
.find_map(|chip| api.open(Family::pid(), chip.vid()).ok())
let (device, family) = Family::iter()
.find_map(|chip| match api.open(Family::pid(), chip.vid()).ok() {
Some(device) => Some((device, chip)),
None => None,
})
.ok_or("Can't find any HPMicro device")?;
Ok(Self { device })
Ok(Self { device, family })
}

pub fn family(&self) -> Family {
self.family
}
}

Expand Down
2 changes: 2 additions & 0 deletions hpm_isp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ fn main() -> Result<(), Box<dyn Error>> {
let is_default = config.is_none();
let config_path = config.unwrap_or(DEFAULT_CONFIG_FILE.into());

println!("Found chip: {}", device.family());

match fs::File::open(config_path) {
Ok(mut config_file) => {
memory_config_bin.resize(12, 0);
Expand Down
Loading