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