📄 main.c
字号:
/******************************************************************************
Copyright (c) Freescale 2006
File Name : $RCSfile: main.c,v $
Current Revision : $Revision: 1.1 $
PURPOSE: Example 9: Emulated EEPROM
DESCRIPTION: function main() providing initial program entry.
function Delay() - simple software delay
ISR definitions for non maskable CPU interrupts
UPDATE HISTORY
REV AUTHOR DATE DESCRIPTION OF CHANGE
--- ------ -------- ---------------------
1.0 r32151 01/03/05 - initial coding
1.1 r32151 26/01/06 - general clean up.
Removed all MPU code.
Added NOPs in front of BGND instructions. in ISRs
*******************************************************************
* File created by: Freescale East Kilbride MCD Applications Group *
*******************************************************************
******************************************************************************/
/*===========================================================================*/
/* Freescale reserves the right to make changes without further notice to any*/
/* product herein to improve reliability, function, or design. Freescale 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. Freescale*/
/* 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 Freescale product could create a situation where*/
/* personal injury or death may occur. Should Buyer purchase or use Freescale*/
/* products for any such intended or unauthorized application, Buyer shall */
/* indemnify and hold Freescale 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 Freescale*/
/* was negligent regarding the design or manufacture of the part. Freescale*/
/* and the Freescale logo* are registered trademarks of Freescale Ltd. */
/*****************************************************************************/
/************************* Include Files *************************************/
/*general includes */
#include <hidef.h> /* also includes boolean definitions in stdtypes.h */
#include "s12x_peripherals.h"
#include "target.h" /* includes peripherals definitions and FSL data types */
#include "interruptConfig.h"
#include "sys_params.h"
/*application includes */
#include "main.h"
#include "s12_io.h"
/*driver includes */
#include "S12XE_Flash.h"
/************************* typedefs ******************************************/
/* check main.h */
/************************* #defines ******************************************/
/* check main.h */
/**** SET THE SIZE OF THE EEE HERE ****/
#define EEE_SECTORS 16 /* x256 bytes ~ can be 0 to 16*/
/**** THESE DFPART MACROS ASSUME MINIMUM EEE RAM/NVM RATIO ****/
/**** REPLACE / MODIFY THEM IF A LARGER RATIO IS REQUIRED ****/
#if(EEE_SECTORS == 1)
#define DFPART (128 - 12)
#else
#define DFPART (128 - (8 * EEE_SECTORS))
#endif
/* define the symbols to be used: */
__SEG_START_DEF(SYSTEM_DATA); // start of system parameter variables
__SEG_SIZE_DEF(SYSTEM_DATA); // size of system parameter variables
/************************* Constants *****************************************/
#pragma CONST_SEG DEFAULT
/************************* Global Variables **********************************/
#pragma DATA_SEG SHARED_DATA
#pragma DATA_SEG EEPROM_DATA
volatile tU16 EEE_Int[8]; /* Array situated in EEE RAM at 0x0F00'L */
/* volatile so we can always see the access */
/* for evaluation purposes only */
#pragma DATA_SEG DEFAULT
/************************* function prototypes *******************************/
/* in s12x_vectors.h */
#pragma CODE_SEG DEFAULT
/************************* Functions *****************************************/
#pragma CODE_SEG DEFAULT
/******************************************************************************
Function Name : main
Engineer : r32151
Date : 01/03/2005
Parameters : NONE
Returns : NONE
Notes : main routine called by Startup.c.
******************************************************************************/
void main(void)
{
volatile tU16 temp = 0;
volatile tU08 ftmStat;
volatile tU16 ErrorCount = 0;
tU08 *__far src_ptr_f08, *__far dst_ptr_f08;
int i;
tU08 checkSum = 0;
unsigned char dfPart, erPart;
while (!FTM.fstat.bit.ccif) {} /* wait for FTM reset to complete */
FTM.fclkdiv.byte = FCLK_DIV; /* write the flash clock divider */
/* before launching first FTM command */
/* but after CCIF flag has set */
if(FTM.fclkdiv.byte != (FCLK_DIV | 0x80))
Error(0);
/************************* Partitioning *****************************************/
//#define FORCE_PARTITON_FOR_DEBUG /* uncomment this line to partition and clear EEE */
/* can ONLY beused in DEBUG mode! */
#ifdef FORCE_PARTITON_FOR_DEBUG
if(LaunchFlashCommand(3 ,FULL_PARTITION_D_FLASH, 0, DFPART, EEE_SECTORS, 0, 0, 0) != CCIF) /* format eee - special mode only - also erases all D-Flash */
Error(0);
#else /* query EEPROM, if not already partitioned then do so */
if(LaunchFlashCommand(1 ,EEPROM_QUERY, 0, 0, 0, 0, 0, 0) != CCIF) /* check the EEE status */
Error(0);
FTM.fccobix.byte = 1;
dfPart = FTM.fccob.byte.lo;
FTM.fccobix.byte++;
erPart = FTM.fccob.byte.lo;
if((erPart == 0xFF) & (dfPart == 0xFF)) /* if D-Flash is clear and has not been partitioned */
if(LaunchFlashCommand(3 ,PARTITION_D_FLASH, 0, DFPART, EEE_SECTORS, 0, 0, 0) != CCIF) /* format eee - special mode only*/
Error(0);
#endif
/* lets have a look to check the partitioning */
if(LaunchFlashCommand(1 ,EEPROM_QUERY, 0, 0, 0, 0, 0, 0) != CCIF) /* check the EEE status */
Error(0);
FTM.fccobix.byte = 1;
dfPart = FTM.fccob.byte.lo;
FTM.fccobix.byte++;
erPart = FTM.fccob.byte.lo;
if((erPart != EEE_SECTORS) | (dfPart != DFPART)) /* erPart should == EEE_SECTORS and dfPart == DFPART */
Error(0);
/************************* Main Code *****************************************/
/* can read / write the EEE from here on */
/* enable EEE error interrupts */
FTM.fercnfg.byte = ERSERIE|PGMERIE|ERSVIE0|ERSVIE1;
EnableInterrupts;
/* EEE is disabled here */
/* copy constant parameters from D-Flash to RAM */
src_ptr_f08 = (tU08 *__far)SYS_PARAM_DFLASH_START; /* pointer to start of D-Flash */
dst_ptr_f08 = (tU08 *__far)SYSTEM_DATA_START; /* pointer to first data word */
for(i = (int)__SEG_SIZE_REF(SYSTEM_DATA) ; i > 0; i--) {
*dst_ptr_f08++ = *src_ptr_f08;
checkSum += *src_ptr_f08++;
}
/* check for the EEE value to be erased the first time after EEE format - */
if (EEE_Int[0] == -1)
EEE_Int[0] = 0x6006;
/* check the intial value of the EEE variable */
/* will only appear if the flow ran down to the while(1) loop last time */
if(EEE_Int[0] != 0x6006)
ErrorCount++;
/* write to the EEE */
EEE_Int[0] = 0x1001;
/* and read it */
temp = EEE_Int[0]; /* this shows that with the EEE disabled it is still possible */
/* to write to the EEE-RAM region. However the value is volatile */
/* enable writes of EEE records */
if (FTM.fstat.byte == 0x80) /* ensure that FTM is operational and error free */
{
ftmStat = LaunchFlashCommand(1 ,ENABLE_EEPROM_EMULATION, 0, 0, 0, 0, 0, 0);
if ((ftmStat & (ACCERR|FPVIOL|MGSTAT1|MGSTAT0))) /* ensure no errors occured during activation */
Error(ftmStat);
}
else
Error(0);
/* update the EEE */
EEE_Int[0] = 0x2002;
/* and read it while the EEE system is active i.e. while the record is being updated */
temp = EEE_Int[0]; /* notice how the variable is stored in EEE-Flash (bottom Memory window) */
/* turn off the EEE record writing */
ftmStat = LaunchFlashCommand(1 ,DISABLE_EEPROM_EMULATION, 0, 0, 0, 0, 0, 0);
if ((ftmStat & (ACCERR|FPVIOL|MGSTAT1|MGSTAT0)))
Error(ftmStat);
/* can still read and write the EEE RAM */
EEE_Int[0] = 0x3003; /* notice how the EEE-FLASH remains unaltered */
/* and read it */
temp = EEE_Int[0];
EEE_Int[1] = 0x4004; /* notice how the tag count (in the DATA1 window - expand the FTM) */
/* remains at one: only one value has to be updated */
/* and read it */
temp = EEE_Int[0];
EEE_Int[2] = 0x5005;
/* and read it */
temp = EEE_Int[0];
EEE_Int[3] = 0x6006;
/* and read it */
temp = EEE_Int[0];
/* the tag counter should be one because only one record needs updating */
temp = FTM.etag;
/* and now back up the data here */
/* enable writes of EEE records */
ftmStat = LaunchFlashCommand(1 ,ENABLE_EEPROM_EMULATION, 0, 0, 0, 0, 0, 0);
if ((ftmStat & (ACCERR|FPVIOL|MGSTAT1|MGSTAT0)))
Error(ftmStat);
/* wait for the EEE store to be completed */
while(FTM.fstat.bit.mgbusy || FTM.etag ){ /* wait for EEE to write*/
} /* notice how only the final value (0x6006) is written to EEE_Flash as this is the latest */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -