summaryrefslogtreecommitdiff
path: root/main.cpp
blob: 24962f4c96cde60cae7127a7a1d63312cbd697a4 (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
#include <rcc/rcc.h>
#include <os/time.h>
#include <gpio/gpio.h>
#include <usb/usb.h>
#include <usb/descriptor.h>

Pin usb_vbus = GPIOB[5];
Pin usb_dm = GPIOA[11];
Pin usb_dp = GPIOA[12];

auto dev_desc = device_desc(0x200, 0, 0, 0, 64, 0x1234, 0x5678, 0, 0, 0, 0, 1);
auto conf_desc = configuration_desc(1, 1, 0, 0xc0, 0,
	interface_desc(1, 0, 1, 0xff, 0x00, 0x00, 0,
		endpoint_desc(0x81, 0x02, 64, 0) // IN
	)
);

desc_t dev_desc_p = {sizeof(dev_desc), (void*)&dev_desc};
desc_t conf_desc_p = {sizeof(conf_desc), (void*)&conf_desc};

USB_f1 usb(USB, dev_desc_p, conf_desc_p);

class USB_TM : public USB_class_driver {
	private:
		USB_generic& usb;
		uint32_t buf[16];
		
	public:
		USB_TM(USB_generic& usbd) : usb(usbd) {
			usb.register_driver(this);
		}
		
	protected:
		virtual void handle_set_configuration(uint8_t configuration) {
			if (configuration) {
				usb.hw_conf_ep(0x81, EPType::Bulk, 64);
				usb.register_out_handler(this, 1);
			}
		}
};

USB_TM usb_tm(usb);

int main() {
	rcc_init();
	
	// Initialize system timer.
	STK.LOAD = 72000000 / 8 / 1000; // 1000 Hz
	STK.CTRL = 0x03;
	
	RCC.enable(RCC.GPIOA);
	
	usb_dm.set_mode(Pin::AF);
	usb_dm.set_af(14);
	usb_dp.set_mode(Pin::AF);
	usb_dp.set_af(14);
	
	RCC.enable(RCC.USB);
	
	usb.init();
	while(1) {
		usb.process();
		if(usb.ep_ready(1)) {
			char a = 'A';
			usb.write(1, (uint32_t*)&a, sizeof(a));
		}
	}
}