Menu

CRC Log in to Edit

Two different CRC algorithms are available with AVR-Ada, CRC-8 implements the 8-bit checksum needed for Dallas' 1-Wire devices and CRC-16 implements the algorithm used by the AVR C library.

CRC8

function CRC8 (Data : Unsigned_8;
               Seed : Unsigned_8)
              return Unsigned_8;

CRC16

This package is available for use in AVR computations of a CRC-16. This package implements the same CRC16 calculation used by the AVR C library, but is fully implemented here in Ada.

Included in this package are the following:

  • CRC_Type (subtype of Interfaces.Unsigned_16)
  • An initialize procedure for CRC_Type
  • An Update procedure of CRC_Type, given one byte as input
  • A CRC_High and CRC_Low byte pair of accessor routines for CRC_Type
  • A CRC_Make function to compose a CRC_Type value from two received bytes

API Summary

The public portion of the package can be summarized as follows:

with Interfaces;                  use Interfaces;

package CRC16 is

   subtype CRC_Type is Unsigned_16;

   -- Initialize CRC-16
   procedure Init(CRC : out CRC_Type);

   -- Compute CRC-16 from Byte
   procedure Update(CRC : in out CRC_Type; Byte : Unsigned_8);

   -- Return High and Low byte of Computed CRC-16 
   function CRC_High(CRC : CRC_Type) return Unsigned_8;
   function CRC_Low(CRC : CRC_Type) return Unsigned_8;

   -- Make a CRC-16 from Two Received CRC Bytes for Comparison
   function CRC_Make(High, Low : Unsigned_8) return CRC_Type;

end CRC16;

Init

The Init procedure initializes the CRC_Type into its "initial state", in order to begin computing a CRC16 value.

procedure Init(CRC : out CRC_Type);

Update

After initialization of the CRC_Type value, bytes are processed by successively calling Update. The Update procedure then updates the CRC_Type to indicate the CRC16 value computed to this point. In other words, if there were no further bytes included in the CRC16 checksum, then the argument CRC contains the CRC16 value upon return from Update.

procedure Update(CRC : in out CRC_Type; Byte : Unsigned_8);

CRC_High and CRC_Low

To facilitate the safe and correct handling of the high and low byte from a computed CRC16 value (CRC_Type), these accessor functions exist. These would be used for example, to take a computed CRC16 for a packet, and place the two CRC bytes into the tail end of the packet structure.

function CRC_High(CRC : CRC_Type) return Unsigned_8;
function CRC_Low(CRC : CRC_Type) return Unsigned_8;

CRC_Make

The CRC_Make function returns a CRC_Type from a high and low byte found inside a packet carrying a CRC16 value. Once CRC_Make returns a CRC_Type, the programmer has a CRC16 value to compare against the computed one from the use of Init and Update.

function CRC_Make(High, Low : Unsigned_8) return CRC_Type;

Related

Wiki: Examples
Wiki: Home
Wiki: Setup