diff options
Diffstat (limited to 'projects/bootloader')
-rw-r--r-- | projects/bootloader/Makefile | 2 | ||||
-rw-r--r-- | projects/bootloader/crc32.c | 62 | ||||
-rw-r--r-- | projects/bootloader/dfu.c | 13 | ||||
-rw-r--r-- | projects/bootloader/log.c | 68 |
4 files changed, 76 insertions, 69 deletions
diff --git a/projects/bootloader/Makefile b/projects/bootloader/Makefile index 7de93c0..fe96982 100644 --- a/projects/bootloader/Makefile +++ b/projects/bootloader/Makefile @@ -1,6 +1,6 @@ PROG = bootloader -OBJS = crc32.o dfu.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); diff --git a/projects/bootloader/log.c b/projects/bootloader/log.c new file mode 100644 index 0000000..c0d9df4 --- /dev/null +++ b/projects/bootloader/log.c @@ -0,0 +1,68 @@ +/* + * log.c + * ----- + * Implement libhal logging API on Alpha. + * + * Copyright (c) 2017, NORDUnet A/S All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of the NORDUnet nor the names of its contributors may + * be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdio.h> +#include <stdarg.h> + +#define HAL_OK CMSIS_HAL_OK +#include "stm-uart.h" +#undef HAL_OK + +#define HAL_OK LIBHAL_OK +#include "hal.h" +#include "hal_internal.h" +#undef HAL_OK + +static hal_log_level_t current_log_level; + +void hal_log_set_level(const hal_log_level_t level) +{ + current_log_level = level; +} + +void hal_log(const hal_log_level_t level, const char *format, ...) +{ + if (level < current_log_level) + return; + + char buffer[2048]; + va_list ap; + + va_start(ap, format); + vsnprintf(buffer, sizeof(buffer), format, ap); + va_end(ap); + + uart_send_string2(STM_UART_MGMT, buffer); + uart_send_string2(STM_UART_MGMT, "\r\n"); +} |