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

📄 f_atmel_arm.s

📁 realview22.rar
💻 S
字号:
; ============================================================================
; F_ATMEL_ARM - Flash programming algorithm for Atmel Flash
;
; Library Name: Included into container .s file.
;
; Module Name:  F_ATMEL_ARM.S
; Module Descr: Flash programming for Atmel Flash devices
; Creation Date:April 2000
; Author:       Paul Kimelman
;
; THIS INFORMATION IS PROPRIETARY TO
; ALLANT SOFTWARE, INC.
; 1280 Civic Drive, Suite 206
; Walnut Creek, CA 94596
; USA
; ----------------------------------------------------------------------
; Copyright (c) 2000  Allant Software, Inc.
; ALL RIGHTS RESERVED;
;
; Revisions of F_ATMEL_ARM.S: (latest revision on top)
; #.#  Name  Date      Description
; ---  ----- --------  -----------------------------------------------------
; $Revision: 1.1.6.1 $
;
; Implementation notes:
;
; GENERAL
; -------
;
;       This file is to be included into another asm file which has the
; board/chip specific knowledge needed for initialization and the like.
; That is, this is the algorithm only.
;
;       This file is based on information on Atmel flash devices
; of various forms. It is generalized to handle x8 and x16 accesses.
; It does not need to know the block sizes or positions as that is all
; passed in. 
;
;       Minimally this will work with various AT49BVxxxx chips
; and AT29LVxxxx/AT29Cxxxx chips. This code started from C code
; from Europe Technologies for evaluation boards (Thanks to
; Patrice V).
;
;       This uses the standard RVDEBUG implementation for an API. That
; means that registers are used for passing arguments and returning
; status. The registers used are based on the save/restore list. The
; first registers are assumed to be the ones to use for passing args.
; In the case of ARM, this is R0, R1, R2, R3, etc.
;
; REGISTER MODEL
; --------------
;
;       The register model for this routine is as follows:
;       R0:     Input: page of Flash (not used on ARM)
;               Output:status return to host
;       R1:     Input: base address of Flash block to operate on
;       R2/R3/R4/R5/R8/R9: Input: arguments
;               Typical meanings: R2=Counter
;       R6/R7:Scratch
;
; -----------------------------------------------------------------------------
; Description:
; ----------------------------------------------------------------------------
;                               NOTICE
;
;       This code is proprietary; the use of this source is
; restricted, and all rights are reserved.
;
;       If reading for interest, enjoy.
;
;       If maintaining, read comments carefully! There are some inter-depen-
; dencies and connectivities. Areas with particular connection are marked.
; There are some variable protocols that are not self evident: check all uses
; of variables before making big changes.
; ============================================================================
;

;************************************************************************/
;* DEFINE ENTRY POINTS                                                  */
;************************************************************************/

;       EXPORT          FLASH_init      // provided by asm that includes us
;       EXPORT          FLASH_erase_all // not available
        EXPORT          FLASH_erase
        EXPORT          FLASH_write_erase
        EXPORT          FLASH_write
        EXPORT          FLASH_validate
        EXPORT          FLASH_break
;       EXPORT          FLASH_term      // we do not need one

;************************************************************************/
;* DEFINE MACROS AND EQUATES                                            */
;************************************************************************/

        IF              WIDTH=1
          MACRO
           STORE        $p1,$p2,$p3
            IF "$p3" = ""
             strb       $p1,$p2
            ELSE
             strb       $p1,$p2,$p3
            ENDIF
          MEND
          MACRO
           LOAD         $p1,$p2,$p3
            IF "$p3" = ""
             ldrb       $p1,$p2
            ELSE
             ldrb       $p1,$p2,$p3
            ENDIF
          MEND
        ELSE                    ; width = 2
          MACRO
           STORE        $p1,$p2,$p3
            IF "$p3" = ""
              strh      $p1,$p2
            ELSE
              strh      $p1,$p2,$p3
            ENDIF
          MEND
          MACRO
           LOAD         $p1,$p2,$p3
            IF "$p3" = ""
             ldrh       $p1,$p2
            ELSE
             ldrh       $p1,$p2,$p3
            ENDIF
          MEND
        ENDIF

        MACRO                   ; RET is return via LR or named reg
          RET           $p1
           IF "$p1" = ""
            mov R15,R14         ; Return with code in R0
           ELSE
            mov R15,$p1         ; Return with code in R0
           ENDIF
        MEND

FC_AT1   EQU            0xAA    ; attention request 1 (OFF1)
FC_AT2   EQU            0x55    ; attention request 2 (OFF2)
FC_ID_EN EQU            0x90    ; enter ID mode
FC_ID_EX EQU            0xF0    ; exit ID mode
FC_WRITE EQU            0xA0    ; start writing block
FC_ERASE EQU            0x80    ; start erase of segment/block
FC_SECTE EQU            0x30    ; sector erase (into sector)

;************************************************************************/
;* THE DATA POINTERS FOR DATA TOO LARGE FOR IMMEDIATES                  */
;************************************************************************/

FOFFSET1 DATA
        IF WIDTH = 1
         DCD            0x5555
        ELSE
         DCD            (0x5555*2)
        ENDIF
FOFFSET2 DATA
        IF WIDTH = 1
         DCD            0x2AAA
        ELSE
         DCD            (0x2AAA*2)
        ENDIF

DELAY   DATA
        DCD             100000


;************************************************************************/
;* THE GLOBAL FUNCTIONS (EXPORTED PUBLICLY)                             */
;************************************************************************/
 
;* -----------------------------------------------------------------------
;  Local_init - handle device part of init (get ID).
;
;  Notes:
;     - this verifies the product ID and returns if OK or not.
;
;  Input/Output:
;     - In: R1=Base of Flash
;           R0=Page of Flash (no meaning for ARM)
;     - Out:R0=status code (0=OK, else error)
;     - Scratch: R2,R6,R7
;     - Persist: R11 - we set with Base of Flash
;  ----------------------------------------------------------------------- */

Local_init
        mov     R11,R1          ; R1 is input R11 is persistance
        mov     R0,#FC_ID_EN    ; enter ID mode
        bl      flash_control   ; send op to Flash

        ldr     R0,DELAY        ; 100000 of delay
init_delay                      ; delay a while
        subs    R0,R0,#1
        bne     init_delay

        LOAD    R2,[R1]         ; get manu ID
        and     R2,R2,#0xFF     ; mask to one byte

        mov     R0,#FC_ID_EX    ; exit ID mode
        bl      flash_control   ; send op to Flash
        mov     R0,#0           ; assume OK

        cmp     R2,#0x1F        ; make sure Atmel manu ID
        addne   R0,R0,#1        ; error
        b       FLASH_break     ; exit
        
;* -----------------------------------------------------------------------
;  FLASH_erase - erase a Flash block
;
;  Notes:
;     - this erases a Flash block on request from host. The host has
;       the information on which end has the boot block and its size,
;       so we do not need to worry about that. 
;     - we may be asked to erase one or a set of blocks, but all will
;       have the same size.
;
;  Input/Output:
;     - In: R1=Base of 1st Flash Block to erase
;           R0=Page of Flash (no meaning for ARM)
;           R2=Count of blocks to do
;           R3=Size of blocks (for iterate)
;     - Out:R0=status code (0=OK, else error)
;     - Scratch: R6,R7,R8
;     - Persist: R11 - set with Base of Flash
;  ----------------------------------------------------------------------- */

FLASH_erase
        ; we use sector erase model
        mov     R0,#FC_ERASE    ; start erase mode
        bl      flash_control   ; send op to Flash
        mov     R0,#FC_SECTE    ; write to sector
        STORE   R0,[R1]         ; do erase of that block

        subs    R2,R2,#1        ; count down
        addne   R1,R1,R4        ; setup for next block if more
        bne     FLASH_erase     ; more to do
        b       FLASH_break     ; Stop routine on breakpoint

;* -----------------------------------------------------------------------
;  FLASH_write - write to a Flash block
;  FLASH_write_erase - erase then write
;
;  Notes:
;     - this writes a buffer into a flash block. The caller is responsible
;       for pre-erasing if needed.
;     - note: this is writing half-words and not bytes
;     - the host knows about block sizes and all, and passes in the needed
;       info.
;     - if we are writing into a later part of the block, this will be
;       handled by the host.
;
;  Input/Output:
;     - In: R1=Base of Flash Block to write
;           R0=Page of Flash (no meaning for ARM)
;           R2=Count of bytes to copy
;           R4=Offset in block to copy into (from base)
;           R5=Address of buffer to copy from
;           R8=Page of buffer (no meaning for ARM)
;           R9=0 if no verify, else same value as R2 (count)
;     - Out:R0=status code (0=OK, else error)
;     - Scratch: R6,R7
;     - Persist: R11 - set with Base of Flash
;  ----------------------------------------------------------------------- */

FLASH_write_erase               ; auto-erase on write
FLASH_write
        mov     R0,#FC_WRITE    ; start write mode
        bl      flash_control   ; send op to Flash
        add     R4,R4,R1        ; compute start address

write_loop
        ; algorithm writes all and then waits for last one to
        ; "stick" by using readback test
        LOAD    R7,[R5],#WIDTH  ; load first value from buffer then inc
        STORE   R7,[R4],#WIDTH  ; write value desired then inc

        subs    R2,R2,#WIDTH    ; decrement count, more?
        bgt     write_loop      ; yes, continue

        ldr     R2,DELAY        ; 100000 tries
write_wait_last
        LOAD    R0,[R4,#-WIDTH] ; read last value
        cmp     R0,R7           ; same as written?
        beq     write_last_ok   ; yes, move on to next step
        subs    R2,R2,#1        ; no, try again?
        bgt     write_wait_last ; yes
        mov     R0,#1           ; failed to stick
        beq     done_write      ; fail

write_last_ok
        mov     R0,#0           ; was good
        cmp     R9,#0           ; want verify?
        beq     done_write      ; no, just quit

        mov     R2,R9           ; reset count (buff size)
        sub     R4,R4,R2        ; back up to start of block to verify
        sub     R5,R5,R2        ; back up to start of buffer to verify
        b       FLASH_validate  ; go on to validate code

done_write
        b       FLASH_break     ; Stop routine on breakpoint

;* -----------------------------------------------------------------------
;  FLASH_validate - validate write to a Flash block
;
;  Notes:
;     - this compares a buffer and a flash block. The caller uses this
;       after a write to verify the write worked.
;     - the host knows about block sizes and all, and passes in the needed
;       info.
;
;  Input/Output:
;     - In: R1=Base of Flash block to verify
;           R0=Page of Flash (no meaning for ARM)
;           R2=Count of bytes to verify
;           R4=Offset in block to verify from
;           R5=Address of buffer to verify against
;           R8=Page of buffer (no meaning for ARM)
;     - Out:R0=status code (0=OK, else error)
;     - Scratch: R6,R7
;  ----------------------------------------------------------------------- */

FLASH_validate
        mov     R6,#0
        mov     R7,#0
validate_loop
        LOAD    R7,[R5],#WIDTH  ; load value from buffer
        LOAD    R6,[R4],#WIDTH  ; load value from Flash
        cmp     R6,R7           ; same?
        bne     bad_validate    ; no, failed

        subs    R2,R2,#WIDTH    ; decrement count, more?
        bgt     validate_loop   ; yes, continue

        b       FLASH_break     ; Stop routine on breakpoint
        
bad_validate
        mov     R0,#1           ; compare
        b       FLASH_break     ; Stop routine on breakpoint

;* -----------------------------------------------------------------------
;  FLASH_break - end of function entry for all Flash routines
;
;  Notes:
;     - this routine is the Stop routine for the flash code. It runs over
;       what should be a break (set by host). If the host messes up, it
;       will loop forever allowing the host to halt it.
;
;  Input/Output:
;     - In: R0=status code to return (0=OK, else error)
;           R1=base of Flash block operated on
;   ----------------------------------------------------------------------- */

FLASH_break
        nop                     ; place to put breakpoint from host
forever
        b       forever         ; in case host screws up

;************************************************************************/
;* STATIC FUNCTIONS                                                     */
;************************************************************************/

;* -----------------------------------------------------------------------
;  flash_control - write 3 values to flash to start operation.
;
;  Notes:
;     - this writes the initialization values to the Flash and
;       then the value in R7.
;
;  Input/Output:
;     - In: R1=Base of Flash (not changed)
;           R7=Operation value to write (not changed)
;     - Out:<none>
;     - Scratch: R0,R6
;     - Persist: R11 - set with Base of Flash
;  ----------------------------------------------------------------------- */

flash_control
        ldr     R6,FOFFSET1     ; write to OFF1
        add     R6,R6,R11       ; note arith since >12 bits of off
        mov     R7,#FC_AT1      ; start of enable ID mode
        STORE   R7,[R6]         ; write byte/half 0xAA to Flash
        ldr     R6,FOFFSET2     ; write to OFF2
        add     R6,R6,R11       ; note arith since >12 bits of off
        mov     R7,#FC_AT2      ; start of enable ID mode
        STORE   R7,[R6]         ; write byte/half 0xFF to Flash
        ldr     R6,FOFFSET1     ; write to OFF1
        add     R6,R6,R11       ; note arith since >12 bits of off
        STORE   R0,[R6]         ; write byte/half 0x90 to Flash
        RET

        END

⌨️ 快捷键说明

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