Skip to content

Commit

Permalink
Always enable USB serial. Make UART configurable at run time
Browse files Browse the repository at this point in the history
  • Loading branch information
shermp committed Aug 31, 2024
1 parent 2db7831 commit 947bce0
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 79 deletions.
34 changes: 9 additions & 25 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,36 +61,20 @@ jobs:
echo "PICO_SDK_PATH=$(realpath ./pico-sdk)" >> "$GITHUB_ENV"
echo "PICO_BTSTACK_PATH=$(realpath ./btstack)" >> "$GITHUB_ENV"
- name: Configure build (USB Serial)
- name: Configure build
run: |
mkdir pico-asha/build-usb
cd pico-asha/build-usb
cmake -DCMAKE_BUILD_TYPE=MinSizeRel -DENABLE_USB_SERIAL=ON ..
- name: Configure build (UART Serial)
run: |
mkdir pico-asha/build-uart
cd pico-asha/build-uart
cmake -DCMAKE_BUILD_TYPE=MinSizeRel -DENABLE_USB_SERIAL=OFF ..
- name: Compile (USB Serial)
run: |
cd pico-asha/build-usb
cmake --build .
mkdir pico-asha/build
cd pico-asha/build
cmake -DCMAKE_BUILD_TYPE=MinSizeRel ..
- name: Compile (UART Serial)
- name: Compile
run: |
cd pico-asha/build-uart
cd pico-asha/build
cmake --build .
- name: Upload UF2 and ELF binaries (USB Serial)
- name: Upload UF2 and ELF binaries
uses: actions/upload-artifact@v4
with:
name: pico-asha-bin-usb
path: pico-asha/build-usb/pico-asha.[ue][fl][2f]
name: pico-asha-bin
path: pico-asha/build/pico-asha.[ue][fl][2f]

- name: Upload UF2 and ELF binaries (UART Serial)
uses: actions/upload-artifact@v4
with:
name: pico-asha-bin-uart
path: pico-asha/build-uart/pico-asha.[ue][fl][2f]
16 changes: 4 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,13 @@ target_include_directories(pico-asha PUBLIC src/ lib/)
option(ENABLE_BLE_AD_DUMP "Enable dumping of advertisement packet data" 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" ON)

list(APPEND pico_asha_defines "PICO_ASHA_VERS=\"${PICO_ASHA_VERSION}\"")

list(APPEND pico_asha_defines "PICO_STDIO_USB_ENABLE_RESET_VIA_BAUD_RATE=1")
pico_enable_stdio_usb(pico-asha 1)
pico_enable_stdio_uart(pico-asha 1)

if(ENABLE_BLE_AD_DUMP)
list(APPEND pico_asha_defines "ASHA_AD_DUMP")
endif()
Expand All @@ -70,17 +73,6 @@ 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")
list(APPEND pico_asha_defines "PICO_STDIO_USB_ENABLE_RESET_VIA_BAUD_RATE=1")

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()

include(CheckCXXCompilerFlag)
check_cxx_compiler_flag(-Wdeprecated-enum-enum-conversion HAVE_DEP_ENNUM_ENUM_CONV)

Expand Down
20 changes: 20 additions & 0 deletions doc/command_protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,26 @@ Note: examples below include whitespace for clarity. They MUST be packed for tra
}
```

### Set UART Serial

#### Command

```json
{
"cmd": "uart_serial",
"enable": true
}
```

#### Response

```json
{
"cmd": "uart_serial",
"success": true
}
```

### Set Log Level

#### Command
Expand Down
20 changes: 14 additions & 6 deletions src/asha_bt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
#include "pico/time.h"
#include "hardware/watchdog.h"

#ifdef ASHA_USB_SERIAL
#include "pico/stdio_usb.h"
#endif
#include "pico/stdio_usb.h"
#include "pico/stdio_uart.h"

#include "etl/string.h"

Expand Down Expand Up @@ -201,10 +200,18 @@ static void handle_stdin_line_worker(async_context_t *context, async_when_pendin
delete_paired_devices();
resp_doc["success"] = true;

} else if (str_eq(cmd, SerCmd::UartSerial)) {
bool uart_enabled = cmd_doc["enabled"];
if (runtime_settings.set_uart_enabled(uart_enabled)) {
stdio_set_driver_enabled(&stdio_uart, runtime_settings.serial_uart_enabled);
}
resp_doc["success"] = true;

} else if (str_eq(cmd, SerCmd::WaitUSBSerCx)) {
bool wait = cmd_doc["wait"];
runtime_settings.set_wait_for_usb_serial_cx(wait);
resp_doc["success"] = true;


} else if (str_eq(cmd, SerCmd::LogLevel)) {
const char* log_level = cmd_doc["level"];
Expand Down Expand Up @@ -240,7 +247,9 @@ extern "C" void bt_main()
}
runtime_settings.init();
runtime_settings.get_settings();


stdio_set_driver_enabled(&stdio_uart, runtime_settings.serial_uart_enabled);

async_context_t *ctx = cyw43_arch_async_context();
bt_audio_pending_worker.do_work = handle_bt_audio_pending_worker;
async_context_add_when_pending_worker(ctx, &bt_audio_pending_worker);
Expand All @@ -250,15 +259,14 @@ extern "C" void bt_main()
async_context_add_when_pending_worker(ctx, &stdin_pending_worker);
usb_ser_ctx = ctx;

#ifdef ASHA_USB_SERIAL
if (runtime_settings.wait_for_usb_serial_cx) {
// Allow time for USB serial to connect before proceeding
while (!stdio_usb_connected()) {
sleep_ms(250);
}
sleep_ms(250);
}
#endif

if (!runtime_settings) {
LOG_ERROR("Runtime settings not initialised");
}
Expand Down
7 changes: 2 additions & 5 deletions src/asha_unique_id.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@

namespace asha
{
#ifdef ASHA_USB_SERIAL
constexpr size_t pico_uid_size = (2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1 + 4);
#else
constexpr size_t pico_uid_size = (2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1);
#endif

constexpr size_t pico_uid_size = (2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1 + 4);
extern char pico_uid[pico_uid_size];

} // namespace asha
1 change: 1 addition & 0 deletions src/asha_usb_serial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace asha
constexpr const char Unpair[] = "unpair";
constexpr const char ClearDevDb[] = "clear_dev_db";
constexpr const char WaitUSBSerCx[] = "wait_usb_ser_cx";
constexpr const char UartSerial[] = "uart_serial";
constexpr const char LogLevel[] = "log_level";
constexpr const char HCIDump[] = "hci_dump";
};
Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ extern "C" int main()
audio_buff.init();
// Get serial
pico_get_unique_board_id_string(pico_uid, sizeof pico_uid);
#ifdef ASHA_USB_SERIAL

const char uid_suffix[4] = {'-', 'C', 'D', 'C'};
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
11 changes: 2 additions & 9 deletions src/tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
extern "C" {
#endif

#ifdef ASHA_USB_SERIAL
#include "pico/stdio_usb.h"
#endif
#include "pico/stdio_usb.h"
#include "usb_descriptors.h"

//--------------------------------------------------------------------+
Expand Down Expand Up @@ -97,11 +95,8 @@ 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 @@ -170,11 +165,9 @@ 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: 1 addition & 18 deletions src/usb_descriptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,64 +79,50 @@ extern "C" 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 @@ -145,10 +131,9 @@ 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 @@ -173,9 +158,7 @@ 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: 0 additions & 2 deletions src/usb_descriptors.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ 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 947bce0

Please sign in to comment.