]> rtime.felk.cvut.cz Git - jenkicar/rpp-simulink.git/blob - rpp/rpp/rpp_srmain.tlc
Test and fixed LOUT Simulink compilation.
[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     boolean_t WORKING = FALSE;
73     boolean_t SHUTDOWN = FALSE;
74     xSemaphoreHandle step_signal = NULL;
75
76     %closefile tmpBuf
77     %<LibSetSourceFileSection(cFile, "Declarations", tmpBuf)>
78     %%%%%%%%
79
80
81     %%%%%%%%
82     %openfile tmpBuf
83
84     /**
85      * Model step control and overrun detection task.
86      */
87     void control_task(void *p) {
88
89         static const portTickType freq_ticks = STEP_SIZE_MILLIS / portTICK_RATE_MS;
90         portTickType last_wake_time = xTaskGetTickCount();
91
92         while(!SHUTDOWN) {
93
94             /* Wait until next step */
95             vTaskDelayUntil(&last_wake_time, freq_ticks);
96
97             if(WORKING) {
98                 /* Overrun detected */
99                 %<LibSetRTModelErrorStatus("\"Overrun\"")>;
100                 /* FIXME: Call overrun routine */
101             } else {
102                 /* Release semaphore */
103                 xSemaphoreGive(step_signal);
104             }
105
106         }
107
108         /* In case of shutdown, delete this task */
109         vTaskDelete(NULL);
110     }
111
112     /**
113      * Model step logic execution task.
114      */
115     void working_task(void *p) {
116
117         while(!SHUTDOWN) {
118
119             /* Lock semaphore */
120             if(xSemaphoreTake(step_signal, portMAX_DELAY)) {
121                 WORKING = TRUE;
122                 %<LibCallModelStep(0)>\
123                 WORKING = FALSE;
124             }
125         }
126
127         /* In case of shutdown, delete this task */
128         vTaskDelete(NULL);
129     }
130
131
132     /**
133      * Hardware initialization, task spawning and OS scheduler start.
134      */
135     void main(void)
136     {
137         /* Initialize RPP board */
138         rpp_init();
139
140         /* Initialize model */
141         %<LibCallModelInitialize()>\
142
143         /* Create and lock semaphore */
144         vSemaphoreCreateBinary(step_signal);
145         xSemaphoreTake(step_signal, 0);
146
147         /* Create tasks to step model and start scheduler */
148         xTaskCreate(control_task, (const signed char*)"control_task",
149             configMINIMAL_STACK_SIZE, NULL, CONTROL_PRIORITY, NULL);
150         xTaskCreate(working_task, (const signed char*)"working_task",
151             %<rppModelTaskStack>, NULL, WORKING_PRIORITY, NULL);
152         vTaskStartScheduler();
153
154         /* We should never get here */
155         %<LibCallModelTerminate()>\
156         #ifdef DEBUG
157         rpp_sci_printf((const char*)
158                 "ERROR: Problem allocating memory for scheduler to start.\r\n"
159             );
160         #endif
161         while(TRUE) {
162             asm("nop");
163         }
164     }
165
166
167     #if configUSE_MALLOC_FAILED_HOOK == 1
168     /**
169      * FreeRTOS malloc() failed hook.
170      */
171     void vApplicationMallocFailedHook(void) {
172         #ifdef DEBUG
173         rpp_sci_printf((const char*)
174                 "ERROR: manual memory allocation failed.\r\n"
175             );
176         #endif
177     }
178     #endif
179
180
181     #if configCHECK_FOR_STACK_OVERFLOW > 0
182     /**
183      * FreeRTOS stack overflow hook.
184      */
185     void vApplicationStackOverflowHook(xTaskHandle xTask,
186                                        signed portCHAR *pcTaskName) {
187         #ifdef DEBUG
188         rpp_sci_printf((const char*)
189                 "ERROR: Stack overflow : \"%s\".\r\n", pcTaskName
190             );
191         #endif
192     }
193     #endif
194
195     %closefile tmpBuf
196     %<LibSetSourceFileSection(cFile, "Functions", tmpBuf)>
197     %%%%%%%%
198
199 %endfunction