⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smb380.c

📁 smb380开发应用和程序(C语言) 包含smb380所有功能的设置函数
💻 C
📖 第 1 页 / 共 3 页
字号:
/*  $Date: 2007/08/07 16:05:10 $
 *  $Revision: 1.4 $
 *  
 */

/*
* Copyright (C) 2007 Bosch Sensortec GmbH
*
* SMB380 acceleration sensor API
* 
* Usage:	Application Programming Interface for SMB380 configuration and data read out
*
* Author:	Lars.Beseke@bosch-sensortec.com
*
* 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抯 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 smb380.c
    \brief This file contains all function implementations for the SMB380 API
    
    Details.
*/


#include "smb380.h"


smb380_t *p_smb380;				/**< pointer to SMB380 device structure  */


/** API Initialization routine
 \param *smb380 pointer to SMB380 structured type
 \return result of communication routines 
 */

int smb380_init(smb380_t *smb380) 
{
	int comres=0;
	unsigned char data;

	p_smb380 = smb380;																			/* assign smb380 ptr */
	p_smb380->dev_addr = SMB380_I2C_ADDR;										/* preset SM380 I2C_addr */
	comres += p_smb380->SMB380_BUS_READ_FUNC(p_smb380->dev_addr, CHIP_ID__REG, &data, 1);	/* read Chip Id */
	
	p_smb380->chip_id = SMB380_GET_BITSLICE(data, CHIP_ID);						/* get bitslice */
		
	comres += p_smb380->SMB380_BUS_READ_FUNC(p_smb380->dev_addr, ML_VERSION__REG, &data, 1); /* read Version reg */
	p_smb380->ml_version = SMB380_GET_BITSLICE(data, ML_VERSION);				/* get ML Version */
	p_smb380->al_version = SMB380_GET_BITSLICE(data, AL_VERSION);				/* get AL Version */

	return comres;

}

/** Perform soft reset of SMB380 via bus command
*/
int smb380_soft_reset() 
{
	int comres;
	unsigned char data=0;
	if (p_smb380==0) 
		return E_SMB_NULL_PTR;
	data = SMB380_SET_BITSLICE(data, SOFT_RESET, 1);
  comres = p_smb380->SMB380_BUS_WRITE_FUNC(p_smb380->dev_addr, SOFT_RESET__REG, &data,1); 
	return comres;
}


/** call SMB380s update image function
	\return bus communication result
*/
int smb380_update_image() 
{
	int comres;
	unsigned char data=0;
	if (p_smb380==0) 
		return E_SMB_NULL_PTR;
	data = SMB380_SET_BITSLICE(data, UPDATE_IMAGE, 1);
    comres = p_smb380->SMB380_BUS_WRITE_FUNC(p_smb380->dev_addr, UPDATE_IMAGE__REG, &data,1); 
	return comres;
}


/** copy image from image structure to SMB380 image memory
   \param smb380Image Pointer to smb380regs_t
   \return result of bus communication function
*/
int smb380_set_image (smb380regs_t *smb380Image) 
{
	int comres;
	unsigned char data;
	if (p_smb380==0)
		return E_SMB_NULL_PTR;
    comres = p_smb380->SMB380_BUS_READ_FUNC(p_smb380->dev_addr, EE_W__REG,&data, 1);
	data = SMB380_SET_BITSLICE(data, EE_W, SMB380_EE_W_ON);
	comres = p_smb380->SMB380_BUS_WRITE_FUNC(p_smb380->dev_addr, EE_W__REG, &data, 1);
	comres = p_smb380->SMB380_BUS_WRITE_FUNC(p_smb380->dev_addr, SMB380_IMAGE_BASE, (unsigned char*)smb380Image, SMB380_IMAGE_LEN);
	comres = p_smb380->SMB380_BUS_READ_FUNC(p_smb380->dev_addr, EE_W__REG,&data, 1);
	data = SMB380_SET_BITSLICE(data, EE_W, SMB380_EE_W_OFF);
	comres = p_smb380->SMB380_BUS_WRITE_FUNC(p_smb380->dev_addr, EE_W__REG, &data, 1);
	return comres;
}



/** read out image from SMB380 and store it to smb380regs_t structure
   \param smb380Image pointer to smb380regs_t 
   \return result of bus communication function

*/
int smb380_get_image(smb380regs_t *smb380Image)
{

	int comres;
	unsigned char data;
	if (p_smb380==0)
		return E_SMB_NULL_PTR;
        comres = p_smb380->SMB380_BUS_READ_FUNC(p_smb380->dev_addr, EE_W__REG,&data, 1);
	data = SMB380_SET_BITSLICE(data, EE_W, SMB380_EE_W_ON);
	comres = p_smb380->SMB380_BUS_WRITE_FUNC(p_smb380->dev_addr, EE_W__REG, &data, 1);
	comres = p_smb380->SMB380_BUS_READ_FUNC(p_smb380->dev_addr, SMB380_IMAGE_BASE, (unsigned char *)smb380Image, SMB380_IMAGE_LEN);
	data = SMB380_SET_BITSLICE(data, EE_W, SMB380_EE_W_OFF);
	comres = p_smb380->SMB380_BUS_WRITE_FUNC(p_smb380->dev_addr, EE_W__REG, &data, 1);
	return comres;
}

/** read out offset data from 
   \param xyz select axis x=0, y=1, z=2
   \param *offset pointer to offset value (offset is in offset binary representation
   \return result of bus communication function
   \note use smb380_set_ee_w() function to enable access to offset registers 
*/
int smb380_get_offset(unsigned char xyz, unsigned short *offset) 
{

   int comres;
   unsigned char data;
   if (p_smb380==0)
   		return E_SMB_NULL_PTR;
   comres = p_smb380->SMB380_BUS_READ_FUNC(p_smb380->dev_addr, (OFFSET_X_LSB__REG+xyz), &data, 1);
   data = SMB380_GET_BITSLICE(data, OFFSET_X_LSB);
   *offset = data;
   comres += p_smb380->SMB380_BUS_READ_FUNC(p_smb380->dev_addr, (OFFSET_X_MSB__REG+xyz), &data, 1);
   *offset |= (data<<2);
   return comres;
}


/** write offset data to SMB380 image
   \param xyz select axis x=0, y=1, z=2
   \param offset value to write (offset is in offset binary representation
   \return result of bus communication function
   \note use smb380_set_ee_w() function to enable access to offset registers 
*/
int smb380_set_offset(unsigned char xyz, unsigned short offset) 
{

   int comres;
   unsigned char data;
   if (p_smb380==0)
   		return E_SMB_NULL_PTR;
   comres = p_smb380->SMB380_BUS_READ_FUNC(p_smb380->dev_addr, (OFFSET_X_LSB__REG+xyz), &data, 1);
   data = SMB380_SET_BITSLICE(data, OFFSET_X_LSB, offset);
   comres += p_smb380->SMB380_BUS_WRITE_FUNC(p_smb380->dev_addr, (OFFSET_X_LSB__REG+xyz), &data, 1);
   data = (offset&0x3ff)>>2;
   comres += p_smb380->SMB380_BUS_WRITE_FUNC(p_smb380->dev_addr, (OFFSET_X_MSB__REG+xyz), &data, 1);
   return comres;
}


/** write offset data to SMB380 image
   \param xyz select axis x=0, y=1, z=2
   \param offset value to write to eeprom(offset is in offset binary representation
   \return result of bus communication function
   \note use smb380_set_ee_w() function to enable access to offset registers in EEPROM space
*/
int smb380_set_offset_eeprom(unsigned char xyz, unsigned short offset) 
{

   int comres;
   unsigned char data;
   if (p_smb380==0)
   		return E_SMB_NULL_PTR;   
   comres = p_smb380->SMB380_BUS_READ_FUNC(p_smb380->dev_addr, (OFFSET_X_LSB__REG+xyz), &data, 1);
   data = SMB380_SET_BITSLICE(data, OFFSET_X_LSB, offset);
   comres += p_smb380->SMB380_BUS_WRITE_FUNC(p_smb380->dev_addr, (SMB380_EEP_OFFSET+OFFSET_X_LSB__REG + xyz), &data, 1);   
   p_smb380->delay_msec(34);
   data = (offset&0x3ff)>>2;
   comres += p_smb380->SMB380_BUS_WRITE_FUNC(p_smb380->dev_addr, (SMB380_EEP_OFFSET+ OFFSET_X_MSB__REG+xyz), &data, 1);
   p_smb380->delay_msec(34);
   return comres;
}




/** write offset data to SMB380 image
   \param eew 0 = lock EEPROM 1 = unlock EEPROM 
   \return result of bus communication function
*/
int smb380_set_ee_w(unsigned char eew)
{
    unsigned char data;
	int comres;
	if (p_smb380==0)
		return E_SMB_NULL_PTR;
    comres = p_smb380->SMB380_BUS_READ_FUNC(p_smb380->dev_addr, EE_W__REG,&data, 1);
	data = SMB380_SET_BITSLICE(data, EE_W, eew);
	comres = p_smb380->SMB380_BUS_WRITE_FUNC(p_smb380->dev_addr, EE_W__REG, &data, 1);
	return comres;
}



/** 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 of bus communication function
*/
int smb380_write_ee(unsigned char addr, unsigned char data) 
{	
	int comres;
	if (p_smb380==0) 			/* check pointers */
		return E_SMB_NULL_PTR;
    if (p_smb380->delay_msec == 0)
	    return E_SMB_NULL_PTR;
    comres = smb380_set_ee_w( SMB380_EE_W_ON );
	addr|=0x20;   /* add eeprom address offset to image address if not applied */
	comres += smb380_write_reg(addr, &data, 1 );
	p_smb380->delay_msec( SMB380_EE_W_DELAY );
	comres += smb380_set_ee_w( SMB380_EE_W_OFF);
	return comres;
}

/**	start SMB380s integrated selftest function
   \param st 1 = selftest0, 3 = selftest1 (see also)
 	 \return result of bus communication function
 	 
 	 \see SMB380_SELF_TEST0_ON
 	 \see SMB380_SELF_TEST1_ON
 
 */
int smb380_selftest(unsigned char st)
{
	int comres;
	unsigned char data;
	comres = p_smb380->SMB380_BUS_READ_FUNC(p_smb380->dev_addr, SELF_TEST__REG, &data, 1);
	data = SMB380_SET_BITSLICE(data, SELF_TEST, st);
	comres += p_smb380->SMB380_BUS_WRITE_FUNC(p_smb380->dev_addr, SELF_TEST__REG, &data, 1);  
	return comres;  

}



/**	set smb380s range 
 \param range 
 \return  result of bus communication function
 
 \see SMB380_RANGE_2G		
 \see SMB380_RANGE_4G			
 \see SMB380_RANGE_8G			
*/
int smb380_set_range(char range) 
{			
   int comres = 0;
   unsigned char data;

   if (p_smb380==0)
	    return E_SMB_NULL_PTR;

   if (range<3) {	
	 	comres = p_smb380->SMB380_BUS_READ_FUNC(p_smb380->dev_addr, RANGE__REG, &data, 1 );
	 	data = SMB380_SET_BITSLICE(data, RANGE, range);		  	
         comres += p_smb380->SMB380_BUS_WRITE_FUNC(p_smb380->dev_addr, RANGE__REG, &data, 1);
   }
   return comres;

}


/* readout select range from SMB380 
   \param *range pointer to range setting
   \return result of bus communication function
   \see SMB380_RANGE_2G, SMB380_RANGE_4G, SMB380_RANGE_8G		
   \see smb380_set_range()
*/
int smb380_get_range(unsigned char *range) 
{

	int comres = 0;
	

	if (p_smb380==0)
		return E_SMB_NULL_PTR;
	comres = p_smb380->SMB380_BUS_READ_FUNC(p_smb380->dev_addr, RANGE__REG, range, 1 );

	*range = SMB380_GET_BITSLICE(*range, RANGE);
	
	return comres;

}



/** set SMB380s operation mode
   \param mode 0 = normal, 2 = sleep, 3 = auto wake up
   \return result of bus communication function
   \note Available constants see below
   \see SMB380_MODE_NORMAL, SMB380_MODE_SLEEP, SMB380_MODE_WAKE_UP     
	 \see smb380_get_mode()
*/
int smb380_set_mode(unsigned char mode) {
	
	int comres=0;
	unsigned char data1, data2;

	if (p_smb380==0)
		return E_SMB_NULL_PTR;

	if (mode<4 || mode!=1) {
		comres = p_smb380->SMB380_BUS_READ_FUNC(p_smb380->dev_addr, WAKE_UP__REG, &data1, 1 );
		data1  = SMB380_SET_BITSLICE(data1, WAKE_UP, mode);		  
        comres += p_smb380->SMB380_BUS_READ_FUNC(p_smb380->dev_addr, SLEEP__REG, &data2, 1 );
		data2  = SMB380_SET_BITSLICE(data2, SLEEP, (mode>>1));
    	comres += p_smb380->SMB380_BUS_WRITE_FUNC(p_smb380->dev_addr, WAKE_UP__REG, &data1, 1);
	  	comres += p_smb380->SMB380_BUS_WRITE_FUNC(p_smb380->dev_addr, SLEEP__REG, &data2, 1);
	  	p_smb380->mode = mode;
	} 
	return comres;
	
}



/** get selected mode
   \return used mode
   \note this function returns the mode stored in \ref smb380_t structure
   \see SMB380_MODE_NORMAL, SMB380_MODE_SLEEP, SMB380_MODE_WAKE_UP
   \see smb380_set_mode()

*/
unsigned char smb380_get_mode(void) 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -