1 %% Copyright (C) 2013-2015 Czech Technical University in Prague
4 %% - Carlos Jenkins <carlos@jenkins.co.cr>
5 %% - Michal Sojka <sojkam1@fel.cvut.cz>
7 %% This document contains proprietary information belonging to Czech
8 %% Technical University in Prague. Passing on and copying of this
9 %% document, and communication of its contents is not permitted
10 %% without prior written authorization.
12 %% File : rpp_srmain.tlc
14 %% Custom TLC file to generate an RPP "main" file.
16 %% This file generates the "main" file for the RPP target on top of the RPP
17 %% library and the FreeRTOS operating system.
19 %% The sr prefix is standard to mark Single Tasking main, which is the case. See
20 %% rpp_file_process.m description above for more information about this.
23 %% Example in <matlabroot>/rtw/c/tlc/mw/bareboard_srmain.tlc
28 %function FcnSingleTaskingMain() void
30 %if GenerateSampleERTMain
31 %assign CompiledModel.GenerateSampleERTMain = TLC_FALSE
35 %assign cFile = LibCreateSourceFile("Source", "Custom", "ert_main")
42 #include "%<LibGetMdlPubHdrBaseName()>.h"
44 /* RPP runtime includes */
45 #include "rpp_simulink_runtime.h"
48 /* External mode header file */
54 %<LibSetSourceFileSection(cFile, "Includes", tmpBuf)>
62 #define STEP_SIZE_MILLIS %<CompiledModel.FundamentalStepSize>*1000.0
63 #define CONTROL_PRIORITY 3
64 #define WORKING_PRIORITY 2
65 #define EXTMODE_PRIORITY 1
68 %<LibSetSourceFileSection(cFile, "Defines", tmpBuf)>
75 static boolean_t working = FALSE;
76 static xSemaphoreHandle step_signal = NULL;
77 static xSemaphoreHandle initialized_signal = NULL;
79 static xSemaphoreHandle ext_mode_ready = NULL;
81 static uint32_t steps_control = 0;
82 static uint32_t steps_working = 0;
83 boolean_t overrun_flag = FALSE;
86 %<LibSetSourceFileSection(cFile, "Declarations", tmpBuf)>
94 * Model step control and overrun detection task.
96 void control_task(void* p)
99 xSemaphoreTake(initialized_signal, portMAX_DELAY);
101 xSemaphoreTake(ext_mode_ready, portMAX_DELAY);
104 static const portTickType freq_ticks = STEP_SIZE_MILLIS / portTICK_RATE_MS;
105 portTickType last_wake_time = xTaskGetTickCount();
107 %% This condition has been added because of the warning:
108 %% "ert_main.c", line 46: warning #238-D: controlling expression is constant
109 %% caused by the expression (void*) 0 passed to the while statement. The rtmGetStopRequested
110 %% macro is defined to this constant if and only if the GenerateSampleERTMain is not set. So
111 %% The condition is not needed in this case as well.
113 %% See <MATLAB_ROOT>/rtw/c/tlc/mw/rtmspecmacs.tlc at line 198.
115 %if !GenerateSampleERTMain
116 while (rtmGetErrorStatus(%<modelName>_M) == NULL) {
118 while (rtmGetErrorStatus(%<modelName>_M) == NULL && !rtmGetStopRequested(%<modelName>_M)) {
120 /* Wait until next step */
121 vTaskDelayUntil(&last_wake_time, freq_ticks);
124 /* Overrun detected */
127 overrun_flag = FALSE;
128 /* Release semaphore */
129 xSemaphoreGive(step_signal);
134 rtExtModeCheckEndTrigger();
139 /* In case of shutdown, delete this task */
144 * Model step logic execution task.
146 void working_task(void* p)
148 static int_T taskCounter[%<FcnNumST()>] = %<FcnInitializeTaskCounter()>;
150 /* Initialize model */
151 %<LibCallModelInitialize()>\
152 /* Step the model for any subrate */
153 xSemaphoreGive(initialized_signal);
156 if (xSemaphoreTake(step_signal, portMAX_DELAY)) {
159 %<LibCallModelStep(0)>\
161 %if LibNumSynchronousSampleTimes() > 1
162 %foreach idx = LibGetNumSyncPeriodicTasks() - 1
163 %assign tid = 1 + idx + tid01Eq
164 /* Task %<tid> handling */
165 if (taskCounter[%<tid>] == 0) {
166 %<LibCallModelStep(tid)>\
168 if (++taskCounter[%<tid>] == %< FcnComputeTaskTickLimit(tid)>)
169 taskCounter[%<tid>]=0;
177 %% /* In case of shutdown, delete this task */
178 %% vTaskDelete(NULL);
182 void ext_mode_comm_task(void* p)
184 drv_sci_set_crlf_conv_en(FALSE); /* Disable CR->CRLF conversion */
186 %<SLibGenERTExtModeInit()>
187 xSemaphoreGive(ext_mode_ready);
189 while (rtmGetErrorStatus(%<modelName>_M) == NULL && !rtmGetStopRequested(%<modelName>_M)) {
190 rtExtModeOneStep(rtmGetRTWExtModeInfo(%<modelName>_M), %<numSampleTimes>, (boolean_T *)&rtmGetStopRequested(%<modelName>_M));
192 rtExtModeShutdown(%<numSampleTimes>);
193 /* In case of shutdown, delete this task */
199 * Hardware initialization, task spawning and OS scheduler start.
203 /* Initialize RPP board */
204 %if EXISTS(::rpp_ain_present)
207 %if EXISTS(::rpp_din_present) || EXISTS(::rpp_dout_present)
208 rpp_gio_init(RPP_GIO_PORT_ALL);
213 rpp_sci_setup(115200);
216 %assign model_info = SPRINTF("'%s' - %s (TLC %s)\\r\\n", LibGetMdlPubHdrBaseName(), TLC_TIME, TLC_VERSION)
217 rpp_sci_printk("%<model_info>");
220 /* Create and lock semaphore */
221 vSemaphoreCreateBinary(step_signal);
222 xSemaphoreTake(step_signal, 0);
223 vSemaphoreCreateBinary(initialized_signal);
224 xSemaphoreTake(initialized_signal, 0);
226 vSemaphoreCreateBinary(ext_mode_ready);
227 xSemaphoreTake(ext_mode_ready, 0);
230 /* Create tasks to step model and model task */
231 if (xTaskCreate(control_task, "control_task",
232 configMINIMAL_STACK_SIZE, NULL, CONTROL_PRIORITY, NULL) != pdPASS) {
233 rpp_sci_printk("ERROR: Cannot spawn control task.\r\n");
236 if (xTaskCreate(working_task, "working_task",
237 %<rppModelTaskStack>, NULL, WORKING_PRIORITY, NULL) != pdPASS) {
238 rpp_sci_printk("ERROR: Cannot spawn model task.\r\n"
244 rtParseArgsForExtMode(0, NULL);
246 if (xTaskCreate(ext_mode_comm_task, "ext_mode_comm_task", 1024, NULL, EXTMODE_PRIORITY, NULL) != pdPASS) {
247 rpp_sci_printk("ERROR: Cannot spawn model task.\r\n");
252 /* Start FreeRTOS Scheduler */
253 vTaskStartScheduler();
255 /* We should never get here */
256 %<LibCallModelTerminate()>
257 rpp_sci_printk("ERROR: Problem allocating memory for idle task.\r\n");
261 #if configUSE_MALLOC_FAILED_HOOK == 1
263 * FreeRTOS malloc() failed hook.
265 void vApplicationMallocFailedHook(void) {
266 rpp_sci_printk("ERROR: Memory allocation failed.\r\n");
271 #if configCHECK_FOR_STACK_OVERFLOW > 0
273 * FreeRTOS stack overflow hook.
275 void vApplicationStackOverflowHook(xTaskHandle xTask, signed portCHAR *pcTaskName)
277 rpp_sci_printk("ERROR: Stack overflow : \"%s\".\r\n", pcTaskName);
282 %<LibSetSourceFileSection(cFile, "Functions", tmpBuf)>