2024-12-04 11:19:36 +01:00

120 lines
3.3 KiB
C

#include "system/task_crc.h"
#include "system/data_channel.h"
#include "system/float_word.h"
#include <stdio.h>
// Funktion zur Berechnung des CRC32
void berechne_crc32(uint32_t data, uint32_t * crc);
void crc32( uint32_t data, uint32_t* crc );
int task_crc_run( void * task ) {
crc_config * config = (crc_config*) task;
uint32_t value = config->start;
// Nachfolgende Antworten Lesen den FIFO der ersten Datenquelle aus und multiplizieren
// den jeweils gelesenen Wert mit 4 und speichern das Ergebnis in der Datensenke
for (uint32_t i = 0; i < DATA_CHANNEL_DEPTH; ++i) {
float_word a;
data_channel_read(config->base.sources[0], (uint32_t * ) & a.value );
if (i<10) {printf("\n Input %i DATA; %x\ CRC: %x\n", i, a.word, value);}
berechne_crc32(a.word, &value); //Startwert jedes Mal, oder mit vorheriger crc starten?
if (i<10) {printf("\n CRC Zwischenwert Output(%i): %x\n", i, value);}
}
printf("CRC Wert: %x\n", value);
data_channel_write( config->base.sink, value);
return 0;
}
// TODO
// Funktion zur Berechnung des CRC32
void crc32( uint32_t data, uint32_t* crc ) {
uint32_t byte_array[4];
uint32_t reg32 = ~*crc;
byte_array[3] = (uint32_t) (data>>24) & 0x000000FF;
byte_array[2] = (uint32_t) (data>>16) & 0x000000FF;
byte_array[1] = (uint32_t) (data>>8) & 0x000000FF;
byte_array[0] = (uint32_t) (data) & 0x000000FF;
for ( uint32_t i = 0; i < 4; ++i ) {
for ( uint8_t j = 0; j < 8; ++j ) {
if ((reg32&1) != (byte_array[i]&1)) { // != hat Prio 7, & hat Prio 8!
reg32 = (reg32>>1)^CRC32POLY;
}
else {
reg32 >>= 1;
}
byte_array[i] >>= 1;
}
*crc= reg32 ^ 0xffffffff; //inverses Ergebnis, MSB zuerst
}
}
void berechne_crc32(uint32_t data, uint32_t * crc) {
uint32_t bytes[4]; //Reihenfolge richtig?
uint32_t reg32 = ~*crc;
bytes[0] = (uint32_t) (data) & 0x000000FF;
bytes[1] = (uint32_t) (data >>8) & 0x000000FF;
bytes[2] = (uint32_t) (data >>16) & 0x000000FF;
bytes[3] = (uint32_t) (data >>24) & 0x000000FF; //aufffüllen
for (uint32_t i = 0; i < 4; i++) {
//von rechts nch links
for (uint8_t j = 0; j <8; j++) {
// Prüfen, ob das am weitesten rechts stehende Bit von crc 1 ist
if ((reg32 & 1) != (bytes[i] & 1)) { //Wieso crc & 1?
reg32 = (reg32 >> 1)^CRC32POLY; // XOR mit dem Polynom
} else {
reg32 >>= 1;
}
bytes[i]>>=1;
}
*crc = reg32 ^0xFFFFFFFF;
}
}
/*
crc := 0000… (Startwert)
für alle Bits b im Datenstrom:
wenn das am weitesten links stehende Bit von crc 1 ist:
crc := (crc * 2 + b) xor CRC-Polynom
sonst:
crc := crc * 2 + b
crc enthält das Ergebnis.
*/
/*
for (uint32_t j = 0; j < 32; j++) {
if (a.word & (1<<31)) {
c.word = (c.word * 2 + ((a.word & (1<<j))>>j)) ^ CRC32POLY;
} else {
c.word = (c.word * 2 + ((a.word & (1<<j))>>j));
}
}
*/
/*
const uint8_t bitstream[] = { 1,0,0,0,1,1,0,0 };
const int bitcount = 8;
uint32_t crc32 = 0; // Schieberegister
int main ()
{
for (int i = 0; i < bitcount; i++)
{
if ( ((crc32 >> 31) & 1) != bitstream[i])
crc32 = (crc32 << 1) ^ CRC32POLY;
else
crc32 = (crc32 << 1);
}
printf ("0x%08X\n", crc32);
}
*/