diff src/start.asm @ 604:ca4556fb60b9

bump to 2.99beta, work on 3.00 stable
author heinrichsweikamp
date Thu, 22 Nov 2018 19:47:26 +0100
parents 05053910d668
children d866684249bd
line wrap: on
line diff
--- a/src/start.asm	Thu Oct 11 21:06:29 2018 +0200
+++ b/src/start.asm	Thu Nov 22 19:47:26 2018 +0100
@@ -1,6 +1,6 @@
 ;=============================================================================
 ;
-;   File start.asm									REFACTORED VERSION	V2.98
+;   File start.asm									REFACTORED VERSION	V2.99e
 ;
 ;   Startup subroutines
 ;
@@ -9,10 +9,10 @@
 ; HISTORY
 ;  2011-08-06 : [mH] moving from OSTC code
 
-#include "hwos.inc"							; Mandatory header
+#include "hwos.inc"							; mandatory header
 #include "ms5541.inc"
 #include "isr.inc"
-#include "shared_definitions.h"				; Mailbox from/to p2_deco.c
+#include "shared_definitions.h"				; mailbox from/to p2_deco.c
 #include "eeprom_rs232.inc"
 #include "math.inc"
 #include "tft.inc"
@@ -28,9 +28,7 @@
 
 	extern	init_ostc
 	extern	option_restore_all
-	extern	deco_setup
 	extern	restore_decodata_from_eeprom
-	extern	color_image
 	extern	oPressureAdjust
 	extern	option_reset
 	extern	option_save
@@ -39,36 +37,48 @@
 	extern	do_new_battery_select
 	extern	use_old_batteries
 	extern	use_old_prior_209
+	extern	get_first_gas_to_WREG
+	extern	get_first_dil_to_WREG
+	extern	option_cleanup_oCCRMode_pSCR
+	extern	option_cleanup_oCCRMode_CCR
+
+ IFDEF _rx_functions
+	extern	option_cleanup_oTrMode_CCR
+	extern	option_cleanup_oTrMode_no_CCR
+ ENDIF
+
 
 ;=============================================================================
-; Reset vector: What to do on device wake-up and hard reset.
+; Reset Vector: entry point on device wake-up and hard reset
 ;
 reset_v code 0x00000
 
 ;	goto	start
-	goto	0x1FF00							; Bootloader
+	goto	0x1FF00							; bootloader
 
-	ORG		0x00004							; Needed for second-level bootloader
+	ORG		0x00004							; needed for second-level bootloader
 	goto	start
 
 ;=============================================================================
 
-boot	CODE
+boot		CODE
+
+;=============================================================================
 
 	global	start
 start:
-	lfsr	FSR0,0x000						; Clear rambank 0-14
+	lfsr	FSR0,0x000						; clear ram-banks 0-14
 clear_rambank:
 	clrf	POSTINC0
 	movlw	0x0F
-	cpfseq	FSR0H							; Bank 14 done?
-	bra		clear_rambank					; clear...
+	cpfseq	FSR0H							; bank 14 done?
+	bra		clear_rambank					; NO - loop
 
-	call	init_ostc
+	call	init_ostc						; initialize hardware (ports, timers, etc.)
 
-	; Get button type from Bootloader-Info
+	; get button type from Bootloader-Info
 	movlw	.16
-	movff	WREG,analog_counter				; init averaging
+	movff	WREG,analog_counter				; initialize averaging
 	bsf		analog_switches
 	movlw	0x7C
 	movwf	TBLPTRL
@@ -76,7 +86,7 @@
 	movwf	TBLPTRH
 	movlw	0x01
 	movwf	TBLPTRU
-	TBLRD*+									; Reads 0x07 for analog buttons
+	TBLRD*+									; reads 0x07 for analog buttons
 	movlw	0x07
 	cpfseq	TABLAT
 	bcf		analog_switches
@@ -86,23 +96,23 @@
 	movwf	EEADR
 	movlw	HIGH .897
 	movwf	EEADRH
-	call	read_eeprom						; EEDATA into EEPROM@EEADR
-	clrf	EEADRH							; Reset EEADRH
-	movff	EEDATA,button_polarity			; 0xFF (Both normal), 0x00 (Both inverted), 0x01 (Left inverted only), 0x02 (Right inverted only) 
+	call	read_eeprom						; EEDATA into EEPROM @ EEADR
+	clrf	EEADRH							; reset EEADRH
+	movff	EEDATA,button_polarity			; 0xFF (both normal), 0x00 (both inverted), 0x01 (left inverted only), 0x02 (right inverted only)
 
-	; Air pressure compensation	after reset
+	; air pressure compensation after reset
 	call	get_calibration_data			; get calibration data from pressure sensor
 	banksel	common							; get_calibration_data uses isr_backup
 
 	call	TFT_DisplayOff					; turn off display
 	bsf		LEDr							; turn on  red LED
 	bcf		pressure_refresh
-	; First pass will not have valid temperature!
-	btfss	pressure_refresh				; Air pressure compensation
+	; first pass will not have valid temperature
+	btfss	pressure_refresh				; air pressure compensation
 	bra		$-2
-	; Second pass
+	; second pass
 	bcf		pressure_refresh
-	btfss	pressure_refresh				; Air pressure compensation
+	btfss	pressure_refresh				; air pressure compensation
 	bra		$-2
 	bcf		LEDr
 
@@ -120,10 +130,10 @@
 	movff	last_surfpressure+0,sub_b+0
 	movff	last_surfpressure+1,sub_b+1
 	call	subU16							; sub_c = sub_a - sub_b
-	btfss	neg_flag						; Is 1080mbar < amb_pressure ?
-	bra		start_copy_pressure				; NO: current air pressure is lower then "allowed" air pressure, ok!
+	btfss	neg_flag						; is 1080 mbar < amb_pressure ?
+	bra		start_copy_pressure				; NO - current air pressure is lower then "allowed" air pressure, ok
 
-	; not ok! Overwrite with max. "allowed" air pressure
+	; not ok - overwrite with max. "allowed" air pressure
 	movlw	LOW  max_surfpressure
 	movff	WREG,last_surfpressure+0		; max. "allowed" air pressure in mbar
 	movlw	HIGH max_surfpressure
@@ -142,9 +152,7 @@
 	movff	int_I_pres_respiration+0,int_I_pres_surface+0	; surface pressure
 	movff	int_I_pres_respiration+1,int_I_pres_surface+1
 
-	call	deco_setup						; set up all model parameters (code located in simulator.asm)
-
-	call	deco_clear_tissue				; Set all tissues to Pamb * N2_ratio (code located in p2_deco.c)
+	call	deco_clear_tissue				; set all tissues to Pamb * N2_ratio (code located in p2_deco.c)
 	banksel	common							; back to bank 1, needed after every return from C code
 
 	call	rtc_init						; init clock
@@ -155,8 +163,8 @@
 	clrf	EEADRH
 	movlw	0xAA
 	cpfseq	EEDATA							; =0xAA
-	bra		no_deco_restore					; No
-	call	restore_decodata_from_eeprom	; Reload deco data and date/time from eeprom
+	bra		no_deco_restore					; NO
+	call	restore_decodata_from_eeprom	; reload deco data and date/time from eeprom
 no_deco_restore:
 	call	deco_calc_dive_interval_1min	; calculate deco in surface mode
 	call	deco_calc_desaturation_time		; calculate desaturation and no-fly time
@@ -164,32 +172,57 @@
 
 	bcf		menubit							; clear menu flag
 
-	; Check for Power-on reset here
+	; check for power-on reset here
 
 	; *****************************************************************************
 	; "new_battery_menu" and "use_old_batteries" 'goto' back to "power_on_return"
 	; *****************************************************************************
 
 	; Try to migrate the old battery status from firmware 2.09 or earlier..
-	btfsc	RCON,POR						; Was this a power-on reset?
-	call	use_old_prior_209				; No
+	btfsc	RCON,POR						; was this a power-on reset?
+	call	use_old_prior_209				; NO
 
 	bcf		use_old_batt_flag
-	btfsc	RCON,POR						; Was this a power-on reset?
-	bsf		use_old_batt_flag				; No
+	btfsc	RCON,POR						; was this a power-on reset?
+	bsf		use_old_batt_flag				; NO
  
-	call	lt2942_get_status				; Check for gauge IC
+	call	lt2942_get_status				; check for gauge IC
 	btfss	battery_gauge_available			; cR or 2 hardware?
-	bra		power_on_return2				; no
+	bra		check_firmware_new				; NO  - skip next
+	movlw	.30								; YES - reset button sensitivity
+	movff	WREG,opt_cR_button_right
+	movff	WREG,opt_cR_button_left			; reset on power-on reset
+	call	piezo_config					; configure buttons
+	call	piezo_config					; configure buttons (2 times)
+
+check_firmware_new:
+	call	TFT_boot						; initialize TFT (includes clear screen)
+	clrf	CCPR1L							; backlight off
+
+	WIN_TOP  .40
+	WIN_LEFT .10
+	TFT_WRITE_PROM_IMAGE_BY_ADDR hw_logo_block ; show heinrichsweikamp logo
 
-	movlw	.30
-	movff	WREG,opt_cR_button_right
-	movff	WREG,opt_cR_button_left			; Reset on power-on reset
-	call	piezo_config					; Yes, configure buttons
-	call	piezo_config					; Yes, configure buttons (2 times)
+	call	TFT_standard_color
+
+	WIN_SMALL .20,.100
+	STRCPY_PRINT "Update successful!"		; hard coded since language switch does not work here
 
-power_on_return2:
-; check firmware and reset Custom Functions after an update
+	WIN_SMALL .20,.140
+	STRCPY	"New Firmware: "
+	call	TFT_cat_firmware				; show firmware version x.y and color-code if outdated
+	STRCAT_PRINT ""							; finalize output
+	bcf		win_invert						; reset inverted output if firmware is outdated
+	call	TFT_standard_color				; reset color           if firmware is outdated
+
+	WIN_SMALL .60,.180
+	call	TFT_cat_beta_release			; if it is a beta version, show "BETA" + issue, else "Release"
+	STRCAT_PRINT ""							; finalize output
+	call	TFT_standard_color				; reset color
+
+	call	TFT_Display_FadeIn				; display resulting surface screen
+
+	; check if a new firmware was loaded, if yes reset Custom Function oPressureAdjust
 	movlw	d'1'
 	movwf	EEADR							; =1
 	movwf	EEADRH							; =1
@@ -198,53 +231,25 @@
 	incf	EEADR,F							; set to 0x102
 	call	read_eeprom						; read current version y
 	movff	EEDATA,hi
-	clrf	EEADRH							; Reset EEADRH
+	clrf	EEADRH							; reset EEADRH
 
 	movlw	softwareversion_x
-	cpfseq	lo							; compare version x
-	bra		check_firmware_new				; is not equal -> reset CF and store new version in EEPROM
+	cpfseq	lo								; compare version x
+	bra		check_firmware_new4				; is not equal -> reset CF and store new version in EEPROM
 
 	movlw	softwareversion_y
-	cpfseq	hi							; compare version y
-	bra		check_firmware_new				; is not equal -> reset CF and store new version in EEPROM
-	bra		restart							; x and y are equal -> do not reset cf
+	cpfseq	hi								; compare version y
+	bra		check_firmware_new4				; is not equal -> reset CF and store new version in EEPROM
+	bra		check_firmware_new5				; x and y are equal -> do not reset CF
 
-check_firmware_new:
-	call	TFT_boot						; Initialize TFT (includes clear screen)
-	clrf	CCPR1L							; Backlight off
-	WIN_TOP  .50
-	WIN_LEFT .10
-	movlw	LOW   0x1E000
-	movwf	TBLPTRL
-	movlw	HIGH  0x1E000
-	movwf	TBLPTRH
-	movlw	UPPER 0x1E000
-	movwf	TBLPTRU
-	call	color_image						; Show logo
-	call	TFT_standard_color
-	WIN_SMALL .10,.100
-	STRCPY_PRINT "Update successful!"		; Hard coded since language switch does not work here
-	WIN_SMALL .10,.140
-	STRCPY	"New Firmware: "
-	movlw	softwareversion_x
-	movwf	lo
-	bsf		leftbind
-	output_8
-	PUTC	"."
-	movlw	softwareversion_y
-	movwf	lo
-	output_99x
-	bcf		leftbind
-	STRCAT_PRINT ""							; Print second row
-	call	TFT_Display_FadeIn				; Display resulting surface screen.
-
-
+check_firmware_new4:
 	; place "after-update reset" here...
 	lfsr	FSR0,oPressureAdjust
-	call	option_reset					; Reset FSR0 option to factory default.
+	call	option_reset					; reset oPressureAdjust to factory default
 	lfsr	FSR0,oPressureAdjust
-	call	option_save						; Save in EEPROM
+	call	option_save						; save new value of oPressureAdjust in EEPROM
 
+check_firmware_new5:
 	rcall	backup_flash_page				; backup the first 128 bytes from flash to EEPROM
 
 	movlw	d'1'							; store current version in EEPROM
@@ -252,35 +257,34 @@
 	movwf	EEADRH							; =1
 	movlw	softwareversion_x
 	movwf	EEDATA
-	call	write_eeprom					; write version x
+	call	write_eeprom					; write version, major number
 	incf	EEADR,F							; set to 0x102
 	movlw	softwareversion_y
 	movwf	EEDATA
-	call	write_eeprom					; write version y
-	clrf	EEADRH							; Reset EEADRH
+	call	write_eeprom					; write version, minor number
+	clrf	EEADRH							; reset EEADRH
 
-	movlw	.7
-	movwf	lo
-check_firmware_new2:
-	; Wait 1 second
-	bcf		onesecupdate
-	btfss	onesecupdate
-	bra		$-2
-	decfsz	lo,F							; Wait 10 seconds...
-	bra		check_firmware_new2
+	; wait 10 seconds
+	movlw	.10								; load loop counter
+check_firmware_new6:
+	call	wait_1s							; wait (about) 1 second
+	decfsz	WREG,W							; YES - decrement loop counter, did it became zero?
+	bra		check_firmware_new6				;       NO  - loop
+	;bra	restart							;       YES - proceed with restart
+
 
 	global	restart
 restart:
-	clrf	STKPTR							; Never return from here
+	clrf	STKPTR							; never return from here
 	clrf	CCP1CON							; stop PWM
-	bcf		PORTC,2							; Pull PWM out to GND
+	bcf		PORTC,2							; pull PWM out to GND
 
-	btfsc	menubit							; Return from Menu/COMM mode or timeout?
-	call	option_save_all					; Yes, save all settings into EEPROM
+	btfsc	menubit							; return from Menu/COMM mode or timeout?
+	call	option_save_all					; YES - save all settings into EEPROM
 
-	call	option_restore_all				; Restore everything from EEPROM into RAM
-	call	option_check_all				; Check all options (and reset if not within their min/max boundaries)
-	call	option_save_all					; Save all settings into EEPROM after they have been checked
+	call	option_restore_all				; restore everything from EEPROM into RAM
+	call	option_check_all				; check all options (and reset if not within their min/max boundaries)
+	call	option_save_all					; save all settings into EEPROM after they have been checked
 
 	clrf	flag1							; clear all flags
 	clrf	flag2
@@ -292,73 +296,102 @@
 	clrf	flag8
 	clrf	flag9
 	clrf	flag10
-	; Do not clear flag11 (Sensor calibration and charger status)
+	; do not clear flag11 (sensor calibration and charger status)
 	clrf	flag12
-	; Do not clear flag13 (Important hardware flags)
+	; do not clear flag13 (important hardware flags)
 	clrf	flag14
-	clrf	hardware_flag					; hardware descriptor flag
+	clrf	flag15
+	clrf	flag16
+	clrf	flag17
+
+	clrf	cvt_flags
+
+	clrf	tft_update_flags+0
+	clrf	tft_update_flags+1
+	clrf	tft_update_flags+2
+
+	clrf	hardware_flag1					; hardware descriptor 1
+	;		hardware_flag2					; hardware descriptor 2 - do not clear here!
 
 	bsf		tft_is_dimming					; TFT is dimming up (soon), ignore ambient sensor!
 
-	; configure hardware_flag byte
-	bsf		ambient_sensor					; Set flag
-	bsf		optical_input					; Set flag
+	; configure hardware descriptor 1
+	bcf		tft_power						; inverted, here needed for I2C_probe_OSTC_rx, to wake-up RX circuity
+	bsf		ambient_sensor					; set flag
+	bsf		optical_input					; set flag
 
-	call	lt2942_get_status				; Check for gauge IC
+	call	lt2942_get_status				; check for gauge IC
 	btfss	battery_gauge_available			; cR/2 hardware?
-	bra		restart2						; No
-
-	call	lt2942_init						; Yes, init battery gauge IC
-	bcf		optical_input					; Clear flag
+	bra		restart2						; NO
+	call	lt2942_init						; YES - initialize battery gauge IC
+	bcf		optical_input					; clear flag
 
 	banksel	0xF16
-	bcf		ANCON0,7						; AN7 Digital input
-	banksel common
-	bcf		lightsen_power					; Power-down ambient light sensor
-	bcf		ambient_sensor					; Clear flag
+	bcf		ANCON0,7						; AN7 digital input
+	banksel	common
+	bcf		lightsen_power					; power-down ambient light sensor
+	bcf		ambient_sensor					; clear flag
 	nop
-	btfss	PORTF,2							; Light sensor available?
-	bsf		ambient_sensor					; Yes.
+	btfss	PORTF,2							; light sensor available?
+	bsf		ambient_sensor					; YES
 	banksel	0xF16
-	bsf		ANCON0,7						; AN7 Analog again
+	bsf		ANCON0,7						; AN7 analog again
 	banksel	common
-	bsf		lightsen_power					; Power-up ambient light sensor again
+	bsf		lightsen_power					; power-up ambient light sensor again
 
 restart2:
 	btfsc	vusb_in
 	bra		restart3						; USB (and powered on)
-	bcf		PORTE,0							; Start comms
+	bcf		PORTE,0							; start comms
 	WAITMS	d'1'
 	btfss	vusb_in
 	bra		restart3						; USB (and powered off)
-	bsf		ble_available					; ble available
+	bsf		ble_available					; BLE available
 
 restart3:
-	bsf		PORTE,0							; Stop comms
-	btfsc	ble_available					; ble available?
-	bra		restart4						; Yes, can't be a cR
-	btfss	battery_gauge_available			; Rechargeable
-	bra		restart4						; No, can't be a cR
-	bsf		analog_o2_input					; Set flag for analog
+	bsf		PORTE,0							; stop comms
+	btfsc	ble_available					; BLE available?
+	bra		restart4						; YES - can't be a cR
+	btfss	battery_gauge_available			; rechargeable?
+	bra		restart4						; NO  - can't be a cR
+	bsf		analog_o2_input					; set flag for analog
 
-	; The hardware_flag is now:
+restart4:
+ IFDEF _rx_functions
+	WAITMS	d'200'
+	call	I2C_probe_OSTC_rx				; set ostc_rx_present flag if this is an OSTC TR model
+
+	; The hardware descriptor is now:
 	; 0x11: 2 with BLE
-	; 0x13: 2 with BLE & ambient
+	; 0x13: +/2 with BLE & ambient
 	; 0x05: cR
 	; 0x0A: 3
 	; 0x1A: 3 with BLE
+	; 0x33: 2 TR
 
-restart4:
-	; Select high altitude (Fly) mode?
+	btfss	ostc_rx_present					; OSTC TR detected?
+	bra		restart5						; NO
+	movff	opt_TR_mode,WREG				; YES - get user-selected TR mode
+	tstfsz	WREG							;       TR functions switched on?
+	bsf		FLAG_tr_enabled					;       YES - switch on displays and calculation functions
+ ENDIF
+
+	; configure hardware descriptor 2
+	; flag  screen_type                  will be configured on each call of TFT_boot
+	; flags compass_type & compass_type2 will be configured on each call of I2C_init_compass
+	; flag  analog_switches              will be configured directly after hard start (in start:)
+
+restart5:
+	; Select high altitude (fly) mode?
 	movff	last_surfpressure_30min+0,sub_b+0
 	movff	last_surfpressure_30min+1,sub_b+1
 	movlw	HIGH high_altitude_threshold
 	movwf	sub_a+1
-	movlw	LOW  high_altitude_threshold	; hard-wired 880mbar
+	movlw	LOW  high_altitude_threshold	; hard-wired 880 hPa
 	movwf	sub_a+0
 	call	subU16							; sub_c = sub_a - sub_b
-	btfss	neg_flag						; result negative (ambient > 880mbar)?
-	bsf		high_altitude_mode				; NO - set flag!
+	btfss	neg_flag						; result negative (ambient > 880 hPa)?
+	bsf		high_altitude_mode				; NO - set flag
 
 	btfss	analog_o2_input
 	bsf		TRISB,3
@@ -366,25 +399,25 @@
 	bsf		TRISG,0
 	call	ext_flash_disable_protection	; disable write protection for external flash
 
-	bsf		flip_screen						; flip 180°
-	TSTOSS	opt_flip_screen					; =1: Flip the screen
-	bcf		flip_screen						; normal orientation
+	bsf		flip_screen						; select screen flip 180°
+	TSTOSS	opt_flip_screen					; shall actually flip? (=1: flip the screen)
+	bcf		flip_screen						; NO - revert to normal orientation
 
 	btfsc	use_old_batt_flag				; =1: load old battery information after power-on reset
-	goto	use_old_batteries				; returns to "surfloop"!
+	goto	use_old_batteries				; returns to surface loop
 
 	btfsc	RCON,POR						; was this a power-on reset?
-	goto	surfloop						; jump to surface loop!
+	goto	surfloop						; YES - jump to surface loop
 	bsf		RCON,POR						; set bit for next detection
 	; Things to do after a power-on reset
-	goto	do_new_battery_select			; returns to "surfloop"!
+	goto	do_new_battery_select			; returns to surface loop
 
 ;=============================================================================
 ; Setup all flags and parameters for divemode and simulator computations.
 ;
 	global	restart_set_modes_and_flags
-restart_set_modes_and_flags:				; "Call"ed from divemode, as well!
-	call	option_restore_all				; Restore everything from EEPROM
+restart_set_modes_and_flags:				; "Call"ed from dive mode as well
+	call	option_restore_all				; restore everything from EEPROM
 
 	; Setup sampling rate
 	movlw	.2
@@ -395,13 +428,31 @@
 	movwf	samplingrate
 
 restart_set_modes_and_flags1:
-	bcf		twosecupdate					; to have divemode routines in sync
+	bcf		FLAG_gauge_mode
 	bcf		FLAG_apnoe_mode
-	bcf		FLAG_ccr_mode					; =1: CCR mode (Fixed ppO2 or Sensor) active
-	bcf		FLAG_gauge_mode					; =1: In Gauge mode
+	bcf		FLAG_oc_mode
+	bcf		FLAG_ccr_mode
 	bcf		FLAG_pscr_mode
+	bcf		FLAG_bailout_mode
 	call	disable_ir_s8					; IR off
 
+ IFDEF _cave_mode
+	bsf		FLAG_cave_mode					; enable cave mode by default
+	movff	opt_calc_asc_gasvolume,WREG		; get gas needs calculation mode (0=off, 1=on, 2=cave mode)
+	xorlw	.2								; coding for cave mode
+	tstfsz	WREG							; cave mode enabled?
+	bcf		FLAG_cave_mode					; NO - disable cave mode again
+	bcf		FLAG_cave_mode_shutdown			; clear flag for cave mode shutdown
+	bcf		FLAG_dive_turned				; clear flag for dive turned
+	bcf		gas_needs_mode_last				; set last gas calculation results as direct ascent needs
+ ENDIF
+
+	; Initialize active_gas and active_dil for surface mode pressure display
+	call	get_first_gas_to_WREG
+	movwf	active_gas
+	call	get_first_dil_to_WREG
+	movwf	active_dil
+
 	; Setup char_I_saturation_multiplier and char_I_desaturation_multiplier
 	movff	opt_sat_multiplier_gf,char_I_saturation_multiplier
 	movff	opt_desat_multiplier_gf,char_I_desaturation_multiplier
@@ -417,30 +468,23 @@
 	bra		restart_set_modes_and_flags2
 
 	; OC Mode
+	bsf		FLAG_oc_mode					; =1: OC mode active
+ IFDEF _rx_functions
+	call	option_cleanup_oTrMode_no_CCR	; revert TR mode from 'CCR Dil+O2' to 'on'
+ ENDIF
 	return
 
 restart_set_modes_and_flags2:
 	decfsz	lo,F
 	bra		restart_set_modes_and_flags3
 
-	; CC Mode
-	btfsc	analog_o2_input					; cR?
-	bra		restart_set_modes_and_flags2b	; Yes, skip mode check
-	btfsc	optical_input					; 3
-	bra		restart_set_modes_and_flags2b	; Yes, skip mode check
-
-	; Make sure Sensor is not selected
-	; opt_ccr_mode must be <> 1 (=0: Fixed SP, =1: Sensor, =2: Auto SP (CCR only))
-	banksel	opt_ccr_mode
-	movlw	.1
-	cpfseq	opt_ccr_mode					; = Sensor?
-	bra		restart_set_modes_and_flags2b	; No
-	clrf	opt_ccr_mode					; Yes, reset to Fixed SP
-
-restart_set_modes_and_flags2b:
-	banksel	common
+	; CCR Mode
 	bsf		FLAG_ccr_mode					; =1: CCR mode (Fixed SP, Auto SP or Sensor) active
-	call	enable_ir_s8					; Enable IR/S8-Port
+	call	option_cleanup_oCCRMode_CCR		; revert CCR mode 'Sensor' to 'fixed SP' if no sensor interface available
+ IFDEF _rx_functions
+	call	option_cleanup_oTrMode_CCR		; revert TR mode from 'ind.double' to 'on'
+ ENDIF
+	call	enable_ir_s8					; enable IR/S8 port
 	return
 
 restart_set_modes_and_flags3:
@@ -448,41 +492,35 @@
 	bra		restart_set_modes_and_flags4
 
 	; Gauge Mode
-	bsf		FLAG_gauge_mode					; =1: In Gauge mode
+	bsf		FLAG_gauge_mode					; =1: in gauge mode
+ IFDEF _rx_functions
+	call	option_cleanup_oTrMode_no_CCR	; revert TR mode from 'CCR Dil+O2' to 'on'
+ ENDIF
 	return
 
 restart_set_modes_and_flags4:
 	decfsz	lo,F
 	bra		restart_set_modes_and_flags5
+
 	; Apnea Mode
-	bsf		FLAG_apnoe_mode
-	return									; start in Surfacemode
+	bsf		FLAG_apnoe_mode					; =1: in Apnea mode
+ IFDEF _rx_functions
+	call	option_cleanup_oTrMode_no_CCR	; revert TR mode from 'CCR Dil+O2' to 'on'
+ ENDIF
+	return									; start in surface mode
 
 restart_set_modes_and_flags5:
-	; PSCR Mode
-	btfsc	analog_o2_input					; cR?
-	bra		restart_set_modes_and_flags5b	; Yes, skip mode check
-	btfsc	optical_input					; 3
-	bra		restart_set_modes_and_flags5b	; Yes, skip mode check
-
-	; Make sure Sensor is not selected
-	; opt_ccr_mode must be <> 1 (=0: calculated ppO2, =1: Sensor, =2: Auto SP - not valid in pSCR, too)
-	banksel	opt_ccr_mode
-	movlw	.1
-	cpfseq	opt_ccr_mode					; = Sensor?
-	bra		restart_set_modes_and_flags5b	; No
-	clrf	opt_ccr_mode					; Yes, reset to calculated ppO2
-
-restart_set_modes_and_flags5b:
-	banksel	opt_ccr_mode
-	bcf		opt_ccr_mode,1					; in pSCR mode only calculated or Sensor, not 2 = Auto SP allowed
-	banksel	common
-	bsf		FLAG_pscr_mode
-	call	enable_ir_s8					; Enable IR/S8-Port
-	return									; start in Surfacemode
+	; pSCR Mode
+	bsf		FLAG_pscr_mode					; set pSCR mode flag
+	call	option_cleanup_oCCRMode_pSCR	; in pSCR mode, revert AutoSP (2) to calculated SP (0), additionally revert Sensor to fixed SP if no sensor interface available
+ IFDEF _rx_functions
+	call	option_cleanup_oTrMode_no_CCR	; revert TR mode from 'CCR Dil+O2' to 'on'
+ ENDIF
+	call	enable_ir_s8					; enable IR/S8 port
+	return									; start in surface mode
 
 
-; backup the first 128bytes from flash to EEPROM
+; backup the first 128 bytes from flash to EEPROM
 backup_flash_page:
 	; Start address in internal flash
 	movlw	0x00
@@ -491,20 +529,20 @@
 	movwf	TBLPTRU
 
 	movlw	.128
-	movwf	lo								; Byte counter
+	movwf	lo								; byte counter
 	clrf	EEADR
 	movlw	.3
-	movwf	EEADRH							; Setup backup address
+	movwf	EEADRH							; setup backup address
 
-	TBLRD*-									; Dummy read to be in 128 byte block
+	TBLRD*-									; dummy read to be in 128 byte block
 backup_flash_loop:
-	tblrd+*									; Table Read with Pre-Increment
+	tblrd+*									; table read with pre-increment
 	movff	TABLAT,EEDATA					; put 1 byte
 	call	write_eeprom					; save it in EEPROM
 	incf	EEADR,F
-	decfsz	lo,F							; 128byte done?
-	bra		backup_flash_loop				; No
-	clrf	EEADRH							; Reset EEADRH
-	return									; Done.
+	decfsz	lo,F							; 128 byte done?
+	bra		backup_flash_loop				; NO - loop
+	clrf	EEADRH							; reset EEADRH
+	return									; done
 
 	END
\ No newline at end of file