Mercurial > public > hwos_code
annotate src/sleepmode.asm @ 642:a9a0188091e4
fix rare upgrade issue with OSTC sport 2019 hardware
author | heinrichsweikamp |
---|---|
date | Thu, 14 Jan 2021 16:24:07 +0100 |
parents | 8c1f1f334275 |
children | 7d8a4c60ec1a |
rev | line source |
---|---|
0 | 1 ;============================================================================= |
2 ; | |
634 | 3 ; File sleepmode.asm * combined next generation V3.09.4n |
0 | 4 ; |
623 | 5 ; Sleep Mode |
0 | 6 ; |
7 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved. | |
582 | 8 ;============================================================================== |
0 | 9 ; HISTORY |
10 ; 2011-08-12 : [mH] moving from OSTC code | |
11 | |
582 | 12 #include "hwos.inc" ; Mandatory header |
13 #include "shared_definitions.h" ; Mailbox from/to p2_deco.c | |
14 #include "surfmode.inc" | |
15 #include "tft.inc" | |
16 #include "start.inc" | |
17 #include "adc_lightsensor.inc" | |
18 #include "math.inc" | |
19 #include "ms5541.inc" | |
20 #include "eeprom_rs232.inc" | |
21 #include "external_flash.inc" | |
22 #include "ghostwriter.inc" | |
23 #include "i2c.inc" | |
24 #include "mcp.inc" | |
623 | 25 #include "wait.inc" |
629 | 26 #include "tft_outputs.inc" |
0 | 27 |
133
939f1e83c4c2
BUGFIX: Surface interval was not displayed correctly in some cases
heinrichsweikamp
parents:
123
diff
changeset
|
28 |
631 | 29 extern eeprom_deco_data_write |
30 extern option_check_and_store_all | |
623 | 31 extern power_up_switches ; from hwos.asm |
582 | 32 |
33 | |
623 | 34 ;---- Private local Variables ------------------------------------------------- |
133
939f1e83c4c2
BUGFIX: Surface interval was not displayed correctly in some cases
heinrichsweikamp
parents:
123
diff
changeset
|
35 |
582 | 36 CBLOCK local1 ; max size is 16 Byte !!! |
623 | 37 accel_reference ; acceleration reference value for detecting movement / terminating deep sleep |
38 sm_timer_10sec ; timer for 10 seconds tasks (pressure check) | |
39 sm_timer_10min ; timer for 10 minutes tasks (tissue updating) | |
628 | 40 ;sm_timer_15min ; timer for 15 minutes tasks (entering deep sleep) |
41 sm_10min_counter ; count #of 10 minutes | |
623 | 42 loop_counter ; loop counter, used in init_avg_switches routine |
43 sm_flags ; local flags | |
44 ENDC ; used: 6 byte, remaining: 10 byte | |
582 | 45 |
46 | |
623 | 47 ;---- Private local Flags ----------------------------------------------------- |
48 | |
49 #DEFINE deep_sleep sm_flags,0 ; =1: in deep sleep mode, =0: normal sleep | |
50 #DEFINE desat_on_10_mins sm_flags,1 ; =1: calculate desaturation every 10 minutes, =0: every minute | |
628 | 51 #DEFINE charge_in_sleep sm_flags,2 ; =1: show charging screen |
623 | 52 ; sm_flags,3 ; unused |
53 ; sm_flags,4 ; unused | |
54 ; sm_flags,5 ; unused | |
55 ; sm_flags,6 ; unused | |
56 ; sm_flags,7 ; unused | |
57 | |
58 | |
634 | 59 ;============================================================================= |
623 | 60 slmode CODE |
582 | 61 ;============================================================================== |
0 | 62 |
634 | 63 |
64 ;----------------------------------------------------------------------------- | |
65 ; Entry Point for Sleep Mode | |
66 ; | |
0 | 67 global sleeploop |
623 | 68 sleeploop: |
69 clrf STKPTR ; clear return addresses stack | |
70 call request_speed_normal ; request CPU speed switch to normal speed | |
71 | |
72 bcf LEDg ; turn off green LED / release reset to RX circuitry | |
73 bcf LEDr ; turn off red LED | |
604 | 74 |
75 IFDEF _screendump | |
623 | 76 bcf screen_dump_avail ; disable screen dump function |
77 ENDIF | |
78 | |
79 bsf sleepmode ; flag being in sleep mode | |
80 bsf block_sensor_interrupt ; suspend ISR from executing sensor interrupts | |
81 | |
634 | 82 call disable_ir_s8_analog ; power-down IR/S8/analog interface |
623 | 83 call mcp_sleep ; power-down RX power supply |
604 | 84 |
85 clrf ADCON0 ; power-down ADC module | |
623 | 86 call disable_rs232 ; power-down USB |
87 call I2C_sleep_compass ; power-down compass | |
490
8dfb93e80338
NEW: Deep Sleep mode for OSTC Plus and OSTC 2 (2017) (Entered automatically)
heinrichsweikamp
parents:
448
diff
changeset
|
88 |
631 | 89 call eeprom_deco_data_write ; update deco data in EEPROM |
90 call eeprom_battery_gauge_write ; update battery gauge in EEPROM | |
91 | |
634 | 92 btfsc option_changed ; do the options need to be stored to EEPROM ? |
631 | 93 call option_check_and_store_all ; YES - check and store all option values in EEPROM |
94 | |
623 | 95 call ext_flash_enable_protection ; enable write protection on external flash |
582 | 96 |
623 | 97 clrf sm_timer_10sec ; clear 10 seconds timer |
98 clrf sm_timer_10min ; clear 10 minutes timer | |
628 | 99 clrf sm_10min_counter ; clear 10mins counter |
623 | 100 clrf sm_flags ; clear all local flags |
490
8dfb93e80338
NEW: Deep Sleep mode for OSTC Plus and OSTC 2 (2017) (Entered automatically)
heinrichsweikamp
parents:
448
diff
changeset
|
101 |
628 | 102 call TFT_Display_FadeOut ; power-down backlight |
103 call TFT_DisplayOff ; power-down display | |
631 | 104 |
0 | 105 sleeploop_loop: |
623 | 106 btfsc trigger_full_second ; one second in sleep? |
107 rcall one_sec_sleep ; YES - check switches, pressure sensor, etc. | |
582 | 108 |
623 | 109 btfss sleepmode ; shall terminate sleep mode? |
110 bra sleeploop_exit ; YES | |
0 | 111 |
634 | 112 rcall sleepmode_sleep ; sleep for 62.5...125 ms |
582 | 113 |
623 | 114 btfss deep_sleep ; shall enter deep sleep? |
115 bra sleeploop_loop ; NO - remain in normal sleep loop | |
116 ;bra deepsleep_pre ; YES - enter deep sleep loop | |
0 | 117 |
623 | 118 deepsleep_pre: |
634 | 119 bcf PIE1,TMR1IE ; disable timer 1 interrupt |
120 bcf PIE2,TMR3IE ; disable timer 2 interrupt | |
121 bcf PIE5,TMR7IE ; disable timer 7 interrupt | |
640 | 122 bcf PIE5,TMR4IE ; disable timer 4 interrupt |
634 | 123 bcf INTCON, INT0IE ; disable INT0 interrupt |
124 bcf INTCON3,INT1IE ; disable INT1 interrupt | |
0 | 125 |
623 | 126 bcf power_sw1 ; power-down switch 1 |
127 bcf power_sw2 ; power-down switch 2 | |
490
8dfb93e80338
NEW: Deep Sleep mode for OSTC Plus and OSTC 2 (2017) (Entered automatically)
heinrichsweikamp
parents:
448
diff
changeset
|
128 |
604 | 129 rcall deepsleep_get_accel ; read accelerometer into WREG |
623 | 130 movwf accel_reference ; store as reference value |
628 | 131 tstfsz accel_reference ; = 0 ? |
132 bra deepsleep_loop ; NO - continue | |
133 bra deepsleep_loop_exit ; YES - no deep sleep (fail-safe) | |
490
8dfb93e80338
NEW: Deep Sleep mode for OSTC Plus and OSTC 2 (2017) (Entered automatically)
heinrichsweikamp
parents:
448
diff
changeset
|
134 |
582 | 135 deepsleep_loop: |
623 | 136 btfsc trigger_full_second ; one second in deep sleep? |
137 rcall check_accelerometer ; YES - check accelerometer | |
138 | |
139 btfsc trigger_full_second ; one second in deep sleep? | |
140 rcall one_sec_sleep ; YES - check switches, check pressure sensor, etc. | |
141 | |
634 | 142 rcall sleepmode_sleep ; sleep for 62.5...125 ms |
623 | 143 |
144 btfss sleepmode ; shall leave sleep mode? | |
145 bcf deep_sleep ; YES - leave deep sleep mode then, too | |
146 | |
147 btfsc deep_sleep ; shall leave deep sleep mode? | |
148 bra deepsleep_loop ; NO - loop in deep sleep loop | |
149 | |
628 | 150 deepsleep_loop_exit: |
151 bcf deep_sleep ; clear flag (again) | |
152 clrf sm_10min_counter ; clear 10mins counter | |
623 | 153 call power_up_switches ; turn on the analog switches |
154 rcall init_avg_switches ; initialize the averaging system | |
155 | |
634 | 156 bsf PIE1,TMR1IE ; enable timer 1 interrupt |
157 bsf PIE2,TMR3IE ; enable timer 2 interrupt | |
158 bsf PIE5,TMR7IE ; enable timer 7 interrupt | |
640 | 159 bsf PIE5,TMR4IE ; enable timer 4 interrupt |
634 | 160 bsf INTCON, INT0IE ; enable INT0 interrupt |
161 bsf INTCON3,INT1IE ; enable INT1 interrupt | |
623 | 162 |
163 bra sleeploop_loop ; enter normal sleep loop | |
164 | |
165 sleeploop_exit: | |
166 bcf switch_left ; eventually clear left button event | |
167 bcf switch_right ; eventually clear right button event | |
168 movlw .0 ; reset ISR sensor state machine | |
169 movff WREG,sensor_state_counter ; ... | |
170 bcf PIR5,TMR7IF ; clear timer 7, driving the ISR sensor interrupts | |
171 bcf block_sensor_interrupt ; re-enable execution of the ISR sensor interrupts | |
172 goto restart ; restart | |
173 | |
174 | |
634 | 175 ;----------------------------------------------------------------------------- |
176 ; Helper Function - Tasks every Second in Sleep Mode | |
177 ; | |
623 | 178 one_sec_sleep: |
179 bcf trigger_full_second ; clear trigger flag | |
180 | |
181 btfsc switch_left ; left switch pressed? | |
182 bcf sleepmode ; YES - terminate sleep mode | |
183 | |
184 btfsc switch_right ; right switch pressed? | |
185 bcf sleepmode ; YES - terminate sleep mode | |
582 | 186 |
623 | 187 btfsc battery_gauge_available ; is a battery gauge IC available? |
188 bra one_sec_sleep_1 ; YES - check for charger | |
629 | 189 btfsc ble_available ; Skip "USB" check in all Bluetooth models (Required for very old OSTC sport) |
190 bra one_sec_sleep_2 ; - continue | |
623 | 191 btfsc vusb_in ; NO - USB plugged in? |
192 bcf sleepmode ; YES - terminate sleep mode | |
193 bra one_sec_sleep_2 ; - continue | |
194 | |
195 one_sec_sleep_1: | |
196 call get_battery_voltage ; check for charger | |
197 | |
628 | 198 ; Test if charging |
199 btfss cc_active ; charging? | |
200 bra one_sec_sleep_1a ; NO | |
201 btfsc charge_in_sleep ; YES - already showing charge screen? | |
202 bra one_sec_sleep_1b ; YES - only update data | |
203 | |
634 | 204 bsf charge_in_sleep |
205 bcf deep_sleep ; wake-up from deepsleep | |
628 | 206 call TFT_boot ; initialize TFT (includes clear screen) |
207 movlw .32 | |
208 movff WREG,max_CCPR1L ; bank safe | |
209 call TFT_Display_FadeIn ; dim up the display | |
210 one_sec_sleep_1b: | |
634 | 211 call TFT_surfmode_batt ; show battery type, voltage and color-coded percentage |
628 | 212 bra one_sec_sleep_2 ; continue |
213 | |
214 one_sec_sleep_1a: | |
215 btfss charge_in_sleep ; was showing charge screen? | |
216 bra one_sec_sleep_2 ; NO | |
217 | |
218 ; yes, power-down screen | |
219 call TFT_Display_FadeOut ; power-down backlight | |
220 call TFT_DisplayOff ; power-down display | |
221 bcf charge_in_sleep | |
222 | |
223 | |
623 | 224 one_sec_sleep_2: |
225 incf sm_timer_10sec,F ; increment 10 seconds timer | |
226 movlw .10 ; load a 10 into WREG | |
227 cpfslt sm_timer_10sec ; timer < 10 yet? | |
228 rcall ten_sec_sleep ; NO - do the every 10 second tasks | |
229 | |
230 btfsc trigger_full_minute ; one minute in sleep? | |
231 rcall one_min_sleep ; YES - do the every minute tasks | |
232 | |
233 btfsc trigger_full_hour ; one hour in sleep? | |
234 rcall one_hour_sleep ; YES - do the every hour tasks | |
235 | |
236 return ; done | |
490
8dfb93e80338
NEW: Deep Sleep mode for OSTC Plus and OSTC 2 (2017) (Entered automatically)
heinrichsweikamp
parents:
448
diff
changeset
|
237 |
623 | 238 |
634 | 239 ;----------------------------------------------------------------------------- |
240 ; Helper Function - Tasks every 10 Seconds in Sleep Mode | |
241 ; | |
623 | 242 ten_sec_sleep: |
243 ; tasks every 10 seconds in sleep mode | |
244 clrf sm_timer_10sec ; clear timer | |
245 rcall pressuretest_sleep_fast ; get pressure without averaging (faster) | |
246 MOVLI wake_up_from_sleep,sub_a ; load wake-up pressure (1160 mbar) into sub_a | |
247 MOVII pressure_abs, sub_b ; load current absolute pressure into sub_b | |
248 call cmpU16 ; sub_a - sub_b = wake-up pressure - current absolute pressure | |
249 btfsc neg_flag ; is the current absolute pressure > 1160 mbar ? | |
250 bcf sleepmode ; YES - terminate sleep mode | |
251 return ; done | |
252 | |
253 | |
634 | 254 ;----------------------------------------------------------------------------- |
255 ; Helper Function - Tasks every Minute in Sleep Mode | |
256 ; | |
623 | 257 one_min_sleep: |
258 ; tasks every minute in sleep mode | |
259 bcf trigger_full_minute ; clear flag | |
260 | |
261 ; tick the 10 minutes timer | |
262 incf sm_timer_10min,F ; increment 10 minutes timer | |
263 movlw .10 ; load a 10 into WREG | |
264 cpfslt sm_timer_10min ; timer < 10 yet? | |
265 rcall ten_min_sleep ; NO - do the every 10 minutes tasks | |
490
8dfb93e80338
NEW: Deep Sleep mode for OSTC Plus and OSTC 2 (2017) (Entered automatically)
heinrichsweikamp
parents:
448
diff
changeset
|
266 |
623 | 267 one_min_sleep_1: |
268 ; continue tasks every minute | |
269 btfsc desat_on_10_mins ; shall do desaturation calculation on 10 minute intervals? | |
270 return ; YES - that's not here then, so done | |
271 call deco_calc_dive_interval_1min ; NO - calculate 1 minute at surface conditions (C-code) | |
272 banksel common ; - back to bank common | |
273 return ; - done | |
274 | |
275 | |
634 | 276 ;----------------------------------------------------------------------------- |
277 ; Helper Function - Tasks every 10 Minutes in Sleep Mode | |
278 ; | |
623 | 279 ten_min_sleep: |
280 ; tasks every 10 minutes in sleep mode | |
281 clrf sm_timer_10min ; reset timer to 0 | |
282 | |
283 call sample_surface_pressure ; sample surface pressure and update ISR and deco engine | |
284 | |
628 | 285 btfss analog_switches ; OSTC with analog switches? |
286 bra ten_min_sleep_0 ; NO - no analog switches, no deep sleep required | |
287 | |
288 ; check if we need to enter deep sleep | |
289 incf sm_10min_counter,F ; count | |
290 movlw deep_sleep_10mins ; threshold | |
291 cpfseq sm_10min_counter ; threshold reached? | |
292 bra ten_min_sleep_0 ; NO - threshold not reached yet | |
293 | |
294 decf sm_10min_counter,F ; -1 to re-trigger every 10mins (in case of charging) | |
295 | |
296 btfss cc_active ; charging? | |
297 bsf deep_sleep ; NO - activate deep-sleep mode | |
298 | |
299 ten_min_sleep_0: | |
623 | 300 btfss desat_on_10_mins ; shall do desaturation calculation on 10 minute intervals? |
301 bra ten_min_sleep_1 ; NO - continue checking if schedule can be switched to 10 minutes | |
302 call deco_calc_dive_interval_10min ; YES - calculate 10 minutes at surface conditions (C-code) | |
303 banksel common ; - back to bank common | |
304 return ; - done | |
305 | |
306 ten_min_sleep_1: | |
307 movff int_O_lead_supersat+0,WREG ; get leading tissue's supersaturation (only the lower byte is used for the value) | |
308 bsf desat_on_10_mins ; switch to 10 minute intervals by default | |
309 tstfsz WREG ; gradient factor = 0 ? | |
310 bcf desat_on_10_mins ; NO - stay on 1 minute intervals | |
311 return ; done | |
312 | |
582 | 313 |
634 | 314 ;----------------------------------------------------------------------------- |
315 ; Helper Function - Tasks every Hour in Sleep Mode | |
316 ; | |
623 | 317 one_hour_sleep: |
318 ; tasks every hour in sleep mode mode | |
319 bcf trigger_full_hour ; clear one hour flag | |
631 | 320 call eeprom_deco_data_write ; update deco data into EEPROM |
321 call eeprom_battery_gauge_write ; update battery gauge into EEPROM | |
628 | 322 return ; done |
623 | 323 |
490
8dfb93e80338
NEW: Deep Sleep mode for OSTC Plus and OSTC 2 (2017) (Entered automatically)
heinrichsweikamp
parents:
448
diff
changeset
|
324 |
634 | 325 ;----------------------------------------------------------------------------- |
326 ; Helper Function - Initialization of the Button Event Averaging System | |
327 ; | |
623 | 328 init_avg_switches: |
329 ; pause 4 seconds using CPU sleep mode to conserve on battery | |
330 movlw .4 ; time to pause | |
331 movwf loop_counter ; initialize loop counter | |
332 bcf trigger_full_second ; clear 'one second elapsed' flag | |
333 activate_switches_1: | |
634 | 334 rcall sleepmode_sleep ; sleep for 62.5...125 ms |
623 | 335 btfss trigger_full_second ; did 1 second elapsed meanwhile? |
336 bra activate_switches_1 ; NO - loop | |
337 bcf trigger_full_second ; YES - clear flag | |
338 decfsz loop_counter,F ; - decrement loop counter, done? | |
339 bra activate_switches_1 ; NO - loop | |
340 | |
341 ; initialize the averaging system | |
342 movlw .32 ; number of readout cycles | |
343 movwf loop_counter ; initialize loop counter | |
344 activate_switches_2: | |
345 call get_analog_switches ; do a analog switch readout | |
346 decfsz loop_counter,F ; decrement loop counter, done? | |
347 bra activate_switches_2 ; NO - loop | |
348 | |
349 ; clear all button events that may have intermediately occurred | |
350 bcf PIR1,TMR1IF ; clear button-hold-down timer | |
634 | 351 bcf INTCON, INT0IF ; clear right button activity |
623 | 352 bcf INTCON3,INT1IF ; clear left button activity |
353 bcf analog_sw1_pressed ; clear analog switch 1 activity | |
354 bcf analog_sw2_pressed ; clear analog switch 2 activity | |
355 bcf switch_right ; clear right button event | |
356 bcf switch_left ; clear left button event | |
357 | |
634 | 358 return ; done |
623 | 359 |
360 | |
634 | 361 ;----------------------------------------------------------------------------- |
362 ; Helper Function - Check for Change in Accelerometer Z-Axis | |
363 ; | |
623 | 364 check_accelerometer: |
604 | 365 rcall deepsleep_get_accel ; read accelerometer into WREG |
623 | 366 subwf accel_reference,W ; reference value - accel_DZ+0 -> WREG |
604 | 367 btfsc STATUS,N ; result negative? |
368 negf WREG ; YES - negate it | |
623 | 369 movwf lo ; save as change of acceleration in Z-axis |
628 | 370 movlw .100 ; load threshold (mg) |
623 | 371 cpfslt lo ; change of acceleration > threshold ? |
372 bcf deep_sleep ; YES - terminate deep sleep mode | |
373 return ; done | |
490
8dfb93e80338
NEW: Deep Sleep mode for OSTC Plus and OSTC 2 (2017) (Entered automatically)
heinrichsweikamp
parents:
448
diff
changeset
|
374 |
582 | 375 |
634 | 376 ;----------------------------------------------------------------------------- |
377 ; Helper Function - read Accelerometer Z-Axis | |
378 ; | |
490
8dfb93e80338
NEW: Deep Sleep mode for OSTC Plus and OSTC 2 (2017) (Entered automatically)
heinrichsweikamp
parents:
448
diff
changeset
|
379 deepsleep_get_accel: |
628 | 380 call I2C_init_compass ; start compass |
634 | 381 rcall sleepmode_sleep ; sleep for 62.5...125 ms |
604 | 382 call I2C_RX_accelerometer ; read accelerometer |
628 | 383 call I2C_RX_accelerometer ; read accelerometer |
384 call I2C_sleep_compass ; shut down compass | |
623 | 385 movff accel_DZ+0,WREG ; transfer result to WREG |
386 return ; done | |
387 | |
388 | |
634 | 389 ;----------------------------------------------------------------------------- |
390 ; Helper Function - read Pressure without Averaging | |
391 ; | |
392 ; faster method to save some power in sleep mode | |
393 ; | |
394 pressuretest_sleep_fast: | |
623 | 395 banksel isr_backup ; select bank ISR data |
396 | |
397 CLRI pressure_abs_avg ; clear pressure average register | |
398 CLRI temperature_avg ; clear temperature average register | |
399 | |
400 call get_temperature_start ; start temperature integration (73.5 us) | |
582 | 401 |
634 | 402 rcall sleepmode_sleep ; sleep for 62.5...125 ms |
403 rcall sleepmode_sleep ; sleep for 62.5...125 ms | |
623 | 404 |
405 call get_temperature_value ; state 1: get temperature | |
406 call get_pressure_start ; start pressure integration | |
407 | |
634 | 408 rcall sleepmode_sleep ; sleep for 62.5...125 ms |
409 rcall sleepmode_sleep ; sleep for 62.5...125 ms | |
623 | 410 |
411 call get_pressure_value ; state2: get pressure (51 us) | |
412 call calculate_compensation ; calculate temperature compensated pressure (27 us) | |
413 | |
414 MOVII pressure_abs_avg,pressure_abs ; get result, bypassing the averaging | |
415 | |
416 banksel common ; back to bank common | |
582 | 417 return |
0 | 418 |
419 | |
634 | 420 ;----------------------------------------------------------------------------- |
421 ; Helper Function - sleep for 62.5...125 ms | |
422 ; | |
0 | 423 sleepmode_sleep: |
634 | 424 movff BSR,BSR_backup ; backup current bank selection |
425 banksel common ; switch to bank common | |
628 | 426 btfsc charge_in_sleep ; already showing charge screen? |
634 | 427 bra sleepmode_sleepwalk ; YES - skip the actual sleep (but wait) |
428 sleep ; NO - put CPU into sleep (wakes up by timer 7 IRQ) | |
429 sleep ; - put CPU into sleep again | |
628 | 430 sleepmode_sleep_1: |
634 | 431 movff BSR_backup,BSR ; restore bank selection |
432 return ; done | |
628 | 433 |
434 sleepmode_sleepwalk: | |
634 | 435 WAITMS d'65' ; wait 65 ms |
436 bra sleepmode_sleep_1 ; continue with common part | |
628 | 437 |
634 | 438 ;----------------------------------------------------------------------------- |
439 | |
440 END |