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