freescale
来自「Freescale 系列单片机常用模块与综合系统设计」· 代码 · 共 219 行
TXT
219 行
/*================================================================*/
/**
* Copyright (c) 2002, Motorola Inc.
* Motorola Reference Design
*
* File name : taxi_flash.c
* Project name : Taximeter Reference Design
* Author : Mauricio Capistran-Garza
* Department : Guadalajara - SPS
*
* Description : API for flash management. With this API the user
* can erase, program and verify flash. It can also
* be used to send and receive data from programmer.
*
* History :
*/
/*================================================================*/
#include "taximeter.h"
/************************************************************
FLASH PROGRAMMING API - FlashManager
************************************************************/
/**
* FlashManager: It manages most ROM-Resident Routines for
* flash reprogramming. There are four basic
* ROM-Resident Routines:
* + Program Range (PROG): it programs a range
* of flash memory. The starting address to
* be programmed must be defined in resiters
* H:X, and the ending address in the virtual
* registers LADDRH:LADDRL (0x8A:0x8B). This
* range is programmmed with the RAM content
* starting in address 0x8C.
* + Erase Page (ERASE): it erases a page (64
* bytes) of flash memory. To specify the
* page to be erased, you must load H:X with
* any address within that page.
* Calling this routine automatically disables
* all interrupts.
* + Read and Verify Range (COPY | SEND): it
* reads flash memory and compares it against
* RAM content (starting at address 0x8C).
* The first address to be compared must be
* loaded in registers H:X and the last
* address in virtual registers LADDRH:LADDRL
* (0x8A:0x8B).
* There are two variants of this routine
* depending on the value of the Acc. If
* the Acc is cleared, after comparing each
* byte, the RAM will be overwritten. So this
* routine can be used to COPY a range of flash
* and place it into RAM. If the Acc is set,
* after comparing each byte, this byte will
* be sent serially through the communication
* port (PTB0). So this routine can be used
* to SEND data to the taximeter programmer.
* + Read Byte: this ROM-Resident routine is not
* managed by FlashManager.
*
* There are also other virtual registers that
* all ROM-Resident routines use:
* + CTRLBYT - 0x0088 (RAM)
* + CPUSPD - 0x0089 (RAM)
* The contents of these virtual registers
* and LADDRH:LADDRL should be backed up before
* calling FlashManager.
*
* For more information on ROM-Resident Routines
* read AN1831.
*
* Entry Coditions: Back up virtual registers or they'll be
* overwritten.
*
* Exit Conditions: PTB0 configured as input.
* If ERASE is called, interrupts are disabled.
*
* Parameters: _type - What you want to do with the flash:
* PROG, ERASE, COPY, SEND
* _start_offset - memory offset from DATA_START
* at which you want to start the process
* chosen in _type. DATA_START is defined
* at the beginning of a 64-bytes page.
* Implicit Parameters: _end_offset - any process chosen in
* _type (excepting ERASE) begins in
* _start_offset and ends 32 bytes later.
* _accumulator - routines for COPY and SEND
* require special values of Acc. This
* are set implicitly.
*
* Variables read: none.
*
* Variables modified: Any variable located between 0x88 and 0x8B.
*
* Subfunctions: PRGRNGE(); Resident ROM routine
* ERARNGE(); Resident ROM routine
* RDVRRNG(); Resident ROM routine
*
* Return: void
*/
void FlashManager(Byte _type, Byte _start_offset) {
Word _temp;
// FLBPR = 0xFF; // Enable flash reading and writing
CPUSPD = OSC; // Set the CPUSPD
CTRLBYT &=0xBF; // Clear bit 6 to page erase mode.
_temp = DATA_START + _start_offset; // Calculate starting absolute address
LADDRH = DATA_START >> 8; // Set Last Address High
LADDRL = (_start_offset + ROW_LIMIT); // Set Last Address Low
PTBDD &= 0xFE; // Set PTB0 as input (Entry Condition)
__asm ldhx _temp; // Load in H:X starting address.
__asm lda _type; // make a switch on variable _type
__asm dbnza _NEXT1; // if _type != PROG, check next
PRGRNGE(); // Program Range
__asm bra _END_FLASH_MANAGER;
__asm _NEXT1: dbnza _NEXT2; // if _type != ERASE, check next
ERARNGE(); // Erase Page
__asm bra _END_FLASH_MANAGER;
__asm _NEXT2: deca; // Decrement Acc to set it up for
RDVRRNG(); // "Read/Verify Range (read above).
__asm _END_FLASH_MANAGER: ;
// FLBPR = 0x00; // Disable flahs reading and writing
return;
}
/************************************************************
FLASH PROGRAMMING API - BackupAccumulators
************************************************************/
/**
* BackupAccumulators: it backs up in flash the following
* accumulators: TOTAL_DISTANCE_TRAVELED
* TOTAL_DISTANCE_IN_SERVICE
* TOTAL_NUMBER_TRAVELS
* TOTAL_NUMBER_INCREMENTS
* TOTAL_INCOME
*
* Parameters: none.
*
* Variables read: accumulator_distance_traveled.
* accumulator_trip.
* accumulator_inc.
*
* Variables modified: total_distance_traveled.
* total_distance_traveled_in_service.
* total_number_travels.
* total_number_increments.
* total_income.
*
* Subfunctions: FlashManager().
*
* Return: void
*/
/*
Start_Copy_In_RAM refers to the begining of the segment ToCopyToRAM.
This segment contains the functions after they have been copied to RAM.
*/
void BackupAccumulators() {
__DI() ;
/* Backup RAM data; form 0x88 to 0x8B in the stack and from 0x8C to 0xAB in flash */
__asm LDHX 0x0088;
__asm PSHH;
__asm PSHX;
__asm LDHX 0x008A;
__asm PSHH;
__asm PSHX;
FlashManager(ERASE, // Erase FLASH backup space.
BACKUP_FLASH_OFFSET);
FlashManager(PROG, // Backup RAM data
BACKUP_FLASH_OFFSET);
/* Bring all accumulators into RAM so they can be modified */
FlashManager(COPY, // Copy flash into RAM
PAGE2_OFFSET);
total_distance_traveled += accumulator_distance_traveled; // Update values
if (accumulator_trip > 0) {
total_income += accumulator_trip;
total_distance_in_service += accumulator_distance_traveled;
total_number_increments += accumulator_inc;
total_number_travels ++;
}
/* Erase Flash Accumulators and program new values */
FlashManager(ERASE, // erase old values of Accumulators
PAGE2_OFFSET);
FlashManager(PROG, // program new values
PAGE2_OFFSET);
/* Set flash-verification byte to indicate that flash programming was successful */
FlashManager(COPY,
PAGE2_OFFSET + ROW); // Copy into RAM flash-verification byte
__asm LDA #0xAA;
__asm STA 0xAB; // Set flash-verification byte in RAM
FlashManager(PROG, // Program flash-verification byte.
PAGE2_OFFSET + ROW);
/* Restore RAM data */
FlashManager(COPY, // Restore RAM data from flash
BACKUP_FLASH_OFFSET);
__asm PULX; // Restore RAM data from stack
__asm PULH;
__asm STHX 0x008A;
__asm PULX;
__asm PULH;
__asm STHX 0x0088;
PTBDD = 0xFF;
__EI();
return;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?