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

Cyclic Redundancy Check driver implementation. More...

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

Go to the source code of this file.

Enumerations

enum  crc_shift_t : uint8_t { k_crc_shift_byte3 = 24U }
 Byte-3 shift for little-endian word assembly. More...
enum  ra8_crccr0_bit_t : uint8_t {
  k_ra8_crccr0_lms_shift = 6U ,
  k_ra8_crccr0_dorclr_shift = 7U ,
  k_ra8_crccr0_dorclr = 1U << 7U ,
  k_ra8_crccr0_gps_mask = 0x07U
}
 Bit positions inside CRCCR0 (HUM Ch 48.2.1 p 3181). More...
enum  ra8_crc_seed_t : uint32_t { k_ra8_crc_32bit_seed = 0xFFFFFFFFUL }
 Standard init / xor-out value for IEEE-style 32-bit CRCs. More...

Functions

static bool internal_ra8_crc_is_32bit_poly (ra8_crc_poly_t poly)
 Determine if a polynomial uses 32-bit data input.
static void internal_crc_feed_words (const uint8_t *data, uint32_t len)
 Feed len bytes through the calculator as packed 32-bit words.
static void internal_crc_feed_bytes (const uint8_t *data, uint32_t len)
 Feed len bytes through the calculator via the 8-bit alias.
ra8_err_t ra8_crc_init (ra8_crc_poly_t poly)
 Configure the CRC block for a given polynomial.
void ra8_crc_reset (void)
 Clear the running CRC state.
ra8_err_t ra8_crc_compute (const uint8_t *data, uint32_t len, uint32_t *out_crc)
 Compute a CRC over a byte buffer.
ra8_err_t ra8_crc_deinit (void)
 Tear down the CRC block (disable + MSTP release).
ra8_err_t ra8_crc_set_poly (ra8_crc_poly_t poly)
 Change the CRC polynomial at runtime without a reset.
ra8_err_t ra8_crc_get_status (uint8_t *out_poly)
 Read the current polynomial selection (CRCCR0.GPS).
ra8_err_t ra8_crc_enter_stop (void)
 Put CRC into MSTP-gated stop.
ra8_err_t ra8_crc_exit_stop (void)
 Exit MSTP-gated stop.

Variables

static const char * s_tag = "CRC"

Detailed Description

Cyclic Redundancy Check driver implementation.

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

Driver for the RA8D2 CRC block. The CRC unit accepts one of five hard-wired polynomials (CRC-8, CRC-16, CRC-CCITT, CRC-32 IEEE 802.3, CRC-32C Castagnoli) selected by CRCCR0.GPS[2:0], accumulates the running result in CRCDOR as data is written to CRCDIR/CRCDIR_BY, and can be reset between operations via the write-only CRCCR0.DORCLR bit.

Cross-verified against FSP r_crc.c (R_CRC_Open, R_CRC_Calculate, R_CRC_Close) at _reference/fsp/ra/fsp/src/ r_crc/r_crc.c. Like FSP, the driver pulses DORCLR during init to clear stale CRCDOR contents (crccr0 |= 1 << R_CRC_CRCCR0_DORCLR_Pos in R_CRC_Open) and uses byte-access CRCDIR_BY for 8/16/CCITT polynomials and 32-bit CRCDIR for 32-bit polynomials. Every register access carries a HUM Ch 48 citation (p 3180-3189).

Definition in file ra8_crc.c.

Enumeration Type Documentation

◆ crc_shift_t

enum crc_shift_t : uint8_t

Byte-3 shift for little-endian word assembly.

Enumerator
k_crc_shift_byte3 

CRC shift byte3.

Definition at line 42 of file ra8_crc.c.

◆ ra8_crc_seed_t

enum ra8_crc_seed_t : uint32_t

Standard init / xor-out value for IEEE-style 32-bit CRCs.

HUM Ch 48 documents the calculator core but not the init / xor-out convention – those are part of the CRC variant spec rather than the hardware. IEEE 802.3 / CRC-32 and Castagnoli / CRC-32C both use init = 0xFFFFFFFF and xor_out = 0xFFFFFFFF (per ITU-T V.42 / RFC 3385 respectively). The on-chip calculator starts CRCDOR at 0 by default, so the driver must pre-load CRCDOR with the init value before clocking data through, and XOR the read- back with the same constant on the way out.

Enumerator
k_ra8_crc_32bit_seed 

IEEE-802.3 / Castagnoli init = xor-out.

Definition at line 93 of file ra8_crc.c.

◆ ra8_crccr0_bit_t

enum ra8_crccr0_bit_t : uint8_t

Bit positions inside CRCCR0 (HUM Ch 48.2.1 p 3181).

Enumerator
k_ra8_crccr0_lms_shift 

CRCCR0.LMS bit position.

k_ra8_crccr0_dorclr_shift 

CRCCR0.DORCLR bit position.

k_ra8_crccr0_dorclr 

CRCCR0.DORCLR mask (write-only).

k_ra8_crccr0_gps_mask 

CRCCR0.GPS[2:0] mask.

Definition at line 50 of file ra8_crc.c.

Function Documentation

◆ internal_crc_feed_bytes()

void internal_crc_feed_bytes ( const uint8_t * data,
uint32_t len )
inlinestatic

Feed len bytes through the calculator via the 8-bit alias.

HUM Ch 48.2.3 p 3182 – CRC-8 / CRC-16 / CRC-CCITT consume one byte per write through CRCDIR_BY at offset +0x04.

Parameters
[in]dataPointer to len bytes (non-NULL).
[in]lenByte count.
Precondition
Driver state has been initialized by ra8_crc_init.
CRCDOR pre-seeded with the desired init value (typically 0).
Postcondition
CRCDOR holds the engine result over all bytes.
No state mutated besides CRCDIR_BY / CRCDOR.
Note
Not thread-safe; caller must serialize.
Since
0.1.0

Definition at line 149 of file ra8_crc.c.

References r_crc_regs_t::CRCDIR_BY, ra8_crc(), and RA8_INTERNAL.

Referenced by ra8_crc_compute().

◆ internal_crc_feed_words()

void internal_crc_feed_words ( const uint8_t * data,
uint32_t len )
inlinestatic

Feed len bytes through the calculator as packed 32-bit words.

HUM Ch 48.2.3 p 3182 – for CRC-32 / CRC-32C the engine consumes 32-bit words. FSP's R_CRC_Calculate and the project's mirror loop pack four bytes little-endian into each CRCDIR write. Trailing bytes that don't form a full word are ignored (FSP behaviour and HUM Note 1 p 3180 "This function cannot divide data used in CRC calculations").

Parameters
[in]dataPointer to len bytes (non-NULL).
[in]lenByte count; the low two bits are ignored.
Precondition
Driver state has been initialized by ra8_crc_init.
CRCDOR pre-seeded by the caller with the desired init value.
Postcondition
CRCDOR holds the engine result over the consumed words.
No state mutated besides CRCDIR / CRCDOR.
Note
Not thread-safe; caller must serialize.
Since
0.1.0

Definition at line 118 of file ra8_crc.c.

References r_crc_regs_t::CRCDIR, k_crc_shift_byte3, ra8_crc(), and RA8_INTERNAL.

Referenced by ra8_crc_compute().

◆ internal_ra8_crc_is_32bit_poly()

bool internal_ra8_crc_is_32bit_poly ( ra8_crc_poly_t poly)
inlinestatic

Determine if a polynomial uses 32-bit data input.

Parameters
[in]polyPolynomial selection.
Returns
true if poly is CRC-32 or CRC-32C (32-bit CRCDIR width).

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 74 of file ra8_crc.c.

References k_ra8_crc_poly_32_ieee802_3, k_ra8_crc_poly_32c_rev, and RA8_INTERNAL.

Referenced by ra8_crc_compute().

◆ ra8_crc_compute()

ra8_err_t ra8_crc_compute ( const uint8_t * data,
uint32_t len,
uint32_t * out_crc )
nodiscard

Compute a CRC over a byte buffer.

Feeds each byte of data into CRCDIR and reads the running result from CRCDOR at the end. The CRC unit is not reset between calls – the caller must write CRCCR1 to clear state via ra8_crc_reset() first.

Parameters
[in]dataPointer to bytes (not NULL).
[in]lenByte count.
[out]out_crcReceives the final result.
Returns
ra8_err_t error code.
Since
0.1.0

Definition at line 190 of file ra8_crc.c.

References r_crc_regs_t::CRCCR0, r_crc_regs_t::CRCDOR, internal_crc_feed_bytes(), internal_crc_feed_words(), internal_ra8_crc_is_32bit_poly(), k_ra8_crc_32bit_seed, k_ra8_crccr0_gps_mask, k_ra8_ok, RA8_CHECK_NULL_PTR, ra8_crc(), and s_tag.

Referenced by crc_demo_one_iter(), and ra8_nsc_crc_compute().

◆ ra8_crc_deinit()

ra8_err_t ra8_crc_deinit ( void )
nodiscard

Tear down the CRC block (disable + MSTP release).

Since
0.1.0

Definition at line 236 of file ra8_crc.c.

References r_crc_regs_t::CRCCR0, r_crc_regs_t::CRCCR1, k_ra8_mstp_crc, ra8_crc(), and ra8_mstp_disable().

◆ ra8_crc_enter_stop()

ra8_err_t ra8_crc_enter_stop ( void )
nodiscard

Put CRC into MSTP-gated stop.

Since
0.1.0

Definition at line 264 of file ra8_crc.c.

References r_crc_regs_t::CRCCR0, k_ra8_mstp_crc, ra8_crc(), and ra8_mstp_disable().

◆ ra8_crc_exit_stop()

ra8_err_t ra8_crc_exit_stop ( void )
nodiscard

Exit MSTP-gated stop.

Since
0.1.0

Definition at line 271 of file ra8_crc.c.

References k_ra8_mstp_crc, and ra8_mstp_enable().

◆ ra8_crc_get_status()

ra8_err_t ra8_crc_get_status ( uint8_t * out_poly)
nodiscard

Read the current polynomial selection (CRCCR0.GPS).

Since
0.1.0

Definition at line 256 of file ra8_crc.c.

References r_crc_regs_t::CRCCR0, k_ra8_ok, RA8_CHECK_NULL_PTR, ra8_crc(), and s_tag.

◆ ra8_crc_init()

ra8_err_t ra8_crc_init ( ra8_crc_poly_t poly)
nodiscard

Configure the CRC block for a given polynomial.

Parameters
[in]polyOne of the k_ra8_crc_poly_* values.
Returns
k_ra8_ok on success.
Since
0.1.0

Definition at line 158 of file ra8_crc.c.

References r_crc_regs_t::CRCCR0, r_crc_regs_t::CRCCR1, k_ra8_crccr0_dorclr, k_ra8_mstp_crc, k_ra8_ok, ra8_crc(), ra8_log_info_val, ra8_mstp_enable(), RA8_RETURN_ON_ERROR, and s_tag.

Referenced by crc_demo_setup_or_halt(), and ra8_nsc_crc_init().

◆ ra8_crc_reset()

void ra8_crc_reset ( void )

Clear the running CRC state.

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.
Since
0.1.0

Definition at line 180 of file ra8_crc.c.

References r_crc_regs_t::CRCCR0, k_ra8_crccr0_dorclr, and ra8_crc().

Referenced by crc_demo_one_iter().

◆ ra8_crc_set_poly()

ra8_err_t ra8_crc_set_poly ( ra8_crc_poly_t poly)
nodiscard

Change the CRC polynomial at runtime without a reset.

Since
0.1.0

Definition at line 246 of file ra8_crc.c.

References r_crc_regs_t::CRCCR0, k_ra8_crccr0_dorclr, k_ra8_ok, and ra8_crc().

Variable Documentation

◆ s_tag

const char* s_tag = "CRC"
static

Definition at line 39 of file ra8_crc.c.