/** * Copyright (c) 2024 Raspberry Pi (Trading) Ltd. * * SPDX-License-Identifier: BSD-3-Clause */ #include #include #include #include #include #include #define LED_BLINK_DELAY_MS 1000 int main(void) { // Initialize TinyUSB stack board_init(); tusb_init(); 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)) { // and echo back OK on CDC 1 tud_cdc_n_write(0, (uint8_t const *) "OK\r\n", 4); tud_cdc_n_write_flush(0); } } } } // 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]; // 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)); // process the received data buf[count] = 0; // null-terminate the string // echo back OK tud_cdc_n_write_str(itf, buf); tud_cdc_n_write_char(itf, '\n'); tud_cdc_n_write_flush(itf); }