]> rtime.felk.cvut.cz Git - jenkicar/rpp-simulink.git/blob - rpp/rpp/rpp_srmain.tlc
Fixed some make bugs. Looking good :D
[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 file processing to generate a "main" file.
22 %%       Reference: ???
23
24
25 %selectfile NULL_FILE
26
27 %function FcnSingleTaskingMain() void
28
29     %if GenerateSampleERTMain
30         %assign CompiledModel.GenerateSampleERTMain = TLC_FALSE
31     %endif
32
33
34     %assign cFile = LibCreateSourceFile("Source", "Custom", "ert_main")
35
36
37     %%%%%%%%
38     %openfile tmpBuf
39
40     /* Standard includes */
41     #include <stdbool.h>
42     #include <math.h>
43
44     /* Kernel includes */
45     #include "FreeRTOS.h"
46     #include "task.h"
47     #include "semphr.h"
48
49     /* System includes */
50     #include "sys_common.h"
51     #include "system.h"
52
53     /* Library includes */
54     // FIXME: Implement conditional #include's
55     %% IF GIO (General Input/Outputs)
56     %% IF HOUT (PWM outputs)
57     %% IF MOUT (Motor outputs)
58     %% IF ADC (Analog to Digital Converter)
59     %% IF SCI (Serial Communication Interface)
60     %% IF CAN (CAN Bus)
61     %% IF SDRAM
62
63     /* Model includes */
64     #include "%<LibGetMdlPubHdrBaseName()>.h"
65
66     %closefile tmpBuf
67     %<LibSetSourceFileSection(cFile, "Includes", tmpBuf)>
68     %%%%%%%%
69
70
71     %%%%%%%%
72     %openfile tmpBuf
73
74     #define STEP_SIZE_MILLIS (%<CompiledModel.FundamentalStepSize>*1000.0)
75     #define CONTROL_PRIORITY 2
76     #define WORKING_PRIORITY 1
77
78     %closefile tmpBuf
79     %<LibSetSourceFileSection(cFile, "Defines", tmpBuf)>
80     %%%%%%%%
81
82
83     %%%%%%%%
84     %openfile tmpBuf
85
86     bool WORKING = false;
87     bool SHUTDOWN = false;
88     xSemaphoreHandle step_signal = NULL;
89
90     %closefile tmpBuf
91     %<LibSetSourceFileSection(cFile, "Declarations", tmpBuf)>
92     %%%%%%%%
93
94
95     %%%%%%%%
96     %openfile tmpBuf
97
98     /**
99      * Model step control and overrun detection task.
100      */
101     void control_task(void *p) {
102
103         const portTickType freq_ticks = STEP_SIZE_MILLIS / portTICK_RATE_MS;
104         portTickType last_wake_time = xTaskGetTickCount();
105
106         while(!SHUTDOWN) {
107
108             // Wait until next step
109             vTaskDelayUntil(&last_wake_time, freq_ticks);
110
111             if(WORKING) {
112                 // Overrun detected
113                 // FIXME: Call overrun routine
114             } else {
115                 // Release semaphore
116                 xSemaphoreGive(step_signal);
117             }
118
119         }
120
121         /* In case of shutdown, delete this task */
122         vTaskDelete(NULL);
123     }
124
125     /**
126      * Model step logic execution task.
127      */
128     void working_task(void *p) {
129
130         while(!SHUTDOWN) {
131
132             // Lock semaphore
133             if(xSemaphoreTake(step_signal, portMAX_DELAY)) {
134                 WORKING = true;
135                 %<LibCallModelStep(0)>\
136                 WORKING = false;
137             }
138         }
139
140         /* In case of shutdown, delete this task */
141         vTaskDelete(NULL);
142     }
143
144
145     /**
146      * Hardware initialization, task spawning and OS scheduler start.
147      */
148     void main(void)
149     {
150
151         /* Initialize peripherals */
152         // FIXME: Implement conditional init()'s
153         _enable_IRQ();
154
155         /* Initialize model */
156         %<LibCallModelInitialize()>\
157
158         /* Create and lock semaphore */
159         vSemaphoreCreateBinary(step_signal);
160         xSemaphoreTake(step_signal, 0);
161
162         /* Create tasks to step model and start scheduler */
163         // FIXME: How to calculate / policy about model stack size
164         xTaskCreate(control_task, (signed char*) "control_task", 128,  NULL,
165             CONTROL_PRIORITY, NULL);
166         xTaskCreate(working_task, (signed char*) "working_task", 4096, NULL,
167             WORKING_PRIORITY, NULL);
168         vTaskStartScheduler();
169
170         // We should never get here
171         %<LibCallModelTerminate()>\
172         while(true) {
173         }
174     }
175
176     %closefile tmpBuf
177     %<LibSetSourceFileSection(cFile, "Functions", tmpBuf)>
178     %%%%%%%%
179
180 %endfunction