-
Notifications
You must be signed in to change notification settings - Fork 904
/
Copy pathmain.c
80 lines (63 loc) · 1.89 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* Copyright (c) 2024 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdlib.h>
#include <bsp/board_api.h>
#include <tusb.h>
#include <pico/stdio.h>
void custom_cdc_task(void);
int main(void)
{
// Initialize TinyUSB stack
board_init();
tusb_init();
// TinyUSB board init callback after init
if (board_init_after_tusb) {
board_init_after_tusb();
}
// let pico sdk use the first cdc interface for std io
stdio_init_all();
// main run loop
while (1) {
// TinyUSB device task | must be called regurlarly
tud_task();
// custom tasks
custom_cdc_task();
}
// indicate no error
return 0;
}
void custom_cdc_task(void)
{
// polling CDC interfaces if wanted
// Check if CDC interface 0 (for pico sdk stdio) is connected and ready
if (tud_cdc_n_connected(0)) {
// print on CDC 0 some debug message
printf("Connected to CDC 0\n");
sleep_ms(5000); // wait for 5 seconds
}
}
// callback when data is received on a CDC interface
void tud_cdc_rx_cb(uint8_t itf)
{
// allocate buffer for the data in the stack
uint8_t buf[CFG_TUD_CDC_RX_BUFSIZE];
printf("RX CDC %d\n", itf);
// read the available data
// | IMPORTANT: also do this for CDC0 because otherwise
// | you won't be able to print anymore to CDC0
// | next time this function is called
uint32_t count = tud_cdc_n_read(itf, buf, sizeof(buf));
// check if the data was received on the second cdc interface
if (itf == 1) {
// process the received data
buf[count] = 0; // null-terminate the string
// now echo data back to the console on CDC 0
printf("Received on CDC 1: %s\n", buf);
// and echo back OK on CDC 1
tud_cdc_n_write(itf, (uint8_t const *) "OK\r\n", 4);
tud_cdc_n_write_flush(itf);
}
}