pylink icon indicating copy to clipboard operation
pylink copied to clipboard

nRF5340 flashing issue: Failed to prepare for programming. Could not preserve target memory.

Open vpmdt opened this issue 2 years ago • 11 comments

Hi, struggling with flashing nRF5340. If I flash it manually with J-Flash Lite, first NET then APP core, it works fine. I need to flash both cores with no reset between them, NET and APP respectively, because of the readback protection.

jlk = pylink.JLink()
jlk.open(serial_no=int(jlink_sn))
jlk.set_tif(pylink.enums.JLinkInterfaces.SWD)
....
....
    jlk.connect(chip_name='nrf5340_xxaa_net', verbose=True)
    jlk.flash(binstr_obj1, min_addr1)

    jlk.connect(chip_name='nrf5340_xxaa_app', verbose=True)
    jlk.flash(binstr_obj2, min_addr2)

first flash is succesfully done, but after second flash I get error: Failed to prepare for programming. Could not preserve target memory.

Any ideas? Thanks!!

vpmdt avatar May 23 '23 11:05 vpmdt

Do you have any more logs? Is it possible that the chip is not being properly held in reset?

hkpeprah avatar May 23 '23 14:05 hkpeprah

I'm trying more things to get it working, so here is the only succesful setup for flashing:

  1. First the manual flash of merged hex (net and app core) is done via nRF Programmer
  2. Then I can run the script which is
jlk = pylink.JLink()
jlk.open(serial_no=int(jlink_sn))
jlk.set_tif(pylink.enums.JLinkInterfaces.SWD)

# SWDSelect
jlk.coresight_configure()

# SWDWriteDP 1 0x50000000
jlk.coresight_write(reg=1, data=0x50000000, ap=False)

# SWDWriteDP 2 0x020000F0
jlk.coresight_write(reg=2, data=0x020000F0, ap=False)

# SWDReadAP 3
jlk.coresight_read(reg=3, ap=True)
time.sleep(0.5)

# SWDReadAP 3
jlk.coresight_read(reg=3, ap=True)
time.sleep(0.5)

# SWDWriteDP 2 0x02000000
jlk.coresight_write(reg=2, data=0x02000000, ap=False)

# SWDWriteAP 1 1
jlk.coresight_write(reg=1, data=1, ap=True)

# SWDReadAP 2
jlk.coresight_read(reg=2, ap=True)
time.sleep(0.5)

# SWDReadAP 2
jlk.coresight_read(reg=2, ap=True)
time.sleep(0.5)

# SWDReadAP 2
jlk.coresight_read(reg=2, ap=True)
time.sleep(0.5)

# SWDWriteDP 2 0x03000000
jlk.coresight_write(reg=2, data=0x03000000, ap=False)

# SWDWriteAP 1 1
jlk.coresight_write(reg=1, data=1, ap=True)

# SWDReadAP 2
jlk.coresight_read(reg=2, ap=True)
time.sleep(0.5)

# SWDReadAP 2
jlk.coresight_read(reg=2, ap=True)
time.sleep(0.5)

# SWDReadAP 2
jlk.coresight_read(reg=2, ap=True)
time.sleep(0.5)


jlk.connect(chip_name='nrf5340_xxaa_net', verbose=True)
jlk.flash(binstr_obj1, min_addr1)

jlk.connect(chip_name='nrf5340_xxaa_app', verbose=True)
jlk.flash(binstr_obj2, min_addr2)

If the chip is fully erased it fails as described in my first comment - it fails when the second flash is executed (when the app core is being flashed). Let me know if you need other information. Thank you!

PS: the sequence of reading and writing to the DAP registers is basicaly this: https://devzone.nordicsemi.com/nordic/nordic-blog/b/blog/posts/allowing-debugger-access-to-an-nrf5340 without that I can not even connect to the target. If this sequence is skipped, I get error:

pylink.errors.JLinkException: Could not find supported CPU. Could not find core in Coresight setup

vpmdt avatar May 29 '23 08:05 vpmdt

I would try playing around with the reset strategies here: https://pylink.readthedocs.io/en/latest/pylink.html#pylink.jlink.JLink.set_reset_strategy

It's possible that the core starts running after you flash the first asset, and changing the reset strategy might happen. There's also the set_reset_pin_high() and set_reset_pin_low() APIs where you can manually hold the core in reset while you do your flashing.

hkpeprah avatar Jun 05 '23 15:06 hkpeprah

Hi, has this been resolved already?

enjeru15 avatar Oct 20 '23 04:10 enjeru15