Austin Schuh | 41baf20 | 2022-01-01 14:33:40 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * The MIT License (MIT) |
| 3 | * |
| 4 | * Copyright (c) 2019 Ha Thach (tinyusb.org) |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | * |
| 24 | * This file is part of the TinyUSB stack. |
| 25 | */ |
| 26 | |
| 27 | #ifndef _TUSB_CDC_DEVICE_H_ |
| 28 | #define _TUSB_CDC_DEVICE_H_ |
| 29 | |
| 30 | #include "common/tusb_common.h" |
| 31 | #include "cdc.h" |
| 32 | |
| 33 | //--------------------------------------------------------------------+ |
| 34 | // Class Driver Configuration |
| 35 | //--------------------------------------------------------------------+ |
| 36 | #if !defined(CFG_TUD_CDC_EP_BUFSIZE) && defined(CFG_TUD_CDC_EPSIZE) |
| 37 | #warning CFG_TUD_CDC_EPSIZE is renamed to CFG_TUD_CDC_EP_BUFSIZE, please update to use the new name |
| 38 | #define CFG_TUD_CDC_EP_BUFSIZE CFG_TUD_CDC_EPSIZE |
| 39 | #endif |
| 40 | |
| 41 | #ifndef CFG_TUD_CDC_EP_BUFSIZE |
| 42 | #define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) |
| 43 | #endif |
| 44 | |
| 45 | #ifdef __cplusplus |
| 46 | extern "C" { |
| 47 | #endif |
| 48 | |
| 49 | /** \addtogroup CDC_Serial Serial |
| 50 | * @{ |
| 51 | * \defgroup CDC_Serial_Device Device |
| 52 | * @{ */ |
| 53 | |
| 54 | //--------------------------------------------------------------------+ |
| 55 | // Application API (Multiple Ports) |
| 56 | // CFG_TUD_CDC > 1 |
| 57 | //--------------------------------------------------------------------+ |
| 58 | |
| 59 | // Check if terminal is connected to this port |
| 60 | bool tud_cdc_n_connected (uint8_t itf); |
| 61 | |
| 62 | // Get current line state. Bit 0: DTR (Data Terminal Ready), Bit 1: RTS (Request to Send) |
| 63 | uint8_t tud_cdc_n_get_line_state (uint8_t itf); |
| 64 | |
| 65 | // Get current line encoding: bit rate, stop bits parity etc .. |
| 66 | void tud_cdc_n_get_line_coding (uint8_t itf, cdc_line_coding_t* coding); |
| 67 | |
| 68 | // Set special character that will trigger tud_cdc_rx_wanted_cb() callback on receiving |
| 69 | void tud_cdc_n_set_wanted_char (uint8_t itf, char wanted); |
| 70 | |
| 71 | // Get the number of bytes available for reading |
| 72 | uint32_t tud_cdc_n_available (uint8_t itf); |
| 73 | |
| 74 | // Read received bytes |
| 75 | uint32_t tud_cdc_n_read (uint8_t itf, void* buffer, uint32_t bufsize); |
| 76 | |
| 77 | // Read a byte, return -1 if there is none |
| 78 | static inline |
| 79 | int32_t tud_cdc_n_read_char (uint8_t itf); |
| 80 | |
| 81 | // Clear the received FIFO |
| 82 | void tud_cdc_n_read_flush (uint8_t itf); |
| 83 | |
| 84 | // Get a byte from FIFO at the specified position without removing it |
| 85 | bool tud_cdc_n_peek (uint8_t itf, uint8_t* ui8); |
| 86 | |
| 87 | // Write bytes to TX FIFO, data may remain in the FIFO for a while |
| 88 | uint32_t tud_cdc_n_write (uint8_t itf, void const* buffer, uint32_t bufsize); |
| 89 | |
| 90 | // Write a byte |
| 91 | static inline |
| 92 | uint32_t tud_cdc_n_write_char (uint8_t itf, char ch); |
| 93 | |
| 94 | // Write a null-terminated string |
| 95 | static inline |
| 96 | uint32_t tud_cdc_n_write_str (uint8_t itf, char const* str); |
| 97 | |
| 98 | // Force sending data if possible, return number of forced bytes |
| 99 | uint32_t tud_cdc_n_write_flush (uint8_t itf); |
| 100 | |
| 101 | // Return the number of bytes (characters) available for writing to TX FIFO buffer in a single n_write operation. |
| 102 | uint32_t tud_cdc_n_write_available (uint8_t itf); |
| 103 | |
| 104 | // Clear the transmit FIFO |
| 105 | bool tud_cdc_n_write_clear (uint8_t itf); |
| 106 | |
| 107 | //--------------------------------------------------------------------+ |
| 108 | // Application API (Single Port) |
| 109 | //--------------------------------------------------------------------+ |
| 110 | static inline bool tud_cdc_connected (void); |
| 111 | static inline uint8_t tud_cdc_get_line_state (void); |
| 112 | static inline void tud_cdc_get_line_coding (cdc_line_coding_t* coding); |
| 113 | static inline void tud_cdc_set_wanted_char (char wanted); |
| 114 | |
| 115 | static inline uint32_t tud_cdc_available (void); |
| 116 | static inline int32_t tud_cdc_read_char (void); |
| 117 | static inline uint32_t tud_cdc_read (void* buffer, uint32_t bufsize); |
| 118 | static inline void tud_cdc_read_flush (void); |
| 119 | static inline bool tud_cdc_peek (uint8_t* ui8); |
| 120 | |
| 121 | static inline uint32_t tud_cdc_write_char (char ch); |
| 122 | static inline uint32_t tud_cdc_write (void const* buffer, uint32_t bufsize); |
| 123 | static inline uint32_t tud_cdc_write_str (char const* str); |
| 124 | static inline uint32_t tud_cdc_write_flush (void); |
| 125 | static inline uint32_t tud_cdc_write_available (void); |
| 126 | static inline bool tud_cdc_write_clear (void); |
| 127 | |
| 128 | //--------------------------------------------------------------------+ |
| 129 | // Application Callback API (weak is optional) |
| 130 | //--------------------------------------------------------------------+ |
| 131 | |
| 132 | // Invoked when received new data |
| 133 | TU_ATTR_WEAK void tud_cdc_rx_cb(uint8_t itf); |
| 134 | |
| 135 | // Invoked when received `wanted_char` |
| 136 | TU_ATTR_WEAK void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char); |
| 137 | |
| 138 | // Invoked when space becomes available in TX buffer |
| 139 | TU_ATTR_WEAK void tud_cdc_tx_complete_cb(uint8_t itf); |
| 140 | |
| 141 | // Invoked when line state DTR & RTS are changed via SET_CONTROL_LINE_STATE |
| 142 | TU_ATTR_WEAK void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts); |
| 143 | |
| 144 | // Invoked when line coding is change via SET_LINE_CODING |
| 145 | TU_ATTR_WEAK void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* p_line_coding); |
| 146 | |
| 147 | // Invoked when received send break |
| 148 | TU_ATTR_WEAK void tud_cdc_send_break_cb(uint8_t itf, uint16_t duration_ms); |
| 149 | |
| 150 | //--------------------------------------------------------------------+ |
| 151 | // Inline Functions |
| 152 | //--------------------------------------------------------------------+ |
| 153 | static inline int32_t tud_cdc_n_read_char (uint8_t itf) |
| 154 | { |
| 155 | uint8_t ch; |
| 156 | return tud_cdc_n_read(itf, &ch, 1) ? (int32_t) ch : -1; |
| 157 | } |
| 158 | |
| 159 | static inline uint32_t tud_cdc_n_write_char(uint8_t itf, char ch) |
| 160 | { |
| 161 | return tud_cdc_n_write(itf, &ch, 1); |
| 162 | } |
| 163 | |
| 164 | static inline uint32_t tud_cdc_n_write_str (uint8_t itf, char const* str) |
| 165 | { |
| 166 | return tud_cdc_n_write(itf, str, strlen(str)); |
| 167 | } |
| 168 | |
| 169 | static inline bool tud_cdc_connected (void) |
| 170 | { |
| 171 | return tud_cdc_n_connected(0); |
| 172 | } |
| 173 | |
| 174 | static inline uint8_t tud_cdc_get_line_state (void) |
| 175 | { |
| 176 | return tud_cdc_n_get_line_state(0); |
| 177 | } |
| 178 | |
| 179 | static inline void tud_cdc_get_line_coding (cdc_line_coding_t* coding) |
| 180 | { |
| 181 | tud_cdc_n_get_line_coding(0, coding); |
| 182 | } |
| 183 | |
| 184 | static inline void tud_cdc_set_wanted_char (char wanted) |
| 185 | { |
| 186 | tud_cdc_n_set_wanted_char(0, wanted); |
| 187 | } |
| 188 | |
| 189 | static inline uint32_t tud_cdc_available (void) |
| 190 | { |
| 191 | return tud_cdc_n_available(0); |
| 192 | } |
| 193 | |
| 194 | static inline int32_t tud_cdc_read_char (void) |
| 195 | { |
| 196 | return tud_cdc_n_read_char(0); |
| 197 | } |
| 198 | |
| 199 | static inline uint32_t tud_cdc_read (void* buffer, uint32_t bufsize) |
| 200 | { |
| 201 | return tud_cdc_n_read(0, buffer, bufsize); |
| 202 | } |
| 203 | |
| 204 | static inline void tud_cdc_read_flush (void) |
| 205 | { |
| 206 | tud_cdc_n_read_flush(0); |
| 207 | } |
| 208 | |
| 209 | static inline bool tud_cdc_peek (uint8_t* ui8) |
| 210 | { |
| 211 | return tud_cdc_n_peek(0, ui8); |
| 212 | } |
| 213 | |
| 214 | static inline uint32_t tud_cdc_write_char (char ch) |
| 215 | { |
| 216 | return tud_cdc_n_write_char(0, ch); |
| 217 | } |
| 218 | |
| 219 | static inline uint32_t tud_cdc_write (void const* buffer, uint32_t bufsize) |
| 220 | { |
| 221 | return tud_cdc_n_write(0, buffer, bufsize); |
| 222 | } |
| 223 | |
| 224 | static inline uint32_t tud_cdc_write_str (char const* str) |
| 225 | { |
| 226 | return tud_cdc_n_write_str(0, str); |
| 227 | } |
| 228 | |
| 229 | static inline uint32_t tud_cdc_write_flush (void) |
| 230 | { |
| 231 | return tud_cdc_n_write_flush(0); |
| 232 | } |
| 233 | |
| 234 | static inline uint32_t tud_cdc_write_available(void) |
| 235 | { |
| 236 | return tud_cdc_n_write_available(0); |
| 237 | } |
| 238 | |
| 239 | static inline bool tud_cdc_write_clear(void) |
| 240 | { |
| 241 | return tud_cdc_n_write_clear(0); |
| 242 | } |
| 243 | |
| 244 | /** @} */ |
| 245 | /** @} */ |
| 246 | |
| 247 | //--------------------------------------------------------------------+ |
| 248 | // INTERNAL USBD-CLASS DRIVER API |
| 249 | //--------------------------------------------------------------------+ |
| 250 | void cdcd_init (void); |
| 251 | void cdcd_reset (uint8_t rhport); |
| 252 | uint16_t cdcd_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len); |
| 253 | bool cdcd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t const * request); |
| 254 | bool cdcd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes); |
| 255 | |
| 256 | #ifdef __cplusplus |
| 257 | } |
| 258 | #endif |
| 259 | |
| 260 | #endif /* _TUSB_CDC_DEVICE_H_ */ |