/** * Copyright (c) 2024 Raspberry Pi (Trading) Ltd. * * SPDX-License-Identifier: BSD-3-Clause */ #include #include #include #include #include #define LED_BLINK_DELAY_MS 1000 int main(void) { // Initialize TinyUSB stack board_init(); tusb_init(); // let pico sdk use the first cdc interface for std io stdio_init_all(); uint32_t start_ms = 0; bool led_state = false; // main run loop while(true) { // TinyUSB device task | must be called regurlarly tud_task(); if(board_millis() - start_ms > LED_BLINK_DELAY_MS) { board_led_write(led_state); led_state = ! led_state; start_ms += LED_BLINK_DELAY_MS; if(tud_cdc_n_connected(0)) { printf("Connected to CDC 0\n"); } if(tud_cdc_n_connected(1)) { printf("Connected to CDC 1\n"); } } } } // 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); } }