rapidstm32_mainlib.tlc
上传用户:zom342
上传日期:2019-12-31
资源大小:473k
文件大小:9k
- %% TLC core library for rapid STM32 e.g. for main.c or system initialization
- %selectfile NULL_FILE
- %include "rapidstm32_it.tlc"
- %function FcnGetPeriodFromTID(tid) void
- %return SampleTime[tid].ClockTickStepSize
- %endfunction
- %% Base sample time is the smallest sample time for the system
- %assign :: BaseSampleTime = FcnGetPeriodFromTID(0)
- %function ERTStopCheck() Output
- %if RTMStopReqAccessed()
- %assign stopCheck = "(%<RTMGetErrStat()> == %<SLibGetNullDefinitionFromTfl()>) && !%<RTMGetStopRequested()>"
- %else
- %assign stopCheck = "%<RTMGetErrStat()> == %<SLibGetNullDefinitionFromTfl()>"
- %endif
-
- %return stopCheck
- %endfunction
- %% Setup Device Settings File
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %function FcnGen_DeviceSettings() void
- %% Startup header file
- %assign hFile = LibCreateSourceFile("Header", "Custom", "%<CompiledModel.Settings.Hardware_Lib_filename>")
- %assign cFile = LibCreateSourceFile("Source", "Custom", "%<CompiledModel.Settings.Hardware_Lib_filename>")
- %openfile tmpBuf
- %switch Stm32DeviceType
- %case "hd"
- /* High Density Device: %<Stm32Cpu> */
- #define STM32F10X_HD
- %break
- %case "md"
- /* Medium Density Device: %<Stm32Cpu> */
- #define STM32F10X_MD
- %break
- %case "ld"
- /* Low Density Device: %<Stm32Cpu> */
- #define STM32F10X_LD
- %endswitch
- /* Use ST Standard Peripheal Driver Library */
- #define USE_STDPERIPH_DRIVER
- %closefile tmpBuf
- %<LibSetSourceFileSection(hFile,"Defines",tmpBuf)>
- %endfunction
- %% Setup Hardware Startup File
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %function FcnGen_HardwareStartup() void
- %% Startup source file
- %assign cFile = LibCreateSourceFile("Source", "Custom", "%<CompiledModel.Settings.Hardware_Lib_filename>")
- %openfile tmpBuf
- %%#include <stdio.h>
- #include "stm32f10x.h"
- %closefile tmpBuf
- %<LibSetSourceFileSection(cFile,"Includes",tmpBuf)>
- %if (EXISTS(::__STM32F10X_RCC__) == 0)
- %assign hFile = LibCreateSourceFile("Header", "Custom", "%<CompiledModel.Settings.CONF_filename>")
- %assign :: __STM32F10X_RCC__ = 1
- %openfile tmpBuf
- #include "stm32f10x_rcc.h"
- %closefile tmpBuf
- %<LibSetSourceFileSection(hFile,"Includes",tmpBuf)>
- %endif
- %endfunction
- %% Prepare Other Hardware Peripheral Startup File
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %function FcnGen_PeripheralHardwareStartup() void
- %% Startup source file
- %assign cFile = LibCreateSourceFile("Source", "Custom", "%<CompiledModel.Settings.Hardware_Lib_filename>")
- %<LibAddSourceFileCustomSection(cFile,"Functions","FcnInitPeripheral_Top")>
- %<LibAddSourceFileCustomSection(cFile,"Functions","FcnInitPeripheral_Bottom")>
- %openfile tmpBuf
- void Peripherals_Hardware_Startup(void) {
- /* Other peripheral modules initialization */
- %closefile tmpBuf
- %<LibSetSourceFileCustomSection(cFile,"FcnInitPeripheral_Top",tmpBuf)>
- %openfile tmpBuf
- }
- %closefile tmpBuf
- %<LibSetSourceFileCustomSection(cFile,"FcnInitPeripheral_Bottom",tmpBuf)>
- %assign hFile = LibCreateSourceFile("Header", "Custom", "%<CompiledModel.Settings.Hardware_Lib_filename>")
- %openfile tmpBuf
- /* Other peripheral hardware initialisation routine */
- void Peripherals_Hardware_Startup(void); /* Other hardware peripheral startup*/
- %closefile tmpBuf
- %<LibSetSourceFileSection(hFile,"Functions",tmpBuf)>
- %endfunction
- %% Setup main.c
- %% Only bareboard is supported (No OS).
- %% 3 Options are available.
- %% 1. Singletasking - Single-Rate (sample times of all blocks in the model and the model's fixed step size are the same)
- %% 2. Singletasking - Multi-Rate (sample times of all blocks in the model and the model's fixed step size are not the same
- %% all sample times in the model must be an integer multiple of the model's fixed step size.)
- %% 3. Multitasking - Multi-Rate
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %function FcnSingleTaskingMain() void
-
- %% %% Add a #define to the model's public header file model.h
-
- %% %assign pubName = LibGetMdlPubHdrBaseName()
- %% %assign modelH = LibCreateSourceFile("Header", "Simulink", pubName)
- %% %% Add a #define to the model's private header file model_private.h
-
- %% %assign prvName = LibGetMdlPrvHdrBaseName()
- %% %assign privateH = LibCreateSourceFile("Header", "Simulink", prvName)
-
- %% Add a #include to the model's C file model.c
- %openfile tmpBuf
- %%#include "%<CompiledModel.Settings.Hardware_Lib_filename>.h"
- #include "stm32f10x.h"
- %assign srcFile = LibGetModelDotCFile()
- %closefile tmpBuf
- %<LibSetSourceFileSection(srcFile, "Includes", tmpBuf)>
- %assign modelC = LibCreateSourceFile("Source", "Simulink", "%<CompiledModel.Settings.Main_filename>")
-
- %openfile tmpBuf
- static boolean_T OverrunFlag = 0;
- %<SLibDeclareFcnProtoCtlGlobalVariables()>
-
- %<LibWriteModelData()>
- %closefile tmpBuf
-
- %<LibSetSourceFileSection(modelC, "Definitions", tmpBuf)>
- %openfile tmpBuf
- #include <stdio.h>
- #include "stm32f10x.h" /* Required STM32 header file */
- #include "%<CompiledModel.Settings.IT_filename>.h" /* Required STM32 header file */
- #include "%<LibGetMdlPubHdrBaseName()>.h"
- #include "rtwtypes.h" /* MathWorks types */
- %if ExtMode
- #include "ext_work.h" /* External Mode header file */
- %endif
- %closefile tmpBuf
- %<LibSetSourceFileSection(modelC, "Includes", tmpBuf)>
-
- %openfile tmpBuf
- /* Main program */
- int_T main(void) {
- %if ExtMode == 1
- %%int_T main(int_T argc, const char_T *argv[]) {
- /* External mode */
- rtERTExtModeParseArgs(ExtMode_argc, ExtMode_argv);
- %else
- %endif
- /* Initialize target-specific data structures and hardware e.g. ADC etc. */
- %% Core_Hardware_Startup can be found in sysclk_config.tlc
- %% Peripherals_Hardware_Startup can be found in rapidstm32_mainlib.tlc
- Core_Hardware_Startup(); /* System clocks: HCLK, PCLK1, PCLK2 */
- Peripherals_Hardware_Startup(); /* Other hardware peripheral startup*/
- %if RSTM32DispDebugInfo
- (void)printf("Init Hardware Success.");
- %endif
- /* Initialize model */
- %<LibCallModelInitialize()>
- %if ExtMode == 1
- /*
- * The External Mode option selected; therefore,
- * simulating the model step behaviour (in non real-time).
- */
- while (%<ERTStopCheck()>) {
- rt_OneStep();
- }
- /* Disable rt_OneStep() here */
- /*
- * Termination functions
- * Disable timer interrupt
- * Peform target-specific cleanup such as zeroing DAC
- */
- %<LibCallModelTerminate()>
- return 0;
- %else
- %% Non ExtMode
- /* Initialize timer hardware */
- /* Enable timer interrupt and start timer */
- /* Using CMSIS function: Systick_Config() defined in core_cm3.h*/
- SysTick_Config(SysTick_Reload_Value);
- /* Continue indefinitely */
- while(1);
-
- /* Termination functions */
- /* Disable timer interrupt */
- /* Peform target-specific cleanup such as zeroing DAC */
- %% %<LibCallModelTerminate()>
- /* Detect and handle errors e.g. timer overruns */
- %endif
- }
- %closefile tmpBuf
- %<LibSetSourceFileSection(modelC, "Functions", tmpBuf)>
- %openfile tmpBuf
- /*
- * rt_Onestep is called from a timer ISR at the base sample time of %<:: BaseSampleTime> sec.
- * The ISR is void SysTick_Handler(void) defined in %<CompiledModel.Settings.IT_filename>.c
- */
- void rt_OneStep(void);
- %closefile tmpBuf
- %<LibSetSourceFileSection(modelC, "Declarations", tmpBuf)>
- %openfile tmpBuf
- /*
- * rt_Onestep is called from a timer ISR at the base sample time of %<:: BaseSampleTime> sec.
- * The ISR is void SysTick_Handler(void) defined in %<CompiledModel.Settings.IT_filename>.c
- */
- void rt_OneStep(void)
- {
- /* Disable interrupts here */
-
- /* Check for overun */
- if (OverrunFlag++) {
- %<LibSetRTModelErrorStatus(""Overrun"")>;
- return;
- }
-
- /* Save FPU context here (if necessary) */
- /* Re-enable timer or interrupt here */
-
- %assign varsbuf = LibWriteModelInputs()
- %if varsbuf != ""
- /* Remove conditional, and set model inputs here */
- %<varsbuf>
- %endif
-
- /* Step the model */
- %<LibCallModelStep(0)>
-
- %assign varsbuf = LibWriteModelOutputs()
- %if varsbuf != ""
- /* Remove conditional, and get model outputs here */
- %<varsbuf>
- %endif
-
- /* Indicate task complete */
- OverrunFlag--;
-
- /* Disable interrupts here */
- /* Restore FPU context here (if necessary) */
- /* Enable interrupts here */
- %if ExtMode == 1
- rtExtModeCheckEndTrigger(); /* External Mode */
- %endif
- }
- %closefile tmpBuf
- %<LibSetSourceFileSection(modelC, "Functions", tmpBuf)>
- %endfunction
- %% Generate All Code
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %function SLibGenerateCode() void
- %% Generate additional codes
- %include "rapidstm32_conf.tlc"
- %include "rapidstm32_startupcode.tlc"
- %include "disp_debug_info.tlc"
- %<Gen_code_for_op_debug_info()>
- %<FcnGen_DeviceSettings()>
- %<FcnGen_CoreITHeader()>
- %<FcnGen_CoreIT()>
- %<FcnGen_HardwareStartup()>
- %<FcnGen_PeripheralHardwareStartup()>
- %<Gen_Startup_Code(CrossCompiler)>
- %% Generate main.c
- %if LibIsSingleRateModel() || LibIsSingleTasking()
- %<FcnSingleTaskingMain()>
- %else
- %error None Single Tasking Non Single Rate Not allow
- %endif
- %endfunction