Files
proxmark3/fpga/proxmark5_fpga.mk
iceman1001andClaude Opus 5.5 f777b91a09 PM5: ship the FPGA image built from the repo sources
client/resources/PM5_FPGA_fix_loedge_bug.bin was a Gowin IDE build
posted in #3449 on 2026-08-15, before the FPGA sources were opened in
#3697. Its lo_edge_detect fix (adc_clk = pck0) has been in the sources
since 7666e8444, so the shipped binary only lagged behind them and
could not be checked against anything in the tree.

Add `make -C fpga fpga_pm5_release`, which builds fpga_pm5.bin and
copies it with its sha512 to client/resources. It stays out of `all`
so a local test build does not replace the shipped image by accident.
Point update_fpga.cmd and update_fpga_and_bwm.cmd at fpga_pm5.bin and
document the build in Getting_Started_With_PM5.md.

The image was built with oss-cad-suite 2026-10-11, the release pinned
in fpga-pm5.yml. A clean rebuild gives the same bytes, IDCODE is
0x1100381B, and all clocks meet timing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 11:18:13 +07:00

183 lines
8.0 KiB
Makefile

#
# Proxmark5 FPGA build (open source toolchain: yosys + nextpnr-himbaechel + apycula)
#
# Invoked by the fpga/Makefile dispatcher, or directly:
# make -C fpga -f proxmark5_fpga.mk [all|clean]
#
# Overridable variables:
# OSS_CAD_SUITE oss-cad-suite root, holds yosys/nextpnr-himbaechel/gowin_pack
# PM5_FAMILY Gowin family, selects the chip database in nextpnr
# PM5_PART exact part, must match the board
# PM5_IDCODE IDCODE the bitstream declares, must match the silicon
# PM5_CST pin constraints, generated by the Gowin IDE
# PM5_SDC timing constraints, generated by the Gowin IDE
# PM5_FREQ fallback clock constraint, used when PM5_SDC is empty
# PM5_DEFINES HDL defines, one image covers both the LF and HF halves
include $(dir $(lastword $(MAKEFILE_LIST)))common_src.mk
# oss-cad-suite is looked up next to the checkout and under the usual Desktop
# locations. Leave OSS_CAD_SUITE empty to use tools that are already on PATH
# (i.e. when make runs inside the suite's own shell).
OSS_CAD_SUITE ?= $(firstword $(wildcard \
$(abspath $(CURDIR)/../../oss-cad-suite) \
$(HOME)/Desktop/oss-cad-suite \
$(USERPROFILE)/Desktop/oss-cad-suite))
PM5_FAMILY ?= GW1N-4
PM5_PART ?= GW1N-UV4QN48C6/I5
PM5_IDCODE ?= 0x1100381B
PM5_CST ?= $(PM5_DIR)/gowin.cst
PM5_SDC ?= $(PM5_DIR)/gowin.sdc
PM5_FREQ ?= 24
PM5_DEFINES ?= PM5 WITH_TIMODE WITH_HF_15 WITH_FELICA_PROBE
# The Gowin IDE generated SDC covers every clock, but nextpnr only takes a
# subset of SDC; it is rewritten into the build directory, leaving the original
# file untouched for the next regeneration from the IDE. See the script for
# what it drops. PM5_FREQ is the fallback for the unconstrained case.
PM5_TIMING = $(if $(PM5_SDC),--sdc $(PM5_NAME).sdc,--freq $(PM5_FREQ))
PM5_SDC_CLEAN = $(OC_ENV) $(OC_PYTHON) $(COMMON_DIR)/gowin_sdc_clean.py
PM5_NAME = fpga_pm5
PM5_OUT = $(PREFIX)build_$(PM5_NAME)
# The oss-cad-suite launchers need their root, and both bin/ and lib/ on PATH
# (lib/ carries the shared libraries), otherwise they exit without a message.
# The suite is laid out per platform, and each of its own environment scripts
# says what to put where. On Windows the shared libraries sit in lib/ and are
# found through PATH, as environment.bat does. Everywhere else they are found by
# the tools themselves, and adding lib/ to PATH or LD_LIBRARY_PATH makes yosys
# load the suite's libstdc++ instead of the system one and segfault at startup.
# With OSS_CAD_SUITE unset the tools come from PATH, which is what sourcing the
# suite's environment gives you.
ifneq ($(OSS_CAD_SUITE),)
ifneq ($(wildcard $(OSS_CAD_SUITE)/lib/python3.exe),)
OC_PATH = $(OSS_CAD_SUITE)/bin:$(OSS_CAD_SUITE)/lib
OC_PYTHON = $(OSS_CAD_SUITE)/lib/python3.exe
else
OC_PATH = $(OSS_CAD_SUITE)/bin:$(OSS_CAD_SUITE)/py3bin
OC_PYTHON = $(firstword $(wildcard $(OSS_CAD_SUITE)/py3bin/python3 $(OSS_CAD_SUITE)/bin/python3) python3)
endif
OC_ENV = YOSYSHQ_ROOT="$(OSS_CAD_SUITE)/" PATH="$(OC_PATH):$$PATH" PYTHON_EXECUTABLE="$(OC_PYTHON)"
else
OC_ENV =
OC_PYTHON = python3
endif
PM5_BUILD_HDL = $(addprefix ../,$(PM5_HDL))
PM5_DEFS = $(foreach d,$(PM5_DEFINES),-D $(d))
.PHONY: all clean help check-tools smoke $(PM5_NAME) $(PM5_NAME)_release
all: $(PM5_NAME)
$(PM5_NAME): $(PM5_NAME).fs $(PM5_NAME).bin
# Only the bitstream is copied next to the Xilinx ones, everything else stays
# in the build directory. Unlike the PM3 targets these recipes run in the fpga
# directory itself, so the destination is this directory, not its parent.
$(PM5_NAME).fs: $(PM5_OUT)/$(PM5_NAME).fs
$(Q)$(CP) $< .
$(PM5_NAME).bin: $(PM5_OUT)/$(PM5_NAME).bin
$(Q)$(CP) $< .
# Ship the bitstream to the client resources, where `hw fpga config` and the
# update_fpga scripts find it. Kept apart from `all` so a local test build does
# not replace the released image by accident.
PM5_RELEASE_DIR = ../client/resources
$(PM5_NAME)_release: $(PM5_NAME).bin
$(info [=] RELEASE $(PM5_RELEASE_DIR)/$(PM5_NAME).bin)
$(Q)cp $(PM5_NAME).bin $(PM5_RELEASE_DIR)/$(PM5_NAME).bin
$(Q)cd $(PM5_RELEASE_DIR) && $(OC_ENV) $(OC_PYTHON) -c "import hashlib; n = '$(PM5_NAME).bin'; print(hashlib.sha512(open(n, 'rb').read()).hexdigest() + ' ' + n)" > $(PM5_NAME).sha512.txt
# The pin constraints are board specific and are not part of the repository.
$(PM5_CST):
$(error [!] missing $@ -- fill in the PM5 pin constraints, then rerun)
# The makefiles are prerequisites of the generated files so that editing the
# defines or the source lists regenerates them.
$(PM5_OUT)/$(PM5_NAME).json: $(PM5_HDL) $(MAKEFILE_LIST) | check-tools $(PM5_OUT)
$(info [-] YOSYS $@)
$(Q)$(RM) $@
$(Q)printf '%s\n' \
"read_verilog $(PM5_DEFS) $(PM5_BUILD_HDL)" \
"hierarchy -top fpga_top" \
"synth_gowin -nolutram -json $(PM5_NAME).json" > $(PM5_OUT)/synth.ys
$(Q)cd $(PM5_OUT) && $(OC_ENV) yosys -q -s synth.ys
$(PM5_OUT)/$(PM5_NAME).sdc: $(PM5_SDC) $(COMMON_DIR)/gowin_sdc_clean.py $(MAKEFILE_LIST) | $(PM5_OUT)
$(Q)$(PM5_SDC_CLEAN) $< $@
$(PM5_OUT)/$(PM5_NAME)_pnr.json: $(PM5_OUT)/$(PM5_NAME).json $(PM5_CST) $(if $(PM5_SDC),$(PM5_OUT)/$(PM5_NAME).sdc)
$(info [=] NEXTPNR $@)
$(Q)cd $(PM5_OUT) && $(OC_ENV) nextpnr-himbaechel \
--device "$(PM5_PART)" \
--vopt family=$(PM5_FAMILY) \
--vopt cst=../$(PM5_CST) \
$(PM5_TIMING) \
--json $(PM5_NAME).json \
--write $(PM5_NAME)_pnr.json
# TODO: workaround, drop once apycula can target GW1N-4D. The GW1N-4 database
# apycula ships stamps the plain GW1N-4 IDCODE (0x0100381B), which the silicon
# on a PM5 (GW1N-4B/4D, 0x1100381B) rejects: done_final stays 0 and nothing
# drives the antennas. gowin_devid.py rewrites that field to PM5_IDCODE here so
# the image is accepted at all. Remove this step, PM5_IDCODE and the workflow's
# IDCODE check once apycula gains a GW1N-4B/4D target or stops discarding the
# C6/I5 device version from the part number. Until then, suspect the fuse map
# first when a bitstream misbehaves: it comes from the non-D database entry.
$(PM5_OUT)/$(PM5_NAME).fs: $(PM5_OUT)/$(PM5_NAME)_pnr.json $(COMMON_DIR)/gowin_devid.py
$(info [=] GOWIN_PACK $@)
$(Q)cd $(PM5_OUT) && $(OC_ENV) gowin_pack -d $(PM5_FAMILY) -o $(PM5_NAME).fs $(PM5_NAME)_pnr.json
$(Q)$(OC_ENV) $(OC_PYTHON) $(COMMON_DIR)/gowin_devid.py $(PM5_OUT)/$(PM5_NAME).fs $(PM5_IDCODE)
$(PM5_OUT)/$(PM5_NAME).bin: $(PM5_OUT)/$(PM5_NAME).fs
$(info [=] FS2BIN $@)
$(Q)cd $(PM5_OUT) && $(OC_ENV) $(OC_PYTHON) ../$(COMMON_DIR)/fs2bin.py $(PM5_NAME).fs $(PM5_NAME).bin
$(PM5_OUT):
$(Q)$(MKDIR) $@
check-tools:
$(Q)missing=""; \
for t in yosys nextpnr-himbaechel gowin_pack; do \
$(OC_ENV) command -v $$t >/dev/null 2>&1 || missing="$$missing $$t"; \
done; \
if [ -n "$$missing" ]; then \
echo "[!] not found:$$missing"; \
echo "[!] point OSS_CAD_SUITE at oss-cad-suite, or add its bin/ to PATH"; \
exit 1; \
fi
# Compile a two line design through the same toolchain and environment the
# build uses, so a suite that cannot run fails here with a clear message
# instead of half way through the build.
smoke: | check-tools $(PM5_OUT)
$(Q)$(OC_ENV) yosys -V
$(Q)printf '%s\n' "module smoke(input a, output b);" " assign b = ~a;" "endmodule" > $(PM5_OUT)/smoke.v
$(Q)cd $(PM5_OUT) && $(OC_ENV) yosys -q -p "read_verilog smoke.v; synth_gowin -json smoke.json"
$(Q)test -s $(PM5_OUT)/smoke.json && echo "[=] SMOKE ok"
# ALWAYS have some hardcoded text after $(PREFIX) to avoid rm -rf * or rm -rf /* situations if PREFIX is incorrectly set to empty "" or just "/"
clean:
$(Q)$(RMDIR) $(PREFIX)build_$(PM5_NAME)
$(info [-] Build files deleted)
help:
@echo "################################################################"
@echo "#"
@echo "# $(PM5_NAME) - Builds the PM5 bitstream into $(PM5_NAME).fs/.bin"
@echo "# all - same as $(PM5_NAME)"
@echo "# $(PM5_NAME)_release - Also copies the bitstream to $(PM5_RELEASE_DIR)"
@echo "# clean - Keeps the bitstream but cleans intermediate build files"
@echo "#"
@echo "# Device: $(PM5_PART) (family $(PM5_FAMILY))"
@echo "# Pins : $(PM5_CST)"
@echo "# Toolchain: $(OSS_CAD_SUITE)"
@echo "#"
@echo "################################################################"