forked from TomKing062/spreadtrum_flash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBMPlatform.cpp
150 lines (127 loc) · 2.7 KB
/
BMPlatform.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "BMPlatform.h"
#include <iostream>
CBMPlatformApp::CBMPlatformApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
m_pfCreateChannel = NULL;
m_pfReleaseChannel = NULL;
m_hChannelLib = NULL;
}
BOOL CBMPlatformApp::InitInstance()
{
m_hChannelLib = LoadLibrary(_T("Channel9.dll"));
if(m_hChannelLib == NULL)
{
std::cout << GetLastError() << std::endl;
return FALSE;
}
m_pfCreateChannel = (pfCreateChannel)GetProcAddress(m_hChannelLib,"CreateChannel");
m_pfReleaseChannel = (pfReleaseChannel)GetProcAddress(m_hChannelLib,"ReleaseChannel");
if(m_pfCreateChannel == NULL || m_pfReleaseChannel == NULL )
{
return FALSE;
}
return TRUE;
}
int CBMPlatformApp::ExitInstance()
{
// TODO: Add your specialized code here and/or call the base class
if(m_hChannelLib)
{
FreeLibrary(m_hChannelLib);
m_hChannelLib = NULL;
//m_pfCreateChannel = NULL;
//m_pfReleaseChannel = NULL;
}
return TRUE;
}
CBMPlatformApp g_theApp;
CBootModeOpr::CBootModeOpr()
{
m_bOpened = FALSE;
m_pChannel = NULL;
g_theApp.InitInstance();
}
CBootModeOpr::~CBootModeOpr()
{
g_theApp.ExitInstance();
}
BOOL CBootModeOpr::Initialize(DWORD dwPort)
{
m_bOpened = FALSE;
if (!g_theApp.m_pfCreateChannel)
{
return FALSE;
}
if (!g_theApp.m_pfCreateChannel((ICommChannel**)&m_pChannel, CHANNEL_TYPE_COM))
{
return FALSE;
}
m_bOpened = ConnectChannel(dwPort);
return m_bOpened;
}
void CBootModeOpr::Uninitialize()
{
DisconnectChannel();
if (m_pChannel)
{
if (g_theApp.m_pfReleaseChannel)
g_theApp.m_pfReleaseChannel(m_pChannel);
m_pChannel = NULL;
}
}
int CBootModeOpr::Read(UCHAR * m_RecvData, int max_len, int dwTimeout)
{
ULONGLONG tBegin;
ULONGLONG tCur;
memset(m_RecvData, 0, 8);
tBegin = GetTickCount64();
do
{
tCur = GetTickCount64();
DWORD dwRead = m_pChannel->Read(m_RecvData, max_len, dwTimeout);
if (dwRead)
{
return dwRead;
}
} while ((tCur - tBegin) < dwTimeout);
return 0;
}
int CBootModeOpr::Write(UCHAR* lpData, int iDataSize)
{
return (int)(m_pChannel->Write(lpData, iDataSize));
}
BOOL CBootModeOpr::ConnectChannel(DWORD dwPort)
{
if(!dwPort) return FALSE;
DWORD dwBaud = 115200;
CHANNEL_ATTRIBUTE ca;
ca.ChannelType = CHANNEL_TYPE_COM;
ca.Com.dwPortNum = dwPort;
ca.Com.dwBaudRate = dwBaud;
//Log(_T("Connect Channel +++"));
BOOL bOK = m_pChannel->Open(&ca);
//Log(_T("Connect Channel ---"));
if (!bOK)
{
//Log(_T("Open Channel fail."));
return FALSE;
}
return TRUE;
}
BOOL CBootModeOpr::DisconnectChannel()
{
if (m_pChannel == NULL)
{
return FALSE;
}
if (m_bOpened)
{
m_bOpened = FALSE;
//Log(_T("Disconnect Channel +++"));
m_pChannel->Close();
//Log(_T("Disconnect Channel ---"));
}
return TRUE;
}