ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_dac_b.c File Reference

12-Bit D/A Converter (DAC_B) driver implementation More...

#include "ra8_dac_b.h"
#include <stdint.h>
#include "ra8_attributes.h"
#include "ra8_check.h"
#include "ra8_dac_b_regs.h"
#include "ra8_err.h"
#include "ra8_log.h"
#include "ra8_mstp.h"
Include dependency graph for ra8_dac_b.c:

Go to the source code of this file.

Data Structures

struct  ra8_dac_b_state_t
 Driver-wide runtime state. More...

Enumerations

enum  ra8_dac_b_channel_t : uint8_t {
  k_ra8_dac_b_channel_0 = 0U ,
  k_ra8_dac_b_channel_1 = 1U
}
 Named channel indices. More...

Functions

static uint16_t internal_ra8_dac_b_clamp (uint16_t value)
 Clamp a raw value to 12-bit range.
static void internal_disable_channel (uint8_t channel)
 Stop a channel and put DACR0 in the FSP "open" baseline.
static void internal_start_channel (uint8_t channel)
 Set DACEN=1 on a channel (FSP R_DAC_B_Start equivalent).
static void internal_stop_channel (uint8_t channel)
 Clear DACEN on a channel (FSP R_DAC_B_Stop equivalent).
ra8_err_t ra8_dac_b_init (void)
 Legacy init – zero every DAC_B register + enable MSTP.
ra8_err_t ra8_dac_b_write (uint8_t channel, uint16_t value)
 Programme a DAC channel with a 12-bit code.
static void internal_apply_cfg (uint8_t channel, const ra8_dac_b_cfg_t *cfg)
 Apply cfg to one DAC_B instance (FSP R_DAC_B_Open body).
ra8_err_t ra8_dac_b_init_configured (const ra8_dac_b_cfg_t *cfg)
 Initialise DAC_B with a full config descriptor.
ra8_err_t ra8_dac_b_deinit (void)
 Tear down the DAC_B peripheral.
ra8_err_t ra8_dac_b_set_vref (ra8_dac_b_vref_t vref)
 Change the DAC voltage reference at runtime.
ra8_err_t ra8_dac_b_set_output_enable (uint8_t channel, bool enable)
 Toggle output-enable for one channel at runtime.
ra8_err_t ra8_dac_b_get_status (uint8_t *out_mask)
 Read the DACR output-enable / DAE mask.
ra8_err_t ra8_dac_b_clear_status (void)
 Clear DAE + DAOE bits (disable DAC outputs).
ra8_err_t ra8_dac_b_attach_handler (ra8_dac_b_update_fn_t fn, void *ctx)
 Attach a DAC-update callback.
ra8_err_t ra8_dac_b_enter_stop (void)
 Put DAC_B into MSTP-gated stop.
ra8_err_t ra8_dac_b_exit_stop (void)
 Exit MSTP-gated stop.
void ra8_dac_b_dispatch_update (uint8_t channel)
 Dispatch a DAC-update event – fire the registered callback.

Variables

static const char * s_tag = "DAC_B"
static ra8_dac_b_state_t s_dac_b_state

Detailed Description

12-Bit D/A Converter (DAC_B) driver implementation

Tag
[Ring 3 / HAL] {World: NS}

Driver for the RA8D2 DAC_B peripheral. DAC_B is the RA8 successor to the pre-RA8 DAC12 block: each DAC "channel" is a separate IP instance with its own DADR + DACR0/1/2 set. DAC_B0 and DAC_B1 live at 0x40233000 and 0x40233100 (stride 0x100). The driver hides this by keeping a 2-channel public API, with channel 0 routed to DAC_B0 and channel 1 routed to DAC_B1.

Cross-verified against FSP r_dac_b.c (R_DAC_B_Open / Write / Start / Stop / Close paths). The bit layout used here mirrors FSP R_DAC_B0_DACR{0,1,2}_*_Msk exactly. See HUM Ch 54 "12-Bit D/A Converter (DAC12)" p 3490..3496.

Definition in file ra8_dac_b.c.

Enumeration Type Documentation

◆ ra8_dac_b_channel_t

enum ra8_dac_b_channel_t : uint8_t

Named channel indices.

Enumerator
k_ra8_dac_b_channel_0 

RA8 DAC b channel 0.

k_ra8_dac_b_channel_1 

RA8 DAC b channel 1.

Definition at line 42 of file ra8_dac_b.c.

Function Documentation

◆ internal_apply_cfg()

void internal_apply_cfg ( uint8_t channel,
const ra8_dac_b_cfg_t * cfg )
static

Apply cfg to one DAC_B instance (FSP R_DAC_B_Open body).

See the matching header declaration for the full contract; this site adds no behaviour beyond what the public API documents.

Parameters
[in]channelSee header declaration for direction and constraints.
[in]cfgSee header declaration for direction and constraints.
Precondition
Driver state has been initialized by the matching *_init.
Caller has validated all pointer parameters.
Postcondition
Side effects are limited to those documented in the header.
No global state is modified on the error path.
Note
Thread safety: see the header declaration.
Since
0.1.0

Definition at line 213 of file ra8_dac_b.c.

References r_dac_b_regs_t::DACR0, r_dac_b_regs_t::DACR1, r_dac_b_regs_t::DACR2, r_dac_b_regs_t::DADR, ra8_dac_b_cfg_t::data_format, ra8_dac_b_cfg_t::internal_output_enabled, k_ra8_dacr0_mask_daoutdis, k_ra8_dacr1_bit_dpsel, k_ra8_dacr2_bit_ofssel, ra8_dac_b(), RA8_INTERNAL, and ra8_dac_b_cfg_t::vref.

Referenced by ra8_dac_b_init_configured().

◆ internal_disable_channel()

void internal_disable_channel ( uint8_t channel)
static

Stop a channel and put DACR0 in the FSP "open" baseline.

Mirrors p_ctrl->p_reg->DACR0 &= ~(DACEN | DAOUTDIS | DAE) from FSP R_DAC_B_Open. Because we always come in here from a fresh power-on (MSTP just released), an unconditional zero is equivalent and avoids a read-modify-write hazard on a just-clocked register block.

Citation: HUM Ch 54 "12-Bit D/A Converter (DAC12)" p 3490.

Parameters
[in]channelSee header declaration for direction and constraints.
Precondition
Driver state has been initialized by the matching *_init.
Caller has validated all pointer parameters.
Postcondition
Side effects are limited to those documented in the header.
No global state is modified on the error path.
Note
Thread safety: see the header declaration.
Since
0.1.0

Definition at line 92 of file ra8_dac_b.c.

References r_dac_b_regs_t::DACR0, r_dac_b_regs_t::DADR, ra8_dac_b(), and RA8_INTERNAL.

Referenced by ra8_dac_b_enter_stop(), and ra8_dac_b_init().

◆ internal_ra8_dac_b_clamp()

uint16_t internal_ra8_dac_b_clamp ( uint16_t value)
inlinestatic

Clamp a raw value to 12-bit range.

See the matching header declaration for the full contract; this site adds no behaviour beyond what the public API documents.

Parameters
[in]valueSee header declaration for direction and constraints.
Returns
ra8_err_t error code (or void if the signature returns void).
Return values
k_ra8_okSuccess path.
k_ra8_err_invalid_argCaller violated a precondition.
Precondition
Driver state has been initialized by the matching *_init.
Caller has validated all pointer parameters.
Postcondition
Side effects are limited to those documented in the header.
No global state is modified on the error path.
Note
Thread safety: see the header declaration.
Since
0.1.0

Definition at line 64 of file ra8_dac_b.c.

References k_ra8_dac_b_max_value, and RA8_INTERNAL.

Referenced by ra8_dac_b_write().

◆ internal_start_channel()

void internal_start_channel ( uint8_t channel)
static

Set DACEN=1 on a channel (FSP R_DAC_B_Start equivalent).

Pure read-modify-write of bit 0 of DACR0; preserves DAE and DAOUTDIS so callers can configure those in DACR0 before starting conversion.

Parameters
[in]channelSee header declaration for direction and constraints.
Precondition
Driver state has been initialized by the matching *_init.
Caller has validated all pointer parameters.
Postcondition
Side effects are limited to those documented in the header.
No global state is modified on the error path.
Note
Thread safety: see the header declaration.
Since
0.1.0

Definition at line 119 of file ra8_dac_b.c.

References r_dac_b_regs_t::DACR0, k_ra8_dacr0_mask_dacen, ra8_dac_b(), and RA8_INTERNAL.

Referenced by ra8_dac_b_init_configured().

◆ internal_stop_channel()

void internal_stop_channel ( uint8_t channel)
static

Clear DACEN on a channel (FSP R_DAC_B_Stop equivalent).

See the matching header declaration for the full contract; this site adds no behaviour beyond what the public API documents.

Parameters
[in]channelSee header declaration for direction and constraints.
Precondition
Driver state has been initialized by the matching *_init.
Caller has validated all pointer parameters.
Postcondition
Side effects are limited to those documented in the header.
No global state is modified on the error path.
Note
Thread safety: see the header declaration.
Since
0.1.0

Definition at line 143 of file ra8_dac_b.c.

References r_dac_b_regs_t::DACR0, k_ra8_dacr0_mask_dacen, ra8_dac_b(), and RA8_INTERNAL.

Referenced by ra8_dac_b_clear_status().

◆ ra8_dac_b_attach_handler()

ra8_err_t ra8_dac_b_attach_handler ( ra8_dac_b_update_fn_t fn,
void * ctx )
nodiscard

Attach a DAC-update callback.

Parameters
[in]fnCallback fired on ISR dispatch.
[in]ctxContext forwarded to the callback.
Returns
ra8_err_t error code.
Since
0.1.0

Definition at line 337 of file ra8_dac_b.c.

References k_ra8_ok, and s_dac_b_state.

◆ ra8_dac_b_clear_status()

ra8_err_t ra8_dac_b_clear_status ( void )
nodiscard

Clear DAE + DAOE bits (disable DAC outputs).

Returns
ra8_err_t error code.
Since
0.1.0

Definition at line 330 of file ra8_dac_b.c.

References internal_stop_channel(), k_ra8_dac_b_channel_0, k_ra8_dac_b_channel_1, and k_ra8_ok.

◆ ra8_dac_b_deinit()

ra8_err_t ra8_dac_b_deinit ( void )
nodiscard

Tear down the DAC_B peripheral.

Returns
ra8_err_t error code.
Postcondition
DACR == 0, MSTP released.
Since
0.1.0

Definition at line 260 of file ra8_dac_b.c.

References r_dac_b_regs_t::DACR0, r_dac_b_regs_t::DADR, k_ra8_dac_b_channel_0, k_ra8_dac_b_channel_1, k_ra8_dacr0_mask_daoutdis, k_ra8_mstp_dac12_0, k_ra8_mstp_dac12_1, ra8_dac_b(), ra8_mstp_disable(), and s_dac_b_state.

◆ ra8_dac_b_dispatch_update()

void ra8_dac_b_dispatch_update ( uint8_t channel)

Dispatch a DAC-update event – fire the registered callback.

Parameters
[in]channelChannel that completed an update.
Since
0.1.0

See the matching header declaration for the full contract; this site adds no behaviour beyond what the public API documents.

Precondition
Driver state has been initialized by the matching *_init.
Caller has validated all pointer parameters.
Postcondition
Side effects are limited to those documented in the header.
No global state is modified on the error path.
Note
Thread safety: see the header declaration.

Definition at line 362 of file ra8_dac_b.c.

References k_ra8_dac_b_channel_count, and s_dac_b_state.

◆ ra8_dac_b_enter_stop()

ra8_err_t ra8_dac_b_enter_stop ( void )
nodiscard

Put DAC_B into MSTP-gated stop.

Since
0.1.0

Definition at line 344 of file ra8_dac_b.c.

References internal_disable_channel(), k_ra8_dac_b_channel_0, k_ra8_dac_b_channel_1, k_ra8_mstp_dac12_0, k_ra8_mstp_dac12_1, and ra8_mstp_disable().

◆ ra8_dac_b_exit_stop()

ra8_err_t ra8_dac_b_exit_stop ( void )
nodiscard

Exit MSTP-gated stop.

Since
0.1.0

Definition at line 352 of file ra8_dac_b.c.

References k_ra8_mstp_dac12_0, k_ra8_mstp_dac12_1, ra8_mstp_enable(), RA8_RETURN_ON_ERROR, and s_tag.

◆ ra8_dac_b_get_status()

ra8_err_t ra8_dac_b_get_status ( uint8_t * out_mask)
nodiscard

Read the DACR output-enable / DAE mask.

Parameters
[out]out_maskDACR value masked to the control bits.
Returns
ra8_err_t error code.
Since
0.1.0

Definition at line 310 of file ra8_dac_b.c.

References r_dac_b_regs_t::DACR0, k_ra8_dac_b_channel_0, k_ra8_dac_b_channel_1, k_ra8_dacr0_mask_dacen, k_ra8_ok, RA8_CHECK_NULL_PTR, ra8_dac_b(), and s_tag.

◆ ra8_dac_b_init()

ra8_err_t ra8_dac_b_init ( void )
nodiscard

Legacy init – zero every DAC_B register + enable MSTP.

Returns
k_ra8_ok on success.
Since
0.1.0

Definition at line 153 of file ra8_dac_b.c.

References internal_disable_channel(), k_ra8_dac_b_channel_0, k_ra8_dac_b_channel_1, k_ra8_mstp_dac12_0, k_ra8_mstp_dac12_1, k_ra8_ok, ra8_log_info, ra8_mstp_enable(), RA8_RETURN_ON_ERROR, and s_tag.

Referenced by ra8_nsc_dac_b_init().

◆ ra8_dac_b_init_configured()

ra8_err_t ra8_dac_b_init_configured ( const ra8_dac_b_cfg_t * cfg)
nodiscard

Initialise DAC_B with a full config descriptor.

Parameters
[in]cfgNon-NULL configuration descriptor.
Returns
ra8_err_t error code.
Precondition
IRQs masked or single-threaded init context.
ra8_mstp_init has been called.
Postcondition
On success DACR matches cfg and the DAC_B module is powered on via MSTP.
Note
Thread safety: not thread-safe.
Since
0.1.0

Definition at line 234 of file ra8_dac_b.c.

References ra8_dac_b_cfg_t::enable_channel0, ra8_dac_b_cfg_t::enable_channel1, internal_apply_cfg(), internal_start_channel(), k_ra8_dac_b_channel_0, k_ra8_dac_b_channel_1, k_ra8_mstp_dac12_0, k_ra8_mstp_dac12_1, k_ra8_ok, RA8_CHECK_NULL_PTR, ra8_log_info, ra8_mstp_enable(), RA8_RETURN_ON_ERROR, and s_tag.

Referenced by internal_dac_b_demo_arm(), and internal_dac_demo_setup_or_halt().

◆ ra8_dac_b_set_output_enable()

ra8_err_t ra8_dac_b_set_output_enable ( uint8_t channel,
bool enable )
nodiscard

Toggle output-enable for one channel at runtime.

Parameters
[in]channel0 or 1.
[in]enableTrue -> drive the pin; false -> Hi-Z.
Returns
ra8_err_t error code.
Since
0.1.0

Definition at line 293 of file ra8_dac_b.c.

References r_dac_b_regs_t::DACR0, k_ra8_dacr0_mask_dacen, k_ra8_err_invalid_arg, k_ra8_ok, and ra8_dac_b().

Referenced by main().

◆ ra8_dac_b_set_vref()

ra8_err_t ra8_dac_b_set_vref ( ra8_dac_b_vref_t vref)
nodiscard

Change the DAC voltage reference at runtime.

Parameters
[in]vrefNew reference source.
Returns
ra8_err_t error code.
Since
0.1.0

Definition at line 281 of file ra8_dac_b.c.

References r_dac_b_regs_t::DACR2, k_ra8_dac_b_channel_0, k_ra8_dac_b_channel_1, k_ra8_dacr2_bit_ofssel, k_ra8_dacr2_mask_ofssel, k_ra8_ok, and ra8_dac_b().

◆ ra8_dac_b_write()

ra8_err_t ra8_dac_b_write ( uint8_t channel,
uint16_t value )
nodiscard

Programme a DAC channel with a 12-bit code.

Parameters
[in]channel0 or 1.
[in]valueRaw 12-bit code (clamped to 0..4095).
Returns
ra8_err_t error code.
Since
0.1.0

Definition at line 172 of file ra8_dac_b.c.

References r_dac_b_regs_t::DADR, internal_ra8_dac_b_clamp(), k_ra8_err_invalid_arg, k_ra8_ok, ra8_dac_b(), ra8_log_info_val, and s_tag.

Referenced by internal_dac_demo_one_triangle_period(), main(), and ra8_nsc_dac_b_write().

Variable Documentation

◆ s_dac_b_state

ra8_dac_b_state_t s_dac_b_state
static

◆ s_tag

const char* s_tag = "DAC_B"
static

Definition at line 36 of file ra8_dac_b.c.