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

📄 dspupdate.cpp

📁 针对TMS320C6000系列的DSP芯片的在线升级的源码
💻 CPP
字号:
// MOUSE_SDK.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "stdlib.h"
#include "usb100.h"

#include "DspUpdate.h"
#include "Resource.h"

//函数主体
// get setup API functions (only available in Win98 and Win2K)
#include <setupapi.h>
// requires to link with setupapi.lib
// Link with SetupAPI.Lib.
#pragma comment (lib, "setupapi.lib")


#define BulkDriverDevice_CLASS_GUID \
{ 0x6b24cc69, 0x42d, 0x431d, { 0xb7, 0x40, 0x37, 0x65, 0x18, 0x1e, 0xb4, 0x95 } };

// { 0xc5df46cf, 0xfb46, 0x45ea, { 0x82, 0x1f, 0xb5, 0x60, 0x5, 0x37, 0xcd, 0xa1 } }

GUID ClassGuid = BulkDriverDevice_CLASS_GUID;
HANDLE OpenByInterface(GUID* pClassGuid, DWORD instance, PDWORD pError);


/*
DEFINE_GUID(GUID_IST_FINGER, 
0xdda31245, 0x1bfc, 0x4225, 0xb2, 0xb8, 0xea, 0xaa, 0xb2, 0xe3, 0x90, 0xb6);
*/



HINSTANCE	g_hMainInst = NULL;
BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
    switch (ul_reason_for_call)
	{
		case DLL_PROCESS_ATTACH:
			g_hMainInst = (HINSTANCE)hModule;
			break;
		case DLL_THREAD_ATTACH:
		case DLL_THREAD_DETACH:
		case DLL_PROCESS_DETACH:
			g_hMainInst = NULL;
			break;
    }
    return TRUE;
}


/*
	Description	: 打开USB设备

	Parameters	: 
		lpHandle 输出, 返回设备句柄

	Return Value	: 
		R_SUCCESS  成功
		其他       失败
*/
int WINAPI OpenUsbDevice(HANDLE * lpHandle)
{
	DWORD dwError;

	if(lpHandle == NULL) 
	{
		return R_ERR_PARAM;
	}
	*lpHandle = OpenByInterface(&ClassGuid, (DWORD)0, &dwError);
	if(*lpHandle == INVALID_HANDLE_VALUE)
	{
		return R_ERR_GENERAL;
	}
	else
	{
		return R_SUCCESS;
	}
}

int WINAPI CloseUsbDevice(HANDLE hDevice )
{
	if(hDevice==INVALID_HANDLE_VALUE)
	{
		return R_ERR_PARAM;
	}

	if(CloseHandle(hDevice))
	{
		return R_SUCCESS;
	}

	return R_ERR_GENERAL;
}

int WINAPI TestDevice(HANDLE hDevice)
{
	BYTE arDataOut[1] = {Cmd_Test};   //PC发送命令	Cmd_Test 
	BYTE arDataIn[2] = {0xFF};        //Mcu回复	Cmd_Test + Error
	const DWORD dwLenToWrite=sizeof(arDataOut);
	const DWORD dwLenToRead = sizeof(arDataIn);
	DWORD dwWritten, dwReceived;

	BOOL bRet = WriteFile(hDevice, arDataOut, dwLenToWrite, &dwWritten, NULL);
	if(!bRet || dwWritten!=dwLenToWrite)
	{
		return R_ERR_GENERAL;
	}

	bRet = ReadFile(hDevice, arDataIn, dwLenToRead, &dwReceived, NULL);
	if(!bRet || dwReceived!=dwLenToRead)
	{
		return R_ERR_GENERAL;
	}

	if(arDataIn[0]!=Cmd_Test || (arDataIn[1]!=0 && arDataIn[1]!=1))
	{
		return R_ERR_GENERAL; //得到的数据,格式错误
	}

	if(arDataIn[0] == 0)
	{
		return R_SUCCESS;  //0x00 	Mcu已经准备好
	}
	else if(arDataIn[1] == 1)
	{
		return R_ERR_MCU;  //0x01	Mcu出现故障。
	}

	return R_ERR_GENERAL;
}

int WINAPI ReadEInit(HANDLE hDevice, LPEmifData pEmifData)
{
	BYTE arDataOut[1] = {Cmd_Read_Einit};
	BYTE arDataIn[30];
	const DWORD dwLenToWrite = sizeof(arDataOut);
	const DWORD dwLenToRead = sizeof(arDataIn);
	DWORD dwWritten, dwReceived;
	
	if(pEmifData==NULL || IsBadReadPtr(pEmifData, sizeof(EmifData)))
	{
		return R_ERR_PARAM;
	}

	BOOL bRet = WriteFile(hDevice, arDataOut, dwLenToWrite, &dwWritten, NULL);
	if(!bRet || dwWritten!=dwLenToWrite)
	{
		return R_ERR_GENERAL;
	}

	bRet = ReadFile(hDevice, arDataIn, dwLenToRead, &dwReceived, NULL);
	if(!bRet || dwReceived!=dwLenToRead)
	{
		return R_ERR_GENERAL;
	}

	if(arDataIn[0]!=Cmd_Read_Einit)
	{
		return R_ERR_GENERAL; //得到的数据,格式错误
	}

	int nResult;
	switch(arDataIn[1])
	{
	case 0: //表示读到EMIF数据
		memcpy(pEmifData, &arDataIn[2], sizeof(EmifData));
		nResult = R_SUCCESS;
		break;
	case 2:
		nResult = R_ERR_GET_EMIF;
		break;
	default:
		nResult = R_ERR_GENERAL;
		break;
	}

	return nResult;
}

int WINAPI WriteEInit(HANDLE hDevice, LPCEmifData lpcEmifData)
{
	BYTE arDataOut[30] = {Cmd_Write_Einit, 0};
	BYTE arDataIn[2] = {0xFF};        //Mcu回复	Cmd_Test + Error
	const DWORD dwLenToWrite=sizeof(arDataOut);
	const DWORD dwLenToRead = sizeof(arDataIn);
	DWORD dwWritten, dwReceived;

	if(lpcEmifData==NULL || IsBadReadPtr(lpcEmifData, sizeof(EmifData)))
	{
		return R_ERR_PARAM;
	}

	memcpy(&arDataOut[2], lpcEmifData, sizeof(*lpcEmifData));

	BOOL bRet = WriteFile(hDevice, lpcEmifData, dwLenToWrite, &dwWritten, NULL);
	if(!bRet || dwWritten!=dwLenToWrite)
	{
		return R_ERR_GENERAL;
	}

	bRet = ReadFile(hDevice, arDataIn, dwLenToRead, &dwReceived, NULL);
	if(!bRet || dwReceived!=dwLenToRead)
	{
		return R_ERR_GENERAL;
	}

	if(arDataIn[0]!=Cmd_Write_Einit)
	{
		return R_ERR_GENERAL; //得到的数据,格式错误
	}

	int nResult = R_ERR_GENERAL;
	switch(arDataIn[1])
	{
	case 0:
		nResult = R_SUCCESS;        //表明已经收到初始化数据,并正确写进EMIF寄存器
		break;						         
	case 3:
		nResult = R_ERR_NOTFULL_EMIFDATA;	//表明没有收到完整的寄存器数据
		break;
	case 4:
		nResult = R_ERR_SET_EMIF; //表明已经收到,但没有正确写进EMIF寄存器
		break;
	default:
		nResult = R_ERR_GENERAL;
		break;
	}

	return nResult;
}

int WINAPI ReadCode(HANDLE hDevice, DWORD dwAddress, LPVOID lpCodeData, DWORD dwSize)
{
	//R_ERR_READ_FLASH
	return R_ERR_GENERAL;
}

BOOL CalcByteArraySum(const BYTE * lpDataArray, DWORD dwSize, USHORT & usSum)
{
	if(lpDataArray==NULL || IsBadReadPtr(lpDataArray, dwSize))
	{
		return FALSE;
	}

	usSum = 0;
	DWORD n;
	for(n=0; n<dwSize; n++)
	{
		usSum += lpDataArray[n];
	}
	return TRUE;
}

int WINAPI WriteCode(HANDLE hDevice, DWORD dwAddress, LPCVOID lpCodeData, DWORD dwSize)
{
	const DWORD PACK_SIZE = 64;
	if(lpCodeData==NULL || IsBadReadPtr(lpCodeData, dwSize))
	{
		return R_ERR_PARAM;
	}

	BYTE arDataOut[PACK_SIZE];
	DWORD dwSizeLeft=dwSize;
	BYTE *pDataOffset = (BYTE *)lpCodeData;

	BYTE arDataIn[2] = {0xFF};        //Mcu回复	Cmd_Test + Error
	DWORD dwLenToWrite=11;
	const DWORD dwLenToRead = sizeof(arDataIn);
	DWORD dwWritten, dwReceived;

	USHORT usSum;
	CalcByteArraySum((const BYTE *)lpCodeData, dwSize, usSum);
	arDataOut[0] = Cmd_Write_Code;
	*((DWORD *)&arDataOut[1]) = dwAddress;
	*((DWORD *)&arDataOut[5]) = dwSize;
	*((USHORT *)&arDataOut[9]) = usSum;
	
	BOOL bRet = WriteFile(hDevice, arDataOut, dwLenToWrite, &dwWritten, NULL);
	if(!bRet || dwWritten!=dwLenToWrite)
	{
		return R_ERR_GENERAL;
	}

	while(dwSizeLeft > 0)
	{
		if(dwSizeLeft < PACK_SIZE)
		{
			memset(arDataOut, 0xFF, sizeof(arDataOut));
			memcpy(arDataOut, pDataOffset, dwSizeLeft);

			pDataOffset = arDataOut;
			dwSizeLeft -= PACK_SIZE;
		}

		bRet = WriteFile(hDevice, pDataOffset, PACK_SIZE, &dwWritten, NULL);

		if(!bRet || PACK_SIZE!=dwWritten)
		{
			return R_ERR_GENERAL;
		}

		pDataOffset += PACK_SIZE;
		dwSizeLeft -= PACK_SIZE;
	}

	bRet = ReadFile(hDevice, arDataIn, dwLenToRead, &dwReceived, NULL);
	if(!bRet || dwReceived!=dwLenToRead)
	{
		return R_ERR_GENERAL;
	}

	if(arDataIn[0]!=Cmd_Write_Code)
	{
		return R_ERR_GENERAL;       //得到的数据,格式错误
	}

	int nResult = R_ERR_GENERAL;
	switch(arDataIn[1])
	{
	case 0:
		nResult = R_SUCCESS;        //已经收到初始化数据,并正确写进EMIF寄存器
		break;						         
	case 5:
		nResult = R_ERR_CHKSUM;		//校验和错误
		break;
	default:
		nResult = R_ERR_GENERAL;
		break;
	}

	return nResult;
}

⌨️ 快捷键说明

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