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
  • Hi,

    What is the initial value of the serial_numbers_len?

    Best regards,
    Dejan

  • Hi,

    I initiated serial_numbers_len to 0 as suggested in the header file.

    However, I just discovered that when NRFJPROG_connect_to_emu_without_snr is called, and several JLINKs are attached, a selection pop-up is automatically created. Thus, no need to implement this myself.

    \Eric

  • Hi,

    Did this solve your problem?

    Best regards,
    Dejan

  • No, it did not solve the issue with NRFJPROG_enum_emu_snr(..). Just saying that I circumvented the need for it.

    \Eric

  • Hi,

    Could you please check if your serial_numbers array actually points to an array? Additionally, make sure that the size of that array is passed to the enum_emu_snr.

    Best regards,
    Dejan

Reply
  • Hi,

    Could you please check if your serial_numbers array actually points to an array? Additionally, make sure that the size of that array is passed to the enum_emu_snr.

    Best regards,
    Dejan

Children
  • This is the code I execute:

    private void btnProgram_Click(object sender, RoutedEventArgs e)
    {
    int retVal;
    UInt32[] snrs = new UInt32[10];
    UInt32 snlen = new UInt32();
    UInt32 numAvail = new UInt32();

    NRFJPROG_close_dll();

    retVal = NRFJPROG_open_dll(JLinkPath, Marshal.GetFunctionPointerForDelegate<msg_callback_cb>(mCallBack), 1);

    retVal = NRFJPROG_enum_emu_snr(snrs, snlen, out numAvail);

    }

    As previously stated; retVal is 0, numAvail is 2 but snrs is empty and snlen is 0.

    \Eric

  • Hi,

    You need to ensure that serial_numbers array is not empty and that serial_numbers_length is not 0. Additionally, you need to ensure that retVal value is not zero. 

    Best regards,
    Dejan

  • Hi again,

    Came across a similar problem with the NRFJPROG_read_connected_emu_snr (out snr) function.  If I call it when the JLink is connected to a nRF52 it returns the correct serial number of the JLink. If it is NOT connected to a nRF52 it will simply return "0" as the serial number. 

    This does not make any sense, Any there way around it ?

    \Eric

  • Hi,

    As it is described in nrfjprog header file for specific family (for example jlinkarm_nrf52_nrfjprogdll.h for 52 family), NRFJPROG_read_connected_emu_snr() can be used to check emulator's serial number after connection is established. Before the execution of the function DLL must be opened and connection to the emulator must be established.

    Best regards,
    Dejan

  • Dejan,

    Connection established with what ? Why does the emulator need to be connected to a target in order for its (the emulator's) serial number to be read ?

    \Eric