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

📄 command.c

📁 脱机ISP编程器
💻 C
字号:
/*------------------------------------------------------
Function:	part of Offline Downloader
Author:		avenbbs(8785116@qq.com)
Version:	1.0
Date:		2008-10-1~2008-10-5
--------------------------------------------------------*/
#include <avr/io.h>
#include <stdlib.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <string.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "command.h"
#include "usart.h"
#include "main.h"
#include "spi_soft.h"
//#include "AT45DB041D.h"
#include "AT45.h"
#include "xmodem.h"
#include "isp.h"
#include "common.h"

#define RX_BUF_SIZE 16

unsigned char getfuse_flag=0;
unsigned char getlockbit_flag=0;
unsigned char getprogsize_flag=0;
unsigned char avr_fuse[3]={0xa4,0xd9,0xff};
unsigned char avr_lockbit=0xff;
unsigned char avr_size[2]={3,0};

char msg_fail[] ="Fail!";
char msg_done[] ="Done.";

extern unsigned char buf[];
extern unsigned char section;
extern unsigned char keylock;
extern volatile unsigned char error_flag;
extern unsigned char fuse[3];
extern char fuse_ascii[9];
extern unsigned char chip_id[3];
extern char chip_id_ascii[9];

struct
{
	unsigned char rxbuf[RX_BUF_SIZE];
	unsigned char rxcnt;
	
}com={{0},0};

//volatile unsigned char rxtmp;

#define Ascii2Hex(data)  (data >= '0' && data <= '9')? (data - '0') : \
	((data >= 'A' && data <= 'F')? (data - 'A' + 10) : \
	((data >= 'a' && data <= 'f')? (data - 'a' + 10) : 0))
unsigned char ascii2hex(unsigned char *pBuf)
{
	unsigned char temp;
	unsigned char c;
	
	c = *pBuf++;
	temp = ((unsigned char)Ascii2Hex(c)) << 4;
	c = *pBuf;
	temp += Ascii2Hex(c);
	
	return temp;
}

typedef struct
{
	unsigned char instr[4];
	unsigned char len;
	void (*function)(void);
	char explain[18];
}struct_instr_sheet;

struct_instr_sheet instr_sheet[] PROGMEM=
{
	{"h",1,exe_help,			"[h]Show Help"},
//	{"i",1,exe_info,			"[i]Show Information"},

	{"stp",3,exe_settype,		"[stp]Set Type"},
	
	{"rst",3,exe_reset,			"[rst]Reset"},
	{"ers",3,exe_eraseavr,		"[ers]Erase"},
//	{"eat",3,exe_eraseat45,		"[eat]Erase At45 Chip"},

	{"rid",3,exe_readid,		"[rid]Read ID"},
	{"rfs",3,exe_readfuse,		"[rfs]Read FUSE"},
	{"rlb",3,exe_readlockbit,	"[rlb]Read LOCKBIT"},
	
	{"sfs",3,exe_setfuse,		"[sfs]Set FUSE"},
	{"slb",3,exe_setlockbit,	"[slb]Set LOCKBIT"},
	{"dfl",3,exe_downloadflash,	"[dfl]Down FLASH"},
	{"dee",3,exe_downloadeeprom,"[dee]Down EEPROM"},
	
	{"pfs",3,exe_progfuse,		"[pfs]Prog FUSE"},
	{"plb",3,exe_proglockbit,	"[plb]Prog LOCKBIT"},
	{"pfl",3,exe_progflash,		"[pfl]Prog FLASH"},
	{"pee",3,exe_progeeprom,	"[pee]Prog EEPROM"},
	{"pal",3,exe_progchip,		"[pal]Prog All"},
	
	{"upf",3,exe_updatefirmware,"[upf]Update"},
	
};


unsigned char str_compare(const unsigned char *c, unsigned char *p,
                          unsigned char size)
{
	if (size == 0 || c == NULL || p == NULL)
		return 0;
		
	while (size--)
	{
		if (*c != *p)
			return 0;
			
		c++;
		
		p++;
	}
	
	return (!0);
}

unsigned char pgm_str_compare(const unsigned char *c, unsigned char *p,
                              unsigned char size)
{
	if (size == 0 || c == NULL || p == NULL)
		return 0;
		
	while (size--)
	{
		if (pgm_read_byte(c) != *p)
			return 0;
			
		c++;
		
		p++;
	}
	
	return (!0);
}


void com_match(void)
{
	unsigned char i;
	//tmp=strlen(com.rxbuf);
	
	for (i=0;i<sizeof(instr_sheet)/sizeof(struct_instr_sheet);i++)
	{
		if (com.rxcnt<pgm_read_byte(&instr_sheet[i].len))
		{
			continue;
		}
		
		if (pgm_str_compare(&instr_sheet[i].instr[0], &com.rxbuf[0],
		                    pgm_read_byte(&instr_sheet[i].len)))
		{
			//instr_sheet[i].function();
			//command execute...
			(*((void(*)(void))pgm_read_word(&instr_sheet[i].function)))();
			break;
		}
		
	}
	
	if (i>=sizeof(instr_sheet)/sizeof(struct_instr_sheet))
		sendstr("Invalid instruction!");
}

void get_data(void)
{
	if ((getfuse_flag==1)&&(com.rxcnt==8)&&(com.rxbuf[2]==0x20)&&
	        (com.rxbuf[5]==0x20))
	{
		avr_fuse[0]=ascii2hex(&com.rxbuf[0]);
		avr_fuse[1]=ascii2hex(&com.rxbuf[3]);
		avr_fuse[2]=ascii2hex(&com.rxbuf[6]);
		eeprom_busy_wait();
		eeprom_write_byte(FUSE_ADDRESS+section*3,avr_fuse[0]);
		eeprom_busy_wait();
		eeprom_write_byte(FUSE_ADDRESS+1+section*3,avr_fuse[1]);
		eeprom_busy_wait();
		eeprom_write_byte(FUSE_ADDRESS+2+section*3,avr_fuse[2]);
		eeprom_busy_wait();
		hex2send(eeprom_read_byte(FUSE_ADDRESS+section*3));
		eeprom_busy_wait();
		hex2send(eeprom_read_byte(FUSE_ADDRESS+1+section*3));
		eeprom_busy_wait();
		hex2send(eeprom_read_byte(FUSE_ADDRESS+2+section*3));
		sendstr(msg_done);
	}
	
	else if ((getlockbit_flag==1)&&(com.rxcnt==2))
	{
		avr_lockbit=ascii2hex(&com.rxbuf[0]);
		eeprom_busy_wait();
		eeprom_write_byte(LOCKBIT_ADDRESS+section,avr_lockbit);
		eeprom_busy_wait();
		hex2send(eeprom_read_byte(LOCKBIT_ADDRESS+section));
		sendstr(msg_done);
	}
	
	else if ((getprogsize_flag==1)&&(com.rxcnt==5)&&(com.rxbuf[2]==0x20))
	{
		avr_size[0]=ascii2hex(&com.rxbuf[0]);
		avr_size[1]=ascii2hex(&com.rxbuf[3]);
		eeprom_busy_wait();
		eeprom_write_byte(SIZE_ADDRESS+section*2,avr_size[0]);
		eeprom_busy_wait();
		eeprom_write_byte(SIZE_ADDRESS+1+section*2,avr_size[1]);
		eeprom_busy_wait();
		hex2send(eeprom_read_byte(SIZE_ADDRESS+section*2));
		eeprom_busy_wait();
		hex2send(eeprom_read_byte(SIZE_ADDRESS+1+section*2));
		sendstr(msg_done);
	}
	
	else
		sendstr("Invalid Value!");
		
	getfuse_flag=0;
	
	getlockbit_flag=0;
	
	getprogsize_flag=0;
}

void com_process(unsigned char rxtmp)
{
	//unsigned char rxtmp;
	//rxtmp=usart_rcv();
	if (rxtmp==0x08)
	{
		if (com.rxcnt>0)
		{
			com.rxcnt--;
			usart_tsmt(0x08);
			usart_tsmt(0x20);
			usart_tsmt(0x08);
		}
	}
	
	else if (rxtmp==0x0d)
	{
		usart_tsmt(0x0d);
		usart_tsmt(0x0a);
		//command processing...
		
		if (getfuse_flag||getlockbit_flag||getprogsize_flag)get_data();
		else com_match();
		
		com.rxcnt=0;
	}
	
	else
	{
		if (com.rxcnt>=RX_BUF_SIZE)com.rxcnt=0;
		else
		{
			com.rxbuf[com.rxcnt++]=rxtmp;
			usart_tsmt(rxtmp);
		}
	}
}

//---------function----------------

void exe_help(void)
{
	sendstr("");
	sendstr("Instruction List");
	sendstr("----------------");
	unsigned char i;
	
	for (i=0;i<sizeof(instr_sheet)/sizeof(struct_instr_sheet)-1;i++)
	{
		pgm_sendstr(&instr_sheet[i].explain[0]);
	}
	
	sendstr(msg_done);
//	DF_read_page(0);
//	for(int i=0;i<512;i++)usart_tsmt(buf[i]);
}

/*void exe_info(void)
{
	//pgm_sendstr(&instr_sheet[1].explain[0]);
	sendstr("aven.mail@163.com 2008.10.1");
	sendstr(msg_done);
}*/

void exe_settype(void)
{
	//pgm_sendstr(&instr_sheet[2].explain[0]);
	getprogsize_flag=1;
	sendstr("SIZE:[FLASH EEPROM](00=1K...07=128K,0A=512B)");
}

void exe_reset(void)
{
	//pgm_sendstr(&instr_sheet[3].explain[0]);
	if (reset_avr())sendstr(msg_fail);
	else sendstr(msg_done);
}

void exe_eraseavr(void)
{
	//pgm_sendstr(&instr_sheet[4].explain[0]);
	if (erase_chip()!=0)sendstr(msg_fail);
	else sendstr(msg_done);
	
}

/*void exe_eraseat45(void)
{
	pgm_sendstr(&instr_sheet[5].explain[0]);
}*/

void exe_readid(void)
{
	//pgm_sendstr(&instr_sheet[6].explain[0]);
	if (read_id()!=0)sendstr(msg_fail);
	else
	{
		sendstr(&chip_id_ascii[0]);
		sendstr(msg_done);
	}
}

void exe_readfuse(void)
{
	//pgm_sendstr(&instr_sheet[7].explain[0]);
	if (read_fuse()!=0)sendstr(msg_fail);
	else
	{
		sendstr(&fuse_ascii[0]);
		sendstr(msg_done);
	}
}

void exe_readlockbit(void)
{
	//pgm_sendstr(&instr_sheet[8].explain[0]);
	unsigned char tmp;
	tmp=read_lockbit();
	
	if (tmp!=1)
	{
		hex2send(tmp);
		sendstr(msg_done);
	}
	
	else sendstr(msg_fail);
}

void exe_setfuse(void)
{
	//pgm_sendstr(&instr_sheet[9].explain[0]);
	getfuse_flag=1;
	sendstr("FUSE VALUE:[LOW HIGH EXT]");
	
}

void exe_setlockbit(void)
{
	//pgm_sendstr(&instr_sheet[10].explain[0]);
	getlockbit_flag=1;
	sendstr("LOCKBIT:");
}

void exe_downloadflash(void)
{
	//pgm_sendstr(&instr_sheet[11].explain[0]);
	cli();
	xmodem2df(section*256);
	//hex2send(section);
	
	sei();
}

void exe_downloadeeprom(void)
{
	//pgm_sendstr(&instr_sheet[12].explain[0]);
	cli();
	xmodem2df(3840+section*8);
	sei();
}

void exe_progfuse(void)
{
	
	//pgm_sendstr(&instr_sheet[13].explain[0]);
	eeprom_busy_wait();
	avr_fuse[0]=eeprom_read_byte(FUSE_ADDRESS+section*3);
	eeprom_busy_wait();
	avr_fuse[1]=eeprom_read_byte(FUSE_ADDRESS+1+section*3);
	eeprom_busy_wait();
	avr_fuse[2]=eeprom_read_byte(FUSE_ADDRESS+2+section*3);
	
	if (write_fuse(avr_fuse[0],avr_fuse[1],avr_fuse[2]))sendstr(msg_fail);
	else sendstr(msg_done);
}

void exe_proglockbit(void)
{
	
	//pgm_sendstr(&instr_sheet[14].explain[0]);
	eeprom_busy_wait();
	avr_lockbit=eeprom_read_byte(LOCKBIT_ADDRESS+section);
	
	if (write_lockbit(avr_lockbit))sendstr(msg_fail);
	else sendstr(msg_done);
}

void exe_progflash(void)
{
	if(error_flag==1)
	{
		error_flag=0;
		LED_2_ON();
		keylock=0;
	}
	//pgm_sendstr(&instr_sheet[15].explain[0]);
	unsigned int flash_size=1;
	unsigned char tmp=3;
	eeprom_busy_wait();
	
	tmp=eeprom_read_byte(SIZE_ADDRESS+section*2);
	
	unsigned char avr_page_size=2;
	
	if (tmp<=5)avr_page_size=1;
	
	if (tmp<=3)avr_page_size=0;
	
	for (unsigned char t1=0;t1<tmp+1;t1++)flash_size*=2;
	
	keylock=1;
	
	cli();
	
	sendstr("Erase Chip..");
	
	exe_eraseavr();
	
	sendstr("Prog FLASH..");
	
	ispConnect();
	
	if (ispEnterProgrammingMode())
	{
		sendstr(msg_fail);
		error_flag=1;
		//(*((void(*)(void))(0x0000UL)))();
	}
	
	else
	{
		LED_2_OFF();
		
		unsigned int i;
		
		for (i=0;i<flash_size;i++)
		{
			static unsigned char led_flag=1;
			
			if (led_flag)
			{
				LED_1_ON();
				usart_tsmt(0x2e);
			}
			
			else LED_1_OFF();
			
			led_flag=!led_flag;
			
			DF_read_page(i+section*256);
			
			if (error_flag)
			{
				sendstr(msg_fail);
				return;
			}
			
			else pro_page_1by1(avr_page_size,i);
		}
		
		ispDisconnect();

		error_flag=0;
		keylock=0;
		sendstr(msg_done);
		
	}
	LED_1_OFF();
	LED_2_ON();
	sei();
	
}

void exe_progeeprom(void)
{
	
	//pgm_sendstr(&instr_sheet[16].explain[0]);
	unsigned char eeprom_size=1,tmp;
	eeprom_busy_wait();
	//unsigned int size_address_tmp=SIZE_ADDRESS+1+section*2;
	tmp=eeprom_read_byte(SIZE_ADDRESS+1+section*2);
	
	if (tmp==0x0a)eeprom_size=2;
	else
		for (unsigned char t1=0;t1<tmp+2;t1++)eeprom_size*=2;
		
	keylock=1;
	
	cli();
	
	//erase_chip();
	ispConnect();
	
	if (ispEnterProgrammingMode())
	{
		sendstr(msg_fail);
		error_flag=1;
		//(*((void(*)(void))(0x0000UL)))();
	}
	
	else
	{
		LED_2_OFF();
		
		unsigned char i;
		
		for (i=0;i<eeprom_size;i++)
		{
			static unsigned char led_flag=1;
			
			DF_read_page(3840+section*8+i);
			
			//pro_page_1by1(0,i);
			
			for (unsigned int j=0;j<256;j++)
			{
				if (led_flag&&j%32==0)
				{
					LED_1_ON();
					usart_tsmt(0x2e);
				}
				
				else LED_1_OFF();
				
				led_flag=!led_flag;
				
				ispWriteEEPROM(j+i*256, buf[j]);
			}
		}
		ispDisconnect();
		keylock=0;
		error_flag=0;
		sendstr(msg_done);
	}
	LED_1_OFF();
	LED_2_ON();
	
	sei();
	
		
}

void exe_progchip(void)
{
	//pgm_sendstr(&instr_sheet[17].explain[0]);
	
	sendstr("Prog FUSE..");
	_delay_ms(100);
	exe_progfuse();
	
	//sendstr("ProgFLASH..");
	//_delay_ms(100);
	exe_progflash();
	
	if (KEY_6==0x00)
	{
		sendstr("Prog EEPROM..");
		//_delay_ms(100);
		exe_progeeprom();
	}
	
	sendstr("Prog LOCKBIT..");
	
	_delay_ms(100);
	exe_proglockbit();
}

void exe_updatefirmware(void)
{
	(*((void(*)(void))(0x0e00UL)))();
}

⌨️ 快捷键说明

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