ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_iwdt.c
Go to the documentation of this file.
1
27
28#include "ra8_iwdt.h"
29
30#include <stdint.h>
31
32#include "ra8_attributes.h"
33#include "ra8_check.h"
34#include "ra8_err.h"
35#include "ra8_iwdt_regs.h"
36#include "ra8_log.h"
37
38static const char* s_tag = "IWDT";
39
44typedef struct {
46 void* ctx;
48
50
59
69[[nodiscard]] ra8_err_t ra8_iwdt_init(void)
70{
71 /* HUM Ch 28.1 "Overview" p 1271 + HUM Ch 7 "Option-Setting Memory":
72 * the counter starts automatically per OFS0.IWDTSTRT and cannot be
73 * stopped by software on RA8D2. Init is documentation-only. */
74 ra8_log_info(s_tag, "iwdt_init (OFS0 controls period; auto-start only)");
75 return k_ra8_ok;
76}
77
95{
96 /* HUM Ch 28.2.1 "IWDTRR : IWDT Refresh Register" p 1273 */
98}
99
101{
102 RA8_CHECK_NULL_PTR(out_mask, s_tag, "out_mask must not be nullptr");
103 /* HUM Ch 28.2.2 "IWDTSR : IWDT Status Register" p 1275
104 * + FSP IWDT_PRV_STATUS_START_BIT (14): we expose the latched
105 * UNDFF / REFEF flags only, masking out CNTVAL[13:0]. */
106 *out_mask = (uint16_t)(ra8_iwdt()->IWDTSR & k_ra8_iwdt_status_all);
107 return k_ra8_ok;
108}
109
111{
112 volatile r_iwdt_regs_t* reg = ra8_iwdt();
113 /* HUM Ch 28.2.2 "IWDTSR : IWDT Status Register" p 1275 -- UNDFF and
114 * REFEF are write-0-to-clear. Per FSP R_IWDT_StatusClear the silicon
115 * takes one IWDTCLK cycle to drop the flag, so the flag clear is
116 * idempotent: writing the masked value once and letting the next
117 * read see the cleared bits is sufficient for our test mock. The
118 * real silicon path on a hot IWDT would loop until the read-back
119 * shows the bit gone (FSP do/while). */
120 reg->IWDTSR = (uint16_t)(reg->IWDTSR & (uint16_t)~k_ra8_iwdt_status_all);
121 return k_ra8_ok;
122}
123
124ra8_err_t ra8_iwdt_get_counter(uint16_t* out_counter)
125{
126 RA8_CHECK_NULL_PTR(out_counter, s_tag, "out_counter must not be nullptr");
127 /* HUM Ch 28.2.2 "IWDTSR.CNTVAL[13:0]" p 1274
128 * + FSP R_IWDT_CounterGet: (*p_count) = IWDTSR & 0x3FFF. */
129 *out_counter = (uint16_t)(ra8_iwdt()->IWDTSR & k_ra8_iwdt_sr_cnt_mask);
130 return k_ra8_ok;
131}
132
134{
135 s_iwdt_state.fn = fn;
136 s_iwdt_state.ctx = ctx;
137 return k_ra8_ok;
138}
139
142{
143 volatile r_iwdt_regs_t* reg = ra8_iwdt();
144 /* HUM Ch 28.2.2 "IWDTSR : IWDT Status Register" p 1275 -- snapshot
145 * the latched flags, clear them, then fire the user callback with
146 * the snapshot value. Mirrors the FSP NMI ISR shape. */
147 const uint16_t mask = (uint16_t)(reg->IWDTSR & k_ra8_iwdt_status_all);
148 reg->IWDTSR = (uint16_t)(reg->IWDTSR & (uint16_t)~mask);
149 const ra8_iwdt_event_fn_t fn = s_iwdt_state.fn;
150 void* const ctx = s_iwdt_state.ctx;
151 if (fn != nullptr) {
152 fn(ctx, mask);
153 }
154}
static const char * s_tag
Logging / check tag.
Definition ra8_app.c:17
Annotation-attribute framework macros for ra8-firmware.
#define RA8_ISR_SAFE
The function is callable from interrupt context.
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
void ra8_iwdt_dispatch(void)
Dispatch an IWDT NMI event – snapshot status + fire callback.
Definition ra8_iwdt.c:141
ra8_iwdt_mask_t
Combined IWDTSR flag mask (UNDFF | REFEF).
Definition ra8_iwdt.c:55
@ k_ra8_iwdt_status_all
RA8 iwdt status all.
Definition ra8_iwdt.c:56
ra8_err_t ra8_iwdt_init(void)
Prepare the IWDT driver layer.
Definition ra8_iwdt.c:69
static ra8_iwdt_state_t s_iwdt_state
Definition ra8_iwdt.c:49
ra8_err_t ra8_iwdt_attach_handler(ra8_iwdt_event_fn_t fn, void *ctx)
Attach a handler for the IWDT NMI event.
Definition ra8_iwdt.c:133
ra8_err_t ra8_iwdt_get_status(uint16_t *out_mask)
Read the IWDTSR status bits (UNDFF / REFEF only).
Definition ra8_iwdt.c:100
ra8_err_t ra8_iwdt_get_counter(uint16_t *out_counter)
Read the live 14-bit IWDT down-counter (CNTVAL).
Definition ra8_iwdt.c:124
void ra8_iwdt_refresh_deferred(void)
Refresh the IWDT counter.
Definition ra8_iwdt.c:94
ra8_err_t ra8_iwdt_clear_status(void)
Clear the underflow / refresh-error flags via IWDTSR write.
Definition ra8_iwdt.c:110
Independent Watchdog driver header.
@ k_ra8_iwdt_status_refresh
IWDTSR.REFEF bit 15.
Definition ra8_iwdt.h:50
@ k_ra8_iwdt_status_underflow
IWDTSR.UNDFF bit 14.
Definition ra8_iwdt.h:49
void(* ra8_iwdt_event_fn_t)(void *ctx, uint16_t status_mask)
IWDT NMI underflow event callback.
Definition ra8_iwdt.h:59
Independent Watchdog Timer (IWDT) register layout for the RA8D2.
static volatile r_iwdt_regs_t * ra8_iwdt(void)
Get pointer to the IWDT register block.
static void ra8_iwdt_refresh(void)
Refresh the IWDT counter using the two-byte unlock sequence.
@ k_ra8_iwdt_sr_cnt_mask
CNTVAL[13:0] mask.
Lightweight Logging Interface for ra8-firmware.
#define ra8_log_info(tag, message)
RA8 log info.
Definition ra8_log.h:364
Memory-mapped layout of the IWDT block.
volatile uint16_t IWDTSR
+0x04 Status register.
Driver-wide runtime state.
Definition ra8_iwdt.c:44
ra8_iwdt_event_fn_t fn
Fn.
Definition ra8_iwdt.c:45
void * ctx
Ctx.
Definition ra8_iwdt.c:46