Contiki 3.x
acc-sensor.c
Go to the documentation of this file.
1 /**
2  * \addtogroup mbxxx-platform
3  *
4  * @{
5  */
6 /*
7  * Copyright (c) 2010, STMicroelectronics.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * 3. The name of the author may not be used to endorse or promote
20  * products derived from this software without specific prior
21  * written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
24  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
27  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  * This file is part of the Contiki OS
36  *
37  */
38 /*---------------------------------------------------------------------------*/
39 /**
40 * \file
41 * Accelerometer.
42 * \author
43 * Salvatore Pitrulli <salvopitru@users.sourceforge.net>
44 */
45 /*---------------------------------------------------------------------------*/
46 
47 
48 #include "dev/acc-sensor.h"
49 #include "sys/clock.h"
50 #include "mems.h"
51 
52 #define FALSE 0
53 #define TRUE 1
54 
55 /*---------------------------------------------------------------------------*/
56 static int
57 active(void)
58 {
59  uint8_t reg;
60  if(!MEMS_Read_Reg (kLIS3L02DQ_SLAVE_ADDR, CTRL_REG1, &reg, 1))
61  return FALSE;
62 
63  return (reg & 0x40) ? TRUE : FALSE ;
64 }
65 /*---------------------------------------------------------------------------*/
66 static int
67 value(int type)
68 {
69  int8_t i2c_data = 0;
70  uint8_t reg_addr;
71 
72  switch(type) {
73  case ACC_X_AXIS:
74  reg_addr = OUTX_H;
75  break;
76 
77  case ACC_Y_AXIS:
78  reg_addr = OUTY_H;
79  break;
80 
81  case ACC_Z_AXIS:
82  reg_addr = OUTZ_H;
83  break;
84 
85  default:
86  return 0;
87  }
88 
89  MEMS_Read_Reg(kLIS3L02DQ_SLAVE_ADDR, reg_addr, (uint8_t *)&i2c_data, 1);
90 
91  if(MEMS_GetFullScale()==ACC_HIGH_RANGE){
92  return ((int16_t)i2c_data) * HIGH_RANGE_SENSITIVITY;
93  }
94  else {
95  return ((int16_t)i2c_data) * LOW_RANGE_SENSITIVITY;
96  }
97 }
98 /*---------------------------------------------------------------------------*/
99 static int
100 configure(int type, int value)
101 {
102  switch(type) {
103 
104  case SENSORS_HW_INIT:
105  return MEMS_Init();
106 
107  case SENSORS_ACTIVE:
108  if(value){
109  if(MEMS_On()){
110  clock_wait(8);
111  return 1;
112  }
113  return 0;
114  }
115  else
116  return MEMS_Off();
117 
118  case ACC_RANGE:
119  return MEMS_SetFullScale((boolean)value);
120 
121  case ACC_HPF:
122  if(value < ACC_HPF_DISABLE){
123  return MEMS_Write_Reg(kLIS3L02DQ_SLAVE_ADDR, CTRL_REG2,
124  (1<<4) | (uint8_t)value);
125  }
126  else {
127  return MEMS_Write_Reg(kLIS3L02DQ_SLAVE_ADDR, CTRL_REG2, 0x00);
128  }
129  }
130  return 0;
131 }
132 /*---------------------------------------------------------------------------*/
133 static int
134 status(int type)
135 {
136  switch(type) {
137 
138  case SENSORS_READY:
139  return active();
140  }
141 
142  return 0;
143 }
144 /*---------------------------------------------------------------------------*/
145 SENSORS_SENSOR(acc_sensor, ACC_SENSOR, value, configure, status);
146 
147 
148 /** @} */
#define TRUE
An alias for one, used for clarity.
#define FALSE
An alias for zero, used for clarity.
void clock_wait(clock_time_t t)
Wait for a given number of ticks.
Definition: clock.c:166