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

📄 burn.cpp.svn-base

📁 vc环境下编译的一个对USB设备进行烧录的驱动程序
💻 SVN-BASE
📖 第 1 页 / 共 4 页
字号:
#include "stdafx.h"
#include "burn.h"
#include "gbl_macrodef.h"
#include "vfs.h"
#include "burn_lib.h"

#define TOTAL_SIZE_4G	0x100000000		// 4G Nandflash 容量
#define TOTAL_SIZE_UINT 0xffffffff		// 32 bit 总大小

configuration config;

HANDLE mutex_usb;
HANDLE mutex_time;
HANDLE usb_init_event;
BOOL g_bInit;

char usb_name[MAX_DEVICE_NUM][255];
char *usb_name_index;

static BYTE end_pad[MIN_PACKET_LENGTH] = {
	0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 
		0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 
		0x50, 0x50, 0x50, 0x52, 0x0, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 
		0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 0x50, 
		0x50, 0x50, 0x50, 0x50, 0x4F, 0x50, 0x13, 0x14
};

static TCHAR absolute_path[255];

extern int CheckFormatData(UINT total_size);

static ULONG checksum(UCHAR *pData, int length)
{
	int i;
	ULONG xor;

	if(pData == NULL)
		return 0;

	xor = 0x00;

	for(i = 0; i < length; i++)
		xor ^= (*(pData+i));

	return xor;
}

TCHAR * ConverPathToAbsolute(TCHAR *path)
{
	extern char Current_DirectoryBuffer[MAX_PATH];

	if(strstr(path, ":\\") == NULL)
	{
		strcpy(absolute_path, Current_DirectoryBuffer);
		strcat(absolute_path, "\\");
		strcat(absolute_path, path);
		return absolute_path;
	}
	else
		return path;
}

BurnFlash::BurnFlash()
{
	burn_thread_handle = INVALID_HANDLE_VALUE;
	download_file_len = 0;
	bstandby = FALSE;
	map_index = 0;
    map_len = 0;
	usb_dbcc_name.Empty();

	hWnd = NULL;
	ID = 0;
	
	bDownEncryptFile = false;

    memset(&information, 0, sizeof(information));

	download_init_event = CreateEvent(NULL, TRUE, FALSE, NULL);
}

BurnFlash::~BurnFlash()
{
	CloseHandle(download_init_event);
	download_init_event = NULL;
	
	if(WaitForSingleObject(burn_thread_handle, 100) != WAIT_OBJECT_0)
		TerminateThread(burn_thread_handle, 0);
	
	CloseHandle(burn_thread_handle);

	burn_thread_handle = NULL;

	if(com.is_open())
		com.close();
	
}

bool BurnFlash::open_usb()
{
    return m_usb.M3USBOPEN(usb_dbcc_name);
}

void BurnFlash::set_usb_name(CString dbcc_name)
{
    usb_dbcc_name = dbcc_name;
}

CString BurnFlash::get_usb_name()
{
    return usb_dbcc_name;
}

bool BurnFlash::open_com(int port, int baud_rate, HWND hWnd)
{
	if(com.open(port, baud_rate))
	{
		com.set_notify_num(1);
		com.set_hwnd(hWnd);
		return true;
	}
	
	return false;
}


void BurnFlash::set_wnd(HWND hWnd)
{
    this->hWnd = hWnd;
}

void BurnFlash::set_id(int id)
{
    ID = id;
}


HWND BurnFlash::get_wnd()
{
    return hWnd;
}

int BurnFlash::get_id()
{
    return ID;
}

void BurnFlash::close_com()
{
	com.close();
}

bool BurnFlash::set_dcb(int BaudRate, int ByteSize, int Parity, int StopBits)
{
	return com.set_dcb(BaudRate, ByteSize, Parity, StopBits);
}

bool BurnFlash::set_para()
{
    return com.set_para();
}

int BurnFlash::com_read(char *buf, int buf_len)
{
	return com.read(buf, buf_len);
}

int BurnFlash::com_write(char *buf)
{
	return com.write(buf);
}

int BurnFlash::com_write(char *buf, int buf_len)
{
	return com.write(buf, buf_len);
}

bool BurnFlash::is_standby()
{
	return bstandby;
}

void BurnFlash::set_standby(BOOL bstandby)
{
	this->bstandby = bstandby;
}


int BurnFlash::get_mapindex()
{
	return map_index;
}

void BurnFlash::set_mapindex(int map_index)
{
	this->map_index = map_index;
}

int BurnFlash::get_maplen()
{
    return map_len;
}

void BurnFlash::set_maplen(int map_len)
{
    this->map_len = map_len;
}

bool BurnFlash::begin_burn_flash()
{
	if(is_standby())
	{
		DWORD id;

		set_mapindex(0);
        set_maplen(0);

		burn_thread_handle = CreateThread(NULL, 0, burn_thread, this, 0, &id); 
		assert(burn_thread_handle);
		if(!burn_thread_handle)
		{
			CloseHandle(burn_thread_handle);
			burn_thread_handle = INVALID_HANDLE_VALUE;
		}
		else
			return true;
	}
	
	return false;
}

HANDLE BurnFlash::Get_usb_handle()
{
	return m_usb.Get_M3USBHandle();
}

HANDLE BurnFlash::get_thread()
{
	return burn_thread_handle;
}

bool BurnFlash::suspend_thread()
{
	return burn_thread_handle != NULL ? SuspendThread(burn_thread_handle) != 0xFFFFFFFF : false;
}
//恢复监视线程
bool BurnFlash::resume_thread()
{
	return burn_thread_handle != NULL ? ResumeThread(burn_thread_handle) != 0xFFFFFFFF : false;
}

void BurnFlash::terminate_thread()
{
	extern int m_nWorkNum;

	if(burn_thread_handle != INVALID_HANDLE_VALUE)
	{
		if(WaitForSingleObject(burn_thread_handle, 100) != WAIT_OBJECT_0)
			TerminateThread(burn_thread_handle, 0);
		
		CloseHandle(burn_thread_handle);

		burn_thread_handle = INVALID_HANDLE_VALUE;

		bstandby = FALSE;

		m_nWorkNum--;
	}
}

int BurnFlash::add_download_file_len(int file_len)
{
	download_file_len += file_len;
	return download_file_len;
}

int BurnFlash::get_download_file_len()
{
	return download_file_len;
}

void BurnFlash::set_download_file_len(int len)
{
	download_file_len = len;
}

void BurnFlash::close_usb()
{
	m_usb.M3USBCLOSE();
	usb_name[get_id()][0] = 0;
}

bool BurnFlash::is_com_open()
{
	return com.is_open();
}

bool BurnFlash::set_registers()
{
	if(config.bUBoot)
	{
		return set_registers_usb(config, m_usb);
	}
	else
	{
		return set_registers_com(config, com);
	}
}

bool BurnFlash::write_transc_packet(T_TRANSC_TYPE transc_type, WORD data_length, BYTE *data)
{
	transc_packet packet;
	bool ret;
	UINT real_len, write_len;
	
	packet.type = transc_type;

	packet.packet_length = sizeof(packet.type) + sizeof(packet.packet_length) + data_length;
	
	memcpy(packet.data, data, data_length);
	
	write_len = packet.packet_length;

	if(write_len % MIN_PACKET_LENGTH != 0)
		write_len = packet.packet_length + (MIN_PACKET_LENGTH - packet.packet_length % MIN_PACKET_LENGTH);
	
	ret = m_usb.M3USB_DOWNLOAD((BYTE *)&packet, write_len, &real_len);
	if(!ret)
		return ret;
	
	ret = m_usb.M3USB_DOWNLOAD(end_pad, MIN_PACKET_LENGTH, &real_len);
	if(!ret)
		return ret;
	
	return true;
}

BYTE BurnFlash::read_transc_ack()
{
	transc_packet packet;
	UINT	read_length;
	int overtime = 0;

	while(1)
	{
		if( !m_usb.M3USB_UPLOAD((BYTE *)&packet, MIN_PACKET_LENGTH, &read_length) )
		{
			return ACK_FAIL;	
		}

		if(read_length == 0)
		{
			overtime ++;
			if(overtime >= OVER_TIME)
			{
				return ACK_FAIL;
			}
			Sleep(1000);
			continue;
		}

		return (BYTE)packet.data[0];
	}
}

bool BurnFlash::if_find_chipid()
{
    return chip_para.bFind;
}


bool BurnFlash::test_transc()
{
	BYTE ret;
	//BYTE data = 0xff;
	int data[5];

	data[0] = 8;//default ram size //config.ram_size;

    data[1] = config.bios_main_version;

    data[2] = config.bios_sub_version;

    data[3] = config.bios_sub1_version;

	data[4] = 0x55555555;
	
	
	if( !write_transc_packet(CONN_TEST_TRANSC, sizeof(data), (BYTE *)&data))
		return false;
	
	Sleep(1000);
	
	ret = read_transc_ack();
	
	if(ret == ACK_SUCCESS)
		return true;
	else
		return false;
}

bool BurnFlash::compare_transc(char *file_path)
{
	BYTE ret;
	BYTE data = 0xff;
	HANDLE hFile = NULL;
	DWORD read_len = 0;
	UINT usb_write_len = 0;

	char buf[256];

	if(file_path == NULL)
		return false;

	if( !write_transc_packet(COMPARE_TRANS, 1, (BYTE *)&data))
	{
		sprintf(buf, "[PC PRINT]: WRITE COMPARE TRANSC Error\r\n");
		record_write(buf, strlen(buf), get_id()+config.base_com);
		return false;
	}
	
	Sleep(2000);
	
	ret = read_transc_ack();
	
	if(ret != ACK_SUCCESS)
	{
		sprintf(buf, "[PC PRINT]: READ COMPARE TRANCS ACK Error\r\n");
		record_write(buf, strlen(buf), get_id()+config.base_com);
		
		return false;
	}

    return download_data(file_path);

}

bool BurnFlash::write_config()
{
	BYTE ret;
	BYTE data = 0xff;
	
	
	if( !write_transc_packet(WRITE_CONFIG, 1, (BYTE *)&data))
		return false;
	
	Sleep(2000);
	
	ret = read_transc_ack();
	
	if(ret == ACK_SUCCESS)
		return true;
	else
		return false;

}

bool BurnFlash::erase_transc()
{
	T_ERASE_DATA erase_data;
	BYTE ret;

	for(int i = 0; i < config.erase_count; i++)
	{
        erase_data.type = config.erases[i].type;
		erase_data.End_Address = config.erases[i].End_Address;
		erase_data.Start_Address = config.erases[i].Start_Address;

        if(erase_data.type == ERASE_NANDFLASH)
        {
            if(erase_data.Start_Address == 0)
            {
                information.BootBlock = (erase_data.End_Address - erase_data.Start_Address) /
                    (chip_para.nandflash_parameter.page_size * chip_para.nandflash_parameter.page_per_blk);
            }
        }

		if( !write_transc_packet(ERASE_TRANSC, sizeof(T_ERASE_DATA), (BYTE *)&erase_data))
			return false;

		Sleep(1000);
		
		ret = read_transc_ack();
		
		if(ret != ACK_SUCCESS)
			return false;
		
	}

⌨️ 快捷键说明

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