Mercurial > public > hwos_code
annotate src/sleepmode.asm @ 635:9a64914a8fca
3.10 re-release addressing some minor issues and language fixes
author | heinrichsweikamp |
---|---|
date | Thu, 07 May 2020 09:12:59 +0200 |
parents | 4050675965ea |
children | 8c1f1f334275 |
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 | |
122 bcf INTCON, INT0IE ; disable INT0 interrupt | |
123 bcf INTCON3,INT1IE ; disable INT1 interrupt | |
0 | 124 |
623 | 125 bcf power_sw1 ; power-down switch 1 |
126 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
|
127 |
604 | 128 rcall deepsleep_get_accel ; read accelerometer into WREG |
623 | 129 movwf accel_reference ; store as reference value |
628 | 130 tstfsz accel_reference ; = 0 ? |
131 bra deepsleep_loop ; NO - continue | |
132 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
|
133 |
582 | 134 deepsleep_loop: |
623 | 135 btfsc trigger_full_second ; one second in deep sleep? |
136 rcall check_accelerometer ; YES - check accelerometer | |
137 | |
138 btfsc trigger_full_second ; one second in deep sleep? | |
139 rcall one_sec_sleep ; YES - check switches, check pressure sensor, etc. | |
140 | |
634 | 141 rcall sleepmode_sleep ; sleep for 62.5...125 ms |
623 | 142 |
143 btfss sleepmode ; shall leave sleep mode? | |
144 bcf deep_sleep ; YES - leave deep sleep mode then, too | |
145 | |
146 btfsc deep_sleep ; shall leave deep sleep mode? | |
147 bra deepsleep_loop ; NO - loop in deep sleep loop | |
148 | |
628 | 149 deepsleep_loop_exit: |
150 bcf deep_sleep ; clear flag (again) | |
151 clrf sm_10min_counter ; clear 10mins counter | |
623 | 152 call power_up_switches ; turn on the analog switches |
153 rcall init_avg_switches ; initialize the averaging system | |
154 | |
634 | 155 bsf PIE1,TMR1IE ; enable timer 1 interrupt |
156 bsf PIE2,TMR3IE ; enable timer 2 interrupt | |
157 bsf PIE5,TMR7IE ; enable timer 7 interrupt | |
158 bsf INTCON, INT0IE ; enable INT0 interrupt | |
159 bsf INTCON3,INT1IE ; enable INT1 interrupt | |
623 | 160 |
161 bra sleeploop_loop ; enter normal sleep loop | |
162 | |
163 sleeploop_exit: | |
164 bcf switch_left ; eventually clear left button event | |
165 bcf switch_right ; eventually clear right button event | |
166 movlw .0 ; reset ISR sensor state machine | |
167 movff WREG,sensor_state_counter ; ... | |
168 bcf PIR5,TMR7IF ; clear timer 7, driving the ISR sensor interrupts | |
169 bcf block_sensor_interrupt ; re-enable execution of the ISR sensor interrupts | |
170 goto restart ; restart | |
171 | |
172 | |
634 | 173 ;----------------------------------------------------------------------------- |
174 ; Helper Function - Tasks every Second in Sleep Mode | |
175 ; | |
623 | 176 one_sec_sleep: |
177 bcf trigger_full_second ; clear trigger flag | |
178 | |
179 btfsc switch_left ; left switch pressed? | |
180 bcf sleepmode ; YES - terminate sleep mode | |
181 | |
182 btfsc switch_right ; right switch pressed? | |
183 bcf sleepmode ; YES - terminate sleep mode | |
582 | 184 |
623 | 185 btfsc battery_gauge_available ; is a battery gauge IC available? |
186 bra one_sec_sleep_1 ; YES - check for charger | |
629 | 187 btfsc ble_available ; Skip "USB" check in all Bluetooth models (Required for very old OSTC sport) |
188 bra one_sec_sleep_2 ; - continue | |
623 | 189 btfsc vusb_in ; NO - USB plugged in? |
190 bcf sleepmode ; YES - terminate sleep mode | |
191 bra one_sec_sleep_2 ; - continue | |
192 | |
193 one_sec_sleep_1: | |
194 call get_battery_voltage ; check for charger | |
195 | |
628 | 196 ; Test if charging |
197 btfss cc_active ; charging? | |
198 bra one_sec_sleep_1a ; NO | |
199 btfsc charge_in_sleep ; YES - already showing charge screen? | |
200 bra one_sec_sleep_1b ; YES - only update data | |
201 | |
634 | 202 bsf charge_in_sleep |
203 bcf deep_sleep ; wake-up from deepsleep | |
628 | 204 call TFT_boot ; initialize TFT (includes clear screen) |
205 movlw .32 | |
206 movff WREG,max_CCPR1L ; bank safe | |
207 call TFT_Display_FadeIn ; dim up the display | |
208 one_sec_sleep_1b: | |
634 | 209 call TFT_surfmode_batt ; show battery type, voltage and color-coded percentage |
628 | 210 bra one_sec_sleep_2 ; continue |
211 | |
212 one_sec_sleep_1a: | |
213 btfss charge_in_sleep ; was showing charge screen? | |
214 bra one_sec_sleep_2 ; NO | |
215 | |
216 ; yes, power-down screen | |
217 call TFT_Display_FadeOut ; power-down backlight | |
218 call TFT_DisplayOff ; power-down display | |
219 bcf charge_in_sleep | |
220 | |
221 | |
623 | 222 one_sec_sleep_2: |
223 incf sm_timer_10sec,F ; increment 10 seconds timer | |
224 movlw .10 ; load a 10 into WREG | |
225 cpfslt sm_timer_10sec ; timer < 10 yet? | |
226 rcall ten_sec_sleep ; NO - do the every 10 second tasks | |
227 | |
228 btfsc trigger_full_minute ; one minute in sleep? | |
229 rcall one_min_sleep ; YES - do the every minute tasks | |
230 | |
231 btfsc trigger_full_hour ; one hour in sleep? | |
232 rcall one_hour_sleep ; YES - do the every hour tasks | |
233 | |
234 return ; done | |
490
8dfb93e80338
NEW: Deep Sleep mode for OSTC Plus and OSTC 2 (2017) (Entered automatically)
heinrichsweikamp
parents:
448
diff
changeset
|
235 |
623 | 236 |
634 | 237 ;----------------------------------------------------------------------------- |
238 ; Helper Function - Tasks every 10 Seconds in Sleep Mode | |
239 ; | |
623 | 240 ten_sec_sleep: |
241 ; tasks every 10 seconds in sleep mode | |
242 clrf sm_timer_10sec ; clear timer | |
243 rcall pressuretest_sleep_fast ; get pressure without averaging (faster) | |
244 MOVLI wake_up_from_sleep,sub_a ; load wake-up pressure (1160 mbar) into sub_a | |
245 MOVII pressure_abs, sub_b ; load current absolute pressure into sub_b | |
246 call cmpU16 ; sub_a - sub_b = wake-up pressure - current absolute pressure | |
247 btfsc neg_flag ; is the current absolute pressure > 1160 mbar ? | |
248 bcf sleepmode ; YES - terminate sleep mode | |
249 return ; done | |
250 | |
251 | |
634 | 252 ;----------------------------------------------------------------------------- |
253 ; Helper Function - Tasks every Minute in Sleep Mode | |
254 ; | |
623 | 255 one_min_sleep: |
256 ; tasks every minute in sleep mode | |
257 bcf trigger_full_minute ; clear flag | |
258 | |
259 ; tick the 10 minutes timer | |
260 incf sm_timer_10min,F ; increment 10 minutes timer | |
261 movlw .10 ; load a 10 into WREG | |
262 cpfslt sm_timer_10min ; timer < 10 yet? | |
263 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
|
264 |
623 | 265 one_min_sleep_1: |
266 ; continue tasks every minute | |
267 btfsc desat_on_10_mins ; shall do desaturation calculation on 10 minute intervals? | |
268 return ; YES - that's not here then, so done | |
269 call deco_calc_dive_interval_1min ; NO - calculate 1 minute at surface conditions (C-code) | |
270 banksel common ; - back to bank common | |
271 return ; - done | |
272 | |
273 | |
634 | 274 ;----------------------------------------------------------------------------- |
275 ; Helper Function - Tasks every 10 Minutes in Sleep Mode | |
276 ; | |
623 | 277 ten_min_sleep: |
278 ; tasks every 10 minutes in sleep mode | |
279 clrf sm_timer_10min ; reset timer to 0 | |
280 | |
281 call sample_surface_pressure ; sample surface pressure and update ISR and deco engine | |
282 | |
628 | 283 btfss analog_switches ; OSTC with analog switches? |
284 bra ten_min_sleep_0 ; NO - no analog switches, no deep sleep required | |
285 | |
286 ; check if we need to enter deep sleep | |
287 incf sm_10min_counter,F ; count | |
288 movlw deep_sleep_10mins ; threshold | |
289 cpfseq sm_10min_counter ; threshold reached? | |
290 bra ten_min_sleep_0 ; NO - threshold not reached yet | |
291 | |
292 decf sm_10min_counter,F ; -1 to re-trigger every 10mins (in case of charging) | |
293 | |
294 btfss cc_active ; charging? | |
295 bsf deep_sleep ; NO - activate deep-sleep mode | |
296 | |
297 ten_min_sleep_0: | |
623 | 298 btfss desat_on_10_mins ; shall do desaturation calculation on 10 minute intervals? |
299 bra ten_min_sleep_1 ; NO - continue checking if schedule can be switched to 10 minutes | |
300 call deco_calc_dive_interval_10min ; YES - calculate 10 minutes at surface conditions (C-code) | |
301 banksel common ; - back to bank common | |
302 return ; - done | |
303 | |
304 ten_min_sleep_1: | |
305 movff int_O_lead_supersat+0,WREG ; get leading tissue's supersaturation (only the lower byte is used for the value) | |
306 bsf desat_on_10_mins ; switch to 10 minute intervals by default | |
307 tstfsz WREG ; gradient factor = 0 ? | |
308 bcf desat_on_10_mins ; NO - stay on 1 minute intervals | |
309 return ; done | |
310 | |
582 | 311 |
634 | 312 ;----------------------------------------------------------------------------- |
313 ; Helper Function - Tasks every Hour in Sleep Mode | |
314 ; | |
623 | 315 one_hour_sleep: |
316 ; tasks every hour in sleep mode mode | |
317 bcf trigger_full_hour ; clear one hour flag | |
631 | 318 call eeprom_deco_data_write ; update deco data into EEPROM |
319 call eeprom_battery_gauge_write ; update battery gauge into EEPROM | |
628 | 320 return ; done |
623 | 321 |
490
8dfb93e80338
NEW: Deep Sleep mode for OSTC Plus and OSTC 2 (2017) (Entered automatically)
heinrichsweikamp
parents:
448
diff
changeset
|
322 |
634 | 323 ;----------------------------------------------------------------------------- |
324 ; Helper Function - Initialization of the Button Event Averaging System | |
325 ; | |
623 | 326 init_avg_switches: |
327 ; pause 4 seconds using CPU sleep mode to conserve on battery | |
328 movlw .4 ; time to pause | |
329 movwf loop_counter ; initialize loop counter | |
330 bcf trigger_full_second ; clear 'one second elapsed' flag | |
331 activate_switches_1: | |
634 | 332 rcall sleepmode_sleep ; sleep for 62.5...125 ms |
623 | 333 btfss trigger_full_second ; did 1 second elapsed meanwhile? |
334 bra activate_switches_1 ; NO - loop | |
335 bcf trigger_full_second ; YES - clear flag | |
336 decfsz loop_counter,F ; - decrement loop counter, done? | |
337 bra activate_switches_1 ; NO - loop | |
338 | |
339 ; initialize the averaging system | |
340 movlw .32 ; number of readout cycles | |
341 movwf loop_counter ; initialize loop counter | |
342 activate_switches_2: | |
343 call get_analog_switches ; do a analog switch readout | |
344 decfsz loop_counter,F ; decrement loop counter, done? | |
345 bra activate_switches_2 ; NO - loop | |
346 | |
347 ; clear all button events that may have intermediately occurred | |
348 bcf PIR1,TMR1IF ; clear button-hold-down timer | |
634 | 349 bcf INTCON, INT0IF ; clear right button activity |
623 | 350 bcf INTCON3,INT1IF ; clear left button activity |
351 bcf analog_sw1_pressed ; clear analog switch 1 activity | |
352 bcf analog_sw2_pressed ; clear analog switch 2 activity | |
353 bcf switch_right ; clear right button event | |
354 bcf switch_left ; clear left button event | |
355 | |
634 | 356 return ; done |
623 | 357 |
358 | |
634 | 359 ;----------------------------------------------------------------------------- |
360 ; Helper Function - Check for Change in Accelerometer Z-Axis | |
361 ; | |
623 | 362 check_accelerometer: |
604 | 363 rcall deepsleep_get_accel ; read accelerometer into WREG |
623 | 364 subwf accel_reference,W ; reference value - accel_DZ+0 -> WREG |
604 | 365 btfsc STATUS,N ; result negative? |
366 negf WREG ; YES - negate it | |
623 | 367 movwf lo ; save as change of acceleration in Z-axis |
628 | 368 movlw .100 ; load threshold (mg) |
623 | 369 cpfslt lo ; change of acceleration > threshold ? |
370 bcf deep_sleep ; YES - terminate deep sleep mode | |
371 return ; done | |
490
8dfb93e80338
NEW: Deep Sleep mode for OSTC Plus and OSTC 2 (2017) (Entered automatically)
heinrichsweikamp
parents:
448
diff
changeset
|
372 |
582 | 373 |
634 | 374 ;----------------------------------------------------------------------------- |
375 ; Helper Function - read Accelerometer Z-Axis | |
376 ; | |
490
8dfb93e80338
NEW: Deep Sleep mode for OSTC Plus and OSTC 2 (2017) (Entered automatically)
heinrichsweikamp
parents:
448
diff
changeset
|
377 deepsleep_get_accel: |
628 | 378 call I2C_init_compass ; start compass |
634 | 379 rcall sleepmode_sleep ; sleep for 62.5...125 ms |
604 | 380 call I2C_RX_accelerometer ; read accelerometer |
628 | 381 call I2C_RX_accelerometer ; read accelerometer |
382 call I2C_sleep_compass ; shut down compass | |
623 | 383 movff accel_DZ+0,WREG ; transfer result to WREG |
384 return ; done | |
385 | |
386 | |
634 | 387 ;----------------------------------------------------------------------------- |
388 ; Helper Function - read Pressure without Averaging | |
389 ; | |
390 ; faster method to save some power in sleep mode | |
391 ; | |
392 pressuretest_sleep_fast: | |
623 | 393 banksel isr_backup ; select bank ISR data |
394 | |
395 CLRI pressure_abs_avg ; clear pressure average register | |
396 CLRI temperature_avg ; clear temperature average register | |
397 | |
398 call get_temperature_start ; start temperature integration (73.5 us) | |
582 | 399 |
634 | 400 rcall sleepmode_sleep ; sleep for 62.5...125 ms |
401 rcall sleepmode_sleep ; sleep for 62.5...125 ms | |
623 | 402 |
403 call get_temperature_value ; state 1: get temperature | |
404 call get_pressure_start ; start pressure integration | |
405 | |
634 | 406 rcall sleepmode_sleep ; sleep for 62.5...125 ms |
407 rcall sleepmode_sleep ; sleep for 62.5...125 ms | |
623 | 408 |
409 call get_pressure_value ; state2: get pressure (51 us) | |
410 call calculate_compensation ; calculate temperature compensated pressure (27 us) | |
411 | |
412 MOVII pressure_abs_avg,pressure_abs ; get result, bypassing the averaging | |
413 | |
414 banksel common ; back to bank common | |
582 | 415 return |
0 | 416 |
417 | |
634 | 418 ;----------------------------------------------------------------------------- |
419 ; Helper Function - sleep for 62.5...125 ms | |
420 ; | |
0 | 421 sleepmode_sleep: |
634 | 422 movff BSR,BSR_backup ; backup current bank selection |
423 banksel common ; switch to bank common | |
628 | 424 btfsc charge_in_sleep ; already showing charge screen? |
634 | 425 bra sleepmode_sleepwalk ; YES - skip the actual sleep (but wait) |
426 sleep ; NO - put CPU into sleep (wakes up by timer 7 IRQ) | |
427 sleep ; - put CPU into sleep again | |
628 | 428 sleepmode_sleep_1: |
634 | 429 movff BSR_backup,BSR ; restore bank selection |
430 return ; done | |
628 | 431 |
432 sleepmode_sleepwalk: | |
634 | 433 WAITMS d'65' ; wait 65 ms |
434 bra sleepmode_sleep_1 ; continue with common part | |
628 | 435 |
634 | 436 ;----------------------------------------------------------------------------- |
437 | |
438 END |