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 boolean_t WORKING = FALSE;
73 boolean_t SHUTDOWN = FALSE;
74 xSemaphoreHandle step_signal = NULL;
77 %<LibSetSourceFileSection(cFile, "Declarations", tmpBuf)>
85 * Model step control and overrun detection task.
87 void control_task(void *p) {
89 static const portTickType freq_ticks = STEP_SIZE_MILLIS / portTICK_RATE_MS;
90 portTickType last_wake_time = xTaskGetTickCount();
94 /* Wait until next step */
95 vTaskDelayUntil(&last_wake_time, freq_ticks);
98 /* Overrun detected */
99 %<LibSetRTModelErrorStatus("\"Overrun\"")>;
100 /* FIXME: Call overrun routine */
102 /* Release semaphore */
103 xSemaphoreGive(step_signal);
108 /* In case of shutdown, delete this task */
113 * Model step logic execution task.
115 void working_task(void *p) {
120 if(xSemaphoreTake(step_signal, portMAX_DELAY)) {
122 %<LibCallModelStep(0)>\
127 /* In case of shutdown, delete this task */
133 * Hardware initialization, task spawning and OS scheduler start.
137 /* Initialize RPP board */
140 /* Initialize model */
141 %<LibCallModelInitialize()>\
143 /* Create and lock semaphore */
144 vSemaphoreCreateBinary(step_signal);
145 xSemaphoreTake(step_signal, 0);
147 /* Create tasks to step model and start scheduler */
148 xTaskCreate(control_task, (const signed char*)"control_task",
149 configMINIMAL_STACK_SIZE, NULL, CONTROL_PRIORITY, NULL);
150 xTaskCreate(working_task, (const signed char*)"working_task",
151 %<rppModelTaskStack>, NULL, WORKING_PRIORITY, NULL);
152 vTaskStartScheduler();
154 /* We should never get here */
155 %<LibCallModelTerminate()>\
157 rpp_sci_printf((const char*)
158 "ERROR: Problem allocating memory for scheduler to start.\r\n"
167 #if configUSE_MALLOC_FAILED_HOOK == 1
169 * FreeRTOS malloc() failed hook.
171 void vApplicationMallocFailedHook(void) {
173 rpp_sci_printf((const char*)
174 "ERROR: manual memory allocation failed.\r\n"
181 #if configCHECK_FOR_STACK_OVERFLOW > 0
183 * FreeRTOS stack overflow hook.
185 void vApplicationStackOverflowHook(xTaskHandle xTask,
186 signed portCHAR *pcTaskName) {
188 rpp_sci_printf((const char*)
189 "ERROR: Stack overflow : \"%s\".\r\n", pcTaskName
196 %<LibSetSourceFileSection(cFile, "Functions", tmpBuf)>