1 //=============================================================================
3 //=============================================================================
5 // FUNCTION: Serial download of a new image from PC to SIMpad
7 // AUTHOR: Juergen Messerer, Peter Voser
8 // CREAT.-DATE: 01.04.2001 (dd.mm.yy)
12 //=============================================================================
17 #include "serialdownload.h"
20 const int STRING_LENGTH = 128;
22 //=============================================================================
23 //=============================================================================
26 cout << "serload V1.0 for Linux " << endl;
27 cout << "Downloading a new image to the SIMpad "
28 << "using the serial interface." << endl << endl;
29 cout << "Invocation: serload [IMAGEFILE] [ttyS-PORT]" << endl;
30 cout << "IMAGEFILE: the file with the new image prepared for the"
31 << " SIMpad Bootloader."
33 cout << "ttyS-PORT: number of the ttyS-Port for the download."
35 cout << "Note: ttyS0 = COM1, ttyS1 = COM2, ..." << endl << endl;
38 //=============================================================================
39 //=============================================================================
40 int main(int argc, char *argv[])
43 for(i = 0; i < argc; ++i)
45 if(strcmp(argv[i], "--help") == 0)
47 // The user asks for help.
53 if(argc != 3 && argc != 2)
55 cerr << endl << "Usage: serload [IMAGEFILE] [ttyS-PORT]" << endl;
56 cerr << "See also \"serload --help\"!" << endl << endl;
60 char device[STRING_LENGTH];
63 strcpy(device, "/dev/ttyS");
64 strcat(device, argv[2]);
68 // If no serial port is given, use ttyS0 as default.
69 strcpy(device, "/dev/ttyS0");
72 SerialDownload serload;
73 int myError, imagesize;
76 int success = serload.openSerialPort(device, myError);
79 cerr << "Error: cannot open " << device << ". Aborting."
84 myError = serload.loadFile(argv[1], image, imagesize);
87 cerr << "Error: cannot load file "
88 << argv[1] << "! Aborting." << endl << endl;
92 cout << "Please press RESET at the back of the SIMpad!" << endl;
93 int reply = serload.connectToSimpad(115200, myError);
97 cerr << "Error: cannot connect to SIMpad! Aborting."
102 // Determine number of blocks to send without remaining bytes!
104 int size = imagesize;
105 int totalBlocks = size / 512;
106 int numberOfBlocksToSend = totalBlocks;
107 int numberOfBlocksSent = 0;
110 while(numberOfBlocksToSend)
112 serload.sendBlock(image, 512, myError);
114 --numberOfBlocksToSend;
115 // Update progress info every 100th block.
116 if(!(numberOfBlocksSent % 100))
118 progress = 100 * numberOfBlocksSent / totalBlocks;
120 ++numberOfBlocksSent;
123 // Determine, if there are remaining bytes.
124 int numberOfRemainingBytes = size % 512;
125 if(numberOfRemainingBytes)
127 serload.sendBlock(image, numberOfRemainingBytes, myError);
130 // The bootloader burns the new image.
131 serload.waitForEndOfBurning();
133 cout << "Update successfully finished! Swich the SIMpad on." << endl;