Section 27. 32-Bit Programmable Cyclic Redundancy Check (CRC)
Section 27. 32-Bit Programmable Cyclic Redundancy Check (CRC)
HIGHLIGHTS
This section of the manual contains the following major topics: 27
Cyclic Redundancy
27.1 Introduction .................................................................................................................. 27-2
Programmable
27.2 Module Overview ......................................................................................................... 27-3
27.3 CRC Registers ............................................................................................................. 27-3
32-Bit
27.4 CRC Engine ................................................................................................................. 27-9
27.5 Control Logic.............................................................................................................. 27-10
27.6 Advantages of Programmable CRC Module.............................................................. 27-13
27.7 Application of CRC Module........................................................................................ 27-13
27.8 Operation in Power-Saving Modes ............................................................................ 27-16
27.9 Register Maps............................................................................................................ 27-17
27.10 Related Application Notes.......................................................................................... 27-18
27.11 Revision History ......................................................................................................... 27-19
27.1 INTRODUCTION
The 32-bit Programmable Cyclic Redundancy Check (CRC) module in dsPIC33E/PIC24E
devices is a software configurable CRC checksum generator. The checksum is a unique number
associated with a message or a particular block of data containing several bytes. Whether it is a
data packet for communication or a block of data stored in memory, the checksum helps to
validate it before processing. The simplest way to calculate a checksum is to add together all the
data bytes present in the message. However, this method of checksum calculation fails badly
when the message is modified by inverting or swapping groups of bytes. Also, it fails when null
bytes are added anywhere in the message.
The CRC is a more complicated and robust error checking algorithm. The main idea behind the
CRC algorithm is to treat a message as a binary bit stream and divide it by a fixed binary number.
The remainder from this division is treated as the checksum. Like in division, the CRC calculation
is also an iterative process. The only difference is that these operations are done as modulo
arithmetic based on mod2. For example, division is replaced with the XOR operation (i.e.,
subtraction without carry). The CRC algorithm uses polynomials to perform all of its calculations.
The divisor, dividend and remainder are represented by polynomials with binary coefficients. For
example, the number, 19h (11001), is represented by Equation 27-1.
Equation 27-1:
4 3 2 1 0
(1 ⋅ x ) + (1 ⋅ x ) + (0 ⋅ x ) + (0 ⋅ x ) + (1 ⋅ x )
4 3 0
or, in simpler terms: x + x + x
In order to perform the CRC calculation, a suitable divisor is first selected. This divisor is called
the generator polynomial. Since CRC is used to detect errors, a generator polynomial of a
suitable length needs to be chosen for a given application, as each polynomial has different error
detection capabilities. Some polynomials are widely used by many applications; however,
discussions about the error detecting capabilities of any particular polynomial are beyond the
scope of this reference section.
The CRC calculation is an iterative process and consumes considerable CPU bandwidth when
implemented in software. The software configurable CRC hardware module in
dsPIC33E/PIC24E devices facilitates a fast CRC checksum calculation with minimal software
overhead.
The programmable CRC generator provides a hardware implemented method of quickly
generating checksums for various communication and security applications. It provides the
following features:
• CRC16-CCITT compliant with the x16 + x12 + x5 + 1 polynomial
• Error detection for all single, double, odd and most multi-bit errors
• High-speed hardware CRC calculation
• User-programmable CRC polynomial equation, up to 32 bits
• Programmable shift direction (little-endian or big-endian), up to 32 bits
• Independent data and polynomial lengths
• Configurable Interrupt output
• Data FIFO
CRCDATH CRCDATL
27
Cyclic Redundancy
Variable FIFO FIFO Empty Event
Programmable
(4x32, 8x16 or 16x8)
32-Bit
CRCISEL
2 * FCY Shift Clock
1
Shift Buffer Set CRCIF
0
0 1 LENDIAN
Shift Complete
CRC Shift Engine
Event
CRCWDATH CRCWDATL
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
Cyclic Redundancy
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
Programmable
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
32-Bit
bit 15-13 Unimplemented: Read as ‘0’
bit 12-8 DWIDTH<4:0>: Data Word Width Configuration bits
Configures the width of the data word (data word width – 1).
bit 7-5 Unimplemented: Read as ‘0’
bit 4-0 PLEN<4:0>: Polynomial Length Configuration bits
Configures the length of the polynomial (polynomial length – 1).
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
Cyclic Redundancy
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
Programmable
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
32-Bit
bit 15-0 DATA<15:0>: CRC Input Data bits
Writing to this register fills the FIFO; reading from this register returns ‘0’.
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
-n = Value at POR ‘1’ = Bit is set ‘0’ = Bit is cleared x = Bit is unknown
Cyclic Redundancy
Programmable
Figure 27-2: CRC Shift Engine Detail
32-Bit
CRCWDATH CRCWDATL
Read/Write Bus
Shift Buffer
Data Bit 0 Bit 1 Bit 2 Bit n(2)
Note 1: Each XOR stage of the shift engine is programmable. See text for details.
2: Polynomial length n is determined by (<PLEN4:0> + 1).
x16 + x12 + x5 + 1
To include either of the polynomials into the CRC generator, set the register bits as shown in
Table 27-1.
Table 27-1: CRC Setup Examples for 16-Bit and 32-Bit Polynomials
Bit Values
CRC Control Bits
16-Bit Polynomial 32-Bit Polynomial
PLEN<4:0> 01111 11111
X<31:16> 0000 0000 0000 0001 0000 0100 1100 0001
X<15:0> 0001 0000 0010 000x 0001 1101 1011 011x
Note that the appropriate positions are set to ‘1’ to indicate that they are used in the equation
(e.g., X26 and X23). The 0 bit required by the equation is always XORed; thus, X0 is a “don’t
care”. For a polynomial of length 32, it is assumed that the 32nd bit will be used. Therefore, the
X<31:1> bits do not have the 32nd bit.
Note: For data width less than or equal to 8 bits, the user should feed the input data
through byte operations into the CRCDATL register.
and removes the overhead of reversing the bit order in software. Note that this only changes the
direction the data is shifted into the engine. The result of the CRC calculation will still be a normal
CRC result, not a reverse CRC result.
27.5.4 FIFO
The module incorporates a FIFO that works with a variable data width. The FIFO is physically
implemented as an 8-deep, 16-bit wide storage element. This FIFO width is defined by the
DWIDTH<4:0> bits (CRCCON2<12:8>). Input data width can be configured to any value
between 1 and 32 bits using the DWIDTH bits. The logic associated with the FIFO contains a
5-bit counter, called VWORD (VWORD<4:0> or CRCCON1<12:8>). The value in the
VWORD<4:0> bits indicates the number of new data elements in the FIFO.
27
The FIFO is:
• 16 words deep when DWIDTH<4:0> ≤ 7(data words, 8 bits wide or less)
Cyclic Redundancy
• 8 words deep when DWIDTH<4:0> ≤ 15(data words from 9 to 16 bits wide)
Programmable
• 4 words deep when DWIDTH<4:0> ≤ 31 (data words from 17 to 32 bits wide)
The data for which the CRC is to be calculated must first be written into the FIFO by the CPU
32-Bit
using the CRCDATL register. Reading the CRCDATL register always returns ‘0’.
27.5.4.2 FILLING THE FIFO WITH GREATER THAN 8-BIT AND LESS
THAN/EQUAL TO 16-BIT DATA:
With a greater than 8-bit and less than or equal to a 16-bit data word width setting, the FIFO
increments on a write to the CRCDATL register. Any write to the CRCDATH register will be
ignored. The value of the VWORD<4:0> bits increment for every write to the CRCDATL register.
27.5.4.3 FILLING THE FIFO WITH GREATER THAN 16 AND LESS THAN 32-BIT
DATA:
When the data width is greater than 16 bits, any write to the CRCDATH register increments the
VWORD by one. Writes to the lower word into the CRCDATL register must be made before
writing the upper word into the CRCDATH register. For example, if DWIDTH is 24, VWORD will
increment when bit 7 of CRCDATH is written. Therefore, CRCDATL must be written before
CRCDATH.
To accommodate left or right shift methods, byte swapping can take place when filling the FIFO
with different data widths.
Note: Ensure that the new data is not written into the CRCDATL and CRCDATH registers
when the CRCFUL bit is set; if the new data is written, it will be ignored.
decrement by one when a FIFO location gets shifted completely to the CRC calculator. The serial
shifter continues shifting until the VWORD bits reach zero, at which point, the CRCMPT bit is set
to indicate that the FIFO is empty.
Users can write into the FIFO while the shift operation is in progress. For a continuous data feed
into the CRC engine, the recommended mode of operation is to initially “prime” the FIFO with a
sufficient number of words or bytes. Once this is completely done, the user can start the CRC by
setting the CRCGO bit to ‘1’. From this point onwards, either VWORD or the CRCFUL bit should
be monitored. If the CRCFUL bit is not set, or the VWORD reads less than 8 or 16, another word
can be written onto the FIFO. At least one instruction cycle must pass after a write to the
CRCDATL register before a read of the VWORD bits is done.
When the VWORD bits reach the maximum value for the configured value of the DWIDTH bits
(4, 8, 16, or 32), the CRCFUL bit is set. When the VWORD bits reach zero, the CRCMPT bit is
set. The FIFO is emptied and VWORD<4:0> are set to ‘00000’ whenever CRCEN is set to ‘0’.
The frequency of the CRC shift clock is twice that of the dsPIC33E/PIC24E instruction clock
cycle, thus making this hardware shifting process faster than a software shifter. This means that
for a given data width, it takes half that number of instructions for each word to complete the
calculation. For example, it takes 16 cycles to calculate the CRC for a single word of 32-bit data.
Note 1: When the CPU writes the shift registers directly though the CRCWDATL register,
the CRCGO bit must be ‘0’.
2: The CRCIF flag may get set in the middle of a data process sequence if the data is
not provided to the CRC module in time and the CRC FIFO becomes empty.
Cyclic Redundancy
that is 128 bits (16x8) long. If the same calculation is implemented in software, it will consume
Programmable
more than a thousand instruction cycles, even for an optimized piece of code.
32-Bit
27.7 APPLICATION OF CRC MODULE
Calculating a CRC is a robust error checking algorithm in digital communication for messages
containing several bytes or words. After calculation, the checksum is appended to the message
and transmitted to the receiving station. The receiver calculates the checksum with the received
message to verify the data integrity.
27.7.1 Variations
The 32-bit programmable CRC module of dsPIC33E/PIC24E devices can be programmed to
shift out either the MSb or LSb first. MSb first is a popular implementation as employed in the
XMODEM protocol. In one of the variations (CCITT protocol) for CRC calculation, the LSb is
shifted out first. Discussions on all the variations are beyond the scope of this document, but
many variations of CRC can be implemented using the 32-bit programmable CRC module in
dsPIC33E/PIC24E devices.
The choice of the polynomial length, and the polynomial itself, are application dependent.
Polynomial lengths of 5, 7, 8, 10, 12, 16 and 32 are normally used in various standard
implementations. The CRC module in dsPIC33E/PIC24E devices can be configured for different
polynomial lengths and for different equations. If a polynomial of n bits is selected for calculation,
normally ‘n’ zeros are appended to the message stream, though there are variations in this
process as well. The following sections explain the recommended step-by-step procedure for
CRC calculation, where n zeros are appended to the message stream for an n bit polynomial.
Users can decide whether zeros, or any other values, need to be appended to the message
stream. Depending on the application, the user may decide whether any value needs to be
appended at all.
ptr=(unsigned char *)&CRCDATL; //For data width less than or equal to 8 bits,
//the user has to feed the input data through
//byte operations into the CRCDATL register.
CRCCON1bits.CRCEN=1; // enable CRC
CRCCON2bits.PLEN=0x0F; // configure the polynomial width
CRCXORL=0x1021; // configure polynomial data
CRCXORH=0x0000;//
CRCCON2bits.DWIDTH=0x08; //configure the data width
CRCCON1bits.LENDIAN=0; // set the data shift direction
CRCCON1bits.CRCISEL=0; //select the interrupt source
*ptr=0x01; // write data into the FIFO
*ptr=0x02;
*ptr=0x03;
*ptr=0x04;
*ptr=0x05;
*ptr=0x06;
*ptr=0x07;
*ptr=0x08;
Nop ();
CRCCON1bits.CRCGO=1; // start the CRC calculation
while(IFS4bits.CRCIF!=1); // check for end of calculation
Nop ();
}
Cyclic Redundancy
CRCCON1bits.CRCGO=1; // start the CRC calculation
Programmable
while(IFS4bits.CRCIF!=1); // check for end of calculation
Nop ();
}
32-Bit
Table 27-2: Special Function Registers Associated with the Programmable CRC Module(1)
All
File Name Bit 15 Bit 14 Bit 13 Bit 12 Bit 11 Bit 10 Bit 9 Bit 8 Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
Resets
CRCCON1 CRCEN — CSIDL VWORD<4:0> CRCFUL CRCMPT CRCISEL CRCGO LENDIAN — — — 0040
CRCCON2 — — — DWIDTH<4:0> — — — PLEN<4:0> 0000
CRCXORL X<15:1> — 0000
CRCXORH X<31:16> 0000
CRCDATL DATA<15:0> 0000
CRCDATH DATA<31:16> 0000
CRCWDATL SDATA<15:0> 0000
CRCWDATH SDATA<31:16> 0000
IFS4 — — — — — — — — — — — — CRCIF — — — 0000
IEC4 — — — — — — — — — — — — CRCIE — — — 0000
IPC16 — CRCIP<2:0> — — — — — — — — — — — — 4440
Legend: — = unimplemented, read as ‘0’. Shaded bits are not used in the operation of the programmable CRC module.
Note 1: Refer to the device data sheet for specific memory map details.
DS70346B-page 27-17
Cyclic Redundancy
Programmable
27
32-Bit
dsPIC33E/PIC24E Family Reference Manual
Cyclic Redundancy
Programmable
32-Bit
NOTES: