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

High-Speed Analog Comparator driver implementation. More...

#include "ra8_acmphs.h"
#include <stdint.h>
#include "ra8_acmphs_regs.h"
#include "ra8_attributes.h"
#include "ra8_check.h"
#include "ra8_err.h"
#include "ra8_log.h"
#include "ra8_mstp.h"
#include "ra8_port_constants.h"
Include dependency graph for ra8_acmphs.c:

Go to the source code of this file.

Enumerations

enum  ra8_acmphs_mstp_limit_t : uint8_t { k_ra8_acmphs_mstp_id_count = 4U }
 Number of ACMPHS channels that have dedicated MSTPD bits. More...
enum  ra8_acmphs_bits_t : uint8_t {
  k_ra8_acmphs_ceg_shift = 3U ,
  k_ra8_acmphs_cdfs_shift = 5U ,
  k_ra8_acmphs_cdfs_enabled_value = 1U ,
  k_ra8_acmphs_ctl_mask
}
 CMPCTL field shifts (match FSP R_ACMPHS0_CMPCTL_b). More...

Functions

static ra8_err_t internal_reset_channel (uint8_t ch)
 Reset one ACMPHS channel's control + selector registers.
ra8_err_t ra8_acmphs_init (void)
 Legacy init – reset every channel to its power-on state.
ra8_err_t ra8_acmphs_channel_enable (uint8_t channel)
 Enable a single ACMPHS channel.
ra8_err_t ra8_acmphs_read_output (uint8_t channel, ra8_level_t *out)
 Read the live output of a channel.
ra8_err_t ra8_acmphs_channel_init (uint8_t channel, const ra8_acmphs_cfg_t *cfg)
 Initialise one channel with a full config descriptor.
ra8_err_t ra8_acmphs_channel_deinit (uint8_t channel)
 Tear down one channel (disable + clear CMPCTL).
ra8_err_t ra8_acmphs_set_inputs (uint8_t channel, uint8_t ivpsel, uint8_t ivrefsel)
 Change the input selection of one channel at runtime.
ra8_err_t ra8_acmphs_get_status (uint8_t channel, uint8_t *out_mask)
 Read CMPCTL enable / edge bits.
ra8_err_t ra8_acmphs_clear_status (uint8_t channel)
 Clear CMPCTL (disable + edge mode off) for one channel.
ra8_err_t ra8_acmphs_attach_handler (ra8_acmphs_event_fn_t fn, void *ctx)
 Attach an ACMPHS event callback (shared across all channels).
ra8_err_t ra8_acmphs_enter_stop (uint8_t channel)
 Put one channel into MSTP-gated stop.
ra8_err_t ra8_acmphs_exit_stop (uint8_t channel)
 Exit MSTP-gated stop for one channel.
void ra8_acmphs_dispatch (uint8_t channel)
 Dispatch an ACMPHS edge event – fire the registered callback.

Variables

static const char * s_tag = "ACMPHS"
static const ra8_mstp_t s_acmphs_mstp_table [k_ra8_acmphs_mstp_id_count]
 Channel-index -> MSTP id lookup for the first 4 channels.
static ra8_acmphs_event_fn_t s_acmphs_fn
static void * s_acmphs_ctx

Detailed Description

High-Speed Analog Comparator driver implementation.

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

driver for the RA8D2 ACMPHS block (6 channels total; channels 0..3 have dedicated MSTPD bits, 4..5 share with ACMPHS0). Exposes per-channel init/deinit/enable, output read, input selection, status get/clear, async dispatch, and power transition. Every register access carries a HUM Ch 56 citation.

Definition in file ra8_acmphs.c.

Enumeration Type Documentation

◆ ra8_acmphs_bits_t

enum ra8_acmphs_bits_t : uint8_t

CMPCTL field shifts (match FSP R_ACMPHS0_CMPCTL_b).

Enumerator
k_ra8_acmphs_ceg_shift 

CMPCTL.CEG[1:0] at [4:3].

k_ra8_acmphs_cdfs_shift 

CMPCTL.CDFS[1:0] at [6:5].

k_ra8_acmphs_cdfs_enabled_value 

"Any sampling clock" when filter on.

k_ra8_acmphs_ctl_mask 

RA8 acmphs ctl mask.

Definition at line 154 of file ra8_acmphs.c.

◆ ra8_acmphs_mstp_limit_t

enum ra8_acmphs_mstp_limit_t : uint8_t

Number of ACMPHS channels that have dedicated MSTPD bits.

HUM Ch 11.2.9 p 449 only defines MSTPD25..MSTPD28 covering ACMPHS3..ACMPHS0. Higher-numbered channels on the RA8D2 (k_ra8_acmphs_channel_count = 6) share the same MSTPD28 bit as channel 0; in practice channels 4 and 5 are enabled as a side effect of ACMPHS0 so the driver does not need to request a separate MSTP id for them.

Enumerator
k_ra8_acmphs_mstp_id_count 

RA8 acmphs mstp ID count.

Definition at line 46 of file ra8_acmphs.c.

Function Documentation

◆ internal_reset_channel()

ra8_err_t internal_reset_channel ( uint8_t ch)
static

Reset one ACMPHS channel's control + selector registers.

Parameters
[in]chChannel index already validated against k_ra8_acmphs_channel_count.
Returns
k_ra8_ok or the first error from ra8_mstp / NULL mapping.

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

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 81 of file ra8_acmphs.c.

References r_acmphs_regs_t::CMPCTL, r_acmphs_regs_t::CMPSEL0, r_acmphs_regs_t::CMPSEL1, r_acmphs_regs_t::CPIOC, k_ra8_acmphs_mstp_id_count, k_ra8_err_hw_init_failed, k_ra8_ok, ra8_acmphs(), ra8_mstp_enable(), RA8_RETURN_ON_ERROR, s_acmphs_mstp_table, and s_tag.

Referenced by ra8_acmphs_init().

◆ ra8_acmphs_attach_handler()

ra8_err_t ra8_acmphs_attach_handler ( ra8_acmphs_event_fn_t fn,
void * ctx )
nodiscard

Attach an ACMPHS event callback (shared across all channels).

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 255 of file ra8_acmphs.c.

References k_ra8_ok, s_acmphs_ctx, and s_acmphs_fn.

◆ ra8_acmphs_channel_deinit()

ra8_err_t ra8_acmphs_channel_deinit ( uint8_t channel)
nodiscard

◆ ra8_acmphs_channel_enable()

ra8_err_t ra8_acmphs_channel_enable ( uint8_t channel)
nodiscard

Enable a single ACMPHS channel.

Parameters
[in]channelChannel index (0..5).
Returns
k_ra8_ok on success, k_ra8_err_invalid_arg if channel >= 6.
Since
0.1.0

Definition at line 113 of file ra8_acmphs.c.

References r_acmphs_regs_t::CMPCTL, k_ra8_acmphs_channel_count, k_ra8_acmphs_mask_hcen, k_ra8_err_invalid_arg, k_ra8_ok, ra8_acmphs(), RA8_CHECK_NULL_PTR, ra8_log_info_val, and s_tag.

◆ ra8_acmphs_channel_init()

ra8_err_t ra8_acmphs_channel_init ( uint8_t channel,
const ra8_acmphs_cfg_t * cfg )
nodiscard

Initialise one channel with a full config descriptor.

Parameters
[in]channelChannel 0..5.
[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
CMPCTL / CMPSEL0 / CMPSEL1 / CMPFIR reflect cfg.
Note
Thread safety: not thread-safe.
Since
0.1.0

Definition at line 166 of file ra8_acmphs.c.

References r_acmphs_regs_t::CMPCTL, r_acmphs_regs_t::CMPSEL0, r_acmphs_regs_t::CMPSEL1, ra8_acmphs_cfg_t::edge, ra8_acmphs_cfg_t::filter_en, ra8_acmphs_cfg_t::invert_out, ra8_acmphs_cfg_t::ivpsel, ra8_acmphs_cfg_t::ivrefsel, k_ra8_acmphs_cdfs_enabled_value, k_ra8_acmphs_cdfs_shift, k_ra8_acmphs_ceg_shift, k_ra8_acmphs_channel_count, k_ra8_acmphs_mask_cinv, k_ra8_acmphs_mask_hcen, k_ra8_acmphs_mstp_id_count, k_ra8_err_invalid_arg, k_ra8_ok, ra8_acmphs(), RA8_CHECK_NULL_PTR, ra8_mstp_enable(), RA8_RETURN_ON_ERROR, s_acmphs_mstp_table, and s_tag.

Referenced by internal_acmphs_demo_setup().

◆ ra8_acmphs_clear_status()

ra8_err_t ra8_acmphs_clear_status ( uint8_t channel)
nodiscard

Clear CMPCTL (disable + edge mode off) for one channel.

Since
0.1.0

Definition at line 243 of file ra8_acmphs.c.

References r_acmphs_regs_t::CMPCTL, k_ra8_acmphs_channel_count, k_ra8_err_invalid_arg, k_ra8_ok, ra8_acmphs(), RA8_CHECK_NULL_PTR, and s_tag.

◆ ra8_acmphs_dispatch()

void ra8_acmphs_dispatch ( uint8_t channel)

Dispatch an ACMPHS edge event – fire the registered callback.

Parameters
[in]channelChannel 0..5 that fired the edge.
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 285 of file ra8_acmphs.c.

References k_ra8_acmphs_channel_count, s_acmphs_ctx, and s_acmphs_fn.

◆ ra8_acmphs_enter_stop()

ra8_err_t ra8_acmphs_enter_stop ( uint8_t channel)
nodiscard

Put one channel into MSTP-gated stop.

Since
0.1.0

Definition at line 262 of file ra8_acmphs.c.

References k_ra8_acmphs_channel_count, k_ra8_acmphs_mstp_id_count, k_ra8_err_invalid_arg, k_ra8_ok, ra8_mstp_disable(), and s_acmphs_mstp_table.

◆ ra8_acmphs_exit_stop()

ra8_err_t ra8_acmphs_exit_stop ( uint8_t channel)
nodiscard

Exit MSTP-gated stop for one channel.

Since
0.1.0

Definition at line 273 of file ra8_acmphs.c.

References k_ra8_acmphs_channel_count, k_ra8_acmphs_mstp_id_count, k_ra8_err_invalid_arg, k_ra8_ok, ra8_mstp_enable(), and s_acmphs_mstp_table.

◆ ra8_acmphs_get_status()

ra8_err_t ra8_acmphs_get_status ( uint8_t channel,
uint8_t * out_mask )
nodiscard

Read CMPCTL enable / edge bits.

Parameters
[in]channelChannel 0..5.
[out]out_maskCMPCTL value.
Returns
ra8_err_t error code.
Since
0.1.0

Definition at line 230 of file ra8_acmphs.c.

References r_acmphs_regs_t::CMPCTL, k_ra8_acmphs_channel_count, k_ra8_acmphs_ctl_mask, k_ra8_err_invalid_arg, k_ra8_ok, ra8_acmphs(), RA8_CHECK_NULL_PTR, and s_tag.

◆ ra8_acmphs_init()

ra8_err_t ra8_acmphs_init ( void )
nodiscard

Legacy init – reset every channel to its power-on state.

Returns
k_ra8_ok on success.
Since
0.1.0

Definition at line 101 of file ra8_acmphs.c.

References internal_reset_channel(), k_ra8_acmphs_channel_count, k_ra8_ok, ra8_log_info, RA8_RETURN_ON_ERROR, and s_tag.

Referenced by internal_acmphs_demo_setup(), and ra8_nsc_acmphs_init().

◆ ra8_acmphs_read_output()

ra8_err_t ra8_acmphs_read_output ( uint8_t channel,
ra8_level_t * out )
nodiscard

Read the live output of a channel.

Parameters
[in]channelChannel index (0..5).
[out]outReceives k_ra8_level_high or k_ra8_level_low.
Returns
ra8_err_t error code.
Since
0.1.0

Definition at line 127 of file ra8_acmphs.c.

References r_acmphs_regs_t::CMPMON, k_ra8_acmphs_channel_count, k_ra8_acmphs_mask_hcmon, k_ra8_err_invalid_arg, k_ra8_level_high, k_ra8_level_low, k_ra8_ok, ra8_acmphs(), RA8_CHECK_NULL_PTR, and s_tag.

Referenced by internal_acmphs_demo_one_iter(), and ra8_nsc_acmphs_read_output().

◆ ra8_acmphs_set_inputs()

ra8_err_t ra8_acmphs_set_inputs ( uint8_t channel,
uint8_t ivpsel,
uint8_t ivrefsel )
nodiscard

Change the input selection of one channel at runtime.

Parameters
[in]channelChannel 0..5.
[in]ivpselPlus-input selector.
[in]ivrefselMinus-input selector.
Returns
ra8_err_t error code.
Since
0.1.0

Definition at line 217 of file ra8_acmphs.c.

References r_acmphs_regs_t::CMPSEL0, r_acmphs_regs_t::CMPSEL1, k_ra8_acmphs_channel_count, k_ra8_err_invalid_arg, k_ra8_ok, ra8_acmphs(), RA8_CHECK_NULL_PTR, and s_tag.

Variable Documentation

◆ s_acmphs_ctx

void* s_acmphs_ctx
static

Definition at line 164 of file ra8_acmphs.c.

Referenced by ra8_acmphs_attach_handler(), and ra8_acmphs_dispatch().

◆ s_acmphs_fn

ra8_acmphs_event_fn_t s_acmphs_fn
static

Definition at line 163 of file ra8_acmphs.c.

Referenced by ra8_acmphs_attach_handler(), and ra8_acmphs_dispatch().

◆ s_acmphs_mstp_table

const ra8_mstp_t s_acmphs_mstp_table[k_ra8_acmphs_mstp_id_count]
static
Initial value:
= {
}
@ k_ra8_mstp_acmphs0
MSTPD28 ACMPHS0.
@ k_ra8_mstp_acmphs3
MSTPD25 ACMPHS3.
@ k_ra8_mstp_acmphs2
MSTPD26 ACMPHS2.
@ k_ra8_mstp_acmphs1
MSTPD27 ACMPHS1.

Channel-index -> MSTP id lookup for the first 4 channels.

Definition at line 54 of file ra8_acmphs.c.

Referenced by internal_reset_channel(), ra8_acmphs_channel_deinit(), ra8_acmphs_channel_init(), ra8_acmphs_enter_stop(), and ra8_acmphs_exit_stop().

◆ s_tag

const char* s_tag = "ACMPHS"
static

Definition at line 32 of file ra8_acmphs.c.