28static const char*
s_tag =
"GPT";
58typedef enum : uint32_t {
83typedef enum : uint32_t {
117 static bool s_gpt_clock_inited =
false;
118 if (s_gpt_clock_inited) {
126 s_gpt_clock_inited =
true;
140typedef enum : uint32_t {
173typedef enum : uint32_t {
207typedef enum : uint32_t {
225typedef enum : uint8_t {
293 reg->
GTCR = 0x00000001UL;
403 reg->
GTCCR[(uint8_t)which] = value;
424 const uint32_t current = reg->
GTST;
445 if (reg !=
nullptr) {
464 const uint32_t* periods,
468 uint8_t* out_dma_channel)
500 uint8_t* out_dma_channel)
512 req.
dst_addr = (uintptr_t)out_counts;
565 *out_value = reg->
GTCCR[(uint8_t)which];
583 reg->
GTDNSR = down_source;
601 reg->
GTPBR = period_counts;
606 reg->
GTPR = period_counts;
689 uint32_t gtior = reg->
GTIOR;
722 reg->
GTDVU = rising_dt;
723 reg->
GTDVD = falling_dt;
781 .duty_a = initial_duties[i],
782 .duty_b = initial_duties[i],
787 for (uint8_t j = 0U; j < i; ++j) {
792 mask |= (uint32_t)(1UL << cfg->
channels[i]);
821 if (u_reg !=
nullptr) {
842 const uint32_t period = u_reg->
GTPR;
844 if (duties[i] > period) {
867 if (u_reg !=
nullptr) {
889 if (reg !=
nullptr) {
890 const uint32_t current = reg->
GTST;
891 reg->
GTST = current & ~status_mask;
896 fn(ctx, status_mask);
static const char * s_tag
Logging / check tag.
Annotation-attribute framework macros for ra8-firmware.
#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.
Generic DMA transfer substrate (DMAC engine).
ra8_err_t ra8_dma_request(const ra8_dma_request_t *req, uint8_t *out_channel)
Allocate a DMAC channel, programme it, and start the trigger wiring.
void(* ra8_dma_complete_fn_t)(void *ctx)
Caller-supplied completion callback.
8-channel DMA controller (DMAC0) driver
@ k_ra8_dmac_width_word
32-bit transfers (DMTMD.SZ = 10b).
ra8_elc_event_t
Partial list of ELC events (populate as drivers need them).
Error Code Definitions for ra8-firmware.
@ k_ra8_err_invalid_arg
Invalid function argument.
@ k_ra8_err_invalid_state
Module in wrong state for requested operation.
@ 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.
void ra8_gpt_dispatch_ccrb(uint8_t channel)
Dispatch GPT compare-match B (GTCIB) – clear + fire callback.
ra8_err_t ra8_gpt_pwm_pin_configure(uint8_t channel, ra8_gpt_pwm_pin_t pin, const ra8_gpt_pwm_pin_cfg_t *cfg)
Configure GTIOR for one PWM output pin.
ra8_err_t ra8_gpt_capture_read(uint8_t channel, ra8_gpt_ccr_sel_t which, uint32_t *out_value)
Read the value latched by the last input capture.
ra8_gpt_ccr_idx_t
Index positions inside GTCCR[6] used by the buffer-based duty cycle setter.
@ k_ra8_gpt_ccr_idx_b
RA8 GPT ccr index b.
@ k_ra8_gpt_ccr_idx_c
RA8 GPT ccr index c.
@ k_ra8_gpt_ccr_idx_e
RA8 GPT ccr index e.
@ k_ra8_gpt_ccr_idx_a
RA8 GPT ccr index a.
ra8_err_t ra8_gpt_dead_time_set(uint8_t channel, uint32_t rising_dt, uint32_t falling_dt)
Programme the dead-time value pair for complementary PWM.
static uint32_t internal_gtio_pattern(ra8_gpt_pwm_polarity_t polarity)
Build the GTIO[A/B] sub-field encoding for the requested polarity.
static void internal_dispatch(uint8_t channel, uint32_t status_mask)
ra8_err_t ra8_gpt_start_free_run(uint8_t channel, uint32_t period)
Configure a GPT channel as a free-running up-counter.
static ra8_gpt_state_t s_gpt_state[k_ra8_gpt_channel_count]
static ra8_err_t internal_three_phase_init_subs(const ra8_gpt_three_phase_cfg_t *cfg, uint32_t *out_mask)
Bring up the three GPT submodules and build the start mask.
ra8_err_t ra8_gpt_stop(uint8_t channel)
Stop a GPT channel.
ra8_gtwp_t
GTWP write-protect key.
@ k_ra8_gtwp_key_unlock
Password in upper byte, WP=0.
@ k_ra8_gtwp_key_lock
Password in upper byte, WP=1.
ra8_err_t ra8_gpt_attach_handler(uint8_t channel, ra8_gpt_event_fn_t fn, void *ctx)
Attach a GPT event callback for a channel.
ra8_err_t ra8_gpt_set_period(uint8_t channel, uint32_t period)
Change the GPT period (GTPR / GTPBR) at runtime.
void ra8_gpt_dispatch_und(uint8_t channel)
Dispatch GPT underflow (GTCIU) – clear + fire callback.
ra8_err_t ra8_gpt_set_duty(uint8_t channel, ra8_gpt_ccr_sel_t which, uint32_t value)
Change one compare register (GTCCRA / GTCCRB) at runtime.
ra8_gpt_buffer_bits_t
GTBER bits and GTDTCR bits used by the runtime PWM API.
@ k_ra8_gpt_gtber_ccra_single
RA8 GPT gtber ccra single.
@ k_ra8_gpt_gtdtcr_tde
RA8 GPT gtdtcr tde.
@ k_ra8_gpt_gtber_ccrb_single
RA8 GPT gtber ccrb single.
@ k_ra8_gpt_gtcr_cst_mask
RA8 GPT gtcr cst mask.
ra8_err_t ra8_gpt_write_dma(uint8_t channel, const uint32_t *periods, uint16_t count, ra8_dma_complete_fn_t on_complete, void *ctx, uint8_t *out_dma_channel)
Stream a buffer of period values into GPT GTPR via DMA.
ra8_err_t ra8_gpt_three_phase_set_duty(uint32_t u_duty, uint32_t v_duty, uint32_t w_duty)
Atomically update U/V/W compare values via the shadow buffer.
ra8_err_t ra8_gpt_duty_cycle_set(uint8_t channel, ra8_gpt_pwm_pin_t pin, uint32_t compare_counts)
Update one PWM compare value via the shadow buffer.
ra8_err_t ra8_gpt_read_dma(uint8_t channel, uint32_t *out_counts, uint16_t count, ra8_dma_complete_fn_t on_complete, void *ctx, uint8_t *out_dma_channel)
Capture a stream of GTCNT values into a buffer via DMA.
static const ra8_mstp_t s_gpt_mstp_table[k_ra8_gpt_channel_count]
Channel-index -> MSTP id lookup.
ra8_err_t ra8_gpt_clear_status(uint8_t channel, uint32_t mask)
Clear GTST flag bits (write-0 to clear).
ra8_err_t ra8_gpt_exit_stop(uint8_t channel)
Exit MSTP-gated stop.
ra8_err_t ra8_gpt_three_phase_close(void)
Stop and tear down the three U/V/W channels.
ra8_err_t ra8_gpt_capture_configure(uint8_t channel, ra8_gpt_ccr_sel_t which, uint32_t source_mask)
Arm a GPT channel for input capture on GTCCRA or GTCCRB.
ra8_gpt_bits_t
GTCR / GTSTR / GTSTP / GTST bit positions.
@ k_ra8_gpt_gtst_mask
TCFPU|TCFPO|TCFB|TCFA.
@ k_ra8_gpt_gtcr_cst_set
GTCR.CST start.
@ k_ra8_gpt_gtstp_stop
GTSTP.CSTOP0 write.
@ k_ra8_gpt_gtcr_md_shift
GTCR.MD bit0.
@ k_ra8_gpt_gtstr_start
GTSTR.CSTRT0 write.
@ k_ra8_gpt_gtcr_tpcs_shift
GTCR.TPCS bit0.
ra8_err_t ra8_gpt_three_phase_open(const ra8_gpt_three_phase_cfg_t *cfg)
Open three GPT channels as a phase-synchronized U/V/W triple.
static void internal_gpt_clock_block_init(void)
Block-level GPT clock-control init (one-shot, MSTP-stopped).
ra8_gptclkcr_bits_t
GTCLKCR block-level clock-control bits.
@ k_ra8_gptclkcr_bpen
BPEN: PCLKA <-> GTCLK sync.
void ra8_gpt_dispatch_ccra(uint8_t channel)
Dispatch GPT compare-match A (GTCIA) – clear + fire callback.
void ra8_gpt_dispatch_ovf(uint8_t channel)
Dispatch GPT overflow (GTCIV) – clear + fire callback.
static ra8_gpt_three_phase_state_t s_three_phase
ra8_err_t ra8_gpt_init(uint8_t channel, const ra8_gpt_cfg_t *cfg)
Initialise a GPT channel with a full config descriptor.
ra8_err_t ra8_gpt_counter_set(uint8_t channel, uint32_t value)
Force GTCNT to value (timer must be stopped).
ra8_err_t ra8_gpt_deinit(uint8_t channel)
Tear down a channel (stop + MSTP release).
ra8_gpt_gtior_bits_t
GTIOR field positions and masks.
@ k_ra8_gpt_gtiob_mask
RA8 GPT gtiob mask.
@ k_ra8_gpt_gtio_active_high
RA8 GPT gtio active high.
@ k_ra8_gpt_oadf_shift
RA8 GPT oadf shift.
@ k_ra8_gpt_oadf_mask
RA8 GPT oadf mask.
@ k_ra8_gpt_oadflt_shift
RA8 GPT oadflt shift.
@ k_ra8_gpt_obdflt_mask
RA8 GPT obdflt mask.
@ k_ra8_gpt_gtio_active_low
RA8 GPT gtio active low.
@ k_ra8_gpt_oae_mask
RA8 GPT oae mask.
@ k_ra8_gpt_oadflt_mask
RA8 GPT oadflt mask.
@ k_ra8_gpt_obdflt_shift
RA8 GPT obdflt shift.
@ k_ra8_gpt_gtioa_shift
RA8 GPT gtioa shift.
@ k_ra8_gpt_obdf_shift
RA8 GPT obdf shift.
@ k_ra8_gpt_obdf_mask
RA8 GPT obdf mask.
@ k_ra8_gpt_gtiob_shift
RA8 GPT gtiob shift.
@ k_ra8_gpt_obe_mask
RA8 GPT obe mask.
@ k_ra8_gpt_gtioa_mask
RA8 GPT gtioa mask.
ra8_err_t ra8_gpt_get_status(uint8_t channel, uint32_t *out_mask)
Read GTST overflow / underflow / compare flags.
ra8_err_t ra8_gpt_read(uint8_t channel, uint32_t *out)
Read the current counter value.
static uint32_t internal_gtcr(ra8_gpt_mode_t mode, ra8_gpt_prescaler_t ps)
ra8_err_t ra8_gpt_enter_stop(uint8_t channel)
Put a channel into MSTP-gated stop.
ra8_err_t ra8_gpt_period_set(uint8_t channel, uint32_t period_counts)
Update the GPT period via the shadow buffer (GTPBR).
ra8_err_t ra8_gpt_event_count_configure(uint8_t channel, uint32_t up_source, uint32_t down_source)
Configure external event (edge) counting on GTCNT.
Full-featured General PWM Timer (GPT) driver.
@ k_ra8_gpt_three_phase_count
Number of phases.
ra8_gpt_pwm_pin_t
Selector for the GTIOCnA / GTIOCnB PWM output pins.
@ k_ra8_gpt_pin_b
GTIOCnB – compare register B path.
@ k_ra8_gpt_pin_a
GTIOCnA – compare register A path.
ra8_gpt_ccr_sel_t
Selector for ra8_gpt_set_duty.
@ k_ra8_gpt_status_ccrb
GTST.TCFB (bit 1).
@ k_ra8_gpt_status_ccra
GTST.TCFA (bit 0).
@ k_ra8_gpt_status_overflow
GTST.TCFPO (bit 6).
@ k_ra8_gpt_status_underflow
GTST.TCFPU (bit 7).
void(* ra8_gpt_event_fn_t)(void *ctx, uint32_t status_mask)
GPT IRQ event callback signature.
ra8_gpt_mode_t
GPT counter-mode selection (GTCR.MD field).
ra8_gpt_pwm_polarity_t
Output polarity for ra8_gpt_pwm_pin_configure.
@ k_ra8_gpt_pol_active_low
Output is low while duty < count.
ra8_gpt_prescaler_t
GTCR.TPCS clock prescaler.
GPT input-capture + external event / pulse-count extension.
@ k_ra8_gpt_cnt_src_valid_mask
bits 0..23: all legal bits.
@ k_ra8_gpt_cap_src_valid_mask
bits 0..24: all legal bits.
General PWM Timer (GPT) register layout for the Renesas RA8D2.
@ k_ra8_gpt_channel_count
RA8 GPT channel count.
@ k_ra8_gpt_gtclk_addr
Shared GTCLK regs.
static volatile r_gpt_channel_regs_t * ra8_gpt(uint8_t channel)
Get pointer to GPT channel N.
Lightweight Logging Interface for ra8-firmware.
#define ra8_log_info_val(tag, message, value)
RA8 log info val.
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.
ra8_mstp_t
Packed (reg << 8) | bit module-stop identifier.
@ k_ra8_mstp_gpt2
MSTPE29 GPT2.
@ k_ra8_mstp_gpt11
MSTPE20 GPT11.
@ k_ra8_mstp_gpt3
MSTPE28 GPT3.
@ k_ra8_mstp_gpt10
MSTPE21 GPT10.
@ k_ra8_mstp_gpt4_9
MSTPE27 GPT4..GPT9.
@ k_ra8_mstp_gpt1
MSTPE30 GPT1.
@ k_ra8_mstp_gpt13
MSTPE18 GPT13.
@ k_ra8_mstp_gpt0
MSTPE31 GPT0.
@ k_ra8_mstp_gpt12
MSTPE19 GPT12.
Per-channel GPT register window.
volatile uint32_t GTICBSR
+0x28 Input capture B source select.
volatile uint32_t GTBER
+0x40 Buffer Enable.
volatile uint32_t GTICASR
+0x24 Input capture A source select.
volatile uint32_t GTDNSR
+0x20 Down-count source select.
volatile uint32_t GTPR
+0x64 Period.
volatile uint32_t GTSTP
+0x08 Software Stop.
volatile uint32_t GTPBR
+0x68 Period buffer.
volatile uint32_t GTDVD
+0x90 Dead Time Value D.
volatile uint32_t GTCNT
+0x48 Counter.
volatile uint32_t GTCCR[6]
+0x4C..+0x60 Compare/Capture A..F.
volatile uint32_t GTUPSR
+0x1C Up-count source select.
volatile uint32_t GTIOR
+0x34 I/O Control.
volatile uint32_t GTSTR
+0x04 Software Start.
volatile uint32_t GTWP
+0x00 Write-Protection.
volatile uint32_t GTDTCR
+0x88 Dead Time Control.
volatile uint32_t GTCR
+0x2C Timer Control.
volatile uint32_t GTDVU
+0x8C Dead Time Value U.
volatile uint32_t GTST
+0x3C Status.
Descriptor for a single DMA transfer.
ra8_dma_complete_fn_t on_complete
On complete.
uintptr_t dst_addr
Dst address.
ra8_dmac_width_t width
Width.
uintptr_t src_addr
Src address.
ra8_elc_event_t trigger
Trigger.
Configuration descriptor for ra8_gpt_init.
ra8_gpt_prescaler_t prescaler
Clock divider.
uint32_t duty_b
GTCCRB compare/duty (PWM B).
bool auto_start
True -> start after init.
ra8_gpt_mode_t mode
Counter mode.
uint32_t duty_a
GTCCRA compare/duty (PWM A).
uint32_t period
GTPR period.
Configuration descriptor for ra8_gpt_pwm_pin_configure.
ra8_gpt_pwm_polarity_t polarity
Active-high vs active-low.
bool output_enable
OAE / OBE – enable pin.
ra8_gpt_pwm_disable_t disable_on_fault
OADF / OBDF (POEG).
ra8_gpt_pwm_stop_level_t stop_level
OADFLT / OBDFLT.
Per-channel runtime state (callback, configured flag).
bool configured
True after ra8_gpt_init.
ra8_gpt_event_fn_t fn
Registered callback.
void * ctx
Callback context.
Configuration descriptor for ra8_gpt_three_phase_open.
ra8_gpt_mode_t mode
Triangle/saw.
uint32_t initial_duty_u
Initial duty U.
uint32_t period_counts
Shared GTPR.
ra8_gpt_prescaler_t prescaler
Clock div.
uint32_t initial_duty_w
Initial duty W.
uint8_t channels[k_ra8_gpt_three_phase_count]
U/V/W ch ids.
uint32_t initial_duty_v
Initial duty V.
Driver-internal three-phase tracking state.
uint32_t channel_mask
OR of (1<<ch) bits.
uint8_t channels[k_ra8_gpt_three_phase_count]
Channel ids.