0% found this document useful (0 votes)
266 views

CRC16

Uploaded by

Ruchi Gujarathi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
266 views

CRC16

Uploaded by

Ruchi Gujarathi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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 1

X 0

15

14

13

Shift Register
15 14 2 1 0

Input Data

Seed Register

15

14

CRC16 Block Diagram

August 29, 2003

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. The Polynomial, Shift, Seed, and Control registers of each CRC16 block correspond to the digital PSoC block registers. 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. The Polynomial, Shift, and Seed registers refer to the combined registers from the CRC16_LSB and CRC16_MSB blocks (unless specifically noted). The CRC16_MSB registers form the most significant byte of the register set and the CRC16_LSB registers form the least significant byte. For example, the Polynomial register is composed of the CRC16_MSB Polynomial register and the CRC16_LSB Polynomial register. The CRC16 User Module is implemented as a linear feedback shift register (LFSR). The Shift register computes the LFSR function; the Polynomial register holds the polynomial that defines the LFSR polynomial; 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_LSBs Control register. Writing the seed value into the Seed register, while the CRC16 is not started, causes the seed value to be latched into the Shift register, initializing the starting data. Writing the seed value, after the CRC16 is started, has no effect. Computation of an N-bit LFSR result is specified by a polynomial with N+1 terms, the last of which is the X0 term where X0=1. For example, the widely used CRC-CCITT 16-bit polynomial is X16+X12+X5+1. The CRC algorithm assumes the presence of the X0 term, so that the polynomial for an N-bit result can be expressed by an N bit rather than N+1-bit specification. To specify the polynomial specification, write an N+1 bit binary number corresponding to the full polynomial, with 1s for each term present. The CRC-CCITT polynomial would be 10001000000100001b. Then, drop the right-most bit (the X0 term) to obtain the CRC16 polynomial value. To implement the CRCCCITT example, the Polynomial register is loaded with the value of 8810h. When the seed value and polynomial are initialized, the CRC16 User Module is started. A rising edge of the input clock shifts each bit, MSB first, of the input data stream through the Shift register, computing the specified CRC algorithm. Eight clocks are required to compute the CRC for each byte of input data. Reading the CRC computed result is a two-step process. First, the Shift register is read. This causes the result data to be latched into the Seed register. Then, the result is read directly from the Seed register. 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, per data set. It is advisable to stop the CRC16 User Module before reading the CRC value, to ensure that the data is not inadvertently clocked in while performing a read.

August 29, 2003

16-Bit CRC Generator

DC and AC Electrical Characteristics


CRC16 DC and AC Electrical Characteristics Parameter Max Input Data Clocking Electrical Characteristics Notes 1. If the input data stream or clock is routed using a global bus, then the maximum clock input rate is 12 MHz. Conditions and Notes Typical 121 Limit 48 Units MHz

Parameters and Resources


InputDataStream
The input data stream can be connected to a low, high, neighboring PSoC block, the analog comparator output bus, or one of the global busses. Using a global bus, the input can be connected to one of the external pins.

Clock
The CRC16 is clocked by one of 16 possible sources. 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. The 48 MHz clock, the CPU_32kHz clock, one of the divided clocks, 24V1 or 24V2, or another PSoC block output can be specified as the clock input.

Invert InputDataStream
This parameter gives the user the option to invert the InputDataStream.

Placement
The CRC16 User Module can be placed in any two consecutive digital PSoC blocks.

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. This section specifies the interface to each function together with related constants provided by the include files. Note In this, as in all user module APIs, the values of the A and X register may be altered by calling an API function. 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. This registers are volatile policy was selected for efficiency reasons and has been in force since version 1.0 of PSoC Designer. The C compiler automatically takes care of this requirement. Assembly language programmers must ensure their code observes the policy, too. Though some user module API function may leave A and X unchanged, there is no guarantee they will do so in the future.

Calls to the API function can be made in both assembly and C. The following is the list of CRC16 supplied API functions.

August 29, 2003

User Module Data Sheet

CRC16_Start
Description: Enables the CRC16 User Module for operation. Before the CRC16 is started, the polynomial and seed values should be initialized. 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_Stop
Description: Disables the CRC16 User Module. 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.

CRC16_WriteSeed
Description: Initializes the CRC16 Shift register with an initial seed value. The CRC16 User Module is stopped while updating the Shift register with the new seed value. Upon exit, the start state is restored. C Prototype: void CRC16_WriteSeed(WORD wSeed) Assembler: mov X, [wSeed] mov A, [wSeed+1] call CRC16_WriteSeed Parameters: WORD wSeed: 16-bit seed value. MSB passed in the X register. LSB passed in the A register. For the CCITT polynomial, use the defined constant CRC16_CCITT_SEED, which is set to 0xFFFF. Return Value: None

August 29, 2003

16-Bit CRC Generator

Side Effects: Writing a value into the Seed register also latches it into the Shift register. The A and X registers may be altered by this function.

CRC16_WritePolynomial
Description: Loads the Polynomial register with the CRC function polynomial. The CRC16 User Module is stopped while the Polynomial register is updated. Upon exit, the start state is restored. C Prototype: void CRC16_WritePolynomial(WORD wPolynomial) Assembler: mov X, [wPolynomial] mov A, [wPolynomial+1] call CRC16_Stop Parameters: WORD wPolynomial: 16-bit polynomial value. Reference the CRC16 User Module Functional Description section for a discussion on how to set the polynomial value. For the CCITT polynomial, use the defined constant CRC16_CCITT_POLYNOMIAL, which is set to 0x8810. MSB is passed in the X register and LSB is passed in the A register. Return Value: None Side Effects: The A and X registers may be altered by this function.

CRC16_ReadCRC
Description: Reads the computed CRC resultant data. Calling this function, while data is currently being clocked in, gives inaccurate results. C Prototype: WORD CRC16_wReadCRC(void) Assembler: call CRC16_wReadCRC mov reg[wCrcValue+1], A mov A, X mov reg[wCrcValue], X Parameters: None Return Value: WORD wCrcValue: Value read from the Shift register. Side Effects: The Seed register is overwritten with the computed CRC value. The A and X registers may be altered by this function.

August 29, 2003

User Module Data Sheet

Sample Firmware Source Code


;************************************************************************ ; Setup CCITT CRC16 ; ; This function initializes a CRC16 user module to compute a CCITT CRC ; algorithm. ; ;************************************************************************ include "CRC16.inc" export SetupCCITT SetupCCITT: ; stop the CRC16 user module call CRC16_Stop ; load mov mov call ; load mov mov call the CCITT polynomial A, <CRC_CCITT_POLYNOMIAL X, >CRC_CCITT_POLYNOMIAL CRC16_WritePolynomial the CRC16 seed A, <CRC_CCITT_SEED X, >CRC_CCITT_SEED CRC16_WriteSeed ;LSB ;MSB

;LSB ;MSB

;start the CRC16 call CRC16_Start The same code in C is as follows. #include "CRC16.h" void SetupCCITT(void) { // stop the CRC16 user module CRC16_Stop(); // load the CCITT polynomial CRC16_WritePolynomial(CRC_CCITT_POLYNOMIAL); // load the CCITT seed CRC16_WriteSeed(CRC_CCITT_SEED); // start the CRC16 CRC16_Start();

August 29, 2003

16-Bit CRC Generator

Configuration Registers
The PSoC Digital block registers used to configure a user module are described below.
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. This parameter is set in the Device Editor. 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. Clock selects the input clock from one of 16 sources. These parameters are set in the Device Editor. 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. It is read and configured using the CRC16 API.

August 29, 2003

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. It is read and configured using the CRC16 API. 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. It is modified using the CRC16 API. 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. 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. 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. 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. It is modified using the CRC16 API.

August 29, 2003

You might also like