# KAGAN A1 DSP firmware (STM32G473CEU6; the G431CBU6 is pin-compatible but has too little RAM for the delay).
#   make TOOLCHAIN=/path/to/arm-gnu-toolchain/bin/ CMSIS=/path/to/cmsis/headers
#   make host-test         # runs the DSP tests on the build machine
# Flash with an ST-Link on the SWD pads, e.g.  st-flash write build/kagan_dsp.bin 0x08000000
TOOLCHAIN ?=
CMSIS ?= cmsis
CC = $(TOOLCHAIN)arm-none-eabi-gcc
OBJCOPY = $(TOOLCHAIN)arm-none-eabi-objcopy
SIZE = $(TOOLCHAIN)arm-none-eabi-size

CFLAGS = -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -O2 -g3 \
         -ffunction-sections -fdata-sections -fsingle-precision-constant \
         -Wall -Wextra -Wdouble-promotion -std=c11 -DSTM32G473xx -I$(CMSIS) -I.
LDFLAGS = -T stm32g473ce.ld -Wl,--gc-sections -Wl,-Map=build/kagan_dsp.map --specs=nano.specs \
          --specs=nosys.specs -lm

SRC = main.c dsp.c startup.c

all: build/kagan_dsp.bin

build/kagan_dsp.elf: $(SRC) dsp.h stm32g473ce.ld | build
	$(CC) $(CFLAGS) $(SRC) $(LDFLAGS) -o $@
	$(SIZE) $@

build/kagan_dsp.bin: build/kagan_dsp.elf
	$(OBJCOPY) -O binary $< $@

build:
	mkdir -p build

host-test:
	cd host && cc -O2 -Wall -Wextra -I.. test_dsp.c ../dsp.c -lm -o test_dsp && ./test_dsp

clean:
	rm -rf build host/test_dsp host/*.wav

.PHONY: all clean host-test
