⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 progflash.c

📁 在freescale 的ne64上开发的源代码
💻 C
字号:

/******************************************************************************
													Copyright (c) Motorola 2001
File Name		 :	$RCSfile: ProgFlash.c,v $		
	
Engineer		 :	$Author: r27624 $

Location		 :	EKB

Date Created	 :	05/06/2001

Current Revision :	$Revision: 1.0 $

Notes	:					

*******************************************************************************
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 unintended or
unauthorized application, Buyer shall idemnify 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.
******************************************************************************/

/************************* System Include Files ******************************/

/************************* Project Include Files *****************************/
#include "datatypes.h"
#include "stdtypes.h"
/*#include "mcucfg.h"*/
#include "MC9S12NE64.h"
#include "s12_fectlne64.h"

//#include "debug.h"
#include "ne64debug.h"

extern DoOnStack(UINT32 * address); 

/************************* typedefs ******************************************/

/************************* #Defines ******************************************/
#ifndef ALIGNED_WORD_MASK
#define ALIGNED_WORD_MASK	0x0001
#endif

#ifndef PASS
#define PASS				   1
#endif

#ifndef FAIL
#define FAIL				   0
#endif

/************************* Global Variables **********************************/
#if 0
static tFLASH flash @(REG_BASE + 0x100);
#endif
#if 1
static tFLASH *flash=(tFLASH *)(REG_BASE + 0x100);

void Flash_Init(UINT32 oscclk)
{
	UINT8 fclk_val;
	UINT32 temp;
	/* Next, initialize FCLKDIV register to ensure we can program/erase */
 	 temp = oscclk;
  	if (oscclk >= 12000) 
	{
	    fclk_val = oscclk/8/200 - 1; /* FDIV8 set since above 12MHz clock */
	    flash->fclkdiv.byte = flash->fclkdiv.byte | fclk_val | FDIV8;
	} 
  	else 
	{
  		fclk_val = oscclk/8/200 - 1;
		flash->fclkdiv.byte = flash->fclkdiv.byte | fclk_val;  
	 }
 	 flash->fprot.byte = 0xFF; /* Disable all protection (only in special modes)*/
	flash->fstat.byte = flash->fstat.byte | (PVIOL|ACCERR);/* Clear any errors  */
}

//*****************************************************************************
//* Function Name	: Flash_Write_Word
//* Description 	: Program a given Flash location using address and data
//* 			      passed from calling function. 
//*
//*****************************************************************************
UINT8 Flash_Write_Word(UINT16* far_address,/*UINT32 data*/UINT16 data)
{
	UINT16* address;
	address = (UINT16*)far_address;                        // strip page off
	
	flash->fstat.byte = (ACCERR | PVIOL);                         //   clear errors
	if((UINT16)address & 0x0001) {return Flash_Odd_Access;} // Aligned word?
	if(*far_address != 0xFFFF) {return Flash_Not_Erased;}         // Is it erased?
	(*address) = data;	        // Store desired data to address being programmed 

	flash->fcmd.byte = PROG;	                 // Store programming command in FCMD
	(void)DoOnStack(far_address);            // just passed for PPAGE
	if (flash->fstat.bit.accerr) {return Access_Error;}
	if (flash->fstat.bit.pviol) {return Protection_Error;}

	while(flash->fstat.bit.ccif != 1);/*new add*/		
	
	return 1;
}
//*****************************************************************************
//* Function Name	: Flash_Write_Block
//* Description 	: Program a range of Flash location using address and data
//* 			      pointers passed from calling function. 
//*
//*****************************************************************************
UINT8 Flash_Write_Block(UINT16 * address_source,UINT16 * far_address_destination,UINT16 count)
{
	UINT16 i; // long supports > 64K words
	UINT8 ret_val;

	for (i = 0;i<count;i++)
	{
		ret_val = Flash_Write_Word(far_address_destination++, *address_source++);
		if (ret_val == Access_Error) {return Access_Error;}
		if (ret_val == Protection_Error) {return Protection_Error;}
	}
	
	while(flash->fstat.bit.ccif != 1);/*new add*/
	
	return 1;
}
//*****************************************************************************
//* Function Name	: Flash_Erase_Sector
//* Description 	: Erases a given Flash sector using address
//* 			      passed from calling function. 
//*
//*****************************************************************************
UINT8 Flash_Erase_Sector(UINT16* far_address)
{
	UINT16* address;
	
	address = (UINT16*)far_address;                        // strip page off	
	if((UINT16)address & 0x0001) {return Flash_Odd_Access;} // Aligned word?
	if((UINT16)address % Flash_Sector_Size !=0) {return Not_StartofSector_Error;}
	flash->fstat.byte = (ACCERR | PVIOL);                         //   clear errors
	(*address) = 0xFFFF;	/* Dummy store to page to be erased */

	flash->fcmd.byte=ERASE;
	(void)DoOnStack(far_address);
	if (flash->fstat.bit.accerr) {return Access_Error;}
	if (flash->fstat.bit.pviol) {return Protection_Error;}
	
	while(flash->fstat.bit.ccif != 1);/*new add*/	
	
	return 1;
}
//*****************************************************************************
//* Function Name	: Flash_Erase_Block
//* Description 	: Erases a range of Flash sectors using address
//* 			      pointers passed from calling function. 
//*
//*  S12 have max 64 pag,e and max of 32 sectors per page (800 total sectors)
//*****************************************************************************
UINT8 Flash_Erase_Block(UINT16 * start_address,UINT16* end_address)
{
	UINT16 i; 
	UINT16 count; 
	UINT16 address;
	UINT8 ret_val;

	count = ((((UINT16)end_address)-((UINT16)start_address))/Flash_Sector_Size)+1;
	address = (UINT16)start_address;

	for (i = 0;i < count;i++)
	{
		ret_val = Flash_Erase_Sector((UINT16*)address);
		if (ret_val == Access_Error) {return Access_Error;}
		if (ret_val == Protection_Error) {return Protection_Error;}
		address = address+Flash_Sector_Size;
	}
	
	while(flash->fstat.bit.ccif != 1);/*new add*/		
	
	return 1;
}

#endif








⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -