Mercurial > public > ostc4
annotate Small_CPU/Src/scheduler.c @ 187:485c5135cf7f cleanup-2
cleanup: forgotten cleanup from 9da7dd50e2ec
Simply cleanup something forgotten in 9da7dd50e2ec. There is so much
duplicate code, that things are very easy to forget when cleaning up.
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
author | Jan Mulder <jlmulder@xs4all.nl> |
---|---|
date | Fri, 15 Mar 2019 12:53:14 +0100 |
parents | f11f0bf6ef2d |
children | b95741467355 |
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 "tm_stm32f4_otp.h" | |
44 | |
45 | |
135 | 46 #define INVALID_PREASURE_VALUE (100.0F) |
47 | |
38 | 48 /* Private types -------------------------------------------------------------*/ |
49 const SGas Air = {79,0,0,0,0}; | |
50 | |
51 uint8_t testarrayindex = 0; | |
52 uint32_t testarray[256]; | |
53 uint32_t testarrayMain[256]; | |
54 | |
55 /* Exported variables --------------------------------------------------------*/ | |
56 SGlobal global; | |
57 SDevice DeviceDataFlash; | |
58 uint8_t deviceDataFlashValid = 0; | |
59 uint8_t deviceDataSubSeconds = 0; | |
148
ee744c7160ce
Use SPI TX callback to synchronize to main CPU
Ideenmodellierer
parents:
142
diff
changeset
|
60 uint8_t dohardspisync = 1; |
38 | 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 copyPICdata(void); | |
90 uint16_t schedule_update_timer_helper(int8_t thisSeconds); | |
91 | |
92 | |
93 uint32_t time_elapsed_ms(uint32_t ticksstart,uint32_t ticksnow); | |
94 | |
95 _Bool scheduleCheck_pressure_reached_dive_mode_level(void); | |
96 void scheduleSetDate(SDeviceLine *line); | |
97 | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
98 extern void SPI_Evaluate_RX_Data(); |
38 | 99 /* Exported functions --------------------------------------------------------*/ |
100 | |
101 void initGlobals(void) | |
102 { | |
103 initStructWithZeero((uint8_t*) &global, sizeof(SGlobal)); | |
104 | |
105 global.dataSendToSlavePending = 0; | |
106 global.dataSendToSlaveIsValid = 1; | |
107 global.dataSendToSlaveIsNotValidCount = 0; | |
108 | |
109 global.mode = MODE_POWERUP; | |
110 global.repetitive_dive = 0; | |
111 global.conservatism = 0; | |
112 global.whichGas = 0; | |
113 global.aktualGas[0] = Air; | |
114 global.lifeData.actualGas = global.aktualGas[0]; | |
115 | |
172
c659fda83e44
Minor: Button defaults, release date adjusted, use SPI_SHOW_SYNC_STATS
heinrichsweikamp
parents:
155
diff
changeset
|
116 const uint8_t button_standard_sensitivity = 85; |
38 | 117 global.ButtonResponsiveness[0] = button_standard_sensitivity; |
118 global.ButtonResponsiveness[1] = button_standard_sensitivity; | |
119 global.ButtonResponsiveness[2] = button_standard_sensitivity; | |
120 global.ButtonResponsiveness[3] = button_standard_sensitivity; | |
121 | |
122 global.ButtonPICdata[0] = 0xFF; | |
123 global.ButtonPICdata[1] = 0xFF; | |
124 global.ButtonPICdata[2] = 0xFF; | |
125 global.ButtonPICdata[3] = 0xFF; | |
126 | |
127 global.I2C_SystemStatus = 0xFF; // 0x00 would be everything working | |
128 | |
135 | 129 global.lifeData.pressure_ambient_bar = INVALID_PREASURE_VALUE; |
130 global.lifeData.pressure_surface_bar = INVALID_PREASURE_VALUE; | |
38 | 131 decom_reset_with_1000mbar(&global.lifeData); |
132 | |
133 global.demo_mode = 0; | |
134 | |
135 for(int i = 0; i < MAX_SENSORS; i++) | |
136 { | |
137 global.sensorError[i] = HAL_OK; // HAL_OK = 0; | |
138 } | |
139 | |
140 global.dataSendToMaster.RTE_VERSION_high = firmwareVersionHigh();//RTE_VERSION_HIGH;; | |
141 global.dataSendToMaster.RTE_VERSION_low = firmwareVersionLow();//RTE_VERSION_LOW;; | |
142 global.dataSendToMaster.chargeStatus = 0; | |
143 | |
144 global.dataSendToMaster.power_on_reset = 1; | |
145 global.dataSendToMaster.header.checkCode[0] = 0xA1; | |
146 global.dataSendToMaster.header.checkCode[1] = 0xA2; | |
147 global.dataSendToMaster.header.checkCode[2] = 0xA3; | |
148 global.dataSendToMaster.header.checkCode[3] = 0xA4; | |
149 global.dataSendToMaster.footer.checkCode[3] = 0xE4; | |
150 global.dataSendToMaster.footer.checkCode[2] = 0xE3; | |
151 global.dataSendToMaster.footer.checkCode[1] = 0xE2; | |
152 global.dataSendToMaster.footer.checkCode[0] = 0xE1; | |
153 global.dataSendToMaster.sensorErrors = 0; | |
154 | |
155 global.sync_error_count = 0; | |
156 global.check_sync_not_running = 0; | |
157 | |
158 global.deviceDataSendToMaster.RTE_VERSION_high = firmwareVersionHigh();//RTE_VERSION_HIGH; | |
159 global.deviceDataSendToMaster.RTE_VERSION_low = firmwareVersionLow();//RTE_VERSION_LOW; | |
160 global.deviceDataSendToMaster.chargeStatus = 0; | |
161 | |
162 global.deviceDataSendToMaster.power_on_reset = 1; | |
163 global.deviceDataSendToMaster.header.checkCode[0] = 0xDF; | |
164 global.deviceDataSendToMaster.header.checkCode[1] = 0xDE; | |
165 global.deviceDataSendToMaster.header.checkCode[2] = 0xDD; | |
166 global.deviceDataSendToMaster.header.checkCode[3] = 0xDC; | |
167 global.deviceDataSendToMaster.footer.checkCode[3] = 0xE4; | |
168 global.deviceDataSendToMaster.footer.checkCode[2] = 0xE3; | |
169 global.deviceDataSendToMaster.footer.checkCode[1] = 0xE2; | |
170 global.deviceDataSendToMaster.footer.checkCode[0] = 0xE1; | |
171 | |
172 global.dataSendToSlave.getDeviceDataNow = 0; | |
173 | |
174 global.deviceData.batteryChargeCompleteCycles.value_int32 = 0; | |
175 global.deviceData.batteryChargeCycles.value_int32 = 0; | |
176 global.deviceData.depthMaximum.value_int32 = 0; | |
177 global.deviceData.diveCycles.value_int32 = 0; | |
178 global.deviceData.hoursOfOperation.value_int32 = 0; | |
179 global.deviceData.temperatureMaximum.value_int32 = INT32_MIN; | |
180 global.deviceData.temperatureMinimum.value_int32 = INT32_MAX; | |
181 global.deviceData.voltageMinimum.value_int32 = INT32_MAX; | |
148
ee744c7160ce
Use SPI TX callback to synchronize to main CPU
Ideenmodellierer
parents:
142
diff
changeset
|
182 |
ee744c7160ce
Use SPI TX callback to synchronize to main CPU
Ideenmodellierer
parents:
142
diff
changeset
|
183 dohardspisync = 1; |
38 | 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 { | |
155
4fd8bbc7d841
Do hard sync after communication timeout > 1 second
Ideenmodellierer
parents:
148
diff
changeset
|
432 /* counter is incremented in cyclic 100ms loop and reset to 0 if the transmission complete callback is called */ |
148
ee744c7160ce
Use SPI TX callback to synchronize to main CPU
Ideenmodellierer
parents:
142
diff
changeset
|
433 if((global.check_sync_not_running >= 3)) |
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 } |
155
4fd8bbc7d841
Do hard sync after communication timeout > 1 second
Ideenmodellierer
parents:
148
diff
changeset
|
444 if((global.check_sync_not_running == 10)) /* connection lost for about a second. Could be debugging or Firmware update */ |
4fd8bbc7d841
Do hard sync after communication timeout > 1 second
Ideenmodellierer
parents:
148
diff
changeset
|
445 { |
4fd8bbc7d841
Do hard sync after communication timeout > 1 second
Ideenmodellierer
parents:
148
diff
changeset
|
446 dohardspisync = 1; |
4fd8bbc7d841
Do hard sync after communication timeout > 1 second
Ideenmodellierer
parents:
148
diff
changeset
|
447 } |
38 | 448 } |
449 | |
450 | |
451 /** | |
452 ****************************************************************************** | |
453 * @brief scheduleDiveMode. / Dive Mode: Main Loop | |
454 * @author Peter Ryser | |
455 * @version V0.0.1 | |
456 * @date 22-April-2014 | |
457 ****************************************************************************** | |
458 */ | |
459 void scheduleDiveMode(void) | |
460 { | |
461 uint32_t ticksdiff = 0; | |
462 uint32_t lasttick = 0; | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
463 |
38 | 464 uint8_t counterAscentRate = 0; |
465 float lastPressure_bar = 0.0f; | |
466 global.dataSendToMaster.mode = MODE_DIVE; | |
467 global.deviceDataSendToMaster.mode = MODE_DIVE; | |
468 //uint16_t counterSecondsShallowDepth = 0; | |
469 uint8_t counter_exit = 0; | |
470 | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
471 Scheduler.tickstart = HAL_GetTick() - 1000; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
472 Scheduler.counterSPIdata100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
473 Scheduler.counterCompass100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
474 Scheduler.counterPressure100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
475 Scheduler.counterAmbientLight100msec = 0; |
38 | 476 |
477 global.deviceData.diveCycles.value_int32++; | |
478 scheduleSetDate(&global.deviceData.diveCycles); | |
479 global.lifeData.counterSecondsShallowDepth = 0; | |
480 | |
481 while(global.mode == MODE_DIVE) | |
482 { | |
483 schedule_check_resync(); | |
484 lasttick = HAL_GetTick(); | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
485 ticksdiff = time_elapsed_ms(Scheduler.tickstart,lasttick); |
38 | 486 |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
487 if(ticksdiff >= Scheduler.counterSPIdata100msec * 100 + 10) |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
488 { |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
489 SPI_Evaluate_RX_Data(); |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
490 Scheduler.counterSPIdata100msec++; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
491 } |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
492 |
38 | 493 //Evaluate pressure at 20 ms, 120 ms, 220 ms,.... |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
494 if(ticksdiff >= Scheduler.counterPressure100msec * 100 + 20) |
38 | 495 { |
496 global.check_sync_not_running++; | |
135 | 497 pressure_update(); |
498 scheduleUpdateDeviceData(); | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
181
diff
changeset
|
499 #ifdef DEMOMODE |
38 | 500 if(global.demo_mode) |
501 { | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
181
diff
changeset
|
502 int turbo_seconds = demo_modify_temperature_and_pressure(global.lifeData.dive_time_seconds, Scheduler.counterPressure100msec, global.ceiling_from_main_CPU_mbar); |
38 | 503 if(turbo_seconds) |
504 { | |
505 global.lifeData.dive_time_seconds += turbo_seconds; | |
506 decom_tissues_exposure((int)(turbo_seconds), &global.lifeData); | |
507 copyTissueData(); | |
508 } | |
509 if((global.lifeData.counterSecondsShallowDepth > 1) && (global.lifeData.counterSecondsShallowDepth < (global.settings.timeoutDiveReachedZeroDepth - 10))) | |
510 global.lifeData.counterSecondsShallowDepth = (global.settings.timeoutDiveReachedZeroDepth - 10); | |
511 } | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
181
diff
changeset
|
512 #endif |
38 | 513 |
514 //Calc ascentrate every two second (20 * 100 ms) | |
515 counterAscentRate++; | |
516 if(counterAscentRate == 20) | |
517 { | |
518 global.lifeData.pressure_ambient_bar = get_pressure_mbar() / 1000.0f; | |
519 if(lastPressure_bar >= 0) | |
520 { | |
521 //2 seconds * 30 == 1 minute, bar * 10 = meter | |
522 global.lifeData.ascent_rate_meter_per_min = (lastPressure_bar - global.lifeData.pressure_ambient_bar) * 30 * 10; | |
523 } | |
524 lastPressure_bar = global.lifeData.pressure_ambient_bar; | |
525 counterAscentRate = 0; | |
526 } | |
135 | 527 copyPressureData(); |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
528 Scheduler.counterPressure100msec++; |
38 | 529 } |
530 //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
|
531 if(ticksdiff >= Scheduler.counterCompass100msec * 100 + 50) |
135 | 532 { |
533 compass_read(); | |
534 acceleration_read(); | |
535 compass_calc(); | |
536 copyCompassData(); | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
537 Scheduler.counterCompass100msec++; |
135 | 538 } |
38 | 539 |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
540 if(ticksdiff >= Scheduler.counterAmbientLight100msec * 100 + 70) |
38 | 541 { |
542 adc_ambient_light_sensor_get_data(); | |
543 copyAmbientLightData(); | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
544 Scheduler.counterAmbientLight100msec++; |
38 | 545 } |
546 | |
547 //Evaluate tissues, toxic data, vpm, etc. once a second | |
548 if(ticksdiff >= 1000) | |
549 { | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
550 /* reset counter */ |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
551 Scheduler.tickstart = HAL_GetTick(); |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
552 |
38 | 553 if(global.dataSendToSlave.diveModeInfo != DIVEMODE_Apnea) |
554 { | |
555 scheduleUpdateLifeData(0); // includes tissues | |
556 global.lifeData.dive_time_seconds++; // there is dive_time_seconds_without_surface_time too | |
557 global.lifeData.ppO2 = decom_calc_ppO2(global.lifeData.pressure_ambient_bar, &global.lifeData.actualGas); | |
558 decom_oxygen_calculate_cns(&global.lifeData.cns,global.lifeData.ppO2); | |
559 decom_oxygen_calculate_otu(&global.lifeData.otu,global.lifeData.ppO2); | |
88 | 560 battery_gas_gauge_get_data(); |
38 | 561 |
562 | |
563 /** counter_exit allows safe exit via button for testing | |
564 * and demo_mode is exited too if aplicable. | |
565 */ | |
566 if(global.dataSendToMaster.mode == MODE_ENDDIVE) | |
567 { | |
568 counter_exit++; | |
569 if(counter_exit >= 2) | |
570 { | |
571 global.mode = MODE_SURFACE; | |
572 global.demo_mode = 0; | |
573 } | |
574 } | |
575 | |
576 if(is_ambient_pressure_close_to_surface(&global.lifeData)) | |
577 { | |
578 global.lifeData.counterSecondsShallowDepth++; | |
579 if((global.lifeData.counterSecondsShallowDepth >= global.settings.timeoutDiveReachedZeroDepth) || ((global.lifeData.dive_time_seconds < 60) && (global.demo_mode == 0)) || (global.dataSendToSlave.setEndDive)) | |
580 { | |
581 global.seconds_since_last_dive = 1; // start counter | |
582 schedule_update_timer_helper(0); // zum starten :-) | |
583 global.dataSendToMaster.mode = MODE_ENDDIVE; | |
584 global.deviceDataSendToMaster.mode = MODE_ENDDIVE; | |
585 } | |
586 } | |
587 else | |
588 { | |
589 global.lifeData.counterSecondsShallowDepth = 0; | |
590 global.lifeData.dive_time_seconds_without_surface_time++; | |
591 } | |
592 vpm_crush2(); | |
593 } | |
594 else // DIVEMODE_Apnea | |
595 { | |
596 global.lifeData.dive_time_seconds++; | |
597 | |
598 // exit dive mode | |
599 if(global.dataSendToMaster.mode == MODE_ENDDIVE) | |
600 { | |
601 counter_exit++; | |
602 if(counter_exit >= 2) | |
603 { | |
604 scheduleUpdateLifeData(-1); // 'restart' tissue calculations without calculating time during apnea mode | |
605 global.lifeData.dive_time_seconds = 0; // use backup noflytime and desaturation time | |
606 global.mode = MODE_SURFACE; | |
607 global.demo_mode = 0; | |
608 } | |
609 } | |
610 | |
611 // surface break | |
612 if(is_ambient_pressure_close_to_surface(&global.lifeData)) | |
613 { | |
614 global.lifeData.counterSecondsShallowDepth++; | |
615 if(global.lifeData.counterSecondsShallowDepth > 3) // time for main cpu to copy to apnea_last_dive_time_seconds | |
616 { | |
617 global.lifeData.dive_time_seconds = 0; // this apnea dive ends here | |
618 } | |
619 if((global.lifeData.counterSecondsShallowDepth >= global.settings.timeoutDiveReachedZeroDepth) || (global.dataSendToSlave.setEndDive)) | |
620 { | |
621 global.dataSendToMaster.mode = MODE_ENDDIVE; | |
622 global.deviceDataSendToMaster.mode = MODE_ENDDIVE; | |
623 } | |
624 } | |
625 else | |
626 { | |
627 global.lifeData.counterSecondsShallowDepth = 0; | |
628 global.lifeData.dive_time_seconds_without_surface_time++; | |
629 } | |
630 } // standard dive or DIVEMODE_Apnea | |
631 | |
88 | 632 copyVpmCrushingData(); |
633 copyTimeData(); | |
634 copyCnsAndOtuData(); | |
635 copyBatteryData(); | |
38 | 636 |
88 | 637 // new hw 170523 |
638 if(global.I2C_SystemStatus != HAL_OK) | |
639 { | |
640 MX_I2C1_TestAndClear(); | |
641 MX_I2C1_Init(); | |
642 if(!is_init_pressure_done()) | |
643 { | |
644 init_pressure(); | |
645 } | |
646 } | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
647 Scheduler.counterSPIdata100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
648 Scheduler.counterCompass100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
649 Scheduler.counterPressure100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
650 Scheduler.counterAmbientLight100msec = 0; |
38 | 651 } |
652 } | |
653 } | |
654 | |
655 | |
656 /** | |
657 ****************************************************************************** | |
658 * @brief scheduleSurfaceMode / surface mode: Main Loop | |
659 * @author Peter Ryser | |
660 * @version V0.0.1 | |
661 * @date 22-April-2014 | |
662 ****************************************************************************** | |
663 */ | |
664 | |
665 | |
666 // =============================================================================== | |
667 // scheduleTestMode | |
668 /// @brief included for sealed hardware with permanent RTE update message | |
669 // =============================================================================== | |
670 void scheduleTestMode(void) | |
671 { | |
672 uint32_t ticksdiff = 0; | |
673 uint32_t lasttick = 0; | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
674 Scheduler.tickstart = HAL_GetTick(); |
38 | 675 |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
676 Scheduler.counterPressure100msec = 0; |
38 | 677 |
678 float temperature_carousel = 0.0f; | |
679 float temperature_changer = 0.1f; | |
680 | |
681 while(global.mode == MODE_TEST) | |
682 { | |
683 schedule_check_resync(); | |
684 lasttick = HAL_GetTick(); | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
685 ticksdiff = time_elapsed_ms(Scheduler.tickstart,lasttick); |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
686 |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
687 //Evaluate received data at 10 ms, 110 ms, 210 ms,... |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
688 if(ticksdiff >= Scheduler.counterSPIdata100msec * 100 + 10) |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
689 { |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
690 SPI_Evaluate_RX_Data(); |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
691 Scheduler.counterSPIdata100msec++; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
692 } |
38 | 693 |
694 //Evaluate pressure at 20 ms, 120 ms, 220 ms,... | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
695 if(ticksdiff >= Scheduler.counterPressure100msec * 100 + 20) |
38 | 696 { |
697 global.check_sync_not_running++; | |
698 | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
699 pressure_update(); |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
700 scheduleUpdateDeviceData(); |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
701 global.lifeData.ascent_rate_meter_per_min = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
702 copyPressureData(); |
38 | 703 |
704 if(temperature_carousel > 20.0f) | |
705 { | |
706 temperature_carousel = 20.0f; | |
707 temperature_changer = -0.1f; | |
708 } | |
709 else | |
710 if(temperature_carousel < 0) | |
711 { | |
712 temperature_carousel = 0; | |
713 temperature_changer = +0.1f; | |
714 } | |
715 | |
716 temperature_carousel += temperature_changer; | |
717 | |
718 uint8_t boolPressureData = !global.dataSendToMaster.boolPressureData; | |
719 | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
720 global.dataSendToMaster.data[boolPressureData].pressure_mbar = get_pressure_mbar(); |
38 | 721 |
722 global.dataSendToMaster.data[boolPressureData].temperature = temperature_carousel; | |
723 global.dataSendToMaster.data[boolPressureData].pressure_uTick = HAL_GetTick(); | |
724 global.dataSendToMaster.boolPressureData = boolPressureData; | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
725 Scheduler.counterPressure100msec++; |
38 | 726 } |
727 | |
728 if(ticksdiff >= 1000) | |
729 { | |
730 //Set back tick counter | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
731 Scheduler.tickstart = HAL_GetTick(); |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
732 Scheduler.counterPressure100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
733 Scheduler.counterSPIdata100msec = 0; |
38 | 734 } |
735 }; | |
736 } | |
737 | |
738 | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
739 |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
740 |
38 | 741 void scheduleSurfaceMode(void) |
742 { | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
743 |
38 | 744 uint32_t ticksdiff = 0; |
745 uint32_t lasttick = 0; | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
746 Scheduler.tickstart = HAL_GetTick(); |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
747 Scheduler.counterSPIdata100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
748 Scheduler.counterCompass100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
749 Scheduler.counterPressure100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
750 Scheduler.counterAmbientLight100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
751 |
38 | 752 global.dataSendToMaster.mode = MODE_SURFACE; |
753 global.deviceDataSendToMaster.mode = MODE_SURFACE; | |
754 | |
755 while(global.mode == MODE_SURFACE) | |
756 { | |
120 | 757 /* printf("surface...\n"); */ |
89 | 758 // SPI_Start_single_TxRx_with_Master(); |
38 | 759 schedule_check_resync(); |
760 lasttick = HAL_GetTick(); | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
761 ticksdiff = time_elapsed_ms(Scheduler.tickstart,lasttick); |
38 | 762 |
763 if(setButtonsNow == 1) | |
764 { | |
765 if(scheduleSetButtonResponsiveness()) | |
766 setButtonsNow = 0; | |
767 } | |
768 | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
769 |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
770 //Evaluate received data at 10 ms, 110 ms, 210 ms,... |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
771 if(ticksdiff >= Scheduler.counterSPIdata100msec * 100 + 10) |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
772 { |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
773 SPI_Evaluate_RX_Data(); |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
774 Scheduler.counterSPIdata100msec++; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
775 } |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
776 |
38 | 777 //Evaluate pressure at 20 ms, 120 ms, 220 ms,... |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
778 if(ticksdiff >= Scheduler.counterPressure100msec * 100 + 20) |
38 | 779 { |
780 global.check_sync_not_running++; | |
135 | 781 pressure_update(); |
782 scheduleUpdateDeviceData(); | |
38 | 783 global.lifeData.ascent_rate_meter_per_min = 0; |
135 | 784 copyPressureData(); |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
785 Scheduler.counterPressure100msec++; |
135 | 786 |
38 | 787 if(scheduleCheck_pressure_reached_dive_mode_level()) |
788 global.mode = MODE_DIVE; | |
789 } | |
790 | |
791 //evaluate compass data at 50 ms, 150 ms, 250 ms,... | |
792 | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
793 if(ticksdiff >= Scheduler.counterCompass100msec * 100 + 50) |
135 | 794 { |
795 compass_read(); | |
796 acceleration_read(); | |
797 compass_calc(); | |
798 copyCompassData(); | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
799 Scheduler.counterCompass100msec++; |
135 | 800 } |
38 | 801 |
802 //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
|
803 if(ticksdiff >= Scheduler.counterAmbientLight100msec * 100 + 70) |
38 | 804 { |
805 adc_ambient_light_sensor_get_data(); | |
806 copyAmbientLightData(); | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
807 Scheduler.counterAmbientLight100msec++; |
38 | 808 } |
89 | 809 //Evaluate tissues, toxic data, etc. once a second |
38 | 810 if(ticksdiff >= 1000) |
811 { | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
812 //Set back tick counter |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
813 Scheduler.tickstart = HAL_GetTick(); |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
814 |
38 | 815 if(clearDecoNow) |
816 { | |
817 decom_reset_with_1000mbar(&global.lifeData); ///< this should almost reset desaturation time | |
818 // new 160215 hw | |
819 global.repetitive_dive = 0; | |
820 global.seconds_since_last_dive = 0; ///< this will reset OTU and CNS as well | |
821 global.no_fly_time_minutes = 0; | |
822 global.accidentFlag = 0; | |
823 global.accidentRemainingSeconds = 0; | |
824 vpm_init(&global.vpm, global.conservatism, global.repetitive_dive, global.seconds_since_last_dive); | |
825 clearDecoNow = 0; | |
826 } | |
89 | 827 |
38 | 828 if(global.seconds_since_last_dive) |
829 { | |
830 schedule_update_timer_helper(-1); | |
831 // global.seconds_since_last_dive++; | |
832 // if(global.seconds_since_last_dive > 777900) // a bit more than nine days [seconds] | |
833 // global.seconds_since_last_dive = 0; | |
834 } | |
89 | 835 |
38 | 836 if(global.accidentRemainingSeconds) |
837 { | |
838 global.accidentRemainingSeconds--; | |
839 if(!global.accidentRemainingSeconds) | |
840 global.accidentFlag = 0; | |
841 } | |
842 global.dataSendToMaster.accidentFlags = global.accidentFlag; | |
89 | 843 |
38 | 844 update_surface_pressure(1); |
845 scheduleUpdateLifeData(0); | |
846 decom_oxygen_calculate_otu_degrade(&global.lifeData.otu, global.seconds_since_last_dive); | |
847 decom_oxygen_calculate_cns_degrade(&global.lifeData.cns, global.seconds_since_last_dive); | |
135 | 848 |
849 /* start desaturation calculation after first valid measurement has been done */ | |
850 if(global.lifeData.pressure_surface_bar != INVALID_PREASURE_VALUE) | |
851 { | |
852 global.lifeData.desaturation_time_minutes = decom_calc_desaturation_time(global.lifeData.tissue_nitrogen_bar,global.lifeData.tissue_helium_bar,global.lifeData.pressure_surface_bar); | |
853 } | |
854 else | |
855 { | |
856 global.lifeData.desaturation_time_minutes = 0; | |
857 } | |
38 | 858 battery_charger_get_status_and_contral_battery_gas_gauge(0); |
88 | 859 battery_gas_gauge_get_data(); |
89 | 860 |
88 | 861 copyCnsAndOtuData(); |
862 copyTimeData(); | |
863 copyBatteryData(); | |
864 copyDeviceData(); | |
38 | 865 |
88 | 866 // new hw 170523 |
867 if(global.I2C_SystemStatus != HAL_OK) | |
868 { | |
869 MX_I2C1_TestAndClear(); | |
870 MX_I2C1_Init(); | |
871 if(!is_init_pressure_done()) | |
872 { | |
873 init_pressure(); | |
874 } | |
875 } | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
876 Scheduler.counterSPIdata100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
877 Scheduler.counterCompass100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
878 Scheduler.counterPressure100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
879 Scheduler.counterAmbientLight100msec = 0; |
38 | 880 } |
881 } | |
882 } | |
883 | |
142
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
884 void HardSyncToSPI() |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
885 { |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
886 if(dohardspisync) |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
887 { |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
888 //Set back tick counter |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
889 Scheduler.tickstart = HAL_GetTick(); |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
890 Scheduler.counterSPIdata100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
891 Scheduler.counterCompass100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
892 Scheduler.counterPressure100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
893 Scheduler.counterAmbientLight100msec = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
894 dohardspisync = 0; |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
895 } |
69f4b8067daa
Use one global structure for all schedule counters
Ideenmodellierer
parents:
135
diff
changeset
|
896 } |
38 | 897 |
898 /** | |
899 ****************************************************************************** | |
900 * @brief scheduleCompassCalibrationMode | |
901 * @author heinrichs weikamp gmbh | |
902 * @version V0.0.1 | |
903 * @since 31-March-2015 | |
904 * @date 31-March-2015 | |
905 ****************************************************************************** | |
906 */ | |
907 void scheduleCompassCalibrationMode(void) | |
908 { | |
909 compass_init(1,7); // fast mode, max gain | |
910 compass_calib(); // duration : 1 minute! | |
911 compass_init(0,7); // back to normal mode | |
912 | |
913 if(global.seconds_since_last_dive) | |
914 { | |
915 schedule_update_timer_helper(-1); | |
916 // global.seconds_since_last_dive += 60; | |
917 // if(global.seconds_since_last_dive > 777900) // a bit more than nine days [seconds] | |
918 // global.seconds_since_last_dive = 0; | |
919 } | |
920 | |
921 scheduleUpdateLifeData(0); | |
922 global.mode = MODE_SURFACE; | |
923 } | |
924 | |
925 | |
926 /** | |
927 ****************************************************************************** | |
928 * @brief scheduleSleepMode / sleep mode: Main Loop | |
929 * @author heinrichs weikamp gmbh | |
930 * @version V0.0.2 | |
931 * @since 31-March-2015 | |
932 * @date 22-April-2014 | |
933 ****************************************************************************** | |
934 */ | |
935 | |
936 void scheduleSleepMode(void) | |
937 { | |
938 global.dataSendToMaster.mode = 0; | |
939 global.deviceDataSendToMaster.mode = 0; | |
940 | |
941 /* prevent button wake up problem while in sleep_prepare | |
942 * sleep prepare does I2C_DeInit() | |
943 */ | |
944 if(global.mode != MODE_SLEEP) | |
945 MX_I2C1_Init(); | |
946 else | |
947 do | |
948 { | |
949 I2C_DeInit(); | |
950 | |
951 #ifdef DEBUGMODE | |
952 HAL_Delay(2000); | |
953 #else | |
954 RTC_StopMode_2seconds(); | |
955 #endif | |
956 | |
957 | |
958 | |
959 if(global.mode == MODE_SLEEP) | |
960 secondsCount += 2; | |
961 | |
962 MX_I2C1_Init(); | |
963 pressure_sensor_get_pressure_raw(); | |
964 | |
965 if(secondsCount >= 30) | |
966 { | |
967 pressure_sensor_get_temperature_raw(); | |
968 battery_gas_gauge_get_data(); | |
969 // ReInit_battery_charger_status_pins(); | |
970 battery_charger_get_status_and_contral_battery_gas_gauge(1); | |
971 // DeInit_battery_charger_status_pins(); | |
972 secondsCount = 0; | |
973 } | |
974 | |
975 pressure_calculation(); | |
976 | |
977 scheduleUpdateDeviceData(); | |
978 update_surface_pressure(2); | |
979 | |
980 if(global.seconds_since_last_dive) | |
981 { | |
982 schedule_update_timer_helper(-1); | |
983 // global.seconds_since_last_dive += 2; | |
984 // if(global.seconds_since_last_dive > 777900) // a bit more than nine days [seconds] | |
985 // global.seconds_since_last_dive = 0; | |
986 } | |
987 | |
988 if(global.accidentRemainingSeconds) | |
989 { | |
990 if(global.accidentRemainingSeconds > 2) | |
991 global.accidentRemainingSeconds -= 2; | |
992 else | |
993 { | |
994 global.accidentRemainingSeconds = 0; | |
995 global.accidentFlag = 0; | |
996 } | |
997 } | |
998 | |
999 if(scheduleCheck_pressure_reached_dive_mode_level()) | |
1000 global.mode = MODE_BOOT; | |
1001 | |
1002 scheduleUpdateLifeData(2000); | |
1003 } | |
1004 while(global.mode == MODE_SLEEP); | |
1005 /* new section for system after Standby */ | |
1006 scheduleUpdateLifeData(-1); | |
1007 clearDecoNow = 0; | |
1008 setButtonsNow = 0; | |
1009 } | |
1010 | |
1011 | |
1012 | |
1013 /* Private functions ---------------------------------------------------------*/ | |
1014 | |
1015 | |
1016 /** | |
1017 ****************************************************************************** | |
1018 * @brief scheduleCheck_pressure_reached_dive_mode_level | |
1019 * @author heinrichs weikamp gmbh | |
1020 * @version V0.0.1 from inline code | |
1021 * @date 09-Sept-2015 | |
1022 ****************************************************************************** | |
1023 */ | |
1024 _Bool scheduleCheck_pressure_reached_dive_mode_level(void) | |
1025 { | |
1026 if(get_pressure_mbar() > 1160) | |
1027 return 1; | |
1028 else | |
1029 if((global.mode == MODE_SURFACE) && (get_pressure_mbar() > (get_surface_mbar() + 100)) && (get_surface_mbar() > 880)) | |
1030 return 1; | |
1031 else | |
1032 return 0; | |
1033 } | |
1034 | |
1035 | |
1036 /** | |
1037 ****************************************************************************** | |
1038 * @brief scheduleUpdateLifeData / calculates tissues | |
1039 * @author Peter Ryser | |
1040 * @version V0.0.1 | |
1041 * @date 22-April-2014 | |
1042 ****************************************************************************** | |
1043 */ | |
1044 | |
1045 | |
1046 void scheduleUpdateLifeData(int32_t asynchron_milliseconds_since_last) | |
1047 { | |
1048 static _Bool first = 1; | |
1049 static uint32_t tickstart = 0; | |
1050 static uint32_t ticksrest = 0; | |
1051 | |
1052 uint32_t ticksdiff = 0; | |
1053 uint32_t ticksnow = 0; | |
1054 uint32_t time_seconds = 0; | |
1055 uint8_t whichGasTmp = 0; | |
1056 | |
135 | 1057 uint8_t updateTissueData = 0; |
1058 | |
1059 | |
1060 if(global.lifeData.pressure_surface_bar == INVALID_PREASURE_VALUE) | |
1061 { | |
1062 updateTissueData = 1; | |
1063 } | |
1064 | |
38 | 1065 if(asynchron_milliseconds_since_last < 0) |
1066 { | |
1067 first = 1; | |
1068 tickstart = 0; | |
1069 ticksrest = 0; | |
1070 return; | |
1071 } | |
1072 | |
1073 if(!asynchron_milliseconds_since_last && first) | |
1074 { | |
1075 tickstart = HAL_GetTick(); | |
1076 first = 0; | |
1077 return; | |
1078 } | |
1079 | |
1080 whichGasTmp = global.whichGas; | |
1081 global.lifeData.actualGas = global.aktualGas[whichGasTmp]; | |
1082 global.lifeData.pressure_ambient_bar = get_pressure_mbar() / 1000.0f; | |
1083 global.lifeData.pressure_surface_bar = get_surface_mbar() / 1000.0f; | |
1084 | |
135 | 1085 if(updateTissueData) |
1086 { | |
1087 decom_reset_with_ambientmbar(global.lifeData.pressure_surface_bar,&global.lifeData); | |
1088 } | |
1089 | |
38 | 1090 if(!asynchron_milliseconds_since_last) |
1091 { | |
1092 ticksnow = HAL_GetTick(); | |
1093 ticksdiff = time_elapsed_ms(tickstart,ticksnow); | |
1094 } | |
1095 else | |
1096 { | |
1097 first = 1; | |
1098 ticksdiff = asynchron_milliseconds_since_last; | |
1099 } | |
1100 | |
1101 if(ticksrest > 1000) // whatever happens after standby with STM32L476 | |
1102 ticksrest = 0; // maybe move static to SRAM2 | |
1103 | |
1104 ticksdiff += ticksrest; | |
1105 time_seconds = ticksdiff/ 1000; | |
1106 ticksrest = ticksdiff - time_seconds * 1000; | |
1107 tickstart = ticksnow; | |
1108 | |
1109 decom_tissues_exposure((int)time_seconds, &global.lifeData); | |
1110 if(global.demo_mode) | |
1111 decom_tissues_exposure((int)(3*time_seconds), &global.lifeData); | |
1112 copyTissueData(); | |
1113 } | |
1114 | |
1115 | |
1116 /** | |
1117 ****************************************************************************** | |
1118 * @brief scheduleUpdateDeviceData | |
1119 * @author heinrichs weikamp gmbh | |
1120 * @version V0.0.1 | |
1121 * @date 16-March-2015 | |
1122 * | |
1123 * two step process | |
1124 * first compare with data from main CPU == externalLogbookFlash | |
1125 * second update with new sensor data | |
1126 ****************************************************************************** | |
1127 */ | |
1128 void scheduleSetDate(SDeviceLine *line) | |
1129 { | |
1130 extern RTC_HandleTypeDef RTCHandle; | |
1131 | |
1132 line->date_rtc_dr = (uint32_t)(RTCHandle.Instance->DR & RTC_DR_RESERVED_MASK); | |
1133 line->time_rtc_tr = (uint32_t)(RTCHandle.Instance->TR & RTC_TR_RESERVED_MASK); | |
1134 } | |
1135 | |
1136 | |
1137 void scheduleCopyDeviceData(SDeviceLine *lineWrite, const SDeviceLine *lineRead) | |
1138 { | |
1139 lineWrite->date_rtc_dr = lineRead->date_rtc_dr; | |
1140 lineWrite->time_rtc_tr = lineRead->time_rtc_tr; | |
1141 lineWrite->value_int32 = lineRead->value_int32; | |
1142 } | |
1143 | |
1144 | |
1145 void scheduleUpdateDeviceData(void) | |
1146 { | |
1147 /* first step, main CPU */ | |
1148 | |
1149 if(deviceDataFlashValid) | |
1150 { | |
1151 /* max values */ | |
1152 if(global.deviceData.batteryChargeCompleteCycles.value_int32 < DeviceDataFlash.batteryChargeCompleteCycles.value_int32) | |
1153 { | |
1154 scheduleCopyDeviceData(&global.deviceData.batteryChargeCompleteCycles, &DeviceDataFlash.batteryChargeCompleteCycles); | |
1155 } | |
1156 if(global.deviceData.batteryChargeCycles.value_int32 < DeviceDataFlash.batteryChargeCycles.value_int32) | |
1157 { | |
1158 scheduleCopyDeviceData(&global.deviceData.batteryChargeCycles, &DeviceDataFlash.batteryChargeCycles); | |
1159 } | |
1160 if(global.deviceData.temperatureMaximum.value_int32 < DeviceDataFlash.temperatureMaximum.value_int32) | |
1161 { | |
1162 scheduleCopyDeviceData(&global.deviceData.temperatureMaximum, &DeviceDataFlash.temperatureMaximum); | |
1163 } | |
1164 if(global.deviceData.depthMaximum.value_int32 < DeviceDataFlash.depthMaximum.value_int32) | |
1165 { | |
1166 scheduleCopyDeviceData(&global.deviceData.depthMaximum, &DeviceDataFlash.depthMaximum); | |
1167 } | |
1168 if(global.deviceData.diveCycles.value_int32 < DeviceDataFlash.diveCycles.value_int32) | |
1169 { | |
1170 scheduleCopyDeviceData(&global.deviceData.diveCycles, &DeviceDataFlash.diveCycles); | |
1171 } | |
1172 if(global.deviceData.hoursOfOperation.value_int32 < DeviceDataFlash.hoursOfOperation.value_int32) | |
1173 { | |
1174 scheduleCopyDeviceData(&global.deviceData.hoursOfOperation, &DeviceDataFlash.hoursOfOperation); | |
1175 } | |
1176 | |
1177 /* min values */ | |
1178 if(global.deviceData.temperatureMinimum.value_int32 > DeviceDataFlash.temperatureMinimum.value_int32) | |
1179 { | |
1180 scheduleCopyDeviceData(&global.deviceData.temperatureMinimum, &DeviceDataFlash.temperatureMinimum); | |
1181 } | |
1182 if(global.deviceData.voltageMinimum.value_int32 > DeviceDataFlash.voltageMinimum.value_int32) | |
1183 { | |
1184 scheduleCopyDeviceData(&global.deviceData.voltageMinimum, &DeviceDataFlash.voltageMinimum); | |
1185 } | |
1186 } | |
1187 | |
1188 /* second step, sensor data */ | |
1189 int32_t temperature_centigrad_int32; | |
1190 int32_t pressure_mbar_int32; | |
1191 int32_t voltage_mvolt_int32; | |
1192 | |
1193 temperature_centigrad_int32 = (int32_t)(get_temperature() * 100); | |
1194 if(temperature_centigrad_int32 < global.deviceData.temperatureMinimum.value_int32) | |
1195 { | |
1196 global.deviceData.temperatureMinimum.value_int32 = temperature_centigrad_int32; | |
88 | 1197 scheduleSetDate(&global.deviceData.temperatureMinimum); |
38 | 1198 } |
1199 | |
1200 if(temperature_centigrad_int32 > global.deviceData.temperatureMaximum.value_int32) | |
1201 { | |
1202 global.deviceData.temperatureMaximum.value_int32 = temperature_centigrad_int32; | |
88 | 1203 scheduleSetDate(&global.deviceData.temperatureMaximum); |
38 | 1204 } |
1205 | |
1206 pressure_mbar_int32 = (int32_t)get_pressure_mbar(); | |
1207 if(pressure_mbar_int32 > global.deviceData.depthMaximum.value_int32) | |
1208 { | |
1209 global.deviceData.depthMaximum.value_int32 = pressure_mbar_int32; | |
88 | 1210 scheduleSetDate(&global.deviceData.depthMaximum); |
38 | 1211 } |
1212 | |
1213 voltage_mvolt_int32 = (int32_t)(get_voltage() * 1000); | |
1214 if(voltage_mvolt_int32 < global.deviceData.voltageMinimum.value_int32) | |
1215 { | |
1216 global.deviceData.voltageMinimum.value_int32 = voltage_mvolt_int32; | |
88 | 1217 scheduleSetDate(&global.deviceData.voltageMinimum); |
38 | 1218 } |
1219 | |
1220 /* third step, counter */ | |
1221 switch (global.mode) | |
1222 { | |
1223 case MODE_SURFACE: | |
1224 case MODE_DIVE: | |
1225 default: | |
1226 deviceDataSubSeconds++; | |
1227 if(deviceDataSubSeconds > 10) | |
1228 { | |
1229 deviceDataSubSeconds = 0; | |
1230 global.deviceData.hoursOfOperation.value_int32++; | |
1231 } | |
1232 break; | |
1233 | |
1234 case MODE_SLEEP: | |
1235 case MODE_SHUTDOWN: | |
1236 break; | |
1237 } | |
1238 } | |
1239 | |
1240 | |
1241 void scheduleUpdateDeviceDataChargerFull(void) | |
1242 { | |
1243 global.deviceData.batteryChargeCompleteCycles.value_int32++; | |
88 | 1244 scheduleSetDate(&global.deviceData.batteryChargeCompleteCycles); |
38 | 1245 } |
1246 | |
1247 | |
1248 void scheduleUpdateDeviceDataChargerCharging(void) | |
1249 { | |
1250 global.deviceData.batteryChargeCycles.value_int32++; | |
88 | 1251 scheduleSetDate(&global.deviceData.batteryChargeCycles); |
38 | 1252 } |
1253 | |
1254 | |
1255 /** | |
1256 ****************************************************************************** | |
1257 * @brief vpm_crush / calls vpm calc_crushing_pressure every four seconds during descend | |
1258 * @author Peter Ryser | |
1259 * @version V0.0.1 | |
1260 * @date 22-April-2014 | |
1261 ****************************************************************************** | |
1262 */ | |
1263 _Bool vpm_crush2(void) | |
1264 { | |
1265 int i = 0; | |
1266 static float starting_ambient_pressure = 0; | |
1267 static float ending_ambient_pressure = 0; | |
1268 static float time_calc_begin = -1; | |
1269 static float initial_helium_pressure[16]; | |
1270 static float initial_nitrogen_pressure[16]; | |
1271 ending_ambient_pressure = global.lifeData.pressure_ambient_bar * 10; | |
1272 | |
1273 if((global.lifeData.dive_time_seconds <= 4) || (starting_ambient_pressure >= ending_ambient_pressure)) | |
1274 { | |
1275 time_calc_begin = global.lifeData.dive_time_seconds; | |
1276 starting_ambient_pressure = global.lifeData.pressure_ambient_bar * 10; | |
1277 for( i = 0; i < 16; i++) | |
1278 { | |
1279 initial_helium_pressure[i] = global.lifeData.tissue_helium_bar[i] * 10; | |
1280 initial_nitrogen_pressure[i] = global.lifeData.tissue_nitrogen_bar[i] * 10; | |
1281 } | |
1282 return 0; | |
1283 } | |
1284 if(global.lifeData.dive_time_seconds - time_calc_begin >= 4) | |
1285 { | |
1286 if(ending_ambient_pressure > starting_ambient_pressure + 0.5f) | |
1287 { | |
1288 float rate = (ending_ambient_pressure - starting_ambient_pressure) * 60 / 4; | |
1289 calc_crushing_pressure(&global.lifeData, &global.vpm, initial_helium_pressure, initial_nitrogen_pressure, starting_ambient_pressure, rate); | |
1290 | |
1291 time_calc_begin = global.lifeData.dive_time_seconds; | |
1292 starting_ambient_pressure = global.lifeData.pressure_ambient_bar * 10; | |
1293 for( i = 0; i < 16; i++) | |
1294 { | |
1295 initial_helium_pressure[i] = global.lifeData.tissue_helium_bar[i] * 10; | |
1296 initial_nitrogen_pressure[i] = global.lifeData.tissue_nitrogen_bar[i] * 10; | |
1297 } | |
1298 | |
1299 return 1; | |
1300 } | |
1301 | |
1302 } | |
1303 return 0; | |
1304 }; | |
1305 | |
1306 | |
1307 void initStructWithZeero(uint8_t* data, uint16_t length) | |
1308 { | |
1309 for(uint16_t i = 0; i < length; i++) | |
1310 data[i] = 0; | |
1311 } | |
1312 | |
1313 | |
1314 long get_nofly_time_minutes(void) | |
1315 { | |
1316 | |
1317 if(global.no_fly_time_minutes <= 0) | |
1318 return 0; | |
1319 | |
1320 long minutes_since_last_dive = global.seconds_since_last_dive/60; | |
1321 | |
1322 if((global.seconds_since_last_dive > 0) && (global.no_fly_time_minutes > minutes_since_last_dive)) | |
1323 { | |
1324 return (global.no_fly_time_minutes - minutes_since_last_dive); | |
1325 } | |
1326 else | |
1327 { | |
1328 global.no_fly_time_minutes = 0; | |
1329 return 0; | |
1330 } | |
1331 } | |
1332 | |
1333 | |
1334 //Supports threadsave copying!!! | |
1335 void copyActualGas(SGas gas) | |
1336 { | |
1337 uint8_t whichGas = !global.whichGas; | |
1338 global.aktualGas[whichGas] = gas; | |
1339 global.whichGas = whichGas; | |
1340 } | |
1341 | |
1342 | |
1343 //Supports threadsave copying!!! | |
1344 void copyPressureData(void) | |
1345 { | |
1346 global.dataSendToMaster.sensorErrors = I2C1_Status(); | |
1347 //uint8_t dataSendToMaster. | |
1348 uint8_t boolPressureData = !global.dataSendToMaster.boolPressureData; | |
1349 global.dataSendToMaster.data[boolPressureData].temperature = get_temperature(); | |
1350 global.dataSendToMaster.data[boolPressureData].pressure_mbar = get_pressure_mbar(); | |
1351 global.dataSendToMaster.data[boolPressureData].surface_mbar = get_surface_mbar(); | |
1352 global.dataSendToMaster.data[boolPressureData].ascent_rate_meter_per_min = global.lifeData.ascent_rate_meter_per_min; | |
1353 global.dataSendToMaster.data[boolPressureData].pressure_uTick = HAL_GetTick(); | |
1354 global.dataSendToMaster.boolPressureData = boolPressureData; | |
1355 } | |
1356 | |
1357 | |
1358 //Supports threadsave copying!!! | |
1359 void copyCnsAndOtuData(void) | |
1360 { | |
1361 //uint8_t dataSendToMaster. | |
1362 uint8_t boolToxicData = !global.dataSendToMaster.boolToxicData; | |
1363 global.dataSendToMaster.data[boolToxicData].cns = global.lifeData.cns; | |
1364 global.dataSendToMaster.data[boolToxicData].otu = global.lifeData.otu; | |
1365 global.dataSendToMaster.data[boolToxicData].desaturation_time_minutes = global.lifeData.desaturation_time_minutes; | |
1366 global.dataSendToMaster.data[boolToxicData].no_fly_time_minutes = get_nofly_time_minutes(); | |
1367 global.dataSendToMaster.boolToxicData = boolToxicData; | |
1368 } | |
1369 | |
1370 | |
1371 //Supports threadsave copying!!! | |
1372 void copyTimeData(void) | |
1373 { | |
1374 extern RTC_HandleTypeDef RTCHandle; | |
1375 | |
1376 uint8_t boolTimeData = !global.dataSendToMaster.boolTimeData; | |
1377 global.dataSendToMaster.data[boolTimeData].localtime_rtc_tr = (uint32_t)(RTCHandle.Instance->TR & RTC_TR_RESERVED_MASK); | |
1378 global.dataSendToMaster.data[boolTimeData].localtime_rtc_dr = (uint32_t)(RTCHandle.Instance->DR & RTC_DR_RESERVED_MASK); | |
1379 global.dataSendToMaster.data[boolTimeData].divetime_seconds = (uint32_t)global.lifeData.dive_time_seconds; | |
1380 global.dataSendToMaster.data[boolTimeData].dive_time_seconds_without_surface_time = (uint32_t)global.lifeData.dive_time_seconds_without_surface_time; | |
1381 global.dataSendToMaster.data[boolTimeData].surfacetime_seconds = (uint32_t)global.seconds_since_last_dive; | |
1382 global.dataSendToMaster.data[boolTimeData].counterSecondsShallowDepth = (uint32_t)global.lifeData.counterSecondsShallowDepth; | |
1383 global.dataSendToMaster.boolTimeData = boolTimeData; | |
1384 } | |
1385 | |
1386 | |
1387 //Supports threadsave copying!!! | |
1388 void copyCompassData(void) | |
1389 { | |
1390 extern float compass_heading; | |
1391 extern float compass_roll; | |
1392 extern float compass_pitch; | |
1393 //uint8_t dataSendToMaster. | |
1394 uint8_t boolCompassData = !global.dataSendToMaster.boolCompassData; | |
1395 global.dataSendToMaster.data[boolCompassData].compass_heading = compass_heading; | |
1396 global.dataSendToMaster.data[boolCompassData].compass_roll = compass_roll; | |
1397 global.dataSendToMaster.data[boolCompassData].compass_pitch = compass_pitch; | |
1398 global.dataSendToMaster.data[boolCompassData].compass_DX_f = 0; | |
1399 global.dataSendToMaster.data[boolCompassData].compass_DY_f = 0; | |
1400 global.dataSendToMaster.data[boolCompassData].compass_DZ_f = 0; | |
1401 global.dataSendToMaster.data[boolCompassData].compass_uTick = HAL_GetTick(); | |
1402 global.dataSendToMaster.boolCompassData = boolCompassData; | |
1403 } | |
1404 | |
1405 | |
1406 void copyCompassDataDuringCalibration(int16_t dx, int16_t dy, int16_t dz) | |
1407 { | |
1408 extern float compass_heading; | |
1409 extern float compass_roll; | |
1410 extern float compass_pitch; | |
1411 //uint8_t dataSendToMaster. | |
1412 uint8_t boolCompassData = !global.dataSendToMaster.boolCompassData; | |
1413 global.dataSendToMaster.data[boolCompassData].compass_heading = compass_heading; | |
1414 global.dataSendToMaster.data[boolCompassData].compass_roll = compass_roll; | |
1415 global.dataSendToMaster.data[boolCompassData].compass_pitch = compass_pitch; | |
1416 global.dataSendToMaster.data[boolCompassData].compass_DX_f = dx; | |
1417 global.dataSendToMaster.data[boolCompassData].compass_DY_f = dy; | |
1418 global.dataSendToMaster.data[boolCompassData].compass_DZ_f = dz; | |
1419 global.dataSendToMaster.boolCompassData = boolCompassData; | |
1420 } | |
1421 | |
1422 | |
1423 //Supports threadsave copying!!! | |
1424 void copyBatteryData(void) | |
1425 { | |
1426 uint8_t boolBatteryData = !global.dataSendToMaster.boolBatteryData; | |
1427 global.dataSendToMaster.data[boolBatteryData].battery_voltage = get_voltage(); | |
1428 global.dataSendToMaster.data[boolBatteryData].battery_charge= get_charge(); | |
1429 global.dataSendToMaster.boolBatteryData = boolBatteryData; | |
1430 } | |
1431 | |
1432 | |
1433 //Supports threadsave copying!!! | |
1434 void copyAmbientLightData(void) | |
1435 { | |
1436 uint8_t boolAmbientLightData = !global.dataSendToMaster.boolAmbientLightData; | |
1437 global.dataSendToMaster.data[boolAmbientLightData].ambient_light_level = get_ambient_light_level(); | |
1438 global.dataSendToMaster.boolAmbientLightData = boolAmbientLightData; | |
1439 } | |
1440 | |
1441 | |
1442 //Supports threadsave copying!!! | |
1443 void copyTissueData(void) | |
1444 { | |
1445 //uint8_t dataSendToMaster. | |
1446 uint8_t boolTisssueData = !global.dataSendToMaster.boolTisssueData; | |
1447 for(int i = 0; i < 16; i++) | |
1448 { | |
1449 global.dataSendToMaster.data[boolTisssueData].tissue_nitrogen_bar[i] = global.lifeData.tissue_nitrogen_bar[i]; | |
1450 global.dataSendToMaster.data[boolTisssueData].tissue_helium_bar[i] = global.lifeData.tissue_helium_bar[i]; | |
1451 } | |
1452 global.dataSendToMaster.boolTisssueData = boolTisssueData; | |
1453 } | |
1454 | |
1455 | |
1456 //Supports threadsave copying!!! | |
1457 void copyVpmCrushingData(void) | |
1458 { | |
1459 //uint8_t dataSendToMaster. | |
1460 uint8_t boolCrushingData = !global.dataSendToMaster.boolCrushingData; | |
1461 for(int i = 0; i < 16; i++) | |
1462 { | |
1463 global.dataSendToMaster.data[boolCrushingData].max_crushing_pressure_n2[i] = global.vpm.max_crushing_pressure_n2[i]; | |
1464 global.dataSendToMaster.data[boolCrushingData].max_crushing_pressure_he[i] = global.vpm.max_crushing_pressure_he[i]; | |
1465 global.dataSendToMaster.data[boolCrushingData].adjusted_critical_radius_he[i] = global.vpm.adjusted_critical_radius_he[i]; | |
1466 global.dataSendToMaster.data[boolCrushingData].adjusted_critical_radius_n2[i] = global.vpm.adjusted_critical_radius_n2[i]; | |
1467 } | |
1468 global.dataSendToMaster.boolCrushingData = boolCrushingData; | |
1469 } | |
1470 | |
1471 | |
1472 void copyDeviceData(void) | |
1473 { | |
1474 uint8_t boolDeviceData = !global.deviceDataSendToMaster.boolDeviceData; | |
1475 memcpy(&global.deviceDataSendToMaster.DeviceData[boolDeviceData], &global.deviceData,sizeof(SDevice)); | |
1476 global.deviceDataSendToMaster.boolDeviceData = boolDeviceData; | |
1477 | |
1478 global.deviceDataSendToMaster.boolVpmRepetitiveDataValid = 0; | |
1479 memcpy(&global.deviceDataSendToMaster.VpmRepetitiveData.adjusted_critical_radius_he, &global.vpm.adjusted_critical_radius_he, sizeof(16*4)); | |
1480 memcpy(&global.deviceDataSendToMaster.VpmRepetitiveData.adjusted_critical_radius_n2, &global.vpm.adjusted_critical_radius_n2, sizeof(16*4)); | |
1481 memcpy(&global.deviceDataSendToMaster.VpmRepetitiveData.adjusted_crushing_pressure_he, &global.vpm.adjusted_crushing_pressure_he, sizeof(16*4)); | |
1482 memcpy(&global.deviceDataSendToMaster.VpmRepetitiveData.adjusted_crushing_pressure_n2, &global.vpm.adjusted_crushing_pressure_n2, sizeof(16*4)); | |
1483 memcpy(&global.deviceDataSendToMaster.VpmRepetitiveData.initial_allowable_gradient_he, &global.vpm.initial_allowable_gradient_he, sizeof(16*4)); | |
1484 memcpy(&global.deviceDataSendToMaster.VpmRepetitiveData.initial_allowable_gradient_n2, &global.vpm.initial_allowable_gradient_n2, sizeof(16*4)); | |
1485 memcpy(&global.deviceDataSendToMaster.VpmRepetitiveData.max_actual_gradient, &global.vpm.max_actual_gradient, sizeof(16*4)); | |
1486 global.deviceDataSendToMaster.VpmRepetitiveData.repetitive_variables_not_valid = global.vpm.repetitive_variables_not_valid; | |
1487 global.deviceDataSendToMaster.boolVpmRepetitiveDataValid = 1; | |
1488 } | |
1489 | |
1490 /* copyPICdata(); is used in spi.c */ | |
1491 void copyPICdata(void) | |
1492 { | |
1493 uint8_t boolPICdata = !global.dataSendToMaster.boolPICdata; | |
1494 for(int i = 0; i < 3; i++) | |
1495 { | |
1496 global.dataSendToMaster.data[boolPICdata].button_setting[i] = global.ButtonPICdata[i]; | |
1497 } | |
1498 global.dataSendToMaster.boolPICdata = boolPICdata; | |
1499 } | |
1500 | |
1501 | |
1502 typedef enum | |
1503 { | |
1504 SPI3_OK = 0x00, | |
1505 SPI3_DEINIT = 0x01, | |
1506 } SPI3_StatusTypeDef; | |
1507 /* if spi3 is running and the SPI3_ButtonAdjust call returns OK, all is fine | |
1508 if the SPI3_ButtonAdjust call returns error, the spi3 is DeInit | |
1509 and will be init the next call of scheduleSetButtonResponsiveness() | |
1510 and data will be send again on the third call | |
1511 therefore on return 0 of scheduleSetButtonResponsiveness() the caller flag should kept active | |
1512 */ | |
1513 uint8_t scheduleSetButtonResponsiveness(void) | |
1514 { | |
1515 static uint8_t SPI3status = SPI3_OK; | |
1516 | |
1517 if((SPI3status == SPI3_OK) && (SPI3_ButtonAdjust(global.ButtonResponsiveness, global.ButtonPICdata))) | |
1518 { | |
1519 copyPICdata(); | |
1520 return 1; | |
1521 } | |
1522 else | |
1523 { | |
1524 for(int i=0;i<3;i++) | |
1525 { | |
1526 global.ButtonPICdata[i] = 0xFF; | |
1527 } | |
1528 copyPICdata(); | |
1529 | |
1530 if(SPI3status == SPI3_OK) | |
1531 { | |
1532 MX_SPI3_DeInit(); | |
1533 SPI3status = SPI3_DEINIT; | |
1534 } | |
1535 else | |
1536 { | |
1537 MX_SPI3_Init(); | |
1538 SPI3status = SPI3_OK; | |
1539 } | |
1540 return 0; | |
1541 } | |
1542 } | |
1543 | |
1544 | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
181
diff
changeset
|
1545 //save time difference |
38 | 1546 uint32_t time_elapsed_ms(uint32_t ticksstart,uint32_t ticksnow) |
1547 { | |
1548 if(ticksstart <= ticksnow) | |
1549 { | |
1550 return ticksnow - ticksstart; | |
1551 } | |
1552 else | |
1553 { | |
1554 return 0xFFFFFFFF - ticksstart + ticksnow; | |
1555 } | |
1556 } | |
1557 | |
1558 /* same as in data_central.c */ | |
1559 _Bool is_ambient_pressure_close_to_surface(SLifeData *lifeDataCall) | |
1560 { | |
1561 if(lifeDataCall->pressure_ambient_bar < (lifeDataCall->pressure_surface_bar + 0.1f)) // hw 161121 now 1 mter, before 0.04f | |
1562 return true; | |
1563 else | |
1564 return false; | |
1565 } | |
1566 | |
1567 | |
1568 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/ | |
1569 |