From bb6d9e870fb51833137e82063a3ab08930820a2d Mon Sep 17 00:00:00 2001 From: Rob Austein Date: Sun, 16 Apr 2017 15:55:14 -0400 Subject: Switch to libhal's CRC-32 code. --- projects/cli-test/Makefile | 1 - projects/cli-test/crc32.c | 62 --------------------------------------- projects/cli-test/mgmt-dfu.c | 2 -- projects/cli-test/mgmt-keystore.c | 6 ++-- projects/cli-test/mgmt-misc.c | 30 +++++++++++-------- 5 files changed, 21 insertions(+), 80 deletions(-) delete mode 100644 projects/cli-test/crc32.c (limited to 'projects/cli-test') diff --git a/projects/cli-test/Makefile b/projects/cli-test/Makefile index 1f7faf1..22c8133 100644 --- a/projects/cli-test/Makefile +++ b/projects/cli-test/Makefile @@ -1,7 +1,6 @@ TEST = cli-test OBJS = \ - crc32.o \ mgmt-cli.o \ mgmt-dfu.o \ mgmt-fpga.o \ diff --git a/projects/cli-test/crc32.c b/projects/cli-test/crc32.c deleted file mode 100644 index 4d1a0bc..0000000 --- a/projects/cli-test/crc32.c +++ /dev/null @@ -1,62 +0,0 @@ -/* 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); -} diff --git a/projects/cli-test/mgmt-dfu.c b/projects/cli-test/mgmt-dfu.c index 851c8ea..5c9b4b7 100644 --- a/projects/cli-test/mgmt-dfu.c +++ b/projects/cli-test/mgmt-dfu.c @@ -45,8 +45,6 @@ #define DFU_UPLOAD_CHUNK_SIZE 256 #define HARDWARE_EARLY_DFU_JUMP 0xBADABADA -extern uint32_t update_crc(uint32_t crc, uint8_t *buf, int len); - /* Linker symbols are strange in C. Make regular pointers for sanity. */ __IO uint32_t *dfu_control = &CRYPTECH_DFU_CONTROL; __IO uint32_t *dfu_firmware = &CRYPTECH_FIRMWARE_START; diff --git a/projects/cli-test/mgmt-keystore.c b/projects/cli-test/mgmt-keystore.c index 6e26d6d..e11ef76 100644 --- a/projects/cli-test/mgmt-keystore.c +++ b/projects/cli-test/mgmt-keystore.c @@ -33,17 +33,17 @@ */ /* Rename both CMSIS HAL_OK and libhal HAL_OK to disambiguate */ + #define HAL_OK CMSIS_HAL_OK #include "stm-init.h" #include "stm-keystore.h" #include "stm-fpgacfg.h" #include "stm-uart.h" - #include "mgmt-cli.h" #include "mgmt-show.h" - #undef HAL_OK -#define LIBHAL_OK HAL_OK + +#define HAL_OK LIBHAL_OK #include "hal.h" #warning Really should not be including hal_internal.h here, fix API instead of bypassing it #include "hal_internal.h" diff --git a/projects/cli-test/mgmt-misc.c b/projects/cli-test/mgmt-misc.c index b7b4fcc..7db08f2 100644 --- a/projects/cli-test/mgmt-misc.c +++ b/projects/cli-test/mgmt-misc.c @@ -32,28 +32,32 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#define HAL_OK CMSIS_HAL_OK #include "stm-init.h" #include "stm-uart.h" - #include "mgmt-cli.h" #include "mgmt-misc.h" +#undef HAL_OK -#include - +#define HAL_OK LIBHAL_OK +#include "hal.h" +#include "hal_internal.h" +#undef HAL_OK -extern uint32_t update_crc(uint32_t crc, uint8_t *buf, int len); +#include -static volatile uint32_t demo_crc = 0; +static volatile hal_crc32_t demo_crc; static int _count_bytes_callback(uint8_t *buf, size_t len) { - demo_crc = update_crc(demo_crc, buf, len); + demo_crc = hal_crc32_update(demo_crc, buf, len); return 1; } int cli_receive_data(struct cli_def *cli, uint8_t *buf, size_t len, cli_data_callback data_callback) { - uint32_t filesize = 0, crc = 0, my_crc = 0, counter = 0; + hal_crc32_t crc = 0, my_crc = hal_crc32_init(); + uint32_t filesize = 0, counter = 0; size_t n = len; if (! control_mgmt_uart_dma_rx(DMA_RX_STOP)) { @@ -63,7 +67,7 @@ int cli_receive_data(struct cli_def *cli, uint8_t *buf, size_t len, cli_data_cal cli_print(cli, "OK, write size (4 bytes), data in %li byte chunks, CRC-32 (4 bytes)", (uint32_t) n); - if (uart_receive_bytes(STM_UART_MGMT, (void *) &filesize, 4, 1000) != HAL_OK) { + if (uart_receive_bytes(STM_UART_MGMT, (void *) &filesize, sizeof(filesize), 1000) != CMSIS_HAL_OK) { cli_print(cli, "Receive timed out"); goto fail; } @@ -78,12 +82,12 @@ int cli_receive_data(struct cli_def *cli, uint8_t *buf, size_t len, cli_data_cal if (filesize < n) n = filesize; - if (uart_receive_bytes(STM_UART_MGMT, (void *) buf, n, 1000) != HAL_OK) { + if (uart_receive_bytes(STM_UART_MGMT, (void *) buf, n, 1000) != CMSIS_HAL_OK) { cli_print(cli, "Receive timed out"); goto fail; } filesize -= n; - my_crc = update_crc(my_crc, buf, n); + my_crc = hal_crc32_update(my_crc, buf, n); /* After reception of a chunk but before ACKing we have "all" the time in the world to * calculate CRC and invoke the data_callback. @@ -97,8 +101,9 @@ int cli_receive_data(struct cli_def *cli, uint8_t *buf, size_t len, cli_data_cal uart_send_bytes(STM_UART_MGMT, (void *) &counter, 4); } + my_crc = hal_crc32_finalize(my_crc); cli_print(cli, "Send CRC-32"); - uart_receive_bytes(STM_UART_MGMT, (void *) &crc, 4, 1000); + uart_receive_bytes(STM_UART_MGMT, (void *) &crc, sizeof(crc), 1000); cli_print(cli, "CRC-32 0x%x, calculated CRC 0x%x", (unsigned int) crc, (unsigned int) my_crc); if (crc == my_crc) { cli_print(cli, "CRC checksum MATCHED"); @@ -115,8 +120,9 @@ static int cmd_filetransfer(struct cli_def *cli, const char *command, char *argv { uint8_t buf[FILETRANSFER_UPLOAD_CHUNK_SIZE]; - demo_crc = 0; + demo_crc = hal_crc32_init(); cli_receive_data(cli, &buf[0], sizeof(buf), _count_bytes_callback); + demo_crc = hal_crc32_finalize(demo_crc); cli_print(cli, "Demo CRC is: %li/0x%x", demo_crc, (unsigned int) demo_crc); return CLI_OK; } -- cgit v1.2.3