ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
main.c File Reference

BSP-driven I2S audio playback demo for EK-RA8D2 (DA7212 CODEC). More...

#include <stdint.h>
#include "ra8_attributes.h"
#include "ra8_board_ek_ra8d2.h"
#include "ra8_boot_entry.h"
#include "ra8_cgc.h"
#include "ra8_err.h"
#include "ra8_isr.h"
#include "ra8_time.h"
Include dependency graph for main.c:

Go to the source code of this file.

Enumerations

enum  audio_loopback_config_t : uint32_t {
  k_audio_loopback_baud = 115200U ,
  k_audio_loopback_print_period = 1000U ,
  k_audio_loopback_sample_rate = 48000U
}
 Compile-time settings for the BSP-driven audio playback demo. More...
enum  audio_loopback_chan_t : uint8_t {
  k_audio_loopback_bit_depth = 16U ,
  k_audio_loopback_channels = 2U
}
 uint8_t format ids consumed by the BSP audio path. More...
enum  audio_loopback_block_t : uint32_t { k_audio_loopback_block_samples = 128U }
 Stereo block size in interleaved L/R 16-bit samples. More...

Functions

static void internal_audio_loopback_panic_halt (void)
 Halt forever in WFI – used as a panic stop on init failure.
static void internal_audio_loopback_init_clocks_and_led (void)
 Bring CGC + SysTick + LED1 up.
static void internal_audio_loopback_init_console (void)
 Open the BSP J-Link console (SCI8 @ 115200 8N1, PD02/PD03).
static void internal_audio_loopback_init_codec (void)
 Bring the on-board DA7212 CODEC + SSIE0 up via the BSP.
static uint32_t internal_audio_loopback_u32_to_dec (uint32_t value, uint8_t *buf)
 Convert a 32-bit unsigned integer into an ASCII decimal string.
static void internal_audio_loopback_print_count (uint32_t block_count)
 Print "audio: <count> blocks played\\r\\n" over the BSP console.
void main (void)
 Application entry.

Variables

static const uint8_t s_audio_loopback_msg_prefix [] = "audio: "
 Diagnostic banner emitted every k_audio_loopback_print_period blocks.
static const uint8_t s_audio_loopback_msg_suffix [] = " blocks played\r\n"
static const int16_t s_audio_loopback_silence [k_audio_loopback_block_samples] = {}
 Static stereo silence buffer fed to the CODEC each iteration.

Detailed Description

BSP-driven I2S audio playback demo for EK-RA8D2 (DA7212 CODEC).

Tag
[Ring 6 / APP] {World: S}

Brings the chip up via ra8_cgc_init() (XTAL -> PLL1 -> CPUCLK0 = 1 GHz, PCLKA = 125 MHz, SCICLK = PLL1R / 4), then hands the entire SSIE0 + DA7212 CODEC bring-up to the EK-RA8D2 BSP via ra8_board_audio_init(48000, 16, 2). The BSP routes the CODEC pins (P403/P404/P405/P406/PD06 + I2C control on P511/P512 per UM Table 32 p 38), enables SSIE0 in I2S controller mode, and exposes ra8_board_audio_play_sample_block for stereo PCM block writes.

After init the firmware loops feeding a small repeating stereo buffer to ra8_board_audio_play_sample_block, prints "audio: <N> blocks played\r\n" over the J-Link OB CDC port (SCI8 @ 115200) every k_audio_loopback_print_period blocks, and toggles LED1 once per log line.

Sequence:

  1. ra8_cgc_init() – standard FSP-quickstart clock tree.
  2. ra8_time_init(cpuclk0_hz) for the diagnostic-print throttle.
  3. ra8_board_uart_console_init(115200) – BSP routes PD02 TXD / PD03 RXD and opens SCI8 @ 115200 8N1 for diagnostic output.
  4. ra8_board_audio_init(48000, 16, 2) – BSP wires SSIE0 + CODEC pins and starts the I2S controller.
  5. Loop: push the stereo sample block via ra8_board_audio_play_sample_block, increment counter, log every k_audio_loopback_print_period blocks.
Architectural ring
[Ring 6 / APP] {World: S} – application-layer code that runs in the Secure world.
Author
Brighton Sikarskie
Date
2026-04-29
Since
0.1.0

Definition in file main.c.

Enumeration Type Documentation

◆ audio_loopback_block_t

enum audio_loopback_block_t : uint32_t

Stereo block size in interleaved L/R 16-bit samples.

Enumerator
k_audio_loopback_block_samples 

64 stereo frames * 2 ch.

Definition at line 75 of file main.c.

◆ audio_loopback_chan_t

enum audio_loopback_chan_t : uint8_t

uint8_t format ids consumed by the BSP audio path.

Enumerator
k_audio_loopback_bit_depth 

Audio loopback bit depth.

k_audio_loopback_channels 

Audio loopback channels.

Definition at line 69 of file main.c.

◆ audio_loopback_config_t

enum audio_loopback_config_t : uint32_t

Compile-time settings for the BSP-driven audio playback demo.

The print throttle is per-1000 blocks; at 48 kHz with 64 stereo frames per block that is one log line every ~1.3 s.

Enumerator
k_audio_loopback_baud 

Audio loopback baud.

k_audio_loopback_print_period 

Audio loopback print period.

k_audio_loopback_sample_rate 

Audio loopback sample rate.

Definition at line 62 of file main.c.

Function Documentation

◆ internal_audio_loopback_init_clocks_and_led()

void internal_audio_loopback_init_clocks_and_led ( void )
static

Bring CGC + SysTick + LED1 up.

Halts on any fail.

Splits the long boot sequence so each step stays under the NASA Power-of-10 60-line function-size cap.

Precondition
Reset_Handler has copied .data and zeroed .bss.
Caller is single-threaded init context.
Postcondition
On success the CGC, SysTick, and LED1 are live.
Halts in WFI on init failure.
Note
Initialization is deliberately fail-stop so playback never begins with a partially configured clock or GPIO tree.
Since
0.1.0

Definition at line 135 of file main.c.

References internal_audio_loopback_panic_halt(), k_ra8_board_led1, k_ra8_clock_id_cpuclk0, k_ra8_ok, ra8_board_led_init(), ra8_cgc_get_clock_hz(), ra8_cgc_init(), RA8_INTERNAL, and ra8_time_init().

Referenced by main().

◆ internal_audio_loopback_init_codec()

void internal_audio_loopback_init_codec ( void )
static

Bring the on-board DA7212 CODEC + SSIE0 up via the BSP.

ra8_board_audio_init routes the CODEC pins (P403/P404/P405/P406/ PD06 per UM Table 32 p 38) and brings SSIE0 up in I2S controller mode at the requested format. The application no longer touches SSIE registers directly.

Precondition
Pin mux table is unlocked (single-threaded init context).
J41 jumpers populated (CODEC selected over camera).
Postcondition
DA7212 + SSIE0 are live and ready for sample-block writes.
Halts in WFI on init failure.
Note
The BSP owns board-specific pin routing and codec control traffic.
Since
0.1.0

Definition at line 196 of file main.c.

References internal_audio_loopback_panic_halt(), k_audio_loopback_bit_depth, k_audio_loopback_channels, k_audio_loopback_sample_rate, k_ra8_ok, ra8_board_audio_init(), and RA8_INTERNAL.

Referenced by main().

◆ internal_audio_loopback_init_console()

void internal_audio_loopback_init_console ( void )
static

Open the BSP J-Link console (SCI8 @ 115200 8N1, PD02/PD03).

Hands the SCI8 + PD02 TXD / PD03 RXD pin routing and baud setup to the EK-RA8D2 BSP via ra8_board_uart_console_init.

Precondition
Clocks have been initialized.
Caller is single-threaded init context.
Postcondition
SCI8 is enabled for diagnostic output.
Halts in WFI on init failure.
Note
Console writes remain best-effort after this routine returns.
Since
0.1.0

Definition at line 170 of file main.c.

References internal_audio_loopback_panic_halt(), k_audio_loopback_baud, k_ra8_ok, ra8_board_uart_console_init(), and RA8_INTERNAL.

Referenced by main().

◆ internal_audio_loopback_panic_halt()

void internal_audio_loopback_panic_halt ( void )
static

Halt forever in WFI – used as a panic stop on init failure.

Masks no additional interrupts; repeated WFI instructions keep the core quiescent while preserving register state for a debugger.

Precondition
Called only after a fatal error in boot.
No recovery path remains for the current boot attempt.
Postcondition
CPU is parked; only a debugger or external reset wakes it.
No peripheral state is changed after entry.
Note
This helper deliberately does not attempt logging because the console may be the peripheral whose initialization failed.
Since
0.1.0

Definition at line 110 of file main.c.

References RA8_INTERNAL.

Referenced by internal_audio_loopback_init_clocks_and_led(), internal_audio_loopback_init_codec(), internal_audio_loopback_init_console(), and main().

◆ internal_audio_loopback_print_count()

void internal_audio_loopback_print_count ( uint32_t block_count)
static

Print "audio: <count> blocks played\\r\\n" over the BSP console.

Formats the count into a bounded stack buffer and emits the prefix, digits, and suffix as three allocation-free console writes.

Parameters
[in]block_countCumulative block count.
Precondition
The BSP console is initialized.
block_count is the monotonic playback-loop counter snapshot.
Postcondition
Three writes have been issued on the BSP console.
No heap or dynamic allocations.
Note
Individual write failures are intentionally ignored because this is diagnostic output and must not stop audio playback.
Since
0.1.0

< Dec buffer.

Definition at line 272 of file main.c.

References internal_audio_loopback_u32_to_dec(), ra8_board_uart_console_write(), RA8_INTERNAL, s_audio_loopback_msg_prefix, and s_audio_loopback_msg_suffix.

Referenced by main().

◆ internal_audio_loopback_u32_to_dec()

uint32_t internal_audio_loopback_u32_to_dec ( uint32_t value,
uint8_t * buf )
static

Convert a 32-bit unsigned integer into an ASCII decimal string.

Builds digits least-significant first in a bounded local array, then reverses them into the caller's output buffer without heap use.

Parameters
[in]valueInteger value to convert.
[out]bufCaller buffer, must hold at least 11 bytes.
Returns
Number of bytes written into buf (does not include a NUL).
Return values
1One byte was written for the value zero.
2..10The number of decimal digits written for a nonzero value.
Precondition
buf is non-NULL and has room for 11 bytes.
The caller does not require a terminating NUL byte.
Postcondition
Buffer holds the decimal representation, MSD first.
Bytes beyond the returned length are left unchanged.
Note
The 11-byte capacity precondition leaves room for all uint32_t values even though this helper writes at most ten digits.
Since
0.1.0

< '0'.

< Radix.

< 2^32 - 1 has at most 10 digits.

Definition at line 229 of file main.c.

References RA8_INTERNAL.

Referenced by internal_audio_loopback_print_count().

◆ main()

void main ( void )

Application entry.

The application entry point Reset_Handler hands control to.

Brings up CGC + BSP audio then plays blocks.

Precondition
Reset_Handler has copied .data and zeroed .bss.
SystemInit has set VTOR, FPU, and priority grouping.
Postcondition
On clean entry the CPU stays in the playback loop forever.
On any HAL init failure the function halts in WFI.
Since
0.1.0

Definition at line 298 of file main.c.

Variable Documentation

◆ s_audio_loopback_msg_prefix

const uint8_t s_audio_loopback_msg_prefix[] = "audio: "
static

Diagnostic banner emitted every k_audio_loopback_print_period blocks.

Definition at line 80 of file main.c.

Referenced by internal_audio_loopback_print_count().

◆ s_audio_loopback_msg_suffix

const uint8_t s_audio_loopback_msg_suffix[] = " blocks played\r\n"
static

Definition at line 81 of file main.c.

Referenced by internal_audio_loopback_print_count().

◆ s_audio_loopback_silence

const int16_t s_audio_loopback_silence[k_audio_loopback_block_samples] = {}
static

Static stereo silence buffer fed to the CODEC each iteration.

Zero-amplitude PCM keeps the SSIE0 FIFO primed without driving audible output. Replace with a sine LUT (or a pull from an iso-OUT USB endpoint) to produce a tone.

Definition at line 91 of file main.c.

Referenced by main().