aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 7e364abf7d42784834da1716dd03b91cb48158c7 (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
// 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_rt::entry;
use hal::prelude::*;
use stm32f103xx_usb::UsbBus;
use usb_device::class::UsbClass;

use crate::crc::Crc;
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 = Crc::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 usb_bus = UsbBus::usb_with_reset(p.USB, &mut rcc.apb1, &clocks, &mut gpioa.crh, gpioa.pa12);
    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]) {
            usb_class.poll();
        }
    }
}