From e8f9daac97f55171661679ea79eb1b6b5fc14784 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Mon, 8 Feb 2021 18:59:07 +1300 Subject: Add basic unit testing for util.c --- .gitmodules | 3 +++ Makefile | 54 +++++++++++++++++++++++++++++++++++++----------------- config.mk | 1 - util.c | 4 ++-- 4 files changed, 42 insertions(+), 20 deletions(-) diff --git a/.gitmodules b/.gitmodules index 694f6ad..5c07d44 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "BMP3-Sensor-API"] path = BMP3-Sensor-API url = https://github.com/BoschSensortec/BMP3-Sensor-API +[submodule "unity"] + path = unity + url = https://github.com/ThrowTheSwitch/Unity/ diff --git a/Makefile b/Makefile index 1f57333..7b2f59b 100644 --- a/Makefile +++ b/Makefile @@ -1,24 +1,35 @@ include config.mk -CC ?= avr-gcc +CC ?= gcc +AVR_CC ?= avr-gcc GDB ?= avr-gdb OBJCOPY ?= avr-objcopy AVRDUDE ?= avrdude QEMU ?= qemu-system-avr +# options for native (i.e. unit test) +UNITY_SRC ?= unity/src +UNITY_O = $(UNITY_SRC)/unity.o +CFLAGS += -Wall -Wextra -I$(UNITY_SRC) +LDFLAGS += -lm + +# options for cross-compile MCU ?= atmega32u4 SIM_MCU ?= atmega2560 -COMMON_CFLAGS += -Wall -Wextra -DF_CPU=16000000UL -CFLAGS += $(COMMON_CFLAGS) -mmcu=$(MCU) -O3 -SIM_CFLAGS += $(COMMON_CFLAGS) -mmcu=$(SIM_MCU) -g3 -gdwarf-2 -DWDT_DISABLE -COMMON_LDFLAGS += -Wl,-u,vfprintf -lprintf_flt -LDFLAGS += $(COMMON_LDFLAGS) -mmcu=$(MCU) -SIM_LDFLAGS += $(COMMON_LDFLAGS) -mmcu=$(SIM_MCU) +# Compiler flags for cross-compile +AVR_COMMON_CFLAGS += -Wall -Wextra -DF_CPU=16000000UL +AVR_CFLAGS += $(AVR_COMMON_CFLAGS) -mmcu=$(MCU) -O3 +SIM_CFLAGS += $(AVR_COMMON_CFLAGS) -mmcu=$(SIM_MCU) -g3 -gdwarf-2 -DWDT_DISABLE + +# Linker flags for cross-compile +AVR_COMMON_LDFLAGS += -Wl,-u,vfprintf -lprintf_flt +AVR_LDFLAGS += $(AVR_COMMON_LDFLAGS) -mmcu=$(MCU) +SIM_LDFLAGS += $(AVR_COMMON_LDFLAGS) -mmcu=$(SIM_MCU) -$(shell mkdir -p build/{real,sim}) +$(shell mkdir -p build/{real,sim,native}) -all: build/altimeter_sim.elf build/altimeter.hex +all: build/altimeter_sim.elf build/altimeter.hex test # Run the sim build software in qemu, halted ready for `make gdb` to attach and # continue. Note that sim build mocks out some hardware components @@ -28,13 +39,19 @@ emu: build/altimeter_sim.elf # Attach GDB session to qemu instance started with `make emu` gdb: $(GDB) -ex "target remote :1234" - # Flash AVR software to microcontroller with AVRdude flash: build/altimeter.hex $(AVRDUDE) -F -V -c avr109 -p $(MCU) -P $(PORT) -b 115200 -U flash:w:$< clean: - rm -rf build/ + rm -rf build/ $(UNITY_O) + +test: \ + $(UNITY_O) \ + build/native/test_util.o \ + build/native/util.o + $(CC) $(LDFLAGS) -o $@ $^ + ./$@ # ELF for real hardware. No mock/sim hardware included (except barometer, # haven't written that code yet) @@ -43,7 +60,7 @@ build/altimeter.elf: \ build/real/display_sim.o \ build/real/altimeter.o \ build/real/util.o - $(CC) -o $@ $^ $(LDFLAGS) + $(AVR_CC) $(AVR_LDFLAGS) -o $@ $^ # ELF for simulator/emu. Note that whever I/O is required, the *_sim.{o,c} # versions of each component will provide a mocked interface. This allows for @@ -54,18 +71,21 @@ build/altimeter_sim.elf: \ build/sim/display_sim.o \ build/sim/altimeter.o \ build/sim/util.o - $(CC) -o $@ $^ $(SIM_LDFLAGS) + $(AVR_CC) $(SIM_LDFLAGS) -o $@ $^ build/sim/%.o: %.c - $(CC) -c -o $@ $^ $(SIM_CFLAGS) + $(AVR_CC) $(SIM_CFLAGS) -c -o $@ $^ build/real/%.o: %.c - $(CC) -c -o $@ $^ $(CFLAGS) + $(AVR_CC) $(AVR_CFLAGS) -c -o $@ $^ + +build/native/%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $^ build/%.hex: build/%.elf $(OBJCOPY) -O ihex -R .eeprom $< $@ build/%.elf: - $(CC) -o $@ $^ $(CFLAGS) + $(AVR_CC) $(AVR_CFLAGS) -o $@ $^ -.PHONY: all emu gdb flash clean +.PHONY: all emu gdb flash clean test diff --git a/config.mk b/config.mk index 7eb0c2e..e69de29 100644 --- a/config.mk +++ b/config.mk @@ -1 +0,0 @@ -CC = avr-gcc diff --git a/util.c b/util.c index bd3f71f..d78809f 100644 --- a/util.c +++ b/util.c @@ -5,13 +5,13 @@ float pressure_to_metres_asl(float real, float setting) { - return 44330.f*(1.f-pow(real/setting, 1/5.255)); + return 44330.f*(1.f-powf(real/setting, 1/5.255)); } void blank_to_eol(char *line, size_t len) { size_t i = 0; - for (i = strlen(line); i < len - 1; i++) { + for (i = strnlen(line, len - 1); i < len - 1; i++) { line[i] = ' '; } line[i] = '\0'; -- cgit v1.1