How do I access variables like $PROGNAME or $BUILD_DIR in an openocd .cfg file?

Is it possible to do this? I would like to access these values from inside a target .cfg file, but I can’t figure out any way to get this to work. Is there something equivalent to Import(“env”) that could work in a .cfg file?

I tried this

if { [info exists BUILD_DIR] } {
set myBuild $BUILD_DIR

but no luck there.

I would like to get the BUILD_DIR and PROGNAME and PROG_SUFFIX so it can be used in an openocd flash write command. Currently I have to hard code the entire path and filename like this:

set IMGFILE “C:\Users\tony\Documents\PlatformIO\Projects\TGBlinky2.pio\build\lpc1769\firmware.bin”
flash write_image erase $IMGFILE 0x0 bin

I’m looking for a way to send the path and filename from platformio so I don’t have to keep changing that .cfg file for every project.

Could someone tell me if this can be done and how?

Oh…and the reason for all this is that I need to create a custom upload command to get openocd to use an ST-Link tool to program an debug LPC17xx chip.

You can completely control the OpenOCD invocation yourself using either Advanced scripting or the upload_command. So you can inject -c "set IMGFILE ..." flags from inside there that you can reference in custom config files.

Thanks Max, that works for me.

I have another hurdle…how do you inform the linker that you want the compiled code to start at an offset address( say like 0x00004000 ) instead of whatever the default may be ?

That is very dependent on the used platform. If you’re using the PlatformIO provided NXPLPC platform, then

the upload respects the offset_address information, so you can add

board_upload.offset_address = 0x00004000 

to the platformio.ini.

I’m using platform “nxplpc-arduino-lpc176x” and it doesn’t have those lines in builder/main.py.
But I did try add the command to upload_flags like this:
upload_protocol= custom
upload_command = $PROJECT_PACKAGES_DIR\tool-openocd\bin\openocd
upload_flags =
-c
set LOADADDRESS 0x00004000
-c
set FIRMWARENAME {$PROJECT_DIR$SOURCE}
-f
$PROJECT_PACKAGES_DIR\tool-openocd\scripts\board\lpc1769_upload.cfg

which for some reason does not pass the upload_flags with the upload_command unless I call an extra_script that does this:

env.Replace(
UPLOADER="$PROJECT_PACKAGES_DIR\tool-openocd\bin\openocd",
UPLOADCMD="$UPLOADER $UPLOADERFLAGS"
)

This finally makes it work!
But…then there is another issue.
The code gets placed at the LOADADDRESS I defined at 0x00004000 (verified with TelNet dumping memory). However, the LPC chips need the vector table to be at 0x00000000 and when I checked, it was all empty (0xff bytes) up till the load address at 0x00004000.
So the code will not execute!

If I examine the program code at 0x00004000, it has the vector table starting right at 0x00004000.
How do we tell the compiler/linker to place the vector table at 0x00000000 when the actual program code is at the given offset address?

Well yes the point of moving the application upwards in flash is that the chip contains a bootloader that sits at 0x0 and that then loads the application from the pre-agreed offset. What bootloader have you flashed on your chip?

Well, no bootloader actually, thanks for pointing that out!
I mistakenly thought I could use a command like “mww 0xE000ED08 $_LOADADDRESS”
where 0xE000ED08 is the vector table offset register on the LPC1769 chip.
I figured a reset would then jump to the vector table with the offset applied. But I realize now that doesn’t work.
The chip is built to start at address 0x00000000 after reset (unless the BOOT0 pin is low, which starts the chip’s own bootloader).