From 27dc37713b699f5fb616903c09312c9408beac52 Mon Sep 17 00:00:00 2001 From: Bernd Herzog Date: Sat, 17 Feb 2024 10:38:09 +0100 Subject: [PATCH] added crc32 command to usb shell (#1911) --- .../usb_serial_shell_filesystem.cpp | 33 +++++++++++++++++++ .../usb_serial_shell_filesystem.hpp | 6 ++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/firmware/application/usb_serial_shell_filesystem.cpp b/firmware/application/usb_serial_shell_filesystem.cpp index 821c3019..db3231fd 100644 --- a/firmware/application/usb_serial_shell_filesystem.cpp +++ b/firmware/application/usb_serial_shell_filesystem.cpp @@ -26,6 +26,8 @@ #include "string_format.hpp" #include +#include "crc.hpp" + static File* shell_file = nullptr; static bool report_on_error(BaseSequentialStream* chp, File::Error& error) { @@ -371,3 +373,34 @@ void cmd_sd_write_binary(BaseSequentialStream* chp, int argc, char* argv[]) { chprintf(chp, "ok\r\n"); } + +void cmd_sd_crc32(BaseSequentialStream* chp, int argc, char* argv[]) { + if (argc != 1) { + chprintf(chp, "usage: crc32 \r\n"); + return; + } + + auto path = path_from_string8(argv[0]); + File* crc_file = new File(); + auto error = crc_file->open(path, true, false); + if (report_on_error(chp, error)) return; + + uint8_t buffer[64]; + CRC<32> crc{0x04c11db7, 0xffffffff, 0xffffffff}; + + while (true) { + auto bytes_read = crc_file->read(buffer, 64); + if (report_on_error(chp, bytes_read)) return; + + if (bytes_read.value() > 0) { + crc.process_bytes((void*)buffer, bytes_read.value()); + } + + if (64 != bytes_read.value()) { + chprintf(chp, "CRC32: 0x%08X\r\n", crc.checksum()); + return; + } + } + + delete crc_file; +} diff --git a/firmware/application/usb_serial_shell_filesystem.hpp b/firmware/application/usb_serial_shell_filesystem.hpp index 9b98ae5b..2bd81d1e 100644 --- a/firmware/application/usb_serial_shell_filesystem.hpp +++ b/firmware/application/usb_serial_shell_filesystem.hpp @@ -42,6 +42,7 @@ void cmd_sd_read(BaseSequentialStream* chp, int argc, char* argv[]); void cmd_sd_read_binary(BaseSequentialStream* chp, int argc, char* argv[]); void cmd_sd_write(BaseSequentialStream* chp, int argc, char* argv[]); void cmd_sd_write_binary(BaseSequentialStream* chp, int argc, char* argv[]); +void cmd_sd_crc32(BaseSequentialStream* chp, int argc, char* argv[]); static std::filesystem::path path_from_string8(char* path) { std::wstring_convert, char16_t> conv; @@ -62,6 +63,7 @@ static std::filesystem::path path_from_string8(char* path) { {"ftell", cmd_sd_tell}, \ {"fread", cmd_sd_read}, \ {"frb", cmd_sd_read_binary}, \ - {"fwrite", cmd_sd_write}, \ - {"fwb", cmd_sd_write_binary} + {"fwrite", cmd_sd_write}, \ + {"fwb", cmd_sd_write_binary}, \ + {"crc32", cmd_sd_crc32} // clang-format on