aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Firmware/dfuprog/lmdfuwrap.cpp
blob: 4dc047dc5f777f0103884f0704ee3f8257927d1a (plain)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//*****************************************************************************
//
// lmdfuwrap.cpp : A thin wrapper over the lmdfu.dll library allowing it to be
//                 loaded dynamically rather than statically linking to its .lib
//                 file.
//
// Copyright (c) 2009-2017 Texas Instruments Incorporated.  All rights reserved.
// Software License Agreement
// 
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
// 
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
// 
// This is part of revision 2.1.4.178 of the Tiva Firmware Development Package.
//
//*****************************************************************************

#include <windows.h>
#include "lmdfu.h"
#include "lmdfuwrap.h"

//*****************************************************************************
//
// DLL module handle.
//
//*****************************************************************************
HINSTANCE hLMUSB = (HINSTANCE)NULL;

//*****************************************************************************
//
// Function pointers for each DLL entry point.
//
//*****************************************************************************
tLMDFUInit pfnLMDFUInit = NULL;
tLMDFUDeviceOpen pfnLMDFUDeviceOpen = NULL;
tLMDFUDeviceClose pfnLMDFUDeviceClose = NULL;
tLMDFUDeviceStringGet pfnLMDFUDeviceStringGet = NULL;
tLMDFUDeviceASCIIStringGet pfnLMDFUDeviceASCIIStringGet = NULL;
tLMDFUParamsGet pfnLMDFUParamsGet = NULL;
tLMDFUIsValidImage pfnLMDFUIsValidImage = NULL;
tLMDFUDownload pfnLMDFUDownload = NULL;
tLMDFUDownloadBin pfnLMDFUDownloadBin = NULL;
tLMDFUErase pfnLMDFUErase = NULL;
tLMDFUBlankCheck pfnLMDFUBlankCheck = NULL;
tLMDFUUpload pfnLMDFUUpload = NULL;
tLMDFUStatusGet pfnLMDFUStatusGet = NULL;
tLMDFUErrorStringGet pfnLMDFUErrorStringGet = NULL;
tLMDFUModeSwitch pfnLMDFUModeSwitch = NULL;

tLMDFUErr __stdcall _LMDFUInit(void)
{
    //
    // Try to load the USB DLL.
    //
    hLMUSB = LoadLibrary(TEXT("lmdfu.dll"));

    //
    // Did we find it?
    //
    if(hLMUSB)
    {
        //
        // Yes - query all the entry point addresses.
        //
        pfnLMDFUInit = (tLMDFUInit)GetProcAddress(hLMUSB, "LMDFUInit");
        pfnLMDFUDeviceOpen = (tLMDFUDeviceOpen)GetProcAddress(hLMUSB, "LMDFUDeviceOpen");
        pfnLMDFUDeviceClose = (tLMDFUDeviceClose)GetProcAddress(hLMUSB, "LMDFUDeviceClose");
        pfnLMDFUDeviceStringGet = (tLMDFUDeviceStringGet)GetProcAddress(hLMUSB, "LMDFUDeviceStringGet");
        pfnLMDFUDeviceASCIIStringGet = (tLMDFUDeviceASCIIStringGet)GetProcAddress(hLMUSB, "LMDFUDeviceASCIIStringGet");
        pfnLMDFUParamsGet = (tLMDFUParamsGet)GetProcAddress(hLMUSB, "LMDFUParamsGet");
        pfnLMDFUIsValidImage = (tLMDFUIsValidImage)GetProcAddress(hLMUSB, "LMDFUIsValidImage");
        pfnLMDFUDownload = (tLMDFUDownload)GetProcAddress(hLMUSB, "LMDFUDownload");
        pfnLMDFUDownloadBin = (tLMDFUDownloadBin)GetProcAddress(hLMUSB, "LMDFUDownloadBin");
        pfnLMDFUErase = (tLMDFUErase)GetProcAddress(hLMUSB, "LMDFUErase");
        pfnLMDFUBlankCheck = (tLMDFUBlankCheck)GetProcAddress(hLMUSB, "LMDFUBlankCheck");
        pfnLMDFUUpload = (tLMDFUUpload)GetProcAddress(hLMUSB, "LMDFUUpload");
        pfnLMDFUStatusGet = (tLMDFUStatusGet)GetProcAddress(hLMUSB, "LMDFUStatusGet");
        pfnLMDFUErrorStringGet = (tLMDFUErrorStringGet)GetProcAddress(hLMUSB, "LMDFUErrorStringGet");
        pfnLMDFUModeSwitch = (tLMDFUModeSwitch)GetProcAddress(hLMUSB, "LMDFUModeSwitch");

        //
        // Make sure we actually queried all the expected entry points.
        //
        if(!pfnLMDFUInit || !pfnLMDFUDeviceOpen ||  !pfnLMDFUDeviceClose ||
           !pfnLMDFUDeviceStringGet || !pfnLMDFUDeviceASCIIStringGet ||
           !pfnLMDFUParamsGet || !pfnLMDFUIsValidImage || !pfnLMDFUDownload ||
           !pfnLMDFUDownloadBin || !pfnLMDFUErase || !pfnLMDFUBlankCheck ||
           !pfnLMDFUUpload || !pfnLMDFUStatusGet || !pfnLMDFUErrorStringGet ||
           !pfnLMDFUModeSwitch)
        {
            //
            // We failed to query at least one entry point.  Return
            // DFU_ERR_INVALID_ADDR to signal to the caller that the driver
            // is likely the wrong version.
            //
            return(DFU_ERR_INVALID_ADDR);
        }
        else
        {
            //
            // We got all the expected function pointers so call the library
            // init function and return it's response.
            //
            return(pfnLMDFUInit());
        }
    }
    else
    {
        //
        // The DLL could not be found.  This tends to suggest that the driver
        // is not installed.  Return an appropriate error code to the caller
        // so that they can provide helpful information to the user.
        //
        return(DFU_ERR_NOT_FOUND);
    }
}

tLMDFUErr __stdcall _LMDFUDeviceOpen(int iDeviceIndex,
                                    tLMDFUDeviceInfo *psDevInfo,
                                    tLMDFUHandle *phHandle)
{
    if(pfnLMDFUDeviceOpen)
    {
        return(pfnLMDFUDeviceOpen(iDeviceIndex, psDevInfo, phHandle));
    }

    return(DFU_ERR_HANDLE);
}

tLMDFUErr __stdcall _LMDFUDeviceClose(tLMDFUHandle hHandle, bool bReset)
{
    if(pfnLMDFUDeviceClose)
    {
        return(pfnLMDFUDeviceClose(hHandle, bReset));
    }

    return(DFU_ERR_HANDLE);
}

tLMDFUErr __stdcall _LMDFUDeviceStringGet(tLMDFUHandle hHandle,
                                         unsigned char ucStringIndex,
                                         unsigned short usLanguageID,
                                         char *pcString,
                                         unsigned short *pusStringLen)
{
    if(pfnLMDFUDeviceStringGet)
    {
        return(pfnLMDFUDeviceStringGet(hHandle, ucStringIndex, usLanguageID,
                                       pcString, pusStringLen));
    }

    return(DFU_ERR_HANDLE);
}

tLMDFUErr __stdcall _LMDFUDeviceASCIIStringGet(tLMDFUHandle hHandle,
                                              unsigned char ucStringIndex,
                                              char *pcString,
                                              unsigned short *pusStringLen)
{
    if(pfnLMDFUDeviceASCIIStringGet)
    {
        return(pfnLMDFUDeviceASCIIStringGet(hHandle, ucStringIndex, pcString,
                                            pusStringLen));
    }

    return(DFU_ERR_HANDLE);
}

tLMDFUErr __stdcall _LMDFUParamsGet(tLMDFUHandle hHandle,
                                   tLMDFUParams *psParams)
{
    if(pfnLMDFUParamsGet)
    {
        return(pfnLMDFUParamsGet(hHandle, psParams));
    }

    return(DFU_ERR_HANDLE);
}

tLMDFUErr __stdcall _LMDFUIsValidImage(tLMDFUHandle hHandle,
                                      unsigned char *pcDFUImage,
                                      unsigned long ulImageLen,
                                      bool *pbTivaFormat)
{
    if(pfnLMDFUIsValidImage)
    {
        return(pfnLMDFUIsValidImage(hHandle, pcDFUImage, ulImageLen,
                                    pbTivaFormat));
    }

    return(DFU_ERR_HANDLE);
}

tLMDFUErr __stdcall _LMDFUDownload(tLMDFUHandle hHandle,
                                  unsigned char *pcDFUImage,
                                  unsigned long ulImageLen, bool bVerify,
                                  bool bIgnoreIDs, HWND hwndNotify)
{
    if(pfnLMDFUDownload)
    {
        return(pfnLMDFUDownload(hHandle, pcDFUImage, ulImageLen, bVerify,
                                bIgnoreIDs, hwndNotify));
    }

    return(DFU_ERR_HANDLE);
}

tLMDFUErr __stdcall _LMDFUDownloadBin(tLMDFUHandle hHandle,
                                     unsigned char *pcBinaryImage,
                                     unsigned long ulImageLen,
                                     unsigned long ulStartAddr,
                                     bool bVerify, HWND hwndNotify)
{
    if(pfnLMDFUDownloadBin)
    {
        return(pfnLMDFUDownloadBin(hHandle, pcBinaryImage, ulImageLen,
                                   ulStartAddr, bVerify, hwndNotify));
    }

    return(DFU_ERR_HANDLE);
}

tLMDFUErr __stdcall _LMDFUErase(tLMDFUHandle hHandle, unsigned long ulStartAddr,
                               unsigned long ulEraseLen, bool bVerify,
                               HWND hwndNotify)
{
    if(pfnLMDFUErase)
    {
        return(pfnLMDFUErase(hHandle, ulStartAddr, ulEraseLen, bVerify,
                             hwndNotify));
    }

    return(DFU_ERR_HANDLE);
}

tLMDFUErr __stdcall _LMDFUBlankCheck(tLMDFUHandle hHandle,
                                    unsigned long ulStartAddr,
                                    unsigned long ulLen)
{
    if(pfnLMDFUBlankCheck)
    {
        return(pfnLMDFUBlankCheck(hHandle, ulStartAddr, ulLen));
    }

    return(DFU_ERR_HANDLE);
}

tLMDFUErr __stdcall _LMDFUUpload(tLMDFUHandle hHandle, unsigned char *pcBuffer,
                                unsigned long ulStartAddr,
                                unsigned long ulImageLen, bool bRaw,
                                HWND hwndNotify)
{
    if(pfnLMDFUUpload)
    {
        return(pfnLMDFUUpload(hHandle, pcBuffer, ulStartAddr, ulImageLen,
                              bRaw, hwndNotify));
    }

    return(DFU_ERR_HANDLE);
}

tLMDFUErr __stdcall _LMDFUStatusGet(tLMDFUHandle hHandle, tDFUStatus *pStatus)
{
    if(pfnLMDFUStatusGet)
    {
        return(pfnLMDFUStatusGet(hHandle, pStatus));
    }

    return(DFU_ERR_HANDLE);
}

tLMDFUErr __stdcall _LMDFUModeSwitch(tLMDFUHandle hHandle)
{
    if(pfnLMDFUStatusGet)
    {
        return(pfnLMDFUModeSwitch(hHandle));
    }

    return(DFU_ERR_HANDLE);
}

char * __stdcall _LMDFUErrorStringGet(tLMDFUErr eError)
{
    if(pfnLMDFUErrorStringGet)
    {
        return(pfnLMDFUErrorStringGet(eError));
    }

    return("Driver not installed");
}