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