-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmouse.cxx
133 lines (95 loc) · 3.46 KB
/
mouse.cxx
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
// papoon_usb: "Not Insane" USB library for STM32F103xx MCUs
// Copyright (C) 2019,2020 Mark R. Rubin
//
// This file is part of papoon_usb.
//
// The papoon_usb program is free software: you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// The papoon_usb program is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// (LICENSE.txt) along with the papoon_usb program. If not, see
// <https://www.gnu.org/licenses/gpl.html>
#include <stdint.h>
#include <core_cm3.hxx>
#include <stm32f103xb.hxx>
#include <sys_tick_timer.hxx>
#include <usb_dev_hid_mouse.hxx>
#include <usb_mcu_init.hxx>
#include <usb_randomtest.hxx>
using namespace stm32f103xb;
using namespace stm32f10_12357_xx;
arm::SysTickTimer sys_tick_timer;
UsbDevHidMouse usb_dev ;
static const uint32_t CPU_HZ = 72000000 ,
TICKS_PER_MOVE = CPU_HZ / 24; // 24 Hz
#ifdef USB_DEV_INTERRUPT_DRIVEN
extern "C" void USB_LP_CAN1_RX0_IRQHandler()
{
usb_dev.interrupt_handler();
}
#endif
int main()
{
usb_dev.serial_number_init(); // do before mcu_init() clock speed breaks
usb_mcu_init ();
usb_gpio_init();
sys_tick_timer.init();
gpioc->bsrr = Gpio::Bsrr::BS13; // turn off user LED by setting high
#ifdef USB_DEV_INTERRUPT_DRIVEN
arm::nvic->iser.set(arm::NvicIrqn::USB_LP_CAN1_RX0);
#endif
if (!usb_dev.init())
{
gpioc->bsrr = Gpio::Bsrr::BR13; // turn on user LED by setting low
while (true) asm("nop"); // hang
}
gpioc->bsrr = Gpio::Bsrr::BR13; // turn on user LED by setting low
while (usb_dev.device_state() != UsbDev::DeviceState::CONFIGURED) {
#ifdef USB_DEV_INTERRUPT_DRIVEN
asm("wfi");
#else
usb_dev.poll();
#endif
}
gpioc->bsrr = Gpio::Bsrr::BS13; // set high to turn off user LED
static const uint8_t MAX_DIR = 12,
MAX_STEP = 8,
X_NDX = 1,
Y_NDX = 2;
static const int8_t
X_DIRS [MAX_DIR] = { 0, -1, -1, -1, -1, 0, 0, 1, 1, 1, 1, 0},
Y_DIRS [MAX_DIR] = { 1, 1, 0, 0, -1, -1, -1, -1, 0, 0, 1, 1};
static uint8_t hid_report[4] = { 0, 0, 0, 0};
uint8_t dir = MAX_DIR - 1,
step = MAX_STEP - 1;
while (true) {
if (++step == MAX_STEP) {
if (++dir == MAX_DIR) dir = 0;
hid_report[X_NDX] = X_DIRS[dir];
hid_report[Y_NDX] = Y_DIRS[dir];
step = 0;
}
if (dir & 0x1)
gpioc->bsrr = Gpio::Bsrr::BR13; // set low to turn on user LED
else
gpioc->bsrr = Gpio::Bsrr::BS13; // set high to turn off user LED
sys_tick_timer.begin32();
while (!usb_dev.send(usb_dev.MOUSE_ENDPOINT_IN,
hid_report ,
sizeof(hid_report) ))
#ifdef USB_DEV_INTERRUPT_DRIVEN
asm("wfi") ;
#else
usb_dev.poll();
#endif
while (sys_tick_timer.elapsed32() < TICKS_PER_MOVE)
asm("nop");
}
} // main()