# Bare-metal Streamline RTX5 RTOS example for Cortex-M33
#
# Copyright (c) 2018-2024 Arm Limited (or its affiliates). All rights reserved.
# Use, modification and redistribution of this file is subject to your possession of a
# valid End User License Agreement for the Arm Product of which these examples are part of
# and your compliance with all applicable terms and conditions of such license agreement.
#
# This makefile is intended for use with GNU make
# This example is intended to be built with Arm Compiler for Embedded 6

IMAGE = RTX5_Cortex-M33_Blinky_Streamline.axf
CC=armclang
LD=armlink
AR=armar
FE=fromelf

# Select build rules based on Windows or Unix
ifdef WINDIR
WINPATH=$(subst /,\,$(1))
DONE=@if exist $(call WINPATH,$(1)) echo Build completed.
RM=if exist $(call WINPATH,$(1)) del /q $(call WINPATH,$(1))
MD=if not exist $(call WINPATH,$(1)) mkdir $(call WINPATH,$(1))
SHELL=$(WINDIR)\system32\cmd.exe
else
ifdef windir
WINPATH=$(subst /,\,$(1))
DONE=@if exist $(call WINPATH,$(1)) echo Build completed.
RM=if exist $(call WINPATH,$(1)) del /q $(call WINPATH,$(1))
MD=if not exist $(call WINPATH,$(1)) mkdir $(call WINPATH,$(1))
SHELL=$(windir)\system32\cmd.exe
else
DONE=@if [ -f $(1) ]; then echo Build completed.; fi
RM=rm -f $(1)
MD=@if [ ! -d $(1) ]; then mkdir -p $(1); fi
endif
endif

.phony: all clean

CMSIS_PACKS ?=/path/to/CMSIS-Packs

CMSIS_PACK ?=$(CMSIS_PACKS)/ARM/CMSIS/6.0.0
DFP_PACK ?=$(CMSIS_PACKS)/ARM/Cortex_DFP/1.0.0
RTX_PACK ?=$(CMSIS_PACKS)/ARM/CMSIS-RTX/5.8.0

ifeq ($(wildcard $(CMSIS_PACK)),)
$(error The CMSIS Pack folder was not found at CMSIS_PACK=$(CMSIS_PACK).  To build this example, install the ARM.CMSIS.6.0.0 (or later) Pack using the Pack Manager, then set CMSIS_PACK to its folder path)
endif

ifeq ($(wildcard $(DFP_PACK)),)
$(error The Cortex_DFP Pack folder was not found at DFP_PACK=$(DFP_PACK).  To build this example, install the ARM.Cortex_DFP.1.0.0 (or later) Pack using the Pack Manager, then set DFP_PACK to its folder path)
endif

ifeq ($(wildcard $(RTX_PACK)),)
$(error The CMSIS-RTX Pack folder was not found at RTX_PACK=$(RTX_PACK).  To build this example, install the ARM.CMSIS-RTX.5.8.0 (or later) Pack using the Pack Manager, then set RTX_PACK to its folder path)
endif

CPU=--target=arm-arm-none-eabi -mcpu=cortex-m33+nodsp -mfpu=none
CFLAGS=-xc -std=c99 -O1 -g -MD -MP -c
INCLUDES= -I"./RTE" -I"./RTE/CMSIS" -I"$(RTX_PACK)/Include" -I"$(CMSIS_PACK)/CMSIS/Core/Include" -I"$(CMSIS_PACK)/CMSIS/RTOS2/Include" -I"$(DFP_PACK)/Device/ARMCM33/Include"
DEFINES=-D_RTE_ -DARMCM33 -DSTANDALONE -DBM_ITM_TRACE_ID=1

all:
	$(call MD,./Debug)
	$(call MD,./Debug/RTE)
# Device Support
	$(call MD,./Debug/RTE/Device)
	$(call MD,./Debug/RTE/Device/ARMCM33)
	$(CC) $(CPU) $(DEFINES) $(INCLUDES) $(CFLAGS) -o "./Debug/RTE/Device/ARMCM33/startup_ARMCM33.o" "./RTE/Device/ARMCM33/startup_ARMCM33.c"
	$(CC) $(CPU) $(DEFINES) $(INCLUDES) $(CFLAGS) -o "./Debug/RTE/Device/ARMCM33/system_ARMCM33.o" "./RTE/Device/ARMCM33/system_ARMCM33.c"
# RTX Library
	$(call MD,./Debug/RTE/CMSIS)
	$(CC) $(CPU) $(DEFINES) $(INCLUDES) $(CFLAGS) -o "./Debug/RTE/CMSIS/RTX_Config.o" "./RTE/CMSIS/RTX_Config.c"
	$(CC) $(CPU) $(DEFINES) $(INCLUDES) $(CFLAGS) -o "./Debug/RTE/CMSIS/rtx_lib.o" "$(RTX_PACK)/Source/rtx_lib.c"
# RTX Application
	$(CC) $(CPU) $(DEFINES) $(INCLUDES) $(CFLAGS) -o "main.o" "main.c"
	$(CC) $(CPU) $(DEFINES) $(INCLUDES) $(CFLAGS) -o "retarget.o" "retarget.c"
	$(CC) $(CPU) $(DEFINES) $(INCLUDES) $(CFLAGS) -o "uart.o" "uart.c"
	$(CC) $(CPU) $(DEFINES) $(INCLUDES) $(CFLAGS) -o "barman.o" "barman.c"

	$(LD) --entry=Reset_Handler --scatter="./RTE/Device/ARMCM33/ARMCM33_ac6.sct" --info=totals -o $(IMAGE) ./Debug/RTE/Device/ARMCM33/startup_ARMCM33.o ./Debug/RTE/Device/ARMCM33/system_ARMCM33.o ./Debug/RTE/CMSIS/RTX_Config.o ./Debug/RTE/CMSIS/rtx_lib.o main.o retarget.o uart.o barman.o "$(RTX_PACK)/Library/ARM/RTX_V8MM.lib" --diag_suppress=L6314

clean:
	$(call RM,./Debug/RTE/Device/ARMCM33/*.o)
	$(call RM,./Debug/RTE/CMSIS/*.o)
	$(call RM,*.o)
	$(call RM,$(IMAGE))
