Skip to content

Commit

Permalink
Add option to enable USB serial
Browse files Browse the repository at this point in the history
USB serial is disabled by default
  • Loading branch information
shermp committed Jun 28, 2024
1 parent bee809a commit bf9cc12
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 6 deletions.
13 changes: 10 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ option(ENABLE_BLE_LOG_AUDIO "Enable audio logging" OFF)
option(ENABLE_HCI_DUMP "Enable HCI dump." OFF)
option(ENABLE_UAC2_LOG "Enable USB logging" OFF)
option(ENABLE_PAIR_DEL "Delete all HA pairings" OFF)
option(ENABLE_USB_SERIAL "Enable USB serial support" OFF)

if(ENABLE_BLE_LOG_AUDIO)
list(APPEND pico_asha_defines "ASHA_LOG_AUDIO")
Expand All @@ -46,6 +47,15 @@ if(ENABLE_PAIR_DEL)
list(APPEND pico_asha_defines "ASHA_DELETE_PAIRINGS")
endif()

if(ENABLE_USB_SERIAL)
list(APPEND pico_asha_defines "ASHA_USB_SERIAL")
pico_enable_stdio_usb(pico-asha 1)
pico_enable_stdio_uart(pico-asha 0)
else()
pico_enable_stdio_usb(pico-asha 0)
pico_enable_stdio_uart(pico-asha 1)
endif()

target_compile_definitions(pico-asha PUBLIC ${pico_asha_defines})

pico_add_extra_outputs(pico-asha)
Expand All @@ -61,6 +71,3 @@ target_link_libraries(pico-asha
)

target_link_options(pico-asha PUBLIC -Wl,--print-memory-usage)

pico_enable_stdio_usb(pico-asha 1)
pico_enable_stdio_uart(pico-asha 0)
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ To load the program onto your Pico W, press the `BOOTSEL` button while plugging

### Serial

By default, Pico-ASHA logs output via USB serial using the standard Raspberry Pi Pico serial settings:
By default, Pico-ASHA logs output via UART. To view debugging info over UART, you will need a method to interface with the UART pins. Such options include another pico using the [debugprobe](https://github.com/raspberrypi/debugprobe) project, a USB to UART adapter, The Raspberry Pi debug probe, a full Raspberry Pi or other SBC etc.

Alternatively, Pico-ASHA can log output via USB serial using the standard Raspberry Pi Pico serial settings:
- **Baud Rate:** 115200
- **Data Bits:** 8
- **Stop Bits:"** 1
- **Parity:** None
- **DTR** and **RTS** enabled

Pico-ASHA can also log output via UART. To view debugging info over UART, you will need a method to interface with the UART pins. Such options include another pico using the [debugprobe](https://github.com/raspberrypi/debugprobe) project, a USB to UART adapter, The Raspberry Pi debug probe, a full Raspberry Pi or other SBC etc.
Pass `-DENABLE_USB_SERIAL=ON` to cmake during configure to enable USB serial.

### Reloading program

Expand Down
8 changes: 8 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ void perf_main(void);
#endif

struct asha_audio asha_shared;
#ifdef ASHA_USB_SERIAL
char pico_uid[2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1 + 4];
#else
char pico_uid[2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1];
#endif

int main()
{
// Get serial
pico_get_unique_board_id_string(pico_uid, sizeof pico_uid);
#ifdef ASHA_USB_SERIAL
const char uid_suffix[4] = "-CDC";
memcpy(pico_uid + (sizeof(pico_uid) - sizeof(uid_suffix) - 1), uid_suffix, sizeof(uid_suffix));
#endif
// Init TinyUSB before stdio init
board_init();
// init device stack on configured roothub port
Expand Down
4 changes: 4 additions & 0 deletions src/pico_asha.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

#include <pico/unique_id.h>

#ifdef ASHA_USB_SERIAL
extern char pico_uid[2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1 + 4];
#else
extern char pico_uid[2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1];
#endif

#endif // PICO_ASHA_H
6 changes: 6 additions & 0 deletions src/tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ extern "C" {
#endif

//------------- CLASS -------------//
#ifdef ASHA_USB_SERIAL
#define CFG_TUD_CDC 1
#else
#define CFG_TUD_CDC 0
#endif
#define CFG_TUD_MSC 0
#define CFG_TUD_HID 0
#define CFG_TUD_MIDI 0
Expand Down Expand Up @@ -163,9 +167,11 @@ extern "C" {
// Size of control request buffer
#define CFG_TUD_AUDIO_FUNC_1_CTRL_BUF_SZ 64

#ifdef ASHA_USB_SERIAL
// CDC buffer sizes
#define CFG_TUD_CDC_RX_BUFSIZE (256)
#define CFG_TUD_CDC_TX_BUFSIZE (256)
#endif

#ifdef __cplusplus
}
Expand Down
19 changes: 18 additions & 1 deletion src/usb_descriptors.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,50 +76,64 @@ uint8_t const * tud_descriptor_device_cb(void)
//--------------------------------------------------------------------+
// Configuration Descriptor
//--------------------------------------------------------------------+
#ifdef ASHA_USB_SERIAL
#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + CFG_TUD_AUDIO * TUD_AUDIO_HEADSET_STEREO_DESC_LEN + CFG_TUD_CDC * TUD_CDC_DESC_LEN)
#else
#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + CFG_TUD_AUDIO * TUD_AUDIO_HEADSET_STEREO_DESC_LEN)
#endif

#if CFG_TUSB_MCU == OPT_MCU_LPC175X_6X || CFG_TUSB_MCU == OPT_MCU_LPC177X_8X || CFG_TUSB_MCU == OPT_MCU_LPC40XX
// LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number
// 0 control, 1 In, 2 Bulk, 3 Iso, 4 In etc ...
#define EPNUM_AUDIO_IN 0x03
#define EPNUM_AUDIO_OUT 0x03
#ifdef ASHA_USB_SERIAL
#define EPNUM_CDC_NOTIF 0x84
#define EPNUM_CDC_OUT 0x05
#define EPNUM_CDC_IN 0x85
#endif

#elif CFG_TUSB_MCU == OPT_MCU_NRF5X
// ISO endpoints for NRF5x are fixed to 0x08 (0x88)
#define EPNUM_AUDIO_IN 0x08
#define EPNUM_AUDIO_OUT 0x08
#ifdef ASHA_USB_SERIAL
#define EPNUM_CDC_NOTIF 0x81
#define EPNUM_CDC_OUT 0x02
#define EPNUM_CDC_IN 0x82
#endif

#elif CFG_TUSB_MCU == OPT_MCU_SAMG || CFG_TUSB_MCU == OPT_MCU_SAMX7X
// SAMG & SAME70 don't support a same endpoint number with different direction IN and OUT
// e.g EP1 OUT & EP1 IN cannot exist together
#define EPNUM_AUDIO_IN 0x01
#define EPNUM_AUDIO_OUT 0x02
#ifdef ASHA_USB_SERIAL
#define EPNUM_CDC_NOTIF 0x83
#define EPNUM_CDC_OUT 0x04
#define EPNUM_CDC_IN 0x85
#endif

#elif CFG_TUSB_MCU == OPT_MCU_FT90X || CFG_TUSB_MCU == OPT_MCU_FT93X
// FT9XX doesn't support a same endpoint number with different direction IN and OUT
// e.g EP1 OUT & EP1 IN cannot exist together
#define EPNUM_AUDIO_IN 0x01
#define EPNUM_AUDIO_OUT 0x02
#ifdef ASHA_USB_SERIAL
#define EPNUM_CDC_NOTIF 0x83
#define EPNUM_CDC_OUT 0x04
#define EPNUM_CDC_IN 0x85
#endif

#else
#define EPNUM_AUDIO_IN 0x01
#define EPNUM_AUDIO_OUT 0x01
#ifdef ASHA_USB_SERIAL
#define EPNUM_CDC_NOTIF 0x83
#define EPNUM_CDC_OUT 0x04
#define EPNUM_CDC_IN 0x84
#endif
#endif

uint8_t const desc_configuration[] =
{
Expand All @@ -128,9 +142,10 @@ uint8_t const desc_configuration[] =

// Interface number, string index, EP Out & EP In address, EP size
TUD_AUDIO_HEADSET_STEREO_DESCRIPTOR(2, EPNUM_AUDIO_OUT, EPNUM_AUDIO_IN | 0x80),

#ifdef ASHA_USB_SERIAL
// CDC: Interface number, string index, EP notification address and size, EP data address (out, in) and size.
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 6, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, 64)
#endif
};

// Invoked when received GET CONFIGURATION DESCRIPTOR
Expand All @@ -155,7 +170,9 @@ char const* string_desc_arr [] =
pico_uid, // 3: Unique serial
"Pico-ASHA Speakers", // 4: Audio Interface
"Pico-ASHA Microphone", // 5: Audio Interface
#ifdef ASHA_USB_SERIAL
"Pico-ASHA CDC", // 6: CDC Interface
#endif
};

static uint16_t _desc_str[32];
Expand Down
2 changes: 2 additions & 0 deletions src/usb_descriptors.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ enum
ITF_NUM_AUDIO_CONTROL = 0,
ITF_NUM_AUDIO_STREAMING_SPK,
ITF_NUM_AUDIO_STREAMING_MIC,
#ifdef ASHA_USB_SERIAL
ITF_NUM_CDC,
ITF_NUM_CDC_DATA,
#endif
ITF_NUM_TOTAL
};

Expand Down

0 comments on commit bf9cc12

Please sign in to comment.