Mercurial > public > hwos_code
annotate src/isr.asm @ 656:8af5aefbcdaf default tip
Update to 3.31 beta
| author | heinrichsweikamp |
|---|---|
| date | Thu, 27 Nov 2025 18:32:58 +0100 |
| parents | 75e90cd0c2c3 |
| children |
| rev | line source |
|---|---|
| 0 | 1 ;============================================================================= |
| 2 ; | |
| 640 | 3 ; File isr.asm * combined next generation V3.11.1 |
| 0 | 4 ; |
| 5 ; INTERUPT subroutines | |
| 6 ; | |
| 654 | 7 ; Copyright (c) 2011, JD Gascuel, heinrichs weikamp gmbh, all right reserved. |
| 0 | 8 ;============================================================================= |
| 9 ; HISTORY | |
| 10 ; 2011-05-24 : [jDG] Cleanups from initial Matthias code. | |
| 11 | |
| 275 | 12 #include "hwos.inc" |
| 582 | 13 #include "shared_definitions.h" ; Mailbox from/to p2_deco.c |
| 0 | 14 #include "ms5541.inc" |
| 15 #include "adc_lightsensor.inc" | |
| 623 | 16 |
| 17 extern restore_flash | |
| 0 | 18 |
| 634 | 19 |
| 0 | 20 ;============================================================================= |
| 634 | 21 ; Code to be placed at a fixed Position |
| 623 | 22 ; |
| 656 | 23 isr_high CODE 0x00008 ; high priority interrupts |
| 634 | 24 bra HighInt ; jump to ISR |
| 25 | |
| 26 isr_low CODE 0x00018 ; low priority interrupts *** not used *** | |
| 27 retfie FAST ; do an immediate return from IRQ | |
| 28 ; | |
| 29 ;============================================================================= | |
| 623 | 30 |
| 0 | 31 |
| 634 | 32 ;----------------------------------------------------------------------------- |
| 33 ; Interrupt Dispatcher Entry Point | |
| 623 | 34 ; |
| 0 | 35 HighInt: |
| 623 | 36 ; initialize interrupt code |
| 37 banksel isr_backup ; default bank for all ISR code is bank ISR data | |
| 629 | 38 movff PRODL,PROD_backup+0 ; back-up PRODL |
| 39 movff PRODH,PROD_backup+1 ; back-up PRODH | |
| 623 | 40 ; serve buttons |
| 41 btfsc PIR1,TMR1IF ; timer 1 interrupt (button hold-down timer)? | |
| 42 rcall timer1int ; YES - reset timer | |
| 640 | 43 btfsc PIR5,TMR4IF ; timer 4 interrupt (button debounce) |
| 44 rcall timer4int ; YES, serve the interrupt | |
| 623 | 45 btfsc INTCON,INT0IF ; right button activity? |
| 46 rcall isr_switch_right ; YES - check right switch | |
| 47 btfsc INTCON3,INT1IF ; left button activity? | |
| 48 rcall isr_switch_left ; YES - check left switch | |
| 0 | 49 |
| 656 | 50 IFDEF _external_sensor_eccr |
| 623 | 51 ; serve IR/S8 link timer |
| 52 btfsc PIR3,RC2IF ; UART 2 interrupt? | |
| 53 rcall isr_uart2 ; YES - get a byte from the IR/S8 link | |
| 54 btfsc PIR2,TMR3IF ; timer 3 interrupt? | |
| 55 rcall isr_timer3 ; YES - check bytes received from IR/S8 link for being a valid telegram | |
| 629 | 56 ELSE |
| 57 bcf PIR3,RC2IF ; clear UART 2 interrupt | |
| 58 bcf PIR2,TMR3IF ; clear timer 3 interrupt | |
| 656 | 59 ; fill-up to keep code size identical to _external_sensor_eccr variant, see "Attention" below |
| 629 | 60 nop |
| 61 nop | |
| 623 | 62 ENDIF |
| 63 | |
| 64 ; serve pressure and temperature sensor | |
| 65 btfsc PIR5,TMR7IF ; timer 7 interrupt? | |
| 66 rcall isr_tmr7 ; YES - do every 62.5 ms tasks: read sensors, set CPU speed | |
| 67 | |
| 68 ; serve real-time clock (RTCC) | |
| 69 btfsc PIR3,RTCCIF ; real-time-clock interrupt? | |
| 70 rcall isr_rtcc ; YES - do every 1/2 s tasks: read RTC, trigger timed tasks, adjust CPU speed, gauge battery, etc. | |
| 113 | 71 |
| 623 | 72 ; clean up and exit |
| 629 | 73 movff PROD_backup+0,PRODL ; restore PRODL |
| 74 movff PROD_backup+1,PRODH ; restore PRODH | |
| 623 | 75 bsf trigger_isr_updates ; signal that the ISR had kicked in |
| 76 retfie FAST ; return from interrupt restoring BSR, STATUS and WREG | |
| 77 | |
| 113 | 78 |
| 634 | 79 ;----------------------------------------------------------------------------- |
| 80 ; CPU Speed Adjustment | |
| 623 | 81 ; |
| 82 isr_adjust_speed: | |
| 83 movff cpu_speed_request,cpu_speed_state ; acknowledge CPU speed request | |
| 0 | 84 |
| 623 | 85 btfsc speed_is_eco ; speed 'eco' requested? |
| 86 bra isr_set_speed_to_eco ; YES - set eco speed | |
| 87 btfsc speed_is_fastest ; NO - speed 'fastest' requested? | |
| 88 bra isr_set_speed_to_fastest ; YES - set fastest speed | |
| 89 ;bra isr_set_speed_to_normal ; NO - default to normal speed | |
| 0 | 90 |
|
410
d3087a8ed7e1
BUGFIX: Fix rare issue after battery change (OSTC3 did not start properly)
heinrichsweikamp
parents:
378
diff
changeset
|
91 isr_set_speed_to_normal: |
| 629 | 92 clrf OSCTUNE ; switch off x4 PLL |
| 623 | 93 movlw b'01110010' ; select 16 MHz |
| 94 movwf OSCCON ; set prescaler | |
| 95 movlw T2CON_NORMAL ; PWM1 dimming factor for speed 'normal' | |
| 96 bra isr_adjust_speed_exit | |
| 97 | |
| 98 isr_set_speed_to_eco: | |
| 629 | 99 clrf OSCTUNE ; switch off x4 PLL |
| 623 | 100 movlw b'00110010' ; select 1 MHz |
| 101 movwf OSCCON ; set prescaler | |
| 102 movlw T2CON_ECO ; PWM1 dimming factor for speed 'eco' | |
| 103 bra isr_adjust_speed_exit | |
|
410
d3087a8ed7e1
BUGFIX: Fix rare issue after battery change (OSTC3 did not start properly)
heinrichsweikamp
parents:
378
diff
changeset
|
104 |
| 623 | 105 isr_set_speed_to_fastest: |
| 106 movlw b'01110010' ; select 16 MHz by default | |
| 107 btfsc lv_core ; on OSTC with low voltage core? | |
| 108 movlw b'01100010' ; YES - reduce to 8 MHz | |
| 109 movwf OSCCON ; set prescaler | |
| 629 | 110 bsf OSCTUNE,6 ; switch on x4 PLL -> 64 MHz on high voltage core, 32 MHz on low voltage core |
| 623 | 111 movlw T2CON_FASTEST ; PWM1 dimming factor for speed 'fastest' |
| 640 | 112 bra isr_adjust_speed_exit |
| 623 | 113 |
| 114 ; Attention: fill-up the gap between the end of this section | |
| 115 ; and the next section which starts at 0x00080 !! | |
| 656 | 116 ; nop |
| 640 | 117 |
| 623 | 118 block_0_code_end: ; marker to find end of code in block 0 in linker report file |
|
410
d3087a8ed7e1
BUGFIX: Fix rare issue after battery change (OSTC3 did not start properly)
heinrichsweikamp
parents:
378
diff
changeset
|
119 |
| 634 | 120 |
| 623 | 121 ;============================================================================= |
| 634 | 122 ; Code to be placed at a fixed Position: Jump-Vector for the Bootloader |
| 623 | 123 ; |
| 634 | 124 restore CODE 0x00080 ; restore first flash page from EEPROM |
| 125 | |
|
410
d3087a8ed7e1
BUGFIX: Fix rare issue after battery change (OSTC3 did not start properly)
heinrichsweikamp
parents:
378
diff
changeset
|
126 restore_flash_0x00080: |
| 582 | 127 goto restore_flash |
| 634 | 128 ; |
| 129 ;============================================================================= | |
| 130 | |
|
410
d3087a8ed7e1
BUGFIX: Fix rare issue after battery change (OSTC3 did not start properly)
heinrichsweikamp
parents:
378
diff
changeset
|
131 |
| 623 | 132 ;============================================================================= |
| 634 | 133 isr_handler CODE 0x00084 |
| 134 ;============================================================================= | |
| 135 | |
| 640 | 136 isr_adjust_speed_exit: |
| 137 movwf T2CON ; adjust PWM1 for LED dimming | |
| 138 btfss OSCCON,HFIOFS ; PLL stabilized? | |
| 139 bra isr_adjust_speed_exit ; NO - loop to give it some more time | |
| 140 return ; YES - done | |
| 141 | |
| 634 | 142 ;----------------------------------------------------------------------------- |
| 143 ; Handle digitally-attached external Sensors | |
| 144 ; | |
| 145 ; take a byte received on IR/S8 link and slot it into the RX buffer | |
| 623 | 146 ; |
| 656 | 147 IFDEF _external_sensor_eccr |
| 623 | 148 |
| 149 isr_uart2: | |
| 634 | 150 bcf PIR3,RC2IF ; clear UART 2 interrupt flag |
| 623 | 151 banksel RCREG2 ; RC*2 is outside access RAM |
| 152 movff RCREG2,isr_lo ; copy received byte to isr_lo | |
| 153 bcf RCSTA2,CREN ; clear receiver status | |
| 154 bsf RCSTA2,CREN ; ... | |
| 656 | 155 ; movff RCREG2,TXREG2 ;mH |
| 623 | 156 banksel isr_backup ; back to default ISR bank |
| 656 | 157 movlw .64 ; size of the buffer |
| 623 | 158 cpfslt ir_s8_counter ; number of received bytes < buffer size? |
| 159 bra isr_uart2_1 ; NO - buffer full, do not store the byte | |
| 160 movf ir_s8_counter,W ; YES - copy number of received bytes to WREG | |
| 161 MOVII FSR0L,FSR0_backup ; - back-up FSR0 | |
| 162 lfsr FSR0,ir_s8_buffer ; - load base address of buffer | |
| 163 movff isr_lo,PLUSW0 ; - store received byte | |
| 164 MOVII FSR0_backup,FSR0L ; - restore FSR0 | |
| 165 incf ir_s8_counter,F ; - increment number of received bytes by 1 | |
| 166 isr_uart2_1: | |
| 634 | 167 movlw .253 ; reload timer 3, high byte |
| 623 | 168 movwf TMR3H ; ... |
| 634 | 169 clrf TMR3L ; reload timer 3, low byte |
| 170 bsf T3CON,TMR3ON ; restart timer | |
| 171 return ; done | |
| 623 | 172 |
| 173 | |
| 634 | 174 ;----------------------------------------------------------------------------- |
| 175 ; Timeout on IR/S8 Link: check the Checksum and gather the received Data | |
| 623 | 176 ; |
| 177 isr_timer3: | |
| 604 | 178 bcf T3CON,TMR3ON ; stop timer 3 |
| 634 | 179 movlw .15 ; a IR telegram has 15 bytes |
| 604 | 180 cpfseq ir_s8_counter ; got exactly 15 bytes? |
| 623 | 181 bra isr_timer3_1 ; NO - test for 16 bytes |
| 604 | 182 bra isr_timer3_ir ; YES - got 15 bytes, compute local checksum |
| 0 | 183 isr_timer3_1: |
| 634 | 184 movlw .16 ; a IR telegram may also have 16 bytes, with last byte 0x00 |
| 604 | 185 cpfseq ir_s8_counter ; got exactly 16 bytes? |
| 623 | 186 bra isr_timer3_2 ; NO - test for 17 bytes |
| 656 | 187 banksel common2 ; ir_s8_buffer is in common2 |
| 604 | 188 tstfsz ir_s8_buffer+.15 ; YES - last byte = 0x00 ? |
| 634 | 189 bra isr_timer3_exit ; NO - exit |
| 604 | 190 bra isr_timer3_ir ; YES - got 16 bytes, compute local checksum |
| 113 | 191 isr_timer3_2: |
| 634 | 192 movlw .17 ; a S8 telegram has 17 bytes |
| 604 | 193 cpfseq ir_s8_counter ; got exactly 17 bytes? |
| 656 | 194 bra isr_timer3_3 ; NO - test for 57 bytes |
| 604 | 195 bra isr_timer3_s8 ; YES - S8 data |
| 656 | 196 isr_timer3_3: |
| 197 movlw .57 ; an eccr telegram has 57 bytes | |
| 198 cpfseq ir_s8_counter ; got exactly 17 bytes? | |
| 199 bra isr_timer3_exit ; NO - exit | |
| 200 bra isr_timer3_eccr ; YES - eccr data | |
| 0 | 201 |
| 656 | 202 isr_timer3_eccr: |
| 203 ; ; process telegram received on S8 link | |
| 204 banksel isr_backup ; default bank for all ISR code is bank ISR data | |
| 205 movlw .55 ; compute checksum over 1st and next 55 bytes | |
| 206 rcall compute_IR_S8_checksum ; compute checksum | |
| 207 | |
| 208 | |
| 209 ; tstfsz ir_s8_counter ; checksum ok? | |
| 210 ; bra isr_timer3_exit ; NO - discard data | |
| 211 | |
| 212 ; copy received data to respective variables | |
| 213 ; movff ir_s8_buffer+.1,hud_status_byte | |
| 214 movff ir_s8_buffer+.9,s8_rawdata_sensor1+0 | |
| 215 movff ir_s8_buffer+.10,s8_rawdata_sensor1+1 | |
| 216 movff ir_s8_buffer+.11,s8_rawdata_sensor1+2 | |
| 217 movff ir_s8_buffer+.12,sensor1_ppO2 | |
| 218 movff ir_s8_buffer+.13,s8_rawdata_sensor2+0 | |
| 219 movff ir_s8_buffer+.14,s8_rawdata_sensor2+1 | |
| 220 movff ir_s8_buffer+.15,s8_rawdata_sensor2+2 | |
| 221 movff ir_s8_buffer+.16,sensor2_ppO2 | |
| 222 movff ir_s8_buffer+.17,s8_rawdata_sensor3+0 | |
| 223 movff ir_s8_buffer+.18,s8_rawdata_sensor3+1 | |
| 224 movff ir_s8_buffer+.19,s8_rawdata_sensor3+2 | |
| 225 movff ir_s8_buffer+.20,sensor3_ppO2 | |
| 226 movff ir_s8_buffer+.27,hud_battery_mv+0 | |
| 227 movff ir_s8_buffer+.28,hud_battery_mv+1 ; External battery voltage in mV | |
| 228 movff ir_s8_buffer+.29,ccr_status_byte+0 | |
| 229 movff ir_s8_buffer+.30,ccr_status_byte+1 | |
| 230 | |
| 231 clrf hud_status_byte | |
| 232 | |
| 233 ; manually build HUD status byte for ccr testing | |
| 234 btfsc ccr_status_byte+0,0 | |
| 235 bsf sensor1_active | |
| 236 btfsc ccr_status_byte+0,0 | |
| 237 bsf sensor1_calibrated_ok | |
| 238 | |
| 239 | |
| 240 | |
| 241 btfsc ccr_status_byte+0,1 | |
| 242 bsf sensor2_active | |
| 243 btfsc ccr_status_byte+0,1 | |
| 244 bsf sensor2_calibrated_ok | |
| 245 | |
| 246 btfsc ccr_status_byte+0,2 | |
| 247 bsf sensor3_active | |
| 248 btfsc ccr_status_byte+0,2 | |
| 249 bsf sensor3_calibrated_ok | |
| 250 | |
| 251 bsf trigger_S8_data_update ; YES - set flag for new data available | |
| 252 ;bsf ext_input_optical | |
| 253 bsf ext_s8_full_digital ; Set flag for external S8 full digital mode | |
| 254 bsf hud_connection_ok ; set manually for eccr | |
| 255 bra isr_timer3_reload ; reload timer and exit | |
| 256 | |
| 634 | 257 ; process telegram received on IR link |
| 623 | 258 isr_timer3_ir: |
| 656 | 259 banksel isr_backup ; default bank for all ISR code is bank ISR data |
| 634 | 260 movlw .12 ; compute checksum over 1st and next 12 bytes |
| 623 | 261 rcall compute_IR_S8_checksum ; compute checksum |
| 634 | 262 tstfsz ir_s8_counter ; checksum ok? |
| 263 bra isr_timer3_exit ; NO - discard data | |
| 0 | 264 |
| 634 | 265 ; copy received data to respective variables |
| 656 | 266 movff ir_s8_buffer+.1,hud_status_byte |
| 267 movff ir_s8_buffer+.2,sensor1_mv+0 | |
| 268 movff ir_s8_buffer+.3,sensor1_mv+1 | |
| 269 movff ir_s8_buffer+.4,sensor2_mv+0 | |
| 270 movff ir_s8_buffer+.5,sensor2_mv+1 | |
| 271 movff ir_s8_buffer+.6,sensor3_mv+0 | |
| 272 movff ir_s8_buffer+.7,sensor3_mv+1 | |
| 273 movff ir_s8_buffer+.8,sensor1_ppO2 | |
| 274 movff ir_s8_buffer+.9,sensor2_ppO2 | |
| 623 | 275 movff ir_s8_buffer+.10,sensor3_ppO2 |
| 582 | 276 movff ir_s8_buffer+.11,hud_battery_mv+0 |
| 277 movff ir_s8_buffer+.12,hud_battery_mv+1 | |
| 0 | 278 |
| 623 | 279 bsf hud_connection_ok ; set manually for hwHUD w/o the HUD module |
| 280 bra isr_timer3_reload ; reload timer and exit | |
| 281 | |
| 634 | 282 ; process telegram received on S8 link |
| 623 | 283 isr_timer3_s8: |
| 634 | 284 movlw .14 ; compute checksum over 1st and next 14 bytes |
| 623 | 285 rcall compute_IR_S8_checksum ; compute checksum |
| 634 | 286 tstfsz ir_s8_counter ; checksum ok? |
| 287 bra isr_timer3_exit ; NO - discard data | |
| 623 | 288 |
| 634 | 289 ; copy received data to respective variables |
| 290 movff ir_s8_buffer+.3, hud_status_byte ; also sets hud_connection_ok flag | |
| 623 | 291 movff ir_s8_buffer+.13,hud_battery_mv+0 |
| 292 movff ir_s8_buffer+.14,hud_battery_mv+1 | |
|
227
03946aa48fa5
NEW: Support for hwHUD without the LED module
heinrichsweikamp
parents:
204
diff
changeset
|
293 |
| 634 | 294 ; btfsc trigger_S8_data_update ; last data already processed? |
| 295 ; bra isr_timer3_exit ; NO - skip copying new data (and not reload the timeout) | |
| 623 | 296 bsf trigger_S8_data_update ; YES - set flag for new data available |
| 582 | 297 |
| 634 | 298 ; copy more received data to respective variables |
| 656 | 299 movff ir_s8_buffer+.4,s8_rawdata_sensor1+0 |
| 300 movff ir_s8_buffer+.5,s8_rawdata_sensor1+1 | |
| 301 movff ir_s8_buffer+.6,s8_rawdata_sensor1+2 | |
| 302 movff ir_s8_buffer+.7,s8_rawdata_sensor2+0 | |
| 303 movff ir_s8_buffer+.8,s8_rawdata_sensor2+1 | |
| 304 movff ir_s8_buffer+.9,s8_rawdata_sensor2+2 | |
| 623 | 305 movff ir_s8_buffer+.10,s8_rawdata_sensor3+0 |
| 306 movff ir_s8_buffer+.11,s8_rawdata_sensor3+1 | |
| 307 movff ir_s8_buffer+.12,s8_rawdata_sensor3+2 | |
| 308 | |
| 309 isr_timer3_reload: | |
| 634 | 310 movlw ir_timeout_value ; get timeout value (in multiples of 62.5 ms) |
| 311 movwf ir_s8_timeout ; reload timeout counter | |
| 0 | 312 isr_timer3_exit: |
| 656 | 313 banksel isr_backup ; default bank for all ISR code is bank ISR data |
| 634 | 314 clrf ir_s8_counter ; clear number of received bytes |
| 315 bcf PIR2,TMR3IF ; clear IRQ flag | |
| 316 return ; done | |
| 0 | 317 |
| 318 | |
| 634 | 319 ;----------------------------------------------------------------------------- |
| 320 ; Helper Function - Compute Checksum on Data in RX Buffer | |
| 623 | 321 ; |
| 322 compute_IR_S8_checksum: | |
| 323 movwf ir_s8_counter ; initialize loop counter from WREG | |
| 634 | 324 MOVII FSR0L,FSR0_backup ; back-up FSR0 |
| 325 lfsr FSR0,ir_s8_buffer ; load base address of the receive buffer | |
| 326 movff POSTINC0,isr_mpr+0 ; initialize low byte of the calculated checksum with first byte in buffer | |
| 656 | 327 clrf isr_mpr+1 ; clear the high byte of the calculated checksum |
| 623 | 328 compute_IR_S8_checksum_loop: |
| 656 | 329 movf POSTINC0,W ; read next byte |
| 330 addwf isr_mpr+0,F ; add it to the to checksum, low byte | |
| 331 movlw .0 ; no explicit data to add to the high byte... | |
| 332 addwfc isr_mpr+1,F ; ... besides the carry | |
| 634 | 333 decfsz ir_s8_counter,F ; decrement number of bytes yet to do, all done? |
| 656 | 334 bra compute_IR_S8_checksum_loop ; NO - loop |
| 335 movf POSTINC0,W ; YES - read low byte of the received checksum | |
| 336 cpfseq isr_mpr+0 ; - equal to low byte of the calculated checksum? | |
| 634 | 337 incf ir_s8_counter,F ; NO - mark a checksum error |
| 656 | 338 movf POSTINC0,W ; - read high byte of the received checksum |
| 339 cpfseq isr_mpr+1 ; - equal to high byte of the calculated checksum? | |
| 634 | 340 incf ir_s8_counter,F ; NO - mark a checksum error |
| 341 MOVII FSR0_backup,FSR0L ; - restore FSR0 | |
| 656 | 342 return ; - done |
| 113 | 343 |
| 656 | 344 ENDIF ; _external_sensor_eccr |
| 113 | 345 |
| 640 | 346 |
| 347 ;----------------------------------------------------------------------------- | |
| 348 ; Timer 4 - Button debounce (For new digital piezo circuit) | |
| 349 | |
| 350 timer4int: | |
| 351 bcf PIR5,TMR4IF | |
| 352 decfsz debounce_counter,F | |
| 353 return | |
| 354 bcf T4CON,TMR4ON ; Stop timer 4 | |
| 355 clrf TMR4 ; reset | |
| 356 | |
| 357 movff opt_cR_button_right,WREG ; 20-100; mH: opt_cR_button_right will also affect left button | |
| 358 bcf STATUS,C ; clear carry bit | |
| 359 rrcf WREG ; /2 -> 10-50 | |
| 360 bcf STATUS,C ; clear carry bit | |
| 361 rrcf WREG ; /2 -> 5-25 | |
| 362 decf WREG,W ; -1 | |
| 363 decf WREG,W ; -1 | |
| 364 decf WREG,W ; -1 -> 2-22 | |
| 365 | |
| 366 ; movlw .5 ; initial delay | |
| 367 movwf debounce_counter ; multiples of 16ms | |
| 368 return | |
| 369 | |
| 370 timer4_restart: | |
| 371 movlw .5 ; extra delay | |
| 372 movwf debounce_counter | |
| 373 clrf TMR4 ; reset | |
| 374 bcf INTCON3,INT1IF ; clear ext. int 1 request | |
| 375 bcf INTCON,INT0IF ; clear ext. int 0 request | |
| 376 bsf INTCON,INT0IE ; enable INT0 IRQ | |
| 377 bsf INTCON3,INT1IE ; enable INT1 IRQ | |
| 378 return | |
| 0 | 379 |
| 634 | 380 ;----------------------------------------------------------------------------- |
| 381 ; Tasks every 62.5 ms: Buttons, Dimming, Pressure/Temperature Sensor and CPU Speed | |
| 623 | 382 ; |
| 383 isr_tmr7: | |
| 634 | 384 bcf PIE5,TMR7IE ; disable IRQs by TMR7 |
| 0 | 385 |
| 634 | 386 banksel 0xF16 ; TMR7H/L are not part of the access RAM |
| 387 movlw .248 ; reload timer 7, high byte (8x256 ticks -> 62.5 ms) | |
| 388 movwf TMR7H ; ... (keep low byte running ) | |
| 389 | |
| 643 | 390 banksel isr_backup ; back to ISR default bank |
| 391 decfsz isr_tmr7_helper,F ; decreased every 62,5ms | |
| 392 bra isr_tmr7_0 | |
| 393 movlw .4 ; 62,5ms * 4 = 1/4 second | |
| 394 movwf isr_tmr7_helper | |
| 395 bsf trigger_quarter_second ; set flag (In access RAM) | |
| 396 | |
| 397 | |
| 398 isr_tmr7_0: | |
| 640 | 399 btfss update_surface_pressure ; shall update the surface pressure? |
| 400 bra isr_tmr7_1 ; NO - skip | |
| 401 bcf update_surface_pressure ; YES - clear request flag | |
| 402 MOVII pressure_abs_ref,pressure_surf ; - update surface pressure | |
| 636 | 403 |
| 640 | 404 isr_tmr7_1: |
| 634 | 405 call get_analog_switches ; get analog readings - bank-safe, but CAUTION: returns in bank common |
| 406 banksel isr_backup ; back to ISR default bank | |
| 623 | 407 |
| 634 | 408 btfss INTCON3,INT1IE ; external IRQ 1 enabled? |
| 640 | 409 bra isr_tmr7_2 ; NO - skip next |
| 634 | 410 btfsc analog_sw2_pressed ; YES - analog switch 2 pressed? |
| 411 rcall isr_switch_left ; NO - get digital readings of left switch | |
| 640 | 412 |
| 413 isr_tmr7_2: | |
| 634 | 414 btfss INTCON,INT0IE ; external IRQ 0 enabled? |
| 640 | 415 bra isr_tmr7_3 ; NO - skip next |
| 634 | 416 btfsc analog_sw1_pressed ; YES - analog switch 1 pressed? |
| 417 rcall isr_switch_right ; NO - get digital readings of right switch | |
| 640 | 418 |
| 419 isr_tmr7_3: | |
| 420 btfsc block_sensor_interrupt ; sensor interrupts disabled? | |
| 634 | 421 bra sensor_int_state_exit ; YES - goto exit |
| 640 | 422 ;bra isr_tmr7_4 ; NO - continue |
| 423 | |
| 424 isr_tmr7_4: | |
| 623 | 425 movf max_CCPR1L,W ; dim value |
| 582 | 426 cpfseq CCPR1L ; = current PWM value? |
| 623 | 427 rcall isr_dimm_tft ; NO - adjust until max_CCPR1L = CCPR1L |
| 0 | 428 |
| 656 | 429 IFDEF _external_sensor_eccr |
| 640 | 430 |
| 634 | 431 decfsz ir_s8_timeout,F ; decrement IR/S8 timeout counter, became zero? |
| 640 | 432 bra isr_sensor_state2 ; NO - continue with sensor |
| 634 | 433 movlw ir_timeout_value ; YES - get timeout value (in multiples of 62.5 ms) |
| 434 movwf ir_s8_timeout ; - reload timeout timer | |
| 435 btfsc ext_input_optical ; - optical input in use? | |
| 640 | 436 bra isr_tmr7_5 ; YES - clear data |
| 656 | 437 btfsc ext_s8_full_digital ; are we in external S8 full digital mode? |
| 438 bra isr_tmr7_5 ; YES - clear data | |
| 634 | 439 TSTOSS opt_s8_mode ; NO - S8 input in use? |
| 640 | 440 bra isr_sensor_state2 ; NO - must be analog interface in use, keep data |
| 441 ;bra isr_tmr7_5 ; YES - clear data | |
| 634 | 442 |
| 640 | 443 isr_tmr7_5: |
| 623 | 444 clrf hud_status_byte ; S8/IR timeout clears all analog input readings to zero -> fallback will be triggered when in sensor mode |
| 445 CLRI hud_battery_mv ; clear battery voltage | |
| 582 | 446 |
| 623 | 447 banksel sensor1_mv ; select bank where sensor data are stored |
| 448 CLRI sensor1_mv ; clear all sensor data | |
| 449 CLRI sensor2_mv ; ... | |
| 450 CLRI sensor3_mv ; ... | |
| 451 clrf sensor1_ppO2 ; ... | |
| 452 clrf sensor2_ppO2 ; ... | |
| 453 clrf sensor3_ppO2 ; ... | |
| 628 | 454 banksel s8_rawdata_sensor1 |
| 455 CLRI s8_rawdata_sensor1 ; clear all sensor data (raw data) | |
| 456 clrf s8_rawdata_sensor1+2 | |
| 457 CLRI s8_rawdata_sensor2 ; clear all sensor data (raw data) | |
| 458 clrf s8_rawdata_sensor2+2 | |
| 459 CLRI s8_rawdata_sensor3 ; clear all sensor data (raw data) | |
| 460 clrf s8_rawdata_sensor3+2 | |
| 623 | 461 banksel isr_backup ; back to ISR default bank |
| 462 | |
| 463 bsf trigger_S8_data_update ; signal a data update | |
| 582 | 464 |
| 656 | 465 ENDIF ; _external_sensor_eccr |
| 0 | 466 |
| 640 | 467 isr_sensor_state2: |
| 623 | 468 btfss speed_is_normal ; CPU running on normal speed? |
| 469 rcall isr_set_speed_to_normal ; NO - set CPU speed to normal | |
| 582 | 470 |
| 623 | 471 incf sensor_state_counter,F ; counts to eight for state machine |
| 0 | 472 |
| 623 | 473 ; State 1: clear flags and average registers, get temperature (51 us) and start pressure integration (73.5 us) |
| 474 ; State 2: get pressure (51 us), start temperature integration (73.5 us) and calculate temperature compensated pressure (233 us) | |
| 475 ; State 3: get temperature (51 us) and start pressure integration (73.5 us) | |
| 476 ; State 4: get pressure (51 us), start temperature integration (73.5 us) and calculate temperature compensated pressure (233 us) | |
| 477 ; State 5: get temperature (51 us) and start pressure integration (73.5 us) | |
| 478 ; State 6: get pressure (51 us), start temperature integration (73.5 us) and calculate temperature compensated pressure (233 us) | |
| 479 ; State 7: get temperature (51 us) and start pressure integration (73.5 us) | |
| 480 ; State 8: get pressure (51 us), start temperature integration (73.5 us) and calculate temperature compensated pressure (233 us) and build average for half-second update of temperature and pressure | |
| 582 | 481 |
| 623 | 482 movff sensor_state_counter,WREG ; WREG used as temp here... |
| 582 | 483 dcfsnz WREG,F |
| 623 | 484 bra sensor_int_state1_plus_restart ; do state 1 |
| 582 | 485 dcfsnz WREG,F |
| 623 | 486 bra sensor_int_state2 ; do state 2 |
| 582 | 487 dcfsnz WREG,F |
| 623 | 488 bra sensor_int_state1 ; do state 3 |
| 582 | 489 dcfsnz WREG,F |
| 623 | 490 bra sensor_int_state2 ; do state 4 |
| 582 | 491 dcfsnz WREG,F |
| 623 | 492 bra sensor_int_state1 ; do state 5 |
| 582 | 493 dcfsnz WREG,F |
| 623 | 494 bra sensor_int_state2 ; do state 6 |
| 582 | 495 dcfsnz WREG,F |
| 623 | 496 bra sensor_int_state1 ; do state 7 |
| 497 | |
| 498 ; first, do state 2: | |
| 499 call get_pressure_value ; state 2: get pressure (51 us) | |
| 500 call get_temperature_start ; and start temperature integration (73.5 us) | |
| 501 call calculate_compensation ; calculate temperature compensated pressure (27 us) | |
| 0 | 502 |
| 634 | 503 ; build average for pressure |
| 656 | 504 bcf STATUS,C ; clear carry bit |
| 643 | 505 rrcf pressure_abs_avg+2 ; divide by 2 |
| 506 rrcf pressure_abs_avg+1 ; ... | |
| 634 | 507 rrcf pressure_abs_avg+0 ; ... |
| 656 | 508 bcf STATUS,C ; clear carry bit |
| 643 | 509 rrcf pressure_abs_avg+2 ; divide by 2, again |
| 510 rrcf pressure_abs_avg+1 ; ... | |
| 634 | 511 rrcf pressure_abs_avg+0 ; ... |
| 623 | 512 |
| 634 | 513 ; export averaged pressure |
| 656 | 514 tstfsz pressure_abs_avg+2 ; must be zero for all depths < 262m |
| 515 bra isr_sensor_state_sensor_error ; assume a false reading if <> 0 | |
| 516 | |
| 643 | 517 MOVII pressure_abs_avg,pressure_abs ; export result (Which is 16 bit for all depths < 262m) |
| 0 | 518 |
| 656 | 519 isr_sensor_state_sensor_resume: |
| 634 | 520 ; build average for temperature |
| 623 | 521 bcf STATUS,C ; clear carry bit by default |
| 522 btfsc temperature_avg+1,7 ; sign bit set? | |
| 523 bsf STATUS,C ; YES - copy sign bit to carry bit | |
| 524 rrcf temperature_avg+1 ; divide signed temperature by 2 | |
| 525 rrcf temperature_avg+0 ; ... | |
| 526 bcf STATUS,C ; clear carry bit by default | |
| 527 btfsc temperature_avg+1,7 ; sign bit set? | |
| 528 bsf STATUS,C ; YES - copy sign bit to carry bit | |
| 529 rrcf temperature_avg+1 ; divide signed temperature by 2 again (by 4 in total now) | |
| 530 rrcf temperature_avg+0 ; ... | |
| 656 | 531 |
| 532 btfsc i2c_reinit_sensor2 ; Sensor ok? | |
| 533 bra isr_sensor_state_skip_temp ; likely not, skip updating the temperature | |
| 534 | |
| 623 | 535 MOVII temperature_avg,temperature_cur ; store final result |
| 0 | 536 |
| 656 | 537 isr_sensor_state_skip_temp: |
| 634 | 538 ; check for temperature change |
| 623 | 539 movf temperature_cur+0,W ; get current temperature, low byte |
| 540 cpfseq temperature_last+0 ; compare with last temperature, equal? | |
| 541 bra isr_sensor_state2_2 ; NO - temperature has changed | |
| 542 movf temperature_cur+1,W ; get current temperature, high byte | |
| 543 cpfseq temperature_last+1 ; compare with last temperature, equal? | |
| 544 bra isr_sensor_state2_2 ; NO - temperature has changed | |
| 545 bra isr_sensor_state2_3 ; YES to both - no change | |
| 0 | 546 |
| 547 isr_sensor_state2_2: | |
| 623 | 548 MOVII temperature_cur,temperature_last ; store current temperature as last temperature for next round |
| 549 bsf trigger_temp_changed ; set flag for temperature change | |
| 550 | |
| 0 | 551 isr_sensor_state2_3: |
| 629 | 552 ; reset state counter |
| 623 | 553 clrf sensor_state_counter ; reset state counter |
| 554 btfss reset_max_pressure ; shall clear the max pressure? | |
| 555 bra isr_sensor_state2_3a ; NO - continue with checking for pressure change | |
| 556 bcf reset_max_pressure ; YES - clear request flag | |
| 557 CLRI pressure_rel_max ; - clear max. pressure | |
| 0 | 558 |
| 623 | 559 isr_sensor_state2_3a: |
| 560 ; check for pressure change | |
| 561 movf pressure_abs+0,W ; get current pressure, low byte | |
| 562 cpfseq pressure_abs_last+0 ; compare with last pressure, equal? | |
| 563 bra isr_sensor_state2_4 ; NO - pressure has changed | |
| 564 movf pressure_abs+1,W ; YES - get current pressure, high byte | |
| 565 cpfseq pressure_abs_last+1 ; - compare with last pressure, equal? | |
| 566 bra isr_sensor_state2_4 ; NO - pressure has changed | |
| 567 bra isr_sensor_state2_5 ; YES - no change | |
| 568 | |
| 569 isr_sensor_state2_4: | |
| 570 MOVII pressure_abs,pressure_abs_last ; store current pressure as last pressure for next round | |
| 571 bsf trigger_pres_cur_changed ; signal a pressure change | |
| 572 | |
| 573 isr_sensor_state2_5: | |
| 574 ; compute relative pressure | |
| 575 movf pressure_surf+0,W ; get surface pressure, low byte | |
| 576 subwf pressure_abs+0,W ; WREG = pressure_abs - pressure_surf (low byte) | |
| 577 movwf pressure_rel_cur+0 ; store relative pressure, low byte | |
| 578 movf pressure_surf+1,W ; get surface pressure, high byte | |
| 579 subwfb pressure_abs+1,W ; WREG = pressure_abs - pressure_surf (high byte) | |
| 580 movwf pressure_rel_cur+1 ; store relative pressure, high byte | |
| 581 btfss STATUS,N ; relative pressure < 0 ? | |
| 582 bra isr_sensor_state2_6 ; NO - OK, keep result | |
| 583 CLRI pressure_rel_cur ; YES - set relative pressure to zero | |
| 0 | 584 |
| 623 | 585 isr_sensor_state2_6: |
| 586 ; check for new max relative pressure | |
| 587 movf pressure_rel_cur+0,W ; get current relative pressure, low byte | |
| 588 subwf pressure_rel_max+0,W ; WREG = pressure_rel_max - pressure_rel_cur (low byte) | |
| 589 movf pressure_rel_cur+1,W ; get current relative pressure, high byte | |
| 590 subwfb pressure_rel_max+1,W ; WREG = pressure_rel_max - pressure_rel_cur (high byte) | |
| 591 btfss STATUS,N ; result < 0, i.e. new max rel pressure? | |
| 592 bra isr_sensor_state2_6a ; NO | |
| 593 MOVII pressure_rel_cur,pressure_rel_max ; YES - set new max rel pressure | |
| 594 bsf trigger_pres_max_changed ; - signal a pressure max change | |
| 595 | |
| 596 isr_sensor_state2_6a: | |
| 0 | 597 |
| 623 | 598 IFDEF _min_depth_option |
| 599 ; check if min/max pressures shall be reset | |
| 600 btfss reset_trip_pressure ; shall reset the resettable min/max pressures? | |
| 601 bra isr_sensor_state2_6b ; NO | |
| 602 bcf reset_trip_pressure ; YES - clear request flag | |
| 603 CLRI pressure_rel_max_trip ; - set max pressure to zero | |
| 604 SETI pressure_rel_min_trip ; - set min pressure to biggest value possible | |
| 605 isr_sensor_state2_6b: | |
| 606 ; check for new resettable max relative pressure | |
| 607 movf pressure_rel_cur+0,W ; get current relative pressure, low byte | |
| 608 subwf pressure_rel_max_trip+0,W ; WREG = pressure_rel_max - pressure_rel_cur (low byte) | |
| 609 movf pressure_rel_cur+1,W ; get current relative pressure, high byte | |
| 610 subwfb pressure_rel_max_trip+1,W ; WREG = pressure_rel_max - pressure_rel_cur (high byte) | |
| 611 btfss STATUS,N ; result < 0, i.e. new max rel pressure? | |
| 612 bra isr_sensor_state2_6c ; NO - continue checking for min depth | |
| 613 MOVII pressure_rel_cur,pressure_rel_max_trip ; YES - set new max rel pressure | |
| 614 isr_sensor_state2_6c: | |
| 615 ; check for new resettable min relative pressure | |
| 616 movf pressure_rel_cur+0,W ; get current relative pressure, low byte | |
| 617 subwf pressure_rel_min_trip+0,W ; WREG = pressure_rel_min - pressure_rel_cur (low byte) | |
| 618 movf pressure_rel_cur+1,W ; get current relative pressure, high byte | |
| 619 subwfb pressure_rel_min_trip+1,W ; WREG = pressure_rel_min - pressure_rel_cur (high byte) | |
| 620 btfss STATUS,C ; result > 0, i.e. new min rel pressure? | |
| 621 bra sensor_int_state_exit ; NO - done | |
| 622 MOVII pressure_rel_cur,pressure_rel_min_trip ; YES - set new min rel pressure | |
| 623 ENDIF ; _min_depth_option | |
| 624 bra sensor_int_state_exit ; done | |
| 0 | 625 |
| 626 sensor_int_state1_plus_restart: | |
| 643 | 627 clrf pressure_abs_avg+0 ; clear pressure average register |
| 628 clrf pressure_abs_avg+1 | |
| 629 clrf pressure_abs_avg+2 | |
| 634 | 630 CLRI temperature_avg ; clear average register for temperature |
| 0 | 631 |
| 632 sensor_int_state1: | |
| 643 | 633 call get_temperature_value ; state 1: get temperature... |
| 604 | 634 call get_pressure_start ; ...and start pressure integration |
| 582 | 635 bra sensor_int_state_exit |
| 0 | 636 |
| 637 sensor_int_state2: | |
| 604 | 638 call get_pressure_value ; state 2: get pressure (51 us)... |
| 639 call get_temperature_start ; ...and start temperature integration (73.5 us) | |
| 640 call calculate_compensation ; .. and calculate temperature compensated pressure (233 us) | |
| 582 | 641 |
| 0 | 642 sensor_int_state_exit: |
| 634 | 643 bcf PIR5,TMR7IF ; clear IRQ flag |
| 644 bsf PIE5,TMR7IE ; re-enable IRQs by TMR7 | |
| 623 | 645 bra isr_adjust_speed ; set/restore CPU speed and return |
| 646 | |
| 656 | 647 isr_sensor_state_sensor_error: |
| 648 bsf i2c_reinit_sensor2 ; Do an I2C reset and reinitialize the pressure sensor type 2 | |
| 649 ; Done in I2C_get_press_val_MS5837 and I2C_get_temp_val_MS5837 | |
| 650 bra isr_sensor_state_sensor_resume ; Ignore the reading and continue | |
| 634 | 651 ;----------------------------------------------------------------------------- |
| 652 ; Helper Function for Display Dimming | |
| 653 ; | |
| 623 | 654 isr_dimm_tft: ; adjust until max_CCPR1L = CCPR1L |
| 628 | 655 btfsc screen_type3 ; screen type 3 ? |
| 656 return ; YES - ignore, no dimming function with screen type 3 | |
| 623 | 657 btfsc tft_is_dimming ; is the display dimming? |
| 658 return ; YES - ignore | |
| 659 movf max_CCPR1L,W ; NO - proceed | |
| 628 | 660 cpfsgt CCPR1L ; - CCPR1L > max_CCPR1L ? |
| 661 bra isr_dimm_tft2 ; NO - dim up | |
| 662 decf CCPR1L,F ; YES - dim down | |
| 663 return ; - done | |
| 623 | 664 isr_dimm_tft2: |
| 628 | 665 movf max_CCPR1L,W ; |
| 666 sublw ambient_light_min_eco ; | |
| 623 | 667 cpfsgt CCPR1L ; CCPR1L > max_CCPR1L - ambient_light_min_eco ? |
| 668 bra isr_dimm_tft3 ; NO - dim up slow | |
| 628 | 669 movlw .10 ; YES - dim up faster (+10) |
| 670 addwf CCPR1L,F ; - add to dimming value | |
| 623 | 671 isr_dimm_tft3: |
| 628 | 672 incf CCPR1L,F ; - dim up (+1) |
| 673 return ; - done | |
| 623 | 674 |
| 582 | 675 |
| 634 | 676 ;----------------------------------------------------------------------------- |
| 677 ; RTC Interrupt (invoked every 0.5 Seconds) | |
| 623 | 678 ; |
| 679 isr_rtcc: | |
| 680 bcf PIR3,RTCCIF ; clear flag | |
| 681 bsf trigger_half_second ; set flag for a new 1/2 second has begun | |
| 682 btfsc reset_timebase ; shall reset the timebase? | |
| 683 bra isr_rtcc_1 ; YES - warp to new full second | |
| 684 btg timebase_0sec ; NO - toggle the 1/2 second timebase | |
| 685 btfsc timebase_0sec ; - did it toggled 1 -> 0 ? | |
| 686 return ; NO - on half second, done | |
| 0 | 687 |
| 623 | 688 isr_rtcc_1: |
| 689 ; new full second | |
| 690 bsf trigger_full_second ; set flag for a new 1/1 second has begun | |
| 691 | |
| 692 btfsc block_rtc_access ; ISR suspended from accessing the RTC? | |
| 693 bra isr_rtcc_2 ; YES | |
| 694 | |
| 695 banksel RTCCFG ; RTC registers are outside access RAM | |
| 582 | 696 bsf RTCCFG,RTCPTR1 |
| 623 | 697 bsf RTCCFG,RTCPTR0 |
| 698 banksel isr_backup ; back to ISR default bank | |
| 699 | |
| 700 movff RTCVALL,rtc_year ; read year in BCD | |
| 701 movff RTCVALH,rtc_day ; dummy read | |
| 702 movff RTCVALL,rtc_day ; read day in BCD | |
| 703 movff RTCVALH,rtc_month ; read month in BCD | |
| 704 movff RTCVALL,rtc_hour ; read hour in BCD | |
| 705 movff RTCVALH,rtc_secs ; dummy read | |
| 706 movff RTCVALL,rtc_secs ; read seconds in BCD | |
| 707 movff RTCVALH,rtc_mins ; read minutes in BCD | |
| 582 | 708 |
| 634 | 709 ; convert BCD to DEC and set registers |
| 623 | 710 movf rtc_mins,W |
| 711 rcall isr_rtcc_convert_BCD_DEC ; convert to decimal with result in WREG | |
| 712 movwf rtc_mins | |
| 713 movf rtc_secs,W | |
| 714 rcall isr_rtcc_convert_BCD_DEC ; convert to decimal with result in WREG | |
| 715 movwf rtc_secs | |
| 716 movf rtc_hour,W | |
| 717 rcall isr_rtcc_convert_BCD_DEC ; convert to decimal with result in WREG | |
| 718 movwf rtc_hour | |
| 719 movf rtc_month,W | |
| 720 rcall isr_rtcc_convert_BCD_DEC ; convert to decimal with result in WREG | |
| 721 movwf rtc_month | |
| 722 movf rtc_day,W | |
| 723 rcall isr_rtcc_convert_BCD_DEC ; convert to decimal with result in WREG | |
| 724 movwf rtc_day | |
| 725 movf rtc_year,W | |
| 726 rcall isr_rtcc_convert_BCD_DEC ; convert to decimal with result in WREG | |
| 727 movwf rtc_year | |
| 728 | |
| 729 isr_rtcc_2: | |
| 629 | 730 ; every full second - get ambient light level and set max_CCPR1L |
| 623 | 731 call get_ambient_level ; get ambient light level and set max_CCPR1L |
| 732 banksel isr_backup ; back to ISR default bank (for safety only) | |
| 733 | |
| 734 rcall isr_battery_gauge ; calculate the current charge consumption and add it to the battery gauge | |
| 735 rcall isr_update_uptime ; increment overall OSTC uptime | |
| 736 rcall isr_update_timeout ; process the timeout timer | |
| 737 | |
| 645 | 738 movff opt_brightness_surface,brightness ; copy brightness - will be overwritten with opt_brightness_dive if in dive mdoe |
| 623 | 739 btfsc divemode ; in dive mode? |
| 740 rcall isr_divemode_1sec ; YES - do the every second dive mode tasks | |
| 741 | |
| 629 | 742 ; increment surface interval counted in seconds |
| 623 | 743 btfsc divemode ; in dive mode? |
| 744 btfsc simulatormode ; YES - in simulator mode? | |
| 629 | 745 rcall inc_surface_interval_secs ; NO - YES - increment the surface interval (seconds timer) |
| 746 | |
| 747 ; reset the surface interval timers if requested | |
| 748 btfsc reset_surface_interval ; shall reset both surface interval timers? | |
| 634 | 749 rcall rst_surface_interval ; YES |
| 0 | 750 |
| 623 | 751 ; reset the timebase if requested |
| 752 btfss reset_timebase ; shall reset the timebase? | |
| 753 bra isr_rtcc_3 ; NO | |
| 754 bcf reset_timebase ; YES - clear request flag | |
| 755 clrf eventbase ; - clear all pending events | |
| 756 clrf timebase ; - clear all timebase flags | |
| 757 clrf timebase_secs ; - clear seconds timer | |
| 758 clrf timebase_mins ; - clear minutes timer | |
| 759 clrf simulator_time ; - clear minutes timer of simulator runtime as well | |
| 760 bsf trigger_half_second ; - set flag for a new 1/2 second has begun | |
| 761 bsf trigger_full_second ; - set flag for a new 1/1 second has begun | |
| 762 return ; - done | |
| 763 | |
| 764 isr_rtcc_3: | |
| 765 ; count-up the 2 seconds timebase timer | |
| 766 btg timebase_1sec ; toggle the 1 second timer bit | |
| 767 btfss timebase_1sec ; did it toggled 1 -> 0 ? | |
| 768 btg timebase_2sec ; YES - toggle the 2 seconds timer bit | |
| 769 | |
| 770 ; count-up the seconds timer | |
| 771 incf timebase_secs,F ; increment seconds timer (may temporary become 60 here) | |
| 772 movlw .59 ; max. for seconds timer | |
| 773 cpfsgt timebase_secs ; seconds timer > max.? | |
| 774 return ; NO - done | |
| 0 | 775 |
| 623 | 776 ; new full minute |
| 629 | 777 clrf timebase_secs ; reset timer |
| 778 bsf trigger_full_minute ; set flag for a new minute has begun | |
| 623 | 779 |
| 629 | 780 ; increment surface interval counted in minutes |
| 781 btfsc divemode ; in dive mode? | |
| 782 btfsc simulatormode ; YES - in simulator mode? | |
| 783 rcall inc_surface_interval_mins ; NO - YES - increment surface interval (minutes timer) | |
| 623 | 784 |
| 629 | 785 btfss simulatormode ; in simulator mode? |
| 786 bra isr_rtcc_4 ; NO | |
| 787 infsnz simulator_time,F ; YES - increment real runtime of the simulator, did wrap around (became zero)? | |
| 788 setf simulator_time ; YES - disallow wrap-around, keep at 255 instead | |
| 0 | 789 |
| 623 | 790 isr_rtcc_4: |
| 629 | 791 incf timebase_mins,F ; increment minutes timer |
| 792 movlw .59 ; max. for minutes timer | |
| 793 cpfsgt timebase_mins ; minutes timer > max.? | |
| 794 return ; NO - done | |
| 623 | 795 |
| 796 ; new full hour | |
| 629 | 797 clrf timebase_mins ; YES - reset timer |
| 798 bsf trigger_full_hour ; - set flag for a new hour has begun | |
| 799 return ; - done | |
| 623 | 800 |
| 801 ; increment overall OSTC uptime | |
| 802 isr_update_uptime: | |
| 634 | 803 incf uptime+0,F ; uptime++ |
| 804 clrf WREG ; ... | |
| 805 addwfc uptime+1,F ; ... | |
| 806 addwfc uptime+2,F ; ... | |
| 807 addwfc uptime+3,F ; ... | |
| 808 return ; done | |
| 582 | 809 |
| 623 | 810 ; process the timeout timer |
| 811 isr_update_timeout: | |
| 634 | 812 btfsc restart_timeout ; shall restart the timeout? |
| 623 | 813 bra isr_update_timeout_1 ; YES |
| 814 tstfsz isr_timeout_timer ; NO - timeout timer already at zero? | |
| 634 | 815 decfsz isr_timeout_timer,F ; NO - decrement timer, reached zero now? |
| 623 | 816 return ; YES / NO - nothing further to do |
| 817 bsf trigger_timeout ; YES - set timeout flag | |
| 818 return ; - done | |
| 819 isr_update_timeout_1: | |
| 634 | 820 bcf restart_timeout ; clear request flag |
| 623 | 821 bcf trigger_timeout ; clear pending timeout trigger, if any |
| 822 movff isr_timeout_reload,isr_timeout_timer ; reload timer | |
| 823 return ; done | |
| 0 | 824 |
| 634 | 825 |
| 826 ;----------------------------------------------------------------------------- | |
| 827 ; Calculate Charge drawn from the Battery | |
| 623 | 828 ; |
| 829 isr_battery_gauge: | |
| 830 btfsc block_battery_gauge ; access to battery gauge suspended? | |
| 831 return ; YES - done | |
| 832 MOVLI current_sleepmode,isr_mpr ; NO - default to sleep mode with 100µA/3600 -> nAs | |
| 833 btfss sleepmode ; - in sleep mode? | |
| 634 | 834 rcall isr_battery_gauge2 ; NO - compute current consumption into isr_lo and isr_hi |
| 835 movf isr_mpr+0,W ; - 48 bit add of isr_mpr:2 to battery_gauge:6 | |
| 836 addwf battery_gauge+0,F ; - ... | |
| 837 movf isr_mpr+1,W ; - ... | |
| 838 addwfc battery_gauge+1,F ; - ... | |
| 839 clrf WREG ; - ... | |
| 840 addwfc battery_gauge+2,F ; - ... | |
| 841 addwfc battery_gauge+3,F ; - ... | |
| 842 addwfc battery_gauge+4,F ; - ... | |
| 843 addwfc battery_gauge+5,F ; - ... | |
| 844 return ; - done | |
| 582 | 845 |
| 634 | 846 |
| 847 ;----------------------------------------------------------------------------- | |
| 848 ; Helper Function - compute current Consumption | |
| 849 ; | |
| 0 | 850 isr_battery_gauge2: |
| 634 | 851 ; Set consumption rate in nAs (nano Ampere x seconds) |
| 0 | 852 ; Example: |
| 623 | 853 ; MOVLI .55556,isr_mpr ; 0.2 Ah / 3600 seconds per hour * 1e9s = nAs |
| 854 ; | |
| 855 ; Remark: although all the constants are named current_xxxx, in reality they mean charge! | |
| 0 | 856 |
| 623 | 857 ; Calculate current consumption for LED backlight: 47*CCPR1L+272 (according to values in hwos.inc: 115*CCPR1L+216) |
| 631 | 858 movlw .70 ; screen type 3 has a fix backlight current slope |
| 859 btfss screen_type3 ; does the OSTC have a screen type 3 ? | |
| 860 movf CCPR1L,W ; NO - for screen types 0, 1 and 2 get the slope from CCPR1L | |
| 861 mullw current_backlight_multi ; multiply with backlight factor current_backlight_multi | |
| 862 ADDLI current_backlight_offset,PRODL ; add backlight offset current_backlight_offset | |
| 623 | 863 MOVII PRODL,isr_mpr ; copy result to isr_mpr |
| 0 | 864 |
| 634 | 865 ; add current for CPU and GPU |
| 623 | 866 ; cpu_speed_state = ECO 3.10 mA -> 861 nAs |
| 867 ; = NORMAL 5.50 mA -> 1528 nAs | |
| 868 ; = FASTEST 8.04 mA -> 2233 nAs | |
| 869 btfss speed_is_eco ; speed = eco ? | |
| 870 bra isr_battery_gauge3 ; NO | |
| 871 ADDLI current_speed_eco,isr_mpr ; YES - add current_speed_eco to isr_mpr | |
| 582 | 872 bra isr_battery_gauge5 |
| 0 | 873 isr_battery_gauge3: |
| 623 | 874 btfss speed_is_normal ; speed = normal? |
| 875 bra isr_battery_gauge4 ; NO | |
| 876 ADDLI current_speed_normal,isr_mpr ; YES - add current_speed_normal to isr_mpr | |
| 582 | 877 bra isr_battery_gauge5 |
| 0 | 878 isr_battery_gauge4: |
| 623 | 879 ADDLI current_speed_fastest,isr_mpr ; speed is fastest, add current_speed_fastest to isr_mpr |
| 0 | 880 isr_battery_gauge5: |
| 623 | 881 btfss ir_power ; IR enabled? |
| 882 bra isr_battery_gauge6 ; NO | |
| 883 ADDLI current_ir_receiver,isr_mpr ; YES - add current_ir_receiver to isr_mpr | |
| 0 | 884 isr_battery_gauge6: |
| 623 | 885 btfss compass_enabled ; compass active? |
| 886 bra isr_battery_gauge7 ; NO | |
| 887 ADDLI current_compass,isr_mpr ; YES - add current_compass to isr_mpr | |
| 0 | 888 isr_battery_gauge7: |
| 634 | 889 return ; done |
| 0 | 890 |
| 623 | 891 |
| 634 | 892 ;----------------------------------------------------------------------------- |
| 893 ; Every Second Tasks while in Dive Mode | |
| 623 | 894 ; |
| 0 | 895 isr_divemode_1sec: |
| 645 | 896 movff opt_brightness_divemode,brightness ; copy brightness for dive mode |
| 623 | 897 decfsz sampling_timer,F ; decrement sampling timer, became zero? |
| 898 bra isr_divemode_1sec_1 ; NO | |
| 899 bsf trigger_sample_divedata ; YES - set trigger flag for sampling dive data | |
| 900 movff sampling_rate,sampling_timer; - reload timer | |
| 0 | 901 |
| 623 | 902 isr_divemode_1sec_1: |
| 903 btfss reset_timebase ; shall reset the timebase? (request flag will be cleared later) | |
| 904 bra isr_divemode_1sec_2 ; NO | |
| 905 CLRI total_divetime_secs ; YES - reset total dive time, seconds (2 byte) | |
| 906 clrf counted_divetime_secs ; - reset counted dive time, seconds (1 byte) | |
| 907 CLRI counted_divetime_mins ; - reset counted dive time, minutes (2 byte) | |
| 908 clrf apnoe_dive_secs ; - reset apnoe dive time, seconds (1 byte) | |
| 909 clrf apnoe_dive_mins ; - reset apnoe dive time, minutes (1 byte) | |
| 910 bcf apnoe_at_surface ; - apnoe mode starts in submerged state | |
| 911 return ; - done | |
| 582 | 912 |
| 623 | 913 isr_divemode_1sec_2: |
| 914 INCI total_divetime_secs ; increase total dive time (regardless of start_dive_threshold) | |
| 915 btfss count_divetime ; shall the dive time be counted (regarding start_dive_threshold)? | |
| 629 | 916 bra isr_divemode_1sec_4 ; NO - too shallow / apnoe at surface |
| 623 | 917 incf counted_divetime_secs,F ; YES - increase dive time (displayed dive time) |
| 918 movlw d'59' ; - 60 seconds make a minute | |
| 919 cpfsgt counted_divetime_secs ; - next full minute reached? | |
| 629 | 920 bra isr_divemode_1sec_3 ; NO - continue |
| 623 | 921 clrf counted_divetime_secs ; YES - reset seconds to 0 |
| 922 INCI counted_divetime_mins ; - increase dive minutes | |
| 923 bsf divetime_longer_1min ; - set flag for dive time exceeding 1 minute | |
| 629 | 924 ;bra isr_divemode_1sec_3 ; - continue |
| 0 | 925 |
| 623 | 926 isr_divemode_1sec_3: ; submerged |
| 927 btfss FLAG_apnoe_mode ; in apnoe mode? | |
| 928 return ; NO - done | |
| 929 btfss apnoe_at_surface ; - been at surface before? | |
| 930 bra isr_divemode_1sec_3a ; NO - increment the dive time | |
| 931 bcf apnoe_at_surface ; YES - a new dive has begun | |
| 932 bsf apnoe_new_dive ; - signal a new dive has begun | |
| 933 clrf apnoe_surface_secs ; - clear surface seconds | |
| 934 clrf apnoe_surface_mins ; - clear surface minutes | |
| 935 clrf apnoe_dive_secs ; - clear dive seconds | |
| 936 clrf apnoe_dive_mins ; - clear dive minutes | |
| 937 MOVII pressure_rel_cur,pressure_rel_max ; - reset max pressure to current pressure | |
| 938 bsf trigger_pres_max_changed ; - signal a new maximum pressure | |
| 939 isr_divemode_1sec_3a: | |
| 940 incf apnoe_dive_secs,F ; increment dive time, seconds | |
| 941 movlw d'59' ; 60 seconds make a minute | |
| 942 cpfsgt apnoe_dive_secs ; next full minute reached? | |
| 943 return ; NO - done | |
| 944 clrf apnoe_dive_secs ; YES - reset seconds to 0 | |
| 945 incf apnoe_dive_mins,F ; - increment dive time, minutes | |
| 946 return ; - done | |
| 0 | 947 |
| 623 | 948 isr_divemode_1sec_4: ; at surface |
| 645 | 949 movff opt_brightness_surface,brightness ; copy brightness |
| 623 | 950 btfss FLAG_apnoe_mode ; in apnoe mode? |
| 951 return ; NO - done | |
| 952 bsf apnoe_at_surface ; YES - memorize been at the surface | |
| 953 incf apnoe_surface_secs,F ; - increment surface time, seconds | |
| 954 movlw d'59' ; - 60 seconds make a minute | |
| 955 cpfsgt apnoe_surface_secs ; - next full minute reached? | |
| 956 return ; NO - done | |
| 957 clrf apnoe_surface_secs ; YES - reset seconds to 0 | |
| 958 incf apnoe_surface_mins,F ; - increment surface time, minutes | |
| 959 return ; - done | |
| 960 | |
| 961 | |
| 634 | 962 ;----------------------------------------------------------------------------- |
| 963 ; Helper Function - BCD to Binary conversion | |
| 964 ; | |
| 623 | 965 ; Input WREG = value in BCD |
| 966 ; Output WREG = value in binary | |
| 967 ; | |
| 968 isr_rtcc_convert_BCD_DEC: | |
| 634 | 969 movwf isr_lo ; copy BCD to isr_lo |
| 970 swapf isr_lo, W ; create swapped copy in WREG | |
| 971 andlw 0x0F ; keep only the tens | |
| 972 rlncf WREG, W ; WREG = 2 * tens | |
| 623 | 973 subwf isr_lo, F ; 16 * tens + ones - 2*tens |
| 974 subwf isr_lo, F ; 14 * tens + ones - 2*tens | |
| 975 subwf isr_lo, W ; 12 * tens + ones - 2*tens | |
| 634 | 976 return ; done |
| 0 | 977 |
| 978 | |
| 634 | 979 ;----------------------------------------------------------------------------- |
| 980 ; Check Buttons | |
| 623 | 981 ; |
| 582 | 982 isr_switch_right: |
| 640 | 983 bcf INTCON,INT0IE ; disable external interrupt 0 |
| 984 | |
| 643 | 985 btfsc button_hold_down_allowed ; ignore for mechanical push buttons |
| 640 | 986 bra isr_switch_right2 |
| 987 btfsc T4CON,TMR4ON ; Timer4 running? | |
| 988 bra timer4_restart ; Yes, restart | |
| 989 isr_switch_right2: | |
| 990 | |
| 582 | 991 btfss flip_screen ; 180° flipped? |
| 634 | 992 bsf switch_right ; NO - set flag for right button |
| 582 | 993 btfsc flip_screen ; 180° flipped? |
| 634 | 994 bsf switch_left ; YES - set flag for left button |
| 995 bra isr_switch_common ; continue with common part | |
| 0 | 996 |
| 582 | 997 isr_switch_left: |
| 640 | 998 bcf INTCON3,INT1IE ; disable external interrupt 1 |
| 999 | |
| 643 | 1000 btfsc button_hold_down_allowed ; ignore for mechanical push buttons |
| 640 | 1001 bra isr_switch_left2 |
| 1002 btfsc T4CON,TMR4ON ; Timer4 running? | |
| 1003 bra timer4_restart ; Yes, restart | |
| 1004 isr_switch_left2: | |
| 1005 | |
| 582 | 1006 btfss flip_screen ; 180° flipped? |
| 634 | 1007 bsf switch_left ; NO - set flag for left button |
| 582 | 1008 btfsc flip_screen ; 180° flipped? |
| 634 | 1009 bsf switch_right ; YES - set flag for right button |
| 1010 ;bra isr_switch_common ; continue with common part | |
| 623 | 1011 |
| 0 | 1012 isr_switch_common: |
| 643 | 1013 btfss button_hold_down_allowed ; ignore for mechanical push buttons |
| 640 | 1014 bsf T4CON,TMR4ON ; Start timer 4 |
| 1015 | |
| 634 | 1016 btfsc tmr5_preemtion_allowed ; timer 5 preemption allowed? |
| 1017 bsf PIR5,TMR5IF ; YES - preempt timer 5 | |
| 1018 movlw TMR1H_VALUE_FIRST ; load timer 1 (in steps of 7.8125 ms) | |
| 623 | 1019 movwf TMR1H ; ... |
| 634 | 1020 clrf TMR1L ; ... |
| 604 | 1021 bsf T1CON,TMR1ON ; start timer 1 |
| 640 | 1022 bcf INTCON3,INT1IF ; clear ext. int 1 request |
| 1023 bcf INTCON,INT0IF ; clear ext. int 0 request | |
| 634 | 1024 return ; done |
| 0 | 1025 |
| 623 | 1026 |
| 634 | 1027 ;----------------------------------------------------------------------------- |
| 1028 ; Button hold-down Interrupt | |
| 623 | 1029 ; |
| 0 | 1030 timer1int: |
| 634 | 1031 bcf PIR1,TMR1IF ; clear timer 1 IRQ request |
| 640 | 1032 bcf INTCON,INT0IF ; clear ext. int 0 request |
| 1033 bcf INTCON3,INT1IF ; clear ext. int 1 request | |
| 634 | 1034 |
| 451 | 1035 ; digital |
| 604 | 1036 btfss switch_left1 ; left button hold-down? |
| 1037 bra timer1int_left ; YES | |
| 1038 btfss switch_right2 ; right button hold-down? | |
| 1039 bra timer1int_right ; YES | |
| 582 | 1040 |
| 623 | 1041 ; analog |
| 604 | 1042 btfsc analog_sw2_pressed ; left button hold-down? |
| 1043 bra timer1int_left ; YES | |
| 1044 btfsc analog_sw1_pressed ; right button hold-down? | |
| 1045 bra timer1int_right ; YES | |
| 582 | 1046 |
| 634 | 1047 ; no button hold-down, stop timer 1 |
| 604 | 1048 bcf T1CON,TMR1ON ; stop timer 1 |
| 634 | 1049 bsf INTCON,INT0IE ; enable INT0 IRQ |
| 1050 bsf INTCON3,INT1IE ; enable INT1 IRQ | |
| 451 | 1051 return |
| 0 | 1052 |
| 1053 timer1int_left: | |
| 582 | 1054 btfss flip_screen ; 180° flipped? |
| 634 | 1055 bsf switch_left ; set flag for left button |
| 582 | 1056 btfsc flip_screen ; 180° flipped? |
| 634 | 1057 bsf switch_right ; set flag for right button |
| 604 | 1058 bra timer1int_common ; continue |
| 623 | 1059 |
| 0 | 1060 timer1int_right: |
| 582 | 1061 btfss flip_screen ; 180° flipped? |
| 634 | 1062 bsf switch_right ; set flag for right button |
| 582 | 1063 btfsc flip_screen ; 180° flipped? |
| 634 | 1064 bsf switch_left ; set flag for left button |
| 623 | 1065 ;bra timer1int_common ; continue |
| 1066 | |
| 634 | 1067 timer1int_common: ; load timer 1 for next button press |
| 623 | 1068 movlw TMR1H_VALUE_CONT ; default to surface mode value |
| 1069 btfsc divemode ; in dive mode? | |
| 1070 movlw TMR1H_VALUE_CONT_DIVE ; YES - overwrite with dive mode value | |
| 1071 movwf TMR1H ; write value to timer, high byte | |
| 634 | 1072 clrf TMR1L ; write value to timer, low byte (zero) |
| 1073 return ; done (timer1 kept running) | |
| 0 | 1074 |
| 623 | 1075 |
| 634 | 1076 ;----------------------------------------------------------------------------- |
| 1077 ; Increment Surface Interval (counted in minutes and in seconds) | |
| 623 | 1078 ; |
| 1079 ; int_O_desaturation_time is only computed while in start, surface mode, | |
| 629 | 1080 ; menue_tree or ghostwriter. So the ISR may clock surface_interval_mins |
| 634 | 1081 ; past the actual surface interval time. But TFT_surf_cv_lastdive will |
| 629 | 1082 ; check int_O_desaturation_time and in case int_O_desaturation_time is |
| 1083 ; zero it will not show surface_interval_mins but surface_interval_secs instead. | |
| 1084 ; Thus the glitch will remain invisible. | |
| 623 | 1085 ; |
| 634 | 1086 inc_surface_interval_secs: ; called every second when not in dive mode |
| 1087 incf surface_interval_secs+0,F ; increment the lowest byte | |
| 1088 clrf WREG ; clear WREG | |
| 1089 addwfc surface_interval_secs+1,F ; add carry from byte before, if it did wrap-around | |
| 1090 addwfc surface_interval_secs+2,F ; add carry from byte before, if it did wrap-around | |
| 1091 addwfc surface_interval_secs+3,F ; add carry from byte before, if it did wrap-around | |
| 1092 return ; done | |
| 629 | 1093 |
| 634 | 1094 inc_surface_interval_mins: ; called every minute when not in dive mode |
| 1095 movff int_O_desaturation_time+0,isr_lo ; get desaturation time, low byte | |
| 1096 movff int_O_desaturation_time+1,WREG ; get desaturation time, high byte | |
| 1097 iorwf isr_lo,W ; inclusive-or low & high byte, desaturation time = 0 ? | |
| 1098 bz clr_surface_interval_mins ; YES - reset surface interval minutes counter | |
| 1099 INCI surface_interval_mins ; NO - increment surface interval | |
| 1100 return ; - done | |
| 629 | 1101 |
| 631 | 1102 rst_surface_interval: |
| 629 | 1103 bcf reset_surface_interval ; reset request flag |
| 631 | 1104 ; reset the surface interval counted in seconds |
| 629 | 1105 clrf surface_interval_secs+0 ; reset surface interval (seconds), lowest byte |
| 1106 clrf surface_interval_secs+1 ; ... | |
| 1107 clrf surface_interval_secs+2 ; ... | |
| 1108 clrf surface_interval_secs+3 ; reset surface interval (seconds), highest byte | |
| 631 | 1109 ; reset the surface interval counted in minutes |
| 629 | 1110 movff opt_diveTimeout,surface_interval_mins+0 ; set surface interval (minutes), low byte, to dive timeout offset |
| 1111 clrf surface_interval_mins+1 ; reset surface interval (minutes), high byte | |
| 631 | 1112 return ; done |
| 629 | 1113 |
| 631 | 1114 clr_surface_interval_mins: |
| 1115 clrf surface_interval_mins+0 ; reset surface interval (minutes), low byte | |
| 1116 clrf surface_interval_mins+1 ; reset surface interval (minutes), high byte | |
| 629 | 1117 return ; done |
| 1118 | |
| 634 | 1119 ;----------------------------------------------------------------------------- |
| 0 | 1120 |
| 582 | 1121 END |
