Added ability to set and check board info.
[betaflight.git] / support / stmloader / loader.c
blob8a060fa2585d50ac78600ae0e8cb146be0b23b60
1 /*
2 This file is part of AutoQuad.
4 AutoQuad is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 AutoQuad is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with AutoQuad. If not, see <http://www.gnu.org/licenses/>.
16 Copyright © 2011 Bill Nesbitt
19 #include "serial.h"
20 #include "stmbootloader.h"
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <termios.h>
25 #include <fcntl.h>
26 #include <getopt.h>
27 #include <stdlib.h>
29 #define DEFAULT_PORT "/dev/ttyUSB0"
30 #define DEFAULT_BAUD 115200
31 #define FIRMWARE_FILENAME "STM32.hex"
33 serialStruct_t *s;
35 char port[256];
36 unsigned int baud;
37 unsigned char overrideParity;
38 unsigned char noSendR;
39 char firmFile[256];
41 void loaderUsage(void) {
42 fprintf(stderr, "usage: loader <-h> <-p device_file> <-b baud_rate> <-f firmware_file> <-o> <-n>\n");
45 unsigned int loaderOptions(int argc, char **argv) {
46 int ch;
48 strncpy(port, DEFAULT_PORT, sizeof(port));
49 baud = DEFAULT_BAUD;
50 overrideParity = 0;
51 noSendR = 0;
52 strncpy(firmFile, FIRMWARE_FILENAME, sizeof(firmFile));
54 /* options descriptor */
55 static struct option longopts[] = {
56 { "help", required_argument, NULL, 'h' },
57 { "port", required_argument, NULL, 'p' },
58 { "baud", required_argument, NULL, 's' },
59 { "firm_file", required_argument, NULL, 'f' },
60 { "override_parity", no_argument, NULL, 'o' },
61 { "no_send_r", no_argument, NULL, 'n' },
62 { NULL, 0, NULL, 0 }
65 while ((ch = getopt_long(argc, argv, "hp:b:f:o:n", longopts, NULL)) != -1)
66 switch (ch) {
67 case 'h':
68 loaderUsage();
69 exit(0);
70 break;
71 case 'p':
72 strncpy(port, optarg, sizeof(port));
73 break;
74 case 'b':
75 baud = atoi(optarg);
76 break;
77 case 'f':
78 strncpy(firmFile, optarg, sizeof(firmFile));
79 break;
80 case 'o':
81 overrideParity = 1;
82 break;
83 case 'n':
84 noSendR = 1;
85 break;
86 default:
87 loaderUsage();
88 return 0;
90 argc -= optind;
91 argv += optind;
93 return 1;
96 int main(int argc, char **argv) {
97 FILE *fw;
99 // init
100 if (!loaderOptions(argc, argv)) {
101 fprintf(stderr, "Init failed, aborting\n");
102 return 1;
105 s = initSerial(port, baud, 0);
106 if (!s) {
107 fprintf(stderr, "Cannot open serial port '%s', aborting.\n", port);
108 return 1;
111 fw = fopen(firmFile, "r");
112 if (!fw) {
113 fprintf(stderr, "Cannot open firmware file '%s', aborting.\n", firmFile);
114 return 1;
116 else {
117 printf("Upgrading STM on port %s from %s...\n", port, firmFile);
118 stmLoader(s, fw, overrideParity, noSendR);
121 return 0;