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

GPT input-capture-style demo: measure SW1 press period. 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_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_capture_const_t : uint32_t {
  k_gpt_capture_period = 0xFFFFFFFFUL ,
  k_gpt_capture_poll_period_ms = 10U ,
  k_gpt_capture_min_delta_ms = 50U
}
 Demo tunables. More...
enum  gpt_capture_layout_t : uint8_t { k_gpt_capture_channel = 0U }
 Channel + LED selection. More...

Functions

static void internal_panic_halt (void)
 Park the CPU after a fatal initialization failure.
static void internal_setup_or_halt (void)
 Bring CGC + SysTick + LEDs + SW1 + GPT0 up.
static uint32_t internal_read (void)
 Read current GPT counter ticks, or return UINT32_MAX on err.
static bool internal_edge (ra8_board_sw_state_t *prev)
 Detect a falling edge on SW1.
void main (void)
 Run the software input-capture and GPT liveness loop.

Variables

volatile uint32_t g_gpt_capture_tick = 0U
 HIL liveness counter – incremented every iteration where the GPT counter advanced relative to the previous sample (proves GTCNT is actually counting).

Detailed Description

GPT input-capture-style demo: measure SW1 press period.

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

Programs GPT0 as a free-running 32-bit up-counter with prescaler PCLKD/64. On each falling edge of SW1 (active-low) the application latches the current GPT counter value and computes the delta from the previous press: that delta is the measured "input capture period" in GPT counter ticks. The lowest LED is toggled on every captured edge so a human can see live activity.

Note: the RA8D2 GPT supports hardware GTIOC pin capture, and the HAL now exposes it via ra8_gpt_capture_configure / ra8_gpt_capture_read. This demo deliberately stays on the free-run + counter-read primitives and approximates input capture in software via SW1 polling, so it needs no external signal source and stays HIL-able on a bare EVM. The real hardware-capture + external-event-count path is demonstrated by the gpt_edge_capture_count hw_pending app.

Since
0.1.0

Definition in file main.c.

Enumeration Type Documentation

◆ gpt_capture_const_t

enum gpt_capture_const_t : uint32_t

Demo tunables.

Enumerator
k_gpt_capture_period 

GPT capture period.

k_gpt_capture_poll_period_ms 

GPT capture poll period ms.

k_gpt_capture_min_delta_ms 

GPT capture minimum delta ms.

Definition at line 41 of file main.c.

◆ gpt_capture_layout_t

enum gpt_capture_layout_t : uint8_t

Channel + LED selection.

Enumerator
k_gpt_capture_channel 

GPT capture channel.

Definition at line 64 of file main.c.

Function Documentation

◆ internal_edge()

bool internal_edge ( ra8_board_sw_state_t * prev)
static

Detect a falling edge on SW1.

Samples SW1 once, compares released-to-pressed state, and updates the caller-owned prior sample only when the board read succeeds.

MC/DC:
Compound decision: prev == released && now == pressed. Two atomic conditions x N+1 = 3 vectors – both true (edge), prev pressed (no edge), now released (no edge). Companion host test exercises each vector.
Parameters
[in,out]prevLast sampled state (updated on return).
Returns
true on the falling edge, false otherwise.
Return values
trueThe sample changed from released to pressed.
falseNo falling edge was observed or the board read failed.
Precondition
prev points to writable caller-owned state.
SW1 was initialized by internal_setup_or_halt.
Postcondition
On a successful read, *prev contains the new switch state.
On a failed read, *prev is unchanged.
Note
Not thread-safe when multiple callers share prev.
Since
0.1.0

Definition at line 183 of file main.c.

References k_ra8_board_sw1, k_ra8_board_sw_pressed, k_ra8_board_sw_released, k_ra8_ok, ra8_board_sw_read(), and RA8_INTERNAL.

Referenced by main().

◆ internal_panic_halt()

void internal_panic_halt ( void )
static

Park the CPU after a fatal initialization failure.

Repeatedly executes WFI, preserving the failed peripheral state for an attached debugger until reset.

Precondition
Called only from the boot or terminal foreground path.
The caller does not require this function to return.
Postcondition
The core remains in the WFI loop until external intervention.
No application data changes after entry.
Note
Not thread-safe; this is a terminal single-threaded path.
Since
0.1.0

Definition at line 81 of file main.c.

References RA8_INTERNAL.

◆ internal_read()

uint32_t internal_read ( void )
static

Read current GPT counter ticks, or return UINT32_MAX on err.

Delegates to the channel-zero HAL accessor and maps any HAL error onto a sentinel that the foreground liveness probe can ignore.

Returns
Current free-running GPT count or the error sentinel.
Return values
UINT32_MAXThe counter read failed.
0..UINT32_MAX-1A sampled counter value.
Precondition
GPT0 was initialized and auto-started by internal_setup_or_halt.
No other context is reconfiguring GPT0 concurrently.
Postcondition
GPT0 remains running and unmodified.
The returned value is either a direct sample or UINT32_MAX.
Note
Not thread-safe with concurrent GPT reconfiguration.
Since
0.1.0

Definition at line 150 of file main.c.

References k_gpt_capture_channel, k_ra8_ok, ra8_gpt_read(), and RA8_INTERNAL.

Referenced by main().

◆ internal_setup_or_halt()

void internal_setup_or_halt ( void )
static

Bring CGC + SysTick + LEDs + SW1 + GPT0 up.

Initializes the clock tree and delay service, configures LED1 and SW1, then starts GPT0 as a PCLKD/64 free-running counter. A failed prerequisite transfers control to the panic helper.

Precondition
Reset startup initialized static storage and the vector table.
Called once before global interrupt enable.
Postcondition
On return, GPT0 is counting and SW1/LED1 are ready for polling.
The delay service uses the measured CPU clock.
Note
Not thread-safe; it owns the demo's peripheral configuration.
Since
0.1.0

Definition at line 102 of file main.c.

References internal_panic_halt(), k_gpt_capture_channel, k_gpt_capture_period, k_ra8_board_led1, k_ra8_board_sw1, k_ra8_clock_id_cpuclk0, k_ra8_gpt_mode_saw_pwm, k_ra8_gpt_ps_div_64, k_ra8_ok, ra8_board_led_init(), ra8_board_sw_init(), ra8_cgc_get_clock_hz(), ra8_cgc_init(), ra8_gpt_init(), RA8_INTERNAL, and ra8_time_init().

◆ main()

void main ( void )

Run the software input-capture and GPT liveness loop.

The application entry point Reset_Handler hands control to.

Initializes the demo, tracks falling SW1 edges as counter deltas, and independently increments the exported liveness counter whenever successive GPT samples advance.

Precondition
Reset startup and SystemInit completed successfully.
The board wiring matches the EK-RA8D2 LED1 and SW1 definitions.
Postcondition
During operation, each detected edge toggles LED1.
A terminal control-flow escape parks the core in the panic helper.
Note
Does not return during normal operation.
Since
0.1.0

Definition at line 208 of file main.c.

References g_gpt_capture_tick, internal_edge(), internal_panic_halt(), internal_read(), internal_setup_or_halt(), k_gpt_capture_poll_period_ms, k_ra8_board_led1, k_ra8_board_sw_released, ra8_board_led_toggle(), ra8_delay_ms(), and ra8_isr_globals_enable().

Variable Documentation

◆ g_gpt_capture_tick

volatile uint32_t g_gpt_capture_tick = 0U

HIL liveness counter – incremented every iteration where the GPT counter advanced relative to the previous sample (proves GTCNT is actually counting).

Read by hil_jlink_memprobe.sh. SW1-button-driven input capture is out-of-scope for automated HIL (would need a wire loopback), so the gate just confirms the free-running counter the demo uses for delta measurement is alive.

Note
Read externally by J-Link.
Since
0.1.0

Definition at line 61 of file main.c.

Referenced by main().