📄 ex5_eeprom.c
字号:
/******************************************************************************
* COPYRIGHT (c) MOTOROLA 2002
* FILE NAME: eeprom.c REVISION R1B
*
* PURPOSE: This module handles all tasks related to internal EEPROM module
*
*******************************************************************************
*******************************************************************************
** THIS CODE IS ONLY INTENDED AS AN EXAMPLE FOR THE METROWERKS COMPILER AND **
** THE HCS 12 EVB AND HAS ONLY BEEN GIVEN A MIMIMUM LEVEL OF TEST. **
** IT IS PROVIDED 'AS SEEN' WITH NO GUARANTEES AND NO PROMISE OF SUPPORT. **
*******************************************************************************
*******************************************************************************
*
* DOCUMENTATION SOURCE: Motorola TSPG 8/16 MCU bit Division (HKG)
*
* TARGET DEVICE: Hardware EVB912DP256 or equivalent
*
* INCLUDE FILES: <hidef.h>
* "com.h"
* "DP256Port.h"
*
* FUNCTIONS : eeprom_erase_cmd
* eeprom_program_cmd
* eeprom_program
* eeprom_erase
*
* COMPILER: Metrowerks VERSION: ADS v1.2
*
* DESCRIPTION: Program to control the EEPROM module
*
* INPUTS
* ------
* All commands are inovked by PC Hyperterminal via SCI0.
*
* NOTES
* -----
* All modules remain at their reset addresses.
*
*
* UPDATE HISTORY
* REV AUTHOR DATE DESCRIPTION OF CHANGE
* --- ------ --------- ---------------------
* R1A r29566 02/04/20 Initial version
* R1B r29566 02/07/12 Demo version
*
******************************************************************************/
/*===============================a============================================*/
/* Motorola reserves the right to make changes without further notice to any */
/* product herein to improve reliability, function, or design. Motorola does */
/* not assume any liability arising out of the application or use of any */
/* product, circuit, or software described herein; neither does it convey */
/* any license under its patent rights nor the rights of others. Motorola */
/* products are not designed, intended, or authorized for use as components */
/* in systems intended for surgical implant into the body, or other */
/* applications intended to support life, or for any other application in */
/* which the failure of the Motorola product could create a situation where */
/* personal injury or death may occur. Should Buyer purchase or use Motorola */
/* products for any such intended or unauthorized application, Buyer shall */
/* indemnify and hold Motorola and its officers, employees, subsidiaries, */
/* affiliates, and distributors harmless against all claims costs, damages, */
/* and expenses, and reasonable attorney fees arising out of, directly or */
/* indirectly, any claim of personal injury or death associated with such */
/* unintended or unauthorized use, even if such claim alleges that Motorola */
/* was negligent regarding the design or manufacture of the part. Motorola */
/* and the Motorola logo* are registered trademarks of Motorola Ltd. */
/*****************************************************************************/
#include <hidef.h>
#include "const.h"
#include "ut_extern.h"
#include "ex5_extern.h"
#include "DP256Port.h"
#define pviol 0x20
#define accerr 0x10
#define prog 0x20
#define ccif 0x40
#define cbeif 0x80
#define erase 0x40
#define fcnfg (*((volatile unsigned char*)(0x0103)))
#define fstat (*((volatile unsigned char*)(0x0105)))
#define fcmd (*((volatile unsigned char*)(0x0106)))
#define eclkdiv (*((volatile unsigned char*)(0x0110)))
#define ecnfg (*((volatile unsigned char*)(0x0113)))
#define eprot (*((volatile unsigned char*)(0x0114)))
#define estat (*((volatile unsigned char*)(0x0115)))
#define ecmd (*((volatile unsigned char*)(0x0116)))
/**********************************************
* PORTB bit test menu subroutine
* input : PORTB menu
* output : Corresponding sub-menu
***********************************************/
void eeprom_test_menu(void)
{
byte cdata,loop=ENABLE;
while(loop==ENABLE)
{
printf0("\n\t\tEEPROM Test Menu\n\r");
printf0("\t0. ROOT Test Menu\n\r");
printf0("\t1. EEPROM Program\n\r");
printf0("\t2. EEPROM Erase\n\r");
printf0("\t9. Display DATA\n\r");
do
{
cdata=menurx_char0(FLASH_PAGE);
if(cdata==CR || cdata=='9')
break;
}
while((cdata>'3') || (cdata<'0'));
//get input from user
switch(cdata)
{
case '0': loop=DISABLE;
break;
case '1': eeprom_program(); //flash_program();
break;
case '2': eeprom_erase();
break;
case '9': data_display();
break;
}
}
}
/*******************************************************************
* EEPROM program command subroutine
* Description : Copy from RAM area to EEPROM area
* : Input RAM,EEPROM start address and no. of word to be
* : programmed
* Example : Copy 4 words from RAM(0x1000) to EEPROM (0x600)
* input : eram_addi=0x1000, eeprom_addi=0x600, no_of word=0x4
* modify : eeprom content (0x600 to 0x608)= (0x1000 to 0x1008)
*********************************************************************/
void eeprom_program_cmd(int eram_addi, int eeprom_addi, int no_of_word)
{
int *ram_add,*eeprom_add,count=0;
ram_add=(int*)eram_addi;
eeprom_add=(int*)eeprom_addi;
eclkdiv = 0x49;
DisableInterrupts;
estat=pviol+accerr; //movb #$30,$115 ; clear previous command error
while(count!=no_of_word)
{
*eeprom_add=*ram_add; //; get data and put it to EEPROM
ecmd=prog; //; initiate PROGRAM COMMAND
estat=cbeif; //; begin command
if(((estat&pviol)==pviol)||((estat&accerr)==accerr))
printf0("EEprom programming error\n\r"); //display eree_program_error
else
while((estat&ccif)==0x00); // wait for command to complete
count++;
eeprom_add++;
ram_add++;
}
EnableInterrupts;
}
/*******************************************************************
* EEPROM erase command subroutine
* Description : Erase EEPROM long word (4 bytes)
* : Input EEPROM start address and no. of word to be
* : erased
* Example : Erase 4 long words from EEPROM (0x600)
* input : eeprom_addi=0x600, no_of word=0x4
* modify : eeprom content (0x600-0x610)=0xff
*********************************************************************/
void eeprom_erase_cmd(int eeprom_addi, int no_of_long_word)
{
int *eeprom_add,count=0;
eeprom_add=(int*)eeprom_addi;
eclkdiv = 0x49;
DisableInterrupts;
estat=pviol+accerr; //movb #$30,$115 ; clear previous command error
while(count!= no_of_long_word)
{
*eeprom_add=0; // to erase the data at EEPROM
ecmd=erase; //; initiate ERASE COMMAND
estat=cbeif; //; begin command
if(((estat&pviol)==pviol)||((estat&accerr)==accerr))
{
printf0("EEprom programming error in \n\r"); //display eree_program_error
hex_asc((byte)(eeprom_addi>>8));
hex_asc((byte)(eeprom_addi));
printf0("\n\r"); //display lf
}
else
while((estat&ccif)==0x00); // wait for command to complete
count++;
eeprom_add+=2; //int pointer increment
eeprom_addi+=4; //eeprom_addi increment by 4 (long)
tx_char0('.');
}
EnableInterrupts;
}
/*******************************************************************
* EEPROM program subroutine
* Description : User input RAM area and EEPROM area to be copied
* : Input RAM,EEPROM start address and no. of word to be
* : programmed
* Example : Copy 4 words from RAM(0x1000) to EEPROM (0x600)
* input : eram_addi=0x1000, eeprom_addi=0x600, no_of word=0x4
* modify : eeprom content
*********************************************************************/
void eeprom_program()
{
unsigned int *eeprom_addi,add0,*eram_addi,add1;
byte status=DISABLE,ebyte;
eeprom_addi=&add0;
eram_addi=&add1;
printf0("RAM start address (0x1000-0x12ff)= \r");
if(input_word(eram_addi)==OK)
{
if((*eram_addi>=0x1000)&&(*eram_addi<0x1300))
status=ENABLE;
else
{
printf0("\nOut of range!\r");
status=DISABLE;
}
}
else
status=DISABLE;
printf0("\n\r");
if (status==ENABLE)
{
printf0("EEPROM start address to be programmed ($400 default)= \r");
if(input_word(eeprom_addi)==OK)
{
if((*eeprom_addi>=0x400)&&(*eeprom_addi<0x1000))
status=ENABLE;
else
{
printf0("\nOut of range!\r");
status=DISABLE;
}
}
else
status=DISABLE;
printf0("\n\r");
}
if (status==ENABLE)
{
printf0("No. of word to be programmed (Dec) = \r");
if(input_dec(&ebyte)==OK)
{
if (((ebyte)==DOT)||((ebyte)==ESC))
status=DISABLE;
else
status=ENABLE;
printf0("\n\r");
}
}
if (status==ENABLE)
eeprom_program_cmd(*eram_addi, *eeprom_addi, (unsigned int) ebyte);
}
/*******************************************************************
* EEPROM erase subroutine
* Description : Erase EEPROM long word (4 bytes)
* : User input EEPROM start address and no. of word to be
* : erased
* Example : Erase 4 long words from EEPROM (0x600)
* input : eeprom_addi=0x600, no_of word=0x4
* modify : eeprom content (0x600-0x610)=0xff
*********************************************************************/
void eeprom_erase()
{
unsigned int *address,add;
byte status=DISABLE,ebyte;
address=&add;
printf0("EEPROM start address to be Erase (hex) (0x400-0xfff) = \r");
if(input_word(address)==OK)
{
if((*address>=0x400)&&(*address<0x1000))
status=ENABLE;
else
{
printf0("\nOut of range!\r");
status=DISABLE;
}
}
else
status=DISABLE;
printf0("\n\r");
if (status==ENABLE)
{
printf0("No. of long word (4 bytes) to be erased (Dec) = \r");
if(input_dec(&ebyte)==OK)
{
if (((ebyte)==DOT)||((ebyte)==ESC))
status=DISABLE;
else
status=ENABLE;
printf0("\n\r");
}
}
if (status==ENABLE)
eeprom_erase_cmd(*address, (unsigned int) ebyte);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -