This application note describes how to interface a Microsoft® Visual Studio® project with the
dynamic link library (DLL) of the application programming interface (API) of the iNEMO
software development kit (SDK).
The DLL object handles the hardware communication interfacing and is an easy way to
achieve (soft) real-time performance. The use of DLLs helps with the modularization of
code, code reuse, efficient memory use, and reduction of disk space. Typically, this is
preferred when you want to access the iNEMO
MKI062V1) capabilities directly from a software project without accessing the
microcontroller firmware.
The STEVAL-MKI062V1 board can be supplied by USB. Before connecting the board to a
USB port, configure switch S4 as described in the user manual for the board (UM0752).
The STEVAL-MKI062V1 PC software must be pre-installed on the computer before using
the C++ code described in this application note, and it is necessary to download iNEMO
application 1.0.8 or higher.
1.2 Notes on Microsoft Visual Studio version
The software code described in this application note is compatible with the following
versions of the development environment:
●Microsoft Visual Studio 2005
●Microsoft Visual Studio .NET 2003
●Microsoft Visual Studio .NET 2002
As a guideline, users can follow these steps to make a project capable of managing the
iNEMO DLL from native C++ code:
1.Start Visual Studio.
2. In the File menu, click New, and then Project.
3. If you are using Visual C++
Project Types, and then click Win32 Console Project or Win32 Project under
Templates. If you are using Visual C++ 2005, click Visual C++ under Project Types, and
then click Win32 Project under Templates.
4. In the Name text box, type iNEMO_Cpp_Wrapper, and then click OK.
5. If you are using Visual C++ .NET 2003 or Visual C++ 2005, click Finish. If you are using
Visual C++ .NET 2002, click Application Settings, click Console Application, and then
click Finish.
6. Open the iNEMO_Cpp_Wrapper file in Code view.
®
.NET 2002 or 2003, click Visual C++ Projects under
If you receive the error message “Run-Time Check Failure #0”, the value of ESP was not
properly saved across a function call. This is usually a result of calling a function declared
with one calling convention with a function pointer declared using a different calling
convention. When you run the debug build of your application, you can resolve this by the
following steps:
1.In Solution Explorer, right-click on your ProjectName, and then click Properties. The
ProjectName Property Pages dialog box appears.
2. In the left pane of the ProjectName Property Pages dialog box, click Configuration
Properties.
3. Under Configuration Properties, click C/C++, and then click Code Generation.
4. Set the Basic Runtime Checks property to Default.
5. In the ProjectName Property Pages dialog box, click OK.
6. In the File menu, click Save All to save all of the files.
Doc ID 17013 Rev 13/15
Getting startedAN3144
1.3 iNEMO connection
Once the iNEMO demonstration board is connected to a USB port, it is identified by the PC
through the virtual COM port driver, and the PC assigns a port number to the iNEMO board.
To identify which port number has been assigned by the PC, follow these steps:
1.On the PC desktop, right click on the My Computer icon and click on Manage (see
Figure 2).
2. In Computer Management under System Tools, click on Device Manager to see all the
devices connected to the PC in the right-hand pane.
3. In the right-hand pane, click the “+” next to Ports (COM & LPT) to see the list of
available ports. In this list you should find the STEVAL-MKI062V1 iNEMO Platform
(COMx) string (see Figure 3).
The string containing the COM port number assigned to the board is used by the source
code described in Section 2.3.1: Connecting the PC to iNEMO.
Note:The COM port number assigned is valid for one session only; when the board is unplugged
and plugged in again (especially into a new USB port) the COM port number assigned could
be different. It should therefore be checked again using the procedure described above.
Figure 2.Identifying the COM port used by the iNEMO board - first step
4/15Doc ID 17013 Rev 1
AN3144Getting started
Figure 3.Identifying the COM port used by iNEMO board - second step
1.4 Releasing the application
You can freely release your own application which is linked with iNEMO DLL. Because the
iNEMO_SDK.dll is dependent on the PL_001.dll, both DLLs must be located in the same
folder. It is recommended to have the two DLLs in the application folder to avoid conflicts
with different versions.
Doc ID 17013 Rev 15/15
Application codeAN3144
2 Application code
2.1 Package description
The example code provided below uses the files contained in the subfolder:
\STMicroelectronics\iNEMO Application\Redist
(the exact folder name may be different on your computer)
This subfolder contains:
●iNEMO_SDK.chm - Help file for the SDK
●iNEMO_SDK.dll - Dynamic link library of the SDK
●iNEMO_SDL.h - Header file for the iNEMO SDK dll
●iNEMO_SDK_Wrapper.cs - Wrapper file in C#
●PL_001.dll - PL_001 Dynamic Link Library
The source code in the sections that follow can be used as a guideline to develop your own
application.
2.2 Dynamic link library loading
Generally, two loading methods allow you to call the exported DLL functions when loading a
DLL in your application. The two loading methods are static load and dynamic load.
In static loading, the loading/unloading of the DLL is managed automatically by the run-time
framework when the user runs/closes the application. In this case, in the Visual Studio C++
application you need to include the iNEMO_SDK.h and the relative import library file
(iNEMO_SDK.lib).
In dynamic loading, the application explicitlyloads/unloads the DLL through the LoadLibrary
and FreeLibrary functions.
The DLL of the application programming interface (API) of the iNEMO software development
kit (SDK) can be easily linked to your application in dynamic mode. In this way your
application calls either the LoadLibrary function or the LoadLibraryEx function to load the
DLL at run-time. After the DLL is successfully loaded, you can use the GetProcAddress
function to obtain the address of the exported DLL function that you want to call. When you
load the DLL dynamically, you do not need an import library file.
Dynamic loading offers two advantages: the startup performance of the application, and the
capability of the application to branch and load different modules if required (i.e. load two
versions of the same DLL).
2.2.1 Loading the iNEMO_SDK.DLL
The following code is an example of a Win32 Application project that calls the exported DLL
function in the iNEMO_SDK.dll file using dynamic loading:
// iNEMO_Cpp_Wrapper.cpp
//
...
6/15Doc ID 17013 Rev 1
AN3144Application code
HINSTANCE hinstDLL;
BOOL fFreeDLL;
...
hinstDLL = LoadLibrary("iNEMO_SDK.dll");
if (hinstDLL != NULL)
{
// Manage the functions here
...
fFreeDLL = FreeLibrary(hinstDLL);
}
...
When you compile and link the iNEMO_Cpp_Wrapper application, the Windows
®
operating
system searches for the iNEMO_SDK DLL in the following locations, in the following order:
1.Application folder
2. The current folder
3. Windows system folder
4. Windows folder
The FreeLibrary instruction frees the loaded dynamic-link library (DLL) module, the module
is unloaded from the address space of the calling process, and the handle is no longer valid.
If the function succeeds, the fFreeDLL return value is non-zero. Otherwise, if the function
fails, the return value is zero. To obtain extended error information, call the GetLastError
function.
Note:1To return the iNEMO board to its initial state, unplug and plug in the board again.
2The iNEMO_SDK.DLL is dependent on the PL_001.DLL, therefore this DLL must be in your
search path.
2.2.2 How to use the DLL functions
The iNEMO_SDK DLL exports the following functions:
The following subsections describe the relative software code for each function available
within the iNEMO software development kit.
Users can manage errors by adding the variable below. For each function, an error code is
returned if an error is present, while a “0” is returned if there is no error.
...
int iNEMO_Return;
...
2.3.1 Connecting the PC to iNEMO
The following code opens the Virtual COM port assigned by the Windows operating system:
The variable iNEMO_Return returns a “0” if there is no error, otherwise it returns an error
code. Moreover, nFreq is the acquisition frequency (Hz) and nSamples is the number of
samples to receive. If the latter is “0”, the iNEMO board sends data in infinite loop mode until
an INEMO_SDK_Stop command is received. Otherwise the board sends only the finite
number of samples indicated in nSamples parameter.
The nFreq parameter accepts the values 1, 10, 25 or 50, while the nSamples parameter
accepts values in the range of 0 - 65535. As described above, this parameter is also used to
choose the acquisition mode.
The variable iNEMO_Return returns a “0” if there is no error, otherwise it returns an error
code.
2.3.7 Getting a data sample
As described above, the INEMO_SDK_Start instruction starts an internal process of the
Dynamic Link Library which collects the data from the iNEMO board in a FIFO (first in, first
out) list. To obtain the data from this list, the following instructions must be used:
where frame is a C-language structure organized as follows:
typedef struct
{
SampleTimeStamp // timestamp (milliseconds from start)
Accelerometer // Accelerometer component (X, Y, Z) in mg unit
Gyroscope // Gyroscope component (X, Y1, Y2, Z) in dps unit
Magnetometer // Magnetometer component (X, Y, Z) in mGa unit
Pressure // Pressure value in 1/10 of mbar unit
Doc ID 17013 Rev 111/15
Application codeAN3144
Temperature // Temperature value in 1/10 of ºC unit
}
Note:The FIFO filling is dictated by the frequency indicated with the nFreq parameter, since the
use of the INEMO_SDK_GetSample instruction must correspond with this frequency.
Otherwise, a faster INEMO_SDK_GetSample returns an empty frame structure and an
excessively slow INEMO_SDK_GetSample returns values which are too old.
To verify the effectiveness of the frame structure, it is suggested to check the returned
iNEMO_Return value, which could be:
INEMO_SDK_ERROR_NONE = No error
INEMO_SDK_ERROR_STACK_EMPTY = The stack of data is empty
2.3.8 Getting the internal buffer usage of the FIFO
The following code checks the internal buffer usage:
The variable iNEMO_Return returns a “0” if there is no error, otherwise it returns an error
code. Meanwhile, the variable perc contains the actual percentage of internal buffer FIFO
usage.
12/15Doc ID 17013 Rev 1
AN3144References and related materials
3 References and related materials
The following additional information for the STEVAL-MKI062V1 is available at
●Application Note AN3138: Wrapper in MATLAB for the STEVAL-MKI062V1
Doc ID 17013 Rev 113/15
Revision historyAN3144
4 Revision history
Table 1.Document revision history
DateRevisionChanges
08-Jul-20101Initial release.
14/15Doc ID 17013 Rev 1
AN3144
Please Read Carefully:
Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its subsidiaries (“ST”) reserve the
right to make changes, corrections, modifications or improvements, to this document, and the products and services described herein at any
time, without notice.
All ST products are sold pursuant to ST’s terms and conditions of sale.
Purchasers are solely responsible for the choice, selection and use of the ST products and services described herein, and ST assumes no
liability whatsoever relating to the choice, selection or use of the ST products and services described herein.
No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. If any part of this
document refers to any third party products or services it shall not be deemed a license grant by ST for the use of such third party products
or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of such
third party products or services or any intellectual property contained therein.
UNLESS OTHERWISE SET FORTH IN ST’S TERMS AND CONDITIONS OF SALE ST DISCLAIMS ANY EXPRESS OR IMPLIED
WARRANTY WITH RESPECT TO THE USE AND/OR SALE OF ST PRODUCTS INCLUDING WITHOUT LIMITATION IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWS
OF ANY JURISDICTION), OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.
UNLESS EXPRESSLY APPROVED IN WRITING BY AN AUTHORIZED ST REPRESENTATIVE, ST PRODUCTS ARE NOT
RECOMMENDED, AUTHORIZED OR WARRANTED FOR USE IN MILITARY, AIR CRAFT, SPACE, LIFE SAVING, OR LIFE SUSTAINING
APPLICATIONS, NOR IN PRODUCTS OR SYSTEMS WHERE FAILURE OR MALFUNCTION MAY RESULT IN PERSONAL INJURY,
DEATH, OR SEVERE PROPERTY OR ENVIRONMENTAL DAMAGE. ST PRODUCTS WHICH ARE NOT SPECIFIED AS "AUTOMOTIVE
GRADE" MAY ONLY BE USED IN AUTOMOTIVE APPLICATIONS AT USER’S OWN RISK.
Resale of ST products with provisions different from the statements and/or technical features set forth in this document shall immediately void
any warranty granted by ST for the ST product or service described herein and shall not create or extend in any manner whatsoever, any
liability of ST.
ST and the ST logo are trademarks or registered trademarks of ST in various countries.
Information in this document supersedes and replaces all information previously supplied.
The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners.