2023-10-31 07:47:27 +01:00
|
|
|
#include "system/task_crc.h"
|
|
|
|
#include "system/data_channel.h"
|
|
|
|
#include "system/float_word.h"
|
2023-12-02 23:50:26 +01:00
|
|
|
//#include <zlib.h> /* c-library for crc32() */
|
|
|
|
#include <stddef.h> /* for "size_t" */
|
2023-12-05 11:09:00 +01:00
|
|
|
#include <stdio.h>
|
2023-12-02 23:50:26 +01:00
|
|
|
|
2023-12-05 11:09:00 +01:00
|
|
|
#if 0
|
|
|
|
#define PRINT_DEBUG_VALUES
|
|
|
|
#endif
|
2023-12-02 23:50:26 +01:00
|
|
|
|
2023-12-05 11:09:00 +01:00
|
|
|
|
|
|
|
/* used basic algo from and modified it: https://lxp32.github.io/docs/a-simple-example-crc32-calculation/*/
|
|
|
|
uint32_t crc32(uint32_t *crc_res, const char *s,size_t n)
|
|
|
|
{
|
|
|
|
uint32_t crc = ~(*crc_res); // always invert the input!
|
|
|
|
|
|
|
|
#ifdef PRINT_DEBUG_VALUES
|
|
|
|
printf("SIZE OF SEED, INPUT & OUTPUT: %i\n", n);
|
|
|
|
printf("SEED: %08x\n", *crc_res);
|
|
|
|
printf("INPUT: %08x\n", *(uint32_t*)s);
|
|
|
|
#endif /*PRINT_DEBUG_VALUES*/
|
|
|
|
|
|
|
|
for(size_t i=0;i<n;++i)
|
2023-12-02 23:50:26 +01:00
|
|
|
{
|
2023-12-05 11:09:00 +01:00
|
|
|
unsigned char ch=s[i];
|
|
|
|
|
|
|
|
#ifdef PRINT_DEBUG_VALUES
|
|
|
|
printf("ch: %02x\n", ch);
|
|
|
|
#endif /*PRINT_DEBUG_VALUES*/
|
|
|
|
|
|
|
|
for(size_t j=0;j<8;++j) {
|
2023-12-02 23:50:26 +01:00
|
|
|
uint32_t b=(ch^crc)&1;
|
|
|
|
crc>>=1;
|
2023-12-05 11:09:00 +01:00
|
|
|
//if(b) crc=crc^0x04C11DB7; // Polynomial representations: NORMAL
|
|
|
|
if(b) crc=crc^0xEDB88320; // Polynomial representations: REVERSED
|
2023-12-02 23:50:26 +01:00
|
|
|
ch>>=1;
|
|
|
|
}
|
|
|
|
}
|
2023-12-05 11:09:00 +01:00
|
|
|
|
|
|
|
#ifdef PRINT_DEBUG_VALUES
|
|
|
|
printf("OUTPUT: %08x\n", crc);
|
|
|
|
printf("~OUTPUT: %08x\n", ~crc);
|
|
|
|
getchar(); // just here for debugging step by step
|
|
|
|
#endif /*PRINT_DEBUG_VALUES*/
|
|
|
|
|
2023-12-02 23:50:26 +01:00
|
|
|
return ~crc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-10-31 07:47:27 +01:00
|
|
|
|
2023-12-05 11:09:00 +01:00
|
|
|
int task_crc_run( void * task )
|
|
|
|
{
|
2023-10-31 07:47:27 +01:00
|
|
|
// TODO
|
|
|
|
|
2023-11-28 11:12:37 +01:00
|
|
|
crc_config * crc = ( crc_config * ) task;
|
|
|
|
uint32_t data_channel_base = crc->base.sink;
|
|
|
|
data_channel_clear( data_channel_base);
|
2023-12-05 11:09:00 +01:00
|
|
|
|
2023-11-28 11:12:37 +01:00
|
|
|
float_word crc_res;
|
|
|
|
float_word crc_input;
|
|
|
|
|
2023-12-05 11:09:00 +01:00
|
|
|
crc_res.word = crc->start;
|
|
|
|
|
2023-11-28 11:12:37 +01:00
|
|
|
for ( uint32_t i = 0; i < DATA_CHANNEL_DEPTH; ++i )
|
|
|
|
{
|
2023-12-02 23:50:26 +01:00
|
|
|
data_channel_read(crc->base.sources[0], (uint32_t*)&crc_input.value);
|
2023-12-05 11:09:00 +01:00
|
|
|
crc_res.word = crc32(&crc_res.word,(const void *)&crc_input, sizeof(crc_input));
|
2023-11-28 11:12:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
data_channel_write( data_channel_base, crc_res.word );
|
|
|
|
|
2023-10-31 07:47:27 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|