📄 flash.c
字号:
/********************************************************************************
*
* Copyright (C) 2007 Freescale Semiconductor, Inc.
* All Rights Reserved
*
* Filename: flash.c
*
* Revision:
*
* Functions: Includes flash routines
*
* Description:
*
* Notes:
*
*********************************************************************************/
/*********************************** Includes ***********************************/
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#include "flash.h" /* include flash driver header file */
/*********************************** Macros ************************************/
/*********************************** Defines ***********************************/
/********************************** Constant ***********************************/
/*********************************** Variables *********************************/
/*********************************** Prototype *********************************/
/*********************************** Function **********************************/
/*******************************************************************************
* Function: Flash_Init
*
* Description: Set the flash clock
*
* Returns: never return
*
* Notes:
*
*******************************************************************************/
void Flash_Init(unsigned char FlashClockDiv)
{
/* Flash clock between 150-200kHz - > 8MHz/(39+1)=200kHz*/
FCDIV = FlashClockDiv;
}
/*******************************************************************************
* Function: Flash_SectorErase
*
* Description: erase a sector of the flash
*
* Returns: Error Code
*
* Notes:
*
*******************************************************************************/
unsigned char Flash_SectorErase(unsigned long *FlashPtr)
{
unsigned char Return = Flash_OK;
/* Allocate space on stack to run flash command out of SRAM */
char localbuf[100];
int (*RunOnStack)(void) = (int(*)(void))localbuf;
memcpy(localbuf, (void*)SpSub, (char*)SpSubEnd - (char*)SpSub);
if(FCDIV_FDIVLD == 1)
{
/* flash is init */
/* wait until FCBEF is set in FSTAT */
while(FSTAT_FCBEF == 0){}
/* check for errors */
if(FSTAT_FACCERR == 1)
{
/* clear error flag */
FSTAT = 0x10;
}
if(FSTAT_FPVIOL == 1)
{
/* clear error flag */
FSTAT = 0x20;
}
/* dummy write to flash */
*FlashPtr = (unsigned long)0x11111111;
/* write command */
FCMD = FlashCmd_SectorErase;
RunOnStack();
/* launch command */
//FSTAT = 0x80;
/* wait for command completion */
//while(FSTAT_FCCF == 0){}
/* check for errors */
if(FSTAT_FACCERR == 1)
{
/* clear error flag */
FSTAT = 0x10;
/* update return value*/
Return |= Flash_FACCERR;
}
if(FSTAT_FPVIOL == 1)
{
/* clear error flag */
FSTAT = 0x20;
/* update return value*/
Return |= Flash_FPVIOL;
}
}
else
{
/* flash is not init */
Return = Flash_NOT_INIT;
}
/* function return */
return Return;
}
/*******************************************************************************
* Function: Flash_ByteProgram
*
* Description: byte program the flash
*
* Returns: Error Code
*
* Notes:
*
*******************************************************************************/
unsigned char Flash_ByteProgram(unsigned long *FlashStartAdd,unsigned long *DataSrcPtr,unsigned long NumberOfBytes)
{
unsigned char Return = Flash_OK;
/* Allocate space on stack to run flash command out of SRAM */
char localbuf[100];
int (*RunOnStack)(void) = (int(*)(void))localbuf;
memcpy(localbuf, (void*)SpSub, (char*)SpSubEnd - (char*)SpSub);
if(FCDIV_FDIVLD == 1)
{
/* flash is init */
/* wait until FCBEF is set in FSTAT */
while(FSTAT_FCBEF == 0){}
/* check for errors */
if(FSTAT_FACCERR == 1)
{
/* clear error flag */
FSTAT = 0x10;
}
if(FSTAT_FPVIOL == 1)
{
/* clear error flag */
FSTAT = 0x20;
}
while((NumberOfBytes) && (Return == Flash_OK))
{
/* write data to flash and increase pointers by 1 */
*FlashStartAdd++ = *DataSrcPtr++;
/* write command */
FCMD = FlashCmd_Program;
RunOnStack();
/* launch command */
//FSTAT = 0x80;
/* wait for command completion */
//while(FSTAT_FCCF == 0){}
/* check for errors */
if(FSTAT_FACCERR == 1)
{
/* clear error flag */
FSTAT = 0x10;
/* update return value*/
Return |= Flash_FACCERR;
}
if(FSTAT_FPVIOL == 1)
{
/* clear error flag */
FSTAT = 0x20;
/* update return value*/
Return |= Flash_FPVIOL;
}
/* decrement byte count */
NumberOfBytes--;
}
}
else
{
/* flash is not init */
Return = Flash_NOT_INIT;
}
/* function return */
return Return;
}
/*******************************************************************************
* Function: SpSub
*
* Description: Execute the Flash write while running out of SRAM
*
* Returns:
*
* Notes:
*
*******************************************************************************/
void SpSub(void)
{
asm(
//Save off registers d0, d1, and d2
link a6,#-12
movem.l d0-d2,(sp)
// MCF_CFM_CFMUSTAT = CBEIF;
mvs.w #128,d0
move.b d0,0xffff9825
// while (!(MCF_CFM_CFMUSTAT & CCIF)){};//wait until execution complete
loop:
mvz.b 0xffff9825,d1
moveq #25,d0
lsl.l d0,d1
moveq #31,d2
lsr.l d2,d1
tst.b d1
beq.s loop
//Restore registers d0, d1, and d2
movem.l (sp),d0-d2
lea 12(sp),sp
unlk a6
);
}
/* Leave this immediately after SpSub */
void SpSubEnd(void){}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -