@echo off

set defines=-D"STM32F407xx" -D"USE_HAL_DRIVER"^
 -D"MATLAB"^
 -D"__sizeof_ptr=8"
:: -------------------------USERS PATHS AND CODE---------------------------
:: заголовочные файлы (не добавлять CMSIS и HAL, они добавлены ниже)
set includes_USER= -I".\Core\Inc"

:: исходный код
set code_USER=.\Core\Src\main.c^
 .\Core\Src\tim.c^
 .\Core\Src\gpio.c^
 .\Core\Src\stm32f4xx_it.c^
 .\Core\Src\system_stm32f4xx.c

:: .\Core\Src\stm32f4xx_hal_msp.c^
::-------------------------------------------------------------------------


:: -----------------------MCU LIBRARIES & SIMULATOR------------------------
:: -----MCU LIBRARIES STUFF----
:: заголовочные файлы
set includes_MCU= -I".\MCU_STM32F4xx_Matlab"^
 -I".\MCU_STM32F4xx_Matlab\Drivers\STM32F4xx_SIMULINK"^
 -I".\MCU_STM32F4xx_Matlab\Drivers\CMSIS"^
 -I".\MCU_STM32F4xx_Matlab\Drivers\STM32F4xx_HAL_Driver\Inc"^
 -I".\MCU_STM32F4xx_Matlab\Drivers\STM32F4xx_HAL_Driver\Inc\Legacy"
  
:: код библиотек МК, переделанный для матлаб
set code_MCU=.\MCU_STM32F4xx_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_tim.c^
 .\MCU_STM32F4xx_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_tim_ex.c^
 .\MCU_STM32F4xx_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_rcc.c^
 .\MCU_STM32F4xx_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_gpio.c^
 .\MCU_STM32F4xx_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_dma.c^
 .\MCU_STM32F4xx_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_pwr.c^
 .\MCU_STM32F4xx_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_pwr_ex.c^
 .\MCU_STM32F4xx_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_cortex.c^
 .\MCU_STM32F4xx_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal.c^
 .\MCU_STM32F4xx_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_exti.c

:: .\MCU_STM32F4xx_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_flash_ramfunc.c^
:: .\MCU_STM32F4xx_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_flash.c^
:: .\MCU_STM32F4xx_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_flash_ex.c^
:: .\MCU_STM32F4xx_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_rcc_ex.c^

:: --------MCU SIMULATOR-------
:: код, которая будет симулировать перефирию МК в симулинке
set code_MCU_Sim= .\MCU_STM32F4xx_Matlab\stm32f4xx_matlab_conf.c^
 .\MCU_STM32F4xx_Matlab\Drivers\STM32F4xx_SIMULINK\stm32f4xx_matlab_gpio.c^
 .\MCU_STM32F4xx_Matlab\Drivers\STM32F4xx_SIMULINK\stm32f4xx_matlab_tim.c^
 .\MCU_STM32F4xx_Matlab\Drivers\STM32F4xx_SIMULINK\stm32f4xx_periph_registers.c 
::-------------------------------------------------------------------------


:: -------------------------WRAPPER PATHS AND CODE---------------------------
:: оболочка, которая будет моделировать работу МК в симулинке
set includes_WRAPPER= -I".\MCU_Wrapper"
set code_WRAPPER= .\MCU_Wrapper\Outputs\asmjmp.obj^
 .\MCU_Wrapper\MCU.c^
 .\MCU_Wrapper\controller.c
 
:: оболочка, которая будет моделировать работу МК в симулинке
set asm_WRAPPER=asmjmp
set output_folder=.\MCU_Wrapper\Outputs\
set asm_source=.\MCU_Wrapper\%asm_WRAPPER%.asm
set asm_obj=%output_folder%%asm_WRAPPER%.obj
::-------------------------------------------------------------------------


:: ---------------------SET PARAMS FOR MEX COMPILING-----------------------
:: --------ALL INCLUDES--------
set includes= %includes_USER% %includes_MCU% %includes_WRAPPER%
set codes= %code_WRAPPER% %code_USER% %code_MCU% %code_MCU_Sim%
:: -------OUTPUT FOLDER--------
set output= -outdir "."

:: если нужен дебаг, до запускаем run mex с припиской debug
IF [%1]==[debug] (set debug= -g)
::-------------------------------------------------------------------------


::------START COMPILING-------    
set asm_exe= "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\Hostx64\x64\ml64.exe"
set asm_command= /c /Zi /Fo %asm_obj% %asm_source%
%asm_exe% %asm_command%

echo.
echo Compiling...
mex %output% %defines% %includes% %codes% %debug%
