Skip to content

Commit

Permalink
Add verbosity to operation to set Silabs VCP baud rate (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
josesimoes authored Jul 14, 2022
1 parent 181bbb4 commit d0d8578
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
4 changes: 2 additions & 2 deletions nanoFirmwareFlasher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,7 @@ static async Task RunOptionsAndReturnExitCodeAsync(Options o)
}

// set VCP baud rate (if needed)
_ = SilinkCli.SetVcpBaudRate(o.JLinkDeviceId is null ? connectedJLinkDevices.First() : "");
_ = SilinkCli.SetVcpBaudRate(o.JLinkDeviceId is null ? connectedJLinkDevices.First() : "", _verbosityLevel);

// set verbosity
jlinkDevice.Verbosity = _verbosityLevel;
Expand Down Expand Up @@ -1201,7 +1201,7 @@ static async Task RunOptionsAndReturnExitCodeAsync(Options o)
operationPerformed = true;

// set VCP baud rate (if needed)
_ = SilinkCli.SetVcpBaudRate(o.JLinkDeviceId is null ? connectedJLinkDevices.First() : "");
_ = SilinkCli.SetVcpBaudRate(o.JLinkDeviceId is null ? connectedJLinkDevices.First() : "", _verbosityLevel);

if (_exitCode != ExitCodes.OK)
{
Expand Down
61 changes: 54 additions & 7 deletions nanoFirmwareFlasher/SilinkCli.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,26 @@ public static ExitCodes SetVcpBaudRate(
return ExitCodes.E8002;
}

Console.ForegroundColor = ConsoleColor.White;

// launch silink
if (verbosity >= VerbosityLevel.Detailed)
{
Console.WriteLine("Launching silink...");
}

var silinkCli = RunSilinkCLI(Path.Combine(probeId));

var silinkSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint silinkEndPoint = new(IPAddress.Parse("127.0.0.1"), SilinkAdminPort);

try
{
if (verbosity >= VerbosityLevel.Diagnostic)
{
Console.WriteLine("Connecting to admin console...");
}

silinkSocket.Connect(silinkEndPoint);

Thread.Sleep(250);
Expand All @@ -66,13 +78,23 @@ public static ExitCodes SetVcpBaudRate(
byte[] buffer = Encoding.Default.GetBytes("serial vcom\r");
silinkSocket.Send(buffer);

if (verbosity >= VerbosityLevel.Diagnostic)
{
Console.WriteLine("Querying current config...");
}

Thread.Sleep(250);

buffer = new byte[1024];
int receiveCount = silinkSocket.Receive(buffer, 0, buffer.Length, 0);

var currentConfig = Encoding.Default.GetString(buffer, 0, receiveCount);

if (verbosity >= VerbosityLevel.Diagnostic)
{
Console.WriteLine($"{currentConfig}");
}

if (!string.IsNullOrEmpty(currentConfig))
{
// interpret reply
Expand All @@ -86,21 +108,30 @@ public static ExitCodes SetVcpBaudRate(
// verify current setting
if (int.TryParse(currentBaud.Groups["baudrate"].Value, out int baudRate) && baudRate == TargetBaudRate)
{
if (verbosity >= VerbosityLevel.Detailed)
{
Console.WriteLine("VCP baud rate it's correct! Nothing to do here.");
}

return ExitCodes.OK;
}
}

// need to set baud rate because it's different

if (verbosity >= VerbosityLevel.Normal)
if (verbosity == VerbosityLevel.Normal)
{
Console.Write("Setting VCP baud rate...");
Console.Write("Trying to set VCP baud rate...");
}
else if (verbosity > VerbosityLevel.Normal)
{
Console.WriteLine("Trying to set VCP baud rate...");
}

Thread.Sleep(250);

// compose command
buffer = Encoding.Default.GetBytes("serial vcom config speed {TargetBaudRate}\r");
buffer = Encoding.Default.GetBytes($"serial vcom config speed {TargetBaudRate}\r");
silinkSocket.Send(buffer);

Thread.Sleep(250);
Expand All @@ -110,9 +141,14 @@ public static ExitCodes SetVcpBaudRate(

var opResult = Encoding.Default.GetString(buffer, 0, receiveCount);

if (opResult.Contains("Baudrate set to 921600 bps"))
if (verbosity >= VerbosityLevel.Diagnostic)
{
if (verbosity >= VerbosityLevel.Normal)
Console.WriteLine($"{opResult}");
}

if (opResult.Contains($"Baudrate set to {TargetBaudRate} bps"))
{
if (verbosity == VerbosityLevel.Normal)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(" OK");
Expand All @@ -121,12 +157,16 @@ public static ExitCodes SetVcpBaudRate(

Console.ForegroundColor = ConsoleColor.White;
}
else if (verbosity > VerbosityLevel.Normal)
{
Console.WriteLine("Success!");
}

return ExitCodes.OK;
}
else
{
if (verbosity >= VerbosityLevel.Normal)
if (verbosity == VerbosityLevel.Normal)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("FAILED!");
Expand All @@ -139,6 +179,13 @@ public static ExitCodes SetVcpBaudRate(
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("");
}
else if (verbosity > VerbosityLevel.Normal)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("FAILED!");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("");
}

return ExitCodes.E8002;
}
Expand All @@ -149,7 +196,7 @@ public static ExitCodes SetVcpBaudRate(
Console.WriteLine("");
Console.ForegroundColor = ConsoleColor.Red;

Console.WriteLine($"Error occurred: {ex.Message}");
Console.WriteLine($"Exception occurred: {ex.Message}");

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("");
Expand Down

0 comments on commit d0d8578

Please sign in to comment.