30.11.2012 Views

Using the CRC module on the Flexis AC Family - Freescale

Using the CRC module on the Flexis AC Family - Freescale

Using the CRC module on the Flexis AC Family - Freescale

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Freescale</strong> Semic<strong>on</strong>ductor<br />

Applicati<strong>on</strong> Note<br />

Document Number: AN3795<br />

Rev. 0, 04/2009<br />

<str<strong>on</strong>g>Using</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g><br />

<strong>Flexis</strong> <strong>AC</strong> <strong>Family</strong><br />

by: Gord<strong>on</strong> Borland<br />

MSG Applicati<strong>on</strong>s Engineering. East Kilbride, Scotland<br />

1 Introducti<strong>on</strong><br />

There are increasing requirements for in-applicati<strong>on</strong> tests<br />

and checks up<strong>on</strong> embedded c<strong>on</strong>trol software, to ensure it<br />

will operate without a malfuncti<strong>on</strong> that may cause a<br />

hazard. Peer reviews and following best programming<br />

practice can help to ensure that <str<strong>on</strong>g>the</str<strong>on</strong>g> fundamental software<br />

operati<strong>on</strong> is safe, but it cannot protect an applicati<strong>on</strong><br />

against a corrupti<strong>on</strong> of it’s software, for example<br />

an EMC event.<br />

The incorporati<strong>on</strong> of a cyclic redundancy check <str<strong>on</strong>g>module</str<strong>on</strong>g><br />

within <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>Flexis</strong> <strong>AC</strong> family provides a mechanism for<br />

users to verify, that <str<strong>on</strong>g>the</str<strong>on</strong>g>ir program data has not been<br />

corrupted.<br />

1.1 Abstract<br />

This document is written to show how <str<strong>on</strong>g>the</str<strong>on</strong>g> cyclic<br />

redundancy check <str<strong>on</strong>g>module</str<strong>on</strong>g> of <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>Flexis</strong> <strong>AC</strong> family can be<br />

used to check in-applicati<strong>on</strong> software for corrupti<strong>on</strong> and<br />

verify data integrity.<br />

© <strong>Freescale</strong> Semic<strong>on</strong>ductor, Inc., 2009. All rights reserved.<br />

C<strong>on</strong>tents<br />

1 Introducti<strong>on</strong> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1<br />

1.1 Abstract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1<br />

1.2 Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2<br />

2 The <strong>Flexis</strong> <strong>AC</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module . . . . . . . . . . . . . . . . . . . . . . 2<br />

2.1 Advantages of <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>Flexis</strong> <strong>AC</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module . . . . . . . 2<br />

2.2 Cyclic Redundancy Checking . . . . . . . . . . . . . . . . . 3<br />

2.3 <strong>Flexis</strong> <strong>AC</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Implementati<strong>on</strong>. . . . . . . . . . . . . . . . 8<br />

2.4 How to Use <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module . . . . . . . . . . . . . . . . 10<br />

3 C<strong>on</strong>clusi<strong>on</strong>. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14<br />

4 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14<br />

5 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15<br />

Appendix A<br />

Cyclic Redundancy Check Software . . . . . . . . . . . . . . . . . . . 15<br />

A.1 Main.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15<br />

A.2 init_crcCCITT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16<br />

A.3 crc_flash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16<br />

A.4 update_crcCCITT . . . . . . . . . . . . . . . . . . . . . . . . . 16<br />

A.5 augment_message_for_crc_16(). . . . . . . . . . . . . . 17<br />

A.6 crc_check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17


The <strong>Flexis</strong> <strong>AC</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module<br />

1.2 Objective<br />

This applicati<strong>on</strong> note will explain <str<strong>on</strong>g>the</str<strong>on</strong>g> fundamental principles behind cyclic redundancy checking, it’s<br />

implementati<strong>on</strong> in <str<strong>on</strong>g>the</str<strong>on</strong>g> MC9S08<strong>AC</strong>128, and how it can be used to meet <str<strong>on</strong>g>the</str<strong>on</strong>g> embedded c<strong>on</strong>trol software<br />

safety requirements by verifying <str<strong>on</strong>g>the</str<strong>on</strong>g> software integrity. Example code is also provided.<br />

2 The <strong>Flexis</strong> <strong>AC</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module<br />

2.1 Advantages of <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>Flexis</strong> <strong>AC</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module<br />

Whilst it is possible to implement a cyclic redundancy check using software routines <strong>on</strong>ly, <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>Flexis</strong> <strong>AC</strong><br />

<str<strong>on</strong>g>CRC</str<strong>on</strong>g> offers several advantages over a software-<strong>on</strong>ly soluti<strong>on</strong>.<br />

Software <str<strong>on</strong>g>CRC</str<strong>on</strong>g> soluti<strong>on</strong>s will require certificati<strong>on</strong> to show that <str<strong>on</strong>g>the</str<strong>on</strong>g>y fulfill <str<strong>on</strong>g>the</str<strong>on</strong>g> requirements of <str<strong>on</strong>g>the</str<strong>on</strong>g> protocol<br />

being followed. The <strong>Flexis</strong> <strong>AC</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> <str<strong>on</strong>g>module</str<strong>on</strong>g> already has <str<strong>on</strong>g>the</str<strong>on</strong>g> compliancy to <str<strong>on</strong>g>CRC</str<strong>on</strong>g>16-CCITT built in using<br />

<str<strong>on</strong>g>the</str<strong>on</strong>g> x 16 + x 12 + x 5 + 1 polynomial and it’s implementati<strong>on</strong> meets <str<strong>on</strong>g>the</str<strong>on</strong>g> ITU-T (Internati<strong>on</strong>al<br />

Telecommunicati<strong>on</strong> Uni<strong>on</strong> Telecommunicati<strong>on</strong> Standardizati<strong>on</strong> Sector) V.41 recommendati<strong>on</strong>.<br />

The compliance to <str<strong>on</strong>g>CRC</str<strong>on</strong>g>16-CCITT means that <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>Flexis</strong> <strong>AC</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> <str<strong>on</strong>g>module</str<strong>on</strong>g> can detect all single, double,<br />

odd, and most multi-bit errors.<br />

A software soluti<strong>on</strong> will require several hundred bus cycles (~700) to process a single byte through <str<strong>on</strong>g>the</str<strong>on</strong>g><br />

<str<strong>on</strong>g>CRC</str<strong>on</strong>g> algorithm. The <strong>Flexis</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> <str<strong>on</strong>g>module</str<strong>on</strong>g> can shift a byte into <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> generator in <strong>on</strong>e bus cycle resulting<br />

in a greatly increased speed of calculati<strong>on</strong>.<br />

For example measurements <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> MC9S08<strong>AC</strong>128 show that a software soluti<strong>on</strong> compliant with<br />

<str<strong>on</strong>g>CRC</str<strong>on</strong>g>16-CCITT running at 20 MHz bus speed takes approximately 6.7 sec<strong>on</strong>ds to perform a cyclic<br />

redundancy check <strong>on</strong> 128 K of flash memory. By comparis<strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> same check requires <strong>on</strong>ly 170 ms using<br />

<str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> <str<strong>on</strong>g>module</str<strong>on</strong>g>, which is significantly faster.<br />

The increased speed of cyclic redundancy calculati<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> <str<strong>on</strong>g>module</str<strong>on</strong>g> over software means that <str<strong>on</strong>g>the</str<strong>on</strong>g><br />

<strong>Flexis</strong> <strong>AC</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> <str<strong>on</strong>g>module</str<strong>on</strong>g> is ideally suited to perform cyclic redundancy checking <strong>on</strong> data transmissi<strong>on</strong>s with<br />

minimal additi<strong>on</strong>al overhead, when compared to a software soluti<strong>on</strong>.<br />

The <strong>Flexis</strong> <strong>AC</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> <str<strong>on</strong>g>module</str<strong>on</strong>g>s’ high-speed <str<strong>on</strong>g>CRC</str<strong>on</strong>g> calculati<strong>on</strong> means that a self check of <str<strong>on</strong>g>the</str<strong>on</strong>g> applicati<strong>on</strong><br />

software at startup can be performed without significantly adding to <str<strong>on</strong>g>the</str<strong>on</strong>g> initializati<strong>on</strong> time.<br />

Fur<str<strong>on</strong>g>the</str<strong>on</strong>g>r, <str<strong>on</strong>g>the</str<strong>on</strong>g> user can take <str<strong>on</strong>g>the</str<strong>on</strong>g> advantage of <str<strong>on</strong>g>the</str<strong>on</strong>g> fast cyclic redundancy calculati<strong>on</strong> to implement a self test<br />

of <str<strong>on</strong>g>the</str<strong>on</strong>g> software at regular intervals, whilst <str<strong>on</strong>g>the</str<strong>on</strong>g> applicati<strong>on</strong> is running without adversely impacting <str<strong>on</strong>g>the</str<strong>on</strong>g><br />

performance.<br />

In additi<strong>on</strong> to <str<strong>on</strong>g>the</str<strong>on</strong>g> advantages offered over a software <str<strong>on</strong>g>CRC</str<strong>on</strong>g>, <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>Flexis</strong> <strong>AC</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> <str<strong>on</strong>g>module</str<strong>on</strong>g> is capable of<br />

handling programmable seed values and opti<strong>on</strong>al augmentati<strong>on</strong>. This flexibility allows <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>Flexis</strong> <strong>AC</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g><br />

<str<strong>on</strong>g>module</str<strong>on</strong>g> to comply with slightly different ITU-T protocols based up<strong>on</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g>16-CCITT without any major<br />

code changes.<br />

2<br />

<str<strong>on</strong>g>Using</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>Flexis</strong> <strong>AC</strong> <strong>Family</strong>, Rev. 0<br />

<strong>Freescale</strong> Semic<strong>on</strong>ductor


2.2 Cyclic Redundancy Checking<br />

<str<strong>on</strong>g>Using</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>Flexis</strong> <strong>AC</strong> <strong>Family</strong>, Rev. 0<br />

The <strong>Flexis</strong> <strong>AC</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module<br />

Error detecti<strong>on</strong> is important, whenever <str<strong>on</strong>g>the</str<strong>on</strong>g>re is a chance of data getting corrupted. Whe<str<strong>on</strong>g>the</str<strong>on</strong>g>r it’s a piece of<br />

stored code or a data transmissi<strong>on</strong>, you can add a piece of redundant informati<strong>on</strong> to validate <str<strong>on</strong>g>the</str<strong>on</strong>g> data and<br />

protect it against corrupti<strong>on</strong>.<br />

The simplest example is a parity bit. Typically <strong>on</strong>e parity bit is used for each byte. For odd parity, <str<strong>on</strong>g>the</str<strong>on</strong>g><br />

number of n<strong>on</strong>-zero bits in a byte are counted. If <str<strong>on</strong>g>the</str<strong>on</strong>g> number is even <str<strong>on</strong>g>the</str<strong>on</strong>g> parity bit is set, o<str<strong>on</strong>g>the</str<strong>on</strong>g>rwise it is<br />

cleared. If <strong>on</strong>e of <str<strong>on</strong>g>the</str<strong>on</strong>g> nine bits is flipped, <str<strong>on</strong>g>the</str<strong>on</strong>g> sum will be even and an error will be detected. Of course, if<br />

two bits are flipped, <str<strong>on</strong>g>the</str<strong>on</strong>g> error will remain undetected.<br />

Cyclic redundancy checking is a much more robust error-checking algorithm, which is comm<strong>on</strong>ly used to<br />

detect errors ei<str<strong>on</strong>g>the</str<strong>on</strong>g>r in data transmissi<strong>on</strong> or data storage.<br />

Whilst it is not <str<strong>on</strong>g>the</str<strong>on</strong>g> intenti<strong>on</strong> of this document to go into detail regarding <str<strong>on</strong>g>the</str<strong>on</strong>g> ma<str<strong>on</strong>g>the</str<strong>on</strong>g>matics behind <str<strong>on</strong>g>the</str<strong>on</strong>g><br />

c<strong>on</strong>cept of cyclic redundancy, it is worthwhile to explain a few basic principles. Computati<strong>on</strong> of a cyclic<br />

redundancy check is derived from <str<strong>on</strong>g>the</str<strong>on</strong>g> ma<str<strong>on</strong>g>the</str<strong>on</strong>g>matics of:<br />

polynomial divisi<strong>on</strong><br />

modulo two arithmetic<br />

In practice it resembles a l<strong>on</strong>g divisi<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> binary message string by a generator polynomial string,<br />

except that exclusive OR operati<strong>on</strong>s replace <str<strong>on</strong>g>the</str<strong>on</strong>g> subtracti<strong>on</strong>s.<br />

2.2.1 Modulo Two Arithmetic<br />

Modulo two arithmetic is simple single-bit binary arithmetic with all “carries or borrows” ignored. Each<br />

digit is c<strong>on</strong>sidered independently. For example, we can add two binary numbers X and Y as follows:<br />

10101001 (X)<br />

+ 00111010 (Y)<br />

10010011 (Z)<br />

From this example <str<strong>on</strong>g>the</str<strong>on</strong>g> modulo two additi<strong>on</strong> is equivalent to an exclusive OR operati<strong>on</strong>. What is less<br />

obvious is that modulo two subtracti<strong>on</strong> gives <str<strong>on</strong>g>the</str<strong>on</strong>g> same results as an additi<strong>on</strong>. From <str<strong>on</strong>g>the</str<strong>on</strong>g> previous example<br />

let’s add X and Z:<br />

10101001 (X)<br />

+ 10010011 (Z)<br />

00111010 (Y)<br />

In our previous example we have seen how X + Y = Z <str<strong>on</strong>g>the</str<strong>on</strong>g>refore Y = Z – X, but <str<strong>on</strong>g>the</str<strong>on</strong>g> example above shows<br />

that Z+X = Y also, hence modulo two additi<strong>on</strong> is equivalent to modulo two subtracti<strong>on</strong>, and can be<br />

performed using an exclusive OR operati<strong>on</strong>.<br />

<strong>Freescale</strong> Semic<strong>on</strong>ductor 3


The <strong>Flexis</strong> <strong>AC</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module<br />

2.2.2 Polynomial Divisi<strong>on</strong><br />

In integer divisi<strong>on</strong> dividing A by B will result in a quotient Q, and a remainder R.<br />

4<br />

A / B = Q with remainder R where R < B. Eqn. 1<br />

Polynomial divisi<strong>on</strong> is similar except that when A and B are polynomials, <str<strong>on</strong>g>the</str<strong>on</strong>g> remainder is a polynomial,<br />

whose degree is less than B.<br />

Thus if <str<strong>on</strong>g>the</str<strong>on</strong>g> polynomial represented by A = 2x 3 + 5x 2 + 9x + 5 and B = x 2 + 2x + 3 (degree 2), <str<strong>on</strong>g>the</str<strong>on</strong>g>n<br />

can be represented as:<br />

A / B = Q with remainder R Eqn. 2<br />

(2x 3 + 5x 2 + 9x + 5) / (x 2 + 2x + 3) = (2x+1) with remainder x + 2 Eqn. 3<br />

Where Q = 2x + 1 (degree 2) and R = x + 2 (degree 1).<br />

If a change is made to polynomial A, <str<strong>on</strong>g>the</str<strong>on</strong>g>n <str<strong>on</strong>g>the</str<strong>on</strong>g>re will be a resultant change in <str<strong>on</strong>g>the</str<strong>on</strong>g> remainder R.<br />

Let A = 2x 3 + 5x 2 + 9x + 4<br />

Is now represented as:<br />

R is now x + 1.<br />

A / B = Q with remainder R Eqn. 4<br />

(2x 3 + 5x 2 + 9x + 4) / (x 2 + 2x + 3) = (2x+1) with remainder x + 1 Eqn. 5<br />

The key point here is that any change to <str<strong>on</strong>g>the</str<strong>on</strong>g> polynomial A causes a change to <str<strong>on</strong>g>the</str<strong>on</strong>g> remainder R. This<br />

behaviour forms <str<strong>on</strong>g>the</str<strong>on</strong>g> basis of <str<strong>on</strong>g>the</str<strong>on</strong>g> cyclic redundancy checking.<br />

If we c<strong>on</strong>sider a polynomial, whose coefficients are zeros and <strong>on</strong>es (modulo two), this polynomial can be<br />

easily represented by its coefficients as binary powers of two.<br />

For example<br />

1x 7 + 1x 6 + 0x 5 + 1x 4 + 0x 3 + 1x 2 + 1x + 1 = 11010111 Eqn. 6<br />

Thus it is apparent that a polynomial can be represented as a series of bits, and c<strong>on</strong>versely any binary string<br />

of bits, such as a data message or software code, can be represented as a polynomial.<br />

In terms of cyclic redundancy calculati<strong>on</strong>s, <str<strong>on</strong>g>the</str<strong>on</strong>g> polynomial A would be <str<strong>on</strong>g>the</str<strong>on</strong>g> binary message string or data<br />

and polynomial B would be <str<strong>on</strong>g>the</str<strong>on</strong>g> generator polynomial. The remainder R would be <str<strong>on</strong>g>the</str<strong>on</strong>g> cyclic redundancy<br />

checksum. If <str<strong>on</strong>g>the</str<strong>on</strong>g> data changed or became corrupt, <str<strong>on</strong>g>the</str<strong>on</strong>g>n a different remainder would be calculated.<br />

2.2.3 Basic <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Calculati<strong>on</strong><br />

Now we can begin to perform a basic cyclic redundancy checksum calculati<strong>on</strong> using polynomial divisi<strong>on</strong><br />

and modulo two arithmetic. As described previously, this is basically <str<strong>on</strong>g>the</str<strong>on</strong>g> divisi<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> data polynomial<br />

<str<strong>on</strong>g>Using</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>Flexis</strong> <strong>AC</strong> <strong>Family</strong>, Rev. 0<br />

<strong>Freescale</strong> Semic<strong>on</strong>ductor


<str<strong>on</strong>g>Using</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>Flexis</strong> <strong>AC</strong> <strong>Family</strong>, Rev. 0<br />

The <strong>Flexis</strong> <strong>AC</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module<br />

by a generator polynomial. This basic calculati<strong>on</strong> will use a simple piece of data such as 0xD6B5 (in binary<br />

1101011010110101) and use <str<strong>on</strong>g>the</str<strong>on</strong>g> polynomial x 3 + x 2 + 1 as <str<strong>on</strong>g>the</str<strong>on</strong>g> generator polynomial.<br />

The data polynomial is divided by <str<strong>on</strong>g>the</str<strong>on</strong>g> generator polynomial x 3 + x 2 + 1 (or 1101), using an exclusive OR<br />

to replace <str<strong>on</strong>g>the</str<strong>on</strong>g> subtracti<strong>on</strong> (modulo two arithmetic), and we record <str<strong>on</strong>g>the</str<strong>on</strong>g> remainder, which is <str<strong>on</strong>g>the</str<strong>on</strong>g> cyclic<br />

redundancy checksum.<br />

1000010000100 (Q)<br />

1101 ) 1101011010110101 (A)<br />

(B) 1101xxxxx<br />

001101<br />

1101xxxxx<br />

001101<br />

1101xx<br />

001 (R, <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g>)<br />

Thus in this example <str<strong>on</strong>g>the</str<strong>on</strong>g> remainder (cyclic redundancy checksum) is 001. If any of <str<strong>on</strong>g>the</str<strong>on</strong>g> bits in <str<strong>on</strong>g>the</str<strong>on</strong>g> dividend<br />

(binary message) were to change, <str<strong>on</strong>g>the</str<strong>on</strong>g>n a different cyclic redundancy checksum would be calculated. <str<strong>on</strong>g>Using</str<strong>on</strong>g><br />

<str<strong>on</strong>g>the</str<strong>on</strong>g> previous data, let’s assume that bit <strong>on</strong>e of <str<strong>on</strong>g>the</str<strong>on</strong>g> data has been corrupted. The data will now read 0xD6B7<br />

instead of 0xD6B5.<br />

1000010000100 (Q)<br />

1101 ) 1101011010110111 (A)<br />

(B) 1101xxxxx<br />

001101<br />

1101xxxxx<br />

001101<br />

1101xx<br />

011 (R, <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g>)<br />

Thus to determine, whe<str<strong>on</strong>g>the</str<strong>on</strong>g>r any corrupti<strong>on</strong> of data has occurred, it is a simple matter to repeat <str<strong>on</strong>g>the</str<strong>on</strong>g> divisi<strong>on</strong><br />

using <str<strong>on</strong>g>the</str<strong>on</strong>g> same generator polynomial, and compare <str<strong>on</strong>g>the</str<strong>on</strong>g> remainder (cyclic redundancy checksum) produced<br />

against <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>on</strong>e calculated previously. If <str<strong>on</strong>g>the</str<strong>on</strong>g>y are <str<strong>on</strong>g>the</str<strong>on</strong>g> same, <str<strong>on</strong>g>the</str<strong>on</strong>g>n no corrupti<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> data has occurred and<br />

<str<strong>on</strong>g>the</str<strong>on</strong>g> integrity of data is intact.<br />

2.2.4 Null Cyclic Redundancy Checksum<br />

It has already been dem<strong>on</strong>strated, how <str<strong>on</strong>g>the</str<strong>on</strong>g> integrity of data can be verified by calculating <str<strong>on</strong>g>the</str<strong>on</strong>g> cyclic<br />

redundancy checksum and comparing it to <strong>on</strong>e calculated previously. An alternate method is to add <str<strong>on</strong>g>the</str<strong>on</strong>g><br />

cyclic redundancy checksum to <str<strong>on</strong>g>the</str<strong>on</strong>g> data and perform <str<strong>on</strong>g>the</str<strong>on</strong>g> cyclic redundancy checksum calculati<strong>on</strong>. If <str<strong>on</strong>g>the</str<strong>on</strong>g><br />

data is not corrupted, <str<strong>on</strong>g>the</str<strong>on</strong>g>n <str<strong>on</strong>g>the</str<strong>on</strong>g> result will be null (0x00). In <str<strong>on</strong>g>the</str<strong>on</strong>g> earlier analogy with integer divisi<strong>on</strong>,<br />

dividing A by B will result in a quotient Q, and a remainder R.<br />

<strong>Freescale</strong> Semic<strong>on</strong>ductor 5


The <strong>Flexis</strong> <strong>AC</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module<br />

For example:<br />

6<br />

A / B = Q with remainder R Eqn. 7<br />

We defined A to be <str<strong>on</strong>g>the</str<strong>on</strong>g> data polynomial and B <str<strong>on</strong>g>the</str<strong>on</strong>g> generator polynomial.<br />

If<br />

<str<strong>on</strong>g>the</str<strong>on</strong>g>n<br />

A / B = Q with remainder R Eqn. 8<br />

(A – R) / B = Q with no remainder Eqn. 9<br />

No remainder = 0x00 = Null <str<strong>on</strong>g>CRC</str<strong>on</strong>g> result. Eqn. 10<br />

Let’s illustrate this using our previous example. <str<strong>on</strong>g>Using</str<strong>on</strong>g> 0xD6B5 = 1101011010110101 as our data<br />

polynomial, and x 3 + x 2 + 1 = 1101 as <str<strong>on</strong>g>the</str<strong>on</strong>g> generator polynomial, we calculate a checksum of 001.<br />

1000010000100 (Q)<br />

1101 ) 1101011010110101 (A)<br />

(B) 1101xxxxx<br />

001101<br />

1101xxxxx<br />

001101<br />

1101xx<br />

001 (R)<br />

Now let’s add <str<strong>on</strong>g>the</str<strong>on</strong>g> checksum to <str<strong>on</strong>g>the</str<strong>on</strong>g> data polynomial using modulo two arithmetic. Remember that in<br />

modulo two arithmetic, additi<strong>on</strong> is <str<strong>on</strong>g>the</str<strong>on</strong>g> same as subtracti<strong>on</strong>.<br />

Data polynomial = 0xD6B5 = 1101011010110101<br />

+ <str<strong>on</strong>g>CRC</str<strong>on</strong>g> 001<br />

1101011010110100 = 0xD6B4<br />

Calculating <str<strong>on</strong>g>the</str<strong>on</strong>g> cyclic redundancy checksum for <str<strong>on</strong>g>the</str<strong>on</strong>g> data + checksum we get:<br />

1000010000100 (Q)<br />

1101 ) 1101011010110100 (A-R)<br />

(B) 1101xxxxx<br />

001101<br />

1101xxxxx<br />

001101<br />

1101xx<br />

000 NULL <str<strong>on</strong>g>CRC</str<strong>on</strong>g><br />

<str<strong>on</strong>g>Using</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>Flexis</strong> <strong>AC</strong> <strong>Family</strong>, Rev. 0<br />

<strong>Freescale</strong> Semic<strong>on</strong>ductor


As shown <str<strong>on</strong>g>the</str<strong>on</strong>g> result is 0x00 or null.<br />

2.2.5 Augmentati<strong>on</strong><br />

<str<strong>on</strong>g>Using</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>Flexis</strong> <strong>AC</strong> <strong>Family</strong>, Rev. 0<br />

The <strong>Flexis</strong> <strong>AC</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module<br />

Augmentati<strong>on</strong> is a technique used to produce a null <str<strong>on</strong>g>CRC</str<strong>on</strong>g> result, whilst preserving both <str<strong>on</strong>g>the</str<strong>on</strong>g> original data<br />

and <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> checksum.<br />

No augmentati<strong>on</strong> is required with <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>Flexis</strong> <strong>AC</strong> <strong>Family</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g>. A null <str<strong>on</strong>g>CRC</str<strong>on</strong>g> result can be obtained by<br />

writing <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> result to <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> <str<strong>on</strong>g>module</str<strong>on</strong>g> after <str<strong>on</strong>g>the</str<strong>on</strong>g> data. Augmentati<strong>on</strong> is menti<strong>on</strong>ed <strong>on</strong>ly to explain <str<strong>on</strong>g>the</str<strong>on</strong>g><br />

c<strong>on</strong>cept and assist users of o<str<strong>on</strong>g>the</str<strong>on</strong>g>r cyclic redundancy implementati<strong>on</strong>s, using augmentati<strong>on</strong> migrate to <str<strong>on</strong>g>the</str<strong>on</strong>g><br />

<strong>Flexis</strong> <strong>AC</strong> <strong>Family</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g>.<br />

In a communicati<strong>on</strong>s system using cyclic redundancy checking, it would be desirable to obtain a null <str<strong>on</strong>g>CRC</str<strong>on</strong>g><br />

result for each transimissi<strong>on</strong>, as <str<strong>on</strong>g>the</str<strong>on</strong>g> simplified verificati<strong>on</strong> will help to speed up <str<strong>on</strong>g>the</str<strong>on</strong>g> data handling.<br />

The previous secti<strong>on</strong> dem<strong>on</strong>strated, how to produce a null <str<strong>on</strong>g>CRC</str<strong>on</strong>g> result by adding <str<strong>on</strong>g>the</str<strong>on</strong>g> cyclic redundancy<br />

checksum (0b001) to <str<strong>on</strong>g>the</str<strong>on</strong>g> data (0xD6B5), and calculating <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> new data (0xD6B4). Whilst this<br />

simplifies <str<strong>on</strong>g>the</str<strong>on</strong>g> verificati<strong>on</strong>, it has <str<strong>on</strong>g>the</str<strong>on</strong>g> unfortunate side effect of changing <str<strong>on</strong>g>the</str<strong>on</strong>g> data. Any node receiving<br />

0xD6B4 will be able to verify that no corrupti<strong>on</strong> has occurred, but will be unable to extract <str<strong>on</strong>g>the</str<strong>on</strong>g> original<br />

data, because <str<strong>on</strong>g>the</str<strong>on</strong>g> checksum (0b001) is not known. This can be overcome by transmitting <str<strong>on</strong>g>the</str<strong>on</strong>g> checksum<br />

al<strong>on</strong>g with <str<strong>on</strong>g>the</str<strong>on</strong>g> modified data, but any data-handling advantage gained in <str<strong>on</strong>g>the</str<strong>on</strong>g> verficati<strong>on</strong> process is offset<br />

by <str<strong>on</strong>g>the</str<strong>on</strong>g> additi<strong>on</strong>al steps needed to recover <str<strong>on</strong>g>the</str<strong>on</strong>g> original data.<br />

Augmentati<strong>on</strong> allows <str<strong>on</strong>g>the</str<strong>on</strong>g> data to be transmitted al<strong>on</strong>g with its checksum, and still obtain a null <str<strong>on</strong>g>CRC</str<strong>on</strong>g> result.<br />

In <str<strong>on</strong>g>the</str<strong>on</strong>g> previous example to obtain a null <str<strong>on</strong>g>CRC</str<strong>on</strong>g> result, <str<strong>on</strong>g>the</str<strong>on</strong>g> data changed from 0xD6B5 to 0xD6B4, when <str<strong>on</strong>g>the</str<strong>on</strong>g><br />

checksum was added. Augmentati<strong>on</strong> avoids this by shifting <str<strong>on</strong>g>the</str<strong>on</strong>g> data left or augmenting it with a number<br />

of zeros, equivalent to <str<strong>on</strong>g>the</str<strong>on</strong>g> degree of <str<strong>on</strong>g>the</str<strong>on</strong>g> generator polynomial. When <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> result for <str<strong>on</strong>g>the</str<strong>on</strong>g> shifted data is<br />

added, both <str<strong>on</strong>g>the</str<strong>on</strong>g> original data and <str<strong>on</strong>g>the</str<strong>on</strong>g> checksum are preserved.<br />

In our previous example, our generator polynomial (x 3 + x 2 + 1 or 1101) is of degree 3, so <str<strong>on</strong>g>the</str<strong>on</strong>g> data<br />

(0xD6B5) is shifted to <str<strong>on</strong>g>the</str<strong>on</strong>g> left by three places or augmented by three zeros.<br />

0xD6B5 = 1101011010110101 becomes 0x6B5A8 = 1101011010110101000.<br />

Note that <str<strong>on</strong>g>the</str<strong>on</strong>g> original data is still present within <str<strong>on</strong>g>the</str<strong>on</strong>g> augmented data.<br />

0x6B5A8 = 1101011010110101000<br />

Data = D6B5 Augmentati<strong>on</strong> = 000<br />

Calculating <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> result for <str<strong>on</strong>g>the</str<strong>on</strong>g> augmented data (0x6B5A8) using our generator polynomial (1101),<br />

gives a remainder of 101 (degree 2). If we add this to <str<strong>on</strong>g>the</str<strong>on</strong>g> augmented data, we get:<br />

0x6B5A8 + 0b101 = 1101011010110101000 + 101<br />

= 1101011010110101101<br />

= 0x6B5AD<br />

We know from Secti<strong>on</strong> 2.1, “Advantages of <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>Flexis</strong> <strong>AC</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module” that calculating <str<strong>on</strong>g>the</str<strong>on</strong>g> cyclic<br />

redundancy checksum for 0x6B5AD will result in a null checksum, simplifying <str<strong>on</strong>g>the</str<strong>on</strong>g> verificati<strong>on</strong>. What is<br />

less apparent is that <str<strong>on</strong>g>the</str<strong>on</strong>g> original data is still preserved intact.<br />

<strong>Freescale</strong> Semic<strong>on</strong>ductor 7


The <strong>Flexis</strong> <strong>AC</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module<br />

8<br />

0x6B5AD = 1101011010110101101<br />

Data = D6B5 <str<strong>on</strong>g>CRC</str<strong>on</strong>g> = 101<br />

The degree of <str<strong>on</strong>g>the</str<strong>on</strong>g> remainder or cyclic redundancy checksum is always less than <str<strong>on</strong>g>the</str<strong>on</strong>g> degree of <str<strong>on</strong>g>the</str<strong>on</strong>g> generator<br />

polynomial. By augmenting <str<strong>on</strong>g>the</str<strong>on</strong>g> data with a number of zeros equivalent to <str<strong>on</strong>g>the</str<strong>on</strong>g> degree of <str<strong>on</strong>g>the</str<strong>on</strong>g> generator<br />

polynomial, we ensure that <str<strong>on</strong>g>the</str<strong>on</strong>g> additi<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> checksum does not affect <str<strong>on</strong>g>the</str<strong>on</strong>g> augmented data.<br />

In any communicati<strong>on</strong>s system using cyclic redundancy checking, <str<strong>on</strong>g>the</str<strong>on</strong>g> same generator polynomial will be<br />

used by both transmitting and receiving nodes to generate checksums and verify data. As <str<strong>on</strong>g>the</str<strong>on</strong>g> receiving<br />

node knows <str<strong>on</strong>g>the</str<strong>on</strong>g> degree of <str<strong>on</strong>g>the</str<strong>on</strong>g> generator polynomial, it is a simple task for it to verify <str<strong>on</strong>g>the</str<strong>on</strong>g> transmissi<strong>on</strong> by<br />

calculating <str<strong>on</strong>g>the</str<strong>on</strong>g> checksum and testing for zero, and <str<strong>on</strong>g>the</str<strong>on</strong>g>n extract <str<strong>on</strong>g>the</str<strong>on</strong>g> data by discarding <str<strong>on</strong>g>the</str<strong>on</strong>g> last three bits.<br />

Thus augmentati<strong>on</strong> preserves <str<strong>on</strong>g>the</str<strong>on</strong>g> data, whilst allowing a null cyclic redundancy checksum for faster<br />

verificati<strong>on</strong> and data handling.<br />

2.3 <strong>Flexis</strong> <strong>AC</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Implementati<strong>on</strong><br />

Although <str<strong>on</strong>g>the</str<strong>on</strong>g> algorithm for cyclic redundancy calculati<strong>on</strong>s looks complicated, it <strong>on</strong>ly involves shifting and<br />

exclusive OR operati<strong>on</strong>s. <str<strong>on</strong>g>Using</str<strong>on</strong>g> modulo two arithmetic, divisi<strong>on</strong> is just a shift operati<strong>on</strong> and subtracti<strong>on</strong> is<br />

an exclusive OR operati<strong>on</strong>.<br />

Cyclic redundancy calculati<strong>on</strong>s can <str<strong>on</strong>g>the</str<strong>on</strong>g>refore be efficiently implemented in hardware, using a shift register<br />

modified with XOR gates. The shift register should have <str<strong>on</strong>g>the</str<strong>on</strong>g> same number of bits as <str<strong>on</strong>g>the</str<strong>on</strong>g> degree of <str<strong>on</strong>g>the</str<strong>on</strong>g><br />

generator polynomial and an XOR gate at each bit, where <str<strong>on</strong>g>the</str<strong>on</strong>g> generator polynomial coefficient is <strong>on</strong>e.<br />

The <strong>Flexis</strong> <strong>AC</strong> family use a generator polynomial of degree 16; x 16 + x 12 + x 5 + 1. This is often represented<br />

by <str<strong>on</strong>g>the</str<strong>on</strong>g> hexadecimal number 0x1021, where each coefficient in <str<strong>on</strong>g>the</str<strong>on</strong>g> polynomial is represented by it’s<br />

equivalent power of two. As <str<strong>on</strong>g>the</str<strong>on</strong>g> highest coefficient in a polynomial is always <strong>on</strong>e, it is assumed and not<br />

recorded in <str<strong>on</strong>g>the</str<strong>on</strong>g> hexadecimal representati<strong>on</strong>.<br />

The generator polynomial used by <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>Flexis</strong> <strong>AC</strong> family is comm<strong>on</strong>ly known as <str<strong>on</strong>g>CRC</str<strong>on</strong>g>16-CCITT, and is<br />

used in communicati<strong>on</strong> protocols such as CDMA, Bluetooth, and V.41 to name a few.<br />

The hardware implementati<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> cyclic redundancy check using <str<strong>on</strong>g>the</str<strong>on</strong>g> generator polynomial is<br />

represented in <str<strong>on</strong>g>the</str<strong>on</strong>g> block diagram shown in Figure 1.<br />

<str<strong>on</strong>g>Using</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>Flexis</strong> <strong>AC</strong> <strong>Family</strong>, Rev. 0<br />

<strong>Freescale</strong> Semic<strong>on</strong>ductor


Figure 1. Cyclic Redundancy Check (<str<strong>on</strong>g>CRC</str<strong>on</strong>g>) Module Block Diagram<br />

<str<strong>on</strong>g>Using</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>Flexis</strong> <strong>AC</strong> <strong>Family</strong>, Rev. 0<br />

The <strong>Flexis</strong> <strong>AC</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module<br />

It can be seen from <str<strong>on</strong>g>the</str<strong>on</strong>g> block diagram that <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> <str<strong>on</strong>g>module</str<strong>on</strong>g> c<strong>on</strong>tains <strong>on</strong>ly two registers, <str<strong>on</strong>g>CRC</str<strong>on</strong>g> High and<br />

<str<strong>on</strong>g>CRC</str<strong>on</strong>g> Low, which combine to create a 16-Bit <str<strong>on</strong>g>CRC</str<strong>on</strong>g> seed and result register (<str<strong>on</strong>g>CRC</str<strong>on</strong>g>H:L).<br />

At <str<strong>on</strong>g>the</str<strong>on</strong>g> start of any <str<strong>on</strong>g>CRC</str<strong>on</strong>g> calculati<strong>on</strong>, a programmable seed value is written into register <str<strong>on</strong>g>CRC</str<strong>on</strong>g>H:L. <str<strong>on</strong>g>Using</str<strong>on</strong>g> a<br />

programmable seed value allows <str<strong>on</strong>g>the</str<strong>on</strong>g> user to pre-c<strong>on</strong>figure <str<strong>on</strong>g>the</str<strong>on</strong>g> shift register.<br />

The seed value is programmable to allow <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> <str<strong>on</strong>g>module</str<strong>on</strong>g> to meet <str<strong>on</strong>g>the</str<strong>on</strong>g> seed value requirements of different<br />

communicati<strong>on</strong> protocols. The <str<strong>on</strong>g>CRC</str<strong>on</strong>g>16-CCITT standard uses a seed value of 0xFFFF.<br />

At <str<strong>on</strong>g>the</str<strong>on</strong>g> end of a <str<strong>on</strong>g>CRC</str<strong>on</strong>g> calculati<strong>on</strong>, <str<strong>on</strong>g>the</str<strong>on</strong>g> result is stored in <str<strong>on</strong>g>CRC</str<strong>on</strong>g>H:L. The procedure to calculate a <str<strong>on</strong>g>CRC</str<strong>on</strong>g><br />

checksum using <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> <str<strong>on</strong>g>module</str<strong>on</strong>g> is very simple.<br />

Firstly to enable <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> functi<strong>on</strong>, a seed value should be written into <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g>H:L. The seed value is<br />

placed directly into bits 15:0 of <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> generator shift register. Writing a byte to <str<strong>on</strong>g>CRC</str<strong>on</strong>g>H triggers <str<strong>on</strong>g>the</str<strong>on</strong>g> start<br />

of <str<strong>on</strong>g>the</str<strong>on</strong>g> seed mechanism and loads <str<strong>on</strong>g>the</str<strong>on</strong>g> value written to <str<strong>on</strong>g>CRC</str<strong>on</strong>g>H into <str<strong>on</strong>g>the</str<strong>on</strong>g> upper byte of <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> generator shift<br />

register. The <str<strong>on</strong>g>CRC</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g>n requires a write to <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g>L to prime <str<strong>on</strong>g>the</str<strong>on</strong>g> lower byte of <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> generator shift<br />

register, and completes <str<strong>on</strong>g>the</str<strong>on</strong>g> seed mechanism.<br />

Now that <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> seed has been loaded, <str<strong>on</strong>g>the</str<strong>on</strong>g> first byte of <str<strong>on</strong>g>the</str<strong>on</strong>g> data <strong>on</strong> which a <str<strong>on</strong>g>CRC</str<strong>on</strong>g> calculati<strong>on</strong> is to be<br />

performed, can be passed to <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> <str<strong>on</strong>g>module</str<strong>on</strong>g>. This is achieved by writing <str<strong>on</strong>g>the</str<strong>on</strong>g> byte of data to <str<strong>on</strong>g>CRC</str<strong>on</strong>g>L. This<br />

will cause <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> <str<strong>on</strong>g>module</str<strong>on</strong>g> to begin <str<strong>on</strong>g>the</str<strong>on</strong>g> cyclic redundancy calculati<strong>on</strong> by shifting <str<strong>on</strong>g>the</str<strong>on</strong>g> bits in <str<strong>on</strong>g>CRC</str<strong>on</strong>g>L into<br />

<str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> generator shift register, MSB first. This step should be repeated until all of <str<strong>on</strong>g>the</str<strong>on</strong>g> data bytes have<br />

been written to <str<strong>on</strong>g>CRC</str<strong>on</strong>g>L.<br />

<strong>Freescale</strong> Semic<strong>on</strong>ductor 9


The <strong>Flexis</strong> <strong>AC</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module<br />

After each byte has been written, a new <str<strong>on</strong>g>CRC</str<strong>on</strong>g> result can be read from <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g>H:L registers <strong>on</strong>e-bus cycle<br />

after <str<strong>on</strong>g>the</str<strong>on</strong>g> previous write to <str<strong>on</strong>g>CRC</str<strong>on</strong>g>L.<br />

Once all of <str<strong>on</strong>g>the</str<strong>on</strong>g> data bytes have been written to <str<strong>on</strong>g>CRC</str<strong>on</strong>g>L, <str<strong>on</strong>g>the</str<strong>on</strong>g> result of <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> calculati<strong>on</strong> or cyclic<br />

redundancy checksum is available in <str<strong>on</strong>g>CRC</str<strong>on</strong>g>H:L.<br />

The process can be restarted by writing a new seed value to <str<strong>on</strong>g>CRC</str<strong>on</strong>g>H:L, to initiate <str<strong>on</strong>g>the</str<strong>on</strong>g> seed mechanism and<br />

a new <str<strong>on</strong>g>CRC</str<strong>on</strong>g> calculati<strong>on</strong> will begin.<br />

2.4 How to Use <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module<br />

There are increasing requirements for in-applicati<strong>on</strong> tests and checks, to ensure that embedded c<strong>on</strong>trol<br />

software will operate reliably and safely. Examples include:<br />

Checking for bit errors in <str<strong>on</strong>g>the</str<strong>on</strong>g> program code.<br />

Ensuring <str<strong>on</strong>g>the</str<strong>on</strong>g>re is no corrupti<strong>on</strong> of a specific string of data.<br />

The <str<strong>on</strong>g>CRC</str<strong>on</strong>g> <str<strong>on</strong>g>module</str<strong>on</strong>g> offers a means to meet <str<strong>on</strong>g>the</str<strong>on</strong>g>se requirements simply and efficiently, without making<br />

excessive demands <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> CPU or memory resources.<br />

Four simple steps are all that is required to use <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> to verify <str<strong>on</strong>g>the</str<strong>on</strong>g> embedded software.<br />

1. Write <str<strong>on</strong>g>the</str<strong>on</strong>g> seed value into <str<strong>on</strong>g>CRC</str<strong>on</strong>g>H:L.<br />

2. Write <str<strong>on</strong>g>the</str<strong>on</strong>g> data, byte by byte to <str<strong>on</strong>g>CRC</str<strong>on</strong>g>L, until <str<strong>on</strong>g>the</str<strong>on</strong>g> end of <str<strong>on</strong>g>the</str<strong>on</strong>g> data to be checked.<br />

3. Augment <str<strong>on</strong>g>the</str<strong>on</strong>g> data with zeros. This step is dependent up<strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> implementati<strong>on</strong> and is opti<strong>on</strong>al.<br />

4. Verify <str<strong>on</strong>g>the</str<strong>on</strong>g> calculated cyclic redundancy checksum against a stored value.<br />

To illustrate this, an example using <str<strong>on</strong>g>the</str<strong>on</strong>g> MC9S08<strong>AC</strong>128 has been included in this document (See<br />

Secti<strong>on</strong> Appendix A, “Cyclic Redundancy Check Software”). The MC9S08<strong>AC</strong>128 memory map has an<br />

extended address memory, which can <strong>on</strong>ly be accessed through a paging window, unlike o<str<strong>on</strong>g>the</str<strong>on</strong>g>r <strong>Flexis</strong> <strong>AC</strong><br />

family MCUs, which can access all of <str<strong>on</strong>g>the</str<strong>on</strong>g>ir memory directly.<br />

10<br />

<str<strong>on</strong>g>Using</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>Flexis</strong> <strong>AC</strong> <strong>Family</strong>, Rev. 0<br />

<strong>Freescale</strong> Semic<strong>on</strong>ductor


Figure 2. MC9S08<strong>AC</strong>128 Memory Map<br />

<str<strong>on</strong>g>Using</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>Flexis</strong> <strong>AC</strong> <strong>Family</strong>, Rev. 0<br />

The <strong>Flexis</strong> <strong>AC</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module<br />

The HCS08 core architecture limits <str<strong>on</strong>g>the</str<strong>on</strong>g> available CPU addressable space to 64 KB. The addressable space<br />

can be extended to 128 KB using a paging window scheme c<strong>on</strong>trolled by a memory management unit<br />

(MMU). A full descripti<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> memory management unit and <str<strong>on</strong>g>the</str<strong>on</strong>g> paging window scheme is available<br />

in <str<strong>on</strong>g>the</str<strong>on</strong>g> MC9S08<strong>AC</strong>128 data sheet.<br />

The procedures and example software provided for <str<strong>on</strong>g>the</str<strong>on</strong>g> MC9S08<strong>AC</strong>128 use it’s memory management unit<br />

to access <str<strong>on</strong>g>the</str<strong>on</strong>g> data, though <str<strong>on</strong>g>the</str<strong>on</strong>g>y can be readily adapted for use <strong>on</strong> o<str<strong>on</strong>g>the</str<strong>on</strong>g>r <strong>Flexis</strong> <strong>AC</strong> family MCUs.<br />

2.4.1 Seeding <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g><br />

The example calculates a cyclic redundancy checksum for <str<strong>on</strong>g>the</str<strong>on</strong>g> flash memory of <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>AC</strong>128. Writing a seed<br />

value is easily performed as <str<strong>on</strong>g>the</str<strong>on</strong>g> functi<strong>on</strong> init_crcCCITT shown below.<br />

void init_crcCCITT(void)<br />

{<br />

<str<strong>on</strong>g>CRC</str<strong>on</strong>g>H = 0xFF; // <str<strong>on</strong>g>CRC</str<strong>on</strong>g> seeded 0xFFFF<br />

<str<strong>on</strong>g>CRC</str<strong>on</strong>g>L = 0xFF; // <str<strong>on</strong>g>CRC</str<strong>on</strong>g> seeded 0xFFFF<br />

}<br />

As <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> <str<strong>on</strong>g>module</str<strong>on</strong>g> complies with <str<strong>on</strong>g>CRC</str<strong>on</strong>g>16-CCITT, <str<strong>on</strong>g>the</str<strong>on</strong>g> seed value used in <str<strong>on</strong>g>the</str<strong>on</strong>g> example is 0xFFFF, though<br />

o<str<strong>on</strong>g>the</str<strong>on</strong>g>r values can be used to allow <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> <str<strong>on</strong>g>module</str<strong>on</strong>g> to be compatible with different communicati<strong>on</strong><br />

protocols, which use <str<strong>on</strong>g>CRC</str<strong>on</strong>g>16-CCITT. O<str<strong>on</strong>g>the</str<strong>on</strong>g>r comm<strong>on</strong> seed values include 0x102D and 0x0000. If greater<br />

flexibility is required, <str<strong>on</strong>g>the</str<strong>on</strong>g> seed value could be passed to <str<strong>on</strong>g>the</str<strong>on</strong>g> functi<strong>on</strong> as a variable.<br />

<strong>Freescale</strong> Semic<strong>on</strong>ductor 11


The <strong>Flexis</strong> <strong>AC</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module<br />

2.4.2 Writing <str<strong>on</strong>g>the</str<strong>on</strong>g> Data<br />

The functi<strong>on</strong>s crc_flash and update_crcCCITT dem<strong>on</strong>strate how to write <str<strong>on</strong>g>the</str<strong>on</strong>g> data, or in this case <str<strong>on</strong>g>the</str<strong>on</strong>g><br />

embedded software c<strong>on</strong>tained in <str<strong>on</strong>g>the</str<strong>on</strong>g> n<strong>on</strong>-volatile memory of <str<strong>on</strong>g>the</str<strong>on</strong>g> MCU to <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> <str<strong>on</strong>g>module</str<strong>on</strong>g> for inclusi<strong>on</strong> in<br />

<str<strong>on</strong>g>the</str<strong>on</strong>g> checksum calcuati<strong>on</strong>.<br />

A start and stop memory address indicating <str<strong>on</strong>g>the</str<strong>on</strong>g> range of <str<strong>on</strong>g>the</str<strong>on</strong>g> memory to be checked is passed to crc_flash.<br />

The functi<strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g>n accesses <str<strong>on</strong>g>the</str<strong>on</strong>g> data c<strong>on</strong>tents at each locati<strong>on</strong> within <str<strong>on</strong>g>the</str<strong>on</strong>g> specified memory range, and<br />

passes <str<strong>on</strong>g>the</str<strong>on</strong>g> data to <str<strong>on</strong>g>the</str<strong>on</strong>g> functi<strong>on</strong> update_crcCCITT, which in turn writes <str<strong>on</strong>g>the</str<strong>on</strong>g> data to <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> <str<strong>on</strong>g>module</str<strong>on</strong>g> via<br />

register <str<strong>on</strong>g>CRC</str<strong>on</strong>g>L.<br />

There is no difficulty using <str<strong>on</strong>g>the</str<strong>on</strong>g> functi<strong>on</strong> crc_flash should <str<strong>on</strong>g>the</str<strong>on</strong>g> n<strong>on</strong>-volatile memory or <str<strong>on</strong>g>the</str<strong>on</strong>g> embedded<br />

software not be c<strong>on</strong>tained within a single c<strong>on</strong>tinuous block. To perform <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> calculati<strong>on</strong> for all of <str<strong>on</strong>g>the</str<strong>on</strong>g><br />

desired memory locati<strong>on</strong>s, call crc_flash more than <strong>on</strong>ce, each time specifying a different memory range,<br />

until all of <str<strong>on</strong>g>the</str<strong>on</strong>g> n<strong>on</strong>-volatile memory or embedded software has been accounted for.<br />

void crc_flash (l<strong>on</strong>g start_addr, l<strong>on</strong>g stop_addr)<br />

{<br />

/* Put base address of flash into <str<strong>on</strong>g>the</str<strong>on</strong>g> LAP registers */<br />

LAP2 = (byte) (start_addr>>16);<br />

LAP1 = (byte) (start_addr>>8);<br />

LAP0 = (byte) start_addr;<br />

/* increment through <str<strong>on</strong>g>the</str<strong>on</strong>g> memory array passed */<br />

for ( 1; start_addr


2.4.4 Verifying <str<strong>on</strong>g>the</str<strong>on</strong>g> Calculated Checksum<br />

<str<strong>on</strong>g>Using</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>Flexis</strong> <strong>AC</strong> <strong>Family</strong>, Rev. 0<br />

The <strong>Flexis</strong> <strong>AC</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module<br />

Once <str<strong>on</strong>g>the</str<strong>on</strong>g> checksum has been calculated, it is necessary to compare it against a previously stored value.<br />

The functi<strong>on</strong> crc_check provides an example of how this can be achieved. A pointer to <str<strong>on</strong>g>the</str<strong>on</strong>g> memory<br />

locati<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> stored checksum is passed to <str<strong>on</strong>g>the</str<strong>on</strong>g> functi<strong>on</strong>. The calculated value in <str<strong>on</strong>g>CRC</str<strong>on</strong>g>H:L is <str<strong>on</strong>g>the</str<strong>on</strong>g>n<br />

compared against <str<strong>on</strong>g>the</str<strong>on</strong>g> stored value. If <str<strong>on</strong>g>the</str<strong>on</strong>g> two checksums are <str<strong>on</strong>g>the</str<strong>on</strong>g> same, no acti<strong>on</strong> is taken and <str<strong>on</strong>g>the</str<strong>on</strong>g> software<br />

is safe to c<strong>on</strong>tinue executing. If <str<strong>on</strong>g>the</str<strong>on</strong>g>re is a difference, <str<strong>on</strong>g>the</str<strong>on</strong>g> functi<strong>on</strong> sys_error is called, which is used to place<br />

<str<strong>on</strong>g>the</str<strong>on</strong>g> MCU in a safe state.<br />

void crc_check(short *blog)<br />

{<br />

short <str<strong>on</strong>g>CRC</str<strong>on</strong>g> = ((<str<strong>on</strong>g>CRC</str<strong>on</strong>g>H


C<strong>on</strong>clusi<strong>on</strong><br />

In this example <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> check of <str<strong>on</strong>g>the</str<strong>on</strong>g> embedded software is performed at <str<strong>on</strong>g>the</str<strong>on</strong>g> start of <str<strong>on</strong>g>the</str<strong>on</strong>g> applicati<strong>on</strong><br />

program. This will execute each time <str<strong>on</strong>g>the</str<strong>on</strong>g> applicati<strong>on</strong> is started or reset. Alternatively, <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> verificati<strong>on</strong><br />

software could be included in a separate functi<strong>on</strong>, which is called by a periodic interrupt, allowing for<br />

a regular verificati<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> embedded software throughout <str<strong>on</strong>g>the</str<strong>on</strong>g> time that <str<strong>on</strong>g>the</str<strong>on</strong>g> applicati<strong>on</strong> is running.<br />

2.4.6 Calculating a Stored Checksum Value<br />

If <str<strong>on</strong>g>the</str<strong>on</strong>g> cyclic redundancy checking is being used to verify n<strong>on</strong>-volatile memory, <str<strong>on</strong>g>the</str<strong>on</strong>g>n a checksum value can<br />

be calculated prior to final programming by running <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> verificati<strong>on</strong> software without including <str<strong>on</strong>g>the</str<strong>on</strong>g><br />

memory locati<strong>on</strong> where <str<strong>on</strong>g>the</str<strong>on</strong>g> checksum will be stored, and noting <str<strong>on</strong>g>the</str<strong>on</strong>g> calculated value. This value is <str<strong>on</strong>g>the</str<strong>on</strong>g>n<br />

stored in <str<strong>on</strong>g>the</str<strong>on</strong>g> skipped memory locati<strong>on</strong>.<br />

It is important not to include <str<strong>on</strong>g>the</str<strong>on</strong>g> checksum memory locati<strong>on</strong>, when verifying <str<strong>on</strong>g>the</str<strong>on</strong>g> embedded software in an<br />

active applicati<strong>on</strong>. Including <str<strong>on</strong>g>the</str<strong>on</strong>g> stored checksum memory locati<strong>on</strong> in <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> calculati<strong>on</strong> will affect <str<strong>on</strong>g>the</str<strong>on</strong>g><br />

calculated result, causing it to differ from <str<strong>on</strong>g>the</str<strong>on</strong>g> stored checksum. This is because <str<strong>on</strong>g>the</str<strong>on</strong>g> original calculati<strong>on</strong> to<br />

determine <str<strong>on</strong>g>the</str<strong>on</strong>g> value of <str<strong>on</strong>g>the</str<strong>on</strong>g> stored checksum did not take <str<strong>on</strong>g>the</str<strong>on</strong>g> c<strong>on</strong>tents of its memory locati<strong>on</strong> into account.<br />

This has <str<strong>on</strong>g>the</str<strong>on</strong>g> advantage that <str<strong>on</strong>g>the</str<strong>on</strong>g> software used by <str<strong>on</strong>g>the</str<strong>on</strong>g> applicati<strong>on</strong> to verify it’s embedded software can also<br />

be used to determine <str<strong>on</strong>g>the</str<strong>on</strong>g> value of <str<strong>on</strong>g>the</str<strong>on</strong>g> stored checksum.<br />

Returning to <str<strong>on</strong>g>the</str<strong>on</strong>g> example program in Secti<strong>on</strong> A.1, “Main.c,” <str<strong>on</strong>g>the</str<strong>on</strong>g> value of <str<strong>on</strong>g>the</str<strong>on</strong>g> stored checksum can be<br />

readily obtained by running <str<strong>on</strong>g>the</str<strong>on</strong>g> code using a debugger with a breakpoint set at <str<strong>on</strong>g>the</str<strong>on</strong>g> call to functi<strong>on</strong><br />

crc_check.<br />

When <str<strong>on</strong>g>the</str<strong>on</strong>g> program halts at <str<strong>on</strong>g>the</str<strong>on</strong>g> breakpoint, <str<strong>on</strong>g>the</str<strong>on</strong>g> registers <str<strong>on</strong>g>CRC</str<strong>on</strong>g>H:L will c<strong>on</strong>tain <str<strong>on</strong>g>the</str<strong>on</strong>g> calculated checksum,<br />

which can be read out from <str<strong>on</strong>g>the</str<strong>on</strong>g> debugger memory window. This value should be noted and stored in <str<strong>on</strong>g>the</str<strong>on</strong>g><br />

checksum memory locati<strong>on</strong> as a declarati<strong>on</strong> at <str<strong>on</strong>g>the</str<strong>on</strong>g> start of <str<strong>on</strong>g>the</str<strong>on</strong>g> software.<br />

3 C<strong>on</strong>clusi<strong>on</strong><br />

<strong>Freescale</strong>’s <strong>Flexis</strong> <strong>AC</strong> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> <str<strong>on</strong>g>module</str<strong>on</strong>g> represents an evoluti<strong>on</strong>ary step in error detecti<strong>on</strong>, offering improved<br />

performance and enhanced features over a software soluti<strong>on</strong>.<br />

The user is advised to read <str<strong>on</strong>g>the</str<strong>on</strong>g> relevant chapters of <str<strong>on</strong>g>the</str<strong>on</strong>g> latest reference manual for <str<strong>on</strong>g>the</str<strong>on</strong>g> particular <strong>Flexis</strong> <strong>AC</strong><br />

microc<strong>on</strong>troller, to ensure that all of <str<strong>on</strong>g>the</str<strong>on</strong>g> features and operati<strong>on</strong> of <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> <str<strong>on</strong>g>module</str<strong>on</strong>g> have been fully<br />

captured.<br />

4 References<br />

MC9S08<strong>AC</strong>128 Reference Manual www.freescale.com<br />

ITU-T V.41 Recommendati<strong>on</strong>: Code-Independent Error C<strong>on</strong>trol System, available at<br />

http://www.itu.int/publicati<strong>on</strong>s/index.html.<br />

ITU-T X.25 Recommendati<strong>on</strong>: Interface between Data Terminal Equipment (DTE) and Data<br />

Circuit-terminating Equipment (DCE) for terminals operating in <str<strong>on</strong>g>the</str<strong>on</strong>g> packet mode and c<strong>on</strong>nected to public<br />

data networks by dedicated circuit, available at http://www.itu.int/publicati<strong>on</strong>s/index.html.<br />

ITU-T T.30 Recommendati<strong>on</strong>: Procedures for document facsimile transmissi<strong>on</strong> in <str<strong>on</strong>g>the</str<strong>on</strong>g> general switched<br />

teleph<strong>on</strong>e network, available at http://www.itu.int/publicati<strong>on</strong>s/index.html<br />

14<br />

<str<strong>on</strong>g>Using</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>Flexis</strong> <strong>AC</strong> <strong>Family</strong>, Rev. 0<br />

<strong>Freescale</strong> Semic<strong>on</strong>ductor


5 Glossary<br />

<str<strong>on</strong>g>CRC</str<strong>on</strong>g> — cyclic redundancy check<br />

<str<strong>on</strong>g>Using</str<strong>on</strong>g> <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> Module <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>Flexis</strong> <strong>AC</strong> <strong>Family</strong>, Rev. 0<br />

Glossary<br />

<str<strong>on</strong>g>CRC</str<strong>on</strong>g>16 — CCITT — A cyclic redundancy-checking algorithm based <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> polynomial x 16 + x 12 + x 5 + 1.<br />

CHECKSUM — A number that has been calculated as a functi<strong>on</strong> of some data, which can be used to verify<br />

its au<str<strong>on</strong>g>the</str<strong>on</strong>g>nticity.<br />

ITU-T — Internati<strong>on</strong>al Telecommunicati<strong>on</strong> Uni<strong>on</strong> Telecommunicati<strong>on</strong> Standardizati<strong>on</strong> Sector<br />

Appendix A<br />

Cyclic Redundancy Check Software<br />

The code shown within this appendix is <strong>on</strong>ly intended as an example for <str<strong>on</strong>g>the</str<strong>on</strong>g> CodeWarrior compiler and<br />

<str<strong>on</strong>g>the</str<strong>on</strong>g> MC9S08<strong>AC</strong>128. It has <strong>on</strong>ly been given a minimum level of test. It is provided as seen with no<br />

guarantees and promise of support.<br />

A.1 Main.c<br />

#include /* for EnableInterrupts macro */<br />

#include "derivative.h" /* include peripheral declarati<strong>on</strong>s */<br />

l<strong>on</strong>g start1_address ; // start1 address to pass to crc_flash();<br />

l<strong>on</strong>g stop1_address; // end1 address to pass to crc_flash();<br />

l<strong>on</strong>g start2_address ; // start2 address to pass to crc_flash();<br />

l<strong>on</strong>g stop2_address; // end2 address to pass to crc_flash();<br />

/* This 16bit c<strong>on</strong>stant resides in <str<strong>on</strong>g>the</str<strong>on</strong>g> unused vector space outwith <str<strong>on</strong>g>the</str<strong>on</strong>g> main program */<br />

c<strong>on</strong>st short <str<strong>on</strong>g>CRC</str<strong>on</strong>g>16signature @0xFF80 = (0xFBF9);<br />

/* Error Routine If <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> fails <str<strong>on</strong>g>the</str<strong>on</strong>g>n <str<strong>on</strong>g>the</str<strong>on</strong>g> MCU will get locked in here!!!! */<br />

void sys_error(void)<br />

{<br />

for (;;) ;<br />

/* At this point <str<strong>on</strong>g>the</str<strong>on</strong>g> MCU system should be placed in a safe state */<br />

__RESET_WATCHDOG();<br />

}<br />

void main(void) {<br />

EnableInterrupts;<br />

SOPT = 0x53; // Disable Watchdog<br />

/* START OF FLASH <str<strong>on</strong>g>CRC</str<strong>on</strong>g> CHECK *****************************************************************/<br />

/* Test H/W <str<strong>on</strong>g>CRC</str<strong>on</strong>g> engine by<br />

1) Initialise <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> engine with 0xFFFF<br />

2) update <str<strong>on</strong>g>the</str<strong>on</strong>g> <str<strong>on</strong>g>CRC</str<strong>on</strong>g> engine from start_address to stop_address<br />

3) check <str<strong>on</strong>g>the</str<strong>on</strong>g> calculated result compares with expected result<br />

*/<br />

start1_address = 0x00000; // Start of Program code given to 16bit pointer<br />

<strong>Freescale</strong> Semic<strong>on</strong>ductor 15


Glossary<br />

stop1_address = 0x0FF7F; // End of Program code give to 16bit pointer<br />

start2_address = 0x0FF82; // Start of Program code given to 16bit pointer<br />

stop2_address = 0x1FFFF; // End of Program code give to 16bit pointer<br />

init_crcCCITT(); // 0xFFFF seed <str<strong>on</strong>g>CRC</str<strong>on</strong>g> engine<br />

crc_flash (start1_address,stop1_address); // calculate <str<strong>on</strong>g>CRC</str<strong>on</strong>g> signature<br />

crc_flash (start2_address,stop2_address); // calculate <str<strong>on</strong>g>CRC</str<strong>on</strong>g> signature<br />

augment_message_for_crc_16(); // shift 16 0's to comply with CCITT algorithm (this is opti<strong>on</strong>al)<br />

crc_check (&<str<strong>on</strong>g>CRC</str<strong>on</strong>g>16signature); // check calculated <str<strong>on</strong>g>CRC</str<strong>on</strong>g> against <str<strong>on</strong>g>CRC</str<strong>on</strong>g>16signature<br />

/* Send <str<strong>on</strong>g>the</str<strong>on</strong>g> address of <str<strong>on</strong>g>the</str<strong>on</strong>g> c<strong>on</strong>stant <str<strong>on</strong>g>CRC</str<strong>on</strong>g>16signature to this functi<strong>on</strong> */<br />

/* END OF FLASH <str<strong>on</strong>g>CRC</str<strong>on</strong>g> CHECK *****************************************************************/<br />

/* The applicati<strong>on</strong> software can begin from here */<br />

}<br />

16<br />

for(;;) {<br />

__RESET_WATCHDOG(); /* feeds <str<strong>on</strong>g>the</str<strong>on</strong>g> dog */<br />

} /* loop forever */<br />

/* please make sure that you never leave main */<br />

A.2 init_crcCCITT<br />

void init_crcCCITT(void)<br />

{<br />

<str<strong>on</strong>g>CRC</str<strong>on</strong>g>H = 0xFF; // <str<strong>on</strong>g>CRC</str<strong>on</strong>g> seeded 0xFFFF<br />

<str<strong>on</strong>g>CRC</str<strong>on</strong>g>L = 0xFF; // <str<strong>on</strong>g>CRC</str<strong>on</strong>g> seeded 0xFFFF<br />

}<br />

A.3 crc_flash<br />

void crc_flash (l<strong>on</strong>g start_addr, l<strong>on</strong>g stop_addr)<br />

{<br />

/* Put base address of flash into <str<strong>on</strong>g>the</str<strong>on</strong>g> LAP registers */<br />

LAP2 = (byte) (start_addr>>16);<br />

LAP1 = (byte) (start_addr>>8);<br />

LAP0 = (byte) start_addr;<br />

/* increment through <str<strong>on</strong>g>the</str<strong>on</strong>g> memory array passed */<br />

for ( 1; start_addr


A.5 augment_message_for_crc_16()<br />

void augment_message_for_crc_16()<br />

{<br />

<str<strong>on</strong>g>CRC</str<strong>on</strong>g>L=0x00;<br />

<str<strong>on</strong>g>CRC</str<strong>on</strong>g>L=0x00;<br />

}<br />

A.6 crc_check<br />

void crc_check(short *blog)<br />

{<br />

short <str<strong>on</strong>g>CRC</str<strong>on</strong>g> = ((<str<strong>on</strong>g>CRC</str<strong>on</strong>g>H


How to Reach Us:<br />

Home Page:<br />

www.freescale.com<br />

Web Support:<br />

http://www.freescale.com/support<br />

USA/Europe or Locati<strong>on</strong>s Not Listed:<br />

<strong>Freescale</strong> Semic<strong>on</strong>ductor, Inc.<br />

Technical Informati<strong>on</strong> Center, EL516<br />

2100 East Elliot Road<br />

Tempe, Ariz<strong>on</strong>a 85284<br />

+1-800-521-6274 or +1-480-768-2130<br />

www.freescale.com/support<br />

Europe, Middle East, and Africa:<br />

<strong>Freescale</strong> Halbleiter Deutschland GmbH<br />

Technical Informati<strong>on</strong> Center<br />

Schatzbogen 7<br />

81829 Muenchen, Germany<br />

+44 1296 380 456 (English)<br />

+46 8 52200080 (English)<br />

+49 89 92103 559 (German)<br />

+33 1 69 35 48 48 (French)<br />

www.freescale.com/support<br />

Japan:<br />

<strong>Freescale</strong> Semic<strong>on</strong>ductor Japan Ltd.<br />

Headquarters<br />

ARCO Tower 15F<br />

1-8-1, Shimo-Meguro, Meguro-ku,<br />

Tokyo 153-0064<br />

Japan<br />

0120 191014 or +81 3 5437 9125<br />

support.japan@freescale.com<br />

Asia/Pacific:<br />

<strong>Freescale</strong> Semic<strong>on</strong>ductor China Ltd.<br />

Exchange Building 23F<br />

No. 118 Jianguo Road<br />

Chaoyang District<br />

Beijing 100022<br />

China<br />

+86 10 5879 8000<br />

support.asia@freescale.com<br />

For Literature Requests Only:<br />

<strong>Freescale</strong> Semic<strong>on</strong>ductor Literature Distributi<strong>on</strong> Center<br />

1-800-441-2447 or 303-675-2140<br />

Fax: 303-675-2150<br />

LDCFor<strong>Freescale</strong>Semic<strong>on</strong>ductor@hibbertgroup.com<br />

Document Number: AN3795<br />

Rev. 0<br />

04/2009<br />

Informati<strong>on</strong> in this document is provided solely to enable system and software<br />

implementers to use <strong>Freescale</strong> Semic<strong>on</strong>ductor products. There are no express or<br />

implied copyright licenses granted hereunder to design or fabricate any integrated<br />

circuits or integrated circuits based <strong>on</strong> <str<strong>on</strong>g>the</str<strong>on</strong>g> informati<strong>on</strong> in this document.<br />

<strong>Freescale</strong> Semic<strong>on</strong>ductor reserves <str<strong>on</strong>g>the</str<strong>on</strong>g> right to make changes without fur<str<strong>on</strong>g>the</str<strong>on</strong>g>r notice to<br />

any products herein. <strong>Freescale</strong> Semic<strong>on</strong>ductor makes no warranty, representati<strong>on</strong> or<br />

guarantee regarding <str<strong>on</strong>g>the</str<strong>on</strong>g> suitability of its products for any particular purpose, nor does<br />

<strong>Freescale</strong> Semic<strong>on</strong>ductor assume any liability arising out of <str<strong>on</strong>g>the</str<strong>on</strong>g> applicati<strong>on</strong> or use of any<br />

product or circuit, and specifically disclaims any and all liability, including without<br />

limitati<strong>on</strong> c<strong>on</strong>sequential or incidental damages. “Typical” parameters that may be<br />

provided in <strong>Freescale</strong> Semic<strong>on</strong>ductor data sheets and/or specificati<strong>on</strong>s can and do vary<br />

in different applicati<strong>on</strong>s and actual performance may vary over time. All operating<br />

parameters, including “Typicals”, must be validated for each customer applicati<strong>on</strong> by<br />

customer’s technical experts. <strong>Freescale</strong> Semic<strong>on</strong>ductor does not c<strong>on</strong>vey any license<br />

under its patent rights nor <str<strong>on</strong>g>the</str<strong>on</strong>g> rights of o<str<strong>on</strong>g>the</str<strong>on</strong>g>rs. <strong>Freescale</strong> Semic<strong>on</strong>ductor products are<br />

not designed, intended, or authorized for use as comp<strong>on</strong>ents in systems intended for<br />

surgical implant into <str<strong>on</strong>g>the</str<strong>on</strong>g> body, or o<str<strong>on</strong>g>the</str<strong>on</strong>g>r applicati<strong>on</strong>s intended to support or sustain life,<br />

or for any o<str<strong>on</strong>g>the</str<strong>on</strong>g>r applicati<strong>on</strong> in which <str<strong>on</strong>g>the</str<strong>on</strong>g> failure of <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>Freescale</strong> Semic<strong>on</strong>ductor product<br />

could create a situati<strong>on</strong> where pers<strong>on</strong>al injury or death may occur. Should Buyer<br />

purchase or use <strong>Freescale</strong> Semic<strong>on</strong>ductor products for any such unintended or<br />

unauthorized applicati<strong>on</strong>, Buyer shall indemnify and hold <strong>Freescale</strong> Semic<strong>on</strong>ductor and<br />

its officers, employees, subsidiaries, affiliates, and distributors harmless against all<br />

claims, costs, damages, and expenses, and reas<strong>on</strong>able attorney fees arising out of,<br />

directly or indirectly, any claim of pers<strong>on</strong>al injury or death associated with such<br />

unintended or unauthorized use, even if such claim alleges that <strong>Freescale</strong><br />

Semic<strong>on</strong>ductor was negligent regarding <str<strong>on</strong>g>the</str<strong>on</strong>g> design or manufacture of <str<strong>on</strong>g>the</str<strong>on</strong>g> part.<br />

RoHS-compliant and/or Pb-free versi<strong>on</strong>s of <strong>Freescale</strong> products have <str<strong>on</strong>g>the</str<strong>on</strong>g> functi<strong>on</strong>ality<br />

and electrical characteristics as <str<strong>on</strong>g>the</str<strong>on</strong>g>ir n<strong>on</strong>-RoHS-compliant and/or n<strong>on</strong>-Pb-free<br />

counterparts. For fur<str<strong>on</strong>g>the</str<strong>on</strong>g>r informati<strong>on</strong>, see http://www.freescale.com or c<strong>on</strong>tact your<br />

<strong>Freescale</strong> sales representative.<br />

For informati<strong>on</strong> <strong>on</strong> <strong>Freescale</strong>’s Envir<strong>on</strong>mental Products program, go to<br />

http://www.freescale.com/epp.<br />

<strong>Freescale</strong> and <str<strong>on</strong>g>the</str<strong>on</strong>g> <strong>Freescale</strong> logo are trademarks of <strong>Freescale</strong> Semic<strong>on</strong>ductor, Inc.<br />

All o<str<strong>on</strong>g>the</str<strong>on</strong>g>r product or service names are <str<strong>on</strong>g>the</str<strong>on</strong>g> property of <str<strong>on</strong>g>the</str<strong>on</strong>g>ir respective owners.<br />

© <strong>Freescale</strong> Semic<strong>on</strong>ductor, Inc. 2009. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!