First version of bootloader companion application

This commit is contained in:
Thomas Klaehn 2021-08-13 09:01:19 +02:00
parent bd1cdfe5b5
commit 147ee2469e

107
src/main.cc Normal file
View File

@ -0,0 +1,107 @@
#include <cstdlib>
#include <cstdint>
#include <cstdio>
#include <unistd.h>
#include <fcntl.h>
#include "Uart.h"
#include "Bootloader.h"
int main()
{
Uart uart("/dev/ttyACM1");
Bootloader bootloader(uart);
if (bootloader.chip_is_supported())
{
printf(bootloader.SUPPORTED_DEVICE_NAME);
printf("\r\n");
printf("Chip ID: 0x%04x\r\n", bootloader.chip_id());
// printf("Bootloader protocol version: %d.%d\r\n", chip.protocol_version_major, chip.protocol_version_minor);
printf("Flash size: %d Bytes\r\n", bootloader.flash_size());
printf("\r\nErasing memory ...\r\r");
if (!bootloader.full_flash_erase())
{
printf("Full flash erase not successful.\r\n");
exit(1);
}
printf("\r\nProgramming ");
fflush(stdout);
int f = open("nucleo.bin", O_RDONLY);
if (f < 0)
{
printf("\r\nunable to open \"nucleo.bin\"");
exit(1);
}
unsigned char file_buffer[256];
uint32_t addr = bootloader.MAIN_FLASH_START_ADDRESS;
int res = read(f, file_buffer, sizeof(file_buffer));
while (res > 0)
{
if (!bootloader.write_memory(addr, file_buffer, res))
{
printf("\r\nError while writing.");
exit(1);
}
else
{
printf(".");
fflush(stdout);
}
addr += res;
res = read(f, file_buffer, sizeof(file_buffer));
}
close(f);
printf("\r\nReading back ");
fflush(stdout);
f = open("nucleo.bin", O_RDONLY);
if (f < 0)
{
printf("\r\runable to open \"nucleo.bin\"\r\r");
exit(1);
}
unsigned char device_buffer[256];
addr = bootloader.MAIN_FLASH_START_ADDRESS;
res = read(f, file_buffer, sizeof(file_buffer));
while (res > 0)
{
if (!bootloader.read_memory(addr, device_buffer, res))
{
printf("\r\nError while reading");
exit(1);
}
else
{
if (memcmp(file_buffer, device_buffer, res))
{
printf("\r\nchunk doesn't match.");
}
else
{
printf(".");
fflush(stdout);
}
}
addr += res;
res = read(f, file_buffer, sizeof(file_buffer));
}
printf("\r\nNew application successfully flashed.\r\n");
if (!bootloader.go(bootloader.MAIN_FLASH_START_ADDRESS))
{
printf("Unable to start new application\r\n");
}
else
{
printf("Successfully started new application\r\n");
}
close(f);
}
else
{
printf("Chip not supported.\r\n");
}
}