📄 smb380userdll.cpp
字号:
/* $Date: 2008/01/24 09:50:09 $
* $Revision: 1.1 $
*
*/
/*
* Copyright (C) 2007 Bosch Sensortec GmbH
*
* MODULE NAME
*
* Usage: DESCRIPTION
*
*
* Disclaimer
*
* Common:
* Bosch Sensortec products are developed for the consumer goods industry. They may only be used
* within the parameters of the respective valid product data sheet. Bosch Sensortec products are
* provided with the express understanding that there is no warranty of fitness for a particular purpose.
* They are not fit for use in life-sustaining, safety or security sensitive systems or any system or device
* that may lead to bodily harm or property damage if the system or device malfunctions. In addition,
* Bosch Sensortec products are not fit for use in products which interact with motor vehicle systems.
* The resale and/or use of products are at the purchaser's own risk and his own responsibility. The
* examination of fitness for the intended use is the sole responsibility of the Purchaser.
*
* The purchaser shall indemnify Bosch Sensortec from all third party claims, including any claims for
* incidental, or consequential damages, arising from any product use not covered by the parameters of
* the respective valid product data sheet or not approved by Bosch Sensortec and reimburse Bosch
* Sensortec for all costs in connection with such claims.
*
* The purchaser must monitor the market for the purchased products, particularly with regard to
* product safety and inform Bosch Sensortec without delay of all security relevant incidents.
*
* Engineering Samples are marked with an asterisk (*) or (e). Samples may vary from the valid
* technical specifications of the product series. They are therefore not intended or fit for resale to third
* parties or for use in end products. Their sole purpose is internal client testing. The testing of an
* engineering sample may in no way replace the testing of a product series. Bosch Sensortec
* assumes no liability for the use of engineering samples. By accepting the engineering samples, the
* Purchaser agrees to indemnify Bosch Sensortec from all claims arising from the use of engineering
* samples.
*
* Special:
* This software module (hereinafter called "Software") and any information on application-sheets
* (hereinafter called "Information") is provided free of charge for the sole purpose to support your
* application work. The Software and Information is subject to the following terms and conditions:
*
* The Software is specifically designed for the exclusive use for Bosch Sensortec products by
* personnel who have special experience and training. Do not use this Software if you do not have the
* proper experience or training.
*
* This Software package is provided `` as is `` and without any expressed or implied warranties,
* including without limitation, the implied warranties of merchantability and fitness for a particular
* purpose.
*
* Bosch Sensortec and their representatives and agents deny any liability for the functional impairment
* of this Software in terms of fitness, performance and safety. Bosch Sensortec and their
* representatives and agents shall not be liable for any direct or indirect damages or injury, except as
* otherwise stipulated in mandatory applicable law.
*
* The Information provided is believed to be accurate and reliable. Bosch Sensortec assumes no
* responsibility for the consequences of use of such Information nor for any infringement of patents or
* other rights of third parties which may result from its use. No license is granted by implication or
* otherwise under any patent or patent rights of Bosch. Specifications mentioned in the Information are
* subject to change without notice.
*
* It is not allowed to deliver the source code of the Software to any third party without permission of
* Bosch Sensortec.
*/
/*! \file SMB380UserDll.cpp
\brief Defines the entry point for the DLL application.
*Details : SMB380UserDll.cpp supplies user level APIs to native user application to let it can leverage from native
*smb380 driver's functions.
* The SMB380 API enables quick access to Bosch Sensortec's 3-Axis digital accelerometer.
* The only mandatory steps are:
* calling the smb380_init() routine, which initializes all necessary data structures for using all functions
* call the smb380_deinit() routine before application exit, which deinitializes and frees all allocated resources.
*/
#include "stdafx.h"
#include "memory.h"
#include "windows.h"
#include "smb380_ioctl.h"
#include "smb380.h"
#undef DEBUG
#undef DEBUGMSG
#define DEBUGMSG
HANDLE smb380handle = NULL;
HANDLE smb380driverhandle = NULL;
/** API Initialization routine
\return result 0 -> success; -1 -> fail;
*/
int smb380_init()
{
BOOL fRet = TRUE;
DEBUGMSG(1,(_T("smb380_init In>>>---\r\n")));
/* smb380driverhandle = ActivateDeviceEx( SMB_REG_PATH, NULL, 0, NULL);
if ( NULL == smb380driverhandle )
{
printf("Can not load driver in registry %s driver\r\n", SMB_REG_PATH);
return -1;
} */
DEBUGMSG(1,(_T("smb380_open In>>>---\r\n")));
smb380handle = NULL;
smb380handle=CreateFile(_T("SMB1:"),GENERIC_READ|GENERIC_WRITE,0,
NULL,OPEN_EXISTING,0,NULL);
if (INVALID_HANDLE_VALUE == smb380handle){
DEBUGMSG(1, (TEXT("Open SMB380 device failed!")));
smb380handle = NULL;
return -1;
}
DEBUGMSG(1,(_T("smb380_open Out<<<+++\r\n")));
if(smb380handle == NULL){
DEBUGMSG(1,(_T("Should do smb380_open first to get device handle\r\n")));
return -1;
}
fRet = DeviceIoControl( smb380handle,
IOCTL_SMB380_INIT,
NULL,
0,
NULL,
0,
NULL,
NULL );
//
// if the operation failed, log an error condition
//
if( fRet == FALSE )
{
DEBUGMSG(1,(_T("smb380_init failed!\r\n")));
return -1;
}
DEBUGMSG(1,(_T("smb380_init Out<<<+++\r\n")));
return 0;
}
/** Perform soft reset of SMB380 via bus command
\return result 0 -> success; -1 -> fail;
*/
int smb380_soft_reset()
{
BOOL fRet = TRUE;
DEBUGMSG(1,(_T("smb380_soft_reset In>>>---\r\n")));
if(smb380handle == NULL){
DEBUGMSG(1,(_T("Should do smb380_open first to get device handle\r\n")));
return -1;
}
fRet = DeviceIoControl( smb380handle,
IOCTL_SMB380_SOFT_RESET,
NULL,
0,
NULL,
0,
NULL,
NULL );
//
// if the operation failed, log an error condition
//
if( fRet == FALSE )
{
DEBUGMSG(1,(_T("smb380_soft_reset failed!\r\n")));
return -1;
}
DEBUGMSG(1,(_T("smb380_soft_reset Out<<<+++\r\n")));
return 0;
}
/** call SMB380s update image function
\return result 0 -> success; -1 -> fail;
*/
int smb380_update_image()
{
BOOL fRet = TRUE;
DEBUGMSG(1,(_T("smb380_update_image In>>>---\r\n")));
if(smb380handle == NULL){
DEBUGMSG(1,(_T("Should do smb380_open first to get device handle\r\n")));
return -1;
}
fRet = DeviceIoControl( smb380handle,
IOCTL_SMB380_UPDATE_IMAGE,
NULL,
0,
NULL,
0,
NULL,
NULL );
//
// if the operation failed, log an error condition
//
if( fRet == FALSE )
{
DEBUGMSG(1,(_T("smb380_update_image failed!\r\n")));
return -1;
}
DEBUGMSG(1,(_T("smb380_update_image Out<<<+++\r\n")));
return 0;
}
/** copy image from image structure to SMB380 image memory
\param smb380Image Pointer to smb380regs_t
\return result 0 -> success; -1 -> fail;
*/
int smb380_set_image (smb380regs_t *smb380Image)
{
BOOL fRet = TRUE;
DEBUGMSG(1,(_T("smb380_set_image In>>>---\r\n")));
if(smb380handle == NULL){
DEBUGMSG(1,(_T("Should do smb380_open first to get device handle\r\n")));
return -1;
}
fRet = DeviceIoControl( smb380handle,
IOCTL_SMB380_SET_IMAGE,
smb380Image,
sizeof(smb380regs_t),
NULL,
0,
NULL,
NULL );
//
// if the operation failed, log an error condition
//
if( fRet == FALSE )
{
DEBUGMSG(1,(_T("smb380_set_image failed!\r\n")));
return -1;
}
DEBUGMSG(1,(_T("smb380_set_image Out<<<+++\r\n")));
return 0;
}
/** read out image from SMB380 and store it to smb380regs_t structure
\param smb380Image pointer to smb380regs_t
\return result 0 -> success; -1 -> fail;
*/
int smb380_get_image(smb380regs_t *smb380Image)
{
BOOL fRet = TRUE;
DEBUGMSG(1,(_T("smb380_get_image In>>>---\r\n")));
if(smb380handle == NULL){
DEBUGMSG(1,(_T("Should do smb380_open first to get device handle\r\n")));
return -1;
}
fRet = DeviceIoControl( smb380handle,
IOCTL_SMB380_GET_IMAGE,
NULL,
0,
smb380Image,
sizeof(smb380regs_t),
NULL,
NULL );
//
// if the operation failed, log an error condition
//
if(fRet == FALSE )
{
DEBUGMSG(1,(_T("smb380_get_image failed!\r\n")));
return -1;
}
DEBUGMSG(1,(_T("smb380_get_image Out<<<+++\r\n")));
return 0;
}
/** read out offset data from
\param xyzt select axis x=0, y=1, z=2, t=3
\param *offset pointer to offset value (offset is in offset binary representation
\return result 0 -> success; -1 -> fail;
*/
int smb380_get_offset(unsigned char xyzt, unsigned short *offset)
{
BOOL fRet = TRUE;
DEBUGMSG(1,(_T("smb380_get_offset In>>>---\r\n")));
if(smb380handle == NULL){
DEBUGMSG(1,(_T("Should do smb380_open first to get device handle\r\n")));
return -1;
}
fRet = DeviceIoControl( smb380handle,
IOCTL_SMB380_GET_OFFSET,
&xyzt,
sizeof(unsigned char),
offset,
sizeof(unsigned short),
NULL,
NULL );
//
// if the operation failed, log an error condition
//
if(fRet == FALSE )
{
DEBUGMSG(1,(_T("smb380_get_offset failed!\r\n")));
return -1;
}
DEBUGMSG(1,(_T("smb380_get_offset Out<<<+++\r\n")));
return 0;
}
/** write offset data to SMB380 image
\param xyz select axis x=0, y=1, z=2, t=3
\param offset value to write (offset is in offset binary representation
\return result 0 -> success; -1 -> fail;
*/
int smb380_set_offset(unsigned char xyzt, unsigned short offset)
{
BOOL fRet = TRUE;
set_offset tmp;
DEBUGMSG(1,(_T("smb380_set_offset In>>>---\r\n")));
if(smb380handle == NULL){
DEBUGMSG(1,(_T("Should do smb380_open first to get device handle\r\n")));
return -1;
}
tmp.xyzt = xyzt;
tmp.offset = offset;
fRet = DeviceIoControl( smb380handle,
IOCTL_SMB380_SET_OFFSET,
&tmp,
sizeof(set_offset),
NULL,
0,
NULL,
NULL );
//
// if the operation failed, log an error condition
//
if(fRet == FALSE )
{
DEBUGMSG(1,(_T("smb380_set_offset failed!\r\n")));
return -1;
}
DEBUGMSG(1,(_T("smb380_set_offset Out<<<+++\r\n")));
return 0;
}
/** write offset data to SMB380 image
\param xyz select axis x=0, y=1, z=2, t=3
\param offset value to write to eeprom(offset is in offset binary representation
\return result 0 -> success; -1 -> fail;
\note use smb380_set_ee_w() function to enable access to offset registers in EEPROM space
*/
int smb380_set_offset_eeprom(unsigned char xyzt, unsigned short offset)
{
BOOL fRet = TRUE;
set_offset tmp;
DEBUGMSG(1,(_T("smb380_set_offset_eeprom In>>>---\r\n")));
if(smb380handle == NULL){
DEBUGMSG(1,(_T("Should do smb380_open first to get device handle\r\n")));
return -1;
}
tmp.xyzt = xyzt;
tmp.offset = offset;
fRet = DeviceIoControl( smb380handle,
IOCTL_SMB380_SET_OFFSET_EEPROM,
&tmp,
sizeof(set_offset),
NULL,
0,
NULL,
NULL );
//
// if the operation failed, log an error condition
//
if(fRet == FALSE )
{
DEBUGMSG(1,(_T("smb380_set_offset_eeprom failed!\r\n")));
return -1;
}
DEBUGMSG(1,(_T("smb380_set_offset_eeprom Out<<<+++\r\n")));
return 0;
}
/** write offset data to SMB380 image
\param eew 0 = lock EEPROM 1 = unlock EEPROM
\return result 0 -> success; -1 -> fail;
*/
int smb380_set_ee_w(unsigned char eew)
{
BOOL fRet = TRUE;
DEBUGMSG(1,(_T("smb380_set_ee_w In>>>---\r\n")));
if(smb380handle == NULL){
DEBUGMSG(1,(_T("Should do smb380_open first to get device handle\r\n")));
return -1;
}
fRet = DeviceIoControl( smb380handle,
IOCTL_SMB380_SET_EE_W,
&eew,
sizeof(unsigned char),
NULL,
0,
NULL,
NULL );
//
// if the operation failed, log an error condition
//
if(fRet == FALSE )
{
DEBUGMSG(1,(_T("smb380_set_ee_w failed!\r\n")));
return -1;
}
DEBUGMSG(1,(_T("smb380_set_ee_w Out<<<+++\r\n")));
return 0;
}
/** write byte to SMB380 EEPROM
\param addr address to write to (image addresses are automatically extended to EEPROM space)
\param data byte content to write
\return result 0 -> success; -1 -> fail;
*/
int smb380_write_ee(unsigned char addr, unsigned char data)
{
BOOL fRet = TRUE;
write_ee tmp;
DEBUGMSG(1,(_T("smb380_write_ee In>>>---\r\n")));
if(smb380handle == NULL){
DEBUGMSG(1,(_T("Should do smb380_open first to get device handle\r\n")));
return -1;
}
tmp.addr = addr;
tmp.data = data;
fRet = DeviceIoControl( smb380handle,
IOCTL_SMB380_WRITE_EE,
&tmp,
sizeof(write_ee),
NULL,
0,
NULL,
NULL );
//
// if the operation failed, log an error condition
//
if(fRet == FALSE )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -