ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1
50
51#include <stddef.h>
52#include <stdint.h>
53
54#include "ra8_attributes.h"
55#include "ra8_board_ek_ra8d2.h"
56#include "ra8_boot_entry.h"
57#include "ra8_cgc.h"
58#include "ra8_check.h"
59#include "ra8_err.h"
60#include "ra8_gpt.h"
61#include "ra8_isr.h"
62#include "ra8_mstp.h"
63#include "ra8_poeg.h"
64#include "ra8_time.h"
65
67static const char* s_tag = "poeg_demo";
68
70typedef enum : uint32_t {
71 k_poeg_demo_baud = 115200U,
74 k_poeg_demo_gpt_prd = 0x0000FFFFU,
75 k_poeg_demo_gpt_duty = 0x00008000U,
77
79typedef enum : uint8_t {
83
91static const uint8_t s_poeg_demo_ok_msg[] = "poeg: pwm=run shutoff=highZ reenable=on ok=Y\r\n";
99static const uint8_t s_poeg_demo_bad_msg[] = "poeg: pwm=? shutoff=? reenable=? ok=N\r\n";
100
107volatile uint32_t g_poeg_shutoffs = 0U;
108
115volatile uint32_t g_poeg_last_status = 0U;
116
123volatile uint32_t g_poeg_heartbeat = 0U;
124
131volatile uint32_t g_poeg_mismatch = 0U;
132
144{
145 while (1) {
146 __asm__ volatile("wfi");
147 }
148}
149
187
210{
211 const ra8_gpt_cfg_t cfg = {
213 .prescaler = k_ra8_gpt_ps_div_4,
214 .period = (uint32_t)k_poeg_demo_gpt_prd,
215 .duty_a = (uint32_t)k_poeg_demo_gpt_duty,
216 .duty_b = (uint32_t)k_poeg_demo_gpt_duty,
217 .auto_start = true,
218 };
219 return ra8_gpt_init((uint8_t)k_poeg_demo_gpt_channel, &cfg);
220}
221
246{
247 const ra8_poeg_cfg_t cfg = {
248 .enable_pin = true,
249 .enable_ioc = false,
250 .enable_osc_stop = false,
251 .invert_input = false,
252 };
253 return ra8_poeg_init((uint8_t)k_poeg_demo_group, &cfg);
254}
255
286[[nodiscard]] RA8_INTERNAL static ra8_err_t internal_poeg_demo_cycle(uint8_t* out_ok)
287{
288 RA8_CHECK_NULL_PTR(out_ok, s_tag, "out_ok must not be nullptr");
289 *out_ok = 0U;
290
291 /* RUN: sample the free-running PWM counter. */
292 uint32_t before = 0U;
293 const ra8_err_t rd0 = ra8_gpt_read((uint8_t)k_poeg_demo_gpt_channel, &before);
294 if (rd0 != k_ra8_ok) {
295 return rd0;
296 }
297
298 /* SHUTOFF: assert the software output-disable request and read ST back. */
300 if (terr != k_ra8_ok) {
301 return terr;
302 }
303 uint32_t asserted = 0U;
304 const ra8_err_t serr = ra8_poeg_get_status((uint8_t)k_poeg_demo_group, &asserted);
305 if (serr != k_ra8_ok) {
306 return serr;
307 }
308 g_poeg_last_status = asserted;
309 const uint8_t shutoff = ((asserted & (uint32_t)k_ra8_poeg_status_st) != 0U) ? 1U : 0U;
310
311 /* Hold the shutoff briefly; the GPT counter keeps advancing meanwhile. */
313
314 /* RE-ENABLE: clear the request and confirm ST fell back to 0. */
315 const ra8_err_t cerr =
317 if (cerr != k_ra8_ok) {
318 return cerr;
319 }
320 uint32_t cleared = 0U;
321 const ra8_err_t cs = ra8_poeg_get_status((uint8_t)k_poeg_demo_group, &cleared);
322 if (cs != k_ra8_ok) {
323 return cs;
324 }
325 const uint8_t reenabled = ((cleared & (uint32_t)k_ra8_poeg_status_st) == 0U) ? 1U : 0U;
326
327 /* RUN confirm: the PWM counter must have advanced across the hold. */
328 uint32_t after = 0U;
329 const ra8_err_t rd1 = ra8_gpt_read((uint8_t)k_poeg_demo_gpt_channel, &after);
330 if (rd1 != k_ra8_ok) {
331 return rd1;
332 }
333 const uint8_t running = (after != before) ? 1U : 0U;
334
335 *out_ok = (shutoff != 0U && reenabled != 0U && running != 0U) ? 1U : 0U;
336 return k_ra8_ok;
337}
338
339void main(void)
340{
342 /* Clear PRIMASK so SysTick can dispatch and ra8_delay_ms() uses the SysTick
343 * path (ra8_emulator does not advance DWT_CYCCNT). No NVIC sources are armed. */
345
348 }
351 }
352
353 while (1) {
354 uint8_t ok = 0U;
355 const ra8_err_t err = internal_poeg_demo_cycle(&ok);
356 const uint8_t good = (err == k_ra8_ok && ok != 0U) ? 1U : 0U;
357 if (good != 0U) {
359 (size_t)(sizeof(s_poeg_demo_ok_msg) - 1U));
362 } else {
364 (size_t)(sizeof(s_poeg_demo_bad_msg) - 1U));
367 }
370 }
372}
void main(void)
Secure fallback main entry point.
Definition main.c:37
static ra8_err_t internal_poeg_demo_arm_gpt(void)
Configure GPT0 for saw-wave PWM with auto_start = true.
Definition main.c:209
volatile uint32_t g_poeg_last_status
Last POEGG snapshot latched immediately after the shutoff assert.
Definition main.c:115
poeg_demo_config_t
Compile-time settings.
Definition main.c:70
@ k_poeg_demo_gpt_duty
Mid-scale PWM duty (50%).
Definition main.c:75
@ k_poeg_demo_gpt_prd
16-bit GPT period (saw PWM).
Definition main.c:74
@ k_poeg_demo_period_ms
Delay between shutoff cycles.
Definition main.c:72
@ k_poeg_demo_baud
SCI console baud rate.
Definition main.c:71
@ k_poeg_demo_settle_ms
High-Z hold while the PWM runs.
Definition main.c:73
poeg_demo_unit_t
GPT channel + POEG group the demo pairs.
Definition main.c:79
@ k_poeg_demo_group
POEG group gating GPT0.
Definition main.c:81
@ k_poeg_demo_gpt_channel
GPT channel driving the PWM.
Definition main.c:80
static void internal_poeg_demo_panic_halt(void)
Park forever after a fatal initialization failure.
Definition main.c:143
volatile uint32_t g_poeg_heartbeat
Bumps once per main-loop pass – liveness for headless probes.
Definition main.c:123
volatile uint32_t g_poeg_mismatch
Latches the count of cycles whose verdict was not healthy.
Definition main.c:131
volatile uint32_t g_poeg_shutoffs
Count of healthy safe-shutoff cycles completed (assert + re-enable).
Definition main.c:107
static ra8_err_t internal_poeg_demo_cycle(uint8_t *out_ok)
Run one safe-shutoff cycle and fold the result into the verdict.
Definition main.c:286
static const uint8_t s_poeg_demo_ok_msg[]
Success banner for a complete safe-shutoff and re-enable cycle.
Definition main.c:91
static void internal_poeg_demo_setup_or_halt(void)
Bring CGC + SysTick + MSTP + SCI console + LEDs up.
Definition main.c:161
static const uint8_t s_poeg_demo_bad_msg[]
Failure banner for an incomplete POEG safety cycle.
Definition main.c:99
static ra8_err_t internal_poeg_demo_arm_poeg(void)
Arm POEG group 0 to accept an output-disable request.
Definition main.c:245
static const char * s_tag
Logging / check tag.
Definition ra8_app.c:17
Annotation-attribute framework macros for ra8-firmware.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
Board-support layer for the Renesas EK-RA8D2 v1 evaluation kit.
ra8_err_t ra8_board_led_toggle(ra8_board_led_id_t led)
Toggle led's output state.
ra8_err_t ra8_board_led_init(ra8_board_led_id_t led)
Configure led as a digital output, initial level low (off).
@ k_ra8_board_led2
LED2, GREEN, P303 (jumper E26).
@ k_ra8_board_led1
LED1, BLUE, P600 (jumper E27).
ra8_err_t ra8_board_uart_console_write(const uint8_t *data, size_t len)
Polled blocking write to the J-Link OB VCOM console.
ra8_err_t ra8_board_uart_console_init(uint32_t baud)
Configure SCI8 + PD02/PD03 as the debug-console UART.
Boot entry points shared between a vector table and its startup code.
High-level Clock Generation Circuit driver.
ra8_err_t ra8_cgc_get_clock_hz(ra8_clock_id_t id, uint32_t *out_hz)
Query the current frequency of a clock-tree domain.
Definition ra8_cgc.c:132
@ k_ra8_clock_id_cpuclk0
Cortex-M85 CPUCLK0.
Definition ra8_cgc.h:70
ra8_err_t ra8_cgc_init(void)
Configure the clock tree to a safe default.
Definition ra8_cgc.c:727
Validation and Error-Checking Macros for ra8-firmware.
#define RA8_CHECK_NULL_PTR(ptr, tag, message)
Reject nullptr pointer, returning k_ra8_err_null_ptr.
Definition ra8_check.h:243
Error Code Definitions for ra8-firmware.
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
Full-featured General PWM Timer (GPT) driver.
@ k_ra8_gpt_mode_saw_pwm
Saw-wave PWM (up-count).
Definition ra8_gpt.h:54
ra8_err_t ra8_gpt_init(uint8_t channel, const ra8_gpt_cfg_t *cfg)
Initialise a GPT channel with a full config descriptor.
Definition ra8_gpt.c:329
ra8_err_t ra8_gpt_read(uint8_t channel, uint32_t *out)
Read the current counter value.
Definition ra8_gpt.c:314
@ k_ra8_gpt_ps_div_4
PCLKD / 4.
Definition ra8_gpt.h:67
NVIC + ICU IELSR allocator.
void ra8_isr_globals_enable(void)
Globally enable maskable interrupts (PRIMASK = 0).
Definition ra8_isr.c:439
Ref-counted Module Stop Control wrapper for the RA8D2.
ra8_err_t ra8_mstp_init(void)
Re-establish the ra8_mstp ref-count table from current hardware state.
Definition ra8_mstp.c:291
Port Output Enable for GPT (POEG) driver.
ra8_err_t ra8_poeg_trigger_stop(uint8_t group)
Trigger an emergency stop via POEGG.SSF (software).
Definition ra8_poeg.c:202
ra8_err_t ra8_poeg_get_status(uint8_t group, uint32_t *out_mask)
Read POEGG status flag bits.
Definition ra8_poeg.c:236
ra8_err_t ra8_poeg_init(uint8_t group, const ra8_poeg_cfg_t *cfg)
Initialise a POEG group with a configuration descriptor.
Definition ra8_poeg.c:126
ra8_err_t ra8_poeg_clear_status(uint8_t group, uint32_t mask)
Clear one or more POEGG status flag bits.
Definition ra8_poeg.c:270
@ k_ra8_poeg_status_st
POEGG.ST state.
Definition ra8_poeg.h:62
@ k_ra8_poeg_status_ssf
POEGG.SSF Software.
Definition ra8_poeg.h:61
SysTick-based tick counter, delay and timestamp helpers.
ra8_err_t ra8_time_init(uint32_t cpu_hz)
Initialise SysTick for a 1 kHz tick interrupt.
Definition ra8_time.c:59
void ra8_delay_ms(uint32_t ms)
Busy-wait for at least ms milliseconds.
Definition ra8_time.c:129
Configuration descriptor for ra8_gpt_init.
Definition ra8_gpt.h:83
Configuration descriptor for ra8_poeg_init.
Definition ra8_poeg.h:85