aboutsummaryrefslogtreecommitdiff
path: root/projects/bootloader
diff options
context:
space:
mode:
authorRob Austein <sra@hactrn.net>2017-04-16 15:55:14 -0400
committerRob Austein <sra@hactrn.net>2017-04-16 15:55:14 -0400
commitbb6d9e870fb51833137e82063a3ab08930820a2d (patch)
tree91501087fdfa7536909dd8cdff4d7e1d0929e7e9 /projects/bootloader
parenta2e32d326c342a965b733c9a42cfd1568b2afed4 (diff)
Switch to libhal's CRC-32 code.
Diffstat (limited to 'projects/bootloader')
-rw-r--r--projects/bootloader/Makefile2
-rw-r--r--projects/bootloader/crc32.c62
-rw-r--r--projects/bootloader/dfu.c13
3 files changed, 8 insertions, 69 deletions
diff --git a/projects/bootloader/Makefile b/projects/bootloader/Makefile
index 8415c69..fe96982 100644
--- a/projects/bootloader/Makefile
+++ b/projects/bootloader/Makefile
@@ -1,6 +1,6 @@
PROG = bootloader
-OBJS = crc32.o dfu.o log.o
+OBJS = dfu.o log.o
BOARD_OBJS = \
./stm-init.o \
diff --git a/projects/bootloader/crc32.c b/projects/bootloader/crc32.c
deleted file mode 100644
index 4d1a0bc..0000000
--- a/projects/bootloader/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/bootloader/dfu.c b/projects/bootloader/dfu.c
index f4a9cf9..a0ff372 100644
--- a/projects/bootloader/dfu.c
+++ b/projects/bootloader/dfu.c
@@ -47,8 +47,6 @@
#include <string.h>
-extern uint32_t update_crc(uint32_t crc, uint8_t *buf, int len);
-
static int getline(char *buf, int len)
{
int i;
@@ -111,8 +109,9 @@ static int do_login(void)
int dfu_receive_firmware(void)
{
- uint32_t filesize = 0, crc = 0, my_crc = 0, counter = 0;
uint32_t offset = DFU_FIRMWARE_ADDR, n = DFU_UPLOAD_CHUNK_SIZE;
+ hal_crc32_t crc = 0, my_crc = hal_crc32_init();
+ uint32_t filesize = 0, counter = 0;
uint8_t buf[DFU_UPLOAD_CHUNK_SIZE];
if (do_login() != 0)
@@ -133,7 +132,7 @@ int dfu_receive_firmware(void)
uart_send_string2(STM_UART_MGMT, "OK, write size (4 bytes), data in 4096 byte chunks, CRC-32 (4 bytes)\r\n");
/* Read file size (4 bytes) */
- uart_receive_bytes(STM_UART_MGMT, (void *) &filesize, 4, 10000);
+ uart_receive_bytes(STM_UART_MGMT, (void *) &filesize, sizeof(filesize), 10000);
if (filesize < 512 || filesize > DFU_FIRMWARE_END_ADDR - DFU_FIRMWARE_ADDR) {
uart_send_string2(STM_UART_MGMT, "Invalid filesize ");
uart_send_number2(STM_UART_MGMT, filesize, 1, 10);
@@ -165,7 +164,7 @@ int dfu_receive_firmware(void)
/* After reception of a chunk but before ACKing we have "all" the time in the world to
* calculate CRC and write it to flash.
*/
- my_crc = update_crc(my_crc, buf, n);
+ my_crc = hal_crc32_update(my_crc, buf, n);
stm_flash_write32(offset, (uint32_t *)buf, sizeof(buf)/4);
offset += DFU_UPLOAD_CHUNK_SIZE;
@@ -175,12 +174,14 @@ int dfu_receive_firmware(void)
led_toggle(LED_BLUE);
}
+ my_crc = hal_crc32_finalize(my_crc);
+
HAL_FLASH_Lock();
uart_send_string2(STM_UART_MGMT, "Send CRC-32\r\n");
/* The sending side will now send its calculated CRC-32 */
- uart_receive_bytes(STM_UART_MGMT, (void *) &crc, 4, 10000);
+ uart_receive_bytes(STM_UART_MGMT, (void *) &crc, sizeof(crc), 10000);
uart_send_string2(STM_UART_MGMT, "CRC-32 0x");
uart_send_number2(STM_UART_MGMT, crc, 1, 16);