📄 flash_test.c
字号:
/*==================================================================*
*
* Copyright (c) 2002, Motorola Inc.
* Motorola Application Note
*
* File name : flash_test.c
* Author : Mauricio Capistran-Garza
* Department : Guadalajara - SPS
*
* Description : This is a sample program that shows how
* ROM-resident routines can be used through
* the use of a simple C-language API
*
* Compiler : HC08CW v3.0
*
* History :
*
*==================================================================*/
/********************************************************************
INCLUDES
********************************************************************/
//#include "jl3.h"
#include <MC68HC908JL3.h> // Include Peripheral declarations
#include <hidef.h>
#include <stdtypes.h>
#include "flash_api.h"
/********************************************************************
DEFINES
********************************************************************/
#define MY_INFO_ADDRESS 0xFB00
#define MY_INFO_SIZE 8
#define MY_TRANSMISION_ADDRESS 0xFB08
#define MY_TRANSMISION_SIZE 29
#define FLASH_TEST_ADDRESS 0xFB40
#define DATA_START 0x008C
#define DATA_END 0x00CC
/*******************************************************************
TABLES
*******************************************************************/
#pragma CONST_SEG MY_INFO
volatile const Byte TABLE1[8] = {'M','o','t','o','r','o','l','a'};
#pragma CONST_SEG MY_TRANSMISION
volatile const Byte TABLE2[29] = {
'T','r','a','n','s','m','i','t',' ','R','a','n','g','e',' ',
'w','a','s',' ','s','u','c','c','e','s','s','f','u','l' };
#pragma CONST_SEG DEFAULT
/********************************************************************
ISR
********************************************************************/
/* These ISR are written to show a possible work-around to
avoid routine ReadByte from entering into an infinite loop.
*/
/*
interrupt 6 void Timer(void) {
TASC=TASC;
TASC &= TOF; // If a timer interrupt happens
__asm swi; // call the Software Interrupt
}
interrupt 2 void SWI(void) { // Software Interrupt ISR
DisableInterrupts; // may include user-supplied code.
while (-1) { // The stack can be reset and
TransmitByte ('X'); // the whole program started again.
}
}
*/
/********************************************************************
MAIN
********************************************************************/
void main(void) {
Byte temp;
Byte size;
Word address;
/* System configuration */
CONFIG2 = 0x80; /* 10000000b;
||||||||_________ Reserved
|||||____________ LVIT
||| _____________ Reserved
|________________ IRQ Internal Pull-Up */
CONFIG1 = 0x11; /* 00010001b;
||||||||_________ COP disabled
|||||||__________ STOP as illegal opcode
||||||___________ Short Stop Recovery Bit
|||||____________ Reserved
||||_____________ LVI enabled
|||______________ Reserved
|________________ COP reset period */
/* Configure the timer if wanted */
/*
TMODH = 0x4C; // Interrupt every 16 ms
TMODL = 0xCD;
TSC = 0x40; // 01000000b;
// ||||||||_________ Timer Prescaled by 1
// ||||| Int Bus Clk = 1.2288MHz
// ||||| 1229 counts,
// ||||| error = 0.58 sec/hour
// |||||____________ Unimplemented
// ||||_____________ Reset Bit = 0 -> No effect
// |||______________ Stop Bit: Start Counter
// ||_______________ TIM Overflow ints enabled
// |________________ TIM Overflow Flag
*/
temp = TABLE1[0];
size = TABLE2[0];
/*
* Receive one byte and echo it. This shows a typical usage
* of API functions GetByte and Transmit Byte.
* This functions can not be debugged with the In-Circuit
* Debugger (ICD) since the Communication Port is used by the ICD.
*/
temp = ReadByte();
TransmitByte(temp);
DATA(0) = temp; // this is a way of accessing DATA,
// however it is very space-consuming
// when it gets translated into assembly
size = 1; // One byte is ready to be programmed
/*
* Erase a PAGE of flash named FLASH_TEST. Notice how Interrupts
* will be automatically disabled.
*/
address = FLASH_TEST_ADDRESS;
EnableInterrupts;
ErasePage(&address);
/*
* Program FLASH_TEST with the received Byte.
*/
ProgramRange(&address, size);
/*
* Verify that the programming was successful.
*/
temp = VerifyRange(&address, size); // Now temp will be set to
// SUCCESS or FAIL, and address
// will be set with the checksum
// the data verified
if (temp == SUCCESS) {
temp = temp + 1; // Do anything wanted
}
/*
* Read a section in FLASH named MY_INFO. In this section
* it is stored the message "Motorola". Notice how
* the DATA (RAM_START + 0x0C) will be replaced with FLASH data.
*/
address = MY_INFO_ADDRESS;
size = MY_INFO_SIZE;
temp = ReadRange(&address, size);
/*
* Transmit a range of FLASH named MY_TRANSMISION. In this
* sections it's stored the message "TransmitRange was successful".
* Notice how the DATA (RAM_START + 0x0C) will NOT be replaced
* with the data transmitted.
*/
address = MY_TRANSMISION_ADDRESS;
size = MY_TRANSMISION_SIZE;
temp = TransmitRange(&address, size);
/*
* Fill out manually 60 bytes of DATA (RAM_START + 0x0C).
*/
/* for (i=0; i<64; i++) { // This is a way of filling out DATA
DATA_STR(i) = 'm'; // but it is very space-consuming
} // Next, another way to do it:
*/
__asm LDHX #DATA_START; // Where DATA begins
__asm LDA #122;
__asm _FILL_RAM: STA ,X; // save the received byte
__asm INCX; // point to next location in RAM
__asm DECA;
__asm CPX #DATA_END; // if DATA_END reached exit loop
__asm BNE _FILL_RAM;
/*
* Try to program again the section FLASH_TEST with ProgramRangeX
* It is going to fail since this section is not blank.
*/
address = FLASH_TEST_ADDRESS;
size = 10;
temp = ProgramRangeX(&address, size);
if (temp == FAIL) {
temp = temp + 1; // Do anything wanted
}
/*
* Erase section FLASH_TEST. Notice how interrupts won't be disabled.
*/
EnableInterrupts;
ErasePageX(&address);
/*
* Program section FLASH_TEST with ProgramRangeX.
* This time ProgramRangeX is going to succeed.
*/
temp = ProgramRangeX(&address, size);
if (temp == FAIL) {
temp = temp + 1; // Do anything wanted
}
/*
* Finally Erase the entire FLASH.
*/
EraseFlash();
for ( ;; );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -