Mercurial > public > hwos_code
annotate src/sleepmode.asm @ 623:c40025d8e750
3.03 beta released
author | heinrichsweikamp |
---|---|
date | Mon, 03 Jun 2019 14:01:48 +0200 |
parents | ca4556fb60b9 |
children | cd58f7fc86db |
rev | line source |
---|---|
0 | 1 ;============================================================================= |
2 ; | |
623 | 3 ; File sleepmode.asm combined next generation V3.03.1 |
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" |
0 | 26 |
133
939f1e83c4c2
BUGFIX: Surface interval was not displayed correctly in some cases
heinrichsweikamp
parents:
123
diff
changeset
|
27 |
582 | 28 extern vault_decodata_into_eeprom |
623 | 29 extern power_up_switches ; from hwos.asm |
582 | 30 |
31 | |
623 | 32 ;---- Private local Variables ------------------------------------------------- |
133
939f1e83c4c2
BUGFIX: Surface interval was not displayed correctly in some cases
heinrichsweikamp
parents:
123
diff
changeset
|
33 |
582 | 34 CBLOCK local1 ; max size is 16 Byte !!! |
623 | 35 accel_reference ; acceleration reference value for detecting movement / terminating deep sleep |
36 sm_timer_10sec ; timer for 10 seconds tasks (pressure check) | |
37 sm_timer_10min ; timer for 10 minutes tasks (tissue updating) | |
38 sm_timer_15min ; timer for 15 minutes tasks (entering deep sleep) | |
39 loop_counter ; loop counter, used in init_avg_switches routine | |
40 sm_flags ; local flags | |
41 ENDC ; used: 6 byte, remaining: 10 byte | |
582 | 42 |
43 | |
623 | 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 | |
582 | 57 |
58 ;============================================================================== | |
0 | 59 |
60 global sleeploop | |
623 | 61 sleeploop: |
62 clrf STKPTR ; clear return addresses stack | |
63 call request_speed_normal ; request CPU speed switch to normal speed | |
64 | |
65 bcf LEDg ; turn off green LED / release reset to RX circuitry | |
66 bcf LEDr ; turn off red LED | |
604 | 67 |
68 IFDEF _screendump | |
623 | 69 bcf screen_dump_avail ; disable screen dump function |
70 ENDIF | |
71 | |
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 | |
604 | 78 ENDIF |
79 | |
80 clrf ADCON0 ; power-down ADC module | |
623 | 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 | |
490
8dfb93e80338
NEW: Deep Sleep mode for OSTC Plus and OSTC 2 (2017) (Entered automatically)
heinrichsweikamp
parents:
448
diff
changeset
|
86 |
623 | 87 call vault_decodata_into_eeprom ; store deco data |
88 call ext_flash_enable_protection ; enable write protection on external flash | |
89 call update_battery_registers ; update battery registers into EEPROM | |
582 | 90 |
623 | 91 clrf sm_timer_10sec ; clear 10 seconds timer |
92 clrf sm_timer_10min ; clear 10 minutes timer | |
93 clrf sm_timer_15min ; clear 15 minutes timer | |
94 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
|
95 |
0 | 96 sleeploop_loop: |
623 | 97 btfsc trigger_full_second ; one second in sleep? |
98 rcall one_sec_sleep ; YES - check switches, pressure sensor, etc. | |
582 | 99 |
623 | 100 btfss sleepmode ; shall terminate sleep mode? |
101 bra sleeploop_exit ; YES | |
0 | 102 |
623 | 103 rcall sleepmode_sleep ; wait at least 35 ms (every 62.5 ms timer7 wakeup) |
582 | 104 |
623 | 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 | |
0 | 108 |
623 | 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 | |
0 | 115 |
623 | 116 bcf power_sw1 ; power-down switch 1 |
117 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
|
118 |
604 | 119 rcall deepsleep_get_accel ; read accelerometer into WREG |
623 | 120 movwf accel_reference ; store as reference value |
490
8dfb93e80338
NEW: Deep Sleep mode for OSTC Plus and OSTC 2 (2017) (Entered automatically)
heinrichsweikamp
parents:
448
diff
changeset
|
121 |
582 | 122 deepsleep_loop: |
623 | 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 | |
582 | 168 |
623 | 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 | |
490
8dfb93e80338
NEW: Deep Sleep mode for OSTC Plus and OSTC 2 (2017) (Entered automatically)
heinrichsweikamp
parents:
448
diff
changeset
|
191 |
623 | 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 | |
490
8dfb93e80338
NEW: Deep Sleep mode for OSTC Plus and OSTC 2 (2017) (Entered automatically)
heinrichsweikamp
parents:
448
diff
changeset
|
214 |
623 | 215 ; the 15 minutes timer only ticks on OSTC with analog switches |
216 btfss analog_switches ; OSTC with analog switches? | |
217 bra one_min_sleep_1 ; NO - no analog switches, no deep sleep required | |
218 | |
219 ; the 15 minutes timer also ticks only when not in deep sleep | |
220 btfsc deep_sleep ; in deep sleep mode? | |
221 bra one_min_sleep_1 ; YES - already in deep sleep | |
222 | |
223 ; tick the 15 minutes timer | |
224 incf sm_timer_15min,F ; increment 15 minutes timer | |
225 movlw .15 ; load a 15 into WREG | |
226 cpfslt sm_timer_15min ; timer < 15 yet? | |
227 rcall fifteen_min_sleep ; NO - do the every 15 minute tasks | |
228 | |
229 one_min_sleep_1: | |
230 ; continue tasks every minute | |
231 btfsc desat_on_10_mins ; shall do desaturation calculation on 10 minute intervals? | |
232 return ; YES - that's not here then, so done | |
233 call deco_calc_dive_interval_1min ; NO - calculate 1 minute at surface conditions (C-code) | |
234 banksel common ; - back to bank common | |
235 return ; - done | |
236 | |
237 | |
238 ten_min_sleep: | |
239 ; tasks every 10 minutes in sleep mode | |
240 clrf sm_timer_10min ; reset timer to 0 | |
241 | |
242 call sample_surface_pressure ; sample surface pressure and update ISR and deco engine | |
243 | |
244 btfss desat_on_10_mins ; shall do desaturation calculation on 10 minute intervals? | |
245 bra ten_min_sleep_1 ; NO - continue checking if schedule can be switched to 10 minutes | |
246 call deco_calc_dive_interval_10min ; YES - calculate 10 minutes at surface conditions (C-code) | |
247 banksel common ; - back to bank common | |
248 return ; - done | |
249 | |
250 ten_min_sleep_1: | |
251 movff int_O_lead_supersat+0,WREG ; get leading tissue's supersaturation (only the lower byte is used for the value) | |
252 bsf desat_on_10_mins ; switch to 10 minute intervals by default | |
253 tstfsz WREG ; gradient factor = 0 ? | |
254 bcf desat_on_10_mins ; NO - stay on 1 minute intervals | |
255 return ; done | |
256 | |
582 | 257 |
623 | 258 fifteen_min_sleep: |
259 ; tasks every 15 minutes in sleep mode | |
260 clrf sm_timer_15min ; reset timer to 0 | |
261 bsf deep_sleep ; enable deep-sleep mode | |
262 return | |
263 | |
264 | |
265 one_hour_sleep: | |
266 ; tasks every hour in sleep mode mode | |
267 bcf trigger_full_hour ; clear one hour flag | |
268 call update_battery_registers ; update battery registers into EEPROM | |
269 call vault_decodata_into_eeprom ; update tissue pressures into EEPROM | |
270 return | |
271 | |
490
8dfb93e80338
NEW: Deep Sleep mode for OSTC Plus and OSTC 2 (2017) (Entered automatically)
heinrichsweikamp
parents:
448
diff
changeset
|
272 |
623 | 273 init_avg_switches: |
274 ; pause 4 seconds using CPU sleep mode to conserve on battery | |
275 movlw .4 ; time to pause | |
276 movwf loop_counter ; initialize loop counter | |
277 bcf trigger_full_second ; clear 'one second elapsed' flag | |
278 activate_switches_1: | |
279 rcall sleepmode_sleep ; wait at least 35 ms (every 62.5 ms timer7 wakeup) | |
280 btfss trigger_full_second ; did 1 second elapsed meanwhile? | |
281 bra activate_switches_1 ; NO - loop | |
282 bcf trigger_full_second ; YES - clear flag | |
283 decfsz loop_counter,F ; - decrement loop counter, done? | |
284 bra activate_switches_1 ; NO - loop | |
285 | |
286 ; initialize the averaging system | |
287 movlw .32 ; number of readout cycles | |
288 movwf loop_counter ; initialize loop counter | |
289 activate_switches_2: | |
290 call get_analog_switches ; do a analog switch readout | |
291 decfsz loop_counter,F ; decrement loop counter, done? | |
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: | |
604 | 308 rcall deepsleep_get_accel ; read accelerometer into WREG |
623 | 309 subwf accel_reference,W ; reference value - accel_DZ+0 -> WREG |
604 | 310 btfsc STATUS,N ; result negative? |
311 negf WREG ; YES - negate it | |
623 | 312 movwf lo ; save as change of acceleration in Z-axis |
313 movlw .50 ; load threshold (mg) | |
314 cpfslt lo ; change of acceleration > threshold ? | |
315 bcf deep_sleep ; YES - terminate deep sleep mode | |
316 return ; done | |
490
8dfb93e80338
NEW: Deep Sleep mode for OSTC Plus and OSTC 2 (2017) (Entered automatically)
heinrichsweikamp
parents:
448
diff
changeset
|
317 |
582 | 318 |
490
8dfb93e80338
NEW: Deep Sleep mode for OSTC Plus and OSTC 2 (2017) (Entered automatically)
heinrichsweikamp
parents:
448
diff
changeset
|
319 deepsleep_get_accel: |
623 | 320 call I2C_init_compass ; start compass, required for compass1 |
321 call I2C_init_accelerometer ; start accelerometer, required for compass2 | |
604 | 322 call I2C_RX_accelerometer ; read accelerometer |
623 | 323 call I2C_sleep_compass ; shut down compass, required for compass1 |
324 call I2C_sleep_accelerometer ; shut down accelerometer, required for compass2 | |
325 movff accel_DZ+0,WREG ; transfer result to WREG | |
326 return ; done | |
327 | |
328 | |
329 pressuretest_sleep_fast: ; get pressure without averaging (faster to save some power in sleep mode) | |
330 banksel isr_backup ; select bank ISR data | |
331 | |
332 CLRI pressure_abs_avg ; clear pressure average register | |
333 CLRI temperature_avg ; clear temperature average register | |
334 | |
335 call get_temperature_start ; start temperature integration (73.5 us) | |
582 | 336 |
623 | 337 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) | |
339 | |
340 call get_temperature_value ; state 1: get temperature | |
341 call get_pressure_start ; start pressure integration | |
342 | |
343 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) | |
345 | |
346 call get_pressure_value ; state2: get pressure (51 us) | |
347 call calculate_compensation ; calculate temperature compensated pressure (27 us) | |
348 | |
349 MOVII pressure_abs_avg,pressure_abs ; get result, bypassing the averaging | |
350 | |
351 banksel common ; back to bank common | |
582 | 352 return |
0 | 353 |
354 | |
355 sleepmode_sleep: | |
623 | 356 movff BSR,BSR_backup ; backup BSR |
357 banksel T7GCON ; switch bank, T7* is outside access RAM | |
604 | 358 clrf T7GCON ; reset timer7 gate control register |
359 movlw b'10001101' ; 1:1 prescaler -> 2 seconds @ 32768 Hz, not synced | |
582 | 360 movwf T7CON |
0 | 361 sleep |
198
c511dc403d7e
(Slightly) reduce current consumption in Eco and sleep mode
heinrichsweikamp
parents:
134
diff
changeset
|
362 sleep |
604 | 363 clrf T7GCON ; reset timer7 gate control register |
623 | 364 movlw b'10001001' ; 1:1 prescaler -> 2 seconds @ 32768 Hz, synced |
582 | 365 movwf T7CON |
623 | 366 movff BSR_backup,BSR ; restore BSR |
0 | 367 return |
368 | |
369 END |