NRF52840 (Adafruit\Ardunio) Wont run code

I have been unable to get any code running on my custom nrf52840 (BMD-340) board using arduino/ platformio. (Just trying to get a blinky running)

I have uploaded the bootloader using nrfconnect and a JLink from adafruit/Adafruit_nRF52_Bootloader

My ini file is as follows:

platform = nordicnrf52
board = nrf52840_dk_adafruit
framework = arduino
build_flags = -DNRF52_S140 -DUSE_LFRC
upload_protocol = nrfjprog
debug_tool = jlink

I have followed what people have done here and here but my code still does not run

When I program a file it either closes the bootloader then precedes to do nothing or the upload completes and then the bootloader immediately reopens. I have also tried modifying the bootloader to change LED pins and the Bootloader state pins which did change the LED but did not change the overall behavior.

Thanks for any help or insight into solving this issue!

Well what I can imagine is that this variant expects the 32.768kHz low frequency crystal to be present

but the uBlox BMD-340 does not provide that on the module, on the P0.00 and P0.01 pins (XTAL1, XTAL2)

So most trivially, it might just be hanging itself up in the clock init code.

Since you have a JLink attached, I would propose to upload a sketch that does not use a bootloader (better debugability) and then start the debugger to see where it hangs or whether it reaches the setup() just fine.

To do so, just duplicate the linker script file in your project as e.g. no_bootloader.ld and change the ORIGIN of the FLASH section from 0x26000 to just 0. Add

board_build.ldscript = no_bootloader.ld

in the platformio.ini accordingly to make use of it.

Then go to the debugging sidebar and hit the “Play” button on “PIO Debug”. See where you end up.

I went through and followed your steps I changed the variant.h file to:

//#define USE_LFXO      // Board uses 32khz crystal for LF
  #define USE_LFRC    // Board uses RC for LF

I am also using the build flags:

build_flags = -DNRF52_S140 -DUSE_LFRC

shouldn’t -DUSE_LFRC Also set the LF clock to the internal RC?

Changing the linker script did allow me to debug thank you for that suggestion! The code runs as expected until Delay(1000); is reached then the program hard faults in gcc_startup_nrf52840.s. This suggests the LF clock is still not properly initialized even with the build flag and the variant.h file change.

Reading the LFCLKSTAT register it says the LF clock is initialized to be internal and running:

 nrfjprog --memrd 0x40000418
0x40000418: 00010000

Am I missing a setting change somewhere else?

Thanks for your help!

I believe I have solved this other issue as well. I was manually flashing the bootloader with a hex file from the adafruit github using nrf programmer which resulted in the weird behavior. Using the platformio Burn bootloader function seems to have corrected the issue. I can only assume some version conflict or an error with the flashing with the bootloader happened.