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

📄 usbasp.cpp

📁 一个很好的基于USB开发的VC++资料.一个很好的基于USB开发的VC++资料
💻 CPP
字号:
// USBASP.cpp: implementation of the CUSBASP class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "USBISP.h"
#include "USBASP.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif



//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CUSBASP::CUSBASP()
{

}

CUSBASP::~CUSBASP()
{

}

BOOL CUSBASP::chipInit()
{
	int info;
	info=m_usbop.usbaspOpen();
	if (info==0)
	{ //"USB设备已打开"
		m_usbop.usbaspInitialize();
		return TRUE;
	}
	else if (info==1)
	{
		AfxMessageBox("没检到USBISP下载器!");
		return FALSE;
	}
	else if (info==2)
	{
		AfxMessageBox("打开USB设备失败!");
		return FALSE;
	}
	return FALSE;
}

BOOL CUSBASP::chipEntryProgram()
{ //进入usbasp编程模式
	//uchar n[4];

	if (m_usbop.usbaspCheck()!=0)
	{ //"USBASP 未连接或连接错误!"
		return FALSE;
	}
	m_usbop.usbaspConnect();
	if(m_usbop.usbaspEnterProMode()) return TRUE;
	else return FALSE;
}

BOOL CUSBASP::chipErasure()
{ //擦除命令
	uchar n[4];

	if (m_usbop.usbaspCheck()!=0)
	{ //"USBASP 未连接或连接错误!"
		return FALSE;
	}
	//usbasp_Connect(); 再连接无法擦除,因为进入编程模式环境被破坏
	//发送命令
	n[0]=0xac;
	n[1]=0x80;
	m_usbop.usbaspCmd(n, n);
	Sleep(200);
	if (n[2]!=0x00)
	{ //"擦除错误!"
		return FALSE;
	}
	else
	{ //"擦除成功!"
		return TRUE;
	}
}

BOOL CUSBASP::chipSignature(uchar *p)
{ //读芯片标识字,参数p送过来为3个字节的数组
	uchar n[4];
	uchar i;

	if (m_usbop.usbaspCheck()!=0)
	{ //"USBASP 未连接或连接错误!"
		return FALSE;
	}

	for (i=0;i<3;i++)
	{
		//发送命令
		if(m_ChipType.FType==1) //S5x类型
		{
			n[0]=0x28;
			n[1]=i;
			n[2]=0;
		}
		else if(m_ChipType.FType==0) //AVR类型
		{
			n[0]=0x30;
			n[1]=0;
			n[2]=i;			
		}

		m_usbop.usbaspCmd(n, n);

		if(m_ChipType.FType==0)
		{
			if (n[2]!=0x0)
			{ //"读标识字错误!"
				return FALSE;
			}
		}

		p[i]=n[3];

	}
	return TRUE;
}

BOOL CUSBASP::chipCrystal(uchar *p)
{ //读芯片效准字节,参数p送过来为4个字节的数组
	uchar n[4];
	uchar i;

	if (m_usbop.usbaspCheck()!=0)
	{ //"USBASP 未连接或连接错误!"
		return FALSE;
	}

	for (i=0;i<4;i++)
	{
		//发送命令
		n[0]=0x38;
		n[1]=0x0;
		n[2]=i;
		m_usbop.usbaspCmd(n, n);
		if (n[2]!=0x0)
		{ //"读效准字节错误!"
			return FALSE;
		}
		p[i]=n[3];
	}
	return TRUE;
}

BOOL CUSBASP::chipRWLock(uchar rw,uchar *s)
{  //读写芯片的锁定位 rw 1 read 0 write
	uchar temp[4];

	if (m_usbop.usbaspCheck())
	{ //0 连接正常
		return FALSE;
	}
	memset(temp, 0, sizeof(temp));
	if (rw==3)
	{ //读锁定位方式0 Attiny10加密位在第二个参数的00000xx0处xx
		temp[0]=0x58;temp[1]=0x00;temp[2]=0x00;temp[3]=0x00;
		m_usbop.usbaspCmd(temp, temp);
		*s=(temp[3]>>1);
	}
	else if (rw==2)
	{ //写锁定位方式0 Attiny10加密位在第二个参数的00000xx0处xx
		temp[0]=0xAC;temp[1]=(0xf9|((*s)<<1));temp[2]=0x00;temp[3]=0x00;
		m_usbop.usbaspCmd(temp, temp);
		*s=temp[3];
	}
	else if (rw==1)
	{ //read Atmega8之类加密位在第四个参数xxxxxxxx处
		temp[0]=0x58;temp[1]=0x00;temp[2]=0x00;temp[3]=0x00;
		m_usbop.usbaspCmd(temp, temp);
		*s=temp[3];
	}
	else if(rw==0)
	{ //write
		temp[0]=0xAC;temp[1]=0xE0;temp[2]=0x00;temp[3]=*s;
		m_usbop.usbaspCmd(temp, temp);
		*s=temp[3];
	}
	else if(rw==5)
	{ //read S52之类加密位在第四个参数xxxxxxxx处,第一参数为0x24
		temp[0]=0x24;temp[1]=0x00;temp[2]=0x00;temp[3]=0x00;
		m_usbop.usbaspCmd(temp, temp);
		*s=temp[3];
	}
	else if(rw==4)
	{//write S52加密位在第二参数
		temp[0]=0xAC;temp[1]=*s;temp[2]=0x00;temp[3]=0x00;
		m_usbop.usbaspCmd(temp, temp);
		*s=temp[3];
	}
	else
	{
		return FALSE;
	}
	return TRUE;
}

BOOL CUSBASP::chipRWFuse(uchar rw,uchar hl,uchar *s)
{ //读写芯片熔丝位  rw: 1 read  0 write  hl: 1 high fuse  0 low fuse 2 ext fuse
	uchar temp[4];

	if (m_usbop.usbaspCheck())
	{ //0 连接正常
		return FALSE;
	}
	if(rw==1)
	{ //read
		switch(hl)
		{
			case 0:   //low fuse
			temp[0]=0x50;temp[1]=0x00;temp[2]=0x00;temp[3]=0x00;
			break;
			case 1:   //high fuse
			temp[0]=0x58;temp[1]=0x08;temp[2]=0x00;temp[3]=0x00;
			break;
			case 2:   //ext  fuse
			temp[0]=0x50;temp[1]=0x08;temp[2]=0x00;temp[3]=0x00;
			break;
		}
	}
	else if(rw==0)
	{ //write
		switch(hl)
		{
			case 0:   //low fuse
			temp[0]=0xAC;temp[1]=0xA0;temp[2]=0x00;temp[3]=*s;
			break;
			case 1:   //high fuse
			temp[0]=0xAC;temp[1]=0xA8;temp[2]=0x00;temp[3]=*s;
			break;
			case 2:   //ext  fuse
			temp[0]=0xAC;temp[1]=0xA4;temp[2]=0x00;temp[3]=*s;
			break;
		}
	}
	else
	{
		return FALSE;
	}
	m_usbop.usbaspCmd(temp, temp);
	*s=temp[3];
	return TRUE;
}

void CUSBASP::chipSetSpeed(int ispSCK)
{
	uchar res[4];
	uchar cmd[4];

	if (m_usbop.usbaspCheck()!=0)
	{ //check use connect;
		return;
	}
	memset(cmd, 0, sizeof(cmd));
	memset(res, 0, sizeof(res));

	cmd[0] = ispSCK;
	m_usbop.usbaspTransmit(1,USBASP_FUNC_SPEED, cmd, res, sizeof(res));
	Sleep(10);
}

BOOL CUSBASP::chipProcOver()
{
	m_usbop.usbaspDisconnect();
	return TRUE;
}

BOOL CUSBASP::chipPageWrite(int flashoreeprom,unsigned char *buf,int page_size, int n_bytes)
{ //写页 flashoreeprom=0 write flash, flashoreeprom=1 write eeprom
	int n;
	uchar cmd[4];
	int address = 0;
	int wbytes = n_bytes;
	int blocksize;
	unsigned char * buffer = (unsigned char *)buf;
	unsigned char blockflags = USBASP_BLOCKFLAG_FIRST;
	int function=flashoreeprom;

	((CProgressCtrl *)pDlg)->SetPos(0); //初始化进度

	if (m_usbop.usbaspCheck())
	{ //0 连接正常
		AfxMessageBox("..1");
		return FALSE;
	}
	if (flashoreeprom==0)
	{
		function = USBASP_FUNC_WRITEFLASH;
	}
	else if (flashoreeprom==1)
	{
		function = USBASP_FUNC_WRITEEEPROM;
	}
	else
	{
		AfxMessageBox("..2");
		return FALSE;
	}

	while (wbytes)
	{
		if (wbytes > USBASP_WRITEBLOCKSIZE)
		{
			blocksize = USBASP_WRITEBLOCKSIZE;
			wbytes -= USBASP_WRITEBLOCKSIZE;
		}
		else
		{
			blocksize = wbytes;
			wbytes = 0;
			blockflags |= USBASP_BLOCKFLAG_LAST;
		}

		cmd[0] = address & 0xFF;
		cmd[1] = address >> 8;
		cmd[2] = page_size & 0xFF;
		cmd[3] = blockflags;
		blockflags = 0;

		n = m_usbop.usbaspTransmit(0, function, cmd, buffer, blocksize);
		if (n != blocksize)
		{
			//outMsg("块写入的总字节数和块大小不符!");
			AfxMessageBox("..3");
			return FALSE;
		}
		//显示进程
		char nPos=((CProgressCtrl *)pDlg)->GetPos();
		((CProgressCtrl *)pDlg)->SetPos(nPos+(blocksize*100/n_bytes));
		buffer += blocksize;
		address += blocksize;
	}
  //显示进程
	((CProgressCtrl *)pDlg)->SetPos(100);
	return TRUE;
}

BOOL CUSBASP::chipPageRead(int flashoreeprom, uchar *buf,int n_bytes)
{ //读块 flashoreeprom=0 flash, flashoreeprom=1 eeprom
	int n;
	uchar cmd[4];
	int address = 0;
	int wbytes = n_bytes;
	int blocksize;
	uchar * buffer=buf;
	int function;
	char nPos=0;

	((CProgressCtrl *)pDlg)->SetPos(0); //初始化进度
	CString a;

	if (m_usbop.usbaspCheck())
	{ //0 连接正常
		return FALSE;
	}
	if (flashoreeprom==0)
	{
		function = USBASP_FUNC_READFLASH;
	}
	else if (flashoreeprom==1)
	{
		function = USBASP_FUNC_READEEPROM;
	}
	else
	{
		return FALSE;
	}
	while(wbytes)
	{
		if(wbytes > USBASP_READBLOCKSIZE)
		{
			blocksize = USBASP_READBLOCKSIZE;
			wbytes -= USBASP_READBLOCKSIZE;
		}
		else
		{
			blocksize = wbytes;
			wbytes = 0;
		}

		cmd[0] = address & 0xFF;
		cmd[1] = address >> 8;

		n = m_usbop.usbaspTransmit(1, function, cmd, buffer, blocksize);

		if (n != blocksize)
		{
			return FALSE;
		}
		((CProgressCtrl *)pDlg)->SetPos(nPos+(blocksize*100/n_bytes));
		nPos += blocksize*100/n_bytes;
		buffer += blocksize;
		address+= blocksize;
	}
	((CProgressCtrl *)pDlg)->SetPos(100);
	return TRUE;
}

⌨️ 快捷键说明

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