diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/commands.rs | 6 | ||||
-rw-r--r-- | src/crc.rs | 32 | ||||
-rw-r--r-- | src/device.rs | 14 | ||||
-rw-r--r-- | src/main.rs | 23 | ||||
-rw-r--r-- | src/util.rs | 3 |
5 files changed, 50 insertions, 28 deletions
diff --git a/src/commands.rs b/src/commands.rs index 66851c1..0b4e00f 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -34,7 +34,7 @@ struct GetStatusResponse { config_delete_user_password: u8, } -assert_maximum_size!(GetStatusResponse; GetStatusResponse, crate::device::RESPONSE_DATA_LEN); +assert_maximum_size!(GetStatusResponse, crate::device::RESPONSE_DATA_LEN); #[derive(Debug, Default)] struct GetStatusCommand {} @@ -55,14 +55,14 @@ struct ReadSlotNameRequest { internal_slot_number: u8, } -assert_maximum_size!(ReadSlotNameRequest; ReadSlotNameRequest, crate::device::REQUEST_DATA_LEN); +assert_maximum_size!(ReadSlotNameRequest, crate::device::REQUEST_DATA_LEN); #[derive(Debug, Default, Serialize)] struct ReadSlotNameResponse { slot_name: [u8; 15], } -assert_maximum_size!(ReadSlotNameResponse; ReadSlotNameResponse, crate::device::RESPONSE_DATA_LEN); +assert_maximum_size!(ReadSlotNameResponse, crate::device::RESPONSE_DATA_LEN); #[derive(Debug, Default)] struct ReadSlotNameCommand {} @@ -1,23 +1,25 @@ // Copyright 2019 Robin Krahl <robin.krahl@ireas.org> // SPDX-License-Identifier: GPL-3.0-or-later +use core::iter::Iterator; + use hal::stm32::CRC; -pub struct Crc { +pub trait Crc { + fn get(&self, data: &[u8]) -> u32 { + self.get_u32(data.chunks(4).map(slice_to_u32)) + } + + fn get_u32<I: Iterator<Item = u32>>(&self, iter: I) -> u32; +} + +pub struct Stm32Crc { crc: CRC, } -impl Crc { +impl Stm32Crc { pub fn new(crc: CRC) -> Self { - Crc { crc } - } - - pub fn get(&self, data: &[u8]) -> u32 { - self.reset(); - data.chunks(4) - .map(slice_to_u32) - .for_each(|val| self.write(val)); - self.read() + Self { crc } } fn write(&self, val: u32) { @@ -33,6 +35,14 @@ impl Crc { } } +impl Crc for Stm32Crc { + fn get_u32<I: Iterator<Item = u32>>(&self, iter: I) -> u32 { + self.reset(); + iter.for_each(|val| self.write(val)); + self.read() + } +} + fn slice_to_u32(data: &[u8]) -> u32 { data.iter() .enumerate() diff --git a/src/device.rs b/src/device.rs index 9f3d1a7..7820a80 100644 --- a/src/device.rs +++ b/src/device.rs @@ -66,7 +66,7 @@ struct Request { pub crc: u32, } -assert_eq_size!(request; [u8; REPORT_LEN], Request); +assert_eq_size!([u8; REPORT_LEN], Request); #[derive(Serialize)] struct Response { @@ -79,7 +79,7 @@ struct Response { pub crc: u32, } -assert_eq_size!(response; [u8; REPORT_LEN], Response); +assert_eq_size!([u8; REPORT_LEN], Response); impl Response { fn new(device_status: DeviceStatus, command_id: u8, last_crc: u32) -> Response { @@ -94,15 +94,15 @@ impl Response { } } -pub struct Nitrokey { - crc: Crc, +pub struct Nitrokey<C: Crc> { + crc: C, request: Option<Request>, request_crc: u32, buf: [u8; REPORT_LEN], } -impl Nitrokey { - pub fn new(crc: Crc) -> Self { +impl<C: Crc> Nitrokey<C> { + pub fn new(crc: C) -> Self { Nitrokey { crc, request: None, @@ -112,7 +112,7 @@ impl Nitrokey { } } -impl HidDevice for Nitrokey { +impl<C: Crc> HidDevice for Nitrokey<C> { fn subclass(&self) -> Subclass { Subclass::BootInterface } diff --git a/src/main.rs b/src/main.rs index 7e364ab..7c9ee9c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ #![no_std] #![no_main] +#![allow(clippy::missing_safety_doc)] extern crate panic_halt; @@ -14,12 +15,14 @@ mod crc; mod device; mod hid; +use cortex_m::asm::delay; use cortex_m_rt::entry; +use embedded_hal::digital::v2::OutputPin; use hal::prelude::*; -use stm32f103xx_usb::UsbBus; +use hal::usb::{Peripheral, UsbBus}; use usb_device::class::UsbClass; -use crate::crc::Crc; +use crate::crc::Stm32Crc; use crate::device::Nitrokey; use crate::hid::HidClass; @@ -28,7 +31,7 @@ fn main() -> ! { let p = hal::stm32::Peripherals::take().unwrap(); p.RCC.ahbenr.modify(|_, w| w.crcen().set_bit()); - let crc = Crc::new(p.CRC); + let crc = Stm32Crc::new(p.CRC); let mut flash = p.FLASH.constrain(); let mut rcc = p.RCC.constrain(); @@ -44,10 +47,20 @@ fn main() -> ! { let mut gpioa = p.GPIOA.split(&mut rcc.apb2); - let usb_bus = UsbBus::usb_with_reset(p.USB, &mut rcc.apb1, &clocks, &mut gpioa.crh, gpioa.pa12); + let mut pin_dp = gpioa.pa12.into_push_pull_output(&mut gpioa.crh); + pin_dp.set_low().unwrap(); + delay(clocks.sysclk().0 / 100); + let pin_dp = pin_dp.into_floating_input(&mut gpioa.crh); + let pin_dm = gpioa.pa11; + + let usb = Peripheral { + usb: p.USB, + pin_dm, + pin_dp, + }; + let usb_bus = UsbBus::new(usb); let mut usb_class = HidClass::new(Nitrokey::new(crc), &usb_bus); let mut usb_dev = device::create_usb_device(&usb_bus); - usb_dev.force_reset().expect("USB reset failed"); loop { if usb_dev.poll(&mut [&mut usb_class]) { diff --git a/src/util.rs b/src/util.rs index 7b12afb..61bcfe7 100644 --- a/src/util.rs +++ b/src/util.rs @@ -78,9 +78,8 @@ macro_rules! enum_u8 { } macro_rules! assert_maximum_size { - ($i:ident; $t:ident, $e: expr) => { + ($t:ident, $e: expr) => { ::static_assertions::const_assert!( - $i; ::core::mem::size_of::<$t>() <= $e ); } |