; copy scratchpad
; master
; 0x55 + 0xa5 => scratchpad(000-0x1f) --> EEPROM(0x00-0x1F)
; held hi for 10ms
; 0xf0 + reset pulse => copy whole from EEPROM to SCRATCHPAD
; use timer as a delay timer
;
; author : Rex Chen
; date : May,8th
; ver : 1.81
;
;; application register , application register is OTP, there are 7 bytes
;; pay attention when you use application register , it's a 64 bit OTP
WApR equ H'99' ; 0x99 + Addr + data + data .. + reset pulse
; [Master --> Application Reg]
RStatus equ H'66' ; 0x66 + 0x00 + status + reset pulse
RApR equ H'C3' ; 0xc3 + addr + data + .. + reset pulse
; application register --> master ( locked)
; register scratchpad --> master ( not lock)
LockAp equ H'5A' ; 0x5A + 0xa5
; [ register scratchpad --> application register]
; 0x5A + reset pulse cancel copy command
; a OTP type do once only
AppLocked equ B'11111100' ; application locked, the value is data in the status
;; ROM function command// implement skip rom only
SkipROM equ H'CC' ;access memory directly
SerachROM equ H'f0' ;read a bit , read the complement of the bit,write bit
MatchROM equ H'55' ;0x55 + 64 bit rom bit
ReadROM equ H'33' ;[family code & serial number & CRC ==> master ]
exit_int_source_check ;; restore STATUS & W before exit from
swapf STATUS_SAVE,W ;; W=STATUS_SAVE=STATUS[3:0]& STATUS[7:4]
movwf STATUS
swapf W_SAVE,F ;; restore W
swapf W_SAVE,W
retfie
;; ISR &&&
main
goto $
;----------------------------------------------
; template to init before write
; to DS2430
;-----------------------------------------------
; movlw H'00'
; movwf Addr
; movlw H'3C'
; movwf TxBuffer
;---------------------------------------------
; write DS2430 one byte
; input
; Addr = starting address to write
; TxB = data to save
; output =
; ErrCode = 0 -> write ok
; = 1 -> write fail
;-----------------------------------------------
WrDS2430
call WrScratchPAD
call CP2EE
return
;------------------------------------------------
; template to init before read
; to DS2430
;-------------------------------------------------
; movlw H'00'
; movwf Addr
;-------------------------------------------------
; read DS2430 one byte
; input
; Addr = starting address to read
; output
; RxB = data to read
;--------------------------------------------------
RdDS2430
call REE2ScratchPAD
call HostRdScratchPAD
return
;;--------- host read scratch pad ----------------------------
;; save desired data into RxB
;;
;;--------------------------------------------------------------
call R2430A ; read desired scratch pad into RxB
call RstPulse
return
;-------------- write memory scratch scratch pad ------------------------
; write one byte into scratch PAD
; input
; Addr = starting address to write
; TxBuffer = data to save
; output
; ErrCode = 0 -> write ok
; = 1 -> write fail
;---------------------------------------------------------------------------
;---------- CP2EE ---copy whole scratch pad to EE ----------------------
; write memory scratch
; verify memory scratch
; copy memory scratch to EE
;----------------------------------------------------------------------
CP2EE
call DS2430AInitState ; 0x55 + 0xa5 + hi 10ms // [ScratchPad -> EEPROM ]
banksel TxB
movlw SkipROM
movwf TxB
call TxBYTE ; SkipROM
movlw CPScratch ; 0x55
movwf TxB
call TxBYTE
movlw H'a5'
movwf TxB
call TxBYTE
call ForceHi ; min 10ms refter to datasheet
return
;--------- REE2ScratchPAD --- copy whole EE to ScrathPAD ----------------
; copy EE to whole memory scatch
; read memory scratch
;format I: 0xf0 + Addr +R +R + reset pulse, copy byte by byte
;format II: 0xf0 + Addr + reset pulse, copy whole
;------------------------------------------------------------------------
REE2ScratchPAD
call DS2430AInitState
banksel TxB
movlw SkipROM
movwf TxB
call TxBYTE ; SkipROM
movlw REEROM ;; Read from EE into ScratchPAD
movwf TxB
call TxBYTE ;;
call RstPulse ;; copy all
return
;----------R2430A ---- Host read the scratch PAD ------------------------
; Read 2430A Scratch PAD
; LSB first
; save the result in RxB
;------------------------------------------------------------------------
R2430A ; returns byte in RxB
movlw .8
movwf LoopCounter
clrf RxB
RLoop
call SetLo ;
nop
call SetHi ; it will pull up by resistor
nop ; delay to sample reliable bit
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
goto gLo
bsf CARRY
goto rol
gLo
bcf CARRY
rol
banksel RxB
rrf RxB,F
call delay60us ; tdrv + trelease
banksel LoopCounter
decfsz LoopCounter,F
goto RLoop
movf RxB,W ; save the result in W
return
;----------------------------------------------------------
; strong pull up
;
;----------------------------------------------------------
ForceHi
init
movlw H'3f'
movwf PBDir ; 0x00111111
banksel TRISB
movwf TRISB ; force all portC as input
; movlw b'00000000' ; GP2 pull-up,GP4 output
; movwf WPU ; connect the D of DS2430A to Port A
;--------------------------------------------------------------------
; toggle from bank0 to bank1
; option register
; 7: 1: port A pull-up OFF, 0:port A pull-up ON
; 6: 1: rising INT , 0:fall INT
; 5: 1: T0CKI to TMR0, 0: internal cycle to TMR0
; 4: 1: Hi to Lo count 0: Lo to Hi count
; 3: 1: Precale to WDT 0: Prescale to TMR0
; 2:0 prescale value
;---------------------------------------------------------------------
movlw b'11000010' ;; pull-up ON,rising INT, internal cycle to TMR0,Lo to Hi count,Pre to TMR0,1:8
movwf OPTION_REG ;; ,prescal over 8
banksel INTCON
clrf INTCON
clrf PORTB
movlw 0x07 ;; comparator off
movwf CMCON ;; comparator off
clrf TMR0
;;--------------------------------------------------------
;; 7: X
;; 6: 1: Timer1 on if T1G\
;; 5:4 : 11:/8,10:/4,01:/2,00:/1
;; 3 : 1 LP for timer 1 on, 0: LP for timer1 of
;; 2 : 1 do not sync external , 0 sync external clock
;; 1 : 1: clock source external , 0: clock source internal
;; 0 : 1: enable timer 1 ,0: disable timer 1
;;---------------------------------------------------------
;;---------------------------------------------------------
TxLo:
call SetLo
call delay60us ; min 60 us
call SetHi
nop ; recovery time min 1us
nop
return
;-----------------------------------------------------------
; tx hi
;-----------------------------------------------------------
TxHi:
call SetLo ;1us<tlow1<15us
nop
call SetHi
call delay60us
nop
nop ;recovery time min 1us
return
;;---------------------------------------------------------------------------
;; toggle the RC0 as input and the bus was pulled up by externl resistor
;; application default PCDir.Bit0 to output
;;---------------------------------------------------------------------------
SetHi:
bsf PBDir,Bit2
movf PBDir,w
; bsf bank1
banksel TRISB
movwf TRISB
return
;;----------------------------------------------------------------------------
;; clear register file RC0
;;----------------------------------------------------------------------------
SetLo:
banksel PORTB
bcf PORTB,Bit2 ; data lo
bcf PBDir,Bit2 ; direction out
movf PBDir,W
banksel TRISB
movwf TRISB
return