Makefile 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # OpenPuck developer convenience targets.
  2. # Pin to clang-format 18 to match CI; output differs between versions, so a
  3. # mismatch would make CI reject locally-formatted code. Resolve the binary in
  4. # order of preference: the apt-style versioned name (CI / Linux), the Homebrew
  5. # llvm@18 keg (macOS), then a bare clang-format as a last resort. Override with
  6. # `make format CLANG_FORMAT=/path/to/clang-format` if yours lives elsewhere.
  7. CLANG_FORMAT ?= $(shell command -v clang-format-18 \
  8. || ([ -x /opt/homebrew/opt/llvm@18/bin/clang-format ] && echo /opt/homebrew/opt/llvm@18/bin/clang-format) \
  9. || ([ -x /usr/local/opt/llvm@18/bin/clang-format ] && echo /usr/local/opt/llvm@18/bin/clang-format) \
  10. || command -v clang-format)
  11. # All hand-written C/C++ sources. Generated headers (git_version.h) are
  12. # gitignored and excluded here so formatting never touches them.
  13. FORMAT_FILES := $(shell find OpenPuck ReversePuckFirmware puck_sniffer pairtui \
  14. \( -name '*.c' -o -name '*.cpp' -o -name '*.h' -o -name '*.hpp' -o -name '*.ino' \) \
  15. -not -name 'git_version.h')
  16. # --- firmware build ---------------------------------------------------------
  17. # The OpenPuck firmware needs two TinyUSB config values that differ from the Adafruit nRF52 core defaults:
  18. # CFG_TUD_HID=6 -- four HID interfaces (Steam mode exposes four puck slots) plus one for mouse
  19. # and one for WebUSB; core default is 2.
  20. # CFG_TUD_TASK_QUEUE_SZ=64 -- deeper usbd event queue; the default 16 deadlocks the loop task under comms
  21. # load -> watchdog reset (see OpenPuck.ino + docs/BUILD_AND_DEPLOY.md).
  22. # CFG_TUD_VENDOR_TX_BUFSIZE=256 -- the WebUSB status blob is ~118 B; the core default 64 can't hold it whole,
  23. # so the panel send (which drops rather than blocks when the FIFO is full) could
  24. # never fit a frame -> blank panel. Sized to hold a full blob with headroom.
  25. # They're baked in here so a normal build is just `make build` -- no need to remember the flags. Override any
  26. # on the command line, e.g. make build CFG_TUD_HID=8 CFG_TUD_TASK_QUEUE_SZ=128
  27. # or add your own defines: make build EXTRA_FLAGS="-DOPK_LOG=1"
  28. FQBN ?= adafruit:nrf52:feather52840
  29. CFG_TUD_HID ?= 6
  30. # Optional output paths; when set, --clean is implied and build artifacts land in OUTPUT_DIR.
  31. # Used by CI: make build BUILD_PATH=build/cache/openpuck OUTPUT_DIR=build/openpuck
  32. BUILD_PATH ?=
  33. OUTPUT_DIR ?=
  34. # Deep so a loop()-context sendReport never blocks on a full usbd event queue (the watchdog path). Sends run
  35. # from loop() now (usbTxPump) for stable RF timing, so this depth is what keeps that block from happening.
  36. CFG_TUD_TASK_QUEUE_SZ ?= 512
  37. # Must hold a whole WebUSB status blob (~118 B) so the drop-on-full panel send can fit one; core default 64 is
  38. # too small and would silently drop every frame (blank panel / stale mappings).
  39. CFG_TUD_VENDOR_TX_BUFSIZE ?= 256
  40. EXTRA_FLAGS ?=
  41. # {build.flags.usb} is expanded by arduino-cli (VID/PID/strings); pass it through verbatim.
  42. USB_EXTRA_FLAGS = -DNRF52840_XXAA {build.flags.usb} -DCFG_TUD_HID=$(CFG_TUD_HID) -DCFG_TUD_TASK_QUEUE_SZ=$(CFG_TUD_TASK_QUEUE_SZ) -DCFG_TUD_VENDOR_TX_BUFSIZE=$(CFG_TUD_VENDOR_TX_BUFSIZE) $(EXTRA_FLAGS)
  43. # When BUILD_PATH is set, --clean + path flags are injected; omitted for fast incremental dev builds.
  44. _PATH_FLAGS = $(if $(BUILD_PATH),--clean --build-path $(BUILD_PATH) --output-dir $(OUTPUT_DIR))
  45. # `flash`/`deploy` take the serial port as a REQUIRED POSITIONAL arg: `make flash /dev/cu.usbmodem1101`.
  46. # (No auto-detect -- uploading to a guessed serial port risks writing to the wrong device. List with
  47. # `arduino-cli board list`.) FLASH_PORT = whatever goal isn't one of our real targets; the catch-all rule at
  48. # the bottom swallows it so make doesn't try to build the port path as a target.
  49. FLASH_PORT := $(filter-out format format-check check build build-raytac \
  50. package-raytac flash-raytac deploy-raytac provision-raytac-softdevice \
  51. build-recovery reversepuck reversepuck-flash reversepuck-deploy flash deploy,$(MAKECMDGOALS))
  52. UPLOAD = arduino-cli upload -b $(FQBN) -p "$(FLASH_PORT)" OpenPuck
  53. # ReversePuck (controller dongle, 28DE:1302) build flags. It has ONE HID interface (core default 2 is fine),
  54. # so it doesn't need CFG_TUD_HID; it DOES need the deeper vendor TX FIFO to hold the 0xAC paired-pucks list
  55. # whole, and the deeper usbd task queue keeps loop()-context WebUSB/CDC/HID sends off the watchdog path.
  56. RP_USB_FLAGS = -DNRF52840_XXAA {build.flags.usb} -DCFG_TUD_TASK_QUEUE_SZ=$(CFG_TUD_TASK_QUEUE_SZ) -DCFG_TUD_VENDOR_TX_BUFSIZE=$(CFG_TUD_VENDOR_TX_BUFSIZE) $(EXTRA_FLAGS)
  57. RP_UPLOAD = arduino-cli upload -b $(FQBN) -p "$(FLASH_PORT)" ReversePuckFirmware
  58. .PHONY: format format-check check build build-raytac package-raytac \
  59. flash-raytac deploy-raytac provision-raytac-softdevice build-recovery \
  60. reversepuck reversepuck-flash reversepuck-deploy flash deploy
  61. ## Compile the firmware with the required USB flags baked in. Override CFG_TUD_HID / CFG_TUD_TASK_QUEUE_SZ /
  62. ## EXTRA_FLAGS / FQBN as make variables if needed.
  63. build:
  64. arduino-cli compile -b $(FQBN) $(_PATH_FLAGS) --build-property "build.extra_flags=$(USB_EXTRA_FLAGS)" OpenPuck
  65. ## Build for the Raytac MDBT50Q-CX-40 without replacing its Open DFU bootloader.
  66. ## Like `build`, this does NOT run gen_version.sh -- run it yourself first if you want version provenance
  67. ## baked in. (It must not run here: CI generates git_version.h once with OPK_BUILD_VERSION set, and a
  68. ## regeneration mid-workflow would strip the release version out of every build that follows.)
  69. build-raytac:
  70. mkdir -p build/raytac build/cache/raytac
  71. arduino-cli compile --clean -b adafruit:nrf52:mdbt50qrx \
  72. --build-path build/cache/raytac \
  73. --output-dir build/raytac \
  74. --build-property "build.extra_flags=$(USB_EXTRA_FLAGS) -DOPK_BOARD_MDBT50Q_CX_40=1" OpenPuck
  75. ## Package an existing Raytac build for its factory Nordic Open DFU bootloader.
  76. package-raytac:
  77. @test -f build/raytac/OpenPuck.ino.hex || { \
  78. echo "run 'make build-raytac' first"; exit 1; }
  79. $(RM) build/raytac/OpenPuck-mdbt50q-cx-40.zip
  80. nrfutil nrf5sdk-tools pkg generate --hw-version 52 --sd-req 0xB6 \
  81. --application-version 1 \
  82. --application build/raytac/OpenPuck.ino.hex \
  83. build/raytac/OpenPuck-mdbt50q-cx-40.zip
  84. ## Program the application package with the device in physical Open DFU mode.
  85. flash-raytac:
  86. @test -f build/raytac/OpenPuck-mdbt50q-cx-40.zip || { \
  87. echo "run 'make package-raytac' first"; exit 1; }
  88. nrfutil device program \
  89. --firmware build/raytac/OpenPuck-mdbt50q-cx-40.zip \
  90. --traits nordicDfu
  91. ## Build, package, and program the application without changing the SoftDevice.
  92. deploy-raytac:
  93. $(MAKE) build-raytac
  94. $(MAKE) package-raytac
  95. $(MAKE) flash-raytac
  96. ## One-time S140 6.1.1 provisioning; re-enter Open DFU before flashing the app.
  97. provision-raytac-softdevice:
  98. @test -f "$(SOFTDEVICE_HEX)" || { \
  99. echo "set SOFTDEVICE_HEX to Nordic's S140 6.1.1 HEX"; \
  100. exit 1; }
  101. mkdir -p build/raytac
  102. $(RM) build/raytac/s140-6.1.1.zip
  103. nrfutil nrf5sdk-tools pkg generate --hw-version 52 \
  104. --sd-req 0xAE,0xB6 --sd-id 0xB6 \
  105. --softdevice "$(SOFTDEVICE_HEX)" \
  106. build/raytac/s140-6.1.1.zip
  107. nrfutil device program --firmware build/raytac/s140-6.1.1.zip \
  108. --traits nordicDfu
  109. ## One-time factory-reset recovery image (wipes persistent storage once on first boot). See §6 of the build doc.
  110. build-recovery:
  111. $(MAKE) build BUILD_PATH=$(BUILD_PATH) OUTPUT_DIR=$(OUTPUT_DIR) EXTRA_FLAGS="$(EXTRA_FLAGS) -DOPK_FACTORY_RESET=1"
  112. ## Compile the ReversePuck controller dongle firmware (28DE:1302) with its WebUSB vendor flags baked in.
  113. reversepuck:
  114. arduino-cli compile -b $(FQBN) $(_PATH_FLAGS) --build-property "build.extra_flags=$(RP_USB_FLAGS)" ReversePuckFirmware
  115. ## Upload the most recent ReversePuck build. Usage: make reversepuck-flash <port>
  116. reversepuck-flash:
  117. @[ -n "$(FLASH_PORT)" ] || { echo "usage: make reversepuck-flash <port> e.g. make reversepuck-flash /dev/cu.usbmodem1101"; exit 1; }
  118. $(RP_UPLOAD)
  119. ## Build then upload the ReversePuck dongle in one step. Usage: make reversepuck-deploy <port>
  120. reversepuck-deploy:
  121. @[ -n "$(FLASH_PORT)" ] || { echo "usage: make reversepuck-deploy <port> e.g. make reversepuck-deploy /dev/cu.usbmodem1101"; exit 1; }
  122. $(MAKE) reversepuck BUILD_PATH=$(BUILD_PATH) OUTPUT_DIR=$(OUTPUT_DIR)
  123. $(RP_UPLOAD)
  124. ## Upload the most recent build. Usage: make flash <port> e.g. make flash /dev/cu.usbmodem1101
  125. ## The board may need DFU/bootloader mode first (double-tap RST): puck (Steam/Lizard) mode drops the CDC port
  126. ## so arduino-cli can't auto-reset it -- see docs/BUILD_AND_DEPLOY.md.
  127. flash:
  128. @[ -n "$(FLASH_PORT)" ] || { echo "usage: make flash <port> e.g. make flash /dev/cu.usbmodem1101 (list ports: arduino-cli board list)"; exit 1; }
  129. $(UPLOAD)
  130. ## Build then upload in one step. Usage: make deploy <port> (same build overrides as `build`).
  131. deploy:
  132. @[ -n "$(FLASH_PORT)" ] || { echo "usage: make deploy <port> e.g. make deploy /dev/cu.usbmodem1101"; exit 1; }
  133. $(MAKE) build BUILD_PATH=$(BUILD_PATH) OUTPUT_DIR=$(OUTPUT_DIR)
  134. $(UPLOAD)
  135. # Swallow the positional <port> arg so make doesn't error trying to build it as a target. Scoped to things
  136. # that look like a serial port (so a real typo like `make buld` still errors instead of silently no-op'ing).
  137. /dev/% COM% tty% cu.%:
  138. @:
  139. ## Reformat all C/C++ sources in place using the Linux kernel style.
  140. format:
  141. $(CLANG_FORMAT) -i $(FORMAT_FILES)
  142. ## Verify all C/C++ sources are formatted; exit non-zero if any differ.
  143. ## Used by CI to reject unformatted code.
  144. format-check:
  145. $(CLANG_FORMAT) --dry-run --Werror $(FORMAT_FILES)
  146. ## Everything CI gates on.
  147. check: format-check