Contiki 2.5
mts300.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009, University of Colombo School of Computing
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 copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * @(#)$$
32  */
33 
34 /**
35  * \file
36  * Device drivers implementation for MTS300 sensor board.
37  * \author
38  * Kasun Hewage <kasun.ch@gmail.com>
39  */
40 
41 #include "mts300.h"
42 /*---------------------------------------------------------------------------*/
43 void
44 sounder_on()
45 {
46  SOUNDER_DDR |= SOUNDER_MASK;
47  SOUNDER_PORT &= ~SOUNDER_MASK;
48  SOUNDER_PORT |= SOUNDER_MASK;
49 }
50 /*---------------------------------------------------------------------------*/
51 void
52 sounder_off()
53 {
54  SOUNDER_PORT &= ~(SOUNDER_MASK);
55  SOUNDER_DDR &= ~(SOUNDER_MASK);
56 }
57 /*---------------------------------------------------------------------------*/
58 void
59 adc_init()
60 {
61  ADMUX = 0;
62  /* AVCC with external capacitor at AREF pin. */
63  //ADMUX |= _BV(REFS0)
64  /* Disable ADC interrupts. */
65  ADCSRA &= ~( _BV(ADIE) | _BV(ADIF) );
66  /* Set ADC prescaler to 64 and clear interrupt flag. */
67  ADCSRA |= _BV(ADPS2) | _BV(ADPS1) | _BV(ADIE);
68 
69 }
70 /*---------------------------------------------------------------------------*/
71 /* Poll based approach. The interrupt based adc is currently not used.
72  The ADC result is right adjusted. First 8 bits(from left) are in ADCL and
73  other two bits are in ADCH. See Atmega128 datasheet page 228. */
74 uint16_t
75 get_adc(int channel)
76 {
77  uint16_t reading;
78 
79  ADMUX |= (channel & 0x1F);
80 
81  /* Disable ADC interrupts. */
82  ADCSRA &= ~_BV(ADIE);
83  /* Clear previous interrupts. */
84  ADCSRA |= _BV(ADIF);
85  /* Enable ADC and start conversion. */
86  ADCSRA |= _BV(ADEN) | _BV(ADSC);
87  /* Wait until conversion is completed. */
88  while ( ADCSRA & _BV(ADSC) );
89  /* Get first 8 bits. */
90  reading = ADCL;
91  /* Get last two bits. */
92  reading |= (ADCH & 3) << 8;
93  /* Disable ADC. */
94  ADCSRA &= ~_BV(ADEN);
95  return reading;
96 }
97 /*---------------------------------------------------------------------------*/
98 uint16_t
99 get_light()
100 {
101  uint16_t reading;
102 
103  /* Enable light sensor. */
104  LIGHT_PORT |= LIGHT_PIN_MASK;
105  LIGHT_PORT_DDR |= LIGHT_PIN_MASK;
106  /* Disable temperature sensor. */
107  TEMP_PORT_DDR &= ~TEMP_PIN_MASK;
108  TEMP_PORT &= ~TEMP_PIN_MASK;
109  /* Read ADC. */
110  reading = get_adc(LIGHT_ADC_CHANNEL);
111  /* Disable light sensor. */
112  LIGHT_PORT &= ~LIGHT_PIN_MASK;
113  LIGHT_PORT_DDR &= ~LIGHT_PIN_MASK;
114  return reading;
115 }
116 /*---------------------------------------------------------------------------*/
117 uint16_t
118 get_temp()
119 {
120  uint16_t reading;
121 
122  /* Disable light sensor. */
123  LIGHT_PORT &= ~LIGHT_PIN_MASK;
124  LIGHT_PORT_DDR &= ~LIGHT_PIN_MASK;
125  /* Enable temperature sensor. */
126  TEMP_PORT_DDR |= TEMP_PIN_MASK;
127  TEMP_PORT |= TEMP_PIN_MASK;
128  /* Read ADC. */
129  reading = get_adc(TEMP_ADC_CHANNEL);
130  /* Disable temperature sensor. */
131  TEMP_PORT_DDR &= ~TEMP_PIN_MASK;
132  TEMP_PORT &= ~TEMP_PIN_MASK;
133 
134  return reading;
135 }
136 /*---------------------------------------------------------------------------*/
137 uint16_t
138 get_accx()
139 {
140  uint16_t reading;
141 
142  /* Enable accelerometer. */
143  ACCEL_PORT_DDR |= ACCEL_PIN_MASK;
144  ACCEL_PORT |= ACCEL_PIN_MASK;
145  /* Read ADC. */
146  reading = get_adc(ACCELX_ADC_CHANNEL);
147  /* Enable accelerometer. */
148  ACCEL_PORT_DDR &= ~ACCEL_PIN_MASK;
149  ACCEL_PORT &= ~ACCEL_PIN_MASK;
150 
151  return reading;
152 }
153 /*---------------------------------------------------------------------------*/
154 uint16_t
155 get_accy()
156 {
157  uint16_t reading;
158 
159  /* Enable accelerometer. */
160  ACCEL_PORT_DDR |= ACCEL_PIN_MASK;
161  ACCEL_PORT |= ACCEL_PIN_MASK;
162  /* Read ADC. */
163  reading = get_adc(ACCELY_ADC_CHANNEL);
164  /* Enable accelerometer. */
165  ACCEL_PORT_DDR &= ~ACCEL_PIN_MASK;
166  ACCEL_PORT &= ~ACCEL_PIN_MASK;
167 
168  return reading;
169 }
170 /*---------------------------------------------------------------------------*/
171 uint16_t
172 get_magx()
173 {
174  uint16_t reading;
175 
176  /* Enable magnetometer. */
177  MAGNET_PORT_DDR |= MAGNET_PIN_MASK;
178  MAGNET_PORT |= MAGNET_PIN_MASK;
179  /* Read ADC. */
180  reading = get_adc(MAGNETX_ADC_CHANNEL);
181  /* Enable magnetometer. */
182  MAGNET_PORT_DDR &= ~MAGNET_PIN_MASK;
183  MAGNET_PORT &= ~MAGNET_PIN_MASK;
184 
185  return reading;
186 }
187 /*---------------------------------------------------------------------------*/
188 uint16_t
189 get_magy()
190 {
191  uint16_t reading;
192 
193  /* Enable magnetometer. */
194  MAGNET_PORT_DDR |= MAGNET_PIN_MASK;
195  MAGNET_PORT |= MAGNET_PIN_MASK;
196  /* Read ADC. */
197  reading = get_adc(MAGNETY_ADC_CHANNEL);
198  /* Enable magnetometer. */
199  MAGNET_PORT_DDR &= ~MAGNET_PIN_MASK;
200  MAGNET_PORT &= ~MAGNET_PIN_MASK;
201 
202  return reading;
203 }
204 /*---------------------------------------------------------------------------*/
205 uint16_t
206 get_mic()
207 {
208  uint16_t reading;
209 
210  /* Enable mic. */
211  MIC_PORT_DDR |= MIC_PIN_MASK;
212  MIC_PORT |= MIC_PIN_MASK;
213  /* Read ADC. */
214  reading = get_adc(MIC_ADC_CHANNEL);
215  /* Enable mic. */
216  MIC_PORT_DDR &= ~MIC_PIN_MASK;
217  MIC_PORT &= ~MIC_PIN_MASK;
218 
219  return reading;
220 }
221 /*---------------------------------------------------------------------------*/
222