sh7045writer.cpp
00001
00002
00003
00004
00005
00006
00007 #include "sformatCtrl.h"
00008 #include "fztatCtrl.h"
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include "wctrl_dat.h"
00012
00013
00014 #ifdef Linux
00015 static char DefaultDevice[] = "/dev/ttyS0";
00016 #else
00017 static char* DefaultDevice = "COM1";
00018 #endif
00019 enum {
00020 DefaultBaudrate = 115200,
00021 };
00022
00023
00024 static void errorExit(const char* message) {
00025 printf("Fail: %s\n", message);
00026 exit(1);
00027 }
00028
00029
00030 static void progressView(int now, int last) {
00031 if (now == last) {
00032 printf("\rcomplete! (%d byte) \n", last);
00033 return;
00034 }
00035
00036 static int pre_percent = -1;
00037 double percent = 100.0 * now / last;
00038 if (static_cast<int>(percent) != pre_percent) {
00039 printf("\r%d / %d (%3.2f %%)", now, last, percent);
00040 pre_percent = static_cast<int>(percent);
00041 fflush(stdout);
00042 }
00043 }
00044
00045
00046 static void printUsage(const char* progname) {
00047 printf("usage:\n"
00048 "\t%s [options] <mot file>\n"
00049 "\n"
00050 "options:\n"
00051 "-h, --help\t\tprint this message.\n"
00052 "--port=<PORT>\t\tspecify tty device. (default %s)\n"
00053 "--baudrate=<BAUDRATE>\tspecify tty baudrate. (default %d bps)\n",
00054 progname, DefaultDevice, DefaultBaudrate);
00055 }
00056
00057
00058 static void printWriteInfo(const char* device,
00059 int writeBaudrate, const char* writeMotFile) {
00060 printf("device: %s\n"
00061 "write baudrate: %d\n"
00062 "mot file: %s\n", device, writeBaudrate, writeMotFile);
00063 }
00064
00065
00066 int main(int argc, char *argv[]) {
00067 #ifndef Linux
00068 std::vector<std::string> ports = SerialDevice::getComPorts();
00069 if (!ports.empty()) {
00070 DefaultDevice = &ports[0][0];
00071 }
00072 #endif
00073
00074 if (argc < 2) {
00075 printUsage(argv[0]);
00076 exit(1);
00077 }
00078
00079
00080 char* writeMotFile = NULL;
00081 char* device = DefaultDevice;
00082 int writeBaudrate = DefaultBaudrate;
00083 for (int i = 1; i < argc; ++i) {
00084 if (!strcmp("-h", argv[i]) || !strcmp("--help", argv[i])) {
00085 printUsage(argv[0]);
00086 exit(0);
00087 } else if ((strlen(argv[i]) > 7) && !strncmp("--port=", argv[i], 7)) {
00088 device = &argv[i][7];
00089 } else if ((strlen(argv[i]) > 11) && !strncmp("--baudrate=", argv[i], 11)){
00090 writeBaudrate = atoi(&argv[i][11]);
00091 } else {
00092 writeMotFile = argv[i];
00093 }
00094 }
00095 if (!writeMotFile) {
00096 printUsage(argv[0]);
00097 exit(1);
00098 }
00099 printWriteInfo(device, writeBaudrate, writeMotFile);
00100
00101
00102 printf("check S-format file ... ");
00103 SFormat_Ctrl mot;
00104 if (!mot.load(writeMotFile)) {
00105 errorExit(mot.what());
00106 }
00107 printf("O.K.\n");
00108
00109
00110 printf("adjust SH7045F baudrate ... ");
00111 FZTAT_Ctrl fztat(device, writeBaudrate);
00112 if (!fztat.adjustBaudrate()) {
00113 errorExit(fztat.what());
00114 }
00115 printf("O.K.\n");
00116
00117
00118 int wctrl_size = sizeof(write_program) -1;
00119 printf("send write program (%d byte) ... ", wctrl_size);
00120 fflush(stdout);
00121 if (!fztat.sendWriteProgram(write_program, wctrl_size)) {
00122 errorExit(fztat.what());
00123 }
00124 printf("O.K.\n");
00125
00126
00127 printf("writing started.\n");
00128 fztat.setProgressCallback(progressView);
00129 std::vector<char> code = mot.text;
00130 if (!fztat.writeRom(&code[0], code.size(), writeBaudrate)) {
00131 errorExit(fztat.what());
00132 }
00133
00134
00135 printf("exit normaly.\n");
00136 return 0;
00137 }
00138