Atmel Software Framework

crc.c File Reference

Cyclic Redundancy Check module driver.

Copyright (c) 2011-2015 Atmel Corporation. All rights reserved.

#include <stdbool.h>
#include <assert.h>
#include "crc.h"

Functions

void crc16_append_value (uint16_t value, void *ptr)
 Appends a CRC value to the given pointer. More...
 
static uint16_t crc16_checksum_read (void)
 Read CRC-16 checksum. More...
 
void crc32_append_value (uint32_t value, void *ptr)
 Get and append data to generate checksum zero with CRC32. More...
 
static uint32_t crc32_checksum_read (void)
 Read CRC-32 checksum. More...
 
static void crc_32_enable (void)
 Enable CRC-32. More...
 
static uint32_t crc_bit_revert (uint32_t value)
 Bit reverse the given value. More...
 
static uint32_t crc_checksum_complete (void)
 Complete CRC calculation by reading checksum and disabling module. More...
 
static uint32_t crc_checksum_read (void)
 Read checksum. More...
 
static void crc_disable (void)
 Disable the CRC module. More...
 
void crc_dma_checksum_start (uint8_t dma_channel, enum crc_16_32_t crc_16_32)
 Start CRC calculations on data passing through a DMA channel. More...
 
uint32_t crc_dma_checksum_stop (void)
 Disable CRC module and return checksum after DMA transfer. More...
 
uint32_t crc_flash_checksum (NVM_CMD_t crc_type, flash_addr_t flash_addr, uint32_t length)
 Perform a CRC-32 calculation on the entire flash memory, on only the application section, on only the boot section, or on a selectable range of the flash memory. More...
 
uint32_t crc_io_checksum (void *data, uint16_t len, enum crc_16_32_t crc_16_32)
 Calculate the CRC checksum for data in a buffer. More...
 
void crc_io_checksum_byte_add (uint8_t data)
 Add one byte of data to CRC performed on I/O interface. More...
 
void crc_io_checksum_byte_start (enum crc_16_32_t crc_16_32)
 Reset CRC module and set source to I/O interface. More...
 
uint32_t crc_io_checksum_byte_stop (void)
 Disable CRC module and return checksum. More...
 
static bool crc_is_busy (void)
 Check if the CRC module is busy. More...
 
static void crc_reset (void)
 Reset CRC module and set CHECKSUM to initial value. More...
 
void crc_set_initial_value (uint32_t value)
 Set initial CRC value for next CRC calculation. More...
 
static void crc_set_source (CRC_SOURCE_t source)
 Set data source for the CRC module after reset. More...
 

Variables

uint32_t crc_initial_value = 0
 Initial CRC value to use on next CRC calculation. More...
 

static uint16_t crc16_checksum_read ( void  )
inlinestatic

Read CRC-16 checksum.

This function returns the computed CRC-16 checksum.

Returns
checksum checksum value

Referenced by crc_checksum_read().

static uint32_t crc32_checksum_read ( void  )
inlinestatic

Read CRC-32 checksum.

This function extracts the CRC-32 checksum from the registers.

Note
To comply with IEEE 802.3 CRC-32, the checksum is bit reversed and complemented.
Returns
checksum checksum value

References crc_bit_revert().

Referenced by crc_checksum_read().

static void crc_32_enable ( void  )
inlinestatic

Enable CRC-32.

This function will set the CRC-32 polynomial to be used to generate the checksum.

Note
This cannot be changed while the busy flag is set.

Referenced by crc_dma_checksum_start(), crc_flash_checksum(), and crc_io_checksum_byte_start().

static uint32_t crc_bit_revert ( uint32_t  value)
inlinestatic

Bit reverse the given value.

Parameters
valuethe value to bit revert
Returns
bit reverted value

References result.

Referenced by crc32_append_value(), and crc32_checksum_read().

static uint32_t crc_checksum_complete ( void  )
inlinestatic

Complete CRC calculation by reading checksum and disabling module.

Returns
checksum checksum value

References crc_checksum_read(), and crc_disable().

Referenced by crc_dma_checksum_stop(), crc_flash_checksum(), and crc_io_checksum_byte_stop().

static uint32_t crc_checksum_read ( void  )
inlinestatic

Read checksum.

This function waits until the CRC conversion is complete and returns either the 32 bit or 16 bit checksum as a 32 bit value.

Note
This function must not be called before a IO CRC calculation is completed or a FLASH or DMA CRC calculation is started, as this will cause it to wait for the BUSY flag to be cleared forever.
Returns
checksum checksum value

References crc16_checksum_read(), crc32_checksum_read(), and crc_is_busy().

Referenced by crc_checksum_complete().

static void crc_disable ( void  )
inlinestatic

Disable the CRC module.

References crc_set_source().

Referenced by crc_checksum_complete().

static bool crc_is_busy ( void  )
inlinestatic

Check if the CRC module is busy.

This function can be used to see if a CRC checksum generation is completed.

Return values
trueif module is busy
falseif module is not busy

Referenced by crc_checksum_read().

static void crc_reset ( void  )
inlinestatic

Reset CRC module and set CHECKSUM to initial value.

The CRC registers will be reset one peripheral clock cycle after the RESET[1] bit is set. The initial value is reset to 0 after loading it into the CHECKSUM registers.

References crc_initial_value.

Referenced by crc_dma_checksum_start(), crc_flash_checksum(), and crc_io_checksum_byte_start().

static void crc_set_source ( CRC_SOURCE_t  source)
inlinestatic

Set data source for the CRC module after reset.

The selected source is locked until either the CRC generation is completed or the CRC module is reset. CRC generation complete is generated and signalled from the selected source when used with the DMA controller or flash memory. To disable the CRC module, use CRC_SOURCE_DISABLE_gc as source.

Parameters
sourcethe data source for the CRC module

References source.

Referenced by crc_disable(), crc_dma_checksum_start(), crc_flash_checksum(), and crc_io_checksum_byte_start().

uint32_t crc_initial_value = 0

Initial CRC value to use on next CRC calculation.