view src/eeprom_rs232.asm @ 592:05053910d668

BUGFIX: Re-enable Sensors after sleep in PSCR mode
author heinrichsweikamp
date Wed, 18 Apr 2018 17:03:52 +0200
parents b455b31ce022
children ca4556fb60b9
line wrap: on
line source

;=============================================================================
;
;   File eeprom_rs232.asm												V2.98
;
;   Internal EEPROM, RS232
;
;   Copyright (c) 2011, JD Gascuel, HeinrichsWeikamp, all right reserved.
;=============================================================================
; HISTORY
;  2011-08-06 : [mH] moving from OSTC code

#include "hwos.inc"
#include "wait.inc"
#include "shared_definitions.h"
#include "rtc.inc"

write_int_eeprom	macro	eeprom_address
	movlw	eeprom_address
	call	write_int_eeprom_1
	endm

read_int_eeprom		macro	eeprom_address
	movlw	eeprom_address
	call	read_int_eeprom_1
	endm

;=============================================================================
eeprom	code	0xF00000+0x10
; Skip SERIAL number. Should not be overwritten.

	global	eeprom_serial_save
	global	eeprom_opt_backup

eeprom_serial_save	res 2
eeprom_opt_backup	res 0x3E

;=============================================================================
basic	CODE

	global	write_int_eeprom_1
write_int_eeprom_1:
	movwf	EEADR
;	bra		write_eeprom				; writes and "returns" after write
	
;=============================================================================
; writes into internal eeprom
; Input:   EEADRH:EEADR = EEPROM address.
;          EEDATA = byte to write.
; Trashed: WREG.
	global	write_eeprom
write_eeprom:
	bcf	EECON1,EEPGD
	bcf	EECON1,CFGS
	bsf	EECON1,WREN

	bcf	INTCON,GIE					; Disable interrupts for the next 5 instructions
	movlw	0x55
	movwf	EECON2
	movlw	0xAA
	movwf	EECON2
	bsf	EECON1,WR
	bsf	INTCON,GIE					; ...but the flag for the ISR routines were still set, so they will interrupt now!

write_eep2:
	btfsc	EECON1,WR
	bra	write_eep2					; wait about 4ms...
	bcf	EECON1,WREN
	return



	global read_int_eeprom_1
read_int_eeprom_1:
	movwf	EEADR
;	bra		read_eeprom					; reads and "returns" after write

;=============================================================================
; reads from internal eeprom
; Input:   EEADRH:EEADR = EEPROM address.
; Output:  EEDATA.
; Trashed: NONE.
	global	read_eeprom
read_eeprom:
	bcf		EECON1,EEPGD
	bcf		EECON1,CFGS
	bsf		EECON1,RD
	return

	global	disable_ir_s8
disable_ir_s8:
	banksel	TXSTA2
	clrf	TXSTA2
	clrf	RCSTA2
	banksel	common
	bcf		ir_power					; IR off
	bcf		mcp_power					; Power-down instrumentation amp
	bsf		s8_npower					; Power-down S8 HUD
	return


	global	enable_ir_s8
enable_ir_s8:
;init serial port2 (TRISG2)
	btfsc	analog_o2_input
	bra		enable_s8					; Start S8

	banksel	BAUDCON2
	movlw	b'00100000'					; BRG16=0			; inverted for IR
	movwf	BAUDCON2
	movlw	b'00100000'					; BRGH=0, SYNC=0
	movwf	TXSTA2
	movlw	.102						; SPBRGH:SPBRG = .102 : 2403 BAUD @ 16MHz
	movwf	SPBRG2
	movlw	b'10010000'
	movwf	RCSTA2
	banksel	common
	bsf		ir_power					; Power-up IR
	btfss	ir_power
	bra		$-6
	return

enable_s8:
	; Check for Digital/Analog
	bsf		s8_npower					; Power-down S8 HUD
	WAITMS	d'1'						; Very short delay
	bsf		mcp_power					; Power-up instrumentation amp
	btfss	mcp_power
	bra		$-6
	banksel	TXSTA2
	clrf	TXSTA2
	clrf	RCSTA2
	banksel	common

	; It may be digital, check for voltage when isolator is powered
	bcf		s8_npower					; Power S8 HUD
	WAITMS	d'1'						; Very short delay

	btfsc	PORTG,2						; RX2=1?
	bra	enable_s8_2						; Yes, digital
	WAITMS	d'30'
	btfsc	PORTG,2						; RX2=1?
	bra		enable_s8_2					; Yes, digital

	; Not found, set to analog (fail-safe)

enable_s8_analog:
	; S8 Analog
	bsf		s8_npower					; Power-down S8 HUD
	bcf		s8_digital					; Clear flag
	return

enable_s8_2:							; S8 Digital
	banksel BAUDCON2
	movlw	b'00000000'					; BRG16=0			; normal for S8
	movwf	BAUDCON2
	movlw	b'00100000'					; BRGH=0, SYNC=0
	movwf	TXSTA2
	movlw	.25							; SPBRGH:SPBRG = .25 : 9615 BAUD @ 16MHz
	movwf	SPBRG2
	movlw	b'10010000'
	movwf	RCSTA2
	banksel	common
	bsf		s8_digital					; Set flag
	return

;=============================================================================

	global	enable_rs232
enable_rs232:
	call	speed_normal				; 16MHz
enable_rs232_2:
	movlw	T2CON_NORMAL
	cpfseq	T2CON
	bra		enable_rs232_2				; Wait until speed is normal
	bcf		PORTE,0						; Start comms
;init serial port1 (TRISC6/7)
	movlw	b'00100100'					; BRGH=1, SYNC=0
	movwf	TXSTA1
	movlw	b'10010000'
	movwf	RCSTA1
	return


	global	disable_rs232
disable_rs232:
	clrf	RCSTA1
	clrf	TXSTA1						; UART disable
	bcf		PORTC,6						; TX hard to GND
	bsf		PORTE,0						; Stop comms
	return


	global	rs232_wait_tx
rs232_wait_tx:
	btfss	TXSTA1,TRMT					; RS232 Busy?
	bra		rs232_wait_tx				; yes, wait...

	btfss	ble_available				; ble available?
	return								; No, done.

	btfsc	NRTS						; Wait for Bluetooth module
	bra		rs232_wait_tx				; yes, wait...
	return								; Done.


	global	rs232_wait_tx2
rs232_wait_tx2:
	banksel	TXSTA2
rs232_wait_tx2_1:
	btfss	TXSTA2,TRMT					; RS232 Busy?
	bra		rs232_wait_tx2_1			; yes, wait...
	banksel	common
	return								; Done.

	global	rs232_get_byte
rs232_get_byte:
	bcf		rs232_receive_overflow		; clear flag
	movlw	.10
	movwf	uart1_temp
	clrf	uart2_temp
	clrf	uart3_temp
rs232_get_byte2:
	btfsc	PIR1,RCIF					; data arrived?
	return								; Yes
	decfsz	uart3_temp,F
	bra		rs232_get_byte2
	decfsz	uart2_temp,F
	bra		rs232_get_byte2
	decfsz	uart1_temp,F
	bra		rs232_get_byte2
										; timeout occurred (about 400ms)
	bsf		rs232_receive_overflow		; set flag
rs232_get_byte3:
	bcf		RCSTA1,CREN					; Clear receiver status
	bsf		RCSTA1,CREN
	return								; and return anyway

;=============================================================================

	global	do_logoffset_common_write
do_logoffset_common_write:
	; TODO: limit [lo:hi] to 9999 ? may arrive here as >= 10000 because of +10 increment option 
	; SIMPLER WAY: return without writing to eeprom if hi == 0x27 -> 0x2700 = 9984, this is within 
	; the save range for a +10 increment, shall do it...
	; mH

	movff	lo,EEDATA
	write_int_eeprom 0x0D
	movff	hi,EEDATA
	write_int_eeprom 0x0E
	return

	global	do_logoffset_common_read
do_logoffset_common_read:
	clrf	EEADRH
	read_int_eeprom 0x0D
	movff	EEDATA,lo
	read_int_eeprom 0x0E
	movff	EEDATA,hi						; Existing logbook offset into lo:hi
	return


	global	update_battery_registers
update_battery_registers:
	; save battery_gauge:6 into EEPROM 0x07-0x0C
	clrf	EEADRH
	movff	battery_gauge+0,EEDATA
	write_int_eeprom 0x07
	movff	battery_gauge+1,EEDATA
	write_int_eeprom 0x08
	movff	battery_gauge+2,EEDATA
	write_int_eeprom 0x09
	movff	battery_gauge+3,EEDATA
	write_int_eeprom 0x0A
	movff	battery_gauge+4,EEDATA
	write_int_eeprom 0x0B
	movff	battery_gauge+5,EEDATA
	write_int_eeprom 0x0C
	movff	battery_type,EEDATA				; =0:1.5V, =1:3,6V Saft, =2:LiIon 3,7V/0.8Ah, =3:LiIon 3,7V/3.1Ah, =4: LiIon 3,7V/2.3Ah
	write_int_eeprom 0x0F
	return


	global	vault_decodata_into_eeprom
vault_decodata_into_eeprom:
	; Vault in EEPROM 512...1023
	; Write 0xAA at 512 to indicate valid data in vault
	; Store last time/date
	; Store 0x700 to 0x780 (pres_tissue_N2 and pres_tissue_He)
	movlw	HIGH .512						; =2
	movwf	EEADRH
	movlw	0xAA
	movwf	EEDATA
	write_int_eeprom .0
	; Store date/time
	movff	year,EEDATA
	write_int_eeprom .1
	movff	month,EEDATA
	write_int_eeprom .2
	movff	day,EEDATA
	write_int_eeprom .3
	movff	hours,EEDATA
	write_int_eeprom .4
	movff	mins,EEDATA
	write_int_eeprom .5
	movff	secs,EEDATA
	write_int_eeprom .6

	movff	int_O_CNS_fraction+0,EEDATA
	write_int_eeprom .7
	movff	int_O_CNS_fraction+1,EEDATA
	write_int_eeprom .8
	movff	int_O_desaturation_time+0,EEDATA
	write_int_eeprom .9
	movff	int_O_desaturation_time+1,EEDATA
	write_int_eeprom .10
	movff	surface_interval+0,EEDATA
	write_int_eeprom .11
	movff	surface_interval+1,EEDATA
	write_int_eeprom .12
	movff	int_O_gradient_factor+0,EEDATA	; value limited to 255, only lower byte in use for value
	write_int_eeprom .13
	movff	int_O_nofly_time+0,EEDATA
	write_int_eeprom .14
	movff	int_O_nofly_time+1,EEDATA
	write_int_eeprom .15

	; Tissue data from 16 to 144
	movlw	.16
	movwf	EEADR
	movlw	.128
	movwf	lo
	lfsr	FSR1,0x700						; pres_tissue_N2+0, 32*4Byte Float = 128Bytes
vault_decodata_into_eeprom2:
	movff	POSTINC1,EEDATA
	call	write_eeprom					; EEDATA into EEPROM@EEADR
	incf	EEADR,F
	decfsz	lo,F							; All done?
	bra		vault_decodata_into_eeprom2		; No
	clrf	EEADRH
	return

	global	restore_decodata_from_eeprom
restore_decodata_from_eeprom:
	movlw	LOW  .512						; =0
	movwf	EEADR
	movlw	HIGH .512						; =2
	movwf	EEADRH

	; Restore date/time
	read_int_eeprom .1
	movff	EEDATA,year
	read_int_eeprom .2
	movff	EEDATA,month
	read_int_eeprom .3
	movff	EEDATA,day
	read_int_eeprom .4
	movff	EEDATA,hours
	read_int_eeprom .5
	movff	EEDATA,mins
	read_int_eeprom .6
	movff	EEDATA,secs
	call	rtc_set_rtc

	read_int_eeprom .7
	movff	EEDATA,int_O_CNS_fraction+0
	read_int_eeprom .8
	movff	EEDATA,int_O_CNS_fraction+1
	read_int_eeprom .9
	movff	EEDATA,int_O_desaturation_time+0
	read_int_eeprom .10
	movff	EEDATA,int_O_desaturation_time+1
	read_int_eeprom .11
	movff	EEDATA,surface_interval+0
	read_int_eeprom .12
	movff	EEDATA,surface_interval+1
	read_int_eeprom .13
	movff	EEDATA,int_O_gradient_factor+0
	read_int_eeprom .14
	movff	EEDATA,int_O_nofly_time+0
	read_int_eeprom .15
	movff	EEDATA,int_O_nofly_time+1

	; Tissue data from 16 to 144
	movlw	.16
	movwf	EEADR
	movlw	.128
	movwf	lo
	lfsr	FSR1,0x700						; pres_tissue_N2+0, 32*4Byte Float = 128Bytes
restore_decodata_from_eeprom2:
	call	read_eeprom						; EEPROM@EEADR into EEDATA
	movff	EEDATA,POSTINC1
	incf	EEADR,F
	decfsz	lo,F							; All done?
	bra		restore_decodata_from_eeprom2	; No
	clrf	EEADRH
	return


	global	reset_battery_internal_only
reset_battery_internal_only:
	clrf	EEADRH
	clrf	EEDATA							; Delete to zero
	write_int_eeprom 0x07
	write_int_eeprom 0x08
	write_int_eeprom 0x09
	write_int_eeprom 0x0A
	write_int_eeprom 0x0B
	write_int_eeprom 0x0C
	banksel	battery_gauge+0
	clrf	battery_gauge+0
	clrf	battery_gauge+1
	clrf	battery_gauge+2
	clrf	battery_gauge+3
	clrf	battery_gauge+4
	clrf	battery_gauge+5
	banksel	common
	movlw	.100
	movwf	batt_percent
	return


	global	eeprom_reset_logbook_pointers
eeprom_reset_logbook_pointers:
	clrf	EEADRH							; Make sure to select EEPROM bank 0
	clrf	EEDATA
	write_int_eeprom .4
	write_int_eeprom .5
	write_int_eeprom .6
	write_int_eeprom .2						; Also, delete total dive counter
	write_int_eeprom .3
	write_int_eeprom .16
	write_int_eeprom .17					; And the backup counter, too
	return

	END