]> rtime.felk.cvut.cz Git - jenkicar/rpp-simulink.git/blob - rpp/rpp/rpp_srmain.tlc
Revert "Both created task have configurable stack size"
[jenkicar/rpp-simulink.git] / rpp / rpp / rpp_srmain.tlc
1 %% Copyright (C) 2013 Czech Technical University in Prague
2 %%
3 %% Authors:
4 %%     - Carlos Jenkins <carlos@jenkins.co.cr>
5 %%
6 %% This program is free software; you can redistribute it and/or modify
7 %% it under the terms of the GNU General Public License as published by
8 %% the Free Software Foundation; either version 2 of the License, or
9 %% (at your option) any later version.
10 %%
11 %% This program is distributed in the hope that it will be useful,
12 %% but WITHOUT ANY WARRANTY; without even the implied warranty of
13 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 %% GNU General Public License for more details.
15 %%
16 %% You should have received a copy of the GNU General Public License
17 %% along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 %%
19 %% File : rpp_srmain.tlc
20 %% Abstract:
21 %%     Custom TLC file to generate an RPP "main" file.
22 %%
23 %%     This file generates the "main" file for the RPP target on top of the RPP
24 %%     library and the FreeRTOS operating system.
25 %%
26 %% References:
27 %%     Example in <matlabroot>/rtw/c/tlc/mw/bareboard_srmain.tlc
28
29
30 %selectfile NULL_FILE
31
32 %function FcnSingleTaskingMain() void
33
34     %if GenerateSampleERTMain
35         %assign CompiledModel.GenerateSampleERTMain = TLC_FALSE
36     %endif
37
38
39     %assign cFile = LibCreateSourceFile("Source", "Custom", "ert_main")
40
41
42     %%%%%%%%
43     %openfile tmpBuf
44
45     /* RPP includes */
46     #include "rpp/rpp.h"
47
48     /* Model includes */
49     #include "%<LibGetMdlPubHdrBaseName()>.h"
50
51     %closefile tmpBuf
52     %<LibSetSourceFileSection(cFile, "Includes", tmpBuf)>
53     %%%%%%%%
54
55
56     %%%%%%%%
57     %openfile tmpBuf
58
59     /* Definitions */
60     #define STEP_SIZE_MILLIS %<CompiledModel.FundamentalStepSize>*1000.0
61     #define CONTROL_PRIORITY 2
62     #define WORKING_PRIORITY 1
63
64     %closefile tmpBuf
65     %<LibSetSourceFileSection(cFile, "Defines", tmpBuf)>
66     %%%%%%%%
67
68
69     %%%%%%%%
70     %openfile tmpBuf
71
72     static boolean_t WORKING = FALSE;
73     static boolean_t SHUTDOWN = FALSE;
74     static xSemaphoreHandle step_signal = NULL;
75     static uint32_t steps_control = 0;
76     static uint32_t steps_working = 0;
77
78     %closefile tmpBuf
79     %<LibSetSourceFileSection(cFile, "Declarations", tmpBuf)>
80     %%%%%%%%
81
82
83     %%%%%%%%
84     %openfile tmpBuf
85
86     void working_task(void* p);
87
88     /**
89      * Model step control and overrun detection task.
90      */
91     void control_task(void* p)
92     {
93
94         static const portTickType freq_ticks = STEP_SIZE_MILLIS / portTICK_RATE_MS;
95         portTickType last_wake_time = xTaskGetTickCount();
96
97         %if rppPrintMeta
98         %assign model_info = SPRINTF("'%s' - %s (TLC %s)\\r\\n", LibGetMdlPubHdrBaseName(), TLC_TIME, TLC_VERSION)
99         rpp_sci_printf((const char*)
100                 "%<model_info>"
101             );
102         %endif
103
104         /* Initialize model */
105         %<LibCallModelInitialize()>\
106
107         if(xTaskCreate(working_task, (const signed char*)"working_task",
108             %<rppModelTaskStack>, NULL, WORKING_PRIORITY, NULL) != pdPASS) {
109             #ifdef DEBUG
110             rpp_sci_printf((const char*)
111                 "ERROR: Cannot spawn model task.\r\n"
112             );
113             #endif
114             while(TRUE) {
115                 asm(" nop");
116             }
117         }
118
119         while(!SHUTDOWN) {
120
121             /* Wait until next step */
122             vTaskDelayUntil(&last_wake_time, freq_ticks);
123
124             if(WORKING) {
125                 /* Overrun detected */
126                 %<LibSetRTModelErrorStatus("\"Overrun\"")>;
127                 /* FIXME: Call overrun routine or set some overrun flag. */
128             } else {
129                 /* Release semaphore */
130                 xSemaphoreGive(step_signal);
131             }
132             steps_control++;
133
134         }
135
136         /* In case of shutdown, delete this task */
137         vTaskDelete(NULL);
138     }
139
140     /**
141      * Model step logic execution task.
142      */
143     void working_task(void* p)
144     {
145         while(!SHUTDOWN) {
146
147             /* Lock semaphore */
148             if(xSemaphoreTake(step_signal, portMAX_DELAY)) {
149                 WORKING = TRUE;
150                 %<LibCallModelStep(0)>\
151                 steps_working++;
152                 WORKING = FALSE;
153             }
154         }
155
156         /* In case of shutdown, delete this task */
157         vTaskDelete(NULL);
158     }
159
160
161     /**
162      * Hardware initialization, task spawning and OS scheduler start.
163      */
164     void main(void)
165     {
166         /* Initialize RPP board */
167         rpp_init();
168
169         // Speed up the SCI
170         rpp_sci_setup(115200);
171
172         /* Create and lock semaphore */
173         vSemaphoreCreateBinary(step_signal);
174         xSemaphoreTake(step_signal, 0);
175
176         /* Create tasks to step model and model task */
177         if(xTaskCreate(control_task, (const signed char*)"control_task",
178             configMINIMAL_STACK_SIZE, NULL, CONTROL_PRIORITY, NULL) != pdPASS) {
179             #ifdef DEBUG
180             rpp_sci_printf((const char*)
181                 "ERROR: Cannot spawn control task.\r\n"
182             );
183             #endif
184             while(TRUE) {
185                 asm("nop");
186             }
187         }
188
189         /* Start FreeRTOS Scheduler */
190         vTaskStartScheduler();
191
192         /* We should never get here */
193         %<LibCallModelTerminate()>\
194         #ifdef DEBUG
195         rpp_sci_printf((const char*)
196                 "ERROR: Problem allocating memory for idle task.\r\n"
197             );
198         #endif
199         while(TRUE) {
200             asm(" nop");
201         }
202     }
203
204
205     #if configUSE_MALLOC_FAILED_HOOK == 1
206     /**
207      * FreeRTOS malloc() failed hook.
208      */
209     void vApplicationMallocFailedHook(void) {
210         #ifdef DEBUG
211         rpp_sci_printf((const char*)
212                 "ERROR: manual memory allocation failed.\r\n"
213             );
214         #endif
215     }
216     #endif
217
218
219     #if configCHECK_FOR_STACK_OVERFLOW > 0
220     /**
221      * FreeRTOS stack overflow hook.
222      */
223     void vApplicationStackOverflowHook(xTaskHandle xTask,
224                                        signed portCHAR *pcTaskName) {
225         #ifdef DEBUG
226         rpp_sci_printf((const char*)
227                 "ERROR: Stack overflow : \"%s\".\r\n", pcTaskName
228             );
229         #endif
230     }
231     #endif
232
233     %closefile tmpBuf
234     %<LibSetSourceFileSection(cFile, "Functions", tmpBuf)>
235     %%%%%%%%
236
237 %endfunction