48 lines
1.1 KiB
C
48 lines
1.1 KiB
C
#include "system/task_crc.h"
|
|
#include "system/data_channel.h"
|
|
#include "system/float_word.h"
|
|
|
|
#define CRC32_POLY_REVERSED 0xEDB88320u
|
|
|
|
// CRC32-Update über ein komplettes 32-bit Wort (LSB-first)
|
|
static inline uint32_t crc32_update_word(uint32_t crc, uint32_t word)
|
|
{
|
|
for (int i = 0; i < 4; i++)
|
|
{
|
|
uint8_t b = (word >> (8 * i)) & 0xFF; // LSB first!
|
|
crc ^= b;
|
|
for (int j = 0; j < 8; j++)
|
|
{
|
|
if (crc & 1)
|
|
crc = (crc >> 1) ^ CRC32_POLY_REVERSED;
|
|
else
|
|
crc >>= 1;
|
|
}
|
|
}
|
|
return crc;
|
|
}
|
|
|
|
int task_crc_run(void *task)
|
|
{
|
|
crc_config *config = (crc_config*)task;
|
|
|
|
// WICHTIG: Startwert invertieren (wie im PDF Seite 14 beschrieben)
|
|
uint32_t crc = ~config->start; // = ~0xFFFFFFFF = 0x00000000
|
|
|
|
for (uint32_t i = 0; i < DATA_CHANNEL_DEPTH; i++)
|
|
{
|
|
uint32_t word;
|
|
data_channel_read(config->base.sources[0], &word);
|
|
crc = crc32_update_word(crc, word);
|
|
}
|
|
|
|
// Endwert invertieren (zlib-Standard)
|
|
crc = ~crc;
|
|
|
|
// Ergebnis schreiben
|
|
data_channel_write(config->base.sink, crc);
|
|
|
|
return 0;
|
|
}
|
|
|