Redid i2c upload to work with stm32 client device
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
ec3f538734
commit
2ecdbf6d1f
59
src/main.cpp
59
src/main.cpp
|
@ -116,7 +116,9 @@ void write_data(std::unique_ptr<serial::Serial>& ser, uint8_t* data, uint16_t le
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check CRC
|
// 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");
|
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
|
// @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->flushInput();
|
||||||
ser->flushOutput();
|
ser->flushOutput();
|
||||||
ser->flush();
|
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");
|
throw std::runtime_error("Failed to read byte");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (r == 0x02) {
|
std::cout << "Waiting for the target to be ready... (target: 0x" << std::hex << (uint32_t)address << std::dec << ")\n";
|
||||||
std::cout << "Restarting target to bootloader...\n";
|
|
||||||
|
if (r == 0xFE) {
|
||||||
|
throw std::runtime_error("Please reboot device to bootloader first!");
|
||||||
} else if (r != 0x01) {
|
} 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");
|
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);
|
wait_for_ack(ser);
|
||||||
|
|
||||||
char version_string[16] = {0};
|
std::cout << "Sending upload command\n";
|
||||||
read_data(ser, reinterpret_cast<uint8_t*>(version_string), sizeof(version_string));
|
wait_for_ack(ser);
|
||||||
std::cout << "Bootloader version: " << std::string(version_string, 16) << '\n';
|
|
||||||
|
|
||||||
|
std::cout << "Sending length\n";
|
||||||
|
wait_for_ack(ser);
|
||||||
|
|
||||||
|
|
||||||
|
std::cout << "Sending data...\n";
|
||||||
|
wait_for_ack(ser);
|
||||||
|
|
||||||
|
// @todo Implement this
|
||||||
// Track progress
|
// Track progress
|
||||||
std::cout << "Programming...\n";
|
// std::cout << "Programming...\n";
|
||||||
for (uint16_t progress = 0; ; progress = read_length<uint16_t>(ser)) {
|
// for (uint16_t progress = 0; ; progress = read_length<uint16_t>(ser)) {
|
||||||
std::cout << "Progress: " << progress << '/' << length << '\r';
|
// std::cout << "Progress: " << progress << '/' << length << '\r';
|
||||||
if (progress == length) {
|
// if (progress == length) {
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
std::cout << '\n';
|
// std::cout << '\n';
|
||||||
|
|
||||||
// Wait for otherside to signal we are done
|
std::cout << "Validating data...\n";
|
||||||
wait_for_ack(ser);
|
wait_for_ack(ser);
|
||||||
|
|
||||||
|
std::cout << "Programming...\n";
|
||||||
|
wait_for_ack(ser);
|
||||||
|
|
||||||
std::cout << "Done!\n";
|
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("filename", filename, "File to upload")->required();
|
||||||
app.add_option("--dev,-d", device, "Serial device to use")->default_val("/dev/ttyUSB0");
|
app.add_option("--dev,-d", device, "Serial device to use")->default_val("/dev/ttyUSB0");
|
||||||
|
|
||||||
app.add_subcommand("stm", "Program STM32 mcu")->callback([&] {
|
app.add_subcommand("dfu", "Program STM32 mcu using dfu mode")->callback([&] {
|
||||||
program_stm32(ser, filename);
|
program_dfu(ser, filename);
|
||||||
});
|
});
|
||||||
|
|
||||||
CLI::App* rom = app.add_subcommand("rom", "Program ROM/RAM")->callback([&] {
|
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");
|
// CLI::App* bload = app.add_subcommand("bload", "Upload for bload");
|
||||||
|
|
||||||
app.parse_complete_callback([&] {
|
app.parse_complete_callback([&] {
|
||||||
std::cout << "Initializing serial\n";
|
std::cout << "Initializing serial (" << device << ")\n";
|
||||||
ser = std::make_unique<serial::Serial>("/dev/ttyUSB0", 115200, serial::Timeout::simpleTimeout(1000));
|
ser = std::make_unique<serial::Serial>(device, 115200, serial::Timeout::simpleTimeout(30000));
|
||||||
|
|
||||||
if (!ser->isOpen()) {
|
if (!ser->isOpen()) {
|
||||||
throw std::runtime_error("Port is not open");
|
throw std::runtime_error("Port is not open");
|
||||||
|
|
Loading…
Reference in New Issue
Block a user