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