Mercurial > public > hwos_code
comparison src/sleepmode.asm @ 623:c40025d8e750
3.03 beta released
author | heinrichsweikamp |
---|---|
date | Mon, 03 Jun 2019 14:01:48 +0200 |
parents | ca4556fb60b9 |
children | cd58f7fc86db |
comparison
equal
deleted
inserted
replaced
622:02d1386429a6 | 623:c40025d8e750 |
---|---|
1 ;============================================================================= | 1 ;============================================================================= |
2 ; | 2 ; |
3 ; File sleepmode.asm Version 2.99e | 3 ; File sleepmode.asm combined next generation V3.03.1 |
4 ; | 4 ; |
5 ; Sleepmode | 5 ; Sleep Mode |
6 ; | 6 ; |
7 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved. | 7 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved. |
8 ;============================================================================== | 8 ;============================================================================== |
9 ; HISTORY | 9 ; HISTORY |
10 ; 2011-08-12 : [mH] moving from OSTC code | 10 ; 2011-08-12 : [mH] moving from OSTC code |
11 | 11 |
12 #include "hwos.inc" ; Mandatory header | 12 #include "hwos.inc" ; Mandatory header |
13 #include "shared_definitions.h" ; Mailbox from/to p2_deco.c | 13 #include "shared_definitions.h" ; Mailbox from/to p2_deco.c |
14 #include "surfmode.inc" | 14 #include "surfmode.inc" |
15 #include "tft.inc" | 15 #include "tft.inc" |
16 #include "isr.inc" | |
17 #include "start.inc" | 16 #include "start.inc" |
18 #include "adc_lightsensor.inc" | 17 #include "adc_lightsensor.inc" |
19 #include "math.inc" | 18 #include "math.inc" |
20 #include "ms5541.inc" | 19 #include "ms5541.inc" |
21 #include "wait.inc" | |
22 #include "eeprom_rs232.inc" | 20 #include "eeprom_rs232.inc" |
23 #include "external_flash.inc" | 21 #include "external_flash.inc" |
24 #include "ghostwriter.inc" | 22 #include "ghostwriter.inc" |
25 #include "i2c.inc" | 23 #include "i2c.inc" |
26 #include "mcp.inc" | 24 #include "mcp.inc" |
25 #include "wait.inc" | |
27 | 26 |
28 | 27 |
29 extern vault_decodata_into_eeprom | 28 extern vault_decodata_into_eeprom |
30 | 29 extern power_up_switches ; from hwos.asm |
31 | 30 |
32 ;---- Private local variables ------------------------------------------------- | 31 |
32 ;---- Private local Variables ------------------------------------------------- | |
33 | 33 |
34 CBLOCK local1 ; max size is 16 Byte !!! | 34 CBLOCK local1 ; max size is 16 Byte !!! |
35 sm_temp1 ; sleepmode temporary 1 | 35 accel_reference ; acceleration reference value for detecting movement / terminating deep sleep |
36 sm_temp2 ; sleepmode temporary 2 | 36 sm_timer_10sec ; timer for 10 seconds tasks (pressure check) |
37 sm_timer1 ; timer for pressure check every 10 seconds | 37 sm_timer_10min ; timer for 10 minutes tasks (tissue updating) |
38 sm_timer2 ; timer for 10 minutes tasks (updating of tissues) | 38 sm_timer_15min ; timer for 15 minutes tasks (entering deep sleep) |
39 sm_timer3 ; timer for 15 minutes tasks (updating of surface pressure) | 39 loop_counter ; loop counter, used in init_avg_switches routine |
40 ENDC ; used: 5 byte, remaining: 11 byte | 40 sm_flags ; local flags |
41 | 41 ENDC ; used: 6 byte, remaining: 10 byte |
42 | 42 |
43 slmode CODE | 43 |
44 ;---- Private local Flags ----------------------------------------------------- | |
45 | |
46 #DEFINE deep_sleep sm_flags,0 ; =1: in deep sleep mode, =0: normal sleep | |
47 #DEFINE desat_on_10_mins sm_flags,1 ; =1: calculate desaturation every 10 minutes, =0: every minute | |
48 ; sm_flags,2 ; unused | |
49 ; sm_flags,3 ; unused | |
50 ; sm_flags,4 ; unused | |
51 ; sm_flags,5 ; unused | |
52 ; sm_flags,6 ; unused | |
53 ; sm_flags,7 ; unused | |
54 | |
55 | |
56 slmode CODE | |
44 | 57 |
45 ;============================================================================== | 58 ;============================================================================== |
46 | 59 |
47 global sleeploop | 60 global sleeploop |
48 sleeploop: ; enter sleep mode | 61 sleeploop: |
49 call disable_ir_s8 ; IR/S8 off | 62 clrf STKPTR ; clear return addresses stack |
50 call mcp_sleep | 63 call request_speed_normal ; request CPU speed switch to normal speed |
51 bcf LEDg | 64 |
52 bcf LEDr | 65 bcf LEDg ; turn off green LED / release reset to RX circuitry |
53 call TFT_Display_FadeOut | 66 bcf LEDr ; turn off red LED |
54 call TFT_DisplayOff ; display off | |
55 | 67 |
56 IFDEF _screendump | 68 IFDEF _screendump |
57 bcf enable_screen_dumps ; =1: Ignore vin_usb, wait for "l" command (screen dump) | 69 bcf screen_dump_avail ; disable screen dump function |
58 ENDIF | 70 ENDIF |
59 | 71 |
60 call disable_rs232 ; USB off | 72 bsf sleepmode ; flag being in sleep mode |
73 bsf block_sensor_interrupt ; suspend ISR from executing sensor interrupts | |
74 | |
75 IFDEF _external_sensor | |
76 call disable_ir_s8 ; power-down IR/S8 interrupts | |
77 call mcp_sleep ; power-down RX power supply | |
78 ENDIF | |
79 | |
80 clrf ADCON0 ; power-down ADC module | |
81 call TFT_Display_FadeOut ; power-down backlight | |
82 call TFT_DisplayOff ; power-down display | |
83 call disable_rs232 ; power-down USB | |
84 call I2C_sleep_accelerometer ; power-down accelerometer | |
85 call I2C_sleep_compass ; power-down compass | |
86 | |
61 call vault_decodata_into_eeprom ; store deco data | 87 call vault_decodata_into_eeprom ; store deco data |
62 call ext_flash_enable_protection ; enable write protection for external flash | 88 call ext_flash_enable_protection ; enable write protection on external flash |
63 call update_battery_registers ; update battery registers into EEPROM | 89 call update_battery_registers ; update battery registers into EEPROM |
64 clrf sm_temp1 | 90 |
65 clrf sm_temp2 | 91 clrf sm_timer_10sec ; clear 10 seconds timer |
66 clrf sm_timer1 | 92 clrf sm_timer_10min ; clear 10 minutes timer |
67 clrf sm_timer2 | 93 clrf sm_timer_15min ; clear 15 minutes timer |
68 clrf sm_timer3 | 94 clrf sm_flags ; clear all local flags |
69 call speed_normal | 95 |
70 bsf no_sensor_int ; inhibit sensor interrupts | 96 sleeploop_loop: |
71 clrf ADCON0 ; power-down ADC module | 97 btfsc trigger_full_second ; one second in sleep? |
72 | 98 rcall one_sec_sleep ; YES - check switches, pressure sensor, etc. |
73 sleeploop_pre: | 99 |
74 bcf deep_sleep ; normal sleep mode | 100 btfss sleepmode ; shall terminate sleep mode? |
75 call I2C_sleep_accelerometer | 101 bra sleeploop_exit ; YES |
76 call I2C_sleep_compass | 102 |
103 rcall sleepmode_sleep ; wait at least 35 ms (every 62.5 ms timer7 wakeup) | |
104 | |
105 btfss deep_sleep ; shall enter deep sleep? | |
106 bra sleeploop_loop ; NO - remain in normal sleep loop | |
107 ;bra deepsleep_pre ; YES - enter deep sleep loop | |
108 | |
109 deepsleep_pre: | |
110 bcf PIE1,0 ; disable timer 1 interrupt | |
111 bcf PIE2,1 ; disable timer 2 interrupt | |
112 bcf PIE5,3 ; disable timer 7 interrupt | |
113 bcf INTCON,4 ; disable INT0 interrupt | |
114 bcf INTCON3,3 ; disable INT1 interrupt | |
115 | |
116 bcf power_sw1 ; power-down switch 1 | |
117 bcf power_sw2 ; power-down switch 2 | |
118 | |
119 rcall deepsleep_get_accel ; read accelerometer into WREG | |
120 movwf accel_reference ; store as reference value | |
121 | |
122 deepsleep_loop: | |
123 btfsc trigger_full_second ; one second in deep sleep? | |
124 rcall check_accelerometer ; YES - check accelerometer | |
125 | |
126 btfsc trigger_full_second ; one second in deep sleep? | |
127 rcall one_sec_sleep ; YES - check switches, check pressure sensor, etc. | |
128 | |
129 rcall sleepmode_sleep ; wait at least 35 ms (every 62.5 ms timer7 wakeup) | |
130 | |
131 btfss sleepmode ; shall leave sleep mode? | |
132 bcf deep_sleep ; YES - leave deep sleep mode then, too | |
133 | |
134 btfsc deep_sleep ; shall leave deep sleep mode? | |
135 bra deepsleep_loop ; NO - loop in deep sleep loop | |
136 | |
137 call power_up_switches ; turn on the analog switches | |
138 rcall init_avg_switches ; initialize the averaging system | |
139 | |
140 bsf PIE1,0 ; enable timer 1 interrupt | |
141 bsf PIE2,1 ; enable timer 2 interrupt | |
142 bsf PIE5,3 ; enable timer 7 interrupt | |
143 bsf INTCON,4 ; enable INT0 interrupt | |
144 bsf INTCON3,3 ; enable INT1 interrupt | |
145 | |
146 bra sleeploop_loop ; enter normal sleep loop | |
147 | |
148 | |
149 sleeploop_exit: | |
150 bcf switch_left ; eventually clear left button event | |
151 bcf switch_right ; eventually clear right button event | |
152 movlw .0 ; reset ISR sensor state machine | |
153 movff WREG,sensor_state_counter ; ... | |
154 bcf PIR5,TMR7IF ; clear timer 7, driving the ISR sensor interrupts | |
155 bcf block_sensor_interrupt ; re-enable execution of the ISR sensor interrupts | |
156 goto restart ; restart | |
157 | |
158 | |
159 one_sec_sleep: | |
160 ; tasks every second in sleep mode | |
161 bcf trigger_full_second ; clear trigger flag | |
162 | |
163 btfsc switch_left ; left switch pressed? | |
164 bcf sleepmode ; YES - terminate sleep mode | |
165 | |
166 btfsc switch_right ; right switch pressed? | |
167 bcf sleepmode ; YES - terminate sleep mode | |
168 | |
169 btfsc battery_gauge_available ; is a battery gauge IC available? | |
170 bra one_sec_sleep_1 ; YES - check for charger | |
171 btfsc vusb_in ; NO - USB plugged in? | |
172 bcf sleepmode ; YES - terminate sleep mode | |
173 bra one_sec_sleep_2 ; - continue | |
174 | |
175 one_sec_sleep_1: | |
176 call get_battery_voltage ; check for charger | |
177 | |
178 one_sec_sleep_2: | |
179 incf sm_timer_10sec,F ; increment 10 seconds timer | |
180 movlw .10 ; load a 10 into WREG | |
181 cpfslt sm_timer_10sec ; timer < 10 yet? | |
182 rcall ten_sec_sleep ; NO - do the every 10 second tasks | |
183 | |
184 btfsc trigger_full_minute ; one minute in sleep? | |
185 rcall one_min_sleep ; YES - do the every minute tasks | |
186 | |
187 btfsc trigger_full_hour ; one hour in sleep? | |
188 rcall one_hour_sleep ; YES - do the every hour tasks | |
189 | |
190 return ; done | |
191 | |
192 | |
193 ten_sec_sleep: | |
194 ; tasks every 10 seconds in sleep mode | |
195 clrf sm_timer_10sec ; clear timer | |
196 rcall pressuretest_sleep_fast ; get pressure without averaging (faster) | |
197 MOVLI wake_up_from_sleep,sub_a ; load wake-up pressure (1160 mbar) into sub_a | |
198 MOVII pressure_abs, sub_b ; load current absolute pressure into sub_b | |
199 call cmpU16 ; sub_a - sub_b = wake-up pressure - current absolute pressure | |
200 btfsc neg_flag ; is the current absolute pressure > 1160 mbar ? | |
201 bcf sleepmode ; YES - terminate sleep mode | |
202 return ; done | |
203 | |
204 | |
205 one_min_sleep: | |
206 ; tasks every minute in sleep mode | |
207 bcf trigger_full_minute ; clear flag | |
208 | |
209 ; tick the 10 minutes timer | |
210 incf sm_timer_10min,F ; increment 10 minutes timer | |
211 movlw .10 ; load a 10 into WREG | |
212 cpfslt sm_timer_10min ; timer < 10 yet? | |
213 rcall ten_min_sleep ; NO - do the every 10 minutes tasks | |
214 | |
215 ; the 15 minutes timer only ticks on OSTC with analog switches | |
77 btfss analog_switches ; OSTC with analog switches? | 216 btfss analog_switches ; OSTC with analog switches? |
78 bra sleeploop_loop ; NO - no analog switches | 217 bra one_min_sleep_1 ; NO - no analog switches, no deep sleep required |
79 | 218 |
80 bsf power_sw1 | 219 ; the 15 minutes timer also ticks only when not in deep sleep |
81 btfss power_sw1 | 220 btfsc deep_sleep ; in deep sleep mode? |
82 bra $-4 | 221 bra one_min_sleep_1 ; YES - already in deep sleep |
83 bsf power_sw2 | 222 |
84 btfss power_sw2 | 223 ; tick the 15 minutes timer |
85 bra $-4 | 224 incf sm_timer_15min,F ; increment 15 minutes timer |
86 movlw .4 ; wait for button circuity | 225 movlw .15 ; load a 15 into WREG |
87 movwf sm_temp1 ; used as temp | 226 cpfslt sm_timer_15min ; timer < 15 yet? |
88 bcf onesecupdate | 227 rcall fifteen_min_sleep ; NO - do the every 15 minute tasks |
89 | 228 |
90 sleeploop_pre1: | 229 one_min_sleep_1: |
91 rcall sleepmode_sleep | 230 ; continue tasks every minute |
92 btfss onesecupdate ; wait 1 second | 231 btfsc desat_on_10_mins ; shall do desaturation calculation on 10 minute intervals? |
93 bra sleeploop_pre1 | 232 return ; YES - that's not here then, so done |
94 bcf onesecupdate | 233 call deco_calc_dive_interval_1min ; NO - calculate 1 minute at surface conditions (C-code) |
95 decfsz sm_temp1,F | 234 banksel common ; - back to bank common |
96 bra sleeploop_pre1 | 235 return ; - done |
97 movlw .32 ; wait for button circuity | 236 |
98 movwf sm_temp1 ; used as temp | 237 |
99 | 238 ten_min_sleep: |
100 sleeploop_pre2: | 239 ; tasks every 10 minutes in sleep mode |
101 call get_analog_switches | 240 clrf sm_timer_10min ; reset timer to 0 |
102 decfsz sm_temp1,F | 241 |
103 bra sleeploop_pre2 | 242 call sample_surface_pressure ; sample surface pressure and update ISR and deco engine |
104 | 243 |
105 bcf PIR1,TMR1IF | 244 btfss desat_on_10_mins ; shall do desaturation calculation on 10 minute intervals? |
106 bcf INTCON,INT0IF | 245 bra ten_min_sleep_1 ; NO - continue checking if schedule can be switched to 10 minutes |
107 bcf INTCON3,INT1IF | 246 call deco_calc_dive_interval_10min ; YES - calculate 10 minutes at surface conditions (C-code) |
108 bcf PIR5,TMR7IF | 247 banksel common ; - back to bank common |
109 bcf switch_left | 248 return ; - done |
110 bcf switch_right | 249 |
111 bcf analog_sw2_pressed | 250 ten_min_sleep_1: |
112 bcf analog_sw1_pressed | 251 movff int_O_lead_supersat+0,WREG ; get leading tissue's supersaturation (only the lower byte is used for the value) |
113 bsf PIE1,0 ; (re)start timer 1 interrupt | 252 bsf desat_on_10_mins ; switch to 10 minute intervals by default |
114 bsf PIE2,1 ; (re)start timer 2 interrupt | 253 tstfsz WREG ; gradient factor = 0 ? |
115 bsf PIE5,3 ; (re)start timer 7 interrupt | 254 bcf desat_on_10_mins ; NO - stay on 1 minute intervals |
116 bsf INTCON,4 ; (re)start INT0 interrupt | 255 return ; done |
117 bsf INTCON3,3 ; (re)start INT1 interrupt | 256 |
118 | 257 |
119 sleeploop_loop: | 258 fifteen_min_sleep: |
120 btfsc onesecupdate ; one second in sleep? | 259 ; tasks every 15 minutes in sleep mode |
121 rcall onesec_sleep ; check switches, check pressure sensor, etc. | 260 clrf sm_timer_15min ; reset timer to 0 |
122 | 261 bsf deep_sleep ; enable deep-sleep mode |
123 btfss sleepmode ; wake up? (This bit will be set in other routines) | 262 return |
124 goto restart ; YES | 263 |
125 | 264 |
126 btfsc deep_sleep ; enter deep sleep? | 265 one_hour_sleep: |
127 bra deepsleep ; YES | 266 ; tasks every hour in sleep mode mode |
128 | 267 bcf trigger_full_hour ; clear one hour flag |
129 no_deepsleep: | 268 call update_battery_registers ; update battery registers into EEPROM |
130 rcall sleepmode_sleep ; wait at least 35 ms (every 62.5ms timer7 wakeup) | 269 call vault_decodata_into_eeprom ; update tissue pressures into EEPROM |
131 | 270 return |
132 ; Any button pressed in sleep? | 271 |
133 ; btfsc switch_left | 272 |
134 ; rcall onesec_sleep1a | 273 init_avg_switches: |
135 ; btfsc switch_right | 274 ; pause 4 seconds using CPU sleep mode to conserve on battery |
136 ; rcall onesec_sleep1a | 275 movlw .4 ; time to pause |
137 ; | 276 movwf loop_counter ; initialize loop counter |
138 ; btfss sleepmode ; wake up? (this bit will be set in other routines) | 277 bcf trigger_full_second ; clear 'one second elapsed' flag |
139 ; goto restart ; YES | 278 activate_switches_1: |
140 | 279 rcall sleepmode_sleep ; wait at least 35 ms (every 62.5 ms timer7 wakeup) |
141 bra sleeploop_loop ; do loop until something happens | 280 btfss trigger_full_second ; did 1 second elapsed meanwhile? |
142 | 281 bra activate_switches_1 ; NO - loop |
143 deepsleep: | 282 bcf trigger_full_second ; YES - clear flag |
144 btfss analog_switches | 283 decfsz loop_counter,F ; - decrement loop counter, done? |
145 bra no_deepsleep ; no analog switches, no deep sleep required | 284 bra activate_switches_1 ; NO - loop |
146 | 285 |
147 bcf PIE1,0 ; stop timer 1 interrupt | 286 ; initialize the averaging system |
148 bcf PIE2,1 ; stop timer 2 interrupt | 287 movlw .32 ; number of readout cycles |
149 bcf PIE5,3 ; stop timer 7 interrupt | 288 movwf loop_counter ; initialize loop counter |
150 bcf INTCON,4 ; stop INT0 interrupt | 289 activate_switches_2: |
151 bcf INTCON3,3 ; stop INT1 interrupt | 290 call get_analog_switches ; do a analog switch readout |
152 bcf power_sw1 | 291 decfsz loop_counter,F ; decrement loop counter, done? |
153 bcf power_sw2 | 292 bra activate_switches_2 ; NO - loop |
293 | |
294 ; clear all button events that may have intermediately occurred | |
295 bcf PIR1,TMR1IF ; clear button-hold-down timer | |
296 bcf INTCON,INT0IF ; clear right button activity | |
297 bcf INTCON3,INT1IF ; clear left button activity | |
298 bcf analog_sw1_pressed ; clear analog switch 1 activity | |
299 bcf analog_sw2_pressed ; clear analog switch 2 activity | |
300 bcf switch_right ; clear right button event | |
301 bcf switch_left ; clear left button event | |
302 | |
303 ; done | |
304 return | |
305 | |
306 | |
307 check_accelerometer: | |
154 rcall deepsleep_get_accel ; read accelerometer into WREG | 308 rcall deepsleep_get_accel ; read accelerometer into WREG |
155 movwf sm_temp1 ; store init value | 309 subwf accel_reference,W ; reference value - accel_DZ+0 -> WREG |
156 | |
157 deepsleep_loop: | |
158 btfsc onesecupdate ; one second in sleep? | |
159 rcall onesec_deepsleep ; YES - check accelerometer | |
160 | |
161 btfsc onesecupdate ; one second in sleep? | |
162 rcall onesec_sleep ; YES - check switches, check pressure sensor, etc. | |
163 | |
164 rcall sleepmode_sleep | |
165 | |
166 btfss deep_sleep ; enter normal sleep mode? | |
167 bra sleeploop_pre ; Yes | |
168 | |
169 bra deepsleep_loop ; do loop until something happens | |
170 | |
171 onesec_deepsleep: | |
172 rcall deepsleep_get_accel ; read accelerometer into WREG | |
173 subwf sm_temp1,W ; sm_temp1 - accel_DZ+0 -> WREG | |
174 btfsc STATUS,N ; result negative? | 310 btfsc STATUS,N ; result negative? |
175 negf WREG ; YES - negate it | 311 negf WREG ; YES - negate it |
176 movwf sm_temp2 ; change of acceleration in Z-axis | 312 movwf lo ; save as change of acceleration in Z-axis |
177 movlw .50 ; threshold (mg) | 313 movlw .50 ; load threshold (mg) |
178 cpfslt sm_temp2 ; bigger then the threshold? | 314 cpfslt lo ; change of acceleration > threshold ? |
179 bcf deep_sleep ; YES | 315 bcf deep_sleep ; YES - terminate deep sleep mode |
180 | 316 return ; done |
181 ; extern piezo_config_tx | 317 |
182 ; movff sm_temp1,WREG | |
183 ; call piezo_config_tx | |
184 ; movff accel_DZ+0,WREG | |
185 ; call piezo_config_tx | |
186 ; movff sm_temp2,WREG | |
187 ; call piezo_config_tx | |
188 | |
189 return | |
190 | 318 |
191 deepsleep_get_accel: | 319 deepsleep_get_accel: |
192 call I2C_init_compass ; required for compass1 | 320 call I2C_init_compass ; start compass, required for compass1 |
193 call I2C_init_accelerometer ; required for compass2 | 321 call I2C_init_accelerometer ; start accelerometer, required for compass2 |
194 call I2C_RX_accelerometer ; read accelerometer | 322 call I2C_RX_accelerometer ; read accelerometer |
195 call I2C_sleep_compass ; required for compass1 | 323 call I2C_sleep_compass ; shut down compass, required for compass1 |
196 call I2C_sleep_accelerometer ; required for compass2 | 324 call I2C_sleep_accelerometer ; shut down accelerometer, required for compass2 |
197 movff accel_DZ+0,WREG | 325 movff accel_DZ+0,WREG ; transfer result to WREG |
198 return | 326 return ; done |
199 | 327 |
200 onehour_sleep: | |
201 call update_battery_registers ; update battery registers into EEPROM | |
202 call vault_decodata_into_eeprom ; update deco data | |
203 bcf onehourupdate ; all done | |
204 return | |
205 | |
206 onemin_sleep: | |
207 btfsc onehourupdate ; one hour in sleep? | |
208 rcall onehour_sleep ; YES | |
209 | |
210 btfsc battery_gauge_available | |
211 call get_battery_voltage ; check for charger | |
212 | |
213 ;---- update tissues and CNS every 10 minutes when gradient factor is 0 (no supersaturation in any tissue any more) | |
214 movff int_O_gradient_factor+0,WREG ; get gradient factor, only the lower byte is used for the value | |
215 tstfsz WREG ; gradient factor = 0? | |
216 bra onemin_sleep1 ; NO - continue with air pressure compensation | |
217 incf sm_timer2,F ; count-up... | |
218 movlw d'9' ; ...to 9 | |
219 cpfsgt sm_timer2 ; 10 minutes over? | |
220 bra onemin_sleep1 ; NO - continue with air pressure compensation | |
221 clrf sm_timer2 ; reset counter | |
222 SAFE_2BYTE_COPY amb_pressure, int_I_pres_respiration ; copy pressure to deco routine | |
223 call deco_calc_dive_interval_10min ; calculate 10 minutes under surface conditions | |
224 banksel common | |
225 | |
226 onemin_sleep1: | |
227 ;---- adjust air pressure compensation any 15 minutes | |
228 incf sm_timer3,F ; count-up... | |
229 movlw d'14' ; ...to 14 | |
230 cpfsgt sm_timer3 ; 15 minutes over? | |
231 bra onemin_sleep2 ; NO - continue with every-minute-tasks | |
232 | |
233 ; Tasks every 15 minutes in sleep | |
234 bsf deep_sleep ; enter deep-sleep mode | |
235 clrf sm_timer3 ; reset counter | |
236 | |
237 SAFE_2BYTE_COPY last_surfpressure_15min, last_surfpressure_30min ; save older air pressure | |
238 SAFE_2BYTE_COPY amb_pressure, last_surfpressure_15min ; save new air pressure | |
239 | |
240 movlw LOW max_surfpressure | |
241 movff WREG,sub_a+0 ; max. "allowed" air pressure in mbar | |
242 movlw HIGH max_surfpressure | |
243 movff WREG,sub_a+1 ; max. "allowed" air pressure in mbar | |
244 movff last_surfpressure_15min+0,sub_b+0 | |
245 movff last_surfpressure_15min+1,sub_b+1 | |
246 call subU16 ; sub_c = sub_a - sub_b | |
247 btfss neg_flag ; Is 1080mbar < amb_pressure ? | |
248 bra onemin_sleep2 ; NO: current air pressure is lower then "allowed" air pressure, ok! | |
249 | |
250 ; not ok! Overwrite with max. "allowed" air pressure | |
251 movlw LOW max_surfpressure | |
252 movff WREG,last_surfpressure_15min+0 ; max. "allowed" air pressure in mbar | |
253 movlw HIGH max_surfpressure | |
254 movff WREG,last_surfpressure_15min+1 ; max. "allowed" air pressure in mbar | |
255 | |
256 onemin_sleep2: | |
257 ; Tasks every minute in sleep | |
258 | |
259 ;---- update tissues and CNS every minute when gradient factor is >0 (supersaturation in at least one tissue) | |
260 movff int_O_gradient_factor+0,WREG ; get gradient factor, only the lower byte is used for the value | |
261 tstfsz WREG ; gradient factor = 0? | |
262 bra onemin_sleep3 ; NO - do tissue update on 1 minute schedule | |
263 bra onemin_sleep4 ; YES - tissue update is done on 10 minutes schedule | |
264 | |
265 onemin_sleep3: | |
266 SAFE_2BYTE_COPY amb_pressure, int_I_pres_respiration ; copy pressure to deco routine | |
267 call deco_calc_dive_interval_1min ; calculate 1 minute under surface conditions | |
268 banksel common | |
269 | |
270 onemin_sleep4: | |
271 bcf oneminupdate ; all done | |
272 return | |
273 | |
274 onesec_sleep: | |
275 btfsc oneminupdate ; one minute in sleep? | |
276 rcall onemin_sleep ; YES - do one-minute tasks, e.g. calculate desaturation | |
277 | |
278 btfsc battery_gauge_available | |
279 call get_battery_voltage ; check for charger | |
280 | |
281 incf sm_timer1,F ; counts to #test_pressure_in_sleep (10) | |
282 movlw d'10' | |
283 cpfsgt sm_timer1 ; here: temp variable | |
284 bra onesec_sleep1 ; #test_pressure_in_sleep not done yet | |
285 | |
286 clrf sm_timer1 ; clear counter | |
287 rcall pressuretest_sleep_fast ; Gets pressure without averaging (faster!) | |
288 ; compare current ambient pressure with wake_up_from_sleep | |
289 movlw LOW wake_up_from_sleep | |
290 movwf sub_a+0 ; power on if ambient pressure is greater threshold | |
291 movlw HIGH wake_up_from_sleep | |
292 movwf sub_a+1 ; power on if ambient pressure is greater threshold | |
293 SAFE_2BYTE_COPY amb_pressure, sub_b | |
294 call subU16 ; is (1160mbar - averaged(amb_pressure)) < 0 ? | |
295 btfsc neg_flag ; wake up from sleep? | |
296 bra onesec_sleep1a ; YES - skip button checks, wake up! | |
297 | |
298 btfsc battery_gauge_available | |
299 bra onesec_sleep1 ; no wake-up with cR hardware | |
300 btfsc vusb_in ; USB plugged in? | |
301 bra onesec_sleep1a ; YES - skip button checks, wake up | |
302 | |
303 onesec_sleep1: | |
304 bcf onesecupdate ; clear flag | |
305 btfsc switch_left ; left switch pressed? | |
306 bra onesec_sleep1a ; YES | |
307 btfsc switch_right ; right switch pressed? | |
308 bra onesec_sleep1a ; YES | |
309 return ; NO to both - done | |
310 | |
311 onesec_sleep1a: ; at least one button pressed or amb_pressure -> wake_up_from_sleep | |
312 bcf sleepmode ; wake up | |
313 SAFE_2BYTE_COPY last_surfpressure_30min, amb_pressure ; copy for compatibility | |
314 movlw .0 | |
315 movff WREG,sensor_state_counter ; reset sensor state counter | |
316 bcf no_sensor_int ; normal sensor interrupt mode | |
317 return | |
318 | 328 |
319 pressuretest_sleep_fast: ; get pressure without averaging (faster to save some power in sleep mode) | 329 pressuretest_sleep_fast: ; get pressure without averaging (faster to save some power in sleep mode) |
320 banksel isr_backup ; back to Bank0 ISR data | 330 banksel isr_backup ; select bank ISR data |
321 clrf amb_pressure_avg+0 ; clear pressure average registers | 331 |
322 clrf amb_pressure_avg+1 | 332 CLRI pressure_abs_avg ; clear pressure average register |
323 clrf temperature_avg+0 ; clear temperature average registers | 333 CLRI temperature_avg ; clear temperature average register |
324 clrf temperature_avg+1 | 334 |
325 call get_temperature_start ; start temperature integration (73.5 us) | 335 call get_temperature_start ; start temperature integration (73.5 us) |
326 banksel common | 336 |
327 rcall sleepmode_sleep ; wait at least 35 ms (every 62.5 ms timer7 wakeup) | 337 rcall sleepmode_sleep ; wait at least 35 ms (every 62.5 ms timer7 wakeup) |
328 rcall sleepmode_sleep ; wait at least 35 ms (every 62.5 ms timer7 wakeup) | 338 rcall sleepmode_sleep ; wait at least 35 ms (every 62.5 ms timer7 wakeup) |
329 banksel isr_backup ; back to Bank0 ISR data | 339 |
330 call get_temperature_value ; state 1: get temperature | 340 call get_temperature_value ; state 1: get temperature |
331 call get_pressure_start ; start pressure integration | 341 call get_pressure_start ; start pressure integration |
332 banksel common | 342 |
333 rcall sleepmode_sleep ; wait at least 35 ms (every 62.5 ms timer7 wakeup) | 343 rcall sleepmode_sleep ; wait at least 35 ms (every 62.5 ms timer7 wakeup) |
334 rcall sleepmode_sleep ; wait at least 35 ms (every 62.5 ms timer7 wakeup) | 344 rcall sleepmode_sleep ; wait at least 35 ms (every 62.5 ms timer7 wakeup) |
335 banksel isr_backup ; back to bank0 ISR data | 345 |
336 call get_pressure_value ; state2: get pressure (51 us) | 346 call get_pressure_value ; state2: get pressure (51 us) |
337 call calculate_compensation ; calculate temperature compensated pressure (27 us) | 347 call calculate_compensation ; calculate temperature compensated pressure (27 us) |
338 banksel common | 348 |
339 SAFE_2BYTE_COPY amb_pressure_avg, amb_pressure ; copy for compatibility | 349 MOVII pressure_abs_avg,pressure_abs ; get result, bypassing the averaging |
340 return | 350 |
351 banksel common ; back to bank common | |
352 return | |
353 | |
341 | 354 |
342 sleepmode_sleep: | 355 sleepmode_sleep: |
343 banksel 0xF16 ; addresses F16h through F5Fh are also used by SFRs, but are not part of the access RAM | 356 movff BSR,BSR_backup ; backup BSR |
357 banksel T7GCON ; switch bank, T7* is outside access RAM | |
344 clrf T7GCON ; reset timer7 gate control register | 358 clrf T7GCON ; reset timer7 gate control register |
345 movlw b'10001101' ; 1:1 prescaler -> 2 seconds @ 32768 Hz, not synced | 359 movlw b'10001101' ; 1:1 prescaler -> 2 seconds @ 32768 Hz, not synced |
346 movwf T7CON | 360 movwf T7CON |
347 sleep | 361 sleep |
348 sleep | 362 sleep |
349 clrf T7GCON ; reset timer7 gate control register | 363 clrf T7GCON ; reset timer7 gate control register |
350 movlw b'10001001' ; 1:1 prescaler -> 2 seconds @ 32768Hz, synced | 364 movlw b'10001001' ; 1:1 prescaler -> 2 seconds @ 32768 Hz, synced |
351 movwf T7CON | 365 movwf T7CON |
352 banksel common ; back to bank1 | 366 movff BSR_backup,BSR ; restore BSR |
353 return | 367 return |
354 | 368 |
355 END | 369 END |