Skip to content

Commit

Permalink
Add option to set Silabs JLink VCP baud rate (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
josesimoes authored Jul 14, 2022
1 parent d0d8578 commit 5330b2d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
6 changes: 6 additions & 0 deletions nanoFirmwareFlasher/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ public class Options
HelpText = "ID of the J-Link device to update. If not specified the first connected J-Link device will be used.")]
public string JLinkDeviceId { get; set; }

[Option(
"setvcpbr",
Required = false,
HelpText = "Set baud rate of J-Link Virtual COM Port. If a value is not specified it will use the default value for Wire Protocol.")]
public int? SetVcpBaudRate { get; set; }

#endregion


Expand Down
20 changes: 16 additions & 4 deletions nanoFirmwareFlasher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1148,8 +1148,14 @@ static async Task RunOptionsAndReturnExitCodeAsync(Options o)
Console.WriteLine($"Connected to J-Link device with ID {jlinkDevice.ProbeId}");
}

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

// set verbosity
jlinkDevice.Verbosity = _verbosityLevel;
Expand Down Expand Up @@ -1200,8 +1206,14 @@ static async Task RunOptionsAndReturnExitCodeAsync(Options o)

operationPerformed = true;

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

if (_exitCode != ExitCodes.OK)
{
Expand Down
17 changes: 11 additions & 6 deletions nanoFirmwareFlasher/SilinkCli.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,22 @@ internal class SilinkCli
{
private const int SilinkTelnetPort = 49000;
private const int SilinkAdminPort = SilinkTelnetPort + 2;
private const int TargetBaudRate = 921600;
private const int DefaultBaudRate = 921600;

/// <summary>
/// This will set the baud rate of the VCP in a Silabs Jlink board.
/// Before setting the value the devices is queried and the new setting is applied only if needed.
/// </summary>
/// <param name="probeId">Id of the JLink probe to adjust the baud rate.</param>
/// <param name="baudRate">Value of baud rate to set.</param>
/// <param name="verbosity">Verbosity level for the operation. <see cref="VerbosityLevel.Quiet"/> will be used if not specified.</param>
/// <returns></returns>
/// <remarks>
/// Currently this operation is only supported in Windows machines.
/// </remarks>
public static ExitCodes SetVcpBaudRate(
string probeId,
int baudRate,
VerbosityLevel verbosity = VerbosityLevel.Quiet)
{
// check that we can run on this platform
Expand All @@ -50,6 +52,9 @@ public static ExitCodes SetVcpBaudRate(
return ExitCodes.E8002;
}

// store baud rate value
int targetBaudRate = baudRate == 0 ? DefaultBaudRate : baudRate;

Console.ForegroundColor = ConsoleColor.White;

// launch silink
Expand Down Expand Up @@ -101,12 +106,12 @@ public static ExitCodes SetVcpBaudRate(
const string regexPattern = "(?:Stored port speed : )(?'baudrate'\\d+)";

var myRegex1 = new Regex(regexPattern, RegexOptions.Multiline);
var currentBaud = myRegex1.Match(currentConfig);
var currentVcomConfig = myRegex1.Match(currentConfig);

if (currentBaud.Success)
if (currentVcomConfig.Success)
{
// verify current setting
if (int.TryParse(currentBaud.Groups["baudrate"].Value, out int baudRate) && baudRate == TargetBaudRate)
if (int.TryParse(currentVcomConfig.Groups["baud rate"].Value, out int currentBaudRate) && currentBaudRate == targetBaudRate)
{
if (verbosity >= VerbosityLevel.Detailed)
{
Expand All @@ -131,7 +136,7 @@ public static ExitCodes SetVcpBaudRate(
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 @@ -146,7 +151,7 @@ public static ExitCodes SetVcpBaudRate(
Console.WriteLine($"{opResult}");
}

if (opResult.Contains($"Baudrate set to {TargetBaudRate} bps"))
if (opResult.Contains($"Baudrate set to {targetBaudRate} bps"))
{
if (verbosity == VerbosityLevel.Normal)
{
Expand Down

0 comments on commit 5330b2d

Please sign in to comment.