Commit 5655d108 authored by Sergi Hernandez's avatar Sergi Hernandez
Browse files

Changed the firmware downloader tool to use the new Intel Hex file parser.

parent 7315ca57
......@@ -8,9 +8,9 @@ INCLUDE_DIRECTORIES(${iriutils_INCLUDE_DIR})
INCLUDE_DIRECTORIES(${comm_INCLUDE_DIR})
# application source files
SET(sources fw_downloader.cpp hex2bin.cpp)
SET(sources fw_downloader.cpp hex.cpp)
# application header files
SET(headers fw_downloader.h hex2bin.h)
SET(headers fw_downloader.h hex.h parsers.h)
# create the executable file
ADD_EXECUTABLE(fw_downloader ${sources})
......
#include "eventexceptions.h"
#include "eventserver.h"
#include "exceptions.h"
#include "hex2bin.h"
#include "hex.h"
#include "parsers.h"
#include "rs232.h"
#include <stdlib.h>
#include <getopt.h>
......@@ -52,19 +53,22 @@ int main(int argc,char *argv[])
std::string device;
protocol_t protocol;
// binary file variables
unsigned char binary_file[MEMORY_MAXSIZE];
long int downloaded_size=0;
long int start_address=0;
long int binary_size=0;
long int written_size=0;
long int write_size=0;
int block_size=0,count=0;
parser_err error;
hex_t *hex_file;
size_t data_len;
off_t offset = 0;
bool done=false;
unsigned long int written_size=0;
unsigned long int write_size=0;
unsigned int base,addr,left,len;
unsigned char buffer[64];
std::stringstream base_address;
// bootloader variables
unsigned char bootloader_data[MEMORY_MAXSIZE];
unsigned char bootloader_data[1024];
bool bootloader_connected=false;
bool write_fw=false;
unsigned char checksum=0x00;
unsigned int count=0;
// eeprom variables
std::ofstream ee_file;
bool read_eeprom=false,clear_eeprom=false;
......@@ -75,7 +79,6 @@ int main(int argc,char *argv[])
serial_config.parity=none;
serial_config.stop_bits=1;
memset(binary_file,0xFF,MEMORY_MAXSIZE);
// parse the input arguments
while((option = getopt(argc, argv, "hd:f:p:cr:")) != -1)
......@@ -212,82 +215,103 @@ int main(int argc,char *argv[])
{
if(opt_fw_file.size()!=0)
{
if(hex2bin((char *)opt_fw_file.c_str(), binary_file, &start_address, &binary_size) == false)
hex_file=hex_init();
if((error=hex_open(hex_file,opt_fw_file.c_str()))!=PARSER_ERR_OK)
{
serial_port.close();
std::cout << "Error converting the firmware hex file" << std::endl;
hex_close(hex_file);
std::cout << "Impossible to open firmware file" << std::endl;
return 0;
}
}
// start the download
do{
base_address.str("");
if(device=="cm730" || device=="mx")
base_address << "l " << std::hex << 0x08003000 + written_size << std::endl;
else
base_address << "l" << std::endl;
serial_port.write((unsigned char *)base_address.str().c_str(),base_address.str().size()-1);
if(protocol==new_protocol)
serial_port.write((unsigned char *)&bootloader_ack,1);
end=false;
while(!end)
done=hex_get_first_segment(hex_file,&base,&data_len);
while(!done)
{
try{
if((num_data=serial_port.get_num_data()==0))
event_server->wait_all(events,300);
num_data=serial_port.get_num_data();
serial_port.read(bootloader_data,num_data);
bootloader_data[num_data]='\0';
printf("%s",bootloader_data);
fflush(stdout);
}catch(CEventTimeoutException &e){
end=true;
}
written_size=0;
do{
base_address.str("");
if(device=="cm730" || device=="mx")
base_address << "l " << std::hex << 0x08003000 + written_size << std::endl;
else
base_address << "l " << std::hex << base + written_size << std::endl;
serial_port.write((unsigned char *)base_address.str().c_str(),base_address.str().size()-1);
if(protocol==new_protocol)
serial_port.write((unsigned char *)&bootloader_ack,1);
end=false;
while(!end)
{
try{
if((num_data=serial_port.get_num_data()==0))
event_server->wait_all(events,300);
num_data=serial_port.get_num_data();
serial_port.read(bootloader_data,num_data);
bootloader_data[num_data]='\0';
printf("%s",bootloader_data);
fflush(stdout);
}catch(CEventTimeoutException &e){
end=true;
}
}
if((data_len-written_size)>1024*100)
write_size=1024*100;
else
write_size=data_len-written_size;
std::cout << "segment: start address: " << std::hex << base+written_size << ", length: " << std::dec << write_size << std::endl;
addr = base;
std::cout << '\xd';
checksum=0;
while(((unsigned int)offset) < write_size)
{
usleep(1000);
left = write_size-offset;
len = sizeof(buffer) > left ? left : sizeof(buffer);
if (hex_read(hex_file, buffer, &len) != PARSER_ERR_OK)
{
std::cout << "Error reading the hex file" << std::endl;
hex_close(hex_file);
return 1;
}
for(i=0;i<len;i++)
checksum+=buffer[i];
serial_port.write(buffer,len);
addr += len;
offset += len;
std::cout << '\xd';
std::cout.width(3);
std::cout << "write done: " << (int)((100.0f / write_size) * offset) << "%";
std::cout.flush();
}
std::cout << std::endl;
offset=0;
usleep(1000);
// send the checksum
serial_port.write(&checksum,1);
end=false;
while(!end)
{
try{
if((num_data=serial_port.get_num_data()==0))
event_server->wait_all(events,300);
// there has been an error
num_data=serial_port.get_num_data();
serial_port.read(bootloader_data,num_data);
bootloader_data[num_data]='\0';
printf("%s",bootloader_data);
}catch(CEventTimeoutException &e){
// no error to report
end=true;
}
}
written_size+=write_size;
}while(written_size<data_len);
done=hex_get_next_segment(hex_file,&base,&data_len);
}
if((binary_size-written_size)>1024*100)
write_size=1024*100;
else
write_size=binary_size-written_size;
// compute the binary file checksum
checksum=0;
for(i=0;i<write_size;i++)
checksum+=binary_file[start_address+i];
// send the binary file in 64 byte segments
while(downloaded_size < write_size)
{
usleep(1000);
block_size=write_size-downloaded_size;
if(block_size>64)
block_size=64;
serial_port.write(&binary_file[start_address+downloaded_size],block_size);
downloaded_size+=block_size;
printf("\rDownloading Firmware (%ld/%ld byte)", downloaded_size, write_size);
fflush(stdout);
}
usleep(1000);
// send the checksum
serial_port.write(&checksum,1);
end=false;
while(!end)
{
try{
if((num_data=serial_port.get_num_data()==0))
event_server->wait_all(events,300);
// there has been an error
num_data=serial_port.get_num_data();
serial_port.read(bootloader_data,num_data);
bootloader_data[num_data]='\0';
printf("%s",bootloader_data);
}catch(CEventTimeoutException &e){
// no error to report
end=true;
}
}
written_size+=write_size;
start_address+=write_size;
downloaded_size=0;
}while(written_size<binary_size);
std::cout << std::endl << "Download complete" << std::endl;
std::cout << std::endl << "Download complete" << std::endl;
hex_close(hex_file);
}
else
{
std::cout << "No input file provided" << std::endl;
return 0;
}
}
}
else if(read_eeprom)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment