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