Alarm Sensor Module  0.1
⇒ back to Project Page
crc.c
Go to the documentation of this file.
1 /****************************************************************************/
31 #include <stdint.h>
32 #include "crc.h"
33 
34 
35 /****************************************************************************/
68 /****************************************************************************/
69 
70 uint32_t calculate_crc32(uint8_t *buffer, uint8_t number_bytes, uint8_t mode) {
71 
72  static uint32_t crc = CRC_START_VALUE;
73  uint8_t i, j;
74 
75  //do a initialization if requested
76  if ((mode == CRC_INITIALIZATION) || (mode == CRC_COMPLETE)) {
77  crc = CRC_START_VALUE;
78  }
79 
80  //do a updating if requested
81  if ((mode == CRC_UPDATE) || (mode == CRC_COMPLETE)) {
82 
83  //for each data byte
84  for (i = 0; i < number_bytes; i++) {
85 
86  //for each bit
87  for (j = 0; j < 8; j++) {
88 
89  if ((crc & 0x01u) != ((buffer[i] >> j) & 0x01u)) {
90  crc = ((crc >> 1) ^ CRC_REV_POLYNOM);
91  } else {
92  crc = (crc >> 1);
93  }
94 
95  }
96 
97  }
98 
99  }
100 
101  //do a finalization if requested
102  if ((mode == CRC_FINALIZATION) || (mode == CRC_COMPLETE)) {
103  crc = (crc ^ CRC_START_VALUE);
104  }
105 
106  return crc;
107 
108 
109 }
110 
111 
#define CRC_UPDATE
do only an updating and return the current crc value
Definition: crc.h:53
Contains CRC calculation functions.
#define CRC_START_VALUE
used as start value for CRC and for finalizing
Definition: crc.h:49
#define CRC_COMPLETE
perform a complete calculation of the crc value
Definition: crc.h:55
#define CRC_FINALIZATION
do only the finalization and return the crc value
Definition: crc.h:54
#define CRC_INITIALIZATION
do only the initialization and return the init value
Definition: crc.h:52
#define CRC_REV_POLYNOM
reverse polynom (used also in STM32) (0b11101101101110001000001100100000)
Definition: crc.h:47
uint32_t calculate_crc32(uint8_t *buffer, uint8_t number_bytes, uint8_t mode)
Definition: crc.c:70