Hello,
I am using nRF52840 DK's P20 SWD to communicate with an external nRF52840 board. My setup works fine with nrfjprog running from command line. To communicate with the external nRF I established RTT communication using jlinkarm_nrf52_nrfjprog.dll in C#.
I have loaded the libraries as described here.
I have successfully opened the JLink_x64.dll, connected and ask information about the board using NRFJPROG_read().
I have also successfully established RTT connection using NRFJPROG_rtt_start() and it finds the control block automatically (verified with NRFJPROG_rtt_is_control_block_found).
The point where I am stuck is using the RTT read write functions of the dll.
I have the following function in C#:
public override bool SendCommand(string command, int timeoutMsec, out CommandResult result)
{
//Create command (Place for protocol design)
StringBuilder sbTemp = new StringBuilder();
sbTemp.Append(command);
sbTemp.Append('\r');
sbTemp.Append('\n');
Thread WriteAndRead = new Thread(p => {
Console.WriteLine("Hello!1");
nrfjprog.WriteRTT(sbTemp.ToString());
Console.WriteLine("Hello!2");
_logCommand(command + " written to DUT");
for (int i = 0; i < timeoutMsec; i++)
{
if (nrfjprog.ReadRTT(out string data) && data != string.Empty)
{
if (data[data.Length - 2] == '\r' && data[data.Length - 1] == '\n')
{
data = data.ToString().Substring(0, data.Length - 2);
m_CommandResult = CommandResult.ParseResponse(data);
_logCommand(data + " received from DUT.");
return;
}
}
Thread.Sleep(1);
}
});
WriteAndRead.Start();
Thread.Sleep(10000); //Wait for WriteAndRead to finish
Console.WriteLine("Hello!3");
//Wait for thread and use m_CommandResult here
result = m_CommandResult;
return true;
}
With the write and read rtt functions:
public bool WriteRTT(string data, int channel = 0)
{
NRFJPROG_RESULT return_value;
if (data == null)
return false;
return_value = NRFJPROG_rtt_write(channel, data, data.Length, out int data_written);
if (return_value != NRFJPROG_RESULT.SUCCESS || data_written != data.Length)
return false;
else
return true;
}
public bool ReadRTT(out string data)
{
NRFJPROG_RESULT return_value;
StringBuilder sb = new StringBuilder(1024);
return_value = NRFJPROG_rtt_read(0, sb, sb.Capacity, out int data_read);
data = data_read > 0 ? sb.ToString() : string.Empty;
if (return_value != NRFJPROG_RESULT.SUCCESS || data_read == 0) return false;
else return true;
}
RTT Communication works! But I got one problem. The output from Console.WriteLine(s) are:
Hello!1
Hello!3
Hello!2
I want it to be
Hello!1
Hello!2
Hello!3
Using the debugger, the WriteAndRead thread waits at return_value = NRFJPROG_rtt_write(channel, data, data.Length, out int data_written); until the caller thread is finished. Why? There is no information about it in the jlinkarm_nrf52_nrfjprogdll.h. I can't use the results I want. (Yes I can use a C# handler, but then I'd have to refactor alot of code.)
I am using nrfjprog version 10.15.2.0 and JLink version 7.58.2.0