1 %% Copyright (C) 2013-2015 Czech Technical University in Prague
4 %% - Carlos Jenkins <carlos@jenkins.co.cr>
5 %% - Michal Sojka <sojkam1@fel.cvut.cz>
6 %% - Michal Horn <hornmich@fel.cvut.cz>
8 %% This document contains proprietary information belonging to Czech
9 %% Technical University in Prague. Passing on and copying of this
10 %% document, and communication of its contents is not permitted
11 %% without prior written authorization.
13 %% File : rpp_mrmain.tlc
15 %% Custom TLC file to generate an RPP "main" file.
17 %% This file generates the "main" file for the RPP target on top of the RPP
18 %% library and the FreeRTOS operating system.
20 %% The mr prefix is standard to mark Multi Tasking main, which is the case. See
21 %% rpp_file_process.m description above for more information about this.
24 %% Example in <matlabroot>/rtw/c/tlc/mw/bareboard_mrmain.tlc
29 %function FcnMultiTaskingMain() void
31 %if GenerateSampleERTMain
32 %assign CompiledModel.GenerateSampleERTMain = TLC_FALSE
36 %assign cFile = LibCreateSourceFile("Source", "Custom", "ert_main")
43 #include "%<LibGetMdlPubHdrBaseName()>.h"
45 /* RPP runtime includes */
46 #include "rpp_simulink_runtime.h"
49 /* External mode header file */
55 %<LibSetSourceFileSection(cFile, "Includes", tmpBuf)>
63 #define STEP_SIZE_MILLIS (%<CompiledModel.FundamentalStepSize>*1000.0)
64 #define NUM_SAMPLE_TIMES %<numSampleTimes>
65 #define CONTROL_PRIORITY (configMAX_PRIORITIES - 1)
67 %% Worker task priority assignment - rate transition block
68 %% requires rate monotonic priority assignment
69 %foreach i = numSampleTimes - tid01Eq
70 #define WORKING%<i + tid01Eq>_PRIORITY (configMAX_PRIORITIES - %<2 + i>)
74 #define EXTMODE_PRIORITY 1
76 #define IDLE_PRIORITY 0
77 #define EXTMODE_PRIORITY IDLE_PRIORITY /* Extmode not used */
80 %foreach i = numSampleTimes - tid01Eq
81 %assign j = i + tid01Eq
82 #if (WORKING%<j>_PRIORITY <= EXTMODE_PRIORITY) || (WORKING%<j>_PRIORITY >= CONTROL_PRIORITY)
83 #error Too many tasks. Increase configMAX_PRIORITIES.
88 %<LibSetSourceFileSection(cFile, "Defines", tmpBuf)>
94 typedef struct sub_rate_st {
100 static xSemaphoreHandle ext_mode_ready = NULL;
102 static uint32_t steps_control = 0;
103 static uint32_t steps_working[NUM_SAMPLE_TIMES];
104 boolean_t overrun_flag = FALSE;
105 static sub_rate_t sub_rate[NUM_SAMPLE_TIMES];
106 static boolean_t simulationFinished = 0;
109 %<LibSetSourceFileSection(cFile, "Declarations", tmpBuf)>
117 * Model step control and overrun detection task.
119 void control_task(void* p)
123 %% Variables used by LibCallSetEventForThisBaseStep (static => zero initialized)
124 static uint32_t taskCounter[NUM_SAMPLE_TIMES];
125 static boolean_t eventFlags[NUM_SAMPLE_TIMES];
126 static boolean_t OverrunFlags[NUM_SAMPLE_TIMES];
128 (void)OverrunFlags; /* Not used in multitasking mode, but required by TLC macro */
132 xSemaphoreTake(ext_mode_ready, portMAX_DELAY);
135 static const portTickType freq_ticks = STEP_SIZE_MILLIS / portTICK_RATE_MS;
136 portTickType last_wake_time = xTaskGetTickCount();
138 %% This condition has been added because of the warning:
139 %% "ert_main.c", line 46: warning #238-D: controlling expression is constant
140 %% caused by the expression (void*) 0 passed to the while statement. The rtmGetStopRequested
141 %% macro is defined to this constant if and only if the GenerateSampleERTMain is not set. So
142 %% The condition is not needed in this case as well.
144 %% See <MATLAB_ROOT>/rtw/c/tlc/mw/rtmspecmacs.tlc at line 198.
146 %if !GenerateSampleERTMain
147 while (rtmGetErrorStatus(%<modelName>_M) == NULL) {
149 while (rtmGetErrorStatus(%<modelName>_M) == NULL && !rtmGetStopRequested(%<modelName>_M)) {
151 /* Wait until next step */
152 vTaskDelayUntil(&last_wake_time, freq_ticks);
154 /* Check subrate overrun, set rates that need to run this time step*/
155 %<LibCallSetEventForThisBaseStep("eventFlags")>\
157 /* Trigger sub-rate threads */
158 %foreach j = numSampleTimes - tid01Eq
159 %assign i = j + tid01Eq
160 %assign s = CompiledModel.SampleTime[i].PeriodAndOffset[0]
161 %assign o = CompiledModel.SampleTime[i].PeriodAndOffset[1]
162 /* Sampling rate %<i>, sample time = %<s>, offset = %<o> */
165 for (i = %<tid01Eq>; i < NUM_SAMPLE_TIMES; i++) {
166 if (i == 0 || eventFlags[i]) {
167 eventFlags[i] = FALSE;
168 uint32_t step_sem_value = uxQueueMessagesWaiting(sub_rate[i].sem);
169 if (step_sem_value) {
170 rtmSetErrorStatus(%<modelName>_M, "Overrun");
174 if (xSemaphoreGive(sub_rate[i].sem) == pdFALSE) {
175 rpp_sci_printk("ERROR: Task %d semaphore 1 give.\n", i);
178 if (xSemaphoreGive(sub_rate[i].sem) == pdFALSE) {
179 rpp_sci_printk("ERROR: Task %d semaphore 2 give.\n", i);
188 rtExtModeCheckEndTrigger();
192 simulationFinished = 1;
195 for (i = %<tid01Eq>; i < NUM_SAMPLE_TIMES; i++) {
196 xSemaphoreGive(sub_rate[i].sem);
197 xSemaphoreGive(sub_rate[i].sem);
200 /* In case of shutdown, delete this task */
204 %foreach j = numSampleTimes - tid01Eq
205 %assign i = j + tid01Eq
207 * Model step logic execution task for sample time %<CompiledModel.SampleTime[i].ClockTickStepSize>.
209 void working_task%<i>(void *p)
211 steps_working[%<i>] = 0;
213 while(!simulationFinished) {
214 xSemaphoreTake(sub_rate[%<i>].sem, portMAX_DELAY); /* Sem_val = 1 */
215 %<LibCallModelStep(i)>\
216 steps_working[%<i>]++;
217 xSemaphoreTake(sub_rate[%<i>].sem, portMAX_DELAY); /* Sem_val = 0 */
220 %% /* In case of shutdown, delete this task */
226 void ext_mode_comm_task(void* p)
228 drv_sci_set_crlf_conv_en(FALSE); /* Disable CR->CRLF conversion */
230 %<SLibGenERTExtModeInit()>
231 xSemaphoreGive(ext_mode_ready);
233 while (rtmGetErrorStatus(%<modelName>_M) == NULL && !rtmGetStopRequested(%<modelName>_M)) {
234 rtExtModeOneStep(rtmGetRTWExtModeInfo(%<modelName>_M), %<numSampleTimes>, (boolean_T *)&rtmGetStopRequested(%<modelName>_M));
236 rtExtModeShutdown(%<numSampleTimes>);
237 /* In case of shutdown, delete this task */
243 * Hardware initialization, task spawning and OS scheduler start.
247 /* Initialize RPP board */
248 /* Initialize RPP board */
249 %if EXISTS(::rpp_ain_present)
252 %if EXISTS(::rpp_aout_present)
255 %if EXISTS(::rpp_din_present)
258 %if EXISTS(::rpp_hbr_present)
261 %if EXISTS(::rpp_irc_present)
264 %if EXISTS(::rpp_lout_present)
267 %if EXISTS(::rpp_mout_present)
270 %if EXISTS(::rpp_sdr_present)
273 %if EXISTS(::rpp_gio_in_present) || EXISTS(::rpp_gio_out_present)
274 rpp_gio_init(RPP_GIO_PORT_ALL);
279 rpp_sci_setup(115200);
281 /* Initialize model */
282 %<LibCallModelInitialize()>\
285 %assign model_info = SPRINTF("'%s' - %s (TLC %s)\\r\\n", LibGetMdlPubHdrBaseName(), TLC_TIME, TLC_VERSION)
286 rpp_sci_printk("%<model_info>");
290 vSemaphoreCreateBinary(ext_mode_ready);
291 xSemaphoreTake(ext_mode_ready, 0);
294 %foreach j = numSampleTimes - tid01Eq
295 %assign i = j + tid01Eq
296 %assign s = CompiledModel.SampleTime[i].PeriodAndOffset[0]
297 %assign o = CompiledModel.SampleTime[i].PeriodAndOffset[1]
299 /* Initializing the step semaphore of the loop %<i> */
300 sub_rate[%<i>].sem = xSemaphoreCreateCounting(2, 0);
301 if (sub_rate[%<i>].sem == NULL) {
302 rpp_sci_printk("ERROR: Can not create semaphore for task %<i>.\r\n");
306 /* Starting loop %<i> thread for sample time = %<s>s, offset = %<o>s. */
307 if (xTaskCreate(working_task%<i>,
309 %<rppModelTaskStack>,
311 WORKING%<i>_PRIORITY,
312 &sub_rate[%<i>].thread ) != pdPASS) {
313 rpp_sci_printk("ERROR: Cannot spawn working task %<i>.\r\n");
318 /* Create tasks to step model and model task */
320 if (xTaskCreate(control_task,
322 %<rppModelTaskStack>,
326 rpp_sci_printk("ERROR: Cannot spawn control task.\r\n");
332 rtParseArgsForExtMode(0, NULL);
334 if (xTaskCreate(ext_mode_comm_task, "ext_mode_comm_task", 1024, NULL, EXTMODE_PRIORITY, NULL) != pdPASS) {
335 rpp_sci_printk("ERROR: Cannot spawn model task.\r\n");
340 /* Start FreeRTOS Scheduler */
341 vTaskStartScheduler();
343 /* We should never get here */
344 %<LibCallModelTerminate()>\
345 rpp_sci_printk("ERROR: Problem allocating memory for idle task.\r\n");
350 #if configUSE_MALLOC_FAILED_HOOK == 1
352 * FreeRTOS malloc() failed hook.
354 void vApplicationMallocFailedHook(void) {
355 rpp_sci_printk("ERROR: Memory allocation failed.\r\n");
361 #if configCHECK_FOR_STACK_OVERFLOW > 0
363 * FreeRTOS stack overflow hook.
365 void vApplicationStackOverflowHook(xTaskHandle xTask, signed portCHAR *pcTaskName)
367 rpp_sci_printk("ERROR: Stack overflow : \"%s\".\r\n", pcTaskName);
373 %<LibSetSourceFileSection(cFile, "Functions", tmpBuf)>