📄 atlasapi.cpp
字号:
// AtlasApi.cpp: implementation of the AtlasApi class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "AtlasMsg.h"
#include "AtlasApi.h"
#include "AtlasFunc.h"
#include "AtlasUsbDev.h"
#include "AtlasUsbMsg.h"
#include "ApiThread.h"
#include "jtag\\winio.h"
#include "jtag\\jtag_define.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
typedef struct tagUSBPIPEINDEX
{
DWORD dwDownloadIndex;
DWORD dwStatusIndex;
DWORD dwMessageInIndex;
DWORD dwMessageOutIndex;
}USBPIPEINDEX, *PUSBPIPEINDEX;
DWORD g_dwTransport;
DWORD g_dwComPort;
DWORD g_dwComBaud;
DWORD g_dwChip;
DWORD g_dwFlashWP_GPIO_GROUP;
DWORD g_dwFlashWP_GPIO_PINS;
CAtlasUsbDev g_usb;
CAtlasUsbMsg g_msg;
BOOL g_bUsbOpened = FALSE;
HWND g_hWnd = NULL;
USBPIPEINDEX g_usbPipeIndexInfo;
CApiThread * g_pRs232Thread = NULL;
ATLAS_DEV_INFO g_atlasdevinfo;
DumpNFInfo g_DumpNFInfo;
UINT TerminalThread( LPVOID pParam );
#define AtlasIsUsbOpened() (g_bUsbOpened)
BOOL AtlasIsConnected()
{
if(g_dwTransport == ATLAS_TRANSPORT_USB)
return g_usb.IsConnected();
return TRUE;
}
BOOL AtlasInitUsb()
{
if(g_dwTransport == ATLAS_TRANSPORT_USB)
return g_usb.Init();
return TRUE;
}
BOOL AtlasOpenUsb(HWND hParentWnd)
{
g_bUsbOpened = g_usb.OpenDevice();
if(!AtlasGetDeviceInfo())
return FALSE;
if(!AtlasGetUsbPipeIndex(g_usbPipeIndexInfo.dwDownloadIndex,
g_usbPipeIndexInfo.dwStatusIndex,
g_usbPipeIndexInfo.dwMessageInIndex,
g_usbPipeIndexInfo.dwMessageOutIndex))
return FALSE;
if(!g_usb.OpenPipe(g_usbPipeIndexInfo.dwStatusIndex))
{
TRACE("Failed to open status pipe");
return FALSE;
}
if(!g_usb.OpenPipe(g_usbPipeIndexInfo.dwMessageInIndex))
{
TRACE("Failed to open input msg pipe");
return FALSE;
}
if(!g_usb.OpenPipe(g_usbPipeIndexInfo.dwMessageOutIndex))
{
TRACE("Failed to open output msg pipe");
return FALSE;
}
/* if(!g_usb.OpenPipe(5))
{
TRACE("Failed to open output msg pipe");
return FALSE;
}
*/
if(!AtlasBeginSession())
return FALSE;
g_hWnd = hParentWnd;
AfxBeginThread(TerminalThread, (LPVOID)&g_usb);
return g_bUsbOpened;
}
VOID AtlasCloseUsb()
{
AtlasEndSession();
g_usb.CloseDevice();
g_hWnd = NULL;
}
BOOL AtlasInitRs232(DWORD dwPort , DWORD dwBaud)
{
CApiThread::PrepareEvent();
g_pRs232Thread = new CApiThread;
g_pRs232Thread->CreateThread();
WaitForSingleObject(CApiThread::m_hEventThreadCreated, INFINITE);
if(!AtlasSendRs232Msg(WM_API_CONNECT, (WPARAM)dwPort, (LPARAM)dwBaud)
)
{
AtlasDeinitRs232();
return FALSE;
}
TRACE("Pass connect !!!\r\n");
if(!AtlasGetDeviceInfo())
{
TRACE("fail to get info !!!\r\n");
AtlasDeinitRs232();
return FALSE;
}
TRACE("Pass get info !!!\r\n");
return TRUE;
}
BOOL AtlasDeinitRs232()
{
if (g_pRs232Thread != NULL)
{
::PostThreadMessage(g_pRs232Thread->m_nThreadID, WM_API_DISCONNECT, 0, 0);
// OutputDebugString("Waiting for api thread to end...\n");
WaitForSingleObject(CApiThread::m_hEventThreadKilled, INFINITE);
g_pRs232Thread = NULL;
CApiThread::UnprepareEvent();
}
return TRUE;
}
BOOL AtlasSendRs232Msg(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (::PostThreadMessage(g_pRs232Thread->m_nThreadID, uMsg, wParam, lParam) == FALSE)
{
TRACE("PostThreadMessage failed in AtlasSendRs232Msg\n");
return FALSE;
}
if (WaitForSingleObject(CApiThread::m_hEventProtocol, P_TIME_OUT) == WAIT_TIMEOUT)
{
TRACE("Wait timeout\n");
return FALSE;
}
return TRUE;
}
BOOL AtlasOpenDownloadPipe()
{
if(g_dwTransport == ATLAS_TRANSPORT_USB)
{
return g_usb.OpenPipe(g_usbPipeIndexInfo.dwDownloadIndex);
}
else if(g_dwTransport == ATLAS_TRANSPORT_RS232)
{
WORD w;
w = MAKEWORD(P_CMD_ACTION, P_ACTION_OPEN_DATA_PIPE);
if (AtlasSendRs232Msg(WM_API_COMMAND, (WPARAM)w, (LPARAM)0) == FALSE)
return FALSE;
return TRUE;
}
return FALSE;
}
void AtlasCloseDownloadPipe()
{
if(g_dwTransport == ATLAS_TRANSPORT_USB)
g_usb.ClosePipe(g_usbPipeIndexInfo.dwDownloadIndex);
else
{
WORD w;
w = MAKEWORD(P_CMD_ACTION, P_ACTION_CLOSE_DATA_PIPE);
AtlasSendRs232Msg(WM_API_COMMAND, (WPARAM)w, (LPARAM)0);
}
}
BOOL AtlasRequestImageBuf(DWORD dwImgLength, DWORD & deviceAddr)
{
if(g_dwTransport == ATLAS_TRANSPORT_USB)
{
if(!AtlasIsUsbOpened())
return FALSE;
g_msg.FormatMsg(ATLAS_MSG_REQUEST_IMG_BUF, (PBYTE)&dwImgLength);
if(!g_usb.SendVendorCommand(g_msg.GetMsg()))
return FALSE;
return TRUE;
}
else if(g_dwTransport == ATLAS_TRANSPORT_RS232)
{
WORD w;
w = MAKEWORD(P_CMD_SET_REG, P_CMD_CUR_IMG_LENGTH);
if (AtlasSendRs232Msg(WM_API_COMMAND, (WPARAM)w, (LPARAM)dwImgLength) == FALSE)
return FALSE;
return TRUE;
}
return FALSE;
}
BOOL AtlasSetCurImageType(DWORD dwImgType)
{
if(g_dwTransport == ATLAS_TRANSPORT_USB)
{
if(!AtlasIsUsbOpened())
return FALSE;
g_msg.FormatMsg(ATLAS_MSG_SET_CUR_IMG_TYPE, (PBYTE)&dwImgType);
if(!g_usb.SendVendorCommand(g_msg.GetMsg()))
return FALSE;
return TRUE;
}
else if(g_dwTransport == ATLAS_TRANSPORT_RS232)
{
WORD w;
w = MAKEWORD(P_CMD_SET_REG, P_CMD_CUR_IMG_TYPE);
if (AtlasSendRs232Msg(WM_API_COMMAND, (WPARAM)w, (LPARAM)dwImgType) == FALSE)
return FALSE;
return TRUE;
}
return FALSE;
}
BOOL AtlasWriteImage2Nand()
{
if(g_dwTransport == ATLAS_TRANSPORT_USB)
{
if(!AtlasIsUsbOpened())
return FALSE;
g_msg.FormatMsg(ATLAS_MSG_WRITE_IMG_2_NAND, NULL);
if(!g_usb.SendVendorCommand(g_msg.GetMsg()))
return FALSE;
return TRUE;
}
else if(g_dwTransport == ATLAS_TRANSPORT_RS232)
{
WORD w;
w = MAKEWORD(P_CMD_ACTION, P_ACTION_WRITE_IMG);
if (AtlasSendRs232Msg(WM_API_COMMAND, (WPARAM)w, 0) == FALSE)
return FALSE;
return TRUE;
}
return FALSE;
}
BOOL AtlasWriteBlock2Device(PBYTE pData, DWORD dwLength)
{
UINT nBytesWriten;
if(g_dwTransport == ATLAS_TRANSPORT_USB)
{
if(!(g_usb.WritePipe(g_usbPipeIndexInfo.dwDownloadIndex, pData, dwLength, nBytesWriten) && nBytesWriten == dwLength))
return FALSE;
return TRUE;
}
else if(g_dwTransport == ATLAS_TRANSPORT_RS232)
{
DWORD w;
w = MAKEWPARAM(P_DATA_UNFINISHED, dwLength);
if (AtlasSendRs232Msg(WM_API_DATA, (WPARAM)w, (LPARAM)pData) == FALSE)
return FALSE;
return TRUE;
}
return FALSE;
}
BOOL AtlasWriteLastBlock2Device(PBYTE pData, DWORD dwLength)
{
UINT nBytesWriten;
if(g_dwTransport == ATLAS_TRANSPORT_USB)
{
if(!(g_usb.WritePipe(g_usbPipeIndexInfo.dwDownloadIndex, pData, dwLength, nBytesWriten) && nBytesWriten == dwLength))
return FALSE;
return TRUE;
}
else if(g_dwTransport == ATLAS_TRANSPORT_RS232)
{
DWORD w;
w = MAKEWPARAM(P_DATA, dwLength);
if (AtlasSendRs232Msg(WM_API_DATA, (WPARAM)w, (LPARAM)pData) == FALSE)
return FALSE;
return TRUE;
}
return FALSE;
}
BOOL AtlasSetNandCS(DWORD nCs)
{
if(g_dwTransport == ATLAS_TRANSPORT_USB)
{
if(!AtlasIsUsbOpened())
return FALSE;
g_msg.FormatMsg(ATLAS_MSG_SET_NAND_CS, (PBYTE)&nCs);
if(!g_usb.SendVendorCommand(g_msg.GetMsg()))
return FALSE;
return TRUE;
}
else if(g_dwTransport == ATLAS_TRANSPORT_RS232)
{
WORD w;
w = MAKEWORD(P_CMD_SET_REG, P_CMD_NAND_CS);
if (AtlasSendRs232Msg(WM_API_COMMAND, (WPARAM)w, (LPARAM)nCs) == FALSE)
return FALSE;
return TRUE;
}
return FALSE;
}
BOOL AtlasGetNFBlockData(DWORD dwBlockIndex)
{
if(g_dwTransport == ATLAS_TRANSPORT_USB)
{
if(!AtlasIsUsbOpened())
return FALSE;
g_msg.FormatMsg(ATLAS_MSG_GET_ONE_BLOCK_DATA, (PBYTE)&dwBlockIndex);
if(!g_usb.SendVendorCommand(g_msg.GetMsg()))
return FALSE;
return TRUE;
}
else if(g_dwTransport == ATLAS_TRANSPORT_RS232)
{
return TRUE;
}
return FALSE;
}
BOOL AtlasUpdateNandToc()
{
if(g_dwTransport == ATLAS_TRANSPORT_USB)
{
if(!AtlasIsUsbOpened())
return FALSE;
g_msg.FormatMsg(ATLAS_MSG_UPDATE_NAND_TOC, NULL);
if(!g_usb.SendVendorCommand(g_msg.GetMsg()))
return FALSE;
return TRUE;
}
else if(g_dwTransport == ATLAS_TRANSPORT_RS232)
{
WORD w;
w = MAKEWORD(P_CMD_ACTION, P_ACTION_UPDATE_TOC);
if (AtlasSendRs232Msg(WM_API_COMMAND, (WPARAM)w, 0) == FALSE)
return FALSE;
return TRUE;
}
return FALSE;
}
BOOL AtlasGetBootStage(DWORD &dwBootStage)
{
if(g_dwTransport == ATLAS_TRANSPORT_USB)
{
if(!AtlasIsUsbOpened())
return FALSE;
g_msg.FormatMsg(ATLAS_MSG_GET_BOOT_STAGE, NULL);
if(!g_usb.SendVendorCommand(g_msg.GetMsg()))
return FALSE;
memcpy(&dwBootStage, g_msg.GetMsg()->dataBuf, sizeof(DWORD));
return TRUE;
}
else if(g_dwTransport == ATLAS_TRANSPORT_RS232)
{
WORD w;
w = MAKEWORD(P_CMD_GET_REG, P_CMD_BOOT_STAGE);
if (AtlasSendRs232Msg(WM_API_COMMAND, (WPARAM)w, 0) == FALSE)
return FALSE;
dwBootStage = g_pRs232Thread->GetDataValue();
return TRUE;
}
return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -