Detecting JLINK with nrfjprog

Hi all,

I'm trying something similar to this:

https://devzone.nordicsemi.com/f/nordic-q-a/21628/how-to-use-nrfjprog-dll

i.e. using the nrfprog.dll to connect with a Jlink from a c# application.

Opening the .dll works fine and I now want to use this function:

nrfjprogdll_err_t NRFJPROG_enum_emu_snr(uint32_t serial_numbers[], uint32_t serial_numbers_len, uint32_t * num_available);

wrapped in my App as:

[DllImport("nrfjprog.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int NRFJPROG_enum_emu_snr(UInt32[] serial_numbers,UInt32 serial_numbers_len, out UInt32 num_available);

The num_available returned is correct, i.e. 1 if one JLINK is connected, 2 if two are connected etc. The serial_numbers and serial_numbers_len are, however, empty.

Looking at the arguments one would think that this is because serial_numbers and serial_numbers_len are not "pointers" and can therefore not "return" any values. 

Consequently I try to modify the wrapper to:

private static extern int NRFJPROG_enum_emu_snr(out UInt32[] serial_numbers,out UInt32 serial_numbers_len, out UInt32 num_available);

Unfortunately this result in the following runtime error:

>>Exception thrown: 'System.AccessViolationException' in NRFJPROG.exe
>>An unhandled exception of type 'System.AccessViolationException' occurred in NRFJPROG.exe
>>Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Any ideas on how to make this work ?

Cheers

Eric

Parents Reply Children
  • Hi,

    The function NRFJPROG_read_connected_emu_snr() returns the serial number of the emulator to which it is connected, but before that it is necessary to establish the connection to the emulator, which is done by using the functions NRFJPROG_connect_to_emu_with_snr() or NRFJPROG_connect_to_emu_without_snr().
    In other words, the function NRFJPROG_read_connected_emu_snr()  requires as a precondition a live connection to the debug probe.

    Best regards,
    Dejan

  • Yes, but I'm doing that. Here is the complete code:

    static string JLinkPath = "C:\\Program Files\\SEGGER\\JLink\\JLink_x64.dll";

    [DllImport("nrfjprog.dll", CallingConvention = CallingConvention.Cdecl)]
    private static extern int NRFJPROG_open_dll(string jlink_path, System.IntPtr cb, int family);

    [DllImport("nrfjprog.dll", CallingConvention = CallingConvention.Cdecl)]
    private static extern void NRFJPROG_close_dll();

    [DllImport("nrfjprog.dll", CallingConvention = CallingConvention.Cdecl)]
    private static extern int NRFJPROG_connect_to_emu_without_snr(int clock_speed_in_khz);

    [DllImport("nrfjprog.dll", CallingConvention = CallingConvention.Cdecl)]
    private static extern int NRFJPROG_read_connected_emu_snr(out int serial_number);

    private void btnJlink_Click(object sender, RoutedEventArgs e)
    {

    int snr,clock_speed_in_khz = 4000;
    IntPtr cb = new IntPtr();

    NRFJPROG_close_dll();

    NRFJPROG_open_dll(JLinkPath, cb, 1);

    NRFJPROG_connect_to_emu_without_snr(clock_speed_in_khz);

    NRFJPROG_read_connected_emu_snr(out snr);

    }

    And as previously stated, snr will have the correct serial number IF the emulator is (physically) connected to a target but "0" if it is not.

    \Eric

  • Hi,

    Could you please provide the return value of the NRFJPROG_read_connected_emu_snr() in the failing case?

    Best regards,
    Dejan

  • Like I said it is "0", i.e. a valid null terminated string with one character.

    \Eric

  • Hi,

    Could you provide  a log either by providing a callback to the cb parameter or by setting jlink to log the output to a file (J-Link log file)?

    Best regards,
    Dejan

Related