0% found this document useful (0 votes)
53 views19 pages

Erriez CRC32

This document describes an optimized CRC32 library for Arduino. It provides functions for calculating CRC32 checksums on strings, single buffers, and multiple buffers in an efficient manner without using large CRC tables. The library has a small code size, requires no RAM allocation, and works on various Arduino platforms and microcontrollers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views19 pages

Erriez CRC32

This document describes an optimized CRC32 library for Arduino. It provides functions for calculating CRC32 checksums on strings, single buffers, and multiple buffers in an efficient manner without using large CRC tables. The library has a small code size, requires no RAM allocation, and works on various Arduino platforms and microcontrollers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Erriez CRC32 library for Arduino

1.0.1

Generated by Doxygen 1.8.13


Contents

1 Optimized CRC32 library for Arduino 1

2 File Index 7

2.1 File List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

3 File Documentation 9

3.1 src/ErriezCRC32.c File Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

3.1.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

3.1.2 Function Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

3.1.2.1 crc32Buffer() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

3.1.2.2 crc32Final() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

3.1.2.3 crc32String() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

3.1.2.4 crc32Update() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

3.2 src/ErriezCRC32.h File Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

3.2.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

3.2.2 Function Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

3.2.2.1 crc32Buffer() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

3.2.2.2 crc32Final() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

3.2.2.3 crc32String() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

3.2.2.4 crc32Update() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

Index 15
Chapter 1

Optimized CRC32 library for Arduino

This is a target independent, flash and RAM size optimized CRC32 library for Arduino without CRC tables.

Library features

• Compatible with standard CRC32 algorithms

• Small flash footprint

• No RAM buffer allocations

• 32-bit table-less CRC32 calculation on:

– Arduino String object


– Single null-terminated character array
– Single buffer
– Multiple null-terminated character arrays
– Multiple buffers

• No buffer alignment needed

• Buffer input length in Bytes

• Big and little endian input buffers

• Arduino C library, compatible with C and C++ applications

• CRC32 polynomial: 0xEDB88320

Hardware

This library can be used on any 8 or 32-bit target and is optimized for small flash/RAM footprints without CRC table
allocations in RAM.
2 Optimized CRC32 library for Arduino

Examples and benchmarks

Arduino IDE | Examples | Erriez CRC32:

• ErriezCRC32

• ErriezCRC32Benchmark

Benchmark results on buffers

The benchmark results below may be different for each target and run. Lower duration means faster CRC calcula-
tion.

Generated by Doxygen
3

Board F_CPU 4 Bytes 64 Bytes 512 Bytes 1024 Bytes


Arduino UNO (ATMega328) 16MHz 80us 1172us 9000us 17112us
ESP8266 (WeMOS D1 R2) 160MHz 3us 35us 279us 557us
ESP32 (WeMOS Lolin32) 80MHz 2us 15us 111us 223us
DUE (SAM3X8E ARM Cortex-M3) 84MHz 6us 70us 547us 1103us

Benchmarks performed with the following versions:

• Arduino IDE v1.8.5

• Arduino AVR Boards v1.6.21 (Arduino UNO)

• esp8266 v2.4.2

• Arduino SAM Boards (32-bits ARM Cortex-M3) v1.6.11

Unit tests

This library contains extensive unit tests on a various number of buffer types and lengths:

• ErriezCRC32UnitTest

Library documentation

• Doxygen online HTML

• Doxygen PDF

Usage

Introduction

The library consists of four C-functions for CRC calculation:

{c++}
// Calculate CRC32 on null-terminated character array (string)
uint32_t crc32String(const char *buffer);
// Calculate CRC32 on one single buffer
uint32_t crc32Buffer(const void *buffer, size_t bufferLength);

// Calculate CRC32 on multiple buffers


// First crc32Update() call should start with argument crc = CRC32_INITIAL
// Next crc32Update() calls should set argument crc = previousCRC
uint32_t crc32Update(const void *buffer, size_t bufferLength, uint32_t crc);
// Call crc32Update() at the end after the last crc32Update()
uint32_t crc32Final(uint32_t crc);

Initialization

Only an include file is required. No object allocation or library initialization required.

{c++}
#include <ErriezCRC32.h>

Generated by Doxygen
4 Optimized CRC32 library for Arduino

Calculate CRC32 on Arduino String


{c++}
String msg = "Hello world String!";
uint32_t crc = crc32String(msg.c_str());

Calculate CRC32 on null-terminated character array


{c++}
char msg[] = "Hello world char array!";
uint32_t crc = crc32String(msg);

Calculate CRC32 on single character buffer


{c++}
char msg[] = "Hello world single buffer!";
uint32_t crc = crc32Buffer(msg, strlen(msg));

Calculate CRC32 on multiple characters


{c++}
uint32_t crc;

crc = crc32Update("Hello ", 6, CRC32_INITIAL);


crc = crc32Update("world ", 6, crc);
crc = crc32Update("multiple ", 9, crc);
crc = crc32Update("buffers!", 8, crc);
crc = crc32Final(crc);

Calculate CRC32 on multiple buffers


{c++}
const uint8_t testBuffer1[3] = { 0xEB, 0xE5, 0x51 };
const uint8_t testBuffer2[5] = { 0x87, 0x7F, 0xB8, 0x18, 0x4E };
uint32_t crc;

crc = crc32Update(testBuffer1, sizeof(testBuffer1), CRC32_INITIAL);


crc = crc32Update(testBuffer2, sizeof(testBuffer2), crc);
crc = crc32Final(crc);

Check CRC
{c++}
void checkCRC(uint32_t crcCalculated, uint32_t crcExpected)
{
printCRC(crcCalculated);
Serial.print(F("..."));

if (crcCalculated == crcExpected) {
Serial.println(F("OK"));
} else {
Serial.print(F("FAILED! Expected: "));
printCRC(crcExpected);
Serial.println(F(""));
}
}

Print 32-bit CRC value


Printing a uint32_t value is not implemented on some targets which is a limitation of Serial.print() or
sprintf(). This is a workaround to print a 32-bit hex value:
{c++}
void printCRC(uint32_t crc)
{
Serial.print("0x");
for (int8_t i = 3; i >= 0; i--) {
uint8_t c = crc >> (i * 8);
if (c < 0x10) {
Serial.print("0");
}
Serial.print(c, HEX);
}
}

CRC-calculation with Python


>>> import binascii
>>> hex(binascii.crc32(b’Hello world String!’) & 0xffffffff)
’0x55df869b’

Generated by Doxygen
5

Library dependencies

• ErriezCRC32Benchmark.ino: ErriezTimestamp.h

• ErriezCRC32UnitTest.ino: ArduinoUnit.h

Library installation

Please refer to the Wiki page.

Other Arduino Libraries and Sketches from Erriez

• Erriez Libraries and Sketches

Generated by Doxygen
6 Optimized CRC32 library for Arduino

Generated by Doxygen
Chapter 2

File Index

2.1 File List

Here is a list of all documented files with brief descriptions:

src/ErriezCRC32.c
Flash size optimized CRC32 library for Arduino without using CRC tables . . . . . . . . . . . 9
src/ErriezCRC32.h
CRC32 library for Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
8 File Index

Generated by Doxygen
Chapter 3

File Documentation

3.1 src/ErriezCRC32.c File Reference

Flash size optimized CRC32 library for Arduino without using CRC tables.

#include <string.h>
#include "ErriezCRC32.h"
Include dependency graph for ErriezCRC32.c:

src/ErriezCRC32.c

string.h ErriezCRC32.h

inttypes.h stddef.h

Functions

• uint32_t crc32String (const char ∗buffer)


Calculate CRC on a character string.
• uint32_t crc32Buffer (const void ∗buffer, size_t bufferLength)
• uint32_t crc32Update (const void ∗buffer, size_t bufferLength, uint32_t crc)
Calculate CRC on multiple buffers.
• uint32_t crc32Final (uint32_t crc)
Finalize CRC32 output.
10 File Documentation

3.1.1 Detailed Description

Flash size optimized CRC32 library for Arduino without using CRC tables.

Source: https://github.com/Erriez/ErriezCRC32 Documentation: https://erriez.←-


github.io/ErriezCRC32

3.1.2 Function Documentation

3.1.2.1 crc32Buffer()

uint32_t crc32Buffer (
const void ∗ buffer,
size_t bufferLength )

This function is thread safe and maybe called from interrupt handlers. No buffer alignment needed.

Parameters

buffer Input buffer.


bufferLength Buffer length in Bytes.

Returns

32-bit CRC value.

Definition at line 66 of file ErriezCRC32.c.

3.1.2.2 crc32Final()

uint32_t crc32Final (
uint32_t crc )

Finalize CRC32 output.

This function should be called after crc32Update(). This function is thread safe and maybe called from interrupt
handlers. No buffer alignment needed.

Parameters

crc Previous CRC32 value.

Generated by Doxygen
3.1 src/ErriezCRC32.c File Reference 11

Returns

32-bit CRC value.

Definition at line 113 of file ErriezCRC32.c.

3.1.2.3 crc32String()

uint32_t crc32String (
const char ∗ buffer )

Calculate CRC on a character string.

This function is thread safe and maybe called from interrupt handlers. No buffer alignment needed.

Parameters

buffer Null terminated character input array.

Returns

32-bit CRC value.

Definition at line 49 of file ErriezCRC32.c.

3.1.2.4 crc32Update()

uint32_t crc32Update (
const void ∗ buffer,
size_t bufferLength,
uint32_t crc )

Calculate CRC on multiple buffers.

This function is thread safe and maybe called from interrupt handlers. No buffer alignment needed.

Parameters

buffer Input buffer. No alignment needed.


bufferLength Input buffer length in Bytes. No alignment needed.
crc Previous CRC value. Call with crc = CRC32_INITIAL on first calculation.

Returns

32-bit updated CRC value.

Definition at line 85 of file ErriezCRC32.c.

Generated by Doxygen
12 File Documentation

3.2 src/ErriezCRC32.h File Reference

CRC32 library for Arduino.

#include <inttypes.h>
#include <stddef.h>
Include dependency graph for ErriezCRC32.h:

src/ErriezCRC32.h

inttypes.h stddef.h

This graph shows which files directly or indirectly include this file:

src/ErriezCRC32.h

src/ErriezCRC32.c

Macros
• #define CRC32_INITIAL 0xFFFFFFFFUL
CRC32 initial value.
• #define CRC32_POLYNOMIAL 0xEDB88320UL
CRC32 polynomial.

Functions
• uint32_t crc32String (const char ∗buffer)
Calculate CRC on a character string.
• uint32_t crc32Buffer (const void ∗buffer, size_t bufferLength)
• uint32_t crc32Update (const void ∗buffer, size_t bufferLength, uint32_t crc)
Calculate CRC on multiple buffers.
• uint32_t crc32Final (uint32_t crc)
Finalize CRC32 output.

Generated by Doxygen
3.2 src/ErriezCRC32.h File Reference 13

3.2.1 Detailed Description

CRC32 library for Arduino.

Source: https://github.com/Erriez/ErriezCRC32 Documentation: https://erriez.←-


github.io/ErriezCRC32

3.2.2 Function Documentation

3.2.2.1 crc32Buffer()

uint32_t crc32Buffer (
const void ∗ buffer,
size_t bufferLength )

This function is thread safe and maybe called from interrupt handlers. No buffer alignment needed.

Parameters

buffer Input buffer.


bufferLength Buffer length in Bytes.

Returns

32-bit CRC value.

Definition at line 66 of file ErriezCRC32.c.

3.2.2.2 crc32Final()

uint32_t crc32Final (
uint32_t crc )

Finalize CRC32 output.

This function should be called after crc32Update(). This function is thread safe and maybe called from interrupt
handlers. No buffer alignment needed.

Parameters

crc Previous CRC32 value.

Generated by Doxygen
14 File Documentation

Returns

32-bit CRC value.

Definition at line 113 of file ErriezCRC32.c.

3.2.2.3 crc32String()

uint32_t crc32String (
const char ∗ buffer )

Calculate CRC on a character string.

This function is thread safe and maybe called from interrupt handlers. No buffer alignment needed.

Parameters

buffer Null terminated character input array.

Returns

32-bit CRC value.

Definition at line 49 of file ErriezCRC32.c.

3.2.2.4 crc32Update()

uint32_t crc32Update (
const void ∗ buffer,
size_t bufferLength,
uint32_t crc )

Calculate CRC on multiple buffers.

This function is thread safe and maybe called from interrupt handlers. No buffer alignment needed.

Parameters

buffer Input buffer. No alignment needed.


bufferLength Input buffer length in Bytes. No alignment needed.
crc Previous CRC value. Call with crc = CRC32_INITIAL on first calculation.

Returns

32-bit updated CRC value.

Definition at line 85 of file ErriezCRC32.c.

Generated by Doxygen
Index

crc32Buffer
ErriezCRC32.c, 10
ErriezCRC32.h, 13
crc32Final
ErriezCRC32.c, 10
ErriezCRC32.h, 13
crc32String
ErriezCRC32.c, 11
ErriezCRC32.h, 14
crc32Update
ErriezCRC32.c, 11
ErriezCRC32.h, 14

ErriezCRC32.c
crc32Buffer, 10
crc32Final, 10
crc32String, 11
crc32Update, 11
ErriezCRC32.h
crc32Buffer, 13
crc32Final, 13
crc32String, 14
crc32Update, 14

src/ErriezCRC32.c, 9
src/ErriezCRC32.h, 12

You might also like