Skip to content

Commit 274551c

Browse files
authored
Merge pull request #1 from maxgerhardt/master
Make Firmware PlatformIO compatible as of UnifiedEngineering#207
2 parents 217281c + 9b006c9 commit 274551c

10 files changed

Lines changed: 137 additions & 4 deletions

Makefile

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ COLOR_END = $(shell echo "\033[0m")
3030
# Source files
3131
C_SRCS += $(wildcard $(SRC_DIR)*.c) $(BUILD_DIR)version.c
3232

33-
S_SRCS += $(wildcard $(SRC_DIR)*.s)
33+
S_SRCS += $(wildcard $(SRC_DIR)*.S)
3434

35-
OBJS := $(patsubst $(SRC_DIR)%.c,$(BUILD_DIR)%.o,$(C_SRCS)) $(patsubst $(SRC_DIR)%.s,$(BUILD_DIR)%.o,$(S_SRCS))
35+
# filter out src/version.c created by PlatformIO
36+
TMPVAR := $(C_SRCS)
37+
C_SRCS = $(filter-out $(SRC_DIR)version.c, $(TMPVAR))
38+
39+
OBJS := $(patsubst $(SRC_DIR)%.c,$(BUILD_DIR)%.o,$(C_SRCS)) $(patsubst $(SRC_DIR)%.S,$(BUILD_DIR)%.o,$(S_SRCS))
3640

3741
C_DEPS := $(wildcard *.d)
3842

@@ -41,21 +45,23 @@ all: axf
4145
$(BUILD_DIR)version.c: $(BUILD_DIR)tag
4246
git describe --tag --always --dirty | \
4347
sed 's/.*/const char* Version_GetGitVersion(void) { return "&"; }/' > $@
44-
4548
# Always regenerate the git version
4649
.PHONY: $(BUILD_DIR)version.c
4750

4851
$(BUILD_DIR)tag:
52+
$(RM) $(SRC_DIR)version.c
4953
mkdir -p $(BUILD_DIR)
5054
touch $(BUILD_DIR)tag
55+
# platformio will generate a version.c in this place, which messes with the build.
56+
# remove it we're building from the makefile..
5157

5258
$(BUILD_DIR)%.o: $(SRC_DIR)%.c $(BUILD_DIR)tag
5359
@echo 'Building file: $<'
5460
$(CC) -std=gnu99 -DNDEBUG -D__NEWLIB__ -Os -g -Wall -Wunused -c -fmessage-length=0 -fno-builtin -ffunction-sections -fdata-sections -flto -ffat-lto-objects -mcpu=arm7tdmi -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.o)" -MT"$(@:%.o=%.d)" -o "$@" "$<"
5561
@echo 'Finished building: $(COLOR_GREEN)$<$(COLOR_END)'
5662
@echo ' '
5763

58-
$(BUILD_DIR)%.o: $(SRC_DIR)%.s $(BUILD_DIR)tag
64+
$(BUILD_DIR)%.o: $(SRC_DIR)%.S $(BUILD_DIR)tag
5965
@echo 'Building file: $<'
6066
$(CC) -c -x assembler-with-cpp -I $(BUILD_DIR) -DNDEBUG -D__NEWLIB__ -mcpu=arm7tdmi -o "$@" "$<"
6167
@echo 'Finished building: $(COLOR_GREEN)$<$(COLOR_END)'

boards/lpc2134_01.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"build": {
3+
"cpu": "arm7tdmi",
4+
"f_cpu": "55296000L",
5+
"mcu": "lpc2134"
6+
},
7+
"connectivity": [
8+
],
9+
"debug": {
10+
"jlink_device": "LPC2134",
11+
"pyocd_target": "lpc2134",
12+
"svd_path": "LPC176x5x_v0.2.svd"
13+
},
14+
"frameworks": [
15+
"mbed"
16+
],
17+
"name": "NXP LPC2134/01",
18+
"upload": {
19+
"maximum_ram_size": 16384,
20+
"maximum_size": 131072,
21+
"protocol": "mbed",
22+
"protocols": [
23+
"jlink",
24+
"blackmagic",
25+
"cmsis-dap",
26+
"mbed"
27+
]
28+
},
29+
"url": "https://www.nxp.com/products/processors-and-microcontrollers/arm-microcontrollers/general-purpose-mcus/lpc2000-arm7/single-chip-16-32-bit-microcontrollers-32-64-128-256-512-kb-isp-iap-flash-with-10-bit-adc-and-dac:LPC2134FBD64",
30+
"vendor": "NXP"
31+
}
32+

create_version.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import subprocess
2+
import os
3+
import traceback
4+
import shutil
5+
Import("env")
6+
7+
def create_version_c(*args, **kwargs):
8+
print("Creating version.c file from git describe..")
9+
# sanity check: was this downlaoded via git?
10+
if not os.path.isdir(".git"):
11+
print("Aborting creation of version.c since this project was not cloned via `git`...")
12+
return
13+
# sanity check: do we have git?
14+
if shutil.which("git") is None:
15+
print("Command `git` is not available, aborting creation of src/version.c..")
16+
return
17+
try:
18+
git_tag_cmd = ["git", "describe", "--tag", "--always", "--dirty"]
19+
git_tag = subprocess.check_output(git_tag_cmd).decode('utf-8').strip()
20+
print("Got tag: %s" % git_tag)
21+
file_content = 'const char* Version_GetGitVersion(void) { return "%s"; }' % git_tag
22+
with open("src/version.c", 'w') as out_file:
23+
out_file.write(file_content)
24+
print("Writing version.c okay.")
25+
except Exception as exc:
26+
print("Exception during creation of version.c occured: %s" % str(exc))
27+
print(traceback.format_exc())
28+
os.remove("src/version.c")
29+
30+
create_version_c()

custom_upload.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Import("env")
2+
import os
3+
import platform
4+
from platformio.builder.tools.pioupload import AutodetectUploadPort
5+
6+
config = env.GetProjectConfig()
7+
mcu_clock = config.get("upload_settings", "MCU_CLOCK")
8+
flash_baud = config.get("upload_settings", "FLASH_BAUD")
9+
#print("MCU_CLOCK: %s" % str(mcu_clock))
10+
11+
# Python callback
12+
def on_upload(source, target, env):
13+
AutodetectUploadPort(env)
14+
#print(source, target)
15+
firmware_path = str(source[0])
16+
# replace .bin with .hex. Upload needs .hex not .bin
17+
firmware_path = firmware_path[:-3] + "hex"
18+
print("Firmware path: %s" % firmware_path)
19+
# find out what executable to call
20+
uploader = "./lpc21isp_linux"
21+
if platform.system() == "Windows":
22+
uploader = "lpc21isp_win.exe"
23+
# do something
24+
env.Execute("%s \"%s\" %s %s %s" % (uploader, firmware_path, env.subst("$UPLOAD_PORT"), flash_baud, mcu_clock))
25+
26+
env.Replace(UPLOADCMD=on_upload)

fix_linkflags.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Import("env")
2+
env.Append(LINKFLAGS=["-nostdlib"])
3+
4+
# Custom HEX from ELF
5+
env.AddPostAction(
6+
"$BUILD_DIR/${PROGNAME}.elf",
7+
env.VerboseAction(" ".join([
8+
"$OBJCOPY", "-O", "ihex", "-R", ".eeprom",
9+
"\"$BUILD_DIR/${PROGNAME}.elf\"", "\"$BUILD_DIR/${PROGNAME}.hex\""
10+
]), "Building $BUILD_DIR/${PROGNAME}.hex")
11+
)

lpc21isp_linux

944 KB
Binary file not shown.

lpc21isp_win.exe

464 KB
Binary file not shown.

platformio.ini

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[upload_settings]
2+
; value will be read by python script. analogous to Makefile
3+
FLASH_BAUD = 57600
4+
MCU_CLOCK = 11059
5+
6+
[env:lpc2134_01]
7+
platform = nxplpc
8+
; use custom board definition for NXP LPC 2134/01
9+
board = lpc2134_01
10+
; use same linkerscript as Makefile uses
11+
board_build.ldscript = T-962-controller.ld
12+
; remove default thumb mode flag
13+
build_unflags = -mthumb
14+
; reproduce build flags from Makefile
15+
build_flags = -DNDEBUG -D__NEWLIB__ -fno-builtin -fmessage-length=0 -flto -ffat-lto-objects -Wl,-u_printf_float -Wl,-u_scanf_float
16+
; add -nostdlib to linkerflags via script; cannot be done via build_flags in this case
17+
; see https://docs.platformio.org/en/latest/projectconf/advanced_scripting.html#extra-linker-flags-without-wl-prefix
18+
; also creates additional HEX file (otherwise only .elf and .bin are there, but uploader needs .hex)
19+
; reproduce creation of version.c file, as Makefile does (could also work without, but why not..)
20+
extra_scripts =
21+
fix_linkflags.py
22+
pre:create_version.py
23+
custom_upload.py
24+
; upload via custom lpc21isp program.
25+
; precompiled binaries for Windows and Linux exist in this directory. Assume Windows by default
26+
upload_protocol = custom
27+
;if explicit upload port is needed (will be auto-detected otherwise)
28+
;upload_port = COM1
File renamed without changes.

0 commit comments

Comments
 (0)