1 %% Copyright (C) 2013-2014 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.
20 %% Example in <matlabroot>/rtw/c/tlc/mw/bareboard_srmain.tlc
25 %function FcnSingleTaskingMain() void
27 %if GenerateSampleERTMain
28 %assign CompiledModel.GenerateSampleERTMain = TLC_FALSE
32 %assign cFile = LibCreateSourceFile("Source", "Custom", "ert_main")
41 /* RPP runtime includes */
42 #include "rpp_simulink_runtime.h"
45 #include "%<LibGetMdlPubHdrBaseName()>.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 while (rtmGetErrorStatus(%<modelName>_M) == NULL && !rtmGetStopRequested(%<modelName>_M)) {
109 /* Wait until next step */
110 vTaskDelayUntil(&last_wake_time, freq_ticks);
113 /* Overrun detected */
116 overrun_flag = FALSE;
117 /* Release semaphore */
118 xSemaphoreGive(step_signal);
123 rtExtModeCheckEndTrigger();
128 /* In case of shutdown, delete this task */
133 * Model step logic execution task.
135 void working_task(void* p)
137 /* Initialize model */
138 %<LibCallModelInitialize()>\
140 xSemaphoreGive(initialized_signal);
145 if (xSemaphoreTake(step_signal, portMAX_DELAY)) {
147 %<LibCallModelStep(0)>\
153 /* In case of shutdown, delete this task */
158 void ext_mode_comm_task(void* p)
160 drv_sci_set_crlf_conv_en(FALSE); /* Disable CR->CRLF conversion */
162 %<SLibGenERTExtModeInit()>
163 xSemaphoreGive(ext_mode_ready);
165 while (rtmGetErrorStatus(%<modelName>_M) == NULL && !rtmGetStopRequested(%<modelName>_M)) {
166 rtExtModeOneStep(rtmGetRTWExtModeInfo(%<modelName>_M), %<numSampleTimes>, (boolean_T *)&rtmGetStopRequested(%<modelName>_M));
168 rtExtModeShutdown(%<numSampleTimes>);
169 /* In case of shutdown, delete this task */
175 * Hardware initialization, task spawning and OS scheduler start.
179 /* Initialize RPP board */
183 rpp_sci_setup(115200);
186 %assign model_info = SPRINTF("'%s' - %s (TLC %s)\\r\\n", LibGetMdlPubHdrBaseName(), TLC_TIME, TLC_VERSION)
187 rpp_sci_printk("%<model_info>");
190 /* Create and lock semaphore */
191 vSemaphoreCreateBinary(step_signal);
192 xSemaphoreTake(step_signal, 0);
193 vSemaphoreCreateBinary(initialized_signal);
194 xSemaphoreTake(initialized_signal, 0);
196 vSemaphoreCreateBinary(ext_mode_ready);
197 xSemaphoreTake(ext_mode_ready, 0);
200 /* Create tasks to step model and model task */
201 if (xTaskCreate(control_task, "control_task",
202 configMINIMAL_STACK_SIZE, NULL, CONTROL_PRIORITY, NULL) != pdPASS) {
203 rpp_sci_printk("ERROR: Cannot spawn control task.\r\n");
206 if (xTaskCreate(working_task, "working_task",
207 %<rppModelTaskStack>, NULL, WORKING_PRIORITY, NULL) != pdPASS) {
208 rpp_sci_printk("ERROR: Cannot spawn model task.\r\n"
214 rtParseArgsForExtMode(0, NULL);
216 if (xTaskCreate(ext_mode_comm_task, "ext_mode_comm_task", 1024, NULL, EXTMODE_PRIORITY, NULL) != pdPASS) {
217 rpp_sci_printk("ERROR: Cannot spawn model task.\r\n");
222 /* Start FreeRTOS Scheduler */
223 vTaskStartScheduler();
225 /* We should never get here */
226 %<LibCallModelTerminate()>
227 rpp_sci_printk("ERROR: Problem allocating memory for idle task.\r\n");
231 #if configUSE_MALLOC_FAILED_HOOK == 1
233 * FreeRTOS malloc() failed hook.
235 void vApplicationMallocFailedHook(void) {
236 rpp_sci_printk("ERROR: Memory allocation failed.\r\n");
241 #if configCHECK_FOR_STACK_OVERFLOW > 0
243 * FreeRTOS stack overflow hook.
245 void vApplicationStackOverflowHook(xTaskHandle xTask, signed portCHAR *pcTaskName)
247 rpp_sci_printk("ERROR: Stack overflow : \"%s\".\r\n", pcTaskName);
252 %<LibSetSourceFileSection(cFile, "Functions", tmpBuf)>