-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathProgram.cs
171 lines (157 loc) · 6.19 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace wm_tools
{
internal class Program
{
enum ACTION_ARGS
{
NULL = 0, //null,nothing to do
DOWNLOAD , //download firmware
ERASE , //erase the flash
CHIP_ID , //read the chip id
FLASH_ID , //read the flash id
VERSION , //read the wm_tools version
};
static string Version = "v0.40";
static ACTION_ARGS action_arg = ACTION_ARGS.NULL;
private static int Main(string[] args)
{
var arguments = CommandLineArgumentParser.Parse(args);
string filepath = "wm600_sec.bin";
string portname = "COM1";
string baudrate = "115200";
if (arguments.Has("-h"))
{
Console.WriteLine("usage: \r\n" +
"\twm_tools.exe [-h] [-p] [-b] {write_flash, erase_flash, version}\r\n" +
"examples: \r\n" +
"\twm_tools.exe -p COM6 -b 2000000 erase_flash\r\n" +
"\twm_tools.exe -p COM6 -b 2000000 write_flash wm600_sec.img");
return 0;
}
if (arguments.Has("-p"))
{
try
{
portname = arguments.Get("-p").Next;
}
catch (Exception)
{
Console.WriteLine("get port name error !");
}
}
if (arguments.Has("-b"))
{
try
{
baudrate = arguments.Get("-b").Next;
}
catch (Exception)
{
Console.WriteLine("get baudrate error !");
}
}
if (arguments.Has("write_flash"))
{
action_arg = ACTION_ARGS.DOWNLOAD;
try
{
filepath = arguments.Get("write_flash").Next;
}
catch (Exception)
{
Console.WriteLine("get firmware path error !");
}
}
if (arguments.Has("erase_flash"))
{
action_arg = ACTION_ARGS.ERASE;
}
if (arguments.Has("chip_id"))
{
action_arg = ACTION_ARGS.CHIP_ID;
}
if (arguments.Has("flash_id"))
{
action_arg = ACTION_ARGS.FLASH_ID;
}
if (arguments.Has("version"))
{
action_arg = ACTION_ARGS.VERSION;
}
switch(action_arg)
{
case ACTION_ARGS.NULL:
Console.WriteLine("error: invalid argument");
Console.WriteLine("usage: \r\n" +
"\twm_tools.exe [-h] [-p] [-b] {write_flash, erase_flash, version}\r\n" +
"examples: \r\n" +
"\twm_tools.exe -p COM6 -b 2000000 erase_flash\r\n" +
"\twm_tools.exe -p COM6 -b 2000000 write_flash wm600_sec.img");
break;
case ACTION_ARGS.DOWNLOAD:
Console.WriteLine("need to download ...");
int baudrate_index = 4;
switch(baudrate)
{
case "2000000": baudrate_index = 0; break;
case "1000000": baudrate_index = 1; break;
case "921600": baudrate_index = 2; break;
case "460800": baudrate_index = 3; break;
default: baudrate_index = 4; break;
}
if(File.Exists(filepath) == false)
{
Console.WriteLine("Error firmware path !");
return -1;
}
var controller_write = new W600Controller();
try
{
controller_write.Open(portname);
Console.WriteLine("opend {0} !", portname);
controller_write.Sync_To_Download(baudrate_index);
controller_write.LoadFirmware(filepath);
Console.WriteLine("All done.");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return -1;
}
break;
case ACTION_ARGS.ERASE:
Console.WriteLine("need to erase ...");
var controller_erase = new W600Controller();
try
{
controller_erase.Open(portname);
Console.WriteLine("opend {0} !", portname);
controller_erase.Sync_To_Erase();
Console.WriteLine("All done.");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return -1;
}
break;
case ACTION_ARGS.CHIP_ID:
break;
case ACTION_ARGS.FLASH_ID:
break;
case ACTION_ARGS.VERSION:
Assembly assembly = Assembly.GetExecutingAssembly();
Console.WriteLine("wm_tools {0} for w600", Version);
Console.WriteLine("written by thingsturn");
Console.WriteLine("compile @ {0}", System.IO.File.GetLastWriteTime(assembly.Location).ToString());
break;
}
return 0;
}
}
}