Commit c2495187 authored by Reto Da Forno's avatar Reto Da Forno
Browse files

crc functions moved into one file and some math functions added

parent c1346f9c
/*
* Copyright (c) 2017 - 2021, ETH Zurich, Computer Engineering Group (TEC)
* Copyright (c) 2017 - 2022, ETH Zurich, Computer Engineering Group (TEC)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
......@@ -28,19 +28,66 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdint.h>
#include "dpp_lib.h"
/*---------------------------------------------------------------------------*/
/* CRC-8-Dallas/Maxim, based on: http://stackoverflow.com/questions/29214301/ios-how-to-calculate-crc-8-dallas-maxim-of-nsdata */
uint8_t crc8(const uint8_t* data, uint32_t num_bytes, uint8_t init_val)
{
const uint16_t poly = 0x131;
uint32_t crc = init_val;
while (num_bytes) {
crc ^= *data;
uint32_t bit = 8;
while (bit) {
if (crc & 0x80) {
crc = (crc << 1) ^ poly;
} else {
crc <<= 1;
}
bit--;
}
data++;
num_bytes--;
}
return (uint8_t)crc;
}
/*---------------------------------------------------------------------------*/
/* CRC-16-IBM, CRC-16-ANSI */
uint16_t crc16(const uint8_t* data, uint16_t num_bytes, uint16_t init_val)
{
const uint16_t poly = 0xa001;
uint16_t crc = init_val;
while (num_bytes) {
uint16_t ch = *data;
uint16_t bit = 8;
while (bit) {
if ((crc & 1) ^ (ch & 1)) {
crc = (crc >> 1) ^ poly;
} else {
crc >>= 1;
}
ch >>= 1;
bit--;
}
data++;
num_bytes--;
}
return crc;
}
/*---------------------------------------------------------------------------*/
uint32_t crc32(const uint8_t* data, uint32_t num_bytes, uint32_t seed)
uint32_t crc32(const uint8_t* data, uint32_t num_bytes, uint32_t init_val)
{
uint32_t crc = ~seed;
const uint32_t poly = 0xedb88320;
uint32_t crc = ~init_val;
while (num_bytes) {
crc ^= (*data);
uint16_t i = 8;
while (i) {
crc ^= (*data);
uint32_t bit = 8;
while (bit) {
uint32_t mask = -(crc & 1);
crc = (crc >> 1) ^ (0xedb88320 & mask);
i--;
crc = (crc >> 1) ^ (poly & mask);
bit--;
}
data++;
num_bytes--;
......
......@@ -58,6 +58,18 @@
static fifo_meta_t fifo_name##_fifo_info = { 0, 0, element_size, num_elements, fifo_name##_fifo_buffer }; \
fifo_t fifo_name = &fifo_name##_fifo_info
#define SWAP_VALUES(a, b, tmp) tmp = a; a = b; b = tmp
#ifndef ABS
#define ABS(x) (((x) < 0) ? (-(x)) : (x))
#endif
#ifndef MIN
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#endif
#ifndef MAX
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#endif
/* --- typedefs --- */
......@@ -95,8 +107,9 @@ typedef struct list_meta {
/* --- function prototypes --- */
uint16_t crc16(const uint8_t* data, uint16_t num_bytes, uint16_t init_value);
uint32_t crc32(const uint8_t* data, uint32_t num_bytes, uint32_t seed);
uint8_t crc8(const uint8_t* data, uint32_t num_bytes, uint8_t init_val);
uint16_t crc16(const uint8_t* data, uint16_t num_bytes, uint16_t init_val);
uint32_t crc32(const uint8_t* data, uint32_t num_bytes, uint32_t init_val);
uint32_t hexstr_to_uint32(const char* str);
uint32_t uint16_to_str(uint16_t val, char* out_buffer);
......@@ -129,4 +142,7 @@ uint32_t fifo_get_free_space(const fifo_t fifo);
void bubble_sort_uint8(uint8_t* a, uint16_t len);
uint32_t gcd(uint32_t a, uint32_t b);
uint32_t lcm(uint32_t a, uint32_t b);
#endif /* __DPP_LIB_H__ */
/*
* Copyright (c) 2017 - 2021, ETH Zurich, Computer Engineering Group (TEC)
* Copyright (c) 2017 - 2022, ETH Zurich, Computer Engineering Group (TEC)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
......@@ -28,28 +28,33 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdint.h>
#include "dpp_lib.h"
/*---------------------------------------------------------------------------*/
/* CRC-16-IBM, CRC-16-ANSI */
uint16_t crc16(const uint8_t* data, uint16_t num_bytes, uint16_t init_value)
/* Greatest common divisor (Euclidean algorithm) */
uint32_t gcd(uint32_t a, uint32_t b)
{
uint16_t crc = init_value;
while(num_bytes) {
uint16_t ch = *data;
uint16_t bit = 8;
while(bit) {
if((crc & 1) ^ (ch & 1)) {
crc = (crc >> 1) ^ 0xa001; /* mask is 0xa001*/
} else {
crc >>= 1;
}
ch >>= 1;
bit--;
}
data++;
num_bytes--;
if (a == 0 || b == 0) {
return 0;
}
if (a < b) {
unsigned int tmp;
SWAP_VALUES(a, b, tmp);
}
unsigned int r = a % b;
while (r) {
a = b;
b = r;
r = a % b;
}
return crc;
return b;
}
/*---------------------------------------------------------------------------*/
/* Least (lowest) common multiple */
uint32_t lcm(uint32_t a, uint32_t b)
{
uint32_t div = gcd(a, b);
return (div ? (a * b / div) : 0);
}
/*---------------------------------------------------------------------------*/
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment