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

📄 progeeprom.c

📁 Code Warrior 4.7 Target : MC9S12DG128B Crystal: 16.000Mhz busclock: 8.000MHz pllclock:16.000MHz
💻 C
字号:
/******************************************************************************
													Copyright (c) LongQiu s.&t. 2008
这是光盘带的整理后的文件,没有测试过				

*******************************************************************************/
#include <mc9s12dg128.h>    /* derivative information */

typedef unsigned char	UINT8;	
typedef unsigned int	UINT16;	
typedef unsigned long	UINT32;
typedef signed char		INT8;		 
typedef int				    INT16;
typedef long int		  INT32;


#ifndef FALSE
#define FALSE 0
#endif

#ifndef TRUE
#define TRUE 1
#endif

#ifndef	ALIGNED_WORD_MASK
#define ALIGNED_WORD_MASK	0x0001
#endif

#ifndef PASS
#define PASS		1
#endif

#ifndef FAIL
#define FAIL		0
#endif

/* Oscillator frequency in kHz */
#define	OSCCLK_FREQ_KHZ		16000L		/* must have "L" appended for F/ECLK_PRESCALER macro */

/* Bus frequency in kHz */
#define BUSCLK_FREQ_KHZ		OSCCLK_FREQ_KHZ*(SYNR+1)/(REFDV+1)		
/* register base address */
#define REG_BASE			0x0000

static tEEPROM 	eeprom 		@(REG_BASE + 0x110);  

typedef union uECLKDIV
  {
  UINT8	byte;
  struct
    {
    UINT8 ediv	 :6;		/*clk divider */
    UINT8 ediv8	 :1;		/*clk /8 prescaler enable */
    UINT8 edivld :1;		/*clock divider loaded flag */
    }bit;
  }tECLKDIV;

typedef union uECNFG
  {
  UINT8	byte;
  struct
    {
    UINT8		:4;		/*not used */
    UINT8 eswai	:1;		/*eeprom stopped in wait mode */
    UINT8		:1;		/*not used */
    UINT8 ccie	:1;		/*command complete interrupt enable */
    UINT8 cbeie	:1;		/*command buffer empty interrupt enable	*/
    }bit;
  }tECNFG;

typedef union uEPROT
  {
  UINT8	byte;
  struct
    {
    UINT8 ep		:3;		/*protection block size: (ep+1)*64 bytes */
    UINT8 epdis		:1;		/*protection disable */
    UINT8 			:3;		/*contain value of equivalent bits in protection byte */
    UINT8 eopen		:1;		/*open block for program/erase */
    }bit;
  }tEPROT;

typedef union uESTAT
  {
  UINT8	byte;
  struct
    {
    UINT8 		 :2;		/*not used */
    UINT8 blank	 :1;		/*blank verify flag */
    UINT8 		 :1;		/*not used */
    UINT8 accerr :1;		/*access error flag */
    UINT8 pviol	 :1;		/*protection violation flag */
    UINT8 ccif	 :1;		/*command complete interrupt flag */
    UINT8 cbeif	 :1;		/*command buffer empty interrupt flag */
    }bit;
  }tESTAT;

typedef union uECMD
  {
  UINT8	byte;
  struct
    {
    UINT8 mass	:1;		/*mass erase enable	*/
	UINT8 		:1;		/*not used */
    UINT8 erver	:1;		/*erase verify enable */
	UINT8 		:2;		/*not used */
    UINT8 prog	:1;		/*word programming */
    UINT8 erase	:1;		/*erase control */
	UINT8 		:1;		/*not used */
    }bit;
  }tECMD;

typedef struct						/*eeprom datastructure */
  {
  volatile tECLKDIV		eclkdiv;	/*eeprom clock divider register */
		   UINT8		rsvee1[2];	/*reserved */
  volatile tECNFG		ecnfg;		/*eeprom configuration register */
  volatile tEPROT		eprot;		/*eeprom protection register */
  volatile tESTAT		estat;		/*eeprom status register */
  volatile tECMD		ecmd;		/*eeprom command buffer & status register */
           UINT8		rsvee2[5];	/*reserved */
  }tEEPROM;

#define EDIV8	0x40	/*bit masks	*/
#define EDIVLD	0x80

#define ESWAI	0x10	/*bit masks	*/
#define CCIE	0x40	
#define CCBIE	0x80

#define EP0		0x01	/*bit masks	*/
#define EP1		0x02
#define EP2		0x04	
#define EP		0x07	/*ep block mask */
#define EPDIS   0x08
#define EOPEN	0x80

#define BLANK	0x04	/*bit masks	*/
#define ACCERR	0x10
#define PVIOL 	0x20
#define CCIF	0x40
#define CBEIF	0x80

#define MASS	0x01	/*bit masks	*/
#define ERVER	0x04
#define PROG	0x20
#define ERASE	0x40

   /* Macro that generates the EEPROM clock precaler as per data book. 
      If the crystal frequency is above 12.8MHz then the EDIV bit must 
      be set, this divides the OSCCLK frequency by 8 before the prescaler. */
#if (OSCCLK_FREQ_KHZ > 12800)  
   /* This macro calculates the prescaler, but also implements the EDIV8 bit */
#if ((OSCCLK_FREQ_KHZ * (BUSCLK_FREQ_KHZ + 200)) % (1600 * BUSCLK_FREQ_KHZ) == 0)
#define EECLK_PRESCALER ((OSCCLK_FREQ_KHZ * (BUSCLK_FREQ_KHZ + 200) / (1600 * BUSCLK_FREQ_KHZ)) + EDIV8 - 1)
#else
#define EECLK_PRESCALER ((OSCCLK_FREQ_KHZ * (BUSCLK_FREQ_KHZ + 200) / (1600 * BUSCLK_FREQ_KHZ)) + EDIV8)
#endif /* OSCCLK_FREQ_KHZ * (BUSCLK_FREQ_KHZ + 200) % (1600 * BUSCLK_FREQ_KHZ) == 0 */
   /* Make sure EECLK is within specified range. */
#define EECLK_FREQ_KHZ (OSCCLK_FREQ_KHZ / (8 * (1 + EECLK_PRESCALER - EDIV8))) 
#if ((EECLK_FREQ_KHZ < 150) || (EECLK_FREQ_KHZ > 200) || (EECLK_PRESCALER > 0x7F))
#error EEPROM prescaler or clock out of range.
#endif /* Incorrect EECLK frequency. */

#else
   /* This macro calculates the prescaler. */
#if ((OSCCLK_FREQ_KHZ * (BUSCLK_FREQ_KHZ + 200)) % (200 * BUSCLK_FREQ_KHZ) == 0)
#define EECLK_PRESCALER ((OSCCLK_FREQ_KHZ * (BUSCLK_FREQ_KHZ + 200) / (200 * BUSCLK_FREQ_KHZ)) - 1)
#else
#define EECLK_PRESCALER (OSCCLK_FREQ_KHZ * (BUSCLK_FREQ_KHZ + 200) / (200 * BUSCLK_FREQ_KHZ))
#endif /* OSCCLK_FREQ_KHZ * (BUSCLK_FREQ_KHZ + 200) % (200 * BUSCLK_FREQ_KHZ) == 0 */ 
   /* Make sure ECLK is within specified range. */
#define EECLK_FREQ_KHZ (OSCCLK_FREQ_KHZ / (1 + EECLK_PRESCALER)) 
#if ((EECLK_FREQ_KHZ < 150) || (EECLK_FREQ_KHZ > 200) || (EECLK_PRESCALER > 0x3F))
#error EEPROM prescaler or clock out of range.
#endif /* Incorrect EECLK frequency. */
#endif /* OSCCLK_FREQ_KHZ > 12800 */

void  ConfigECLKDIV(void);
UINT8 ProgEeprom(UINT16*, UINT16*, UINT16);


/******************************************************************************
Function Name	:	ConfigECLKDIV
Engineer		:	r27624
Date			:	17/9/2001

Arguments		:	none
                  
Return			:	none

Notes			:	This function configures the EEPROM clock prescaler  
                  	in preparation for programming.
******************************************************************************/
void ConfigECLKDIV(void)
{
	if(eeprom.eclkdiv.bit.edivld == 0)
	{															/* configure EEPROM clock prescaler */
		eeprom.eclkdiv.byte  = (UINT8)EECLK_PRESCALER; 	
	}
	return;
}

/******************************************************************************
Function Name	:	ProgEeprom
Engineer		:	r27624
Date			:	28/06/2001

Arguments		:	progAdr		Pointer to the start of the destination 
									EEPROM location to be programmed
                                    
					bufferPtr	Pointer to the start of the source data 
                                    
					size        Number of WORDS to be programmed
                  
Return			:	status      FAIL: 
                           		if progAdr does not point to an aligned word, or
                           		the ACCERR bit is set during programming sequence, or
								the PVIOL bit is set during programming sequence.

                          		PASS:
                           		if not FAIL.

Notes			:	This function does not check if the EEPROM is erased.
              		This function does not verify that the data has been 
					sucessfully programmed.
******************************************************************************/
UINT8 ProgEeprom(UINT16* progAdr, UINT16* bufferPtr, UINT16 size)
{

	if(((UINT16)progAdr & ALIGNED_WORD_MASK) != 0)				/* Check for aligned word */
	{
		return(FAIL);
	}
																/* Clear error flags */	
	eeprom.estat.byte = (ACCERR | PVIOL);
																/* Word to program? */
	while(size != 0)
	{															/* Is command buffer empty? */
		if(eeprom.estat.bit.cbeif == 1)
		{
																/* Latch data and address */
			*progAdr++ = *bufferPtr++;				
																/* Configure prog command */	
			eeprom.ecmd.byte = PROG;
																/* Launch the command */
			eeprom.estat.byte = CBEIF;
																/* Was the access error flag set? */
																/* Was the protection violation error flags set */
			if((eeprom.estat.bit.accerr == 1) || 
			   (eeprom.estat.bit.pviol == 1))
			{
				return(FAIL);
			}
																/* next word */  
			size--;
		}

	}
																/* Wait for last command to finish */
	while(eeprom.estat.bit.ccif != 1)								
	{
	}
																/* finished, no errors */
	return(PASS);
}

⌨️ 快捷键说明

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