comparison src/sleepmode.asm @ 628:cd58f7fc86db

3.05 stable work
author heinrichsweikamp
date Thu, 19 Sep 2019 12:01:29 +0200
parents c40025d8e750
children 237931377539
comparison
equal deleted inserted replaced
627:bf5fee575701 628:cd58f7fc86db
1 ;============================================================================= 1 ;=============================================================================
2 ; 2 ;
3 ; File sleepmode.asm combined next generation V3.03.1 3 ; File sleepmode.asm combined next generation V3.03.6
4 ; 4 ;
5 ; Sleep Mode 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 ;==============================================================================
21 #include "external_flash.inc" 21 #include "external_flash.inc"
22 #include "ghostwriter.inc" 22 #include "ghostwriter.inc"
23 #include "i2c.inc" 23 #include "i2c.inc"
24 #include "mcp.inc" 24 #include "mcp.inc"
25 #include "wait.inc" 25 #include "wait.inc"
26 #include "tft_outputs.inc"
26 27
27 28
28 extern vault_decodata_into_eeprom 29 extern vault_decodata_into_eeprom
29 extern power_up_switches ; from hwos.asm 30 extern power_up_switches ; from hwos.asm
30 31
33 34
34 CBLOCK local1 ; max size is 16 Byte !!! 35 CBLOCK local1 ; max size is 16 Byte !!!
35 accel_reference ; acceleration reference value for detecting movement / terminating deep sleep 36 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_10sec ; timer for 10 seconds tasks (pressure check)
37 sm_timer_10min ; timer for 10 minutes tasks (tissue updating) 38 sm_timer_10min ; timer for 10 minutes tasks (tissue updating)
38 sm_timer_15min ; timer for 15 minutes tasks (entering deep sleep) 39 ;sm_timer_15min ; timer for 15 minutes tasks (entering deep sleep)
40 sm_10min_counter ; count #of 10 minutes
39 loop_counter ; loop counter, used in init_avg_switches routine 41 loop_counter ; loop counter, used in init_avg_switches routine
40 sm_flags ; local flags 42 sm_flags ; local flags
41 ENDC ; used: 6 byte, remaining: 10 byte 43 ENDC ; used: 6 byte, remaining: 10 byte
42 44
43 45
44 ;---- Private local Flags ----------------------------------------------------- 46 ;---- Private local Flags -----------------------------------------------------
45 47
46 #DEFINE deep_sleep sm_flags,0 ; =1: in deep sleep mode, =0: normal sleep 48 #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 49 #DEFINE desat_on_10_mins sm_flags,1 ; =1: calculate desaturation every 10 minutes, =0: every minute
48 ; sm_flags,2 ; unused 50 #DEFINE charge_in_sleep sm_flags,2 ; =1: show charging screen
49 ; sm_flags,3 ; unused 51 ; sm_flags,3 ; unused
50 ; sm_flags,4 ; unused 52 ; sm_flags,4 ; unused
51 ; sm_flags,5 ; unused 53 ; sm_flags,5 ; unused
52 ; sm_flags,6 ; unused 54 ; sm_flags,6 ; unused
53 ; sm_flags,7 ; unused 55 ; sm_flags,7 ; unused
76 call disable_ir_s8 ; power-down IR/S8 interrupts 78 call disable_ir_s8 ; power-down IR/S8 interrupts
77 call mcp_sleep ; power-down RX power supply 79 call mcp_sleep ; power-down RX power supply
78 ENDIF 80 ENDIF
79 81
80 clrf ADCON0 ; power-down ADC module 82 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 83 call disable_rs232 ; power-down USB
84 call I2C_sleep_accelerometer ; power-down accelerometer
85 call I2C_sleep_compass ; power-down compass 84 call I2C_sleep_compass ; power-down compass
86 85
87 call vault_decodata_into_eeprom ; store deco data 86 call vault_decodata_into_eeprom ; store deco data
88 call ext_flash_enable_protection ; enable write protection on external flash 87 call ext_flash_enable_protection ; enable write protection on external flash
89 call update_battery_registers ; update battery registers into EEPROM 88 call update_battery_registers ; update battery registers into EEPROM
90 89
91 clrf sm_timer_10sec ; clear 10 seconds timer 90 clrf sm_timer_10sec ; clear 10 seconds timer
92 clrf sm_timer_10min ; clear 10 minutes timer 91 clrf sm_timer_10min ; clear 10 minutes timer
93 clrf sm_timer_15min ; clear 15 minutes timer 92 clrf sm_10min_counter ; clear 10mins counter
94 clrf sm_flags ; clear all local flags 93 clrf sm_flags ; clear all local flags
95 94
95 call TFT_Display_FadeOut ; power-down backlight
96 call TFT_DisplayOff ; power-down display
97
96 sleeploop_loop: 98 sleeploop_loop:
97 btfsc trigger_full_second ; one second in sleep? 99 btfsc trigger_full_second ; one second in sleep?
98 rcall one_sec_sleep ; YES - check switches, pressure sensor, etc. 100 rcall one_sec_sleep ; YES - check switches, pressure sensor, etc.
99 101
100 btfss sleepmode ; shall terminate sleep mode? 102 btfss sleepmode ; shall terminate sleep mode?
116 bcf power_sw1 ; power-down switch 1 118 bcf power_sw1 ; power-down switch 1
117 bcf power_sw2 ; power-down switch 2 119 bcf power_sw2 ; power-down switch 2
118 120
119 rcall deepsleep_get_accel ; read accelerometer into WREG 121 rcall deepsleep_get_accel ; read accelerometer into WREG
120 movwf accel_reference ; store as reference value 122 movwf accel_reference ; store as reference value
123 tstfsz accel_reference ; = 0 ?
124 bra deepsleep_loop ; NO - continue
125 bra deepsleep_loop_exit ; YES - no deep sleep (fail-safe)
121 126
122 deepsleep_loop: 127 deepsleep_loop:
123 btfsc trigger_full_second ; one second in deep sleep? 128 btfsc trigger_full_second ; one second in deep sleep?
124 rcall check_accelerometer ; YES - check accelerometer 129 rcall check_accelerometer ; YES - check accelerometer
125 130
132 bcf deep_sleep ; YES - leave deep sleep mode then, too 137 bcf deep_sleep ; YES - leave deep sleep mode then, too
133 138
134 btfsc deep_sleep ; shall leave deep sleep mode? 139 btfsc deep_sleep ; shall leave deep sleep mode?
135 bra deepsleep_loop ; NO - loop in deep sleep loop 140 bra deepsleep_loop ; NO - loop in deep sleep loop
136 141
142 deepsleep_loop_exit:
143 bcf deep_sleep ; clear flag (again)
144 clrf sm_10min_counter ; clear 10mins counter
137 call power_up_switches ; turn on the analog switches 145 call power_up_switches ; turn on the analog switches
138 rcall init_avg_switches ; initialize the averaging system 146 rcall init_avg_switches ; initialize the averaging system
139 147
140 bsf PIE1,0 ; enable timer 1 interrupt 148 bsf PIE1,0 ; enable timer 1 interrupt
141 bsf PIE2,1 ; enable timer 2 interrupt 149 bsf PIE2,1 ; enable timer 2 interrupt
172 bcf sleepmode ; YES - terminate sleep mode 180 bcf sleepmode ; YES - terminate sleep mode
173 bra one_sec_sleep_2 ; - continue 181 bra one_sec_sleep_2 ; - continue
174 182
175 one_sec_sleep_1: 183 one_sec_sleep_1:
176 call get_battery_voltage ; check for charger 184 call get_battery_voltage ; check for charger
185
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
177 211
178 one_sec_sleep_2: 212 one_sec_sleep_2:
179 incf sm_timer_10sec,F ; increment 10 seconds timer 213 incf sm_timer_10sec,F ; increment 10 seconds timer
180 movlw .10 ; load a 10 into WREG 214 movlw .10 ; load a 10 into WREG
181 cpfslt sm_timer_10sec ; timer < 10 yet? 215 cpfslt sm_timer_10sec ; timer < 10 yet?
210 incf sm_timer_10min,F ; increment 10 minutes timer 244 incf sm_timer_10min,F ; increment 10 minutes timer
211 movlw .10 ; load a 10 into WREG 245 movlw .10 ; load a 10 into WREG
212 cpfslt sm_timer_10min ; timer < 10 yet? 246 cpfslt sm_timer_10min ; timer < 10 yet?
213 rcall ten_min_sleep ; NO - do the every 10 minutes tasks 247 rcall ten_min_sleep ; NO - do the every 10 minutes tasks
214 248
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: 249 one_min_sleep_1:
230 ; continue tasks every minute 250 ; continue tasks every minute
231 btfsc desat_on_10_mins ; shall do desaturation calculation on 10 minute intervals? 251 btfsc desat_on_10_mins ; shall do desaturation calculation on 10 minute intervals?
232 return ; YES - that's not here then, so done 252 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) 253 call deco_calc_dive_interval_1min ; NO - calculate 1 minute at surface conditions (C-code)
239 ; tasks every 10 minutes in sleep mode 259 ; tasks every 10 minutes in sleep mode
240 clrf sm_timer_10min ; reset timer to 0 260 clrf sm_timer_10min ; reset timer to 0
241 261
242 call sample_surface_pressure ; sample surface pressure and update ISR and deco engine 262 call sample_surface_pressure ; sample surface pressure and update ISR and deco engine
243 263
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:
244 btfss desat_on_10_mins ; shall do desaturation calculation on 10 minute intervals? 279 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 280 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) 281 call deco_calc_dive_interval_10min ; YES - calculate 10 minutes at surface conditions (C-code)
247 banksel common ; - back to bank common 282 banksel common ; - back to bank common
248 return ; - done 283 return ; - done
253 tstfsz WREG ; gradient factor = 0 ? 288 tstfsz WREG ; gradient factor = 0 ?
254 bcf desat_on_10_mins ; NO - stay on 1 minute intervals 289 bcf desat_on_10_mins ; NO - stay on 1 minute intervals
255 return ; done 290 return ; done
256 291
257 292
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: 293 one_hour_sleep:
266 ; tasks every hour in sleep mode mode 294 ; tasks every hour in sleep mode mode
267 bcf trigger_full_hour ; clear one hour flag 295 bcf trigger_full_hour ; clear one hour flag
268 call update_battery_registers ; update battery registers into EEPROM 296 call update_battery_registers ; update battery registers into EEPROM
269 call vault_decodata_into_eeprom ; update tissue pressures into EEPROM 297 call vault_decodata_into_eeprom ; update tissue pressures into EEPROM
270 return 298 return ; done
271 299
272 300
273 init_avg_switches: 301 init_avg_switches:
274 ; pause 4 seconds using CPU sleep mode to conserve on battery 302 ; pause 4 seconds using CPU sleep mode to conserve on battery
275 movlw .4 ; time to pause 303 movlw .4 ; time to pause
308 rcall deepsleep_get_accel ; read accelerometer into WREG 336 rcall deepsleep_get_accel ; read accelerometer into WREG
309 subwf accel_reference,W ; reference value - accel_DZ+0 -> WREG 337 subwf accel_reference,W ; reference value - accel_DZ+0 -> WREG
310 btfsc STATUS,N ; result negative? 338 btfsc STATUS,N ; result negative?
311 negf WREG ; YES - negate it 339 negf WREG ; YES - negate it
312 movwf lo ; save as change of acceleration in Z-axis 340 movwf lo ; save as change of acceleration in Z-axis
313 movlw .50 ; load threshold (mg) 341 movlw .100 ; load threshold (mg)
314 cpfslt lo ; change of acceleration > threshold ? 342 cpfslt lo ; change of acceleration > threshold ?
315 bcf deep_sleep ; YES - terminate deep sleep mode 343 bcf deep_sleep ; YES - terminate deep sleep mode
316 return ; done 344 return ; done
317 345
318 346
319 deepsleep_get_accel: 347 deepsleep_get_accel:
320 call I2C_init_compass ; start compass, required for compass1 348 call I2C_init_compass ; start compass
321 call I2C_init_accelerometer ; start accelerometer, required for compass2 349 rcall sleepmode_sleep ; wait a little bit
322 call I2C_RX_accelerometer ; read accelerometer 350 call I2C_RX_accelerometer ; read accelerometer
323 call I2C_sleep_compass ; shut down compass, required for compass1 351 call I2C_RX_accelerometer ; read accelerometer
324 call I2C_sleep_accelerometer ; shut down accelerometer, required for compass2 352 call I2C_sleep_compass ; shut down compass
325 movff accel_DZ+0,WREG ; transfer result to WREG 353 movff accel_DZ+0,WREG ; transfer result to WREG
326 return ; done 354 return ; done
327 355
328 356
329 pressuretest_sleep_fast: ; get pressure without averaging (faster to save some power in sleep mode) 357 pressuretest_sleep_fast: ; get pressure without averaging (faster to save some power in sleep mode)
352 return 380 return
353 381
354 382
355 sleepmode_sleep: 383 sleepmode_sleep:
356 movff BSR,BSR_backup ; backup BSR 384 movff BSR,BSR_backup ; backup BSR
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
357 banksel T7GCON ; switch bank, T7* is outside access RAM 390 banksel T7GCON ; switch bank, T7* is outside access RAM
358 clrf T7GCON ; reset timer7 gate control register 391 clrf T7GCON ; reset timer7 gate control register
359 movlw b'10001101' ; 1:1 prescaler -> 2 seconds @ 32768 Hz, not synced 392 movlw b'10001101' ; 1:1 prescaler -> 2 seconds @ 32768 Hz, not synced
360 movwf T7CON 393 movwf T7CON
361 sleep 394 sleep
362 sleep 395 sleep
363 clrf T7GCON ; reset timer7 gate control register 396 clrf T7GCON ; reset timer7 gate control register
364 movlw b'10001001' ; 1:1 prescaler -> 2 seconds @ 32768 Hz, synced 397 movlw b'10001001' ; 1:1 prescaler -> 2 seconds @ 32768 Hz, synced
365 movwf T7CON 398 movwf T7CON
399 sleepmode_sleep_1:
366 movff BSR_backup,BSR ; restore BSR 400 movff BSR_backup,BSR ; restore BSR
367 return 401 return
368 402
403
404 sleepmode_sleepwalk:
405 WAITMS d'65'
406 bra sleepmode_sleep_1
407
369 END 408 END