annotate src/sleepmode.asm @ 410:d3087a8ed7e1

BUGFIX: Fix rare issue after battery change (OSTC3 did not start properly)
author heinrichsweikamp
date Tue, 08 Mar 2016 11:42:58 +0100
parents 653a3ab08062
children 4389fe9673b2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
heinrichsweikamp
parents:
diff changeset
1 ;=============================================================================
heinrichsweikamp
parents:
diff changeset
2 ;
heinrichsweikamp
parents:
diff changeset
3 ; File sleepmode.asm
heinrichsweikamp
parents:
diff changeset
4 ;
heinrichsweikamp
parents:
diff changeset
5 ; Sleepmode
heinrichsweikamp
parents:
diff changeset
6 ;
heinrichsweikamp
parents:
diff changeset
7 ; Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved.
heinrichsweikamp
parents:
diff changeset
8 ;=============================================================================
heinrichsweikamp
parents:
diff changeset
9 ; HISTORY
heinrichsweikamp
parents:
diff changeset
10 ; 2011-08-12 : [mH] moving from OSTC code
heinrichsweikamp
parents:
diff changeset
11
275
653a3ab08062 rename into hwOS
heinrichsweikamp
parents: 235
diff changeset
12 #include "hwos.inc" ; Mandatory header
0
heinrichsweikamp
parents:
diff changeset
13 #include "shared_definitions.h" ; Mailbox from/to p2_deco.c
heinrichsweikamp
parents:
diff changeset
14 #include "surfmode.inc"
heinrichsweikamp
parents:
diff changeset
15 #include "tft.inc"
heinrichsweikamp
parents:
diff changeset
16 #include "isr.inc"
heinrichsweikamp
parents:
diff changeset
17 #include "start.inc"
heinrichsweikamp
parents:
diff changeset
18 #include "adc_lightsensor.inc"
heinrichsweikamp
parents:
diff changeset
19 #include "math.inc"
heinrichsweikamp
parents:
diff changeset
20 #include "ms5541.inc"
heinrichsweikamp
parents:
diff changeset
21 #include "wait.inc"
heinrichsweikamp
parents:
diff changeset
22 #include "eeprom_rs232.inc"
heinrichsweikamp
parents:
diff changeset
23 #include "external_flash.inc"
heinrichsweikamp
parents:
diff changeset
24 #include "ghostwriter.inc"
heinrichsweikamp
parents:
diff changeset
25 #include "i2c.inc"
28
heinrichsweikamp
parents: 0
diff changeset
26 #include "mcp.inc"
0
heinrichsweikamp
parents:
diff changeset
27
133
939f1e83c4c2 BUGFIX: Surface interval was not displayed correctly in some cases
heinrichsweikamp
parents: 123
diff changeset
28
939f1e83c4c2 BUGFIX: Surface interval was not displayed correctly in some cases
heinrichsweikamp
parents: 123
diff changeset
29 extern vault_decodata_into_eeprom
939f1e83c4c2 BUGFIX: Surface interval was not displayed correctly in some cases
heinrichsweikamp
parents: 123
diff changeset
30
0
heinrichsweikamp
parents:
diff changeset
31 gui CODE
heinrichsweikamp
parents:
diff changeset
32
heinrichsweikamp
parents:
diff changeset
33 global sleeploop
heinrichsweikamp
parents:
diff changeset
34 sleeploop: ; enter sleepmode!
224
5a4801918be9 temporally disable "Copy disable flags from digital input" routine
heinrichsweikamp
parents: 205
diff changeset
35 call disable_ir_s8 ; IR/S8 off
28
heinrichsweikamp
parents: 0
diff changeset
36 call mcp_sleep
0
heinrichsweikamp
parents:
diff changeset
37 bcf LEDg
heinrichsweikamp
parents:
diff changeset
38 bcf LEDr
39
e4e91fe8b09d remember last customview in dive and surface mode
mh@mh-THINK.fritz.box
parents: 28
diff changeset
39 movff menupos3,customview_surfmode; save last customview
0
heinrichsweikamp
parents:
diff changeset
40 call TFT_Display_FadeOut
heinrichsweikamp
parents:
diff changeset
41 call TFT_DisplayOff ; display off
heinrichsweikamp
parents:
diff changeset
42 call disable_rs232 ; USB off
133
939f1e83c4c2 BUGFIX: Surface interval was not displayed correctly in some cases
heinrichsweikamp
parents: 123
diff changeset
43 call vault_decodata_into_eeprom ; store deco data
0
heinrichsweikamp
parents:
diff changeset
44 call I2C_sleep_accelerometer
heinrichsweikamp
parents:
diff changeset
45 call I2C_sleep_compass
heinrichsweikamp
parents:
diff changeset
46 call ext_flash_enable_protection ; enable write protection for external flash
heinrichsweikamp
parents:
diff changeset
47 call update_battery_registers ; update battery registers into EEPROM
heinrichsweikamp
parents:
diff changeset
48 clrf divemins+0
heinrichsweikamp
parents:
diff changeset
49 clrf divemins+1
heinrichsweikamp
parents:
diff changeset
50 call speed_normal
heinrichsweikamp
parents:
diff changeset
51 bsf no_sensor_int ; No sensor interrupt
heinrichsweikamp
parents:
diff changeset
52 bcf enable_screen_dumps ; =1: Ignore vin_usb, wait for "l" command (Screen dump)
heinrichsweikamp
parents:
diff changeset
53 clrf ADCON0 ; Power-Down ADC Module
heinrichsweikamp
parents:
diff changeset
54 sleeploop_loop:
134
4574aed5cd4c Show clock in Apnea mode, minor layout changes in Apnea mode
heinrichsweikamp
parents: 133
diff changeset
55 btfsc onesecupdate ; one second in sleep?
4574aed5cd4c Show clock in Apnea mode, minor layout changes in Apnea mode
heinrichsweikamp
parents: 133
diff changeset
56 rcall onesec_sleep ; check switches, check pressure sensor, etc.
0
heinrichsweikamp
parents:
diff changeset
57
heinrichsweikamp
parents:
diff changeset
58 btfss sleepmode ; wake up? (This bit will be set in other routines)
heinrichsweikamp
parents:
diff changeset
59 goto restart ; yes
heinrichsweikamp
parents:
diff changeset
60
heinrichsweikamp
parents:
diff changeset
61 rcall sleepmode_sleep ; Wait at least 35ms (every 62,5ms Timer7 wakeup)
heinrichsweikamp
parents:
diff changeset
62
heinrichsweikamp
parents:
diff changeset
63 ; Any button pressed in sleep?
heinrichsweikamp
parents:
diff changeset
64 btfsc switch_left
heinrichsweikamp
parents:
diff changeset
65 rcall onesec_sleep1a
heinrichsweikamp
parents:
diff changeset
66 btfsc switch_right
heinrichsweikamp
parents:
diff changeset
67 rcall onesec_sleep1a
heinrichsweikamp
parents:
diff changeset
68
heinrichsweikamp
parents:
diff changeset
69 btfss sleepmode ; wake up? (This bit will be set in other routines)
heinrichsweikamp
parents:
diff changeset
70 goto restart ; yes
heinrichsweikamp
parents:
diff changeset
71
heinrichsweikamp
parents:
diff changeset
72 bra sleeploop_loop ; do loop until someting happens
heinrichsweikamp
parents:
diff changeset
73
134
4574aed5cd4c Show clock in Apnea mode, minor layout changes in Apnea mode
heinrichsweikamp
parents: 133
diff changeset
74 onehour_sleep:
4574aed5cd4c Show clock in Apnea mode, minor layout changes in Apnea mode
heinrichsweikamp
parents: 133
diff changeset
75 call update_battery_registers ; update battery registers into EEPROM
4574aed5cd4c Show clock in Apnea mode, minor layout changes in Apnea mode
heinrichsweikamp
parents: 133
diff changeset
76 call vault_decodata_into_eeprom ; update deco data
4574aed5cd4c Show clock in Apnea mode, minor layout changes in Apnea mode
heinrichsweikamp
parents: 133
diff changeset
77 bcf onehourupdate ; all done
4574aed5cd4c Show clock in Apnea mode, minor layout changes in Apnea mode
heinrichsweikamp
parents: 133
diff changeset
78 return
0
heinrichsweikamp
parents:
diff changeset
79
heinrichsweikamp
parents:
diff changeset
80 onemin_sleep:
198
c511dc403d7e (Slightly) reduce current consumption in Eco and sleep mode
heinrichsweikamp
parents: 134
diff changeset
81 btfsc onehourupdate ; one hour in sleep?
c511dc403d7e (Slightly) reduce current consumption in Eco and sleep mode
heinrichsweikamp
parents: 134
diff changeset
82 rcall onehour_sleep ; Yes
c511dc403d7e (Slightly) reduce current consumption in Eco and sleep mode
heinrichsweikamp
parents: 134
diff changeset
83
0
heinrichsweikamp
parents:
diff changeset
84 ;---- adjust airpressure compensation any 15 minutes
heinrichsweikamp
parents:
diff changeset
85 incf divemins+1,F ; counts to 14...
heinrichsweikamp
parents:
diff changeset
86 movlw d'14'
heinrichsweikamp
parents:
diff changeset
87 cpfsgt divemins+1
heinrichsweikamp
parents:
diff changeset
88 bra onemin_sleep2 ; 15 minutes not done!
heinrichsweikamp
parents:
diff changeset
89
heinrichsweikamp
parents:
diff changeset
90 ; Tasks every 15 minutes in sleep
heinrichsweikamp
parents:
diff changeset
91 clrf divemins+1 ; reset counter
heinrichsweikamp
parents:
diff changeset
92
heinrichsweikamp
parents:
diff changeset
93 call deco_calc_CNS_decrease_15min ; compute CNS decay in sleep only
heinrichsweikamp
parents:
diff changeset
94 banksel common
heinrichsweikamp
parents:
diff changeset
95
heinrichsweikamp
parents:
diff changeset
96 SAFE_2BYTE_COPY last_surfpressure_15min, last_surfpressure_30min ; save older airpressure
heinrichsweikamp
parents:
diff changeset
97 SAFE_2BYTE_COPY amb_pressure, last_surfpressure_15min ; save new airpressure
heinrichsweikamp
parents:
diff changeset
98
heinrichsweikamp
parents:
diff changeset
99 movlw LOW max_surfpressure
heinrichsweikamp
parents:
diff changeset
100 movff WREG,sub_a+0 ; max. "allowed" airpressure in mbar
heinrichsweikamp
parents:
diff changeset
101 movlw HIGH max_surfpressure
heinrichsweikamp
parents:
diff changeset
102 movff WREG,sub_a+1 ; max. "allowed" airpressure in mbar
heinrichsweikamp
parents:
diff changeset
103 movff last_surfpressure_15min+0,sub_b+0
heinrichsweikamp
parents:
diff changeset
104 movff last_surfpressure_15min+1,sub_b+1
heinrichsweikamp
parents:
diff changeset
105 call subU16 ; sub_c = sub_a - sub_b
heinrichsweikamp
parents:
diff changeset
106 btfss neg_flag ; Is 1080mbar < amb_pressure ?
heinrichsweikamp
parents:
diff changeset
107 bra onemin_sleep2 ; NO: current airpressure is lower then "allowed" airpressure, ok!
heinrichsweikamp
parents:
diff changeset
108
heinrichsweikamp
parents:
diff changeset
109 ; not ok! Overwrite with max. "allowed" airpressure
heinrichsweikamp
parents:
diff changeset
110 movlw LOW max_surfpressure
heinrichsweikamp
parents:
diff changeset
111 movff WREG,last_surfpressure_15min+0 ; max. "allowed" airpressure in mbar
heinrichsweikamp
parents:
diff changeset
112 movlw HIGH max_surfpressure
heinrichsweikamp
parents:
diff changeset
113 movff WREG,last_surfpressure_15min+1 ; max. "allowed" airpressure in mbar
heinrichsweikamp
parents:
diff changeset
114
heinrichsweikamp
parents:
diff changeset
115 onemin_sleep2:
heinrichsweikamp
parents:
diff changeset
116 ; Tasks every minute in sleep
heinrichsweikamp
parents:
diff changeset
117 SAFE_2BYTE_COPY amb_pressure, int_I_pres_respiration ; LOW copy pressure to deco routine
heinrichsweikamp
parents:
diff changeset
118 call deco_calc_wo_deco_step_1_min ; "calc_tissue_sleep"
heinrichsweikamp
parents:
diff changeset
119 banksel common
heinrichsweikamp
parents:
diff changeset
120
heinrichsweikamp
parents:
diff changeset
121 bcf oneminupdate ; all done
heinrichsweikamp
parents:
diff changeset
122 return
heinrichsweikamp
parents:
diff changeset
123
heinrichsweikamp
parents:
diff changeset
124 onesec_sleep:
198
c511dc403d7e (Slightly) reduce current consumption in Eco and sleep mode
heinrichsweikamp
parents: 134
diff changeset
125 btfsc oneminupdate ; one minute in sleep?
c511dc403d7e (Slightly) reduce current consumption in Eco and sleep mode
heinrichsweikamp
parents: 134
diff changeset
126 rcall onemin_sleep ; do oneminute tasks, e.g. calculate desaturation
c511dc403d7e (Slightly) reduce current consumption in Eco and sleep mode
heinrichsweikamp
parents: 134
diff changeset
127
235
23311219dacc under construction: new hardware_flag to configure different hardware versions
heinrichsweikamp
parents: 224
diff changeset
128 btfsc rechargeable
120
e2f04bb2539c battery check in sleep
heinrichsweikamp
parents: 50
diff changeset
129 call get_battery_voltage ; Check for charger
e2f04bb2539c battery check in sleep
heinrichsweikamp
parents: 50
diff changeset
130
0
heinrichsweikamp
parents:
diff changeset
131 incf divemins+0,F ; counts to #test_pressure_in_sleep (5)
heinrichsweikamp
parents:
diff changeset
132 movlw d'5'
heinrichsweikamp
parents:
diff changeset
133 cpfsgt divemins+0 ; here: temp variable
heinrichsweikamp
parents:
diff changeset
134 bra onesec_sleep1 ; #test_pressure_in_sleep not done yet
heinrichsweikamp
parents:
diff changeset
135
heinrichsweikamp
parents:
diff changeset
136 clrf divemins+0 ; clear counter
heinrichsweikamp
parents:
diff changeset
137 rcall pressuretest_sleep_fast ; Gets pressure without averaging (faster!)
heinrichsweikamp
parents:
diff changeset
138 ; compare current ambient pressure with wake_up_from_sleep
heinrichsweikamp
parents:
diff changeset
139 movlw LOW wake_up_from_sleep
heinrichsweikamp
parents:
diff changeset
140 movwf sub_a+0 ; power on if ambient pressure is greater threshold
heinrichsweikamp
parents:
diff changeset
141 movlw HIGH wake_up_from_sleep
heinrichsweikamp
parents:
diff changeset
142 movwf sub_a+1 ; power on if ambient pressure is greater threshold
heinrichsweikamp
parents:
diff changeset
143 SAFE_2BYTE_COPY amb_pressure, sub_b
heinrichsweikamp
parents:
diff changeset
144 call subU16 ; Is (1160mbar - averaged(amb_pressure)) < 0 ?
heinrichsweikamp
parents:
diff changeset
145 btfsc neg_flag ; Wake up from Sleep?
heinrichsweikamp
parents:
diff changeset
146 bra onesec_sleep1a ; Yes, skip button checks, wake up!
heinrichsweikamp
parents:
diff changeset
147
235
23311219dacc under construction: new hardware_flag to configure different hardware versions
heinrichsweikamp
parents: 224
diff changeset
148 btfsc rechargeable
200
dcd513840c6c cleanup
heinrichsweikamp
parents: 198
diff changeset
149 bra onesec_sleep1 ; No wake-up with cR hardware
0
heinrichsweikamp
parents:
diff changeset
150 btfsc vusb_in ; USB plugged in?
123
d076e9c02438 add ceiling display
heinrichsweikamp
parents: 121
diff changeset
151 bra onesec_sleep1a ; Yes, skip button checks, wake up!
0
heinrichsweikamp
parents:
diff changeset
152
heinrichsweikamp
parents:
diff changeset
153 onesec_sleep1:
heinrichsweikamp
parents:
diff changeset
154 bcf onesecupdate ; all done.
heinrichsweikamp
parents:
diff changeset
155 ; Check switches
heinrichsweikamp
parents:
diff changeset
156 btfsc switch_left
heinrichsweikamp
parents:
diff changeset
157 bra onesec_sleep1a
heinrichsweikamp
parents:
diff changeset
158 btfsc switch_right
heinrichsweikamp
parents:
diff changeset
159 bra onesec_sleep1a
heinrichsweikamp
parents:
diff changeset
160 ; No button pressed
heinrichsweikamp
parents:
diff changeset
161 bcf INTCON,INT0IF ; Clear flag
heinrichsweikamp
parents:
diff changeset
162 bcf INTCON3,INT1IF ; Clear flag
heinrichsweikamp
parents:
diff changeset
163 return
heinrichsweikamp
parents:
diff changeset
164
heinrichsweikamp
parents:
diff changeset
165 onesec_sleep1a: ; At least one button pressed or amb_pressure > wake_up_from_sleep
heinrichsweikamp
parents:
diff changeset
166 bcf INTCON,INT0IF ; Clear flag
heinrichsweikamp
parents:
diff changeset
167 bcf INTCON3,INT1IF ; Clear flag
heinrichsweikamp
parents:
diff changeset
168 bcf sleepmode ; wake up!
heinrichsweikamp
parents:
diff changeset
169 SAFE_2BYTE_COPY last_surfpressure_30min, amb_pressure ; copy for compatibility
heinrichsweikamp
parents:
diff changeset
170 movlw .0
heinrichsweikamp
parents:
diff changeset
171 movff WREG,sensor_state_counter ; Reset sensor state counter
heinrichsweikamp
parents:
diff changeset
172 bcf no_sensor_int ; normal sensor interrupt mode
heinrichsweikamp
parents:
diff changeset
173 return
heinrichsweikamp
parents:
diff changeset
174
heinrichsweikamp
parents:
diff changeset
175 pressuretest_sleep_fast: ; Get pressure without averaging (Faster to save some power in sleep mode)
heinrichsweikamp
parents:
diff changeset
176 banksel isr_backup ; Back to Bank0 ISR data
heinrichsweikamp
parents:
diff changeset
177 clrf amb_pressure_avg+0 ; pressure average registers
heinrichsweikamp
parents:
diff changeset
178 clrf amb_pressure_avg+1
heinrichsweikamp
parents:
diff changeset
179 clrf temperature_avg+0
heinrichsweikamp
parents:
diff changeset
180 clrf temperature_avg+1
heinrichsweikamp
parents:
diff changeset
181 call get_temperature_start ; and start temperature integration (73,5us)
heinrichsweikamp
parents:
diff changeset
182 banksel common
heinrichsweikamp
parents:
diff changeset
183 rcall sleepmode_sleep ; Wait at least 35ms (every 62,5ms Timer7 wakeup)
heinrichsweikamp
parents:
diff changeset
184 rcall sleepmode_sleep ; Wait at least 35ms (every 62,5ms Timer7 wakeup)
heinrichsweikamp
parents:
diff changeset
185 banksel isr_backup ; Back to Bank0 ISR data
heinrichsweikamp
parents:
diff changeset
186 call get_temperature_value ; State 1: Get temperature
heinrichsweikamp
parents:
diff changeset
187 call get_pressure_start ; Start pressure integration.
heinrichsweikamp
parents:
diff changeset
188 banksel common
heinrichsweikamp
parents:
diff changeset
189 rcall sleepmode_sleep ; Wait at least 35ms (every 62,5ms Timer7 wakeup)
heinrichsweikamp
parents:
diff changeset
190 rcall sleepmode_sleep ; Wait at least 35ms (every 62,5ms Timer7 wakeup)
50
ec4d8503ec45 NEW: user-selectable color schemes
heinrichsweikamp
parents: 39
diff changeset
191 bsf LEDg ; Show some activity
0
heinrichsweikamp
parents:
diff changeset
192 banksel isr_backup ; Back to Bank0 ISR data
heinrichsweikamp
parents:
diff changeset
193 call get_pressure_value ; State2: Get pressure (51us)
heinrichsweikamp
parents:
diff changeset
194 call calculate_compensation ; calculate temperature compensated pressure (27us)
heinrichsweikamp
parents:
diff changeset
195 banksel common
heinrichsweikamp
parents:
diff changeset
196 SAFE_2BYTE_COPY amb_pressure_avg, amb_pressure ; copy for compatibility
50
ec4d8503ec45 NEW: user-selectable color schemes
heinrichsweikamp
parents: 39
diff changeset
197 bcf LEDg
0
heinrichsweikamp
parents:
diff changeset
198 return
heinrichsweikamp
parents:
diff changeset
199
heinrichsweikamp
parents:
diff changeset
200 sleepmode_sleep:
heinrichsweikamp
parents:
diff changeset
201 banksel 0xF16 ; Addresses, F16h through F5Fh, are also used by SFRs, but are not part of the Access RAM.
heinrichsweikamp
parents:
diff changeset
202 clrf T7GCON ; Reset Timer7 Gate Control register
heinrichsweikamp
parents:
diff changeset
203 movlw b'10001101' ; 1:1 Prescaler -> 2seconds@32768Hz, not synced
heinrichsweikamp
parents:
diff changeset
204 movwf T7CON
heinrichsweikamp
parents:
diff changeset
205 sleep
198
c511dc403d7e (Slightly) reduce current consumption in Eco and sleep mode
heinrichsweikamp
parents: 134
diff changeset
206 sleep
0
heinrichsweikamp
parents:
diff changeset
207 clrf T7GCON ; Reset Timer7 Gate Control register
heinrichsweikamp
parents:
diff changeset
208 movlw b'10001001' ; 1:1 Prescaler -> 2seconds@32768Hz, synced
heinrichsweikamp
parents:
diff changeset
209 movwf T7CON
heinrichsweikamp
parents:
diff changeset
210 banksel common ; Bank1
heinrichsweikamp
parents:
diff changeset
211 return
heinrichsweikamp
parents:
diff changeset
212
heinrichsweikamp
parents:
diff changeset
213 END