rapidstm32_mainlib.tlc
上传用户:zom342
上传日期:2019-12-31
资源大小:473k
文件大小:9k
源码类别:

其他

开发平台:

Others

  1. %% TLC core library for rapid STM32 e.g. for main.c or system initialization
  2. %selectfile NULL_FILE
  3. %include "rapidstm32_it.tlc"
  4. %function FcnGetPeriodFromTID(tid) void
  5.   %return SampleTime[tid].ClockTickStepSize
  6. %endfunction
  7. %% Base sample time is the smallest sample time for the system
  8. %assign :: BaseSampleTime = FcnGetPeriodFromTID(0)
  9. %function ERTStopCheck() Output
  10.   %if RTMStopReqAccessed()
  11.     %assign stopCheck = "(%<RTMGetErrStat()> == %<SLibGetNullDefinitionFromTfl()>) && !%<RTMGetStopRequested()>"
  12.   %else
  13.     %assign stopCheck = "%<RTMGetErrStat()> == %<SLibGetNullDefinitionFromTfl()>"
  14.   %endif
  15.   
  16.   %return stopCheck
  17. %endfunction
  18. %% Setup Device Settings File
  19. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  20. %function FcnGen_DeviceSettings() void
  21. %% Startup header file
  22. %assign hFile = LibCreateSourceFile("Header", "Custom", "%<CompiledModel.Settings.Hardware_Lib_filename>")
  23. %assign cFile = LibCreateSourceFile("Source", "Custom", "%<CompiledModel.Settings.Hardware_Lib_filename>")
  24. %openfile tmpBuf
  25. %switch Stm32DeviceType
  26.    %case "hd"
  27.       /* High Density Device: %<Stm32Cpu> */
  28.       #define STM32F10X_HD
  29.       %break
  30.    %case "md"
  31.       /* Medium Density Device: %<Stm32Cpu> */
  32.       #define STM32F10X_MD
  33.       %break
  34.    %case "ld"
  35.       /* Low Density Device: %<Stm32Cpu> */
  36.       #define STM32F10X_LD
  37. %endswitch
  38. /* Use ST Standard Peripheal Driver Library */
  39. #define USE_STDPERIPH_DRIVER
  40. %closefile tmpBuf
  41. %<LibSetSourceFileSection(hFile,"Defines",tmpBuf)>
  42. %endfunction
  43. %% Setup Hardware Startup File
  44. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  45. %function FcnGen_HardwareStartup() void
  46. %% Startup source file
  47. %assign cFile = LibCreateSourceFile("Source", "Custom", "%<CompiledModel.Settings.Hardware_Lib_filename>") 
  48. %openfile tmpBuf
  49. %%#include <stdio.h>
  50. #include "stm32f10x.h"
  51. %closefile tmpBuf
  52. %<LibSetSourceFileSection(cFile,"Includes",tmpBuf)>
  53. %if (EXISTS(::__STM32F10X_RCC__) == 0)
  54.    %assign hFile = LibCreateSourceFile("Header", "Custom", "%<CompiledModel.Settings.CONF_filename>") 
  55.    %assign :: __STM32F10X_RCC__ = 1
  56.    %openfile tmpBuf
  57.    #include "stm32f10x_rcc.h"
  58.    %closefile tmpBuf
  59.    %<LibSetSourceFileSection(hFile,"Includes",tmpBuf)>
  60. %endif
  61. %endfunction
  62. %% Prepare Other Hardware Peripheral Startup File
  63. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  64. %function FcnGen_PeripheralHardwareStartup() void
  65. %% Startup source file
  66. %assign cFile = LibCreateSourceFile("Source", "Custom", "%<CompiledModel.Settings.Hardware_Lib_filename>")
  67. %<LibAddSourceFileCustomSection(cFile,"Functions","FcnInitPeripheral_Top")>
  68. %<LibAddSourceFileCustomSection(cFile,"Functions","FcnInitPeripheral_Bottom")>
  69. %openfile tmpBuf
  70. void Peripherals_Hardware_Startup(void) {
  71.    /* Other peripheral modules initialization */
  72. %closefile tmpBuf
  73. %<LibSetSourceFileCustomSection(cFile,"FcnInitPeripheral_Top",tmpBuf)>
  74. %openfile tmpBuf
  75. }
  76. %closefile tmpBuf
  77. %<LibSetSourceFileCustomSection(cFile,"FcnInitPeripheral_Bottom",tmpBuf)>
  78. %assign hFile = LibCreateSourceFile("Header", "Custom", "%<CompiledModel.Settings.Hardware_Lib_filename>")
  79. %openfile tmpBuf
  80. /* Other peripheral hardware initialisation routine */
  81. void Peripherals_Hardware_Startup(void); /* Other hardware peripheral startup*/
  82. %closefile tmpBuf
  83. %<LibSetSourceFileSection(hFile,"Functions",tmpBuf)>
  84. %endfunction
  85. %% Setup main.c 
  86. %% Only bareboard is supported (No OS). 
  87. %% 3 Options are available.
  88. %% 1. Singletasking - Single-Rate (sample times of all blocks in the model and the model's fixed step size are the same)
  89. %% 2. Singletasking - Multi-Rate (sample times of all blocks in the model and the model's fixed step size are not the same
  90. %%                                all sample times in the model must be an integer multiple of the model's fixed step size.)
  91. %% 3. Multitasking - Multi-Rate  
  92. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  93. %function FcnSingleTaskingMain() void
  94.   
  95. %% %% Add a #define to the model's public header file model.h
  96.   
  97. %% %assign pubName = LibGetMdlPubHdrBaseName()
  98. %% %assign modelH  = LibCreateSourceFile("Header", "Simulink", pubName)
  99. %% %% Add a #define to the model's private header file model_private.h
  100.   
  101. %% %assign prvName  = LibGetMdlPrvHdrBaseName()
  102. %% %assign privateH = LibCreateSourceFile("Header", "Simulink", prvName)
  103.   
  104. %% Add a #include to the model's C file model.c
  105. %openfile tmpBuf
  106. %%#include "%<CompiledModel.Settings.Hardware_Lib_filename>.h"
  107. #include "stm32f10x.h"
  108. %assign srcFile = LibGetModelDotCFile()
  109. %closefile tmpBuf
  110. %<LibSetSourceFileSection(srcFile, "Includes", tmpBuf)>
  111. %assign modelC  = LibCreateSourceFile("Source", "Simulink", "%<CompiledModel.Settings.Main_filename>") 
  112.   
  113. %openfile tmpBuf
  114. static boolean_T OverrunFlag = 0;
  115. %<SLibDeclareFcnProtoCtlGlobalVariables()>
  116.   
  117. %<LibWriteModelData()>
  118. %closefile tmpBuf
  119.   
  120. %<LibSetSourceFileSection(modelC, "Definitions", tmpBuf)>
  121. %openfile tmpBuf
  122. #include <stdio.h>
  123. #include "stm32f10x.h" /* Required STM32 header file */
  124. #include "%<CompiledModel.Settings.IT_filename>.h" /* Required STM32 header file */
  125. #include "%<LibGetMdlPubHdrBaseName()>.h"
  126. #include "rtwtypes.h" /* MathWorks types */
  127. %if ExtMode
  128.    #include "ext_work.h" /* External Mode header file */
  129. %endif
  130. %closefile tmpBuf
  131. %<LibSetSourceFileSection(modelC, "Includes", tmpBuf)>
  132.   
  133. %openfile tmpBuf
  134.   /* Main program */
  135.   int_T main(void) { 
  136.   %if ExtMode == 1
  137.      %%int_T main(int_T argc, const char_T *argv[]) {
  138.      /* External mode */
  139.      rtERTExtModeParseArgs(ExtMode_argc, ExtMode_argv);
  140.   %else 
  141.   %endif
  142.   /* Initialize target-specific data structures and hardware e.g. ADC etc. */
  143.   %%  Core_Hardware_Startup can be found in sysclk_config.tlc
  144.   %%  Peripherals_Hardware_Startup can be found in rapidstm32_mainlib.tlc
  145.   Core_Hardware_Startup(); /* System clocks: HCLK, PCLK1, PCLK2 */
  146.   Peripherals_Hardware_Startup(); /* Other hardware peripheral startup*/
  147.   %if RSTM32DispDebugInfo
  148.      (void)printf("Init Hardware Success.");
  149.   %endif
  150.   /* Initialize model */
  151.   %<LibCallModelInitialize()>
  152.   %if ExtMode == 1
  153.      /* 
  154.       * The External Mode option selected; therefore,
  155.       * simulating the model step behaviour (in non real-time).
  156.       */
  157.       while (%<ERTStopCheck()>) {
  158.          rt_OneStep();
  159.       }
  160.      /* Disable rt_OneStep() here */
  161.      /* 
  162.       * Termination functions 
  163.       * Disable timer interrupt 
  164.       * Peform target-specific cleanup such as zeroing DAC 
  165.       */
  166.      %<LibCallModelTerminate()>
  167.      return 0;     
  168.   %else
  169.      %% Non ExtMode
  170.      /* Initialize timer hardware */
  171.      /* Enable timer interrupt and start timer */
  172.      /* Using CMSIS function: Systick_Config() defined in core_cm3.h*/
  173.      SysTick_Config(SysTick_Reload_Value);
  174.      /* Continue indefinitely */
  175.      while(1);
  176.    
  177.      /* Termination functions */
  178.      /* Disable timer interrupt */
  179.      /* Peform target-specific cleanup such as zeroing DAC */
  180.      %% %<LibCallModelTerminate()>
  181.      /* Detect and handle errors e.g. timer overruns */
  182.   %endif
  183.  }
  184. %closefile tmpBuf
  185. %<LibSetSourceFileSection(modelC, "Functions", tmpBuf)>
  186. %openfile tmpBuf
  187. /* 
  188.  * rt_Onestep is called from a timer ISR at the base sample time of %<:: BaseSampleTime> sec.
  189.  * The ISR is void SysTick_Handler(void) defined in %<CompiledModel.Settings.IT_filename>.c
  190.  */
  191. void rt_OneStep(void);
  192. %closefile tmpBuf  
  193. %<LibSetSourceFileSection(modelC, "Declarations", tmpBuf)>  
  194. %openfile tmpBuf
  195. /* 
  196.  * rt_Onestep is called from a timer ISR at the base sample time of %<:: BaseSampleTime> sec.
  197.  * The ISR is void SysTick_Handler(void) defined in %<CompiledModel.Settings.IT_filename>.c
  198.  */
  199. void rt_OneStep(void)
  200. {
  201.   /* Disable interrupts here */
  202.     
  203.   /* Check for overun */
  204.   if (OverrunFlag++) {
  205.     %<LibSetRTModelErrorStatus(""Overrun"")>;
  206.     return;
  207.   }
  208.     
  209.   /* Save FPU context here (if necessary) */
  210.   /* Re-enable timer or interrupt here */
  211.     
  212.   %assign varsbuf = LibWriteModelInputs()
  213.   %if varsbuf != ""
  214.   /* Remove conditional, and set model inputs here */
  215.     %<varsbuf>
  216.   %endif
  217.     
  218.   /* Step the model */
  219.   %<LibCallModelStep(0)>
  220.     
  221.   %assign varsbuf = LibWriteModelOutputs()
  222.   %if varsbuf != ""
  223.     /* Remove conditional, and get model outputs here */
  224.     %<varsbuf>
  225.   %endif
  226.     
  227.   /* Indicate task complete */
  228.   OverrunFlag--;
  229.     
  230.   /* Disable interrupts here */
  231.   /* Restore FPU context here (if necessary) */
  232.   /* Enable interrupts here */
  233.   %if ExtMode == 1
  234.     rtExtModeCheckEndTrigger(); /* External Mode */
  235.   %endif
  236. }
  237. %closefile tmpBuf  
  238. %<LibSetSourceFileSection(modelC, "Functions", tmpBuf)>  
  239. %endfunction
  240. %% Generate All Code
  241. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  242. %function SLibGenerateCode() void
  243.   %% Generate additional codes
  244.   %include "rapidstm32_conf.tlc"
  245.   %include "rapidstm32_startupcode.tlc"
  246.   %include "disp_debug_info.tlc"
  247.   %<Gen_code_for_op_debug_info()>
  248.   %<FcnGen_DeviceSettings()>
  249.   %<FcnGen_CoreITHeader()>
  250.   %<FcnGen_CoreIT()>
  251.   %<FcnGen_HardwareStartup()>
  252.   %<FcnGen_PeripheralHardwareStartup()>
  253.   %<Gen_Startup_Code(CrossCompiler)>
  254.   %% Generate main.c
  255.   %if LibIsSingleRateModel() || LibIsSingleTasking()
  256.      %<FcnSingleTaskingMain()>
  257.   %else
  258.      %error None Single Tasking Non Single Rate Not allow
  259.   %endif
  260. %endfunction