comparison src/sleepmode.asm @ 634:4050675965ea

3.10 stable release
author heinrichsweikamp
date Tue, 28 Apr 2020 17:34:31 +0200
parents 185ba2f91f59
children 8c1f1f334275
comparison
equal deleted inserted replaced
633:690c48db7b5b 634:4050675965ea
1 ;============================================================================= 1 ;=============================================================================
2 ; 2 ;
3 ; File sleepmode.asm combined next generation V3.08.8 3 ; File sleepmode.asm * combined next generation V3.09.4n
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 ;==============================================================================
54 ; sm_flags,5 ; unused 54 ; sm_flags,5 ; unused
55 ; sm_flags,6 ; unused 55 ; sm_flags,6 ; unused
56 ; sm_flags,7 ; unused 56 ; sm_flags,7 ; unused
57 57
58 58
59 ;=============================================================================
59 slmode CODE 60 slmode CODE
60
61 ;============================================================================== 61 ;==============================================================================
62 62
63
64 ;-----------------------------------------------------------------------------
65 ; Entry Point for Sleep Mode
66 ;
63 global sleeploop 67 global sleeploop
64 sleeploop: 68 sleeploop:
65 clrf STKPTR ; clear return addresses stack 69 clrf STKPTR ; clear return addresses stack
66 call request_speed_normal ; request CPU speed switch to normal speed 70 call request_speed_normal ; request CPU speed switch to normal speed
67 71
73 ENDIF 77 ENDIF
74 78
75 bsf sleepmode ; flag being in sleep mode 79 bsf sleepmode ; flag being in sleep mode
76 bsf block_sensor_interrupt ; suspend ISR from executing sensor interrupts 80 bsf block_sensor_interrupt ; suspend ISR from executing sensor interrupts
77 81
78 call disable_ir_s8 ; power-down IR/S8 interrupts 82 call disable_ir_s8_analog ; power-down IR/S8/analog interface
79 call mcp_sleep ; power-down RX power supply 83 call mcp_sleep ; power-down RX power supply
80 84
81 clrf ADCON0 ; power-down ADC module 85 clrf ADCON0 ; power-down ADC module
82 call disable_rs232 ; power-down USB 86 call disable_rs232 ; power-down USB
83 call I2C_sleep_compass ; power-down compass 87 call I2C_sleep_compass ; power-down compass
84 88
85 call eeprom_deco_data_write ; update deco data in EEPROM 89 call eeprom_deco_data_write ; update deco data in EEPROM
86 call eeprom_battery_gauge_write ; update battery gauge in EEPROM 90 call eeprom_battery_gauge_write ; update battery gauge in EEPROM
87 91
88 btfsc options_changed ; do the options need to be stored to EEPROM ? 92 btfsc option_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 93 call option_check_and_store_all ; YES - check and store all option values in EEPROM
90 bcf options_changed ; clear flag
91 94
92 call ext_flash_enable_protection ; enable write protection on external flash 95 call ext_flash_enable_protection ; enable write protection on external flash
93 96
94 clrf sm_timer_10sec ; clear 10 seconds timer 97 clrf sm_timer_10sec ; clear 10 seconds timer
95 clrf sm_timer_10min ; clear 10 minutes timer 98 clrf sm_timer_10min ; clear 10 minutes timer
104 rcall one_sec_sleep ; YES - check switches, pressure sensor, etc. 107 rcall one_sec_sleep ; YES - check switches, pressure sensor, etc.
105 108
106 btfss sleepmode ; shall terminate sleep mode? 109 btfss sleepmode ; shall terminate sleep mode?
107 bra sleeploop_exit ; YES 110 bra sleeploop_exit ; YES
108 111
109 rcall sleepmode_sleep ; wait at least 35 ms (every 62.5 ms timer7 wakeup) 112 rcall sleepmode_sleep ; sleep for 62.5...125 ms
110 113
111 btfss deep_sleep ; shall enter deep sleep? 114 btfss deep_sleep ; shall enter deep sleep?
112 bra sleeploop_loop ; NO - remain in normal sleep loop 115 bra sleeploop_loop ; NO - remain in normal sleep loop
113 ;bra deepsleep_pre ; YES - enter deep sleep loop 116 ;bra deepsleep_pre ; YES - enter deep sleep loop
114 117
115 deepsleep_pre: 118 deepsleep_pre:
116 bcf PIE1,0 ; disable timer 1 interrupt 119 bcf PIE1,TMR1IE ; disable timer 1 interrupt
117 bcf PIE2,1 ; disable timer 2 interrupt 120 bcf PIE2,TMR3IE ; disable timer 2 interrupt
118 bcf PIE5,3 ; disable timer 7 interrupt 121 bcf PIE5,TMR7IE ; disable timer 7 interrupt
119 bcf INTCON,4 ; disable INT0 interrupt 122 bcf INTCON, INT0IE ; disable INT0 interrupt
120 bcf INTCON3,3 ; disable INT1 interrupt 123 bcf INTCON3,INT1IE ; disable INT1 interrupt
121 124
122 bcf power_sw1 ; power-down switch 1 125 bcf power_sw1 ; power-down switch 1
123 bcf power_sw2 ; power-down switch 2 126 bcf power_sw2 ; power-down switch 2
124 127
125 rcall deepsleep_get_accel ; read accelerometer into WREG 128 rcall deepsleep_get_accel ; read accelerometer into WREG
133 rcall check_accelerometer ; YES - check accelerometer 136 rcall check_accelerometer ; YES - check accelerometer
134 137
135 btfsc trigger_full_second ; one second in deep sleep? 138 btfsc trigger_full_second ; one second in deep sleep?
136 rcall one_sec_sleep ; YES - check switches, check pressure sensor, etc. 139 rcall one_sec_sleep ; YES - check switches, check pressure sensor, etc.
137 140
138 rcall sleepmode_sleep ; wait at least 35 ms (every 62.5 ms timer7 wakeup) 141 rcall sleepmode_sleep ; sleep for 62.5...125 ms
139 142
140 btfss sleepmode ; shall leave sleep mode? 143 btfss sleepmode ; shall leave sleep mode?
141 bcf deep_sleep ; YES - leave deep sleep mode then, too 144 bcf deep_sleep ; YES - leave deep sleep mode then, too
142 145
143 btfsc deep_sleep ; shall leave deep sleep mode? 146 btfsc deep_sleep ; shall leave deep sleep mode?
147 bcf deep_sleep ; clear flag (again) 150 bcf deep_sleep ; clear flag (again)
148 clrf sm_10min_counter ; clear 10mins counter 151 clrf sm_10min_counter ; clear 10mins counter
149 call power_up_switches ; turn on the analog switches 152 call power_up_switches ; turn on the analog switches
150 rcall init_avg_switches ; initialize the averaging system 153 rcall init_avg_switches ; initialize the averaging system
151 154
152 bsf PIE1,0 ; enable timer 1 interrupt 155 bsf PIE1,TMR1IE ; enable timer 1 interrupt
153 bsf PIE2,1 ; enable timer 2 interrupt 156 bsf PIE2,TMR3IE ; enable timer 2 interrupt
154 bsf PIE5,3 ; enable timer 7 interrupt 157 bsf PIE5,TMR7IE ; enable timer 7 interrupt
155 bsf INTCON,4 ; enable INT0 interrupt 158 bsf INTCON, INT0IE ; enable INT0 interrupt
156 bsf INTCON3,3 ; enable INT1 interrupt 159 bsf INTCON3,INT1IE ; enable INT1 interrupt
157 160
158 bra sleeploop_loop ; enter normal sleep loop 161 bra sleeploop_loop ; enter normal sleep loop
159
160 162
161 sleeploop_exit: 163 sleeploop_exit:
162 bcf switch_left ; eventually clear left button event 164 bcf switch_left ; eventually clear left button event
163 bcf switch_right ; eventually clear right button event 165 bcf switch_right ; eventually clear right button event
164 movlw .0 ; reset ISR sensor state machine 166 movlw .0 ; reset ISR sensor state machine
166 bcf PIR5,TMR7IF ; clear timer 7, driving the ISR sensor interrupts 168 bcf PIR5,TMR7IF ; clear timer 7, driving the ISR sensor interrupts
167 bcf block_sensor_interrupt ; re-enable execution of the ISR sensor interrupts 169 bcf block_sensor_interrupt ; re-enable execution of the ISR sensor interrupts
168 goto restart ; restart 170 goto restart ; restart
169 171
170 172
173 ;-----------------------------------------------------------------------------
174 ; Helper Function - Tasks every Second in Sleep Mode
175 ;
171 one_sec_sleep: 176 one_sec_sleep:
172 ; tasks every second in sleep mode
173 bcf trigger_full_second ; clear trigger flag 177 bcf trigger_full_second ; clear trigger flag
174 178
175 btfsc switch_left ; left switch pressed? 179 btfsc switch_left ; left switch pressed?
176 bcf sleepmode ; YES - terminate sleep mode 180 bcf sleepmode ; YES - terminate sleep mode
177 181
193 btfss cc_active ; charging? 197 btfss cc_active ; charging?
194 bra one_sec_sleep_1a ; NO 198 bra one_sec_sleep_1a ; NO
195 btfsc charge_in_sleep ; YES - already showing charge screen? 199 btfsc charge_in_sleep ; YES - already showing charge screen?
196 bra one_sec_sleep_1b ; YES - only update data 200 bra one_sec_sleep_1b ; YES - only update data
197 201
198 bsf charge_in_sleep 202 bsf charge_in_sleep
199 bcf deep_sleep ; wake-up from deepsleep 203 bcf deep_sleep ; wake-up from deepsleep
200 call TFT_boot ; initialize TFT (includes clear screen) 204 call TFT_boot ; initialize TFT (includes clear screen)
201 movlw .32 205 movlw .32
202 movff WREG,max_CCPR1L ; bank safe 206 movff WREG,max_CCPR1L ; bank safe
203 call TFT_Display_FadeIn ; dim up the display 207 call TFT_Display_FadeIn ; dim up the display
204 one_sec_sleep_1b: 208 one_sec_sleep_1b:
205 call TFT_batt_surfmode ; show battery type, voltage and color-coded percentage 209 call TFT_surfmode_batt ; show battery type, voltage and color-coded percentage
206 bra one_sec_sleep_2 ; continue 210 bra one_sec_sleep_2 ; continue
207 211
208 one_sec_sleep_1a: 212 one_sec_sleep_1a:
209 btfss charge_in_sleep ; was showing charge screen? 213 btfss charge_in_sleep ; was showing charge screen?
210 bra one_sec_sleep_2 ; NO 214 bra one_sec_sleep_2 ; NO
228 rcall one_hour_sleep ; YES - do the every hour tasks 232 rcall one_hour_sleep ; YES - do the every hour tasks
229 233
230 return ; done 234 return ; done
231 235
232 236
237 ;-----------------------------------------------------------------------------
238 ; Helper Function - Tasks every 10 Seconds in Sleep Mode
239 ;
233 ten_sec_sleep: 240 ten_sec_sleep:
234 ; tasks every 10 seconds in sleep mode 241 ; tasks every 10 seconds in sleep mode
235 clrf sm_timer_10sec ; clear timer 242 clrf sm_timer_10sec ; clear timer
236 rcall pressuretest_sleep_fast ; get pressure without averaging (faster) 243 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 244 MOVLI wake_up_from_sleep,sub_a ; load wake-up pressure (1160 mbar) into sub_a
240 btfsc neg_flag ; is the current absolute pressure > 1160 mbar ? 247 btfsc neg_flag ; is the current absolute pressure > 1160 mbar ?
241 bcf sleepmode ; YES - terminate sleep mode 248 bcf sleepmode ; YES - terminate sleep mode
242 return ; done 249 return ; done
243 250
244 251
252 ;-----------------------------------------------------------------------------
253 ; Helper Function - Tasks every Minute in Sleep Mode
254 ;
245 one_min_sleep: 255 one_min_sleep:
246 ; tasks every minute in sleep mode 256 ; tasks every minute in sleep mode
247 bcf trigger_full_minute ; clear flag 257 bcf trigger_full_minute ; clear flag
248 258
249 ; tick the 10 minutes timer 259 ; tick the 10 minutes timer
259 call deco_calc_dive_interval_1min ; NO - calculate 1 minute at surface conditions (C-code) 269 call deco_calc_dive_interval_1min ; NO - calculate 1 minute at surface conditions (C-code)
260 banksel common ; - back to bank common 270 banksel common ; - back to bank common
261 return ; - done 271 return ; - done
262 272
263 273
274 ;-----------------------------------------------------------------------------
275 ; Helper Function - Tasks every 10 Minutes in Sleep Mode
276 ;
264 ten_min_sleep: 277 ten_min_sleep:
265 ; tasks every 10 minutes in sleep mode 278 ; tasks every 10 minutes in sleep mode
266 clrf sm_timer_10min ; reset timer to 0 279 clrf sm_timer_10min ; reset timer to 0
267 280
268 call sample_surface_pressure ; sample surface pressure and update ISR and deco engine 281 call sample_surface_pressure ; sample surface pressure and update ISR and deco engine
294 tstfsz WREG ; gradient factor = 0 ? 307 tstfsz WREG ; gradient factor = 0 ?
295 bcf desat_on_10_mins ; NO - stay on 1 minute intervals 308 bcf desat_on_10_mins ; NO - stay on 1 minute intervals
296 return ; done 309 return ; done
297 310
298 311
312 ;-----------------------------------------------------------------------------
313 ; Helper Function - Tasks every Hour in Sleep Mode
314 ;
299 one_hour_sleep: 315 one_hour_sleep:
300 ; tasks every hour in sleep mode mode 316 ; tasks every hour in sleep mode mode
301 bcf trigger_full_hour ; clear one hour flag 317 bcf trigger_full_hour ; clear one hour flag
302 call eeprom_deco_data_write ; update deco data into EEPROM 318 call eeprom_deco_data_write ; update deco data into EEPROM
303 call eeprom_battery_gauge_write ; update battery gauge into EEPROM 319 call eeprom_battery_gauge_write ; update battery gauge into EEPROM
304 return ; done 320 return ; done
305 321
306 322
323 ;-----------------------------------------------------------------------------
324 ; Helper Function - Initialization of the Button Event Averaging System
325 ;
307 init_avg_switches: 326 init_avg_switches:
308 ; pause 4 seconds using CPU sleep mode to conserve on battery 327 ; pause 4 seconds using CPU sleep mode to conserve on battery
309 movlw .4 ; time to pause 328 movlw .4 ; time to pause
310 movwf loop_counter ; initialize loop counter 329 movwf loop_counter ; initialize loop counter
311 bcf trigger_full_second ; clear 'one second elapsed' flag 330 bcf trigger_full_second ; clear 'one second elapsed' flag
312 activate_switches_1: 331 activate_switches_1:
313 rcall sleepmode_sleep ; wait at least 35 ms (every 62.5 ms timer7 wakeup) 332 rcall sleepmode_sleep ; sleep for 62.5...125 ms
314 btfss trigger_full_second ; did 1 second elapsed meanwhile? 333 btfss trigger_full_second ; did 1 second elapsed meanwhile?
315 bra activate_switches_1 ; NO - loop 334 bra activate_switches_1 ; NO - loop
316 bcf trigger_full_second ; YES - clear flag 335 bcf trigger_full_second ; YES - clear flag
317 decfsz loop_counter,F ; - decrement loop counter, done? 336 decfsz loop_counter,F ; - decrement loop counter, done?
318 bra activate_switches_1 ; NO - loop 337 bra activate_switches_1 ; NO - loop
325 decfsz loop_counter,F ; decrement loop counter, done? 344 decfsz loop_counter,F ; decrement loop counter, done?
326 bra activate_switches_2 ; NO - loop 345 bra activate_switches_2 ; NO - loop
327 346
328 ; clear all button events that may have intermediately occurred 347 ; clear all button events that may have intermediately occurred
329 bcf PIR1,TMR1IF ; clear button-hold-down timer 348 bcf PIR1,TMR1IF ; clear button-hold-down timer
330 bcf INTCON,INT0IF ; clear right button activity 349 bcf INTCON, INT0IF ; clear right button activity
331 bcf INTCON3,INT1IF ; clear left button activity 350 bcf INTCON3,INT1IF ; clear left button activity
332 bcf analog_sw1_pressed ; clear analog switch 1 activity 351 bcf analog_sw1_pressed ; clear analog switch 1 activity
333 bcf analog_sw2_pressed ; clear analog switch 2 activity 352 bcf analog_sw2_pressed ; clear analog switch 2 activity
334 bcf switch_right ; clear right button event 353 bcf switch_right ; clear right button event
335 bcf switch_left ; clear left button event 354 bcf switch_left ; clear left button event
336 355
337 ; done 356 return ; done
338 return 357
339 358
340 359 ;-----------------------------------------------------------------------------
360 ; Helper Function - Check for Change in Accelerometer Z-Axis
361 ;
341 check_accelerometer: 362 check_accelerometer:
342 rcall deepsleep_get_accel ; read accelerometer into WREG 363 rcall deepsleep_get_accel ; read accelerometer into WREG
343 subwf accel_reference,W ; reference value - accel_DZ+0 -> WREG 364 subwf accel_reference,W ; reference value - accel_DZ+0 -> WREG
344 btfsc STATUS,N ; result negative? 365 btfsc STATUS,N ; result negative?
345 negf WREG ; YES - negate it 366 negf WREG ; YES - negate it
348 cpfslt lo ; change of acceleration > threshold ? 369 cpfslt lo ; change of acceleration > threshold ?
349 bcf deep_sleep ; YES - terminate deep sleep mode 370 bcf deep_sleep ; YES - terminate deep sleep mode
350 return ; done 371 return ; done
351 372
352 373
374 ;-----------------------------------------------------------------------------
375 ; Helper Function - read Accelerometer Z-Axis
376 ;
353 deepsleep_get_accel: 377 deepsleep_get_accel:
354 call I2C_init_compass ; start compass 378 call I2C_init_compass ; start compass
355 rcall sleepmode_sleep ; wait a little bit 379 rcall sleepmode_sleep ; sleep for 62.5...125 ms
356 call I2C_RX_accelerometer ; read accelerometer 380 call I2C_RX_accelerometer ; read accelerometer
357 call I2C_RX_accelerometer ; read accelerometer 381 call I2C_RX_accelerometer ; read accelerometer
358 call I2C_sleep_compass ; shut down compass 382 call I2C_sleep_compass ; shut down compass
359 movff accel_DZ+0,WREG ; transfer result to WREG 383 movff accel_DZ+0,WREG ; transfer result to WREG
360 return ; done 384 return ; done
361 385
362 386
363 pressuretest_sleep_fast: ; get pressure without averaging (faster to save some power in sleep mode) 387 ;-----------------------------------------------------------------------------
388 ; Helper Function - read Pressure without Averaging
389 ;
390 ; faster method to save some power in sleep mode
391 ;
392 pressuretest_sleep_fast:
364 banksel isr_backup ; select bank ISR data 393 banksel isr_backup ; select bank ISR data
365 394
366 CLRI pressure_abs_avg ; clear pressure average register 395 CLRI pressure_abs_avg ; clear pressure average register
367 CLRI temperature_avg ; clear temperature average register 396 CLRI temperature_avg ; clear temperature average register
368 397
369 call get_temperature_start ; start temperature integration (73.5 us) 398 call get_temperature_start ; start temperature integration (73.5 us)
370 399
371 rcall sleepmode_sleep ; wait at least 35 ms (every 62.5 ms timer7 wakeup) 400 rcall sleepmode_sleep ; sleep for 62.5...125 ms
372 rcall sleepmode_sleep ; wait at least 35 ms (every 62.5 ms timer7 wakeup) 401 rcall sleepmode_sleep ; sleep for 62.5...125 ms
373 402
374 call get_temperature_value ; state 1: get temperature 403 call get_temperature_value ; state 1: get temperature
375 call get_pressure_start ; start pressure integration 404 call get_pressure_start ; start pressure integration
376 405
377 rcall sleepmode_sleep ; wait at least 35 ms (every 62.5 ms timer7 wakeup) 406 rcall sleepmode_sleep ; sleep for 62.5...125 ms
378 rcall sleepmode_sleep ; wait at least 35 ms (every 62.5 ms timer7 wakeup) 407 rcall sleepmode_sleep ; sleep for 62.5...125 ms
379 408
380 call get_pressure_value ; state2: get pressure (51 us) 409 call get_pressure_value ; state2: get pressure (51 us)
381 call calculate_compensation ; calculate temperature compensated pressure (27 us) 410 call calculate_compensation ; calculate temperature compensated pressure (27 us)
382 411
383 MOVII pressure_abs_avg,pressure_abs ; get result, bypassing the averaging 412 MOVII pressure_abs_avg,pressure_abs ; get result, bypassing the averaging
384 413
385 banksel common ; back to bank common 414 banksel common ; back to bank common
386 return 415 return
387 416
388 417
418 ;-----------------------------------------------------------------------------
419 ; Helper Function - sleep for 62.5...125 ms
420 ;
389 sleepmode_sleep: 421 sleepmode_sleep:
390 movff BSR,BSR_backup ; backup BSR 422 movff BSR,BSR_backup ; backup current bank selection
391 423 banksel common ; switch to bank common
392 banksel common
393 btfsc charge_in_sleep ; already showing charge screen? 424 btfsc charge_in_sleep ; already showing charge screen?
394 bra sleepmode_sleepwalk ; YES - skip the actual sleep (But wait) 425 bra sleepmode_sleepwalk ; YES - skip the actual sleep (but wait)
395 426 sleep ; NO - put CPU into sleep (wakes up by timer 7 IRQ)
396 ; banksel T7GCON ; switch bank, T7* is outside access RAM 427 sleep ; - put CPU into sleep again
397 ; clrf T7GCON ; reset timer7 gate control register
398 ; movlw b'10001101' ; 1:1 prescaler -> 2 seconds @ 32768 Hz, not synced
399 ; movwf T7CON
400 sleep
401 sleep
402 ; clrf T7GCON ; reset timer7 gate control register
403 ; movlw b'10001001' ; 1:1 prescaler -> 2 seconds @ 32768 Hz, synced
404 ; movwf T7CON
405 sleepmode_sleep_1: 428 sleepmode_sleep_1:
406 movff BSR_backup,BSR ; restore BSR 429 movff BSR_backup,BSR ; restore bank selection
407 return 430 return ; done
408
409 431
410 sleepmode_sleepwalk: 432 sleepmode_sleepwalk:
411 WAITMS d'65' 433 WAITMS d'65' ; wait 65 ms
412 bra sleepmode_sleep_1 434 bra sleepmode_sleep_1 ; continue with common part
435
436 ;-----------------------------------------------------------------------------
413 437
414 END 438 END