EE445M RTOS
Taken at the University of Texas Spring 2015
Sw_crc_api

Macros

#define CRC8_ITER(crc, data)   g_pui8Crc8CCITT[(uint8_t)((crc) ^ (data))]
 
#define CRC16_ITER(crc, data)
 
#define CRC32_ITER(crc, data)
 

Functions

uint8_t Crc8CCITT (uint8_t ui8Crc, const uint8_t *pui8Data, uint32_t ui32Count)
 
uint16_t Crc16 (uint16_t ui16Crc, const uint8_t *pui8Data, uint32_t ui32Count)
 
uint16_t Crc16Array (uint32_t ui32WordLen, const uint32_t *pui32Data)
 
void Crc16Array3 (uint32_t ui32WordLen, const uint32_t *pui32Data, uint16_t *pui16Crc3)
 
uint32_t Crc32 (uint32_t ui32Crc, const uint8_t *pui8Data, uint32_t ui32Count)
 

Variables

static const uint8_t g_pui8Crc8CCITT [256]
 
static const uint16_t g_pui16Crc16 [256]
 
static const uint32_t g_pui32Crc32 []
 

Detailed Description

Macro Definition Documentation

#define CRC16_ITER (   crc,
  data 
)
Value:
(((crc) >> 8) ^ \
g_pui16Crc16[(uint8_t)((crc) ^ (data))])
static const uint16_t g_pui16Crc16[256]
Definition: sw_crc.c:97

Definition at line 220 of file sw_crc.c.

Referenced by Crc16(), and Crc16Array3().

#define CRC32_ITER (   crc,
  data 
)
Value:
(((crc) >> 8) ^ \
g_pui32Crc32[(uint8_t)((crc & 0xFF) ^ \
(data))])
static const uint32_t g_pui32Crc32[]
Definition: sw_crc.c:140

Definition at line 228 of file sw_crc.c.

Referenced by Crc32().

#define CRC8_ITER (   crc,
  data 
)    g_pui8Crc8CCITT[(uint8_t)((crc) ^ (data))]

Definition at line 213 of file sw_crc.c.

Referenced by Crc8CCITT().

Function Documentation

uint16_t Crc16 ( uint16_t  ui16Crc,
const uint8_t *  pui8Data,
uint32_t  ui32Count 
)

Calculates the CRC-16 of an array of bytes.

Parameters
ui16Crcis the starting CRC-16 value.
pui8Datais a pointer to the data buffer.
ui32Countis the number of bytes in the data buffer.

This function is used to calculate the CRC-16 of the input buffer. The CRC-16 is computed in a running fashion, meaning that the entire data block that is to have its CRC-16 computed does not need to be supplied all at once. If the input buffer contains the entire block of data, then ui16Crc should be set to 0. If, however, the entire block of data is not available, then ui16Crc should be set to 0 for the first portion of the data, and then the returned value should be passed back in as ui16Crc for the next portion of the data.

For example, to compute the CRC-16 of a block that has been split into three pieces, use the following:

//!     ui16Crc = Crc16(0, pui8Data1, ui32Len1);
//!     ui16Crc = Crc16(ui16Crc, pui8Data2, ui32Len2);
//!     ui16Crc = Crc16(ui16Crc, pui8Data3, ui32Len3);
//! 
Computing a CRC-16 in a running fashion is useful in cases where the data
is arriving via a serial link (for example) and is therefore not all
available at one time.

\return The CRC-16 of the input data.  

Definition at line 411 of file sw_crc.c.

References CRC16_ITER.

Referenced by Crc16Array().

412 {
413  uint32_t ui32Temp;
414 
415  //
416  // If the data buffer is not 16 bit-aligned, then perform a single step of
417  // the CRC to make it 16 bit-aligned.
418  //
419  if((uint32_t)pui8Data & 1)
420  {
421  //
422  // Perform the CRC on this input byte.
423  //
424  ui16Crc = CRC16_ITER(ui16Crc, *pui8Data);
425 
426  //
427  // Skip this input byte.
428  //
429  pui8Data++;
430  ui32Count--;
431  }
432 
433  //
434  // If the data buffer is not word-aligned and there are at least two bytes
435  // of data left, then perform two steps of the CRC to make it word-aligned.
436  //
437  if(((uint32_t)pui8Data & 2) && (ui32Count > 1))
438  {
439  //
440  // Read the next 16 bits.
441  //
442  ui32Temp = *(uint16_t *)pui8Data;
443 
444  //
445  // Perform the CRC on these two bytes.
446  //
447  ui16Crc = CRC16_ITER(ui16Crc, ui32Temp);
448  ui16Crc = CRC16_ITER(ui16Crc, ui32Temp >> 8);
449 
450  //
451  // Skip these input bytes.
452  //
453  pui8Data += 2;
454  ui32Count -= 2;
455  }
456 
457  //
458  // While there is at least a word remaining in the data buffer, perform
459  // four steps of the CRC to consume a word.
460  //
461  while(ui32Count > 3)
462  {
463  //
464  // Read the next word.
465  //
466  ui32Temp = *(uint32_t *)pui8Data;
467 
468  //
469  // Perform the CRC on these four bytes.
470  //
471  ui16Crc = CRC16_ITER(ui16Crc, ui32Temp);
472  ui16Crc = CRC16_ITER(ui16Crc, ui32Temp >> 8);
473  ui16Crc = CRC16_ITER(ui16Crc, ui32Temp >> 16);
474  ui16Crc = CRC16_ITER(ui16Crc, ui32Temp >> 24);
475 
476  //
477  // Skip these input bytes.
478  //
479  pui8Data += 4;
480  ui32Count -= 4;
481  }
482 
483  //
484  // If there are two bytes left in the input buffer, then perform two steps
485  // of the CRC.
486  //
487  if(ui32Count > 1)
488  {
489  //
490  // Read the two bytes.
491  //
492  ui32Temp = *(uint16_t *)pui8Data;
493 
494  //
495  // Perform the CRC on these two bytes.
496  //
497  ui16Crc = CRC16_ITER(ui16Crc, ui32Temp);
498  ui16Crc = CRC16_ITER(ui16Crc, ui32Temp >> 8);
499 
500  //
501  // Skip these input bytes.
502  //
503  pui8Data += 2;
504  ui32Count -= 2;
505  }
506 
507  //
508  // If there is a final byte remaining in the input buffer, then perform a
509  // single step of the CRC.
510  //
511  if(ui32Count != 0)
512  {
513  ui16Crc = CRC16_ITER(ui16Crc, *pui8Data);
514  }
515 
516  //
517  // Return the resulting CRC-16 value.
518  //
519  return(ui16Crc);
520 }
#define CRC16_ITER(crc, data)
Definition: sw_crc.c:220

Here is the caller graph for this function:

uint16_t Crc16Array ( uint32_t  ui32WordLen,
const uint32_t *  pui32Data 
)

Calculates the CRC-16 of an array of words.

Parameters
ui32WordLenis the length of the array in words (the number of bytes divided by 4).
pui32Datais a pointer to the data buffer.

This function is a wrapper around the running CRC-16 function, providing the CRC-16 for a single block of data.

Returns
The CRC-16 of the input data.

Definition at line 537 of file sw_crc.c.

References Crc16().

538 {
539  //
540  // Calculate and return the CRC-16 of this array of words.
541  //
542  return(Crc16(0, (const uint8_t *)pui32Data, ui32WordLen * 4));
543 }
uint16_t Crc16(uint16_t ui16Crc, const uint8_t *pui8Data, uint32_t ui32Count)
Definition: sw_crc.c:411

Here is the call graph for this function:

void Crc16Array3 ( uint32_t  ui32WordLen,
const uint32_t *  pui32Data,
uint16_t *  pui16Crc3 
)

Calculates three CRC-16s of an array of words.

Parameters
ui32WordLenis the length of the array in words (the number of bytes divided by 4).
pui32Datais a pointer to the data buffer.
pui16Crc3is a pointer to an array in which to place the three CRC-16 values.

This function is used to calculate three CRC-16s of the input buffer; the first uses every byte from the array, the second uses only the even-index bytes from the array (in other words, bytes 0, 2, 4, etc.), and the third uses only the odd-index bytes from the array (in other words, bytes 1, 3, 5, etc.).

Returns
None

Definition at line 565 of file sw_crc.c.

References CRC16_ITER.

567 {
568  uint16_t ui16Crc, ui16Cri8Odd, ui16Cri8Even;
569  uint32_t ui32Temp;
570 
571  //
572  // Initialize the CRC values to zero.
573  //
574  ui16Crc = 0;
575  ui16Cri8Odd = 0;
576  ui16Cri8Even = 0;
577 
578  //
579  // Loop while there are more words in the data buffer.
580  //
581  while(ui32WordLen--)
582  {
583  //
584  // Read the next word.
585  //
586  ui32Temp = *pui32Data++;
587 
588  //
589  // Perform the first CRC on all four data bytes.
590  //
591  ui16Crc = CRC16_ITER(ui16Crc, ui32Temp);
592  ui16Crc = CRC16_ITER(ui16Crc, ui32Temp >> 8);
593  ui16Crc = CRC16_ITER(ui16Crc, ui32Temp >> 16);
594  ui16Crc = CRC16_ITER(ui16Crc, ui32Temp >> 24);
595 
596  //
597  // Perform the second CRC on only the even-index data bytes.
598  //
599  ui16Cri8Even = CRC16_ITER(ui16Cri8Even, ui32Temp);
600  ui16Cri8Even = CRC16_ITER(ui16Cri8Even, ui32Temp >> 16);
601 
602  //
603  // Perform the third CRC on only the odd-index data bytes.
604  //
605  ui16Cri8Odd = CRC16_ITER(ui16Cri8Odd, ui32Temp >> 8);
606  ui16Cri8Odd = CRC16_ITER(ui16Cri8Odd, ui32Temp >> 24);
607  }
608 
609  //
610  // Return the resulting CRC-16 values.
611  //
612  pui16Crc3[0] = ui16Crc;
613  pui16Crc3[1] = ui16Cri8Even;
614  pui16Crc3[2] = ui16Cri8Odd;
615 }
#define CRC16_ITER(crc, data)
Definition: sw_crc.c:220
uint32_t Crc32 ( uint32_t  ui32Crc,
const uint8_t *  pui8Data,
uint32_t  ui32Count 
)

Calculates the CRC-32 of an array of bytes.

Parameters
ui32Crcis the starting CRC-32 value.
pui8Datais a pointer to the data buffer.
ui32Countis the number of bytes in the data buffer.

This function is used to calculate the CRC-32 of the input buffer. The CRC-32 is computed in a running fashion, meaning that the entire data block that is to have its CRC-32 computed does not need to be supplied all at once. If the input buffer contains the entire block of data, then ui32Crc should be set to 0xFFFFFFFF. If, however, the entire block of data is not available, then ui32Crc should be set to 0xFFFFFFFF for the first portion of the data, and then the returned value should be passed back in as ui32Crc for the next portion of the data. Once all data has been passed to the function, the final CRC-32 can be obtained by inverting the last returned value.

For example, to compute the CRC-32 of a block that has been split into three pieces, use the following:

//!     ui32Crc = Crc32(0xFFFFFFFF, pui8Data1, ui32Len1);
//!     ui32Crc = Crc32(ui32Crc, pui8Data2, ui32Len2);
//!     ui32Crc = Crc32(ui32Crc, pui8Data3, ui32Len3);
//!     ui32Crc ^= 0xFFFFFFFF;
//! 
Computing a CRC-32 in a running fashion is useful in cases where the data
is arriving via a serial link (for example) and is therefore not all
available at one time.

\return The accumulated CRC-32 of the input data.  

Definition at line 654 of file sw_crc.c.

References CRC32_ITER.

Referenced by EMACHashFilterBitCalculate(), and EMACVLANHashFilterBitCalculate().

655 {
656  uint32_t ui32Temp;
657 
658  //
659  // If the data buffer is not 16 bit-aligned, then perform a single step
660  // of the CRC to make it 16 bit-aligned.
661  //
662  if((uint32_t)pui8Data & 1)
663  {
664  //
665  // Perform the CRC on this input byte.
666  //
667  ui32Crc = CRC32_ITER(ui32Crc, *pui8Data);
668 
669  //
670  // Skip this input byte.
671  //
672  pui8Data++;
673  ui32Count--;
674  }
675 
676  //
677  // If the data buffer is not word-aligned and there are at least two bytes
678  // of data left, then perform two steps of the CRC to make it word-aligned.
679  //
680  if(((uint32_t)pui8Data & 2) && (ui32Count > 1))
681  {
682  //
683  // Read the next int16_t.
684  //
685  ui32Temp = *(uint16_t *)pui8Data;
686 
687  //
688  // Perform the CRC on these two bytes.
689  //
690  ui32Crc = CRC32_ITER(ui32Crc, ui32Temp);
691  ui32Crc = CRC32_ITER(ui32Crc, ui32Temp >> 8);
692 
693  //
694  // Skip these input bytes.
695  //
696  pui8Data += 2;
697  ui32Count -= 2;
698  }
699 
700  //
701  // While there is at least a word remaining in the data buffer, perform
702  // four steps of the CRC to consume a word.
703  //
704  while(ui32Count > 3)
705  {
706  //
707  // Read the next word.
708  //
709  ui32Temp = *(uint32_t *)pui8Data;
710 
711  //
712  // Perform the CRC on these four bytes.
713  //
714  ui32Crc = CRC32_ITER(ui32Crc, ui32Temp);
715  ui32Crc = CRC32_ITER(ui32Crc, ui32Temp >> 8);
716  ui32Crc = CRC32_ITER(ui32Crc, ui32Temp >> 16);
717  ui32Crc = CRC32_ITER(ui32Crc, ui32Temp >> 24);
718 
719  //
720  // Skip these input bytes.
721  //
722  pui8Data += 4;
723  ui32Count -= 4;
724  }
725 
726  //
727  // If there are 16 bits left in the input buffer, then perform two steps of
728  // the CRC.
729  //
730  if(ui32Count > 1)
731  {
732  //
733  // Read the two bytes.
734  //
735  ui32Temp = *(uint16_t *)pui8Data;
736 
737  //
738  // Perform the CRC on these two bytes.
739  //
740  ui32Crc = CRC32_ITER(ui32Crc, ui32Temp);
741  ui32Crc = CRC32_ITER(ui32Crc, ui32Temp >> 8);
742 
743  //
744  // Skip these input bytes.
745  //
746  pui8Data += 2;
747  ui32Count -= 2;
748  }
749 
750  //
751  // If there is a final byte remaining in the input buffer, then perform a
752  // single step of the CRC.
753  //
754  if(ui32Count != 0)
755  {
756  ui32Crc = CRC32_ITER(ui32Crc, *pui8Data);
757  }
758 
759  //
760  // Return the resulting CRC-32 value.
761  //
762  return(ui32Crc);
763 }
#define CRC32_ITER(crc, data)
Definition: sw_crc.c:228

Here is the caller graph for this function:

uint8_t Crc8CCITT ( uint8_t  ui8Crc,
const uint8_t *  pui8Data,
uint32_t  ui32Count 
)

Calculates the CRC-8-CCITT of an array of bytes.

Parameters
ui8Crcis the starting CRC-8-CCITT value.
pui8Datais a pointer to the data buffer.
ui32Countis the number of bytes in the data buffer.

This function is used to calculate the CRC-8-CCITT of the input buffer. The CRC-8-CCITT is computed in a running fashion, meaning that the entire data block that is to have its CRC-8-CCITT computed does not need to be supplied all at once. If the input buffer contains the entire block of data, then ui8Crc should be set to 0. If, however, the entire block of data is not available, then ui8Crc should be set to 0 for the first portion of the data, and then the returned value should be passed back in as ui8Crc for the next portion of the data.

For example, to compute the CRC-8-CCITT of a block that has been split into three pieces, use the following:

//!     ui8Crc = Crc8CCITT(0, pui8Data1, ui32Len1);
//!     ui8Crc = Crc8CCITT(ui8Crc, pui8Data2, ui32Len2);
//!     ui8Crc = Crc8CCITT(ui8Crc, pui8Data3, ui32Len3);
//! 
Computing a CRC-8-CCITT in a running fashion is useful in cases where the
data is arriving via a serial link (for example) and is therefore not all
available at one time.

\return The CRC-8-CCITT of the input data.  

Definition at line 266 of file sw_crc.c.

References CRC8_ITER.

267 {
268  uint32_t ui32Temp;
269 
270  //
271  // If the data buffer is not 16 bit-aligned, then perform a single step of
272  // the CRC to make it 16 bit-aligned.
273  //
274  if((uint32_t)pui8Data & 1)
275  {
276  //
277  // Perform the CRC on this input byte.
278  //
279  ui8Crc = CRC8_ITER(ui8Crc, *pui8Data);
280 
281  //
282  // Skip this input byte.
283  //
284  pui8Data++;
285  ui32Count--;
286  }
287 
288  //
289  // If the data buffer is not word-aligned and there are at least two bytes
290  // of data left, then perform two steps of the CRC to make it word-aligned.
291  //
292  if(((uint32_t)pui8Data & 2) && (ui32Count > 1))
293  {
294  //
295  // Read the next 16 bits.
296  //
297  ui32Temp = *(uint16_t *)pui8Data;
298 
299  //
300  // Perform the CRC on these two bytes.
301  //
302  ui8Crc = CRC8_ITER(ui8Crc, ui32Temp);
303  ui8Crc = CRC8_ITER(ui8Crc, ui32Temp >> 8);
304 
305  //
306  // Skip these input bytes.
307  //
308  pui8Data += 2;
309  ui32Count -= 2;
310  }
311 
312  //
313  // While there is at least a word remaining in the data buffer, perform
314  // four steps of the CRC to consume a word.
315  //
316  while(ui32Count > 3)
317  {
318  //
319  // Read the next word.
320  //
321  ui32Temp = *(uint32_t *)pui8Data;
322 
323  //
324  // Perform the CRC on these four bytes.
325  //
326  ui8Crc = CRC8_ITER(ui8Crc, ui32Temp);
327  ui8Crc = CRC8_ITER(ui8Crc, ui32Temp >> 8);
328  ui8Crc = CRC8_ITER(ui8Crc, ui32Temp >> 16);
329  ui8Crc = CRC8_ITER(ui8Crc, ui32Temp >> 24);
330 
331  //
332  // Skip these input bytes.
333  //
334  pui8Data += 4;
335  ui32Count -= 4;
336  }
337 
338  //
339  // If there are 16 bits left in the input buffer, then perform two steps of
340  // the CRC.
341  //
342  if(ui32Count > 1)
343  {
344  //
345  // Read the 16 bits.
346  //
347  ui32Temp = *(uint16_t *)pui8Data;
348 
349  //
350  // Perform the CRC on these two bytes.
351  //
352  ui8Crc = CRC8_ITER(ui8Crc, ui32Temp);
353  ui8Crc = CRC8_ITER(ui8Crc, ui32Temp >> 8);
354 
355  //
356  // Skip these input bytes.
357  //
358  pui8Data += 2;
359  ui32Count -= 2;
360  }
361 
362  //
363  // If there is a final byte remaining in the input buffer, then perform a
364  // single step of the CRC.
365  //
366  if(ui32Count != 0)
367  {
368  ui8Crc = CRC8_ITER(ui8Crc, *pui8Data);
369  }
370 
371  //
372  // Return the resulting CRC-8-CCITT value.
373  //
374  return(ui8Crc);
375 }
#define CRC8_ITER(crc, data)
Definition: sw_crc.c:213

Variable Documentation

const uint16_t g_pui16Crc16[256]
static

Definition at line 97 of file sw_crc.c.

const uint32_t g_pui32Crc32[]
static

Definition at line 140 of file sw_crc.c.

const uint8_t g_pui8Crc8CCITT[256]
static

Definition at line 55 of file sw_crc.c.