<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>How to use nrfjprog.dll</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/21628/how-to-use-nrfjprog-dll</link><description>We are trying to use nrfjprog.dll in a c# program to help us with manufacturing and quality assurance.
We are able to load and call the functions from c# and all the functions work one by one, but if we call NRFJPROG_open_dll and then one of the other</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 19 Feb 2018 09:22:48 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/21628/how-to-use-nrfjprog-dll" /><item><title>RE: How to use nrfjprog.dll</title><link>https://devzone.nordicsemi.com/thread/121206?ContentTypeID=1</link><pubDate>Mon, 19 Feb 2018 09:22:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b6fa0f90-5d85-44b7-8f51-165677f36cae</guid><dc:creator>vo2Peter</dc:creator><description>&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/members/nkildegaard"&gt;Nicolai Kildegaard&lt;/a&gt; Thank you very much for posting this. It was the starting point for this sample application:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/VO2Master/NrfSeggerRttLogger"&gt;https://github.com/VO2Master/NrfSeggerRttLogger&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Not sure if this is the right place to post this, but I wanted to share since it has taken me all day and night to figure this out. For a while I thought I would have to purchase the Segger SDK for its sample projects, but nope! Astounded no one has posted&amp;nbsp;something like this yet.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to use nrfjprog.dll</title><link>https://devzone.nordicsemi.com/thread/84905?ContentTypeID=1</link><pubDate>Wed, 26 Apr 2017 13:57:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b24a591a-bd45-4e32-87f6-8a03129cab3f</guid><dc:creator>Simen August Tinderholt</dc:creator><description>&lt;p&gt;I would check the channel size by runnning NRFJPROG_rtt_read_channel_info. It might be set to 1.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to use nrfjprog.dll</title><link>https://devzone.nordicsemi.com/thread/84904?ContentTypeID=1</link><pubDate>Wed, 26 Apr 2017 13:46:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:76be83d5-7858-4a98-afb0-79ddf3135c23</guid><dc:creator>Nicolai Kildegaard</dc:creator><description>&lt;p&gt;Yes you are right. That was exactly what happened I just missed that the return_value wasn&amp;#39;t success.&lt;/p&gt;
&lt;p&gt;We tried to pass both strings and char arrays to the NRFJPROG_rtt_read and NRFJPROG_rtt_write functions, but we couldn&amp;#39;t make it work. In the end we ended up trying with a single char and it worked. Is there any reason for this?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to use nrfjprog.dll</title><link>https://devzone.nordicsemi.com/thread/84903?ContentTypeID=1</link><pubDate>Wed, 26 Apr 2017 13:25:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:adfd68a4-cec8-4d72-89e0-917f323732d8</guid><dc:creator>Simen August Tinderholt</dc:creator><description>&lt;p&gt;NRFJPROG_read_device_family is not implemented on the device specific DLLs, and probably returned an invalid operation warning. It&amp;#39;s only implemented for the unknown-family DLL, where it attempts to read the family from the device, this wouldn&amp;#39;t make much sense in the nrf51 and nrf52 dlls where we assume we already know what family we are using. As for the return value it probably did nothing, and left your device_family with it&amp;#39;s 0-initalized value, which corresponds to NRF51_family.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to use nrfjprog.dll</title><link>https://devzone.nordicsemi.com/thread/84902?ContentTypeID=1</link><pubDate>Wed, 26 Apr 2017 12:58:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:40065274-b218-4b1f-8cfd-927ca85e4070</guid><dc:creator>Nicolai Kildegaard</dc:creator><description>&lt;p&gt;After a lot of debugging we found out the cause of the error was how the msg_callback_cb was defined. After changing that to use a IntPtr and making sure the garbage collector didn&amp;#39;t interfere,  it worked.&lt;/p&gt;
&lt;p&gt;We managed to test all the functions we needed, so I will add the entire code here if someone is in the same problem in the future.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;    public class NrfjprogHandler
{
    static string JLinkPath = &amp;quot;C:\\Program Files (x86)\\SEGGER\\JLink_V612a\\JLinkARM.dll&amp;quot;;

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    private delegate void msg_callback_cb([In, MarshalAs(UnmanagedType.LPStr)] string msg_str);

    private void msg_callback([In, MarshalAs(UnmanagedType.LPStr)] string msg_str)
    {
        return;
    }
    //Instance to keep the garbage collector from removing it.
    private msg_callback_cb mCallBack;


    [DllImport(&amp;quot;Flash/nrfjprog/nrfjprog.dll&amp;quot;, CallingConvention = CallingConvention.Cdecl)]
    private static extern nrfjprogdll_err_t NRFJPROG_dll_version(out int major, out int minor, out char revision);

    [DllImport(&amp;quot;Flash/nrfjprog/nrfjprog.dll&amp;quot;, CallingConvention = CallingConvention.Cdecl)]
    private static extern nrfjprogdll_err_t NRFJPROG_is_dll_open(out bool opened);

    [DllImport(&amp;quot;Flash/nrfjprog/nrfjprog.dll&amp;quot;, CallingConvention = CallingConvention.Cdecl)]
    private static extern nrfjprogdll_err_t NRFJPROG_open_dll(string jlink_path, System.IntPtr cb, device_family_t family);

    [DllImport(&amp;quot;Flash/nrfjprog/nrfjprog.dll&amp;quot;, CallingConvention = CallingConvention.Cdecl)]
    private static extern void NRFJPROG_close_dll();

    [DllImport(&amp;quot;Flash/nrfjprog/nrfjprog.dll&amp;quot;, CallingConvention = CallingConvention.Cdecl)]
    private static extern nrfjprogdll_err_t NRFJPROG_connect_to_emu_with_snr(out int serial_number, out int clock_speed_in_khz);

    [DllImport(&amp;quot;Flash/nrfjprog/nrfjprog.dll&amp;quot;, CallingConvention = CallingConvention.Cdecl)]
    private static extern nrfjprogdll_err_t NRFJPROG_connect_to_emu_without_snr(int clock_speed_in_khz);

    [DllImport(&amp;quot;Flash/nrfjprog/nrfjprog.dll&amp;quot;, CallingConvention = CallingConvention.Cdecl)]
    private static extern nrfjprogdll_err_t NRFJPROG_connect_to_device();

    [DllImport(&amp;quot;Flash/nrfjprog/nrfjprog.dll&amp;quot;, CallingConvention = CallingConvention.Cdecl)]
    private static extern nrfjprogdll_err_t NRFJPROG_is_rtt_started(out bool started);

    [DllImport(&amp;quot;Flash/nrfjprog/nrfjprog.dll&amp;quot;, CallingConvention = CallingConvention.Cdecl)]
    private static extern nrfjprogdll_err_t NRFJPROG_rtt_set_control_block_address(int address);

    [DllImport(&amp;quot;Flash/nrfjprog/nrfjprog.dll&amp;quot;, CallingConvention = CallingConvention.Cdecl)]
    private static extern nrfjprogdll_err_t NRFJPROG_rtt_start();

    [DllImport(&amp;quot;Flash/nrfjprog/nrfjprog.dll&amp;quot;, CallingConvention = CallingConvention.Cdecl)]
    private static extern nrfjprogdll_err_t NRFJPROG_rtt_is_control_block_found(out bool is_control_block_found);

    [DllImport(&amp;quot;Flash/nrfjprog/nrfjprog.dll&amp;quot;, CallingConvention = CallingConvention.Cdecl)]
    private static extern nrfjprogdll_err_t NRFJPROG_rtt_stop();

    [DllImport(&amp;quot;Flash/nrfjprog/nrfjprog.dll&amp;quot;, CallingConvention = CallingConvention.Cdecl)]
    private static extern nrfjprogdll_err_t NRFJPROG_rtt_read(int up_channel_index, out char data, int data_len, out int data_read);

    [DllImport(&amp;quot;Flash/nrfjprog/nrfjprog.dll&amp;quot;, CallingConvention = CallingConvention.Cdecl)]
    private static extern nrfjprogdll_err_t NRFJPROG_rtt_write(int down_channel_index, [In, MarshalAs(UnmanagedType.LPWStr)]  string data, int data_len, out int data_written);

    [DllImport(&amp;quot;Flash/nrfjprog/nrfjprog.dll&amp;quot;, CallingConvention = CallingConvention.Cdecl)]
    private static extern nrfjprogdll_err_t NRFJPROG_rtt_read_channel_count(out int down_channel_number, out int up_channel_number);

    [DllImport(&amp;quot;Flash/nrfjprog/nrfjprog.dll&amp;quot;, CallingConvention = CallingConvention.Cdecl)]
    private static extern nrfjprogdll_err_t NRFJPROG_read_device_family(out device_family_t family);

    [DllImport(&amp;quot;Flash/nrfjprog/nrfjprog.dll&amp;quot;, CallingConvention = CallingConvention.Cdecl)]
    private static extern nrfjprogdll_err_t NRFJPROG_read_device_version(out device_version_t version);

    public NrfjprogHandler()
    {
        //Create an instance of the callback
        mCallBack = new msg_callback_cb(msg_callback);
    }

    public void TestNrfj()
    {
        int major, minor;
        char revision;
        bool open, started, found;
        nrfjprogdll_err_t return_value;
        int clock_speed_in_khz = 4000;
        int down_channel_number, up_channel_number;
        
        NRFJPROG_close_dll();

        return_value = NRFJPROG_open_dll(JLinkPath, Marshal.GetFunctionPointerForDelegate&amp;lt;msg_callback_cb&amp;gt;(mCallBack), device_family_t.NRF52_FAMILY);

        return_value = NRFJPROG_is_dll_open(out open);

        return_value = NRFJPROG_dll_version(out major, out minor, out revision);
        
        return_value = NRFJPROG_connect_to_emu_without_snr(clock_speed_in_khz);

        return_value = NRFJPROG_connect_to_device();

        device_family_t device_family;
        device_version_t device_version;

        return_value = NRFJPROG_read_device_family(out device_family);  //This function seems to be returning a NRF51_FAMILY even though we are using a NRF52832
        return_value = NRFJPROG_read_device_version(out device_version);    //This function does a better job than the previous one.

        return_value = NRFJPROG_rtt_start();

        return_value = NRFJPROG_is_rtt_started(out started);

        return_value = NRFJPROG_rtt_is_control_block_found(out found);

        return_value = NRFJPROG_rtt_read_channel_count( out down_channel_number,out up_channel_number);
        
        int dataWritten;

        //This only works if sending one char at a time
        return_value = NRFJPROG_rtt_write(0, &amp;quot;T&amp;quot;, 1, out dataWritten);
        return_value = NRFJPROG_rtt_write(0, &amp;quot;1&amp;quot;, 1, out dataWritten);

        int dataRead;
        char read_char;
        //This only works if reading one char at a time
        return_value = NRFJPROG_rtt_read(0, out read_char, 1, out dataRead);


        return;
    }

}
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to use nrfjprog.dll</title><link>https://devzone.nordicsemi.com/thread/84901?ContentTypeID=1</link><pubDate>Wed, 26 Apr 2017 12:32:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:810adb7a-a99f-4e03-b4fa-4f34dea5ce68</guid><dc:creator>bjorn-spockeli</dc:creator><description>&lt;p&gt;Could you check the return_value from NRFJPROG_open_dll()? The developer also suggested replacing &lt;code&gt;out&lt;/code&gt; with &lt;code&gt;*&lt;/code&gt; .&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to use nrfjprog.dll</title><link>https://devzone.nordicsemi.com/thread/84900?ContentTypeID=1</link><pubDate>Wed, 26 Apr 2017 12:21:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c26ab992-ecd4-47e7-b3c2-0269f1e75921</guid><dc:creator>bjorn-spockeli</dc:creator><description>&lt;p&gt;Hard to say what the cuase is, apparently &lt;code&gt;Attempted to read or write protected memory. This is often an indication that other memory is corrupt&lt;/code&gt; could be anything according to Google. I spoke to one of the developers and his comment was that this is not an error returned by our dll, but the function import looks good and that he cant see any obvious mistakes. He suggested that you take a look at the 64-bit dll distributed with &lt;a href="https://github.com/NordicSemiconductor/pynrfjprog"&gt;pynrfjprog&lt;/a&gt; if you&amp;#39;re running this in 64-bit Windows.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>