Automatically Initializing Data Segment Values in MAX-IDE

[09-11 23:02:11]   来源:http://www.88dzw.com  单片机学习   阅读:8704

文章摘要:Saving and Restoring the Data SegmentA question remains: how can you make the application operate consistently, whether it is connected to a debugger (with MAX-IDE reloading code and data before each run) or free-running (with no particular contents in RAM guaranteed following power-up). The obvious

Automatically Initializing Data Segment Values in MAX-IDE,标签:单片机开发,单片机原理,单片机教程,http://www.88dzw.com

Saving and Restoring the Data Segment

A question remains: how can you make the application operate consistently, whether it is connected to a debugger (with MAX-IDE reloading code and data before each run) or free-running (with no particular contents in RAM guaranteed following power-up). The obvious solution is a two-step process: have the application save the variable values (once they have been initialized) in flash memory, and restore the values following each reset or power-up.

As a first step, the application must save values to flash memory. This action occurs the first time that the application is executed following each master erase and code load cycle.
  1. The application checks a "flag" location to verify that the variables have not been previously copied to flash. This flag can be a special-purpose, nonvariable location, or it can be shared with a variable as long as that variable has a nonzero initial value (to distinguish it from a blank RAM location).
  2. The application copies each variable value from data RAM to flash memory. On most MAXQ microcontrollers with rewriteable flash (such as the MAXQ2000) this is done using the UROM_flashWrite function.
  3. The application writes a flag in flash memory to indicate that the variables have been stored.
As the secondly step, on subsequent runs the application must restore the variable values from flash memory to their expected locations in data RAM.
  1. The application checks the flag location in flash to verify that the variable values have been stored.
  2. The application uses the UROM_copyBuffer routine to copy the variable values from flash memory to their proper locations in data RAM.
The code listing below demonstrates this saving-restoring method with the MAXQ2000 EV kit. In this code, variable values are stored in flash memory at addresses 7000h–71FFh.
$include(maxQ2000.inc)

;; Code memory (flash) : 0000h-7FFFh  (word addr)
;; Data memory (RAM)   : 0000h-03FFh  (word addr)

org 0000h

   ljump   start             ; Skip over password area

org 0020h

start:
   move    DPC, #1Ch         ; Set all pointers to word mode
   move    DP[0], #0F000h    ; Check first variable value (flag)
   lcall   UROM_moveDP0      ; 'move GR, @DP[0]' executed by Utility ROM
   move    Acc, GR
   cmp     #1234h
   jump    NE, copyToFlash

;; This is the "free-running" code, executed on subsequent power-ups, that copies
;; values from the flash back into their proper data segment locations.

   move    DP[0], #0F000h    ; Source: Flash location 7000h
   move    BP,    #0         ; Dest:   Start of RAM
   move    Offs,  #0
   move    LC[0], #100h      ; Copy 256 words
   lcall   UROM_copyBuffer

   jump    main

;; This is the first-pass code. A bit of a trick here; because MAX-IDE enters
;; and exits the loader separately when loading the code and data segment files,
;; the application is allowed to execute briefly before the data segment file
;; has been loaded. The first four lines under copyFlash ensure that the 
;; application waits for MAX-IDE to load the data segment file before continuing.

copyToFlash:
   move    DP[0], #0h        ; Wait for flag variable to be loaded by MAX-IDE.
   move    Acc, @DP[0]       ;    Note that this will reset the application; the
   cmp     #1234h            ;    data segment is not loaded while the application
   jump    NE, copyToFlash   ;    is still running.

   move    DP[0], #0         ; Start of RAM variable area
   move    A[4],  #7000h     ; Location in flash to write to
   move    LC[0], #100h      ; Store 256 words in flash 7000h-70FFh

copyToFlash_loop:
   move    DP[0], DP[0]      ; Refresh the data pointer to read values correctly,
                             ;    because calling UROM_flashWrite changes memory
                             ;    contexts and affects the cached @DP[0] value
   move    A[0], A[4]        ; Location to write
   move    A[1], @DP[0]++    ; Value to write (taken from RAM)
   lcall   UROM_flashWrite
   move    Acc, A[4]
   add     #1
   move    A[4], Acc
   djnz    LC[0], copyToFlash_loop

main:
   move    PD0,   #0FFh      ; Set all port 0 pins to output
   move    PO0,   #000h      ; Drive all port 0 pins low (LEDs off)

   move    DPC,   #1Ch       ; Set pointers to word mode
   move    DP[0], #varA
   move    Acc,   @DP[0]
   cmp     #1234h            ; Verify that the variable is set correctly
   jump    NE, fail

pass:
   move    PO0, #55h
   sjump   $

fail:
   sjump   $

segment data

org 0000h

varA:   
   dw  1234h

org 00FFh

varB:
   dw  5678h

end

上一页  [1] [2] [3] [4] [5]  下一页


Tag:单片机学习单片机开发,单片机原理,单片机教程单片机学习
分类导航
最新更新
热门排行