PlatformIO Community

Custom platformio.ini variables to be passed to build_flags?

Hi guys,
Is it possible to declare “custom” variables in platformio.ini as in a Makefile? I’m trying to port the below Makefile to platformio, but I’m confused about the variables, like “BOOTLOADER_START” and the others shown. This will be the platformio.ini file of an ATtiny85 I2C bootloader project I’ve written, currently compiled with avr-gcc+Make. If there is a better way to solve this, I’m all ears.
Thank you!

TARGET = bootloader
BOOTLOADER_TWI_ADDR = 11
BOOTLOADER_START = 1B00

# Compiler options
CFLAGS += -Wall -g2 -Os -std=gnu99
CFLAGS += -I$(CONFIGPATH) -I$(CMDDIR) -I. -mmcu=$(MCU) -DF_CPU=$(F_CPU)
# Splits up object files per function
CFLAGS += -ffunction-sections -fdata-sections
# The "nostartfiles" option allows ditching the compiler-supplied "crt1.S" file to include a custom one
CFLAGS += -nostartfiles
# Optimizations aimed at reducing memory space usage
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -mno-interrupts -mtiny-stack
CFLAGS += -fno-inline-small-functions -fno-move-loop-invariants -fno-tree-scev-cprop -fno-jump-tables
# Bootloader TWI address and memory start position
CFLAGS += -DTWI_ADDR=$(BOOTLOADER_TWI_ADDR)
CFLAGS += -DBOOTLOADER_START=0x$(BOOTLOADER_START)
# Linker options
LDFLAGS = -Wl,--relax,--section-start=.text=$(BOOTLOADER_START),--gc-sections,-Map=$(TARGET).map

Use build_flags for that task, these will then be visible to your code. Same for adding new linker flags.

Use build_flags for that task, these will then be visible to your code. Same for adding new linker flags.

Thanks for your quick answer. Yes, I’m aware of that, what I define with “build_flags” is visible to the code, that’s great. I meant variables to be used inside “platformio.ini” itself. To declare a parameter once and then using it several times within the .ini, or even shared across different “.ini” files included with "extra_configs ". E.g. this doesn’t work:

[env:tml-t85-small]
build_flags =
; Bootloader setup
    -D TWI_ADDR=19
    -D BOOTLOADER_START=0x1B00
; Linker options
    -Wl,--relax,--section-start=.text=$(BOOTLOADER_START),--gc-sections,-Map=bootloader.map

Unknown options in the platformio.ini will be treated as a warning, so it’s not very clean to do my_variable = someval inside the ini. Though it might still work technically. See e.g. an example in the docs. Accessing that has then to be done via ${environmentname.variablename}.

There also exists the possibility of using Advanced Scripting. In there you can also readout the CFLAGS of the env to get the value you specified via the build flags again.

I’ll take a look at both alternatives, thank you!