Skip to content

Commit b3e63c3

Browse files
committed
updat cdc host app
1 parent cb2af4c commit b3e63c3

File tree

1 file changed

+49
-6
lines changed

1 file changed

+49
-6
lines changed

examples/host/cdc_msc_hid/src/cdc_app.c

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include "tusb.h"
28+
#include "bsp/board.h"
2829

2930
//--------------------------------------------------------------------+
3031
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
@@ -33,19 +34,61 @@
3334

3435
//------------- IMPLEMENTATION -------------//
3536

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+
3652
void cdc_app_task(void)
3753
{
54+
uint8_t buf[64+1]; // +1 for extra null character
55+
memset(buf, 0, sizeof(buf));
3856

57+
size_t count = get_console_inputs(buf, sizeof(buf)-1);
58+
59+
// loop over all mounted interfaces
60+
for(size_t idx=0; idx<CFG_TUH_CDC; idx++)
61+
{
62+
if ( tuh_cdc_mounted(idx) )
63+
{
64+
// console --> cdc interfaces
65+
if (count)
66+
{
67+
tuh_cdc_write(idx, buf, count);
68+
tuh_cdc_write_flush(idx);
69+
}
70+
71+
// cdc interfaces -> console
72+
if ( tuh_cdc_read_available(idx) )
73+
{
74+
printf((char*) buf);
75+
}
76+
}
77+
}
3978
}
4079

41-
void tuh_cdc_mount_cb(uint8_t dev_addr)
80+
void tuh_cdc_mount_cb(uint8_t idx)
4281
{
43-
(void) dev_addr;
44-
printf("A CDC device is mounted\r\n");
82+
tuh_cdc_itf_info_t itf_info;
83+
tuh_cdc_itf_get_info(idx, &itf_info);
84+
85+
printf("CDC Interface is mounted: device address = %u, itf_num = %u\r\n", itf_info.daddr, itf_info.bInterfaceNumber);
4586
}
4687

47-
void tuh_cdc_umount_cb(uint8_t dev_addr)
88+
void tuh_cdc_umount_cb(uint8_t idx)
4889
{
49-
(void) dev_addr;
50-
printf("A CDC device is unmounted\r\n");
90+
tuh_cdc_itf_info_t itf_info;
91+
tuh_cdc_itf_get_info(idx, &itf_info);
92+
93+
printf("CDC Interface is unmounted: device address = %u, itf_num = %u\r\n", itf_info.daddr, itf_info.bInterfaceNumber);
5194
}

0 commit comments

Comments
 (0)