main.cMic-Triggered LED Firmware
avg, peak, and span values; if they remain zero or constant, the remaining fault is likely microphone power, soldering, acoustic-port blockage, or GPIO9/10/11 continuity.src/: mainsrc/: avocaudio_boardsrc/: lora_uartmain.c (10,402 characters), avocaudio_board.h (1,471), lora_uart.pio (1,637), README.md (2,052), and the combined bundle (17,110); the bundle contains all six intended paths.Text
avocaudio-bringup/ ├── CMakeLists.txt ├── pico_sdk_import.cmake ├── README.md └── src/ ├── main.c ├── avocaudio_board.h └── lora_uart.pio
Table
| Function | RP2040 GPIO | Firmware use / constraint |
|---|---|---|
| LoRa-E5 receive | 0 | PIO UART RX; connected to module PB6/UART1_TX |
| LoRa-E5 transmit | 1 | PIO UART TX; connected to module PB7/UART1_RX |
| LoRa reset | 12 | Configurable reset output; module RST is a reset-trigger input |
| Optional user input | 13 | Unconnected; disabled with AVO_USER_INPUT_GPIO=-1 |
| DHT data | 16 | Bounded single-wire probe; external 5 kΩ pull-up exists |
| SD CLK | 17 | Low-speed bit-banged SPI clock |
| SD CMD | 18 | Bit-banged MOSI |
| SD DAT0 | 19 | Bit-banged MISO |
| SD DAT1 | 20 | Pulled-up input during diagnostic |
| SD DAT2 | 21 | Pulled-up input during diagnostic |
| SD DAT3/CS | 22 | Bit-banged chip select |
| User LED | 25 | Configurable active-high output |
| Native QSPI | Dedicated pins | Boot/XIP flash only; never reconfigured |
| USB D+/D− | Native USB | USB CDC stdio |
AVO_SD_PERIODIC_RETRY=0; enabling it can introduce a bounded approximately 1 second worst-case cooperative pause due to ACMD41 retries.0x9F through flash_do_cmd() with interrupts disabled around the RAM-resident transaction. Expected W25Q128JV bytes are EF 40 18. The SDK unique board ID is also reported; all operations are read-only.pull side 1 [7] supplies stop/idle before the next start bit. Reset, settle, AT\r\n, bounded receive, raw response, pass/fail, and retry are state-driven. Default 9600 8N1 is configurable.PICO_SDK_PATH to the SDK root.Sh
mkdir build cd build cmake -DPICO_BOARD=pico .. cmake --build . -j
build/avocaudio_bringup.uf2 to RPI-RP2.Text
AvocAudio v3 Rev 2 RP2040 bring-up ==================================== RESET watchdog=no watchdog-enable=no INPUTS SW2=RUN unreadable; SW1=BOOTSEL/flash-CS not sampled; user GPIO=-1 LED GPIO25 active-high DHT GPIO16 auto-interpret DHT11/DHT22 SD CLK=17 CMD/MOSI=18 DAT0/MISO=19 DAT3/CS=22 bit-banged diagnostic LORA PIO UART RX=0 TX=1 NRST=12 9600 8N1 (defaults require confirmation) FLASH PASS JEDEC=ef 40 18 expected=ef 40 18 UID=... (read-only) SD PASS SPI init type=SDHC/SDXC OCR=... (no filesystem mount) LORA PASS baud=9600 raw="...OK..."
079c6f390236; USB stdio, flash_do_cmd, watchdog, and unique-ID APIs were present. That declaration check is useful evidence but is not a substitute for CMake configuration, compilation, linking, or hardware testing. Exact build compatibility remains unverified until the commands above complete with an installed SDK/toolchain.LORA_NRST and STM32 convention, configurable, and not explicit in the available module datasheet.platform = raspberrypi, board = pico, and framework = picosdk. A pre-build script invokes the installed Pico SDK pioasm to generate src/lora_uart.pio.h from src/lora_uart.pio. The original C, header, and PIO behavior was not changed; therefore the corrected LoRa 8N1 stop interval, GPIO mapping (LoRa 0/1/12, DHT 16, SD 17–22, LED 25), no SW1/SW2 sampling, and read-only QSPI diagnostics remain intact.Sh
python -m pip install platformio pio run -t clean pio run
pip reported Temporary failure in name resolution followed by No matching distribution found for platformio; both pio commands returned exit 127 (pio: command not found). A complete filesystem/cache search found no PlatformIO Core, cached PlatformIO package, RP2040 platform, Pico SDK, pioasm, or Arm GNU toolchain. Available local versions were Python 3.13.5, pip 25.1.1, and CMake 3.31.6..uf2, ELF usage report, artifact checksum, resolved PlatformIO/platform/Pico SDK/compiler versions, or successful clean-build evidence exists from this attempt. The pico board definition normally links for the standard Pico flash layout rather than automatically using the fitted 16 MB W25Q128; no custom incompatible memory layout was introduced.raspberrypi@1.20.0, Arduino-mbed 4.6.0, Arm GCC 9.2.1b63385bb3500d879dc235a887f595036c067e1a65642196e7272c645cdeef593Sh
python -m pip install platformio pio run -t clean pio run
.pio/build/pico/firmware.uf2; copy it to the RP2040 RPI-RP2 mass-storage device while in BOOTSEL mode.Table
| Function | RP2040 GPIO | Schematic path | Direction |
|---|---|---|---|
| Microphone WS/LRCLK | 9 | GPIO9 → R17 50 Ω → MK3 WS | Output |
| Microphone SD | 10 | MK3 SD → R12 50 Ω → GPIO10 | Input |
| Microphone SCK/BCLK | 11 | GPIO11 → R16 50 Ω → MK3 SCK | Output |
| User LED | 25 | GPIO25 → D2 → R19 75 Ω → GND | Output, active high |
| Microphone channel select | — | MK3 LR → R15 10 kΩ → GND | Left channel |
CMakeLists.txtCmake
cmake_minimum_required(VERSION 3.13) include(pico_sdk_import.cmake) project(avocaudio_sound_led C CXX ASM) set(CMAKE_C_STANDARD 11) set(CMAKE_CXX_STANDARD 17) pico_sdk_init() add_executable(avocaudio_sound_led main.c ) target_link_libraries(avocaudio_sound_led pico_stdlib ) pico_enable_stdio_usb(avocaudio_sound_led 0) pico_enable_stdio_uart(avocaudio_sound_led 0) pico_add_extra_outputs(avocaudio_sound_led)
pico_sdk_import.cmake from the Pico SDK examples or copy the one already linked in this project file.main.cC
#include <stdbool.h> #include <stdint.h> #include <stdlib.h> #include "pico/stdlib.h" #include "hardware/gpio.h" // Schematic-grounded pin assignment. #define MIC_WS_PIN 9u #define MIC_SD_PIN 10u #define MIC_SCK_PIN 11u #define USER_LED_PIN 25u // Short cycle delays keep SCK above the microphone's minimum clock rate. #define CLOCK_DELAY_CYCLES 16u #define WINDOW_SAMPLES 128u #define CALIBRATION_WINDOWS 250u #define LED_HOLD_MS 300u // Raw 24-bit microphone amplitude settings. Adjust FIXED_MIN_LEVEL after // observing the board in its real enclosure if more/less sensitivity is wanted. #define FIXED_MIN_LEVEL 2000u #define TRIGGER_MULTIPLIER_NUM 2u #define TRIGGER_MULTIPLIER_DEN 1u #define RELEASE_PERCENT 70u static int32_t samples[WINDOW_SAMPLES]; static inline void clock_low(void) { gpio_put(MIC_SCK_PIN, 0); busy_wait_at_least_cycles(CLOCK_DELAY_CYCLES); } static inline uint32_t clock_high_and_sample(void) { gpio_put(MIC_SCK_PIN, 1); busy_wait_at_least_cycles(CLOCK_DELAY_CYCLES); return gpio_get(MIC_SD_PIN) ? 1u : 0u; } static inline void clock_discard_bit(void) { clock_low(); (void)clock_high_and_sample(); } // Read the left-channel 24-bit sample from one complete 64-bit I2S frame. // ICS-43434 LR is tied low, so its data is driven in the WS-low half-frame. static int32_t microphone_read_sample(void) { uint32_t raw = 0; gpio_put(MIC_WS_PIN, 0); // I2S has a one-bit delay between the WS edge and the first MSB. clock_discard_bit(); for (uint32_t bit = 0; bit < 24; ++bit) { clock_low(); raw = (raw << 1) | clock_high_and_sample(); } // Finish the 32-clock left slot: 1 delay + 24 data + 7 remaining clocks. for (uint32_t bit = 0; bit < 7; ++bit) { clock_discard_bit(); } // Clock the unused right slot so the microphone receives a valid frame. gpio_put(MIC_WS_PIN, 1); for (uint32_t bit = 0; bit < 32; ++bit) { clock_discard_bit(); } gpio_put(MIC_SCK_PIN, 0); // Sign-extend the 24-bit two's-complement microphone sample. if ((raw & 0x00800000u) != 0u) { raw |= 0xFF000000u; } return (int32_t)raw; } // Mean absolute deviation removes the microphone's DC offset and gives a // robust, inexpensive loudness estimate without floating point or sqrt(). static uint32_t measure_window_level(void) { int64_t sum = 0; for (uint32_t i = 0; i < WINDOW_SAMPLES; ++i) { samples[i] = microphone_read_sample(); sum += samples[i]; } const int32_t mean = (int32_t)(sum / (int64_t)WINDOW_SAMPLES); uint64_t absolute_sum = 0; for (uint32_t i = 0; i < WINDOW_SAMPLES; ++i) { int64_t delta = (int64_t)samples[i] - mean; if (delta < 0) { delta = -delta; } absolute_sum += (uint64_t)delta; } return (uint32_t)(absolute_sum / WINDOW_SAMPLES); } static uint32_t maximum_u32(uint32_t a, uint32_t b) { return (a > b) ? a : b; } int main(void) { gpio_init(USER_LED_PIN); gpio_set_dir(USER_LED_PIN, GPIO_OUT); gpio_put(USER_LED_PIN, 0); // Three flashes confirm the replacement image booted and the LED works. for (uint32_t i = 0; i < 3; ++i) { gpio_put(USER_LED_PIN, 1); sleep_ms(80); gpio_put(USER_LED_PIN, 0); sleep_ms(80); } gpio_init(MIC_WS_PIN); gpio_set_dir(MIC_WS_PIN, GPIO_OUT); gpio_put(MIC_WS_PIN, 1); gpio_init(MIC_SCK_PIN); gpio_set_dir(MIC_SCK_PIN, GPIO_OUT); gpio_put(MIC_SCK_PIN, 0); gpio_init(MIC_SD_PIN); gpio_set_dir(MIC_SD_PIN, GPIO_IN); gpio_disable_pulls(MIC_SD_PIN); // Supply valid clocks while the microphone exits startup/standby. for (uint32_t i = 0; i < 1024; ++i) { (void)microphone_read_sample(); } uint32_t noise_floor = FIXED_MIN_LEVEL / TRIGGER_MULTIPLIER_NUM; // Initial ambient calibration. Keep the environment at its normal quiet // level during this period; a loud calibration produces a higher threshold. for (uint32_t i = 0; i < CALIBRATION_WINDOWS; ++i) { const uint32_t level = measure_window_level(); if (i == 0) { noise_floor = level; } else { // 1/8 exponential moving average. noise_floor = ((noise_floor * 7u) + level) / 8u; } } bool led_on = false; absolute_time_t led_off_deadline = get_absolute_time(); while (true) { const uint32_t level = measure_window_level(); const uint32_t adaptive_threshold = (noise_floor * TRIGGER_MULTIPLIER_NUM) / TRIGGER_MULTIPLIER_DEN; const uint32_t trigger_threshold = maximum_u32(FIXED_MIN_LEVEL, adaptive_threshold); const uint32_t release_threshold = (trigger_threshold * RELEASE_PERCENT) / 100u; if (level >= trigger_threshold) { led_on = true; gpio_put(USER_LED_PIN, 1); led_off_deadline = delayed_by_ms(get_absolute_time(), LED_HOLD_MS); } else { // Slowly adapt only while the signal is below the release threshold, // preventing a loud event from immediately raising the noise floor. if (level < release_threshold) { noise_floor = ((noise_floor * 255u) + level) / 256u; } if (led_on && time_reached(led_off_deadline)) { led_on = false; gpio_put(USER_LED_PIN, 0); } } } }
Sh
mkdir -p build cd build cmake -DPICO_BOARD=pico .. cmake --build . -j
build/avocaudio_sound_led.uf2 to the RPI-RP2 drive. On every boot, keep the environment at its normal background level for the first two seconds. To change sensitivity, lower FIXED_MIN_LEVEL or the trigger multiplier for more sensitivity; raise them for less sensitivity.PICO_BOARD=pico. The ELF uses 11,436 bytes of flash text and 1,676 bytes of RAM/BSS; the UF2 SHA-256 is 44ad889365eeadb7ac0f460d5405e31619c9de4d47ef25f9f08bf29de5d0a4f6.hardware_pwm, hardware_pio, and hardware_clocks libraries:avg, peak, span, thresholds, and the loud/not-loud decision for hardware diagnosis.PICO_BOARD=pico with Pico SDK master and Arm GCC 13.2.1. ELF usage is 38,552 bytes text and 4,320 bytes BSS. UF2 SHA-256: 73306299a95f5705ee73279d8f0afdaf23a995ce8d0066db405f7efe15a03b27.avg, peak, and span.peak and span should rise sharply and loud=1 should appear.Milestone scope
Downloadable source files
Pin table
Architecture and behavior
Build, flash, and test
Example startup output
Validation outcome
Explicit validation limits
PlatformIO build attempt
Minimal GPIO25 blink UF2 — successful build
Environmental sound threshold firmware
Pin mapping
`CMakeLists.txt`
`main.c`
Build and flash
Firmware-specific validation limits
V3 correction — hardware-timed PIO I²S receiver
V3 downloads
Build verification
Hardware diagnostic procedure