1/*
2 * Academic License - for use in teaching, academic research, and meeting
3 * course requirements at degree granting institutions only. Not for
4 * government, commercial, or other organizational use.
5 *
6 * File: ert_main.c
7 *
8 * Code generated for Simulink model 'magneto_tarato'.
9 *
10 * Model version : 1.19
11 * Simulink Coder version : 8.12 (R2017a) 16-Feb-2017
12 * C/C++ source code generated on : Tue Nov 07 14:03:19 2017
13 *
14 * Target selection: ert.tlc
15 * Embedded hardware selection: ARM Compatible->ARM Cortex
16 * Code generation objectives: Unspecified
17 * Validation result: Not run
18 */
19
20#include "magneto_tarato.h"
21#include "magneto_tarato_private.h"
22#include "rtwtypes.h"
23#include "limits.h"
24#include "rt_nonfinite.h"
25#include "board.h"
26#include "mw_cmsis_rtos.h"
27#define UNUSED(x) x = x
28#define EXIT_ON_ERROR(msg, cond) if (cond) { return(0); }
29
30extern const char *TgtConnInit(int_T argc, char_T *argv[]);
31extern void TgtConnTerm();
32extern void TgtConnPreStep(int_T tid);
33extern void TgtConnPostStep(int_T tid);
34const char * csErrorStatus;
35
36/* Function prototype declaration*/
37void exitFcn(int sig);
38void *terminateTask(void *arg);
39void *baseRateTask(void *arg);
40void *subrateTask(void *arg);
41volatile boolean_T runModel = true;
42mw_signal_event_t stopSem;
43mw_signal_event_t baserateTaskSem;
44mw_thread_t schedulerThread;
45mw_thread_t baseRateThread;
46mw_thread_t backgroundThread;
47unsigned long threadJoinStatus[8];
48int terminatingmodel = 0;
49void *baseRateTask(void *arg)
50{
51 runModel = (rtmGetErrorStatus(magneto_tarato_M) == (NULL)) &&
52 !rtmGetStopRequested(magneto_tarato_M);
53 while (runModel) {
54 mw_osSignalEventWaitEver(&baserateTaskSem);
55
56 /* External mode */
57 {
58 boolean_T rtmStopReq = false;
59 rtExtModePauseIfNeeded(magneto_tarato_M->extModeInfo, 1, &rtmStopReq);
60 if (rtmStopReq) {
61 rtmSetStopRequested(magneto_tarato_M, true);
62 }
63
64 if (rtmGetStopRequested(magneto_tarato_M) == true) {
65 rtmSetErrorStatus(magneto_tarato_M, "Simulation finished");
66 break;
67 }
68 }
69
70 magneto_tarato_step();
71
72 /* Get model outputs here */
73 rtExtModeCheckEndTrigger();
74 runModel = (rtmGetErrorStatus(magneto_tarato_M) == (NULL)) &&
75 !rtmGetStopRequested(magneto_tarato_M);
76 }
77
78 runModel = 0;
79 terminateTask(arg);
80 mw_osThreadExit((void *)0);
81 return NULL;
82}
83
84void exitFcn(int sig)
85{
86 UNUSED(sig);
87 rtmSetErrorStatus(magneto_tarato_M, "stopping the model");
88}
89
90void *terminateTask(void *arg)
91{
92 UNUSED(arg);
93 terminatingmodel = 1;
94
95 {
96 runModel = 0;
97
98 /* Wait for background task to complete */
99 CHECK_STATUS(mw_osThreadJoin(backgroundThread,(void *)&threadJoinStatus), 0,
100 "mw_osThreadJoin");
101 }
102
103 rtExtModeShutdown(1);
104 TgtConnTerm();
105
106 /* Disable rt_OneStep() here */
107
108 /* Terminate model */
109 magneto_tarato_terminate();
110 mw_osSignalEventRelease(&stopSem);
111 return NULL;
112}
113
114void backgroundTask(void *arg)
115{
116 while (runModel) {
117 /* External mode */
118 {
119 boolean_T rtmStopReq = false;
120 rtExtModeOneStep(magneto_tarato_M->extModeInfo, 1, &rtmStopReq);
121 if (rtmStopReq) {
122 rtmSetStopRequested(magneto_tarato_M, true);
123 }
124 }
125
126 runCommService();
127 }
128}
129
130int main(int argc, char **argv)
131{
132 SystemCoreClockUpdate();
133 hardware_init();
134 rtmSetErrorStatus(magneto_tarato_M, 0);
135 rtExtModeParseArgs(argc, (const char_T **)argv, NULL);
136
137 /* Initialize model */
138 magneto_tarato_initialize();
139
140 /* Target connectivity initialization */
141 csErrorStatus = TgtConnInit(argc, argv);
142 EXIT_ON_ERROR("Error initializing target connectivity: %s\n", csErrorStatus);
143
144 /* External mode */
145 rtSetTFinalForExtMode(&rtmGetTFinal(magneto_tarato_M));
146 rtExtModeCheckInit(1);
147
148 {
149 boolean_T rtmStopReq = false;
150 rtExtModeWaitForStartPkt(magneto_tarato_M->extModeInfo, 1, &rtmStopReq);
151 if (rtmStopReq) {
152 rtmSetStopRequested(magneto_tarato_M, true);
153 }
154 }
155
156 rtERTExtModeStartMsg();
157
158 /* Call RTOS Initialization function */
159 mw_RTOSInit(0.02, 0);
160
161 /* Wait for stop semaphore */
162 mw_osSignalEventWaitEver(&stopSem);
163 return 0;
164}
165
166/*
167 * File trailer for generated code.
168 *
169 * [EOF]
170 */
171