From d1d54eaa4144ea85680fceed4d5c0533a33ce432 Mon Sep 17 00:00:00 2001 From: awalol Date: Thu, 30 Apr 2026 14:25:31 +0800 Subject: [PATCH 1/2] add watchdog --- CMakeLists.txt | 2 +- src/bt.cpp | 5 ----- src/main.cpp | 31 +++++++++++++++++++++++++++---- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f2dd82..e9b4679 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -96,7 +96,7 @@ target_include_directories(${PROJECT_NAME} PUBLIC target_link_libraries(ds5-bridge pico_stdlib pico_multicore - hardware_interp + hardware_watchdog hardware_timer pico_btstack_classic # pico_cyw43_arch_threadsafe_background diff --git a/src/bt.cpp b/src/bt.cpp index 49a35bf..1d90494 100644 --- a/src/bt.cpp +++ b/src/bt.cpp @@ -81,11 +81,6 @@ void bt_l2cap_init() { } int bt_init() { - if (cyw43_arch_init()) { - printf("Failed to initialize CYW43\n"); - return 1; - } - critical_section_init(&queue_lock); bt_l2cap_init(); diff --git a/src/main.cpp b/src/main.cpp index a60b55e..3984c2f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,7 @@ #include "audio.h" #include "hardware/clocks.h" #include "hardware/vreg.h" +#include "hardware/watchdog.h" #include "pico/cyw43_arch.h" int reportSeqCounter = 0; @@ -99,24 +100,46 @@ int main() { vreg_set_voltage(VREG_VOLTAGE_1_20); sleep_ms(1000); set_sys_clock_khz(320000, true); - board_init(); + board_init(); tusb_rhport_init_t dev_init = { .role = TUSB_ROLE_DEVICE, - .speed = TUSB_SPEED_AUTO + .speed = TUSB_SPEED_FULL }; tusb_init(BOARD_TUD_RHPORT, &dev_init); - tud_disconnect(); - board_init_after_tusb(); + if (cyw43_arch_init()) { + printf("Failed to initialize CYW43\n"); + return 1; + } + cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, false); + + if (watchdog_caused_reboot()) { + printf("Rebooted by Watchdog!\n"); + // 当崩溃重启以后,闪三下灯 + for (int i = 0;i < 6;i++) { + if (i % 2 == 0) { + cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, true); + }else { + cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, false); + } + sleep_ms(500); + } + } else { + printf("Clean boot\n"); + } + bt_init(); bt_register_data_callback(on_bt_data); audio_init(); + watchdog_enable(1000, true); + while (1) { + watchdog_update(); cyw43_arch_poll(); tud_task(); audio_loop(); From 5b04cbda801572e8722452b2a93f260d2a0e0ac4 Mon Sep 17 00:00:00 2001 From: awalol Date: Thu, 30 Apr 2026 14:44:25 +0800 Subject: [PATCH 2/2] fix: audio resampler_out buffer overflow. static queue element --- src/audio.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/audio.cpp b/src/audio.cpp index b88a3f5..08fc256 100644 --- a/src/audio.cpp +++ b/src/audio.cpp @@ -75,7 +75,7 @@ void audio_loop() { audio_buf[audio_buf_pos++] = raw[i * INPUT_CHANNELS] / 32768.0f * (volume[0] - 1.0f); audio_buf[audio_buf_pos++] = raw[i * INPUT_CHANNELS + 1] / 32768.0f * (volume[0] - 1.0f); if (audio_buf_pos == 512 * 2) { - audio_raw_element element{}; + static audio_raw_element element{}; memcpy(element.data,audio_buf,512 * 2 * 4); if (queue_is_full(&audio_fifo)){ queue_try_remove(&audio_fifo,NULL); @@ -91,7 +91,7 @@ void audio_loop() { } // 3. 48kHz -> 3kHz 重采样 - WDL_ResampleSample out_buf[SAMPLE_SIZE]; // 64 floats = 32帧 × 2ch + static WDL_ResampleSample out_buf[SAMPLE_SIZE]; // 64 floats = 32帧 × 2ch int out_frames = resampler.ResampleOut(out_buf, nframes, SAMPLE_SIZE / OUTPUT_CHANNELS, OUTPUT_CHANNELS); static int8_t haptic_buf[SAMPLE_SIZE]; @@ -169,7 +169,7 @@ void core1_entry() { resampler_audio.SetFeedMode(true); while (true) { - audio_raw_element audio_element{}; + static audio_raw_element audio_element{}; queue_remove_blocking(&audio_fifo,&audio_element); // 将 512 frames 重采样成 480 frames 以解决噪音问题。感谢 @Junhoo WDL_ResampleSample *in_buf; @@ -177,9 +177,9 @@ void core1_entry() { for (int i = 0; i < nframes * 2;i++) { in_buf[i] = audio_element.data[i]; } - WDL_ResampleSample out_buf[200]; + static WDL_ResampleSample out_buf[480 * 2]; resampler_audio.ResampleOut(out_buf,nframes,480,2); - opus_element opus_element{}; + static opus_element opus_element{}; (void)opus_encode_float(encoder,out_buf,480,opus_element.data,200); if (queue_is_full(&opus_fifo)) { queue_try_remove(&opus_fifo,NULL);