aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 99f1ec6a3c921bc4060381c903923b2663f4acd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2019 Robin Krahl <robin.krahl@ireas.org>
// SPDX-License-Identifier: GPL-3.0-or-later

#![no_std]
#![no_main]

extern crate panic_halt;

#[macro_use]
mod util;

mod commands;
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 hal::usb::{Peripheral, UsbBus};
use usb_device::class::UsbClass;

use crate::crc::Stm32Crc;
use crate::device::Nitrokey;
use crate::hid::HidClass;

#[entry]
fn main() -> ! {
    let p = hal::stm32::Peripherals::take().unwrap();

    p.RCC.ahbenr.modify(|_, w| w.crcen().set_bit());
    let crc = Stm32Crc::new(p.CRC);

    let mut flash = p.FLASH.constrain();
    let mut rcc = p.RCC.constrain();

    let clocks = rcc
        .cfgr
        .use_hse(8.mhz())
        .sysclk(48.mhz())
        .pclk1(24.mhz())
        .freeze(&mut flash.acr);

    assert!(clocks.usbclk_valid(), "USB clocks not valid");

    let mut gpioa = p.GPIOA.split(&mut rcc.apb2);

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

    loop {
        if usb_dev.poll(&mut [&mut usb_class]) {
            usb_class.poll();
        }
    }
}