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;
82 %<LibSetSourceFileSection(cFile, "Declarations", tmpBuf)>
90 * Model step control and overrun detection task.
92 void control_task(void* p)
95 xSemaphoreTake(initialized_signal, portMAX_DELAY);
97 xSemaphoreTake(ext_mode_ready, portMAX_DELAY);
100 static const portTickType freq_ticks = STEP_SIZE_MILLIS / portTICK_RATE_MS;
101 portTickType last_wake_time = xTaskGetTickCount();
103 while (rtmGetErrorStatus(%<modelName>_M) == NULL && !rtmGetStopRequested(%<modelName>_M)) {
105 /* Wait until next step */
106 vTaskDelayUntil(&last_wake_time, freq_ticks);
109 /* Overrun detected */
110 %<LibSetRTModelErrorStatus("\"Overrun\"")>;
111 /* FIXME: Call overrun routine or set some overrun flag. */
113 /* Release semaphore */
114 xSemaphoreGive(step_signal);
119 rtExtModeCheckEndTrigger();
124 /* In case of shutdown, delete this task */
129 * Model step logic execution task.
131 void working_task(void* p)
133 /* Initialize model */
134 %<LibCallModelInitialize()>\
136 xSemaphoreGive(initialized_signal);
141 if (xSemaphoreTake(step_signal, portMAX_DELAY)) {
143 %<LibCallModelStep(0)>\
149 /* In case of shutdown, delete this task */
154 void ext_mode_comm_task(void* p)
156 drv_sci_set_crlf_conv_en(FALSE); /* Disable CR->CRLF conversion */
158 %<SLibGenERTExtModeInit()>
159 xSemaphoreGive(ext_mode_ready);
161 while (rtmGetErrorStatus(%<modelName>_M) == NULL && !rtmGetStopRequested(%<modelName>_M)) {
162 rtExtModeOneStep(rtmGetRTWExtModeInfo(%<modelName>_M), %<numSampleTimes>, (boolean_T *)&rtmGetStopRequested(%<modelName>_M));
164 rtExtModeShutdown(%<numSampleTimes>);
165 /* In case of shutdown, delete this task */
171 * Hardware initialization, task spawning and OS scheduler start.
175 /* Initialize RPP board */
179 rpp_sci_setup(115200);
182 %assign model_info = SPRINTF("'%s' - %s (TLC %s)\\r\\n", LibGetMdlPubHdrBaseName(), TLC_TIME, TLC_VERSION)
183 rpp_sci_printk("%<model_info>");
186 /* Create and lock semaphore */
187 vSemaphoreCreateBinary(step_signal);
188 xSemaphoreTake(step_signal, 0);
189 vSemaphoreCreateBinary(initialized_signal);
190 xSemaphoreTake(initialized_signal, 0);
192 vSemaphoreCreateBinary(ext_mode_ready);
193 xSemaphoreTake(ext_mode_ready, 0);
196 /* Create tasks to step model and model task */
197 if (xTaskCreate(control_task, "control_task",
198 configMINIMAL_STACK_SIZE, NULL, CONTROL_PRIORITY, NULL) != pdPASS) {
199 rpp_sci_printk("ERROR: Cannot spawn control task.\r\n");
202 if (xTaskCreate(working_task, "working_task",
203 %<rppModelTaskStack>, NULL, WORKING_PRIORITY, NULL) != pdPASS) {
204 rpp_sci_printk("ERROR: Cannot spawn model task.\r\n"
210 rtParseArgsForExtMode(0, NULL);
212 if (xTaskCreate(ext_mode_comm_task, "ext_mode_comm_task", 1024, NULL, EXTMODE_PRIORITY, NULL) != pdPASS) {
213 rpp_sci_printk("ERROR: Cannot spawn model task.\r\n");
218 /* Start FreeRTOS Scheduler */
219 vTaskStartScheduler();
221 /* We should never get here */
222 %<LibCallModelTerminate()>
223 rpp_sci_printk("ERROR: Problem allocating memory for idle task.\r\n");
227 #if configUSE_MALLOC_FAILED_HOOK == 1
229 * FreeRTOS malloc() failed hook.
231 void vApplicationMallocFailedHook(void) {
232 rpp_sci_printk("ERROR: Memory allocation failed.\r\n");
237 #if configCHECK_FOR_STACK_OVERFLOW > 0
239 * FreeRTOS stack overflow hook.
241 void vApplicationStackOverflowHook(xTaskHandle xTask, signed portCHAR *pcTaskName)
243 rpp_sci_printk("ERROR: Stack overflow : \"%s\".\r\n", pcTaskName);
248 %<LibSetSourceFileSection(cFile, "Functions", tmpBuf)>