Updated *-sensor.c files from the sky platform.

Fixed battery-sensor plus added simple test (XXX)
This commit is contained in:
Enric M. Calvo 2011-02-18 16:12:56 +01:00
parent e93f84bd4d
commit 659f36eb79
9 changed files with 340 additions and 50 deletions

View File

@ -2,9 +2,9 @@ ifndef TARGET
TARGET=z1
endif
CONTIKI_PROJECT = test-phidgets blink test-adxl345 tmp102-test
CONTIKI_PROJECT = test-phidgets blink test-adxl345 tmp102-test test-battery
CONTIKI_SOURCEFILES += cc2420-arch.c
PROJECT_SOURCEFILES = i2cmaster.c tmp102.c adxl345.c
PROJECT_SOURCEFILES = i2cmaster.c tmp102.c adxl345.c battery-sensor.c sky-sensors.c
APPS=serial-shell

1
examples/z1/TODO Normal file
View File

@ -0,0 +1 @@
+ clean-up test-battery.c

View File

@ -0,0 +1,32 @@
#include "contiki.h"
#include "dev/battery-sensor.h"
#include <stdio.h> /* For printf() */
/*---------------------------------------------------------------------------*/
PROCESS(aplicacio, "Aplicacio de prova");
AUTOSTART_PROCESSES(&aplicacio);
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(aplicacio, ev, data)
{
PROCESS_BEGIN();
SENSORS_ACTIVATE(battery_sensor);
while (1)
{
uint16_t bateria = battery_sensor.value(0);
printf("Battery: %i\n", bateria);
}
SENSORS_DEACTIVATE(battery_sensor);
PROCESS_END();
}
/*---------------------------------------------------------------------------*/

View File

@ -44,24 +44,27 @@
#include "dev/button-sensor.h"
#include "dev/leds.h"
#include "dev/z1-phidgets.h"
#include <stdio.h>
/*---------------------------------------------------------------------------*/
PROCESS(test_button_process, "Test button");
PROCESS(test_button_process, "Test Button & Phidgets");
AUTOSTART_PROCESSES(&test_button_process);
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(test_button_process, ev, data)
{
static struct etimer et;
//static struct etimer et;
PROCESS_BEGIN();
SENSORS_ACTIVATE(phidgets);
SENSORS_ACTIVATE(button_sensor);
while(1) {
etimer_set(&et, CLOCK_SECOND/2);
//etimer_set(&et, CLOCK_SECOND/2);
printf("Please press the User Button\n");
PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event &&
data == &button_sensor);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
//PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
leds_toggle(LEDS_GREEN);
printf("Button clicked\n");
//printf("Button clicked\n");
printf("Phidget 5V 1:%d\n", phidgets.value(PHIDGET5V_1));
printf("Phidget 5V 2:%d\n", phidgets.value(PHIDGET5V_2));
printf("Phidget 3V 1:%d\n", phidgets.value(PHIDGET3V_1));

View File

@ -26,72 +26,44 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: battery-sensor.c,v 1.1 2010/08/24 16:26:38 joxe Exp $
* $Id: battery-sensor.c,v 1.11 2010/08/25 19:30:52 nifi Exp $
*
* -----------------------------------------------------------------
*
* Author : Adam Dunkels, Joakim Eriksson, Niclas Finne
* Created : 2005-11-01
* Updated : $Date: 2010/08/24 16:26:38 $
* $Revision: 1.1 $
* Updated : $Date: 2010/08/25 19:30:52 $
* $Revision: 1.11 $
*/
#include "dev/battery-sensor.h"
#include "dev/sky-sensors.h"
#include <io.h>
/* Configure ADC12_2 to sample channel 11 (voltage) and use */
/* the Vref+ as reference (SREF_1) since it is a stable reference */
#define INPUT_CHANNEL (1 << INCH_11)
#define INPUT_REFERENCE SREF_1
#define BATTERY_MEM ADC12MEM11
const struct sensors_sensor battery_sensor;
static uint8_t active;
/*---------------------------------------------------------------------------*/
static void
activate(void)
{
/* Configure ADC12_2 to sample channel 11 (voltage) and use */
/* the Vref+ as reference (SREF_1) since it is a stable reference */
ADC12MCTL2 = (INCH_11 + SREF_1);
sky_sensors_activate(0x80);
active = 1;
}
/*---------------------------------------------------------------------------*/
static void
deactivate(void)
{
sky_sensors_deactivate(0x80);
active = 0;
}
/*---------------------------------------------------------------------------*/
static int
value(int type)
{
return ADC12MEM2/*battery_value*/;
return BATTERY_MEM;
}
/*---------------------------------------------------------------------------*/
static int
configure(int type, int c)
{
switch(type) {
case SENSORS_ACTIVE:
if(c) {
activate();
} else {
deactivate();
}
}
return 0;
return sky_sensors_configure(INPUT_CHANNEL, INPUT_REFERENCE, type, c);
}
/*---------------------------------------------------------------------------*/
static int
status(int type)
{
switch(type) {
case SENSORS_ACTIVE:
case SENSORS_READY:
return active;
}
return 0;
return sky_sensors_status(INPUT_CHANNEL, type);
}
/*---------------------------------------------------------------------------*/
SENSORS_SENSOR(battery_sensor, BATTERY_SENSOR,
value, configure, status);
SENSORS_SENSOR(battery_sensor, BATTERY_SENSOR, value, configure, status);

View File

@ -28,7 +28,7 @@
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: radio-sensor.c,v 1.1 2010/08/24 16:26:38 joxe Exp $
* @(#)$Id: radio-sensor.c,v 1.7 2010/08/25 19:30:53 nifi Exp $
*/
#include "lib/sensors.h"
@ -36,6 +36,7 @@
#include "dev/radio-sensor.h"
const struct sensors_sensor radio_sensor;
static int active;
/*---------------------------------------------------------------------------*/
static int
@ -53,12 +54,21 @@ value(int type)
static int
configure(int type, int c)
{
if(type == SENSORS_ACTIVE) {
active = c;
return 1;
}
return 0;
}
/*---------------------------------------------------------------------------*/
static int
status(int type)
{
switch(type) {
case SENSORS_ACTIVE:
case SENSORS_READY:
return active;
}
return 0;
}
/*---------------------------------------------------------------------------*/

View File

@ -0,0 +1,154 @@
/*
* Copyright (c) 2010, Swedish Institute of Computer Science.
* 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. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``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 INSTITUTE OR CONTRIBUTORS 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 operating system.
*
* $Id: sky-sensors.c,v 1.3 2010/08/25 19:30:53 nifi Exp $
*
* -----------------------------------------------------------------
*
* Author : Joakim Eriksson
* Created : 2010-02-02
* Updated : $Date: 2010/08/25 19:30:53 $
* $Revision: 1.3 $
*/
#include "contiki.h"
#include "lib/sensors.h"
#include <io.h>
#define ADC12MCTL_NO(adcno) ((unsigned char *) ADC12MCTL0_)[adcno]
static uint16_t adc_on;
static uint16_t ready;
/*---------------------------------------------------------------------------*/
static CC_INLINE void
start(void)
{
uint16_t c, last;
/* Set up the ADC. */
P6DIR = 0xff;
P6OUT = 0x00;
/* Setup ADC12, ref., sampling time */
/* XXX Note according to the specification a minimum of 17 ms should
be allowed after turn on of the internal reference generator. */
ADC12CTL0 = REF2_5V + SHT0_6 + SHT1_6 + MSC + REFON;
/* Use sampling timer, repeat-sequence-of-channels */
ADC12CTL1 = SHP + CONSEQ_3;
last = 15;
for(c = 0; c < 16; c++) {
/* Clear all end-of-sequences */
ADC12MCTL_NO(c) &= ~EOS;
if(adc_on & (1 << c)) {
if(last == 15) {
/* Set new start of sequence to lowest active memory holder */
ADC12CTL1 |= (c * CSTARTADD_1);
}
last = c;
}
}
/* Set highest end-of-sequence. */
ADC12MCTL_NO(last) |= EOS;
ADC12CTL0 |= ADC12ON;
ADC12CTL0 |= ENC; /* enable conversion */
ADC12CTL0 |= ADC12SC; /* sample & convert */
}
/*---------------------------------------------------------------------------*/
static CC_INLINE void
stop(void)
{
/* stop converting immediately, turn off reference voltage, etc. */
ADC12CTL0 &= ~ENC;
/* need to remove CONSEQ_3 if not EOS is configured */
ADC12CTL1 &= ~CONSEQ_3;
/* wait for conversion to stop */
while(ADC12CTL1 & ADC12BUSY);
/* clear any pending interrupts */
ADC12IFG = 0;
}
/*---------------------------------------------------------------------------*/
int
sky_sensors_status(uint16_t input, int type)
{
if(type == SENSORS_ACTIVE) {
return (adc_on & input) == input;
}
if(type == SENSORS_READY) {
ready |= ADC12IFG & adc_on & input;
return (ready & adc_on & input) == input;
}
return 0;
}
/*---------------------------------------------------------------------------*/
int
sky_sensors_configure(uint16_t input, uint8_t ref, int type, int value)
{
uint16_t c;
if(type == SENSORS_ACTIVE) {
stop();
if(value) {
adc_on |= input;
P6SEL |= input & 0xff;
/* Set ADC config */
for(c = 0; c < 16; c++) {
if(input & (1 << c)) {
ADC12MCTL_NO(c) = (c * INCH_1) | ref;
}
}
} else {
adc_on &= ~input;
ready &= ~input;
P6SEL &= ~(input & 0xff);
}
if(adc_on == 0) {
P6DIR = 0x00;
P6SEL = 0x00;
/* Turn off ADC and internal reference generator */
ADC12CTL0 = 0;
ADC12CTL1 = 0;
} else {
start();
}
return 1;
}
return 0;
}
/*---------------------------------------------------------------------------*/

View File

@ -0,0 +1,47 @@
/*
* Copyright (c) 2010, Swedish Institute of Computer Science.
* 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. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``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 INSTITUTE OR CONTRIBUTORS 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 operating system.
* $Id: sky-sensors.h,v 1.2 2010/08/25 19:30:53 nifi Exp $
*
* -----------------------------------------------------------------
*
* Author : Joakim Eriksson
* Created : 2010-02-02
* Updated : $Date: 2010/08/25 19:30:53 $
* $Revision: 1.2 $
*/
#ifndef __SKY_SENSORS_H__
#define __SKY_SENSORS_H__
int sky_sensors_status(uint16_t input, int type);
int sky_sensors_configure(uint16_t input, uint8_t reference,
int type, int value);
#endif /* __SKY_SENSORS_H__ */

View File

@ -0,0 +1,71 @@
/*
* Copyright (c) 2010, Swedish Institute of Computer Science.
* 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. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``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 INSTITUTE OR CONTRIBUTORS 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.
*
* @(#)$Id: temperature-sensor.c,v 1.1 2010/08/25 19:34:06 nifi Exp $
*/
/**
* \file
* Sensor driver for reading the built-in temperature sensor in the CPU.
* \author
* Adam Dunkels <adam@sics.se>
* Joakim Eriksson <joakime@sics.se>
* Niclas Finne <nfi@sics.se>
*/
#include "dev/temperature-sensor.h"
#include "dev/sky-sensors.h"
#include <io.h>
#define INPUT_CHANNEL (1 << INCH_10)
#define INPUT_REFERENCE SREF_1
#define TEMPERATURE_MEM ADC12MEM10
const struct sensors_sensor temperature_sensor;
/*---------------------------------------------------------------------------*/
static int
value(int type)
{
return TEMPERATURE_MEM;
}
/*---------------------------------------------------------------------------*/
static int
configure(int type, int c)
{
return sky_sensors_configure(INPUT_CHANNEL, INPUT_REFERENCE, type, c);
}
/*---------------------------------------------------------------------------*/
static int
status(int type)
{
return sky_sensors_status(INPUT_CHANNEL, type);
}
/*---------------------------------------------------------------------------*/
SENSORS_SENSOR(temperature_sensor, TEMPERATURE_SENSOR,
value, configure, status);