Skip to content

Commit 5330b2d

Browse files
authored
Add option to set Silabs JLink VCP baud rate (#144)
1 parent d0d8578 commit 5330b2d

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

nanoFirmwareFlasher/Options.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ public class Options
179179
HelpText = "ID of the J-Link device to update. If not specified the first connected J-Link device will be used.")]
180180
public string JLinkDeviceId { get; set; }
181181

182+
[Option(
183+
"setvcpbr",
184+
Required = false,
185+
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.")]
186+
public int? SetVcpBaudRate { get; set; }
187+
182188
#endregion
183189

184190

nanoFirmwareFlasher/Program.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,8 +1148,14 @@ static async Task RunOptionsAndReturnExitCodeAsync(Options o)
11481148
Console.WriteLine($"Connected to J-Link device with ID {jlinkDevice.ProbeId}");
11491149
}
11501150

1151-
// set VCP baud rate (if needed)
1152-
_ = SilinkCli.SetVcpBaudRate(o.JLinkDeviceId is null ? connectedJLinkDevices.First() : "", _verbosityLevel);
1151+
// set VCP baud rate (if requested)
1152+
if (o.SetVcpBaudRate.HasValue)
1153+
{
1154+
_ = SilinkCli.SetVcpBaudRate(
1155+
o.JLinkDeviceId is null ? connectedJLinkDevices.First() : "",
1156+
o.SetVcpBaudRate.Value,
1157+
_verbosityLevel);
1158+
}
11531159

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

12011207
operationPerformed = true;
12021208

1203-
// set VCP baud rate (if needed)
1204-
_ = SilinkCli.SetVcpBaudRate(o.JLinkDeviceId is null ? connectedJLinkDevices.First() : "", _verbosityLevel);
1209+
if (o.SetVcpBaudRate.HasValue)
1210+
{
1211+
// set VCP baud rate (if needed)
1212+
_ = SilinkCli.SetVcpBaudRate(
1213+
o.JLinkDeviceId is null ? connectedJLinkDevices.First() : "",
1214+
o.SetVcpBaudRate.Value,
1215+
_verbosityLevel);
1216+
}
12051217

12061218
if (_exitCode != ExitCodes.OK)
12071219
{

nanoFirmwareFlasher/SilinkCli.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,22 @@ internal class SilinkCli
1919
{
2020
private const int SilinkTelnetPort = 49000;
2121
private const int SilinkAdminPort = SilinkTelnetPort + 2;
22-
private const int TargetBaudRate = 921600;
22+
private const int DefaultBaudRate = 921600;
2323

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

55+
// store baud rate value
56+
int targetBaudRate = baudRate == 0 ? DefaultBaudRate : baudRate;
57+
5358
Console.ForegroundColor = ConsoleColor.White;
5459

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

103108
var myRegex1 = new Regex(regexPattern, RegexOptions.Multiline);
104-
var currentBaud = myRegex1.Match(currentConfig);
109+
var currentVcomConfig = myRegex1.Match(currentConfig);
105110

106-
if (currentBaud.Success)
111+
if (currentVcomConfig.Success)
107112
{
108113
// verify current setting
109-
if (int.TryParse(currentBaud.Groups["baudrate"].Value, out int baudRate) && baudRate == TargetBaudRate)
114+
if (int.TryParse(currentVcomConfig.Groups["baud rate"].Value, out int currentBaudRate) && currentBaudRate == targetBaudRate)
110115
{
111116
if (verbosity >= VerbosityLevel.Detailed)
112117
{
@@ -131,7 +136,7 @@ public static ExitCodes SetVcpBaudRate(
131136
Thread.Sleep(250);
132137

133138
// compose command
134-
buffer = Encoding.Default.GetBytes($"serial vcom config speed {TargetBaudRate}\r");
139+
buffer = Encoding.Default.GetBytes($"serial vcom config speed {targetBaudRate}\r");
135140
silinkSocket.Send(buffer);
136141

137142
Thread.Sleep(250);
@@ -146,7 +151,7 @@ public static ExitCodes SetVcpBaudRate(
146151
Console.WriteLine($"{opResult}");
147152
}
148153

149-
if (opResult.Contains($"Baudrate set to {TargetBaudRate} bps"))
154+
if (opResult.Contains($"Baudrate set to {targetBaudRate} bps"))
150155
{
151156
if (verbosity == VerbosityLevel.Normal)
152157
{

0 commit comments

Comments
 (0)