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