13.

16-Bit CRC Generator

16-Bit CRC Generator
CRC16 v2.1
Copyright © 2002-2003. Cypress MicroSystems Inc. All Rights Reserved.

CY8C27/24/22xxx Data Sheet
Resources PSoC™ Blocks Memory Pins Other Modules 2 Digital, 0 Analog 54 Bytes Flash, 0 Bytes SRAM 1 per External I/O and Clock Required Optional

Features and Overview • 2- to 16-bit CRC generator • Data input clocking up to 48 MHz • Programmable polynomial • Programmable seed value • Serial data in, parallel result out
The CRC16 User Module computes a 2- to 16-bit cyclical redundancy check (CRC) algorithm on an input serial data stream. The polynomial can be defined to implement CRC functions, such as the CRC-16 or CCITT algorithm. A seed value can be specified to initialize the starting data value.
Polynomial Register
16 15 14 2 1

X

X

X

X 1

X 0

15

14

13

Shift Register
15 14 2 1 0

Input Data

Seed Register

15

14

2

1

0

CRC16 Block Diagram

August 29, 2003

1

To implement the CRCCCITT example. First. The Shift register computes the LFSR function. the Polynomial register holds the polynomial that defines the LFSR polynomial. A rising edge of the input clock shifts each bit. Shift. so that the polynomial for an N-bit result can be expressed by an N bit rather than N+1-bit specification. Seed. after the CRC16 is started. Shift. the last of which is the X0 term where X0=1. To specify the polynomial specification. while the CRC16 is not started. the CRC16 User Module is started. the Polynomial register is composed of the CRC16_MSB Polynomial register and the CRC16_LSB Polynomial register. with 1’s for each term present. the result is read directly from the Seed register. The Polynomial. 2003 . per data set. write an N+1 bit binary number corresponding to the full polynomial. initializing the starting data. 2 August 29. the Polynomial register is loaded with the value of 8810h. When the seed value and polynomial are initialized. computing the specified CRC algorithm. of the input data stream through the Shift register. and Control registers of each CRC16 block correspond to the digital PSoC block registers. For example. drop the right-most bit (the X0 term) to obtain the CRC16 polynomial value. Computation of an N-bit LFSR result is specified by a polynomial with N+1 terms. This causes the result data to be latched into the Seed register. and Seed registers refer to the combined registers from the CRC16_LSB and CRC16_MSB blocks (unless specifically noted). causes the seed value to be latched into the Shift register. the Shift register is read. The CRC16 User Module is implemented as a linear feedback shift register (LFSR). The CRC algorithm assumes the presence of the X0 term. the widely used CRC-CCITT 16-bit polynomial is X16+X12+X5+1. and the Seed register enables initialization of the starting data. This module requires that the Seed and Polynomial registers are initialized prior to setting the start bit in the CRC16_LSB’s Control register. The CRC16_MSB registers form the most significant byte of the register set and the CRC16_LSB registers form the least significant byte. The CRC-CCITT polynomial would be 10001000000100001b. The Polynomial. MSB first. has no effect. Writing the seed value. to ensure that the data is not inadvertently clocked in while performing a read. The configuration of the underlying connective hardware of the CRC16 digital PSoC blocks coordinate the operation of the two PSoC blocks as a single 16-bit CRC User Module. For example. Then. Note that the initial seed value is lost. This is usually of no consequence since the seed value is only used to initialize the Shift register once. It is advisable to stop the CRC16 User Module before reading the CRC value.User Module Data Sheet Functional Description The CRC16 User Module computes a 16-bit CRC algorithm with two consecutive digital blocks named CRC16_LSB and CRC16_MSB. Writing the seed value into the Seed register. Reading the CRC computed result is a two-step process. Then. Eight clocks are required to compute the CRC for each byte of input data.

This section specifies the interface to each function together with related constants provided by the “include” files. This “registers are volatile” policy was selected for efficiency reasons and has been in force since version 1. one of the divided clocks. or another PSoC block output can be specified as the clock input. the CPU_32kHz clock. 24V1 or 24V2. the analog comparator output bus. Clock The CRC16 is clocked by one of 16 possible sources. Invert InputDataStream This parameter gives the user the option to invert the InputDataStream. The 48 MHz clock. If the input data stream or clock is routed using a global bus. Calls to the API function can be made in both assembly and C. high. 2003 3 . the values of the A and X register may be altered by calling an API function. The Global I/O busses can be used to connect the clock input to an external pin or a clock function generated by a different PSoC block. as in all user module APIs. or one of the global busses. Conditions and Notes Typical 121 Limit 48 Units MHz Parameters and Resources InputDataStream The input data stream can be connected to a low. It is the responsibility of the calling function to preserve the values of A and X prior to the call if those values are required after the call. August 29. The C compiler automatically takes care of this requirement. neighboring PSoC block. Assembly language programmers must ensure their code observes the policy. the input can be connected to one of the external pins. The following is the list of CRC16 supplied API functions. there is no guarantee they will do so in the future. too. Using a global bus.0 of PSoC Designer. Application Programming Interface The Application Programming Interface (API) routines are provided as part of the user module to allow the designer to deal with the module at a higher level. then the maximum clock input rate is 12 MHz. Note In this. Though some user module API function may leave A and X unchanged.16-Bit CRC Generator DC and AC Electrical Characteristics CRC16 DC and AC Electrical Characteristics Parameter Max Input Data Clocking Electrical Characteristics Notes 1. Placement The CRC16 User Module can be placed in any two consecutive digital PSoC blocks.

For the CCITT polynomial. the polynomial and seed values should be initialized. 2003 .User Module Data Sheet CRC16_Start Description: Enables the CRC16 User Module for operation. The CRC16 User Module is stopped while updating the Shift register with the new seed value. C Prototype: void CRC16_WriteSeed(WORD wSeed) Assembler: mov X. C Prototype: void CRC16_Start(void) Assembler: call CRC16_Start Parameters: None Return Value: None Side Effects: The A and X registers may be altered by this function. CRC16_WriteSeed Description: Initializes the CRC16 Shift register with an initial seed value. which is set to 0xFFFF. [wSeed+1] call CRC16_WriteSeed Parameters: WORD wSeed: 16-bit seed value. MSB passed in the X register. CRC16_Stop Description: Disables the CRC16 User Module. Return Value: None 4 August 29. C Prototype: void CRC16_Stop(void) Assembler: call CRC16_Stop Parameters: None Return Value: None Side Effects: The A and X registers may be altered by this function. Before the CRC16 is started. Upon exit. the start state is restored. use the defined constant CRC16_CCITT_SEED. [wSeed] mov A. LSB passed in the A register.

which is set to 0x8810. Return Value: None Side Effects: The A and X registers may be altered by this function. For the CCITT polynomial. while data is currently being clocked in. use the defined constant CRC16_CCITT_POLYNOMIAL. The CRC16 User Module is stopped while the Polynomial register is updated. Side Effects: The Seed register is overwritten with the computed CRC value. 2003 5 . Calling this function. gives inaccurate results. C Prototype: WORD CRC16_wReadCRC(void) Assembler: call CRC16_wReadCRC mov reg[wCrcValue+1]. X mov reg[wCrcValue]. The A and X registers may be altered by this function. The A and X registers may be altered by this function. A mov A.16-Bit CRC Generator Side Effects: Writing a value into the Seed register also latches it into the Shift register. CRC16_ReadCRC Description: Reads the computed CRC resultant data. Upon exit. MSB is passed in the X register and LSB is passed in the A register. CRC16_WritePolynomial Description: Loads the Polynomial register with the CRC function polynomial. Reference the CRC16 User Module Functional Description section for a discussion on how to set the polynomial value. [wPolynomial] mov A. August 29. X Parameters: None Return Value: WORD wCrcValue: Value read from the Shift register. C Prototype: void CRC16_WritePolynomial(WORD wPolynomial) Assembler: mov X. the start state is restored. [wPolynomial+1] call CRC16_Stop Parameters: WORD wPolynomial: 16-bit polynomial value.

// load the CCITT seed CRC16_WriteSeed(CRC_CCITT_SEED). . <CRC_CCITT_SEED X. 2003 . load mov mov call .start the CRC16 call CRC16_Start The same code in ‘C’ is as follows. #include "CRC16. >CRC_CCITT_SEED CRC16_WriteSeed . Setup CCITT CRC16 . .LSB . } 6 August 29.inc" export SetupCCITT SetupCCITT: . <CRC_CCITT_POLYNOMIAL X.LSB . . stop the CRC16 user module call CRC16_Stop . >CRC_CCITT_POLYNOMIAL CRC16_WritePolynomial the CRC16 seed A.h" void SetupCCITT(void) { // stop the CRC16 user module CRC16_Stop().MSB .************************************************************************ . // load the CCITT polynomial CRC16_WritePolynomial(CRC_CCITT_POLYNOMIAL).MSB . This function initializes a CRC16 user module to compute a CCITT CRC .User Module Data Sheet Sample Firmware Source Code .************************************************************************ include "CRC16. algorithm. // start the CRC16 CRC16_Start(). load mov mov call the CCITT polynomial A.

This parameter is set in the Device Editor.16-Bit CRC Generator Configuration Registers The PSoC Digital block registers used to configure a user module are described below. 2003 7 . CRC16_MSB: Register Function Bit Value 7 0 6 0 5 1 4 0 3 0 2 0 1 1 0 0 CRC16_LSB: Register Function Bit Value 7 0 6 0 5 0 4 0 3 0 2 0 1 1 0 0 CRC16_MSB: Register Input Bit Value 7 0 6 0 5 1 4 1 3 2 Clock 1 0 Clock selects the input clock from one of 16 sources. CRC16_LSB: Register Input Bit Value 7 6 5 4 3 2 Clock 1 0 InputDataStream InputDataStream selects the data input from one of 16 sources. It is read and configured using the CRC16 API. Clock selects the input clock from one of 16 sources. CRC16_MSB: Register Output Bit Value 7 0 6 0 5 0 4 0 3 0 2 0 1 0 0 0 CRC16_LSB: Register Output Bit Value 7 0 6 0 5 0 4 0 3 0 2 0 1 0 0 0 CRC16_MSB: Shift Register DR0 Bit Value 7 6 5 4 3 2 1 0 Shift Register (MSB) Shift Register is the CRC16 MSB Shift register. August 29. These parameters are set in the Device Editor.

User Module Data Sheet CRC16_LSB: Shift Register DR0 Bit Value 7 6 5 4 3 2 1 0 Shift Register (LSB) Shift Register is the CRC16 LSB Shift register. 2003 . 8 August 29. It is modified using the CRC16 API. It is read and configured using the CRC16 API. It is modified using the CRC16 API. CRC16_MSB: Seed Register DR2 Bit Value 7 6 5 4 3 2 1 0 Seed Register (MSB) Seed Register is the CRC16 MSB Seed register. CRC16_MSB: Control Register CR0 Bit Value 7 0 6 0 5 0 4 0 3 0 2 0 1 0 0 0 CRC16_LSB: Control Register CR0 Bit Value 7 0 6 0 5 0 4 0 3 0 2 0 1 0 0 Start/Stop Start/Stop indicates that the CRC16 is enabled when set. CRC16_MSB: Polynomial Register DR1 Bit Value 7 6 5 4 3 2 1 0 Polynomial Register (MSB) Polynomial Register is the CRC16 MSB Polynomial register. CRC16_LSB: Polynomial Register DR1 Bit Value 7 6 5 4 3 2 1 0 Polynomial Register (LSB) Polynomial Register is the CRC16 LSB Polynomial register. It is modified using the CRC16 API. It is modified using the CRC16 API. CRC16_LSB: Seed Register DR2 Bit Value 7 6 5 4 3 2 1 0 Seed Register (LSB) Seed Register is the CRC16 LSB Seed register. It is modified using the CRC16 API.