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: untitled.c
7 *
8 * Code generated for Simulink model 'untitled'.
9 *
10 * Model version : 1.0
11 * Simulink Coder version : 8.12 (R2017a) 16-Feb-2017
12 * C/C++ source code generated on : Mon Oct 02 12:36:06 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 "untitled.h"
21#include "untitled_private.h"
22#include "untitled_dt.h"
23#define untitled_NoAck (false)
24#define untitled_RepeatedStart (false)
25#define untitled_SlaveAddress (104U)
26
27/* Named constants for Chart: '<Root>/MPU6050' */
28#define u_IN_Lecture_acceleration3_axes ((uint8_T)1U)
29#define untitled_IN_NO_ACTIVE_CHILD ((uint8_T)0U)
30#define untitled_IN_initialisation ((uint8_T)2U)
31#define untitled_RegisterAddress ((uint8_T)59U)
32#define untitled_SlaveAddress_b (104U)
33
34/* Block signals (auto storage) */
35B_untitled_T untitled_B;
36
37/* Block states (auto storage) */
38DW_untitled_T untitled_DW;
39
40/* Real-time model */
41RT_MODEL_untitled_T untitled_M_;
42RT_MODEL_untitled_T *const untitled_M = &untitled_M_;
43
44/* Start for function-call system: '<S1>/i2cWr' */
45void untitled_i2cWr_Start(DW_i2cWr_untitled_T *localDW)
46{
47 uint32_T i2cname;
48
49 /* Start for MATLABSystem: '<S4>/I2C Master Write' */
50 localDW->obj.isInitialized = 0;
51
52 /* [EOF] */
53 /* */
54 localDW->obj.isInitialized = 1;
55 i2cname = 0;
56 MW_I2C_Open(i2cname, 0U);
57
58 /* 100KHz. */
59 /* KHz */
60 localDW->obj.BusSpeed = 100000U;
61 i2cname = 0;
62 MW_I2C_SetBusSpeed(i2cname, localDW->obj.BusSpeed);
63}
64
65/* Output and update for function-call system: '<S1>/i2cWr' */
66void untitled_i2cWr(uint16_T rtu_dataw, DW_i2cWr_untitled_T *localDW)
67{
68 uint8_T SwappedDataBytes[2];
69 uint32_T i2cname;
70 uint16_T x;
71 uint8_T x_0[2];
72
73 /* Start for MATLABSystem: '<S4>/I2C Master Write' incorporates:
74 * MATLABSystem: '<S4>/I2C Master Write'
75 */
76 memcpy((void *)&SwappedDataBytes[0], (void *)&rtu_dataw, (size_t)2 * sizeof
77 (uint8_T));
78 x_0[0] = SwappedDataBytes[1];
79 x_0[1] = SwappedDataBytes[0];
80 memcpy((void *)&x, (void *)&x_0[0], (size_t)1 * sizeof(uint16_T));
81 memcpy((void *)&SwappedDataBytes[0], (void *)&x, (size_t)2 * sizeof(uint8_T));
82 i2cname = 0;
83 MW_I2C_MasterWrite(i2cname, untitled_SlaveAddress, SwappedDataBytes, 2U,
84 untitled_RepeatedStart, untitled_NoAck);
85 localDW->i2cWr_SubsysRanBC = 4;
86}
87
88/* Termination for function-call system: '<S1>/i2cWr' */
89void untitled_i2cWr_Term(DW_i2cWr_untitled_T *localDW)
90{
91 uint32_T i2cname;
92
93 /* Start for MATLABSystem: '<S4>/I2C Master Write' incorporates:
94 * Terminate for MATLABSystem: '<S4>/I2C Master Write'
95 */
96 if (localDW->obj.isInitialized == 1) {
97 localDW->obj.isInitialized = 2;
98 i2cname = 0;
99 MW_I2C_Close(i2cname);
100 }
101
102 /* End of Start for MATLABSystem: '<S4>/I2C Master Write' */
103}
104
105/* Model step function */
106void untitled_step(void)
107{
108 boolean_T p;
109 boolean_T p_0;
110 uint16_T output[7];
111 uint8_T status;
112 uint32_T i2cname;
113 uint8_T output_raw[14];
114 uint8_T y[2];
115 uint8_T x[2];
116 int32_T i;
117 uint16_T output_0;
118
119 /* Reset subsysRan breadcrumbs */
120 srClearBC(untitled_DW.i2cWr.i2cWr_SubsysRanBC);
121
122 /* Reset subsysRan breadcrumbs */
123 srClearBC(untitled_DW.i2cRd_SubsysRanBC);
124
125 /* Chart: '<Root>/MPU6050' */
126 /* Gateway: MPU6050 */
127 /* During: MPU6050 */
128 if (untitled_DW.is_active_c3_untitled == 0U) {
129 /* Entry: MPU6050 */
130 untitled_DW.is_active_c3_untitled = 1U;
131
132 /* Entry Internal: MPU6050 */
133 /* Transition: '<S1>:35' */
134 untitled_DW.is_c3_untitled = untitled_IN_initialisation;
135
136 /* Entry 'initialisation': '<S1>:34' */
137 /* '<S1>:34:1' i2cWr(uint16(hex2dec( '6B00' ))); */
138 /* Simulink Function 'i2cWr': '<S1>:28' */
139 untitled_B.dataw = 27392U;
140
141 /* Outputs for Function Call SubSystem: '<S1>/i2cWr' */
142 untitled_i2cWr(untitled_B.dataw, &untitled_DW.i2cWr);
143
144 /* End of Outputs for SubSystem: '<S1>/i2cWr' */
145
146 /* Power on */
147 /* '<S1>:34:3' i2cWr(uint16(hex2dec( '1B10' ))); */
148 /* Simulink Function 'i2cWr': '<S1>:28' */
149 untitled_B.dataw = 6928U;
150
151 /* Outputs for Function Call SubSystem: '<S1>/i2cWr' */
152 untitled_i2cWr(untitled_B.dataw, &untitled_DW.i2cWr);
153
154 /* End of Outputs for SubSystem: '<S1>/i2cWr' */
155
156 /* gyro 0x10=1000°/s */
157 /* '<S1>:34:4' i2cWr(uint16(hex2dec( '1C08' ))); */
158 /* Simulink Function 'i2cWr': '<S1>:28' */
159 untitled_B.dataw = 7176U;
160
161 /* Outputs for Function Call SubSystem: '<S1>/i2cWr' */
162 untitled_i2cWr(untitled_B.dataw, &untitled_DW.i2cWr);
163
164 /* End of Outputs for SubSystem: '<S1>/i2cWr' */
165
166 /* acce 0x08= 4G */
167 /* '<S1>:34:5' i2cWr(uint16(hex2dec( '1A06' ))); */
168 /* Simulink Function 'i2cWr': '<S1>:28' */
169 untitled_B.dataw = 6662U;
170
171 /* Outputs for Function Call SubSystem: '<S1>/i2cWr' */
172 untitled_i2cWr(untitled_B.dataw, &untitled_DW.i2cWr);
173
174 /* End of Outputs for SubSystem: '<S1>/i2cWr' */
175
176 /* filtrage+actu 0x06= 19ms */
177 /* '<S1>:34:6' i2cWr(uint16(hex2dec( '3710' ))); */
178 /* Simulink Function 'i2cWr': '<S1>:28' */
179 untitled_B.dataw = 14096U;
180
181 /* Outputs for Function Call SubSystem: '<S1>/i2cWr' */
182 untitled_i2cWr(untitled_B.dataw, &untitled_DW.i2cWr);
183
184 /* End of Outputs for SubSystem: '<S1>/i2cWr' */
185
186 /* INT_PIN_CFG */
187 /* '<S1>:34:7' i2cWr(uint16(hex2dec( '3800' ))); */
188 /* Simulink Function 'i2cWr': '<S1>:28' */
189 untitled_B.dataw = 14336U;
190
191 /* Outputs for Function Call SubSystem: '<S1>/i2cWr' */
192 untitled_i2cWr(untitled_B.dataw, &untitled_DW.i2cWr);
193
194 /* End of Outputs for SubSystem: '<S1>/i2cWr' */
195
196 /* Interrupt Enable 0 désactivé */
197 } else if (untitled_DW.is_c3_untitled == u_IN_Lecture_acceleration3_axes) {
198 /* Outputs for Function Call SubSystem: '<S1>/i2cRd' */
199 /* Start for MATLABSystem: '<S3>/I2C Master Read' incorporates:
200 * MATLABSystem: '<S3>/I2C Master Read'
201 */
202 /* During 'Lecture_acceleration3_axes': '<S1>:52' */
203 /* '<S1>:52:1' val=i2cRd; */
204 /* Simulink Function 'i2cRd': '<S1>:56' */
205 p = false;
206 p_0 = true;
207 if (!(untitled_DW.obj.SampleTime == untitled_P.I2CMasterRead_SampleTime)) {
208 p_0 = false;
209 }
210
211 if (p_0) {
212 p = true;
213 }
214
215 if (!p) {
216 untitled_DW.obj.SampleTime = untitled_P.I2CMasterRead_SampleTime;
217 }
218
219 for (i = 0; i < 7; i++) {
220 output[i] = 0U;
221 }
222
223 status = untitled_RegisterAddress;
224 i2cname = 0;
225 status = MW_I2C_MasterWrite(i2cname, untitled_SlaveAddress_b, &status, 1U,
226 true, false);
227 if (0 == status) {
228 i2cname = 0;
229 MW_I2C_MasterRead(i2cname, untitled_SlaveAddress_b, output_raw, 14U, false,
230 true);
231 memcpy((void *)&output[0], (void *)&output_raw[0], (size_t)7 * sizeof
232 (uint16_T));
233 for (i = 0; i < 7; i++) {
234 output_0 = output[i];
235 memcpy((void *)&y[0], (void *)&output_0, (size_t)2 * sizeof(uint8_T));
236 x[0] = y[1];
237 x[1] = y[0];
238 memcpy((void *)&output_0, (void *)&x[0], (size_t)1 * sizeof(uint16_T));
239 output[i] = output_0;
240 }
241 }
242
243 /* DataTypeConversion: '<S3>/Data Type Conversion' incorporates:
244 * MATLABSystem: '<S3>/I2C Master Read'
245 * Start for MATLABSystem: '<S3>/I2C Master Read'
246 */
247 for (i = 0; i < 7; i++) {
248 untitled_B.DataTypeConversion[i] = (int16_T)output[i];
249 }
250
251 /* End of DataTypeConversion: '<S3>/Data Type Conversion' */
252 untitled_DW.i2cRd_SubsysRanBC = 4;
253
254 /* End of Outputs for SubSystem: '<S1>/i2cRd' */
255 /* '<S1>:52:1' acc=val(1:3); */
256 /* '<S1>:52:1' gyro=val(5:7); */
257 untitled_B.acc[0] = untitled_B.DataTypeConversion[0];
258 untitled_B.gyro[0] = untitled_B.DataTypeConversion[4];
259 untitled_B.acc[1] = untitled_B.DataTypeConversion[1];
260 untitled_B.gyro[1] = untitled_B.DataTypeConversion[5];
261 untitled_B.acc[2] = untitled_B.DataTypeConversion[2];
262 untitled_B.gyro[2] = untitled_B.DataTypeConversion[6];
263
264 /* '<S1>:52:3' temp=val(4:4); */
265 untitled_B.temp = untitled_B.DataTypeConversion[3];
266 } else {
267 /* During 'initialisation': '<S1>:34' */
268 /* Transition: '<S1>:60' */
269 untitled_DW.is_c3_untitled = u_IN_Lecture_acceleration3_axes;
270 }
271
272 /* End of Chart: '<Root>/MPU6050' */
273
274 /* Gain: '<Root>/Gain' */
275 untitled_B.Gain[0] = (real32_T)untitled_P.Gain_Gain * 7.4505806E-9F *
276 (real32_T)untitled_B.acc[0];
277
278 /* Gain: '<Root>/Gain2' */
279 untitled_B.Gain2[0] = (real32_T)untitled_P.Gain2_Gain * 9.53674316E-7F *
280 (real32_T)untitled_B.gyro[0];
281
282 /* Gain: '<Root>/Gain' */
283 untitled_B.Gain[1] = (real32_T)untitled_P.Gain_Gain * 7.4505806E-9F *
284 (real32_T)untitled_B.acc[1];
285
286 /* Gain: '<Root>/Gain2' */
287 untitled_B.Gain2[1] = (real32_T)untitled_P.Gain2_Gain * 9.53674316E-7F *
288 (real32_T)untitled_B.gyro[1];
289
290 /* Gain: '<Root>/Gain' */
291 untitled_B.Gain[2] = (real32_T)untitled_P.Gain_Gain * 7.4505806E-9F *
292 (real32_T)untitled_B.acc[2];
293
294 /* Gain: '<Root>/Gain2' */
295 untitled_B.Gain2[2] = (real32_T)untitled_P.Gain2_Gain * 9.53674316E-7F *
296 (real32_T)untitled_B.gyro[2];
297
298 /* Trigonometry: '<S2>/Trigonometric Function1' incorporates:
299 * Product: '<S2>/Divide1'
300 * Product: '<S2>/Product2'
301 * Product: '<S2>/Product3'
302 * Sqrt: '<S2>/Sqrt1'
303 * Sum: '<S2>/Sum1'
304 */
305 untitled_B.TrigonometricFunction1 = (real32_T)tan(untitled_B.Gain[1] /
306 (real32_T)sqrt(untitled_B.Gain[0] * untitled_B.Gain[0] + untitled_B.Gain[2] *
307 untitled_B.Gain[2]));
308
309 /* Gain: '<Root>/Gain3' */
310 untitled_B.Gain3[0] = untitled_P.Gain3_Gain * untitled_B.Gain[0];
311 untitled_B.Gain3[1] = untitled_P.Gain3_Gain * untitled_B.Gain[1];
312 untitled_B.Gain3[2] = untitled_P.Gain3_Gain * untitled_B.Gain[2];
313
314 /* Sum: '<Root>/Sum' incorporates:
315 * Constant: '<Root>/Constant'
316 * Gain: '<Root>/Gain1'
317 */
318 untitled_B.Sum = (real32_T)untitled_P.Gain1_Gain * 1.1920929E-7F * (real32_T)
319 untitled_B.temp + untitled_P.Constant_Value;
320
321 /* Trigonometry: '<S2>/Trigonometric Function' incorporates:
322 * Product: '<S2>/Divide'
323 * Product: '<S2>/Product'
324 * Product: '<S2>/Product1'
325 * Sqrt: '<S2>/Sqrt'
326 * Sum: '<S2>/Sum'
327 */
328 untitled_B.TrigonometricFunction = (real32_T)tan(untitled_B.Gain[0] /
329 (real32_T)sqrt(untitled_B.Gain[1] * untitled_B.Gain[1] + untitled_B.Gain[2] *
330 untitled_B.Gain[2]));
331
332 /* Trigonometry: '<S2>/Trigonometric Function5' incorporates:
333 * Product: '<S2>/Divide5'
334 * Product: '<S2>/Product10'
335 * Product: '<S2>/Product11'
336 * Sqrt: '<S2>/Sqrt5'
337 * Sum: '<S2>/Sum5'
338 */
339 untitled_B.TrigonometricFunction5 = (real32_T)tan(untitled_B.Gain[2] /
340 (real32_T)sqrt(untitled_B.Gain[2] * untitled_B.Gain[2] + untitled_B.Gain[0] *
341 untitled_B.Gain[0]));
342
343 /* External mode */
344 rtExtModeUploadCheckTrigger(1);
345
346 { /* Sample time: [0.05s, 0.0s] */
347 rtExtModeUpload(0, untitled_M->Timing.taskTime0);
348 }
349
350 /* signal main to stop simulation */
351 { /* Sample time: [0.05s, 0.0s] */
352 if ((rtmGetTFinal(untitled_M)!=-1) &&
353 !((rtmGetTFinal(untitled_M)-untitled_M->Timing.taskTime0) >
354 untitled_M->Timing.taskTime0 * (DBL_EPSILON))) {
355 rtmSetErrorStatus(untitled_M, "Simulation finished");
356 }
357
358 if (rtmGetStopRequested(untitled_M)) {
359 rtmSetErrorStatus(untitled_M, "Simulation finished");
360 }
361 }
362
363 /* Update absolute time for base rate */
364 /* The "clockTick0" counts the number of times the code of this task has
365 * been executed. The absolute time is the multiplication of "clockTick0"
366 * and "Timing.stepSize0". Size of "clockTick0" ensures timer will not
367 * overflow during the application lifespan selected.
368 */
369 untitled_M->Timing.taskTime0 =
370 (++untitled_M->Timing.clockTick0) * untitled_M->Timing.stepSize0;
371}
372
373/* Model initialize function */
374void untitled_initialize(void)
375{
376 /* Registration code */
377
378 /* initialize real-time model */
379 (void) memset((void *)untitled_M, 0,
380 sizeof(RT_MODEL_untitled_T));
381 rtmSetTFinal(untitled_M, 10.0);
382 untitled_M->Timing.stepSize0 = 0.05;
383
384 /* External mode info */
385 untitled_M->Sizes.checksums[0] = (862269518U);
386 untitled_M->Sizes.checksums[1] = (2145128890U);
387 untitled_M->Sizes.checksums[2] = (1230379330U);
388 untitled_M->Sizes.checksums[3] = (2647555232U);
389
390 {
391 static const sysRanDType rtAlwaysEnabled = SUBSYS_RAN_BC_ENABLE;
392 static RTWExtModeInfo rt_ExtModeInfo;
393 static const sysRanDType *systemRan[6];
394 untitled_M->extModeInfo = (&rt_ExtModeInfo);
395 rteiSetSubSystemActiveVectorAddresses(&rt_ExtModeInfo, systemRan);
396 systemRan[0] = &rtAlwaysEnabled;
397 systemRan[1] = (sysRanDType *)&untitled_DW.i2cRd_SubsysRanBC;
398 systemRan[2] = (sysRanDType *)&untitled_DW.i2cRd_SubsysRanBC;
399 systemRan[3] = (sysRanDType *)&untitled_DW.i2cWr.i2cWr_SubsysRanBC;
400 systemRan[4] = (sysRanDType *)&untitled_DW.i2cWr.i2cWr_SubsysRanBC;
401 systemRan[5] = &rtAlwaysEnabled;
402 rteiSetModelMappingInfoPtr(untitled_M->extModeInfo,
403 &untitled_M->SpecialInfo.mappingInfo);
404 rteiSetChecksumsPtr(untitled_M->extModeInfo, untitled_M->Sizes.checksums);
405 rteiSetTPtr(untitled_M->extModeInfo, rtmGetTPtr(untitled_M));
406 }
407
408 /* block I/O */
409 (void) memset(((void *) &untitled_B), 0,
410 sizeof(B_untitled_T));
411
412 /* states (dwork) */
413 (void) memset((void *)&untitled_DW, 0,
414 sizeof(DW_untitled_T));
415
416 /* data type transition information */
417 {
418 static DataTypeTransInfo dtInfo;
419 (void) memset((char_T *) &dtInfo, 0,
420 sizeof(dtInfo));
421 untitled_M->SpecialInfo.mappingInfo = (&dtInfo);
422 dtInfo.numDataTypes = 19;
423 dtInfo.dataTypeSizes = &rtDataTypeSizes[0];
424 dtInfo.dataTypeNames = &rtDataTypeNames[0];
425
426 /* Block I/O transition table */
427 dtInfo.BTransTable = &rtBTransTable;
428
429 /* Parameters transition table */
430 dtInfo.PTransTable = &rtPTransTable;
431 }
432
433 {
434 uint32_T i2cname;
435 int32_T i;
436
437 /* Start for Chart: '<Root>/MPU6050' incorporates:
438 * Start for SubSystem: '<S1>/i2cWr'
439 */
440 untitled_i2cWr_Start(&untitled_DW.i2cWr);
441
442 /* Start for Chart: '<Root>/MPU6050' incorporates:
443 * Start for SubSystem: '<S1>/i2cRd'
444 */
445 /* Start for MATLABSystem: '<S3>/I2C Master Read' */
446 untitled_DW.obj.isInitialized = 0;
447
448 /* [EOF] */
449 /* */
450 untitled_DW.obj.SampleTime = untitled_P.I2CMasterRead_SampleTime;
451 untitled_DW.obj.isInitialized = 1;
452 i2cname = 0;
453 MW_I2C_Open(i2cname, 0U);
454
455 /* 100KHz. */
456 /* KHz */
457 untitled_DW.obj.BusSpeed = 100000U;
458 i2cname = 0;
459 MW_I2C_SetBusSpeed(i2cname, untitled_DW.obj.BusSpeed);
460 untitled_DW.is_active_c3_untitled = 0U;
461 untitled_DW.is_c3_untitled = untitled_IN_NO_ACTIVE_CHILD;
462
463 /* SystemInitialize for Chart: '<Root>/MPU6050' incorporates:
464 * SystemInitialize for SubSystem: '<S1>/i2cRd'
465 */
466 for (i = 0; i < 7; i++) {
467 /* SystemInitialize for Outport: '<S3>/datar' */
468 untitled_B.DataTypeConversion[i] = untitled_P.datar_Y0;
469 }
470 }
471}
472
473/* Model terminate function */
474void untitled_terminate(void)
475{
476 uint32_T i2cname;
477
478 /* Terminate for Chart: '<Root>/MPU6050' incorporates:
479 * Terminate for SubSystem: '<S1>/i2cWr'
480 */
481 untitled_i2cWr_Term(&untitled_DW.i2cWr);
482
483 /* Terminate for Chart: '<Root>/MPU6050' incorporates:
484 * Terminate for SubSystem: '<S1>/i2cRd'
485 */
486 /* Start for MATLABSystem: '<S3>/I2C Master Read' incorporates:
487 * Terminate for MATLABSystem: '<S3>/I2C Master Read'
488 */
489 if (untitled_DW.obj.isInitialized == 1) {
490 untitled_DW.obj.isInitialized = 2;
491 i2cname = 0;
492 MW_I2C_Close(i2cname);
493 }
494
495 /* End of Start for MATLABSystem: '<S3>/I2C Master Read' */
496}
497
498/*
499 * File trailer for generated code.
500 *
501 * [EOF]
502 */
503