Mercurial > public > ostc4
annotate Small_CPU/Src/scheduler.c @ 142:69f4b8067daa FlipDisplay
Use one global structure for all schedule counters
Moved SPI data handling from callback to cyclic 10ms slot
author | Ideenmodellierer |
---|---|
date | Thu, 28 Feb 2019 19:34:08 +0100 |
parents | 5df4f1a315cb |
children | ee744c7160ce |
rev | line source |
---|---|
38 | 1 /** |
2 ****************************************************************************** | |
3 * @file scheduler.c | |
4 * @author heinrichs weikamp gmbh | |
5 * @date 27-March-2014 | |
6 * @version V0.0.6 | |
7 * @since 18-June-2015 | |
8 * @brief the main part except for base.c | |
9 * | |
10 @verbatim | |
11 ============================================================================== | |
12 ##### How to use ##### | |
13 ============================================================================== | |
14 @endverbatim | |
15 ****************************************************************************** | |
16 * @attention | |
17 * | |
18 * <h2><center>© COPYRIGHT(c) 2015 heinrichs weikamp</center></h2> | |
19 * | |
20 ****************************************************************************** | |
21 */ | |
22 | |
23 | |
24 //#define DEBUGMODE | |
25 | |
26 /* Includes ------------------------------------------------------------------*/ | |
27 #include <string.h> | |
28 #include "baseCPU2.h" | |
29 #include "stm32f4xx_hal.h" | |
30 #include "i2c.h" | |
31 #include "scheduler.h" | |
32 #include "pressure.h" | |
33 #include "compass.h" | |
34 #include "batteryGasGauge.h" | |
35 #include "batteryCharger.h" | |
36 #include "spi.h" | |
37 #include "rtc.h" | |
38 #include "dma.h" | |
39 #include "adc.h" | |
40 #include "calc_crush.h" | |
41 #include "stm32f4xx_hal_rtc_ex.h" | |
42 #include "decom.h" | |
43 #include "wireless.h" | |
44 #include "tm_stm32f4_otp.h" | |
45 | |
46 | |
135 | 47 #define INVALID_PREASURE_VALUE (100.0F) |
48 | |
38 | 49 /* Private types -------------------------------------------------------------*/ |
50 const SGas Air = {79,0,0,0,0}; | |
51 | |
52 uint8_t testarrayindex = 0; | |
53 uint32_t testarray[256]; | |
54 uint32_t testarrayMain[256]; | |
55 | |
56 /* Exported variables --------------------------------------------------------*/ | |
57 SGlobal global; | |
58 SDevice DeviceDataFlash; | |
59 uint8_t deviceDataFlashValid = 0; | |
60 uint8_t deviceDataSubSeconds = 0; | |
61 | |
62 /* Private variables ---------------------------------------------------------*/ | |
63 /* can be lost while in sleep */ | |
64 uint8_t clearDecoNow = 0; | |
65 uint8_t setButtonsNow = 0; | |
66 | |
67 /* has to be in SRAM2 */ | |
68 uint8_t secondsCount = 0; | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
69 |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
70 SScheduleCtrl Scheduler; |
38 | 71 |
72 /* Private function prototypes -----------------------------------------------*/ | |
73 | |
74 _Bool vpm_crush2(void); | |
75 void scheduleUpdateDeviceData(void); | |
76 void initStructWithZeero(uint8_t* data, uint16_t length); | |
77 long get_nofly_time_minutes(void); | |
78 void copyActualGas(SGas gas); | |
79 void copyPressureData(void); | |
80 void copyCnsAndOtuData(void); | |
81 void copyTimeData(void); | |
82 void copyCompassData(void); | |
83 void copyCompassDataDuringCalibration(int16_t dx, int16_t dy, int16_t dz); | |
84 //void copyBatteryData(void); now in header | |
85 void copyAmbientLightData(void); | |
86 void copyTissueData(void); | |
87 void copyVpmCrushingData(void); | |
88 void copyDeviceData(void); | |
89 void changeAgeWirelessData(void); | |
90 void copyWirelessData(void); | |
91 void copyPICdata(void); | |
92 uint16_t schedule_update_timer_helper(int8_t thisSeconds); | |
93 | |
94 | |
95 uint32_t time_elapsed_ms(uint32_t ticksstart,uint32_t ticksnow); | |
96 | |
97 _Bool scheduleCheck_pressure_reached_dive_mode_level(void); | |
98 void scheduleSetDate(SDeviceLine *line); | |
99 | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
100 extern void SPI_Evaluate_RX_Data(); |
38 | 101 /* Exported functions --------------------------------------------------------*/ |
102 | |
103 void initGlobals(void) | |
104 { | |
105 initStructWithZeero((uint8_t*) &global, sizeof(SGlobal)); | |
106 | |
107 global.dataSendToSlavePending = 0; | |
108 global.dataSendToSlaveIsValid = 1; | |
109 global.dataSendToSlaveIsNotValidCount = 0; | |
110 | |
111 global.mode = MODE_POWERUP; | |
112 global.repetitive_dive = 0; | |
113 global.conservatism = 0; | |
114 global.whichGas = 0; | |
115 global.aktualGas[0] = Air; | |
116 global.lifeData.actualGas = global.aktualGas[0]; | |
117 | |
88 | 118 const uint8_t button_standard_sensitivity = ((2400-( 40 *24))/10)+15; |
38 | 119 global.ButtonResponsiveness[0] = button_standard_sensitivity; |
120 global.ButtonResponsiveness[1] = button_standard_sensitivity; | |
121 global.ButtonResponsiveness[2] = button_standard_sensitivity; | |
122 global.ButtonResponsiveness[3] = button_standard_sensitivity; | |
123 | |
124 global.ButtonPICdata[0] = 0xFF; | |
125 global.ButtonPICdata[1] = 0xFF; | |
126 global.ButtonPICdata[2] = 0xFF; | |
127 global.ButtonPICdata[3] = 0xFF; | |
128 | |
129 global.I2C_SystemStatus = 0xFF; // 0x00 would be everything working | |
130 | |
135 | 131 global.lifeData.pressure_ambient_bar = INVALID_PREASURE_VALUE; |
132 global.lifeData.pressure_surface_bar = INVALID_PREASURE_VALUE; | |
38 | 133 decom_reset_with_1000mbar(&global.lifeData); |
134 | |
135 global.demo_mode = 0; | |
136 | |
137 for(int i = 0; i < MAX_SENSORS; i++) | |
138 { | |
139 global.sensorError[i] = HAL_OK; // HAL_OK = 0; | |
140 } | |
141 | |
142 global.dataSendToMaster.RTE_VERSION_high = firmwareVersionHigh();//RTE_VERSION_HIGH;; | |
143 global.dataSendToMaster.RTE_VERSION_low = firmwareVersionLow();//RTE_VERSION_LOW;; | |
144 global.dataSendToMaster.chargeStatus = 0; | |
145 | |
146 global.dataSendToMaster.power_on_reset = 1; | |
147 global.dataSendToMaster.header.checkCode[0] = 0xA1; | |
148 global.dataSendToMaster.header.checkCode[1] = 0xA2; | |
149 global.dataSendToMaster.header.checkCode[2] = 0xA3; | |
150 global.dataSendToMaster.header.checkCode[3] = 0xA4; | |
151 global.dataSendToMaster.footer.checkCode[3] = 0xE4; | |
152 global.dataSendToMaster.footer.checkCode[2] = 0xE3; | |
153 global.dataSendToMaster.footer.checkCode[1] = 0xE2; | |
154 global.dataSendToMaster.footer.checkCode[0] = 0xE1; | |
155 global.dataSendToMaster.sensorErrors = 0; | |
156 | |
157 global.sync_error_count = 0; | |
158 global.check_sync_not_running = 0; | |
159 | |
160 global.deviceDataSendToMaster.RTE_VERSION_high = firmwareVersionHigh();//RTE_VERSION_HIGH; | |
161 global.deviceDataSendToMaster.RTE_VERSION_low = firmwareVersionLow();//RTE_VERSION_LOW; | |
162 global.deviceDataSendToMaster.chargeStatus = 0; | |
163 | |
164 global.deviceDataSendToMaster.power_on_reset = 1; | |
165 global.deviceDataSendToMaster.header.checkCode[0] = 0xDF; | |
166 global.deviceDataSendToMaster.header.checkCode[1] = 0xDE; | |
167 global.deviceDataSendToMaster.header.checkCode[2] = 0xDD; | |
168 global.deviceDataSendToMaster.header.checkCode[3] = 0xDC; | |
169 global.deviceDataSendToMaster.footer.checkCode[3] = 0xE4; | |
170 global.deviceDataSendToMaster.footer.checkCode[2] = 0xE3; | |
171 global.deviceDataSendToMaster.footer.checkCode[1] = 0xE2; | |
172 global.deviceDataSendToMaster.footer.checkCode[0] = 0xE1; | |
173 | |
174 global.dataSendToSlave.getDeviceDataNow = 0; | |
175 | |
176 global.deviceData.batteryChargeCompleteCycles.value_int32 = 0; | |
177 global.deviceData.batteryChargeCycles.value_int32 = 0; | |
178 global.deviceData.depthMaximum.value_int32 = 0; | |
179 global.deviceData.diveCycles.value_int32 = 0; | |
180 global.deviceData.hoursOfOperation.value_int32 = 0; | |
181 global.deviceData.temperatureMaximum.value_int32 = INT32_MIN; | |
182 global.deviceData.temperatureMinimum.value_int32 = INT32_MAX; | |
183 global.deviceData.voltageMinimum.value_int32 = INT32_MAX; | |
184 } | |
185 | |
186 | |
187 void scheduleSpecial_Evaluate_DataSendToSlave(void) | |
188 { | |
104 | 189 //TEMPORARY fix for compass calibration. |
190 //TODO: Fix I2C timeout for complete solving problem. | |
191 if(global.mode==MODE_CALIB){ | |
192 return; | |
193 } | |
90 | 194 |
88 | 195 global.dataSendToSlavePending = 0; |
196 if(!global.dataSendToSlaveIsValid) return; | |
38 | 197 |
198 global.dataSendToMaster.confirmRequest.uw = 0; | |
199 | |
200 if(TM_OTP_Read(0,0) == 0xFF) | |
201 { | |
202 if(global.dataSendToSlave.revisionHardware == (global.dataSendToSlave.revisionCRCx0x7A ^ 0x7A)) | |
203 TM_OTP_Write(0,0,global.dataSendToSlave.revisionHardware); | |
204 } | |
205 | |
206 if(global.dataSendToSlave.setAccidentFlag) | |
207 { | |
208 global.dataSendToMaster.confirmRequest.ub.accident = 1; | |
209 global.deviceData.diveAccident.value_int32 = global.dataSendToSlave.setAccidentFlag; | |
210 scheduleSetDate(&global.deviceData.diveAccident); | |
211 global.accidentFlag |= global.dataSendToSlave.setAccidentFlag; | |
212 if(global.accidentFlag == ACCIDENT_CNS) // LVL1 | |
213 global.accidentRemainingSeconds = 2*60*60; | |
214 else | |
215 global.accidentRemainingSeconds = 24*60*60; | |
216 } | |
217 | |
218 if(global.dataSendToSlave.setTimeNow) | |
219 { | |
220 global.dataSendToMaster.confirmRequest.ub.time = 1; | |
221 RTC_SetTime(global.dataSendToSlave.data.newTime); | |
222 schedule_update_timer_helper(0); | |
223 } | |
224 | |
225 if(global.dataSendToSlave.setDateNow) | |
226 { | |
227 global.dataSendToMaster.confirmRequest.ub.date = 1; | |
228 RTC_SetDate(global.dataSendToSlave.data.newDate); | |
229 schedule_update_timer_helper(0); | |
230 } | |
231 | |
232 if(global.dataSendToSlave.calibrateCompassNow) | |
233 { | |
234 global.dataSendToMaster.confirmRequest.ub.compass = 1; | |
235 global.mode = MODE_CALIB; | |
236 } | |
237 | |
238 if(global.dataSendToSlave.clearDecoNow) | |
239 { | |
240 global.dataSendToMaster.confirmRequest.ub.clearDeco = 1; | |
241 clearDecoNow = 1; | |
242 } | |
243 | |
244 if(global.dataSendToSlave.setButtonSensitivityNow) | |
245 { | |
246 global.dataSendToMaster.confirmRequest.ub.button = 1; | |
247 global.ButtonResponsiveness[0] = global.dataSendToSlave.data.buttonResponsiveness[0]; | |
248 global.ButtonResponsiveness[1] = global.dataSendToSlave.data.buttonResponsiveness[1]; | |
249 global.ButtonResponsiveness[2] = global.dataSendToSlave.data.buttonResponsiveness[2]; | |
250 global.ButtonResponsiveness[3] = global.dataSendToSlave.data.buttonResponsiveness[3]; | |
251 setButtonsNow = 1; | |
252 } | |
253 | |
254 if(global.dataSendToSlave.setBatteryGaugeNow) | |
255 { | |
104 | 256 if(global.mode!=MODE_CALIB){ |
38 | 257 global.dataSendToMaster.confirmRequest.ub.batterygauge = 1; |
258 battery_gas_gauge_set(global.dataSendToSlave.data.newBatteryGaugePercentageFloat); | |
104 | 259 } |
38 | 260 } |
261 | |
262 if((global.mode == MODE_SURFACE) && (global.dataSendToSlave.mode == MODE_SHUTDOWN)) | |
263 { | |
264 global.mode = MODE_SHUTDOWN; | |
265 } | |
266 | |
267 if(global.mode == MODE_DIVE) | |
268 { | |
269 copyActualGas(global.dataSendToSlave.data.actualGas); | |
270 } | |
271 else | |
272 { | |
273 copyActualGas(Air); | |
274 global.settings.divetimeToCreateLogbook = global.dataSendToSlave.data.divetimeToCreateLogbook; | |
275 global.settings.timeoutDiveReachedZeroDepth = global.dataSendToSlave.data.timeoutDiveReachedZeroDepth; | |
276 } | |
277 | |
278 /* for simulation / testing */ | |
279 global.ceiling_from_main_CPU_mbar = global.dataSendToSlave.data.ambient_pressure_mbar_ceiling; | |
280 | |
88 | 281 /* for device data updates */ |
282 deviceDataFlashValid = 0; | |
283 memcpy(&DeviceDataFlash, &global.dataSendToSlave.data.DeviceData, sizeof(SDevice)); | |
284 deviceDataFlashValid = 1; | |
89 | 285 |
286 | |
104 | 287 //TODO: Temporary placed here. Duration ~210 ms. |
288 if (global.I2C_SystemStatus != HAL_OK) { | |
289 MX_I2C1_TestAndClear(); | |
290 MX_I2C1_Init(); | |
291 // init_pressure(); | |
292 // compass_init(0, 7); | |
293 // accelerator_init(); | |
294 } | |
38 | 295 } |
296 | |
297 | |
298 /** | |
299 ****************************************************************************** | |
300 * @brief schedule_time_compare_helper. | |
301 * @author heinrichs weikamp gmbh | |
302 * @version V0.0.1 | |
303 * @date 20-Oct-2016 | |
304 ****************************************************************************** | |
305 */ | |
306 | |
307 uint8_t RtcBugFixChsw(uint8_t inStupidTime) | |
308 { | |
309 uint8_t multiplesOf16 = 0; | |
310 | |
311 multiplesOf16 = inStupidTime / 16; | |
312 | |
313 inStupidTime -= multiplesOf16 * 16; | |
314 | |
315 return (10 * multiplesOf16) + inStupidTime; | |
316 } | |
317 | |
318 | |
319 uint32_t minCounterDebug = 0; | |
320 | |
321 uint32_t schedule_time_compare_helper(RTC_TimeTypeDef timeNow, RTC_DateTypeDef dateNow, RTC_TimeTypeDef timeLast, RTC_DateTypeDef dateLast) | |
322 { | |
323 uint32_t nowInSeconds; | |
324 uint32_t lastInSeconds; | |
325 uint32_t resultDiff; | |
326 | |
327 if(timeNow.Minutes != timeLast.Minutes) | |
328 minCounterDebug++; | |
329 | |
330 nowInSeconds = (uint32_t)RtcBugFixChsw(timeNow.Hours) * 3600; | |
331 nowInSeconds += (uint32_t)RtcBugFixChsw(timeNow.Minutes) * 60; | |
332 nowInSeconds += (uint32_t)RtcBugFixChsw(timeNow.Seconds); | |
333 | |
334 lastInSeconds = (uint32_t)RtcBugFixChsw(timeLast.Hours) * 3600; | |
335 lastInSeconds += (uint32_t)RtcBugFixChsw(timeLast.Minutes) * 60; | |
336 lastInSeconds += (uint32_t)RtcBugFixChsw(timeLast.Seconds); | |
337 | |
338 /* | |
339 nowInSeconds = (uint32_t)timeNow.Hours * 3600; | |
340 nowInSeconds += (uint32_t)timeNow.Minutes * 60; | |
341 nowInSeconds += (uint32_t)timeNow.Seconds; | |
342 | |
343 lastInSeconds = (uint32_t)timeLast.Hours * 3600; | |
344 lastInSeconds += (uint32_t)timeLast.Minutes * 60; | |
345 lastInSeconds += (uint32_t)timeLast.Seconds; | |
346 */ | |
347 | |
348 if(dateNow.Date != dateLast.Date) | |
349 { | |
350 resultDiff = 86400 + nowInSeconds - lastInSeconds; | |
351 } | |
352 else | |
353 { | |
354 resultDiff = nowInSeconds - lastInSeconds; | |
355 } | |
356 return resultDiff; | |
357 } | |
358 | |
359 | |
360 | |
361 /** | |
362 ****************************************************************************** | |
363 * @brief schedule_update_timer_helper. | |
364 * @author heinrichs weikamp gmbh | |
365 * @version V0.0.1 | |
366 * @date 20-Oct-2016 | |
367 * @brief use 0 for init | |
368 use -1 for RTC controlled | |
369 use >= 1 for manual control | |
370 ****************************************************************************** | |
371 */ | |
372 extern RTC_HandleTypeDef RTCHandle; | |
373 | |
374 uint16_t schedule_update_timer_helper(int8_t thisSeconds) | |
375 { | |
376 static RTC_TimeTypeDef sTimeLast; | |
377 static RTC_DateTypeDef sDateLast; | |
378 RTC_TimeTypeDef sTimeNow; | |
379 RTC_DateTypeDef sDateNow; | |
380 uint32_t secondsPast; | |
381 uint32_t tempNewValue = 0; | |
382 | |
383 HAL_RTC_GetTime(&RTCHandle, &sTimeNow, RTC_FORMAT_BCD); | |
384 HAL_RTC_GetDate(&RTCHandle, &sDateNow, RTC_FORMAT_BCD); | |
385 | |
386 if(thisSeconds != 0) // otherwise just store sTimeLast, sDateLast and return 0 | |
387 { | |
388 secondsPast = schedule_time_compare_helper(sTimeNow, sDateNow, sTimeLast, sDateLast); | |
389 | |
390 if(thisSeconds > 0) // use this value instead, good for pre-loading sTimeLast and sDateLast | |
391 { | |
392 secondsPast = thisSeconds; | |
393 } | |
394 | |
395 if(global.seconds_since_last_dive) | |
396 { | |
397 if(secondsPast >= 777900) | |
398 { | |
399 global.seconds_since_last_dive = 0; | |
400 } | |
401 else | |
402 { | |
403 tempNewValue = ((uint32_t)global.seconds_since_last_dive) + secondsPast; | |
404 if(tempNewValue > 777900) // a bit more than nine days [seconds] | |
405 global.seconds_since_last_dive = 0; | |
406 else | |
407 global.seconds_since_last_dive = (long)tempNewValue; | |
408 } | |
409 } | |
410 } | |
411 | |
412 sTimeLast = sTimeNow; | |
413 sDateLast = sDateNow; | |
414 | |
415 return tempNewValue; | |
416 } | |
417 | |
418 | |
419 | |
420 | |
421 /** | |
422 ****************************************************************************** | |
423 * @brief schedule_check_resync. | |
424 * @author heinrichs weikamp gmbh | |
425 * @version V0.0.2 | |
426 * @date 18-June-2015 | |
427 ****************************************************************************** | |
428 */ | |
135 | 429 |
38 | 430 void schedule_check_resync(void) |
431 { | |
105 | 432 //TODO: REMOVE |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
433 if((global.check_sync_not_running >= 2)) |
38 | 434 { |
89 | 435 // global.dataSendToSlaveIsNotValidCount = 0; |
135 | 436 global.check_sync_not_running = 0; |
437 global.sync_error_count++; | |
438 | |
439 /* Try to start communication again. If exchange is stuck during execution for some reason the TX will be aborted by the | |
440 * function error handler | |
441 */ | |
442 SPI_Start_single_TxRx_with_Master(); | |
38 | 443 } |
444 } | |
445 | |
446 | |
447 /** | |
448 ****************************************************************************** | |
449 * @brief scheduleDiveMode. / Dive Mode: Main Loop | |
450 * @author Peter Ryser | |
451 * @version V0.0.1 | |
452 * @date 22-April-2014 | |
453 ****************************************************************************** | |
454 */ | |
455 void scheduleDiveMode(void) | |
456 { | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
457 // uint32_t tickstart = 0; |
38 | 458 uint32_t ticksdiff = 0; |
459 uint32_t lasttick = 0; | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
460 |
38 | 461 uint32_t turbo_seconds = 0; |
462 uint8_t counterAscentRate = 0; | |
463 float lastPressure_bar = 0.0f; | |
464 global.dataSendToMaster.mode = MODE_DIVE; | |
465 global.deviceDataSendToMaster.mode = MODE_DIVE; | |
466 //uint16_t counterSecondsShallowDepth = 0; | |
467 uint8_t counter_exit = 0; | |
468 | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
469 Scheduler.tickstart = HAL_GetTick() - 1000; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
470 Scheduler.counterSPIdata100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
471 Scheduler.counterCompass100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
472 Scheduler.counterPressure100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
473 Scheduler.counterAmbientLight100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
474 Scheduler.counterWireless1msec = 0; |
38 | 475 |
476 global.deviceData.diveCycles.value_int32++; | |
477 scheduleSetDate(&global.deviceData.diveCycles); | |
478 global.lifeData.counterSecondsShallowDepth = 0; | |
479 | |
480 while(global.mode == MODE_DIVE) | |
481 { | |
482 schedule_check_resync(); | |
483 lasttick = HAL_GetTick(); | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
484 ticksdiff = time_elapsed_ms(Scheduler.tickstart,lasttick); |
38 | 485 |
486 | |
487 //Evaluate wireless data every ms, if present | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
488 if(ticksdiff > Scheduler.counterWireless1msec) |
38 | 489 { |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
490 Scheduler.counterWireless1msec++; |
38 | 491 changeAgeWirelessData(); |
492 global.wirelessReceived = wireless_evaluate(global.wirelessdata,MAX_WIRELESS_BYTES, &global.wirelessConfidenceStatus); | |
493 if((global.wirelessReceived > 0) && !wireless_evaluate_crc_error(global.wirelessdata,global.wirelessReceived)) | |
494 { | |
495 copyWirelessData(); | |
496 } | |
497 } | |
498 | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
499 if(ticksdiff >= Scheduler.counterSPIdata100msec * 100 + 10) |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
500 { |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
501 SPI_Evaluate_RX_Data(); |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
502 Scheduler.counterSPIdata100msec++; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
503 } |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
504 |
38 | 505 //Evaluate pressure at 20 ms, 120 ms, 220 ms,.... |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
506 if(ticksdiff >= Scheduler.counterPressure100msec * 100 + 20) |
38 | 507 { |
508 global.check_sync_not_running++; | |
135 | 509 pressure_update(); |
510 scheduleUpdateDeviceData(); | |
38 | 511 if(global.demo_mode) |
512 { | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
513 turbo_seconds = demo_modify_temperature_and_pressure(global.lifeData.dive_time_seconds, Scheduler.counterPressure100msec, global.ceiling_from_main_CPU_mbar); |
38 | 514 if(turbo_seconds) |
515 { | |
516 global.lifeData.dive_time_seconds += turbo_seconds; | |
517 decom_tissues_exposure((int)(turbo_seconds), &global.lifeData); | |
518 copyTissueData(); | |
519 } | |
520 if((global.lifeData.counterSecondsShallowDepth > 1) && (global.lifeData.counterSecondsShallowDepth < (global.settings.timeoutDiveReachedZeroDepth - 10))) | |
521 global.lifeData.counterSecondsShallowDepth = (global.settings.timeoutDiveReachedZeroDepth - 10); | |
522 } | |
523 | |
524 | |
525 //Calc ascentrate every two second (20 * 100 ms) | |
526 counterAscentRate++; | |
527 if(counterAscentRate == 20) | |
528 { | |
529 global.lifeData.pressure_ambient_bar = get_pressure_mbar() / 1000.0f; | |
530 if(lastPressure_bar >= 0) | |
531 { | |
532 //2 seconds * 30 == 1 minute, bar * 10 = meter | |
533 global.lifeData.ascent_rate_meter_per_min = (lastPressure_bar - global.lifeData.pressure_ambient_bar) * 30 * 10; | |
534 } | |
535 lastPressure_bar = global.lifeData.pressure_ambient_bar; | |
536 counterAscentRate = 0; | |
537 } | |
135 | 538 copyPressureData(); |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
539 Scheduler.counterPressure100msec++; |
38 | 540 } |
541 //evaluate compass data at 50 ms, 150 ms, 250 ms,.... | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
542 if(ticksdiff >= Scheduler.counterCompass100msec * 100 + 50) |
135 | 543 { |
544 compass_read(); | |
545 acceleration_read(); | |
546 compass_calc(); | |
547 copyCompassData(); | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
548 Scheduler.counterCompass100msec++; |
135 | 549 } |
38 | 550 |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
551 if(ticksdiff >= Scheduler.counterAmbientLight100msec * 100 + 70) |
38 | 552 { |
553 adc_ambient_light_sensor_get_data(); | |
554 copyAmbientLightData(); | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
555 Scheduler.counterAmbientLight100msec++; |
38 | 556 } |
557 | |
558 //Evaluate tissues, toxic data, vpm, etc. once a second | |
559 if(ticksdiff >= 1000) | |
560 { | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
561 /* reset counter */ |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
562 Scheduler.tickstart = HAL_GetTick(); |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
563 |
38 | 564 if(global.dataSendToSlave.diveModeInfo != DIVEMODE_Apnea) |
565 { | |
566 scheduleUpdateLifeData(0); // includes tissues | |
567 global.lifeData.dive_time_seconds++; // there is dive_time_seconds_without_surface_time too | |
568 global.lifeData.ppO2 = decom_calc_ppO2(global.lifeData.pressure_ambient_bar, &global.lifeData.actualGas); | |
569 decom_oxygen_calculate_cns(&global.lifeData.cns,global.lifeData.ppO2); | |
570 decom_oxygen_calculate_otu(&global.lifeData.otu,global.lifeData.ppO2); | |
88 | 571 battery_gas_gauge_get_data(); |
38 | 572 |
573 | |
574 /** counter_exit allows safe exit via button for testing | |
575 * and demo_mode is exited too if aplicable. | |
576 */ | |
577 if(global.dataSendToMaster.mode == MODE_ENDDIVE) | |
578 { | |
579 counter_exit++; | |
580 if(counter_exit >= 2) | |
581 { | |
582 global.mode = MODE_SURFACE; | |
583 global.demo_mode = 0; | |
584 } | |
585 } | |
586 | |
587 if(is_ambient_pressure_close_to_surface(&global.lifeData)) | |
588 { | |
589 global.lifeData.counterSecondsShallowDepth++; | |
590 if((global.lifeData.counterSecondsShallowDepth >= global.settings.timeoutDiveReachedZeroDepth) || ((global.lifeData.dive_time_seconds < 60) && (global.demo_mode == 0)) || (global.dataSendToSlave.setEndDive)) | |
591 { | |
592 global.seconds_since_last_dive = 1; // start counter | |
593 schedule_update_timer_helper(0); // zum starten :-) | |
594 global.dataSendToMaster.mode = MODE_ENDDIVE; | |
595 global.deviceDataSendToMaster.mode = MODE_ENDDIVE; | |
596 } | |
597 } | |
598 else | |
599 { | |
600 global.lifeData.counterSecondsShallowDepth = 0; | |
601 global.lifeData.dive_time_seconds_without_surface_time++; | |
602 } | |
603 vpm_crush2(); | |
604 } | |
605 else // DIVEMODE_Apnea | |
606 { | |
607 global.lifeData.dive_time_seconds++; | |
608 | |
609 // exit dive mode | |
610 if(global.dataSendToMaster.mode == MODE_ENDDIVE) | |
611 { | |
612 counter_exit++; | |
613 if(counter_exit >= 2) | |
614 { | |
615 scheduleUpdateLifeData(-1); // 'restart' tissue calculations without calculating time during apnea mode | |
616 global.lifeData.dive_time_seconds = 0; // use backup noflytime and desaturation time | |
617 global.mode = MODE_SURFACE; | |
618 global.demo_mode = 0; | |
619 } | |
620 } | |
621 | |
622 // surface break | |
623 if(is_ambient_pressure_close_to_surface(&global.lifeData)) | |
624 { | |
625 global.lifeData.counterSecondsShallowDepth++; | |
626 if(global.lifeData.counterSecondsShallowDepth > 3) // time for main cpu to copy to apnea_last_dive_time_seconds | |
627 { | |
628 global.lifeData.dive_time_seconds = 0; // this apnea dive ends here | |
629 } | |
630 if((global.lifeData.counterSecondsShallowDepth >= global.settings.timeoutDiveReachedZeroDepth) || (global.dataSendToSlave.setEndDive)) | |
631 { | |
632 global.dataSendToMaster.mode = MODE_ENDDIVE; | |
633 global.deviceDataSendToMaster.mode = MODE_ENDDIVE; | |
634 } | |
635 } | |
636 else | |
637 { | |
638 global.lifeData.counterSecondsShallowDepth = 0; | |
639 global.lifeData.dive_time_seconds_without_surface_time++; | |
640 } | |
641 } // standard dive or DIVEMODE_Apnea | |
642 | |
88 | 643 copyVpmCrushingData(); |
644 copyTimeData(); | |
645 copyCnsAndOtuData(); | |
646 copyBatteryData(); | |
38 | 647 |
88 | 648 // new hw 170523 |
649 if(global.I2C_SystemStatus != HAL_OK) | |
650 { | |
651 MX_I2C1_TestAndClear(); | |
652 MX_I2C1_Init(); | |
653 if(!is_init_pressure_done()) | |
654 { | |
655 init_pressure(); | |
656 } | |
657 } | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
658 Scheduler.counterSPIdata100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
659 Scheduler.counterCompass100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
660 Scheduler.counterPressure100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
661 Scheduler.counterAmbientLight100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
662 Scheduler.counterWireless1msec = 0; |
38 | 663 } |
664 } | |
665 } | |
666 | |
667 | |
668 /** | |
669 ****************************************************************************** | |
670 * @brief scheduleSurfaceMode / surface mode: Main Loop | |
671 * @author Peter Ryser | |
672 * @version V0.0.1 | |
673 * @date 22-April-2014 | |
674 ****************************************************************************** | |
675 */ | |
676 | |
677 | |
678 // =============================================================================== | |
679 // scheduleTestMode | |
680 /// @brief included for sealed hardware with permanent RTE update message | |
681 // =============================================================================== | |
682 void scheduleTestMode(void) | |
683 { | |
684 uint32_t ticksdiff = 0; | |
685 uint32_t lasttick = 0; | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
686 Scheduler.tickstart = HAL_GetTick(); |
38 | 687 |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
688 Scheduler.counterPressure100msec = 0; |
38 | 689 |
690 float temperature_carousel = 0.0f; | |
691 float temperature_changer = 0.1f; | |
692 | |
693 while(global.mode == MODE_TEST) | |
694 { | |
695 schedule_check_resync(); | |
696 lasttick = HAL_GetTick(); | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
697 ticksdiff = time_elapsed_ms(Scheduler.tickstart,lasttick); |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
698 |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
699 //Evaluate received data at 10 ms, 110 ms, 210 ms,... |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
700 if(ticksdiff >= Scheduler.counterSPIdata100msec * 100 + 10) |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
701 { |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
702 SPI_Evaluate_RX_Data(); |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
703 Scheduler.counterSPIdata100msec++; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
704 } |
38 | 705 |
706 //Evaluate pressure at 20 ms, 120 ms, 220 ms,... | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
707 if(ticksdiff >= Scheduler.counterPressure100msec * 100 + 20) |
38 | 708 { |
709 global.check_sync_not_running++; | |
710 | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
711 pressure_update(); |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
712 scheduleUpdateDeviceData(); |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
713 global.lifeData.ascent_rate_meter_per_min = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
714 copyPressureData(); |
38 | 715 |
716 if(temperature_carousel > 20.0f) | |
717 { | |
718 temperature_carousel = 20.0f; | |
719 temperature_changer = -0.1f; | |
720 } | |
721 else | |
722 if(temperature_carousel < 0) | |
723 { | |
724 temperature_carousel = 0; | |
725 temperature_changer = +0.1f; | |
726 } | |
727 | |
728 temperature_carousel += temperature_changer; | |
729 | |
730 uint8_t boolPressureData = !global.dataSendToMaster.boolPressureData; | |
731 | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
732 global.dataSendToMaster.data[boolPressureData].pressure_mbar = get_pressure_mbar(); |
38 | 733 |
734 global.dataSendToMaster.data[boolPressureData].temperature = temperature_carousel; | |
735 global.dataSendToMaster.data[boolPressureData].pressure_uTick = HAL_GetTick(); | |
736 global.dataSendToMaster.boolPressureData = boolPressureData; | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
737 Scheduler.counterPressure100msec++; |
38 | 738 } |
739 | |
740 if(ticksdiff >= 1000) | |
741 { | |
742 //Set back tick counter | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
743 Scheduler.tickstart = HAL_GetTick(); |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
744 Scheduler.counterPressure100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
745 Scheduler.counterSPIdata100msec = 0; |
38 | 746 } |
747 }; | |
748 } | |
749 | |
750 | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
751 |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
752 |
38 | 753 void scheduleSurfaceMode(void) |
754 { | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
755 |
38 | 756 uint32_t ticksdiff = 0; |
757 uint32_t lasttick = 0; | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
758 Scheduler.tickstart = HAL_GetTick(); |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
759 Scheduler.counterSPIdata100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
760 Scheduler.counterCompass100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
761 Scheduler.counterPressure100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
762 Scheduler.counterAmbientLight100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
763 Scheduler.counterWireless1msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
764 |
38 | 765 global.dataSendToMaster.mode = MODE_SURFACE; |
766 global.deviceDataSendToMaster.mode = MODE_SURFACE; | |
767 | |
768 while(global.mode == MODE_SURFACE) | |
769 { | |
120 | 770 /* printf("surface...\n"); */ |
89 | 771 // SPI_Start_single_TxRx_with_Master(); |
38 | 772 schedule_check_resync(); |
773 lasttick = HAL_GetTick(); | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
774 ticksdiff = time_elapsed_ms(Scheduler.tickstart,lasttick); |
38 | 775 |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
776 if(ticksdiff > Scheduler.counterWireless1msec) |
38 | 777 { |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
778 Scheduler.counterWireless1msec++; |
38 | 779 changeAgeWirelessData(); |
780 global.wirelessReceived = wireless_evaluate(global.wirelessdata,MAX_WIRELESS_BYTES, &global.wirelessConfidenceStatus); | |
781 if((global.wirelessReceived > 0) && !wireless_evaluate_crc_error(global.wirelessdata,global.wirelessReceived)) | |
782 { | |
783 copyWirelessData(); | |
784 } | |
785 } | |
786 if(setButtonsNow == 1) | |
787 { | |
788 if(scheduleSetButtonResponsiveness()) | |
789 setButtonsNow = 0; | |
790 } | |
791 | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
792 |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
793 //Evaluate received data at 10 ms, 110 ms, 210 ms,... |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
794 if(ticksdiff >= Scheduler.counterSPIdata100msec * 100 + 10) |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
795 { |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
796 SPI_Evaluate_RX_Data(); |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
797 Scheduler.counterSPIdata100msec++; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
798 } |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
799 |
38 | 800 //Evaluate pressure at 20 ms, 120 ms, 220 ms,... |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
801 if(ticksdiff >= Scheduler.counterPressure100msec * 100 + 20) |
38 | 802 { |
803 global.check_sync_not_running++; | |
135 | 804 pressure_update(); |
805 scheduleUpdateDeviceData(); | |
38 | 806 global.lifeData.ascent_rate_meter_per_min = 0; |
135 | 807 copyPressureData(); |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
808 Scheduler.counterPressure100msec++; |
135 | 809 |
38 | 810 if(scheduleCheck_pressure_reached_dive_mode_level()) |
811 global.mode = MODE_DIVE; | |
812 } | |
813 | |
814 //evaluate compass data at 50 ms, 150 ms, 250 ms,... | |
815 | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
816 if(ticksdiff >= Scheduler.counterCompass100msec * 100 + 50) |
135 | 817 { |
818 compass_read(); | |
819 acceleration_read(); | |
820 compass_calc(); | |
821 copyCompassData(); | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
822 Scheduler.counterCompass100msec++; |
135 | 823 } |
38 | 824 |
825 //evaluate compass data at 70 ms, 170 ms, 270 ms,... | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
826 if(ticksdiff >= Scheduler.counterAmbientLight100msec * 100 + 70) |
38 | 827 { |
828 adc_ambient_light_sensor_get_data(); | |
829 copyAmbientLightData(); | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
830 Scheduler.counterAmbientLight100msec++; |
38 | 831 } |
89 | 832 //Evaluate tissues, toxic data, etc. once a second |
38 | 833 if(ticksdiff >= 1000) |
834 { | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
835 //Set back tick counter |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
836 Scheduler.tickstart = HAL_GetTick(); |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
837 |
38 | 838 if(clearDecoNow) |
839 { | |
840 decom_reset_with_1000mbar(&global.lifeData); ///< this should almost reset desaturation time | |
841 // new 160215 hw | |
842 global.repetitive_dive = 0; | |
843 global.seconds_since_last_dive = 0; ///< this will reset OTU and CNS as well | |
844 global.no_fly_time_minutes = 0; | |
845 global.accidentFlag = 0; | |
846 global.accidentRemainingSeconds = 0; | |
847 vpm_init(&global.vpm, global.conservatism, global.repetitive_dive, global.seconds_since_last_dive); | |
848 clearDecoNow = 0; | |
849 } | |
89 | 850 |
38 | 851 if(global.seconds_since_last_dive) |
852 { | |
853 schedule_update_timer_helper(-1); | |
854 // global.seconds_since_last_dive++; | |
855 // if(global.seconds_since_last_dive > 777900) // a bit more than nine days [seconds] | |
856 // global.seconds_since_last_dive = 0; | |
857 } | |
89 | 858 |
38 | 859 if(global.accidentRemainingSeconds) |
860 { | |
861 global.accidentRemainingSeconds--; | |
862 if(!global.accidentRemainingSeconds) | |
863 global.accidentFlag = 0; | |
864 } | |
865 global.dataSendToMaster.accidentFlags = global.accidentFlag; | |
89 | 866 |
38 | 867 update_surface_pressure(1); |
868 scheduleUpdateLifeData(0); | |
869 decom_oxygen_calculate_otu_degrade(&global.lifeData.otu, global.seconds_since_last_dive); | |
870 decom_oxygen_calculate_cns_degrade(&global.lifeData.cns, global.seconds_since_last_dive); | |
135 | 871 |
872 /* start desaturation calculation after first valid measurement has been done */ | |
873 if(global.lifeData.pressure_surface_bar != INVALID_PREASURE_VALUE) | |
874 { | |
875 global.lifeData.desaturation_time_minutes = decom_calc_desaturation_time(global.lifeData.tissue_nitrogen_bar,global.lifeData.tissue_helium_bar,global.lifeData.pressure_surface_bar); | |
876 } | |
877 else | |
878 { | |
879 global.lifeData.desaturation_time_minutes = 0; | |
880 } | |
38 | 881 battery_charger_get_status_and_contral_battery_gas_gauge(0); |
88 | 882 battery_gas_gauge_get_data(); |
89 | 883 |
88 | 884 copyCnsAndOtuData(); |
885 copyTimeData(); | |
886 copyBatteryData(); | |
887 copyDeviceData(); | |
38 | 888 |
88 | 889 // new hw 170523 |
890 if(global.I2C_SystemStatus != HAL_OK) | |
891 { | |
892 MX_I2C1_TestAndClear(); | |
893 MX_I2C1_Init(); | |
894 if(!is_init_pressure_done()) | |
895 { | |
896 init_pressure(); | |
897 } | |
898 } | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
899 Scheduler.counterSPIdata100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
900 Scheduler.counterCompass100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
901 Scheduler.counterPressure100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
902 Scheduler.counterAmbientLight100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
903 Scheduler.counterWireless1msec = 0; |
38 | 904 } |
905 } | |
906 } | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
907 static uint8_t dohardspisync = 1; |
38 | 908 |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
909 void HardSyncToSPI() |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
910 { |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
911 if(dohardspisync) |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
912 { |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
913 //Set back tick counter |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
914 Scheduler.tickstart = HAL_GetTick(); |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
915 Scheduler.counterSPIdata100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
916 Scheduler.counterCompass100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
917 Scheduler.counterPressure100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
918 Scheduler.counterAmbientLight100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
919 Scheduler.counterWireless1msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
920 dohardspisync = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
921 } |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
922 } |
38 | 923 |
924 /** | |
925 ****************************************************************************** | |
926 * @brief scheduleCompassCalibrationMode | |
927 * @author heinrichs weikamp gmbh | |
928 * @version V0.0.1 | |
929 * @since 31-March-2015 | |
930 * @date 31-March-2015 | |
931 ****************************************************************************** | |
932 */ | |
933 void scheduleCompassCalibrationMode(void) | |
934 { | |
935 compass_init(1,7); // fast mode, max gain | |
936 compass_calib(); // duration : 1 minute! | |
937 compass_init(0,7); // back to normal mode | |
938 | |
939 if(global.seconds_since_last_dive) | |
940 { | |
941 schedule_update_timer_helper(-1); | |
942 // global.seconds_since_last_dive += 60; | |
943 // if(global.seconds_since_last_dive > 777900) // a bit more than nine days [seconds] | |
944 // global.seconds_since_last_dive = 0; | |
945 } | |
946 | |
947 scheduleUpdateLifeData(0); | |
948 global.mode = MODE_SURFACE; | |
949 } | |
950 | |
951 | |
952 /** | |
953 ****************************************************************************** | |
954 * @brief scheduleSleepMode / sleep mode: Main Loop | |
955 * @author heinrichs weikamp gmbh | |
956 * @version V0.0.2 | |
957 * @since 31-March-2015 | |
958 * @date 22-April-2014 | |
959 ****************************************************************************** | |
960 */ | |
961 | |
962 void scheduleSleepMode(void) | |
963 { | |
964 global.dataSendToMaster.mode = 0; | |
965 global.deviceDataSendToMaster.mode = 0; | |
966 | |
967 /* prevent button wake up problem while in sleep_prepare | |
968 * sleep prepare does I2C_DeInit() | |
969 */ | |
970 if(global.mode != MODE_SLEEP) | |
971 MX_I2C1_Init(); | |
972 else | |
973 do | |
974 { | |
975 I2C_DeInit(); | |
976 | |
977 #ifdef DEBUGMODE | |
978 HAL_Delay(2000); | |
979 #else | |
980 RTC_StopMode_2seconds(); | |
981 #endif | |
982 | |
983 | |
984 | |
985 if(global.mode == MODE_SLEEP) | |
986 secondsCount += 2; | |
987 | |
988 MX_I2C1_Init(); | |
989 pressure_sensor_get_pressure_raw(); | |
990 | |
991 if(secondsCount >= 30) | |
992 { | |
993 pressure_sensor_get_temperature_raw(); | |
994 battery_gas_gauge_get_data(); | |
995 // ReInit_battery_charger_status_pins(); | |
996 battery_charger_get_status_and_contral_battery_gas_gauge(1); | |
997 // DeInit_battery_charger_status_pins(); | |
998 secondsCount = 0; | |
999 } | |
1000 | |
1001 pressure_calculation(); | |
1002 | |
1003 scheduleUpdateDeviceData(); | |
1004 update_surface_pressure(2); | |
1005 | |
1006 if(global.seconds_since_last_dive) | |
1007 { | |
1008 schedule_update_timer_helper(-1); | |
1009 // global.seconds_since_last_dive += 2; | |
1010 // if(global.seconds_since_last_dive > 777900) // a bit more than nine days [seconds] | |
1011 // global.seconds_since_last_dive = 0; | |
1012 } | |
1013 | |
1014 if(global.accidentRemainingSeconds) | |
1015 { | |
1016 if(global.accidentRemainingSeconds > 2) | |
1017 global.accidentRemainingSeconds -= 2; | |
1018 else | |
1019 { | |
1020 global.accidentRemainingSeconds = 0; | |
1021 global.accidentFlag = 0; | |
1022 } | |
1023 } | |
1024 | |
1025 if(scheduleCheck_pressure_reached_dive_mode_level()) | |
1026 global.mode = MODE_BOOT; | |
1027 | |
1028 scheduleUpdateLifeData(2000); | |
1029 } | |
1030 while(global.mode == MODE_SLEEP); | |
1031 /* new section for system after Standby */ | |
1032 scheduleUpdateLifeData(-1); | |
1033 clearDecoNow = 0; | |
1034 setButtonsNow = 0; | |
1035 } | |
1036 | |
1037 | |
1038 | |
1039 /* Private functions ---------------------------------------------------------*/ | |
1040 | |
1041 | |
1042 /** | |
1043 ****************************************************************************** | |
1044 * @brief scheduleCheck_pressure_reached_dive_mode_level | |
1045 * @author heinrichs weikamp gmbh | |
1046 * @version V0.0.1 from inline code | |
1047 * @date 09-Sept-2015 | |
1048 ****************************************************************************** | |
1049 */ | |
1050 _Bool scheduleCheck_pressure_reached_dive_mode_level(void) | |
1051 { | |
1052 if(get_pressure_mbar() > 1160) | |
1053 return 1; | |
1054 else | |
1055 if((global.mode == MODE_SURFACE) && (get_pressure_mbar() > (get_surface_mbar() + 100)) && (get_surface_mbar() > 880)) | |
1056 return 1; | |
1057 else | |
1058 return 0; | |
1059 } | |
1060 | |
1061 | |
1062 /** | |
1063 ****************************************************************************** | |
1064 * @brief scheduleUpdateLifeData / calculates tissues | |
1065 * @author Peter Ryser | |
1066 * @version V0.0.1 | |
1067 * @date 22-April-2014 | |
1068 ****************************************************************************** | |
1069 */ | |
1070 | |
1071 | |
1072 void scheduleUpdateLifeData(int32_t asynchron_milliseconds_since_last) | |
1073 { | |
1074 static _Bool first = 1; | |
1075 static uint32_t tickstart = 0; | |
1076 static uint32_t ticksrest = 0; | |
1077 | |
1078 uint32_t ticksdiff = 0; | |
1079 uint32_t ticksnow = 0; | |
1080 uint32_t time_seconds = 0; | |
1081 uint8_t whichGasTmp = 0; | |
1082 | |
135 | 1083 uint8_t updateTissueData = 0; |
1084 | |
1085 | |
1086 if(global.lifeData.pressure_surface_bar == INVALID_PREASURE_VALUE) | |
1087 { | |
1088 updateTissueData = 1; | |
1089 } | |
1090 | |
38 | 1091 if(asynchron_milliseconds_since_last < 0) |
1092 { | |
1093 first = 1; | |
1094 tickstart = 0; | |
1095 ticksrest = 0; | |
1096 return; | |
1097 } | |
1098 | |
1099 if(!asynchron_milliseconds_since_last && first) | |
1100 { | |
1101 tickstart = HAL_GetTick(); | |
1102 first = 0; | |
1103 return; | |
1104 } | |
1105 | |
1106 whichGasTmp = global.whichGas; | |
1107 global.lifeData.actualGas = global.aktualGas[whichGasTmp]; | |
1108 global.lifeData.pressure_ambient_bar = get_pressure_mbar() / 1000.0f; | |
1109 global.lifeData.pressure_surface_bar = get_surface_mbar() / 1000.0f; | |
1110 | |
135 | 1111 if(updateTissueData) |
1112 { | |
1113 decom_reset_with_ambientmbar(global.lifeData.pressure_surface_bar,&global.lifeData); | |
1114 } | |
1115 | |
38 | 1116 if(!asynchron_milliseconds_since_last) |
1117 { | |
1118 ticksnow = HAL_GetTick(); | |
1119 ticksdiff = time_elapsed_ms(tickstart,ticksnow); | |
1120 } | |
1121 else | |
1122 { | |
1123 first = 1; | |
1124 ticksdiff = asynchron_milliseconds_since_last; | |
1125 } | |
1126 | |
1127 if(ticksrest > 1000) // whatever happens after standby with STM32L476 | |
1128 ticksrest = 0; // maybe move static to SRAM2 | |
1129 | |
1130 ticksdiff += ticksrest; | |
1131 time_seconds = ticksdiff/ 1000; | |
1132 ticksrest = ticksdiff - time_seconds * 1000; | |
1133 tickstart = ticksnow; | |
1134 | |
1135 decom_tissues_exposure((int)time_seconds, &global.lifeData); | |
1136 if(global.demo_mode) | |
1137 decom_tissues_exposure((int)(3*time_seconds), &global.lifeData); | |
1138 copyTissueData(); | |
1139 } | |
1140 | |
1141 | |
1142 /** | |
1143 ****************************************************************************** | |
1144 * @brief scheduleUpdateDeviceData | |
1145 * @author heinrichs weikamp gmbh | |
1146 * @version V0.0.1 | |
1147 * @date 16-March-2015 | |
1148 * | |
1149 * two step process | |
1150 * first compare with data from main CPU == externalLogbookFlash | |
1151 * second update with new sensor data | |
1152 ****************************************************************************** | |
1153 */ | |
1154 void scheduleSetDate(SDeviceLine *line) | |
1155 { | |
1156 extern RTC_HandleTypeDef RTCHandle; | |
1157 | |
1158 line->date_rtc_dr = (uint32_t)(RTCHandle.Instance->DR & RTC_DR_RESERVED_MASK); | |
1159 line->time_rtc_tr = (uint32_t)(RTCHandle.Instance->TR & RTC_TR_RESERVED_MASK); | |
1160 } | |
1161 | |
1162 | |
1163 void scheduleCopyDeviceData(SDeviceLine *lineWrite, const SDeviceLine *lineRead) | |
1164 { | |
1165 lineWrite->date_rtc_dr = lineRead->date_rtc_dr; | |
1166 lineWrite->time_rtc_tr = lineRead->time_rtc_tr; | |
1167 lineWrite->value_int32 = lineRead->value_int32; | |
1168 } | |
1169 | |
1170 | |
1171 void scheduleUpdateDeviceData(void) | |
1172 { | |
1173 /* first step, main CPU */ | |
1174 | |
1175 if(deviceDataFlashValid) | |
1176 { | |
1177 /* max values */ | |
1178 if(global.deviceData.batteryChargeCompleteCycles.value_int32 < DeviceDataFlash.batteryChargeCompleteCycles.value_int32) | |
1179 { | |
1180 scheduleCopyDeviceData(&global.deviceData.batteryChargeCompleteCycles, &DeviceDataFlash.batteryChargeCompleteCycles); | |
1181 } | |
1182 if(global.deviceData.batteryChargeCycles.value_int32 < DeviceDataFlash.batteryChargeCycles.value_int32) | |
1183 { | |
1184 scheduleCopyDeviceData(&global.deviceData.batteryChargeCycles, &DeviceDataFlash.batteryChargeCycles); | |
1185 } | |
1186 if(global.deviceData.temperatureMaximum.value_int32 < DeviceDataFlash.temperatureMaximum.value_int32) | |
1187 { | |
1188 scheduleCopyDeviceData(&global.deviceData.temperatureMaximum, &DeviceDataFlash.temperatureMaximum); | |
1189 } | |
1190 if(global.deviceData.depthMaximum.value_int32 < DeviceDataFlash.depthMaximum.value_int32) | |
1191 { | |
1192 scheduleCopyDeviceData(&global.deviceData.depthMaximum, &DeviceDataFlash.depthMaximum); | |
1193 } | |
1194 if(global.deviceData.diveCycles.value_int32 < DeviceDataFlash.diveCycles.value_int32) | |
1195 { | |
1196 scheduleCopyDeviceData(&global.deviceData.diveCycles, &DeviceDataFlash.diveCycles); | |
1197 } | |
1198 if(global.deviceData.hoursOfOperation.value_int32 < DeviceDataFlash.hoursOfOperation.value_int32) | |
1199 { | |
1200 scheduleCopyDeviceData(&global.deviceData.hoursOfOperation, &DeviceDataFlash.hoursOfOperation); | |
1201 } | |
1202 | |
1203 /* min values */ | |
1204 if(global.deviceData.temperatureMinimum.value_int32 > DeviceDataFlash.temperatureMinimum.value_int32) | |
1205 { | |
1206 scheduleCopyDeviceData(&global.deviceData.temperatureMinimum, &DeviceDataFlash.temperatureMinimum); | |
1207 } | |
1208 if(global.deviceData.voltageMinimum.value_int32 > DeviceDataFlash.voltageMinimum.value_int32) | |
1209 { | |
1210 scheduleCopyDeviceData(&global.deviceData.voltageMinimum, &DeviceDataFlash.voltageMinimum); | |
1211 } | |
1212 } | |
1213 | |
1214 /* second step, sensor data */ | |
1215 int32_t temperature_centigrad_int32; | |
1216 int32_t pressure_mbar_int32; | |
1217 int32_t voltage_mvolt_int32; | |
1218 | |
1219 temperature_centigrad_int32 = (int32_t)(get_temperature() * 100); | |
1220 if(temperature_centigrad_int32 < global.deviceData.temperatureMinimum.value_int32) | |
1221 { | |
1222 global.deviceData.temperatureMinimum.value_int32 = temperature_centigrad_int32; | |
88 | 1223 scheduleSetDate(&global.deviceData.temperatureMinimum); |
38 | 1224 } |
1225 | |
1226 if(temperature_centigrad_int32 > global.deviceData.temperatureMaximum.value_int32) | |
1227 { | |
1228 global.deviceData.temperatureMaximum.value_int32 = temperature_centigrad_int32; | |
88 | 1229 scheduleSetDate(&global.deviceData.temperatureMaximum); |
38 | 1230 } |
1231 | |
1232 pressure_mbar_int32 = (int32_t)get_pressure_mbar(); | |
1233 if(pressure_mbar_int32 > global.deviceData.depthMaximum.value_int32) | |
1234 { | |
1235 global.deviceData.depthMaximum.value_int32 = pressure_mbar_int32; | |
88 | 1236 scheduleSetDate(&global.deviceData.depthMaximum); |
38 | 1237 } |
1238 | |
1239 voltage_mvolt_int32 = (int32_t)(get_voltage() * 1000); | |
1240 if(voltage_mvolt_int32 < global.deviceData.voltageMinimum.value_int32) | |
1241 { | |
1242 global.deviceData.voltageMinimum.value_int32 = voltage_mvolt_int32; | |
88 | 1243 scheduleSetDate(&global.deviceData.voltageMinimum); |
38 | 1244 } |
1245 | |
1246 /* third step, counter */ | |
1247 switch (global.mode) | |
1248 { | |
1249 case MODE_SURFACE: | |
1250 case MODE_DIVE: | |
1251 default: | |
1252 deviceDataSubSeconds++; | |
1253 if(deviceDataSubSeconds > 10) | |
1254 { | |
1255 deviceDataSubSeconds = 0; | |
1256 global.deviceData.hoursOfOperation.value_int32++; | |
1257 } | |
1258 break; | |
1259 | |
1260 case MODE_SLEEP: | |
1261 case MODE_SHUTDOWN: | |
1262 break; | |
1263 } | |
1264 } | |
1265 | |
1266 | |
1267 void scheduleUpdateDeviceDataChargerFull(void) | |
1268 { | |
1269 global.deviceData.batteryChargeCompleteCycles.value_int32++; | |
88 | 1270 scheduleSetDate(&global.deviceData.batteryChargeCompleteCycles); |
38 | 1271 } |
1272 | |
1273 | |
1274 void scheduleUpdateDeviceDataChargerCharging(void) | |
1275 { | |
1276 global.deviceData.batteryChargeCycles.value_int32++; | |
88 | 1277 scheduleSetDate(&global.deviceData.batteryChargeCycles); |
38 | 1278 } |
1279 | |
1280 | |
1281 /** | |
1282 ****************************************************************************** | |
1283 * @brief vpm_crush / calls vpm calc_crushing_pressure every four seconds during descend | |
1284 * @author Peter Ryser | |
1285 * @version V0.0.1 | |
1286 * @date 22-April-2014 | |
1287 ****************************************************************************** | |
1288 */ | |
1289 _Bool vpm_crush2(void) | |
1290 { | |
1291 int i = 0; | |
1292 static float starting_ambient_pressure = 0; | |
1293 static float ending_ambient_pressure = 0; | |
1294 static float time_calc_begin = -1; | |
1295 static float initial_helium_pressure[16]; | |
1296 static float initial_nitrogen_pressure[16]; | |
1297 ending_ambient_pressure = global.lifeData.pressure_ambient_bar * 10; | |
1298 | |
1299 if((global.lifeData.dive_time_seconds <= 4) || (starting_ambient_pressure >= ending_ambient_pressure)) | |
1300 { | |
1301 time_calc_begin = global.lifeData.dive_time_seconds; | |
1302 starting_ambient_pressure = global.lifeData.pressure_ambient_bar * 10; | |
1303 for( i = 0; i < 16; i++) | |
1304 { | |
1305 initial_helium_pressure[i] = global.lifeData.tissue_helium_bar[i] * 10; | |
1306 initial_nitrogen_pressure[i] = global.lifeData.tissue_nitrogen_bar[i] * 10; | |
1307 } | |
1308 return 0; | |
1309 } | |
1310 if(global.lifeData.dive_time_seconds - time_calc_begin >= 4) | |
1311 { | |
1312 if(ending_ambient_pressure > starting_ambient_pressure + 0.5f) | |
1313 { | |
1314 float rate = (ending_ambient_pressure - starting_ambient_pressure) * 60 / 4; | |
1315 calc_crushing_pressure(&global.lifeData, &global.vpm, initial_helium_pressure, initial_nitrogen_pressure, starting_ambient_pressure, rate); | |
1316 | |
1317 time_calc_begin = global.lifeData.dive_time_seconds; | |
1318 starting_ambient_pressure = global.lifeData.pressure_ambient_bar * 10; | |
1319 for( i = 0; i < 16; i++) | |
1320 { | |
1321 initial_helium_pressure[i] = global.lifeData.tissue_helium_bar[i] * 10; | |
1322 initial_nitrogen_pressure[i] = global.lifeData.tissue_nitrogen_bar[i] * 10; | |
1323 } | |
1324 | |
1325 return 1; | |
1326 } | |
1327 | |
1328 } | |
1329 return 0; | |
1330 }; | |
1331 | |
1332 | |
1333 void initStructWithZeero(uint8_t* data, uint16_t length) | |
1334 { | |
1335 for(uint16_t i = 0; i < length; i++) | |
1336 data[i] = 0; | |
1337 } | |
1338 | |
1339 | |
1340 long get_nofly_time_minutes(void) | |
1341 { | |
1342 | |
1343 if(global.no_fly_time_minutes <= 0) | |
1344 return 0; | |
1345 | |
1346 long minutes_since_last_dive = global.seconds_since_last_dive/60; | |
1347 | |
1348 if((global.seconds_since_last_dive > 0) && (global.no_fly_time_minutes > minutes_since_last_dive)) | |
1349 { | |
1350 return (global.no_fly_time_minutes - minutes_since_last_dive); | |
1351 } | |
1352 else | |
1353 { | |
1354 global.no_fly_time_minutes = 0; | |
1355 return 0; | |
1356 } | |
1357 } | |
1358 | |
1359 | |
1360 //Supports threadsave copying!!! | |
1361 void copyActualGas(SGas gas) | |
1362 { | |
1363 uint8_t whichGas = !global.whichGas; | |
1364 global.aktualGas[whichGas] = gas; | |
1365 global.whichGas = whichGas; | |
1366 } | |
1367 | |
1368 | |
1369 //Supports threadsave copying!!! | |
1370 void copyPressureData(void) | |
1371 { | |
1372 global.dataSendToMaster.sensorErrors = I2C1_Status(); | |
1373 //uint8_t dataSendToMaster. | |
1374 uint8_t boolPressureData = !global.dataSendToMaster.boolPressureData; | |
1375 global.dataSendToMaster.data[boolPressureData].temperature = get_temperature(); | |
1376 global.dataSendToMaster.data[boolPressureData].pressure_mbar = get_pressure_mbar(); | |
1377 global.dataSendToMaster.data[boolPressureData].surface_mbar = get_surface_mbar(); | |
1378 global.dataSendToMaster.data[boolPressureData].ascent_rate_meter_per_min = global.lifeData.ascent_rate_meter_per_min; | |
1379 global.dataSendToMaster.data[boolPressureData].pressure_uTick = HAL_GetTick(); | |
1380 global.dataSendToMaster.boolPressureData = boolPressureData; | |
1381 } | |
1382 | |
1383 | |
1384 //Supports threadsave copying!!! | |
1385 void copyCnsAndOtuData(void) | |
1386 { | |
1387 //uint8_t dataSendToMaster. | |
1388 uint8_t boolToxicData = !global.dataSendToMaster.boolToxicData; | |
1389 global.dataSendToMaster.data[boolToxicData].cns = global.lifeData.cns; | |
1390 global.dataSendToMaster.data[boolToxicData].otu = global.lifeData.otu; | |
1391 global.dataSendToMaster.data[boolToxicData].desaturation_time_minutes = global.lifeData.desaturation_time_minutes; | |
1392 global.dataSendToMaster.data[boolToxicData].no_fly_time_minutes = get_nofly_time_minutes(); | |
1393 global.dataSendToMaster.boolToxicData = boolToxicData; | |
1394 } | |
1395 | |
1396 | |
1397 //Supports threadsave copying!!! | |
1398 void copyTimeData(void) | |
1399 { | |
1400 extern RTC_HandleTypeDef RTCHandle; | |
1401 | |
1402 uint8_t boolTimeData = !global.dataSendToMaster.boolTimeData; | |
1403 global.dataSendToMaster.data[boolTimeData].localtime_rtc_tr = (uint32_t)(RTCHandle.Instance->TR & RTC_TR_RESERVED_MASK); | |
1404 global.dataSendToMaster.data[boolTimeData].localtime_rtc_dr = (uint32_t)(RTCHandle.Instance->DR & RTC_DR_RESERVED_MASK); | |
1405 global.dataSendToMaster.data[boolTimeData].divetime_seconds = (uint32_t)global.lifeData.dive_time_seconds; | |
1406 global.dataSendToMaster.data[boolTimeData].dive_time_seconds_without_surface_time = (uint32_t)global.lifeData.dive_time_seconds_without_surface_time; | |
1407 global.dataSendToMaster.data[boolTimeData].surfacetime_seconds = (uint32_t)global.seconds_since_last_dive; | |
1408 global.dataSendToMaster.data[boolTimeData].counterSecondsShallowDepth = (uint32_t)global.lifeData.counterSecondsShallowDepth; | |
1409 global.dataSendToMaster.boolTimeData = boolTimeData; | |
1410 } | |
1411 | |
1412 | |
1413 //Supports threadsave copying!!! | |
1414 void copyCompassData(void) | |
1415 { | |
1416 extern float compass_heading; | |
1417 extern float compass_roll; | |
1418 extern float compass_pitch; | |
1419 //uint8_t dataSendToMaster. | |
1420 uint8_t boolCompassData = !global.dataSendToMaster.boolCompassData; | |
1421 global.dataSendToMaster.data[boolCompassData].compass_heading = compass_heading; | |
1422 global.dataSendToMaster.data[boolCompassData].compass_roll = compass_roll; | |
1423 global.dataSendToMaster.data[boolCompassData].compass_pitch = compass_pitch; | |
1424 global.dataSendToMaster.data[boolCompassData].compass_DX_f = 0; | |
1425 global.dataSendToMaster.data[boolCompassData].compass_DY_f = 0; | |
1426 global.dataSendToMaster.data[boolCompassData].compass_DZ_f = 0; | |
1427 global.dataSendToMaster.data[boolCompassData].compass_uTick = HAL_GetTick(); | |
1428 global.dataSendToMaster.boolCompassData = boolCompassData; | |
1429 } | |
1430 | |
1431 | |
1432 void copyCompassDataDuringCalibration(int16_t dx, int16_t dy, int16_t dz) | |
1433 { | |
1434 extern float compass_heading; | |
1435 extern float compass_roll; | |
1436 extern float compass_pitch; | |
1437 //uint8_t dataSendToMaster. | |
1438 uint8_t boolCompassData = !global.dataSendToMaster.boolCompassData; | |
1439 global.dataSendToMaster.data[boolCompassData].compass_heading = compass_heading; | |
1440 global.dataSendToMaster.data[boolCompassData].compass_roll = compass_roll; | |
1441 global.dataSendToMaster.data[boolCompassData].compass_pitch = compass_pitch; | |
1442 global.dataSendToMaster.data[boolCompassData].compass_DX_f = dx; | |
1443 global.dataSendToMaster.data[boolCompassData].compass_DY_f = dy; | |
1444 global.dataSendToMaster.data[boolCompassData].compass_DZ_f = dz; | |
1445 global.dataSendToMaster.boolCompassData = boolCompassData; | |
1446 } | |
1447 | |
1448 | |
1449 //Supports threadsave copying!!! | |
1450 void copyBatteryData(void) | |
1451 { | |
1452 uint8_t boolBatteryData = !global.dataSendToMaster.boolBatteryData; | |
1453 global.dataSendToMaster.data[boolBatteryData].battery_voltage = get_voltage(); | |
1454 global.dataSendToMaster.data[boolBatteryData].battery_charge= get_charge(); | |
1455 global.dataSendToMaster.boolBatteryData = boolBatteryData; | |
1456 } | |
1457 | |
1458 | |
1459 //Supports threadsave copying!!! | |
1460 void copyAmbientLightData(void) | |
1461 { | |
1462 uint8_t boolAmbientLightData = !global.dataSendToMaster.boolAmbientLightData; | |
1463 global.dataSendToMaster.data[boolAmbientLightData].ambient_light_level = get_ambient_light_level(); | |
1464 global.dataSendToMaster.boolAmbientLightData = boolAmbientLightData; | |
1465 } | |
1466 | |
1467 | |
1468 //Supports threadsave copying!!! | |
1469 void copyTissueData(void) | |
1470 { | |
1471 //uint8_t dataSendToMaster. | |
1472 uint8_t boolTisssueData = !global.dataSendToMaster.boolTisssueData; | |
1473 for(int i = 0; i < 16; i++) | |
1474 { | |
1475 global.dataSendToMaster.data[boolTisssueData].tissue_nitrogen_bar[i] = global.lifeData.tissue_nitrogen_bar[i]; | |
1476 global.dataSendToMaster.data[boolTisssueData].tissue_helium_bar[i] = global.lifeData.tissue_helium_bar[i]; | |
1477 } | |
1478 global.dataSendToMaster.boolTisssueData = boolTisssueData; | |
1479 } | |
1480 | |
1481 | |
1482 //Supports threadsave copying!!! | |
1483 void copyVpmCrushingData(void) | |
1484 { | |
1485 //uint8_t dataSendToMaster. | |
1486 uint8_t boolCrushingData = !global.dataSendToMaster.boolCrushingData; | |
1487 for(int i = 0; i < 16; i++) | |
1488 { | |
1489 global.dataSendToMaster.data[boolCrushingData].max_crushing_pressure_n2[i] = global.vpm.max_crushing_pressure_n2[i]; | |
1490 global.dataSendToMaster.data[boolCrushingData].max_crushing_pressure_he[i] = global.vpm.max_crushing_pressure_he[i]; | |
1491 global.dataSendToMaster.data[boolCrushingData].adjusted_critical_radius_he[i] = global.vpm.adjusted_critical_radius_he[i]; | |
1492 global.dataSendToMaster.data[boolCrushingData].adjusted_critical_radius_n2[i] = global.vpm.adjusted_critical_radius_n2[i]; | |
1493 } | |
1494 global.dataSendToMaster.boolCrushingData = boolCrushingData; | |
1495 } | |
1496 | |
1497 | |
1498 void copyDeviceData(void) | |
1499 { | |
1500 uint8_t boolDeviceData = !global.deviceDataSendToMaster.boolDeviceData; | |
1501 memcpy(&global.deviceDataSendToMaster.DeviceData[boolDeviceData], &global.deviceData,sizeof(SDevice)); | |
1502 global.deviceDataSendToMaster.boolDeviceData = boolDeviceData; | |
1503 | |
1504 global.deviceDataSendToMaster.boolVpmRepetitiveDataValid = 0; | |
1505 memcpy(&global.deviceDataSendToMaster.VpmRepetitiveData.adjusted_critical_radius_he, &global.vpm.adjusted_critical_radius_he, sizeof(16*4)); | |
1506 memcpy(&global.deviceDataSendToMaster.VpmRepetitiveData.adjusted_critical_radius_n2, &global.vpm.adjusted_critical_radius_n2, sizeof(16*4)); | |
1507 memcpy(&global.deviceDataSendToMaster.VpmRepetitiveData.adjusted_crushing_pressure_he, &global.vpm.adjusted_crushing_pressure_he, sizeof(16*4)); | |
1508 memcpy(&global.deviceDataSendToMaster.VpmRepetitiveData.adjusted_crushing_pressure_n2, &global.vpm.adjusted_crushing_pressure_n2, sizeof(16*4)); | |
1509 memcpy(&global.deviceDataSendToMaster.VpmRepetitiveData.initial_allowable_gradient_he, &global.vpm.initial_allowable_gradient_he, sizeof(16*4)); | |
1510 memcpy(&global.deviceDataSendToMaster.VpmRepetitiveData.initial_allowable_gradient_n2, &global.vpm.initial_allowable_gradient_n2, sizeof(16*4)); | |
1511 memcpy(&global.deviceDataSendToMaster.VpmRepetitiveData.max_actual_gradient, &global.vpm.max_actual_gradient, sizeof(16*4)); | |
1512 global.deviceDataSendToMaster.VpmRepetitiveData.repetitive_variables_not_valid = global.vpm.repetitive_variables_not_valid; | |
1513 global.deviceDataSendToMaster.boolVpmRepetitiveDataValid = 1; | |
1514 } | |
1515 | |
1516 void changeAgeWirelessData(void) | |
1517 { | |
1518 for(int i=0;i<4;i++) | |
1519 { | |
1520 if(global.dataSendToMaster.data[global.dataSendToMaster.boolWirelessData].wireless_data[i].ageInMilliSeconds) | |
1521 global.dataSendToMaster.data[global.dataSendToMaster.boolWirelessData].wireless_data[i].ageInMilliSeconds++; | |
1522 } | |
1523 } | |
1524 | |
1525 void copyWirelessData(void) | |
1526 { | |
1527 uint8_t boolWirelessData = !global.dataSendToMaster.boolWirelessData; | |
1528 SDataWireless *dataOld, *dataNew; | |
1529 for(int i=0;i<3;i++) | |
1530 { | |
1531 dataNew = &global.dataSendToMaster.data[boolWirelessData].wireless_data[i+1]; | |
1532 dataOld = &global.dataSendToMaster.data[!boolWirelessData].wireless_data[i]; | |
1533 dataNew->ageInMilliSeconds = dataOld->ageInMilliSeconds; | |
1534 dataNew->numberOfBytes = dataOld->numberOfBytes; | |
1535 dataNew->status = dataOld->status; | |
1536 memcpy(dataNew->data, dataOld->data,8); | |
1537 } | |
1538 | |
1539 global.dataSendToMaster.data[boolWirelessData].wireless_data[0].ageInMilliSeconds = 1; | |
1540 global.dataSendToMaster.data[boolWirelessData].wireless_data[0].numberOfBytes = global.wirelessReceived; | |
1541 global.dataSendToMaster.data[boolWirelessData].wireless_data[0].status = global.wirelessConfidenceStatus; | |
1542 for(int i=0;i<MAX_WIRELESS_BYTES;i++) | |
1543 global.dataSendToMaster.data[boolWirelessData].wireless_data[0].data[i] = global.wirelessdata[i]; | |
1544 for(int i=MAX_WIRELESS_BYTES;i<12;i++) | |
1545 global.dataSendToMaster.data[boolWirelessData].wireless_data[0].data[i] = 0xFF; | |
1546 | |
1547 global.dataSendToMaster.boolWirelessData = boolWirelessData; | |
1548 } | |
1549 | |
1550 /* copyPICdata(); is used in spi.c */ | |
1551 void copyPICdata(void) | |
1552 { | |
1553 uint8_t boolPICdata = !global.dataSendToMaster.boolPICdata; | |
1554 for(int i = 0; i < 3; i++) | |
1555 { | |
1556 global.dataSendToMaster.data[boolPICdata].button_setting[i] = global.ButtonPICdata[i]; | |
1557 } | |
1558 global.dataSendToMaster.boolPICdata = boolPICdata; | |
1559 } | |
1560 | |
1561 | |
1562 | |
1563 | |
1564 typedef enum | |
1565 { | |
1566 SPI3_OK = 0x00, | |
1567 SPI3_DEINIT = 0x01, | |
1568 } SPI3_StatusTypeDef; | |
1569 /* if spi3 is running and the SPI3_ButtonAdjust call returns OK, all is fine | |
1570 if the SPI3_ButtonAdjust call returns error, the spi3 is DeInit | |
1571 and will be init the next call of scheduleSetButtonResponsiveness() | |
1572 and data will be send again on the third call | |
1573 therefore on return 0 of scheduleSetButtonResponsiveness() the caller flag should kept active | |
1574 */ | |
1575 uint8_t scheduleSetButtonResponsiveness(void) | |
1576 { | |
1577 static uint8_t SPI3status = SPI3_OK; | |
1578 | |
1579 if((SPI3status == SPI3_OK) && (SPI3_ButtonAdjust(global.ButtonResponsiveness, global.ButtonPICdata))) | |
1580 { | |
1581 copyPICdata(); | |
1582 return 1; | |
1583 } | |
1584 else | |
1585 { | |
1586 for(int i=0;i<3;i++) | |
1587 { | |
1588 global.ButtonPICdata[i] = 0xFF; | |
1589 } | |
1590 copyPICdata(); | |
1591 | |
1592 if(SPI3status == SPI3_OK) | |
1593 { | |
1594 MX_SPI3_DeInit(); | |
1595 SPI3status = SPI3_DEINIT; | |
1596 } | |
1597 else | |
1598 { | |
1599 MX_SPI3_Init(); | |
1600 SPI3status = SPI3_OK; | |
1601 } | |
1602 return 0; | |
1603 } | |
1604 } | |
1605 | |
1606 | |
1607 //save time diffenrence | |
1608 uint32_t time_elapsed_ms(uint32_t ticksstart,uint32_t ticksnow) | |
1609 { | |
1610 if(ticksstart <= ticksnow) | |
1611 { | |
1612 return ticksnow - ticksstart; | |
1613 } | |
1614 else | |
1615 { | |
1616 return 0xFFFFFFFF - ticksstart + ticksnow; | |
1617 } | |
1618 } | |
1619 | |
1620 /* same as in data_central.c */ | |
1621 _Bool is_ambient_pressure_close_to_surface(SLifeData *lifeDataCall) | |
1622 { | |
1623 if(lifeDataCall->pressure_ambient_bar < (lifeDataCall->pressure_surface_bar + 0.1f)) // hw 161121 now 1 mter, before 0.04f | |
1624 return true; | |
1625 else | |
1626 return false; | |
1627 } | |
1628 | |
1629 | |
1630 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/ | |
1631 |