blob: 40e7c36eb6ff831a3c894ad5294b7b600cf99d12 [file] [log] [blame]
Brian Silverman099196d2017-06-21 23:26:02 -07001/* Teensyduino Core Library
2 * http://www.pjrc.com/teensy/
3 * Copyright (c) 2017 PJRC.COM, LLC.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * 1. The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * 2. If the Software is incorporated into a build system that allows
17 * selection among a list of target devices, then similar target
18 * devices manufactured by PJRC.COM must be included in the list of
19 * target devices and selectable in the same manner.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
25 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
26 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 * SOFTWARE.
29 */
30
31#ifndef USBserial_h_
32#define USBserial_h_
33
Brian Silvermanb79af7c2017-06-21 23:48:02 -070034#include "motors/usb/usb_desc.h"
Brian Silverman099196d2017-06-21 23:26:02 -070035
36#include <inttypes.h>
37
Brian Silverman099196d2017-06-21 23:26:02 -070038// C language implementation
39#ifdef __cplusplus
40extern "C" {
41#endif
Brian Silvermanb79af7c2017-06-21 23:48:02 -070042
43void usb_serial_init(void);
44
45// Reads the next character (if any) and returns it. Returns -1 if no characters
46// are available.
47int usb_serial_getchar(int port);
48
49// Returns the next character (if any) or -1.
50int usb_serial_peekchar(int port);
51
52// Reads as many bytes (up to size) as are available now. Returns 0 immediately
53// if no bytes are available. Returns the number of bytes read.
54int usb_serial_read(int port, void *buffer, uint32_t size);
55
56// Drops any unread input until the most recent packet sent.
57void usb_serial_flush_input(int port);
58
Brian Silverman8d3816a2017-07-03 18:52:15 -070059// Writes data. Returns -1 if it times out or 0 if it succeeds.
Brian Silvermanb79af7c2017-06-21 23:48:02 -070060//
61// NOTE: This does not send immediately. The data is buffered
62int usb_serial_write(int port, const void *buffer, uint32_t size);
63
64// Writes a single character. Returns -1 if it times out or 1 if it succeeds.
65static inline int usb_serial_putchar(int port, uint8_t c) {
66 return usb_serial_write(port, &c, 1);
67}
68
69// Immediately flushes all written data.
70//
71// TODO(Brian): What exactly are the semantics here?
72void usb_serial_flush_output(int port);
73
Brian Silverman099196d2017-06-21 23:26:02 -070074#ifdef __cplusplus
75}
76#endif
77
Brian Silverman099196d2017-06-21 23:26:02 -070078#endif // USBserial_h_