Www Netrino Com

9
CRC Implementation Code in C Embedded C/C++  Programming Algorithms by Michael Barr CRCs are among the best checksums available to detect and/or correct errors in communications transmissions. Unfortunately, the modulo-2 arithmetic used to compute CRCs doesn't map easily into software. This article shows how to implement an efficient CRC in C. I'm going to complete my discussion of checksums by showing you how to implement CRCs in software. I'll start with a naïve implementation and gradually improve the efficiency of the code as I go along. However, I'm going to keep the discussion at the level of the C language, so further steps could be taken to improve the efficiency of the final code simply by moving into the assembly language of your particular processor. For most software engineers, the overwhelmingly confusing thing about CRCs is their  implementation. Knowing that all CRC algorithms are simply long division algorithms in disguise doesn't help. Modulo-2 binary division doesn't map particularly well to the instruction sets of off-the-shelf processors. For one thing, generally no registers are available to hold the very long bit sequence that is the numerator. For another, modulo-2 binary division is not the same as ordinary division. So even if your processor has a division instruction, you won't be able to use it. Modulo-2 binary division Before writing even one line of code, let's first examine the mechanics of modulo-2 binary division. We'll use the example in Figure 1 to guide us. The number to be divided is the message augmented with zeros at the end. The number of zero bits added to the message is the same as the width of the checksum (what I call c); in this case four bits were added. The divisor is a c+1-bit number known as the generator polynomial. Figure 1. An example of modulo-2 binary division The modulo-2 division process is defined as follows: Call the uppermost c+1 bits of the message the remainder  Beginning with the most significant bit in the original message and for each bit position that follows, look at the c+1 bit remainder: If the most significant bit of the remainder is a one, the divisor is said to divide into it. If  that happens (just as in any other long division) it is necessary to indicate a successful division in the appropriate bit position in the quotient and to compute the new remainder. In the case of modulo-2 binary division, we simply: Set the appropriate bit in the quotient to a one, and Glossary Find definitions for technical terms in our  . Take a Quiz T est your embedded programmin g skills in our on li ne  or and be entered to win a free seat a t a future public . Master Firmware Registration is now open for  the popular hands-on . Consult our  fo r upcoming dates and locations. Sign Up Now Receive Michael Barr's Firmware Update newsletter for  free how-to articles and industry news by e-mail. Articles Embedded Systems Glossary A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Sy mb o l s E mbedded C Quiz Embedded C++ Quiz Embedded Software Boot Camp Embedded Software Boot C amp public training calendar Sign up now. C at e ori es Th e next public session of the workshop is set for . Space is limited. E mbedded Sof t ware B oot C amp Oct ober 1 8- 22 in Maryland Register now. H ome Services C ourses How T o Client s A bout U s Cont act Us Login converted by Web2PDFConvert.com

Transcript of Www Netrino Com

8/8/2019 Www Netrino Com

http://slidepdf.com/reader/full/www-netrino-com 1/8

8/8/2019 Www Netrino Com

http://slidepdf.com/reader/full/www-netrino-com 2/8

8/8/2019 Www Netrino Com

http://slidepdf.com/reader/full/www-netrino-com 3/8

8/8/2019 Www Netrino Com

http://slidepdf.com/reader/full/www-netrino-com 4/8

8/8/2019 Www Netrino Com

http://slidepdf.com/reader/full/www-netrino-com 5/8

8/8/2019 Www Netrino Com

http://slidepdf.com/reader/full/www-netrino-com 6/8

8/8/2019 Www Netrino Com

http://slidepdf.com/reader/full/www-netrino-com 7/8

8/8/2019 Www Netrino Com

http://slidepdf.com/reader/full/www-netrino-com 8/8