aboutsummaryrefslogblamecommitdiff
path: root/projects/cli-test/crc32.c
blob: 4d1a0bcb6001d592fb3c0f2ba770adddfbba9c48 (plain) (tree)





























































                                                                          
/* Reference code from RFC1952. Not meant to be used outside test code. */

#include "stm32f4xx_hal.h"


/* Table of CRCs of all 8-bit messages. */
unsigned long crc_table[256];

/* Flag: has the table been computed? Initially false. */
int crc_table_computed = 0;

/* Make the table for a fast CRC. */
void make_crc_table(void)
{
    unsigned long c;

    int n, k;
    for (n = 0; n < 256; n++) {
	c = (unsigned long) n;
	for (k = 0; k < 8; k++) {
	    if (c & 1) {
		c = 0xedb88320L ^ (c >> 1);
	    } else {
		c = c >> 1;
	    }
	}
	crc_table[n] = c;
    }
    crc_table_computed = 1;
}

/*
  Update a running crc with the bytes buf[0..len-1] and return
  the updated crc. The crc should be initialized to zero. Pre- and
  post-conditioning (one's complement) is performed within this
  function so it shouldn't be done by the caller. Usage example:

  unsigned long crc = 0L;

  while (read_buffer(buffer, length) != EOF) {
  crc = update_crc(crc, buffer, length);
  }
  if (crc != original_crc) error();
*/
uint32_t update_crc(uint32_t crc, uint8_t *buf, int len)
{
    unsigned long c = crc ^ 0xffffffffL;
    int n;

    if (!crc_table_computed)
	make_crc_table();
    for (n = 0; n < len; n++) {
	c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8);
    }
    return c ^ 0xffffffffL;
}

/* Return the CRC of the bytes buf[0..len-1]. */
unsigned long crc(unsigned char *buf, int len)
{
    return update_crc(0L, buf, len);
}
pan class="p">, hal_error_string(err)); return 1; } if (data != rnd) { printf("Data bus fail: expected %08lx, got %08lx, diff %08lx\n", (unsigned long) rnd, (unsigned long) data, (unsigned long) (data ^ rnd)); return 1; } return 0; } /* * Time a test. */ static void _time_check(char *label, const struct timeval t0, const int err) { struct timeval t; float rounds; gettimeofday(&t, NULL); t.tv_sec -= t0.tv_sec; t.tv_usec -= t0.tv_usec; if (t.tv_usec < 0) { t.tv_usec += 1000000; t.tv_sec -= 1; } rounds = (float)TEST_NUM_ROUNDS/((float)t.tv_sec + ((float)t.tv_usec / 1000000)); printf("%s%lu.%06lu seconds, %u/sec\n", label, (unsigned long)t.tv_sec, (unsigned long)t.tv_usec, (unsigned)rounds); } #define time_check(_label_, _expr_) \ do { \ struct timeval _t; \ gettimeofday(&_t, NULL); \ int _err = (_expr_); \ _time_check(_label_, _t, _err); \ err |= _err; \ } while (0) /* * Read and write over and over again. */ static int test_read(const hal_core_t *board_core) { uint32_t i, data; hal_error_t err; for (i = 0; i < TEST_NUM_ROUNDS; ++i) { if ((err = hal_io_read(board_core, 0xFF, (uint8_t *) &data, sizeof(data))) != HAL_OK) { printf("reading dummy: %s\n", hal_error_string(err)); return 1; } } return 0; } static int test_write(const hal_core_t *board_core) { uint32_t i; hal_error_t err; for (i = 0; i < TEST_NUM_ROUNDS; ++i) { if ((err = hal_io_write(board_core, 0xFF, (const uint8_t *) &i, sizeof(i))) != HAL_OK) { printf("writing dummy: %s\n", hal_error_string(err)); return 1; } } return 0; } int main(void) { const hal_core_t *board_core = hal_core_find(NOVENA_BOARD_NAME, NULL); int err = 0; if (sanity(board_core) != 0) return 1; time_check("read ", test_read(board_core)); time_check("write ", test_write(board_core)); return err; }