| 
									
										
										
										
											2022-12-14 16:52:28 +07:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * The MIT License (MIT) | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (c) 2022, Ha Thach (tinyusb.org) | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Permission is hereby granted, free of charge, to any person obtaining a copy | 
					
						
							|  |  |  |  * of this software and associated documentation files (the "Software"), to deal | 
					
						
							|  |  |  |  * in the Software without restriction, including without limitation the rights | 
					
						
							|  |  |  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
					
						
							|  |  |  |  * copies of the Software, and to permit persons to whom the Software is | 
					
						
							|  |  |  |  * furnished to do so, subject to the following conditions: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The above copyright notice and this permission notice shall be included in | 
					
						
							|  |  |  |  * all copies or substantial portions of the Software. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
					
						
							|  |  |  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
					
						
							|  |  |  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 
					
						
							|  |  |  |  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
					
						
							|  |  |  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
					
						
							|  |  |  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 
					
						
							|  |  |  |  * THE SOFTWARE. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file is part of the TinyUSB stack. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "tusb.h"
 | 
					
						
							| 
									
										
										
										
											2023-08-03 15:44:05 +07:00
										 |  |  | #include "bsp/board_api.h"
 | 
					
						
							| 
									
										
										
										
											2022-12-14 16:52:28 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-19 15:58:05 +07:00
										 |  |  | size_t get_console_inputs(uint8_t* buf, size_t bufsize) { | 
					
						
							| 
									
										
										
										
											2022-12-21 11:47:07 +07:00
										 |  |  |   size_t count = 0; | 
					
						
							| 
									
										
										
										
											2024-01-19 15:58:05 +07:00
										 |  |  |   while (count < bufsize) { | 
					
						
							| 
									
										
										
										
											2022-12-21 11:47:07 +07:00
										 |  |  |     int ch = board_getchar(); | 
					
						
							| 
									
										
										
										
											2024-01-19 15:58:05 +07:00
										 |  |  |     if (ch <= 0) break; | 
					
						
							| 
									
										
										
										
											2022-12-21 11:47:07 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     buf[count] = (uint8_t) ch; | 
					
						
							|  |  |  |     count++; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return count; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-19 15:58:05 +07:00
										 |  |  | void cdc_app_task(void) { | 
					
						
							|  |  |  |   uint8_t buf[64 + 1]; // +1 for extra null character
 | 
					
						
							|  |  |  |   uint32_t const bufsize = sizeof(buf) - 1; | 
					
						
							| 
									
										
										
										
											2022-12-14 16:52:28 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-21 12:47:00 +07:00
										 |  |  |   uint32_t count = get_console_inputs(buf, bufsize); | 
					
						
							|  |  |  |   buf[count] = 0; | 
					
						
							| 
									
										
										
										
											2022-12-21 11:47:07 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // loop over all mounted interfaces
 | 
					
						
							| 
									
										
										
										
											2024-01-19 15:58:05 +07:00
										 |  |  |   for (uint8_t idx = 0; idx < CFG_TUH_CDC; idx++) { | 
					
						
							|  |  |  |     if (tuh_cdc_mounted(idx)) { | 
					
						
							| 
									
										
										
										
											2022-12-21 11:47:07 +07:00
										 |  |  |       // console --> cdc interfaces
 | 
					
						
							| 
									
										
										
										
											2024-01-19 15:58:05 +07:00
										 |  |  |       if (count) { | 
					
						
							| 
									
										
										
										
											2022-12-21 12:47:00 +07:00
										 |  |  |         tuh_cdc_write(idx, buf, count); | 
					
						
							| 
									
										
										
										
											2022-12-21 11:47:07 +07:00
										 |  |  |         tuh_cdc_write_flush(idx); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-12-14 16:52:28 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-19 15:58:05 +07:00
										 |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // TinyUSB callbacks
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-21 12:47:00 +07:00
										 |  |  | // Invoked when received new data
 | 
					
						
							| 
									
										
										
										
											2024-01-19 15:58:05 +07:00
										 |  |  | void tuh_cdc_rx_cb(uint8_t idx) { | 
					
						
							|  |  |  |   uint8_t buf[64 + 1]; // +1 for extra null character
 | 
					
						
							|  |  |  |   uint32_t const bufsize = sizeof(buf) - 1; | 
					
						
							| 
									
										
										
										
											2022-12-21 12:47:00 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // forward cdc interfaces -> console
 | 
					
						
							|  |  |  |   uint32_t count = tuh_cdc_read(idx, buf, bufsize); | 
					
						
							|  |  |  |   buf[count] = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-22 14:54:35 +07:00
										 |  |  |   printf("%s", (char*) buf); | 
					
						
							| 
									
										
										
										
											2022-12-21 12:47:00 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-19 15:58:05 +07:00
										 |  |  | // Invoked when a device with CDC interface is mounted
 | 
					
						
							|  |  |  | // idx is index of cdc interface in the internal pool.
 | 
					
						
							|  |  |  | void tuh_cdc_mount_cb(uint8_t idx) { | 
					
						
							|  |  |  |   tuh_itf_info_t itf_info = {0}; | 
					
						
							| 
									
										
										
										
											2022-12-21 11:47:07 +07:00
										 |  |  |   tuh_cdc_itf_get_info(idx, &itf_info); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-19 15:58:05 +07:00
										 |  |  |   printf("CDC Interface is mounted: address = %u, itf_num = %u\r\n", itf_info.daddr, | 
					
						
							|  |  |  |          itf_info.desc.bInterfaceNumber); | 
					
						
							| 
									
										
										
										
											2022-12-22 19:41:39 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef CFG_TUH_CDC_LINE_CODING_ON_ENUM
 | 
					
						
							| 
									
										
										
										
											2024-01-20 01:42:31 +07:00
										 |  |  |   // If CFG_TUH_CDC_LINE_CODING_ON_ENUM is defined, line coding will be set by tinyusb stack
 | 
					
						
							|  |  |  |   // while eneumerating new cdc device
 | 
					
						
							| 
									
										
										
										
											2024-01-19 15:58:05 +07:00
										 |  |  |   cdc_line_coding_t line_coding = {0}; | 
					
						
							|  |  |  |   if (tuh_cdc_get_local_line_coding(idx, &line_coding)) { | 
					
						
							| 
									
										
										
										
											2024-04-22 14:54:35 +07:00
										 |  |  |     printf("  Baudrate: %" PRIu32 ", Stop Bits : %u\r\n", line_coding.bit_rate, line_coding.stop_bits); | 
					
						
							| 
									
										
										
										
											2024-01-19 15:58:05 +07:00
										 |  |  |     printf("  Parity  : %u, Data Width: %u\r\n", line_coding.parity, line_coding.data_bits); | 
					
						
							| 
									
										
										
										
											2022-12-22 19:41:39 +07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-01-20 01:42:31 +07:00
										 |  |  | #else
 | 
					
						
							|  |  |  |   // Set Line Coding upon mounted
 | 
					
						
							|  |  |  |   cdc_line_coding_t new_line_coding = { 115200, CDC_LINE_CODING_STOP_BITS_1, CDC_LINE_CODING_PARITY_NONE, 8 }; | 
					
						
							|  |  |  |   tuh_cdc_set_line_coding(idx, &new_line_coding, NULL, 0); | 
					
						
							| 
									
										
										
										
											2022-12-22 19:41:39 +07:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2022-12-14 16:52:28 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-19 15:58:05 +07:00
										 |  |  | // Invoked when a device with CDC interface is unmounted
 | 
					
						
							|  |  |  | void tuh_cdc_umount_cb(uint8_t idx) { | 
					
						
							|  |  |  |   tuh_itf_info_t itf_info = {0}; | 
					
						
							| 
									
										
										
										
											2022-12-21 11:47:07 +07:00
										 |  |  |   tuh_cdc_itf_get_info(idx, &itf_info); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-19 15:58:05 +07:00
										 |  |  |   printf("CDC Interface is unmounted: address = %u, itf_num = %u\r\n", itf_info.daddr, | 
					
						
							|  |  |  |          itf_info.desc.bInterfaceNumber); | 
					
						
							| 
									
										
										
										
											2022-12-14 16:52:28 +07:00
										 |  |  | } |