|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2022, 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 | +#include "tusb.h" |
| 28 | +#include "bsp/board.h" |
| 29 | + |
| 30 | +//--------------------------------------------------------------------+ |
| 31 | +// MACRO TYPEDEF CONSTANT ENUM DECLARATION |
| 32 | +//--------------------------------------------------------------------+ |
| 33 | + |
| 34 | + |
| 35 | +//------------- IMPLEMENTATION -------------// |
| 36 | + |
| 37 | +size_t get_console_inputs(uint8_t* buf, size_t bufsize) |
| 38 | +{ |
| 39 | + size_t count = 0; |
| 40 | + while (count < bufsize) |
| 41 | + { |
| 42 | + int ch = board_getchar(); |
| 43 | + if ( ch <= 0 ) break; |
| 44 | + |
| 45 | + buf[count] = (uint8_t) ch; |
| 46 | + count++; |
| 47 | + } |
| 48 | + |
| 49 | + return count; |
| 50 | +} |
| 51 | + |
| 52 | +void cdc_app_task(void) |
| 53 | +{ |
| 54 | + uint8_t buf[64+1]; // +1 for extra null character |
| 55 | + uint32_t const bufsize = sizeof(buf)-1; |
| 56 | + |
| 57 | + uint32_t count = get_console_inputs(buf, bufsize); |
| 58 | + buf[count] = 0; |
| 59 | + |
| 60 | + // loop over all mounted interfaces |
| 61 | + for(uint8_t idx=0; idx<CFG_TUH_CDC; idx++) |
| 62 | + { |
| 63 | + if ( tuh_cdc_mounted(idx) ) |
| 64 | + { |
| 65 | + // console --> cdc interfaces |
| 66 | + if (count) |
| 67 | + { |
| 68 | + tuh_cdc_write(idx, buf, count); |
| 69 | + tuh_cdc_write_flush(idx); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// Invoked when received new data |
| 76 | +void tuh_cdc_rx_cb(uint8_t idx) |
| 77 | +{ |
| 78 | + uint8_t buf[64+1]; // +1 for extra null character |
| 79 | + uint32_t const bufsize = sizeof(buf)-1; |
| 80 | + |
| 81 | + // forward cdc interfaces -> console |
| 82 | + uint32_t count = tuh_cdc_read(idx, buf, bufsize); |
| 83 | + buf[count] = 0; |
| 84 | + |
| 85 | + printf((char*) buf); |
| 86 | +} |
| 87 | + |
| 88 | +void tuh_cdc_mount_cb(uint8_t idx) |
| 89 | +{ |
| 90 | + tuh_cdc_itf_info_t itf_info = { 0 }; |
| 91 | + tuh_cdc_itf_get_info(idx, &itf_info); |
| 92 | + |
| 93 | + printf("CDC Interface is mounted: address = %u, itf_num = %u\r\n", itf_info.daddr, itf_info.bInterfaceNumber); |
| 94 | + |
| 95 | +#ifdef CFG_TUH_CDC_LINE_CODING_ON_ENUM |
| 96 | + // CFG_TUH_CDC_LINE_CODING_ON_ENUM must be defined for line coding is set by tinyusb in enumeration |
| 97 | + // otherwise you need to call tuh_cdc_set_line_coding() first |
| 98 | + cdc_line_coding_t line_coding = { 0 }; |
| 99 | + if ( tuh_cdc_get_local_line_coding(idx, &line_coding) ) |
| 100 | + { |
| 101 | + printf(" Baudrate: %lu, Stop Bits : %u\r\n", line_coding.bit_rate, line_coding.stop_bits); |
| 102 | + printf(" Parity : %u, Data Width: %u\r\n", line_coding.parity , line_coding.data_bits); |
| 103 | + } |
| 104 | +#endif |
| 105 | +} |
| 106 | + |
| 107 | +void tuh_cdc_umount_cb(uint8_t idx) |
| 108 | +{ |
| 109 | + tuh_cdc_itf_info_t itf_info = { 0 }; |
| 110 | + tuh_cdc_itf_get_info(idx, &itf_info); |
| 111 | + |
| 112 | + printf("CDC Interface is unmounted: address = %u, itf_num = %u\r\n", itf_info.daddr, itf_info.bInterfaceNumber); |
| 113 | +} |
0 commit comments