host msc example work well with rp2040 pio-usb

This commit is contained in:
hathach 2022-11-21 15:49:38 +07:00
parent ba9f88f18c
commit 460bef9dbb
No known key found for this signature in database
GPG Key ID: F5D50C6D51D17CBA
11 changed files with 43 additions and 37 deletions

View File

@ -69,7 +69,7 @@ int main(void)
// echo // echo
uint8_t ch; uint8_t ch;
if ( board_uart_read(&ch, 1) ) if ( board_uart_read(&ch, 1) > 0 )
{ {
board_uart_write(&ch, 1); board_uart_write(&ch, 1);
} }

View File

@ -29,4 +29,16 @@ target_include_directories(${PROJECT} PUBLIC
family_configure_host_example(${PROJECT}) family_configure_host_example(${PROJECT})
# For rp2040, un-comment to enable pico-pio-usb # For rp2040, un-comment to enable pico-pio-usb
# family_add_pico_pio_usb(${PROJECT}) family_add_pico_pio_usb(${PROJECT})
# due to warnings from Pico-PIO-USB
target_compile_options(${PROJECT} PUBLIC
-Wno-error=shadow
-Wno-error=cast-align
-Wno-error=cast-qual
-Wno-error=redundant-decls
-Wno-error=sign-conversion
-Wno-error=conversion
-Wno-error=sign-compare
-Wno-error=unused-function
)

View File

@ -54,13 +54,8 @@ int main(void)
tuh_task(); tuh_task();
led_blinking_task(); led_blinking_task();
#if CFG_TUH_CDC
cdc_task(); cdc_task();
#endif
#if CFG_TUH_HID
hid_app_task(); hid_app_task();
#endif
} }
return 0; return 0;
@ -69,7 +64,6 @@ int main(void)
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// USB CDC // USB CDC
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
#if CFG_TUH_CDC
CFG_TUSB_MEM_SECTION static char serial_in_buffer[64] = { 0 }; CFG_TUSB_MEM_SECTION static char serial_in_buffer[64] = { 0 };
// invoked ISR context // invoked ISR context
@ -90,8 +84,6 @@ void cdc_task(void)
} }
#endif
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// TinyUSB Callbacks // TinyUSB Callbacks
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+

View File

@ -25,8 +25,6 @@
#include "tusb.h" #include "tusb.h"
#if CFG_TUH_MSC
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// MACRO TYPEDEF CONSTANT ENUM DECLARATION // MACRO TYPEDEF CONSTANT ENUM DECLARATION
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
@ -103,4 +101,3 @@ void tuh_msc_umount_cb(uint8_t dev_addr)
// } // }
} }
#endif

View File

@ -34,6 +34,12 @@
// Board Specific Configuration // Board Specific Configuration
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
#if CFG_TUSB_MCU == OPT_MCU_RP2040
// change to 1 if using pico-pio-usb as host controller for raspberry rp2040
#define CFG_TUH_RPI_PIO_USB 0
#define BOARD_TUH_RHPORT CFG_TUH_RPI_PIO_USB
#endif
// RHPort number used for host can be defined by board.mk, default to port 0 // RHPort number used for host can be defined by board.mk, default to port 0
#ifndef BOARD_TUH_RHPORT #ifndef BOARD_TUH_RHPORT
#define BOARD_TUH_RHPORT 0 #define BOARD_TUH_RHPORT 0

View File

@ -162,7 +162,6 @@ void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_re
void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance)
{ {
printf("HID device address = %d, instance = %d is unmounted\r\n", dev_addr, instance); printf("HID device address = %d, instance = %d is unmounted\r\n", dev_addr, instance);
} }
// check if different than 2 // check if different than 2

View File

@ -35,6 +35,7 @@
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
void led_blinking_task(void); void led_blinking_task(void);
// from msc_app.c
extern bool msc_app_init(void); extern bool msc_app_init(void);
extern void msc_app_task(void); extern void msc_app_task(void);
@ -75,7 +76,6 @@ void tuh_umount_cb(uint8_t dev_addr)
(void) dev_addr; (void) dev_addr;
} }
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Blinking Task // Blinking Task
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+

View File

@ -25,6 +25,7 @@
#include <ctype.h> #include <ctype.h>
#include "tusb.h" #include "tusb.h"
#include "bsp/board.h"
#include "ff.h" #include "ff.h"
#include "diskio.h" #include "diskio.h"
@ -75,13 +76,13 @@ void msc_app_task(void)
{ {
if (!_cli) return; if (!_cli) return;
int ch = getchar(); int ch = board_uart_getchar();
if ( ch > 0 ) if ( ch > 0 )
{ {
while( ch > 0 ) while( ch > 0 )
{ {
embeddedCliReceiveChar(_cli, (char) ch); embeddedCliReceiveChar(_cli, (char) ch);
ch = getchar(); ch = board_uart_getchar();
} }
embeddedCliProcess(_cli); embeddedCliProcess(_cli);
} }

View File

@ -132,15 +132,11 @@ static inline void board_delay(uint32_t ms)
} }
} }
// stdio getchar() is blocking, this is non-blocking version for uart
static inline int board_uart_getchar(void) static inline int board_uart_getchar(void)
{ {
uint8_t c; uint8_t c;
return board_uart_read(&c, 1) ? (int) c : (-1); return ( board_uart_read(&c, 1) > 0 ) ? (int) c : (-1);
}
static inline int board_uart_putchar(uint8_t c)
{
return board_uart_write(&c, 1);
} }
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -121,16 +121,6 @@ static uart_inst_t *uart_inst;
void board_init(void) void board_init(void)
{ {
#ifdef LED_PIN
bi_decl(bi_1pin_with_name(LED_PIN, "LED"));
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
#endif
// Button
#ifndef BUTTON_BOOTSEL
#endif
#if CFG_TUH_RPI_PIO_USB || CFG_TUD_RPI_PIO_USB #if CFG_TUH_RPI_PIO_USB || CFG_TUD_RPI_PIO_USB
// Set the system clock to a multiple of 120mhz for bitbanging USB with pico-usb // Set the system clock to a multiple of 120mhz for bitbanging USB with pico-usb
set_sys_clock_khz(120000, true); set_sys_clock_khz(120000, true);
@ -148,6 +138,16 @@ void board_init(void)
tuh_configure(BOARD_TUH_RHPORT, TUH_CFGID_RPI_PIO_USB_CONFIGURATION, &pio_cfg); tuh_configure(BOARD_TUH_RHPORT, TUH_CFGID_RPI_PIO_USB_CONFIGURATION, &pio_cfg);
#endif #endif
#ifdef LED_PIN
bi_decl(bi_1pin_with_name(LED_PIN, "LED"));
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
#endif
// Button
#ifndef BUTTON_BOOTSEL
#endif
#if defined(UART_DEV) && defined(LIB_PICO_STDIO_UART) #if defined(UART_DEV) && defined(LIB_PICO_STDIO_UART)
bi_decl(bi_2pins_with_func(UART_TX_PIN, UART_TX_PIN, GPIO_FUNC_UART)); bi_decl(bi_2pins_with_func(UART_TX_PIN, UART_TX_PIN, GPIO_FUNC_UART));
uart_inst = uart_get_instance(UART_DEV); uart_inst = uart_get_instance(UART_DEV);
@ -192,10 +192,13 @@ uint32_t board_button_read(void)
int board_uart_read(uint8_t* buf, int len) int board_uart_read(uint8_t* buf, int len)
{ {
#ifdef UART_DEV #ifdef UART_DEV
for(int i=0;i<len;i++) { int count = 0;
buf[i] = uart_getc(uart_inst); while ( (count < len) && uart_is_readable(uart_inst) )
{
buf[count] = uart_getc(uart_inst);
count++;
} }
return len; return count;
#else #else
(void) buf; (void) len; (void) buf; (void) len;
return 0; return 0;

View File

@ -71,7 +71,7 @@ struct tuh_xfer_s
// ConfigID for tuh_config() // ConfigID for tuh_config()
enum enum
{ {
TUH_CFGID_RPI_PIO_USB_CONFIGURATION = OPT_MCU_RP2040 // cfg_param: pio_usb_configuration_t TUH_CFGID_RPI_PIO_USB_CONFIGURATION = OPT_MCU_RP2040 << 8 // cfg_param: pio_usb_configuration_t
}; };
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+