/platform/CardS12/tools/srec_converter.cc
C++ | 149 lines | 120 code | 11 blank | 18 comment | 20 complexity | bd1d7fe31ac1e75e51c49811a1b9f073 MD5 | raw file
Possible License(s): LGPL-3.0
- /*
- * REFLEX - Real-time Event FLow EXecutive
- *
- * A lightweight operating system for deeply embedded systems.
- *
- * Revision: $Id: srec_converter.cc 1044 2009-03-16 15:36:34Z shoeckne $
- * Author: Karsten Walther
- *
- * (c) Karsten Walther, BTU Cottbus 2005
- */
- #include <iostream>
- #include <fstream>
- #include <cstdlib>
- #include <iomanip>
- using namespace std;
- unsigned char hex2char(char val){
- //0-9
- if(val>=48 && val<=57){
- return val-48;
- }
- //A-F
- if(val>=65 && val<=70){
- return val-55;
- }
- //a-f
- if(val>=97 && val<=102){
- return val-87;
- }
- cerr << "hex2char failed -- " << (int)val << endl;
- return 0;
- }
- int main(int argc,char* argv[])
- {
- cout << "srec conversion started." << endl;
- if(argc!=3){
- cerr << "usage: srec_converter infile outfile" << endl;
- exit(1);
- }
- ifstream infile(argv[1]);
- if(!infile){
- cerr << "Could not open inputfile: " << argv[1] << endl;
- exit(1);
- }
- ofstream outfile(argv[2],ios::out|ios::trunc);
- if(!outfile){
- cerr << "Could not open outputfile: " << argv[2] << endl;
- exit(1);
- }
- unsigned char ch,count1,count2;
- unsigned char type;
- unsigned char addr[4];
- int address;
- bool first=true;
- int count;
- unsigned char buffer[65536*2];
- int pos=0;
- cout << "Read in ..." << endl;
- while(!infile.eof()){
- //SXCC e.g. S1E1 -- srec-type and length
- infile >> ch >> type >> count1 >> count2;
- count = hex2char(count1)*16 + hex2char(count2);
- switch (type){
- case '1' :
- //read start address
- if(first){
- infile >> addr[3] >> addr[2] >> addr[1] >> addr[0];
- address = hex2char(addr[3])*16*16*16;
- address += hex2char(addr[2])*16*16;
- address += hex2char(addr[1])*16;
- address += hex2char(addr[0]);
- first = false;
- }else{
- infile >> ch >> ch >> ch >> ch;
- }
- count -= 2;
- //read data
- for(int i=0;i<count-1;++i){
- infile >> buffer[pos] >> buffer[pos+1];
- pos+=2;
- }
- count = 1;
- break;
- default :
- //until checksum skip data
- for(int i=0;i<count-1;++i){
- infile >> ch;
- infile >> ch;
- }
- count = 1;
- break;
- }
- for (int i = 0; i < count-1 ; ++i){
- infile >> buffer[pos];
- ++pos;
- infile >> buffer[pos];
- ++pos;
- }
- //skip checksum and linebreak
- infile >> ch >> ch;
- }
- cout << "Write out ..." << endl;
- unsigned char checksum,cs0,cs1;
- for(int i=0;i<pos;i+=64){
- outfile << "S123";
- outfile << setw(4) << hex << (int)address;
- checksum = (unsigned char)(address&0xff) + (unsigned char)((address>>8)&0xff) + (char)(35);
- address+=32;
- for(int j=0;j<64;j+=2){
- if((i+j)<pos){
- outfile << buffer[i+j];
- checksum+=hex2char(buffer[i+j])*16;
- }else{
- outfile << '0';
- }
- if((i+j+1)<pos){
- outfile << buffer[i+j+1];
- checksum+=hex2char(buffer[i+j+1]);
- }else{
- outfile << '0';
- }
- }
- checksum = ~checksum;
- if((checksum&0xf)<0xa){
- cs0=(checksum&0xf)+48;
- }else{
- cs0=(checksum&0xf)+55;
- }
- if(((checksum>>4)&0xf)<0xa){
- cs1=((checksum>>4)&0xf)+48;
- }else{
- cs1=((checksum>>4)&0xf)+55;
- }
- outfile << cs1 << cs0 << (char)13;
- }
- outfile << "S9030000FC" << (char)13;
- cout << "SREC converted." << endl;
- return 0;
- }