aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/luchie.rs
blob: 7d25a0058460efa4acf50577b6663773b1df4296 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#![no_std]
#![no_main]

//extern crate panic_semihosting;

use core::cmp;

use luchie::{
    cirque::Cirque,
    log, logger, logln,
};

use cortex_m::{
    asm::{bkpt, wfi},
    interrupt,
};
use cortex_m_rt::entry;
use embedded_hal::spi;
use stm32f1xx_hal::{
    pac,
    prelude::*,
    serial::{Config, Serial, StopBits, WordLength},
    spi::Spi,
    usb::{self, UsbBus},
};
use usb_device::prelude::*;
use usbd_human_interface_device::{
    prelude::*,
    device::mouse::{WheelMouseInterface, WheelMouseReport},
};
use usbd_serial::{SerialPort, USB_CLASS_CDC};

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

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

    let clocks = rcc
        .cfgr
        .use_hse(8.MHz())
        .sysclk(72.MHz()) // TODO: gd32 can get up to 120MHz
        .pclk1(24.MHz()) // TODO: causes issues with gd32 usb (dropped packets) and garbled usart1 output
        .freeze(&mut flash.acr);

    assert!(clocks.usbclk_valid());

    let mut afio = dp.AFIO.constrain();
    let mut gpioa = dp.GPIOA.split();

    let tx_pin = gpioa.pa9.into_alternate_push_pull(&mut gpioa.crh);
    let rx_pin = gpioa.pa10;
    let serial = Serial::usart1(
        dp.USART1,
        (tx_pin, rx_pin),
        &mut afio.mapr,
        Config::default()
            .baudrate(115200.bps())
            .wordlength(WordLength::Bits8)
            .parity_none()
            .stopbits(StopBits::STOP1),
        clocks,
    );
    let (tx, _) = serial.split();

    logger::init(tx);

    logln!("🐁 luchie starting…");

    // cirque spi connections to spi1:
    //
    // pb0 - dr
    // pa4 - ss1
    // pa5 - clk1
    // pa6 - miso1
    // pa7 - mosi1

    logln!("👆 init trackpad");
    // TODO: hook an interrupt up to the dr pin to trigger a poll.
    //let dr_pin = gpiob.pb1.into_pull_down_input(&mut gpiob.crl);
    let sck_pin = gpioa.pa5.into_alternate_push_pull(&mut gpioa.crl);
    let miso_pin = gpioa.pa6;
    let mosi_pin = gpioa.pa7.into_alternate_push_pull(&mut gpioa.crl);
    let cs_pin = gpioa.pa4.into_push_pull_output(&mut gpioa.crl);
    let mut spi = Spi::spi1(
        dp.SPI1,
        (sck_pin, miso_pin, mosi_pin),
        &mut afio.mapr,
        spi::MODE_1,
        1_000_000.Hz(), // pinnacle supports up to 13mhz
        clocks,
    );
    let mut cirque = match Cirque::new(cs_pin, &mut spi, clocks.sysclk().raw()) {
        Ok(c) => c,
        Err(e) => {
            logln!("err: {:?}", e);
            panic!();
        }
    };

    logln!("🖥️ init usb");
    // BluePill board has a pull-up resistor on the D+ line.
    // Pull the D+ pin down to send a RESET condition to the USB bus.
    // This forced reset is needed only for development, without it host
    // will not reset your device when you upload new firmware.
    let mut usb_dp = gpioa.pa12.into_push_pull_output(&mut gpioa.crh);
    usb_dp.set_low();
    // let mut delay = dp.TIM2.delay_us(&clocks);
    // delay.delay_ms(10u8);
    cortex_m::asm::delay(clocks.sysclk().raw() / 100);

    let usb = usb::Peripheral {
        usb: dp.USB,
        pin_dm: gpioa.pa11,
        pin_dp: usb_dp.into_floating_input(&mut gpioa.crh),
    };
    let usb_bus = UsbBus::new(usb);

    let mut serial = SerialPort::new(&usb_bus);

    let mut mouse_report = WheelMouseReport::default();
    let mut mouse = UsbHidClassBuilder::new()
        .add_interface(WheelMouseInterface::default_config())
        .build(&usb_bus);

    let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0xdead, 0xbeef))
        .manufacturer("Fake company")
        .product("Serial port")
        .serial_number("TEST")
        .device_class(USB_CLASS_CDC)
        .build();

    while usb_dev.state() != UsbDeviceState::Configured {
        usb_dev.poll(&mut [&mut serial, &mut mouse]);
    }

    logln!("💡 init led");
    let mut gpiob = dp.GPIOB.split();
    let mut led = gpiob.pb2.into_push_pull_output(&mut gpiob.crl);
    led.set_low();

    logln!("🎉 luchie started!");

    let mut is_pressed = false;
    let mut last_x = 0u16;
    let mut last_y = 0u16;
    loop {
        // logln!(".");
        usb_dev.poll(&mut [&mut serial, &mut mouse]);

        let mut buf = [0u8; 64];

        match serial.read(&mut buf) {
            Ok(count) if count > 0 => {
                led.set_high();

                // Echo back in upper case
                for c in buf[0..count].iter_mut() {
                    if 0x61 <= *c && *c <= 0x7a {
                        *c &= !0x20;
                    }
                }

                let mut write_offset = 0;
                while write_offset < count {
                    match serial.write(&buf[write_offset..count]) {
                        Ok(len) if len > 0 => {
                            write_offset += len;
                        }
                        _ => {}
                    }
                }
                led.set_low();
            }
            _ => {}
        }

        if let Ok(td) = cirque.poll(&mut spi) {
            logln!("td: {:?}", td);
            if td.is_pressed {
                if is_pressed {
                    /*
                     * The trackpad's actual valid return values are
                     * only about 2^11, so overflow isn't possible
                     * when converting to signed 16 bit integers.
                     */
                    let s_x: i16 = td.x as i16 - last_x as i16;
                    let s_y: i16 = td.y as i16 - last_y as i16;

                    // Clamp to i8 range.
                    let raw_x = cmp::max(i8::MIN as i16, cmp::min(i8::MAX as i16, s_x));
                    let raw_y = cmp::max(i8::MIN as i16, cmp::min(i8::MAX as i16, s_y));
                    mouse_report.x = raw_x as i8;
                    mouse_report.y = raw_y as i8;
                }
                is_pressed = true;
                last_x = td.x;
                last_y = td.y;
            } else {
                mouse_report.x = 0;
                mouse_report.y = 0;
                is_pressed = false;
            }
            match mouse.interface().write_report(&mouse_report) {
                Err(UsbHidError::WouldBlock) => {},
                Err(e) => {
                    panic!("couldn't write mouse report: {:?}", e)
                },
                _ => {},
            }
        }
    }
}

#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
    interrupt::free(|_cs| {
        log!("!!! panic ");
        if let Some(loc) = info.location() {
            log!("in {}:{} ", loc.file(), loc.line());
        }
        if let Some(msg) = info.payload().downcast_ref::<&str>() {
            log!("⇒ {} ", msg);
        }
        logln!("!!!");
    });
    spin();
}

fn spin() -> ! {
    bkpt();
    loop {
        wfi();
    }
}