|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
Periodic-app deep-idle loop: wake-do-work-standby on ULPT0 self-wake. More...
#include <stddef.h>#include <stdint.h>#include "ra8_board_ek_ra8d2.h"#include "ra8_boot_entry.h"#include "ra8_cgc.h"#include "ra8_elc_regs.h"#include "ra8_err.h"#include "ra8_isr.h"#include "ra8_lpm.h"#include "ra8_lpm_regs.h"#include "ra8_time.h"#include "ra8_ulpt.h"#include "ra8_ulpt_regs.h"Go to the source code of this file.
Enumerations | |
| enum | lpi_const_t : uint32_t { k_lpi_baud = 115200U , k_lpi_period_ticks = 0x4000U , k_lpi_tcstf_poll_limit = 0x100000U , k_lpi_period_count = 8U } |
| Demo tunables for the periodic-idle loop. More... | |
| enum | lpi_chan_t : uint8_t { k_lpi_channel = 0U } |
| ULPT channel selector for this demo. More... | |
| enum | lpi_led_t : uint8_t { k_lpi_led = (uint8_t)k_ra8_board_led1 } |
| Heartbeat LED selector. More... | |
Functions | |
| static void | lpi_panic_halt (void) |
| Park the CPU forever on an unrecoverable bring-up failure. | |
| static void | lpi_ulpt_isr (void *ctx) |
| ULPT0 underflow interrupt service routine. | |
| static ra8_err_t | lpi_wait_count_started (uint8_t channel) |
| Spin until the ULPT count operation has actually started. | |
| static void | lpi_setup_or_halt (void) |
| Bring CGC + SysTick + SCI8 + LED + ULPT + LPM block up. | |
| static ra8_err_t | lpi_arm_wake (void) |
| Wire the ULPT0 underflow as a Software-Standby wake source. | |
| static ra8_err_t | lpi_do_work (void) |
| Do one period's unit of work: heartbeat LED + console banner. | |
| static ra8_err_t | lpi_idle_one_period (void) |
| Arm ULPT0, drop to Software Standby, and clean up after wake. | |
| void | main (void) |
| The application entry point Reset_Handler hands control to. | |
Variables | |
| static const uint8_t | k_lpi_boot_msg [] = "lpm_periodic_idle: boot\r\n" |
| Boot banner – emitted once before the first work step. | |
| static const uint8_t | k_lpi_work_msg [] = "lpm_periodic_idle: work\r\n" |
| Per-period work banner – one per active period. | |
| static const uint8_t | k_lpi_pass_msg [] = "lpm_periodic_idle PASS\r\n" |
| Completion banner – emitted once after the last period. | |
| volatile uint32_t | g_lpm_periodic_wake_count = 0U |
| Liveness counter – bumped in the ULPT0 underflow ISR. | |
Periodic-app deep-idle loop: wake-do-work-standby on ULPT0 self-wake.
The "periodic apps" increment of the deep-idle foundation (roadmap issue #153). Where lpm_ulpt_standby proves the bare wake path, this app wraps it into the shape a real periodic application takes: each period it does a small unit of work, then sleeps the CPU in Software Standby until the on-chip Ultra-Low-Power Timer wakes it, and repeats for a fixed number of periods before parking.
Per-period sequence:
The exact, bench-proven wake path is reused verbatim from lpm_ulpt_standby – no new wake mechanism is invented:
Why ULPT and not the RTC: with ULPTMR1.TCK1 = 0 the ULPT counts from ULPTLCLK – the LOCO-derived 32.768 kHz clock (HUM Table 9.2 p 320) – which keeps running through Software Standby while LOCOCR.LCSTP = 0 (HUM Table 11.3 footnote *2 p 433). LOCO is internal, so unlike the RTC alarm (which depends on the bare-board sub-clock crystal) this wake source does not rely on the intermittent SOSC.
Definition in file main.c.
| enum lpi_chan_t : uint8_t |
ULPT channel selector for this demo.
Only ULPT0 is used; its underflow is the wake source.
| Enumerator | |
|---|---|
| k_lpi_channel | ULPT0 (its underflow is the wake source). |
| enum lpi_const_t : uint32_t |
Demo tunables for the periodic-idle loop.
Groups every integer constant the loop needs so no magic numbers leak into the control flow.
| Enumerator | |
|---|---|
| k_lpi_baud | SCI8 console baud rate. |
| k_lpi_period_ticks | Lpi period ticks. |
| k_lpi_tcstf_poll_limit | Lpi tcstf poll limit. |
| k_lpi_period_count | Lpi period count. |
| enum lpi_led_t : uint8_t |
Heartbeat LED selector.
The blue user LED (LED1) is toggled once per period so a bench observer sees the wake cadence without the UART.
| Enumerator | |
|---|---|
| k_lpi_led | Blue user LED heartbeat. |
|
staticnodiscard |
Wire the ULPT0 underflow as a Software-Standby wake source.
Both halves of the RA8D2 wake path are required (see file header): the IELSRn/NVIC link via ra8_isr_register AND the async WUPEN1.ULP0U detector via ra8_lpm_arm_wupen1_bits. This mirrors the bench-proven lpm_ulpt_standby arm step exactly.
| k_ra8_ok | All three wake-path steps succeeded. |
Definition at line 337 of file main.c.
References k_ra8_elc_event_ulpt0_ulpti, k_ra8_isr_prio_default, k_ra8_lpm_wupen1_ulpt0u, k_ra8_ok, lpi_ulpt_isr(), ra8_isr_init(), ra8_isr_register(), and ra8_lpm_arm_wupen1_bits().
Referenced by main().
|
staticnodiscard |
Do one period's unit of work: heartbeat LED + console banner.
The "application payload" stand-in for a real periodic task. Toggling the LED gives a bench observer a visible cadence; the banner gives the UART/HIL scrape a per-period marker.
| k_ra8_ok | LED toggled and banner transmitted. |
Definition at line 373 of file main.c.
References k_lpi_led, k_lpi_work_msg, k_ra8_ok, ra8_board_led_toggle(), and ra8_board_uart_console_write().
Referenced by main().
|
staticnodiscard |
Arm ULPT0, drop to Software Standby, and clean up after wake.
Re-arms the ULPT countdown, confirms ULPTCR.TCSTF = 1, enters Software Standby until the underflow cancels it, then stops ULPT0 (ra8_ulpt_stop clears ULPTCR.TUNF) so the next period starts clean. The standby entry is the bench-proven sequence from lpm_ulpt_standby.
| k_ra8_ok | Slept and woke cleanly. |
| k_ra8_err_hw_timeout | TCSTF never confirmed (see lpi_wait_count_started). |
Definition at line 407 of file main.c.
References k_lpi_channel, k_lpi_period_ticks, k_ra8_ok, k_ra8_sleep_mode_software_std, lpi_wait_count_started(), ra8_lpm_enter_sleep(), ra8_ulpt_start(), and ra8_ulpt_stop().
Referenced by main().
|
static |
Park the CPU forever on an unrecoverable bring-up failure.
Spins in WFI so an attached debugger can inspect state. Reached only when a precondition of the deep-idle loop cannot be met.
Definition at line 179 of file main.c.
Referenced by lpi_setup_or_halt(), and main().
|
static |
Bring CGC + SysTick + SCI8 + LED + ULPT + LPM block up.
Same shape as lpm_ulpt_standby but also initialises the heartbeat LED. The SCI8 J-Link console (PD02 TXD / PD03 RXD) is brought up via the EK-RA8D2 board-support console API. LOCO is kept running so ULPTLCLK survives Software Standby.
Definition at line 272 of file main.c.
References k_lpi_baud, k_lpi_led, k_ra8_clock_id_cpuclk0, k_ra8_lpm_clock_loco, k_ra8_lpm_dcssmode_128us, k_ra8_lpm_ss2lp_default, k_ra8_ok, lpi_panic_halt(), ra8_board_led_init(), ra8_board_uart_console_init(), ra8_cgc_get_clock_hz(), ra8_cgc_init(), ra8_lpm_init(), ra8_lpm_set_clock_stop(), ra8_time_init(), and ra8_ulpt_init().
Referenced by main().
|
static |
ULPT0 underflow interrupt service routine.
Real path: ULPT0 counter underflow -> ULPT0_ULPTI ELC event -> IELSRn link -> NVIC pend -> ra8_isr_dispatch -> here. Cancelling Software Standby is handled by the WUPEN1.ULP0U detector + the NVIC pend; this handler only records liveness. The underflow source flag (ULPTCR.TUNF) is cleared by the ra8_ulpt_stop in the loop's re-arm step, so the ISR stays minimal and allocation-free.
| [in] | ctx | Unused registration context. |
Definition at line 205 of file main.c.
References g_lpm_periodic_wake_count.
Referenced by lpi_arm_wake().
|
staticnodiscard |
Spin until the ULPT count operation has actually started.
HUM Section 25.4.7 (p 1214) requires confirming ULPTCR.TCSTF = 1 (count operation started) before entering a standby mode – otherwise the standby transition can gate the sync clock before the counter has begun, leaving it stalled and unable to underflow. ra8_ulpt_start only sets TSTART; this closes the gap from the application side. The spin is bounded by k_lpi_tcstf_poll_limit (NASA P10 Rule 2).
| [in] | channel | ULPT channel index (0 or 1). |
| k_ra8_ok | Count confirmed running. |
| k_ra8_err_hw_timeout | Bound reached without TCSTF set. |
channel. Definition at line 236 of file main.c.
References k_lpi_tcstf_poll_limit, k_ra8_err_hw_timeout, k_ra8_ok, k_ra8_ulpt_bit_tcstf, and ra8_ulpt_get_status().
Referenced by lpi_idle_one_period().
| void main | ( | void | ) |
The application entry point Reset_Handler hands control to.
Returns void, not int. This is a freestanding image: there is no hosted C environment, no process and nothing to report an exit status to. ISO C fixes main at int only for a hosted implementation; for a freestanding one (C23 5.1.2.1) the startup function's name and type are implementation-defined, and this is that definition. Reset_Handler discards no value because there is none to discard, and if main ever does return, startup halts the CPU rather than resuming anything.
The firmware lane is compiled -ffreestanding (see cmake/ra8_add_app.cmake) and the flag and this signature travel together: without it both GCC and clang reject a non-int main (-Wmain / -Wmain-return-type). Do not remove one without the other.
That coupling is why the declaration sits behind __STDC_HOSTED__ == 0, which -ffreestanding sets and a hosted build does not. The guard is not defensive dressing: this header is reachable from host builds (the unit tests compile ra8_core natively), and an unguarded void main(void); makes every hosted translation unit that includes it fail with conflicting types for 'main' against its own ISO int main. The declaration therefore exists exactly where its contract does.
Hosted first-party code – everything under tests/ and tools/ – uses the ISO int main(...) contract instead, because it genuinely does run under an OS that reads the exit status. scripts/checks/check_entry_points.py holds each domain to its own contract (#707).
Declared here, once, for the same reason SystemInit is: every vector_table.c used to restate it as a local extern int32_t main(void);, sixteen copies that no compiler ever compared against the definition – and roughly thirty of them had silently drifted out of agreement with the main they called.
The application entry point Reset_Handler hands control to.
Brings up CGC + BSP audio then plays blocks.
The application entry point Reset_Handler hands control to.
Brings up CGC + GPT triple, runs sweep.
The application entry point Reset_Handler hands control to.
Brings up clocks + UART + RMII pins, then ThreadX.
The application entry point Reset_Handler hands control to.
Brings up clocks + UART, then enters ThreadX.
The application entry point Reset_Handler hands control to.
Brings up LED, console, SDHI pins, then ThreadX.
The application entry point Reset_Handler hands control to.
Brings up CGC + USB-FS + UAC1, then enters the iso-IN feed loop forever.
The application entry point Reset_Handler hands control to.
The application entry point Reset_Handler hands control to.
See file header.
The application entry point Reset_Handler hands control to.
The application entry point Reset_Handler hands control to.
The application entry point Reset_Handler hands control to.
The application entry point Reset_Handler hands control to.
Definition at line 424 of file main.c.
References k_lpi_boot_msg, k_lpi_pass_msg, k_lpi_period_count, k_ra8_ok, lpi_arm_wake(), lpi_do_work(), lpi_idle_one_period(), lpi_panic_halt(), lpi_setup_or_halt(), ra8_board_uart_console_write(), and ra8_isr_globals_enable().
| volatile uint32_t g_lpm_periodic_wake_count = 0U |
Liveness counter – bumped in the ULPT0 underflow ISR.
Incremented from lpi_ulpt_isr each time a ULPT0 underflow cancels Software Standby. Exposed (non-static, volatile) so a J-Link session can watch the genuine wake cadence independent of the loop's own period counter.
Definition at line 164 of file main.c.
Referenced by lpi_ulpt_isr().
|
static |
|
static |
|
static |
Per-period work banner – one per active period.
Definition at line 144 of file main.c.
Referenced by lpi_do_work().