64static const char*
s_tag =
"PDG";
80typedef enum : uint16_t {
97typedef enum : uint32_t {
107typedef enum : uint32_t {
117typedef enum : uint32_t {
126typedef enum : uint32_t {
203 for (uint16_t u = 0U; u < usec; ++u) {
409 const uint16_t frange_bits =
433 const uint16_t bypass_off =
535 for (uint8_t i = 0U; i < count; ++i) {
546 for (uint8_t i = 0U; i < count; ++i) {
547 volatile uint16_t* cell =
560 if (gptclk_hz == 0U) {
574 const uint64_t numer = (uint64_t)delay_ns * (uint64_t)divider * (uint64_t)gptclk_hz;
580 *out_code = (uint8_t)code;
677 const uint16_t cr = reg->
GTDLYCR;
768 if (gptclk_hz == 0U) {
799 const uint16_t saved_cr2 = reg->
GTDLYCR2;
810 const uint16_t frange_bits =
850 if (gpt ==
nullptr) {
868 if (gpt ==
nullptr) {
883 uint32_t compare_match,
901 if (compare_match >= boundary) {
924 if ((pclka_hz == 0U) || (gptclk_hz == 0U)) {
static const char * s_tag
Logging / check tag.
Annotation-attribute framework macros for ra8-firmware.
#define RA8_HW_REGISTER_ACCESS
Mark an MMIO accessor function (returns volatile register pointer).
#define RA8_ISR_SAFE
The function is callable from interrupt context.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
ra8_board_eth_pin_t pin
Pin.
Validation and Error-Checking Macros for ra8-firmware.
#define RA8_RETURN_ON_ERROR(err, tag, message)
Early return on error, propagating the code upward.
#define RA8_CHECK_NULL_PTR(ptr, tag, message)
Reject nullptr pointer, returning k_ra8_err_null_ptr.
Error Code Definitions for ra8-firmware.
@ k_ra8_err_hw_unmapped
Peripheral register block is not mapped in this MCU variant.
@ k_ra8_err_out_of_range
Sensor or peripheral output out of valid range.
@ k_ra8_err_invalid_arg
Invalid function argument.
@ k_ra8_err_invalid_state
Module in wrong state for requested operation.
@ k_ra8_err_not_initialized
Module not initialized – _init() not yet called successfully.
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
General PWM Timer (GPT) register layout for the Renesas RA8D2.
static volatile r_gpt_channel_regs_t * ra8_gpt(uint8_t channel)
Get pointer to GPT channel N.
Shared CPU-intrinsic seam for the HAL drivers (host/target split).
static void ra8_hw_nop(void)
No-operation used to pad a calibrated busy-wait.
Lightweight Logging Interface for ra8-firmware.
#define ra8_log_info(tag, message)
RA8 log info.
Ref-counted Module Stop Control wrapper for the RA8D2.
ra8_err_t ra8_mstp_enable(ra8_mstp_t id)
Reference-counted "ungate this peripheral" request.
ra8_err_t ra8_mstp_disable(ra8_mstp_t id)
Reference-counted "gate this peripheral" request.
@ k_ra8_mstp_pdg
MSTPD6 PDG.
static void * s_pdg_event_ctx
Opaque pointer forwarded to s_pdg_event_fn.
ra8_pdg_unit_t
Units used by internal_freq_to_period_ns.
@ k_ra8_pdg_ns_per_sec
1 second in ns.
ra8_err_t ra8_pdg_delay_ns_to_code(uint32_t delay_ns, uint32_t gptclk_hz, ra8_pdg_frange_t frange, uint8_t *out_code)
Convert a desired pin-to-pin delay (in nanoseconds) to a DLY[6:0] code given a measured GPTCLK freque...
ra8_err_t ra8_pdg_pin_disable(uint8_t channel, ra8_pdg_pin_t pin)
Force a single A or B pin's delay to zero (per-pin disable) without touching the channel-level bypass...
static bool s_pdg_capture_in_flight
Set true between ra8_pdg_capture_start and the callback fire.
ra8_err_t ra8_pdg_set_frange(ra8_pdg_frange_t new_frange)
Switch FRANGE at runtime, re-running the DLL re-lock sequence with the new band.
static volatile uint16_t * internal_dly_cell(uint8_t channel, ra8_pdg_pin_t pin, ra8_pdg_edge_t edge)
Compute a pointer to the right delay register cell.
ra8_err_t ra8_pdg_exit_stop(uint8_t channel)
Power up one PDG channel (clear the GTDLYCR2.DLYENn bit).
ra8_err_t ra8_pdg_clear_status(uint16_t mask)
Clear the soft-reset bit (DLYRST -> 0) and re-enable the DLL.
ra8_err_t ra8_pdg_attach_handler(ra8_pdg_event_fn_t fn, void *ctx)
Attach an asynchronous PDG event callback.
ra8_err_t ra8_pdg_channel_bypass_set(uint8_t channel, uint8_t bypass)
Set or clear the bypass bit for a channel.
ra8_err_t ra8_pdg_bind_gpt_channel(uint8_t channel)
Bind PDG channel n to its host GPT32n channel.
ra8_err_t ra8_pdg_set_delay(uint8_t channel, ra8_pdg_pin_t pin, ra8_pdg_edge_t edge, uint8_t code)
Set the per-edge fine delay for one PDG channel/pin/edge.
ra8_err_t ra8_pdg_capture_start(const ra8_pdg_delay_entry_t *buf, uint8_t len)
Stage a buffer of ra8_pdg_delay_entry_t records and arm them.
static ra8_err_t internal_validate_cfg(const ra8_pdg_config_t *cfg)
Validate a user-supplied PDG configuration.
ra8_err_t ra8_pdg_set_delay_batch(const ra8_pdg_delay_entry_t *entries, uint8_t count)
Atomically program a batch of (channel, pin, edge, code) delay updates.
static void internal_busy_wait_us(uint16_t usec)
Coarse software busy-wait used during init.
ra8_err_t ra8_pdg_get_delay(uint8_t channel, ra8_pdg_pin_t pin, ra8_pdg_edge_t edge, uint8_t *out_code)
Read back the last-programmed delay code.
static void internal_wait_5_gtclk(void)
Spin >= 5 GTCLK cycles after releasing DLYRST (HUM Fig 23.2).
ra8_err_t ra8_pdg_pick_frange(uint32_t gptclk_hz, ra8_pdg_frange_t *out)
Auto-pick a FRANGE value from a measured GPTCLK frequency.
static uint32_t internal_freq_to_period_ns(uint32_t hz)
Convert a frequency in Hz to a period in nanoseconds.
ra8_err_t ra8_pdg_get_status(uint16_t *out)
Read GTDLYCR packed into a status mask.
ra8_err_t ra8_pdg_required_write_ns(uint32_t pclka_hz, uint32_t gptclk_hz, uint32_t *out_ns)
Compute the minimum register-write interval (HUM 23.4.3 p 1163).
static ra8_err_t internal_validate_slot(uint8_t channel, ra8_pdg_pin_t pin, ra8_pdg_edge_t edge, uint8_t code)
Validate a (channel, pin, edge, code) tuple.
ra8_err_t ra8_pdg_capture_stop(void)
Cancel any pending buffered apply and clear the completion latch.
ra8_err_t ra8_pdg_enter_stop(uint8_t channel)
Power down one PDG channel (set GTDLYCR2.DLYENn).
ra8_err_t ra8_pdg_get_status_full(ra8_pdg_status_full_t *out)
Read a fully-decoded snapshot of GTDLYCR + GTDLYCR2.
ra8_pdg_internal_t
Driver-internal magic values (no magic numbers in functions).
@ k_ra8_pdg_channel_mask_all
Bits 0..3 – 4 channels.
@ k_ra8_pdg_dll_lock_us
HUM Fig 23.2 wait.
@ k_ra8_pdg_busy_loops_per_us
Conservative spin floor.
@ k_ra8_pdg_ns_per_sec_kilo
Used in MHz->ns math.
@ k_ra8_pdg_post_reset_loops
>= 5 GTCLK cycles.
ra8_pdg_write_interval_t
Multipliers in the HUM Ch 23.4.3 (p 1163) interval formula.
@ k_ra8_pdg_pclka_mult
Period_of_PCLKA x 6.
@ k_ra8_pdg_gptclk_mult
Period_of_GPTCLK x 4.
ra8_err_t ra8_pdg_deinit(void)
Tear down the PDG block (reset + module-stop).
ra8_pdg_gtwp_key_t
GPT GTWP write-protect key values (mirrors FSP).
@ k_ra8_pdg_gtwp_unlock
Unlock GTWP (WP = 0).
@ k_ra8_pdg_gtwp_lock
Re-lock GTWP (WP = 1).
static bool internal_pdg_is_initialized(void)
Probe live PDG registers to decide whether ra8_pdg_init has run successfully.
ra8_err_t ra8_pdg_init(const ra8_pdg_config_t *cfg)
Initialise the PDG block per HUM Figure 23.2 (p 1160).
ra8_err_t ra8_pdg_unbind_gpt_channel(uint8_t channel)
Reverse ra8_pdg_bind_gpt_channel, restoring write-protect.
void ra8_pdg_dispatch(void)
Fire the registered handler.
static void internal_program_dll(const ra8_pdg_config_t *cfg, ra8_pdg_frange_t frange_use)
Run the six-step DLL bring-up sequence from HUM Fig 23.2.
static bool internal_frange_ok(ra8_pdg_frange_t f)
Validate the FRANGE encoding.
ra8_err_t ra8_pdg_check_constraints(ra8_pdg_wave_mode_t mode, ra8_pdg_count_dir_t dir, uint32_t compare_match, uint32_t gtpr)
Check Table 23.4 constraints before writing a delay register.
static ra8_pdg_event_fn_t s_pdg_event_fn
Most recent callback registered by ra8_pdg_attach_handler.
ra8_pdg_constraint_t
Numeric thresholds in HUM Ch 23.4.2 Table 23.4.
@ k_ra8_pdg_constraint_high_pad
"GTPR - 2" boundary pad.
@ k_ra8_pdg_constraint_low_thresh
"<= 2" boundary.
PWM Delay Generation Circuit (PDG) driver public API.
ra8_pdg_wave_mode_t
PWM waveform shape – selects which Table 23.4 row applies.
@ k_ra8_pdg_wave_saw
Saw-wave PWM (count up or down).
@ k_ra8_pdg_wave_triangle
Triangle-wave PWM.
ra8_pdg_edge_t
Selects which delay register family is targeted.
@ k_ra8_pdg_edge_rising
GTDLYRnA / GTDLYRnB.
@ k_ra8_pdg_edge_falling
GTDLYFnA / GTDLYFnB.
void(* ra8_pdg_event_fn_t)(void *ctx)
PDG event callback (DLL lock event, etc.).
@ k_ra8_pdg_status_in_reset
DLYRST = 1 (held in reset).
ra8_pdg_pin_t
Picks the A or B pin within a PDG channel.
@ k_ra8_pdg_pin_a
GTIOCnA – "A" output of GPT32n.
@ k_ra8_pdg_pin_b
GTIOCnB – "B" output of GPT32n.
ra8_pdg_count_dir_t
Counter direction passed to ra8_pdg_check_constraints.
@ k_ra8_pdg_dir_up
Counter is up-counting (TUCF == 0).
@ k_ra8_pdg_dir_down
Counter is down-counting (TUCF == 1).
PWM Delay Generation Circuit (PDG) register layout for the Renesas RA8D2.
@ k_ra8_pdg_gtdlycr_shift_frange
FRANGE[1:0] starts at bit 8.
@ k_ra8_pdg_channel_count
4 GPT channels mapped to PDG (0..3).
@ k_ra8_pdg_slot_count
4 ch * 2 pin * 2 edge = 16 delay slots.
@ k_ra8_pdg_dly_div_high
Divider in the 155..300 MHz band.
@ k_ra8_pdg_dly_max
Maximum legal delay code.
@ k_ra8_pdg_dly_div_low
Divider in the 80..160 MHz band.
@ k_ra8_pdg_dly_mask
DLY[6:0] occupies the low 7 bits.
ra8_pdg_frange_t
GTDLYCR.FRANGE encoding (HUM Ch 23.2.1 p 1154).
@ k_ra8_pdg_frange_80_160_mhz
80..160 MHz, 1/128 step.
@ k_ra8_pdg_frange_155_300_mhz
155..300 MHz, 1/64 step.
@ k_ra8_pdg_gtdlycr_mask_dllen
RA8 pdg gtdlycr mask dllen.
@ k_ra8_pdg_gtdlycr_mask_dlyrst
RA8 pdg gtdlycr mask dlyrst.
@ k_ra8_pdg_gtdlycr_mask_frange
RA8 pdg gtdlycr mask frange.
@ k_ra8_pdg_gtdlycr2_shift_dlyen
DLYEN field starts at bit 8.
@ k_ra8_pdg_gtdlycr2_shift_dlybs
DLYBS field starts at bit 0.
@ k_ra8_pdg_freq_high_max_hz
300 MHz.
@ k_ra8_pdg_freq_low_min_hz
80 MHz.
@ k_ra8_pdg_freq_overlap_top
Top of the overlap zone.
static volatile r_pdg_regs_t * ra8_pdg(void)
Pointer to the PDG block.
Per-channel GPT register window.
volatile uint32_t GTWP
+0x00 Write-Protection.
Two consecutive 16-bit delay cells that match the PDG layout.
volatile uint16_t B
+2 GTIOCnB delay code (DLY[6:0]).
volatile uint16_t A
+0 GTIOCnA delay code (DLY[6:0]).
PDG block register window mapped at k_ra8_pdg_base_addr.
volatile uint16_t GTDLYCR
+0x000 HUM 23.2.1 p 1154
r_pdg_dly_pair_t GTDLYR[k_ra8_pdg_channel_count]
+0x018..0x027 rise.
r_pdg_dly_pair_t GTDLYF[k_ra8_pdg_channel_count]
+0x028..0x037 fall.
volatile uint16_t GTDLYCR2
+0x002 HUM 23.2.2 p 1155
Driver configuration descriptor for ra8_pdg_init.
uint32_t gptclk_hz
Measured GTCLK frequency (Hz).
ra8_pdg_frange_t frange
GPT core clock band (HUM 23.2.1).
uint8_t channel_mask
Bit n = enable PDG channel n.
uint8_t auto_tune
1 = ignore frange, derive from gptclk_hz.
Single-edge descriptor for the batch update API.
Decoded snapshot of GTDLYCR / GTDLYCR2 returned by ra8_pdg_get_status_full.
uint16_t raw_gtdlycr2
Raw GTDLYCR2 contents.
uint8_t per_channel_powered[k_ra8_pdg_channel_count]
!DLYENn.
uint8_t in_reset
GTDLYCR.DLYRST.
ra8_pdg_frange_t frange
GTDLYCR.FRANGE[1:0].
uint8_t per_channel_bypass_off[k_ra8_pdg_channel_count]
DLYBSn.
uint8_t dll_enabled
GTDLYCR.DLLEN.
uint16_t raw_gtdlycr
Raw GTDLYCR contents.