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

GPT period-streaming DMA HIL demo for EK-RA8D2. 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_dma.h"
#include "ra8_err.h"
#include "ra8_gpt.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  gpt_dma_demo_const_t : uint32_t {
  k_gpt_dma_demo_initial_period = 0x0000FFFFU ,
  k_gpt_dma_demo_period_count = 4U ,
  k_gpt_dma_demo_rearm_delay_ms = 10U
}
 Demo tunables. More...
enum  gpt_dma_demo_chan_t : uint8_t { k_gpt_dma_demo_channel = 0U }
 GPT channel. More...

Functions

static void internal_panic_halt (void)
 Park the core after an unrecoverable GPT DMA demo failure.
static void internal_setup_or_halt (void)
 Initialize clocks, SysTick, LED1, and the DMA allocator.
static void internal_on_complete (void *ctx)
 DMA completion callback – raise the s_dma_done flag.
static ra8_err_t internal_arm_channel (void)
 Init GPT0 as a saw-wave PWM with a stable initial period.
static bool internal_run_one (void)
 Arm one DMA write of s_period_table into GPT0.GTPR.
void main (void)
 Repeatedly stream the fixed period table into GPT0 through DMA.

Variables

static const uint32_t s_period_table [k_gpt_dma_demo_period_count]
 Four 32-bit period values DMA streams into GPT0.GTPR.
volatile uint32_t g_gpt_dma_match = 0U
 HIL liveness counter – bumped after each DMA-complete callback fires.
volatile uint32_t g_gpt_dma_mismatch = 0U
 HIL failure counter – bumped when ra8_gpt_write_dma returned non-ok or the completion callback never fired inside the bounded wait.
static volatile uint8_t s_dma_done
 Set to 1 inside the DMA completion callback.

Detailed Description

GPT period-streaming DMA HIL demo for EK-RA8D2.

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

Exercises ra8_gpt_write_dma – the "sample streaming" DMA TX path that copies a buffer of 32-bit period values into the GPT's GTPR register via a DMAC channel. The CPU does not touch GTPR each tick; DMA pumps the preloaded period sequence.

The completion callback bumps g_gpt_dma_match once per DMA transfer. The main loop re-arms after each callback so the bench can scrape the counter advancing.

Bring-up sequence:

  • CGC + SysTick + LED1 + ra8_dma_init + ra8_gpt_init.
  • Arm a DMA transfer of 4 period values via ra8_gpt_write_dma.
  • Wait for completion (callback bumps a flag).
  • Re-arm with the same period sequence.
Since
0.1.0

Definition in file main.c.

Enumeration Type Documentation

◆ gpt_dma_demo_chan_t

enum gpt_dma_demo_chan_t : uint8_t

GPT channel.

Enumerator
k_gpt_dma_demo_channel 

GPT DMA demo channel.

Definition at line 49 of file main.c.

◆ gpt_dma_demo_const_t

enum gpt_dma_demo_const_t : uint32_t

Demo tunables.

Enumerator
k_gpt_dma_demo_initial_period 

GPT DMA demo initial period.

k_gpt_dma_demo_period_count 

4 periods per DMA burst.

k_gpt_dma_demo_rearm_delay_ms 

GPT DMA demo rearm delay ms.

Definition at line 42 of file main.c.

Function Documentation

◆ internal_arm_channel()

ra8_err_t internal_arm_channel ( void )
staticnodiscard

Init GPT0 as a saw-wave PWM with a stable initial period.

Programs channel zero for PCLKD/4 saw-wave PWM and starts it so the DMA stream has a live GTPR destination.

Returns
ra8_err_t error code.
Return values
k_ra8_okGPT0 accepted and started the configuration.
(other)The HAL rejected or could not apply the configuration.
Precondition
ra8_cgc_init has succeeded.
GPT0 not yet initialised.
Postcondition
On k_ra8_ok the channel is running.
Note
Not thread-safe.
Since
0.1.0

Definition at line 187 of file main.c.

References k_gpt_dma_demo_channel, k_gpt_dma_demo_initial_period, k_ra8_gpt_mode_saw_pwm, k_ra8_gpt_ps_div_4, ra8_gpt_init(), and RA8_INTERNAL.

Referenced by main().

◆ internal_on_complete()

void internal_on_complete ( void * ctx)
static

DMA completion callback – raise the s_dma_done flag.

Publishes the volatile completion flag before issuing the optional visual LED toggle, keeping interrupt-context work bounded.

Parameters
[in]ctxUnused.
Precondition
A DMA transfer was previously armed.
IRQs enabled.
Postcondition
s_dma_done == 1.
LED1 toggles for visual signal.
Note
ISR context; keep work tiny.
Since
0.1.0

Definition at line 164 of file main.c.

References k_ra8_board_led1, ra8_board_led_toggle(), RA8_INTERNAL, and s_dma_done.

Referenced by internal_run_one().

◆ internal_panic_halt()

void internal_panic_halt ( void )
static

Park the core after an unrecoverable GPT DMA demo failure.

Repeatedly executes WFI, preserving DMA and timer state for debug.

Precondition
Called only from a fatal boot or terminal foreground path.
The caller does not require recovery without reset.
Postcondition
The core stays in WFI until external intervention.
Neither exported HIL counter changes after entry.
Note
Not thread-safe; this is a terminal single-threaded path.
Since
0.1.0

Definition at line 112 of file main.c.

References RA8_INTERNAL.

◆ internal_run_one()

bool internal_run_one ( void )
static

Arm one DMA write of s_period_table into GPT0.GTPR.

Resets the completion flag, allocates and starts one bounded period transfer, waits briefly for the optional callback, then explicitly releases the channel because this app does not wire its DMA IRQ.

Returns
Whether the DMA request itself was accepted.
Return values
trueThe DMA request was accepted and its channel was released.
falseThe DMA request failed before a channel could be used.
Precondition
Channel armed.
IRQs enabled.
Postcondition
s_dma_done observed as 1, or the callback never fired in budget.
An allocated channel is released before return.
Note
Not thread-safe.
Since
0.1.0

< Wait iters.

Definition at line 218 of file main.c.

References internal_on_complete(), k_gpt_dma_demo_channel, k_gpt_dma_demo_period_count, k_ra8_ok, ra8_delay_ms(), ra8_dma_release(), ra8_gpt_write_dma(), RA8_INTERNAL, s_dma_done, and s_period_table.

Referenced by main().

◆ internal_setup_or_halt()

void internal_setup_or_halt ( void )
static

Initialize clocks, SysTick, LED1, and the DMA allocator.

Brings dependencies up in order and parks on the first HAL error.

Precondition
Reset startup initialized static storage and the vector table.
Called once before global interrupt enable.
Postcondition
On return, delays, LED1, and DMA channel allocation are ready.
GPT0 remains unconfigured until internal_arm_channel runs.
Note
Not thread-safe; it owns the demo's peripheral initialization.
Since
0.1.0

Definition at line 129 of file main.c.

References internal_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_dma_init(), RA8_INTERNAL, and ra8_time_init().

◆ main()

void main ( void )

Repeatedly stream the fixed period table into GPT0 through DMA.

The application entry point Reset_Handler hands control to.

Initializes the demo and GPT0, runs one bounded DMA request per iteration, updates the exported match/mismatch counter, and delays.

Precondition
Reset startup and SystemInit completed successfully.
GPT0 and the DMA allocator are not owned by another context.
Postcondition
Every iteration increments exactly one HIL result counter.
Allocated DMA channels are released by internal_run_one.
Note
Does not return during normal operation.
Since
0.1.0

Definition at line 264 of file main.c.

References g_gpt_dma_match, g_gpt_dma_mismatch, internal_arm_channel(), internal_panic_halt(), internal_run_one(), internal_setup_or_halt(), k_gpt_dma_demo_rearm_delay_ms, k_ra8_ok, ra8_delay_ms(), and ra8_isr_globals_enable().

Variable Documentation

◆ g_gpt_dma_match

volatile uint32_t g_gpt_dma_match = 0U

HIL liveness counter – bumped after each DMA-complete callback fires.

Note
Read externally by J-Link.
Since
0.1.0

Definition at line 77 of file main.c.

Referenced by main().

◆ g_gpt_dma_mismatch

volatile uint32_t g_gpt_dma_mismatch = 0U

HIL failure counter – bumped when ra8_gpt_write_dma returned non-ok or the completion callback never fired inside the bounded wait.

Note
Read externally by J-Link.
Since
0.1.0

Definition at line 88 of file main.c.

Referenced by main().

◆ s_dma_done

volatile uint8_t s_dma_done
static

Set to 1 inside the DMA completion callback.

Volatile so the main thread's spin-wait sees the update from the DMA-completion interrupt context.

Note
Read by main thread, written by callback.
Since
0.1.0

Definition at line 100 of file main.c.

Referenced by internal_on_complete(), and internal_run_one().

◆ s_period_table

const uint32_t s_period_table[k_gpt_dma_demo_period_count]
static
Initial value:
= {
0x00001000U,
0x00002000U,
0x00004000U,
0x0000FFFFU,
}

Four 32-bit period values DMA streams into GPT0.GTPR.

Must outlive the transfer; in .rodata so the chip's DMAC sees a stable physical address.

Since
0.1.0

Definition at line 62 of file main.c.

Referenced by internal_run_one().