Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

MbedCRC< polynomial, width, mode_limit > Class Template Reference

MbedCRC< polynomial, width, mode_limit > Class Template Reference
[MbedCRC class]

CRC object provides CRC generation through hardware or software. More...

#include <MbedCRC.h>

Public Member Functions

 MbedCRC (uint32_t initial_xor, uint32_t final_xor, bool reflect_data, bool reflect_remainder)
 Lifetime of CRC object.
int32_t compute (const void *buffer, crc_data_size_t size, uint32_t *crc)
 Compute CRC for the data input Compute CRC performs the initialization, computation and collection of final CRC.
int32_t compute_partial (const void *buffer, crc_data_size_t size, uint32_t *crc)
 Compute partial CRC for the data input.
int32_t compute_partial_start (uint32_t *crc)
 Compute partial start, indicate start of partial computation.
int32_t compute_partial_stop (uint32_t *crc)
 Get the final CRC value of partial computation.

Static Public Member Functions

static constexpr uint32_t get_polynomial ()
 Get the current CRC polynomial.
static constexpr uint8_t get_width ()
 Get the current CRC width.

Detailed Description

template<uint32_t polynomial = POLY_32BIT_ANSI, uint8_t width = 32, CrcMode mode_limit = CrcMode::HARDWARE>
class mbed::MbedCRC< polynomial, width, mode_limit >

CRC object provides CRC generation through hardware or software.

CRC sums can be generated using three different methods: hardware, software ROM tables and bitwise computation. The mode used is normally selected automatically based on required polynomial and hardware capabilities. Any polynomial in standard form (`x^3 + x + 1`) can be used for computation, but custom ones can affect the performance.

First choice is the hardware mode. The supported polynomials are hardware specific, and you need to consult your MCU manual to discover them. Next, ROM polynomial tables are tried (you can find list of supported polynomials here crc_polynomial). If the selected configuration is supported, it will accelerate the software computations. If ROM tables are not available for the selected polynomial, then CRC is computed at run time bit by bit for all data input.

If desired, the mode can be manually limited for a given instance by specifying the mode_limit template parameter. This might be appropriate to ensure a table is not pulled in for a non-speed-critical CRC, or to avoid the hardware set-up overhead if you know you will be calling `compute` with very small data sizes.

Note:
Synchronization level: Thread safe
Template Parameters:
polynomialCRC polynomial value in hex
widthCRC polynomial width
mode_limitMaximum amount of acceleration to use

Example: Compute CRC data

  #include "mbed.h"

  int main() {
      MbedCRC<POLY_32BIT_ANSI, 32> ct;

      char  test[] = "123456789";
      uint32_t crc = 0;

      printf("\nPolynomial = 0x%lx  Width = %d \n", ct.get_polynomial(), ct.get_width());

      ct.compute((void *)test, strlen((const char*)test), &crc);

      printf("The CRC of data \"123456789\" is : 0x%lx\n", crc);
      return 0;
  }

Example: Compute CRC with data available in parts

  #include "mbed.h"
  int main() {
      MbedCRC<POLY_32BIT_ANSI, 32> ct;

      char  test[] = "123456789";
      uint32_t crc = 0;

      printf("\nPolynomial = 0x%lx  Width = %d \n", ct.get_polynomial(), ct.get_width());
      ct.compute_partial_start(&crc);
      ct.compute_partial((void *)&test, 4, &crc);
      ct.compute_partial((void *)&test[4], 5, &crc);
      ct.compute_partial_stop(&crc);
      printf("The CRC of data \"123456789\" is : 0x%lx\n", crc);
      return 0;
  }

Definition at line 148 of file MbedCRC.h.


Constructor & Destructor Documentation

MbedCRC ( uint32_t  initial_xor,
uint32_t  final_xor,
bool  reflect_data,
bool  reflect_remainder 
)

Lifetime of CRC object.

Parameters:
initial_xorInitial value/seed to Xor
final_xorFinal Xor value
reflect_data
reflect_remainder
Note:
Default constructor without any arguments is valid only for supported CRC polynomials. :: crc_polynomial_t MbedCRC <POLY_7BIT_SD, 7> ct; --- Valid POLY_7BIT_SD MbedCRC <0x1021, 16> ct; --- Valid POLY_16BIT_CCITT MbedCRC <POLY_16BIT_CCITT, 32> ct; --- Invalid, compilation error MbedCRC <POLY_16BIT_CCITT, 32> ct (i,f,rd,rr) Constructor can be used for not supported polynomials MbedCRC<POLY_16BIT_CCITT, 16> sd(0, 0, false, false); Constructor can also be used for supported polynomials with different initial/final/reflect values

Definition at line 178 of file MbedCRC.h.


Member Function Documentation

int32_t compute ( const void *  buffer,
crc_data_size_t  size,
uint32_t *  crc 
)

Compute CRC for the data input Compute CRC performs the initialization, computation and collection of final CRC.

Parameters:
bufferData bytes
sizeSize of data
crcCRC is the output value
Returns:
0 on success, negative error code on failure

Definition at line 194 of file MbedCRC.h.

int32_t compute_partial ( const void *  buffer,
crc_data_size_t  size,
uint32_t *  crc 
)

Compute partial CRC for the data input.

CRC data if not available fully, CRC can be computed in parts with available data.

In case of hardware, intermediate values and states are saved by hardware. Mutex locking is used to serialize access to hardware CRC.

In case of software CRC, previous CRC output should be passed as argument to the current compute_partial call. Please note the intermediate CRC value is maintained by application and not the driver.

Precondition:
: Call `compute_partial_start` to start the partial CRC calculation.
Postcondition:
: Call `compute_partial_stop` to get the final CRC value.
Parameters:
bufferData bytes
sizeSize of data
crcCRC value is intermediate CRC value filled by API.
Returns:
0 on success or a negative error code on failure
Note:
: CRC as output in compute_partial is not final CRC value, call `compute_partial_stop` to get final correct CRC value.

Definition at line 220 of file MbedCRC.h.

int32_t compute_partial_start ( uint32_t *  crc )

Compute partial start, indicate start of partial computation.

This API should be called before performing any partial computation with compute_partial API.

Parameters:
crcInitial CRC value set by the API
Returns:
0 on success or a negative in case of failure
Note:
: CRC is an out parameter and must be reused with compute_partial and `compute_partial_stop` without any modifications in application.

Definition at line 235 of file MbedCRC.h.

int32_t compute_partial_stop ( uint32_t *  crc )

Get the final CRC value of partial computation.

CRC value available in partial computation is not correct CRC, as some algorithms require remainder to be reflected and final value to be XORed This API is used to perform final computation to get correct CRC value.

Parameters:
crcCRC result
Returns:
0 on success or a negative in case of failure.

Definition at line 249 of file MbedCRC.h.

static constexpr uint32_t get_polynomial (  ) [static]

Get the current CRC polynomial.

Returns:
Polynomial value

Definition at line 258 of file MbedCRC.h.

static constexpr uint8_t get_width (  ) [static]

Get the current CRC width.

Returns:
CRC width

Definition at line 267 of file MbedCRC.h.