CLI flashing K32L2A by MCU-LINK and OpenOCD

cancel
Showing results for 
Search instead for 
Did you mean: 

CLI flashing K32L2A by MCU-LINK and OpenOCD

Jump to solution
388 Views
jh0
Contributor IV

Hi,

I am trying to flash K32L2A31 with MCU-Link using OpenOCD without success. CLI line that is working fine with KL27, is not working with K32L2A31. I see that @tomasvanek added suport  for KL28  that is (almost) identical device to K32L2A31, but I don't know how to implement this. Log is for default untouched OpenOCD files. It is failing also for modifed klx.cfg file from ...

flash bank $_CHIPNAME.flash kinetis 0 0 0 0 $_TARGETNAME

to ...

flash bank $_CHIPNAME.flash kinetis 0 0 0 0 $_TARGETNAME -sim-base 0x40074000

 

D:\OpenOCD-20210729-0.11.0>bin\openocd -f interface/cmsis-dap.cfg -f target/klx.cfg -c "program fb.hex verify reset exit"
Open On-Chip Debugger 0.11.0 (2021-07-29) [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 "swd". To override use 'transport select <transport>'.
Info : add flash_bank kinetis klx.pflash
Info : CMSIS-DAP: SWD Supported
Info : CMSIS-DAP: JTAG Supported
Info : CMSIS-DAP: FW Version = 1.10
Info : CMSIS-DAP: Serial# = XXXXXXXX
Info : CMSIS-DAP: Interface Initialised (SWD)
Info : SWCLK/TCK = 1 SWDIO/TMS = 1 TDI = 1 TDO = 1 nTRST = 0 nRESET = 1
Info : CMSIS-DAP: Interface ready
Info : clock speed 1000 kHz
Info : SWD DPIDR 0x2ba01477
Info : klx.cpu: hardware has 2 breakpoints, 2 watchpoints
Info : starting gdb server for klx.cpu on 3333
Info : Listening on port 3333 for gdb connections
Error: DP initialisation failed
Info : SWD DPIDR 0x2ba01477
target halted due to debug-request, current mode: Thread
xPSR: 0x61000000 pc: 0x1c000140 msp: 0x1fffb7d0
Info : SWD DPIDR 0x2ba01477
Error: Failed to read memory at 0x40048024
Info : Kinetis MKL21Z256xxx4 detected: 2 flash blocks
Info : 2 PFlash banks: 256k total
Info : SWD DPIDR 0x2ba01477
Error: Failed to read memory at 0x40048100
Error executing event reset-init on target klx.cpu:
embedded:startup.tcl:260: Error:
in procedure 'program'
in procedure 'ocd_process_reset'
in procedure 'ocd_process_reset_inner' called at file "embedded:startup.tcl", line 260
** Programming Started **
Info : Padding image section 0 at 0x00000008 with 1016 bytes
Info : Flash write discontinued at 0x00000410, next section at 0x00001000
Warn : MAXADDR1 0x20 check failed, please report to OpenOCD mailing list
Info : This device supports Program Longword execution only.
Info : SWD DPIDR 0x2ba01477
Error: Failed to read memory at 0x40048100
Info : FOPT requested in the programmed file differs from current setting, set 'kinetis fopt 0x3d'.
Info : Trying to re-program FCF.
Info : This device supports Program Longword execution only.
Info : SWD DPIDR 0x2ba01477
Error: Failed to read memory at 0x40048100
Info : This device supports Program Longword execution only.
Info : SWD DPIDR 0x2ba01477
Error: Failed to read memory at 0x40048100
Info : SWD DPIDR 0x2ba01477
Error: Failed to read memory at 0x2000004c
Error: failed to get read pointer
Info : klx.cpu: external reset detected
Warn : target was in unknown state when halt was requested
Info : SWD DPIDR 0x2ba01477
Info : klx.cpu: external reset detected
Error: error waiting for target flash write algorithm
Error: Error executing kinetis Flash programming algorithm
Info : SWD DPIDR 0x2ba01477
Error: error writing to flash at address 0x00000000 at offset 0x00001000
Info : klx.cpu: external reset detected
** Programming Failed **
shutdown command invoked

 

0 Kudos
1 Solution
258 Views
jh0
Contributor IV

I modified detection part inside "kinetis.c" file from OpenOCD to support K32L2 devices...

 

 

		case KINETIS_SDID_SERIESID_KL:

			/* KL-series */
			k_chip->pflash_sector_size = 1<<10;
			k_chip->nvm_sector_size = 1<<10;
			/* autodetect 1 or 2 blocks */
			k_chip->flash_support = FS_PROGRAM_LONGWORD;
			k_chip->cache_type = KINETIS_CACHE_L;
			k_chip->watchdog_type = KINETIS_WDOG_COP;

			cpu_mhz = 48;
			
			if ((k_chip->sim_sdid & KINETIS_SDID_SUBFAMID_MASK) == KINETIS_SDID_SUBFAMID_KX1) {
				
				/* K32L2 series */

				result = target_read_u32(target, k_chip->sim_base + SIM_FCFG1_OFFSET, &k_chip->sim_fcfg1);
				if (result != ERROR_OK)
					return result;
				
				switch (k_chip->sim_sdid & KINETIS_SDID_FAMILYID_MASK) {

				case KINETIS_SDID_FAMILYID_K4X:

					subfamid = 7;

					switch (k_chip->sim_fcfg1 >> 24) {
						
						case 0x5: snprintf(name, sizeof(name), "K32L2B11"); break;	/* flash 64k */
						
						case 0x7: snprintf(name, sizeof(name), "K32L2B21"); break;	/* flash 128k */

						case 0x9: snprintf(name, sizeof(name), "K32L2B31"); break;	/* flash 256k */

					default: snprintf(name, sizeof(name), "K32L2Bxx");

					}

					break;

				case KINETIS_SDID_FAMILYID_K2X:

					cpu_mhz = 72;
					k_chip->pflash_sector_size = 2 << 10;
					num_blocks = 2;
					k_chip->watchdog_type = KINETIS_WDOG32_KL28;
					k_chip->sysmodectrlr_type = KINETIS_SMC32;
					subfamid = 8;

					switch (k_chip->sim_fcfg1 >> 24) {
						
						case 0x9: snprintf(name, sizeof(name), "K32L2A31"); break;	/* flash 256k */
						
						case 0xB: snprintf(name, sizeof(name), "K32L2A41"); break;	/* flash 512k */
						
						default: snprintf(name, sizeof(name), "K32L2Axx");

					}

					break;
				}
				
			} else {

				/* KL series */

				switch (k_chip->sim_sdid & (KINETIS_SDID_FAMILYID_MASK | KINETIS_SDID_SUBFAMID_MASK)) {
				
				case KINETIS_SDID_FAMILYID_K1X | KINETIS_SDID_SUBFAMID_KX3:
				case KINETIS_SDID_FAMILYID_K2X | KINETIS_SDID_SUBFAMID_KX3:
					subfamid = 7;
					break;

				case KINETIS_SDID_FAMILYID_K2X | KINETIS_SDID_SUBFAMID_KX8:
					cpu_mhz = 72;
					k_chip->pflash_sector_size = 2 << 10;
					num_blocks = 2;
					k_chip->watchdog_type = KINETIS_WDOG32_KL28;
					k_chip->sysmodectrlr_type = KINETIS_SMC32;
					break;
				}

				snprintf(name, sizeof(name), "MKL%u%uZ%%s%u", familyid, subfamid, cpu_mhz / 10);
			}
			
			break;

 

 

 

K32L2A are handed like KL28, and K32L2B like KL27. Unfortunately don't have K32L2B on hand to test it, but for K32L2A it is working fine...

D:\>OpenOCD\bin\openocd -c "adapter driver cmsis-dap" -f "target\klx.cfg" -c "program fb.hex verify reset exit"
Open On-Chip Debugger 0.11.0 (2021-10-16) [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 "swd". To override use 'transport select <transport>'.
Info : add flash_bank kinetis klx.pflash
Info : CMSIS-DAP: SWD Supported
Info : CMSIS-DAP: JTAG Supported
Info : CMSIS-DAP: FW Version = 1.10
Info : CMSIS-DAP: Serial# = XXXXXXXXXXXX
Info : CMSIS-DAP: Interface Initialised (SWD)
Info : SWCLK/TCK = 1 SWDIO/TMS = 1 TDI = 1 TDO = 1 nTRST = 0 nRESET = 1
Info : CMSIS-DAP: Interface ready
Info : clock speed 1000 kHz
Info : SWD DPIDR 0x2ba01477
Info : klx.cpu: hardware has 2 breakpoints, 2 watchpoints
Info : starting gdb server for klx.cpu on 3333
Info : Listening on port 3333 for gdb connections
Error: DP initialisation failed
Info : SWD DPIDR 0x2ba01477
target halted due to debug-request, current mode: Thread
xPSR: 0x61000000 pc: 0x00004096 msp: 0x1fff8400
Info : SWD DPIDR 0x2ba01477
Error: Failed to read memory at 0x40048024
Info : Kinetis K32L2A31 detected: 2 flash blocks
Info : 2 PFlash banks: 256k total
Info : Disabling Kinetis watchdog (initial WDOG_CS 0x00002180)
** Programming Started **
Info : Padding image section 0 at 0x00000008 with 1016 bytes
Info : Flash write discontinued at 0x00000410, next section at 0x00001000
Warn : MAXADDR1 0x20 check failed, please report to OpenOCD mailing list
Info : This device supports Program Longword execution only.
Info : FOPT requested in the programmed file differs from current setting, set 'kinetis fopt 0x3d'.
Info : Trying to re-program FCF.
Info : This device supports Program Longword execution only.
Info : This device supports Program Longword execution only.
** Programming Finished **
** Verify Started **
** Verified OK **
** Resetting Target **
Error: DP initialisation failed
shutdown command invoked

 

Everything else except "kinetis.c" file (in OpenOCD 0.11) is untouched. You can replace original "kinetis.c" with attached modified version, build it and test it. Of course, I am not responsible for broken window or dead cat.

View solution in original post

0 Kudos
10 Replies
367 Views
bobpaddock
Senior Contributor III

Here is what I have at hand, not finding k32l.cfg right now.

# Original:
# This is an Freescale Freedom eval board with a single MKL25Z128VLK4 chip.
# http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=FRDM-KL25Z
# Modified for K32L2A

source [find interface/cmsis-dap.cfg]

# increase working area to 16KB
set WORKAREASIZE 0x4000

# chip name

# 100 Pin:
#set CHIPNAME K32L2A31VLL1A

# 64 Pin:
set CHIPNAME K32L2A41VLH1A

reset_config srst_only

source [find target/k32l.cfg]

 

 

0 Kudos
372 Views
bobpaddock
Senior Contributor III

@ErichS and I discussed adding K32L support to the MCU-LINK someplace, and I can not find that discussion now.

The problem I ran into was there seemed to be some magic addresses that were needed that are not documented in the public documents.
CPUTAPID for one.

Looking at Erich's blogs on the MCU-Link, and the underlying comments, would be helpful if you've not already read them:

https://mcuoneclipse.com

 

359 Views
jh0
Contributor IV

Hi Bob,

On @ErichS blog are only OpenOCD examples related to targets with existing cfg file. I don't have any problems with flashing KL27 (like @ErichS with KL25). I see that OpenOCD support for KL28 is added, but I don' know how to apply this. I need kl28.cfg. BTW, I am using last OpenOCD binary from here...

https://gnutoolchains.com/arm-eabi/openocd/

0 Kudos
351 Views
bobpaddock
Senior Contributor III

In the OpenOCD source tree  src/flash/nor/kinets.c needs modified to support the K32L family.


The various cfg files are ultimately instructions to that area of the code for the flashing.

Must have OpenOCD version 0.11.0 or later.

0 Kudos
346 Views
jh0
Contributor IV

Here is added KL28, 4 years ago. AFAIK KL28 and K32L2A are identical (like KL27 and K32L2B), so I guess that for K32L2A there is no need for source modification. If someone have working OpenOCD KL28 flashing example, and that failed on my setup, than I am wrong of course.

In Log from first post, there you can see that I am using binary from 29.7.2021.(0.11.0)

0 Kudos
320 Views
bobpaddock
Senior Contributor III

In version 11 there were several generic memory size configurations added.
Does the relevant one work for your K32L?

kinetis_128k.cfg
kinetis_1m.cfg
kinetis_256k.cfg
kinetis_32k.cfg
kinetis_512k.cfg
kinetis_64k.cfg

Which all call:

kinetis_generic.cfg

0 Kudos
312 Views
jh0
Contributor IV

There is some bug in kinetis_generic.cfg so I use klx.cfg because I need to flash KL28 like device. I found it in OpenOCD source, why is not working with K32L2 devices. Even I can not test it, I am sure that is (OpenOCD 0.11.0) working just fine with KL28.

-sim-base option is added to flash bank creation...

flash bank $_FLASHNAME kinetis 0 0 0 0 $_TARGETNAME -sim-base 0x40074000

to point location for device identification. And this is working fine, because K32L2A31 is recognized as 256k device. Unfortunately NXP broke device subfamily identification. While family (FAMID) is the same for KL28 and K32L2A (value 2), subfamily SUBFAMID field in KL28 (value 8 ) for K32L2A this field (SUBFAMID) is related to CPU core number (value 1). SERIESID (value 1) is the same for KL and K32L2A.

And because of this (wrong SUBFAMID) OpenOCD looking for WDOG on wrong (KL27) location, and it faild. Unfortunately there is no option (like with -sim-base) for correct WDOG location. This is the part of source code...

case KINETIS_SDID_FAMILYID_K2X | KINETIS_SDID_SUBFAMID_KX8:
  cpu_mhz = 72;
  k_chip->pflash_sector_size = 2<<10;
  num_blocks = 2;
  k_chip->watchdog_type = KINETIS_WDOG32_KL28;
  k_chip->sysmodectrlr_type = KINETIS_SMC32;
  break;
}

And here is log...

D:\>OpenOCD-20210729-0.11.0\bin\openocd -c "adapter driver cmsis-dap" -f "target\klx.cfg" -c "program fb.hex verify reset exit"
Open On-Chip Debugger 0.11.0 (2021-07-29) [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 "swd". To override use 'transport select <transport>'.
Info : add flash_bank kinetis klx.pflash
Info : CMSIS-DAP: SWD Supported
Info : CMSIS-DAP: JTAG Supported
Info : CMSIS-DAP: FW Version = 1.10
Info : CMSIS-DAP: Serial# = XXXXXXXXXX
Info : CMSIS-DAP: Interface Initialised (SWD)
Info : SWCLK/TCK = 1 SWDIO/TMS = 1 TDI = 1 TDO = 1 nTRST = 0 nRESET = 1
Info : CMSIS-DAP: Interface ready
Info : clock speed 1000 kHz
Info : SWD DPIDR 0x2ba01477
Info : klx.cpu: hardware has 2 breakpoints, 2 watchpoints
Info : starting gdb server for klx.cpu on 3333
Info : Listening on port 3333 for gdb connections
Error: DP initialisation failed
Info : SWD DPIDR 0x2ba01477
target halted due to debug-request, current mode: Thread
xPSR: 0xb0000000 pc: 0x30303030 msp: 0x30383038
Info : Kinetis MKL21Z256xxx4 detected: 2 flash blocks
Info : 2 PFlash banks: 256k total
Info : SWD DPIDR 0x2ba01477
Error: Failed to read memory at 0x40048100    KL27 WDOG location

0 Kudos
327 Views
jh0
Contributor IV

Well, something is different between KL28 and K32L2A. I tried flashing K32K2A with pyOCD and KL28 selected as target and it is not working. It is working fine with KL27, even with K32L2B selected as target.

C:\>pyocd flash -t KL27Z4 fb.hex
0003815:INFO:load_cmd:Loading C:\fb.hex
[==================================================] 100%
0006760:INFO:loader:Erased 28672 bytes (28 sectors), programmed 28672 bytes (28 pages), skipped 0 bytes (0 pages) at 10.70 kB/s

C:\>pyocd flash -t k32l2b3 fb.hex
0003569:INFO:load_cmd:Loading C:\fb.hex
[==================================================] 100%
0007031:INFO:loader:Erased 28672 bytes (28 sectors), programmed 28672 bytes (28 pages), skipped 0 bytes (0 pages) at 8.63 kB/s

Tags (1)
0 Kudos
259 Views
jh0
Contributor IV

I modified detection part inside "kinetis.c" file from OpenOCD to support K32L2 devices...

 

 

		case KINETIS_SDID_SERIESID_KL:

			/* KL-series */
			k_chip->pflash_sector_size = 1<<10;
			k_chip->nvm_sector_size = 1<<10;
			/* autodetect 1 or 2 blocks */
			k_chip->flash_support = FS_PROGRAM_LONGWORD;
			k_chip->cache_type = KINETIS_CACHE_L;
			k_chip->watchdog_type = KINETIS_WDOG_COP;

			cpu_mhz = 48;
			
			if ((k_chip->sim_sdid & KINETIS_SDID_SUBFAMID_MASK) == KINETIS_SDID_SUBFAMID_KX1) {
				
				/* K32L2 series */

				result = target_read_u32(target, k_chip->sim_base + SIM_FCFG1_OFFSET, &k_chip->sim_fcfg1);
				if (result != ERROR_OK)
					return result;
				
				switch (k_chip->sim_sdid & KINETIS_SDID_FAMILYID_MASK) {

				case KINETIS_SDID_FAMILYID_K4X:

					subfamid = 7;

					switch (k_chip->sim_fcfg1 >> 24) {
						
						case 0x5: snprintf(name, sizeof(name), "K32L2B11"); break;	/* flash 64k */
						
						case 0x7: snprintf(name, sizeof(name), "K32L2B21"); break;	/* flash 128k */

						case 0x9: snprintf(name, sizeof(name), "K32L2B31"); break;	/* flash 256k */

					default: snprintf(name, sizeof(name), "K32L2Bxx");

					}

					break;

				case KINETIS_SDID_FAMILYID_K2X:

					cpu_mhz = 72;
					k_chip->pflash_sector_size = 2 << 10;
					num_blocks = 2;
					k_chip->watchdog_type = KINETIS_WDOG32_KL28;
					k_chip->sysmodectrlr_type = KINETIS_SMC32;
					subfamid = 8;

					switch (k_chip->sim_fcfg1 >> 24) {
						
						case 0x9: snprintf(name, sizeof(name), "K32L2A31"); break;	/* flash 256k */
						
						case 0xB: snprintf(name, sizeof(name), "K32L2A41"); break;	/* flash 512k */
						
						default: snprintf(name, sizeof(name), "K32L2Axx");

					}

					break;
				}
				
			} else {

				/* KL series */

				switch (k_chip->sim_sdid & (KINETIS_SDID_FAMILYID_MASK | KINETIS_SDID_SUBFAMID_MASK)) {
				
				case KINETIS_SDID_FAMILYID_K1X | KINETIS_SDID_SUBFAMID_KX3:
				case KINETIS_SDID_FAMILYID_K2X | KINETIS_SDID_SUBFAMID_KX3:
					subfamid = 7;
					break;

				case KINETIS_SDID_FAMILYID_K2X | KINETIS_SDID_SUBFAMID_KX8:
					cpu_mhz = 72;
					k_chip->pflash_sector_size = 2 << 10;
					num_blocks = 2;
					k_chip->watchdog_type = KINETIS_WDOG32_KL28;
					k_chip->sysmodectrlr_type = KINETIS_SMC32;
					break;
				}

				snprintf(name, sizeof(name), "MKL%u%uZ%%s%u", familyid, subfamid, cpu_mhz / 10);
			}
			
			break;

 

 

 

K32L2A are handed like KL28, and K32L2B like KL27. Unfortunately don't have K32L2B on hand to test it, but for K32L2A it is working fine...

D:\>OpenOCD\bin\openocd -c "adapter driver cmsis-dap" -f "target\klx.cfg" -c "program fb.hex verify reset exit"
Open On-Chip Debugger 0.11.0 (2021-10-16) [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 "swd". To override use 'transport select <transport>'.
Info : add flash_bank kinetis klx.pflash
Info : CMSIS-DAP: SWD Supported
Info : CMSIS-DAP: JTAG Supported
Info : CMSIS-DAP: FW Version = 1.10
Info : CMSIS-DAP: Serial# = XXXXXXXXXXXX
Info : CMSIS-DAP: Interface Initialised (SWD)
Info : SWCLK/TCK = 1 SWDIO/TMS = 1 TDI = 1 TDO = 1 nTRST = 0 nRESET = 1
Info : CMSIS-DAP: Interface ready
Info : clock speed 1000 kHz
Info : SWD DPIDR 0x2ba01477
Info : klx.cpu: hardware has 2 breakpoints, 2 watchpoints
Info : starting gdb server for klx.cpu on 3333
Info : Listening on port 3333 for gdb connections
Error: DP initialisation failed
Info : SWD DPIDR 0x2ba01477
target halted due to debug-request, current mode: Thread
xPSR: 0x61000000 pc: 0x00004096 msp: 0x1fff8400
Info : SWD DPIDR 0x2ba01477
Error: Failed to read memory at 0x40048024
Info : Kinetis K32L2A31 detected: 2 flash blocks
Info : 2 PFlash banks: 256k total
Info : Disabling Kinetis watchdog (initial WDOG_CS 0x00002180)
** Programming Started **
Info : Padding image section 0 at 0x00000008 with 1016 bytes
Info : Flash write discontinued at 0x00000410, next section at 0x00001000
Warn : MAXADDR1 0x20 check failed, please report to OpenOCD mailing list
Info : This device supports Program Longword execution only.
Info : FOPT requested in the programmed file differs from current setting, set 'kinetis fopt 0x3d'.
Info : Trying to re-program FCF.
Info : This device supports Program Longword execution only.
Info : This device supports Program Longword execution only.
** Programming Finished **
** Verify Started **
** Verified OK **
** Resetting Target **
Error: DP initialisation failed
shutdown command invoked

 

Everything else except "kinetis.c" file (in OpenOCD 0.11) is untouched. You can replace original "kinetis.c" with attached modified version, build it and test it. Of course, I am not responsible for broken window or dead cat.

View solution in original post

0 Kudos
238 Views
bobpaddock
Senior Contributor III

Thank you.

 

0 Kudos