107 lines
2.8 KiB
C
107 lines
2.8 KiB
C
/*
|
|
* The MIT License (MIT)
|
|
*
|
|
* Copyright (c) 2019 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.
|
|
*
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <pico/stdlib.h>
|
|
#include <tusb.h>
|
|
#include <bsp/board.h>
|
|
|
|
#define LED_BLINK_DELAY_MS 1000
|
|
|
|
void cdc_app_task(void);
|
|
|
|
int main(void)
|
|
{
|
|
board_init();
|
|
|
|
// init host stack on configured roothub port
|
|
const tusb_rhport_init_t rh_init = {
|
|
.role = TUSB_ROLE_HOST,
|
|
.speed = TUH_OPT_HIGH_SPEED ? TUSB_SPEED_HIGH : TUSB_SPEED_FULL,
|
|
};
|
|
|
|
if(!tuh_rhport_init(BOARD_TUH_RHPORT, &rh_init))
|
|
{
|
|
printf("Failed to init rhport.\n");
|
|
return PICO_ERROR_GENERIC;
|
|
}
|
|
|
|
gpio_set_function(12, UART_FUNCSEL_NUM(uart0, 12));
|
|
gpio_set_function(13, UART_FUNCSEL_NUM(uart0, 13));
|
|
|
|
uint32_t start_ms = 0;
|
|
bool led_state = false;
|
|
|
|
while(true)
|
|
{
|
|
// tinyusb host task
|
|
tuh_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;
|
|
}
|
|
|
|
cdc_app_task();
|
|
}
|
|
}
|
|
|
|
void cdc_app_task(void)
|
|
{
|
|
uint8_t buf[64 + 1]; // +1 for extra null character
|
|
uint32_t const bufsize = sizeof(buf) - 1;
|
|
|
|
size_t count = 0;
|
|
|
|
while(count < bufsize)
|
|
{
|
|
int ch = board_getchar();
|
|
if (ch <= 0) break;
|
|
|
|
buf[count] = (uint8_t) ch;
|
|
count++;
|
|
}
|
|
|
|
if(count)
|
|
{
|
|
buf[count] = 0;
|
|
|
|
// loop over all mounted interfaces
|
|
for(uint8_t idx = 0; idx < CFG_TUH_CDC; idx++)
|
|
{
|
|
if(tuh_cdc_mounted(idx))
|
|
{
|
|
// console --> cdc interfaces
|
|
tuh_cdc_write(idx, buf, count);
|
|
tuh_cdc_write_flush(idx);
|
|
}
|
|
}
|
|
}
|
|
}
|