⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flash.asm

📁 Cypress 的VOIP DEMO 研究VOIP终端的朋友可以研究研究
💻 ASM
字号:
;------------------------------------------------------------------------------
;
;   FILENAME:   flash.asm
; 
;   DESCRIPTION:
;   Routines in this file perform flash block write and read operations.
;
;------------------------------------------------------------------------------
;   Copyright (c) Cypress MicroSystems 2003. All rights reserved.
;------------------------------------------------------------------------------

export    flash_write_lite
export   _flash_write_lite

export    flash_read_lite
export   _flash_read_lite


include  "m8c.inc"


;--------------------------------------------
;  Declare Flash RAM storage at top of RAM
;     This data area is reserved for exclusive
;     use by Supervisory operations.
;--------------------------------------------

    AREA SSCParamBlk(RAM,ABS)

    org  F8h

SSC_KEY1:                             ; F8h  supervisory key
SSC_RETURNCODE:        blk      1     ; F8h  result code
SSC_KEY2 :             blk      1     ; F9h  supervisory stack ptr key
SSC_BLOCKID:           blk      1     ; FAh  block ID
SSC_POINTER:                          ; FBh  pointer to data buffer
TEMP_M:                blk      1     ; FBh  M of Mx+B
SSC_CLOCK:                            ; FCh  Clock
TEMP_B:                blk      1     ; FCh  B of Mx+B
TEMP_Mult:             blk      1     ; FDh  ClockW ClockE multiplier
SSC_DELAY:             blk      1     ; FEh  flash macro sequence delay count
SSC_TEMP_Revision:
SSC_WRITE_ResultCode:  blk      1     ; FFh  temporary result code

TempE:                 equ      fbh
MultHB:                equ      fch
MultLB:                equ      fdh
ResultHB:              equ      feh
ResultLB:              equ      ffh


;--------------------------------
; SSC_Action macro command codes
;--------------------------------

FLASH_OPER_KEY:        equ      3Ah   ; flash operation key
FLASH_WRITE:           equ      2     ; flash write operation cmd
FLASH_ERASE:           equ      3     ; flash erase operation cmd
FLASH_TABLE_READ:      equ      6     ; flash read table command
TEMP_TABLE:            equ      3


;--------------------------------------------------------------
; Supervisory Operation Templates:
;     Each system supervisory call uses the reserved data area
;     a little different.  Create overlay templates to improve
;     readability and maintenance.
;--------------------------------------------------------------

;SSC Return Codes
SSC_SUCCESS:           equ      00h   ; success return code

;Flash Sequence Time Delay
SSC_DELAY_VALUE:       equ      56h   ; 100us delay at 12MHz

; Temperature Table Template - returned data after performing a
VALID_TABLE_REVISION:  equ      1     ; Temp Table revision number


;--------------------------------------------
;  Restore area to Code area
;--------------------------------------------

   AREA UserModules(ROM, REL)


;-----------------------------------------------------------------------------
;  MACRO NAME: special_SSC
;
;  DESCRIPTION:
;     Performs locally defined supervisory operations.
;
;     !!! DO NOT CHANGE THIS CODE !!!
;
;  RETURNS:
;     BYTE - Nothing
;
;  SIDE EFFECTS:
;     A and X registers are destroyed
;
;  PROCEDURE:
;     1) specify a 3 byte stack frame.  Save in [KEY2]
;     2) insert the flash Supervisory key in [KEY1]
;     3) store flash operation function code in A
;     4) call the supervisory code
;-----------------------------------------------------------------------------

macro special_SSC:
    mov   X, SP              ; copy SP into X
    mov   A, X               ; A temp stored in X
    add   A, 3               ; create 3 byte stack frame (2 + pushed A)
    mov   [SSC_KEY2], A      ; save stack frame for supervisory code

    ; load the supervisory code for flash operations
    mov   [SSC_KEY1], FLASH_OPER_KEY   

    mov   A,@0               ; load A with specific Flash operation
    SSC                      ; SSC call the supervisory code
endm


;-----------------------------------------------------------------------------
;  FUNCTION NAME: flash_write_lite
;
;  DESCRIPTION:
;     Writes 64 bytes of data to the flash at the specified blockId.
;
;     Regardless of the size of the buffer, this routine always writes 64
;     bytes of data. If the buffer is less than 64 bytes, then the next
;     64-N bytes of data will be written to fill the rest of flash block data.
;
;  Assumptions:
;     Single block Write
;     CPU clock is 12MHz
;     Temperature is defined to be 32 degees celcius
;
;  ARGUMENTS:
;     BYTE  - flash block ID
;     void* - source RAM location
;
;  RETURNS:
;     SSC_RETURNCODE
;
;  PROCEDURE:
;     1 ) Compute the pulsewidths
;     2 ) Erase the specified block
;     3 ) Program the specified block
;-----------------------------------------------------------------------------

; Stack frame
FLASHBLOCKLOC:    equ -4
DATA_LO:          equ -5
DATA_HI:          equ -6


    halt    ; Wandering code protection, flash_write_lite must be called.
   
 flash_write_lite:
_flash_write_lite:
    push  X

;Step 1- Calculate ClockE amd ClockW

    ;Get Cal values
    mov   [SSC_BLOCKID], TEMP_TABLE  
    special_SSC FLASH_TABLE_READ

    ;calc ClockE
    asr   [TEMP_M]                      ; M/4
    asr   [TEMP_M]
    mov   A, [TEMP_B]
    sub   A, [TEMP_M]                   ; B - M/4
    push  A                             ; ClockE on stack
     
    ;calc ClockW
    mov   [TempE], A
    mov   A, 0                          ; TempE contains ClockE
    mov   [ResultHB], A
    mov   [ResultLB], A
    mov   [MultHB], A

loop:
    asr   [TempE]
    jnc   endif1                        ; (carry is set)
    mov   A, [MultLB]                   ; add Mult to Result
    add   [ResultLB], A
    mov   A, [MultHB]
    adc   [ResultHB], A

endif1:
    cmp   [TempE],0
    jz    exitloop
    asl   [MultLB]
    rlc   [MultHB]
    jmp   loop
exitloop:   

    mov   A,[ResultHB]   
    asl   [ResultLB]
    rlc   A
    asl   [ResultLB]
    rlc   A
    push  A                              ; ClockW on Stack 

; Step 2 - Erase the specified flash block
    mov   X, SP
    mov   A, [X+FLASHBLOCKLOC-2]         ; get Block ID, 2 pushes since call
    mov   [SSC_BLOCKID], A
    mov   A, [X-2]                       ; get Clock E
    mov   [SSC_CLOCK], A
    mov   [SSC_DELAY], SSC_DELAY_VALUE   ; get Delay
    special_SSC FLASH_ERASE

; Step 3 - Program the flash
    pop   A                              ; ClockW
    mov   [SSC_CLOCK], A
    pop   A                              ; ClockE
    mov   X, SP
    mov   A, [X+FLASHBLOCKLOC]           ; get Block ID
    mov   [SSC_BLOCKID], A
    mov   A, [X+DATA_LO]                 ; get data address
    mov   [SSC_POINTER], A
    special_SSC FLASH_WRITE

    pop   X
    mov   A, [SSC_RETURNCODE]
    ret 
   

;------------------------------------------------------------------------------
;   FUNCTION NAME: flash_read_lite
;
;   DESCRIPTION:
;   Reads a specified flash block to a buffer in RAM.
;
;   ARGUMENTS:
;     BYTE  - Flash block ID
;     void* - destination RAM location
;     BYTE  - number of bytes to read
;       
;
;   RETURNS:
;   Data read is returned at specified RAM location.
;
;   SIDE EFFECTS:
;   Uses SSC storage at FBh and FDh
;
;   PROCEDURE:
;   BlockID is converted to absolute address and then ROMX command is used
;   to read the data from flash into the specified buffer.
;
;------------------------------------------------------------------------------

; Flash Read Operation Template
SSC_READ_FlashBuffer:      equ       FBh      ; pointer to data buffer
SSC_READ_Counter:          equ       FDh      ; byte counter

; Calling stack
PARAM_NVR_BLOCK:           equ      -4
PARAM_RAM_BUF_LO:          equ      -5
PARAM_RAM_BUF_HI:          equ      -6
PARAM_LEN:                 equ      -7


 flash_read_lite:
_flash_read_lite:
    push X
    mov  X, SP

    ; Move some arguments to SSC storage
    mov  A, [X+PARAM_RAM_BUF_LO]       ; get pointer
    mov  [SSC_READ_FlashBuffer], A     ; use SSC storage area
    mov  A, [X+PARAM_LEN]              ; get count
    mov  [SSC_READ_Counter], A         ; use SSC storage area

    ; Compute the absolute address of the flash block
    mov  A, [X+PARAM_NVR_BLOCK]        ; compute the LSB = bBlockId * 64
    rrc  A
    rrc  A
    rrc  A
    and  A, C0h
    push A                             ; save LSB on stack
    mov  A, [X+PARAM_NVR_BLOCK]        ; compute the MSB = bBlockId / 4
    asr  A
    asr  A
    and  A, 3Fh                        ; mask off sign extension bits
    pop  X                             ; LSB in X, MSB in A

    ; Read the Flash
read_flash:
    push A                             ; save MSB
    romx                               ; Read the flash
    mvi  [SSC_READ_FlashBuffer], A     ; store the data in the RAM buffer
    pop  A                             ; restore MSB
    inc  X                             ; increment the LSB of the flash addr
    dec  [SSC_READ_Counter]            ; decrement the byte counter
    jnz  read_flash

    ; Done reading the flash
    pop  X
    ret


;------------------------------------------------------------------------------
;  End of File
;------------------------------------------------------------------------------



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -