aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: caf7fdf777393dd0c172812d174175d30cdfea3f (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
// 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 device;
mod hid;

use cortex_m_rt::entry;
use hal::prelude::*;
use stm32f103xx_usb::UsbBus;
use usb_device::class::UsbClass;

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

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

    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(), &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();
        }
    }
}