@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".\Code\Core\Inc" -I".\Code\GENERAL"^
 -I".\Code\Modbus" -I".\Code\PWM"

:: для добавления в переменую новых файлов. иначе будет перезапись
setlocal enabledelayedexpansion 
:: исходный код
set code_USER=

for %%f in (.\Code\Core\Src\*.c) do (
    set code_USER=!code_USER! %%f
)

for %%f in (.\Code\GENERAL\*.c) do (
    set code_USER=!code_USER! %%f
)

for %%f in (.\Code\Modbus\*.c) do (
    set code_USER=!code_USER! %%f
)

for %%f in (.\Code\PWM\*.c) do (
    set code_USER=!code_USER! %%f
)
::-------------------------------------------------------------------------


:: -----------------------MCU LIBRARIES & SIMULATOR------------------------
:: -----MCU LIBRARIES STUFF----
:: заголовочные файлы
set includes_MCU= -I".\MCU_STM32F4xx_Matlab"^
 -I".\MCU_STM32F4xx_Matlab\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_uart.c^
 .\MCU_STM32F4xx_Matlab\Drivers\STM32F4xx_HAL_Driver\Src\stm32f4xx_hal_usart.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\STM32F4xx_SIMULINK\stm32f4xx_matlab_gpio.c^
 .\MCU_STM32F4xx_Matlab\STM32F4xx_SIMULINK\stm32f4xx_matlab_tim.c^
 .\MCU_STM32F4xx_Matlab\STM32F4xx_SIMULINK\stm32f4xx_periph_registers.c 
::-------------------------------------------------------------------------


:: -------------------------WRAPPER PATHS AND CODE---------------------------
:: оболочка, которая будет моделировать работу МК в симулинке
set includes_WRAPPER= -I".\MCU_Wrapper"
set code_WRAPPER= .\MCU_Wrapper\MCU.c^
 .\MCU_Wrapper\mcu_wrapper.c
::-------------------------------------------------------------------------


:: ---------------------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-------    
echo Compiling...
mex %output% %defines% %includes% %codes% %debug%
