Finally I understand what's causing the problem.

Sector erase does not set FSEC field to unsecure as mass erase does. So if user 
issue sector erase followed device reset then the device gets locked (not 
permanently but it's a trouble as well).
To prevent this the OpenOCD kinetis flash driver builds its own FCF and writes 
it immediately after erase of the sector containing FCF. Ideally the generated 
FCF is same as FCF data in programmed file.

The FCF data in files you send are correct. I was suspicious as there is no 
dedicated section in elf but this does not matter for flash programming. So the 
simplest solution to your problem is to switch off FCF checking in OpenOCD by 
command "kinetis fcf_source write" - be aware that in this mode incorrect FCF 
data in programmed file can peramnently lock the device. See 
http://openocd.org/doc/html/Flash-Commands.html#index-kinetis
for details.

Although the Arduino generated FCF is correct, it differs from one generated by 
OpenOCD. You should let know OpenOCD about the used FOPT value by command 
"kinetis fopt". Unfortunately this is not enough. Arduino sets FSEC byte to 
0xde. This is ok according to RM but MEEN field is used just in secure modes so 
there is no point to set it in unsecure state. That's why OpenOCD uses FSEC 
value 0xfe, same as mass erase sets. So this should be fixed somewhere in the 
startup code of Arduino.

Kinetis K22 doesn't implement ECC (error check and correction) in the flash 
module. Based on the flash principle it is possible to program already 
programmed flash word and the resulted data is "bitwise and". Of course this is 
not a good practice as it overstress the flash memory cell. RM 29.4.12.8 
Program Section command, paragraph CAUTION strictly prohibits this. But if you 
want to program FOPT to something else than 0xff and the device is mass-erased 
then reprogramming takes place. That's why the flash driver allows 
reprogramming of FCF if differs in programmed file. By the time I wrote that 
code I didn't know how unreliable is reprogramming of the Kinetis flash. You 
can try it at any flash location (I would recommend to keep out of FCF):

~~~
> flash fillw 0x7e000 0xfffffffe 1
wrote 4 bytes to 0x0007e000 in 0.018735s (0.209 KiB/s)

> flash fillw 0x7e000 0xfffff9de 1
Flash write error at 0x0007e000
Failed to read memory at 0x0007e004
error reading to flash at address 0x00000000 at offset 0x0007e000
~~~

The memory location is unreadable after the second flash programming.
Interesting this fails on newer rev A silicon only, older MK22FN1M0 withstands 
the same sequence without problem. Anyway the more zeroes in the second write 
the more likely the word gets unreadable.

The fillw command sequence simulates what happens during programming:
Generated FCF is succesfully written
~~~
Debug: 750 1049 kinetis.c:1825 kinetis_write_inner(): flash write @ 0x00000400
Debug: 751 1049 kinetis.c:1760 kinetis_write_sections(): write section @ 
0x00000400 with length 16 bytes
Debug: 752 1049 target.c:2523 target_write_u8(): address: 0x40020000, value: 
0x80
~~~
and reprogramming fails:
~~~
Debug: 775 1134 kinetis.c:1760 kinetis_write_sections(): write section @ 
0x00000400 with length 1024 bytes
Debug: 776 1134 target.c:2523 target_write_u8(): address: 0x40020000, value: 
0x80
Debug: 777 1134 target.c:2440 target_read_u8(): address: 0x40020000, value: 0x00
Debug: 778 1134 target.c:2440 target_read_u8(): address: 0x40020000, value: 0x00
Debug: 779 1149 target.c:2440 target_read_u8(): address: 0x40020000, value: 0x81
Error: 780 1149 kinetis.c:1782 kinetis_write_sections(): Flash write error at 
0x00000400
~~~

The OpenOCD driver should check if FCF is going to be reprogrammed and at least 
warn in this case.


---

** [tickets:#270] Kinetis MK22FX512: Error writing FCF Block 0X400  - 0x40f**

**Status:** new
**Milestone:** 0.9.0
**Created:** Sun Jun 14, 2020 06:14 AM UTC by Noel Diviney
**Last Updated:** Wed Jul 01, 2020 09:09 AM UTC
**Owner:** nobody


Hi nall,
Error at address 0x400 when programming flash on Kinetis MK22F512xxx12
However, when I press the reset buuton after programming the Led Blinks 
normally.

Does the FCF block have to be skipped somehow?
Is the "Error: libusb_open() failed with LIBUSB_ERROR_NOT_SUPPORTED" a problem
given that the program runs.

The terminal output is as follows :-
noel@DESKTOP-0AFLL5T MSYS /w/adgp-1611/portable/sketchbook/BlinkTestBlue/build
$ openocd -f interface/ftdi/olimex-arm-usb-ocd.cfg 
          -f target/kx.cfg 
                  -c "program BlinkTestBlue.ino.elf verify reset"
Open On-Chip Debugger 0.10.0 (2020-05-30) [https://github.com/sysprogs/openocd]
Licensed under GNU GPL v2
libusb1 09e75e98b4d9ea7909e8837b7a3f00dda4589dc3
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
Info : auto-selecting first available session transport "jtag". To override use 
'transport select <transport>'.
Info : add flash_bank kinetis kx.pflash
Error: libusb_open() failed with LIBUSB_ERROR_NOT_SUPPORTED
Info : clock speed 1000 kHz
Info : JTAG tap: kx.cpu tap/device found: 0x4ba00477 (mfg: 0x23b (ARM Ltd.), 
part: 0xba00, ver: 0x4)
Info : kx.cpu: hardware has 6 breakpoints, 4 watchpoints
Info : kx.cpu: external reset detected
Info : MDM: Chip is unsecured. Continuing.
Info : starting gdb server for kx.cpu on 3333
Info : Listening on port 3333 for gdb connections
Info : JTAG tap: kx.cpu tap/device found: 0x4ba00477 (mfg: 0x23b (ARM Ltd.), 
part: 0xba00, ver: 0x4)
Info : MDM: Chip is unsecured. Continuing.
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x00000188 msp: 0x20010000
Info : Kinetis MK22FX512xxx12 detected: 2 flash blocks
Info : 1 PFlash banks: 512k total
Info : 1 FlexNVM banks: 128k total, 128k available as data flash, 4096bytes 
FlexRAM
Info : Disabling Kinetis watchdog (initial WDOG_STCTRLH = 0x01d3)
Info : WDOG_STCTRLH = 0x01d2
** Programming Started **
Error: Flash write error at 0x00000400
** Programming Finished **
** Verify Started **
Error: timed out while waiting for target halted
Error: error executing cortex_m crc algorithm
Error: JTAG-DP STICKY ERROR
Error: Failed to read memory at 0x00000404
embedded:startup.tcl:500: Error: ** Verify Failed **
in procedure 'program'
in procedure 'program_error' called at file "embedded:startup.tcl", line 561
at file "embedded:startup.tcl", line 500


noel@DESKTOP-0AFLL5T MSYS /w/adgp-1611/portable/sketchbook/BlinkTestBlue/build
$ openocd -f interface/ftdi/olimex-arm-usb-ocd.cfg 
          -f interface/ftdi/swd-resistor-hack.cfg 
                  -c "transport select swd" 
                  -f target/kx.cfg 
                  -c "program BlinkTestBlue.ino.elf verify reset"
Open On-Chip Debugger 0.10.0 (2020-05-30) [https://github.com/sysprogs/openocd]
Licensed under GNU GPL v2
libusb1 09e75e98b4d9ea7909e8837b7a3f00dda4589dc3
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
Info : FTDI SWD mode enabled
Warn : Transport "swd" was already selected
swd
Info : add flash_bank kinetis kx.pflash
Error: libusb_open() failed with LIBUSB_ERROR_NOT_SUPPORTED
Info : clock speed 1000 kHz
Info : SWD DPIDR 0x2ba01477
Info : kx.cpu: hardware has 6 breakpoints, 4 watchpoints
Info : kx.cpu: external reset detected
Info : MDM: Chip is unsecured. Continuing.
Info : starting gdb server for kx.cpu on 3333
Info : Listening on port 3333 for gdb connections
Info : MDM: Chip is unsecured. Continuing.
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x00000188 msp: 0x20010000
Info : Kinetis MK22FX512xxx12 detected: 2 flash blocks
Info : 1 PFlash banks: 512k total
Info : 1 FlexNVM banks: 128k total, 128k available as data flash, 4096bytes 
FlexRAM
Info : Disabling Kinetis watchdog (initial WDOG_STCTRLH = 0x01d3)
Info : WDOG_STCTRLH = 0x01d2
** Programming Started **
Error: Flash write error at 0x00000400
** Programming Finished **
** Verify Started **
Error: timed out while waiting for target halted
Error: error executing cortex_m crc algorithm
Info : SWD DPIDR 0x2ba01477
Error: Failed to read memory at 0x00000404
embedded:startup.tcl:500: Error: ** Verify Failed **
in procedure 'program'
in procedure 'program_error' called at file "embedded:startup.tcl", line 561
at file "embedded:startup.tcl", line 500

noel@DESKTOP-0AFLL5T MSYS /w/adgp-1611/portable/sketchbook/BlinkTestBlue/build
$
I am having difficulty understanding how to manage the FCF flash block.
Any help would be much appreciated.

Many Thanks Noel Diviney


---

Sent from sourceforge.net because openocd-devel@lists.sourceforge.net is 
subscribed to https://sourceforge.net/p/openocd/tickets/

To unsubscribe from further messages, a project admin can change settings at 
https://sourceforge.net/p/openocd/admin/tickets/options.  Or, if this is a 
mailing list, you can unsubscribe from the mailing list.
_______________________________________________
OpenOCD-devel mailing list
OpenOCD-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openocd-devel

Reply via email to