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

📄 sam7flash.cpp

📁 AT91SAM7S64 BOOT原代码
💻 CPP
字号:
/*
 *	SAM7FLASH V1.x.x
 *	written 2006 by Thomas Pototschnig (http://www.oxed.de)
 *
 * The software is delivered "AS IS" without warranty or condition of any    
 * kind, either express, implied or statutory. This includes without	     
 * limitation any warranty or condition with respect to merchantability or   
 * fitness for any particular purpose, or against the infringements of	     
 * intellectual property rights of others.
 *
 * Please don't remove the header!
 *
 * Please report any bug/fix, modification, suggestion to
 *		thomas dot pototschnig at gmx.de
 */

#define AT91SAM7S256
//#define AT91SAM7S128
//#define AT91SAM7S64


//#define _AFXDLL
#include <windows.h>
//#include <afxwin.h>
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <process.h>
#include "USBLibrary.h"

#define VERSION "1.0.0"

typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned char u8;
typedef int i32;
typedef short i16;
typedef char i8;

#define STARTADR 0
#define LO(n)	(n & 0x000000ff)
#define HI(n)	((n & 0x0000ff00)>>8)
#define PACKSIZE 512


#ifdef AT91SAM7S64
	#define FLASH_PAGE_SIZE	128
#else
	#define FLASH_PAGE_SIZE	256
#endif

u8 cmdbuf[PACKSIZE];

typedef struct _DEVICE {
	char *name;
	u32 number;
	u32 txrate;
	struct _DEVICE *next;
	char active;
} *PDEVICE, DEVICE;

PDEVICE listOfDevice = (PDEVICE) 0;

u32 deviceNumber = 0;
HANDLE threadMutex;

u32 nbConnectedDevices, nbActiveDevice, i;
char ** strConnectedDevices;
PDEVICE ptDevice = 0;
CFCPipeUSB pipe;

FILE* datei;
//CFile* datei;
ULONG filelength = 0;


void cleanUp()
{
	pipe.Close();
	if (ptDevice)
	{
		free(ptDevice);
		ptDevice =0;
	}
}

void error(char* msg)
{
	printf("Critical Error!\n");
	printf("%s\n",msg);
	cleanUp();
	exit(0);
}


void TryConnect()
{
	// Get all device names
	nbConnectedDevices = GetUsbDeviceListName(&strConnectedDevices);

	switch (nbConnectedDevices)
	{
	case 0:
		printf("No Device connected!\n");
		if (ptDevice!=0)
			cleanUp();
		break;
	case 1:
		// If the device has not been registered, register it and create a new task
		if (!ptDevice) {
			ptDevice = (PDEVICE) malloc(sizeof(DEVICE));
			ptDevice->name = _strdup(strConnectedDevices[0]);
			ptDevice->txrate = 0;
			ptDevice->number = deviceNumber++;
			ptDevice->next = 0;

			listOfDevice = ptDevice;

			printf("Added device %d\n", deviceNumber, ptDevice);
		}
		if (pipe.Open(ptDevice->name))
		{
			printf("Can not open device %d\n%s\n", ptDevice->number, ptDevice->name);
		}

		break;
	default:
		if (ptDevice!=0)
			cleanUp();
		printf("To many devices!\n");
		break;
	}
}

//bool test();
bool connect()
{
	u32 timeout = 20;
	printf("Waiting...\n");
	u8 c;
	u32 length = 0;
	do
	{
		printf("Seconds to abort: %d\n",timeout);
		TryConnect();

		if (ptDevice)
		{
			printf("Trying ...\n",timeout);
			u32 length = pipe.ReadPipe(&c,1);
			if ((length==0) && (c=='X'))
				break;
			if (length!=0)
			{
				printf("Return code:%d\n",length);
			}
		}
		timeout--;
		Sleep(1000);
	} while (timeout);

	if (!timeout)
		error("No Answer from device!\n");

	printf("Device responded\n");
	return true;
}

u16 checksum(u8* b, u32 length)
{
	u32 res = 0;
	for (u32 i=0;i<length;i++)
	{
		res+= (u32) b[i];
		res &= 0x0000ffff;
	}
	return (u16) res;
}

bool writeCmd(u8* b,u32 l)
{
	ULONG written=0;
	pipe.WritePipe(b,l,&written);
	if (written != l)
		error("Error while sending data!\n");

	u8 c[3];
	u32 ok = pipe.ReadPipe(c,3);
	if (ok != 0)
		error("No Answer from device!\n");

	printf("... Data Received\n");

	u16 chk = checksum(b,l);
	u16 lo = (u16) c[0];
	u16 hi = (u16) c[1];
	u16 rec = lo | (hi << 8);
	if (chk!=rec)
		error("Wrong checksum received!\n");

	if (c[2]!='X')
		error("Verify error received!\n");

	return true;
}

bool programPage(u16 page)
{
	cmdbuf[1] = LO(page);
	cmdbuf[2] = HI(page);
	if (!writeCmd(cmdbuf,PACKSIZE))
		return false;
	return true;
}

bool openFile(char* s)
{
	try
	{
		datei = fopen(s,"rb");
		if (!datei)
			return false;
	} catch (...)
	{
		return false;
	}
	return true;
}

bool ReadData(char* s, int length)
{
	try {
		fread(s,1,length,datei);
//		datei->Read(s,length);
	} catch (...)
	{
		return false;
	}
	return true;
}

bool program()
{
	ULONG length = 0;
	try 
	{
		fseek (datei , 0 , SEEK_END);
		length = ftell (datei);
		rewind (datei);
		//		length = (ULONG) datei->GetLength();
	} catch (...)
	{
		error("Error reading file!\n");
	}

	memset(cmdbuf,0,PACKSIZE);
	cmdbuf[0]='W';

	u32 page = (STARTADR/FLASH_PAGE_SIZE);

	while (length)
	{
		u32 toTransfer = min(FLASH_PAGE_SIZE,length);
		printf("Flashing page %d (%d Bytes)...",page,toTransfer);

// zero data first in order to verify correctly
		memset(&cmdbuf[3],0,FLASH_PAGE_SIZE);

		if (!ReadData((char*)&cmdbuf[3],toTransfer))
			error("Error while reading file!\n");

		if (!programPage(page))
			error("Error while flashing");

		length-=toTransfer;
		page++;
	}
	return true;
}


//*----------------------------------------------------------------------------
//* \fn    DllMain
//* \brief 
//*----------------------------------------------------------------------------
u32 main(u32 argc, char* argv[])
{
	printf("SAM7FLASH %s\n",VERSION);
	if (argc!=2)
	{
		printf("usage: SAM7FLASH <.bin file>\n");
		return 1;
	}
	if (!openFile(argv[1]))
		error("Error reading file!\n");

	if (!connect())
		error("Error connecting!\n");

	if (!program())
		error("Error while flashing!\n");

	printf("Finished.\n");
	cleanUp();
	return 1;
}

⌨️ 快捷键说明

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