2013-07-11 17:50:15 +02:00
|
|
|
/**
|
|
|
|
* \addtogroup mbxxx-platform
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
2011-04-01 10:22:29 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010, STMicroelectronics.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials provided
|
|
|
|
* with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
|
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
|
|
|
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki OS
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Accelerometer.
|
|
|
|
* \author
|
|
|
|
* Salvatore Pitrulli <salvopitru@users.sourceforge.net>
|
|
|
|
*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "dev/acc-sensor.h"
|
2012-04-09 21:49:53 +02:00
|
|
|
#include "sys/clock.h"
|
2011-04-01 10:22:29 +02:00
|
|
|
#include "mems.h"
|
|
|
|
|
|
|
|
#define FALSE 0
|
|
|
|
#define TRUE 1
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
active(void)
|
|
|
|
{
|
2013-07-11 17:50:15 +02:00
|
|
|
uint8_t reg;
|
2013-11-20 14:22:27 +01:00
|
|
|
if(!MEMS_Read_Reg (kLIS3L02DQ_SLAVE_ADDR, CTRL_REG1, ®, 1))
|
2011-04-01 10:22:29 +02:00
|
|
|
return FALSE;
|
2013-11-20 14:22:27 +01:00
|
|
|
|
2011-04-01 10:22:29 +02:00
|
|
|
return (reg & 0x40) ? TRUE : FALSE ;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
value(int type)
|
|
|
|
{
|
2013-07-11 17:50:15 +02:00
|
|
|
int8_t i2c_data = 0;
|
|
|
|
uint8_t reg_addr;
|
2013-11-20 14:22:27 +01:00
|
|
|
|
2011-04-01 10:22:29 +02:00
|
|
|
switch(type) {
|
|
|
|
case ACC_X_AXIS:
|
|
|
|
reg_addr = OUTX_H;
|
|
|
|
break;
|
2013-11-20 14:22:27 +01:00
|
|
|
|
2011-04-01 10:22:29 +02:00
|
|
|
case ACC_Y_AXIS:
|
|
|
|
reg_addr = OUTY_H;
|
|
|
|
break;
|
2013-11-20 14:22:27 +01:00
|
|
|
|
2011-04-01 10:22:29 +02:00
|
|
|
case ACC_Z_AXIS:
|
|
|
|
reg_addr = OUTZ_H;
|
|
|
|
break;
|
2013-11-20 14:22:27 +01:00
|
|
|
|
2011-04-01 10:22:29 +02:00
|
|
|
default:
|
2013-11-20 14:22:27 +01:00
|
|
|
return 0;
|
2011-04-01 10:22:29 +02:00
|
|
|
}
|
2013-11-20 14:22:27 +01:00
|
|
|
|
|
|
|
MEMS_Read_Reg(kLIS3L02DQ_SLAVE_ADDR, reg_addr, (uint8_t *)&i2c_data, 1);
|
|
|
|
|
2011-04-01 10:22:29 +02:00
|
|
|
if(MEMS_GetFullScale()==ACC_HIGH_RANGE){
|
2013-11-20 14:22:27 +01:00
|
|
|
return ((int16_t)i2c_data) * HIGH_RANGE_SENSITIVITY;
|
2011-04-01 10:22:29 +02:00
|
|
|
}
|
|
|
|
else {
|
2013-11-20 14:22:27 +01:00
|
|
|
return ((int16_t)i2c_data) * LOW_RANGE_SENSITIVITY;
|
2011-04-01 10:22:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
configure(int type, int value)
|
|
|
|
{
|
|
|
|
switch(type) {
|
2013-11-20 14:22:27 +01:00
|
|
|
|
2011-04-01 10:22:29 +02:00
|
|
|
case SENSORS_HW_INIT:
|
2013-11-20 14:22:27 +01:00
|
|
|
return MEMS_Init();
|
|
|
|
|
2011-04-01 10:22:29 +02:00
|
|
|
case SENSORS_ACTIVE:
|
|
|
|
if(value){
|
|
|
|
if(MEMS_On()){
|
|
|
|
clock_wait(8);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
2013-11-20 14:22:27 +01:00
|
|
|
return MEMS_Off();
|
|
|
|
|
2011-04-01 10:22:29 +02:00
|
|
|
case ACC_RANGE:
|
|
|
|
return MEMS_SetFullScale((boolean)value);
|
2013-11-20 14:22:27 +01:00
|
|
|
|
|
|
|
case ACC_HPF:
|
2011-04-01 10:22:29 +02:00
|
|
|
if(value < ACC_HPF_DISABLE){
|
2013-11-20 14:22:27 +01:00
|
|
|
return MEMS_Write_Reg(kLIS3L02DQ_SLAVE_ADDR, CTRL_REG2,
|
|
|
|
(1<<4) | (uint8_t)value);
|
2011-04-01 10:22:29 +02:00
|
|
|
}
|
|
|
|
else {
|
2013-11-20 14:22:27 +01:00
|
|
|
return MEMS_Write_Reg(kLIS3L02DQ_SLAVE_ADDR, CTRL_REG2, 0x00);
|
2011-04-01 10:22:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
status(int type)
|
|
|
|
{
|
|
|
|
switch(type) {
|
2013-11-20 14:22:27 +01:00
|
|
|
|
2011-04-01 10:22:29 +02:00
|
|
|
case SENSORS_READY:
|
|
|
|
return active();
|
|
|
|
}
|
2013-11-20 14:22:27 +01:00
|
|
|
|
2011-04-01 10:22:29 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-11-20 14:22:27 +01:00
|
|
|
SENSORS_SENSOR(acc_sensor, ACC_SENSOR, value, configure, status);
|
2011-04-01 10:22:29 +02:00
|
|
|
|
|
|
|
|
2013-07-11 17:50:15 +02:00
|
|
|
/** @} */
|