summaryrefslogtreecommitdiff
path: root/usbserial.h
blob: c31282120230aef154ccbf8100494ebc99cc6ebd (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
#ifndef USBSERIAL_H
#define USBSERIAL_H

#include <ch.h>
#include <hal.h>
#include <usb_cdc.h>

class USBSerial {
	public:
		SerialUSBDriver susbdriver;
		
	public:
		void init();
		
		inline msg_t getc(systime_t timeout = TIME_INFINITE) {
			return chIQGetTimeout(&susbdriver.iqueue, timeout);
		}
		
		inline size_t read(uint8_t* buf, size_t len, systime_t timeout = TIME_INFINITE) {
			return chIQReadTimeout(&susbdriver.iqueue, buf, len, timeout);
		}
		
		inline msg_t putc(uint8_t data, systime_t timeout = TIME_INFINITE) {
			if(USBD1.state != USB_ACTIVE) {
				return 0;
			}
			return chOQPutTimeout(&susbdriver.oqueue, data, timeout);
		}
		
		inline size_t write(uint8_t* buf, size_t len, systime_t timeout = TIME_INFINITE) {
			if(USBD1.state != USB_ACTIVE) {
				return 0;
			}
			return chOQWriteTimeout(&susbdriver.oqueue, buf, len, timeout);
		}
};

#endif