Studentenversion des ESY6/A Praktikums "signal_processing".
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

crc.c 1.7KB

1 year ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
11 months ago
1 year ago
1 year ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "system/task_crc.h"
  2. #include "system/data_channel.h"
  3. #include "system/float_word.h"
  4. //#include <zlib.h> /* c-library for crc32() */
  5. #include <stddef.h> /* for "size_t" */
  6. #include <stdio.h>
  7. #if 0
  8. #define PRINT_DEBUG_VALUES
  9. #endif
  10. /* used basic algo from and modified it: https://lxp32.github.io/docs/a-simple-example-crc32-calculation/*/
  11. uint32_t crc32(uint32_t *crc_res, const char *s,size_t n)
  12. {
  13. uint32_t crc = ~(*crc_res); // always invert the input!
  14. #ifdef PRINT_DEBUG_VALUES
  15. printf("SIZE OF SEED, INPUT & OUTPUT: %i\n", n);
  16. printf("SEED: %08x\n", *crc_res);
  17. printf("INPUT: %08x\n", *(uint32_t*)s);
  18. #endif /*PRINT_DEBUG_VALUES*/
  19. for(size_t i=0;i<n;++i)
  20. {
  21. unsigned char ch=s[i];
  22. #ifdef PRINT_DEBUG_VALUES
  23. printf("ch: %02x\n", ch);
  24. #endif /*PRINT_DEBUG_VALUES*/
  25. for(size_t j=0;j<8;++j) {
  26. uint32_t b=(ch^crc)&1;
  27. crc>>=1;
  28. //if(b) crc=crc^0x04C11DB7; // Polynomial representations: NORMAL
  29. if(b) crc=crc^0xEDB88320; // Polynomial representations: REVERSED
  30. ch>>=1;
  31. }
  32. }
  33. #ifdef PRINT_DEBUG_VALUES
  34. printf("OUTPUT: %08x\n", crc);
  35. printf("~OUTPUT: %08x\n", ~crc);
  36. getchar(); // just here for debugging step by step
  37. #endif /*PRINT_DEBUG_VALUES*/
  38. return ~crc;
  39. }
  40. int task_crc_run( void * task )
  41. {
  42. // TODO
  43. crc_config * crc = ( crc_config * ) task;
  44. uint32_t data_channel_base = crc->base.sink;
  45. data_channel_clear( data_channel_base);
  46. float_word crc_res;
  47. float_word crc_input;
  48. crc_res.word = crc->start;
  49. for ( uint32_t i = 0; i < DATA_CHANNEL_DEPTH; ++i )
  50. {
  51. data_channel_read(crc->base.sources[0], (uint32_t*)&crc_input.value);
  52. crc_res.word = crc32(&crc_res.word,(const void *)&crc_input, sizeof(crc_input));
  53. }
  54. data_channel_write( data_channel_base, crc_res.word );
  55. return 0;
  56. }