Brian Silverman | 099196d | 2017-06-21 23:26:02 -0700 | [diff] [blame] | 1 | /* 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 Silverman | b79af7c | 2017-06-21 23:48:02 -0700 | [diff] [blame] | 34 | #include "motors/usb/usb_desc.h" |
Brian Silverman | 099196d | 2017-06-21 23:26:02 -0700 | [diff] [blame] | 35 | |
| 36 | #include <inttypes.h> |
| 37 | |
Brian Silverman | 099196d | 2017-06-21 23:26:02 -0700 | [diff] [blame] | 38 | // C language implementation |
| 39 | #ifdef __cplusplus |
| 40 | extern "C" { |
| 41 | #endif |
Brian Silverman | b79af7c | 2017-06-21 23:48:02 -0700 | [diff] [blame] | 42 | |
| 43 | void usb_serial_init(void); |
| 44 | |
| 45 | // Reads the next character (if any) and returns it. Returns -1 if no characters |
| 46 | // are available. |
| 47 | int usb_serial_getchar(int port); |
| 48 | |
| 49 | // Returns the next character (if any) or -1. |
| 50 | int 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. |
| 54 | int usb_serial_read(int port, void *buffer, uint32_t size); |
| 55 | |
| 56 | // Drops any unread input until the most recent packet sent. |
| 57 | void usb_serial_flush_input(int port); |
| 58 | |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 59 | // Writes data. Returns -1 if it times out or 0 if it succeeds. |
Brian Silverman | b79af7c | 2017-06-21 23:48:02 -0700 | [diff] [blame] | 60 | // |
| 61 | // NOTE: This does not send immediately. The data is buffered |
| 62 | int 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. |
| 65 | static 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? |
| 72 | void usb_serial_flush_output(int port); |
| 73 | |
Brian Silverman | 099196d | 2017-06-21 23:26:02 -0700 | [diff] [blame] | 74 | #ifdef __cplusplus |
| 75 | } |
| 76 | #endif |
| 77 | |
Brian Silverman | 099196d | 2017-06-21 23:26:02 -0700 | [diff] [blame] | 78 | #endif // USBserial_h_ |