~ubuntu-branches/ubuntu/precise/picprog/precise

« back to all changes in this revision

Viewing changes to main.cc

  • Committer: Bazaar Package Importer
  • Author(s): Jan Wagemakers
  • Date: 2006-09-30 11:17:35 UTC
  • Revision ID: james.westby@ubuntu.com-20060930111735-c7qrfvmo3kk1eooa
Tags: upstream-1.8.3
Import upstream version 1.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- c++ -*-
 
2
 
 
3
This is Picprog, Microchip PIC programmer software for the serial port device.
 
4
Copyright © 1997,2002,2003,2004,2006 Jaakko Hyvätti
 
5
 
 
6
This program is free software; you can redistribute it and/or modify
 
7
it under the terms of the GNU General Public License version 2 as
 
8
published by the Free Software Foundation.
 
9
 
 
10
This program is distributed in the hope that it will be useful,
 
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
GNU General Public License for more details.
 
14
 
 
15
You should have received a copy of the GNU General Public License
 
16
along with this program; if not, write to the Free Software Foundation,
 
17
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
18
 
 
19
The author may be contacted at:
 
20
 
 
21
Email: Jaakko.Hyvatti@iki.fi
 
22
URL:   http://www.iki.fi/hyvatti/
 
23
Phone: +358 40 5011222
 
24
 
 
25
Please send any suggestions, bug reports, success stories etc. to the
 
26
Email address above.
 
27
 
 
28
*/
 
29
 
 
30
#include <iostream>
 
31
 
 
32
#include <sysexits.h>
 
33
#include <unistd.h>
 
34
#include <getopt.h>
 
35
#include <string.h>
 
36
 
 
37
#include "hexfile.h"
 
38
#include "program.h"
 
39
 
 
40
using namespace std;
 
41
 
 
42
program prog;
 
43
 
 
44
char short_opts [] = "d:p:i:o:c:qh?";
 
45
 
 
46
int
 
47
main (int argc, char **argv)
 
48
{
 
49
  int opt_warranty = 0;
 
50
  int opt_copying = 0;
 
51
  int opt_quiet = 0;
 
52
  int opt_usage = 0;
 
53
 
 
54
  int opt_format = hexfile::unknown;
 
55
  char *opt_port = (char *)"/dev/ttyS0";
 
56
  char *opt_input = NULL;
 
57
  char *opt_output = NULL;
 
58
  char *opt_cc = NULL;
 
59
  int opt_skip = 0;
 
60
  int opt_erase = 0;
 
61
  int opt_burn = 0;
 
62
  int opt_calibration = 0;
 
63
  int opt_nordtsc = 1;
 
64
  int opt_slow = 0;
 
65
 
 
66
  // Auto ident (-1) is the default, or if that fails, first device (0)
 
67
  int opt_device = -1;
 
68
 
 
69
  struct option long_opts[] = {
 
70
    {"quiet", no_argument, NULL, 'q'},
 
71
    {"warranty", no_argument, &opt_warranty, 1},
 
72
    {"copying", no_argument, &opt_copying, 1},
 
73
    {"help", no_argument, NULL, 'h'},
 
74
    {"device", required_argument, NULL, 'd'},
 
75
    {"pic-serial-port", required_argument, NULL, 'p'},
 
76
    {"input-hexfile", required_argument, NULL, 'i'},
 
77
    {"output-hexfile", required_argument, NULL, 'o'},
 
78
    {"ihx32", no_argument, &opt_format, hexfile::ihx32},
 
79
    {"ihx16", no_argument, &opt_format, hexfile::ihx16},
 
80
    {"ihx8m", no_argument, &opt_format, hexfile::ihx8m},
 
81
    {"cc-hexfile", required_argument, NULL, 'c'},
 
82
    {"skip-ones", no_argument, &opt_skip, 1},
 
83
    {"erase", no_argument, &opt_erase, 1},
 
84
    {"burn", no_argument, &opt_burn, 1},
 
85
    {"force-calibration", no_argument, &opt_calibration, 1},
 
86
    {"nordtsc", no_argument, &opt_nordtsc, 1},
 
87
    {"rdtsc", no_argument, &opt_nordtsc, 0},
 
88
    {"slow", no_argument, &opt_slow, 1},
 
89
    {0, 0, 0, 0}
 
90
  };
 
91
 
 
92
  int optc;
 
93
 
 
94
  prog.init (argv);
 
95
 
 
96
  while (0 <= (optc = getopt_long (argc, argv, short_opts, long_opts, NULL)))
 
97
    switch (optc) {
 
98
    case 0:
 
99
      break;
 
100
    case 'd':
 
101
      if (!strcmp (optarg, "auto"))
 
102
        opt_device = -1;
 
103
      else if (-1 == (opt_device = hexfile::find_device (optarg)))
 
104
        opt_usage = 1;
 
105
      break;
 
106
    case 'p':
 
107
      opt_port = optarg;
 
108
      break;
 
109
    case 'i':
 
110
      opt_input = optarg;
 
111
      break;
 
112
    case 'o':
 
113
      opt_output = optarg;
 
114
      break;
 
115
    case 'c':
 
116
      opt_cc = optarg;
 
117
      break;
 
118
    case 'q':
 
119
      opt_quiet = 1;
 
120
      break;
 
121
    default: // -? -h --help unknown flag
 
122
      opt_usage = 1;
 
123
    }
 
124
 
 
125
  if (!opt_quiet || opt_warranty || opt_copying || opt_usage)
 
126
    // Locale charset should be respected here.
 
127
    cerr << "Picprog version 1.8.3, Copyright © 2006 Jaakko Hyvätti <Jaakko.Hyvatti@iki.fi>\n"
 
128
      "Picprog comes with ABSOLUTELY NO WARRANTY; for details\n"
 
129
      "type `" << prog.name << " --warranty'.  This is free software,\n"
 
130
      "and you are welcome to redistribute it under certain conditions;\n"
 
131
      "type `" << prog.name << " --copying' for details.\n\n";
 
132
 
 
133
  if (opt_copying)
 
134
    prog.copying ();
 
135
 
 
136
  if (opt_warranty)
 
137
    prog.warranty ();
 
138
 
 
139
  if (opt_usage) {
 
140
    cerr << "Full documentation is at "
 
141
      "<URL:http://www.iki.fi/hyvatti/pic/picprog.html>" << endl
 
142
         << "The author may be contacted at:" << endl << endl
 
143
 
 
144
         << "Email: Jaakko.Hyvatti@iki.fi" << endl
 
145
         << "URL:   http://www.iki.fi/hyvatti/" << endl << endl
 
146
 
 
147
         << "Supported devices:" << endl;
 
148
    hexfile::print_devices ();
 
149
    cerr << endl;
 
150
 
 
151
    prog.usage (long_opts, short_opts);
 
152
  }
 
153
 
 
154
  if (opt_warranty || opt_copying || opt_usage)
 
155
    return EX_OK;
 
156
 
 
157
  if (!opt_input && !opt_output && !opt_erase) {
 
158
    cerr << "Please specify either input or output hexfile or --erase option."
 
159
         << endl;
 
160
    prog.usage (long_opts, short_opts);
 
161
  }
 
162
 
 
163
  if (opt_cc && !opt_input) {
 
164
    cerr << "Carbon copy does not make sense without input file." << endl;
 
165
    prog.usage (long_opts, short_opts);
 
166
  }
 
167
 
 
168
  // if both input and output files are specified, first program the device
 
169
  // and then read it.
 
170
 
 
171
  if (opt_input || opt_erase) {
 
172
    hexfile mem;
 
173
    int retval;
 
174
 
 
175
    if (EX_OK != (retval = mem.setdevice (opt_port, opt_device, opt_nordtsc, opt_slow)))
 
176
      return retval;
 
177
    if (opt_input && EX_OK != (retval = mem.load (opt_input)))
 
178
      return retval;
 
179
    if (opt_burn) {
 
180
      if (EX_OK != (retval = mem.program (opt_port, opt_erase, opt_calibration, opt_nordtsc, opt_slow)))
 
181
        return retval;
 
182
    } else
 
183
      cout << "No --burn option specified, device not programmed.\n";
 
184
 
 
185
    if (opt_input && opt_cc)
 
186
      if (EX_OK != (retval = mem.save (opt_cc,
 
187
                                       hexfile::formats (opt_format),
 
188
                                       opt_skip)))
 
189
        return retval;
 
190
  }
 
191
 
 
192
  if (opt_output) {
 
193
    hexfile mem;
 
194
    int retval;
 
195
 
 
196
    if (EX_OK != (retval = mem.setdevice (opt_port, opt_device, opt_nordtsc, opt_slow)))
 
197
      return retval;
 
198
    if (EX_OK != (retval = mem.read (opt_port, opt_nordtsc, opt_slow)))
 
199
      return retval;
 
200
    if (EX_OK != (retval = mem.save (opt_output,
 
201
                                     hexfile::formats (opt_format),
 
202
                                     opt_skip)))
 
203
      return retval;
 
204
  }
 
205
 
 
206
  return EX_OK;
 
207
}