1 %% Copyright (C) 2013 Czech Technical University in Prague
4 %% - Carlos Jenkins <carlos@jenkins.co.cr>
6 %% This program is free software; you can redistribute it and/or modify
7 %% it under the terms of the GNU General Public License as published by
8 %% the Free Software Foundation; either version 2 of the License, or
9 %% (at your option) any later version.
11 %% This program is distributed in the hope that it will be useful,
12 %% but WITHOUT ANY WARRANTY; without even the implied warranty of
13 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 %% GNU General Public License for more details.
16 %% You should have received a copy of the GNU General Public License
17 %% along with this program. If not, see <http://www.gnu.org/licenses/>.
19 %% File : rpp_srmain.tlc
21 %% Custom file processing to generate a "main" file.
27 %function FcnSingleTaskingMain() void
29 %if GenerateSampleERTMain
30 %assign CompiledModel.GenerateSampleERTMain = TLC_FALSE
34 %assign cFile = LibCreateSourceFile("Source", "Custom", "ert_main")
40 /* Standard includes */
50 #include "sys_common.h"
53 /* Library includes */
54 // FIXME: Implement conditional #include's
55 %% IF GIO (General Input/Outputs)
56 %% IF HOUT (PWM outputs)
57 %% IF MOUT (Motor outputs)
58 %% IF ADC (Analog to Digital Converter)
59 %% IF SCI (Serial Communication Interface)
64 #include "%<LibGetMdlPubHdrBaseName()>.h"
67 %<LibSetSourceFileSection(cFile, "Includes", tmpBuf)>
74 #define STEP_SIZE_MILLIS (%<CompiledModel.FundamentalStepSize>*1000.0)
75 #define CONTROL_PRIORITY 2
76 #define WORKING_PRIORITY 1
79 %<LibSetSourceFileSection(cFile, "Defines", tmpBuf)>
87 bool SHUTDOWN = false;
88 xSemaphoreHandle step_signal = NULL;
91 %<LibSetSourceFileSection(cFile, "Declarations", tmpBuf)>
99 * Model step control and overrun detection task.
101 void control_task(void *p) {
103 const portTickType freq_ticks = STEP_SIZE_MILLIS / portTICK_RATE_MS;
104 portTickType last_wake_time = xTaskGetTickCount();
108 // Wait until next step
109 vTaskDelayUntil(&last_wake_time, freq_ticks);
113 // FIXME: Call overrun routine
116 xSemaphoreGive(step_signal);
121 /* In case of shutdown, delete this task */
126 * Model step logic execution task.
128 void working_task(void *p) {
133 if(xSemaphoreTake(step_signal, portMAX_DELAY)) {
135 %<LibCallModelStep(0)>\
140 /* In case of shutdown, delete this task */
146 * Hardware initialization, task spawning and OS scheduler start.
151 /* Initialize peripherals */
152 // FIXME: Implement conditional init()'s
155 /* Initialize model */
156 %<LibCallModelInitialize()>\
158 /* Create and lock semaphore */
159 vSemaphoreCreateBinary(step_signal);
160 xSemaphoreTake(step_signal, 0);
162 /* Create tasks to step model and start scheduler */
163 // FIXME: How to calculate / policy about model stack size
164 xTaskCreate(control_task, (signed char*) "control_task", 128, NULL,
165 CONTROL_PRIORITY, NULL);
166 xTaskCreate(working_task, (signed char*) "working_task", 4096, NULL,
167 WORKING_PRIORITY, NULL);
168 vTaskStartScheduler();
170 // We should never get here
171 %<LibCallModelTerminate()>\
177 %<LibSetSourceFileSection(cFile, "Functions", tmpBuf)>