square / pylink

Python Library for device debugging/programming via J-Link
https://pylink.readthedocs.io/en/latest/
Other
352 stars 127 forks source link

Error when programming or erasing Kinetis microcontrollers #185

Open Agarmonz opened 1 year ago

Agarmonz commented 1 year ago

Hi!

I'm trying to program devices with different types of microcontrollers, for example: KE15, KL26, K64... In this case all from the Kinetis family.

I find these two behaviours that I don't know how to solve:

In all cases the error is as follows (mainly): Failed to erase sectors 0 @ address 0x00000000 (Algo91: Flash protection violation. Flash is write-protected.) Failed to erase sectors.

Summarising the code steps I do the following:

  1. jlink.open()
  2. jlink.set_tif()
  3. jlink.connect()
  4. jlink.unlock()
  5. jlink.halt()
  6. jlink.erase()
  7. jlink.unlock()
  8. jlink.flash()

Unlock seems to work, so I have tried programming directly, without a previous erase. The failure is the same.

I have done something similar for other microcontroller families like LPC and iMX, and I have not had this problem, I can program and delete them without any problem.

Is it related to the library? Or, is it related to these kind of micros? Is a fault that has already been acknowledged and resolved?

Thank you in advance. Hope you can help me asap :)

hkpeprah commented 1 year ago

It sounds like you're possibly programming an image that protects the flash? You will need to erase unless you're writing to a region of flash that is already erased. Some sample code would help.

Agarmonz commented 1 year ago

Hello again! I am not using images with security enabled. In fact, the idea is to program a very basic image and then put the necessary data in memory. And, in case this process is completed correctly, then write into memory the necessary data to activate the flash protection.

Example K64/KE15: import pylink Import time Import sys

try: jlink = pylink.JLink() jlink.open() print(jlink.product_name) print(jlink.oem) print('Open connection? %s' % jlink.opened()) print('JLink connected? %s' % jlink.connected()) print('Jlink found -> %s' % jlink.connected_emulators()) print('Connected device: %s' % jlink.target_connected()) except Exception as e: print('ERROR: error when opening connection...', str(e)) jlink.close() sys.exit()

Target connection

try: print('Specifying target interface...') jlink.set_tif(pylink.enums.JLinkInterfaces.SWD) except Exception as e: print('ERROR: debugg...', str(e)) jlink.close() sys.exit()

try: print('Establishing the connection...') jlink.connect(microJLink, 'auto', True) print(' -> Target connected') except Exception as e: print('ERROR: error when connecting, first attempt...', str(e)) jlink.close() sys.exit()

try: print('Unlocking...') pylink.unlockers.unlock(jlink, 'Kinetis') print(' -> Unlocked') except Exception as e: print('ERROR: error when unlocking...', str(e)) jlink.close() sys.exit()

Target erase

jlink.halt() print('CPU halted') time.sleep(1)

try: print('Erasing memory content...') jlink.erase() print(' -> Memory Erased') except Exception as e: print('ERROR: error when erasing...', str(e)) jlink.close() sys.exit()

Target connection

try: print('Establishing the connection...') jlink.connect(microJLink, 'auto', True) print(' -> Connection established') except Exception as e: print('ERROR: error when connecting after erasing...', str(e)) jlink.close() sys.exit()

try: print('Unlocking...') pylink.unlockers.unlock(jlink, 'Kinetis') print(' -> Unlocked') except Exception as e: print('ERROR: error when unlocking...', str(e)) jlink.close() sys.exit()

Flashing .s19

time.sleep(1) try: print ('Programming module...') jlink.flash_file(file_path, 0) print(' -> Module programmed') except Exception as e: print('ERROR: error when programming...', str(e)) jlink.close() sys.exit()

time.sleep(2)

Memory Write

try: print('Memory write data1: %s (bytes)' % jlink.memory_write8(dirdata1, data1))

print('Memory write data2: %s (bytes)' % jlink.memory_write8(dirdata2, data2))

except Exception as e: print('ERROR: error when modifying data...', str(e)) jlink.close() sys.exit()

Read Modified Memory

try: print('Reading memory...') resultMemoryModifiedData1 = jlink.memory_read8(dirdata1, bytesdata1)

resultMemoryModifiedData2 = jlink.memory_read8(dirdata2, bytesdata2)

except Exception as e: print('ERROR: error when reading memory...', str(e)) jlink.close() sys.exit()

check mem

print('Checking memory...')

if data1 != resultMemoryModifiedData1: print('ERROR: data1 FAILURE'); jlink.close(); sys.exit()

if data2 != resultMemoryModifiedData2: print('ERROR: data2 FAILURE'); jlink.close(); sys.exit()

print(' -> Memory checked')

Security

try: print('Memory write SEC: %s (bytes)' % jlink.memory_write8(dirSEC, SEC)) except Exception as e: print('ERROR: error when modifying SEC...', str(e)) jlink.close() sys.exit()

Close connection

jlink.close() print('OK: boot done')

As I said, this same or similar code works in LPC and iMX. In the Kinetis family, the error I was talking about occurs. The steps I am trying seem simple: Connect, unlock in case you have security enabled, clear memory, reconnect, program the image, program data on the image... and if everything is ok then arm the security.

It si true that normally using JLink Lite or JFlash with Kinetis family, it appears a popup talking about flash protection when programming or erasing, in case you want a mass erase you should press 'Yes'. That pop-up does not appear in LPC or iMX. An extra info in case that has something yo do with the problem!

Hope your answer soon :) Thank you very much un advance

hkpeprah commented 1 year ago

Hm. I'm having a hard time following. There is OTP that permanently locks flash on the Kinetis chips, so even if your image doesn't enable flash security, if you flashed one at one point that did, then flash access would be permanently locked. In terms of the code, I'm having a hard time following. Could you paste a block like:

jlink = pylink.Jlink()
jlink.connect(...)
<...>

Then point out the specific line in that block that is causing the issue and whether it is on the first time you run it or subsequent runs.