Skip to content

Commit

Permalink
added crc32 command to usb shell (#1911)
Browse files Browse the repository at this point in the history
  • Loading branch information
bernd-herzog committed Feb 17, 2024
1 parent ed834e3 commit 27dc377
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
33 changes: 33 additions & 0 deletions firmware/application/usb_serial_shell_filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "string_format.hpp"
#include <cstring>

#include "crc.hpp"

static File* shell_file = nullptr;

static bool report_on_error(BaseSequentialStream* chp, File::Error& error) {
Expand Down Expand Up @@ -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 <path>\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;
}
6 changes: 4 additions & 2 deletions firmware/application/usb_serial_shell_filesystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
Expand All @@ -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

0 comments on commit 27dc377

Please sign in to comment.