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

📄 exflash.c

📁 mega128的bootload, 支持flash(128k)读写,eepron读写,外部扩展rom读写, 主机可发命令提高波特率,可发命令重启.编译环境IAR for AVR4.20
💻 C
字号:
#include "bootloader.h"


#define   AD_INPUT          (DDRA = 0x00)	
#define   AD_OUTPUT      	(DDRA = 0xff)
#define   AD_OUTPUT_D(X)    {DDRA = 0xff; PORTA = X;}
#define   AD_OUT(X)			(PORTA = X)

#define	  SET_CE_FLASH 		(PORTC |= 1 << PC3)
#define	  CLR_CE_FLASH		(PORTC &= ~ (1 << PC3))

#define	  SET_ALE0 			(PORTG |= 1 << PG2)
#define	  CLR_ALE0 			(PORTG &= ~ (1 << PG2))
#define	  SET_ALE1			(PORTC |= 1 << PC4)
#define	  CLR_ALE1	 		(PORTC &= ~ (1 << PC4))

#define	  SET_RD 			(PORTG |= 1 << PG1)
#define	  CLR_RD 			(PORTG &= ~ (1 << PG1))
#define	  SET_WR 			(PORTG |= 1 << PG0)
#define	  CLR_WR        	(PORTG &= ~ (1 << PG0))

#define   ADDR_HIGH(X)      {PORTC &= ~0x07;PORTC |= ((X)&0x07);}
#define   ADDR_HIGH_R      	(PORTC & 0x07)
#define   ALEMIN() {_NOP();_NOP();}
#define   TWMIN()  {_NOP();_NOP();}

#define   SST_ID             	0xBF   				 	/* SST Manufacturer's ID code   */
#define   SST_39VF040        	0xD7    				/* SST 39VF040 device code      */

/*
*********************************************************************************************************
*                                         HALWAIT
*
* Description      :
* Arguments        :
* Returned Values  : none
* Note(s)/Warnings :
*********************************************************************************************************
*/
void halWait(BYTE timeout)
{
    do {
        _NOP();_NOP();_NOP();_NOP();
		_NOP();_NOP();_NOP();_NOP();
		_NOP();_NOP();
    } while (--timeout);
}

/*
*********************************************************************************************************
*                                         PAUSE_1US
*
* Description      :
* Arguments        :
* Returned Values  : none
* Note(s)/Warnings :
*********************************************************************************************************
*/
void Pause_1uS(BYTE delay)
{
  	while(delay--){
    	_NOP();_NOP();_NOP();_NOP();_NOP();
  	}
}


static BYTE Sector_inuse;
/*
*********************************************************************************************************
*                                         FLASH_WRITE
*
* Description      :
* Arguments        :
* Returned Values  : none
* Note(s)/Warnings :
*********************************************************************************************************
*/
static void  Flash_Wrire(WORD addr,BYTE data)
{
	AD_OUTPUT;
	AD_OUT(addr & 0xff);				SET_ALE0; ALEMIN();CLR_ALE0;
	
	AD_OUT((addr >> 8) | Sector_inuse);	SET_ALE1; ALEMIN();CLR_ALE1;

  	CLR_CE_FLASH;   TWMIN();
	AD_OUT(data);
  	CLR_WR;         TWMIN();
  	SET_WR;
  	SET_CE_FLASH ;
}

static void  Flash_Wrire_Cmd(WORD addr,BYTE data)
{
	AD_OUTPUT;
	AD_OUT(addr & 0xff);	SET_ALE0; ALEMIN();CLR_ALE0;
	AD_OUT(addr >> 8);		SET_ALE1; ALEMIN();CLR_ALE1;

  	CLR_CE_FLASH;   TWMIN();
	AD_OUT(data);
  	CLR_WR;         TWMIN();
  	SET_WR;
  	SET_CE_FLASH ;
}

/*
*********************************************************************************************************
*                                        FLASH_SECTOR
*
* Description      :
* Arguments        : none
* Returned Values  :
* Note(s)/Warnings :
*********************************************************************************************************
*/
void  Flash_Sector_Set(BYTE sector)
{
  	ADDR_HIGH(sector>>4);							// 4096 = 2^(12)
	Sector_inuse = sector<<4;
}

/*
*********************************************************************************************************
*                                         FLASH_READ_BYTE
*
* Description      :
* Arguments        :
* Returned Values  :
* Note(s)/Warnings :
*	about 10uS
*********************************************************************************************************
*/
BYTE  Flash_Read_Byte(WORD addr)
{
	BYTE  val;

	AD_OUTPUT;
	AD_OUT(addr&0xff);					SET_ALE0; ALEMIN();CLR_ALE0;
	
	AD_OUT((addr>>8) | Sector_inuse);	SET_ALE1; ALEMIN();CLR_ALE1;

	CLR_CE_FLASH;  TWMIN();
	AD_INPUT;
	CLR_RD;       TWMIN();
	val = PINA;
	SET_RD;
	SET_CE_FLASH;
	
	return val;
}

/*
*********************************************************************************************************
*                                         FLASH_READ_STRING
*
* Description      :
* Arguments        :
* Returned Values  : none
* Note(s)/Warnings :
*********************************************************************************************************
*/
void  Flash_Read_String(WORD addr, char *buf, WORD  n)
{

	CLR_CE_FLASH;
	
	while(n--){
		AD_OUTPUT;
		AD_OUT((addr>>8) | Sector_inuse);	SET_ALE1; TWMIN();CLR_ALE1;
		AD_OUT(addr & 0xff);				SET_ALE0; TWMIN();CLR_ALE0;
		
		AD_INPUT;
		CLR_RD; addr++;
		*buf++  = PINA;
		SET_RD;
	}
	SET_CE_FLASH;
}



/*
*********************************************************************************************************
*                                         FLASH_TOGLE_READY
*
* Description      :
* Arguments        :
* Returned Values  : none
* Note(s)/Warnings :
*********************************************************************************************************
*/
static void Check_Toggle_Ready (WORD Dst)
{
	
	BYTE Loop = 1;
	BYTE PreData;
	BYTE CurrData;
	DWORD TimeOut = 0;

	PreData = Flash_Read_Byte(Dst);
	PreData = PreData & 0x40;
	while ((TimeOut< 0x07FFFFFF) && (Loop))
	{
		CurrData = Flash_Read_Byte(Dst);
		CurrData = CurrData & 0x40;
		if (PreData == CurrData)
				Loop = 0;   			/* ready to exit the while loop */
		PreData = CurrData;
		TimeOut++;
	halWait(1);		
	}
}

/*
*********************************************************************************************************
*                                         FLASH_DATA_POLLING
*
* Description      :
* Arguments        :
* Returned Values  : none
* Note(s)/Warnings :
*********************************************************************************************************
*/
void Check_Data_Polling (WORD Dst,BYTE TrueData)
{
	BYTE Loop = 1;
	BYTE CurrData;
	DWORD TimeOut = 0;

	TrueData = TrueData &  0x80;
	while ((TimeOut< 0x07FFFFFF) && (Loop))
	{
		 CurrData = Flash_Read_Byte(Dst);
		  CurrData = CurrData & 0x80;
		  if (TrueData == CurrData)
			Loop = 0;   /* ready to exit the while loop  */
		  TimeOut++;
		  halWait(1);		
	}
}

/*
*********************************************************************************************************
*                                         FLASH_WRITE_BYTE
*
* Description      :
* Arguments        : none
* Returned Values  : none
* Note(s)/Warnings :
*********************************************************************************************************
*/
void  Flash_Write_Byte(WORD addr,BYTE data)
{
	Flash_Wrire_Cmd(0x5555,0xaa);
	Flash_Wrire_Cmd(0x2AAA,0x55);
	Flash_Wrire_Cmd(0x5555,0xa0);
	Flash_Wrire(addr,data);
	Check_Toggle_Ready(addr);
}

/*
*********************************************************************************************************
*                                         FLASH_WRITE_STRING
*
* Description      :
* Arguments        :
* Returned Values  :
* Note(s)/Warnings :
*********************************************************************************************************
*/
BYTE  Flash_Write_String(WORD addr, char *buf, WORD  n)
{
    	while(n--)
      		Flash_Write_Byte(addr++,*buf++);
    	return 1;
}

/*
*********************************************************************************************************
*                                         FLASH_ERASE_CHIP
*
* Description      :
* Arguments        : none
* Returned Values  :
* Note(s)/Warnings :
*********************************************************************************************************
*/
BYTE  Flash_Erase_Chip(void)
{
  	Flash_Wrire_Cmd(0x5555,0xaa);
  	Flash_Wrire_Cmd(0x2AAA,0x55);
  	Flash_Wrire_Cmd(0x5555,0x80);
  	Flash_Wrire_Cmd(0x5555,0xaa);
  	Flash_Wrire_Cmd(0x2AAA,0x55);
  	Flash_Wrire_Cmd(0x5555,0x10);

  	while(1){
		
    	CLR_CE_FLASH ;  TWMIN();
    	AD_INPUT;
    	CLR_RD;         TWMIN();
   		if((PINA & 0x80) == 0x80) break;
    	SET_RD;         TWMIN();
    	SET_CE_FLASH ;  TWMIN();
  	}
  	SET_RD;         TWMIN();
  	SET_CE_FLASH ;
  	return 1;
}

/*
*********************************************************************************************************
*                                         FLASH_ERASE_SECTOR
*
* Description      :
* Arguments        :
* Returned Values  :
* Note(s)/Warnings :
*********************************************************************************************************
*/
BYTE  Flash_Erase_Sector(BYTE sector)
{
	
  	Flash_Wrire_Cmd(0x5555,0xaa);
  	Flash_Wrire_Cmd(0x2AAA,0x55);
  	Flash_Wrire_Cmd(0x5555,0x80);
  	Flash_Wrire_Cmd(0x5555,0xaa);
  	Flash_Wrire_Cmd(0x2AAA,0x55);

  	Flash_Sector_Set(sector);		
	AD_OUTPUT_D(Sector_inuse);	SET_ALE1; ALEMIN(); CLR_ALE1;
	
  	CLR_CE_FLASH; TWMIN();
  	AD_OUTPUT_D( 0x30);
  	CLR_WR;       TWMIN();
  	SET_WR;       TWMIN();
  	SET_CE_FLASH ;

  	while(1)
  	{
    		CLR_CE_FLASH ;  TWMIN();
    		AD_INPUT;
    		CLR_RD;         TWMIN();
    		if((PINA&0x80) == 0x80) break;
    		SET_RD;         TWMIN();
    		SET_CE_FLASH ;  TWMIN();
  	}
  	SET_RD;         TWMIN();
  	SET_CE_FLASH;
  	return 1;
}
/*
*********************************************************************************************************
*                                         FLASH_READ_ID
*
* Description      :
* Arguments        :
* Returned Values  :
* Note(s)/Warnings :
*********************************************************************************************************
*/
void  Flash_Read_ID(char *p)
{
  	Flash_Sector_Set(0);
  	Flash_Wrire_Cmd(0x5555,0xaa);
  	Flash_Wrire_Cmd(0x2AAA,0x55);
  	Flash_Wrire_Cmd(0x5555,0x90);
	Pause_1uS(1);

  	*p++  = Flash_Read_Byte(0x0000);
  	*p    = Flash_Read_Byte(0x0001);
	

  	Flash_Wrire_Cmd(0x5555,0xaa);
  	Flash_Wrire_Cmd(0x2AAA,0x55);
  	Flash_Wrire_Cmd(0x00,0xF0);
}

⌨️ 快捷键说明

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