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 or set some overrun flag. */
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 model task */
160 if(xTaskCreate(control_task, (const signed char*)"control_task",
161 configMINIMAL_STACK_SIZE, NULL, CONTROL_PRIORITY, NULL) != pdPASS) {
163 rpp_sci_printf((const char*)
164 "ERROR: Cannot spawn control task.\r\n"
171 if(xTaskCreate(working_task, (const signed char*)"working_task",
172 %<rppModelTaskStack>, NULL, WORKING_PRIORITY, NULL) != pdPASS) {
174 rpp_sci_printf((const char*)
175 "ERROR: Cannot spawn model task.\r\n"
183 /* Start FreeRTOS Scheduler */
184 vTaskStartScheduler();
186 /* We should never get here */
187 %<LibCallModelTerminate()>\
189 rpp_sci_printf((const char*)
190 "ERROR: Problem allocating memory for idle task.\r\n"
199 #if configUSE_MALLOC_FAILED_HOOK == 1
201 * FreeRTOS malloc() failed hook.
203 void vApplicationMallocFailedHook(void) {
205 rpp_sci_printf((const char*)
206 "ERROR: manual memory allocation failed.\r\n"
213 #if configCHECK_FOR_STACK_OVERFLOW > 0
215 * FreeRTOS stack overflow hook.
217 void vApplicationStackOverflowHook(xTaskHandle xTask,
218 signed portCHAR *pcTaskName) {
220 rpp_sci_printf((const char*)
221 "ERROR: Stack overflow : \"%s\".\r\n", pcTaskName
228 %<LibSetSourceFileSection(cFile, "Functions", tmpBuf)>