|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Text.RegularExpressions; |
| 8 | + |
| 9 | +namespace nanoFramework.Tools.FirmwareFlasher |
| 10 | +{ |
| 11 | + internal class CC13x26x2Device |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// Configuration file for the device to connect to. |
| 15 | + /// </summary> |
| 16 | + public string ConfigurationFile { get; } |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Option to output progress messages. |
| 20 | + /// Default is <see langword="true"/>. |
| 21 | + /// </summary> |
| 22 | + public VerbosityLevel Verbosity { get; internal set; } = VerbosityLevel.Normal; |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Property with option for performing mass erase on the connected device. |
| 26 | + /// If <see langword="false"/> only the flash sectors that will programmed are erased. |
| 27 | + /// </summary> |
| 28 | + public bool DoMassErase { get; set; } = false; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Creates a new <see cref="CC13x26x2Device"/>. |
| 32 | + /// </summary> |
| 33 | + public CC13x26x2Device(string configurationFile) |
| 34 | + { |
| 35 | + ConfigurationFile = configurationFile; |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Flash the HEX supplied to the connected device. |
| 40 | + /// </summary> |
| 41 | + /// <param name="files"></param> |
| 42 | + public ExitCodes FlashHexFiles(IEnumerable<string> files) |
| 43 | + { |
| 44 | + // check file existence |
| 45 | + foreach (string f in files) |
| 46 | + { |
| 47 | + if (!File.Exists(f)) |
| 48 | + { |
| 49 | + return ExitCodes.E5003; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + string cliOuput; |
| 54 | + |
| 55 | + // TODO |
| 56 | + // use the -d switch |
| 57 | + //// erase flash |
| 58 | + //if (DoMassErase) |
| 59 | + //{ |
| 60 | + // if (Verbosity >= VerbosityLevel.Normal) |
| 61 | + // { |
| 62 | + // Console.Write("Mass erase device..."); |
| 63 | + // } |
| 64 | + |
| 65 | + // cliOuput = RunUniflashCli($"-c SN={DeviceId} UR -ME"); |
| 66 | + |
| 67 | + // if (!cliOuput.Contains("Flash memory erased.")) |
| 68 | + // { |
| 69 | + // return ExitCodes.E5005; |
| 70 | + // } |
| 71 | + |
| 72 | + // if (Verbosity >= VerbosityLevel.Normal) |
| 73 | + // { |
| 74 | + // Console.WriteLine(" OK"); |
| 75 | + // } |
| 76 | + // else |
| 77 | + // { |
| 78 | + // Console.WriteLine(""); |
| 79 | + // } |
| 80 | + |
| 81 | + // // toggle mass erase so it's only performed before the first file is flashed |
| 82 | + // DoMassErase = false; |
| 83 | + //} |
| 84 | + |
| 85 | + if (Verbosity == VerbosityLevel.Normal) |
| 86 | + { |
| 87 | + Console.Write("Flashing device..."); |
| 88 | + } |
| 89 | + else if (Verbosity >= VerbosityLevel.Detailed) |
| 90 | + { |
| 91 | + Console.WriteLine("Flashing device..."); |
| 92 | + } |
| 93 | + |
| 94 | + // program HEX file(s) |
| 95 | + foreach (string hexFile in files) |
| 96 | + { |
| 97 | + if (Verbosity >= VerbosityLevel.Detailed) |
| 98 | + { |
| 99 | + Console.WriteLine($"{Path.GetFileName(hexFile)}"); |
| 100 | + } |
| 101 | + |
| 102 | + cliOuput = RunUniflashCli($" flash -c {ConfigurationFile} -f -v {hexFile}"); |
| 103 | + |
| 104 | + if (!cliOuput.Contains("Program verification successful")) |
| 105 | + { |
| 106 | + return ExitCodes.E5006; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + if (Verbosity == VerbosityLevel.Normal) |
| 111 | + { |
| 112 | + Console.WriteLine(" OK"); |
| 113 | + } |
| 114 | + else if (Verbosity >= VerbosityLevel.Detailed) |
| 115 | + { |
| 116 | + Console.WriteLine("Flashing completed..."); |
| 117 | + } |
| 118 | + |
| 119 | + return ExitCodes.OK; |
| 120 | + } |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// Flash the BIN supplied to the connected device. |
| 124 | + /// </summary> |
| 125 | + /// <param name="files"></param> |
| 126 | + /// <param name="addresses"></param> |
| 127 | + public ExitCodes FlashBinFiles(IEnumerable<string> files, IEnumerable<string> addresses) |
| 128 | + { |
| 129 | + // check file existence |
| 130 | + foreach (string f in files) |
| 131 | + { |
| 132 | + if (!File.Exists(f)) |
| 133 | + { |
| 134 | + return ExitCodes.E5003; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + // check address(es) |
| 139 | + |
| 140 | + // need to match files count |
| 141 | + if(files.Count() != addresses.Count()) |
| 142 | + { |
| 143 | + return ExitCodes.E5009; |
| 144 | + } |
| 145 | + |
| 146 | + foreach (string address in addresses) |
| 147 | + { |
| 148 | + if (string.IsNullOrEmpty(address)) |
| 149 | + { |
| 150 | + return ExitCodes.E5007; |
| 151 | + } |
| 152 | + else |
| 153 | + { |
| 154 | + // format too |
| 155 | + if (!address.StartsWith("0x")) |
| 156 | + { |
| 157 | + return ExitCodes.E5008; |
| 158 | + } |
| 159 | + |
| 160 | + // try parse |
| 161 | + // need to remove the leading 0x and to specify that hexadecimal values are allowed |
| 162 | + int dummyAddress; |
| 163 | + if (!int.TryParse(address.Substring(2), System.Globalization.NumberStyles.AllowHexSpecifier, System.Globalization.CultureInfo.InvariantCulture, out dummyAddress)) |
| 164 | + { |
| 165 | + return ExitCodes.E5008; |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + string cliOuput; |
| 171 | + |
| 172 | + // TODO |
| 173 | + // use the -d switch |
| 174 | + //// erase flash |
| 175 | + //if (DoMassErase) |
| 176 | + //{ |
| 177 | + // if (Verbosity >= VerbosityLevel.Normal) |
| 178 | + // { |
| 179 | + // Console.Write("Mass erase device..."); |
| 180 | + // } |
| 181 | + |
| 182 | + // cliOuput = RunUniflashCli($"-b"); |
| 183 | + |
| 184 | + // if (!cliOuput.Contains("Flash memory erased.")) |
| 185 | + // { |
| 186 | + // Console.WriteLine(""); |
| 187 | + // return ExitCodes.E5005; |
| 188 | + // } |
| 189 | + |
| 190 | + // if (Verbosity >= VerbosityLevel.Normal) |
| 191 | + // { |
| 192 | + // Console.WriteLine(" OK"); |
| 193 | + // } |
| 194 | + // else |
| 195 | + // { |
| 196 | + // Console.WriteLine(""); |
| 197 | + // } |
| 198 | + |
| 199 | + // // toggle mass erase so it's only performed before the first file is flashed |
| 200 | + // DoMassErase = false; |
| 201 | + //} |
| 202 | + |
| 203 | + if (Verbosity == VerbosityLevel.Normal) |
| 204 | + { |
| 205 | + Console.Write("Flashing device..."); |
| 206 | + } |
| 207 | + else if (Verbosity >= VerbosityLevel.Detailed) |
| 208 | + { |
| 209 | + Console.WriteLine("Flashing device..."); |
| 210 | + } |
| 211 | + |
| 212 | + // program BIN file(s) |
| 213 | + int index = 0; |
| 214 | + foreach (string binFile in files) |
| 215 | + { |
| 216 | + if (Verbosity >= VerbosityLevel.Detailed) |
| 217 | + { |
| 218 | + Console.WriteLine($"{Path.GetFileName(binFile)} @ {addresses.ElementAt(index)}"); |
| 219 | + } |
| 220 | + |
| 221 | + cliOuput = RunUniflashCli($" flash -c {ConfigurationFile} -f -v {binFile},{addresses.ElementAt(index++)}"); |
| 222 | + |
| 223 | + if (!cliOuput.Contains("Program verification successful")) |
| 224 | + { |
| 225 | + return ExitCodes.E5006; |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + if (Verbosity == VerbosityLevel.Normal) |
| 230 | + { |
| 231 | + Console.WriteLine(" OK"); |
| 232 | + } |
| 233 | + else if (Verbosity >= VerbosityLevel.Detailed) |
| 234 | + { |
| 235 | + Console.WriteLine("Flashing completed..."); |
| 236 | + } |
| 237 | + |
| 238 | + return ExitCodes.OK; |
| 239 | + } |
| 240 | + |
| 241 | + /// <summary> |
| 242 | + /// Reset MCU of connected CC13x2 device. |
| 243 | + /// </summary> |
| 244 | + public ExitCodes ResetMcu() |
| 245 | + { |
| 246 | + // try to connect to device with RESET |
| 247 | + var cliOuput = RunUniflashCli($" flash -c {ConfigurationFile} -r 0"); |
| 248 | + |
| 249 | + if (!cliOuput.Contains("CPU Reset is issued")) |
| 250 | + { |
| 251 | + Console.WriteLine(""); |
| 252 | + return ExitCodes.E5010; |
| 253 | + } |
| 254 | + |
| 255 | + if (Verbosity >= VerbosityLevel.Normal) |
| 256 | + { |
| 257 | + Console.WriteLine(" OK"); |
| 258 | + } |
| 259 | + else |
| 260 | + { |
| 261 | + Console.WriteLine(""); |
| 262 | + } |
| 263 | + |
| 264 | + return ExitCodes.OK; |
| 265 | + } |
| 266 | + |
| 267 | + private static string RunUniflashCli(string arguments) |
| 268 | + { |
| 269 | + try |
| 270 | + { |
| 271 | + Process uniflashCli = new Process(); |
| 272 | + uniflashCli.StartInfo = new ProcessStartInfo(Path.Combine(Program.ExecutingPath, "uniflash\\DebugServer\\bin", "DSLite.exe"), arguments) |
| 273 | + { |
| 274 | + WorkingDirectory = Path.Combine(Program.ExecutingPath, "uniflash"), |
| 275 | + UseShellExecute = false, |
| 276 | + RedirectStandardOutput = true, |
| 277 | + }; |
| 278 | + |
| 279 | + |
| 280 | + // start Uniflash CLI and... |
| 281 | + uniflashCli.Start(); |
| 282 | + |
| 283 | + // ... wait for exit |
| 284 | + uniflashCli.WaitForExit(); |
| 285 | + |
| 286 | + // collect output messages |
| 287 | + return uniflashCli.StandardOutput.ReadToEnd(); |
| 288 | + } |
| 289 | + catch(Exception ex) |
| 290 | + { |
| 291 | + throw new UniflashCliExecutionException(ex.Message); |
| 292 | + } |
| 293 | + } |
| 294 | + } |
| 295 | +} |
0 commit comments