Redid i2c upload to work with stm32 client device
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Dreaded_X 2021-06-02 22:56:40 +02:00
parent ec3f538734
commit 2ecdbf6d1f

View File

@ -116,7 +116,9 @@ void write_data(std::unique_ptr<serial::Serial>& ser, uint8_t* data, uint16_t le
}
// Check CRC
if (crc32(data, length) != read_length<uint32_t>(ser)) {
uint32_t c = crc32(data, length);
std::cout << std::hex << c << std::dec << '\n';
if (c != read_length<uint32_t>(ser)) {
throw std::runtime_error("CRC Mismatch");
}
}
@ -128,7 +130,7 @@ void read_data(std::unique_ptr<serial::Serial>& ser, uint8_t* data, uint16_t len
}
// @todo We need to check if we are already in the bootloader
void program_stm32(std::unique_ptr<serial::Serial>& ser, std::string filename) {
void program_dfu(std::unique_ptr<serial::Serial>& ser, std::string filename) {
ser->flushInput();
ser->flushOutput();
ser->flush();
@ -232,33 +234,44 @@ void program_i2c(std::unique_ptr<serial::Serial>& ser, std::string filename, uin
throw std::runtime_error("Failed to read byte");
}
if (r == 0x02) {
std::cout << "Restarting target to bootloader...\n";
std::cout << "Waiting for the target to be ready... (target: 0x" << std::hex << (uint32_t)address << std::dec << ")\n";
if (r == 0xFE) {
throw std::runtime_error("Please reboot device to bootloader first!");
} else if (r != 0x01) {
std::cerr << "Response: " << r << ", " << std::hex << (uint32_t)r << '\n';
std::cout << "Response: " << r << ", " << std::hex << (uint32_t)r << '\n';
throw std::runtime_error("Invalid response");
}
std::cout << "Waiting for the target to be ready... (target: 0x" << std::hex << (uint32_t)address << std::dec << ")\n";
wait_for_ack(ser);
char version_string[16] = {0};
read_data(ser, reinterpret_cast<uint8_t*>(version_string), sizeof(version_string));
std::cout << "Bootloader version: " << std::string(version_string, 16) << '\n';
std::cout << "Sending upload command\n";
wait_for_ack(ser);
std::cout << "Sending length\n";
wait_for_ack(ser);
std::cout << "Sending data...\n";
wait_for_ack(ser);
// @todo Implement this
// Track progress
std::cout << "Programming...\n";
for (uint16_t progress = 0; ; progress = read_length<uint16_t>(ser)) {
std::cout << "Progress: " << progress << '/' << length << '\r';
if (progress == length) {
break;
}
}
std::cout << '\n';
// std::cout << "Programming...\n";
// for (uint16_t progress = 0; ; progress = read_length<uint16_t>(ser)) {
// std::cout << "Progress: " << progress << '/' << length << '\r';
// if (progress == length) {
// break;
// }
// }
// std::cout << '\n';
// Wait for otherside to signal we are done
std::cout << "Validating data...\n";
wait_for_ack(ser);
std::cout << "Programming...\n";
wait_for_ack(ser);
std::cout << "Done!\n";
}
@ -274,8 +287,8 @@ int main(int argc, const char** argv) {
app.add_option("filename", filename, "File to upload")->required();
app.add_option("--dev,-d", device, "Serial device to use")->default_val("/dev/ttyUSB0");
app.add_subcommand("stm", "Program STM32 mcu")->callback([&] {
program_stm32(ser, filename);
app.add_subcommand("dfu", "Program STM32 mcu using dfu mode")->callback([&] {
program_dfu(ser, filename);
});
CLI::App* rom = app.add_subcommand("rom", "Program ROM/RAM")->callback([&] {
@ -292,8 +305,8 @@ int main(int argc, const char** argv) {
// CLI::App* bload = app.add_subcommand("bload", "Upload for bload");
app.parse_complete_callback([&] {
std::cout << "Initializing serial\n";
ser = std::make_unique<serial::Serial>("/dev/ttyUSB0", 115200, serial::Timeout::simpleTimeout(1000));
std::cout << "Initializing serial (" << device << ")\n";
ser = std::make_unique<serial::Serial>(device, 115200, serial::Timeout::simpleTimeout(30000));
if (!ser->isOpen()) {
throw std::runtime_error("Port is not open");