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")
42 #include "%<LibGetMdlPubHdrBaseName()>.h"
45 /* External mode header file */
51 %<LibSetSourceFileSection(cFile, "Includes", tmpBuf)>
59 #define STEP_SIZE_MILLIS %<CompiledModel.FundamentalStepSize>*1000.0
60 #define CONTROL_PRIORITY 3
61 #define WORKING_PRIORITY 2
62 #define EXTMODE_PRIORITY 1
65 %<LibSetSourceFileSection(cFile, "Defines", tmpBuf)>
72 static boolean_t working = FALSE;
73 static xSemaphoreHandle step_signal = NULL;
74 static xSemaphoreHandle initialized_signal = NULL;
76 static xSemaphoreHandle ext_mode_ready = NULL;
78 static uint32_t steps_control = 0;
79 static uint32_t steps_working = 0;
80 boolean_t overrun_flag = FALSE;
83 %<LibSetSourceFileSection(cFile, "Declarations", tmpBuf)>
91 * Model step control and overrun detection task.
93 void control_task(void* p)
96 xSemaphoreTake(initialized_signal, portMAX_DELAY);
98 xSemaphoreTake(ext_mode_ready, portMAX_DELAY);
101 static const portTickType freq_ticks = STEP_SIZE_MILLIS / portTICK_RATE_MS;
102 portTickType last_wake_time = xTaskGetTickCount();
104 while (rtmGetErrorStatus(%<modelName>_M) == NULL && !rtmGetStopRequested(%<modelName>_M)) {
106 /* Wait until next step */
107 vTaskDelayUntil(&last_wake_time, freq_ticks);
110 /* Overrun detected */
113 overrun_flag = FALSE;
114 /* Release semaphore */
115 xSemaphoreGive(step_signal);
120 rtExtModeCheckEndTrigger();
125 /* In case of shutdown, delete this task */
130 * Model step logic execution task.
132 void working_task(void* p)
134 /* Initialize model */
135 %<LibCallModelInitialize()>\
137 xSemaphoreGive(initialized_signal);
142 if (xSemaphoreTake(step_signal, portMAX_DELAY)) {
144 %<LibCallModelStep(0)>\
150 /* In case of shutdown, delete this task */
155 void ext_mode_comm_task(void* p)
157 drv_sci_set_crlf_conv_en(FALSE); /* Disable CR->CRLF conversion */
159 %<SLibGenERTExtModeInit()>
160 xSemaphoreGive(ext_mode_ready);
162 while (rtmGetErrorStatus(%<modelName>_M) == NULL && !rtmGetStopRequested(%<modelName>_M)) {
163 rtExtModeOneStep(rtmGetRTWExtModeInfo(%<modelName>_M), %<numSampleTimes>, (boolean_T *)&rtmGetStopRequested(%<modelName>_M));
165 rtExtModeShutdown(%<numSampleTimes>);
166 /* In case of shutdown, delete this task */
172 * Hardware initialization, task spawning and OS scheduler start.
176 /* Initialize RPP board */
180 rpp_sci_setup(115200);
183 %assign model_info = SPRINTF("'%s' - %s (TLC %s)\\r\\n", LibGetMdlPubHdrBaseName(), TLC_TIME, TLC_VERSION)
184 rpp_sci_printk("%<model_info>");
187 /* Create and lock semaphore */
188 vSemaphoreCreateBinary(step_signal);
189 xSemaphoreTake(step_signal, 0);
190 vSemaphoreCreateBinary(initialized_signal);
191 xSemaphoreTake(initialized_signal, 0);
193 vSemaphoreCreateBinary(ext_mode_ready);
194 xSemaphoreTake(ext_mode_ready, 0);
197 /* Create tasks to step model and model task */
198 if (xTaskCreate(control_task, "control_task",
199 configMINIMAL_STACK_SIZE, NULL, CONTROL_PRIORITY, NULL) != pdPASS) {
200 rpp_sci_printk("ERROR: Cannot spawn control task.\r\n");
203 if (xTaskCreate(working_task, "working_task",
204 %<rppModelTaskStack>, NULL, WORKING_PRIORITY, NULL) != pdPASS) {
205 rpp_sci_printk("ERROR: Cannot spawn model task.\r\n"
211 rtParseArgsForExtMode(0, NULL);
213 if (xTaskCreate(ext_mode_comm_task, "ext_mode_comm_task", 1024, NULL, EXTMODE_PRIORITY, NULL) != pdPASS) {
214 rpp_sci_printk("ERROR: Cannot spawn model task.\r\n");
219 /* Start FreeRTOS Scheduler */
220 vTaskStartScheduler();
222 /* We should never get here */
223 %<LibCallModelTerminate()>
224 rpp_sci_printk("ERROR: Problem allocating memory for idle task.\r\n");
228 #if configUSE_MALLOC_FAILED_HOOK == 1
230 * FreeRTOS malloc() failed hook.
232 void vApplicationMallocFailedHook(void) {
233 rpp_sci_printk("ERROR: Memory allocation failed.\r\n");
238 #if configCHECK_FOR_STACK_OVERFLOW > 0
240 * FreeRTOS stack overflow hook.
242 void vApplicationStackOverflowHook(xTaskHandle xTask, signed portCHAR *pcTaskName)
244 rpp_sci_printk("ERROR: Stack overflow : \"%s\".\r\n", pcTaskName);
249 %<LibSetSourceFileSection(cFile, "Functions", tmpBuf)>