diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d793d77..7462382 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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] diff --git a/CMakeLists.txt b/CMakeLists.txt index 876727b..51979fd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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() @@ -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) diff --git a/doc/command_protocol.md b/doc/command_protocol.md index 9aed6f5..c010278 100644 --- a/doc/command_protocol.md +++ b/doc/command_protocol.md @@ -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 diff --git a/src/asha_bt.cpp b/src/asha_bt.cpp index a09d62a..1268cc2 100644 --- a/src/asha_bt.cpp +++ b/src/asha_bt.cpp @@ -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" @@ -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"]; @@ -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); @@ -250,7 +259,6 @@ 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()) { @@ -258,7 +266,7 @@ extern "C" void bt_main() } sleep_ms(250); } -#endif + if (!runtime_settings) { LOG_ERROR("Runtime settings not initialised"); } diff --git a/src/asha_unique_id.hpp b/src/asha_unique_id.hpp index 2730e0d..1597d63 100644 --- a/src/asha_unique_id.hpp +++ b/src/asha_unique_id.hpp @@ -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 diff --git a/src/asha_usb_serial.hpp b/src/asha_usb_serial.hpp index 51773c3..9baba2d 100644 --- a/src/asha_usb_serial.hpp +++ b/src/asha_usb_serial.hpp @@ -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"; }; diff --git a/src/main.cpp b/src/main.cpp index 8d161e8..18be476 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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 diff --git a/src/tusb_config.h b/src/tusb_config.h index a585aac..3e78d2d 100644 --- a/src/tusb_config.h +++ b/src/tusb_config.h @@ -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" //--------------------------------------------------------------------+ @@ -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 @@ -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 } diff --git a/src/usb_descriptors.cpp b/src/usb_descriptors.cpp index 443aa49..581948f 100644 --- a/src/usb_descriptors.cpp +++ b/src/usb_descriptors.cpp @@ -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[] = { @@ -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 @@ -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]; diff --git a/src/usb_descriptors.h b/src/usb_descriptors.h index f0c1443..69943fe 100644 --- a/src/usb_descriptors.h +++ b/src/usb_descriptors.h @@ -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 };