MetaWear custom firmware

Hello, I am trying to upload my custom firmware to the MetaWear RG (nrf51822) platform but currently without luck. Steps that I have performed:

  • Connected to the device using the debug out of the nRF51DK as described here
  • Uploaded Softdevice S130
  • Uploaded custom application

Now, uploading seems to be working fine, but the application/mcu doesn't seem to start/work. I believe the problem lies in not correctly configuring the application to use the 32MHz external crystal, which the MetaWear uses.

To try to achieve that:


Step 1

it is being suggested to write the value 0xFFFFFF00 to the UICR (User Information Configuration Register) at address 0x10001008 (XTALFREQ).

# After softdevice upload and when the UICR can be written to
# Using nrfprog.exe (not tested, but should work)
nrfjprog.exe --snr <your_jlink_debugger_serial_number> --memwr 0x10001008 --val 0xFFFFFF00
# Using jlink (my method)
w4 0x10001008 0xFFFFFF00

I can verify that address 0x10001008 has been written to correctly.

Step 2

I edited the nrf_drv_config.h to use NRF_CLOCK_XTALFREQ_32MHz.

Step 3 (Edit 1)

based on this I changed the definition at the top of the system.nrf51.c file to

#define __SYSTEM_CLOCK      (32000000UL)

and I also explicitly enabled the use of the external 32MHz crystal in the application code like:

int main(void)
{
    // Set the external high frequency clock source to 32 MHz
    NRF_CLOCK->XTALFREQ = 0xFFFFFF00;

    // Start the external high frequency crystal
    NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
    NRF_CLOCK->TASKS_HFCLKSTART = 1;

    // Wait for the external oscillator to start up
    while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) {}

    while (true) {
        // run application
    }
}

Using nRF51_SDK_9.0.0_2e23562

Any clues? Has anyone successfully managed to run custom code in the metawear?

Any help appreciated! Thanks!

  • well how far through that code do you get? If the HFCLK starts then you know that's not the problem, ie if you get to run application

  • atm, as far as I can understand, the application is not even starting. To check that - first - I used Segger's RTT to print a "hello" right when the main starts and - secondly - I tried to use gdb. With gdb I get:

    (gdb) continue 
    Continuing.
    
    Program received signal SIGTRAP, Trace/breakpoint trap.
    0xfffffffe in ?? ()
    (gdb) where
    #0  0xfffffffe in ?? ()
    #1  <signal handler called>
    #2  0x00000000 in ?? ()
    

    What's more, gdb, if I understood correctly, comes with a limitation when using a softdevice (my case). So it is really hard to debug this situation. Maybe any ideas on how to debug this?

    Note that the RTT method works perfectly with the rRF51DK.

  • If you haven't started the softdevice then there's no issue debugging. In fact just enabling it is fine, only after you start advertising or get in a connection, then you have issues, you're far from there.

    If you're not even starting then it has nothing to do with the crystal. The chip begins running from the internal RC oscillator until the crystal one is enabled. What exact chip revision do you have on your development board and what exact chip revision is there on the Metaware? Not impossible that you're building for a chip with a different amount of RAM and so your stack is being put in a place there's no actual memory.

  • Good point on the Softdevice! You are right!

    • nRF51DK: nrf51422 QFAC (256KB flash and 32KB RAM)
    • MetaWear: nrf51822 (256KB flash and 16KB RAM) - Taiyo Yuden EYSFCNZXX

    So I can see a difference on the RAM. This is the size of the current app:

       text	   data	    bss	    dec	    hex	filename
      66200	    268	   4284	  70752	  11460	build/nrf51422_xxac_s130.elf
    

    You are saying that "your stack is being put in a place there's no actual memory.". But why would or is that happening? How can I check this? thx!

  • For people that haven't figured this out yet, in order to port your work to any custom hardware you need to know

    • Flash/RAM size of the nRF chip and the SoftDevice version. Based on that, the correct information needs to be passed in the linker
    • the use of internal/external clock and the clock-frequency. This information is used to adapt:
      • the system.nrf51.c file

      • in case of external clock also explicitly enable it the beginning of the program in main:

           int main(void)
           {
               // Set the external high frequency clock source to 32 MHz
               NRF_CLOCK->XTALFREQ = 0xFFFFFF00;
        
               // Start the external high frequency crystal
               NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
               NRF_CLOCK->TASKS_HFCLKSTART = 1;
        
               // Wait for the external oscillator to start up
               while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) {}
        
               while (true) {
                   // run application
               }
           }
        

    For more details on this read the following: turlucode.com/.../