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 TLC file to generate an RPP "main" file.
23 %% This file generates the "main" file for the RPP target on top of the RPP
24 %% library and the FreeRTOS operating system.
27 %% Example in <matlabroot>/rtw/c/tlc/mw/bareboard_srmain.tlc
32 %function FcnSingleTaskingMain() void
34 %if GenerateSampleERTMain
35 %assign CompiledModel.GenerateSampleERTMain = TLC_FALSE
39 %assign cFile = LibCreateSourceFile("Source", "Custom", "ert_main")
49 #include "%<LibGetMdlPubHdrBaseName()>.h"
52 %<LibSetSourceFileSection(cFile, "Includes", tmpBuf)>
60 #define STEP_SIZE_MILLIS %<CompiledModel.FundamentalStepSize>*1000.0
61 #define CONTROL_PRIORITY 2
62 #define WORKING_PRIORITY 1
65 %<LibSetSourceFileSection(cFile, "Defines", tmpBuf)>
72 static boolean_t WORKING = FALSE;
73 static boolean_t SHUTDOWN = FALSE;
74 static xSemaphoreHandle step_signal = NULL;
75 static uint32_t steps_control = 0;
76 static uint32_t steps_working = 0;
79 %<LibSetSourceFileSection(cFile, "Declarations", tmpBuf)>
87 * Model step control and overrun detection task.
89 void control_task(void *p)
92 static const portTickType freq_ticks = STEP_SIZE_MILLIS / portTICK_RATE_MS;
93 portTickType last_wake_time = xTaskGetTickCount();
97 /* Wait until next step */
98 vTaskDelayUntil(&last_wake_time, freq_ticks);
101 /* Overrun detected */
102 %<LibSetRTModelErrorStatus("\"Overrun\"")>;
103 /* FIXME: Call overrun routine */
105 /* Release semaphore */
106 xSemaphoreGive(step_signal);
112 /* In case of shutdown, delete this task */
117 * Model step logic execution task.
119 void working_task(void *p)
124 if(xSemaphoreTake(step_signal, portMAX_DELAY)) {
126 %<LibCallModelStep(0)>\
132 /* In case of shutdown, delete this task */
138 * Hardware initialization, task spawning and OS scheduler start.
142 /* Initialize RPP board */
146 %assign model_info = SPRINTF("'%s' - %s (TLC %s)\\r\\n", LibGetMdlPubHdrBaseName(), TLC_TIME, TLC_VERSION)
147 rpp_sci_printf((const char*)
152 /* Initialize model */
153 %<LibCallModelInitialize()>\
155 /* Create and lock semaphore */
156 vSemaphoreCreateBinary(step_signal);
157 xSemaphoreTake(step_signal, 0);
159 /* Create tasks to step model and start scheduler */
160 xTaskCreate(control_task, (const signed char*)"control_task",
161 configMINIMAL_STACK_SIZE, NULL, CONTROL_PRIORITY, NULL);
162 xTaskCreate(working_task, (const signed char*)"working_task",
163 %<rppModelTaskStack>, NULL, WORKING_PRIORITY, NULL);
164 vTaskStartScheduler();
166 /* We should never get here */
167 %<LibCallModelTerminate()>\
169 rpp_sci_printf((const char*)
170 "ERROR: Problem allocating memory for scheduler to start.\r\n"
179 #if configUSE_MALLOC_FAILED_HOOK == 1
181 * FreeRTOS malloc() failed hook.
183 void vApplicationMallocFailedHook(void) {
185 rpp_sci_printf((const char*)
186 "ERROR: manual memory allocation failed.\r\n"
193 #if configCHECK_FOR_STACK_OVERFLOW > 0
195 * FreeRTOS stack overflow hook.
197 void vApplicationStackOverflowHook(xTaskHandle xTask,
198 signed portCHAR *pcTaskName) {
200 rpp_sci_printf((const char*)
201 "ERROR: Stack overflow : \"%s\".\r\n", pcTaskName
208 %<LibSetSourceFileSection(cFile, "Functions", tmpBuf)>