|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
PDM MEMS microphone capture + plausibility demo (SPH0690 / ch 2). More...
#include <stdint.h>#include "ra8_audio.h"#include "ra8_audio_source_pdm.h"#include "ra8_board_ek_ra8d2.h"#include "ra8_boot_entry.h"#include "ra8_err.h"#include "ra8_mstp.h"#include "ra8_time.h"Go to the source code of this file.
Data Structures | |
| struct | pdm_metrics_t |
| Statistics for one captured PCM window. More... | |
| struct | pdm_mic_result_t |
| Debugger-probable capture result. More... | |
Enumerations | |
| enum | pdm_demo_const_t : uint32_t { k_pdm_demo_baud = 115200U , k_pdm_demo_decimal = 10U , k_pdm_demo_settle_ms = 300U , k_pdm_demo_window = 256U , k_pdm_demo_discard = 1024U , k_pdm_demo_report_win = 8U , k_pdm_demo_poll_max = 500000U } |
| App-wide tunables. More... | |
| enum | pdm_demo_threshold_t : int32_t { k_pdm_demo_min_span = 8 , k_pdm_demo_rms_floor = 16 } |
| Plausibility thresholds (20-bit signed full scale = +-524288). More... | |
| enum | pdm_demo_str_cap_t : uint32_t { k_pdm_demo_int_str_cap = 12U } |
| Sizing for the decimal serialiser buffer ("-2147483648" + NUL). More... | |
| enum | pdm_isqrt_const_t : uint32_t { k_pdm_isqrt_steps = 32U , k_pdm_isqrt_top2 = 62U } |
| Integer-sqrt bit-window constants (64-bit radicand). More... | |
| enum | pdm_demo_magic_t : uint32_t { k_pdm_demo_magic = 0x50444D31U } |
| Result magic marker ('PDM1'). More... | |
Functions | |
| static void | pdm_demo_panic_halt (void) |
| Park forever after a fatal error. | |
| static uint32_t | pdm_demo_i32_to_dec (int32_t value, uint8_t *out) |
| Convert a signed 32-bit int into a decimal ASCII string. | |
| static uint32_t | pdm_demo_isqrt (uint64_t value) |
| Integer square root of a 64-bit value (floor). | |
| static void | pdm_demo_tx (const uint8_t *bytes, uint32_t len) |
| Emit a byte run over the SCI8 console. | |
| static void | pdm_demo_emit_kv (const uint8_t *label, uint32_t label_len, int32_t value) |
| Emit "<label><value>" with a decimal integer. | |
| static void | pdm_demo_clocks_or_halt (void) |
| Bring CGC + MSTP + SysTick up; halts on any failure. | |
| static void | pdm_demo_io_or_halt (void) |
| Route P812/P502 to the PDM function and open the console. | |
| static void | pdm_demo_pdm_or_halt (void) |
| Bind the on-board MIC1 through the reusable PDM audio source. | |
| static void | pdm_demo_analyze (const int32_t *buf, uint32_t n, pdm_metrics_t *m) |
| Compute min/max/mean/peak/RMS for a captured window. | |
| static bool | pdm_demo_is_active (const pdm_metrics_t *m) |
| Decide whether a window carries plausible live audio. | |
| static void | pdm_demo_publish (const pdm_metrics_t *m, int32_t vary, bool active) |
| Print a per-window detail line and latch g_pdm_mic_result. | |
| static bool | pdm_demo_run_window (int32_t vary, pdm_metrics_t *out_m) |
| Capture one window, analyse it and publish it. | |
| void | main (void) |
| Application entry: bring up the PDM mic and report plausibility. | |
Variables | |
| volatile pdm_mic_result_t | g_pdm_mic_result |
| J-Link-probable capture result (read at &g_pdm_mic_result). | |
| static int32_t | s_pdm_demo_buf [k_pdm_demo_window] |
| Static capture buffer (kept off the stack). | |
| static ra8_audio_source_t | s_pdm_demo_source |
| Generic caller-owned audio source handle. | |
| static ra8_audio_source_pdm_state_t | s_pdm_demo_source_state |
| Caller-owned PDM backend state. | |
| static const uint8_t | k_pdm_demo_banner_init [] = "pdm: init ch=2 clk=4mhz fs=16khz\r\n" |
| static const uint8_t | k_pdm_demo_banner_fail [] = "pdm: init FAIL\r\n" |
| static const uint8_t | k_pdm_demo_banner_nodat [] = "pdm: no data (FIFO empty) -- clock/mic?\r\n" |
| static const uint8_t | k_pdm_demo_banner_pass [] = "pdm: verdict=PASS active=Y\r\n" |
| static const uint8_t | k_pdm_demo_banner_inactive [] = "pdm: verdict=FAIL active=N\r\n" |
PDM MEMS microphone capture + plausibility demo (SPH0690 / ch 2).
Stand-alone EK-RA8D2 application that brings up the on-board SPH0690 PDM MEMS microphones and reports whether they produce plausible audio.
Board wiring (EK-RA8D2 v1 UM Table 31 p 37): two SPH0690 mics (MIC1, MIC2) on the PCB underside share one clock and one data line ->
Signal path (all in hardware, HUM Ch 49): 4 MHz PDM_CLK2 (PDMIFCLK = MOCO 8 MHz / 2) -> 4th-order sinc decimation (M=125) -> compensation FIR -> high-pass (DC block) -> half-band decimation -> 16 kHz, 20-bit signed PCM read out of PDDRRCH2. Filter coefficients are the RA8D2 reset-default SPH0690 set (HUM Ch 49.2 register reset values).
Flow:
Definition in file main.c.
| enum pdm_demo_const_t : uint32_t |
App-wide tunables.
| enum pdm_demo_magic_t : uint32_t |
| enum pdm_demo_str_cap_t : uint32_t |
| enum pdm_demo_threshold_t : int32_t |
| enum pdm_isqrt_const_t : uint32_t |
| void main | ( | void | ) |
Application entry: bring up the PDM mic and report plausibility.
The application entry point Reset_Handler hands control to.
Definition at line 610 of file main.c.
References g_pdm_mic_result, k_pdm_demo_banner_inactive, k_pdm_demo_banner_pass, k_pdm_demo_report_win, pdm_demo_clocks_or_halt(), pdm_demo_io_or_halt(), pdm_demo_panic_halt(), pdm_demo_pdm_or_halt(), pdm_demo_run_window(), pdm_demo_tx(), and pdm_metrics_t::rms.
|
static |
Compute min/max/mean/peak/RMS for a captured window.
Two bounded passes: pass 1 accumulates min/max/mean; pass 2 accumulates the AC peak and mean-square about that mean. A zero-sample window is rejected before either pass runs, so a source that reports an empty capture cannot divide by zero.
| [in] | buf | Sample buffer (non-NULL). |
| [in] | n | Sample count. |
| [out] | m | Metrics output (non-NULL). |
Definition at line 439 of file main.c.
References pdm_metrics_t::max, pdm_metrics_t::mean, pdm_metrics_t::min, pdm_metrics_t::n, pdm_demo_isqrt(), pdm_metrics_t::peak, and pdm_metrics_t::rms.
Referenced by pdm_demo_run_window().
|
static |
Bring CGC + MSTP + SysTick up; halts on any failure.
Runs on MOCO (~8 MHz), which also sources PDMIFCLK (8 MHz).
Definition at line 331 of file main.c.
References ra8_board_clock_rates_t::cpuclk0_hz, k_ra8_ok, pdm_demo_panic_halt(), ra8_board_clocks_init(), ra8_mstp_init(), and ra8_time_init().
Referenced by main().
|
static |
Emit "<label><value>" with a decimal integer.
Serializes the signed value into fixed stack storage after the label.
| [in] | label | Label bytes (non-NULL). |
| [in] | label_len | Label length. |
| [in] | value | Signed integer to print. |
Definition at line 303 of file main.c.
References k_pdm_demo_int_str_cap, pdm_demo_i32_to_dec(), and pdm_demo_tx().
Referenced by pdm_demo_publish().
|
static |
Convert a signed 32-bit int into a decimal ASCII string.
Avoids newlib printf. Buffer must hold k_pdm_demo_int_str_cap.
| [in] | value | Integer to convert. |
| [out] | out | Destination buffer (>= 12 bytes, non-NULL). |
| uint32_t | Decimal character count excluding the terminator. |
Definition at line 196 of file main.c.
References k_pdm_demo_decimal, and k_pdm_demo_int_str_cap.
Referenced by pdm_demo_emit_kv().
|
static |
Route P812/P502 to the PDM function and open the console.
Both pins take PSEL k_ra8_psel_pdm; the console is SCI8.
Definition at line 360 of file main.c.
References k_pdm_demo_baud, k_ra8_board_led1, k_ra8_ok, pdm_demo_panic_halt(), ra8_board_led_init(), ra8_board_pdm_mic_route(), and ra8_board_uart_console_init().
Referenced by main().
|
static |
Decide whether a window carries plausible live audio.
Non-degenerate (span >= k_pdm_demo_min_span) AND carrying AC energy (rms >= k_pdm_demo_rms_floor). Written as nested single-condition tests (no compound decision).
| [in] | m | Window metrics (non-NULL). |
| true | The span and RMS meet their configured floors. |
| false | At least one activity threshold was not met. |
Definition at line 493 of file main.c.
References k_pdm_demo_min_span, k_pdm_demo_rms_floor, pdm_metrics_t::max, pdm_metrics_t::min, and pdm_metrics_t::rms.
Referenced by pdm_demo_run_window().
|
static |
Integer square root of a 64-bit value (floor).
Bit-by-bit restoring algorithm; 32 fixed iterations, no FPU.
| [in] | value | Radicand. |
| uint32_t | Greatest integer whose square does not exceed value. |
Definition at line 244 of file main.c.
References k_pdm_isqrt_steps, and k_pdm_isqrt_top2.
Referenced by pdm_demo_analyze().
|
static |
Park forever after a fatal error.
Waits in a low-power spin; only a debugger or reset wakes it.
Definition at line 170 of file main.c.
Referenced by main(), pdm_demo_clocks_or_halt(), pdm_demo_io_or_halt(), and pdm_demo_pdm_or_halt().
|
static |
Bind the on-board MIC1 through the reusable PDM audio source.
Runs the HUM Ch 49.4.1 start flow with the settling delay. Prints k_pdm_demo_banner_fail and parks on any error.
Definition at line 387 of file main.c.
References ra8_board_pdm_mic_config_t::channel, k_pdm_demo_banner_fail, k_pdm_demo_banner_init, k_pdm_demo_discard, k_pdm_demo_poll_max, k_pdm_demo_settle_ms, k_pdm_demo_window, k_ra8_board_pdm_mic1, k_ra8_ok, ra8_board_pdm_mic_config_t::pdm, pdm_demo_panic_halt(), pdm_demo_tx(), ra8_audio_source_pdm_init(), ra8_board_pdm_mic_get_config(), s_pdm_demo_source, s_pdm_demo_source_state, ra8_board_pdm_mic_config_t::sample_rate_hz, and ra8_board_pdm_mic_config_t::valid_bits.
Referenced by main().
|
static |
Print a per-window detail line and latch g_pdm_mic_result.
Emits each metric as a bounded decimal field before updating the SWD result object.
| [in] | m | Window metrics (non-NULL). |
| [in] | vary | RMS spread across the report sweep so far. |
| [in] | active | Plausibility verdict for this window. |
Definition at line 522 of file main.c.
References g_pdm_mic_result, k_pdm_demo_magic, pdm_metrics_t::max, pdm_metrics_t::mean, pdm_metrics_t::min, pdm_metrics_t::n, pdm_demo_emit_kv(), pdm_demo_tx(), pdm_metrics_t::peak, and pdm_metrics_t::rms.
Referenced by pdm_demo_run_window().
|
static |
Capture one window, analyse it and publish it.
Reports the no-data banner and returns false if the FIFO never fills (dead clock / mic).
| [in] | vary | RMS spread so far (for the detail line). |
| [in,out] | out_m | Receives the window metrics (non-NULL). |
| true | Capture, analysis, and publication completed. |
| false | The audio source did not provide a frame. |
Definition at line 577 of file main.c.
References g_pdm_mic_result, k_pdm_demo_banner_nodat, k_ra8_ok, pdm_demo_analyze(), pdm_demo_is_active(), pdm_demo_publish(), pdm_demo_tx(), ra8_audio_source_capture(), s_pdm_demo_buf, s_pdm_demo_source, ra8_audio_frame_t::sample_count, and ra8_audio_frame_t::timestamp_ms.
Referenced by main().
|
static |
Emit a byte run over the SCI8 console.
Forwards the exact caller span through the board console without formatting.
| [in] | bytes | Byte array (non-NULL). |
| [in] | len | Byte count. |
Definition at line 280 of file main.c.
References ra8_board_uart_console_write().
Referenced by main(), pdm_demo_emit_kv(), pdm_demo_pdm_or_halt(), pdm_demo_publish(), and pdm_demo_run_window().
| volatile pdm_mic_result_t g_pdm_mic_result |
J-Link-probable capture result (read at &g_pdm_mic_result).
Definition at line 123 of file main.c.
Referenced by main(), pdm_demo_publish(), and pdm_demo_run_window().
|
static |
Definition at line 145 of file main.c.
Referenced by pdm_demo_pdm_or_halt().
|
static |
|
static |
Definition at line 144 of file main.c.
Referenced by pdm_demo_pdm_or_halt().
|
static |
Definition at line 146 of file main.c.
Referenced by pdm_demo_run_window().
|
static |
|
static |
Static capture buffer (kept off the stack).
Definition at line 126 of file main.c.
Referenced by pdm_demo_run_window().
|
static |
Generic caller-owned audio source handle.
Definition at line 129 of file main.c.
Referenced by pdm_demo_pdm_or_halt(), and pdm_demo_run_window().
|
static |
Caller-owned PDM backend state.
Definition at line 132 of file main.c.
Referenced by pdm_demo_pdm_or_halt().