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