PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/platform/CardS12/tools/srec_converter.cc

https://bitbucket.org/sho/reflex-dev
C++ | 149 lines | 120 code | 11 blank | 18 comment | 20 complexity | bd1d7fe31ac1e75e51c49811a1b9f073 MD5 | raw file
Possible License(s): LGPL-3.0
  1. /*
  2. * REFLEX - Real-time Event FLow EXecutive
  3. *
  4. * A lightweight operating system for deeply embedded systems.
  5. *
  6. * Revision: $Id: srec_converter.cc 1044 2009-03-16 15:36:34Z shoeckne $
  7. * Author: Karsten Walther
  8. *
  9. * (c) Karsten Walther, BTU Cottbus 2005
  10. */
  11. #include <iostream>
  12. #include <fstream>
  13. #include <cstdlib>
  14. #include <iomanip>
  15. using namespace std;
  16. unsigned char hex2char(char val){
  17. //0-9
  18. if(val>=48 && val<=57){
  19. return val-48;
  20. }
  21. //A-F
  22. if(val>=65 && val<=70){
  23. return val-55;
  24. }
  25. //a-f
  26. if(val>=97 && val<=102){
  27. return val-87;
  28. }
  29. cerr << "hex2char failed -- " << (int)val << endl;
  30. return 0;
  31. }
  32. int main(int argc,char* argv[])
  33. {
  34. cout << "srec conversion started." << endl;
  35. if(argc!=3){
  36. cerr << "usage: srec_converter infile outfile" << endl;
  37. exit(1);
  38. }
  39. ifstream infile(argv[1]);
  40. if(!infile){
  41. cerr << "Could not open inputfile: " << argv[1] << endl;
  42. exit(1);
  43. }
  44. ofstream outfile(argv[2],ios::out|ios::trunc);
  45. if(!outfile){
  46. cerr << "Could not open outputfile: " << argv[2] << endl;
  47. exit(1);
  48. }
  49. unsigned char ch,count1,count2;
  50. unsigned char type;
  51. unsigned char addr[4];
  52. int address;
  53. bool first=true;
  54. int count;
  55. unsigned char buffer[65536*2];
  56. int pos=0;
  57. cout << "Read in ..." << endl;
  58. while(!infile.eof()){
  59. //SXCC e.g. S1E1 -- srec-type and length
  60. infile >> ch >> type >> count1 >> count2;
  61. count = hex2char(count1)*16 + hex2char(count2);
  62. switch (type){
  63. case '1' :
  64. //read start address
  65. if(first){
  66. infile >> addr[3] >> addr[2] >> addr[1] >> addr[0];
  67. address = hex2char(addr[3])*16*16*16;
  68. address += hex2char(addr[2])*16*16;
  69. address += hex2char(addr[1])*16;
  70. address += hex2char(addr[0]);
  71. first = false;
  72. }else{
  73. infile >> ch >> ch >> ch >> ch;
  74. }
  75. count -= 2;
  76. //read data
  77. for(int i=0;i<count-1;++i){
  78. infile >> buffer[pos] >> buffer[pos+1];
  79. pos+=2;
  80. }
  81. count = 1;
  82. break;
  83. default :
  84. //until checksum skip data
  85. for(int i=0;i<count-1;++i){
  86. infile >> ch;
  87. infile >> ch;
  88. }
  89. count = 1;
  90. break;
  91. }
  92. for (int i = 0; i < count-1 ; ++i){
  93. infile >> buffer[pos];
  94. ++pos;
  95. infile >> buffer[pos];
  96. ++pos;
  97. }
  98. //skip checksum and linebreak
  99. infile >> ch >> ch;
  100. }
  101. cout << "Write out ..." << endl;
  102. unsigned char checksum,cs0,cs1;
  103. for(int i=0;i<pos;i+=64){
  104. outfile << "S123";
  105. outfile << setw(4) << hex << (int)address;
  106. checksum = (unsigned char)(address&0xff) + (unsigned char)((address>>8)&0xff) + (char)(35);
  107. address+=32;
  108. for(int j=0;j<64;j+=2){
  109. if((i+j)<pos){
  110. outfile << buffer[i+j];
  111. checksum+=hex2char(buffer[i+j])*16;
  112. }else{
  113. outfile << '0';
  114. }
  115. if((i+j+1)<pos){
  116. outfile << buffer[i+j+1];
  117. checksum+=hex2char(buffer[i+j+1]);
  118. }else{
  119. outfile << '0';
  120. }
  121. }
  122. checksum = ~checksum;
  123. if((checksum&0xf)<0xa){
  124. cs0=(checksum&0xf)+48;
  125. }else{
  126. cs0=(checksum&0xf)+55;
  127. }
  128. if(((checksum>>4)&0xf)<0xa){
  129. cs1=((checksum>>4)&0xf)+48;
  130. }else{
  131. cs1=((checksum>>4)&0xf)+55;
  132. }
  133. outfile << cs1 << cs0 << (char)13;
  134. }
  135. outfile << "S9030000FC" << (char)13;
  136. cout << "SREC converted." << endl;
  137. return 0;
  138. }